From a16789d3d79a403c2caf1f4b5739e67f53f6c1bb Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Jos=C3=A9=20Valim?= Date: Tue, 6 Jul 2010 11:38:35 +0200 Subject: [PATCH] Added error_method to tidy up error messages. --- .../simple_form/templates/simple_form.rb | 3 +++ lib/simple_form.rb | 4 ++++ lib/simple_form/components/errors.rb | 10 +++++++--- test/components/error_test.rb | 17 +++++++++++++---- test/support/models.rb | 17 ++++++++++------- 5 files changed, 37 insertions(+), 14 deletions(-) diff --git a/lib/generators/simple_form/templates/simple_form.rb b/lib/generators/simple_form/templates/simple_form.rb index 263da0542..4200f7446 100644 --- a/lib/generators/simple_form/templates/simple_form.rb +++ b/lib/generators/simple_form/templates/simple_form.rb @@ -11,6 +11,9 @@ # Default tag used on errors. # config.error_tag = :span + # Method used to tidy up errors. + # config.error_method = :first + # You can wrap all inputs in a pre-defined tag. # config.wrapper_tag = :div diff --git a/lib/simple_form.rb b/lib/simple_form.rb index 7770027a6..9f4bfdd61 100644 --- a/lib/simple_form.rb +++ b/lib/simple_form.rb @@ -17,6 +17,10 @@ module SimpleForm mattr_accessor :error_tag @@error_tag = :span + # Method used to tidy up errors. + mattr_accessor :error_method + @@error_method = :first + # Components used by the form builder. mattr_accessor :components @@components = [ :label_input, :hint, :error ] diff --git a/lib/simple_form/components/errors.rb b/lib/simple_form/components/errors.rb index 20b4099ac..77867d95d 100644 --- a/lib/simple_form/components/errors.rb +++ b/lib/simple_form/components/errors.rb @@ -10,7 +10,11 @@ def error_tag end def error_text - errors.to_sentence + errors.send(error_method) + end + + def error_method + options[:error_method] || SimpleForm.error_method end def error_html_options @@ -24,11 +28,11 @@ def errors end def errors_on_attribute - Array(object.errors[attribute_name]) + object.errors[attribute_name] end def errors_on_association - reflection ? Array(object.errors[reflection.name]) : [] + reflection ? object.errors[reflection.name] : [] end end end diff --git a/test/components/error_test.rb b/test/components/error_test.rb index 4ccfe9a46..11a7ff5d5 100644 --- a/test/components/error_test.rb +++ b/test/components/error_test.rb @@ -28,9 +28,18 @@ def with_error_for(object, attribute_name, type, options={}, &block) assert_select 'span.error', "can't be blank" end - test 'error should generate messages for attribute with several errors' do - with_error_for @user, :age, :numeric - assert_select 'span.error', 'is not a number and must be greater than 18' + test 'error should generate messages for attribute with one error when using first' do + swap SimpleForm, :error_method => :first do + with_error_for @user, :age, :numeric + assert_select 'span.error', 'is not a number' + end + end + + test 'error should generate messages for attribute with several errors when using to_sentence' do + swap SimpleForm, :error_method => :to_sentence do + with_error_for @user, :age, :numeric + assert_select 'span.error', 'is not a number and must be greater than 18' + end end test 'error should be able to pass html options' do @@ -39,7 +48,7 @@ def with_error_for(object, attribute_name, type, options={}, &block) end test 'error should find errors on attribute and association' do - with_error_for @user, :company_id, :select, :setup_association => true + with_error_for @user, :company_id, :select, :setup_association => true, :error_method => :to_sentence assert_select 'span.error', 'must be valid and company must be present' end end diff --git a/test/support/models.rb b/test/support/models.rb index 7085ba433..7fdbe400a 100644 --- a/test/support/models.rb +++ b/test/support/models.rb @@ -91,13 +91,16 @@ def self.reflect_on_association(association) end def errors - @errors ||= { - :name => "can't be blank", - :description => "must be longer than 15 characters", - :age => ["is not a number", "must be greater than 18"], - :company => "company must be present", - :company_id => "must be valid" - } + @errors ||= begin + hash = Hash.new { |h,k| h[k] = [] } + hash.merge!( + :name => ["can't be blank"], + :description => ["must be longer than 15 characters"], + :age => ["is not a number", "must be greater than 18"], + :company => ["company must be present"], + :company_id => ["must be valid"] + ) + end end end