Remove some redundant code.

This commit is contained in:
Klaus Zanders
2026-03-03 16:19:52 +01:00
parent 73f7e95a3d
commit b6b34a53fb
5 changed files with 10 additions and 18 deletions
@@ -74,10 +74,11 @@ module Users
end
def user_events
system_dates = non_working_times.grep(NonWorkingDay).to_set(&:date)
non_working_times
.grep(UserNonWorkingTime)
.map do |nwt|
clipped = nwt.clip_to_year(year)
clipped = nwt.clip_to_year(year, system_non_working_dates: system_dates)
{
start: clipped.start_date.iso8601,
end: (clipped.end_date + 1.day).iso8601,
@@ -40,10 +40,11 @@ module Users
private
def user_non_working_times
system_dates = non_working_times.grep(NonWorkingDay).to_set(&:date)
non_working_times
.grep(UserNonWorkingTime)
.sort_by(&:start_date)
.map { |nwt| nwt.clip_to_year(year) }
.map { |nwt| nwt.clip_to_year(year, system_non_working_dates: system_dates) }
end
def global_day_count
@@ -46,14 +46,7 @@ module UserNonWorkingTimes
private
def validate_manage_permission
unless can_manage_working_times?
errors.add :base, :error_unauthorized
end
end
def can_manage_working_times?
user.allowed_globally?(:manage_working_times) ||
(model.user_id == user.id && user.allowed_globally?(:manage_own_working_times))
errors.add(:base, :error_unauthorized) unless self.class.can_manage?(user:, target_user: model.user)
end
end
end
@@ -30,10 +30,7 @@
module UserNonWorkingTimes
class DeleteContract < ::DeleteContract
delete_permission -> {
user.allowed_globally?(:manage_working_times) ||
(model.user_id == user.id && user.allowed_globally?(:manage_own_working_times))
}
delete_permission -> { BaseContract.can_manage?(user:, target_user: model.user) }
def self.can_delete?(user:, target_user:)
BaseContract.can_manage?(user:, target_user:)
+4 -4
View File
@@ -77,7 +77,7 @@ class UserNonWorkingTime < ApplicationRecord
delegate :count, to: :working_days, prefix: true
def clip_to_year(year)
def clip_to_year(year, system_non_working_dates: nil)
year_start = Date.new(year, 1, 1)
year_end = Date.new(year, 12, 31)
@@ -88,7 +88,7 @@ class UserNonWorkingTime < ApplicationRecord
non_working_time: self,
start_date: clipped_start,
end_date: clipped_end,
working_days_count: working_days_in(clipped_start..clipped_end).count,
working_days_count: working_days_in(clipped_start..clipped_end, system_non_working_dates:).count,
continues_from_previous_year: start_date < year_start,
continues_into_next_year: end_date > year_end
)
@@ -96,9 +96,9 @@ class UserNonWorkingTime < ApplicationRecord
private
def working_days_in(date_range)
def working_days_in(date_range, system_non_working_dates: nil)
working_wdays = Setting.working_days.map { |d| d % 7 }
system_wide = NonWorkingDay.where(date: date_range).pluck(:date).to_set
system_wide = system_non_working_dates || NonWorkingDay.where(date: date_range).pluck(:date).to_set
date_range.select { |date| working_wdays.include?(date.wday) && system_wide.exclude?(date) }
end