|
| 1 | +# frozen_string_literal: true |
| 2 | + |
| 3 | +#-- copyright |
| 4 | +# OpenProject is an open source project management software. |
| 5 | +# Copyright (C) the OpenProject GmbH |
| 6 | +# |
| 7 | +# This program is free software; you can redistribute it and/or |
| 8 | +# modify it under the terms of the GNU General Public License version 3. |
| 9 | +# |
| 10 | +# OpenProject is a fork of ChiliProject, which is a fork of Redmine. The copyright follows: |
| 11 | +# Copyright (C) 2006-2013 Jean-Philippe Lang |
| 12 | +# Copyright (C) 2010-2013 the ChiliProject Team |
| 13 | +# |
| 14 | +# This program is free software; you can redistribute it and/or |
| 15 | +# modify it under the terms of the GNU General Public License |
| 16 | +# as published by the Free Software Foundation; either version 2 |
| 17 | +# of the License, or (at your option) any later version. |
| 18 | +# |
| 19 | +# This program is distributed in the hope that it will be useful, |
| 20 | +# but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 21 | +# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 22 | +# GNU General Public License for more details. |
| 23 | +# |
| 24 | +# You should have received a copy of the GNU General Public License |
| 25 | +# along with this program; if not, write to the Free Software |
| 26 | +# Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. |
| 27 | +# |
| 28 | +# See COPYRIGHT and LICENSE files for more details. |
| 29 | +#++ |
| 30 | + |
| 31 | +module WorkPackageTypes |
| 32 | + # Turns a project's per-project custom field deactivations into a variant, so the narrowing a |
| 33 | + # project used to express by disabling single fields becomes part of the form configuration |
| 34 | + # instead. |
| 35 | + # |
| 36 | + # The variant inherits every aspect from the given type and excludes only the custom fields the |
| 37 | + # project has not enabled. A project that narrows nothing needs no variant, so the given type is |
| 38 | + # returned unchanged — callers can assign the result to the project either way. |
| 39 | + class BuildVariantFromProjectService < ::BaseServices::BaseCallable |
| 40 | + def initialize(user:, type:) |
| 41 | + super() |
| 42 | + @user = user |
| 43 | + @source = type |
| 44 | + end |
| 45 | + |
| 46 | + protected |
| 47 | + |
| 48 | + def perform(*) |
| 49 | + project = params[:project] |
| 50 | + |
| 51 | + elements = elements_to_exclude(project) |
| 52 | + return ServiceResult.success(result: source) if elements.empty? |
| 53 | + |
| 54 | + build_variant(project, elements) |
| 55 | + end |
| 56 | + |
| 57 | + private |
| 58 | + |
| 59 | + attr_reader :source, :user |
| 60 | + |
| 61 | + def build_variant(project, elements) |
| 62 | + result = nil |
| 63 | + |
| 64 | + Type.transaction do |
| 65 | + result = create_variant(project) |
| 66 | + raise ActiveRecord::Rollback if result.failure? |
| 67 | + |
| 68 | + variant = result.result |
| 69 | + link_aspects_to_source(variant) |
| 70 | + |
| 71 | + exclusion = exclude_elements(variant, elements) |
| 72 | + if exclusion.failure? |
| 73 | + result = exclusion |
| 74 | + raise ActiveRecord::Rollback |
| 75 | + end |
| 76 | + end |
| 77 | + |
| 78 | + result |
| 79 | + end |
| 80 | + |
| 81 | + def create_variant(project) |
| 82 | + # TODO: When FND-204 is fully implemented and we can create project specific variants, let's ensure that |
| 83 | + # we create a project specific variant here. |
| 84 | + CreateService |
| 85 | + .new(user:) |
| 86 | + .call(name: variant_name(project), parent_id: source.root_id) |
| 87 | + end |
| 88 | + |
| 89 | + # A variant is always a child of a root, so a variant built from another variant cannot nest |
| 90 | + # under it. The configuration links are what carry the relationship: pointing them at the |
| 91 | + # source variant makes its own exclusions accumulate with the ones added below. |
| 92 | + def link_aspects_to_source(variant) |
| 93 | + return unless source.variant? |
| 94 | + |
| 95 | + Type::ConfigurationLink::ASPECTS.each { |aspect| variant.link!(aspect, source:) } |
| 96 | + end |
| 97 | + |
| 98 | + def exclude_elements(variant, elements) |
| 99 | + ExcludedElements::AddService |
| 100 | + .new(user:, type: variant) |
| 101 | + .call(aspect: Type::ConfigurationLink::FORM_CONFIGURATION, elements:) |
| 102 | + end |
| 103 | + |
| 104 | + # `source.custom_fields` is the type-level set the form configuration puts on a work package, |
| 105 | + # already resolved through the source's own links and exclusions. Whatever of it the project |
| 106 | + # has not enabled is exactly what disabling single fields used to hide. |
| 107 | + def elements_to_exclude(project) |
| 108 | + active_ids = project.all_work_package_custom_fields.pluck(:id) |
| 109 | + |
| 110 | + source.custom_fields |
| 111 | + .reject { active_ids.include?(it.id) } |
| 112 | + .map(&:attribute_name) |
| 113 | + end |
| 114 | + |
| 115 | + # Name of a variant must stay unique per root type |
| 116 | + def variant_name(project) |
| 117 | + base = "#{source.own_name} - #{project.name}" |
| 118 | + taken = Type.where(parent_id: source.root_id).pluck(:name).map(&:downcase) |
| 119 | + |
| 120 | + return base unless taken.include?(base.downcase) |
| 121 | + |
| 122 | + counter = 2 |
| 123 | + counter += 1 while taken.include?("#{base} (#{counter})".downcase) |
| 124 | + "#{base} (#{counter})" |
| 125 | + end |
| 126 | + end |
| 127 | +end |
0 commit comments