diff --git a/lib/generators/simple_form/templates/config/initializers/simple_form.rb.tt b/lib/generators/simple_form/templates/config/initializers/simple_form.rb.tt index dcf7e9087..d9c787a56 100644 --- a/lib/generators/simple_form/templates/config/initializers/simple_form.rb.tt +++ b/lib/generators/simple_form/templates/config/initializers/simple_form.rb.tt @@ -138,6 +138,9 @@ SimpleForm.setup do |config| # You can define the class to use on all forms. Default is simple_form. # config.form_class = :simple_form + # You can define which elements should obtain additional classes + # config.generate_additional_classes_for = [:wrapper, :label, :input] + # Whether attributes are required by default (or not). Default is true. # config.required_by_default = true diff --git a/lib/simple_form.rb b/lib/simple_form.rb index 28eb7b38f..19b3d9001 100644 --- a/lib/simple_form.rb +++ b/lib/simple_form.rb @@ -73,6 +73,10 @@ module SimpleForm mattr_accessor :form_class @@form_class = :simple_form + # You can define which elements should obtain additional classes + mattr_accessor :generate_additional_classes_for + @@generate_additional_classes_for = [:wrapper, :label, :input] + # Whether attributes are required by default (or not). mattr_accessor :required_by_default @@required_by_default = true diff --git a/lib/simple_form/components/labels.rb b/lib/simple_form/components/labels.rb index 85d529300..1bc68206a 100644 --- a/lib/simple_form/components/labels.rb +++ b/lib/simple_form/components/labels.rb @@ -38,7 +38,9 @@ def label_target end def label_html_options - label_options = html_options_for(:label, [input_type, required_class, SimpleForm.label_class].compact) + label_html_classes = SimpleForm.generate_additional_classes_for.include?(:label) ? [input_type, required_class, SimpleForm.label_class].compact : [] + + label_options = html_options_for(:label, label_html_classes) label_options[:for] = options[:input_html][:id] if options.key?(:input_html) && options[:input_html].key?(:id) label_options end diff --git a/lib/simple_form/inputs/base.rb b/lib/simple_form/inputs/base.rb index 985abfb5c..3ae9a7fe4 100644 --- a/lib/simple_form/inputs/base.rb +++ b/lib/simple_form/inputs/base.rb @@ -59,7 +59,8 @@ def initialize(builder, attribute_name, column, input_type, options = {}) # Notice that html_options_for receives a reference to input_html_classes. # This means that classes added dynamically to input_html_classes will # still propagate to input_html_options. - @html_classes = [input_type, required_class, readonly_class, disabled_class].compact + @html_classes = SimpleForm.generate_additional_classes_for.include?(:input) ? [input_type, required_class, readonly_class, disabled_class].compact : [] + @input_html_classes = @html_classes.dup @input_html_options = html_options_for(:input, input_html_classes).tap do |o| o[:readonly] = true if has_readonly? @@ -100,7 +101,7 @@ def html_options_for(namespace, css_classes) html_options = options[:"#{namespace}_html"] || {} css_classes << html_options[:class] if html_options.key?(:class) html_options[:class] = css_classes - html_options + html_options.delete_if{|key, value| value.blank?} end # Lookup translations for the given namespace using I18n, based on object name, diff --git a/lib/simple_form/wrappers/root.rb b/lib/simple_form/wrappers/root.rb index e1886eb22..3f6336a0b 100644 --- a/lib/simple_form/wrappers/root.rb +++ b/lib/simple_form/wrappers/root.rb @@ -24,7 +24,7 @@ def find(name) def html_classes(input, options) css = options[:wrapper_class] ? Array.wrap(options[:wrapper_class]) : @defaults[:class] - css += input.html_classes + css += input.html_classes if SimpleForm.generate_additional_classes_for.include?(:wrapper) css << (options[:wrapper_error_class] || @defaults[:error_class]) if input.has_errors? css << (options[:wrapper_hint_class] || @defaults[:hint_class]) if input.has_hint? css diff --git a/test/components/label_test.rb b/test/components/label_test.rb index 1b7449018..1548a509d 100644 --- a/test/components/label_test.rb +++ b/test/components/label_test.rb @@ -166,6 +166,21 @@ def @controller.action_name; nil; end assert_select 'label.datetime' end + test 'label should not have css class from type when generate_additional_classes_for does not include :label' do + swap SimpleForm, :generate_additional_classes_for => [:wrapper, :input] do + with_label_for @user, :name, :string + assert_no_select 'label.string' + with_label_for @user, :description, :text + assert_no_select 'label.text' + with_label_for @user, :age, :integer + assert_no_select 'label.integer' + with_label_for @user, :born_at, :date + assert_no_select 'label.date' + with_label_for @user, :created_at, :datetime + assert_no_select 'label.datetime' + end + end + test 'label should obtain required from ActiveModel::Validations when it is included' do with_label_for @validating_user, :name, :string assert_select 'label.required' @@ -173,6 +188,15 @@ def @controller.action_name; nil; end assert_select 'label.optional' end + test 'label should not obtain required from ActiveModel::Validations when generate_additional_classes_for does not include :label' do + swap SimpleForm, :generate_additional_classes_for => [:wrapper, :input] do + with_label_for @validating_user, :name, :string + assert_no_select 'label.required' + with_label_for @validating_user, :status, :string + assert_no_select 'label.optional' + end + end + test 'label should allow overriding required when ActiveModel::Validations is included' do with_label_for @validating_user, :name, :string, :required => false assert_select 'label.optional' diff --git a/test/form_builder/general_test.rb b/test/form_builder/general_test.rb index 032b93d57..f15d297f0 100644 --- a/test/form_builder/general_test.rb +++ b/test/form_builder/general_test.rb @@ -38,6 +38,14 @@ def with_custom_form_for(object, *args, &block) end end + test 'builder should allow to skip input_type class' do + swap SimpleForm, :generate_additional_classes_for => [:label, :wrapper] do + with_form_for @user, :post_count + assert_no_select "form input#user_post_count.integer" + assert_select "form input#user_post_count" + end + end + test 'builder should allow adding custom input mappings for integer input types' do swap SimpleForm, :input_mappings => { /lock_version/ => :hidden } do with_form_for @user, :lock_version