Skip to content

Commit fd6f846

Browse files
authored
Merge pull request #702 from noesya/refactor_menus
Refactor menus
2 parents bc16d05 + 3197178 commit fd6f846

4 files changed

Lines changed: 37 additions & 166 deletions

File tree

app/models/communication/website/menu/item.rb

Lines changed: 11 additions & 34 deletions
Original file line numberDiff line numberDiff line change
@@ -36,7 +36,6 @@ class Communication::Website::Menu::Item < ApplicationRecord
3636
include Sanitizable
3737
include WithTree
3838
include WithPosition
39-
include WithTargets
4039

4140
attr_accessor :skip_publication_callback
4241

@@ -53,22 +52,11 @@ class Communication::Website::Menu::Item < ApplicationRecord
5352
blank: 0,
5453
url: 10,
5554
page: 20,
56-
programs: 30,
5755
program: 31,
58-
diplomas: 32,
5956
diploma: 33,
60-
posts: 40,
6157
category: 41,
6258
post: 42,
63-
organizations: 45,
64-
persons: 50,
65-
administrators: 51,
66-
authors: 52,
67-
researchers: 53,
68-
teachers: 54,
69-
volumes: 60,
7059
volume: 61,
71-
papers: 62,
7260
paper: 63
7361
}, _prefix: :kind
7462

@@ -79,25 +67,14 @@ class Communication::Website::Menu::Item < ApplicationRecord
7967

8068
def self.icon_for(kind)
8169
icons = {
82-
'administrators' => Icon::UNIVERSITY_PERSON_ADMINISTRATORS,
83-
'authors' => Icon::UNIVERSITY_PERSON,
8470
'blank' => 'font',
8571
'diploma' => Icon::EDUCATION_DIPLOMA,
86-
'diplomas' => Icon::EDUCATION_DIPLOMA,
87-
'posts' => Icon::COMMUNICATION_WEBSITE_POST,
8872
'post' => Icon::COMMUNICATION_WEBSITE_POST,
8973
'category' => Icon::COMMUNICATION_WEBSITE_POST,
9074
'page' => Icon::COMMUNICATION_WEBSITE_PAGE,
9175
'program' => Icon::EDUCATION_PROGRAM,
92-
'programs' => Icon::EDUCATION_PROGRAM,
9376
'paper' => Icon::RESEARCH_LABORATORY,
94-
'papers' => Icon::RESEARCH_LABORATORY,
95-
'volumes' => Icon::RESEARCH_LABORATORY,
9677
'volume' => Icon::RESEARCH_LABORATORY,
97-
'researchers' => Icon::RESEARCH_RESEARCHER,
98-
'organizations' => Icon::UNIVERSITY_ORGANIZATION,
99-
'persons' => Icon::UNIVERSITY_PERSON,
100-
'teachers' => Icon::EDUCATION_TEACHER,
10178
'url' => 'globe',
10279
}
10380
"fas fa-#{icons[kind]}" if icons.has_key? kind
@@ -108,17 +85,17 @@ def to_s
10885
end
10986

11087
def static_target
111-
target = nil
112-
active = website.send "menu_item_kind_#{kind}?"
113-
return nil unless active
114-
# Les méthodes target_for_ sont définies dans le concern WithTarget
115-
method = "target_for_#{kind}"
116-
# Le true sert à examiner les méthodes protected
117-
target = respond_to?(method, true) ? send(method)
118-
: about&.path
119-
return nil if target.nil?
120-
target.end_with?('/') ? target
121-
: "#{target}/"
88+
test_kind_method = "menu_item_kind_#{kind}?"
89+
return nil if website.respond_to?(test_kind_method) && !website.public_send(test_kind_method)
90+
91+
case kind
92+
when "blank"
93+
''
94+
when "url"
95+
url
96+
else
97+
about.new_permalink_in_website(website).computed_path
98+
end
12299
end
123100

124101
def list_of_other_items

app/models/communication/website/menu/item/with_targets.rb

Lines changed: 0 additions & 91 deletions
This file was deleted.

app/models/communication/website/with_menus.rb

Lines changed: 2 additions & 41 deletions
Original file line numberDiff line numberDiff line change
@@ -12,23 +12,11 @@ module Communication::Website::WithMenus
1212

1313
def menu_item_kinds
1414
Communication::Website::Menu::Item.kinds.reject do |key, value|
15-
active = send "menu_item_kind_#{key}?"
16-
!active
15+
method_name = "menu_item_kind_#{key}?"
16+
respond_to?(method_name) && !public_send(method_name)
1717
end
1818
end
1919

20-
def menu_item_kind_blank?
21-
true
22-
end
23-
24-
def menu_item_kind_url?
25-
true
26-
end
27-
28-
def menu_item_kind_page?
29-
pages.any?
30-
end
31-
3220
def menu_item_kind_programs?
3321
has_education_programs?
3422
end
@@ -45,37 +33,10 @@ def menu_item_kind_diplomas?
4533
has_education_diplomas?
4634
end
4735

48-
def menu_item_kind_posts?
49-
has_communication_posts?
50-
end
51-
52-
def menu_item_kind_category?
53-
has_communication_categories?
54-
end
55-
56-
def menu_item_kind_post?
57-
has_communication_posts?
58-
end
59-
60-
def menu_item_kind_organizations?
61-
# TODO: has_organization takes a looong time when having a lot of blocks.
62-
# when we have a direct relation between website & organizations re-adjust this test.
63-
# has_organizations?
64-
true
65-
end
66-
67-
def menu_item_kind_persons?
68-
has_persons?
69-
end
70-
7136
def menu_item_kind_administrators?
7237
has_administrators?
7338
end
7439

75-
def menu_item_kind_authors?
76-
has_authors?
77-
end
78-
7940
def menu_item_kind_researchers?
8041
has_researchers?
8142
end
Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
class SetAboutFromMenuItemKind < ActiveRecord::Migration[7.0]
2+
def change
3+
mapping = {
4+
'30' => Communication::Website::Page::EducationProgram,
5+
'32' => Communication::Website::Page::EducationDiploma,
6+
'40' => Communication::Website::Page::CommunicationPost,
7+
'45' => Communication::Website::Page::Organization,
8+
'50' => Communication::Website::Page::Person,
9+
'51' => Communication::Website::Page::Administrator,
10+
'52' => Communication::Website::Page::Author,
11+
'53' => Communication::Website::Page::Researcher,
12+
'54' => Communication::Website::Page::Teacher,
13+
'60' => Communication::Website::Page::ResearchVolume,
14+
'62' => Communication::Website::Page::ResearchPaper
15+
}
16+
17+
kinds = mapping.keys.map(&:to_i)
18+
Communication::Website::Menu::Item.includes(:website).where(kind: kinds).find_each do |menu_item|
19+
page_class = mapping[menu_item.kind_before_type_cast.to_s]
20+
about = menu_item.website.special_page(page_class)
21+
menu_item.update(about: about, kind: :page)
22+
end
23+
end
24+
end

0 commit comments

Comments
 (0)