From d959016bbff407286331f67bf564cddd24460e77 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Micha=C5=82=20Krzyz=CC=87anowski?= Date: Mon, 13 Feb 2012 23:22:49 +0100 Subject: [PATCH] added ability to disable automatically added classes to div/label/input --- lib/simple_form.rb | 28 ++++++++++++++++++++++++++++ lib/simple_form/components/labels.rb | 6 +++++- lib/simple_form/inputs/base.rb | 8 +++++++- lib/simple_form/wrappers/root.rb | 2 +- test/components/label_test.rb | 24 ++++++++++++++++++++++++ test/form_builder/general_test.rb | 8 ++++++++ 6 files changed, 73 insertions(+), 3 deletions(-) diff --git a/lib/simple_form.rb b/lib/simple_form.rb index 74edfd90..eaa7f2ca 100644 --- a/lib/simple_form.rb +++ b/lib/simple_form.rb @@ -73,6 +73,34 @@ module SimpleForm mattr_accessor :form_class @@form_class = :simple_form + # Add input_type class to input. Default is true. + mattr_accessor :add_input_type_class_to_input + @@add_input_type_class_to_input = true + + # Add required class to input. Default is true. + mattr_accessor :add_required_class_to_input + @@add_required_class_to_input = true + + # Add readonly class to input. Defualt is true. + mattr_accessor :add_readonly_class_to_input + @@add_readonly_class_to_input = true + + # Add disabled class to input. Default is true. + mattr_accessor :add_disabled_class_to_input + @@add_disabled_class_to_input = true + + # Add input_type class to label. Default is true. + mattr_accessor :add_input_type_to_label + @@add_input_type_to_label = true + + # Add required class to label. Default is true. + mattr_accessor :add_required_class_to_label + @@add_required_class_to_label = true + + # Add input classes to wrapper. Default is true. + mattr_accessor :add_input_classes_to_wrapper + @@add_input_classes_to_wrapper = true + # 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 85d52930..d0576b14 100644 --- a/lib/simple_form/components/labels.rb +++ b/lib/simple_form/components/labels.rb @@ -38,7 +38,11 @@ module SimpleForm end def label_html_options - label_options = html_options_for(:label, [input_type, required_class, SimpleForm.label_class].compact) + @label_html_classes = [input_type, required_class, SimpleForm.label_class].compact.reject do |klass| + klass == (input_type unless SimpleForm.add_input_type_to_label) || \ + klass == (required_class unless SimpleForm.add_required_class_to_label) + end + 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 985abfb5..372a09f6 100644 --- a/lib/simple_form/inputs/base.rb +++ b/lib/simple_form/inputs/base.rb @@ -59,7 +59,13 @@ module SimpleForm # 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 = [input_type, required_class, readonly_class, disabled_class].compact.reject do |klass| + klass == (input_type unless SimpleForm.add_input_type_class_to_input) || \ + klass == (required_class unless SimpleForm.add_required_class_to_input) || \ + klass == (readonly_class unless SimpleForm.add_readonly_class_to_input) || \ + klass == (disabled_class unless SimpleForm.add_disabled_class_to_input) + end + @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? diff --git a/lib/simple_form/wrappers/root.rb b/lib/simple_form/wrappers/root.rb index e1886eb2..3a29ff0f 100644 --- a/lib/simple_form/wrappers/root.rb +++ b/lib/simple_form/wrappers/root.rb @@ -24,7 +24,7 @@ module SimpleForm 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.add_input_classes_to_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 1b744901..41ea6490 100644 --- a/test/components/label_test.rb +++ b/test/components/label_test.rb @@ -166,6 +166,21 @@ class IsolatedLabelTest < ActionView::TestCase assert_select 'label.datetime' end + test 'label should not have css class from type if add_input_type_to_label if false' do + swap SimpleForm, :add_input_type_to_label => false 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 @@ class IsolatedLabelTest < ActionView::TestCase assert_select 'label.optional' end + test 'label should not obtain required from ActiveModel::Validations when add_required_class_to_label is false' do + swap SimpleForm, :add_required_class_to_label => false 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 032b93d5..d520381a 100644 --- a/test/form_builder/general_test.rb +++ b/test/form_builder/general_test.rb @@ -38,6 +38,14 @@ class FormBuilderTest < ActionView::TestCase end end + test 'builder should allow to skip input_type class' do + swap SimpleForm, :add_input_type_class_to_input => false 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