Skip to content

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

fix contracts

Klaus Zanders4f85b450c845
diff --git a/app/models/work_packages/costs.rb b/app/models/work_packages/costs.rb
index 617aa805410..2ac797d5903 100644
--- a/app/models/work_packages/costs.rb
+++ b/app/models/work_packages/costs.rb
@@ -31,7 +31,7 @@ module WorkPackages::Costs
included do
belongs_to :budget, inverse_of: :work_packages, optional: true
- has_many :cost_entries, dependent: :delete_all
+ has_many :cost_entries, dependent: :delete_all, inverse_of: :entity
# disabled for now, implements part of ticket blocking
validate :validate_budget
@@ -82,7 +82,7 @@ module WorkPackages::Costs
return unless saved_change_to_project_id?
CostEntry
- .where(work_package_id: id)
+ .where(entity: self)
.update_all(project_id:)
end
end
@@ -125,13 +125,13 @@ module WorkPackages::Costs
false
else
- condition = "work_package_id = #{reassign_to.id}, project_id = #{reassign_to.project_id}"
- ::WorkPackage.update_cost_entries(work_packages.map(&:id), condition)
+ condition = "entity_type='WorkPackage', entity_id = #{reassign_to.id}, project_id = #{reassign_to.project_id}"
+ ::WorkPackage.update_cost_entries(work_packages, condition)
end
end
def update_cost_entries(work_packages, action)
- CostEntry.where(work_package_id: work_packages).update_all(action)
+ CostEntry.where(entity: work_packages).update_all(action)
end
end
end
diff --git a/modules/costs/app/components/time_entries/work_package_form.rb b/modules/costs/app/components/time_entries/entity_form.rb
similarity index 95%
rename from modules/costs/app/components/time_entries/work_package_form.rb
rename to modules/costs/app/components/time_entries/entity_form.rb
index e0d460237f1..ec53256469c 100644
--- a/modules/costs/app/components/time_entries/work_package_form.rb
+++ b/modules/costs/app/components/time_entries/entity_form.rb
@@ -29,7 +29,7 @@
#++
module TimeEntries
- class WorkPackageForm < ApplicationForm
+ class EntityForm < ApplicationForm
def initialize(visible: true, limit_to_project_id: nil)
super()
@visible = visible
@@ -39,9 +39,11 @@ module TimeEntries
form do |f|
f.hidden name: :show_work_package, value: @visible
f.hidden name: :limit_to_project_id, value: @limit_to_project_id
+ # TODO: Use global ID in the autocompleter
+ f.hidden name: :entity_type, value: "WorkPackage"
if show_work_package_field?
- f.work_package_autocompleter name: :work_package_id,
+ f.work_package_autocompleter name: :entity_id,
label: TimeEntry.human_attribute_name(:work_package),
required: work_package_required?,
validation_message: work_package_validation_error,
diff --git a/modules/costs/app/components/time_entries/time_entry_form_component.html.erb b/modules/costs/app/components/time_entries/time_entry_form_component.html.erb
index 58cdec812cf..7f677c8015b 100644
--- a/modules/costs/app/components/time_entries/time_entry_form_component.html.erb
+++ b/modules/costs/app/components/time_entries/time_entry_form_component.html.erb
@@ -36,7 +36,7 @@ Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
body.with_row { render(TimeEntries::DaysAndHoursForm.new(form)) }
body.with_row(display: show_work_package ? :block : :none) do
render(
- TimeEntries::WorkPackageForm.new(form, visible: show_work_package, limit_to_project_id: limit_to_project_id)
+ TimeEntries::EntityForm.new(form, visible: show_work_package, limit_to_project_id: limit_to_project_id)
)
end
body.with_row { render(TimeEntries::ActivityForm.new(form)) }
diff --git a/modules/costs/app/contracts/time_entries/base_contract.rb b/modules/costs/app/contracts/time_entries/base_contract.rb
index 6bb5e6b6d1b..adaf2400124 100644
--- a/modules/costs/app/contracts/time_entries/base_contract.rb
+++ b/modules/costs/app/contracts/time_entries/base_contract.rb
@@ -33,7 +33,7 @@ module TimeEntries
include AssignableValuesContract
include AssignableCustomFieldValues
- delegate :work_package,
+ delegate :entity,
:project,
:available_custom_fields,
:new_record?,
@@ -54,7 +54,9 @@ module TimeEntries
unless: Proc.new { spent_on.blank? }
attribute :project_id
- attribute :work_package_id
+ attribute :entity
+ attribute :entity_id
+ attribute :entity_type
attribute :activity_id do
validate_activity_active
end
@@ -85,21 +87,18 @@ module TimeEntries
# Necessary for custom fields of type version.
def assignable_versions(only_open: true)
- work_package.try(:assignable_versions, only_open:) || project.try(:assignable_versions, only_open:) || []
+ entity.try(:assignable_versions, only_open:) || project.try(:assignable_versions, only_open:) || []
end
private
def validate_entity
- return unless model.entity || model.entity_changed?
-
if model.entity.is_a?(WorkPackage)
if work_package_invisible? || work_package_not_in_project?
errors.add :entity, :invalid
end
- elsif model.entity.is_a?(TimeEntry)
- # TODO: Add validation for time entry
- true
+ elsif model.entity.is_a?(Meeting)
+ # TODO: Add validation for meeting
end
end
diff --git a/modules/costs/app/contracts/time_entries/create_contract.rb b/modules/costs/app/contracts/time_entries/create_contract.rb
index 8aa141a151d..cfaeeca9900 100644
--- a/modules/costs/app/contracts/time_entries/create_contract.rb
+++ b/modules/costs/app/contracts/time_entries/create_contract.rb
@@ -37,7 +37,7 @@ module TimeEntries
return if allowed_to_log_for_others?
return if allowed_to_log_to_himself?
- errors.add :work_package, :cannot_log_for_this_work_package
+ errors.add :entity, :cannot_log_for_this_work_package
errors.add :base, :error_unauthorized
end
@@ -46,7 +46,15 @@ module TimeEntries
end
def allowed_to_log_to_himself?
- model.user == user && user.allowed_in_work_package?(:log_own_time, model.work_package)
+ model.user == user && allowed_to_log_own?
+ end
+
+ def allowed_to_log_own?
+ case model.entity
+ when WorkPackage then user.allowed_in_work_package?(:log_own_time, model.entity)
+ when Meeting then user.allowed_in_project?(:log_own_time, model.project)
+ else false
+ end
end
end
end
diff --git a/modules/costs/app/contracts/time_entries/delete_contract.rb b/modules/costs/app/contracts/time_entries/delete_contract.rb
index 23ff697b2f5..7efa66e810f 100644
--- a/modules/costs/app/contracts/time_entries/delete_contract.rb
+++ b/modules/costs/app/contracts/time_entries/delete_contract.rb
@@ -30,8 +30,17 @@ module TimeEntries
class DeleteContract < ::DeleteContract
delete_permission -> {
edit_all = user.allowed_in_project?(:edit_time_entries, model.project)
- edit_own = user.allowed_in_work_package?(:edit_own_time_entries, model.work_package)
- edit_ongoing = model.ongoing && user.allowed_in_work_package?(:log_own_time, model.work_package)
+ edit_own = if model.entity.is_a?(WorkPackage)
+ user.allowed_in_work_package?(:edit_own_time_entries, model.entity)
+ else
+ user.allowed_in_project?(:edit_own_time_entries, model.project)
+ end
+ edit_ongoing = if model.entity.is_a?(WorkPackage)
+ model.ongoing && user.allowed_in_work_package?(:log_own_time, model.entity)
+ else
+ # TODO: Ongoing on meeting?
+ false
+ end
if model.user == user
edit_own || edit_all || edit_ongoing
diff --git a/modules/costs/app/contracts/time_entries/update_contract.rb b/modules/costs/app/contracts/time_entries/update_contract.rb
index 7084f2ddd88..1dbb41bd739 100644
--- a/modules/costs/app/contracts/time_entries/update_contract.rb
+++ b/modules/costs/app/contracts/time_entries/update_contract.rb
@@ -57,15 +57,25 @@ module TimeEntries
private
def user_allowed_to_update_in?(time_entry)
- user.allowed_in_project?(:edit_time_entries, time_entry.project) ||
- (model.user == user && user.allowed_in_work_package?(:edit_own_time_entries, time_entry.work_package))
+ user.allowed_in_project?(:edit_time_entries, time_entry.project) || (model.user == user && edit_own?(time_entry))
end
def user_allowed_to_modify_ongoing?(time_entry)
- model.user == user && (
- user.allowed_in_project?(:log_time, time_entry.project) ||
- user.allowed_in_work_package?(:log_own_time, time_entry.work_package)
- )
+ model.user == user && (user.allowed_in_project?(:log_time, time_entry.project) || log_own?(time_entry))
+ end
+
+ def edit_own?(time_entry)
+ case time_entry.entity
+ when WorkPackage then user.allowed_in_work_package?(:edit_own_time_entries, time_entry.entity)
+ when Meeting then user.allowed_in_project?(:edit_own_time_entries, time_entry.project)
+ end
+ end
+
+ def log_own?(time_entry)
+ case time_entry.entity
+ when WorkPackage then user.allowed_in_work_package?(:log_own_time, time_entry.entity)
+ when Meeting then user.allowed_in_project?(:log_own_time, time_entry.project)
+ end
end
end
end
diff --git a/modules/costs/lib/costs/patches/permitted_params_patch.rb b/modules/costs/lib/costs/patches/permitted_params_patch.rb
index 6a770885345..1783584042d 100644
--- a/modules/costs/lib/costs/patches/permitted_params_patch.rb
+++ b/modules/costs/lib/costs/patches/permitted_params_patch.rb
@@ -77,7 +77,8 @@ module Costs::Patches::PermittedParamsPatch
:hours,
:comments,
:spent_on,
- :work_package_id,
+ :entity_type,
+ :entity_id,
:activity_id,
:project_id,
:issue_id,
diff --git a/modules/costs/spec/contracts/time_entries/create_contract_spec.rb b/modules/costs/spec/contracts/time_entries/create_contract_spec.rb
index 8f189c446ad..df2379d5ba5 100644
--- a/modules/costs/spec/contracts/time_entries/create_contract_spec.rb
+++ b/modules/costs/spec/contracts/time_entries/create_contract_spec.rb
@@ -37,7 +37,7 @@ RSpec.describe TimeEntries::CreateContract do
let(:time_entry) do
TimeEntry.new(project: time_entry_project,
- work_package: time_entry_work_package,
+ entity: time_entry_entity,
user: time_entry_user,
activity: time_entry_activity,
spent_on: time_entry_spent_on,
@@ -63,7 +63,7 @@ RSpec.describe TimeEntries::CreateContract do
it "is invalid" do
expect_valid(false,
- work_package: %i(cannot_log_for_this_work_package),
+ entity: %i(cannot_log_for_this_work_package),
base: %i(error_unauthorized))
end
end
@@ -90,7 +90,7 @@ RSpec.describe TimeEntries::CreateContract do
it "is invalid" do
expect_valid(false,
- work_package: %i(cannot_log_for_this_work_package),
+ entity: %i(cannot_log_for_this_work_package),
base: %i(error_unauthorized))
end
end
@@ -109,7 +109,7 @@ RSpec.describe TimeEntries::CreateContract do
it "is invalid" do
expect_valid(false,
- work_package: %i(cannot_log_for_this_work_package),
+ entity: %i(cannot_log_for_this_work_package),
base: %i(error_unauthorized))
end
end
@@ -136,7 +136,7 @@ RSpec.describe TimeEntries::CreateContract do
it "is invalid" do
expect_valid(false,
- work_package: %i(cannot_log_for_this_work_package),
+ entity: %i(cannot_log_for_this_work_package),
base: %i(error_unauthorized))
end
end
diff --git a/modules/costs/spec/contracts/time_entries/delete_contract_spec.rb b/modules/costs/spec/contracts/time_entries/delete_contract_spec.rb
index 30a25890992..0bba14c6291 100644
--- a/modules/costs/spec/contracts/time_entries/delete_contract_spec.rb
+++ b/modules/costs/spec/contracts/time_entries/delete_contract_spec.rb
@@ -31,7 +31,7 @@ require "spec_helper"
RSpec.describe TimeEntries::DeleteContract do
let(:current_user) { build_stubbed(:user) }
let(:other_user) { build_stubbed(:user) }
- let(:time_entry_work_package) { build_stubbed(:work_package, project: time_entry_project) }
+ let(:time_entry_entity) { build_stubbed(:work_package, project: time_entry_project) }
let(:time_entry_project) { build_stubbed(:project) }
let(:time_entry_user) { current_user }
let(:time_entry_activity) { build_stubbed(:time_entry_activity) }
@@ -44,7 +44,7 @@ RSpec.describe TimeEntries::DeleteContract do
let(:time_entry) do
build_stubbed(:time_entry,
project: time_entry_project,
- work_package: time_entry_work_package,
+ entity: time_entry_entity,
user: time_entry_user,
ongoing: time_entry_ongoing,
activity: time_entry_activity,
@@ -58,7 +58,7 @@ RSpec.describe TimeEntries::DeleteContract do
mock.allow_in_project *permissions, project: time_entry_project
end
- allow(time_entry_work_package)
+ allow(time_entry_entity)
.to receive(:visible?)
.with(current_user)
.and_return(work_package_visible)
diff --git a/modules/costs/spec/contracts/time_entries/shared_contract_examples.rb b/modules/costs/spec/contracts/time_entries/shared_contract_examples.rb
index bba2f60b038..02358bd2bdd 100644
--- a/modules/costs/spec/contracts/time_entries/shared_contract_examples.rb
+++ b/modules/costs/spec/contracts/time_entries/shared_contract_examples.rb
@@ -37,15 +37,10 @@ RSpec.shared_examples_for "time entry contract" do
end
let(:other_user) { build_stubbed(:user) }
- let(:time_entry_work_package) do
- build_stubbed(:work_package,
- project: time_entry_project)
- end
+ let(:time_entry_entity) { build_stubbed(:work_package, project: time_entry_project) }
let(:time_entry_project) { build_stubbed(:project) }
let(:time_entry_user) { current_user }
- let(:time_entry_activity) do
- build_stubbed(:time_entry_activity)
- end
+ let(:time_entry_activity) { build_stubbed(:time_entry_activity) }
let(:time_entry_activity_active) { true }
let(:time_entry_spent_on) { Time.zone.today }
let(:time_entry_hours) { 5 }
@@ -68,8 +63,8 @@ RSpec.shared_examples_for "time entry contract" do
end
before do
- if time_entry_work_package
- allow(time_entry_work_package)
+ if time_entry_entity
+ allow(time_entry_entity)
.to receive(:visible?)
.with(current_user)
.and_return(work_package_visible)
@@ -120,17 +115,20 @@ RSpec.shared_examples_for "time entry contract" do
it_behaves_like "is valid"
context "when the work_package is within a different project than the provided project" do
- let(:time_entry_work_package) { build_stubbed(:work_package) }
+ let(:another_project) { build_stubbed(:project) }
+ let(:time_entry_entity) { build_stubbed(:work_package, project: another_project) }
it "is invalid" do
- expect_valid(false, work_package_id: %i(invalid))
+ expect_valid(false, entity: %i(invalid))
end
end
context "when the work_package is nil" do
- let(:time_entry_work_package) { nil }
+ let(:time_entry_entity) { nil }
- it_behaves_like "is valid"
+ it "is invalid" do
+ expect_valid(false, entity: %i(blank))
+ end
end
context "when the project is nil" do
@@ -269,8 +267,8 @@ RSpec.shared_examples_for "time entry contract" do
.and_return project_versions
end
- if time_entry_work_package
- allow(time_entry_work_package)
+ if time_entry_entity
+ allow(time_entry_entity)
.to receive(:assignable_versions)
.and_return wp_versions
end
@@ -278,7 +276,7 @@ RSpec.shared_examples_for "time entry contract" do
context "if no project and no work package is set" do
let(:time_entry_project) { nil }
- let(:time_entry_work_package) { nil }
+ let(:time_entry_entity) { nil }
it "is empty" do
expect(contract.assignable_versions)
@@ -287,7 +285,7 @@ RSpec.shared_examples_for "time entry contract" do
end
context "if a project is set but no work package" do
- let(:time_entry_work_package) { nil }
+ let(:time_entry_entity) { nil }
it "returns assignable_versions of the project" do
expect(contract.assignable_versions)
diff --git a/modules/costs/spec/contracts/time_entries/update_contract_spec.rb b/modules/costs/spec/contracts/time_entries/update_contract_spec.rb
index db5ebc848fb..a3d6193dca1 100644
--- a/modules/costs/spec/contracts/time_entries/update_contract_spec.rb
+++ b/modules/costs/spec/contracts/time_entries/update_contract_spec.rb
@@ -34,7 +34,7 @@ RSpec.describe TimeEntries::UpdateContract do
let(:time_entry) do
build_stubbed(:time_entry,
project: time_entry_project,
- work_package: time_entry_work_package,
+ entity: time_entry_entity,
user: time_entry_user,
activity: time_entry_activity,
spent_on: time_entry_spent_on,
@@ -73,11 +73,11 @@ RSpec.describe TimeEntries::UpdateContract do
end
end
- time_entry_work_package.project = new_project
+ time_entry_entity.project = new_project
mock_permissions_for(current_user) do |mock|
- mock.allow_in_project *new_project_permissions, project: new_project
- mock.allow_in_project *permissions, project: time_entry_project
+ mock.allow_in_project(*new_project_permissions, project: new_project)
+ mock.allow_in_project(*permissions, project: time_entry_project)
end
end
end
diff --git a/modules/costs/spec/factories/cost_entry_factory.rb b/modules/costs/spec/factories/cost_entry_factory.rb
index 7620571be93..32c86bb81f3 100644
--- a/modules/costs/spec/factories/cost_entry_factory.rb
+++ b/modules/costs/spec/factories/cost_entry_factory.rb
@@ -32,7 +32,7 @@ FactoryBot.define do
user
entity factory: :work_package
cost_type
- spent_on { Date.today }
+ spent_on { Date.current }
units { 1 }
comments { "" }