diff --git a/app/models/communication/website/category.rb b/app/models/communication/website/category.rb index a22fb6efc8c72290b35d095a6b430e1d67659e21..ba31fe5647d9d23b7538539e105754248c51f586 100644 --- a/app/models/communication/website/category.rb +++ b/app/models/communication/website/category.rb @@ -111,6 +111,13 @@ class Communication::Website::Category < ApplicationRecord (ancestors.map(&:slug) << slug).join('-') end + def best_featured_image_source(fallback: true) + return self if featured_image.attached? + best_source = parent&.best_featured_image_source(fallback: false) + best_source ||= self if fallback + best_source + end + protected def last_ordered_element diff --git a/app/models/communication/website/page.rb b/app/models/communication/website/page.rb index d16d0e281d30a9e811a9909aef06c389bdabc82f..01499c8da5d93e38fb7fd87600ed886ba953d0e4 100644 --- a/app/models/communication/website/page.rb +++ b/app/models/communication/website/page.rb @@ -116,10 +116,10 @@ class Communication::Website::Page < ApplicationRecord "#{title}" end - def best_featured_image + def best_featured_image_source(fallback: true) # we don't want to fallback on homepage featured_image - return featured_image if featured_image.attached? || is_home? || parent&.is_home? - parent&.best_featured_image + return self if featured_image.attached? || is_home? || parent&.is_home? + parent&.best_featured_image_source end def best_bodyclass diff --git a/app/models/concerns/with_featured_image.rb b/app/models/concerns/with_featured_image.rb index 5def3f002ddd93a2f638bb0ea74aa7c79e16cbf6..4543ce6fc1c2b948ba663ed3168e9a9551508f9b 100644 --- a/app/models/concerns/with_featured_image.rb +++ b/app/models/concerns/with_featured_image.rb @@ -8,8 +8,20 @@ module WithFeaturedImage end # Can be overwrite to get featured_image from associated objects (ex: parents) - def best_featured_image(fallback: true) - featured_image + def best_featured_image_source(fallback: true) + self + end + + def best_featured_image + best_featured_image_source.featured_image + end + + def best_featured_image_alt + best_featured_image_source.featured_image_alt + end + + def best_featured_image_credit + best_featured_image_source.featured_image_credit end def add_unsplash_image(id) diff --git a/app/models/education/program.rb b/app/models/education/program.rb index 99dcc822cce1aaf6d2638917dfb1057ae3b3b4fa..9eec8676b0ec5d81319b22b27f3d8ec226a917fa 100644 --- a/app/models/education/program.rb +++ b/app/models/education/program.rb @@ -126,11 +126,11 @@ class Education::Program < ApplicationRecord "#{name}" end - def best_featured_image(fallback: true) - return featured_image if featured_image.attached? - best_image = parent&.best_featured_image(fallback: false) - best_image ||= featured_image if fallback - best_image + def best_featured_image_source(fallback: true) + return self if featured_image.attached? + best_source = parent&.best_featured_image_source(fallback: false) + best_source ||= self if fallback + best_source end def git_path(website) diff --git a/app/views/admin/application/featured_image/_show.html.erb b/app/views/admin/application/featured_image/_show.html.erb index 31ded1fa4c23cf7b987ce7b664d5f702e0b7f656..6e3c9ee6b76ad475e4090a4a14a7ac172f65e88b 100644 --- a/app/views/admin/application/featured_image/_show.html.erb +++ b/app/views/admin/application/featured_image/_show.html.erb @@ -1,23 +1,26 @@ -<% if about.featured_image.attached? %> +<% if about.best_featured_image.attached? %> <div class="card flex-fill w-100"> <div class="card-header"> <h2 class="card-title mb-0 h5"> <%= about.class.human_attribute_name :featured_image %> + <% unless about.featured_image.attached? %> + (<%= t('admin.inheritance.sentence_without_link').downcase %>) + <% end %> </h2> </div> <div class="card-body"> - <% image = about.featured_image.variable? ? about.featured_image.variant(resize: '600') - : about.featured_image %> + <% image = about.best_featured_image.variable? ? about.best_featured_image.variant(resize: '600') + : about.best_featured_image %> <%= image_tag image, class: 'img-fluid mb-2' %> - <% if about.featured_image_credit %> + <% if about.best_featured_image_alt %> <p class="mb-0"> - <%= about.featured_image_alt %> + <%= about.best_featured_image_alt %> </p> <% end %> - <% if about.featured_image_credit %> - <p class="small mb-0"> - <%= about.featured_image_credit.html_safe %> - </p> + <% if about.best_featured_image_credit %> + <div class="small mb-0"> + <%= sanitize about.best_featured_image_credit %> + </div> <% end %> </div> </div> diff --git a/app/views/admin/application/featured_image/_static.html.erb b/app/views/admin/application/featured_image/_static.html.erb new file mode 100644 index 0000000000000000000000000000000000000000..12e6953f1f610be00416291540fadb544e4c6872 --- /dev/null +++ b/app/views/admin/application/featured_image/_static.html.erb @@ -0,0 +1,10 @@ +<% +best_featured_image = @about.best_featured_image +if best_featured_image&.attached? +%> +image: + id: "<%= best_featured_image.blob.id %>" + alt: "<%= @about.best_featured_image_alt %>" + credit: > + <%= prepare_html_for_static @about.best_featured_image_credit, @about.university %> +<% end %> diff --git a/app/views/admin/communication/unsplash/_static.html.erb b/app/views/admin/communication/unsplash/_static.html.erb deleted file mode 100644 index 2264bbf50564bb181d33d5edd4bfc9244073e96c..0000000000000000000000000000000000000000 --- a/app/views/admin/communication/unsplash/_static.html.erb +++ /dev/null @@ -1,10 +0,0 @@ -<% -featured_image = @about&.best_featured_image || @about.featured_image -if featured_image.attached? -%> -image: - id: "<%= featured_image.blob.id %>" - alt: "<%= @about.featured_image_alt %>" - credit: > - <%= prepare_html_for_static @about.featured_image_credit, @about.university %> -<% end %> diff --git a/app/views/admin/communication/websites/categories/static.html.erb b/app/views/admin/communication/websites/categories/static.html.erb index 015ae14d9c4ec618638cbc5d99dd237a9c6fd2c6..01c2e54a0f63171fba4f35c1020590bbcd2fb866 100644 --- a/app/views/admin/communication/websites/categories/static.html.erb +++ b/app/views/admin/communication/websites/categories/static.html.erb @@ -12,7 +12,7 @@ children: <% end %> <% end %> position: <%= @about.position %> -<%= render 'admin/communication/unsplash/static' %> +<%= render 'admin/application/featured_image/static' %> <%= render 'admin/application/meta_description/static' %> <%= render 'admin/application/summary/static' %> <%= render 'admin/communication/blocks/static', about: @about %> diff --git a/app/views/admin/communication/websites/pages/static.html.erb b/app/views/admin/communication/websites/pages/static.html.erb index 1c31214a481bb0fee8f49068cf1cb8ce3776591e..211f38113bb59e5e27121d35142f36e45a727893 100644 --- a/app/views/admin/communication/websites/pages/static.html.erb +++ b/app/views/admin/communication/websites/pages/static.html.erb @@ -13,7 +13,7 @@ has: <% end %> position: <%= @about.position %> bodyclass: <%= @about.best_bodyclass %> -<%= render 'admin/communication/unsplash/static' %> +<%= render 'admin/application/featured_image/static' %> <% if @about.children.published.any? %> children: <% @about.children.published.ordered.each do |child| %> diff --git a/app/views/admin/communication/websites/posts/static.html.erb b/app/views/admin/communication/websites/posts/static.html.erb index 3da24344ae40e7bf3c3b0ef8a75b7bd0508e1f93..e89b9fd4cac31e15f5478a9a45f9680a84c91c29 100644 --- a/app/views/admin/communication/websites/posts/static.html.erb +++ b/app/views/admin/communication/websites/posts/static.html.erb @@ -16,7 +16,7 @@ categories: - "<%= category.slug_with_ancestors_slugs %>" <% end %> <% end %> -<%= render 'admin/communication/unsplash/static' %> +<%= render 'admin/application/featured_image/static' %> <%= render 'admin/application/meta_description/static' %> <%= render 'admin/application/summary/static' %> <%= render 'admin/communication/blocks/static', about: @about %> diff --git a/app/views/admin/education/programs/static.html.erb b/app/views/admin/education/programs/static.html.erb index 85007b6ca513c033c55998fd372813d2501f3e62..fc78a8f231898e3c70e1cd104a8a5d0a8b877882 100644 --- a/app/views/admin/education/programs/static.html.erb +++ b/app/views/admin/education/programs/static.html.erb @@ -27,7 +27,7 @@ parent: <%= @about.parent.name %> url: <%= @about.parent.current_permalink_in_website(@website)&.path %> <% end %> -<%= render 'admin/communication/unsplash/static' %> +<%= render 'admin/application/featured_image/static' %> related_category: "<%= @website.categories.find_by(program_id: @about.id)&.slug_with_ancestors_slugs %>" diplomas: "<%= @about.diploma&.slug %>" teachers: diff --git a/app/views/admin/research/journals/volumes/static.html.erb b/app/views/admin/research/journals/volumes/static.html.erb index bd0d0a26b7b6cc0766697c261044303cfa11a4ec..ca58770310ba3c07db7278d6e9b0db78921f00b0 100644 --- a/app/views/admin/research/journals/volumes/static.html.erb +++ b/app/views/admin/research/journals/volumes/static.html.erb @@ -6,7 +6,7 @@ issn: "<%= @about.journal.issn %>" keywords: > <%= @about.keywords %> date: "<%= @about.published_at.iso8601 %>" -<%= render 'admin/communication/unsplash/static' %> +<%= render 'admin/application/featured_image/static' %> <%= render 'admin/application/meta_description/static' %> --- <%= prepare_html_for_static @about.text, @about.university %>