Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions app/models/timer_session.rb
Original file line number Diff line number Diff line change
Expand Up @@ -13,8 +13,8 @@ class TimerSession < RedmineTrackyApplicationRecord
has_many :time_entries, through: :timer_session_time_entries

validates :timer_start, presence: true
before_save :set_recorded_hours
before_save :round_timer_to_nearest_minute
before_save :set_recorded_hours

scope :active, -> { where(finished: false) }
scope :finished, -> { where(finished: true) }
Expand Down Expand Up @@ -65,7 +65,7 @@ def can_overlap?(other_session)
end

def round_timer_to_nearest_minute
self.timer_start = round_to_nearest_minute(timer_start) if timer_end.present?
self.timer_start = round_to_nearest_minute(timer_start) if timer_start.present?
self.timer_end = round_to_nearest_minute(timer_end) if timer_end.present?
end

Expand Down
11 changes: 11 additions & 0 deletions test/unit/timer_session_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -59,6 +59,7 @@ class TimerSessionTest < ActiveSupport::TestCase
end

test 'round_timer_to_nearest_minute' do
@timer_session.timer_end = nil
Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
@timer_session.timer_end = nil

timer_start_before = @timer_session.timer_start
Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

hardcode seconds in the spec


timer_start_after = timer_start_before + 10.seconds
Expand Down Expand Up @@ -106,4 +107,14 @@ class TimerSessionTest < ActiveSupport::TestCase
assert session1.overlaps?(session2)
assert session2.overlaps?(session1)
end

test 'round_timer_start_when_timer_end_is_nil' do
timer_session = FactoryBot.build(:timer_session, user: User.current, finished: false)
timer_session.timer_end = nil
Comment on lines +112 to +113
Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
timer_session = FactoryBot.build(:timer_session, user: User.current, finished: false)
timer_session.timer_end = nil
timer_session = FactoryBot.build(:timer_session, user: User.current, finished: false, timer_end: nil)

timer_session.timer_start = Time.zone.now + 45.seconds

timer_session.save!

assert_equal 0, timer_session.timer_start.sec, 'timer_start should be rounded to nearest minute even when timer_end is nil' # rubocop:disable Layout/LineLength
end
end