diff --git a/lib/simple_form/inputs/collection_input.rb b/lib/simple_form/inputs/collection_input.rb index 8b08139d..b0b600f7 100644 --- a/lib/simple_form/inputs/collection_input.rb +++ b/lib/simple_form/inputs/collection_input.rb @@ -13,10 +13,10 @@ module SimpleForm end def input - collection = (options[:collection] || self.class.boolean_collection).to_a - detect_collection_methods(collection, options) - @builder.send(:"collection_#{input_type}", attribute_name, collection, options[:value_method], - options[:label_method], input_options, input_html_options) + label_method, value_method = detect_collection_methods + + @builder.send(:"collection_#{input_type}", attribute_name, collection, + value_method, label_method, input_options, input_html_options) end def input_options @@ -27,6 +27,10 @@ module SimpleForm protected + def collection + @collection ||= (options.delete(:collection) || self.class.boolean_collection).to_a + end + # Check if :include_blank must be included by default. def skip_include_blank? (options.keys & [:prompt, :include_blank, :default, :selected]).any? || @@ -38,15 +42,20 @@ module SimpleForm # on default label and value methods that can be configured through # SimpleForm.collection_label_methods and # SimpleForm.collection_value_methods. - def detect_collection_methods(collection, options) - common_method_for = detect_common_display_methods(collection) + def detect_collection_methods + label, value = options.delete(:label_method), options.delete(:value_method) - options[:label_method] ||= common_method_for[:label] - options[:value_method] ||= common_method_for[:value] + unless label && value + common_method_for = detect_common_display_methods + label ||= common_method_for[:label] + value ||= common_method_for[:value] + end + + [label, value] end - def detect_common_display_methods(collection) - collection_classes = detect_collection_classes(collection) + def detect_common_display_methods + collection_classes = detect_collection_classes if collection_classes.include?(Array) { :label => :first, :value => :last } @@ -60,13 +69,13 @@ module SimpleForm end end - def detect_collection_classes(collection) + def detect_collection_classes collection.map { |e| e.class }.uniq end def collection_includes_basic_objects?(collection_classes) (collection_classes & [ - String, Integer, Fixnum, Bignum, Float, NilClass, Symbol, TrueClass, FalseClass + String, Integer, Fixnum, Bignum, Float, NilClass, Symbol, TrueClass, FalseClass ]).any? end end diff --git a/test/inputs_test.rb b/test/inputs_test.rb index b39c2358..6f30fd7b 100644 --- a/test/inputs_test.rb +++ b/test/inputs_test.rb @@ -521,6 +521,22 @@ class InputTest < ActionView::TestCase assert_select 'label.collection_radio', 'Carlos' end + test 'input should allow overriding only label method for collections' do + with_input_for @user, :name, :radio, + :collection => ['Jose' , 'Carlos'], + :label_method => :upcase + assert_select 'label.collection_radio', 'JOSE' + assert_select 'label.collection_radio', 'CARLOS' + end + + test 'input should allow overriding only value method for collections' do + with_input_for @user, :name, :radio, + :collection => ['Jose' , 'Carlos'], + :value_method => :upcase + assert_select 'input[type=radio][value=JOSE]' + assert_select 'input[type=radio][value=CARLOS]' + end + test 'input should allow overriding label and value method for collections' do with_input_for @user, :name, :radio, :collection => ['Jose' , 'Carlos'],