works, basically

This commit is contained in:
Jens Kraemer
2020-04-24 17:20:18 +08:00
parent 70257cdee0
commit 20ccbba2c1
24 changed files with 355 additions and 27 deletions

View File

@@ -0,0 +1,11 @@
module Stopwatch
class Hooks < Redmine::Hook::ViewListener
render_on :view_layouts_base_html_head,
partial: 'stopwatch/hooks/layouts_base_html_head'
render_on :view_layouts_base_body_bottom,
partial: 'stopwatch/hooks/layouts_base_body_bottom'
render_on :view_time_entries_context_menu_start,
partial: 'stopwatch/hooks/time_entries_context_menu_start'
end
end

View File

@@ -9,16 +9,20 @@ module Stopwatch
end
def call
if @time_entry.project && @user.allowed_to?(:log_time, @time_entry.project)
if @time_entry.project && !@user.allowed_to?(:log_time, @time_entry.project)
return Result.new(error: :unauthorized)
end
stop_existing_timer
StopTimer.new(user: @user).call
@time_entry.hours = 0 if @time_entry.hours.nil?
# we want to start tracking time if this is an existing time entry, or a
# new entry with 0 hours was created.
# new entries with hours > 0 are just saved as is.
start_timer = !@time_entry.new_record? || @time_entry.hours == 0
if @time_entry.save
start_new_timer
start_new_timer if start_timer
return Result.new(success: true)
else
Rails.logger.error("could not save time entry: \n#{@time_entry.errors.inspect}")
@@ -33,12 +37,5 @@ module Stopwatch
timer.start @time_entry
end
def stop_existing_timer
timer = Timer.new @user
if timer.running?
timer.stop
end
end
end
end

View File

@@ -0,0 +1,20 @@
module Stopwatch
class StopTimer
Result = ImmutableStruct.new(:success?, :error)
def initialize(user: User.current)
@user = user
end
def call
timer = Timer.new @user
if timer.running?
timer.stop
end
return Result.new(success: true)
end
end
end

View File

@@ -21,7 +21,7 @@ module Stopwatch
def stop
if hours = runtime_hours and
time_entry = TimeEntry.find_by_id(data[:time_entry_id])
time_entry = TimeEntry.find_by_id(time_entry_id)
time_entry.update_column :hours, time_entry.hours + hours
end
@@ -29,11 +29,22 @@ module Stopwatch
save
end
def to_json
{
time_entry_id: time_entry_id,
running: running?
}.to_json
end
def save
user.pref[:current_timer] = data.to_json
user.pref.save
end
def time_entry_id
data[:time_entry_id]
end
private
def data

View File

@@ -7,5 +7,10 @@ module Stopwatch
def timer_running?
Stopwatch::Timer.new(self).running?
end
def is_running_timer?(time_entry)
timer = Stopwatch::Timer.new(self)
timer.running? and timer.time_entry_id == time_entry.id
end
end
end