Skip to content

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

Update patterns by changing subject-pattern only

Jan Sandbrink66363d4b5427
diff --git a/app/forms/work_packages/types/subject_configuration_form.rb b/app/forms/work_packages/types/subject_configuration_form.rb
index 2c1076e2ed7..9faf6d55fae 100644
--- a/app/forms/work_packages/types/subject_configuration_form.rb
+++ b/app/forms/work_packages/types/subject_configuration_form.rb
@@ -77,9 +77,9 @@ module WorkPackages
end
def subject_pattern
- return "" if model.patterns.nil? || model.patterns[:subject].nil?
+ return "" if model.patterns.subject.nil?
- model.patterns[:subject].blueprint
+ model.patterns.subject.blueprint
end
end
end
diff --git a/app/models/type.rb b/app/models/type.rb
index f41382aa2be..1eadea9c31c 100644
--- a/app/models/type.rb
+++ b/app/models/type.rb
@@ -109,8 +109,6 @@ class Type < ApplicationRecord
end
def enabled_patterns
- return {} if patterns.blank?
-
patterns.all_enabled
end
diff --git a/app/models/types/patterns/collection.rb b/app/models/types/patterns/collection.rb
index 2b29e620520..d22c0949b21 100644
--- a/app/models/types/patterns/collection.rb
+++ b/app/models/types/patterns/collection.rb
@@ -34,6 +34,10 @@ module Types
extend Dry::Monads[:result]
private_class_method :new
+ def self.empty
+ new(patterns: {})
+ end
+
def self.build(patterns:, contract: CollectionContract.new)
contract.call(patterns).to_monad.fmap { |success| new(success.to_h) }
rescue ArgumentError => e
@@ -46,12 +50,16 @@ module Types
super(patterns: transformed)
end
+ def subject
+ patterns[:subject]
+ end
+
def all_enabled
patterns.select { |_, pattern| pattern.enabled? }
end
- def [](value)
- patterns[value]
+ def update_pattern(name, blueprint:, enabled:)
+ self.class.build(patterns: patterns.to_h.merge(name => { blueprint:, enabled: }))
end
def to_h
diff --git a/app/models/types/patterns/collection_contract.rb b/app/models/types/patterns/collection_contract.rb
index 02f79b288e4..b8dc28cd5fa 100644
--- a/app/models/types/patterns/collection_contract.rb
+++ b/app/models/types/patterns/collection_contract.rb
@@ -32,7 +32,7 @@ module Types
module Patterns
class CollectionContract < Dry::Validation::Contract
params do
- required(:subject).hash do
+ optional(:subject).hash do
required(:blueprint).filled(:string)
required(:enabled).filled(:bool)
end
diff --git a/app/models/types/patterns/collection_type.rb b/app/models/types/patterns/collection_type.rb
index 932cf17ec33..f7c84b23416 100644
--- a/app/models/types/patterns/collection_type.rb
+++ b/app/models/types/patterns/collection_type.rb
@@ -36,7 +36,9 @@ module Types
end
def cast(value)
- Collection.build(patterns: value).value_or { nil }
+ return value if value.is_a?(Collection)
+
+ Collection.build(patterns: value).value_or { Collection.empty }
end
def serialize(pattern)
@@ -46,7 +48,7 @@ module Types
end
def deserialize(value)
- return if value.blank?
+ return Collection.empty if value.blank?
data = YAML.safe_load(value)
cast(data)
diff --git a/app/services/base_type_service.rb b/app/services/base_type_service.rb
index 1e41f1cc126..be2d15b25c4 100644
--- a/app/services/base_type_service.rb
+++ b/app/services/base_type_service.rb
@@ -1,3 +1,5 @@
+# frozen_string_literal: true
+
#-- copyright
# OpenProject is an open source project management software.
# Copyright (C) the OpenProject GmbH
@@ -106,12 +108,9 @@ class BaseTypeService
end
def set_subject_pattern(params)
- type.patterns = {
- subject: {
- blueprint: params[:subject_pattern],
- enabled: params[:subject_configuration] == "auto"
- }
- }
+ type.patterns = type.patterns.update_pattern(:subject,
+ blueprint: params[:subject_pattern],
+ enabled: params[:subject_configuration] == "auto").value!
end
def parse_attribute_groups_params(params)
diff --git a/spec/controllers/types_controller_spec.rb b/spec/controllers/types_controller_spec.rb
index 63ddce35c48..0549fc1c906 100644
--- a/spec/controllers/types_controller_spec.rb
+++ b/spec/controllers/types_controller_spec.rb
@@ -334,7 +334,7 @@ RSpec.describe TypesController do
end
it "stores the pattern" do
- pattern = Type.find(type.id).patterns[:subject]
+ pattern = Type.find(type.id).patterns.subject
expect(pattern).to be_present
expect(pattern).to be_enabled
@@ -349,14 +349,14 @@ RSpec.describe TypesController do
end
it "disables the pattern" do
- pattern = Type.find(type.id).patterns[:subject]
+ pattern = Type.find(type.id).patterns.subject
expect(pattern).to be_present
expect(pattern).not_to be_enabled
end
it "stores the previously used blueprint" do
- pattern = Type.find(type.id).patterns[:subject]
+ pattern = Type.find(type.id).patterns.subject
expect(pattern).to be_present
expect(pattern.blueprint).to eq(blueprint)
diff --git a/spec/models/type_spec.rb b/spec/models/type_spec.rb
index 7a1e0d25dd6..dc010a922ac 100644
--- a/spec/models/type_spec.rb
+++ b/spec/models/type_spec.rb
@@ -129,10 +129,10 @@ RSpec.describe Type do
end
describe "#patterns" do
- it "returns nil when no patterns are defined" do
+ it "returns an empty collection when no patterns are defined" do
type = create(:type)
- expect(type.patterns).to be_nil
+ expect(type.patterns).to eq(Types::Patterns::Collection.empty)
end
it "returns a PatternCollection" do
@@ -141,7 +141,7 @@ RSpec.describe Type do
})
expect(type.patterns).to be_a(Types::Patterns::Collection)
- expect(type.patterns[:subject])
+ expect(type.patterns.subject)
.to eq(Types::Pattern.new("{{work_package:custom_field_123}} - {{project:custom_field_321}}", true))
end
end
@@ -149,22 +149,35 @@ RSpec.describe Type do
describe "#patterns=" do
subject(:type) { build(:type) }
+ it "assigns a patterns collection as-is" do
+ collection = Types::Patterns::Collection.build(patterns: {
+ subject: { blueprint: "some_string", enabled: false }
+ }).value!
+
+ type.patterns = collection
+
+ expect(type.patterns).to eq(collection)
+ expect { type.save! }.not_to raise_error
+ end
+
context "when an invalid value is passed" do
- it "defaults to nil" do
+ it "defaults to an empty collection" do
type.patterns = 4
- expect(type.patterns).to be_nil
+ expect(type.patterns).to eq(Types::Patterns::Collection.empty)
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 } }
+ context "when a hash is passed" do
+ it "converts the incoming hash into a PatternCollection" do
+ type.patterns = { subject: { blueprint: "some_string", enabled: false } }
- expect(type.patterns).to be_a(Types::Patterns::Collection)
- expect(type.patterns[:subject]).to be_a(Types::Pattern)
+ expect(type.patterns).to be_a(Types::Patterns::Collection)
+ expect(type.patterns.subject).to be_a(Types::Pattern)
- expect { type.save! }.not_to raise_error
+ expect { type.save! }.not_to raise_error
+ end
end
end
end