From ec8329069d36cc6aa444c8a48b6b34a3e8f5c9d2 Mon Sep 17 00:00:00 2001 From: erdgeist Date: Tue, 21 Jul 2026 21:34:14 +0200 Subject: Extract Page#headline_asset The same related_assets.find_by(headline: true) query was written by hand in three places -- content_helper, nodes#show, and the chapter teaser partial (which was still using assets.first, the old position-based rule, until now). Collapsed to one method, one query. --- app/helpers/content_helper.rb | 2 +- app/models/page.rb | 4 ++++ app/views/custom/partials/_chapter.html.erb | 4 ++-- app/views/nodes/show.html.erb | 2 +- 4 files changed, 8 insertions(+), 4 deletions(-) (limited to 'app') diff --git a/app/helpers/content_helper.rb b/app/helpers/content_helper.rb index b3c6bad..459d9e0 100644 --- a/app/helpers/content_helper.rb +++ b/app/helpers/content_helper.rb @@ -53,7 +53,7 @@ module ContentHelper end def headline_image - @headline_asset = @page.related_assets.find_by(headline: true)&.asset + @headline_asset = @page.headline_asset render :partial => 'content/headline_image' if @headline_asset || @page.assets.images.any? end diff --git a/app/models/page.rb b/app/models/page.rb index c1498fc..817e3bd 100644 --- a/app/models/page.rb +++ b/app/models/page.rb @@ -292,6 +292,10 @@ class Page < ApplicationRecord end end + def headline_asset + related_assets.find_by(headline: true)&.asset + end + # Returns true if a page has translations where one of them is significantly # older than the other. # Takes the I18n.default locale and a second :locale to test if the diff --git a/app/views/custom/partials/_chapter.html.erb b/app/views/custom/partials/_chapter.html.erb index 47f3950..5d47679 100644 --- a/app/views/custom/partials/_chapter.html.erb +++ b/app/views/custom/partials/_chapter.html.erb @@ -1,7 +1,7 @@
- <% if page.assets.images.any? %> - <%= link_to_path image_tag(page.assets.images.first.upload.url(:thumb), :alt => ""), page.node.unique_name, class: "chapter_thumbnail" %> + <% if page.headline_asset %> + <%= link_to_path image_tag(page.headline_asset.upload.url(:thumb), :alt => ""), page.node.unique_name, class: "chapter_thumbnail" %> <% end %>

<%= link_to_path page.title, page.node.unique_name %>

diff --git a/app/views/nodes/show.html.erb b/app/views/nodes/show.html.erb index a7be3c6..f2b4b15 100644 --- a/app/views/nodes/show.html.erb +++ b/app/views/nodes/show.html.erb @@ -254,7 +254,7 @@
<% if @page.assets.images.any? %> - <% headline_asset_id = @page.related_assets.find_by(headline: true)&.asset_id %> + <% headline_asset_id = @page.headline_asset&.id %>
Images
    -- cgit v1.3