Skip to content

Commit

Permalink
Allow date_value= and time_value= to handle nils.
Browse files Browse the repository at this point in the history
Also adds specs for the existing (not null) behavior.
  • Loading branch information
rsutphin committed May 1, 2013
1 parent 0f35354 commit 04b9dd4
Show file tree
Hide file tree
Showing 2 changed files with 42 additions and 2 deletions.
14 changes: 12 additions & 2 deletions lib/surveyor/models/response_methods.rb
Original file line number Diff line number Diff line change
Expand Up @@ -55,15 +55,25 @@ def time_value
end

def time_value=(val)
self.datetime_value = Time.zone.parse("#{Date.today.to_s} #{val}") ? Time.zone.parse("#{Date.today.to_s} #{val}").to_datetime : nil
self.datetime_value =
if val && time = Time.zone.parse("#{Date.today.to_s} #{val}")
time.to_datetime
else
nil
end
end

def date_value
read_attribute(:datetime_value).strftime( date_format ) unless read_attribute(:datetime_value).blank?
end

def date_value=(val)
self.datetime_value = Time.zone.parse(val) ? Time.zone.parse(val).to_datetime : nil
self.datetime_value =
if val && time = Time.zone.parse(val)
time.to_datetime
else
nil
end
end

def time_format
Expand Down
30 changes: 30 additions & 0 deletions spec/models/response_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -174,3 +174,33 @@
end
end
end

describe Response, 'value methods' do
let(:response) { Response.new }

describe '#date_value=' do
it 'accepts a parseable date string' do
response.date_value = '2010-01-15'
response.datetime_value.strftime('%Y %m %d').should == '2010 01 15'
end

it 'clears when given nil' do
response.datetime_value = Time.new
response.date_value = nil
response.datetime_value.should be_nil
end
end

describe 'time_value=' do
it 'accepts a parseable time string' do
response.time_value = '11:30'
response.datetime_value.strftime('%H %M %S').should == '11 30 00'
end

it 'clears when given nil' do
response.datetime_value = Time.new
response.time_value = nil
response.datetime_value.should be_nil
end
end
end

0 comments on commit 04b9dd4

Please sign in to comment.