Skip to content

Instantly share code, notes, and snippets.

@thomasklemm
Created August 26, 2014 19:38
Show Gist options
  • Save thomasklemm/1f7d732c77916b3717d1 to your computer and use it in GitHub Desktop.
Save thomasklemm/1f7d732c77916b3717d1 to your computer and use it in GitHub Desktop.
Icon Helper for Font Awesome in Ruby on Rails
module IconHelper
ICON_MAPPINGS = {
# Generic actions
:save => 'fa-check',
:cancel => 'fa-times',
:back => 'fa-arrow-left',
# Record actions
:new => 'fa-plus',
:edit => 'fa-pencil',
:destroy => 'fa-remove',
:trash => 'fa-trash-o',
:restore => 'fa-repeat',
# Model icons
:dashboard => 'fa-dashboard',
# ... more
}
def icon(kind, label = nil, &block)
label ||= capture(&block) if block_given?
icon = content_tag :i, nil, class: icon_class(kind)
if label
icon + label
else
icon
end
end
def icon_class(kind)
icon_class = ICON_MAPPINGS.fetch(kind.to_sym) do
raise "Undefined icon: #{ kind }"
end
"icon fa #{icon_class}"
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment