Skip to content
Merged
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
20 changes: 19 additions & 1 deletion app/controllers/reports_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ def new
else
Report.new(reported_on: Date.current)
end
@report.learning_times.build
build_learning_times(@report)

return unless params[:id]

Expand Down Expand Up @@ -181,4 +181,22 @@ def canonicalize_learning_times(report)
learning_time.assign_attributes(started_at: new_started_at, finished_at: new_finished_at)
end
end

def build_learning_times(report)
reported_weekday = LearningTimeFrame::WEEK_DAY_NAMES_JA[report.reported_on.wday]

min_hour = current_user.learning_time_frames
.where(week_day: reported_weekday)
.minimum(:activity_time)

started_at =
if min_hour
t = report.reported_on.in_time_zone.change(hour: min_hour, min: 0)
t <= Time.current ? t : Time.current.change(min: 0)
else
Time.current.change(min: 0)
end

report.learning_times.build(started_at: started_at)
end
Comment thread
coderabbitai[bot] marked this conversation as resolved.
end
50 changes: 50 additions & 0 deletions test/system/reports/create_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -50,5 +50,55 @@ class CreateTest < ApplicationSystemTestCase
assert_selector('span.a-user-role.is-mentor')
end
end

test 'set default learning start time from user learning time frame' do
travel_to Time.zone.local(2025, 1, 1, 10, 0, 0) do
visit_with_auth '/reports/new', 'kimura'

start_hour, start_minutes = learning_start_time_select_values

assert_equal '10', start_hour
assert_equal '00', start_minutes

user = users(:kimura)
one_hour_ago = 1.hour.ago
week_day = LearningTimeFrame::WEEK_DAY_NAMES_JA[one_hour_ago.wday]
frame_hour = one_hour_ago.hour
frame = LearningTimeFrame.find_by!(week_day: week_day, activity_time: frame_hour)
LearningTimeFramesUser.create!(user: user, learning_time_frame: frame)

visit_with_auth '/reports/new', 'kimura'

start_hour, start_minutes = learning_start_time_select_values

assert_equal '09', start_hour
assert_equal '00', start_minutes
end
end

test 'set current time when scheduled learning start time is in the future' do
travel_to Time.zone.local(2025, 1, 1, 10, 0, 0) do
user = users(:kimura)
one_hour_since = 1.hour.since
week_day = LearningTimeFrame::WEEK_DAY_NAMES_JA[one_hour_since.wday]
frame_hour = one_hour_since.hour
frame = LearningTimeFrame.find_by!(week_day: week_day, activity_time: frame_hour)
LearningTimeFramesUser.create!(user: user, learning_time_frame: frame)

visit_with_auth '/reports/new', 'kimura'

start_hour, start_minutes = learning_start_time_select_values

assert_equal '10', start_hour
assert_equal '00', start_minutes
end
end

private

def learning_start_time_select_values
hour_select, minutes_select = first('.learning-time').all('.learning-time__started-at select')
[hour_select.value, minutes_select.value]
end
end
end
Loading