Skip to content

Commit

Permalink
Merge pull request #1109 from rmm5t/simple_form_for-config-overrides
Browse files Browse the repository at this point in the history
Allow overriding of default configured form_class
  • Loading branch information
Rafael Mendonça França committed Nov 19, 2014
2 parents 4a0f63d + 2e61a54 commit 3f04084
Show file tree
Hide file tree
Showing 6 changed files with 64 additions and 6 deletions.
3 changes: 3 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand All @@ -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.
Original file line number Diff line number Diff line change
Expand Up @@ -103,8 +103,9 @@
# 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]
Expand Down
13 changes: 12 additions & 1 deletion lib/simple_form.rb
Original file line number Diff line number Diff line change
Expand Up @@ -96,10 +96,16 @@ def self.configured? #:nodoc:
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]
Expand Down Expand Up @@ -249,6 +255,11 @@ def self.default_input_size=(*)
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
Expand Down
6 changes: 5 additions & 1 deletion lib/simple_form/action_view_extensions/form_helper.rb
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,11 @@ def simple_form_for(record, options = {}, &block)
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)
Expand Down
19 changes: 19 additions & 0 deletions test/action_view_extensions/form_helper_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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]'
Expand Down
24 changes: 22 additions & 2 deletions test/form_builder/general_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -217,10 +217,30 @@ def with_custom_form_for(object, *args, &block)
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

Expand Down

0 comments on commit 3f04084

Please sign in to comment.