Closes #1486. Profile update was crashing with invalid birthday date

This commit is contained in:
Gonzalo Rodriguez 2011-08-20 00:34:59 -03:00
parent 234af2a15b
commit ceedbb14cb
3 changed files with 53 additions and 2 deletions

View file

@ -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

View file

@ -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,

View file

@ -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