Skip to content

Instantly share code, notes, and snippets.

@tlux
Created May 6, 2014 08:25
Show Gist options
  • Select an option

  • Save tlux/c0124275ffcac4f75f81 to your computer and use it in GitHub Desktop.

Select an option

Save tlux/c0124275ffcac4f75f81 to your computer and use it in GitHub Desktop.
Font Awesome Icon Helper
class FontAwesome::Icon < FontAwesome::Iconish
VALID_OPTIONS = :shape, :direction, :outline, :spin, :size, :rotate, :flip, :inverse, :border, :fixed_width
attr_reader :name
def initialize(name, options = {})
@name = name
@html_options = options.except(*VALID_OPTIONS)
@options = options.slice(*VALID_OPTIONS).reverse_merge(
border: false,
fixed_width: false,
inverse: false,
outline: false,
rotate: 0,
spin: false
)
end
protected
def css_classes
css = %w(fa)
css << name_css
set_size(css)
set_fixed_width(css)
set_transform(css)
set_color(css)
set_border(css)
set_spin(css)
css
end
private
def name_css
name_css = "fa-#{name.to_s.dasherize}"
name_css << "-#{@options[:shape].to_s.dasherize}" if @options[:shape]
name_css << '-o' if @options[:outline]
name_css << "-#{@options[:direction].to_s.dasherize}" if @options[:direction]
name_css
end
def set_border(css)
css << 'fa-border' if @options[:border]
end
def set_color(css)
css << 'fa-inverse' if @options[:inverse]
end
def set_fixed_width(css)
css << 'fa-fw' if @options[:fixed_width]
end
def set_flip(css)
if @options[:flip]
if @options[:flip].to_sym.in? [:horizontal, :vertical]
css << "fa-flip-#{@options[:flip]}"
else
raise ArgumentError, ':flip must be :horizontal or :vertical'
end
end
end
def set_spin(css)
css << 'fa-spin' if @options[:spin]
end
def set_transform(css)
css << "fa-rotate-#{@options[:rotate]}" if @options[:rotate] and @options[:rotate] != 0
end
end
module IconHelper
def icon(name, options = {})
icon = FontAwesome::Icon.new(name, options)
content_tag :i, nil, icon.tag_options
end
def icon_stack(options = {}, &block)
icon_stack = FontAwesome::IconStack.new(options)
content_tag :span, icon_stack.tag_options, &block
end
def append_icon(*args, &block)
_labelled_icon(:append, *args, &block)
end
def prepend_icon(*args, &block)
_labelled_icon(:prepend, *args, &block)
end
private
def _labelled_icon(location, name, *args, &block)
options = args.extract_options!
caption = args.first
caption ||= capture(&block) if block_given?
caption_options = options.delete(:caption_html) { Hash.new }
caption_options[:class] = (caption_options[:class].to_s.split << 'caption').uniq.join(' ')
content_tag :span, class: "#{location}-icon" do
concat icon(name, options) if location == :prepend
concat content_tag(:span, caption, caption_options) if caption.present?
concat icon(name, options) if location == :append
end
end
end
class FontAwesome::IconStack < FontAwesome::Iconish
VALID_OPTIONS = [:size]
def initialize(options = {})
@html_options = options.except(*VALID_OPTIONS)
@options = options.slice(*VALID_OPTIONS)
end
protected
def css_classes
css = %w(fa-stack)
set_size(css)
css
end
end
class FontAwesome::Iconish
attr_reader :options, :html_options
def tag_options
css = (css_classes + @html_options[:class].to_s.split).uniq.join(' ')
@html_options.merge(class: css)
end
protected
def css_classes
raise NotImplementedError
end
def set_size(css)
css << (@options[:size] == :large ? 'fa-lg' : "fa-#{@options[:size]}x") if @options[:size]
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment