Merge pull request #7729 from artoscinote/ma_SCI_10859

Implement sub-location count column [SCI-10859]
This commit is contained in:
Martin Artnik 2024-08-05 10:34:34 +02:00 committed by GitHub
commit d470a3a5ac
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
4 changed files with 19 additions and 5 deletions

View file

@ -101,8 +101,9 @@ export default {
sortable: true
},
{
field: 'sub_locations',
field: 'sub_location_count',
headerName: this.i18n.t('storage_locations.index.table.sub_locations'),
width: 250,
sortable: true
},
{

View file

@ -14,8 +14,8 @@ class StorageLocation < ApplicationRecord
belongs_to :parent, class_name: 'StorageLocation', optional: true
belongs_to :created_by, class_name: 'User'
has_many :storage_location_repository_rows, inverse_of: :storage_location
has_many :storage_locations, foreign_key: :parent_id, dependent: :destroy, inverse_of: :parent
has_many :storage_location_repository_rows, inverse_of: :storage_location, dependent: :destroy
has_many :storage_locations, foreign_key: :parent_id, inverse_of: :parent, dependent: :destroy
has_many :repository_rows, through: :storage_location_repository_row
validates :name, length: { maximum: Constants::NAME_MAX_LENGTH }

View file

@ -5,7 +5,7 @@ module Lists
include Rails.application.routes.url_helpers
attributes :id, :code, :name, :container, :description, :owned_by, :created_by,
:created_on, :urls, :metadata, :file_name
:created_on, :urls, :metadata, :file_name, :sub_location_count
def owned_by
object.team.name
@ -30,6 +30,14 @@ module Lists
I18n.l(object.created_at, format: :full)
end
def sub_location_count
if object.respond_to?(:sub_location_count)
object.sub_location_count
else
StorageLocation.where(parent_id: object.id).count
end
end
def urls
show_url = if @object.container
storage_location_path(@object)

View file

@ -10,7 +10,12 @@ module Lists
end
def fetch_records
@records = @team.storage_locations
@records =
StorageLocation.joins('LEFT JOIN storage_locations AS sub_locations ' \
'ON storage_locations.id = sub_locations.parent_id')
.select('storage_locations.*, COUNT(sub_locations.id) AS sub_location_count')
.where(team: @team)
.group(:id)
end
def filter_records