diff --git a/app/models/profile.rb b/app/models/profile.rb index 0160f6689..77341a8a9 100644 --- a/app/models/profile.rb +++ b/app/models/profile.rb @@ -35,6 +35,7 @@ class Profile < ActiveRecord::Base validates_format_of :first_name, :with => /\A[^;]+\z/, :allow_blank => true validates_format_of :last_name, :with => /\A[^;]+\z/, :allow_blank => true validate :max_tags + validate :valid_birthday attr_accessible :first_name, :last_name, :image_url, :image_url_medium, :image_url_small, :birthday, :gender, :bio, :location, :searchable, :date, :tag_string @@ -105,8 +106,11 @@ class Profile < ActiveRecord::Base def date= params if ['month', 'day'].all? { |key| params[key].present? } params['year'] = '1000' if params['year'].blank? - date = Date.new(params['year'].to_i, params['month'].to_i, params['day'].to_i) - self.birthday = date + if Date.valid_civil?(params['year'].to_i, params['month'].to_i, params['day'].to_i) + self.birthday = Date.new(params['year'].to_i, params['month'].to_i, params['day'].to_i) + else + @invalid_birthday_date = true + end elsif [ 'year', 'month', 'day'].all? { |key| params[key].blank? } self.birthday = nil end @@ -140,6 +144,13 @@ class Profile < ActiveRecord::Base end end + def valid_birthday + if @invalid_birthday_date + errors.add(:birthday) + @invalid_birthday_date = nil + end + end + private def absolutify_local_url url pod_url = AppConfig[:pod_url].dup diff --git a/spec/controllers/profiles_controller_spec.rb b/spec/controllers/profiles_controller_spec.rb index 28da2f5ca..910558b76 100644 --- a/spec/controllers/profiles_controller_spec.rb +++ b/spec/controllers/profiles_controller_spec.rb @@ -68,6 +68,32 @@ describe ProfilesController do @user.person(true).profile.tag_list.to_set.should == ['apples', 'oranges', 'bananas'].to_set end + it 'sets valid birthday' do + params = { :id => @user.person.id, + :profile => { + :date => { + :year => '2001', + :month => '02', + :day => '28' } } } + + put :update, params + @user.person(true).profile.birthday.year.should == 2001 + @user.person(true).profile.birthday.month.should == 2 + @user.person(true).profile.birthday.day.should == 28 + end + + it 'displays error for invalid birthday' do + params = { :id => @user.person.id, + :profile => { + :date => { + :year => '2001', + :month => '02', + :day => '31' } } } + + put :update, params + flash[:error].should_not be_blank + end + context 'with a profile photo set' do before do @params = { :id => @user.person.id, diff --git a/spec/models/profile_spec.rb b/spec/models/profile_spec.rb index 7ee762c6f..2ce3ce6fb 100644 --- a/spec/models/profile_spec.rb +++ b/spec/models/profile_spec.rb @@ -183,6 +183,20 @@ describe Profile do profile.date = { 'year' => '2001', 'month' => '', 'day' => ''} profile.birthday.should == nil end + + it 'does not accept invalid dates' do + profile.birthday = nil + profile.date = { 'year' => '2001', 'month' => '02', 'day' => '31' } + profile.birthday.should == nil + end + + it 'does not change with invalid dates' do + profile.birthday = Date.new(2000, 1, 1) + profile.date = { 'year' => '2001', 'month' => '02', 'day' => '31' } + profile.birthday.year.should == 2000 + profile.birthday.month.should == 1 + profile.birthday.day.should == 1 + end end describe 'tags' do