Skip to content

Commit

Permalink
allow #body_class to take an optional argument of a symbol which it w…
Browse files Browse the repository at this point in the history
…ill use #content_for on to add more body classes, defaults to :extra_body_classes
  • Loading branch information
mjankowski committed Oct 1, 2011
1 parent 371ce9d commit ddb52a8
Show file tree
Hide file tree
Showing 2 changed files with 23 additions and 3 deletions.
11 changes: 9 additions & 2 deletions app/helpers/body_class_helper.rb
@@ -1,6 +1,13 @@
module BodyClassHelper
def body_class
def body_class(options = {})
extra_body_classes_symbol = options[:extra_body_classes_symbol] || :extra_body_classes
qualified_controller_name = controller.controller_path.gsub('/','-')
"#{qualified_controller_name} #{qualified_controller_name}-#{controller.action_name}"
basic_body_class = "#{qualified_controller_name} #{qualified_controller_name}-#{controller.action_name}"

if content_for?(extra_body_classes_symbol)
[basic_body_class, content_for(extra_body_classes_symbol)].join(' ')
else
basic_body_class
end
end
end
15 changes: 14 additions & 1 deletion spec/helpers/body_class_helper_spec.rb
@@ -1,6 +1,6 @@
require 'spec_helper'

describe BodyClassHelper do
describe BodyClassHelper, 'without any extra body classes' do
describe "body_class with a normal controller" do
before do
controller = mock
Expand All @@ -24,3 +24,16 @@
end
end
end

describe BodyClassHelper, 'with extra body classes' do
before do
controller = mock
controller.stubs(:controller_path).returns('widgets')
controller.stubs(:action_name).returns('show')
helper.stubs(:controller).returns controller
helper.content_for(:extra_body_classes, 'extra_class')
end
it "adds extra body classes to the controller classes" do
helper.body_class.should == 'widgets widgets-show extra_class'
end
end

0 comments on commit ddb52a8

Please sign in to comment.