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

change implementation to association extension

ulferts55e1755e64f8
diff --git a/app/components/work_package_relations_tab/relations_mediator.rb b/app/components/work_package_relations_tab/relations_mediator.rb
index 5de66987a39..358c82f2540 100644
--- a/app/components/work_package_relations_tab/relations_mediator.rb
+++ b/app/components/work_package_relations_tab/relations_mediator.rb
@@ -38,7 +38,7 @@ class WorkPackageRelationsTab::RelationsMediator
end
def visible_relations
- @visible_relations ||= work_package.visible_relations.includes(:to, :from)
+ @visible_relations ||= work_package.relations.visible.includes(:to, :from)
end
def visible_children
diff --git a/app/models/work_packages/relations.rb b/app/models/work_packages/relations.rb
index b17a9f63949..d28b23f159f 100644
--- a/app/models/work_packages/relations.rb
+++ b/app/models/work_packages/relations.rb
@@ -29,14 +29,23 @@ module WorkPackages::Relations
included do
# All relations of the work package in both directions.
- # Mostly used to have the relations destroyed upon destruction of the work package.
# rubocop:disable Rails/InverseOf
has_many :relations,
->(work_package) {
unscope(:where)
.of_work_package(work_package)
},
- dependent: :destroy
+ dependent: :destroy do
+ def visible(user = User.current)
+ # The work_package_focus_scope in here is used to improve performance by reducing the number of
+ # work packages that have to be checked for whether they are visible.
+ # Since the method looks for relations on the current work package, only work packages
+ # that are on either end of a relation with the current work package need to be considered.
+ # The number should be quite small compared to the total number of work packages.
+ merge(Relation.visible(user,
+ work_package_focus_scope: Arel::Nodes::UnionAll.new(select(:from_id).arel, select(:to_id).arel)))
+ end
+ end
# rubocop:enable Rails/InverseOf
# Relations where the current work package follows another one.
@@ -87,31 +96,4 @@ module WorkPackages::Relations
dependent: :nullify,
inverse_of: :to
end
-
- def visible_relations(user = User.current)
- # The query in here is used to improve performance by reducing the number of
- # work packages that have to be checked for whether they are visible.
- # Since the method looks for relations on the current work package, only work packages
- # that are on either end of a relation with the current work package need to be considered.
- # The number should be quite small compared to the total number of work packages.
- relation_from_or_to = Relation
- .select(
- <<~SQL.squish
- CASE
- WHEN from_id = #{id}
- THEN to_id
- ELSE from_id
- END id
- SQL
- )
- .where("relations.from_id = :id OR relations.to_id = :id", id: id)
- .arel
-
- self_id = Arel.sql("SELECT #{id} AS id")
-
- wp_focus_scope = Arel::Nodes::UnionAll.new(relation_from_or_to, self_id)
-
- relations
- .visible(user, work_package_focus_scope: wp_focus_scope)
- end
end
diff --git a/lib/api/v3/work_packages/work_package_representer.rb b/lib/api/v3/work_packages/work_package_representer.rb
index 20ea4a61abe..96ccff0b962 100644
--- a/lib/api/v3/work_packages/work_package_representer.rb
+++ b/lib/api/v3/work_packages/work_package_representer.rb
@@ -645,7 +645,8 @@ module API
def relations
self_path = api_v3_paths.work_package_relations(represented.id)
visible_relations = represented
- .visible_relations(current_user)
+ .relations
+ .visible(current_user)
.includes(::API::V3::Relations::RelationCollectionRepresenter.to_eager_load)
::API::V3::Relations::RelationCollectionRepresenter.new(visible_relations,
diff --git a/modules/xls_export/app/models/xls_export/work_package/exporter/xls.rb b/modules/xls_export/app/models/xls_export/work_package/exporter/xls.rb
index 7763d4dc3cd..482b820fa2e 100644
--- a/modules/xls_export/app/models/xls_export/work_package/exporter/xls.rb
+++ b/modules/xls_export/app/models/xls_export/work_package/exporter/xls.rb
@@ -155,7 +155,7 @@ module XlsExport::WorkPackage::Exporter
end
def work_package_relations(work_package)
- work_package.visible_relations
+ work_package.relations.visible
end
end
end
diff --git a/spec/lib/api/v3/work_packages/work_package_representer_spec.rb b/spec/lib/api/v3/work_packages/work_package_representer_spec.rb
index ad8e3533b04..af7024edad8 100644
--- a/spec/lib/api/v3/work_packages/work_package_representer_spec.rb
+++ b/spec/lib/api/v3/work_packages/work_package_representer_spec.rb
@@ -1233,15 +1233,13 @@ RSpec.describe API::V3::WorkPackages::WorkPackageRepresenter do
end
before do
- scope = instance_double(ActiveRecord::Relation)
+ scope = double(ActiveRecord::Relation, # rubocop:disable RSpec/VerifiedDoubles
+ visible: instance_double(ActiveRecord::Relation,
+ includes: [relation]))
allow(work_package)
- .to receive(:visible_relations)
- .with(current_user)
+ .to receive(:relations)
.and_return(scope)
- allow(scope)
- .to receive(:includes)
- .and_return([relation])
end
it "embeds a collection" do
diff --git a/spec/models/work_package/work_package_relations_spec.rb b/spec/models/work_package/work_package_relations_spec.rb
index 2fca4a7988b..ca3a9a1ec2b 100644
--- a/spec/models/work_package/work_package_relations_spec.rb
+++ b/spec/models/work_package/work_package_relations_spec.rb
@@ -255,7 +255,8 @@ RSpec.describe WorkPackage do
end
end
- describe "#visible_relations" do
+ # The combination is speced because it is implemented in a non trivial way.
+ describe "#relations.visible" do
let!(:user) { create(:user) }
let!(:view_work_packages_role) { create(:project_role, permissions: %i[view_work_packages]) }
let!(:no_permission_role) { create(:project_role, permissions: %i[]) }
@@ -282,7 +283,7 @@ RSpec.describe WorkPackage do
let!(:other_relation) { create(:relation, from: other_work_package, to: visible_work_package) }
it "returns all relations from the called on work package visible to the user" do
- expect(origin.visible_relations(user))
+ expect(origin.relations.visible(user))
.to contain_exactly(visible_relation, inverted_visible_relation, shared_relation)
end
end