Skip to content

Commit

Permalink
Adding a test case for [concerto#134 state:resolved], also resolving …
Browse files Browse the repository at this point in the history
…a 1.8.7 bug (Time doesn't have strptime).
  • Loading branch information
bamnet committed Apr 25, 2011
1 parent 0e55b90 commit c7c6032
Show file tree
Hide file tree
Showing 2 changed files with 26 additions and 4 deletions.
9 changes: 5 additions & 4 deletions app/models/content.rb
Original file line number Diff line number Diff line change
Expand Up @@ -31,12 +31,13 @@ def is_active?
(start_time.nil? || start_time < Time.now) && (end_time.nil? || end_time > Time.now)
end

# Setter for the start time. If a hash is passed, convert that into a Time object and then a string.
# Otherwise, just set it like normal.
# Setter for the start time. If a hash is passed, convert that into a DateTime object and then a string.
# Otherwise, just set it like normal. This is a bit confusing due to the differences in how Ruby handles
# times between 1.9.x and 1.8.x.
def start_time=(_start_time)
if _start_time.kind_of?(Hash)
#write_attribute(:start_time, Time.parse("#{_start_time[:date]} #{_start_time[:time]}").to_s(:db))
write_attribute(:start_time, Time.strptime("#{_start_time[:date]} #{_start_time[:time]}","%m/%d/%Y %l:%M %p").to_s(:db))
write_attribute(:start_time, DateTime.strptime("#{_start_time[:date]} #{_start_time[:time]}","%m/%d/%Y %l:%M %p").to_s(:db))
else
write_attribute(:start_time, _start_time)
end
Expand All @@ -45,7 +46,7 @@ def start_time=(_start_time)
# See start_time=.
def end_time=(_end_time)
if _end_time.kind_of?(Hash)
write_attribute(:end_time, Time.strptime("#{_end_time[:date]} #{_end_time[:time]}","%m/%d/%Y %l:%M %p").to_s(:db))
write_attribute(:end_time, DateTime.strptime("#{_end_time[:date]} #{_end_time[:time]}","%m/%d/%Y %l:%M %p").to_s(:db))
else
write_attribute(:end_time, _end_time)
end
Expand Down
21 changes: 21 additions & 0 deletions test/unit/content_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -54,4 +54,25 @@ class ContentTest < ActiveSupport::TestCase
end
end
end

# start_time should correctly translate a hash
# into a datetime object in addition to a string.
test "start_time translation" do
c = Content.new(:start_time => {:date => "4/12/2011", :time => "1:23 am"})
assert_equal c.start_time.strftime('%Y-%m-%d %H:%M:%S'), "2011-04-12 01:23:00"

c = Content.new(:start_time => "2011-04-12 01:34:00")
assert_equal c.start_time.strftime('%Y-%m-%d %H:%M:%S'), "2011-04-12 01:34:00"
end

# end_time should correctly translate a hash
# into a datetime object in addition to a string.
test "end_time translation" do
c = Content.new(:end_time => {:date => "4/12/2011", :time => "5:00 pm"})
assert_equal c.end_time.strftime('%Y-%m-%d %H:%M:%S'), "2011-04-12 17:00:00"

c = Content.new(:end_time => "2011-01-01 00:00:00")
assert_equal c.end_time.strftime('%Y-%m-%d %H:%M:%S'), "2011-01-01 00:00:00"
end

end

0 comments on commit c7c6032

Please sign in to comment.