Display SDG icons alongside tags
We're using the translation fallbacks for the icons, just like we do for texts. Note we use the `render?` method provided by view_component to make sure the component will not be rendered when certain features are disabled. Also note the `find_asset` method works differently in development and production, and so we use different approaches here.
This commit is contained in:
1
app/components/sdg/goals/icon_component.html.erb
Normal file
1
app/components/sdg/goals/icon_component.html.erb
Normal file
@@ -0,0 +1 @@
|
||||
<%= image_tag image_path, alt: image_text, class: "sdg-goal-icon" %>
|
||||
32
app/components/sdg/goals/icon_component.rb
Normal file
32
app/components/sdg/goals/icon_component.rb
Normal file
@@ -0,0 +1,32 @@
|
||||
class SDG::Goals::IconComponent < ApplicationComponent
|
||||
attr_reader :goal
|
||||
delegate :code, to: :goal
|
||||
|
||||
def initialize(goal)
|
||||
@goal = goal
|
||||
end
|
||||
|
||||
def image_path
|
||||
"sdg/#{folder}/goal_#{code}.png"
|
||||
end
|
||||
|
||||
private
|
||||
|
||||
def image_text
|
||||
goal.code_and_title
|
||||
end
|
||||
|
||||
def folder
|
||||
[*I18n.fallbacks[I18n.locale], "default"].find do |locale|
|
||||
find_asset("sdg/#{locale}/goal_#{code}.png")
|
||||
end
|
||||
end
|
||||
|
||||
def find_asset(path)
|
||||
if Rails.application.assets
|
||||
Rails.application.assets.find_asset(path)
|
||||
else
|
||||
Rails.application.assets_manifest.assets[path]
|
||||
end
|
||||
end
|
||||
end
|
||||
1
app/components/sdg/goals/tag_list_component.html.erb
Normal file
1
app/components/sdg/goals/tag_list_component.html.erb
Normal file
@@ -0,0 +1 @@
|
||||
<%= link_list(*links, class: "sdg-goal-tag-list") %>
|
||||
57
app/components/sdg/goals/tag_list_component.rb
Normal file
57
app/components/sdg/goals/tag_list_component.rb
Normal file
@@ -0,0 +1,57 @@
|
||||
class SDG::Goals::TagListComponent < ApplicationComponent
|
||||
attr_reader :record, :limit
|
||||
delegate :link_list, to: :helpers
|
||||
|
||||
def initialize(record, limit: nil)
|
||||
@record = record
|
||||
@limit = limit
|
||||
end
|
||||
|
||||
def render?
|
||||
SDG::ProcessEnabled.new(record.class.name).enabled?
|
||||
end
|
||||
|
||||
private
|
||||
|
||||
def links
|
||||
[*goal_links, see_more_link]
|
||||
end
|
||||
|
||||
def goal_links
|
||||
goals.limit(limit).map do |goal|
|
||||
[
|
||||
render(SDG::Goals::IconComponent.new(goal)),
|
||||
index_by_goal(goal),
|
||||
title: filter_text(goal)
|
||||
]
|
||||
end
|
||||
end
|
||||
|
||||
def goals
|
||||
record.sdg_goals.order(:code)
|
||||
end
|
||||
|
||||
def see_more_link
|
||||
return unless limit && count_out_of_limit > 0
|
||||
|
||||
[
|
||||
"#{count_out_of_limit}+",
|
||||
polymorphic_path(record),
|
||||
class: "more-goals", title: t("sdg.goals.filter.more", count: count_out_of_limit)
|
||||
]
|
||||
end
|
||||
|
||||
def index_by_goal(goal)
|
||||
polymorphic_path(record.class, advanced_search: { goal: goal.code })
|
||||
end
|
||||
|
||||
def filter_text(goal)
|
||||
t("sdg.goals.filter.link",
|
||||
resources: record.model_name.human(count: :other),
|
||||
code: goal.code)
|
||||
end
|
||||
|
||||
def count_out_of_limit
|
||||
goals.size - limit
|
||||
end
|
||||
end
|
||||
Reference in New Issue
Block a user