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

Move enabled/disabled check up into token mapper

Jan Sandbrinkdbdc7950d526
diff --git a/app/components/work_package_types/subject_configuration_component.rb b/app/components/work_package_types/subject_configuration_component.rb
index eb329e2fd39..1ddc5a1cf72 100644
--- a/app/components/work_package_types/subject_configuration_component.rb
+++ b/app/components/work_package_types/subject_configuration_component.rb
@@ -75,9 +75,7 @@ module WorkPackageTypes
end
def supported_attributes
- token_mapper = Patterns::TokenPropertyMapper.new
- valid_tokens = token_mapper.all_tokens
- suggestable_tokens = token_mapper.tokens_for_type(model).to_set
+ enabled, disabled = Patterns::TokenPropertyMapper.new.partitioned_tokens_for_type(model)
result = {
work_package: {
@@ -94,9 +92,8 @@ module WorkPackageTypes
}
}
- valid_tokens.each do |token|
- result.dig(token.context, :tokens) << token_to_hash(token, enabled: suggestable_tokens.include?(token))
- end
+ enabled.each { |token| result.dig(token.context, :tokens) << token_to_hash(token, enabled: true) }
+ disabled.each { |token| result.dig(token.context, :tokens) << token_to_hash(token, enabled: false) }
result
end
diff --git a/app/contracts/work_package_types/update_subject_pattern_contract.rb b/app/contracts/work_package_types/update_subject_pattern_contract.rb
index 4d913591fba..65fe8836139 100644
--- a/app/contracts/work_package_types/update_subject_pattern_contract.rb
+++ b/app/contracts/work_package_types/update_subject_pattern_contract.rb
@@ -60,6 +60,9 @@ module WorkPackageTypes
end
end
- def flat_valid_token_list = WorkPackageTypes::Patterns::TokenPropertyMapper.new.tokens_for_type(model).map(&:key)
+ def flat_valid_token_list
+ enabled, _disabled = WorkPackageTypes::Patterns::TokenPropertyMapper.new.partitioned_tokens_for_type(model)
+ enabled.map(&:key)
+ end
end
end
diff --git a/app/models/work_package_types/pattern_resolver.rb b/app/models/work_package_types/pattern_resolver.rb
index 1fb0dcde3bb..2f53976aba5 100644
--- a/app/models/work_package_types/pattern_resolver.rb
+++ b/app/models/work_package_types/pattern_resolver.rb
@@ -40,7 +40,7 @@ module WorkPackageTypes
end
def resolve(work_package)
- @tokens = @mapper.tokens_for_type(work_package.type)
+ @tokens, = @mapper.partitioned_tokens_for_type(work_package.type)
@pattern_tokens.inject(@pattern) do |pattern, token|
pattern.gsub(token.pattern, get_value(work_package, token))
diff --git a/app/models/work_package_types/patterns/token_property_mapper.rb b/app/models/work_package_types/patterns/token_property_mapper.rb
index d3335e762c1..ac6238ed341 100644
--- a/app/models/work_package_types/patterns/token_property_mapper.rb
+++ b/app/models/work_package_types/patterns/token_property_mapper.rb
@@ -69,15 +69,19 @@ module WorkPackageTypes
].freeze
# rubocop:enable Layout/LineLength
- def tokens_for_type(type)
- [
+ def partitioned_tokens_for_type(type)
+ enabled_tokens = [
*BASE_ATTRIBUTE_TOKENS,
*tokenize(work_package_cfs_for(type)),
*tokenize(project_cfs, "project_"),
*tokenize(all_work_package_cfs, "parent_")
- ]
+ ].to_set
+
+ all_tokens.partition { |token| enabled_tokens.include?(token) }
end
+ private
+
def all_tokens
[
*BASE_ATTRIBUTE_TOKENS,
@@ -87,8 +91,6 @@ module WorkPackageTypes
]
end
- private
-
def default_tokens
BASE_ATTRIBUTE_TOKENS.each_with_object({ work_package: {}, project: {}, parent: {} }) do |token, obj|
case token.key.to_s
diff --git a/spec/models/work_package_types/patterns/token_property_mapper_spec.rb b/spec/models/work_package_types/patterns/token_property_mapper_spec.rb
index 649f853271a..0925808b713 100644
--- a/spec/models/work_package_types/patterns/token_property_mapper_spec.rb
+++ b/spec/models/work_package_types/patterns/token_property_mapper_spec.rb
@@ -103,24 +103,29 @@ RSpec.describe WorkPackageTypes::Patterns::TokenPropertyMapper do
end
end
- describe "#tokens_for_type" do
+ describe "#partitioned_tokens_for_type" do
+ subject { described_class.new.partitioned_tokens_for_type(work_package.type) }
+
it "multi value fields are supported" do
- token = described_class.new.tokens_for_type(work_package.type).detect do |t|
+ enabled, = subject
+ token = enabled.detect do |t|
t.key == :"custom_field_#{mult_list_custom_field.id}"
end
expect(token.call(work_package)).to eq(%w[A B])
end
it "supports boolean custom fields" do
- token = described_class.new.tokens_for_type(work_package.type).detect do |t|
+ enabled, = subject
+ token = enabled.detect do |t|
t.key == :"custom_field_#{boolean_custom_field.id}"
end
expect(token.call(work_package)).to be(true)
end
- it "must return nil if custom field is not activated in project" do
- token = described_class.new.tokens_for_type(work_package.type).detect do |t|
+ it "must return :attribute_not_available if custom field is not activated in project" do
+ enabled, = subject
+ token = enabled.detect do |t|
t.key == :"custom_field_#{not_activated_custom_field.id}"
end
@@ -128,36 +133,28 @@ RSpec.describe WorkPackageTypes::Patterns::TokenPropertyMapper do
expect(token.call(work_package)).to eq(:attribute_not_available)
end
- it "returns all possible tokens" do
+ it "returns all possible tokens as enabled" do
cf = string_custom_field
- tokens = described_class.new.tokens_for_type(work_package.type)
+ enabled, = subject
- expect(tokens.first).to be_a(WorkPackageTypes::Patterns::AttributeToken)
- expect(detect(tokens, :project_status).label).to eq(Project.human_attribute_name(:status_code))
- expect(detect(tokens, :"custom_field_#{cf.id}").label).to eq(cf.name)
+ expect(enabled.first).to be_a(WorkPackageTypes::Patterns::AttributeToken)
+ expect(detect(enabled, :project_status)&.label).to eq(Project.human_attribute_name(:status_code))
+ expect(detect(enabled, :"custom_field_#{cf.id}")&.label).to eq(cf.name)
end
- it "does not return a token that's not on the correct type" do
- cf = custom_field_not_on_type
- tokens = described_class.new.tokens_for_type(work_package.type)
- expect(detect(tokens, :"custom_field_#{cf.id}")).to be_nil
- end
- end
-
- describe "#all_tokens" do
- it "returns all possible tokens" do
+ it "does not return possible tokens as disabled" do
cf = string_custom_field
- tokens = described_class.new.all_tokens
+ _, disabled = subject
- expect(tokens.first).to be_a(WorkPackageTypes::Patterns::AttributeToken)
- expect(detect(tokens, :project_status).label).to eq(Project.human_attribute_name(:status_code))
- expect(detect(tokens, :"custom_field_#{cf.id}").label).to eq(cf.name)
+ expect(detect(disabled, :project_status)).to be_nil
+ expect(detect(disabled, :"custom_field_#{cf.id}")).to be_nil
end
- it "returns a token that's not on the correct type" do
+ it "returns a token that's not on the correct type as disabled" do
cf = custom_field_not_on_type
- tokens = described_class.new.all_tokens
- expect(detect(tokens, :"custom_field_#{cf.id}").label).to eq(cf.name)
+ enabled, disabled = subject
+ expect(detect(enabled, :"custom_field_#{cf.id}")).to be_nil
+ expect(detect(disabled, :"custom_field_#{cf.id}")&.label).to eq(cf.name)
end
end