Skip to content
Snippets Groups Projects
Commit a613a252 authored by Arnaud Levy's avatar Arnaud Levy
Browse files

Merge branch 'master' of github.com:noesya/osuny

parents c8385236 126521d3
No related branches found
No related tags found
No related merge requests found
...@@ -22,13 +22,13 @@ ...@@ -22,13 +22,13 @@
# #
class Communication::Website::Home < ApplicationRecord class Communication::Website::Home < ApplicationRecord
include WithGit include WithGit
include WithMedia include WithFeaturedImage
include WithBlobs
belongs_to :university belongs_to :university
belongs_to :website, foreign_key: :communication_website_id belongs_to :website, foreign_key: :communication_website_id
has_rich_text :text has_rich_text :text
has_one_attached_deletable :featured_image
def to_s def to_s
website.to_s website.to_s
...@@ -45,4 +45,14 @@ class Communication::Website::Home < ApplicationRecord ...@@ -45,4 +45,14 @@ class Communication::Website::Home < ApplicationRecord
def git_destroy_dependencies(website) def git_destroy_dependencies(website)
[self] + active_storage_blobs [self] + active_storage_blobs
end 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 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
...@@ -32,6 +32,9 @@ ...@@ -32,6 +32,9 @@
# fk_rails_... (website_id => communication_websites.id) # fk_rails_... (website_id => communication_websites.id)
# #
class Communication::Website::Menu::Item < ApplicationRecord class Communication::Website::Menu::Item < ApplicationRecord
KINDS_FOR_SCHOOL = ['programs', 'program', 'administrators', 'teachers']
KINDS_FOR_JOURNAL = ['researchers', 'research_volumes', 'research_volume', 'research_articles', 'research_article']
include WithTree include WithTree
include WithPosition include WithPosition
...@@ -72,6 +75,19 @@ class Communication::Website::Menu::Item < ApplicationRecord ...@@ -72,6 +75,19 @@ class Communication::Website::Menu::Item < ApplicationRecord
after_commit :sync_menu after_commit :sync_menu
def self.kinds_for_website(website)
whitelisted_kinds = self.kinds.dup
KINDS_FOR_SCHOOL.each { |school_kind|
whitelisted_kinds.delete(school_kind)
} unless website.about_school?
KINDS_FOR_JOURNAL.each { |journal_kind|
whitelisted_kinds.delete(journal_kind)
} unless website.about_journal?
whitelisted_kinds
end
def to_s def to_s
"#{title}" "#{title}"
end end
......
...@@ -39,14 +39,14 @@ ...@@ -39,14 +39,14 @@
class Communication::Website::Page < ApplicationRecord class Communication::Website::Page < ApplicationRecord
include WithGit include WithGit
include WithMedia include WithFeaturedImage
include WithBlobs
include WithMenuItemTarget include WithMenuItemTarget
include WithSlug # We override slug_unavailable? method include WithSlug # We override slug_unavailable? method
include WithTree include WithTree
include WithPosition include WithPosition
has_rich_text :text has_rich_text :text
has_one_attached_deletable :featured_image
belongs_to :university belongs_to :university
belongs_to :website, belongs_to :website,
...@@ -117,4 +117,12 @@ class Communication::Website::Page < ApplicationRecord ...@@ -117,4 +117,12 @@ class Communication::Website::Page < ApplicationRecord
.where.not(id: self.id) .where.not(id: self.id)
.exists? .exists?
end 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 end
...@@ -32,12 +32,12 @@ ...@@ -32,12 +32,12 @@
# #
class Communication::Website::Post < ApplicationRecord class Communication::Website::Post < ApplicationRecord
include WithGit include WithGit
include WithMedia include WithFeaturedImage
include WithBlobs
include WithMenuItemTarget include WithMenuItemTarget
include WithSlug # We override slug_unavailable? method include WithSlug # We override slug_unavailable? method
has_rich_text :text has_rich_text :text
has_one_attached_deletable :featured_image
has_one :imported_post, has_one :imported_post,
class_name: 'Communication::Website::Imported::Post', class_name: 'Communication::Website::Imported::Post',
...@@ -96,4 +96,12 @@ class Communication::Website::Post < ApplicationRecord ...@@ -96,4 +96,12 @@ class Communication::Website::Post < ApplicationRecord
def set_published_at def set_published_at
self.published_at = Time.zone.now if published? && published_at.nil? self.published_at = Time.zone.now if published? && published_at.nil?
end 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 end
module WithMedia module WithBlobs
extend ActiveSupport::Concern extend ActiveSupport::Concern
def active_storage_blobs def active_storage_blobs
...@@ -6,22 +6,21 @@ module WithMedia ...@@ -6,22 +6,21 @@ module WithMedia
end end
def explicit_active_storage_blobs def explicit_active_storage_blobs
blobs_with_ids [featured_image&.blob_id, rich_text_blob_ids] blobs_with_ids explicit_blob_ids
end end
def inherited_active_storage_blobs def inherited_active_storage_blobs
blobs_with_ids [best_featured_image] blobs_with_ids inherited_blob_ids
end
# Can be overwrite to get featured_image from associated objects (ex: parents)
def best_featured_image(fallback: true)
featured_image
end end
protected protected
def rich_text_reflection_names def explicit_blob_ids
@rich_text_reflection_names ||= _reflections.select { |name, reflection| reflection.class_name == "ActionText::RichText" }.keys [rich_text_blob_ids]
end
def inherited_blob_ids
[]
end end
def rich_text_blob_ids def rich_text_blob_ids
...@@ -34,4 +33,8 @@ module WithMedia ...@@ -34,4 +33,8 @@ module WithMedia
def blobs_with_ids(ids) def blobs_with_ids(ids)
university.active_storage_blobs.where(id: ids.flatten.compact) university.active_storage_blobs.where(id: ids.flatten.compact)
end end
def rich_text_reflection_names
@rich_text_reflection_names ||= _reflections.select { |name, reflection| reflection.class_name == "ActionText::RichText" }.keys
end
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 @@ ...@@ -31,7 +31,8 @@
# #
class Education::Program < ApplicationRecord class Education::Program < ApplicationRecord
include WithGit include WithGit
include WithMedia include WithFeaturedImage
include WithBlobs
include WithMenuItemTarget include WithMenuItemTarget
include WithSlug include WithSlug
include WithTree include WithTree
...@@ -54,8 +55,6 @@ class Education::Program < ApplicationRecord ...@@ -54,8 +55,6 @@ class Education::Program < ApplicationRecord
attr_accessor :skip_websites_categories_callback attr_accessor :skip_websites_categories_callback
has_one_attached_deletable :featured_image
belongs_to :university belongs_to :university
belongs_to :parent, belongs_to :parent,
class_name: 'Education::Program', class_name: 'Education::Program',
...@@ -150,4 +149,12 @@ class Education::Program < ApplicationRecord ...@@ -150,4 +149,12 @@ class Education::Program < ApplicationRecord
def last_ordered_element def last_ordered_element
university.education_programs.where(parent_id: parent_id).ordered.last university.education_programs.where(parent_id: parent_id).ordered.last
end 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 end
...@@ -35,7 +35,7 @@ ...@@ -35,7 +35,7 @@
# #
class Research::Journal::Article < ApplicationRecord class Research::Journal::Article < ApplicationRecord
include WithGit include WithGit
include WithMedia include WithBlobs
include WithPosition include WithPosition
has_rich_text :text has_rich_text :text
......
...@@ -28,9 +28,8 @@ ...@@ -28,9 +28,8 @@
# #
class Research::Journal::Volume < ApplicationRecord class Research::Journal::Volume < ApplicationRecord
include WithGit include WithGit
include WithMedia include WithFeaturedImage
include WithBlobs
has_one_attached_deletable :featured_image
belongs_to :university belongs_to :university
belongs_to :journal, foreign_key: :research_journal_id belongs_to :journal, foreign_key: :research_journal_id
...@@ -72,4 +71,12 @@ class Research::Journal::Volume < ApplicationRecord ...@@ -72,4 +71,12 @@ class Research::Journal::Volume < ApplicationRecord
def set_published_at def set_published_at
self.published_at = published? ? Time.zone.now : nil self.published_at = published? ? Time.zone.now : nil
end 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 end
...@@ -9,6 +9,7 @@ ...@@ -9,6 +9,7 @@
<div class="card-body"> <div class="card-body">
<%= f.input :title %> <%= f.input :title %>
<%= f.input :kind, <%= f.input :kind,
collection: Communication::Website::Menu::Item.kinds_for_website(@website),
include_blank: false, include_blank: false,
input_html: { input_html: {
class: 'js-kind-input', class: 'js-kind-input',
......
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