Skip to content
Snippets Groups Projects
Unverified Commit 8459e60d authored by Sébastien Gaya's avatar Sébastien Gaya
Browse files

split methods

parent d4751d72
No related branches found
No related tags found
No related merge requests found
......@@ -49,7 +49,7 @@ class Communication::Website::Imported::Page < ApplicationRecord
optional: true
before_validation :sync
after_commit :sync_attachments, on: [:create, :update]
after_commit :sync_featured_image, on: [:create, :update]
default_scope { order(:path) }
......@@ -102,36 +102,45 @@ class Communication::Website::Imported::Page < ApplicationRecord
chapter.save
end
def sync_attachments
def sync_featured_image
return unless ENV['APPLICATION_ENV'] == 'development' || updated_at > page.updated_at
chapter = page.blocks.where(university: website.university, template_kind: :chapter).first_or_create
chapter_data = chapter.data.deep_dup
if featured_medium.present?
unless featured_medium.file.attached?
featured_medium.load_remote_file!
featured_medium.save
end
page.featured_image.attach(
io: URI.open(featured_medium.file.blob.url),
filename: featured_medium.file.blob.filename,
content_type: featured_medium.file.blob.content_type
)
sync_featured_image_from_featured_medium
else
fragment = Nokogiri::HTML.fragment(chapter_data['text'].to_s)
image = fragment.css('img').first
if image.present?
begin
url = image.attr('src')
download_service = DownloadService.download(url)
page.featured_image.attach(download_service.attachable_data)
image.remove
chapter_data['text'] = fragment.to_html
chapter.data = chapter_data
chapter.save
rescue
end
sync_featured_image_from_content
end
end
def sync_featured_image_from_featured_medium
unless featured_medium.file.attached?
featured_medium.load_remote_file!
featured_medium.save
end
page.featured_image.attach(
io: URI.open(featured_medium.file.blob.url),
filename: featured_medium.file.blob.filename,
content_type: featured_medium.file.blob.content_type
)
end
def sync_featured_image_from_content
chapter = page.blocks.where(university: website.university, template_kind: :chapter).first_or_create
chapter_data = chapter.data.deep_dup
fragment = Nokogiri::HTML.fragment(chapter_data['text'].to_s)
image = fragment.css('img').first
if image.present?
begin
url = image.attr('src')
download_service = DownloadService.download(url)
page.featured_image.attach(download_service.attachable_data)
image.remove
chapter_data['text'] = fragment.to_html
chapter.data = chapter_data
chapter.save
rescue
end
end
end
end
......@@ -49,7 +49,7 @@ class Communication::Website::Imported::Post < ApplicationRecord
optional: true
before_validation :sync
after_commit :sync_attachments, on: [:create, :update]
after_commit :sync_featured_image, on: [:create, :update]
default_scope { order(path: :desc) }
......@@ -114,36 +114,46 @@ class Communication::Website::Imported::Post < ApplicationRecord
chapter.save
end
def sync_attachments
def sync_featured_image
return unless ENV['APPLICATION_ENV'] == 'development' || updated_at > post.updated_at
chapter = post.blocks.where(university: website.university, template_kind: :chapter).first_or_create
chapter_data = chapter.data.deep_dup
if featured_medium.present?
unless featured_medium.file.attached?
featured_medium.load_remote_file!
featured_medium.save
end
post.featured_image.attach(
io: URI.open(featured_medium.file.blob.url),
filename: featured_medium.file.blob.filename,
content_type: featured_medium.file.blob.content_type
)
sync_featured_image_from_featured_medium
else
fragment = Nokogiri::HTML.fragment(chapter_data['text'].to_s)
image = fragment.css('img').first
if image.present?
begin
url = image.attr('src')
download_service = DownloadService.download(url)
post.featured_image.attach(download_service.attachable_data)
image.remove
chapter_data['text'] = fragment.to_html
chapter.data = chapter_data
chapter.save
rescue
end
sync_featured_image_from_content
end
end
def sync_featured_image_from_featured_medium
unless featured_medium.file.attached?
featured_medium.load_remote_file!
featured_medium.save
end
post.featured_image.attach(
io: URI.open(featured_medium.file.blob.url),
filename: featured_medium.file.blob.filename,
content_type: featured_medium.file.blob.content_type
)
end
def sync_featured_image_from_content
chapter = post.blocks.where(university: website.university, template_kind: :chapter).first_or_create
chapter_data = chapter.data.deep_dup
fragment = Nokogiri::HTML.fragment(chapter_data['text'].to_s)
image = fragment.css('img').first
# If content has an image, we extract it as the featured image
if image.present?
begin
url = image.attr('src')
download_service = DownloadService.download(url)
post.featured_image.attach(download_service.attachable_data)
image.remove
chapter_data['text'] = fragment.to_html
chapter.data = chapter_data
chapter.save
rescue
end
end
end
end
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment