Reorganize SDG tag list components
So now we've got a component receiving records (goals or targets) and a related model (Debate, Proposal, ...), with optionally a link to see more tags. This way we simplify some logic since the `TagList` classes were dealing with too many cases (a record is passed, a class name is passed, a limit is passed), ... Now `TagList` only deal with the natural `TagList` case, which is listing the tags for a record. The case where a class name is passed is used in the `TagCloud` class.
This commit is contained in:
@@ -1,36 +1,31 @@
|
|||||||
module SDG::TagList
|
module SDG::TagList
|
||||||
extend ActiveSupport::Concern
|
extend ActiveSupport::Concern
|
||||||
attr_reader :record_or_name, :limit
|
attr_reader :record, :limit
|
||||||
delegate :link_list, to: :helpers
|
|
||||||
|
|
||||||
def initialize(record_or_name, limit: nil)
|
def initialize(record, limit: nil)
|
||||||
@record_or_name = record_or_name
|
@record = record
|
||||||
@limit = limit
|
@limit = limit
|
||||||
end
|
end
|
||||||
|
|
||||||
def render?
|
def render?
|
||||||
process.enabled?
|
SDG::ProcessEnabled.new(record).enabled?
|
||||||
end
|
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)
|
render Shared::SeeMoreLinkComponent.new(record, association_name, limit: limit)
|
||||||
end
|
end
|
||||||
|
|
||||||
def filter_text(goal_or_target)
|
def association_name
|
||||||
t("sdg.#{i18n_namespace}.filter.link",
|
raise NotImplementedError, "method must be implemented in the included class"
|
||||||
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
|
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|||||||
1
app/components/sdg/filter_links_component.html.erb
Normal file
1
app/components/sdg/filter_links_component.html.erb
Normal file
@@ -0,0 +1 @@
|
|||||||
|
<%= link_list(*links, class: "sdg-#{parameter_name}-tag-list") %>
|
||||||
49
app/components/sdg/filter_links_component.rb
Normal file
49
app/components/sdg/filter_links_component.rb
Normal file
@@ -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
|
||||||
@@ -3,25 +3,17 @@ class SDG::Goals::PlainTagListComponent < ApplicationComponent
|
|||||||
|
|
||||||
private
|
private
|
||||||
|
|
||||||
def record
|
|
||||||
record_or_name
|
|
||||||
end
|
|
||||||
|
|
||||||
def tags
|
def tags
|
||||||
[*goal_tags, see_more_link(:sdg_goals)].select(&:present?)
|
[*goal_tags, see_more_link].select(&:present?)
|
||||||
end
|
end
|
||||||
|
|
||||||
def goal_tags
|
def goal_tags
|
||||||
goals.order(:code).limit(limit).map do |goal|
|
tag_records.map do |goal|
|
||||||
render SDG::TagComponent.new(goal)
|
render SDG::TagComponent.new(goal)
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
def goals
|
def association_name
|
||||||
record.sdg_goals
|
:sdg_goals
|
||||||
end
|
|
||||||
|
|
||||||
def i18n_namespace
|
|
||||||
"goals"
|
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|||||||
@@ -1,5 +1,5 @@
|
|||||||
<div class="sdg-goal-tag-cloud">
|
<div class="sdg-goal-tag-cloud">
|
||||||
<div class="sidebar-divider"></div>
|
<div class="sidebar-divider"></div>
|
||||||
<h2 class="sidebar-title"><%= heading %></h2>
|
<h2 class="sidebar-title"><%= heading %></h2>
|
||||||
<%= render SDG::Goals::TagListComponent.new(class_name) %>
|
<%= render SDG::FilterLinksComponent.new(goals, class_name.constantize) %>
|
||||||
</div>
|
</div>
|
||||||
|
|||||||
@@ -14,4 +14,8 @@ class SDG::Goals::TagCloudComponent < ApplicationComponent
|
|||||||
def heading
|
def heading
|
||||||
t("sdg.goals.filter.heading")
|
t("sdg.goals.filter.heading")
|
||||||
end
|
end
|
||||||
|
|
||||||
|
def goals
|
||||||
|
SDG::Goal.order(:code)
|
||||||
|
end
|
||||||
end
|
end
|
||||||
|
|||||||
@@ -1 +1 @@
|
|||||||
<%= link_list(*links, class: "sdg-goal-tag-list") %>
|
<%= render SDG::FilterLinksComponent.new(tag_records, related_model, see_more_link: see_more_link) %>
|
||||||
|
|||||||
@@ -3,33 +3,11 @@ class SDG::Goals::TagListComponent < ApplicationComponent
|
|||||||
|
|
||||||
private
|
private
|
||||||
|
|
||||||
def record
|
def association_name
|
||||||
record_or_name if record_or_name.respond_to?(:sdg_goals)
|
:sdg_goals
|
||||||
end
|
end
|
||||||
|
|
||||||
def links
|
def related_model
|
||||||
[*goal_links, see_more_link(:sdg_goals)]
|
record.class
|
||||||
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"
|
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|||||||
@@ -3,25 +3,17 @@ class SDG::Targets::PlainTagListComponent < ApplicationComponent
|
|||||||
|
|
||||||
private
|
private
|
||||||
|
|
||||||
def record
|
|
||||||
record_or_name
|
|
||||||
end
|
|
||||||
|
|
||||||
def tags
|
def tags
|
||||||
[*target_tags, see_more_link(:sdg_targets)].select(&:present?)
|
[*target_tags, see_more_link].select(&:present?)
|
||||||
end
|
end
|
||||||
|
|
||||||
def target_tags
|
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 })
|
tag.span(render(SDG::TagComponent.new(target)), data: { code: target.code })
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
def targets
|
def association_name
|
||||||
record.sdg_targets
|
:sdg_targets
|
||||||
end
|
|
||||||
|
|
||||||
def i18n_namespace
|
|
||||||
"targets"
|
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|||||||
@@ -1 +1 @@
|
|||||||
<%= link_list(*links, class: "sdg-target-tag-list") %>
|
<%= render SDG::FilterLinksComponent.new(tag_records, related_model, see_more_link: see_more_link) %>
|
||||||
|
|||||||
@@ -3,34 +3,11 @@ class SDG::Targets::TagListComponent < ApplicationComponent
|
|||||||
|
|
||||||
private
|
private
|
||||||
|
|
||||||
def record
|
def association_name
|
||||||
record_or_name
|
:sdg_targets
|
||||||
end
|
end
|
||||||
|
|
||||||
def links
|
def related_model
|
||||||
[*target_links, see_more_link(:sdg_targets)]
|
record.class
|
||||||
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"
|
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|||||||
@@ -15,11 +15,13 @@ describe SDG::Goals::TagCloudComponent, type: :component do
|
|||||||
expect(page).to have_content "Filters by SDG"
|
expect(page).to have_content "Filters by SDG"
|
||||||
end
|
end
|
||||||
|
|
||||||
it "renders all goals" do
|
it "renders all goals ordered by code" do
|
||||||
component = SDG::Goals::TagCloudComponent.new("Proposal")
|
component = SDG::Goals::TagCloudComponent.new("Proposal")
|
||||||
|
|
||||||
render_inline component
|
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
|
||||||
end
|
end
|
||||||
|
|||||||
@@ -60,16 +60,4 @@ describe SDG::Goals::TagListComponent, type: :component do
|
|||||||
title: "One more goal",
|
title: "One more goal",
|
||||||
href: "/debates/#{debate.to_param}"
|
href: "/debates/#{debate.to_param}"
|
||||||
end
|
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
|
end
|
||||||
|
|||||||
Reference in New Issue
Block a user