Created
August 26, 2014 19:38
-
-
Save thomasklemm/1f7d732c77916b3717d1 to your computer and use it in GitHub Desktop.
Icon Helper for Font Awesome in Ruby on Rails
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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