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

use value parameter to ensure toggling to the correct state in project custom field mapping

Ivan Kuchin49ee96a500a1
diff --git a/app/models/permitted_params.rb b/app/models/permitted_params.rb
index 533c873ede5..c0ff87a2288 100644
--- a/app/models/permitted_params.rb
+++ b/app/models/permitted_params.rb
@@ -317,6 +317,7 @@ class PermittedParams
def project_custom_field_project_mapping
params.require(:project_custom_field_project_mapping)
.permit(*self.class.permitted_attributes[:project_custom_field_project_mapping])
+ .merge(params.permit(:value))
end
def news
diff --git a/app/services/project_custom_field_project_mappings/toggle_service.rb b/app/services/project_custom_field_project_mappings/toggle_service.rb
index e9d51ae9dba..4b434d8059e 100644
--- a/app/services/project_custom_field_project_mappings/toggle_service.rb
+++ b/app/services/project_custom_field_project_mappings/toggle_service.rb
@@ -31,25 +31,33 @@
module ProjectCustomFieldProjectMappings
class ToggleService < ::BaseServices::Write
def persist(service_result)
- if service_result.result.persisted?
- # destroy the mapping if it exists and catch any errors which would not be caught by active record
- begin
- service_result.result.destroy
- rescue StandardError => e
- service_result.errors = e.message
- service_result.success = false
- end
+ if ActiveModel::Type::Boolean.new.cast(params[:value])
+ create_mapping(service_result)
else
- # create the mapping if it does not exist
- unless service_result.result.save
- service_result.errors = service_result.result.errors
- service_result.success = false
- end
+ destroy_mapping(service_result)
end
service_result
end
+ def create_mapping(service_result)
+ return if service_result.result.persisted?
+
+ unless service_result.result.save
+ service_result.errors = service_result.result.errors
+ service_result.success = false
+ end
+ end
+
+ def destroy_mapping(service_result)
+ return unless service_result.result.persisted?
+
+ service_result.result.destroy
+ rescue StandardError => e
+ service_result.errors = e.message
+ service_result.success = false
+ end
+
def instance(params)
instance_class.find_or_initialize_by(
project_id: params[:project_id],
@@ -57,6 +65,11 @@ module ProjectCustomFieldProjectMappings
)
end
+ # no need to set attributes, also required to remove value parameter
+ def set_attributes_params(_params)
+ {}
+ end
+
def default_contract_class
ProjectCustomFieldProjectMappings::UpdateContract
end
diff --git a/spec/services/project_custom_field_project_mappings/toggle_service_spec.rb b/spec/services/project_custom_field_project_mappings/toggle_service_spec.rb
index 13176420c3b..83a5b502ad0 100644
--- a/spec/services/project_custom_field_project_mappings/toggle_service_spec.rb
+++ b/spec/services/project_custom_field_project_mappings/toggle_service_spec.rb
@@ -32,80 +32,98 @@ require "spec_helper"
RSpec.describe ProjectCustomFieldProjectMappings::ToggleService do
let!(:project) { create(:project) }
- let!(:section_with_invisible_fields) { create(:project_custom_field_section, name: "Section with invisible fields") }
+ let!(:project_custom_field_section) { create(:project_custom_field_section, name: "Section with invisible fields") }
- let!(:visible_project_custom_field) do
+ let!(:visible_custom_field) do
create(:project_custom_field,
name: "Visible field",
admin_only: false,
- project_custom_field_section: section_with_invisible_fields)
+ project_custom_field_section:)
end
- let!(:visible_required_project_custom_field) do
+ let!(:required_custom_field) do
create(:project_custom_field,
name: "Visible required field",
admin_only: false,
is_required: true,
- project_custom_field_section: section_with_invisible_fields)
+ project_custom_field_section:)
end
- let!(:invisible_project_custom_field) do
+ let!(:invisible_custom_field) do
create(:project_custom_field,
name: "Admin only field",
admin_only: true,
- project_custom_field_section: section_with_invisible_fields)
+ project_custom_field_section:)
end
let(:instance) { described_class.new(user:) }
+ let(:visible_custom_field_params) { { project_id: project.id, custom_field_id: visible_custom_field.id } }
+ let(:required_custom_field_params) { { project_id: project.id, custom_field_id: required_custom_field.id } }
+ let(:invisible_custom_field_params) { { project_id: project.id, custom_field_id: invisible_custom_field.id } }
+
context "with admin permissions" do
let(:user) { create(:admin) }
it "toggles visible, non-required fields" do
expect(project.project_custom_fields).to contain_exactly(
- visible_required_project_custom_field
+ required_custom_field
)
- expect(instance.call(project_id: project.id, custom_field_id: visible_project_custom_field.id)).to be_success
+ 2.times do
+ expect(instance.call(**visible_custom_field_params, value: "1")).to be_success
- expect(project.reload.project_custom_fields).to contain_exactly(
- visible_required_project_custom_field, visible_project_custom_field
- )
+ expect(project.reload.project_custom_fields).to contain_exactly(
+ required_custom_field, visible_custom_field
+ )
+ end
- expect(instance.call(project_id: project.id, custom_field_id: visible_project_custom_field.id)).to be_success
+ 2.times do
+ expect(instance.call(**visible_custom_field_params, value: "0")).to be_success
- expect(project.reload.project_custom_fields).to contain_exactly(
- visible_required_project_custom_field
- )
+ expect(project.reload.project_custom_fields).to contain_exactly(
+ required_custom_field
+ )
+ end
end
it "toggles invisible, non-required fields" do
expect(project.project_custom_fields).to contain_exactly(
- visible_required_project_custom_field
+ required_custom_field
)
- expect(instance.call(project_id: project.id, custom_field_id: invisible_project_custom_field.id)).to be_success
+ 2.times do
+ expect(instance.call(**invisible_custom_field_params, value: "1")).to be_success
- expect(project.reload.project_custom_fields).to contain_exactly(
- visible_required_project_custom_field, invisible_project_custom_field
- )
+ expect(project.reload.project_custom_fields).to contain_exactly(
+ required_custom_field, invisible_custom_field
+ )
+ end
- expect(instance.call(project_id: project.id, custom_field_id: invisible_project_custom_field.id)).to be_success
+ 2.times do
+ expect(instance.call(**invisible_custom_field_params, value: "0")).to be_success
- expect(project.reload.project_custom_fields).to contain_exactly(
- visible_required_project_custom_field
- )
+ expect(project.reload.project_custom_fields).to contain_exactly(
+ required_custom_field
+ )
+ end
end
it "does not toggle required fields" do
expect(project.project_custom_fields).to contain_exactly(
- visible_required_project_custom_field
+ required_custom_field
)
- expect(instance.call(project_id: project.id, custom_field_id: visible_required_project_custom_field.id)).to be_failure
+ expect(instance.call(**required_custom_field_params, value: "1")).to be_failure
expect(project.reload.project_custom_fields).to contain_exactly(
- visible_required_project_custom_field
+ required_custom_field
+ )
+
+ expect(instance.call(**required_custom_field_params, value: "0")).to be_failure
+
+ expect(project.reload.project_custom_fields).to contain_exactly(
+ required_custom_field
)
end
end
@@ -126,43 +144,59 @@ RSpec.describe ProjectCustomFieldProjectMappings::ToggleService do
it "toggles visible, non-required fields" do
expect(project.project_custom_fields).to contain_exactly(
- visible_required_project_custom_field
+ required_custom_field
)
- expect(instance.call(project_id: project.id, custom_field_id: visible_project_custom_field.id)).to be_success
+ 2.times do
+ expect(instance.call(**visible_custom_field_params, value: "1")).to be_success
- expect(project.reload.project_custom_fields).to contain_exactly(
- visible_required_project_custom_field, visible_project_custom_field
- )
+ expect(project.reload.project_custom_fields).to contain_exactly(
+ required_custom_field, visible_custom_field
+ )
+ end
- expect(instance.call(project_id: project.id, custom_field_id: visible_project_custom_field.id)).to be_success
+ 2.times do
+ expect(instance.call(**visible_custom_field_params, value: "0")).to be_success
- expect(project.reload.project_custom_fields).to contain_exactly(
- visible_required_project_custom_field
- )
+ expect(project.reload.project_custom_fields).to contain_exactly(
+ required_custom_field
+ )
+ end
end
it "does not toggle invisible, non-required fields" do
expect(project.project_custom_fields).to contain_exactly(
- visible_required_project_custom_field
+ required_custom_field
)
- expect(instance.call(project_id: project.id, custom_field_id: invisible_project_custom_field.id)).to be_failure
+ expect(instance.call(**invisible_custom_field_params, value: "1")).to be_failure
expect(project.reload.project_custom_fields).to contain_exactly(
- visible_required_project_custom_field
+ required_custom_field
+ )
+
+ expect(instance.call(**invisible_custom_field_params, value: "0")).to be_failure
+
+ expect(project.reload.project_custom_fields).to contain_exactly(
+ required_custom_field
)
end
it "does not toggle required fields" do
expect(project.project_custom_fields).to contain_exactly(
- visible_required_project_custom_field
+ required_custom_field
)
- expect(instance.call(project_id: project.id, custom_field_id: visible_required_project_custom_field.id)).to be_failure
+ expect(instance.call(**required_custom_field_params, value: "1")).to be_failure
expect(project.reload.project_custom_fields).to contain_exactly(
- visible_required_project_custom_field
+ required_custom_field
+ )
+
+ expect(instance.call(**required_custom_field_params, value: "0")).to be_failure
+
+ expect(project.reload.project_custom_fields).to contain_exactly(
+ required_custom_field
)
end
end
@@ -182,37 +216,55 @@ RSpec.describe ProjectCustomFieldProjectMappings::ToggleService do
it "does not toggle visible, non-required fields" do
expect(project.project_custom_fields).to contain_exactly(
- visible_required_project_custom_field
+ required_custom_field
+ )
+
+ expect(instance.call(**visible_custom_field_params, value: "1")).to be_failure
+
+ expect(project.reload.project_custom_fields).to contain_exactly(
+ required_custom_field
)
- expect(instance.call(project_id: project.id, custom_field_id: visible_project_custom_field.id)).to be_failure
+ expect(instance.call(**visible_custom_field_params, value: "0")).to be_failure
expect(project.reload.project_custom_fields).to contain_exactly(
- visible_required_project_custom_field
+ required_custom_field
)
end
it "does not toggle invisible, non-required fields" do
expect(project.project_custom_fields).to contain_exactly(
- visible_required_project_custom_field
+ required_custom_field
+ )
+
+ expect(instance.call(**invisible_custom_field_params, value: "1")).to be_failure
+
+ expect(project.reload.project_custom_fields).to contain_exactly(
+ required_custom_field
)
- expect(instance.call(project_id: project.id, custom_field_id: invisible_project_custom_field.id)).to be_failure
+ expect(instance.call(**invisible_custom_field_params, value: "0")).to be_failure
expect(project.reload.project_custom_fields).to contain_exactly(
- visible_required_project_custom_field
+ required_custom_field
)
end
it "does not toggle required fields" do
expect(project.project_custom_fields).to contain_exactly(
- visible_required_project_custom_field
+ required_custom_field
+ )
+
+ expect(instance.call(**required_custom_field_params, value: "1")).to be_failure
+
+ expect(project.reload.project_custom_fields).to contain_exactly(
+ required_custom_field
)
- expect(instance.call(project_id: project.id, custom_field_id: visible_required_project_custom_field.id)).to be_failure
+ expect(instance.call(**required_custom_field_params, value: "0")).to be_failure
expect(project.reload.project_custom_fields).to contain_exactly(
- visible_required_project_custom_field
+ required_custom_field
)
end
end