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
Add entity_type and entity_id columns
Klaus Zanders3c2618ceace5
diff --git a/modules/costs/app/models/time_entry.rb b/modules/costs/app/models/time_entry.rbindex ee502d13d4c..325c4d5977c 100644--- a/modules/costs/app/models/time_entry.rb+++ b/modules/costs/app/models/time_entry.rb@@ -29,10 +29,12 @@#++class TimeEntry < ApplicationRecord+ ALLOWED_ENTITY_TYPES = %w[WorkPackage Meeting].freeze+# could have used polymorphic association# project association here allows easy loading of time entries at project level with one database tripbelongs_to :project- belongs_to :work_package+ belongs_to :entity, polymorphic: truebelongs_to :userbelongs_to :activity, class_name: "TimeEntryActivity"belongs_to :rate, -> { where(type: %w[HourlyRate DefaultHourlyRate]) }, class_name: "Rate"@@ -76,7 +78,10 @@ class TimeEntry < ApplicationRecord},allow_blank: true- scope :on_work_packages, ->(work_packages) { where(work_package_id: work_packages) }+ validates :entity_type,+ inclusion: { in: ALLOWED_ENTITY_TYPES }++ scope :on_work_packages, ->(work_packages) { where(entity: work_packages) }extend ::TimeEntries::TimeEntryScopesinclude ::Scopes::Scoped@@ -110,6 +115,19 @@ class TimeEntry < ApplicationRecordendend+ def work_package+ OpenProject::Deprecation.replaced(:work_package, :entity, caller_locations)++ if entity_type == "WorkPackage"+ entity+ end+ end++ def work_package=(value)+ OpenProject::Deprecation.replaced(:work_package=, :entity=, caller_locations)+ self.entity = value+ end+def hours=(value)write_attribute :hours, (value.is_a?(String) ? (value.to_hours || value) : value)enddiff --git a/modules/costs/db/migrate/20250416095154_add_entity_to_time_entry.rb b/modules/costs/db/migrate/20250416095154_add_entity_to_time_entry.rbnew file mode 100644index 00000000000..a6a0c4e2c9d--- /dev/null+++ b/modules/costs/db/migrate/20250416095154_add_entity_to_time_entry.rb@@ -0,0 +1,26 @@+# frozen_string_literal: true++class AddEntityToTimeEntry < ActiveRecord::Migration[8.0]+ def up+ add_reference :time_entries, :entity, polymorphic: true, index: true+ add_reference :time_entry_journals, :entity, polymorphic: true, index: true++ execute <<~SQL.squish+ UPDATE time_entries+ SET+ entity_type = 'WorkPackage',+ entity_id = work_package_id+ WHERE+ work_package_id IS NOT NULL;+ SQL++ execute <<~SQL.squish+ UPDATE time_entry_journals+ SET+ entity_type = 'WorkPackage',+ entity_id = work_package_id+ WHERE+ work_package_id IS NOT NULL;+ SQL+ end+enddiff --git a/modules/costs/spec/factories/time_entry_factory.rb b/modules/costs/spec/factories/time_entry_factory.rbindex 413839143bf..0759b812aaf 100644--- a/modules/costs/spec/factories/time_entry_factory.rb+++ b/modules/costs/spec/factories/time_entry_factory.rb@@ -29,19 +29,19 @@FactoryBot.define dofactory :time_entry douser- work_package+ entity factory: :work_packagespent_on { Time.zone.today }activity factory: :time_entry_activityhours { 1.0 }logged_by { user }after(:build) do |time_entry|- time_entry.project ||= time_entry.work_package.project+ time_entry.project ||= time_entry.entity.projectendafter(:create) do |time_entry|- if time_entry.work_package.present?- time_entry.update(project: time_entry.work_package.project)+ if time_entry.entity.present?+ time_entry.update(project: time_entry.entity.project)end# ensure user is member of project@@ -56,5 +56,9 @@ FactoryBot.define dostart_time { 390 } # 6:30 AMhours { 2.5 }end++ trait :on_meeting do+ entity factory: :meeting+ endendend