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
[#64613] Create update form configuration contract (#19261)
Yauheni Suhakou38bac05f83b1
diff --git a/app/contracts/work_package_types/update_form_configuration_contract.rb b/app/contracts/work_package_types/update_form_configuration_contract.rbnew file mode 100644index 00000000000..021e8538e5b--- /dev/null+++ b/app/contracts/work_package_types/update_form_configuration_contract.rb@@ -0,0 +1,87 @@+# frozen_string_literal: true++#-- copyright+# OpenProject is an open source project management software.+# Copyright (C) the OpenProject GmbH+#+# This program is free software; you can redistribute it and/or+# modify it under the terms of the GNU General Public License version 3.+#+# OpenProject is a fork of ChiliProject, which is a fork of Redmine. The copyright follows:+# Copyright (C) 2006-2013 Jean-Philippe Lang+# Copyright (C) 2010-2013 the ChiliProject Team+#+# This program is free software; you can redistribute it and/or+# modify it under the terms of the GNU General Public License+# as published by the Free Software Foundation; either version 2+# of the License, or (at your option) any later version.+#+# This program is distributed in the hope that it will be useful,+# but WITHOUT ANY WARRANTY; without even the implied warranty of+# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the+# GNU General Public License for more details.+#+# You should have received a copy of the GNU General Public License+# along with this program; if not, write to the Free Software+# Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.+#+# See COPYRIGHT and LICENSE files for more details.+#++++module WorkPackageTypes+ class UpdateFormConfigurationContract < BaseContract+ attribute :attribute_groups++ validate :validate_attribute_group_names+ validate :validate_attribute_groups++ private++ def validate_attribute_group_names+ return unless model.attribute_groups_changed?++ seen = Set.new+ model.attribute_groups.each do |group|+ errors.add(:attribute_groups, :group_without_name) if group.key.blank?+ errors.add(:attribute_groups, :duplicate_group, group: group.key) if seen.add?(group.key).nil?+ end+ end++ def validate_attribute_groups+ return unless model.attribute_groups_changed?++ model.attribute_groups_objects.each do |group|+ if group.is_a?(Type::QueryGroup)+ validate_query_group(group)+ else+ validate_attribute_group(group)+ end+ end+ end++ def validate_query_group(group)+ query = group.query++ contract_class = query.persisted? ? Queries::UpdateContract : Queries::CreateContract+ contract = contract_class.new(query, user)++ unless contract.validate+ errors.add(:attribute_groups, :query_invalid, group: group.key, details: contract.errors.full_messages.join)+ end+ end++ def validate_attribute_group(group)+ valid_attributes = model.work_package_attributes.keys++ group.attributes.each do |key|+ if key.is_a?(String) && valid_attributes.exclude?(key)+ errors.add(+ :attribute_groups,+ I18n.t("activerecord.errors.models.type.attributes.attribute_groups.attribute_unknown_name",+ attribute: key)+ )+ end+ end+ end+ end+enddiff --git a/app/services/base_type_service.rb b/app/services/base_type_service.rbindex 8ba1b086bda..ea4165261e3 100644--- a/app/services/base_type_service.rb+++ b/app/services/base_type_service.rb@@ -171,8 +171,8 @@ class BaseTypeServicetype.custom_field_ids = type.attribute_groups.flat_map(&:members)- .select { CustomField.custom_field_attribute? _1 }- .map { _1.gsub(/^custom_field_/, "").to_i }+ .select { CustomField.custom_field_attribute? it }+ .map { it.gsub(/^custom_field_/, "").to_i }.uniqenddiff --git a/app/services/work_package_types/attribute_groups/transformer.rb b/app/services/work_package_types/attribute_groups/transformer.rbnew file mode 100644index 00000000000..1c83c3ae1d1--- /dev/null+++ b/app/services/work_package_types/attribute_groups/transformer.rb@@ -0,0 +1,80 @@+# frozen_string_literal: true++#-- copyright+# OpenProject is an open source project management software.+# Copyright (C) the OpenProject GmbH+#+# This program is free software; you can redistribute it and/or+# modify it under the terms of the GNU General Public License version 3.+#+# OpenProject is a fork of ChiliProject, which is a fork of Redmine. The copyright follows:+# Copyright (C) 2006-2013 Jean-Philippe Lang+# Copyright (C) 2010-2013 the ChiliProject Team+#+# This program is free software; you can redistribute it and/or+# modify it under the terms of the GNU General Public License+# as published by the Free Software Foundation; either version 2+# of the License, or (at your option) any later version.+#+# This program is distributed in the hope that it will be useful,+# but WITHOUT ANY WARRANTY; without even the implied warranty of+# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the+# GNU General Public License for more details.+#+# You should have received a copy of the GNU General Public License+# along with this program; if not, write to the Free Software+# Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.+#+# See COPYRIGHT and LICENSE files for more details.+#++++module WorkPackageTypes+ module AttributeGroups+ class Transformer+ def initialize(groups:, user:)+ @groups = groups+ @user = user+ end++ def call+ return [] if groups.blank?++ groups.map do |group|+ if group["type"] == "query"+ transform_query_group(group)+ else+ transform_attribute_group(group)+ end+ end+ end++ private++ attr_reader :groups, :user++ def transform_attribute_group(group)+ name = group["key"]&.to_sym || group["name"]+ attributes = group["attributes"].pluck("key")++ [name, attributes]+ end++ def transform_query_group(group)+ name = group["name"]+ props = JSON.parse(group["query"])++ query = Query.new_default(name: "Embedded table: #{name}")+ query.extend(OpenProject::ChangedBySystem)+ query.change_by_system { query.user = User.system }++ ::API::V3::UpdateQueryFromV3ParamsService+ .new(query, user)+ .call(props.with_indifferent_access)++ query.show_hierarchies = false++ [name, [query]]+ end+ end+ end+enddiff --git a/spec/contracts/work_package_types/update_form_configuration_contract_spec.rb b/spec/contracts/work_package_types/update_form_configuration_contract_spec.rbnew file mode 100644index 00000000000..02636d72ab8--- /dev/null+++ b/spec/contracts/work_package_types/update_form_configuration_contract_spec.rb@@ -0,0 +1,113 @@+# frozen_string_literal: true++#-- copyright+# OpenProject is an open source project management software.+# Copyright (C) the OpenProject GmbH+#+# This program is free software; you can redistribute it and/or+# modify it under the terms of the GNU General Public License version 3.+#+# OpenProject is a fork of ChiliProject, which is a fork of Redmine. The copyright follows:+# Copyright (C) 2006-2013 Jean-Philippe Lang+# Copyright (C) 2010-2013 the ChiliProject Team+#+# This program is free software; you can redistribute it and/or+# modify it under the terms of the GNU General Public License+# as published by the Free Software Foundation; either version 2+# of the License, or (at your option) any later version.+#+# This program is distributed in the hope that it will be useful,+# but WITHOUT ANY WARRANTY; without even the implied warranty of+# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the+# GNU General Public License for more details.+#+# You should have received a copy of the GNU General Public License+# along with this program; if not, write to the Free Software+# Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.+#+# See COPYRIGHT and LICENSE files for more details.+#++++require "spec_helper"++module WorkPackageTypes+ RSpec.describe UpdateFormConfigurationContract do+ let(:user) { create(:admin) }+ let(:model) { create(:type, name: "O-Negative") }++ subject(:contract) { described_class.new(model, user, options: {}) }++ context "when the user isn't admin" do+ let(:user) { create(:user) }++ it "the contract is invalid" do+ expect(contract.validate).to be_falsey+ end++ it "adds and error to the contract" do+ contract.validate+ expect(contract.errors.details).to eq(base: [{ error: :error_unauthorized }])+ end+ end++ describe "validations" do+ context "when attribute_groups is present and valid" do+ let(:valid_group) { ["foo", ["assignee", "responsible"]] }++ it "is valid" do+ model.attribute_groups = [valid_group]++ expect(contract.validate).to be_truthy+ end+ end++ context "when a group has no name" do+ let(:invalid_group) { ["", ["assignee"]] }++ it "is invalid and adds :group_without_name error" do+ model.attribute_groups = [invalid_group]++ expect(contract.validate).to be_falsey+ expect(contract.errors.details[:attribute_groups]).to include(error: :group_without_name)+ end+ end++ context "when there are duplicate group names" do+ let(:duplicate_group) { ["foo", ["assignee"]] }++ it "is invalid and adds :duplicate_group error" do+ model.attribute_groups = [duplicate_group, duplicate_group]++ expect(contract.validate).to be_falsey+ expect(contract.errors.details[:attribute_groups]).to include(error: :duplicate_group, group: "foo")+ end+ end++ context "when an attribute group contains unknown attributes" do+ let(:invalid_group) { ["foo", ["unknown_attribute"]] }++ it "is invalid and adds an error for the unknown attribute" do+ model.attribute_groups = [invalid_group]++ expect(contract.validate).to be_falsey+ expect(contract.errors.details[:attribute_groups]).to include(+ error: "Invalid work package attribute used: unknown_attribute"+ )+ end+ end++ context "with invalid query group" do+ let(:query) { Query.new(name: "Invalid Query", user:) }+ let(:invalid_query_group) { ["query_group", [query]] }++ it "is invalid and adds an error for the query group" do+ model.attribute_groups = [invalid_query_group]++ expect(contract.validate).to be_falsey+ expect(contract.errors.details[:attribute_groups])+ .to include(hash_including(error: :query_invalid, group: "query_group"))+ end+ end+ end+ end+enddiff --git a/spec/services/work_package_types/attribute_groups/transformer_spec.rb b/spec/services/work_package_types/attribute_groups/transformer_spec.rbnew file mode 100644index 00000000000..deaff323541--- /dev/null+++ b/spec/services/work_package_types/attribute_groups/transformer_spec.rb@@ -0,0 +1,126 @@+# frozen_string_literal: true++#-- copyright+# OpenProject is an open source project management software.+# Copyright (C) the OpenProject GmbH+#+# This program is free software; you can redistribute it and/or+# modify it under the terms of the GNU General Public License version 3.+#+# OpenProject is a fork of ChiliProject, which is a fork of Redmine. The copyright follows:+# Copyright (C) 2006-2013 Jean-Philippe Lang+# Copyright (C) 2010-2013 the ChiliProject Team+#+# This program is free software; you can redistribute it and/or+# modify it under the terms of the GNU General Public License+# as published by the Free Software Foundation; either version 2+# of the License, or (at your option) any later version.+#+# This program is distributed in the hope that it will be useful,+# but WITHOUT ANY WARRANTY; without even the implied warranty of+# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the+# GNU General Public License for more details.+#+# You should have received a copy of the GNU General Public License+# along with this program; if not, write to the Free Software+# Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.+#+# See COPYRIGHT and LICENSE files for more details.+#++++require "spec_helper"++RSpec.describe WorkPackageTypes::AttributeGroups::Transformer do+ subject(:transformer) { described_class.new(groups: raw_groups, user: user) }++ let(:user) { build(:admin) }++ describe "#call" do+ context "when groups are empty" do+ let(:raw_groups) { [] }++ it "returns an empty array" do+ expect(transformer.call).to eq([])+ end+ end++ context "when given a regular attribute group with key" do+ let(:raw_groups) do+ [+ {+ "name" => "Custom",+ "key" => "custom",+ "type" => "attribute",+ "attributes" => [+ { "key" => "custom_field_1" },+ { "key" => "custom_field_2" }+ ]+ }+ ]+ end++ it "returns transformed group with symbolized key and attribute keys" do+ expect(transformer.call).to eq([[:custom, ["custom_field_1", "custom_field_2"]]])+ end+ end++ context "when group has no key" do+ let(:raw_groups) do+ [+ {+ "name" => "General Info",+ "type" => "attribute",+ "attributes" => [{ "key" => "subject" }]+ }+ ]+ end++ it "uses name as group name" do+ expect(transformer.call).to eq([["General Info", ["subject"]]])+ end+ end++ context "when given a query group with valid JSON" do+ let(:raw_groups) do+ [+ {+ "name" => "Embedded Table",+ "type" => "query",+ "attributes" => nil,+ "query" => {+ "columns[]" => %w[id subject],+ "filters" => "[]",+ "sortBy" => "[['id', 'asc']]",+ "groupBy" => ""+ }.to_json+ }+ ]+ end++ it "returns a group with a Query instance" do+ result = transformer.call+ name, entries = result.first++ expect(name).to eq("Embedded Table")+ expect(entries.first).to be_a(Query)+ expect(entries.first.name).to eq("Embedded table: Embedded Table")+ end+ end++ context "when given a query group with invalid JSON" do+ let(:raw_groups) do+ [+ {+ "name" => "Broken",+ "type" => "query",+ "query" => "not a json"+ }+ ]+ end++ it "raises JSON::ParserError" do+ expect { transformer.call }.to raise_error(JSON::ParserError)+ end+ end+ end+end