Sha256: 09dca8b67b4f6dfea3ed443b2c2611eba007af71ef7954dc0fda7fc54e2677b8

Contents?: true

Size: 1.49 KB

Versions: 5

Compression:

Stored size: 1.49 KB

Contents

require 'active_admin/iconic/icons'

module ActiveAdmin
  module Iconic

    # Default color to use for icons
    @@default_color = "#5E6469"
    mattr_accessor :default_color


    # Render an icon:
    #   Iconic.icon :loop, width: 100, height: 100, color: "#5E6469"
    #   Iconic.icon :loop, width: "1em", height: "1em", color: "#5E6469"
    # NOTE: you can omit the dimensions if they are specified in css
    def self.icon(name, options = {})
      options = {
        color: default_color,
        id: ""
      }.merge(options)

      options[:style] = "fill:#{options[:color]};"
      options[:fill] = options.delete(:color)

      css = options.delete(:css) || {}

      # extract desired dimensions to be used as the wrapper's inline styles
      # Convert to strings representations of pixels
      [:width, :height].each do |key|
        value = options.delete key
        css[key] ||= "#{value}px" unless value.blank? || value.is_a?(String)
      end
      css_str = css.map {|k,v| "#{k}:#{v}"}.join(";")
      css_str = "style=\"#{css_str}\"" if css_str.present?

      
      # make the svg itself expand to its parent
      options[:width] = options[:height] = "100%"

      template = ICONS[name.to_sym]

      if template
        svg = template.dup
        options.each do |key, value|
          svg.gsub!("{#{key}}", value)
        end

        "<span class=\"icon icon_#{name}\" #{css_str}>#{svg}</span>".html_safe
      else
        raise "Could not find the icon named #{name}"
      end
    end

  end
end

Version data entries

5 entries across 5 versions & 2 rubygems

Version Path
active_administration-0.0.3 lib/active_admin/iconic.rb
activeadministration-0.0.2 lib/active_admin/iconic.rb
active_administration-0.0.2 lib/active_admin/iconic.rb
activeadministration-0.0.1 lib/active_admin/iconic.rb
active_administration-0.0.1 lib/active_admin/iconic.rb