1
0
Fork 0
mirror of https://github.com/heartcombo/simple_form.git synced 2022-11-09 12:19:26 -05:00

Merge pull request #346 from mfila/add-collection-wrapper-class

Allow to set the class for collection_wrapper_tag
This commit is contained in:
Rafael Mendonça França 2011-11-04 21:24:59 -07:00
commit 6c283ec3d2
4 changed files with 88 additions and 5 deletions

View file

@ -32,6 +32,9 @@ SimpleForm.setup do |config|
# You can wrap a collection of radio/check boxes in a pre-defined tag, defaulting to none.
# config.collection_wrapper_tag = nil
# You can define the class to use on all collection wrappers. Defaulting to none.
# config.collection_wrapper_class = nil
# You can wrap each item in a collection of radio/check boxes with a tag, defaulting to span.
# config.item_wrapper_tag = :span

View file

@ -42,6 +42,10 @@ module SimpleForm
mattr_accessor :collection_wrapper_tag
@@collection_wrapper_tag = nil
# You can define the class to use on all collection wrappers. Defaulting to none.
mattr_accessor :collection_wrapper_class
@@collection_wrapper_class = nil
# You can wrap each item in a collection of radio/check boxes with a tag, defaulting to none.
mattr_accessor :item_wrapper_tag
@@item_wrapper_tag = :span

View file

@ -31,9 +31,11 @@ module SimpleForm
# * disabled => the value or values that should be disabled. Accepts a single
# item or an array of items.
#
# * collection_wrapper_tag => the tag to wrap the entire collection.
# * collection_wrapper_tag => the tag to wrap the entire collection.
#
# * item_wrapper_tag => the tag to wrap each item in the collection.
# * collection_wrapper_class => the CSS class to use for collection_wrapper_tag
#
# * item_wrapper_tag => the tag to wrap each item in the collection.
#
def collection_radio(attribute, collection, value_method, text_method, options={}, html_options={})
render_collection(
@ -73,9 +75,11 @@ module SimpleForm
# * disabled => the value or values that should be disabled. Accepts a single
# item or an array of items.
#
# * collection_wrapper_tag => the tag to wrap the entire collection.
# * collection_wrapper_tag => the tag to wrap the entire collection.
#
# * item_wrapper_tag => the tag to wrap each item in the collection.
# * collection_wrapper_class => the CSS class to use for collection_wrapper_tag
#
# * item_wrapper_tag => the tag to wrap each item in the collection.
#
def collection_check_boxes(attribute, collection, value_method, text_method, options={}, html_options={})
render_collection(
@ -135,6 +139,8 @@ module SimpleForm
def render_collection(attribute, collection, value_method, text_method, options={}, html_options={}) #:nodoc:
collection_wrapper_tag = options.has_key?(:collection_wrapper_tag) ? options[:collection_wrapper_tag] : SimpleForm.collection_wrapper_tag
collection_wrapper_class = [SimpleForm.collection_wrapper_class, options[:collection_wrapper_class]].compact
collection_wrapper_class = nil if collection_wrapper_class.empty?
item_wrapper_tag = options.has_key?(:item_wrapper_tag) ? options[:item_wrapper_tag] : SimpleForm.item_wrapper_tag
rendered_collection = collection.map do |item|
@ -147,7 +153,7 @@ module SimpleForm
item_wrapper_tag ? @template.content_tag(item_wrapper_tag, rendered_item) : rendered_item
end.join.html_safe
collection_wrapper_tag ? @template.content_tag(collection_wrapper_tag, rendered_collection) : rendered_collection
collection_wrapper_tag ? @template.content_tag(collection_wrapper_tag, rendered_collection, :class => collection_wrapper_class) : rendered_collection
end
def value_for_collection(item, value) #:nodoc:

View file

@ -120,6 +120,41 @@ class BuilderTest < ActionView::TestCase
assert_no_select 'form ul'
end
test 'collection radio uses the configured class for collection wrapper tag' do
swap SimpleForm, :collection_wrapper_tag => :ul, :collection_wrapper_class => :'inputs-list' do
with_collection_radio @user, :active, [true, false], :to_s, :to_s
assert_select 'form ul.inputs-list input[type=radio][value=true]#user_active_true'
assert_select 'form ul.inputs-list input[type=radio][value=false]#user_active_false'
end
end
test 'collection radio uses the given class for collection wrapper tag' do
swap SimpleForm, :collection_wrapper_tag => :ul do
with_collection_radio @user, :active, [true, false], :to_s, :to_s, :collection_wrapper_class => :'items-list'
assert_select 'form ul.items-list input[type=radio][value=true]#user_active_true'
assert_select 'form ul.items-list input[type=radio][value=false]#user_active_false'
end
end
test 'collection radio uses both configured and given classes for collection wrapper tag' do
swap SimpleForm, :collection_wrapper_tag => :ul, :collection_wrapper_class => :'inputs-list' do
with_collection_radio @user, :active, [true, false], :to_s, :to_s, :collection_wrapper_class => :'items-list'
assert_select 'form ul.inputs-list.items-list input[type=radio][value=true]#user_active_true'
assert_select 'form ul.inputs-list.items-list input[type=radio][value=false]#user_active_false'
end
end
test 'collection radio uses no class for collection wrapper tag by default' do
swap SimpleForm, :collection_wrapper_tag => :ul do
with_collection_radio @user, :active, [true, false], :to_s, :to_s
assert_no_select 'form ul[class]'
end
end
test 'collection radio wraps each label/radio in the configured item wrapper tag' do
swap SimpleForm, :item_wrapper_tag => :li do
with_collection_radio @user, :active, [true, false], :to_s, :to_s
@ -291,6 +326,41 @@ class BuilderTest < ActionView::TestCase
assert_no_select 'form ul'
end
test 'collection check box uses the configured class for collection wrapper tag' do
swap SimpleForm, :collection_wrapper_tag => :ul, :collection_wrapper_class => :'inputs-list' do
with_collection_check_boxes @user, :active, [true, false], :to_s, :to_s
assert_select 'form ul.inputs-list input[type=checkbox][value=true]#user_active_true'
assert_select 'form ul.inputs-list input[type=checkbox][value=false]#user_active_false'
end
end
test 'collection check box uses the given class for collection wrapper tag' do
swap SimpleForm, :collection_wrapper_tag => :ul do
with_collection_check_boxes @user, :active, [true, false], :to_s, :to_s, :collection_wrapper_class => :'items-list'
assert_select 'form ul.items-list input[type=checkbox][value=true]#user_active_true'
assert_select 'form ul.items-list input[type=checkbox][value=false]#user_active_false'
end
end
test 'collection check box uses both configured and given classes for collection wrapper tag' do
swap SimpleForm, :collection_wrapper_tag => :ul, :collection_wrapper_class => :'inputs-list' do
with_collection_check_boxes @user, :active, [true, false], :to_s, :to_s, :collection_wrapper_class => :'items-list'
assert_select 'form ul.inputs-list.items-list input[type=checkbox][value=true]#user_active_true'
assert_select 'form ul.inputs-list.items-list input[type=checkbox][value=false]#user_active_false'
end
end
test 'collection check box uses no class for collection wrapper tag by default' do
swap SimpleForm, :collection_wrapper_tag => :ul do
with_collection_check_boxes @user, :active, [true, false], :to_s, :to_s
assert_no_select 'form ul[class]'
end
end
test 'collection check box wraps each label/radio in the configured item wrapper tag' do
swap SimpleForm, :item_wrapper_tag => :li do
with_collection_check_boxes @user, :active, [true, false], :to_s, :to_s