Add delay job for handling unknown tinyMCE images (#1781)

* Add delay job for handling unknown tinyMCE images
This commit is contained in:
aignatov-bio 2019-05-20 12:44:16 +02:00 committed by GitHub
parent d959769b65
commit b22dba7f05
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
3 changed files with 49 additions and 1 deletions

View file

@ -79,6 +79,41 @@ module TinyMceImages
target.reassign_tiny_mce_image_references(cloned_img_ids)
end
def copy_unknown_tiny_mce_images
asset_team_id = Team.find_by_object(self)
return unless asset_team_id
object_field = Extends::RICH_TEXT_FIELD_MAPPINGS[self.class.name]
image_changed = false
parsed_description = Nokogiri::HTML(read_attribute(object_field))
parsed_description.css('img').each do |image|
if image['data-mce-token']
asset = TinyMceAsset.find_by_id(Base62.decode(image['data-mce-token']))
next if asset && asset.object == self
new_image = asset.image
else
new_image = URI.parse(image['src'])
end
new_asset = TinyMceAsset.create(
image: new_image,
object: self,
team_id: asset_team_id
)
image['src'] = ''
image['class'] = 'img-responsive'
image['data-mce-token'] = Base62.encode(new_asset.id)
image_changed = true
end
update(object_field => parsed_description.css('body').inner_html.to_s) if image_changed
rescue StandardError => e
Rails.logger.error "Object: #{self.class.name}, id: #{id}, error: #{e.message}"
end
private
def clean_tiny_mce_image_urls

View file

@ -324,6 +324,17 @@ class Team < ApplicationRecord
query.select(:id, :name).map { |i| { id: i[:id], name: ApplicationController.helpers.escape_input(i[:name]) } }
end
def self.find_by_object(obj)
case obj.class.name
when 'Protocol'
obj.team_id
when 'MyModule', 'Step'
obj.protocol.team_id
when 'ResultText'
obj.result.my_module.protocol.team_id
end
end
private
def generate_template_project

View file

@ -41,9 +41,11 @@ class TinyMceAsset < ApplicationRecord
end
images.each do |image|
image_to_update = find_by_id(Base62.decode(image))
image_to_update&.update(object: object, saved: true)
image_to_update&.update(object: object, saved: true) unless image_to_update.object
end
where(id: images_to_delete).destroy_all
object.delay(queue: :assets).copy_unknown_tiny_mce_images
rescue StandardError => e
Rails.logger.error e.message
end