From 1fefeee3d1d38e85e43e21b86e499f75cc8e1082 Mon Sep 17 00:00:00 2001 From: zmagod Date: Tue, 25 Apr 2017 13:44:31 +0200 Subject: [PATCH] adds team space related methods --- app/controllers/tiny_mce_assets_controller.rb | 4 +++- app/helpers/application_helper.rb | 2 +- app/models/tiny_mce_asset.rb | 11 +++++++++++ ...0075905_add_attachment_image_to_tiny_mce_assets.rb | 1 + 4 files changed, 16 insertions(+), 2 deletions(-) diff --git a/app/controllers/tiny_mce_assets_controller.rb b/app/controllers/tiny_mce_assets_controller.rb index d604f356d..85783f510 100644 --- a/app/controllers/tiny_mce_assets_controller.rb +++ b/app/controllers/tiny_mce_assets_controller.rb @@ -3,7 +3,9 @@ class TinyMceAssetsController < ApplicationController def create image = params.fetch(:file) { render_404 } - tiny_img = TinyMceAsset.new(image: image, reference: @obj) + tiny_img = TinyMceAsset.new(image: image, + reference: @obj, + team_id: current_team.id) if tiny_img.save render json: { image: { diff --git a/app/helpers/application_helper.rb b/app/helpers/application_helper.rb index bc0fbcc40..3ad20a8da 100644 --- a/app/helpers/application_helper.rb +++ b/app/helpers/application_helper.rb @@ -103,7 +103,7 @@ module ApplicationHelper end def smart_annotation_parser(text, team = nil) - team = nil unless team.kind_of? Team + team = nil unless team.is_a? Team new_text = smart_annotation_filter_resources(text) new_text = smart_annotation_filter_users(new_text, team) new_text diff --git a/app/models/tiny_mce_asset.rb b/app/models/tiny_mce_asset.rb index 15a63a791..f81549544 100644 --- a/app/models/tiny_mce_asset.rb +++ b/app/models/tiny_mce_asset.rb @@ -2,6 +2,7 @@ class TinyMceAsset < ActiveRecord::Base attr_accessor :reference before_create :set_reference after_create :update_estimated_size + after_destroy :release_team_space belongs_to :step, inverse_of: :tiny_mce_assets belongs_to :result_text, inverse_of: :tiny_mce_assets @@ -56,9 +57,19 @@ class TinyMceAsset < ActiveRecord::Base def update_estimated_size return if image_file_size.blank? + team = Team.find_by_id(team_id) es = image_file_size * Constants::ASSET_ESTIMATED_SIZE_FACTOR update(estimated_size: es) Rails.logger.info "Asset #{id}: Estimated size successfully calculated" + # update team space taken + team.take_space(es) + team.save + end + + def release_team_space + team = Team.find_by_id(team_id) + team.release_space(estimated_size) + team.save end def set_reference diff --git a/db/migrate/20170420075905_add_attachment_image_to_tiny_mce_assets.rb b/db/migrate/20170420075905_add_attachment_image_to_tiny_mce_assets.rb index 58c960d98..07eba08b3 100644 --- a/db/migrate/20170420075905_add_attachment_image_to_tiny_mce_assets.rb +++ b/db/migrate/20170420075905_add_attachment_image_to_tiny_mce_assets.rb @@ -4,6 +4,7 @@ class AddAttachmentImageToTinyMceAssets < ActiveRecord::Migration t.attachment :image t.integer :estimated_size, default: 0, null: false t.references :step, index: true + t.references :team, index: true t.references :result_text, index: true t.timestamps null: false end