From bff47a74c5740c52385b5ac956a13aa7228465b5 Mon Sep 17 00:00:00 2001 From: Marius BALTEANU Date: Mon, 2 Apr 2018 19:50:02 +0000 Subject: [PATCH] Setting to restrict spent times on future dates (#3322) --- app/models/time_entry.rb | 3 +++ app/views/settings/_timelog.html.erb | 2 ++ config/locales/en.yml | 2 ++ config/settings.yml | 2 ++ test/unit/time_entry_test.rb | 17 +++++++++++++++++ 5 files changed, 26 insertions(+) diff --git a/app/models/time_entry.rb b/app/models/time_entry.rb index d64c311..0956532 100644 --- a/app/models/time_entry.rb +++ b/app/models/time_entry.rb @@ -136,6 +136,9 @@ class TimeEntry < ActiveRecord::Base errors.add :project_id, :invalid if project.nil? errors.add :issue_id, :invalid if (issue_id && !issue) || (issue && project!=issue.project) || @invalid_issue_id errors.add :activity_id, :inclusion if activity_id_changed? && project && !project.activities.include?(activity) + if spent_on_changed? && user + errors.add :base, I18n.t(:error_spent_on_future_date) if (!Setting.timelog_accept_future_dates? && (spent_on > user.today)) + end end def hours=(h) diff --git a/app/views/settings/_timelog.html.erb b/app/views/settings/_timelog.html.erb index bc5f054..01d4238 100644 --- a/app/views/settings/_timelog.html.erb +++ b/app/views/settings/_timelog.html.erb @@ -7,6 +7,8 @@

<%= setting_text_field :timelog_max_hours_per_day, :size => 6 %>

<%= setting_check_box :timelog_accept_0_hours %>

+ +

<%= setting_check_box :timelog_accept_future_dates %>

diff --git a/config/locales/en.yml b/config/locales/en.yml index 36d2099..28da4ce 100644 --- a/config/locales/en.yml +++ b/config/locales/en.yml @@ -223,6 +223,7 @@ en: warning_fields_cleared_on_bulk_edit: "Changes will result in the automatic deletion of values from one or more fields on the selected objects" error_exceeds_maximum_hours_per_day: "Cannot log more than %{max_hours} hours on the same day (%{logged_hours} hours have already been logged)" error_can_not_delete_auth_source: "This authentication mode is in use and cannot be deleted." + error_spent_on_future_date: "Cannot log time on a future date" mail_subject_lost_password: "Your %{value} password" mail_body_lost_password: 'To change your password, click on the following link:' @@ -469,6 +470,7 @@ en: setting_time_entry_list_defaults: Timelog list defaults setting_timelog_accept_0_hours: Accept time logs with 0 hours setting_timelog_max_hours_per_day: Maximum hours that can be logged per day and user + setting_timelog_accept_future_dates: Accept time logs on future dates permission_add_project: Create project permission_add_subprojects: Create subprojects diff --git a/config/settings.yml b/config/settings.yml index 80946ec..b6c0261 100644 --- a/config/settings.yml +++ b/config/settings.yml @@ -308,3 +308,5 @@ timelog_accept_0_hours: timelog_max_hours_per_day: format: int default: 999 +timelog_accept_future_dates: + default: 1 diff --git a/test/unit/time_entry_test.rb b/test/unit/time_entry_test.rb index 9b3fbf7..2551c6f 100644 --- a/test/unit/time_entry_test.rb +++ b/test/unit/time_entry_test.rb @@ -119,6 +119,23 @@ class TimeEntryTest < ActiveSupport::TestCase end end + def test_should_accept_future_dates + entry = TimeEntry.generate + entry.spent_on = User.current.today + 1 + + assert entry.save + end + + def test_should_not_accept_future_dates_if_disabled + with_settings :timelog_accept_future_dates => '0' do + entry = TimeEntry.generate + entry.spent_on = User.current.today + 1 + + assert !entry.save + assert entry.errors[:base].present? + end + end + def test_spent_on_with_blank c = TimeEntry.new c.spent_on = '' -- 2.1.4