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:
Javi Martín
2021-01-01 15:40:18 +01:00
parent 53af5749b9
commit ceed3c18d3
13 changed files with 206 additions and 0 deletions

View File

@@ -0,0 +1 @@
<%= image_tag image_path, alt: image_text, class: "sdg-goal-icon" %>

View 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

View File

@@ -0,0 +1 @@
<%= link_list(*links, class: "sdg-goal-tag-list") %>

View 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