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

Show proper names of tokens disabled for current type

Jan Sandbrink3cac56a69dfb
diff --git a/app/components/work_package_types/subject_configuration_component.rb b/app/components/work_package_types/subject_configuration_component.rb
index 5be7e6bb509..eb329e2fd39 100644
--- a/app/components/work_package_types/subject_configuration_component.rb
+++ b/app/components/work_package_types/subject_configuration_component.rb
@@ -75,7 +75,9 @@ module WorkPackageTypes
end
def supported_attributes
- attribute_tokens = Patterns::TokenPropertyMapper.new.tokens_for_type(model)
+ token_mapper = Patterns::TokenPropertyMapper.new
+ valid_tokens = token_mapper.all_tokens
+ suggestable_tokens = token_mapper.tokens_for_type(model).to_set
result = {
work_package: {
@@ -92,10 +94,20 @@ module WorkPackageTypes
}
}
- attribute_tokens.each_with_object(result) do |token, obj|
- token_hash = { key: token.key, label: token.label, label_with_context: token.label_with_context }
- obj[token.context][:tokens] << token_hash
+ valid_tokens.each do |token|
+ result.dig(token.context, :tokens) << token_to_hash(token, enabled: suggestable_tokens.include?(token))
end
+
+ result
+ end
+
+ def token_to_hash(token, enabled:)
+ {
+ key: token.key,
+ label: token.label,
+ label_with_context: token.label_with_context,
+ enabled:
+ }
end
def sort_attributes(attributes)
diff --git a/app/models/work_package_types/patterns/attribute_token.rb b/app/models/work_package_types/patterns/attribute_token.rb
index 7a1ffc5ea2f..25845665d50 100644
--- a/app/models/work_package_types/patterns/attribute_token.rb
+++ b/app/models/work_package_types/patterns/attribute_token.rb
@@ -54,6 +54,23 @@ module WorkPackageTypes
:work_package
end
end
+
+ # --- Equality overrides ---
+ # We want to make sure that two tokens are considered equal if they represent the attribute. This is regardless
+ # of identity of the methods used to resolve their labels etc.
+
+ def ==(other)
+ eql?(other)
+ end
+
+ def eql?(other)
+ self.class == other.class && key == other.key
+ end
+
+ def hash
+ [self.class, key].hash
+ end
+ # --- END Equality overrides ---
end
end
end
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 c9391be3ad9..d3335e762c1 100644
--- a/app/models/work_package_types/patterns/token_property_mapper.rb
+++ b/app/models/work_package_types/patterns/token_property_mapper.rb
@@ -78,6 +78,15 @@ module WorkPackageTypes
]
end
+ def all_tokens
+ [
+ *BASE_ATTRIBUTE_TOKENS,
+ *tokenize(all_work_package_cfs),
+ *tokenize(project_cfs, "project_"),
+ *tokenize(all_work_package_cfs, "parent_")
+ ]
+ end
+
private
def default_tokens
diff --git a/frontend/src/stimulus/controllers/pattern-input.controller.ts b/frontend/src/stimulus/controllers/pattern-input.controller.ts
index 98ba0fb89cb..854398856a6 100644
--- a/frontend/src/stimulus/controllers/pattern-input.controller.ts
+++ b/frontend/src/stimulus/controllers/pattern-input.controller.ts
@@ -39,7 +39,14 @@ type FilteredSuggestions = Array<{
type TokenElement = HTMLElement&{ dataset:{ role:'token', prop:string } };
type ListElement = HTMLElement&{ dataset:{ role:'list_item', prop:string } };
-type AttributeToken = { key:string, label:string, label_with_context?:string, insert_as_text?:boolean };
+
+interface AttributeToken {
+ key:string;
+ label:string;
+ label_with_context?:string;
+ insert_as_text?:boolean;
+ enabled:boolean;
+};
const COMPLETION_CHARACTER = '/';
const TOKEN_REGEX = /{{([0-9A-Za-z_]+)}}/g;
@@ -84,11 +91,13 @@ export default class PatternInputController extends Controller {
declare readonly insertAsTextTemplateValue:string;
validTokenMap:Record<string, AttributeToken> = {};
+ validSuggestions:Record<string, { title:string, tokens:AttributeToken[] }> = {};
currentRange:Range|undefined = undefined;
connect() {
this.validTokenMap = this.flatTokensKeyToLabelWithContext();
this.contentTarget.innerHTML = this.toHtml(this.patternInitialValue) || ' ';
+ this.populateValidSuggestions();
this.tagInvalidTokens();
this.clearSuggestionsFilter();
}
@@ -433,8 +442,8 @@ export default class PatternInputController extends Controller {
}
private getFilteredSuggestionsData(word:string):FilteredSuggestions {
- return Object.keys(this.suggestionsInitialValue).map((key) => {
- const group = this.suggestionsInitialValue[key];
+ return Object.keys(this.validSuggestions).map((key) => {
+ const group = this.validSuggestions[key];
return {
key,
label: group.title,
@@ -445,9 +454,19 @@ export default class PatternInputController extends Controller {
}).filter((group) => group.values.length > 0);
}
+ private populateValidSuggestions():void {
+ for (const key of Object.keys(this.suggestionsInitialValue)) {
+ const group = this.suggestionsInitialValue[key];
+ this.validSuggestions[key] = {
+ title: group.title,
+ tokens: group.tokens.filter((token) => token.enabled)
+ };
+ }
+ }
+
private tagInvalidTokens():void {
this.contentTarget.querySelectorAll('[data-role="token"]').forEach((element:TokenElement) => {
- const exists = Object.keys(this.validTokenMap).some((key) => key === element.dataset.prop);
+ const exists = Object.keys(this.validTokenMap).some((key) => this.validTokenMap[key].enabled && key === element.dataset.prop);
if (exists) {
this.setStyle(element, 'accent');
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 995fa744e49..649f853271a 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
@@ -57,6 +57,9 @@ RSpec.describe WorkPackageTypes::Patterns::TokenPropertyMapper do
work_package.type.custom_fields << custom_field
end
end
+ shared_let(:custom_field_not_on_type) do
+ create(:string_wp_custom_field)
+ end
shared_let(:boolean_custom_field) do
create(:boolean_wp_custom_field).tap do |custom_field|
@@ -100,37 +103,62 @@ RSpec.describe WorkPackageTypes::Patterns::TokenPropertyMapper do
end
end
- it "multi value fields are supported" do
- token = described_class.new.tokens_for_type(work_package.type).detect do |t|
- t.key == :"custom_field_#{mult_list_custom_field.id}"
+ describe "#tokens_for_type" do
+ it "multi value fields are supported" do
+ token = described_class.new.tokens_for_type(work_package.type).detect do |t|
+ t.key == :"custom_field_#{mult_list_custom_field.id}"
+ end
+ expect(token.call(work_package)).to eq(%w[A B])
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|
- t.key == :"custom_field_#{boolean_custom_field.id}"
+ it "supports boolean custom fields" do
+ token = described_class.new.tokens_for_type(work_package.type).detect do |t|
+ t.key == :"custom_field_#{boolean_custom_field.id}"
+ end
+
+ expect(token.call(work_package)).to be(true)
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|
+ t.key == :"custom_field_#{not_activated_custom_field.id}"
+ 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|
- t.key == :"custom_field_#{not_activated_custom_field.id}"
+ expect { token.call(work_package) }.not_to raise_error
+ expect(token.call(work_package)).to eq(:attribute_not_available)
end
- expect { token.call(work_package) }.not_to raise_error
- expect(token.call(work_package)).to eq(:attribute_not_available)
+ it "returns all possible tokens" do
+ cf = string_custom_field
+ tokens = described_class.new.tokens_for_type(work_package.type)
+
+ 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)
+ 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
- it "returns all possible tokens" do
- cf = string_custom_field
- tokens = described_class.new.tokens_for_type(work_package.type)
+ describe "#all_tokens" do
+ it "returns all possible tokens" do
+ cf = string_custom_field
+ tokens = described_class.new.all_tokens
- 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(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)
+ end
+
+ it "returns a token that's not on the correct type" 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)
+ end
end
private