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 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.rbindex 533c873ede5..c0ff87a2288 100644--- a/app/models/permitted_params.rb+++ b/app/models/permitted_params.rb@@ -317,6 +317,7 @@ class PermittedParamsdef project_custom_field_project_mappingparams.require(:project_custom_field_project_mapping).permit(*self.class.permitted_attributes[:project_custom_field_project_mapping])+ .merge(params.permit(:value))enddef newsdiff --git a/app/services/project_custom_field_project_mappings/toggle_service.rb b/app/services/project_custom_field_project_mappings/toggle_service.rbindex 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 ProjectCustomFieldProjectMappingsclass ToggleService < ::BaseServices::Writedef 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)endservice_resultend+ 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_classProjectCustomFieldProjectMappings::UpdateContractenddiff --git a/spec/services/project_custom_field_project_mappings/toggle_service_spec.rb b/spec/services/project_custom_field_project_mappings/toggle_service_spec.rbindex 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 dolet!(: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) docreate(: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) docreate(: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) docreate(:project_custom_field,name: "Admin only field",admin_only: true,- project_custom_field_section: section_with_invisible_fields)+ project_custom_field_section:)endlet(: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" dolet(:user) { create(:admin) }it "toggles visible, non-required fields" doexpect(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+ )+ endendit "toggles invisible, non-required fields" doexpect(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+ )+ endendit "does not toggle required fields" doexpect(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_failureexpect(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)endend@@ -126,43 +144,59 @@ RSpec.describe ProjectCustomFieldProjectMappings::ToggleService doit "toggles visible, non-required fields" doexpect(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+ )+ endendit "does not toggle invisible, non-required fields" doexpect(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_failureexpect(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)endit "does not toggle required fields" doexpect(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_failureexpect(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)endend@@ -182,37 +216,55 @@ RSpec.describe ProjectCustomFieldProjectMappings::ToggleService doit "does not toggle visible, non-required fields" doexpect(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_failureexpect(project.reload.project_custom_fields).to contain_exactly(- visible_required_project_custom_field+ required_custom_field)endit "does not toggle invisible, non-required fields" doexpect(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_failureexpect(project.reload.project_custom_fields).to contain_exactly(- visible_required_project_custom_field+ required_custom_field)endit "does not toggle required fields" doexpect(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_failureexpect(project.reload.project_custom_fields).to contain_exactly(- visible_required_project_custom_field+ required_custom_field)endend