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
Introduces the `patterns` attribute to Type
Marcello Rocha3629d5d00e90
diff --git a/app/models/type.rb b/app/models/type.rbindex 2c7deb92807..0117f8aaa6b 100644--- a/app/models/type.rb+++ b/app/models/type.rb@@ -34,6 +34,8 @@ class Type < ApplicationRecordinclude ::Scopes::Scoped+ attribute :patterns, Types::PatternCollectionType.new+before_destroy :check_integrityhas_many :work_packages@@ -50,16 +52,11 @@ class Type < ApplicationRecordjoin_table: "#{table_name_prefix}custom_fields_types#{table_name_suffix}",association_foreign_key: "custom_field_id"- belongs_to :color,- optional: true,- class_name: "Color"+ belongs_to :color, optional: true, class_name: "Color"acts_as_list- validates :name,- presence: true,- uniqueness: { case_sensitive: false },- length: { maximum: 255 }+ validates :name, presence: true, uniqueness: { case_sensitive: false }, length: { maximum: 255 }validates :is_default, :is_milestone, inclusion: { in: [true, false] }@@ -67,10 +64,7 @@ class Type < ApplicationRecorddefault_scope { order("position ASC") }- scope :without_standard, -> {- where(is_standard: false)- .order(:position)- }+ scope :without_standard, -> { where(is_standard: false).order(:position) }def to_s; name enddiff --git a/app/models/types/pattern.rb b/app/models/types/pattern.rbnew file mode 100644index 00000000000..62dcbd2b2c1--- /dev/null+++ b/app/models/types/pattern.rb@@ -0,0 +1,42 @@+# 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 Types+ Pattern = Data.define(:blueprint, :enabled) do+ def call(object)+ # calculate string using object+ blueprint.to_s + object.to_s+ end++ def to_h+ super.stringify_keys+ end+ end+enddiff --git a/app/models/types/pattern_collection.rb b/app/models/types/pattern_collection.rbnew file mode 100644index 00000000000..52a6dda2e45--- /dev/null+++ b/app/models/types/pattern_collection.rb@@ -0,0 +1,53 @@+# 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 Types+ PatternCollection = Data.define(:patterns) do+ private_class_method :new++ def self.build(patterns:, contract: PatternCollectionContract.new)+ contract.call(patterns).to_monad.fmap { |success| new(success.to_h) }+ end++ def initialize(patterns:)+ transformed = patterns.transform_values { Pattern.new(**_1) }.freeze++ super(patterns: transformed)+ end++ def [](value)+ patterns.fetch(value)+ end++ def to_h+ patterns.stringify_keys.transform_values(&:to_h)+ end+ end+enddiff --git a/app/models/types/pattern_collection_contract.rb b/app/models/types/pattern_collection_contract.rbnew file mode 100644index 00000000000..2a8e7c16a1e--- /dev/null+++ b/app/models/types/pattern_collection_contract.rb@@ -0,0 +1,40 @@+# 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 Types+ class PatternCollectionContract < Dry::Validation::Contract+ params do+ required(:subject).hash do+ required(:blueprint).filled(:string)+ required(:enabled).filled(:bool)+ end+ end+ end+enddiff --git a/app/models/types/pattern_collection_type.rb b/app/models/types/pattern_collection_type.rbnew file mode 100644index 00000000000..9758ee50c33--- /dev/null+++ b/app/models/types/pattern_collection_type.rb@@ -0,0 +1,54 @@+# 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 Types+ class PatternCollectionType < ActiveModel::Type::Value+ def assert_valid_value(value)+ cast(value)+ end++ def cast(value)+ PatternCollection.build(patterns: value).value_or { nil }+ end++ def serialize(pattern)+ return super if pattern.nil?++ YAML.dump(pattern.to_h)+ end++ def deserialize(value)+ return if value.blank?++ data = YAML.safe_load(value)+ cast(data)+ end+ end+enddiff --git a/db/migrate/20241211152749_introduce_patterns_to_types.rb b/db/migrate/20241211152749_introduce_patterns_to_types.rbnew file mode 100644index 00000000000..4688c751f3b--- /dev/null+++ b/db/migrate/20241211152749_introduce_patterns_to_types.rb@@ -0,0 +1,37 @@+# 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.+#++++class IntroducePatternsToTypes < ActiveRecord::Migration[7.1]+ def change+ change_table :types do |t|+ t.text :patterns, null: true+ end+ end+enddiff --git a/spec/models/type_spec.rb b/spec/models/type_spec.rbindex 60c62456443..7f4073acc72 100644--- a/spec/models/type_spec.rb+++ b/spec/models/type_spec.rb@@ -1,3 +1,5 @@+# frozen_string_literal: true+#-- copyright# OpenProject is an open source project management software.# Copyright (C) the OpenProject GmbH@@ -42,7 +44,7 @@ RSpec.describe Type doendit "returns the types enabled in the provided project" do- expect(Type.enabled_in(project)).to contain_exactly(type)+ expect(described_class.enabled_in(project)).to contain_exactly(type)endend@@ -125,4 +127,44 @@ RSpec.describe Type doendendend++ describe "#patterns" do+ it "returns nil when no patterns are defined" do+ type = create(:type)++ expect(type.patterns).to be_nil+ end++ it "returns a PatternCollection" do+ type = create(:type, patterns: {+ subject: { blueprint: "{{work_package:custom_field_123}} - {{project:custom_field_321}}", enabled: true }+ })++ expect(type.patterns).to be_a(Types::PatternCollection)+ expect(type.patterns[:subject])+ .to eq(Types::Pattern.new("{{work_package:custom_field_123}} - {{project:custom_field_321}}", true))+ end+ end++ describe "#patterns=" do+ subject(:type) { build(:type) }++ context "when an invalid value is passed" do+ it "defaults to nil" do+ type.patterns = 4++ expect(type.patterns).to be_nil+ expect { type.save! }.not_to raise_error+ end+ end++ it "converts the incoming hash into a PatternCollection" do+ type.patterns = { subject: { blueprint: "some_string", enabled: false } }++ expect(type.patterns).to be_a(Types::PatternCollection)+ expect(type.patterns[:subject]).to be_a(Types::Pattern)++ expect { type.save! }.not_to raise_error+ end+ endend