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

refactor: Avoid storing work_package instance in attributes

Christophe Bliard9b6d4384cd6f
diff --git a/app/seeders/demo_data/work_package_seeder.rb b/app/seeders/demo_data/work_package_seeder.rb
index 2c560a9b3c8..1596c49f072 100644
--- a/app/seeders/demo_data/work_package_seeder.rb
+++ b/app/seeders/demo_data/work_package_seeder.rb
@@ -39,6 +39,7 @@ module DemoData
@statuses = Status.all
@repository = Repository.first
@types = project.types.all.reject(&:is_milestone?)
+ @relations_to_create = []
end
def seed_data!
@@ -50,10 +51,13 @@ module DemoData
private
+ RelationData = Data.define(:from, :to_reference, :type)
+
+ attr_reader :relations_to_create
+
def seed_demo_work_packages
project_data.each('work_packages') do |attributes|
- work_package = create_or_update_work_package(attributes)
- memorize_work_package(work_package, attributes)
+ create_or_update_work_package(attributes)
end
end
@@ -75,26 +79,13 @@ module DemoData
create_children! work_package, attributes
create_attachments! work_package, attributes
-
- description = work_package.description
- description = link_attachments description, work_package.attachments
- description = with_references description
-
- work_package.update(description:)
+ update_description! work_package
+ add_relations_to_create work_package, attributes
+ memorize_work_package work_package, attributes
work_package
end
- def create_children!(work_package, attributes)
- Array(attributes['children']).each do |child_attributes|
- child = create_work_package child_attributes
-
- child.parent = work_package
- child.save!
- memorize_work_package(child, child_attributes)
- end
- end
-
def base_work_package_attributes(attributes)
{
project:,
@@ -109,9 +100,32 @@ module DemoData
}
end
+ def create_children!(work_package, attributes)
+ Array(attributes['children']).each do |child_attributes|
+ child = create_work_package child_attributes
+
+ child.parent = work_package
+ child.save!
+ end
+ end
+
+ def update_description!(work_package)
+ description = work_package.description
+ description = link_attachments description, work_package.attachments
+ description = with_references description
+
+ work_package.update(description:)
+ end
+
+ def add_relations_to_create(work_package, attributes)
+ Array(attributes['relations']).each do |relation|
+ relation_data = RelationData.new(from: work_package, to_reference: relation['to'], type: relation['type'])
+ relations_to_create.push(relation_data)
+ end
+ end
+
def memorize_work_package(work_package, attributes)
project_data.store_reference(attributes['reference'], work_package)
- attributes['work_package'] = work_package
end
def find_work_package(reference)
@@ -155,25 +169,16 @@ module DemoData
end
def set_work_package_relations
- project_data.each('work_packages') do |attributes|
- create_relations attributes
- end
- end
-
- def create_relations(attributes)
- Array(attributes['relations']).each do |relation|
- root_work_package = attributes['work_package'] # memorized on creation
- to_work_package = find_work_package(relation['to'])
+ while relations_to_create.any?
+ relation_data = relations_to_create.pop
+ from_work_package = relation_data.from
+ to_work_package = find_work_package(relation_data.to_reference)
create_relation(
to: to_work_package,
- from: root_work_package,
- type: relation['type']
+ from: from_work_package,
+ type: relation_data.type
)
end
-
- Array(attributes['children']).each do |child_attributes|
- create_relations child_attributes
- end
end
def create_relation(to:, from:, type:)
diff --git a/spec/seeders/demo_data/work_package_seeder_spec.rb b/spec/seeders/demo_data/work_package_seeder_spec.rb
index 759437a0641..eb5ac6c9645 100644
--- a/spec/seeders/demo_data/work_package_seeder_spec.rb
+++ b/spec/seeders/demo_data/work_package_seeder_spec.rb
@@ -263,6 +263,65 @@ describe DemoData::WorkPackageSeeder do
end
end
+ context 'with relations defined between multiple work packages' do
+ let(:work_packages_data) do
+ [
+ work_package_data(
+ subject: 'Predecessor',
+ reference: :wp_predecessor,
+ relations: [{
+ type: :relates,
+ to: :wp_child
+ }]
+ ),
+ work_package_data(
+ subject: 'Successor',
+ reference: :wp_successor,
+ relations: [{
+ type: :follows,
+ to: :wp_predecessor
+ }, {
+ type: :includes,
+ to: :wp_child
+ }],
+ children: [
+ work_package_data(
+ subject: 'Child',
+ reference: :wp_child,
+ relations: [{
+ type: :blocks,
+ to: :wp_successor
+ }]
+ )
+ ]
+ )
+ ]
+ end
+
+ it 'creates the given relationships between the work packages' do
+ predecessor = WorkPackage.find_by!(subject: 'Predecessor')
+ successor = WorkPackage.find_by!(subject: 'Successor')
+ child = WorkPackage.find_by!(subject: 'Child')
+
+ expect(Relation.find_by!(relation_type: Relation::TYPE_RELATES)).to have_attributes(
+ from_id: predecessor.id,
+ to_id: child.id
+ )
+ expect(Relation.find_by!(relation_type: Relation::TYPE_FOLLOWS)).to have_attributes(
+ from_id: successor.id,
+ to_id: predecessor.id
+ )
+ expect(Relation.find_by!(relation_type: Relation::TYPE_INCLUDES)).to have_attributes(
+ from_id: successor.id,
+ to_id: child.id
+ )
+ expect(Relation.find_by!(relation_type: Relation::TYPE_BLOCKS)).to have_attributes(
+ from_id: child.id,
+ to_id: successor.id
+ )
+ end
+ end
+
context 'with a work package description referencing a work package with ##wp:ref notation' do
let(:work_packages_data) do
[
@@ -273,7 +332,7 @@ describe DemoData::WorkPackageSeeder do
]
end
- it 'creates parent-child relations between work packages' do
+ it 'updates the description with a link to the work package with the right id' do
wp_major, wp_other = WorkPackage.order(:id).to_a
expect(wp_other.description)
.to eq("Check [this work package](/projects/#{project.identifier}/" \