collection tags accept html attributes as the last element of collection

This commit is contained in:
Vasiliy Ermolovich 2013-06-18 22:55:27 +03:00
parent 0da7b473cb
commit e264a43dfe
6 changed files with 21 additions and 2 deletions

View File

@ -1,6 +1,8 @@
## master ## master
### enhancements ### enhancements
* Collection tags accept html attributes as the last element of collection [@nashby](https://github.com/nashby)
* Change default `:value_method` of collection tags from `:last` to `:second` [@nashby](https://github.com/nashby)
* Support `Proc` object in `:conditions` option of associations [@bradly](https://github.com/bradly) * Support `Proc` object in `:conditions` option of associations [@bradly](https://github.com/bradly)
* `input_field` supports `html5` component [@nashby](https://github.com/nashby) * `input_field` supports `html5` component [@nashby](https://github.com/nashby)

View File

@ -66,7 +66,7 @@ module SimpleForm
collection_translated = translate_collection if collection_classes == [Symbol] collection_translated = translate_collection if collection_classes == [Symbol]
if collection_translated || collection_classes.include?(Array) if collection_translated || collection_classes.include?(Array)
{ label: :first, value: :last } { label: :first, value: :second }
elsif collection_includes_basic_objects?(collection_classes) elsif collection_includes_basic_objects?(collection_classes)
{ label: :to_s, value: :to_s } { label: :to_s, value: :to_s }
else else

View File

@ -11,8 +11,9 @@ module SimpleForm
value = value_for_collection(item, @value_method) value = value_for_collection(item, @value_method)
text = value_for_collection(item, @text_method) text = value_for_collection(item, @text_method)
default_html_options = default_html_options_for_collection(item, value) default_html_options = default_html_options_for_collection(item, value)
additional_html_options = option_html_attributes(item)
rendered_item = yield item, value, text, default_html_options rendered_item = yield item, value, text, default_html_options.merge(additional_html_options)
item_wrapper_tag ? @template_object.content_tag(item_wrapper_tag, rendered_item, class: item_wrapper_class) : rendered_item item_wrapper_tag ? @template_object.content_tag(item_wrapper_tag, rendered_item, class: item_wrapper_class) : rendered_item
end.join.html_safe end.join.html_safe

View File

@ -60,6 +60,11 @@ class CollectionCheckBoxesInputTest < ActionView::TestCase
assert_no_select 'form ul' assert_no_select 'form ul'
end end
test 'input check boxes accepts html options as the last element of collection' do
with_input_for @user, :name, :check_boxes, collection: [['Jose', 'jose', class: 'foo']]
assert_select 'input.foo[type=checkbox][value=jose]'
end
test 'input check boxes wraps the collection in the configured collection wrapper tag' do test 'input check boxes wraps the collection in the configured collection wrapper tag' do
swap SimpleForm, collection_wrapper_tag: :ul do swap SimpleForm, collection_wrapper_tag: :ul do
with_input_for @user, :active, :check_boxes with_input_for @user, :active, :check_boxes

View File

@ -82,6 +82,11 @@ class CollectionRadioButtonsInputTest < ActionView::TestCase
assert_select 'input[type=radio][value=Carlos][checked=checked]' assert_select 'input[type=radio][value=Carlos][checked=checked]'
end end
test 'input should accept html options as the last element of collection' do
with_input_for @user, :name, :radio_buttons, collection: [['Jose', 'jose', class: 'foo']]
assert_select 'input.foo[type=radio][value=jose]'
end
test 'input should allow using a collection with text/value arrays' do test 'input should allow using a collection with text/value arrays' do
with_input_for @user, :name, :radio_buttons, collection: [['Jose', 'jose'], ['Carlos', 'carlos']] with_input_for @user, :name, :radio_buttons, collection: [['Jose', 'jose'], ['Carlos', 'carlos']]
assert_select 'input[type=radio][value=jose]' assert_select 'input[type=radio][value=jose]'

View File

@ -56,6 +56,12 @@ class CollectionSelectInputTest < ActionView::TestCase
assert_select 'select option[selected=selected]', 'Carlos' assert_select 'select option[selected=selected]', 'Carlos'
end end
test 'input should accept html options as the last element of collection' do
with_input_for @user, :name, :select, collection: [['Jose', class: 'foo']]
assert_select 'select.select#user_name'
assert_select 'select option.foo', 'Jose'
end
test 'input should mark the selected value also when using integers' do test 'input should mark the selected value also when using integers' do
@user.age = 18 @user.age = 18
with_input_for @user, :age, :select, collection: 18..60 with_input_for @user, :age, :select, collection: 18..60