diff --git a/CHANGELOG.md b/CHANGELOG.md index 052202d4..9ad2edf1 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -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. diff --git a/lib/generators/simple_form/templates/config/initializers/simple_form.rb b/lib/generators/simple_form/templates/config/initializers/simple_form.rb index fac7026e..d5492e52 100644 --- a/lib/generators/simple_form/templates/config/initializers/simple_form.rb +++ b/lib/generators/simple_form/templates/config/initializers/simple_form.rb @@ -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] diff --git a/lib/simple_form.rb b/lib/simple_form.rb index a5be36e1..68754a48 100644 --- a/lib/simple_form.rb +++ b/lib/simple_form.rb @@ -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 diff --git a/lib/simple_form/action_view_extensions/form_helper.rb b/lib/simple_form/action_view_extensions/form_helper.rb index e5bf66b0..17fa4d85 100644 --- a/lib/simple_form/action_view_extensions/form_helper.rb +++ b/lib/simple_form/action_view_extensions/form_helper.rb @@ -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) diff --git a/test/action_view_extensions/form_helper_test.rb b/test/action_view_extensions/form_helper_test.rb index 25b444a2..52b515b1 100644 --- a/test/action_view_extensions/form_helper_test.rb +++ b/test/action_view_extensions/form_helper_test.rb @@ -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]' diff --git a/test/form_builder/general_test.rb b/test/form_builder/general_test.rb index 7022ebeb..06b11f29 100644 --- a/test/form_builder/general_test.rb +++ b/test/form_builder/general_test.rb @@ -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