Extract component to render a goal/target tag

This commit is contained in:
Javi Martín
2021-02-02 18:27:17 +01:00
parent 999d1a2cfd
commit a78663120a
6 changed files with 20 additions and 8 deletions

View File

@@ -13,7 +13,7 @@ class SDG::Goals::PlainTagListComponent < ApplicationComponent
def goal_tags def goal_tags
goals.order(:code).limit(limit).map do |goal| goals.order(:code).limit(limit).map do |goal|
render SDG::Goals::IconComponent.new(goal) render SDG::TagComponent.new(goal)
end end
end end

View File

@@ -14,7 +14,7 @@ class SDG::Goals::TagListComponent < ApplicationComponent
def goal_links def goal_links
goals.order(:code).limit(limit).map do |goal| goals.order(:code).limit(limit).map do |goal|
[ [
render(SDG::Goals::IconComponent.new(goal)), render(SDG::TagComponent.new(goal)),
index_by_goal(goal), index_by_goal(goal),
title: filter_text(goal) title: filter_text(goal)
] ]

View File

@@ -0,0 +1 @@
<%= text -%>

View File

@@ -0,0 +1,15 @@
class SDG::TagComponent < ApplicationComponent
attr_reader :goal_or_target
def initialize(goal_or_target)
@goal_or_target = goal_or_target
end
def text
if goal_or_target.is_a?(SDG::Goal)
render SDG::Goals::IconComponent.new(goal_or_target)
else
"#{SDG::Target.model_name.human} #{goal_or_target.code}"
end
end
end

View File

@@ -13,7 +13,7 @@ class SDG::Targets::PlainTagListComponent < ApplicationComponent
def target_tags def target_tags
targets.sort[0..(limit.to_i - 1)].map do |target| targets.sort[0..(limit.to_i - 1)].map do |target|
tag.span(text(target), data: { code: target.code }) tag.span(render(SDG::TagComponent.new(target)), data: { code: target.code })
end end
end end
@@ -21,10 +21,6 @@ class SDG::Targets::PlainTagListComponent < ApplicationComponent
record.sdg_targets record.sdg_targets
end end
def text(target)
"#{SDG::Target.model_name.human} #{target.code}"
end
def i18n_namespace def i18n_namespace
"targets" "targets"
end end

View File

@@ -14,7 +14,7 @@ class SDG::Targets::TagListComponent < ApplicationComponent
def target_links def target_links
targets.sort[0..(limit.to_i - 1)].map do |target| targets.sort[0..(limit.to_i - 1)].map do |target|
[ [
"#{SDG::Target.model_name.human} #{target.code}", render(SDG::TagComponent.new(target)),
index_by_target(target), index_by_target(target),
title: filter_text(target), title: filter_text(target),
data: { code: target.code } data: { code: target.code }