Files
nairobi/app/components/sdg/goals/icon_component.rb
Javi Martín ceed3c18d3 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.
2021-01-09 14:53:54 +01:00

33 lines
626 B
Ruby

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