Skip to content

Commit

Permalink
add project id of project embeds to macro node, so frontend does not …
Browse files Browse the repository at this point in the history
…need to search;

Closes #16542 (comment)
  • Loading branch information
as-op committed Sep 2, 2024
1 parent c08e9b6 commit b10caed
Show file tree
Hide file tree
Showing 2 changed files with 35 additions and 14 deletions.
25 changes: 18 additions & 7 deletions lib/open_project/text_formatting/matchers/attribute_macros.rb
Original file line number Diff line number Diff line change
Expand Up @@ -53,11 +53,21 @@ def self.applicable?(content)
end

def self.work_package_context?(context)
# workPackageValue can be used in e.g. wiki and meeting notes without a work package,
# relative embedding is not supported in these cases
context[:object].is_a?(WorkPackage) || context[:object].is_a?(API::V3::WorkPackages::WorkPackageEagerLoadingWrapper)
end

def self.relative_work_package_embed?(macro_attributes)
macro_attributes[:id].nil? && macro_attributes[:model] == "workPackage"
def self.work_package_embed?(macro_attributes)
macro_attributes[:model] == "workPackage"
end

def self.project_embed?(macro_attributes)
macro_attributes[:model] == "project"
end

def self.relative_embed?(macro_attributes)
macro_attributes[:id].nil?
end

def self.process_match(match, _matched_string, context)
Expand All @@ -69,11 +79,12 @@ def self.process_match(match, _matched_string, context)
}
type = match[2].downcase

# If no ID is given, try to use the current work package ID if applicable
# workPackageValue can be used in e.g. wiki and meeting notes without a work package,
# relative embedding is not supported in these cases
if relative_work_package_embed?(macro_attributes) && work_package_context?(context)
macro_attributes[:id] = context[:object].try(:id)
if relative_embed?(macro_attributes)
if project_embed?(macro_attributes) && context[:project].present?
macro_attributes[:id] = context[:project].try(:id)
elsif work_package_embed?(macro_attributes) && work_package_context?(context)
macro_attributes[:id] = context[:object].try(:id)
end
end

ApplicationController.helpers.content_tag "opce-macro-attribute-#{type}",
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@
RSpec.describe OpenProject::TextFormatting,
"Attribute macros" do
include_context "expected markdown modules"
shared_let(:project) { create(:valid_project) }
shared_let(:project) { create(:valid_project, id: 4321) }
let(:work_package) { create(:work_package, project:, id: 1234) }
let(:options) { { project:, object: work_package } }

Expand All @@ -50,7 +50,9 @@
Inline reference to project: projectLabel:status
Inline reference to project with id: projectLabel:"some id":status
Inline reference to project with id: projectLabel:4321:status
Inline reference to project with name: projectLabel:"some name":status
RAW
end

Expand All @@ -70,10 +72,13 @@
Inline reference to WP by subject: <opce-macro-attribute-label data-model="workPackage" data-id="Some subject" data-attribute="Some custom field with spaces"></opce-macro-attribute-label>
</p>
<p class="op-uc-p">
Inline reference to project: <opce-macro-attribute-label data-model="project" data-attribute="status"></opce-macro-attribute-label>
Inline reference to project: <opce-macro-attribute-label data-model="project" data-id="4321" data-attribute="status"></opce-macro-attribute-label>
</p>
<p class="op-uc-p">
Inline reference to project with id: <opce-macro-attribute-label data-model="project" data-id="4321" data-attribute="status"></opce-macro-attribute-label>
</p>
<p class="op-uc-p">
Inline reference to project with id: <opce-macro-attribute-label data-model="project" data-id="some id" data-attribute="status"></opce-macro-attribute-label>
Inline reference to project with name: <opce-macro-attribute-label data-model="project" data-id="some name" data-attribute="status"></opce-macro-attribute-label>
</p>
EXPECTED
end
Expand All @@ -94,7 +99,9 @@
Inline reference to project: projectValue:status
Inline reference to project with id: projectValue:"some id":status
Inline reference to project with id: projectValue:4321:status
Inline reference to project with name: projectValue:"some name":status
RAW
end

Expand All @@ -114,10 +121,13 @@
Inline reference to WP by subject: <opce-macro-attribute-value data-model="workPackage" data-id="Some subject" data-attribute="Some custom field with spaces"></opce-macro-attribute-value>
</p>
<p class="op-uc-p">
Inline reference to project: <opce-macro-attribute-value data-model="project" data-attribute="status"></opce-macro-attribute-value>
Inline reference to project: <opce-macro-attribute-value data-model="project" data-id="4321" data-attribute="status"></opce-macro-attribute-value>
</p>
<p class="op-uc-p">
Inline reference to project with id: <opce-macro-attribute-value data-model="project" data-id="4321" data-attribute="status"></opce-macro-attribute-value>
</p>
<p class="op-uc-p">
Inline reference to project with id: <opce-macro-attribute-value data-model="project" data-id="some id" data-attribute="status"></opce-macro-attribute-value>
Inline reference to project with name: <opce-macro-attribute-value data-model="project" data-id="some name" data-attribute="status"></opce-macro-attribute-value>
</p>
EXPECTED
end
Expand Down

0 comments on commit b10caed

Please sign in to comment.