From d12004f8e99c4402795b2aa8c05c98e3b42c590d Mon Sep 17 00:00:00 2001
From: Dmytro Litvinov
+ <%= label_tag 'do_not_track_in_future' %> + <%= check_box_tag 'settings[do_not_track_in_future]', true, @settings[:do_not_track_in_future] %> +
+<%= label_tag 'daily_hours_limit' %> <%= number_field_tag "settings[daily_hours_limit]", @settings[:daily_hours_limit], :min => 0 %> diff --git a/config/locales/en.yml b/config/locales/en.yml index d7e968f..af97081 100644 --- a/config/locales/en.yml +++ b/config/locales/en.yml @@ -4,3 +4,4 @@ en: time_entry_restiction_week: "You can not log hours for the past week and earlier" time_entry_restiction_mounth: "You can not log hours for the past month and earlier" time_entry_restiction_day_per_day: "You can not log more than %{num} hours per day" + time_entry_restiction_future_day: "Unfortunately, you can not log in future" diff --git a/config/locales/ru.yml b/config/locales/ru.yml index 53c2d07..f527e1a 100644 --- a/config/locales/ru.yml +++ b/config/locales/ru.yml @@ -4,3 +4,4 @@ ru: time_entry_restiction_week: "Вы не можете залогировать время за прошлую неделю и ранее" time_entry_restiction_mounth: "Вы не можете залогировать время за прошлый месяц и ранее" time_entry_restiction_day_per_day: "Вы не можете залогировать больше чем %{num} часов за сегодня" + time_entry_restiction_future_day: "К сожалению, вы не можете залогировать время в будущем" diff --git a/init.rb b/init.rb index 5529d0b..0322295 100644 --- a/init.rb +++ b/init.rb @@ -16,6 +16,7 @@ :do_not_track_past_hours => true, :do_not_track_hours_for_the_past_week => true, :do_not_track_hours_for_the_past_month => true, + :do_not_track_in_future => true, :daily_hours_limit => 0, }) end \ No newline at end of file diff --git a/lib/time_entry_restrictions_patch.rb b/lib/time_entry_restrictions_patch.rb index 782f1af..e7743fe 100644 --- a/lib/time_entry_restrictions_patch.rb +++ b/lib/time_entry_restrictions_patch.rb @@ -10,35 +10,38 @@ def self.included(base) unloadable validates_each :hours do |record, attr, value| - today = Date.today - monday = today - (today.wday - 1) % 7 + today = Date.today # need for "past hours" validaiton + monday = today - (today.wday - 1) % 7 # need for "past week" validation user = User.current - usr = user if !Setting.plugin_redmine_log_hours_restrictions['daily_hours_limit'].to_i.zero? - record.errors.add attr, I18n.t(:time_entry_restiction_day_per_day, :num => Setting.plugin_redmine_log_hours_restrictions['daily_hours_limit']) if !can_log_hours?(user, record) + record.errors.add :base, I18n.t(:time_entry_restiction_day_per_day, :num => Setting.plugin_redmine_log_hours_restrictions['daily_hours_limit']) if !can_log_hours?(user, record) + end + + if Setting.plugin_redmine_log_hours_restrictions['do_not_track_in_future'] and record.spent_on > today + record.errors.add :base, I18n.t(:time_entry_restiction_future_day) end # Check past hours if Setting.plugin_redmine_log_hours_restrictions['do_not_track_past_hours'] and record.spent_on < today - record.errors.add attr, I18n.t(:time_entry_restiction_day) + record.errors.add :base, I18n.t(:time_entry_restiction_day) # Check past week and earlier elsif Setting.plugin_redmine_log_hours_restrictions['do_not_track_hours_for_the_past_week'] and record.spent_on < monday - record.errors.add attr, I18n.t(:time_entry_restiction_week) + record.errors.add :base, I18n.t(:time_entry_restiction_week) # Check previous month and earlier elsif Setting.plugin_redmine_log_hours_restrictions['do_not_track_hours_for_the_past_month'] and record.spent_on < Date.today.at_beginning_of_month - record.errors.add attr, I18n.t(:time_entry_restiction_mounth) + record.errors.add :base, I18n.t(:time_entry_restiction_mounth) end end class << base - def can_log_hours?(usr, record) + def can_log_hours?(user, record) if record.new_record? - logged_hours = TimeEntry.where(:user_id => usr.id).where('DATE(spent_on) = ?', record.spent_on) + logged_hours = TimeEntry.where(:user_id => user.id).where('DATE(spent_on) = ?', record.spent_on) else - logged_hours = TimeEntry.where(:user_id => usr.id).where('DATE(spent_on) = ?', record.spent_on).where(TimeEntry.arel_table[:id].not_eq(record.id)) + logged_hours = TimeEntry.where(:user_id => user.id).where('DATE(spent_on) = ?', record.spent_on).where(TimeEntry.arel_table[:id].not_eq(record.id)) time_entry_hours_before_edit = TimeEntry.where(:id => record.id).first.hours if time_entry_hours_before_edit > record.hours: return true