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 using Hashie::Mash

Christophe Bliard83564887c62f
diff --git a/app/services/work_packages/update_ancestors/loader.rb b/app/services/work_packages/update_ancestors/loader.rb
index bebb80880e2..21eda6038a1 100644
--- a/app/services/work_packages/update_ancestors/loader.rb
+++ b/app/services/work_packages/update_ancestors/loader.rb
@@ -104,7 +104,7 @@ class WorkPackages::UpdateAncestors::Loader
attributes = selected_descendants_attributes
scope
.pluck(*attributes)
- .map { |p| LoaderStruct.new(attributes.zip(p).to_h) }
+ .map { |p| WorkPackageLikeStruct.new(**attributes.zip(p).to_h) }
end
# Returns the current ancestors sorted by distance (called generations in the table)
@@ -126,7 +126,7 @@ class WorkPackages::UpdateAncestors::Loader
def selected_descendants_attributes
# By having the id in here, we can avoid DISTINCT queries squashing duplicate values
- %i[id estimated_hours parent_id schedule_manually ignore_non_working_days remaining_hours]
+ %i[id parent_id estimated_hours remaining_hours schedule_manually ignore_non_working_days]
end
##
@@ -152,6 +152,7 @@ class WorkPackages::UpdateAncestors::Loader
previous_parent_changes&.first
end
- class LoaderStruct < Hashie::Mash; end
- LoaderStruct.disable_warnings
+ WorkPackageLikeStruct = Data.define(:id, :parent_id,
+ :estimated_hours, :remaining_hours,
+ :schedule_manually, :ignore_non_working_days)
end
diff --git a/spec/services/work_packages/update_ancestors/loader_spec.rb b/spec/services/work_packages/update_ancestors/loader_spec.rb
index 0b4f7c70bd2..6e20a2ca707 100644
--- a/spec/services/work_packages/update_ancestors/loader_spec.rb
+++ b/spec/services/work_packages/update_ancestors/loader_spec.rb
@@ -223,67 +223,55 @@ RSpec.describe WorkPackages::UpdateAncestors::Loader, type: :model do
end
end
- describe "#descendants_of" do
- def descendants_of_hash(hashed_work_package)
- { "estimated_hours" => nil,
- "id" => hashed_work_package.id,
- "ignore_non_working_days" => false,
- "parent_id" => hashed_work_package.parent_id,
- "remaining_hours" => nil,
- "schedule_manually" => false }
- end
+ def work_package_struct(work_package)
+ attribute_names = WorkPackages::UpdateAncestors::Loader::WorkPackageLikeStruct.members.map(&:to_s)
+ attributes = work_package.attributes.slice(*attribute_names)
+ WorkPackages::UpdateAncestors::Loader::WorkPackageLikeStruct.new(**attributes)
+ end
+ describe "#descendants_of" do
context "for the work_package" do
- it "is its child (as a hash)" do
+ it "is its child (as a struct)" do
expect(instance.descendants_of(work_package))
- .to contain_exactly(descendants_of_hash(child))
+ .to contain_exactly(work_package_struct(child))
end
end
context "for the parent" do
- it "is the work package, its child (as a hash) and its sibling (as a hash)" do
+ it "is the work package, its child (as a struct) and its sibling (as a struct)" do
expect(instance.descendants_of(parent))
- .to contain_exactly(descendants_of_hash(child), work_package, descendants_of_hash(sibling))
+ .to contain_exactly(work_package_struct(child), work_package, work_package_struct(sibling))
end
end
context "for the grandparent" do
- it "is the parent, the work package, its child (as a hash) and its sibling (as a hash)" do
+ it "is the parent, the work package, its child (as a struct) and its sibling (as a struct)" do
expect(instance.descendants_of(grandparent))
- .to contain_exactly(parent, work_package, descendants_of_hash(child), descendants_of_hash(sibling))
+ .to contain_exactly(parent, work_package, work_package_struct(child), work_package_struct(sibling))
end
end
context "for the grandgrandparent (the root)" do
- it "is the complete tree, partly as a hash and partly as the preloaded work packages" do
+ it "is the complete tree, partly as a struct and partly as the preloaded work packages" do
expect(instance.descendants_of(grandgrandparent))
- .to contain_exactly(descendants_of_hash(grandparent_sibling), grandparent, parent, work_package,
- descendants_of_hash(child), descendants_of_hash(sibling))
+ .to contain_exactly(work_package_struct(grandparent_sibling), grandparent, parent, work_package,
+ work_package_struct(child), work_package_struct(sibling))
end
end
end
describe "#children_of" do
- def children_of_hash(hashed_work_package)
- { "estimated_hours" => nil,
- "id" => hashed_work_package.id,
- "ignore_non_working_days" => false,
- "parent_id" => hashed_work_package.parent_id,
- "remaining_hours" => nil,
- "schedule_manually" => false }
- end
-
context "for the work_package" do
- it "is its child (as a hash)" do
+ it "is its child (as a struct)" do
expect(instance.children_of(work_package))
- .to contain_exactly(children_of_hash(child))
+ .to contain_exactly(work_package_struct(child))
end
end
context "for the parent" do
- it "is the work package and its sibling (as a hash)" do
+ it "is the work package and its sibling (as a struct)" do
expect(instance.children_of(parent))
- .to contain_exactly(work_package, children_of_hash(sibling))
+ .to contain_exactly(work_package, work_package_struct(sibling))
end
end
@@ -295,9 +283,9 @@ RSpec.describe WorkPackages::UpdateAncestors::Loader, type: :model do
end
context "for the grandgrandparent" do
- it "is the grandparent and its sibling (as a hash)" do
+ it "is the grandparent and its sibling (as a struct)" do
expect(instance.children_of(grandgrandparent))
- .to contain_exactly(children_of_hash(grandparent_sibling), grandparent)
+ .to contain_exactly(work_package_struct(grandparent_sibling), grandparent)
end
end
end