Permalink
Browse files

fixes

  • Loading branch information...
1 parent 1645939 commit 6478bb578d226ebdfa089b6a76ebab8a243b22dc @yuhunglin yuhunglin committed Dec 5, 2012
Showing with 43 additions and 12 deletions.
  1. +13 −8 app/models/my_tasks.rb
  2. +1 −1 config/application.rb
  3. +29 −3 spec/models/my_tasks_spec.rb
View
21 app/models/my_tasks.rb
@@ -1,8 +1,9 @@
class MyTasks
include ActiveAttr::Model
- def initialize(uid, starting_date=Date.today)
+ def initialize(uid, starting_date=Date.today.to_time_in_current_zone)
@uid = uid
+ #To avoid issues with tz, use time or DateTime instead of Date (http://www.elabs.se/blog/36-working-with-time-zones-in-ruby-on-rails)
@starting_date = starting_date
@buckets = {
"due" => {"title" => "Due", "tasks" => []},
@@ -58,7 +59,10 @@ def fetch_google_tasks
status = "needs_action" if entry["status"] == "needsAction"
status ||= "completed"
formatted_entry["status"] = status
- due_date = entry["due"]
+ # Google task dates have misleading datetime accuracy. There is no way to record a specific due time
+ # for tasks (through the UI), thus the reported time+tz is always 00:00:00+0000. Stripping off the false
+ # accuracy so the application will apply the proper timezone when needed.
+ due_date = Date.parse(entry["due"].to_s) unless entry["due"].blank?
convert_due_date(due_date, formatted_entry)
bucket = determine_bucket(due_date, status, formatted_entry)
logger.info "#{self.class.name} Putting Google task in #{bucket} bucket: #{formatted_entry}"
@@ -98,7 +102,7 @@ def fetch_canvas_tasks
def convert_due_date(due_date, formatted_entry)
if !due_date.blank?
- due = DateTime.parse(due_date.to_s)
+ due = DateTime.parse(due_date.to_s).in_time_zone
formatted_entry["due_date"] = {
"epoch" => due.to_i,
"datetime" => due.rfc3339(3),
@@ -111,10 +115,11 @@ def convert_due_date(due_date, formatted_entry)
def determine_bucket(due_date, status, formatted_entry)
bucket = "unscheduled"
if !due_date.blank?
- due = DateTime.parse(due_date.to_s)
- today = @starting_date.to_time.to_i
- tomorrow = (@starting_date.to_date + 1).to_time.to_i
- day_after_tomorrow = (@starting_date.to_date + 2).to_time.to_i
+ due = DateTime.parse(due_date.to_s).in_time_zone
+ @starting_date = @starting_date.to_time.in_time_zone.to_i unless @starting_date.is_a?(Time)
+ today = @starting_date.to_i
+ tomorrow = @starting_date.advance(:days => 1).to_i
+ day_after_tomorrow = @starting_date.advance(:days => 2).to_i
if due.to_i < tomorrow
bucket = "due"
@@ -125,7 +130,7 @@ def determine_bucket(due_date, status, formatted_entry)
bucket = "upcoming"
end
- logger.info "In determine_bucket, today = #{today}, today.zone = #{@starting_date.to_time.zone}, tomorrow = #{tomorrow} day after tomorrow = #{day_after_tomorrow}; formatted entry = #{formatted_entry}"
+ logger.info "In determine_bucket, today = #{today}, today.zone = #{@starting_date.zone}, tomorrow = #{tomorrow} day after tomorrow = #{day_after_tomorrow}; formatted entry = #{formatted_entry}"
end
bucket
end
View
2 config/application.rb
@@ -31,7 +31,7 @@ class Application < Rails::Application
# Set Time.zone default to the specified zone and make Active Record auto-convert to this zone.
# Run "rake -D time" for a list of tasks for finding time zone names. Default is UTC.
- # config.time_zone = 'Central Time (US & Canada)'
+ config.time_zone = 'Pacific Time (US & Canada)'
# The default locale is :en and all translations from config/locales/*.rb,yml are auto loaded.
# config.i18n.load_path += Dir[Rails.root.join('my', 'locales', '*.{rb,yml}').to_s]
View
32 spec/models/my_tasks_spec.rb
@@ -8,19 +8,22 @@
@fake_canvas_proxy = CanvasProxy.new({fake: true})
end
- it "should load nicely with the pre-recorded fake Google and Canvas proxy feeds" do
+ it "should load nicely with the pre-recorded fake Google and Canvas proxy feeds using the server's timezone" do
GoogleProxy.stub(:access_granted?).and_return(true)
CanvasProxy.stub(:access_granted?).and_return(true)
GoogleProxy.stub(:new).and_return(@fake_google_proxy)
CanvasProxy.stub(:new).and_return(@fake_canvas_proxy)
- GoogleProxy.any_instance.stub(:events_list).and_return(@fake_google_tasks_array)
- my_tasks_model = MyTasks.new(@user_id, Time.utc(2012, 11, 27, 8, 0))
+ my_tasks_model = MyTasks.new(@user_id, Date.new(2012, 11, 27).to_time_in_current_zone)
valid_feed = my_tasks_model.get_feed
valid_feed["sections"].length.should == 4
valid_feed["sections"][0]["title"].should == "Due"
valid_feed["sections"][0]["tasks"].size.should == 3
+ valid_feed["sections"][1]["title"].should == "Due Tomorrow"
+ valid_feed["sections"][1]["tasks"].size.should == 1
valid_feed["sections"][2]["title"].should == "Upcoming"
valid_feed["sections"][2]["tasks"].size.should == 3
+ valid_feed["sections"][3]["title"].should == "Unscheduled"
+ valid_feed["sections"][3]["tasks"].size.should == 1
valid_feed["sections"].each do |section|
section["tasks"].each do |task|
task["title"].blank?.should == false
@@ -45,4 +48,27 @@
end
end
+ it "should shift tasks into different buckets with a different timezone " do
+ #(since Berkeley assignments are probably always due at -0800, irregardless of where the student is)
+ old_time_zone = Time.zone
+ Time.zone = 13.hours
+
+ GoogleProxy.stub(:access_granted?).and_return(true)
+ CanvasProxy.stub(:access_granted?).and_return(true)
+ GoogleProxy.stub(:new).and_return(@fake_google_proxy)
+ CanvasProxy.stub(:new).and_return(@fake_canvas_proxy)
+ my_tasks_model = MyTasks.new(@user_id, Date.new(2012, 11, 27).to_time_in_current_zone)
+ valid_feed = my_tasks_model.get_feed
+ valid_feed["sections"][0]["title"].should == "Due"
+ valid_feed["sections"][0]["tasks"].size.should == 1
+ valid_feed["sections"][1]["title"].should == "Due Tomorrow"
+ valid_feed["sections"][1]["tasks"].size.should == 2
+ valid_feed["sections"][2]["title"].should == "Upcoming"
+ valid_feed["sections"][2]["tasks"].size.should == 4
+ valid_feed["sections"][3]["title"].should == "Unscheduled"
+ valid_feed["sections"][3]["tasks"].size.should == 1
+
+ Time.zone = old_time_zone
+
+ end
end

0 comments on commit 6478bb5

Please sign in to comment.