diff --git a/app/controllers/steps_controller.rb b/app/controllers/steps_controller.rb index 78b3f103b..a23ea533d 100644 --- a/app/controllers/steps_controller.rb +++ b/app/controllers/steps_controller.rb @@ -164,10 +164,12 @@ class StepsController < ApplicationController table.last_modified_by = current_user unless table.new_record? table.team = current_team end - - # gerate a tag that replaces img tag in database - @step.description = parse_tiny_mce_asset_to_token(@step.description, - @step) + + # gerate a tag that replaces img tag in databases + @step.description = parse_tiny_mce_asset_to_token( + params[:step][:description], + @step + ) if @step.save @step.reload diff --git a/app/models/team.rb b/app/models/team.rb index 9102a6da8..9148fb898 100644 --- a/app/models/team.rb +++ b/app/models/team.rb @@ -24,7 +24,7 @@ class Team < ActiveRecord::Base has_many :custom_fields, inverse_of: :team has_many :protocols, inverse_of: :team, dependent: :destroy has_many :protocol_keywords, inverse_of: :team, dependent: :destroy - + has_many :tiny_mce_assets, inverse_of: :team, dependent: :destroy # Based on file's extension opens file (used for importing) def self.open_spreadsheet(file) filename = file.original_filename diff --git a/app/models/tiny_mce_asset.rb b/app/models/tiny_mce_asset.rb index f81549544..6898c1ea5 100644 --- a/app/models/tiny_mce_asset.rb +++ b/app/models/tiny_mce_asset.rb @@ -4,6 +4,7 @@ class TinyMceAsset < ActiveRecord::Base after_create :update_estimated_size after_destroy :release_team_space + belongs_to :team, inverse_of: :tiny_mce_assets belongs_to :step, inverse_of: :tiny_mce_assets belongs_to :result_text, inverse_of: :tiny_mce_assets has_attached_file :image, @@ -57,19 +58,17 @@ 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 + self.team.take_space(es) + self.team.save end def release_team_space - team = Team.find_by_id(team_id) - team.release_space(estimated_size) - team.save + self.team.release_space(estimated_size) + self.team.save end def set_reference diff --git a/db/schema.rb b/db/schema.rb index 2f27f8a66..98b5bc403 100644 --- a/db/schema.rb +++ b/db/schema.rb @@ -567,6 +567,7 @@ ActiveRecord::Schema.define(version: 20170420075905) do t.datetime "image_updated_at" t.integer "estimated_size", default: 0, null: false t.integer "step_id" + t.integer "team_id" t.integer "result_text_id" t.datetime "created_at", null: false t.datetime "updated_at", null: false @@ -574,6 +575,7 @@ ActiveRecord::Schema.define(version: 20170420075905) do add_index "tiny_mce_assets", ["result_text_id"], name: "index_tiny_mce_assets_on_result_text_id", using: :btree add_index "tiny_mce_assets", ["step_id"], name: "index_tiny_mce_assets_on_step_id", using: :btree + add_index "tiny_mce_assets", ["team_id"], name: "index_tiny_mce_assets_on_team_id", using: :btree create_table "tokens", force: :cascade do |t| t.string "token", null: false diff --git a/lib/tasks/tiny_mce_asset.rake b/lib/tasks/tiny_mce_asset.rake new file mode 100644 index 000000000..119b4f8d2 --- /dev/null +++ b/lib/tasks/tiny_mce_asset.rake @@ -0,0 +1,10 @@ +namespace :tiny_mce_asset do + desc 'Remove obsolete images that were created on new steps or '\ + 'results and the step/result didn\'t get saved.' + task remote_obsolete_images: :environment do + TinyMceAsset.where('step_id IS ? AND ' \ + 'result_text_id IS ? AND created_at < ?', + nil, nil, 7.days.ago) + end + +end