Implements defaults options for simple_form_for.
Closes #355 with help from @hugobarauna and @rafaelfranca
This commit is contained in:
parent
ca61a2860d
commit
c04f719489
|
@ -4,6 +4,7 @@
|
|||
* Add `button_class` configuration to change the class of buttons (github.com/sryche)
|
||||
* Add `disabled` class to a disabled input
|
||||
* Generate configuration file with `browser_validations` disabled
|
||||
* `simple_form_for` allows default options for its inputs (:defaults => {})
|
||||
|
||||
* bug fix
|
||||
* Fallback to default label when block is provided (github.com/pivotal-casebook)
|
||||
|
|
13
README.md
13
README.md
|
@ -97,6 +97,17 @@ It is also possible to pass any html attribute straight to the input, by using t
|
|||
<% end %>
|
||||
```
|
||||
|
||||
If you want to pass the same :input_html to all inputs in the form (for example, a default class), you can use the :defaults option in `simple_form_for`. Specific options in :input_html will overwrite the defaults:
|
||||
|
||||
```erb
|
||||
<%= simple_form_for @user, :defaults => { :class => 'default_class' } do |f| %>
|
||||
<%= f.input :username, :input_html => { :class => 'special' } %>
|
||||
<%= f.input :password, :input_html => { :maxlength => 20 } %>
|
||||
<%= f.input :remember_me, :input_html => { :value => '1' } %>
|
||||
<%= f.button :submit %>
|
||||
<% end %>
|
||||
```
|
||||
|
||||
Since simple_form generates a wrapper div around your label and input by default, you can pass any html attribute to that wrapper as well using the :wrapper_html option, like so:
|
||||
|
||||
```erb
|
||||
|
@ -547,4 +558,4 @@ SimpleForm has several configuration values. You can read and change them in the
|
|||
|
||||
## License
|
||||
|
||||
MIT License. Copyright 2011 Plataforma Tecnologia. http://blog.plataformatec.com.br
|
||||
MIT License. Copyright 2011 Plataforma Tecnologia. http://blog.plataformatec.com.br
|
||||
|
|
|
@ -28,6 +28,7 @@ module SimpleForm
|
|||
|
||||
def initialize(*) #:nodoc:
|
||||
super
|
||||
@defaults = options[:defaults]
|
||||
@wrapper = SimpleForm.wrapper(options[:wrapper] || :default)
|
||||
end
|
||||
|
||||
|
@ -97,6 +98,8 @@ module SimpleForm
|
|||
# given SimpleForm.time_zone_priority and SimpleForm.country_priority are used respectivelly.
|
||||
#
|
||||
def input(attribute_name, options={}, &block)
|
||||
options = @defaults.deep_merge(options) if @defaults
|
||||
|
||||
chosen =
|
||||
if name = options[:wrapper]
|
||||
name.respond_to?(:render) ? name : SimpleForm.wrapper(name)
|
||||
|
|
|
@ -259,6 +259,29 @@ class FormBuilderTest < ActionView::TestCase
|
|||
end
|
||||
end
|
||||
|
||||
# DEFAULT OPTIONS
|
||||
test 'builder should receive a default argument and pass it to the inputs' do
|
||||
concat(simple_form_for(@user, :defaults => { :input_html => { :class => 'default_class' } }) do |f|
|
||||
f.input :name
|
||||
end)
|
||||
assert_select 'input.default_class'
|
||||
end
|
||||
|
||||
test 'builder should receive a default argument and pass it to the inputs, respecting the specific options' do
|
||||
concat(simple_form_for(@user, :defaults => { :input_html => { :class => 'default_class' } }) do |f|
|
||||
f.input :name, :input_html => { :id => 'specific_id' }
|
||||
end)
|
||||
assert_select 'input.default_class#specific_id'
|
||||
end
|
||||
|
||||
test 'builder should receive a default argument and pass it to the inputs, overwriting the defaults with specific options' do
|
||||
concat(simple_form_for(@user, :defaults => { :input_html => { :class => 'default_class', :id => 'default_id' } }) do |f|
|
||||
f.input :name, :input_html => { :id => 'specific_id' }
|
||||
end)
|
||||
assert_select 'input.default_class#specific_id'
|
||||
end
|
||||
|
||||
|
||||
# WITHOUT OBJECT
|
||||
test 'builder should generate properly when object is not present' do
|
||||
with_form_for :project, :name
|
||||
|
|
Loading…
Reference in New Issue