diff --git a/app/models/preference.rb b/app/models/preference.rb index 8d6d78bc..e0150cd8 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 - DateUtils::midnight_for(self, date) + UserTime.new(user).midnight(date) end end diff --git a/app/models/todo.rb b/app/models/todo.rb index 46faee58..5aad233a 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 = DateUtils.midnight_for(user.prefs, date) if (date.is_a? Date) + date = UserTime.new(user).midnight(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 8b3e1478..eb7339e5 100644 --- a/app/models/user.rb +++ b/app/models/user.rb @@ -164,7 +164,7 @@ class User < ActiveRecord::Base end def date - time.midnight + UserTime.new(self).midnight(Time.now) end def generate_token diff --git a/lib/date_utils.rb b/lib/date_utils.rb deleted file mode 100644 index e2af8609..00000000 --- a/lib/date_utils.rb +++ /dev/null @@ -1,5 +0,0 @@ -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/lib/user_time.rb b/lib/user_time.rb new file mode 100644 index 00000000..847471a0 --- /dev/null +++ b/lib/user_time.rb @@ -0,0 +1,14 @@ +require 'active_support/values/time_zone' + +class UserTime + attr_reader :user, :timezone + + def initialize(user) + @user = user + @timezone = ActiveSupport::TimeZone[user.prefs.time_zone] + end + + def midnight(date) + timezone.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 e0fad920..d9e91395 100644 --- a/test/controllers/recurring_todos_controller_test.rb +++ b/test/controllers/recurring_todos_controller_test.rb @@ -119,7 +119,7 @@ class RecurringTodosControllerTest < ActionController::TestCase # due date should be the target_date 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 + assert_equal UserTime.new(user).midnight(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