Merge pull request #7895 from artoscinote/ma_SCI_11103

Fix shared location status display [SCI-11103]
This commit is contained in:
Martin Artnik 2024-09-27 13:47:02 +02:00 committed by GitHub
commit d617a4de02
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
4 changed files with 58 additions and 47 deletions

View file

@ -23,8 +23,13 @@ class StorageLocationsController < ApplicationController
format.html
format.json do
storage_locations = Lists::StorageLocationsService.new(current_user, current_team, params).call
render json: storage_locations, each_serializer: Lists::StorageLocationSerializer,
user: current_user, meta: pagination_dict(storage_locations)
render json: storage_locations,
each_serializer: Lists::StorageLocationSerializer,
user: current_user,
meta: pagination_dict(storage_locations),
shared_object:
@parent_location &&
StorageLocation.select('*').select(StorageLocation.shared_sql_select(current_user)).find(@parent_location.root_storage_location.id)
end
end
end
@ -225,7 +230,7 @@ class StorageLocationsController < ApplicationController
storage_locations = []
if params[:parent_id] || @storage_location
location = (current_team.storage_locations.find_by(id: params[:parent_id]) || @storage_location)
location = StorageLocation.find_by(id: params[:parent_id]) || @storage_location
if location
storage_locations.unshift(breadcrumbs_item(location))
while location.parent

View file

@ -96,6 +96,43 @@ class StorageLocation < ApplicationRecord
rows
end
def self.shared_sql_select(user)
shared_write_value = TeamSharedObject.permission_levels['shared_write']
team_id = user.current_team.id
case_statement = <<-SQL.squish
CASE
WHEN EXISTS (
SELECT 1 FROM team_shared_objects
WHERE team_shared_objects.shared_object_id = storage_locations.id
AND team_shared_objects.shared_object_type = 'StorageLocation'
AND storage_locations.team_id = :team_id
) THEN 1
WHEN EXISTS (
SELECT 1 FROM team_shared_objects
WHERE team_shared_objects.shared_object_id = storage_locations.id
AND team_shared_objects.shared_object_type = 'StorageLocation'
AND team_shared_objects.team_id = :team_id
) THEN
CASE
WHEN EXISTS (
SELECT 1 FROM team_shared_objects
WHERE team_shared_objects.shared_object_id = storage_locations.id
AND team_shared_objects.shared_object_type = 'StorageLocation'
AND team_shared_objects.permission_level = :shared_write_value
AND team_shared_objects.team_id = :team_id
) THEN 2
ELSE 3
END
ELSE 4
END as shared
SQL
ActiveRecord::Base.sanitize_sql_array(
[case_statement, { team_id: team_id, shared_write_value: shared_write_value }]
)
end
def self.storage_locations_enabled?
ApplicationSettings.instance.values['storage_locations_enabled']
end

View file

@ -8,11 +8,11 @@ module ShareableSerializer
end
def shared
object.shared_with?(current_user.current_team)
shared_object.shared_with?(current_user.current_team)
end
def shared_label
case object[:shared]
case shared_object[:shared]
when 1
I18n.t('libraries.index.shared')
when 2
@ -25,18 +25,24 @@ module ShareableSerializer
end
def ishared
object.i_shared?(current_user.current_team)
shared_object.i_shared?(current_user.current_team)
end
def shared_read
object.shared_read?
shared_object.shared_read?
end
def shared_write
object.shared_write?
shared_object.shared_write?
end
def shareable_write
object.shareable_write?
shared_object.shareable_write?
end
private
def shared_object
instance_options[:shared_object] || object
end
end

View file

@ -22,7 +22,7 @@ module Lists
StorageLocation.joins('LEFT JOIN storage_locations AS sub_locations ' \
'ON storage_locations.id = sub_locations.parent_id AND sub_locations.discarded_at IS NULL')
.left_joins(:team, :created_by)
.select(shared_sql_select)
.select(StorageLocation.shared_sql_select(@user))
.select(
'storage_locations.*,
MAX(teams.name) as team_name,
@ -86,42 +86,5 @@ module Lists
@records = @records.order('created_by_full_name DESC')
end
end
def shared_sql_select
shared_write_value = TeamSharedObject.permission_levels['shared_write']
team_id = @user.current_team.id
case_statement = <<-SQL.squish
CASE
WHEN EXISTS (
SELECT 1 FROM team_shared_objects
WHERE team_shared_objects.shared_object_id = storage_locations.id
AND team_shared_objects.shared_object_type = 'StorageLocation'
AND storage_locations.team_id = :team_id
) THEN 1
WHEN EXISTS (
SELECT 1 FROM team_shared_objects
WHERE team_shared_objects.shared_object_id = storage_locations.id
AND team_shared_objects.shared_object_type = 'StorageLocation'
AND team_shared_objects.team_id = :team_id
) THEN
CASE
WHEN EXISTS (
SELECT 1 FROM team_shared_objects
WHERE team_shared_objects.shared_object_id = storage_locations.id
AND team_shared_objects.shared_object_type = 'StorageLocation'
AND team_shared_objects.permission_level = :shared_write_value
AND team_shared_objects.team_id = :team_id
) THEN 2
ELSE 3
END
ELSE 4
END as shared
SQL
ActiveRecord::Base.sanitize_sql_array(
[case_statement, { team_id: team_id, shared_write_value: shared_write_value }]
)
end
end
end