Remove User#at_midnight

Replace it with a class called DateUtils with a method called
`#midnight_for` that takes a preference instance and date to convert to
midnight.
This commit is contained in:
Matt Rogers 2013-07-29 16:34:23 -05:00
parent e7c6142684
commit a1aaa283f1
6 changed files with 12 additions and 8 deletions

View file

@ -22,6 +22,6 @@ class Preference < ActiveRecord::Base
raise ArgumentError.new("Bad argument type:#{s.class}") raise ArgumentError.new("Bad argument type:#{s.class}")
end end
user.at_midnight(date) DateUtils::midnight_for(self, date)
end end
end end

View file

@ -260,7 +260,7 @@ class Todo < ActiveRecord::Base
activate activate
else else
# parse Date objects into the proper timezone # parse Date objects into the proper timezone
date = user.at_midnight(date) if (date.is_a? Date) date = DateUtils.midnight_for(user.prefs, date) if (date.is_a? Date)
# show_from needs to be set before state_change because of "bug" in aasm. # show_from needs to be set before state_change because of "bug" in aasm.
# If show_from is not set, the todo will not validate and thus aasm will not save # If show_from is not set, the todo will not validate and thus aasm will not save

View file

@ -167,10 +167,6 @@ class User < ActiveRecord::Base
time.midnight time.midnight
end end
def at_midnight(date)
return ActiveSupport::TimeZone[prefs.time_zone].local(date.year, date.month, date.day, 0, 0, 0)
end
def generate_token def generate_token
self.token = Digest::SHA1.hexdigest "#{Time.now.to_i}#{rand}" self.token = Digest::SHA1.hexdigest "#{Time.now.to_i}#{rand}"
end end

5
lib/date_utils.rb Normal file
View file

@ -0,0 +1,5 @@
class DateUtils
def self.midnight_for(prefs, date)
ActiveSupport::TimeZone[prefs.time_zone].local(date.year, date.month, date.day, 0, 0, 0)
end
end

View file

@ -117,7 +117,9 @@ class RecurringTodosControllerTest < ActionController::TestCase
new_todo = Todo.where(:recurring_todo_id => 5).first new_todo = Todo.where(:recurring_todo_id => 5).first
# due date should be the target_date # due date should be the target_date
assert_equal users(:admin_user).at_midnight(Date.new(target_date.year, target_date.month, target_date.day)), new_todo.due user = users(:admin_user)
target_date = Date.new(target_date.year, target_date.month, target_date.day)
assert_equal DateUtils.midnight_for(user.prefs, target_date), new_todo.due
# show_from should be nil since now+4.days-10.days is in the past # show_from should be nil since now+4.days-10.days is in the past
assert_equal nil, new_todo.show_from assert_equal nil, new_todo.show_from

View file

@ -20,7 +20,8 @@ class PreferenceTest < ActiveSupport::TestCase
end end
def test_parse_date def test_parse_date
assert_equal @admin_user.at_midnight(Date.new(2007, 5, 20)).to_s, @admin_user.preference.parse_date('20/5/2007').to_s date = Time.new(2007, 05, 20).in_time_zone(@admin_user.preference.time_zone).at_midnight
assert_equal date.to_s, @admin_user.preference.parse_date('20/5/2007').to_s
end end
def test_parse_date_returns_nil_if_string_is_empty def test_parse_date_returns_nil_if_string_is_empty