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 CostEntry#logged_by in fashion of time entries
Oliver Güntherc809153bb458
diff --git a/db/migrate/20221018160449_add_logged_by_to_cost_entries.rb b/db/migrate/20221018160449_add_logged_by_to_cost_entries.rbnew file mode 100644index 00000000000..5322cf99111--- /dev/null+++ b/db/migrate/20221018160449_add_logged_by_to_cost_entries.rb@@ -0,0 +1,15 @@+class AddLoggedByToCostEntries < ActiveRecord::Migration[7.0]+ def change+ add_reference :cost_entries, :logged_by, foreign_key: { to_table: :users }, index: true++ reversible do |dir|+ dir.up do+ CostEntry+ .where.not(user_id: User.select(:id))+ .update_all(user_id: DeletedUser.first.id)++ CostEntry.update_all('logged_by_id = user_id')+ end+ end+ end+enddiff --git a/modules/costs/app/models/cost_entry.rb b/modules/costs/app/models/cost_entry.rbindex 5e2fd8d9742..2cfa83eb284 100644--- a/modules/costs/app/models/cost_entry.rb+++ b/modules/costs/app/models/cost_entry.rb@@ -30,6 +30,7 @@ class CostEntry < ApplicationRecordbelongs_to :projectbelongs_to :work_packagebelongs_to :user+ belongs_to :logged_by, class_name: 'User'include ::Costs::DeletedUserFallbackbelongs_to :cost_typebelongs_to :budget@@ -37,7 +38,7 @@ class CostEntry < ApplicationRecordinclude ActiveModel::ForbiddenAttributesProtection- validates_presence_of :work_package_id, :project_id, :user_id, :cost_type_id, :units, :spent_on+ validates_presence_of :work_package_id, :project_id, :user_id, :logged_by_id, :cost_type_id, :units, :spent_onvalidates_numericality_of :units, allow_nil: false, message: :invalidvalidates_length_of :comments, maximum: 255, allow_nil: true@@ -53,7 +54,12 @@ class CostEntry < ApplicationRecordinclude Entry::SplashedDatesdef after_initialize- if new_record? && cost_type.nil? && default_cost_type = CostType.default+ return unless new_record?++ # This belongs in a SetAttributesService, but cost_entries are not yet created as such+ self.logged_by = User.current++ if cost_type.nil? && default_cost_type = CostType.defaultself.cost_type_id = default_cost_type.idendenddiff --git a/modules/costs/spec/models/cost_entry_spec.rb b/modules/costs/spec/models/cost_entry_spec.rbindex cd7800f3186..e90b8d4ba40 100644--- a/modules/costs/spec/models/cost_entry_spec.rb+++ b/modules/costs/spec/models/cost_entry_spec.rb@@ -361,6 +361,19 @@ describe CostEntry, type: :model doendend+ describe '#logged_by' do+ it 'validates' do+ cost_entry.logged_by = nil+ expect(cost_entry).not_to be_valid+ expect(cost_entry.errors[:logged_by_id]).to be_present+ end++ it 'sets logged_by from current user' do+ entry = User.execute_as(user2) { described_class.new logged_by: user }+ expect(entry.logged_by).to eq(user2)+ end+ end+describe '#editable_by?' dodescribe "WHEN the user has the edit_cost_entries permissionWHEN the cost entry is not created by the user" dodiff --git a/modules/reporting/app/models/cost_query/sql_statement.rb b/modules/reporting/app/models/cost_query/sql_statement.rbindex ad8eb2c92bb..3385f22b2a8 100644--- a/modules/reporting/app/models/cost_query/sql_statement.rb+++ b/modules/reporting/app/models/cost_query/sql_statement.rb@@ -111,7 +111,7 @@ class CostQuery::SqlStatement < Report::SqlStatement## @param [CostQuery::SqlStatement] query The statement to adjustdef self.unify_cost_entries(query)- query.select :units, :cost_type_id, activity_id: -1, logged_by_id: -1+ query.select :units, :cost_type_id, :logged_by_id, activity_id: -1query.select cost_type: "cost_types.name"query.join CostTypeend