diff --git a/app/controllers/stopwatch_controller.rb b/app/controllers/stopwatch_controller.rb
new file mode 100644
index 0000000..7ddf659
--- /dev/null
+++ b/app/controllers/stopwatch_controller.rb
@@ -0,0 +1,12 @@
+# base class for stopwatch controllers
+class StopwatchController < ApplicationController
+ helper :timelog, :custom_fields
+
+ before_action :require_login
+
+ private
+
+ def authorize_edit_time
+ @time_entry.editable_by?(User.current) or deny_access
+ end
+end
diff --git a/app/controllers/stopwatch_issue_timers_controller.rb b/app/controllers/stopwatch_issue_timers_controller.rb
new file mode 100644
index 0000000..60606de
--- /dev/null
+++ b/app/controllers/stopwatch_issue_timers_controller.rb
@@ -0,0 +1,48 @@
+class StopwatchIssueTimersController < StopwatchController
+ before_action :find_issue
+ before_action :authorize_log_time
+
+ def start
+ t = Stopwatch::IssueTimer.new(issue: @issue)
+ if t.running?
+ head 422; return
+ end
+
+ time_entry = User.current.todays_time_entry_for(@issue)
+ if time_entry.new_record?
+ sys_default_activity = time_entry.activity
+ time_entry.activity = Stopwatch.default_activity_for time_entry
+ end
+
+ r = Stopwatch::StartTimer.new(time_entry).call
+ if r.success?
+ @started_time_entry = time_entry
+ render status: :created
+ else
+ @time_entry = time_entry
+ # in case the setting is 'always ask', still preset the form to the global default
+ @time_entry.activity ||= sys_default_activity
+ @time_entry.errors.clear
+ render status: :ok
+ end
+ end
+
+ def stop
+ r = Stopwatch::StopTimer.new.call
+ unless r.success?
+ logger.error "unable to stop timer"
+ head 422; return
+ end
+ end
+
+ private
+
+ def authorize_log_time
+ User.current.allowed_to?(:log_time, @project) or deny_access
+ end
+
+ def find_issue
+ @issue = Issue.find params[:issue_id]
+ @project = @issue.project
+ end
+end
diff --git a/app/controllers/stopwatch_timers_controller.rb b/app/controllers/stopwatch_timers_controller.rb
index 8337cdc..bdfa2a5 100644
--- a/app/controllers/stopwatch_timers_controller.rb
+++ b/app/controllers/stopwatch_timers_controller.rb
@@ -8,10 +8,8 @@
# - same for project, unless we are in a project context
# - focus first field that needs an action, depending on above
#
-class StopwatchTimersController < ApplicationController
- helper :timelog, :custom_fields
+class StopwatchTimersController < StopwatchController
- before_action :require_login
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)
@@ -62,7 +60,9 @@ class StopwatchTimersController < ApplicationController
def stop
r = Stopwatch::StopTimer.new.call
- unless r.success?
+ if r.success?
+ @stopped_time_entry = @time_entry
+ else
logger.error "unable to stop timer"
end
new unless params[:context]
@@ -73,6 +73,17 @@ class StopwatchTimersController < ApplicationController
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
def find_timer
@@ -82,7 +93,6 @@ class StopwatchTimersController < ApplicationController
def find_time_entry
@time_entry = time_entries.find params[:id]
-
end
def load_todays_entries
diff --git a/app/views/stopwatch/_settings.html.erb b/app/views/stopwatch/_settings.html.erb
new file mode 100644
index 0000000..873d442
--- /dev/null
+++ b/app/views/stopwatch/_settings.html.erb
@@ -0,0 +1,4 @@
+