mirror of
https://github.com/rickbarrette/stopwatch.git
synced 2026-04-02 09:51:57 -04:00
remove running timer info when the time entry is removed
This commit is contained in:
1
init.rb
1
init.rb
@@ -19,5 +19,6 @@ end
|
|||||||
|
|
||||||
Rails.configuration.to_prepare do
|
Rails.configuration.to_prepare do
|
||||||
Stopwatch::UserPatch.apply
|
Stopwatch::UserPatch.apply
|
||||||
|
Stopwatch::TimeEntryPatch.apply
|
||||||
end
|
end
|
||||||
|
|
||||||
|
|||||||
18
lib/stopwatch/time_entry_patch.rb
Normal file
18
lib/stopwatch/time_entry_patch.rb
Normal file
@@ -0,0 +1,18 @@
|
|||||||
|
module Stopwatch
|
||||||
|
module TimeEntryPatch
|
||||||
|
def self.apply
|
||||||
|
TimeEntry.prepend self unless TimeEntry < self
|
||||||
|
end
|
||||||
|
|
||||||
|
def self.prepended(base)
|
||||||
|
base.class_eval do
|
||||||
|
before_destroy :stop_timer
|
||||||
|
end
|
||||||
|
end
|
||||||
|
|
||||||
|
def stop_timer
|
||||||
|
t = Stopwatch::Timer.new(user)
|
||||||
|
t.update(stop: true) if t.running?
|
||||||
|
end
|
||||||
|
end
|
||||||
|
end
|
||||||
24
test/unit/time_entry_test.rb
Normal file
24
test/unit/time_entry_test.rb
Normal file
@@ -0,0 +1,24 @@
|
|||||||
|
require_relative '../test_helper'
|
||||||
|
|
||||||
|
class StartTimerTest < ActiveSupport::TestCase
|
||||||
|
fixtures :users, :user_preferences, :time_entries, :projects,
|
||||||
|
:roles, :member_roles, :members, :enumerations, :enabled_modules
|
||||||
|
|
||||||
|
setup do
|
||||||
|
@user = User.find 1
|
||||||
|
@time_entry = TimeEntry.where(user_id: 1).first
|
||||||
|
# so we dont have to load all the issue and related fixtures:
|
||||||
|
@time_entry.update_column :issue_id, nil
|
||||||
|
end
|
||||||
|
|
||||||
|
test "should stop timer before destroy" do
|
||||||
|
assert r = Stopwatch::StartTimer.new(@time_entry, user: @user).call
|
||||||
|
assert r.success?, r.inspect
|
||||||
|
assert User.find(@user.id).timer_running?
|
||||||
|
|
||||||
|
@time_entry.destroy
|
||||||
|
|
||||||
|
refute User.find(@user.id).timer_running?
|
||||||
|
end
|
||||||
|
|
||||||
|
end
|
||||||
Reference in New Issue
Block a user