Skip to content
Browse files

allow #body_class to take an optional argument of a symbol which it w…

…ill use #content_for on to add more body classes, defaults to :extra_body_classes
  • Loading branch information...
1 parent 371ce9d commit ddb52a814ede3307ee9dba58cda465bbae09eb34 @mjankowski mjankowski committed Oct 1, 2011
Showing with 23 additions and 3 deletions.
  1. +9 −2 app/helpers/body_class_helper.rb
  2. +14 −1 spec/helpers/body_class_helper_spec.rb
View
11 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
View
15 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
@@ -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.
Something went wrong with that request. Please try again.