markup fixes

This commit is contained in:
Anton Ignatov 2019-03-20 14:40:18 +01:00
parent 07c2819163
commit 55c93922ea
10 changed files with 56 additions and 43 deletions

View file

@ -39,8 +39,8 @@
});
// Let's make image selection looks fancy
$('<div class="image-selection-container">'
+'<div class="select_button btn btn-primary">Choose file</div>'
+'<input type="text" placeholder="No file chosen" disabled></input>'
+'<div class="select_button btn btn-primary">' + I18n.t('tiny_mce.choose_file') + '</div>'
+'<input type="text" placeholder="' + I18n.t('tiny_mce.no_image_chosen') + '" disabled></input>'
+'</div>').insertAfter('.mce-image-loader')
.click(() => {$('.mce-image-loader').click()})
.parent().css('height','32px')

View file

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

View file

@ -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 = "<img class='img-responsive' src='data:image/jpg;base64,#{encoded_image}'>"
tm_asset.image.copy_to_local_file(:large, tmp_f.path)
encoded_tm_asset = Base64.strict_encode64(tmp_f.read)
new_tm_asset = "<img class='img-responsive' src='data:image/jpg;base64,#{encoded_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

View file

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

View file

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

View file

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

View file

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

View file

@ -1,3 +1,5 @@
# frozen_string_literal: true
class AddPolymorphicToTinyMceAsset < ActiveRecord::Migration[5.1]
def change
add_reference :tiny_mce_assets, :object, polymorphic: true

View file

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

View file

@ -128,7 +128,7 @@ RSpec.describe "Api::V1::ResultsController", type: :request do
attributes: {
text: 'Result text 1 <img src="data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAAAIAA'\
'AACCAIAAAD91JpzAAAACXBIWXMAAC4jAAAuIwF4pT92AAAAE0lE'\
'QVQIHWP8//8/AwMDExADAQAkBgMBOOSShwAAAABJRU5ErkJggg==" data-token="a1">'
'QVQIHWP8//8/AwMDExADAQAkBgMBOOSShwAAAABJRU5ErkJggg==" data-token="a1">'
} },
{ type: 'tiny_mce_assets',
attributes: {