OpenProject is the leading open source project management software for product, project and portfolio management. A powerful Jira alternative with agile planning, issue tracking, roadmaps, Gantt charts, time tracking, collaboration features, and more. Available on premises or in the cloud. ⭐ Star us on GitHub
Ruby agile-developmentangularbcfboardsbug-trackerganttgantt-chartifcissue-trackerjira-alternativekanbanopenprojectproduct-managementproject-managementproject-planningroadmaprubyscrumtimelineworkflows
refactor: Extract class TimeTrackingAttributes to handle calculations
Christophe Bliard9b75335c1e0a
diff --git a/app/seeders/demo_data/work_package_seeder.rb b/app/seeders/demo_data/work_package_seeder.rbindex a2caa5ea0e8..997692f0228 100644--- a/app/seeders/demo_data/work_package_seeder.rb+++ b/app/seeders/demo_data/work_package_seeder.rb@@ -146,13 +146,7 @@ module DemoDataenddef set_time_tracking_attributes!(wp_attr, attributes)- start_date = calculate_start_date(attributes[:start])-- wp_attr[:start_date] = start_date- wp_attr[:due_date] = calculate_due_date(start_date, attributes[:duration])- wp_attr[:ignore_non_working_days] = calculate_ignore_non_working_days(wp_attr)- wp_attr[:duration] = calculate_duration(wp_attr)- wp_attr[:estimated_hours] = attributes[:estimated_hours].to_i if attributes[:estimated_hours]+ wp_attr.merge!(TimeTrackingAttributes.for(attributes))enddef set_backlogs_attributes!(wp_attr, attributes)@@ -201,30 +195,63 @@ module DemoDataRelation.create!(from:, to:, relation_type: type)end- def calculate_start_date(days_ahead)- Time.zone.today.monday + (days_ahead || 0).days- end+ class TimeTrackingAttributes+ def self.for(attributes)+ new(attributes).work_package_attributes+ end- # Returns the due date based on the starting date and the duration.- def calculate_due_date(date, duration)- WorkPackages::Shared::AllDays.new.due_date(date, duration)- end+ def work_package_attributes+ {+ start_date:,+ due_date:,+ duration:,+ ignore_non_working_days:,+ estimated_hours:+ }+ end- def calculate_ignore_non_working_days(work_package_attributes)- work_package_attributes- .values_at(:start_date, :due_date)- .compact- .any? { |date| working_days.non_working?(date) }- end+ private- def calculate_duration(work_package_attributes)- start_date, due_date, ignore_non_working_days = work_package_attributes.values_at(:start_date, :due_date, :ignore_non_working_days)- days = WorkPackages::Shared::Days.for_ignore_non_working_days(ignore_non_working_days)- days.duration(start_date, due_date)- end+ attr_reader :attributes++ def initialize(attributes)+ @attributes = attributes+ end++ def start_date+ days_ahead = attributes[:start] || 0+ Time.zone.today.monday + days_ahead.days+ end++ def due_date+ all_days.due_date(start_date, attributes[:duration])+ end++ def duration+ days.duration(start_date, due_date)+ end++ def ignore_non_working_days+ [start_date, due_date]+ .compact+ .any? { |date| working_days.non_working?(date) }+ end++ def estimated_hours+ attributes[:estimated_hours]&.to_i+ end++ def all_days+ @all_days ||= WorkPackages::Shared::AllDays.new+ end- def working_days- @working_days ||= WorkPackages::Shared::WorkingDays.new+ def working_days+ @working_days ||= WorkPackages::Shared::WorkingDays.new+ end++ def days+ ignore_non_working_days ? all_days : working_days+ endendendenddiff --git a/app/services/work_packages/shared/days.rb b/app/services/work_packages/shared/days.rbindex 71cedd06086..b74c722aa43 100644--- a/app/services/work_packages/shared/days.rb+++ b/app/services/work_packages/shared/days.rb@@ -31,11 +31,7 @@ module WorkPackagesclass Days# Returns the right day computation instance for the given instance.def self.for(work_package)- for_ignore_non_working_days(work_package.ignore_non_working_days)- end-- def self.for_ignore_non_working_days(ignore_non_working_days)- ignore_non_working_days ? AllDays.new : WorkingDays.new+ work_package.ignore_non_working_days ? AllDays.new : WorkingDays.newendendend