Detect label and value automatically.
This commit is contained in:
parent
af89e090f2
commit
d9cc0a3f94
|
@ -5,7 +5,6 @@
|
||||||
* Sample CSS
|
* Sample CSS
|
||||||
* Test forms with non objects
|
* Test forms with non objects
|
||||||
* Get default string options from column definition
|
* Get default string options from column definition
|
||||||
* Detect label and values automatically
|
|
||||||
* Add support to default :prompt methods on datetime inputs
|
* Add support to default :prompt methods on datetime inputs
|
||||||
* Add support to default label method
|
* Add support to default label method
|
||||||
* Add wrapper support
|
* Add wrapper support
|
||||||
|
|
|
@ -18,4 +18,12 @@ module SimpleForm
|
||||||
SimpleForm::Components::Label, SimpleForm::Components::Input,
|
SimpleForm::Components::Label, SimpleForm::Components::Input,
|
||||||
SimpleForm::Components::Hint, SimpleForm::Components::Error
|
SimpleForm::Components::Hint, SimpleForm::Components::Error
|
||||||
]
|
]
|
||||||
|
|
||||||
|
# Series of attemps to detect a default label method for collection
|
||||||
|
mattr_accessor :collection_label_methods
|
||||||
|
@@collection_label_methods = [ :name, :title, :to_s ]
|
||||||
|
|
||||||
|
# Series of attemps to detect a default value method for collection
|
||||||
|
mattr_accessor :collection_value_methods
|
||||||
|
@@collection_value_methods = [ :id, :to_s ]
|
||||||
end
|
end
|
|
@ -57,19 +57,19 @@ module SimpleForm
|
||||||
end
|
end
|
||||||
|
|
||||||
def detect_collection_methods(collection, options)
|
def detect_collection_methods(collection, options)
|
||||||
case collection.first
|
sample = collection.first || collection.last
|
||||||
|
|
||||||
|
case sample
|
||||||
when Array
|
when Array
|
||||||
label, value = :first, :last
|
label, value = :first, :last
|
||||||
when Integer
|
when Integer
|
||||||
value = :to_i
|
label, value = :to_s, :to_i
|
||||||
when String
|
when String, NilClass
|
||||||
# Do nothing ...
|
label, value = :to_s, :to_s
|
||||||
else
|
|
||||||
# TODO Implement detection logic
|
|
||||||
end
|
end
|
||||||
|
|
||||||
options[:label_method] ||= label || :to_s
|
options[:label_method] ||= label || SimpleForm.collection_label_methods.find { |m| sample.respond_to?(m) }
|
||||||
options[:value_method] ||= value || :to_s
|
options[:value_method] ||= value || SimpleForm.collection_value_methods.find { |m| sample.respond_to?(m) }
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
|
@ -190,6 +190,13 @@ class InputTest < ActionView::TestCase
|
||||||
assert_no_select 'select option[value=]', ""
|
assert_no_select 'select option[value=]', ""
|
||||||
end
|
end
|
||||||
|
|
||||||
|
test 'input should detect label and value on collections' do
|
||||||
|
users = [ setup_new_user(:id => 1, :name => "Jose"), setup_new_user(:id => 2, :name => "Carlos") ]
|
||||||
|
with_input_for :description, :select, :collection => users
|
||||||
|
assert_select 'select option[value=1]', 'Jose'
|
||||||
|
assert_select 'select option[value=2]', 'Carlos'
|
||||||
|
end
|
||||||
|
|
||||||
test 'input should allow overriding collection for radio types' do
|
test 'input should allow overriding collection for radio types' do
|
||||||
with_input_for :name, :radio, :collection => ['Jose', 'Carlos']
|
with_input_for :name, :radio, :collection => ['Jose', 'Carlos']
|
||||||
assert_select 'input[type=radio][value=Jose]'
|
assert_select 'input[type=radio][value=Jose]'
|
||||||
|
|
|
@ -10,9 +10,11 @@ class Column
|
||||||
end
|
end
|
||||||
|
|
||||||
class User < OpenStruct
|
class User < OpenStruct
|
||||||
|
attr_reader :id
|
||||||
|
|
||||||
def id
|
def initialize(attributes={})
|
||||||
1
|
@id = attributes.delete(:id)
|
||||||
|
super
|
||||||
end
|
end
|
||||||
|
|
||||||
def new_record?
|
def new_record?
|
||||||
|
|
|
@ -32,12 +32,13 @@ class ActionView::TestCase
|
||||||
@response = MockResponse.new(self)
|
@response = MockResponse.new(self)
|
||||||
end
|
end
|
||||||
|
|
||||||
def setup_new_user
|
def setup_new_user(options={})
|
||||||
@user = User.new(
|
@user = User.new({
|
||||||
|
:id => 1,
|
||||||
:name => 'New in Simple Form!',
|
:name => 'New in Simple Form!',
|
||||||
:description => 'Hello!',
|
:description => 'Hello!',
|
||||||
:created_at => Time.now
|
:created_at => Time.now
|
||||||
)
|
}.merge(options))
|
||||||
end
|
end
|
||||||
|
|
||||||
def protect_against_forgery?
|
def protect_against_forgery?
|
||||||
|
|
Loading…
Reference in New Issue