diff --git a/lib/stopwatch.rb b/lib/stopwatch.rb index 1cfd4f7..4ab9e0b 100644 --- a/lib/stopwatch.rb +++ b/lib/stopwatch.rb @@ -2,7 +2,6 @@ module Stopwatch def self.setup - #Stopwatch::ContextMenusControllerPatch.apply Stopwatch::IssuesControllerPatch.apply ::TimeEntry.prepend Stopwatch::TimeEntryPatch ::User.prepend Stopwatch::UserPatch diff --git a/lib/stopwatch/context_menus_controller_patch.rb b/lib/stopwatch/context_menus_controller_patch.rb deleted file mode 100644 index 49327b1..0000000 --- a/lib/stopwatch/context_menus_controller_patch.rb +++ /dev/null @@ -1,28 +0,0 @@ -# frozen_string_literal: true - -module Stopwatch - module ContextMenusControllerPatch - module Helper - def watcher_link(objects, user) - link = +'' - if params[:action] == 'issues' and - objects.one? and (issue = objects[0]).is_a?(Issue) and - User.current.allowed_to?(:log_time, issue.project) - t = Stopwatch::IssueTimer.new(issue: issue) - if t.running? - link << IssueLinks.new(issue, self).stop_timer - else - link << IssueLinks.new(issue, self).start_timer - end - end - super + content_tag(:li, link.html_safe) - end - end - - def self.apply - ContextMenusController.class_eval do - helper Helper - end - end - end -end diff --git a/lib/stopwatch/hooks.rb b/lib/stopwatch/hooks.rb index 9a97cf1..e981576 100644 --- a/lib/stopwatch/hooks.rb +++ b/lib/stopwatch/hooks.rb @@ -6,5 +6,28 @@ module Stopwatch partial: 'stopwatch/hooks/layouts_base_body_bottom' render_on :view_time_entries_context_menu_start, partial: 'stopwatch/hooks/time_entries_context_menu_start' + + # This hook is natively provided by Redmine 7's issues context menu view + def view_issues_context_menu_start(context = {}) + issues = context[:issues] + + # We only append our element if exactly one issue is highlighted + return '' unless issues&.one? + + issue = issues.first + return '' unless User.current.allowed_to?(:log_time, issue.project) + + # 'controller' in this context gives access to view helper context + view = context[:controller].view_context + timer = Stopwatch::IssueTimer.new(issue: issue) + + link = if timer.running? + IssueLinks.new(issue, view).stop_timer + else + IssueLinks.new(issue, view).start_timer + end + + view.content_tag(:li, link.html_safe) + end end end