Files
stopwatch/app/controllers/stopwatch_timers_controller.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

139 lines
3.6 KiB
Ruby

# Ideas carried over from upstream (translated from the original German notes):
#
# - update title / menu item after starting a new entry
# - update the time entry row after stopping from the context menu (show the saved time)
# - 0:60 instead of 1:00? Redmine bug?
#
# - remember the last activity and preselect it in the 'new' form
# - same for the project, unless we are in a project context
# - focus the first field that needs input, depending on the above
#
class StopwatchTimersController < StopwatchController
before_action :find_optional_data, only: %i(new create)
before_action :authorize_log_time, only: %i(new create start stop current)
before_action :find_time_entry, only: %i(edit update start stop)
before_action :authorize_edit_time, only: %i(edit update)
before_action :find_timer, only: %i(new edit current)
def new
@time_entry = new_time_entry
load_todays_entries
end
def create
@time_entry = new_time_entry
@time_entry.safe_attributes = params[:time_entry]
@result = Stopwatch::StartTimer.new(@time_entry).call
unless @result.success?
if @result.error == :unauthorized
render_403
else
render_error status: 422, message: "could not start timer: #{@result.error}"
end
end
end
def edit
@entries = load_todays_entries #.where.not(id: @time_entry.id)
end
def update
@time_entry.safe_attributes = params[:time_entry]
@success = @time_entry.save
if !@success
edit
elsif params[:continue]
new
end
end
def start
r = Stopwatch::StartTimer.new(@time_entry).call
if r.success?
@started_time_entry = @time_entry
else
logger.error "unable to start timer: #{r.error}"
end
new unless params[:context]
end
def stop
r = Stopwatch::StopTimer.new.call
if r.success?
@stopped_time_entry = @time_entry
else
logger.error "unable to stop timer"
end
new unless params[:context]
render action: 'start'
end
def current
render json: @timer.to_json
end
def update_form
if id = params[:time_entry_id].presence
@time_entry = TimeEntry.visible.find id
else
@time_entry = TimeEntry.new
end
@time_entry.safe_attributes = params[:time_entry]
rescue ActiveRecord::RecordNotFound
head 404
end
private
# Saves the elapsed time into the running entry (also done every minute on the server by
# StopwatchTimerTickJob). Safe from several tabs at once: Timer#update locks the timer record.
def find_timer
@timer = Stopwatch::Timer.new User.current
@timer.update
end
def find_time_entry
@time_entry = time_entries.find params[:id]
end
def load_todays_entries
@entries = time_entries.where(spent_on: User.current.today).or(
time_entries.where(id: User.current.running_time_entry_id)
).order(created_on: :asc)
end
def time_entries
TimeEntry.where(user: User.current)
end
def new_time_entry
entry = TimeEntry.new(project: @project, issue: @issue,
user: User.current, spent_on: User.current.today)
if entry.respond_to?(:author=) # Redmine 4
entry.author = User.current
end
entry
end
def find_optional_data
if params[:issue_id].present?
@issue = Issue.find(params[:issue_id])
@project = @issue.project
elsif params[:project_id].present?
@project = Project.find(params[:project_id])
end
rescue ActiveRecord::RecordNotFound
render_404
end
def authorize_log_time
User.current.allowed_to?(:log_time, nil, global: true) or
deny_access
end
def authorize_edit_time
@time_entry.editable_by?(User.current) or deny_access
end
end