Sha256: 23d54f93f213cfebe6d7ae61b0eec9962b10b9dbffce1c5a05f0263b9500f0b6

Contents?: true

Size: 1.96 KB

Versions: 16

Compression:

Stored size: 1.96 KB

Contents

# frozen_string_literal: true

module Decidim
  class IconRegistry
    def initialize
      @icons = ActiveSupport::HashWithIndifferentAccess.new
    end

    # It allows detecting with icons are valid remixicons icons, and also for documenting them in the
    # `decidim-design` (aka Decidim Design Guide or DDG).
    #
    # Some of these fields are used to load and work with the icon (`name` and `icon`) and others are
    # for documentation purposes in DDG (`category`, `description`, and `engine`).
    #
    # @param name [String] The name of the icon. It will be used to find the icon later.
    # @param icon [String] The id of the icon. It will be used to load the icon from remixicon library.
    # @param category [String] The category name. It will be used to group the icons by category.
    # @param description [String] The description of the icon. It will be used to show the purpose of the icon in DDG.
    # @param engine [String] The engine name. It is used internally to identify the module where the icon is being used.
    def register(name:, icon:, description:, category:, engine:)
      ActiveSupport::Deprecation.warn("#{name} already registered. #{@icons[name].inspect}") if @icons[name]

      @icons[name] = { name:, icon:, description:, category:, engine: }
    end

    def find(name)
      if name.blank?
        ActiveSupport::Deprecation.warn "The requested icon is blank."
        name = "other"
      end

      @icons[name] || deprecated(name)
    end

    def all
      @icons
    end

    def categories(field = :category)
      all.values.group_by { |d| d[field].try(:to_s) }
    end

    private

    def deprecated(name)
      message = %{Icon #{name} not found. Register it with \n
        Decidim.icons.register(name: "#{name}", icon: "#{name}", category: "system", description: "", engine: :core)
      }

      ActiveSupport::Deprecation.warn(message)
      raise message if Rails.env.development? || Rails.env.test?

      @icons["other"]
    end
  end
end

Version data entries

16 entries across 16 versions & 1 rubygems

Version Path
decidim-core-0.30.0.rc2 lib/decidim/icon_registry.rb
decidim-core-0.30.0.rc1 lib/decidim/icon_registry.rb
decidim-core-0.29.2 lib/decidim/icon_registry.rb
decidim-core-0.28.5 lib/decidim/icon_registry.rb
decidim-core-0.29.1 lib/decidim/icon_registry.rb
decidim-core-0.28.4 lib/decidim/icon_registry.rb
decidim-core-0.29.0 lib/decidim/icon_registry.rb
decidim-core-0.28.3 lib/decidim/icon_registry.rb
decidim-core-0.29.0.rc4 lib/decidim/icon_registry.rb
decidim-core-0.29.0.rc3 lib/decidim/icon_registry.rb
decidim-core-0.29.0.rc2 lib/decidim/icon_registry.rb
decidim-core-0.29.0.rc1 lib/decidim/icon_registry.rb
decidim-core-0.28.2 lib/decidim/icon_registry.rb
decidim-core-0.28.1 lib/decidim/icon_registry.rb
decidim-core-0.28.0 lib/decidim/icon_registry.rb
decidim-core-0.28.0.rc5 lib/decidim/icon_registry.rb