From 675b5782724fcfcc1bf58a5112fdd7c4361ef17e Mon Sep 17 00:00:00 2001 From: Oleksii Kriuchykhin Date: Tue, 17 Mar 2020 16:22:27 +0100 Subject: [PATCH] Add used space calculation method to the team model [SCI-4459] --- app/models/team.rb | 15 ++++++++++++++- spec/models/team_spec.rb | 1 - 2 files changed, 14 insertions(+), 2 deletions(-) diff --git a/app/models/team.rb b/app/models/team.rb index da0b661a4..5ea70ea18 100644 --- a/app/models/team.rb +++ b/app/models/team.rb @@ -19,7 +19,6 @@ class Team < ApplicationRecord length: { minimum: Constants::NAME_MIN_LENGTH, maximum: Constants::NAME_MAX_LENGTH } validates :description, length: { maximum: Constants::TEXT_MAX_LENGTH } - validates :space_taken, presence: true belongs_to :created_by, foreign_key: 'created_by_id', @@ -153,6 +152,20 @@ class Team < ApplicationRecord fields end + def storage_used + by_assets = Asset.joins(:file_blob) + .where(assets: { team_id: id }) + .select('active_storage_blobs.byte_size') + + by_tiny_mce_assets = TinyMceAsset.joins(:image_blob) + .where(tiny_mce_assets: { team_id: id }) + .select('active_storage_blobs.byte_size') + + ActiveStorage::Blob + .from("((#{by_assets.to_sql}) UNION ALL (#{by_tiny_mce_assets.to_sql})) AS active_storage_blobs") + .sum(:byte_size) + end + # (re)calculate the space taken by this team def calculate_space_taken st = 0 diff --git a/spec/models/team_spec.rb b/spec/models/team_spec.rb index 4f11485c2..c397e1718 100644 --- a/spec/models/team_spec.rb +++ b/spec/models/team_spec.rb @@ -44,7 +44,6 @@ describe Team, type: :model do end describe 'Validations' do - it { should validate_presence_of :space_taken } it do should validate_length_of(:name) .is_at_least(Constants::NAME_MIN_LENGTH)