diff --git a/app/controllers/people_controller.rb b/app/controllers/people_controller.rb index f7e35c779..0e28c3fb8 100644 --- a/app/controllers/people_controller.rb +++ b/app/controllers/people_controller.rb @@ -61,13 +61,6 @@ class PeopleController < ApplicationController end def update - # convert date selector into proper timestamp - - if birthday = params[:date] - unless [:month, :day, :year].any?{|x| birthday[x].blank?} - params[:person][:profile][:birthday] ||= Date.parse("#{birthday[:year]}-#{birthday[:month]}-#{birthday[:day]}") - end - end # upload and set new profile photo params[:person][:profile] ||= {} diff --git a/app/models/profile.rb b/app/models/profile.rb index 7794c287e..c87c97f46 100644 --- a/app/models/profile.rb +++ b/app/models/profile.rb @@ -36,7 +36,8 @@ class Profile before_save :strip_names - attr_accessible :first_name, :last_name, :image_url, :image_url_medium, :image_url_small, :birthday, :gender, :bio, :searchable + attr_accessible :first_name, :last_name, :image_url, :image_url_medium, + :image_url_small, :birthday, :gender, :bio, :searchable, :date def person @@ -86,6 +87,15 @@ class Profile end end + def date= params + if ['year', 'month', 'day'].all? { |key| params[key].present? } + date = Date.new(params['year'].to_i, params['month'].to_i, params['day'].to_i) + self.birthday = date + elsif ['year', 'month', 'day'].all? { |key| params[key] == '' } + self.birthday = nil + end + end + protected def strip_names diff --git a/app/views/people/edit.html.haml b/app/views/people/edit.html.haml index 61c2f4cdc..ce9a733e0 100644 --- a/app/views/people/edit.html.haml +++ b/app/views/people/edit.html.haml @@ -34,7 +34,7 @@ %h4 = t('.your_birthday') %br - = select_date @person.profile.birthday, :prompt => true, + = select_date @person.profile.birthday, :prompt => true, :prefix => 'person[profile][date]', :default => true, :order => t('date.order'), :start_year => 2000, :end_year => 1904 %h4 diff --git a/spec/models/profile_spec.rb b/spec/models/profile_spec.rb index e74a08c78..a7b9abe86 100644 --- a/spec/models/profile_spec.rb +++ b/spec/models/profile_spec.rb @@ -73,4 +73,37 @@ describe Profile do xml.should_not include person.id.to_s end end + + describe 'date=' do + let(:profile) { make_user.profile } + + it 'accepts form data' do + profile.birthday.should == nil + profile.date = { 'year' => '2000', 'month' => '01', 'day' => '01' } + profile.birthday.year.should == 2000 + profile.birthday.month.should == 1 + profile.birthday.day.should == 1 + end + + it 'unsets the birthday' do + profile.birthday = Date.new(2000, 1, 1) + profile.date = { 'year' => '', 'month' => '', 'day' => ''} + profile.birthday.should == nil + end + + it 'does not change with one or more blank values' do + profile.birthday = Date.new(2000, 1, 1) + profile.date = { 'year' => '2001', 'month' => '', 'day' => ''} + profile.birthday.year.should == 2000 + profile.birthday.month.should == 1 + profile.birthday.day.should == 1 + end + + it 'accepts blank initial vallues' do + profile.birthday.should == nil + profile.date = { 'year' => '2001', 'month' => '', 'day' => ''} + profile.birthday.should == nil + end + end + end