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

with featured image & with blobs

parent 3ee1b6c8
No related branches found
No related tags found
No related merge requests found
......@@ -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
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
......@@ -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
......@@ -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
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
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
......@@ -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
......@@ -35,7 +35,7 @@
#
class Research::Journal::Article < ApplicationRecord
include WithGit
include WithMedia
include WithBlobs
include WithPosition
has_rich_text :text
......
......@@ -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
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