Merge pull request #7865 from artoscinote/ma_SCI_11057

Simplify and fix shareable scope [SCI-11057]
This commit is contained in:
Martin Artnik 2024-09-18 15:09:10 +02:00 committed by GitHub
commit 926a9ee6d4
No known key found for this signature in database
GPG key ID: B5690EEEBB952194

View file

@ -27,21 +27,23 @@ module Shareable
end end
scope :viewable_by_user, lambda { |user, teams = user.current_team| scope :viewable_by_user, lambda { |user, teams = user.current_team|
readable = readable_by_user(user).left_outer_joins(:team_shared_objects) readable_ids = readable_by_user(user).pluck(:id)
readable shared_with_team_ids = joins(:team_shared_objects, :team).where(team_shared_objects: { team: teams }).pluck(:id)
.where(team: teams) globally_shared_ids =
.or(model.where(team_shared_objects: { team: teams })) if column_names.include?('permission_level')
.or(model.where(if column_names.include?('permission_level') joins(:team).where(
{ {
permission_level: [ permission_level: [
Extends::SHARED_OBJECTS_PERMISSION_LEVELS[:shared_read], Extends::SHARED_OBJECTS_PERMISSION_LEVELS[:shared_read],
Extends::SHARED_OBJECTS_PERMISSION_LEVELS[:shared_write] Extends::SHARED_OBJECTS_PERMISSION_LEVELS[:shared_write]
] ]
} }
else ).pluck(:id)
{} else
end).where.not(team: teams)) none.pluck(:id)
.distinct end
where(id: (readable_ids + shared_with_team_ids + globally_shared_ids).uniq)
} }
rescue ActiveRecord::NoDatabaseError, rescue ActiveRecord::NoDatabaseError,
ActiveRecord::ConnectionNotEstablished, ActiveRecord::ConnectionNotEstablished,