diff --git a/modules/resource_management/app/models/resource_allocation.rb b/modules/resource_management/app/models/resource_allocation.rb index 90bfae4c71c..115206a6455 100644 --- a/modules/resource_management/app/models/resource_allocation.rb +++ b/modules/resource_management/app/models/resource_allocation.rb @@ -62,6 +62,18 @@ class ResourceAllocation < ApplicationRecord entity&.project end + def entity_gid + entity&.to_gid.to_s + end + + def entity=(value) + if value.is_a?(String) && value.starts_with?("gid://") + super(GlobalID::Locator.locate(value, only: ALLOWED_ENTITY_TYPES.map(&:safe_constantize))) + else + super + end + end + def allocated_hours return if allocated_time.nil? diff --git a/modules/resource_management/spec/models/resource_allocation_spec.rb b/modules/resource_management/spec/models/resource_allocation_spec.rb index 9343f43a9c3..e95dc8d10d2 100644 --- a/modules/resource_management/spec/models/resource_allocation_spec.rb +++ b/modules/resource_management/spec/models/resource_allocation_spec.rb @@ -102,6 +102,47 @@ RSpec.describe ResourceAllocation do end end + describe "entity GlobalID handling" do + shared_let(:project) { create(:project) } + shared_let(:work_package) { create(:work_package, project:) } + + subject(:allocation) { described_class.new } + + describe "#entity_gid" do + it "returns the GlobalID string of the entity" do + allocation.entity = work_package + expect(allocation.entity_gid).to eq(work_package.to_gid.to_s) + end + + it "is an empty string when no entity is set" do + expect(allocation.entity_gid).to eq("") + end + end + + describe "#entity=" do + it "assigns a plain record directly" do + allocation.entity = work_package + expect(allocation.entity).to eq(work_package) + end + + it "resolves a GlobalID string to the record" do + allocation.entity = work_package.to_gid.to_s + expect(allocation.entity).to eq(work_package) + end + + it "round-trips an entity through entity_gid" do + allocation.entity = work_package.to_gid.to_s + expect(allocation.entity_gid).to eq(work_package.to_gid.to_s) + end + + it "ignores a GlobalID of a type outside ALLOWED_ENTITY_TYPES" do + disallowed = create(:user) + allocation.entity = disallowed.to_gid.to_s + expect(allocation.entity).to be_nil + end + end + end + describe "validations" do shared_let(:project) { create(:project, enabled_module_names: %w[resource_management]) } shared_let(:owner) { create(:user, member_with_permissions: { project => %i[view_resource_planners] }) }