Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

日報の学習日入力部分に範囲バリデーションを追加した #8181

Open
wants to merge 10 commits into
base: main
Choose a base branch
from
Open
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
15 changes: 10 additions & 5 deletions app/models/report.rb
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ class Report < ApplicationRecord
validates :user, presence: true
validates :reported_on, presence: true, uniqueness: { scope: :user }
validates :emotion, presence: true
validate :reported_on_or_before_today
validate :limit_date_within_range

after_create ReportCallbacks.new
after_destroy ReportCallbacks.new
Expand Down Expand Up @@ -114,10 +114,6 @@ def total_learning_time
(learning_times.sum(&:diff) / 60).to_i
end

def reported_on_or_before_today
errors.add(:reported_on, 'は今日以前の日付にしてください') if reported_on > Date.current
end

def latest_of_user?
self == Report.not_wip
.where(user:, wip: false)
Expand All @@ -134,4 +130,13 @@ def not_wip_previous_of_user
.order(reported_on: :desc)
.second
end

private

def limit_date_within_range
min_date = Date.new(2013, 1, 1)
return if min_date <= reported_on && reported_on <= Date.current

errors.add(:reported_on, "は#{I18n.l min_date, format: :default}から今日以前の間の日付にしてください")
end
end
2 changes: 1 addition & 1 deletion app/views/reports/_form.html.slim
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@

.form-item
= f.label :reported_on, class: 'a-form-label'
= f.date_field :reported_on, class: 'a-text-input'
= f.date_field :reported_on, min: '2013-01-01', class: 'a-text-input'
.form-item.is-sm
= f.label :emotion, class: 'a-form-label'
ul.block-checks.is-3-items.is-inline
Expand Down
15 changes: 14 additions & 1 deletion test/system/reports_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -467,7 +467,20 @@ def wait_for_watch_change
fill_in('report[reported_on]', with: Date.current.next_day)
end
click_button '提出'
assert_text '学習日は今日以前の日付にしてください'
assert_text '学習日は2013年01月01日から今日以前の間の日付にしてください'
end

test 'cannot post a new report with min date' do
visit_with_auth '/reports/new', 'komagata'
within('form[name=report]') do
fill_in('report[title]', with: '学習日が2013年1月1日より前では日報を作成できない')
fill_in('report[description]', with: 'エラーになる')
fill_in('report[reported_on]', with: Date.new(2012, 12, 31))
end
click_button '提出'
html_validataion_message = page.find('#report_reported_on').native.attribute('validationMessage')
assert_not_nil html_validataion_message
assert_not_empty html_validataion_message
end

test 'display recently reports' do
Expand Down