diff --git a/app/components/concerns/sdg/tag_list.rb b/app/components/concerns/sdg/tag_list.rb index b936853d5..59ba6d4e3 100644 --- a/app/components/concerns/sdg/tag_list.rb +++ b/app/components/concerns/sdg/tag_list.rb @@ -1,36 +1,31 @@ module SDG::TagList extend ActiveSupport::Concern - attr_reader :record_or_name, :limit - delegate :link_list, to: :helpers + attr_reader :record, :limit - def initialize(record_or_name, limit: nil) - @record_or_name = record_or_name + def initialize(record, limit: nil) + @record = record @limit = limit end def render? - process.enabled? + SDG::ProcessEnabled.new(record).enabled? end - def see_more_link(association_name) + def tag_records + tags = record.send(association_name) + + if tags.respond_to?(:limit) + tags.order(:code).limit(limit) + else + tags.sort[0..(limit.to_i - 1)] + end + end + + def see_more_link render Shared::SeeMoreLinkComponent.new(record, association_name, limit: limit) end - def filter_text(goal_or_target) - t("sdg.#{i18n_namespace}.filter.link", - resources: model.model_name.human(count: :other), - code: goal_or_target.code) - end - - def index_by(advanced_search) - polymorphic_path(model, advanced_search: advanced_search) - end - - def process - @process ||= SDG::ProcessEnabled.new(record_or_name) - end - - def model - process.name.constantize + def association_name + raise NotImplementedError, "method must be implemented in the included class" end end diff --git a/app/components/sdg/filter_links_component.html.erb b/app/components/sdg/filter_links_component.html.erb new file mode 100644 index 000000000..874755b8a --- /dev/null +++ b/app/components/sdg/filter_links_component.html.erb @@ -0,0 +1 @@ +<%= link_list(*links, class: "sdg-#{parameter_name}-tag-list") %> diff --git a/app/components/sdg/filter_links_component.rb b/app/components/sdg/filter_links_component.rb new file mode 100644 index 000000000..80cc6b548 --- /dev/null +++ b/app/components/sdg/filter_links_component.rb @@ -0,0 +1,49 @@ +class SDG::FilterLinksComponent < ApplicationComponent + attr_reader :records, :related_model, :see_more_link + delegate :link_list, to: :helpers + + def initialize(records, related_model, see_more_link: nil) + @records = records + @related_model = related_model + @see_more_link = see_more_link + end + + def links + [*sdg_links, see_more_link] + end + + private + + def sdg_links + records.map do |goal_or_target| + [ + render(SDG::TagComponent.new(goal_or_target)), + index_by(parameter_name => goal_or_target.code), + title: filter_text(goal_or_target), + data: { code: goal_or_target.code } + ] + end + end + + def filter_text(goal_or_target) + t("sdg.#{i18n_namespace}.filter.link", + resources: related_model.model_name.human(count: :other), + code: goal_or_target.code) + end + + def index_by(advanced_search) + polymorphic_path(related_model, advanced_search: advanced_search) + end + + def i18n_namespace + parameter_name.pluralize + end + + def parameter_name + if records.first.is_a?(SDG::Goal) + "goal" + else + "target" + end + end +end diff --git a/app/components/sdg/goals/plain_tag_list_component.rb b/app/components/sdg/goals/plain_tag_list_component.rb index a99175dc8..5e8b4ff53 100644 --- a/app/components/sdg/goals/plain_tag_list_component.rb +++ b/app/components/sdg/goals/plain_tag_list_component.rb @@ -3,25 +3,17 @@ class SDG::Goals::PlainTagListComponent < ApplicationComponent private - def record - record_or_name - end - def tags - [*goal_tags, see_more_link(:sdg_goals)].select(&:present?) + [*goal_tags, see_more_link].select(&:present?) end def goal_tags - goals.order(:code).limit(limit).map do |goal| + tag_records.map do |goal| render SDG::TagComponent.new(goal) end end - def goals - record.sdg_goals - end - - def i18n_namespace - "goals" + def association_name + :sdg_goals end end diff --git a/app/components/sdg/goals/tag_cloud_component.html.erb b/app/components/sdg/goals/tag_cloud_component.html.erb index ebd572a6f..d44f9768f 100644 --- a/app/components/sdg/goals/tag_cloud_component.html.erb +++ b/app/components/sdg/goals/tag_cloud_component.html.erb @@ -1,5 +1,5 @@
- <%= render SDG::Goals::TagListComponent.new(class_name) %> + <%= render SDG::FilterLinksComponent.new(goals, class_name.constantize) %>
diff --git a/app/components/sdg/goals/tag_cloud_component.rb b/app/components/sdg/goals/tag_cloud_component.rb index 36a202263..0ffac19b9 100644 --- a/app/components/sdg/goals/tag_cloud_component.rb +++ b/app/components/sdg/goals/tag_cloud_component.rb @@ -14,4 +14,8 @@ class SDG::Goals::TagCloudComponent < ApplicationComponent def heading t("sdg.goals.filter.heading") end + + def goals + SDG::Goal.order(:code) + end end diff --git a/app/components/sdg/goals/tag_list_component.html.erb b/app/components/sdg/goals/tag_list_component.html.erb index 344d54181..1ddd0dd2d 100644 --- a/app/components/sdg/goals/tag_list_component.html.erb +++ b/app/components/sdg/goals/tag_list_component.html.erb @@ -1 +1 @@ -<%= link_list(*links, class: "sdg-goal-tag-list") %> +<%= render SDG::FilterLinksComponent.new(tag_records, related_model, see_more_link: see_more_link) %> diff --git a/app/components/sdg/goals/tag_list_component.rb b/app/components/sdg/goals/tag_list_component.rb index 528102678..fb9c181e9 100644 --- a/app/components/sdg/goals/tag_list_component.rb +++ b/app/components/sdg/goals/tag_list_component.rb @@ -3,33 +3,11 @@ class SDG::Goals::TagListComponent < ApplicationComponent private - def record - record_or_name if record_or_name.respond_to?(:sdg_goals) + def association_name + :sdg_goals end - def links - [*goal_links, see_more_link(:sdg_goals)] - end - - def goal_links - goals.order(:code).limit(limit).map do |goal| - [ - render(SDG::TagComponent.new(goal)), - index_by_goal(goal), - title: filter_text(goal) - ] - end - end - - def goals - record&.sdg_goals || SDG::Goal.all - end - - def index_by_goal(goal) - index_by(goal: goal.code) - end - - def i18n_namespace - "goals" + def related_model + record.class end end diff --git a/app/components/sdg/targets/plain_tag_list_component.rb b/app/components/sdg/targets/plain_tag_list_component.rb index 25954544d..78ccc4b42 100644 --- a/app/components/sdg/targets/plain_tag_list_component.rb +++ b/app/components/sdg/targets/plain_tag_list_component.rb @@ -3,25 +3,17 @@ class SDG::Targets::PlainTagListComponent < ApplicationComponent private - def record - record_or_name - end - def tags - [*target_tags, see_more_link(:sdg_targets)].select(&:present?) + [*target_tags, see_more_link].select(&:present?) end def target_tags - targets.sort[0..(limit.to_i - 1)].map do |target| + tag_records.map do |target| tag.span(render(SDG::TagComponent.new(target)), data: { code: target.code }) end end - def targets - record.sdg_targets - end - - def i18n_namespace - "targets" + def association_name + :sdg_targets end end diff --git a/app/components/sdg/targets/tag_list_component.html.erb b/app/components/sdg/targets/tag_list_component.html.erb index 1c97ade36..1ddd0dd2d 100644 --- a/app/components/sdg/targets/tag_list_component.html.erb +++ b/app/components/sdg/targets/tag_list_component.html.erb @@ -1 +1 @@ -<%= link_list(*links, class: "sdg-target-tag-list") %> +<%= render SDG::FilterLinksComponent.new(tag_records, related_model, see_more_link: see_more_link) %> diff --git a/app/components/sdg/targets/tag_list_component.rb b/app/components/sdg/targets/tag_list_component.rb index 6c2ea07fe..2d8e1a92b 100644 --- a/app/components/sdg/targets/tag_list_component.rb +++ b/app/components/sdg/targets/tag_list_component.rb @@ -3,34 +3,11 @@ class SDG::Targets::TagListComponent < ApplicationComponent private - def record - record_or_name + def association_name + :sdg_targets end - def links - [*target_links, see_more_link(:sdg_targets)] - end - - def target_links - targets.sort[0..(limit.to_i - 1)].map do |target| - [ - render(SDG::TagComponent.new(target)), - index_by_target(target), - title: filter_text(target), - data: { code: target.code } - ] - end - end - - def targets - record.sdg_targets - end - - def index_by_target(target) - index_by(target: target.code) - end - - def i18n_namespace - "targets" + def related_model + record.class end end diff --git a/spec/components/sdg/goals/tag_cloud_component_spec.rb b/spec/components/sdg/goals/tag_cloud_component_spec.rb index 46f543987..6738ba0da 100644 --- a/spec/components/sdg/goals/tag_cloud_component_spec.rb +++ b/spec/components/sdg/goals/tag_cloud_component_spec.rb @@ -15,11 +15,13 @@ describe SDG::Goals::TagCloudComponent, type: :component do expect(page).to have_content "Filters by SDG" end - it "renders all goals" do + it "renders all goals ordered by code" do component = SDG::Goals::TagCloudComponent.new("Proposal") render_inline component - expect(page).to have_css ".sdg-goal-icon", count: 17 + expect(page).to have_selector ".sdg-goal-icon", count: 17 + expect(page.first("a")[:title]).to end_with "goal 1" + expect(page.all("a").last[:title]).to end_with "goal 17" end end diff --git a/spec/components/sdg/goals/tag_list_component_spec.rb b/spec/components/sdg/goals/tag_list_component_spec.rb index 6f1d3f12b..493dc9dbc 100644 --- a/spec/components/sdg/goals/tag_list_component_spec.rb +++ b/spec/components/sdg/goals/tag_list_component_spec.rb @@ -60,16 +60,4 @@ describe SDG::Goals::TagListComponent, type: :component do title: "One more goal", href: "/debates/#{debate.to_param}" end - - context "given a class name" do - let(:component) { SDG::Goals::TagListComponent.new("Debate") } - - it "renders all goals ordered by code" do - render_inline component - - expect(page).to have_selector "li", count: 17 - expect(page.first("a")[:title]).to end_with "goal 1" - expect(page.all("a").last[:title]).to end_with "goal 17" - end - end end