Commit bd7942b8dca9ab796f53d76bee80153a623a22c8
Committed by
Daniela Feitosa
1 parent
b3c57860
Exists in
master
and in
23 other branches
Show a more descriptive message to user when not fill the birthdate properly
- Also, adding 'validates_multiparameter_assignments' plugin (ActionItem1951)
Showing
7 changed files
with
142 additions
and
0 deletions
Show diff stats
app/models/person.rb
| ... | ... | @@ -0,0 +1,57 @@ |
| 1 | +Feature: edit profile | |
| 2 | + | |
| 3 | + Background: | |
| 4 | + Given the following users | |
| 5 | + | login | | |
| 6 | + | joao | | |
| 7 | + Given I am logged in as "joao" | |
| 8 | + | |
| 9 | + Scenario: Warn about invalid birth date when active | |
| 10 | + Given the following person fields are active fields | |
| 11 | + | display_name | | |
| 12 | + | birth_date | | |
| 13 | + When I go to joao's control panel | |
| 14 | + And I follow "Profile Info and settings" | |
| 15 | + And I select "November" | |
| 16 | + And I select "15" | |
| 17 | + And I press "Save" | |
| 18 | + Then I should see "Birth date is invalid" | |
| 19 | + And I should not see "Birth date is mandatory" | |
| 20 | + | |
| 21 | + Scenario: Warn about invalid birth date when required | |
| 22 | + Given the following person fields are required fields | |
| 23 | + | display_name | | |
| 24 | + | birth_date | | |
| 25 | + When I go to joao's control panel | |
| 26 | + And I follow "Profile Info and settings" | |
| 27 | + And I select "November" | |
| 28 | + And I select "15" | |
| 29 | + And I press "Save" | |
| 30 | + Then I should see "Birth date is invalid" | |
| 31 | + And I should see "Birth date is mandatory" | |
| 32 | + | |
| 33 | + Scenario: Not warn if birth date is valid when active | |
| 34 | + Given the following person fields are active fields | |
| 35 | + | display_name | | |
| 36 | + | birth_date | | |
| 37 | + When I go to joao's control panel | |
| 38 | + And I follow "Profile Info and settings" | |
| 39 | + And I select "November" | |
| 40 | + And I select "15" | |
| 41 | + And I select "1980" | |
| 42 | + And I press "Save" | |
| 43 | + Then I should not see "Birth date is invalid" | |
| 44 | + And I should not see "Birth date is mandatory" | |
| 45 | + | |
| 46 | + Scenario: Not warn if birth date is valid when required | |
| 47 | + Given the following person fields are required fields | |
| 48 | + | display_name | | |
| 49 | + | birth_date | | |
| 50 | + When I go to joao's control panel | |
| 51 | + And I follow "Profile Info and settings" | |
| 52 | + And I select "November" | |
| 53 | + And I select "15" | |
| 54 | + And I select "1980" | |
| 55 | + And I press "Save" | |
| 56 | + Then I should not see "Birth date is invalid" | |
| 57 | + And I should not see "Birth date is mandatory" | ... | ... |
test/unit/person_test.rb
| ... | ... | @@ -1183,4 +1183,15 @@ class PersonTest < Test::Unit::TestCase |
| 1183 | 1183 | assert_includes Person.more_active, profile |
| 1184 | 1184 | end |
| 1185 | 1185 | |
| 1186 | + should 'handle multiparameter attribute exception on birth date field' do | |
| 1187 | + assert_nothing_raised ActiveRecord::MultiparameterAssignmentErrors do | |
| 1188 | + p = Person.new( | |
| 1189 | + :name => 'birthday', :identifier => 'birthday', :user_id => 999, | |
| 1190 | + 'birth_date(1i)' => '', 'birth_date(2i)' => '6', 'birth_date(3i)' => '16' | |
| 1191 | + ) | |
| 1192 | + assert !p.valid? | |
| 1193 | + assert p.errors.invalid?(:birth_date) | |
| 1194 | + end | |
| 1195 | + end | |
| 1196 | + | |
| 1186 | 1197 | end | ... | ... |
vendor/plugins/validates_multiparameter_assignments/MIT-LICENSE
0 → 100644
| ... | ... | @@ -0,0 +1,20 @@ |
| 1 | +Copyright (c) 2006 Shinya Kasatani | |
| 2 | + | |
| 3 | +Permission is hereby granted, free of charge, to any person obtaining | |
| 4 | +a copy of this software and associated documentation files (the | |
| 5 | +"Software"), to deal in the Software without restriction, including | |
| 6 | +without limitation the rights to use, copy, modify, merge, publish, | |
| 7 | +distribute, sublicense, and/or sell copies of the Software, and to | |
| 8 | +permit persons to whom the Software is furnished to do so, subject to | |
| 9 | +the following conditions: | |
| 10 | + | |
| 11 | +The above copyright notice and this permission notice shall be | |
| 12 | +included in all copies or substantial portions of the Software. | |
| 13 | + | |
| 14 | +THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, | |
| 15 | +EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF | |
| 16 | +MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND | |
| 17 | +NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE | |
| 18 | +LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION | |
| 19 | +OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION | |
| 20 | +WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. | ... | ... |
vendor/plugins/validates_multiparameter_assignments/README
0 → 100644
| ... | ... | @@ -0,0 +1,17 @@ |
| 1 | +* validates_multiparameter_assignments plugin | |
| 2 | + | |
| 3 | +A Ruby on Rails plugin that adds "validates_multiparameter_assignments" method to ActiveRecord::Base. | |
| 4 | +Example: | |
| 5 | + | |
| 6 | +class User < ActiveRecord::Base | |
| 7 | + validates_multiparameter_assignments | |
| 8 | +end | |
| 9 | + | |
| 10 | +This makes multiparameter assignment errors to be added to the model, rather than raising ActiveRecord::MultiparameterAssignmentErrors. | |
| 11 | +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: | |
| 12 | + | |
| 13 | +validates_multiparameter_assignments :message => " is not entered correctly." | |
| 14 | + | |
| 15 | +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". | |
| 16 | + | |
| 17 | +- Shinya Kasatani <kasatani at gmail.com> | ... | ... |
vendor/plugins/validates_multiparameter_assignments/init.rb
0 → 100644
| ... | ... | @@ -0,0 +1 @@ |
| 1 | +require 'validates_multiparameter_assignments' | ... | ... |
vendor/plugins/validates_multiparameter_assignments/lib/validates_multiparameter_assignments.rb
0 → 100644
| ... | ... | @@ -0,0 +1,34 @@ |
| 1 | +module ActiveRecord | |
| 2 | + module Validations | |
| 3 | + module ClassMethods | |
| 4 | + def validates_multiparameter_assignments(options = {}) | |
| 5 | + configuration = if Rails::VERSION::STRING < "2.2.0" | |
| 6 | + { :message => _("%{fn} is invalid.") } | |
| 7 | + else | |
| 8 | + { :message => I18n.translate('activerecord.errors.messages')[:invalid] } | |
| 9 | + end.update(options) | |
| 10 | + | |
| 11 | + alias_method :assign_multiparameter_attributes_without_rescuing, :assign_multiparameter_attributes | |
| 12 | + attr_accessor :assignment_error_attrs | |
| 13 | + | |
| 14 | + define_method(:assign_multiparameter_attributes) do |pairs| | |
| 15 | + self.assignment_error_attrs = [] | |
| 16 | + begin | |
| 17 | + assign_multiparameter_attributes_without_rescuing(pairs) | |
| 18 | + rescue ActiveRecord::MultiparameterAssignmentErrors | |
| 19 | + $!.errors.each do |error| | |
| 20 | + self.assignment_error_attrs << error.attribute | |
| 21 | + end | |
| 22 | + end | |
| 23 | + end | |
| 24 | + private :assign_multiparameter_attributes | |
| 25 | + | |
| 26 | + validate do |record| | |
| 27 | + record.assignment_error_attrs && record.assignment_error_attrs.each do |attr| | |
| 28 | + record.errors.add(attr, configuration[:message]) | |
| 29 | + end | |
| 30 | + end | |
| 31 | + end | |
| 32 | + end | |
| 33 | + end | |
| 34 | +end | ... | ... |