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
Delete the unused code
Marcello Rochad1028bdfb9e2
diff --git a/app/contracts/types/base_contract.rb b/app/contracts/types/base_contract.rbdeleted file mode 100644index af2fbd1b02e..00000000000--- a/app/contracts/types/base_contract.rb+++ /dev/null@@ -1,128 +0,0 @@-# 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 "model_contract"--module Types- class BaseContract < ::ModelContract- def self.model- Type- end-- attribute :name- attribute :is_in_roadmap- attribute :is_milestone- attribute :is_default- attribute :color_id- attribute :project_ids- attribute :description- attribute :attribute_groups- attribute :patterns-- validates :name, presence: true, length: { maximum: 255 }- validates :is_default, :is_milestone, :is_in_roadmap, inclusion: { in: [true, false] }-- validate :validate_current_user_is_admin- validate :validate_attribute_group_names- validate :validate_attribute_groups- validate :validate_subject_generation_pattern-- def validate_current_user_is_admin- unless user.admin?- errors.add(:base, :error_unauthorized)- end- end-- 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-- def validate_subject_generation_pattern- blueprint = model.patterns.subject&.blueprint- return if blueprint.nil?-- valid_tokens = flat_valid_token_list- invalid_tokens = blueprint.scan(::WorkPackageTypes::PatternResolver::TOKEN_REGEX)- .reduce([]) do |acc, match|- token = ::WorkPackageTypes::Patterns::PatternToken.build(match).key- valid_tokens.include?(token) ? acc : acc << token- end-- if invalid_tokens.any?- errors.add(:patterns, :invalid_tokens)- end- end-- def flat_valid_token_list = ::WorkPackageTypes::Patterns::TokenPropertyMapper.new.tokens_for_type(model).map(&:key)- end-enddiff --git a/app/services/work_package_types/attribute_groups/transformer.rb b/app/services/work_package_types/attribute_groups/transformer.rbindex bb5490f8e5a..e367cdc70b6 100644--- a/app/services/work_package_types/attribute_groups/transformer.rb+++ b/app/services/work_package_types/attribute_groups/transformer.rb@@ -67,13 +67,14 @@ module WorkPackageTypesquery.extend(OpenProject::ChangedBySystem)query.change_by_system { query.user = User.system }- ::API::V3::UpdateQueryFromV3ParamsService+ result = ::API::V3::UpdateQueryFromV3ParamsService.new(query, user).call(props.with_indifferent_access)- query.show_hierarchies = false-- [name, [query]]+ if result.success?+ query.show_hierarchies = false+ [name, [query]]+ endendendenddiff --git a/app/services/work_package_types/update_service.rb b/app/services/work_package_types/update_service.rbindex de92d113f16..cc98f7a8095 100644--- a/app/services/work_package_types/update_service.rb+++ b/app/services/work_package_types/update_service.rb@@ -51,6 +51,8 @@ module WorkPackageTypesif params[:attribute_groups].empty?model.reset_attribute_groupselse+ # FIXME: We lost the ability to react to errors on the transformation. Might not be a big issue on day to day, but still+ # a regression - 2025-08-07 noted by @mereghostmodel.attribute_groups = AttributeGroups::Transformer.new(groups: params[:attribute_groups], user: user).callparams.delete(:attribute_groups)enddiff --git a/spec/contracts/types/base_contract_spec.rb b/spec/contracts/types/base_contract_spec.rbdeleted file mode 100644index 7c40efc395f..00000000000--- a/spec/contracts/types/base_contract_spec.rb+++ /dev/null@@ -1,58 +0,0 @@-# 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 Types::BaseContract do- let(:current_user) { build_stubbed(:user) }- let(:work_package_type) { Type.new }-- subject(:contract) { described_class.new(work_package_type, current_user) }-- describe "#validation" do- context "if subject generation patterns contains invalid tokens" do- let(:valid_tokens) { [WorkPackageTypes::Patterns::AttributeToken.new(:assignee, nil, nil)] }- let(:work_package_type) do- Type.new(patterns: { subject: { blueprint: "Vacation {{vaders_toy}}", enabled: true } })- end-- before do- token_mapper_double = instance_double(WorkPackageTypes::Patterns::TokenPropertyMapper)- allow(token_mapper_double).to receive(:tokens_for_type).and_return(valid_tokens)- allow(WorkPackageTypes::Patterns::TokenPropertyMapper).to receive(:new).and_return(token_mapper_double)- end-- it "is invalid" do- contract.validate- expect(subject.errors.symbols_for(:patterns)).to contain_exactly(:invalid_tokens)- end- end- end-enddiff --git a/spec/services/shared_type_service.rb b/spec/services/shared_type_service.rbdeleted file mode 100644index 0c8525e4379..00000000000--- a/spec/services/shared_type_service.rb+++ /dev/null@@ -1,324 +0,0 @@-# 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.-#++--RSpec.shared_context "with custom field params" do- let(:cf1) { create(:work_package_custom_field, field_format: "text") }- let(:cf2) { create(:work_package_custom_field, field_format: "text") }- let!(:cf3) { create(:work_package_custom_field, field_format: "text") }-- let(:attribute_groups) do- {- attribute_groups: [- { "type" => "attribute",- "name" => "group1",- "attributes" => [{ "key" => cf1.attribute_name }, { "key" => cf2.attribute_name }] },- { "type" => "attribute",- "name" => "groups",- "attributes" => [{ "key" => cf2.attribute_name }] }- ]- }- end-- let(:params) { attribute_groups }-end--RSpec.shared_examples_for "type service" do- let(:success) { true }- let(:params) { {} }- let!(:contract) do- instance_double(Types::BaseContract).tap do |contract|- allow(contract)- .to receive(:validate)- .and_return(contract_valid)- allow(contract)- .to receive(:errors)- .and_return(contract_errors)- allow(Types::BaseContract)- .to receive(:new)- .and_return(contract)- end- end- let(:contract_errors) { instance_double(ActiveModel::Errors) }- let(:contract_valid) { success }-- describe "#call" do- before do- allow(type)- .to receive(:save)- .and_return(success)- end-- it "is successful" do- expect(service_call).to be_success- end-- it "yields the block with success" do- expect(service_call(&:success?)).to be_truthy- end-- describe "with attributes" do- let(:params) { { name: "blubs blubs" } }-- it "set the values provided on the call" do- service_call-- expect(type.name).to eql params[:name]- end- end-- describe "attribute groups" do- before do- allow(type).to receive(:reset_attribute_groups)- allow(type).to receive(:attribute_groups=)- end-- context "when not given" do- let(:params) { { name: "blubs blubs" } }-- it "set the values provided on the call" do- service_call-- expect(type).not_to have_received(:reset_attribute_groups)- expect(type).not_to have_received(:attribute_groups=)- expect(type.name).to eql params[:name]- end- end-- context "when empty" do- let(:params) { { attribute_groups: [] } }-- it "set the values provided on the call" do- service_call-- expect(type).to have_received(:reset_attribute_groups)- end- end-- context "when other" do- let(:params) { { attribute_groups: [{ "type" => "attribute", "name" => "foo", "attributes" => [] }] } }-- it "set the values provided on the call" do- service_call-- expect(type).not_to have_received(:reset_attribute_groups)- expect(type).to have_received(:attribute_groups=)- end- end- end-- describe "custom fields" do- include_context "with custom field params"-- it "enables the custom fields that are passed via attribute_groups" do- allow(type)- .to receive(:work_package_attributes)- .and_return(cf1.attribute_name => {}, cf2.attribute_name => {})-- allow(type)- .to receive(:custom_field_ids=)- .with([cf1.id, cf2.id])-- service_call-- expect(type).to have_received(:custom_field_ids=)- end-- context "when all the projects are already associated with the type" do- before do- type.projects = create_list :project, 2- end-- it "does not enable the custom fields in the projects (used to be different until OP 15.0)" do- expect { service_call }- .not_to change { Project.where(id: type.project_ids).map(&:work_package_custom_field_ids) }- .from([[], []])- end-- context "when all custom fields are already associated with the type" do- before do- type.custom_field_ids = [cf1.id, cf2.id]- end-- it "enables no custom field" do- expect { service_call }- .not_to change { Project.where(id: type.project_ids).map(&:work_package_custom_field_ids) }- .from([[], []])- end- end- end-- context "when a project is being set on the type" do- let(:projects) { create_list(:project, 2) }- let(:active_project) { create(:project) }- let(:new_project) { create(:project) }- let(:project_ids) { { project_ids: [*[active_project, new_project].map { |p| p.id.to_s }, ""] } }- let(:params) { attribute_groups.merge(project_ids) }-- before do- type.projects << active_project- end-- it "enables the custom field on the newly added project" do- expect { service_call }- .to change { Project.find(new_project.id).work_package_custom_field_ids }- .from([])- .to([cf1.id, cf2.id])- end-- it "does not enable the custom fields on the project the type was already active in" do- expect { service_call }- .not_to change { Project.find(active_project.id).work_package_custom_field_ids }- .from([])- end-- context "when a custom field is already associated with the type" do- before do- type.custom_field_ids = [cf1.id]- end-- it "enables the custom field on the newly added project" do- expect { service_call }- .to change { Project.find(new_project.id).work_package_custom_field_ids }- .from([])- .to([cf1.id, cf2.id])- end-- it "does not enable the custom fields on the project the type was already active in" do- expect { service_call }- .not_to change { Project.find(active_project.id).work_package_custom_field_ids }- .from([])- end- end-- context "when all custom fields are already associated with the type" do- let(:params) { project_ids }-- before do- type.custom_field_ids = [cf1.id, cf2.id]- end-- it "enables the custom field on the newly added project" do- expect { service_call }- .to change { Project.find(new_project.id).work_package_custom_field_ids }- .from([])- .to([cf1.id, cf2.id])- end-- it "does not enable the custom fields on the project the type was already active in" do- expect { service_call }- .not_to change { Project.find(active_project.id).work_package_custom_field_ids }- .from([])- end- end- end- end-- describe "query group" do- let(:query_params) do- sort_by = JSON::dump(["status:desc"])- filters = JSON::dump([{ "status_id" => { "operator" => "=", "values" => %w(1 2) } }])-- { "sortBy" => sort_by, "filters" => filters }- end- let(:query_group_params) do- { "type" => "query", "name" => "group1", "query" => JSON.dump(query_params) }- end- let(:params) { { attribute_groups: [query_group_params] } }- let(:query) { Query.new }- let(:service_result) { ServiceResult.success(result: query) }-- before do- allow(Query)- .to receive(:new_default)- .with(name: "Embedded table: group1")- .and_return(query)- end-- it "assigns the fully parsed query to the type's attribute group with the system user as the querie's user" do- expect(service_call).to be_success-- expect(type.attribute_groups[0].query)- .to eql query-- expect(query.filters.length)- .to be 1-- expect(query.filters[0].name)- .to be :status_id-- expect(query.user)- .to eq User.system- end-- context "when the query parse service reports an error" do- let(:success) { false }- let(:service_result) { ServiceResult.failure(result: nil) }-- it "reports the error" do- expect(service_call).to be_failure-- expect(type.attribute_groups[0].query)- .to eql query- end- end- end-- describe "on failure" do- let(:success) { false }- let(:params) { { name: nil } }-- subject { service_call }-- it "returns a failed service result" do- expect(subject).not_to be_success- end-- it "returns the errors of the type" do- expect(subject.errors).to be_a(ActiveModel::Errors)- end-- describe "custom fields" do- include_context "with custom field params"-- context "when the type is associated with projects" do- before do- type.projects = create_list :project, 2- end-- it "does not changes project custom fields" do- expect { service_call }- .not_to change { Project.where(id: type.project_ids).map(&:work_package_custom_field_ids) }- .from([[], []])- end- 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.rbindex ddb426ff6e8..b57885d2645 100644--- a/spec/services/work_package_types/attribute_groups/transformer_spec.rb+++ b/spec/services/work_package_types/attribute_groups/transformer_spec.rb@@ -90,7 +90,7 @@ RSpec.describe WorkPackageTypes::AttributeGroups::Transformer doquery: {"columns[]" => %w[id subject],"filters" => "[]",- "sortBy" => "[['id', 'asc']]",+ "sortBy" => JSON.dump(["id:asc"]),"groupBy" => ""}.to_json}diff --git a/spec/services/work_package_types/update_service_spec.rb b/spec/services/work_package_types/update_service_spec.rbindex 23d3ab7f321..2ed039d10ba 100644--- a/spec/services/work_package_types/update_service_spec.rb+++ b/spec/services/work_package_types/update_service_spec.rb@@ -29,12 +29,14 @@#++require "spec_helper"-require "services/shared_type_service"require "services/base_services/behaves_like_update_service"module WorkPackageTypesRSpec.describe UpdateService, type: :service doshared_let(:user) { create(:admin) }+ shared_let(:cf1) { create(:work_package_custom_field, field_format: "text") }+ shared_let(:cf2) { create(:work_package_custom_field, field_format: "text") }+ shared_let(:cf3) { create(:work_package_custom_field, field_format: "text") }let(:model) { create(:type, name: "Types-R-Us") }let(:type) { model }@@ -42,8 +44,6 @@ module WorkPackageTypessubject(:service) { described_class.new(user:, model:) }- it_behaves_like "type service"-it_behaves_like "BaseServices update service" dolet(:factory) { :type }let(:model_class) { Type }@@ -60,10 +60,143 @@ module WorkPackageTypesit "defaults to the UpdateSettingsContract" doparams = { patterns: { subject: { blueprint: "{{author}}", enabled: true } } }- # NOTE: This still applies changes to the model... which is far from ideal - 2025-06-23 @mereghostresult = service.call(params)expect(result).to be_failureexpect(result.errors.full_messages).to be_presentend++ context "when updating attribute groups" do+ let(:contract_class) { UpdateFormConfigurationContract }+ let(:params) do+ { attribute_groups: [+ { "type" => "attribute",+ "name" => "group1",+ "attributes" => [{ "key" => cf1.attribute_name }, { "key" => cf2.attribute_name }] },+ { "type" => "attribute",+ "name" => "groups",+ "attributes" => [{ "key" => cf2.attribute_name }] }+ ] }+ end++ subject(:service) { described_class.new(user:, model:, contract_class:) }++ it "doesn't change the type if no attribute group is passed" do+ service_result = service.call({})+ expect(service_result.result).to eq(type)+ end++ it "set the attribute groups to the default value if empty" do+ allow(type).to receive(:reset_attribute_groups).and_call_original+ service_result = service.call(attribute_groups: [])++ expect(service_result).to be_success+ expect(type).to have_received(:reset_attribute_groups)+ end++ it "set the attribute groups to the passed values" do+ service_result = service.call(params)++ expect(service_result).to be_success+ group1, groups = *type.reload.attribute_groups++ expect(group1.key).to eq("group1")+ expect(group1.attributes).to contain_exactly(cf1.attribute_name, cf2.attribute_name)+ expect(groups.key).to eq("groups")+ expect(groups.attributes).to contain_exactly(cf2.attribute_name)+ end+ end++ describe "custom field handling" do+ let(:contract_class) { UpdateFormConfigurationContract }+ let(:params) do+ { attribute_groups: [+ { "type" => "attribute",+ "name" => "group1",+ "attributes" => [{ "key" => cf1.attribute_name }, { "key" => cf2.attribute_name }] }+ ] }+ end++ it "enables the custom fields" do+ service.call(params)++ expect(type.reload.custom_field_ids).to contain_exactly(cf1.id, cf2.id)+ end++ context "when a project already uses the type" do+ before { type.projects = create_list(:project, 2) }++ it "does not automatically enable the custom field" do+ expect { service.call(params) }+ .not_to change { Project.where(id: type.project_ids).map(&:work_package_custom_field_ids) }+ .from([[], []])+ end++ it "does not tries to change the project in case all custom fields are already added" do+ type.custom_field_ids = [cf1.id, cf2.id]++ expect { service.call(params) }+ .not_to change { Project.where(id: type.project_ids).map(&:work_package_custom_field_ids) }+ .from([[], []])+ end+ end+ end++ describe "query group handling" do+ let(:query_params) do+ statuses = create_list(:status, 2)+ sort_by = JSON::dump(["status:desc"])+ filters = JSON::dump([{ "status_id" => { "operator" => "=", "values" => statuses.map { it.id.to_s } } }])++ { "sortBy" => sort_by, "filters" => filters }+ end++ let(:query_group_params) do+ { "type" => "query", "name" => "group1", "query" => JSON.dump(query_params) }+ end++ let(:params) { { attribute_groups: [query_group_params] } }+ let(:contract_class) { UpdateFormConfigurationContract }++ it "assigns the fully parsed query to the type attribute groups" do+ expect(service.call(params)).to be_success++ query_group = type.attribute_groups.first+ expect(query_group.query).to be_a(Query)+ query = query_group.query+ expect(query.filters.length).to eq(1)+ expect(query.filters[0].name).to eq(:status_id)+ end+ end++ context "when adding the type to a project" do+ let(:projects) { create_list(:project, 2) }+ let(:active_project) { create(:project) }+ let(:new_project) { create(:project) }+ let(:project_ids) do+ { project_ids: [*[active_project, new_project].map { it.id.to_s }, ""] }+ end+ let(:contract_class) { UpdateProjectsContract }++ before do+ groups = { attribute_groups: [+ { "type" => "attribute",+ "name" => "group1",+ "attributes" => [{ "key" => cf1.attribute_name }, { "key" => cf2.attribute_name }] }+ ] }++ type.projects << active_project+ described_class.new(model:, user:, contract_class: UpdateFormConfigurationContract).call(groups)+ end++ it "enables the custom field on the newly added project" do+ expect { service.call(project_ids) }.to change { Project.find(new_project.id).work_package_custom_field_ids }+ .from([]).to([cf1.id, cf2.id])+ end++ it "does not enable the custom fields on the already added project" do+ expect { service.call(project_ids) }.not_to change { Project.find(active_project.id).work_package_custom_field_ids }+ .from([])+ end+ endendend