From bd7942b8dca9ab796f53d76bee80153a623a22c8 Mon Sep 17 00:00:00 2001 From: Joenio Costa Date: Thu, 12 May 2011 14:13:38 -0300 Subject: [PATCH] Show a more descriptive message to user when not fill the birthdate properly --- app/models/person.rb | 2 ++ features/edit_profile.feature | 57 +++++++++++++++++++++++++++++++++++++++++++++++++++++++++ test/unit/person_test.rb | 11 +++++++++++ vendor/plugins/validates_multiparameter_assignments/MIT-LICENSE | 20 ++++++++++++++++++++ vendor/plugins/validates_multiparameter_assignments/README | 17 +++++++++++++++++ vendor/plugins/validates_multiparameter_assignments/init.rb | 1 + vendor/plugins/validates_multiparameter_assignments/lib/validates_multiparameter_assignments.rb | 34 ++++++++++++++++++++++++++++++++++ 7 files changed, 142 insertions(+), 0 deletions(-) create mode 100644 features/edit_profile.feature create mode 100644 vendor/plugins/validates_multiparameter_assignments/MIT-LICENSE create mode 100644 vendor/plugins/validates_multiparameter_assignments/README create mode 100644 vendor/plugins/validates_multiparameter_assignments/init.rb create mode 100644 vendor/plugins/validates_multiparameter_assignments/lib/validates_multiparameter_assignments.rb diff --git a/app/models/person.rb b/app/models/person.rb index 64ef15c..d8b48b9 100644 --- a/app/models/person.rb +++ b/app/models/person.rb @@ -116,6 +116,8 @@ class Person < Profile description ] + validates_multiparameter_assignments + def self.fields FIELDS end diff --git a/features/edit_profile.feature b/features/edit_profile.feature new file mode 100644 index 0000000..8df1446 --- /dev/null +++ b/features/edit_profile.feature @@ -0,0 +1,57 @@ +Feature: edit profile + + Background: + Given the following users + | login | + | joao | + Given I am logged in as "joao" + + Scenario: Warn about invalid birth date when active + Given the following person fields are active fields + | display_name | + | birth_date | + When I go to joao's control panel + And I follow "Profile Info and settings" + And I select "November" + And I select "15" + And I press "Save" + Then I should see "Birth date is invalid" + And I should not see "Birth date is mandatory" + + Scenario: Warn about invalid birth date when required + Given the following person fields are required fields + | display_name | + | birth_date | + When I go to joao's control panel + And I follow "Profile Info and settings" + And I select "November" + And I select "15" + And I press "Save" + Then I should see "Birth date is invalid" + And I should see "Birth date is mandatory" + + Scenario: Not warn if birth date is valid when active + Given the following person fields are active fields + | display_name | + | birth_date | + When I go to joao's control panel + And I follow "Profile Info and settings" + And I select "November" + And I select "15" + And I select "1980" + And I press "Save" + Then I should not see "Birth date is invalid" + And I should not see "Birth date is mandatory" + + Scenario: Not warn if birth date is valid when required + Given the following person fields are required fields + | display_name | + | birth_date | + When I go to joao's control panel + And I follow "Profile Info and settings" + And I select "November" + And I select "15" + And I select "1980" + And I press "Save" + Then I should not see "Birth date is invalid" + And I should not see "Birth date is mandatory" diff --git a/test/unit/person_test.rb b/test/unit/person_test.rb index c2d9e4e..0e4ca05 100644 --- a/test/unit/person_test.rb +++ b/test/unit/person_test.rb @@ -1183,4 +1183,15 @@ class PersonTest < Test::Unit::TestCase assert_includes Person.more_active, profile end + should 'handle multiparameter attribute exception on birth date field' do + assert_nothing_raised ActiveRecord::MultiparameterAssignmentErrors do + p = Person.new( + :name => 'birthday', :identifier => 'birthday', :user_id => 999, + 'birth_date(1i)' => '', 'birth_date(2i)' => '6', 'birth_date(3i)' => '16' + ) + assert !p.valid? + assert p.errors.invalid?(:birth_date) + end + end + end diff --git a/vendor/plugins/validates_multiparameter_assignments/MIT-LICENSE b/vendor/plugins/validates_multiparameter_assignments/MIT-LICENSE new file mode 100644 index 0000000..49dd1cc --- /dev/null +++ b/vendor/plugins/validates_multiparameter_assignments/MIT-LICENSE @@ -0,0 +1,20 @@ +Copyright (c) 2006 Shinya Kasatani + +Permission is hereby granted, free of charge, to any person obtaining +a copy of this software and associated documentation files (the +"Software"), to deal in the Software without restriction, including +without limitation the rights to use, copy, modify, merge, publish, +distribute, sublicense, and/or sell copies of the Software, and to +permit persons to whom the Software is furnished to do so, subject to +the following conditions: + +The above copyright notice and this permission notice shall be +included in all copies or substantial portions of the Software. + +THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, +EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF +MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND +NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE +LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION +OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION +WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. diff --git a/vendor/plugins/validates_multiparameter_assignments/README b/vendor/plugins/validates_multiparameter_assignments/README new file mode 100644 index 0000000..1c12c67 --- /dev/null +++ b/vendor/plugins/validates_multiparameter_assignments/README @@ -0,0 +1,17 @@ +* validates_multiparameter_assignments plugin + +A Ruby on Rails plugin that adds "validates_multiparameter_assignments" method to ActiveRecord::Base. +Example: + +class User < ActiveRecord::Base + validates_multiparameter_assignments +end + +This makes multiparameter assignment errors to be added to the model, rather than raising ActiveRecord::MultiparameterAssignmentErrors. +By default the error message is read from ActiveRecord::Base.default_error_messages[:invalid]. You can change the message by adding an option like this: + +validates_multiparameter_assignments :message => " is not entered correctly." + +This plugin is especially useful when you use date_select helper method in the view, where the user can enter invalid dates such as "Feb 31, 2006". + +- Shinya Kasatani diff --git a/vendor/plugins/validates_multiparameter_assignments/init.rb b/vendor/plugins/validates_multiparameter_assignments/init.rb new file mode 100644 index 0000000..a69bd26 --- /dev/null +++ b/vendor/plugins/validates_multiparameter_assignments/init.rb @@ -0,0 +1 @@ +require 'validates_multiparameter_assignments' diff --git a/vendor/plugins/validates_multiparameter_assignments/lib/validates_multiparameter_assignments.rb b/vendor/plugins/validates_multiparameter_assignments/lib/validates_multiparameter_assignments.rb new file mode 100644 index 0000000..1b96f4c --- /dev/null +++ b/vendor/plugins/validates_multiparameter_assignments/lib/validates_multiparameter_assignments.rb @@ -0,0 +1,34 @@ +module ActiveRecord + module Validations + module ClassMethods + def validates_multiparameter_assignments(options = {}) + configuration = if Rails::VERSION::STRING < "2.2.0" + { :message => _("%{fn} is invalid.") } + else + { :message => I18n.translate('activerecord.errors.messages')[:invalid] } + end.update(options) + + alias_method :assign_multiparameter_attributes_without_rescuing, :assign_multiparameter_attributes + attr_accessor :assignment_error_attrs + + define_method(:assign_multiparameter_attributes) do |pairs| + self.assignment_error_attrs = [] + begin + assign_multiparameter_attributes_without_rescuing(pairs) + rescue ActiveRecord::MultiparameterAssignmentErrors + $!.errors.each do |error| + self.assignment_error_attrs << error.attribute + end + end + end + private :assign_multiparameter_attributes + + validate do |record| + record.assignment_error_attrs && record.assignment_error_attrs.each do |attr| + record.errors.add(attr, configuration[:message]) + end + end + end + end + end +end -- libgit2 0.21.2