Files
stopwatch/lib/stopwatch.rb
T
Claude 626db60658 Timer safety: length limit, server-side saving, stop on close
- Maximum timer length setting (default 10h): a timer that runs past it
  is stopped at the limit and its entry's comment is flagged, so a
  forgotten timer can't add a night or weekend of hours
- StopwatchTimerTickJob saves a running timer's time every minute on the
  server (no browser needed) and stops it at the limit on time; open
  tabs keep saving too
- Timer updates lock the user's preference row and re-read the timer, so
  simultaneous saves (several tabs, the job) never add time twice
- Closing an issue stops timers running on it before anything else (e.g.
  billing) uses its time; deleting a time entry only stops the timer
  running on that entry
- Issue timers require a visible issue; starting again moves the timer
  instead of raising "timer is already running"
- Tests updated and extended (limit, locking, tick job, close, delete,
  visibility, issue context menu); CI runs on Redmine 7.0
- Upstream's German notes translated

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_01BiwYMjhoQvL7XsKKfRz4pM
2026-10-29 00:13:21 -04:00

59 lines
1.6 KiB
Ruby

# frozen_string_literal: true
module Stopwatch
def self.setup
Stopwatch::IssuesControllerPatch.apply
::TimeEntry.prepend Stopwatch::TimeEntryPatch
::Issue.prepend Stopwatch::IssuePatch
::User.prepend Stopwatch::UserPatch
Stopwatch::Hooks # just load it
end
def self.settings
Setting.plugin_stopwatch
end
def self.default_activity
if id = settings['default_activity'].presence
if id.to_s =~ /^\d+$/
TimeEntryActivity.find_by_id id
else
id
end
end
end
def self.default_activity_for(time_entry)
default = Stopwatch.default_activity
return nil if default == 'always_ask'
project = time_entry.project || time_entry.issue&.project
if project.nil?
activities = TimeEntryActivity.shared.active
else
activities = project.activities
end
if default == 'system'
activities.detect(&:is_default?) || activities.detect{|a| a.parent&.is_default?} || (activities.one? && activities[0]).presence
else
return activities.detect{ |a| a == default || a.parent == default }
end
end
# Longest a timer may run before it is stopped automatically (hours), or nil for no limit.
# Protects against a forgotten timer adding a night or weekend to a time entry.
def self.max_timer_hours
value = settings['max_timer_hours']
return 10.0 if value.nil? # setting never saved: use the default
hours = value.to_s.strip.tr(',', '.').to_f
hours.positive? ? hours : nil
end
# Colour-code the issue Start/Stop links (green to start, red to stop)
def self.highlighted_links
settings['highlighted_links'].to_s == '1'
end
end