Skip to content

Instantly share code, notes, and snippets.

@merbjedi
Created March 12, 2010 14:47
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save merbjedi/330366 to your computer and use it in GitHub Desktop.
Save merbjedi/330366 to your computer and use it in GitHub Desktop.
# provides a slick way to add classes inside haml attribute collections
#
# examples:
# %div{add_class("current")}
# #=> adds the "current" class to the div
#
# %div{add_class("current", :if => current?)}
# #=> adds the "current" class to the div if current? method
#
# %div{add_class("highlight", :unless => logged_in?)}
# #=> adds the "highlight" class to the div unless logged_in? method returns true
def add_class(css_class, options = {})
return {} unless css_class
if options.has_key?(:unless) && !options[:unless]
return {:class => css_class}
end
if options.has_key?(:if) && options[:if]
return {:class => css_class}
end
if !options.has_key?(:if) and !options.has_key?(:unless)
{:class => css_class}
else
{}
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment