diff --git a/app/models/preference.rb b/app/models/preference.rb index afe2b69f..c12d0ccb 100644 --- a/app/models/preference.rb +++ b/app/models/preference.rb @@ -22,6 +22,6 @@ class Preference < ActiveRecord::Base raise ArgumentError.new("Bad argument type:#{s.class}") end - user.at_midnight(date) + DateUtils::midnight_for(self, date) end end diff --git a/app/models/todo.rb b/app/models/todo.rb index d3a8511b..46faee58 100644 --- a/app/models/todo.rb +++ b/app/models/todo.rb @@ -260,7 +260,7 @@ class Todo < ActiveRecord::Base activate else # 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. # If show_from is not set, the todo will not validate and thus aasm will not save diff --git a/app/models/user.rb b/app/models/user.rb index 5aa94f8a..8b3e1478 100644 --- a/app/models/user.rb +++ b/app/models/user.rb @@ -167,10 +167,6 @@ class User < ActiveRecord::Base time.midnight 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 self.token = Digest::SHA1.hexdigest "#{Time.now.to_i}#{rand}" end diff --git a/lib/date_utils.rb b/lib/date_utils.rb new file mode 100644 index 00000000..e2af8609 --- /dev/null +++ b/lib/date_utils.rb @@ -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 diff --git a/test/controllers/recurring_todos_controller_test.rb b/test/controllers/recurring_todos_controller_test.rb index 54baacf1..e0fad920 100644 --- a/test/controllers/recurring_todos_controller_test.rb +++ b/test/controllers/recurring_todos_controller_test.rb @@ -117,7 +117,9 @@ class RecurringTodosControllerTest < ActionController::TestCase new_todo = Todo.where(:recurring_todo_id => 5).first # 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 assert_equal nil, new_todo.show_from diff --git a/test/models/preference_test.rb b/test/models/preference_test.rb index 4080cb62..fc93d3ee 100644 --- a/test/models/preference_test.rb +++ b/test/models/preference_test.rb @@ -20,7 +20,8 @@ class PreferenceTest < ActiveSupport::TestCase end 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 def test_parse_date_returns_nil_if_string_is_empty