From dca86314fc2ac27e9611bc187f247f6ca1996d80 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Jos=C3=A9=20Valim?= Date: Tue, 6 Jul 2010 11:28:23 +0200 Subject: [PATCH] Add required_by_default as option. --- .../simple_form/templates/simple_form.rb | 19 ++++++++++--------- lib/simple_form.rb | 4 ++++ lib/simple_form/inputs/base.rb | 2 +- test/form_builder_test.rb | 8 ++++++++ 4 files changed, 23 insertions(+), 10 deletions(-) diff --git a/lib/generators/simple_form/templates/simple_form.rb b/lib/generators/simple_form/templates/simple_form.rb index 7578dee3..263da054 100644 --- a/lib/generators/simple_form/templates/simple_form.rb +++ b/lib/generators/simple_form/templates/simple_form.rb @@ -1,10 +1,8 @@ # Use this setup block to configure all options available in SimpleForm. SimpleForm.setup do |config| - # Components used by the form builder to generate a complete input. You can - # remove any of them, change the order, or even add your own components in the - # stack. By inheriting your component from SimpleForm::Components::Base you'll - # have some extra helpers for free. + # Components used by the form builder to generate a complete input. You can remove + # any of them, change the order, or even add your own components to the stack. # config.components = [ :label_input, :hint, :error ] # Default tag used on hints. @@ -16,19 +14,22 @@ SimpleForm.setup do |config| # You can wrap all inputs in a pre-defined tag. # config.wrapper_tag = :div - # CSS class to add to all wrapper tags + # CSS class to add to all wrapper tags. # config.wrapper_class = :input - # CSS class to add to the wrapper if the field has errors + # CSS class to add to the wrapper if the field has errors. # config.wrapper_error_class = :fieldWithErrors # How the label text should be generated altogether with the required text. # config.label_text = lambda { |label, required| "#{required} #{label}" } - # Series of attemps to detect a default label method for collection + # Whether attributes are required by default (or not). Default is true. + # config.required_by_default = true + + # Series of attemps to detect a default label method for collection. # config.collection_label_methods = [ :to_label, :name, :title, :to_s ] - # Series of attemps to detect a default value method for collection + # Series of attemps to detect a default value method for collection. # config.collection_value_methods = [ :id, :to_s ] # Collection of methods to detect if a file type was given. @@ -40,6 +41,6 @@ SimpleForm.setup do |config| # Default priority for country inputs. # config.country_priority = nil - # Default size for text inputs + # Default size for text inputs. # config.default_input_size = 50 end diff --git a/lib/simple_form.rb b/lib/simple_form.rb index 85e994bc..7770027a 100644 --- a/lib/simple_form.rb +++ b/lib/simple_form.rb @@ -45,6 +45,10 @@ module SimpleForm mattr_accessor :label_text @@label_text = lambda { |label, required| "#{required} #{label}" } + # Whether attributes are required by default (or not). + mattr_accessor :required_by_default + @@required_by_default = true + # Collection of methods to detect if a file type was given. mattr_accessor :file_methods @@file_methods = [ :mounted_as, :file?, :public_filename ] diff --git a/lib/simple_form/inputs/base.rb b/lib/simple_form/inputs/base.rb index eac2ec5d..a850000f 100644 --- a/lib/simple_form/inputs/base.rb +++ b/lib/simple_form/inputs/base.rb @@ -63,7 +63,7 @@ module SimpleForm end def attribute_required_by_default? - true + SimpleForm.required_by_default end def required_class diff --git a/test/form_builder_test.rb b/test/form_builder_test.rb index 3e1c9ed1..63359190 100644 --- a/test/form_builder_test.rb +++ b/test/form_builder_test.rb @@ -250,6 +250,14 @@ class FormBuilderTest < ActionView::TestCase assert_select 'input.required#user_name' end + test 'builder input should not be required by default when ActiveModel::Validations is not included if option is set to false' do + swap SimpleForm, :required_by_default => false do + with_form_for @user, :name + assert_select 'input.optional#user_name' + end + end + + test 'builder input should allow disabling required when ActiveModel::Validations is not included' do with_form_for @user, :name, :required => false assert_no_select 'input.required'