Sha256: c5197b67574f49396306edbc2087b1d3382181ffa1a0ac6b79498b617213a1eb

Contents?: true

Size: 1.45 KB

Versions: 1

Compression:

Stored size: 1.45 KB

Contents

# @private
class Bootstrap::IconRenderer
  ArgumentError = Class.new(StandardError)
  include ActionView::Helpers::SanitizeHelper
  
  delegate :canonicalize_options, :content_tag, :ensure_class, :h, to: :template
  attr_accessor :template, :options, :args, :text
  
  def initialize(template, *args)
    self.template = template
    process_arguments(args)
  end
  
  def html
    icon_tag.tap do |tag|
      tag << text if text.present?
    end
  end
  
  private
  
  def icon_tag
    content_tag(:i, nil, options)
  end
  
  def process_arguments(args)
    self.options = canonicalize_options(args.extract_options!)
    self.args = args.map { |e| e.to_s }

    process_icon_type
    process_icon_white
    process_text
  end
  
  def process_icon_type
    icon_type = args.shift.presence or raise(ArgumentError, "must pass an icon type")
    icon_class = icon_type == 'blank' ? 'glyphicon glyphicon-search' : "glyphicon glyphicon-#{icon_type.gsub("_", "-")}"
    self.options = ensure_class(self.options, icon_class)
    
    if icon_type == 'blank'
      options[:style] = [options[:style], 'opacity: 0'].map(&:presence).compact.join('; ')
    end
  end
  
  def process_icon_white
    if args.delete('white')
      self.options = ensure_class(options, "icon-white")
    end
  end

  def process_text
    self.text = if args.size == 0
      nil
    elsif args.size == 1
      sanitize(" #{args.first}")
    else
      raise ArgumentError, 'too many arguments'
    end 
  end

end

Version data entries

1 entries across 1 versions & 1 rubygems

Version Path
bootstrap-view-helpers-0.0.14 app/helpers/bootstrap/icon_renderer.rb