Skip to content
Extraits de code Groupes Projets
Valider ceedbb14 rédigé par Gonzalo Rodriguez's avatar Gonzalo Rodriguez
Parcourir les fichiers

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

parent 234af2a1
Branches
Étiquettes
Aucune requête de fusion associée trouvée
...@@ -35,6 +35,7 @@ class Profile < ActiveRecord::Base ...@@ -35,6 +35,7 @@ class Profile < ActiveRecord::Base
validates_format_of :first_name, :with => /\A[^;]+\z/, :allow_blank => true validates_format_of :first_name, :with => /\A[^;]+\z/, :allow_blank => true
validates_format_of :last_name, :with => /\A[^;]+\z/, :allow_blank => true validates_format_of :last_name, :with => /\A[^;]+\z/, :allow_blank => true
validate :max_tags validate :max_tags
validate :valid_birthday
attr_accessible :first_name, :last_name, :image_url, :image_url_medium, attr_accessible :first_name, :last_name, :image_url, :image_url_medium,
:image_url_small, :birthday, :gender, :bio, :location, :searchable, :date, :tag_string :image_url_small, :birthday, :gender, :bio, :location, :searchable, :date, :tag_string
...@@ -105,8 +106,11 @@ class Profile < ActiveRecord::Base ...@@ -105,8 +106,11 @@ class Profile < ActiveRecord::Base
def date= params def date= params
if ['month', 'day'].all? { |key| params[key].present? } if ['month', 'day'].all? { |key| params[key].present? }
params['year'] = '1000' if params['year'].blank? params['year'] = '1000' if params['year'].blank?
date = Date.new(params['year'].to_i, params['month'].to_i, params['day'].to_i) if Date.valid_civil?(params['year'].to_i, params['month'].to_i, params['day'].to_i)
self.birthday = date 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? } elsif [ 'year', 'month', 'day'].all? { |key| params[key].blank? }
self.birthday = nil self.birthday = nil
end end
...@@ -140,6 +144,13 @@ class Profile < ActiveRecord::Base ...@@ -140,6 +144,13 @@ class Profile < ActiveRecord::Base
end end
end end
def valid_birthday
if @invalid_birthday_date
errors.add(:birthday)
@invalid_birthday_date = nil
end
end
private private
def absolutify_local_url url def absolutify_local_url url
pod_url = AppConfig[:pod_url].dup pod_url = AppConfig[:pod_url].dup
......
...@@ -68,6 +68,32 @@ describe ProfilesController do ...@@ -68,6 +68,32 @@ describe ProfilesController do
@user.person(true).profile.tag_list.to_set.should == ['apples', 'oranges', 'bananas'].to_set @user.person(true).profile.tag_list.to_set.should == ['apples', 'oranges', 'bananas'].to_set
end 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 context 'with a profile photo set' do
before do before do
@params = { :id => @user.person.id, @params = { :id => @user.person.id,
......
...@@ -183,6 +183,20 @@ describe Profile do ...@@ -183,6 +183,20 @@ describe Profile do
profile.date = { 'year' => '2001', 'month' => '', 'day' => ''} profile.date = { 'year' => '2001', 'month' => '', 'day' => ''}
profile.birthday.should == nil profile.birthday.should == nil
end 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 end
describe 'tags' do describe 'tags' do
......
0% Chargement en cours ou .
You are about to add 0 people to the discussion. Proceed with caution.
Veuillez vous inscrire ou vous pour commenter