Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

add show_for_class option to config #42

Merged
merged 1 commit into from
May 23, 2012
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions lib/generators/show_for/templates/show_for.rb
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,9 @@
# The tag which wraps show_for calls.
# config.show_for_tag = :div

# The DOM class set for show_for tag. Default is nil
# config.show_for_class = :custom

# The tag which wraps each attribute/association call. Default is :p.
# config.wrapper_tag = :dl

Expand Down
3 changes: 3 additions & 0 deletions lib/show_for.rb
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,9 @@ module ShowFor
mattr_accessor :show_for_tag
@@show_for_tag = :div

mattr_accessor :show_for_class
@@show_for_class = nil

mattr_accessor :label_tag
@@label_tag = :strong

Expand Down
8 changes: 7 additions & 1 deletion lib/show_for/helper.rb
Original file line number Diff line number Diff line change
Expand Up @@ -13,13 +13,19 @@ def show_for(object, html_options={}, &block)
tag = html_options.delete(:show_for_tag) || ShowFor.show_for_tag

html_options[:id] ||= dom_id(object)
html_options[:class] = "show_for #{dom_class(object)} #{html_options[:class]}".rstrip
html_options[:class] = show_for_html_class(object, html_options)

builder = html_options.delete(:builder) || ShowFor::Builder
content = capture(builder.new(object, self), &block)

content_tag(tag, content, html_options)
end

private

def show_for_html_class(object, html_options)
"show_for #{dom_class(object)} #{html_options[:class]} #{ShowFor.show_for_class}".squeeze(" ").rstrip
end
end
end

Expand Down
7 changes: 7 additions & 0 deletions test/helper_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -37,4 +37,11 @@ class HelperTest < ActionView::TestCase
assert_select "p.show_for"
end
end

test "show for class should be configurable" do
swap ShowFor, :show_for_class => :awesome do
concat(show_for(@user) do |f| end)
assert_select "div.show_for.user.awesome"
end
end
end