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:
parent
f0d1f3a31a
commit
9eef9d1cb1
|
@ -13,10 +13,10 @@ module SimpleForm
|
||||||
end
|
end
|
||||||
|
|
||||||
def input
|
def input
|
||||||
collection = (options[:collection] || self.class.boolean_collection).to_a
|
label_method, value_method = detect_collection_methods
|
||||||
detect_collection_methods(collection, options)
|
|
||||||
@builder.send(:"collection_#{input_type}", attribute_name, collection, options[:value_method],
|
@builder.send(:"collection_#{input_type}", attribute_name, collection,
|
||||||
options[:label_method], input_options, input_html_options)
|
value_method, label_method, input_options, input_html_options)
|
||||||
end
|
end
|
||||||
|
|
||||||
def input_options
|
def input_options
|
||||||
|
@ -27,6 +27,10 @@ module SimpleForm
|
||||||
|
|
||||||
protected
|
protected
|
||||||
|
|
||||||
|
def collection
|
||||||
|
@collection ||= (options.delete(:collection) || self.class.boolean_collection).to_a
|
||||||
|
end
|
||||||
|
|
||||||
# Check if :include_blank must be included by default.
|
# Check if :include_blank must be included by default.
|
||||||
def skip_include_blank?
|
def skip_include_blank?
|
||||||
(options.keys & [:prompt, :include_blank, :default, :selected]).any? ||
|
(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
|
# on default label and value methods that can be configured through
|
||||||
# SimpleForm.collection_label_methods and
|
# SimpleForm.collection_label_methods and
|
||||||
# SimpleForm.collection_value_methods.
|
# SimpleForm.collection_value_methods.
|
||||||
def detect_collection_methods(collection, options)
|
def detect_collection_methods
|
||||||
common_method_for = detect_common_display_methods(collection)
|
label, value = options.delete(:label_method), options.delete(:value_method)
|
||||||
|
|
||||||
options[:label_method] ||= common_method_for[:label]
|
unless label && value
|
||||||
options[:value_method] ||= common_method_for[:value]
|
common_method_for = detect_common_display_methods
|
||||||
|
label ||= common_method_for[:label]
|
||||||
|
value ||= common_method_for[:value]
|
||||||
|
end
|
||||||
|
|
||||||
|
[label, value]
|
||||||
end
|
end
|
||||||
|
|
||||||
def detect_common_display_methods(collection)
|
def detect_common_display_methods
|
||||||
collection_classes = detect_collection_classes(collection)
|
collection_classes = detect_collection_classes
|
||||||
|
|
||||||
if collection_classes.include?(Array)
|
if collection_classes.include?(Array)
|
||||||
{ :label => :first, :value => :last }
|
{ :label => :first, :value => :last }
|
||||||
|
@ -60,13 +69,13 @@ module SimpleForm
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
def detect_collection_classes(collection)
|
def detect_collection_classes
|
||||||
collection.map { |e| e.class }.uniq
|
collection.map { |e| e.class }.uniq
|
||||||
end
|
end
|
||||||
|
|
||||||
def collection_includes_basic_objects?(collection_classes)
|
def collection_includes_basic_objects?(collection_classes)
|
||||||
(collection_classes & [
|
(collection_classes & [
|
||||||
String, Integer, Fixnum, Bignum, Float, NilClass, Symbol, TrueClass, FalseClass
|
String, Integer, Fixnum, Bignum, Float, NilClass, Symbol, TrueClass, FalseClass
|
||||||
]).any?
|
]).any?
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
|
@ -521,6 +521,22 @@ class InputTest < ActionView::TestCase
|
||||||
assert_select 'label.collection_radio', 'Carlos'
|
assert_select 'label.collection_radio', 'Carlos'
|
||||||
end
|
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
|
test 'input should allow overriding label and value method for collections' do
|
||||||
with_input_for @user, :name, :radio,
|
with_input_for @user, :name, :radio,
|
||||||
:collection => ['Jose' , 'Carlos'],
|
:collection => ['Jose' , 'Carlos'],
|
||||||
|
|
Loading…
Reference in New Issue