From 8459e60d3d0229f704f6f4deb351f248f8c4280f Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?S=C3=A9bastien=20Gaya?= <sebastien.gaya@gmail.com> Date: Mon, 13 Feb 2023 15:54:30 +0100 Subject: [PATCH] split methods --- .../communication/website/imported/page.rb | 61 ++++++++++-------- .../communication/website/imported/post.rb | 62 +++++++++++-------- 2 files changed, 71 insertions(+), 52 deletions(-) diff --git a/app/models/communication/website/imported/page.rb b/app/models/communication/website/imported/page.rb index a60d45c9a..72565fbd8 100644 --- a/app/models/communication/website/imported/page.rb +++ b/app/models/communication/website/imported/page.rb @@ -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 diff --git a/app/models/communication/website/imported/post.rb b/app/models/communication/website/imported/post.rb index d2750d98d..2316a0783 100644 --- a/app/models/communication/website/imported/post.rb +++ b/app/models/communication/website/imported/post.rb @@ -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 -- GitLab