Merge pull request #2475 from okriuchykhin/ok_SCI_4459

Add used space calculation method to the team model [SCI-4459]
This commit is contained in:
Alex Kriuchykhin 2020-03-17 16:58:37 +01:00 committed by GitHub
commit 749c1dda69
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
2 changed files with 14 additions and 2 deletions

View file

@ -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

View file

@ -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)