diff --git a/app/javascript/vue/storage_locations/table.vue b/app/javascript/vue/storage_locations/table.vue index 5155e1d92..4cf760b67 100644 --- a/app/javascript/vue/storage_locations/table.vue +++ b/app/javascript/vue/storage_locations/table.vue @@ -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 }, { diff --git a/app/models/storage_location.rb b/app/models/storage_location.rb index f4070ce97..a88cefe95 100644 --- a/app/models/storage_location.rb +++ b/app/models/storage_location.rb @@ -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 } diff --git a/app/serializers/lists/storage_location_serializer.rb b/app/serializers/lists/storage_location_serializer.rb index e29d31a02..10c04c9ee 100644 --- a/app/serializers/lists/storage_location_serializer.rb +++ b/app/serializers/lists/storage_location_serializer.rb @@ -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) diff --git a/app/services/lists/storage_locations_service.rb b/app/services/lists/storage_locations_service.rb index 664688f32..c1e02bc9a 100644 --- a/app/services/lists/storage_locations_service.rb +++ b/app/services/lists/storage_locations_service.rb @@ -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