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 label_class, content_class etc global option support #25

Merged
merged 1 commit into from Jan 18, 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.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
14 changes: 13 additions & 1 deletion lib/show_for.rb
Expand Up @@ -9,21 +9,33 @@ module ShowFor
mattr_accessor :label_tag mattr_accessor :label_tag
@@label_tag = :strong @@label_tag = :strong


mattr_accessor :label_class
@@label_class = :label

mattr_accessor :separator mattr_accessor :separator
@@separator = "<br />" @@separator = "<br />"


mattr_accessor :content_tag mattr_accessor :content_tag
@@content_tag = nil @@content_tag = nil


mattr_accessor :content_class
@@content_class = :content

mattr_accessor :blank_content_class mattr_accessor :blank_content_class
@@blank_content_class = "blank" @@blank_content_class = "blank"


mattr_accessor :wrapper_tag mattr_accessor :wrapper_tag
@@wrapper_tag = :p @@wrapper_tag = :p


mattr_accessor :wrapper_class
@@wrapper_class = :wrapper

mattr_accessor :collection_tag mattr_accessor :collection_tag
@@collection_tag = :ul @@collection_tag = :ul


mattr_accessor :collection_class
@@collection_class = :collection

mattr_accessor :default_collection_proc mattr_accessor :default_collection_proc
@@default_collection_proc = lambda { |value| "<li>#{ERB::Util.html_escape(value)}</li>".html_safe } @@default_collection_proc = lambda { |value| "<li>#{ERB::Util.html_escape(value)}</li>".html_safe }


Expand All @@ -45,4 +57,4 @@ module ShowFor
def self.setup def self.setup
yield self yield self
end end
end end
3 changes: 2 additions & 1 deletion lib/show_for/builder.rb
Expand Up @@ -46,8 +46,9 @@ def wrap_with(type, content, options) #:nodoc:
tag = options.delete(:"#{type}_tag") || ShowFor.send(:"#{type}_tag") tag = options.delete(:"#{type}_tag") || ShowFor.send(:"#{type}_tag")


if tag if tag
type_class = ShowFor.send :"#{type}_class"
html_options = options.delete(:"#{type}_html") || {} html_options = options.delete(:"#{type}_html") || {}
html_options[:class] = "#{type} #{html_options[:class]}".strip html_options[:class] = "#{type_class} #{html_options[:class]}".strip
@template.content_tag(tag, content, html_options) @template.content_tag(tag, content, html_options)
else else
content content
Expand Down
21 changes: 15 additions & 6 deletions test/builder_test.rb
Expand Up @@ -39,6 +39,15 @@ def with_attributes_for(object, *attributes)
end end


# WRAPPER # WRAPPER
test "show_for allows wrapper to be configured globally" do
swap ShowFor, :wrapper_tag => "li", :wrapper_class => "my_wrapper" do
with_attribute_for @user, :name
assert_select "div.show_for li.user_name.my_wrapper"
assert_select "div.show_for li.my_wrapper strong.label"
assert_select "div.show_for li.my_wrapper"
end
end

test "show_for attribute wraps each attribute with a label and content" do test "show_for attribute wraps each attribute with a label and content" do
with_attribute_for @user, :name with_attribute_for @user, :name
assert_select "div.show_for p.user_name.wrapper" assert_select "div.show_for p.user_name.wrapper"
Expand Down Expand Up @@ -82,9 +91,9 @@ def with_attributes_for(object, *attributes)
end end


test "show_for allows label to be configured globally" do test "show_for allows label to be configured globally" do
swap ShowFor, :label_tag => :span do swap ShowFor, :label_tag => :span, :label_class => "my_label" do
with_attribute_for @user, :name with_attribute_for @user, :name
assert_select "div.show_for p.wrapper span.label" assert_select "div.show_for p.wrapper span.my_label"
end end
end end


Expand Down Expand Up @@ -120,9 +129,9 @@ def with_attributes_for(object, *attributes)


# CONTENT # CONTENT
test "show_for allows content tag to be configured globally" do test "show_for allows content tag to be configured globally" do
swap ShowFor, :content_tag => :span do swap ShowFor, :content_tag => :span, :content_class => :my_content do
with_attribute_for @user, :name with_attribute_for @user, :name
assert_select "div.show_for p.wrapper span.content" assert_select "div.show_for p.wrapper span.my_content"
end end
end end


Expand Down Expand Up @@ -357,9 +366,9 @@ def with_attributes_for(object, *attributes)
end end


test "show_for allows collection tag to be configured globally" do test "show_for allows collection tag to be configured globally" do
swap ShowFor, :collection_tag => :ol do swap ShowFor, :collection_tag => :ol, :collection_class => "my_collection" do
with_attribute_for @user, :scopes with_attribute_for @user, :scopes
assert_select "div.show_for p.wrapper ol.collection" assert_select "div.show_for p.wrapper ol.my_collection"
end end
end end


Expand Down