diff --git a/lib/bootstrap_form/form_builder.rb b/lib/bootstrap_form/form_builder.rb index 3f4a9c7a1..0b54b8dd4 100644 --- a/lib/bootstrap_form/form_builder.rb +++ b/lib/bootstrap_form/form_builder.rb @@ -363,17 +363,18 @@ def inputs_collection(name, collection, value, text, options = {}, &block) inputs = "" collection.each do |obj| - input_options = options.merge(label: obj.send(text)) + input_options = options.merge(label: text.respond_to?(:call) ? text.call(obj) : obj.send(text)) + input_value = value.respond_to?(:call) ? value.call(obj) : obj.send(value) if checked = input_options[:checked] - input_options[:checked] = checked == obj.send(value) || - checked.try(:include?, obj.send(value)) || - checked == obj || - checked.try(:include?, obj) + input_options[:checked] = checked == input_value || + Array(checked).try(:include?, input_value) || + checked == obj || + Array(checked).try(:include?, obj) end input_options.delete(:class) - inputs << block.call(name, obj.send(value), input_options) + inputs << block.call(name, input_value, input_options) end inputs.html_safe diff --git a/test/bootstrap_checkbox_test.rb b/test/bootstrap_checkbox_test.rb index c2631f5c2..e15f43136 100644 --- a/test/bootstrap_checkbox_test.rb +++ b/test/bootstrap_checkbox_test.rb @@ -88,4 +88,50 @@ def setup assert_equal expected, @builder.collection_check_boxes(:misc, collection, :id, :street, checked: [1, 2]) assert_equal expected, @builder.collection_check_boxes(:misc, collection, :id, :street, checked: collection) end + + test 'collection_check_boxes renders multiple checkboxes with labels defined by Proc :text_method correctly' do + collection = [Address.new(id: 1, street: 'Foo'), Address.new(id: 2, street: 'Bar')] + expected = %{
} + + assert_equal expected, @builder.collection_check_boxes(:misc, collection, :id, Proc.new { |a| a.street.reverse }) + end + + test 'collection_check_boxes renders multiple checkboxes with values defined by Proc :value_method correctly' do + collection = [Address.new(id: 1, street: 'Foo'), Address.new(id: 2, street: 'Bar')] + expected = %{
} + + assert_equal expected, @builder.collection_check_boxes(:misc, collection, Proc.new { |a| "address_#{a.id}" }, :street) + end + + test 'collection_check_boxes renders multiple checkboxes with labels defined by lambda :text_method correctly' do + collection = [Address.new(id: 1, street: 'Foo'), Address.new(id: 2, street: 'Bar')] + expected = %{
} + + assert_equal expected, @builder.collection_check_boxes(:misc, collection, :id, lambda { |a| a.street.reverse }) + end + + test 'collection_check_boxes renders multiple checkboxes with values defined by lambda :value_method correctly' do + collection = [Address.new(id: 1, street: 'Foo'), Address.new(id: 2, street: 'Bar')] + expected = %{
} + + assert_equal expected, @builder.collection_check_boxes(:misc, collection, lambda { |a| "address_#{a.id}" }, :street) + end + + test 'collection_check_boxes renders with checked option correctly with Proc :value_method' do + collection = [Address.new(id: 1, street: 'Foo'), Address.new(id: 2, street: 'Bar')] + expected = %{
} + + assert_equal expected, @builder.collection_check_boxes(:misc, collection, Proc.new { |a| "address_#{a.id}" }, :street, checked: "address_1") + assert_equal expected, @builder.collection_check_boxes(:misc, collection, Proc.new { |a| "address_#{a.id}" }, :street, checked: collection.first) + end + + test 'collection_check_boxes renders with multiple checked options correctly with lambda :value_method' do + collection = [Address.new(id: 1, street: 'Foo'), Address.new(id: 2, street: 'Bar')] + expected = %{
} + + assert_equal expected, @builder.collection_check_boxes(:misc, collection, lambda { |a| "address_#{a.id}" }, :street, checked: ["address_1", "address_2"]) + assert_equal expected, @builder.collection_check_boxes(:misc, collection, lambda { |a| "address_#{a.id}" }, :street, checked: collection) + end + + end diff --git a/test/bootstrap_radio_button_test.rb b/test/bootstrap_radio_button_test.rb index 2c928dec2..a1204c879 100644 --- a/test/bootstrap_radio_button_test.rb +++ b/test/bootstrap_radio_button_test.rb @@ -64,4 +64,61 @@ def setup assert_equal expected, @builder.collection_radio_buttons(:misc, collection, :id, :street, checked: 1) end + + test 'collection_radio_buttons renders label defined by Proc correctly' do + collection = [Address.new(id: 1, street: 'Foobar')] + expected = %{
With a help!
} + + assert_equal expected, @builder.collection_radio_buttons(:misc, collection, :id, Proc.new { |a| a.street.reverse }, label: 'This is a radio button collection', help: 'With a help!') + end + + test 'collection_radio_buttons renders value defined by Proc correctly' do + collection = [Address.new(id: 1, street: 'Foobar')] + expected = %{
With a help!
} + + assert_equal expected, @builder.collection_radio_buttons(:misc, collection, Proc.new { |a| "address_#{a.id}" }, :street, label: 'This is a radio button collection', help: 'With a help!') + end + + test 'collection_radio_buttons renders multiple radios with label defined by Proc correctly' do + collection = [Address.new(id: 1, street: 'Foo'), Address.new(id: 2, street: 'Bar')] + expected = %{
} + + assert_equal expected, @builder.collection_radio_buttons(:misc, collection, :id, Proc.new { |a| a.street.reverse }) + end + + test 'collection_radio_buttons renders multiple radios with value defined by Proc correctly' do + collection = [Address.new(id: 1, street: 'Foo'), Address.new(id: 2, street: 'Bar')] + expected = %{
} + + assert_equal expected, @builder.collection_radio_buttons(:misc, collection, Proc.new { |a| "address_#{a.id}" }, :street) + end + + test 'collection_radio_buttons renders label defined by lambda correctly' do + collection = [Address.new(id: 1, street: 'Foobar')] + expected = %{
With a help!
} + + assert_equal expected, @builder.collection_radio_buttons(:misc, collection, :id, lambda { |a| a.street.reverse }, label: 'This is a radio button collection', help: 'With a help!') + end + + test 'collection_radio_buttons renders value defined by lambda correctly' do + collection = [Address.new(id: 1, street: 'Foobar')] + expected = %{
With a help!
} + + assert_equal expected, @builder.collection_radio_buttons(:misc, collection, lambda { |a| "address_#{a.id}" }, :street, label: 'This is a radio button collection', help: 'With a help!') + end + + test 'collection_radio_buttons renders multiple radios with label defined by lambda correctly' do + collection = [Address.new(id: 1, street: 'Foo'), Address.new(id: 2, street: 'Bar')] + expected = %{
} + + assert_equal expected, @builder.collection_radio_buttons(:misc, collection, :id, lambda { |a| a.street.reverse }) + end + + test 'collection_radio_buttons renders multiple radios with value defined by lambda correctly' do + collection = [Address.new(id: 1, street: 'Foo'), Address.new(id: 2, street: 'Bar')] + expected = %{
} + + assert_equal expected, @builder.collection_radio_buttons(:misc, collection, lambda { |a| "address_#{a.id}" }, :street) + end + end