Skip to content

Commit

Permalink
Fix task 'Error: Time gap!' caused by ARel .order() on the same colum…
Browse files Browse the repository at this point in the history
…n as the default_scope
  • Loading branch information
esambo committed Aug 8, 2011
1 parent 541159d commit 6adf124
Show file tree
Hide file tree
Showing 7 changed files with 34 additions and 26 deletions.
2 changes: 1 addition & 1 deletion app/helpers/date_time_helper.rb
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ def microformats_date_time(date_and_time)
end

def parse_human_duration(duration_in_natural_language)
ChronicDuration.parse(duration_in_natural_language)
ChronicDuration.parse(duration_in_natural_language) || 0
end

def parse_us_date_time(date_and_time)
Expand Down
2 changes: 1 addition & 1 deletion app/models/task.rb
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ def set_stop_on_last
previous_task.stop = self[:start]
previous_task.save!
elsif self[:title] != 'Error: Time gap!'
previous_task = user.tasks.where("stop < ?", self[:start]).order("start").last
previous_task = user.tasks.where("stop < ?", self[:start]).first
if previous_task
gap = Task.create!(
:title => 'Error: Time gap!',
Expand Down
9 changes: 4 additions & 5 deletions features/step_definitions/task_steps.rb
Original file line number Diff line number Diff line change
Expand Up @@ -20,11 +20,10 @@
@task = Task.create!(:title => 'task title', :start => duration, :stop => Time.now, :user => current_user)
end

Given /^I have a stop "([^\"]*)" ago$/ do |duration_in_natural_language|
seconds = parse_human_duration(duration_in_natural_language).round
stop = seconds.seconds.ago
start = (seconds + 1).seconds.ago
@task = Task.create!(:title => 'task with stop',
Given /^I have a task "([^\"]*)" that started "([^\"]*)" and stopped "([^\"]*)" ago$/ do |title, start_age_duration, stop_age_duration|
start = parse_human_duration(start_age_duration).round.seconds.ago
stop = parse_human_duration(stop_age_duration).round.seconds.ago
@task = Task.create!(:title => title,
:start => start,
:stop => stop,
:user => current_user
Expand Down
18 changes: 18 additions & 0 deletions features/task_error_time_gap.feature
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
Feature: Task: Error: Time Gap
>I order to see where I spend my time
As a person reflecting on my time
I want to have a continuous timeline without gaps or overlaps

Background: Signed in
Given I am signed in as a new user

@time_travel
Scenario: Fill time gap
Given it is "1/3/2010 8:13:23 AM"
And I have a task "Oldest" that started "10m" ago
And I have a task "Old" that started "6m" ago
And I have a task "With STOP!" that started "3m" and stopped "1m" ago
When I have a task "New" that started "0s" ago
And I am on the tasks page
Then I should see "Error: Time gap!" title in task 2

11 changes: 0 additions & 11 deletions features/task_switch.feature
Original file line number Diff line number Diff line change
Expand Up @@ -16,17 +16,6 @@ Feature: Task Switch
And I should see "Active"
And I should see "0s"

@time_travel
Scenario: Prevent time gaps
Given it is "1/3/2010 8:13:23 AM"
And I have a stop "1m" ago
And I am on the new task page
When I press "Switch Now"
And it is "1/3/2010 8:13:24 AM"
And I follow "Back"
And I should see "Error: Time gap!"
And I should see "1m"

@time_travel
Scenario: Duration on new task using 'Switch Now'
Given it is "1/3/2010 8:13:23 AM"
Expand Down
4 changes: 4 additions & 0 deletions spec/helpers/date_time_helper_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,10 @@
it "should parse duration into seconds" do
parse_human_duration('1m 9s').should == 69
end

it "should return 0 instead of nil" do
parse_human_duration('0s').should == 0
end
end

describe "#parse_us_date_time" do
Expand Down
14 changes: 6 additions & 8 deletions spec/models/task_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -268,23 +268,21 @@
t.save!
end

it "should create a 'Error: Time gap!' task if the previous task already had a stop (which could happen since I don't use a transaction)" do
prev_t = @valid_user.tasks.create! :start => 5.minutes.ago, :stop => 4.minutes.ago
it "should create a 'Error: Time gap!' task if the previous task already had a stop" do
prev_t = @valid_user.tasks.create! :start => 10.minutes.ago
prev_t = @valid_user.tasks.create! :start => 6.minutes.ago
prev_t = @valid_user.tasks.create! :start => 3.minutes.ago, :stop => 1.minutes.ago

t = @valid_user.tasks.new
t.switch_now
t.save!
t.reload
t.start.should == Time.zone.now

# gap_t = @valid_user.tasks.first(:order => "start DESC", :conditions => ["stop = ?", t.start.to_s(:db) + '.000000'])
gap_t = @valid_user.tasks.first(:order => "start DESC", :conditions => ["stop = ?", t.start])
gap_t = @valid_user.tasks.where(:title => 'Error: Time gap!').first
gap_t.should be_kind_of Task
gap_t.class.to_s.should == Task.to_s
gap_t.title.should == 'Error: Time gap!'
gap_t.start.should == 4.minutes.ago
gap_t.start.should == 1.minutes.ago
gap_t.stop.should == t.start
gap_t.user_id.should == t.user_id
end

end
Expand Down

0 comments on commit 6adf124

Please sign in to comment.