app/models/blacklight/icon.rb in blacklight-7.6.0 vs app/models/blacklight/icon.rb in blacklight-7.7.0

- old
+ new

@@ -1,23 +1,25 @@ # frozen_string_literal: true module Blacklight class Icon - attr_reader :icon_name, :aria_hidden, :label, :role + attr_reader :icon_name, :aria_hidden, :label, :role, :additional_options ## # @param [String, Symbol] icon_name # @param [Hash] options # @param [String] classes additional classes separated by a string # @param [Boolean] aria_hidden include aria_hidden attribute # @param [Boolean] label include <title> and aria-label as part of svg # @param [String] role role attribute to be included in svg - def initialize(icon_name, classes: '', aria_hidden: false, label: true, role: 'img') + # @param [Hash] additional_options the way forward instead of named arguments + def initialize(icon_name, classes: '', aria_hidden: false, label: true, role: 'img', additional_options: {}) @icon_name = icon_name @classes = classes @aria_hidden = aria_hidden @label = label @role = role + @additional_options = additional_options end ## # Returns an updated version of the svg source # @return [String] @@ -28,15 +30,15 @@ svg.add_child("<title id='#{unique_id}'>#{icon_label}</title>") if label ng_xml.to_xml end def icon_label - I18n.translate("blacklight.icon.#{icon_name}", default: "#{icon_name} icon") + I18n.translate("blacklight.icon.#{icon_name_context}", default: "#{icon_name} icon") end def unique_id - @unique_id ||= "bl-icon-#{icon_name}-#{SecureRandom.hex(8)}" + @unique_id ||= "bl-icon-#{icon_name_context}-#{SecureRandom.hex(8)}" end ## # @return [Hash] def options @@ -63,9 +65,13 @@ def ng_xml @ng_xml ||= Nokogiri::XML(file_source).remove_namespaces! end private + + def icon_name_context + [icon_name, additional_options[:label_context]].compact.join('_') + end def file # Rails.application.assets is `nil` in production mode (where compile assets is enabled). # This workaround is based off of this comment: https://github.com/fphilipe/premailer-rails/issues/145#issuecomment-225992564 (Rails.application.assets || ::Sprockets::Railtie.build_environment(Rails.application)).find_asset(path)