diff --git a/app/assets/javascripts/sitewide/tiny_mce_file_upload_plugin.js.erb b/app/assets/javascripts/sitewide/tiny_mce_file_upload_plugin.js.erb
index 73a8b97d7..1e49e473c 100644
--- a/app/assets/javascripts/sitewide/tiny_mce_file_upload_plugin.js.erb
+++ b/app/assets/javascripts/sitewide/tiny_mce_file_upload_plugin.js.erb
@@ -39,8 +39,8 @@
});
// Let's make image selection looks fancy
$('
'
- +'
Choose file
'
- +'
'
+ +'
' + I18n.t('tiny_mce.choose_file') + '
'
+ +'
'
+'
').insertAfter('.mce-image-loader')
.click(() => {$('.mce-image-loader').click()})
.parent().css('height','32px')
diff --git a/app/controllers/steps_controller.rb b/app/controllers/steps_controller.rb
index 27f8d7c59..cba6023cb 100644
--- a/app/controllers/steps_controller.rb
+++ b/app/controllers/steps_controller.rb
@@ -31,7 +31,6 @@ class StepsController < ApplicationController
def create
@step = Step.new(step_params)
# gerate a tag that replaces img tag in database
- @step.description = @step.description
@step.completed = false
@step.position = @protocol.number_of_steps
@step.protocol = @protocol
diff --git a/app/models/concerns/tiny_mce_images.rb b/app/models/concerns/tiny_mce_images.rb
index 7ff88bd56..782f12378 100644
--- a/app/models/concerns/tiny_mce_images.rb
+++ b/app/models/concerns/tiny_mce_images.rb
@@ -10,24 +10,22 @@ module TinyMceImages
dependent: :destroy
def prepare_for_report(field)
- images = tiny_mce_assets
description = self[field]
- images.each do |image|
- tmp_f = Tempfile.open(image.image_file_name, Rails.root.join('tmp'))
+ tiny_mce_assets.each do |tm_asset|
+ tmp_f = Tempfile.open(tm_asset.image_file_name, Rails.root.join('tmp'))
begin
- image.image.copy_to_local_file(:large, tmp_f.path)
- encoded_image = Base64.strict_encode64(tmp_f.read)
- new_image = ""
+ tm_asset.image.copy_to_local_file(:large, tmp_f.path)
+ encoded_tm_asset = Base64.strict_encode64(tmp_f.read)
+ new_tm_asset = ""
html_description = Nokogiri::HTML(description)
- image_to_update = html_description.css("img[data-token=\"#{Base62.encode(image.id)}\"]")[0]
- image_to_update.replace new_image
+ tm_asset_to_update = html_description.css("img[data-token=\"#{Base62.encode(tm_asset.id)}\"]")[0]
+ tm_asset.replace new_tm_asset
description = html_description.css('body').inner_html.to_s
ensure
tmp_f.close
tmp_f.unlink
end
end
-
description
end
end
diff --git a/app/models/result_text.rb b/app/models/result_text.rb
index feaf506df..3d00547b1 100644
--- a/app/models/result_text.rb
+++ b/app/models/result_text.rb
@@ -1,11 +1,10 @@
class ResultText < ApplicationRecord
include TinyMceImages
-
+
auto_strip_attributes :text, nullify: false
validates :text,
presence: true,
length: { maximum: Constants::RICH_TEXT_MAX_LENGTH }
validates :result, presence: true
belongs_to :result, inverse_of: :result_text, touch: true, optional: true
- #has_many :tiny_mce_assets, inverse_of: :result_text, dependent: :destroy
end
diff --git a/app/models/tiny_mce_asset.rb b/app/models/tiny_mce_asset.rb
index 2a2d45059..f18c5d4ae 100644
--- a/app/models/tiny_mce_asset.rb
+++ b/app/models/tiny_mce_asset.rb
@@ -3,7 +3,7 @@
class TinyMceAsset < ApplicationRecord
attr_accessor :reference
before_create :set_reference, optional: true
- after_create :update_estimated_size, :self_destroyer
+ after_create :update_estimated_size, :self_destruct
after_destroy :release_team_space
after_save :update_description
@@ -14,7 +14,9 @@ class TinyMceAsset < ApplicationRecord
touch: true,
optional: true
- belongs_to :object, polymorphic: true, optional: true, inverse_of: :tiny_mce_assets
+ belongs_to :object, polymorphic: true,
+ optional: true,
+ inverse_of: :tiny_mce_assets
has_attached_file :image,
styles: { large: [Constants::LARGE_PIC_FORMAT, :jpg] },
convert_options: { large: '-quality 100 -strip' }
@@ -26,21 +28,24 @@ class TinyMceAsset < ApplicationRecord
validates_attachment :image,
presence: true,
size: {
- less_than: Rails.configuration.x.file_max_size_mb.megabytes
+ less_than: Rails.configuration.x\
+ .file_max_size_mb.megabytes
}
validates :estimated_size, presence: true
def self.update_images(object, images)
images = JSON.parse(images)
current_images = object.tiny_mce_assets.pluck(:id)
- images_to_delete = current_images.reject { |x| (images.include? Base62.encode(x)) }
+ images_to_delete = current_images.reject do |x|
+ (images.include? Base62.encode(x))
+ end
images.each do |image|
image_to_update = find_by_id(Base62.decode(image))
image_to_update&.update(object: object, saved: true)
end
where(id: images_to_delete).destroy_all
- rescue StandardError
- false
+ rescue StandardError => e
+ Rails.logger.error e.message
end
def self.reload_images(images = [])
@@ -54,7 +59,9 @@ class TinyMceAsset < ApplicationRecord
next unless image_to_update.object
old_description = Nokogiri::HTML(image_to_update.object[object_field])
- descirption_image = old_description.css("img[data-token=\"#{Base62.encode(old_id)}\"]")
+ descirption_image = old_description.css(
+ "img[data-token=\"#{Base62.encode(old_id)}\"]"
+ )
descirption_image.attr('src').value = image_to_update.url
descirption_image.attr('data-token').value = Base62.encode(new_id)
descirption_image[0]['class'] = 'img-responsive'
@@ -68,7 +75,7 @@ class TinyMceAsset < ApplicationRecord
timeout: Constants::URL_LONG_EXPIRE_TIME)
if stored_on_s3?
download_arg = if download
- 'attachment; filename=' + URI.escape(image_file_name)
+ 'attachment; filename=' + CGI.escape(image_file_name)
end
signer = Aws::S3::Presigner.new(client: S3_BUCKET.client)
@@ -100,8 +107,16 @@ class TinyMceAsset < ApplicationRecord
end
end
- def delete_unsaved_image
- destroy unless saved
+ def self.delete_unsaved_image(id)
+ asset = find_by_id(id)
+ asset.destroy if asset && !asset.saved
+ end
+
+ def self.data_fields
+ {
+ 'Step' => :description,
+ 'ResultText' => :text
+ }
end
private
@@ -110,15 +125,8 @@ class TinyMceAsset < ApplicationRecord
TinyMceAsset.reload_images([id]) if object
end
- def self_destroyer
- delay(queue: :assets, run_at: 1.days.from_now).delete_unsaved_image
- end
-
- def self.data_fields
- {
- 'Step' => :description,
- 'ResultText' => :text
- }
+ def self_destruct
+ delay(queue: :assets, run_at: 1.days.from_now).delete_unsaved_image(id)
end
def update_estimated_size
diff --git a/config/locales/en.yml b/config/locales/en.yml
index 165e541e0..0f325d615 100644
--- a/config/locales/en.yml
+++ b/config/locales/en.yml
@@ -1971,6 +1971,8 @@ en:
tiny_mce:
upload_window_title: 'Insert an image from your computer'
upload_window_label: 'Choose an image'
+ choose_file: 'Choose file'
+ no_image_chosen: 'No image chosen'
insert_btn: 'Insert'
error_message: 'You must choose a file'
server_not_respond: "Didn't get a response from the server"
diff --git a/db/migrate/20190307102521_add_column_saved_to_tiny_mce_asset.rb b/db/migrate/20190307102521_add_column_saved_to_tiny_mce_asset.rb
index 5a3781907..2f206c174 100644
--- a/db/migrate/20190307102521_add_column_saved_to_tiny_mce_asset.rb
+++ b/db/migrate/20190307102521_add_column_saved_to_tiny_mce_asset.rb
@@ -1,3 +1,5 @@
+# frozen_string_literal: true
+
class AddColumnSavedToTinyMceAsset < ActiveRecord::Migration[5.1]
def change
add_column :tiny_mce_assets, :saved, :boolean, default: true
diff --git a/db/migrate/20190308092130_add_polymorphic_to_tiny_mce_asset.rb b/db/migrate/20190308092130_add_polymorphic_to_tiny_mce_asset.rb
index 185c07b52..552702235 100644
--- a/db/migrate/20190308092130_add_polymorphic_to_tiny_mce_asset.rb
+++ b/db/migrate/20190308092130_add_polymorphic_to_tiny_mce_asset.rb
@@ -1,3 +1,5 @@
+# frozen_string_literal: true
+
class AddPolymorphicToTinyMceAsset < ActiveRecord::Migration[5.1]
def change
add_reference :tiny_mce_assets, :object, polymorphic: true
diff --git a/db/schema.rb b/db/schema.rb
index 6df1aa646..268233558 100644
--- a/db/schema.rb
+++ b/db/schema.rb
@@ -10,11 +10,7 @@
#
# It's strongly recommended that you check this file into your version control system.
-<<<<<<< HEAD
-ActiveRecord::Schema.define(version: 20190304153544) do
-=======
ActiveRecord::Schema.define(version: 20190308092130) do
->>>>>>> refactoring tinymce image uploading
# These are extensions that must be enabled in order to support this database
enable_extension "plpgsql"
@@ -23,19 +19,26 @@ ActiveRecord::Schema.define(version: 20190308092130) do
create_table "activities", force: :cascade do |t|
t.bigint "my_module_id"
- t.bigint "user_id"
+ t.bigint "owner_id"
t.integer "type_of", null: false
- t.string "message", null: false
+ t.string "message"
t.datetime "created_at", null: false
t.datetime "updated_at", null: false
- t.bigint "project_id", null: false
+ t.bigint "project_id"
t.bigint "experiment_id"
+ t.string "subject_type"
+ t.bigint "subject_id"
+ t.bigint "team_id"
+ t.integer "group_type"
+ t.json "values"
t.index ["created_at"], name: "index_activities_on_created_at"
t.index ["experiment_id"], name: "index_activities_on_experiment_id"
t.index ["my_module_id"], name: "index_activities_on_my_module_id"
+ t.index ["owner_id"], name: "index_activities_on_owner_id"
t.index ["project_id"], name: "index_activities_on_project_id"
+ t.index ["subject_type", "subject_id"], name: "index_activities_on_subject_type_and_subject_id"
+ t.index ["team_id"], name: "index_activities_on_team_id"
t.index ["type_of"], name: "index_activities_on_type_of"
- t.index ["user_id"], name: "index_activities_on_user_id"
end
create_table "asset_text_data", force: :cascade do |t|
@@ -927,7 +930,7 @@ ActiveRecord::Schema.define(version: 20190308092130) do
add_foreign_key "activities", "experiments"
add_foreign_key "activities", "my_modules"
add_foreign_key "activities", "projects"
- add_foreign_key "activities", "users"
+ add_foreign_key "activities", "users", column: "owner_id"
add_foreign_key "asset_text_data", "assets"
add_foreign_key "assets", "users", column: "created_by_id"
add_foreign_key "assets", "users", column: "last_modified_by_id"
diff --git a/spec/requests/api/v1/results_controller_spec.rb b/spec/requests/api/v1/results_controller_spec.rb
index ca5bb830b..e815c8080 100644
--- a/spec/requests/api/v1/results_controller_spec.rb
+++ b/spec/requests/api/v1/results_controller_spec.rb
@@ -128,7 +128,7 @@ RSpec.describe "Api::V1::ResultsController", type: :request do
attributes: {
text: 'Result text 1 '
+ 'QVQIHWP8//8/AwMDExADAQAkBgMBOOSShwAAAABJRU5ErkJggg==" data-token="a1">'
} },
{ type: 'tiny_mce_assets',
attributes: {