Skip to content

Commit

Permalink
Merge pull request NUBIC#450 from NUBIC/fix_response_datetime_nils
Browse files Browse the repository at this point in the history
Correct nil handling in date_value= and time_value=
  • Loading branch information
yoon committed May 1, 2013
2 parents 0f35354 + c09cf52 commit 7e5f6e7
Show file tree
Hide file tree
Showing 3 changed files with 46 additions and 2 deletions.
4 changes: 4 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,10 @@ History for Surveyor
1.4.1
-----

### Fixes

- Handle `nil` in `ResponseMethods#date_value=` and `ResponseMethods#time_value`.
(#450)

1.4.0
-----
Expand Down
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 7e5f6e7

Please sign in to comment.