Refactor a bit collection input and ensure only label and value methods can be passed

There is no need to keep passing the collection as an arg to the methods,
just use it as an internal method in collection input.
This commit is contained in:
Carlos Antonio da Silva 2010-12-07 23:55:10 -02:00
parent f0d1f3a31a
commit 9eef9d1cb1
2 changed files with 37 additions and 12 deletions

View File

@ -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

View File

@ -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'],