Skip to content

Commit

Permalink
Refactor plugin, add support of disallowing track time in future
Browse files Browse the repository at this point in the history
  • Loading branch information
Dmytro Litvinov committed Feb 22, 2019
1 parent 1208012 commit d12004f
Show file tree
Hide file tree
Showing 5 changed files with 21 additions and 10 deletions.
5 changes: 5 additions & 0 deletions app/views/settings/_log_hours_restrictions_settings.html.erb
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,11 @@
<%= check_box_tag 'settings[do_not_track_hours_for_the_past_month]', true, @settings[:do_not_track_hours_for_the_past_month] %>
</p>

<p>
<%= label_tag 'do_not_track_in_future' %>
<%= check_box_tag 'settings[do_not_track_in_future]', true, @settings[:do_not_track_in_future] %>
</p>

<p>
<%= label_tag 'daily_hours_limit' %>
<%= number_field_tag "settings[daily_hours_limit]", @settings[:daily_hours_limit], :min => 0 %>
Expand Down
1 change: 1 addition & 0 deletions config/locales/en.yml
Original file line number Diff line number Diff line change
Expand Up @@ -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"
1 change: 1 addition & 0 deletions config/locales/ru.yml
Original file line number Diff line number Diff line change
Expand Up @@ -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: "К сожалению, вы не можете залогировать время в будущем"
1 change: 1 addition & 0 deletions init.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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
23 changes: 13 additions & 10 deletions lib/time_entry_restrictions_patch.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down

0 comments on commit d12004f

Please sign in to comment.