Skip to content

Instantly share code, notes, and snippets.

@pheisiph
Created April 8, 2012 13:08
Show Gist options
  • Save pheisiph/2337231 to your computer and use it in GitHub Desktop.
Save pheisiph/2337231 to your computer and use it in GitHub Desktop.
Useful standard classes I add to body tags in all my rails apps
module ApplicationHelper
# Public: Add a bunch of default class names to your html body element.
# By default it adds the name of the application, the current
# controller and the action
#
# classes - Arbitrary class names that are appended. Can be an Array
# of Strings or Symbols, or a single String or
# Symbol (or any to_s responder).
#
# Examples
#
# in your layout application.html.erb:
#
# <body class="<%= body_classes(yield :body_classes) %>">
#
# in your view, e.g. for users#show:
#
# <% content_for :body_classes, %w(slim bright) %>
#
# will output:
#
# <body class="yourapp users show mobile slim bright">
#
# Returns a string of class names to be added to your body element
def body_classes(classes=nil)
ary = [Rails.application.class.to_s.split("::").first.downcase]
ary << controller.controller_name
ary << controller.action_name
ary << 'mobile' if mobile_agent?
unless classes.nil?
method = classes.is_a?(Array) ? :concat : :<<
ary.send method, classes
end
ary.join(' ')
end
def mobile_agent?
return true if params[:mobile] == "1"
request.user_agent =~ /Mobile|webOS/
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment