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
Use where condition on all_available_custom_fields and move it to a private contract method.
Dombi Attila59bf8ccfc99d
diff --git a/app/contracts/projects/base_contract.rb b/app/contracts/projects/base_contract.rbindex 6a8c861eb32..a74d8a4e5b5 100644--- a/app/contracts/projects/base_contract.rb+++ b/app/contracts/projects/base_contract.rb@@ -75,14 +75,6 @@ module Projectsendend- def all_available_custom_fields- if user.admin?- model.all_available_custom_fields- else- model.all_available_custom_fields.reject(&:admin_only?)- end- end-delegate :assignable_versions, to: :modeldef assignable_status_codes@@ -156,5 +148,13 @@ module Projectsvalidate_and_merge_errors(contract)end++ def all_available_custom_fields+ if user.admin?+ model.all_available_custom_fields+ else+ model.all_available_custom_fields.where(admin_only: false)+ end+ endendenddiff --git a/spec/contracts/projects/update_contract_spec.rb b/spec/contracts/projects/update_contract_spec.rbindex 1f2fac270e6..a22deb1a3f1 100644--- a/spec/contracts/projects/update_contract_spec.rb+++ b/spec/contracts/projects/update_contract_spec.rb@@ -33,29 +33,9 @@ require_relative "shared_contract_examples"RSpec.describe Projects::UpdateContract doit_behaves_like "project contract" do- let(:custom_field) do- build_stubbed(:integer_project_custom_field).tap do |cf|- cf.id = "1001"- allow_any_instance_of(CustomValue) # rubocop:disable RSpec/AnyInstance- .to receive(:custom_field).and_return(cf)- end- end-- let(:admin_only_custom_field) do- build_stubbed(:integer_project_custom_field, admin_only: true).tap do |cf|- cf.id = "1002"- allow_any_instance_of(CustomValue) # rubocop:disable RSpec/AnyInstance- .to receive(:custom_field).and_return(cf)- end- end-- let(:not_enabled_custom_field) do- build_stubbed(:integer_project_custom_field).tap do |cf|- cf.id = "1003"- allow_any_instance_of(CustomValue) # rubocop:disable RSpec/AnyInstance- .to receive(:custom_field).and_return(cf)- end- end+ shared_let(:custom_field) { create(:integer_project_custom_field) }+ shared_let(:admin_only_custom_field) { create(:integer_project_custom_field, admin_only: true) }+ shared_let(:not_enabled_custom_field) { create(:integer_project_custom_field) }let(:project) dobuild_stubbed(:project,@@ -63,9 +43,13 @@ RSpec.describe Projects::UpdateContract dopublic: project_public,status_code: project_status_code,status_explanation: project_status_explanation).tap do |p|- allow(p).to receive_messages(available_custom_fields: [custom_field, admin_only_custom_field],- all_available_custom_fields: [custom_field, admin_only_custom_field,- not_enabled_custom_field])+ # Use real AR relations for the custom field associations with actual IDs+ available_custom_fields = ProjectCustomField.where(id: [custom_field, admin_only_custom_field])+ all_available_custom_fields = ProjectCustomField.where(id: [custom_field, admin_only_custom_field,+ not_enabled_custom_field])++ allow(p).to receive_messages(available_custom_fields:, all_available_custom_fields:)+next unless project_changed# in order to actually have something changed@@ -234,13 +218,23 @@ RSpec.describe Projects::UpdateContract doshared_examples "can write" do |attribute|it "can write #{attribute}" do- expect(contract.writable_attributes).to include(attribute.to_s)+ attr_name = if respond_to?(attribute)+ "custom_field_#{send(attribute).id}"+ else+ attribute.to_s+ end+ expect(contract.writable_attributes).to include(attr_name)endendshared_examples "can not write" do |attribute|it "can not write #{attribute}" do- expect(contract.writable_attributes).not_to include(attribute.to_s)+ attr_name = if respond_to?(attribute)+ "custom_field_#{send(attribute).id}"+ else+ attribute.to_s+ end+ expect(contract.writable_attributes).not_to include(attr_name)endend@@ -250,14 +244,14 @@ RSpec.describe Projects::UpdateContract docontext "when project_attributes_only flag is true" dolet(:options) { { project_attributes_only: true } }- it_behaves_like "can write", :custom_field_1001+ it_behaves_like "can write", :custom_fieldit_behaves_like "can not write", :nameendcontext "when project_attributes_only flag is false" dolet(:options) { { project_attributes_only: false } }- it_behaves_like "can write", :custom_field_1001+ it_behaves_like "can write", :custom_fieldit_behaves_like "can not write", :nameendend@@ -266,14 +260,14 @@ RSpec.describe Projects::UpdateContract docontext "when project_attributes_only flag is true" dolet(:options) { { project_attributes_only: true } }- it_behaves_like "can not write", :custom_field_1001+ it_behaves_like "can not write", :custom_fieldit_behaves_like "can not write", :nameendcontext "when project_attributes_only flag is false" dolet(:options) { { project_attributes_only: false } }- it_behaves_like "can not write", :custom_field_1001, "1"+ it_behaves_like "can not write", :custom_fieldit_behaves_like "can write", :nameendend@@ -284,14 +278,14 @@ RSpec.describe Projects::UpdateContract docontext "when project_attributes_only flag is true" dolet(:options) { { project_attributes_only: true } }- it_behaves_like "can write", :custom_field_1001+ it_behaves_like "can write", :custom_fieldit_behaves_like "can not write", :nameendcontext "when project_attributes_only flag is false" dolet(:options) { { project_attributes_only: false } }- it_behaves_like "can write", :custom_field_1001+ it_behaves_like "can write", :custom_fieldit_behaves_like "can write", :nameendend@@ -302,14 +296,14 @@ RSpec.describe Projects::UpdateContract docontext "when project_attributes_only flag is true" dolet(:options) { { project_attributes_only: true } }- it_behaves_like "can not write", :custom_field_1001+ it_behaves_like "can not write", :custom_fieldit_behaves_like "can not write", :nameendcontext "when project_attributes_only flag is false" dolet(:options) { { project_attributes_only: false } }- it_behaves_like "can not write", :custom_field_1001+ it_behaves_like "can not write", :custom_fieldit_behaves_like "can not write", :nameendend@@ -320,19 +314,19 @@ RSpec.describe Projects::UpdateContract dolet(:current_user) { build_stubbed(:admin) }let(:project_permissions) { %i(edit_project_attributes) }- it_behaves_like "can write", :custom_field_1002+ it_behaves_like "can write", :admin_only_custom_fieldendcontext "when user is not admin" dolet(:current_user) { build_stubbed(:user) }let(:project_permissions) { %i(edit_project_attributes) }- it_behaves_like "can not write", :custom_field_1002+ it_behaves_like "can not write", :admin_only_custom_fieldcontext "with all permissions" dolet(:project_permissions) { %i(edit_project edit_project_attributes) }- it_behaves_like "can not write", :custom_field_1002+ it_behaves_like "can not write", :admin_only_custom_fieldendendend@@ -358,19 +352,19 @@ RSpec.describe Projects::UpdateContract dolet(:current_user) { build_stubbed(:admin) }let(:project_permissions) { %i(edit_project_attributes) }- it_behaves_like "can not write", :custom_field_1003+ it_behaves_like "can not write", :not_enabled_custom_fieldendcontext "when user is not admin" dolet(:current_user) { build_stubbed(:user) }let(:project_permissions) { %i(edit_project_attributes) }- it_behaves_like "can not write", :custom_field_1003+ it_behaves_like "can not write", :not_enabled_custom_fieldcontext "with all permissions" dolet(:project_permissions) { %i(edit_project edit_project_attributes) }- it_behaves_like "can not write", :custom_field_1003+ it_behaves_like "can not write", :not_enabled_custom_fieldendendend@@ -382,19 +376,19 @@ RSpec.describe Projects::UpdateContract dolet(:current_user) { build_stubbed(:admin) }let(:project_permissions) { %i(edit_project edit_project_attributes) }- it_behaves_like "can write", :custom_field_1003+ it_behaves_like "can write", :not_enabled_custom_fieldendcontext "when user is not admin" dolet(:current_user) { build_stubbed(:user) }let(:project_permissions) { %i(edit_project_attributes) }- it_behaves_like "can not write", :custom_field_1003+ it_behaves_like "can not write", :not_enabled_custom_fieldcontext "with all permissions" dolet(:project_permissions) { %i(edit_project edit_project_attributes) }- it_behaves_like "can write", :custom_field_1003+ it_behaves_like "can write", :not_enabled_custom_fieldendendend