From 867995d561e59d6f4af1da81d583019d2e733147 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?S=C3=A9bastien=20Gaya?= <sebastien.gaya@gmail.com> Date: Wed, 19 Jan 2022 10:37:42 +0100 Subject: [PATCH] with featured image & with blobs --- app/models/communication/website/home.rb | 14 ++++++++-- .../website/imported/with_featured_image.rb | 28 ------------------- app/models/communication/website/page.rb | 12 ++++++-- app/models/communication/website/post.rb | 12 ++++++-- .../concerns/{with_media.rb => with_blobs.rb} | 23 ++++++++------- app/models/concerns/with_featured_image.rb | 12 ++++++++ app/models/education/program.rb | 13 +++++++-- app/models/research/journal/article.rb | 2 +- app/models/research/journal/volume.rb | 13 +++++++-- 9 files changed, 78 insertions(+), 51 deletions(-) delete mode 100644 app/models/communication/website/imported/with_featured_image.rb rename app/models/concerns/{with_media.rb => with_blobs.rb} (74%) create mode 100644 app/models/concerns/with_featured_image.rb diff --git a/app/models/communication/website/home.rb b/app/models/communication/website/home.rb index 2a8408d45..5c5f2890f 100644 --- a/app/models/communication/website/home.rb +++ b/app/models/communication/website/home.rb @@ -22,13 +22,13 @@ # class Communication::Website::Home < ApplicationRecord include WithGit - include WithMedia + include WithFeaturedImage + include WithBlobs belongs_to :university belongs_to :website, foreign_key: :communication_website_id has_rich_text :text - has_one_attached_deletable :featured_image def to_s website.to_s @@ -45,4 +45,14 @@ class Communication::Website::Home < ApplicationRecord def git_destroy_dependencies(website) [self] + active_storage_blobs end + + protected + + def explicit_blob_ids + [featured_image&.blob_id, rich_text_blob_ids] + end + + def inherited_blob_ids + [best_featured_image&.blob_id] + end end diff --git a/app/models/communication/website/imported/with_featured_image.rb b/app/models/communication/website/imported/with_featured_image.rb deleted file mode 100644 index 460d0d8b3..000000000 --- a/app/models/communication/website/imported/with_featured_image.rb +++ /dev/null @@ -1,28 +0,0 @@ -module Communication::Website::Imported::WithFeaturedImage - extend ActiveSupport::Concern - - protected - - def download_featured_medium_file_as_featured_image(object) - featured_medium.load_remote_file! unless featured_medium.file.attached? - object.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 download_first_image_in_text_as_featured_image(object) - fragment = Nokogiri::HTML.fragment(object.text.to_s) - image = fragment.css('img').first - return unless image.present? - begin - url = image.attr('src') - download_service = DownloadService.download(url) - object.featured_image.attach(download_service.attachable_data) - image.remove - object.update(text: fragment.to_html) - rescue - end - end -end diff --git a/app/models/communication/website/page.rb b/app/models/communication/website/page.rb index 11b61e888..b6024cf98 100644 --- a/app/models/communication/website/page.rb +++ b/app/models/communication/website/page.rb @@ -39,14 +39,14 @@ class Communication::Website::Page < ApplicationRecord include WithGit - include WithMedia + include WithFeaturedImage + include WithBlobs include WithMenuItemTarget include WithSlug # We override slug_unavailable? method include WithTree include WithPosition has_rich_text :text - has_one_attached_deletable :featured_image belongs_to :university belongs_to :website, @@ -117,4 +117,12 @@ class Communication::Website::Page < ApplicationRecord .where.not(id: self.id) .exists? end + + def explicit_blob_ids + [featured_image&.blob_id, rich_text_blob_ids] + end + + def inherited_blob_ids + [best_featured_image&.blob_id] + end end diff --git a/app/models/communication/website/post.rb b/app/models/communication/website/post.rb index 77f295375..82ca505ae 100644 --- a/app/models/communication/website/post.rb +++ b/app/models/communication/website/post.rb @@ -32,12 +32,12 @@ # class Communication::Website::Post < ApplicationRecord include WithGit - include WithMedia + include WithFeaturedImage + include WithBlobs include WithMenuItemTarget include WithSlug # We override slug_unavailable? method has_rich_text :text - has_one_attached_deletable :featured_image has_one :imported_post, class_name: 'Communication::Website::Imported::Post', @@ -96,4 +96,12 @@ class Communication::Website::Post < ApplicationRecord def set_published_at self.published_at = Time.zone.now if published? && published_at.nil? end + + def explicit_blob_ids + [featured_image&.blob_id, rich_text_blob_ids] + end + + def inherited_blob_ids + [best_featured_image&.blob_id] + end end diff --git a/app/models/concerns/with_media.rb b/app/models/concerns/with_blobs.rb similarity index 74% rename from app/models/concerns/with_media.rb rename to app/models/concerns/with_blobs.rb index 816bb071c..8a0cd6874 100644 --- a/app/models/concerns/with_media.rb +++ b/app/models/concerns/with_blobs.rb @@ -1,4 +1,4 @@ -module WithMedia +module WithBlobs extend ActiveSupport::Concern def active_storage_blobs @@ -6,22 +6,21 @@ module WithMedia end def explicit_active_storage_blobs - blobs_with_ids [featured_image&.blob_id, rich_text_blob_ids] + blobs_with_ids explicit_blob_ids end def inherited_active_storage_blobs - blobs_with_ids [best_featured_image] - end - - # Can be overwrite to get featured_image from associated objects (ex: parents) - def best_featured_image(fallback: true) - featured_image + blobs_with_ids inherited_blob_ids end protected - def rich_text_reflection_names - @rich_text_reflection_names ||= _reflections.select { |name, reflection| reflection.class_name == "ActionText::RichText" }.keys + def explicit_blob_ids + [rich_text_blob_ids] + end + + def inherited_blob_ids + [] end def rich_text_blob_ids @@ -34,4 +33,8 @@ module WithMedia def blobs_with_ids(ids) university.active_storage_blobs.where(id: ids.flatten.compact) end + + def rich_text_reflection_names + @rich_text_reflection_names ||= _reflections.select { |name, reflection| reflection.class_name == "ActionText::RichText" }.keys + end end diff --git a/app/models/concerns/with_featured_image.rb b/app/models/concerns/with_featured_image.rb new file mode 100644 index 000000000..c38424175 --- /dev/null +++ b/app/models/concerns/with_featured_image.rb @@ -0,0 +1,12 @@ +module WithFeaturedImage + extend ActiveSupport::Concern + + included do + has_one_attached_deletable :featured_image + end + + # Can be overwrite to get featured_image from associated objects (ex: parents) + def best_featured_image(fallback: true) + featured_image + end +end diff --git a/app/models/education/program.rb b/app/models/education/program.rb index 1116c3e01..e8f3edf30 100644 --- a/app/models/education/program.rb +++ b/app/models/education/program.rb @@ -31,7 +31,8 @@ # class Education::Program < ApplicationRecord include WithGit - include WithMedia + include WithFeaturedImage + include WithBlobs include WithMenuItemTarget include WithSlug include WithTree @@ -54,8 +55,6 @@ class Education::Program < ApplicationRecord attr_accessor :skip_websites_categories_callback - has_one_attached_deletable :featured_image - belongs_to :university belongs_to :parent, class_name: 'Education::Program', @@ -150,4 +149,12 @@ class Education::Program < ApplicationRecord def last_ordered_element university.education_programs.where(parent_id: parent_id).ordered.last end + + def explicit_blob_ids + [featured_image&.blob_id, rich_text_blob_ids] + end + + def inherited_blob_ids + [best_featured_image&.blob_id] + end end diff --git a/app/models/research/journal/article.rb b/app/models/research/journal/article.rb index b7d7c5780..f4df5a574 100644 --- a/app/models/research/journal/article.rb +++ b/app/models/research/journal/article.rb @@ -35,7 +35,7 @@ # class Research::Journal::Article < ApplicationRecord include WithGit - include WithMedia + include WithBlobs include WithPosition has_rich_text :text diff --git a/app/models/research/journal/volume.rb b/app/models/research/journal/volume.rb index c80b3af6b..8ee063285 100644 --- a/app/models/research/journal/volume.rb +++ b/app/models/research/journal/volume.rb @@ -28,9 +28,8 @@ # class Research::Journal::Volume < ApplicationRecord include WithGit - include WithMedia - - has_one_attached_deletable :featured_image + include WithFeaturedImage + include WithBlobs belongs_to :university belongs_to :journal, foreign_key: :research_journal_id @@ -72,4 +71,12 @@ class Research::Journal::Volume < ApplicationRecord def set_published_at self.published_at = published? ? Time.zone.now : nil end + + def explicit_blob_ids + [featured_image&.blob_id, rich_text_blob_ids] + end + + def inherited_blob_ids + [best_featured_image&.blob_id] + end end -- GitLab