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

Only reference seed data by reference name

Christophe Bliard45918535604a
diff --git a/app/seeders/demo_data/overview_seeder.rb b/app/seeders/demo_data/overview_seeder.rb
index 1f501ddeb60..53923772320 100644
--- a/app/seeders/demo_data/overview_seeder.rb
+++ b/app/seeders/demo_data/overview_seeder.rb
@@ -83,14 +83,14 @@ module DemoData
def text_with_references(overview, widget_options)
if widget_options && widget_options['text']
- widget_options['text'] = with_references(widget_options['text'], overview.project)
+ widget_options['text'] = with_references(widget_options['text'])
widget_options['text'] = link_attachments(widget_options['text'], overview.attachments)
end
end
def query_id_references(overview, widget_options)
if widget_options && widget_options['queryId']
- widget_options['queryId'] = with_references(widget_options['queryId'], overview.project)
+ widget_options['queryId'] = with_references(widget_options['queryId'])
end
end
diff --git a/app/seeders/demo_data/references.rb b/app/seeders/demo_data/references.rb
index f0375832b8b..163237a5730 100644
--- a/app/seeders/demo_data/references.rb
+++ b/app/seeders/demo_data/references.rb
@@ -37,10 +37,10 @@ module DemoData
API::V3::Utilities::PathHelper::ApiV3Path
end
- def with_references(str, project)
- res = link_work_packages str, project
- res = link_queries res, project
- link_sprints res, project
+ def with_references(str)
+ res = link_work_packages str
+ res = link_queries res
+ link_sprints res
end
##
@@ -52,7 +52,7 @@ module DemoData
# @param str [String] String in which to substitute attachment references.
# @param attachments [ActiveRecord::QueryMethods] Query to set of attachments which can be referenced.
def link_attachments(str, attachments)
- return str unless str.present?
+ return str if str.blank?
str.gsub(/##attachment(\.id)?:"[^"]+"/) do |match|
file = match.split(":", 2).last[1..-2] # strip quotes of part behind :
@@ -67,85 +67,62 @@ module DemoData
end
##
- # Turns `##query:"Gantt chart"` into
+ # Turns `##query:gantt_chart` into
# `/projects/demo-project/work_packages?query_id=1` given there is a query
- # named "Gantt chart" (its ID here being 1).
+ # referenced with :gantt_chart and its ID here is 1.
#
- # Alternatively `##query.id:"Gantt chart"` is translated into just the ID.`
- def link_queries(str, project)
- link_reference(
- str,
- model: Query,
- find_by: :name,
- project:,
- link: ->(query) { query_link query }
- )
+ # Alternatively `##query.id:gantt_chart` is translated into just the ID.
+ def link_queries(str)
+ link_references(str, tag: 'query')
end
- ## Turns `##wp:"Some subject"` or `##wp:some_subject` into
+ ## Turns `##wp:some_subject` into
# `/projects/demo-project/work_packages/42/activity` given there is a work
- # package named "Some subject" (its subject here being "Some subject") or
- # referenced with :some_subject.
+ # package referenced with :some_subject and ID here is 42.
#
- # Alternatively `##wp.id:"Some subject"` or `##wp.id:some_subject` is
- # translated into just the ID.`
- def link_work_packages(str, project)
- link_reference(
- str,
- model: WorkPackage,
- tag: "wp",
- find_by: :subject,
- project:,
- link: ->(wp) { work_package_link wp }
- )
+ # Alternatively `##wp.id:some_subject` is translated into just the ID.
+ def link_work_packages(str)
+ link_references(str, tag: 'wp')
end
- def link_sprints(str, project)
+ def link_sprints(str)
return str unless defined? OpenProject::Backlogs
- link_reference(
- str,
- model: Sprint,
- find_by: :name,
- project:,
- link: ->(sprint) { sprint_link sprint }
- )
+ link_references(str, tag: 'sprint')
end
- def link_reference(str, model:, find_by:, project:, link:, tag: nil)
+ def link_references(str, tag:)
return str if str.blank?
- tag ||= model.name.downcase
-
- [
- [/###{tag}(\.id)?:"[^"]+"/, ->(match) { find_instance_by_query(match, model:, find_by:, project:) }],
- [/###{tag}(\.id)?:[a-z_0-9]+/, ->(match) { find_instance_by_reference(match) }]
- ].reduce(str) do |str_acc, (regex, find_instance)|
- str_acc.gsub(regex) do |match|
- instance = find_instance.(match)
- if match.include?(".id")
- instance.id
- else
- link.call instance
- end
+ str.gsub(/###{tag}(\.id)?:[a-z_0-9]+/) do |match|
+ reference = match.split(":", 2).last.to_sym
+ instance = seed_data.find_reference(reference)
+ if match.include?(".id")
+ instance.id
+ else
+ link(tag, instance)
end
end
end
- def find_instance_by_query(text, model:, find_by:, project:)
- identifier = text.split(":", 2).last[1..-2] # strip quotes of part behind :
- model.where(find_by => identifier, :project => project).first!
- end
-
- def find_instance_by_reference(text)
- reference = text.split(":", 2).last.to_sym
- seed_data.find_reference(reference)
+ def link(tag, instance)
+ case tag
+ when 'query'
+ query_link(instance)
+ when 'wp'
+ work_package_link(instance)
+ when 'sprint'
+ sprint_link(instance)
+ else
+ raise ArgumentError, "cannot create link for #{tag.inspect}"
+ end
end
def query_link(query)
- path = url_helpers.project_work_packages_path project_id: query.project.identifier
-
- "#{path}?query_id=#{query.id}"
+ url_helpers.project_work_packages_path(
+ project_id: query.project.identifier,
+ query_id: query.id
+ )
end
def work_package_link(work_package)
diff --git a/app/seeders/demo_data/version_builder.rb b/app/seeders/demo_data/version_builder.rb
index d83a0363760..97dcb45ee5f 100644
--- a/app/seeders/demo_data/version_builder.rb
+++ b/app/seeders/demo_data/version_builder.rb
@@ -73,7 +73,7 @@ module DemoData
version.wiki_page_title = config['title']
page = WikiPage.create! wiki: version.project.wiki, title: version.wiki_page_title
- content = with_references config['content'], project
+ content = with_references config['content']
Journal::NotificationConfiguration.with false do
WikiContent.create! page:, author: user, text: content
end
diff --git a/app/seeders/demo_data/work_package_seeder.rb b/app/seeders/demo_data/work_package_seeder.rb
index c7c48e0d746..42be12ad7c5 100644
--- a/app/seeders/demo_data/work_package_seeder.rb
+++ b/app/seeders/demo_data/work_package_seeder.rb
@@ -77,7 +77,7 @@ module DemoData
description = work_package.description
description = link_attachments description, work_package.attachments
- description = with_references description, project
+ description = with_references description
work_package.update(description:)
@@ -113,15 +113,8 @@ module DemoData
attributes['work_package'] = work_package
end
- def find_work_package(subject_or_reference)
- case subject_or_reference
- when String
- subject = subject_or_reference
- WorkPackage.find_by(subject:)
- when Symbol
- reference = subject_or_reference
- seed_data.find_reference(reference)
- end
+ def find_work_package(reference)
+ seed_data.find_reference(reference)
end
def find_principal(reference)
diff --git a/spec/seeders/demo_data/work_package_seeder_spec.rb b/spec/seeders/demo_data/work_package_seeder_spec.rb
index dd229e9309a..528cd5500c0 100644
--- a/spec/seeders/demo_data/work_package_seeder_spec.rb
+++ b/spec/seeders/demo_data/work_package_seeder_spec.rb
@@ -213,20 +213,6 @@ describe DemoData::WorkPackageSeeder do
end
end
- context 'with a parent relation by subject name' do
- let(:work_packages_data) do
- [
- work_package_data(subject: 'Parent'),
- work_package_data(subject: 'Child', parent: 'Parent')
- ]
- end
-
- it 'creates a parent-child relation between work packages' do
- expect(WorkPackage.count).to eq(2)
- expect(WorkPackage.second.parent).to eq(WorkPackage.first)
- end
- end
-
context 'with a parent relation by reference' do
let(:work_packages_data) do
[
@@ -277,35 +263,61 @@ describe DemoData::WorkPackageSeeder do
end
end
- context 'with a work package description referencing another work package by a reference name' do
+ context 'with a work package description referencing a work package with ##wp:ref notation' do
let(:work_packages_data) do
[
work_package_data(subject: 'Major thing to do',
reference: :major_thing),
work_package_data(subject: 'Other thing',
- description: 'Please also check [this one]({{opSetting:base_url}}/wp/##wp.id:major_thing).')
+ description: 'Check [this work package](##wp:major_thing) of id ##wp.id:major_thing.')
]
end
it 'creates parent-child relations between work packages' do
wp_major, wp_other = WorkPackage.order(:id).to_a
- expect(wp_other.description).to eq("Please also check [this one]({{opSetting:base_url}}/wp/#{wp_major.id}).")
+ expect(wp_other.description)
+ .to eq("Check [this work package](/projects/#{project.identifier}/" \
+ "work_packages/#{wp_major.id}/activity) of id #{wp_major.id}.")
end
end
- context 'with a work package description referencing another work package by subject name' do
+ context 'with a work package description referencing a query with ##query:ref notation' do
+ let(:seed_data) do
+ seed_data = SeedData.new('work_packages' => work_packages_data)
+ seed_data.store_reference(:q_project_plan, query)
+ seed_data
+ end
+ let(:query) { create(:query, project:) }
let(:work_packages_data) do
[
- work_package_data(subject: 'Major thing to do',
- reference: :major_thing),
- work_package_data(subject: 'Other thing',
- description: 'Please also check [this one]({{opSetting:base_url}}/wp/##wp.id:"Major thing to do").')
+ work_package_data(subject: 'Referencing query',
+ description: 'The [query](##query:q_project_plan) of id ##query.id:q_project_plan.')
]
end
- it 'creates parent-child relations between work packages' do
- wp_major, wp_other = WorkPackage.order(:id).to_a
- expect(wp_other.description).to eq("Please also check [this one]({{opSetting:base_url}}/wp/#{wp_major.id}).")
+ it 'creates link to the query with the right id' do
+ expect(WorkPackage.last.description)
+ .to eq("The [query](/projects/#{project.identifier}/work_packages?query_id=#{query.id}) of id #{query.id}.")
+ end
+ end
+
+ context 'with a work package description referencing a sprint with ##sprint:ref notation' do
+ let(:seed_data) do
+ seed_data = SeedData.new('work_packages' => work_packages_data)
+ seed_data.store_reference(:sprint_backlog, sprint)
+ seed_data
+ end
+ let(:sprint) { create(:sprint, project:) }
+ let(:work_packages_data) do
+ [
+ work_package_data(subject: 'Referencing sprint',
+ description: 'The [sprint](##sprint:sprint_backlog) of id ##sprint.id:sprint_backlog.')
+ ]
+ end
+
+ it 'creates link to the sprint with the right id' do
+ expect(WorkPackage.last.description)
+ .to eq("The [sprint](/projects/#{project.identifier}/sprints/#{sprint.id}/taskboard) of id #{sprint.id}.")
end
end