diff --git a/app/models/report.rb b/app/models/report.rb index 333f7f7f325..3069a7fa787 100644 --- a/app/models/report.rb +++ b/app/models/report.rb @@ -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 @@ -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) @@ -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 diff --git a/app/views/reports/_form.html.slim b/app/views/reports/_form.html.slim index 8a52a3792dc..94274f02c0c 100644 --- a/app/views/reports/_form.html.slim +++ b/app/views/reports/_form.html.slim @@ -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 diff --git a/test/system/reports_test.rb b/test/system/reports_test.rb index f558eafe35a..f6a35884ce5 100644 --- a/test/system/reports_test.rb +++ b/test/system/reports_test.rb @@ -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