diff --git a/app/assets/javascripts/protocols/steps.js.erb b/app/assets/javascripts/protocols/steps.js.erb index f32927663..e86003071 100644 --- a/app/assets/javascripts/protocols/steps.js.erb +++ b/app/assets/javascripts/protocols/steps.js.erb @@ -517,12 +517,15 @@ $("[data-action='new-step']").on("ajax:success", function(e, data) { applyCancelOnNew(); toggleButtons(false); initializeCheckboxSorting(); - TinyMCE.init(); + $("#step_name").focus(); $("#new-step-main-tab a").on("shown.bs.tab", function() { $("#step_name").focus(); }); + + TinyMCE.refresh(); + }); // Needed because server-side validation failure clears locations of diff --git a/app/controllers/result_texts_controller.rb b/app/controllers/result_texts_controller.rb index 7fe013435..ccc8ffb3c 100644 --- a/app/controllers/result_texts_controller.rb +++ b/app/controllers/result_texts_controller.rb @@ -2,6 +2,7 @@ class ResultTextsController < ApplicationController include ResultsHelper include ActionView::Helpers::UrlHelper include ApplicationHelper + include TinyMceHelper include Rails.application.routes.url_helpers before_action :load_vars, only: [:edit, :update, :download] @@ -31,6 +32,7 @@ class ResultTextsController < ApplicationController def create @result_text = ResultText.new(result_params[:result_text_attributes]) + @result_text.text = parse_tiny_mce_asset_to_token(@result_text.text) @result = Result.new( user: current_user, my_module: @my_module, @@ -41,6 +43,8 @@ class ResultTextsController < ApplicationController respond_to do |format| if (@result.save and @result_text.save) then + #link tiny_mce_assets to the result text + link_tiny_mce_assets(@result_text.text, @result_text) result_annotation_notification # Generate activity @@ -81,6 +85,7 @@ class ResultTextsController < ApplicationController end def edit + @result_text.text = generate_image_tag_from_token(@result_text.text) respond_to do |format| format.json { render json: { @@ -97,6 +102,8 @@ class ResultTextsController < ApplicationController update_params = result_params @result.last_modified_by = current_user @result.assign_attributes(update_params) + @result_text.text = parse_tiny_mce_asset_to_token(@result_text.text, + @result_text) success_flash = t("result_texts.update.success_flash", module: @my_module.name) if @result.archived_changed?(from: false, to: true) diff --git a/app/controllers/steps_controller.rb b/app/controllers/steps_controller.rb index d59cd4027..e20e64d63 100644 --- a/app/controllers/steps_controller.rb +++ b/app/controllers/steps_controller.rb @@ -60,7 +60,11 @@ class StepsController < ApplicationController asset.post_process_file(@protocol.team) end + #link tiny_mce_assets to the step + link_tiny_mce_assets(@step.description, @step) + create_annotation_notifications(@step) + # Generate activity if @protocol.in_module? Activity.create( diff --git a/app/controllers/tiny_mce_assets_controller.rb b/app/controllers/tiny_mce_assets_controller.rb index 851b3a45c..4c33f5ade 100644 --- a/app/controllers/tiny_mce_assets_controller.rb +++ b/app/controllers/tiny_mce_assets_controller.rb @@ -3,7 +3,10 @@ class TinyMceAssetsController < ApplicationController def create image = params.fetch(:file) { render_404 } - tiny_img = TinyMceAsset.new(image: image, reference: @obj, editing: true) + tiny_img = TinyMceAsset.new(image: image, + reference: @obj, + editing: true, + team_id: current_team.id) if tiny_img.save render json: { image: { diff --git a/app/helpers/tiny_mce_helper.rb b/app/helpers/tiny_mce_helper.rb index 8bde70b58..bb5a26b9a 100644 --- a/app/helpers/tiny_mce_helper.rb +++ b/app/helpers/tiny_mce_helper.rb @@ -7,10 +7,10 @@ module TinyMceHelper img_id = Base62.decode(img['data-token']) token = "[~tiny_mce_id:#{img_id}]" img.replace(token) - byebug next unless ref tiny_img = TinyMceAsset.find_by_id(img_id) tiny_img.reference = ref unless tiny_img.step || tiny_img.result_text + tiny_img.editing = false tiny_img.save end html @@ -21,7 +21,20 @@ module TinyMceHelper new_text = text.gsub(regex) do |el| match = el.match(regex) img = TinyMceAsset.find_by_id(match[1]) + next unless img image_tag img.url, data: { token: Base62.encode(img.id) } end end + + def link_tiny_mce_assets(text, ref) + regex = /\[~tiny_mce_id:([0-9a-zA-Z]+)\]/ + text.gsub(regex) do |img| + match = img.match(regex) + tiny_img = TinyMceAsset.find_by_id(match[1]) + next unless tiny_img + tiny_img.public_send("#{ref.class.to_s.underscore}=".to_sym, ref) + tiny_img.editing = false + tiny_img.save! + end + end end diff --git a/app/models/result_text.rb b/app/models/result_text.rb index fcc8597f6..542ba2c34 100644 --- a/app/models/result_text.rb +++ b/app/models/result_text.rb @@ -5,5 +5,5 @@ class ResultText < ActiveRecord::Base length: { maximum: Constants::RICH_TEXT_MAX_LENGTH } validates :result, presence: true belongs_to :result, inverse_of: :result_text - has_many :tiny_mce_assets, inverse_of: :result_text + has_many :tiny_mce_assets, inverse_of: :result_text, dependent: :destroy end diff --git a/app/models/step.rb b/app/models/step.rb index 5624b8755..b28adb1f7 100644 --- a/app/models/step.rb +++ b/app/models/step.rb @@ -25,7 +25,8 @@ class Step < ActiveRecord::Base has_many :tables, through: :step_tables has_many :report_elements, inverse_of: :step, dependent: :destroy - + has_many :tiny_mce_assets, inverse_of: :step, dependent: :destroy + accepts_nested_attributes_for :checklists, reject_if: :all_blank, allow_destroy: true diff --git a/app/models/team.rb b/app/models/team.rb index d635679f7..9102a6da8 100644 --- a/app/models/team.rb +++ b/app/models/team.rb @@ -268,10 +268,13 @@ class Team < ActiveRecord::Base project.project_my_modules.find_each do |my_module| my_module.protocol.steps.find_each do |step| step.assets.find_each { |asset| st += asset.estimated_size } + step.tiny_mce_assets.find_each { |tiny| st += tiny.estimated_size } end my_module.results.find_each do |result| - if result.is_asset then - st += result.asset.estimated_size + st += result.asset.estimated_size if result.is_asset + if result.is_text + tiny_assets = TinyMceAsset.where(result_text: result.result_text) + tiny_assets.find_each { |tiny| st += tiny.estimated_size } end end end diff --git a/app/models/tiny_mce_asset.rb b/app/models/tiny_mce_asset.rb index d65446628..7c1ed348b 100644 --- a/app/models/tiny_mce_asset.rb +++ b/app/models/tiny_mce_asset.rb @@ -1,9 +1,11 @@ class TinyMceAsset < ActiveRecord::Base attr_accessor :reference before_create :set_reference + after_create :update_estimated_size - belongs_to :step - belongs_to :result_text + belongs_to :step, inverse_of: :tiny_mce_assets + belongs_to :result_text, inverse_of: :tiny_mce_assets + belongs_to :team has_attached_file :image, styles: { medium: [Constants::MEDIUM_PIC_FORMAT, :jpg] }, convert_options: { medium: '-quality 70 -strip' } @@ -12,6 +14,8 @@ class TinyMceAsset < ActiveRecord::Base content_type: %r{^image/#{ Regexp.union( Constants::WHITELISTED_IMAGE_TYPES ) }} + validates :estimated_size, presence: true + # When using S3 file upload, we can limit file accessibility with url signing def presigned_url(style = :medium, download: false, @@ -44,6 +48,17 @@ class TinyMceAsset < ActiveRecord::Base end end + private + + # If team is provided, its space_taken + # is updated as well + def update_estimated_size + return if image_file_size.blank? + es = image_file_size * Constants::ASSET_ESTIMATED_SIZE_FACTOR + update(estimated_size: es) + Rails.logger.info "Asset #{id}: Estimated size successfully calculated" + end + def set_reference obj_type = "#{@reference.class.to_s.underscore}=".to_sym self.public_send(obj_type, @reference) if @reference diff --git a/app/views/result_texts/_edit.html.erb b/app/views/result_texts/_edit.html.erb index 01585ec85..10acf25ce 100644 --- a/app/views/result_texts/_edit.html.erb +++ b/app/views/result_texts/_edit.html.erb @@ -3,7 +3,10 @@ <%= f.text_field :name, style: "margin-top: 10px;" %>
<%= f.fields_for :result_text do |ff| %>
- <%= ff.tiny_mce_editor(:text, value: @result.result_text.text) %> + <%= ff.tiny_mce_editor(:text, + value: @result.result_text.text, + data: { object_type: 'result_text', + object_id: @result.result_text.id }) %>
<% end %>
<%= f.submit t("result_texts.edit.update"), class: 'btn btn-primary save-result', onclick: "processResult(event, ResultTypeEnum.TEXT, true);" %> diff --git a/app/views/result_texts/_new.html.erb b/app/views/result_texts/_new.html.erb index 5ea696def..fc340637c 100644 --- a/app/views/result_texts/_new.html.erb +++ b/app/views/result_texts/_new.html.erb @@ -3,7 +3,8 @@ <%= f.text_field :name, style: "margin-top: 10px;" %>
<%= f.fields_for :result_text do |ff| %>
- <%= ff.tiny_mce_editor(:text) %> + <%= ff.tiny_mce_editor(:text, data: { object_type: 'result_text', + object_id: @result.result_text.id }) %>
<% end %>
<%= f.submit t("result_texts.new.create"), class: 'btn btn-primary save-result', onclick: "processResult(event, ResultTypeEnum.TEXT, false);" %> diff --git a/app/views/results/_result_text.html.erb b/app/views/results/_result_text.html.erb index 6b11b1a47..82359ba68 100644 --- a/app/views/results/_result_text.html.erb +++ b/app/views/results/_result_text.html.erb @@ -1,3 +1,5 @@
- <%= custom_auto_link(result.result_text.text, simple_format: false) %> + <%= custom_auto_link(generate_image_tag_from_token(result.result_text.text), + simple_format: false, + tags: %w(img)) %>
diff --git a/app/views/steps/_step.html.erb b/app/views/steps/_step.html.erb index d6e32e50b..ce106d211 100644 --- a/app/views/steps/_step.html.erb +++ b/app/views/steps/_step.html.erb @@ -56,7 +56,7 @@
<%= custom_auto_link(generate_image_tag_from_token(step.description), simple_format: false, - tags: ['img']) %> + tags: %w(img)) %>
<% end %> 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 1d5538df1..21cc7cb92 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 @@ -3,6 +3,8 @@ class AddAttachmentImageToTinyMceAssets < ActiveRecord::Migration create_table :tiny_mce_assets do |t| t.attachment :image t.boolean :editing, default: false + t.integer :estimated_size, default: 0, null: false + t.references :team, index: true t.references :step, index: true t.references :result_text, index: true t.timestamps null: false diff --git a/db/schema.rb b/db/schema.rb index dac8bfd4d..6b89e0b1d 100644 --- a/db/schema.rb +++ b/db/schema.rb @@ -566,6 +566,8 @@ ActiveRecord::Schema.define(version: 20170420075905) do t.integer "image_file_size" t.datetime "image_updated_at" t.boolean "editing", default: false + t.integer "estimated_size", default: 0, null: false + t.integer "team_id" t.integer "step_id" t.integer "result_text_id" t.datetime "created_at", null: false @@ -574,6 +576,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