Skip to content

Commit

Permalink
Remove collection_wrapper* and item_wrapper* options
Browse files Browse the repository at this point in the history
[Carlos Antonio da Silva + Rafael Mendonça França]
  • Loading branch information
carlosantoniodasilva committed Feb 2, 2012
1 parent 471b855 commit b17be29
Show file tree
Hide file tree
Showing 4 changed files with 15 additions and 218 deletions.
36 changes: 12 additions & 24 deletions actionpack/lib/action_view/helpers/form_collections_helper.rb
Expand Up @@ -24,18 +24,12 @@ module FormCollectionsHelper
# collection_radio_buttons(:post, :author_id, Author.all, :id, :name_with_initial)
#
# If <tt>@post.author_id</tt> is already <tt>1</tt>, this would return:
# <span>
# <input id="post_author_id_1" name="post[author_id]" type="radio" value="1" checked="checked" />
# <label class="collection_radio_buttons" for="post_author_id_1">D. Heinemeier Hansson</label>
# </span>
# <span>
# <input id="post_author_id_2" name="post[author_id]" type="radio" value="2" />
# <label class="collection_radio_buttons" for="post_author_id_2">D. Thomas</label>
# </span>
# <span>
# <input id="post_author_id_3" name="post[author_id]" type="radio" value="3" />
# <label class="collection_radio_buttons" for="post_author_id_3">M. Clark</label>
# </span>
# <input id="post_author_id_1" name="post[author_id]" type="radio" value="1" checked="checked" />
# <label class="collection_radio_buttons" for="post_author_id_1">D. Heinemeier Hansson</label>
# <input id="post_author_id_2" name="post[author_id]" type="radio" value="2" />
# <label class="collection_radio_buttons" for="post_author_id_2">D. Thomas</label>
# <input id="post_author_id_3" name="post[author_id]" type="radio" value="3" />
# <label class="collection_radio_buttons" for="post_author_id_3">M. Clark</label>
def collection_radio_buttons(object, method, collection, value_method, text_method, options = {}, html_options = {}, &block)
Tags::CollectionRadioButtons.new(object, method, self, collection, value_method, text_method, options, html_options).render(&block)
end
Expand Down Expand Up @@ -63,18 +57,12 @@ def collection_radio_buttons(object, method, collection, value_method, text_meth
# collection_check_boxes(:post, :author_ids, Author.all, :id, :name_with_initial)
#
# If <tt>@post.author_ids</tt> is already <tt>[1]</tt>, this would return:
# <span>
# <input id="post_author_ids_1" name="post[author_ids][]" type="checkbox" value="1" checked="checked" />
# <label class="collection_check_boxes" for="post_author_ids_1">D. Heinemeier Hansson</label>
# </span>
# <span>
# <input id="post_author_ids_1" name="post[author_ids][]" type="checkbox" value="2" />
# <label class="collection_check_boxes" for="post_author_ids_1">D. Thomas</label>
# </span>
# <span>
# <input id="post_author_ids_3" name="post[author_ids][]" type="checkbox" value="3" />
# <label class="collection_check_boxes" for="post_author_ids_3">M. Clark</label>
# </span>
# <input id="post_author_ids_1" name="post[author_ids][]" type="checkbox" value="1" checked="checked" />
# <label class="collection_check_boxes" for="post_author_ids_1">D. Heinemeier Hansson</label>
# <input id="post_author_ids_1" name="post[author_ids][]" type="checkbox" value="2" />
# <label class="collection_check_boxes" for="post_author_ids_1">D. Thomas</label>
# <input id="post_author_ids_3" name="post[author_ids][]" type="checkbox" value="3" />
# <label class="collection_check_boxes" for="post_author_ids_3">M. Clark</label>
# <input name="post[author_ids][]" type="hidden" value="" />
def collection_check_boxes(object, method, collection, value_method, text_method, options = {}, html_options = {}, &block)
Tags::CollectionCheckBoxes.new(object, method, self, collection, value_method, text_method, options, html_options).render(&block)
Expand Down
Expand Up @@ -20,7 +20,7 @@ def render
# server if all check boxes are unchecked.
hidden = @template_object.hidden_field_tag(tag_name_multiple, "", :id => nil)

wrap_rendered_collection(rendered_collection + hidden)
rendered_collection + hidden
end
end
end
Expand Down
Expand Up @@ -5,16 +5,14 @@ class CollectionRadioButtons < CollectionSelect
delegate :radio_button, :label, :to => :@template_object

def render
rendered_collection = render_collection do |value, text, default_html_options|
render_collection do |value, text, default_html_options|
if block_given?
yield sanitize_attribute_name(value), text, value, default_html_options
else
radio_button(@object_name, @method_name, value, default_html_options) +
label(@object_name, sanitize_attribute_name(value), text, :class => "collection_radio_buttons")
end
end

wrap_rendered_collection(rendered_collection)
end

private
Expand Down Expand Up @@ -49,34 +47,18 @@ def sanitize_attribute_name(value) #:nodoc:
end

def render_collection #:nodoc:
item_wrapper_tag = @options.fetch(:item_wrapper_tag, :span)
item_wrapper_class = @options[:item_wrapper_class]

@collection.map do |item|
value = value_for_collection(item, @value_method)
text = value_for_collection(item, @text_method)
default_html_options = default_html_options_for_collection(item, value)

rendered_item = yield value, text, default_html_options

item_wrapper_tag ? @template_object.content_tag(item_wrapper_tag, rendered_item, :class => item_wrapper_class) : rendered_item
yield value, text, default_html_options
end.join.html_safe
end

def value_for_collection(item, value) #:nodoc:
value.respond_to?(:call) ? value.call(item) : item.send(value)
end

def wrap_rendered_collection(collection)
wrapper_tag = @options[:collection_wrapper_tag]

if wrapper_tag
wrapper_class = @options[:collection_wrapper_class]
@template_object.content_tag(wrapper_tag, collection, :class => wrapper_class)
else
collection
end
end
end
end
end
Expand Down
173 changes: 0 additions & 173 deletions actionpack/test/template/form_collections_helper_test.rb
Expand Up @@ -76,93 +76,6 @@ def with_collection_check_boxes(*args, &block)
assert_select 'input[type=radio][value=false].special-radio#user_active_false'
end

test 'collection radio wraps the collection in the given collection wrapper tag' do
with_collection_radio_buttons :user, :active, [true, false], :to_s, :to_s, :collection_wrapper_tag => :ul

assert_select 'ul input[type=radio]', :count => 2
end

test 'collection radio does not render any wrapper tag by default' do
with_collection_radio_buttons :user, :active, [true, false], :to_s, :to_s

assert_select 'input[type=radio]', :count => 2
assert_no_select 'ul'
end

test 'collection radio does not wrap the collection when given falsy values' do
with_collection_radio_buttons :user, :active, [true, false], :to_s, :to_s, :collection_wrapper_tag => false

assert_select 'input[type=radio]', :count => 2
assert_no_select 'ul'
end

test 'collection radio uses the given class for collection wrapper tag' do
with_collection_radio_buttons :user, :active, [true, false], :to_s, :to_s,
:collection_wrapper_tag => :ul, :collection_wrapper_class => "items-list"

assert_select 'ul.items-list input[type=radio]', :count => 2
end

test 'collection radio uses no class for collection wrapper tag when no wrapper tag is given' do
with_collection_radio_buttons :user, :active, [true, false], :to_s, :to_s,
:collection_wrapper_class => "items-list"

assert_select 'input[type=radio]', :count => 2
assert_no_select 'ul'
assert_no_select '.items-list'
end

test 'collection radio uses no class for collection wrapper tag by default' do
with_collection_radio_buttons :user, :active, [true, false], :to_s, :to_s, :collection_wrapper_tag => :ul

assert_select 'ul'
assert_no_select 'ul[class]'
end

test 'collection radio wrap items in a span tag by default' do
with_collection_radio_buttons :user, :active, [true, false], :to_s, :to_s

assert_select 'span input[type=radio][value=true]#user_active_true + label'
assert_select 'span input[type=radio][value=false]#user_active_false + label'
end

test 'collection radio wraps each item in the given item wrapper tag' do
with_collection_radio_buttons :user, :active, [true, false], :to_s, :to_s, :item_wrapper_tag => :li

assert_select 'li input[type=radio]', :count => 2
end

test 'collection radio does not wrap each item when given explicitly falsy value' do
with_collection_radio_buttons :user, :active, [true, false], :to_s, :to_s, :item_wrapper_tag => false

assert_select 'input[type=radio]'
assert_no_select 'span input[type=radio]'
end

test 'collection radio uses the given class for item wrapper tag' do
with_collection_radio_buttons :user, :active, [true, false], :to_s, :to_s,
:item_wrapper_tag => :li, :item_wrapper_class => "inline"

assert_select "li.inline input[type=radio]", :count => 2
end

test 'collection radio uses no class for item wrapper tag when no wrapper tag is given' do
with_collection_radio_buttons :user, :active, [true, false], :to_s, :to_s,
:item_wrapper_tag => nil, :item_wrapper_class => "inline"

assert_select 'input[type=radio]', :count => 2
assert_no_select 'li'
assert_no_select '.inline'
end

test 'collection radio uses no class for item wrapper tag by default' do
with_collection_radio_buttons :user, :active, [true, false], :to_s, :to_s,
:item_wrapper_tag => :li

assert_select "li", :count => 2
assert_no_select "li[class]"
end

test 'collection radio does not wrap input inside the label' do
with_collection_radio_buttons :user, :active, [true, false], :to_s, :to_s

Expand Down Expand Up @@ -308,92 +221,6 @@ def with_collection_check_boxes(*args, &block)
assert_select 'label.collection_check_boxes[for=post_category_ids_2]', 'Category 2'
end

test 'collection check boxeses wraps the collection in the given collection wrapper tag' do
with_collection_check_boxes :user, :active, [true, false], :to_s, :to_s, :collection_wrapper_tag => :ul

assert_select 'ul input[type=checkbox]', :count => 2
end

test 'collection check boxeses does not render any wrapper tag by default' do
with_collection_check_boxes :user, :active, [true, false], :to_s, :to_s

assert_select 'input[type=checkbox]', :count => 2
assert_no_select 'ul'
end

test 'collection check boxeses does not wrap the collection when given falsy values' do
with_collection_check_boxes :user, :active, [true, false], :to_s, :to_s, :collection_wrapper_tag => false

assert_select 'input[type=checkbox]', :count => 2
assert_no_select 'ul'
end

test 'collection check boxeses uses the given class for collection wrapper tag' do
with_collection_check_boxes :user, :active, [true, false], :to_s, :to_s,
:collection_wrapper_tag => :ul, :collection_wrapper_class => "items-list"

assert_select 'ul.items-list input[type=checkbox]', :count => 2
end

test 'collection check boxeses uses no class for collection wrapper tag when no wrapper tag is given' do
with_collection_check_boxes :user, :active, [true, false], :to_s, :to_s,
:collection_wrapper_class => "items-list"

assert_select 'input[type=checkbox]', :count => 2
assert_no_select 'ul'
assert_no_select '.items-list'
end

test 'collection check boxeses uses no class for collection wrapper tag by default' do
with_collection_check_boxes :user, :active, [true, false], :to_s, :to_s, :collection_wrapper_tag => :ul

assert_select 'ul'
assert_no_select 'ul[class]'
end

test 'collection check boxeses wrap items in a span tag by default' do
with_collection_check_boxes :user, :active, [true, false], :to_s, :to_s

assert_select 'span input[type=checkbox]', :count => 2
end

test 'collection check boxeses wraps each item in the given item wrapper tag' do
with_collection_check_boxes :user, :active, [true, false], :to_s, :to_s, :item_wrapper_tag => :li

assert_select 'li input[type=checkbox]', :count => 2
end

test 'collection check boxeses does not wrap each item when given explicitly falsy value' do
with_collection_check_boxes :user, :active, [true, false], :to_s, :to_s, :item_wrapper_tag => false

assert_select 'input[type=checkbox]'
assert_no_select 'span input[type=checkbox]'
end

test 'collection check boxeses uses the given class for item wrapper tag' do
with_collection_check_boxes :user, :active, [true, false], :to_s, :to_s,
:item_wrapper_tag => :li, :item_wrapper_class => "inline"

assert_select "li.inline input[type=checkbox]", :count => 2
end

test 'collection check boxeses uses no class for item wrapper tag when no wrapper tag is given' do
with_collection_check_boxes :user, :active, [true, false], :to_s, :to_s,
:item_wrapper_tag => nil, :item_wrapper_class => "inline"

assert_select 'input[type=checkbox]', :count => 2
assert_no_select 'li'
assert_no_select '.inline'
end

test 'collection check boxeses uses no class for item wrapper tag by default' do
with_collection_check_boxes :user, :active, [true, false], :to_s, :to_s,
:item_wrapper_tag => :li

assert_select "li", :count => 2
assert_no_select "li[class]"
end

test 'collection check boxes does not wrap input inside the label' do
with_collection_check_boxes :user, :active, [true, false], :to_s, :to_s

Expand Down

0 comments on commit b17be29

Please sign in to comment.