Merge pull request #1109 from rmm5t/simple_form_for-config-overrides

Allow overriding of default configured form_class
This commit is contained in:
Rafael Mendonça França 2014-11-19 14:23:45 -02:00
commit 3f04084e25
6 changed files with 64 additions and 6 deletions

View File

@ -35,6 +35,7 @@
* Add I18n support to `:include_blank` and `:prompt` when `:translate` is used as value. [@haines](https://github.com/plataformatec/simple_form/pull/616)
* Add support to define custom error messages for the attributes.
* Add support to change the I18n scope to be used in Simple Form. [@nielsbuus](https://github.com/nielsbuus)
* The default form class can now be overridden with `html: { :class }`. [@rmm5t](https://github.com/rmm5t)
### bug fix
* Collection input that uses automatic collection translation properly sets checked values.
@ -45,5 +46,7 @@
## deprecation
* Methods on custom inputs now accept a required argument with the wrapper options.
See https://github.com/plataformatec/simple_form/pull/997 for more information.
* SimpleForm.form_class is deprecated in favor of SimpleForm.default_form_class
See https://github.com/plataformatec/simple_form/pull/1109 for more information.
Please check [v3.0](https://github.com/plataformatec/simple_form/blob/v3.0/CHANGELOG.md) for previous changes.

View File

@ -103,8 +103,9 @@ SimpleForm.setup do |config|
# You can define the class to use on all labels. Default is nil.
# config.label_class = nil
# You can define the class to use on all forms. Default is simple_form.
# config.form_class = :simple_form
# You can define the default class to be used on forms. Can be overriden
# with `html: { :class }`. Defaulting to none.
# config.default_form_class = nil
# You can define which elements should obtain additional classes
# config.generate_additional_classes_for = [:wrapper, :label, :input]

View File

@ -96,10 +96,16 @@ See https://github.com/plataformatec/simple_form/pull/997 for more information.
mattr_accessor :boolean_style
@@boolean_style = :inline
# You can define the class to be used on all forms. Default is simple_form.
# DEPRECATED: You can define the class to be used on all forms. Default is
# simple_form.
mattr_accessor :form_class
@@form_class = :simple_form
# You can define the default class to be used on all forms. Can be overriden
# with `html: { :class }`. Defaults to none.
mattr_accessor :default_form_class
@@default_form_class = nil
# You can define which elements should obtain additional classes.
mattr_accessor :generate_additional_classes_for
@@generate_additional_classes_for = [:wrapper, :label, :input]
@ -249,6 +255,11 @@ See https://github.com/plataformatec/simple_form/pull/997 for more information.
ActiveSupport::Deprecation.warn "[SIMPLE_FORM] SimpleForm.default_input_size= is deprecated and has no effect", caller
end
def self.form_class=(value)
ActiveSupport::Deprecation.warn "[SIMPLE_FORM] SimpleForm.form_class= is deprecated and will be removed in 4.x. Use SimpleForm.default_form_class= instead", caller
@@form_class = value
end
# Default way to setup Simple Form. Run rails generate simple_form:install
# to create a fresh initializer with all configuration values.
def self.setup

View File

@ -16,7 +16,11 @@ module SimpleForm
unless options[:html].key?(:novalidate)
options[:html][:novalidate] = !SimpleForm.browser_validations
end
options[:html][:class] = [SimpleForm.form_class, simple_form_css_class(record, options)].compact.join(" ")
if options[:html].key?(:class)
options[:html][:class] = [SimpleForm.form_class, options[:html][:class]].compact
else
options[:html][:class] = [SimpleForm.form_class, SimpleForm.default_form_class, simple_form_css_class(record, options)].compact
end
with_simple_form_field_error_proc do
form_for(record, options, &block)

View File

@ -13,6 +13,25 @@ class FormHelperTest < ActionView::TestCase
assert_select 'form.simple_form'
end
test 'SimpleForm allows overriding default form class' do
swap SimpleForm, default_form_class: "my_custom_class" do
with_concat_form_for :user, html: { class: "override_class" }
assert_no_select 'form.my_custom_class'
assert_select 'form.override_class'
end
end
# Remove this test when SimpleForm.form_class is removed in 4.x
test 'SimpleForm allows overriding default form class, but not form class' do
ActiveSupport::Deprecation.silence do
swap SimpleForm, form_class: "fixed_class", default_form_class: "my_custom_class" do
with_concat_form_for :user, html: { class: "override_class" }
assert_no_select 'form.my_custom_class'
assert_select 'form.fixed_class.override_class'
end
end
end
test 'SimpleForm uses default browser validations by default' do
with_concat_form_for(:user)
assert_no_select 'form[novalidate]'

View File

@ -217,10 +217,30 @@ class FormBuilderTest < ActionView::TestCase
end
# COMMON OPTIONS
# Remove this test when SimpleForm.form_class is removed in 4.x
test 'builder adds chosen form class' do
swap SimpleForm, form_class: :my_custom_class do
ActiveSupport::Deprecation.silence do
swap SimpleForm, form_class: :my_custom_class do
with_form_for @user, :name
assert_select 'form.my_custom_class'
end
end
end
# Remove this test when SimpleForm.form_class is removed in 4.x
test 'builder adds chosen form class and default form class' do
ActiveSupport::Deprecation.silence do
swap SimpleForm, form_class: "my_custom_class", default_form_class: "my_default_class" do
with_form_for @user, :name
assert_select 'form.my_custom_class.my_default_class'
end
end
end
test 'builder adds default form class' do
swap SimpleForm, default_form_class: "default_class" do
with_form_for @user, :name
assert_select 'form.my_custom_class'
assert_select 'form.default_class'
end
end