From 9c951f3a0f379b0f459988106cf3f839acdadbe2 Mon Sep 17 00:00:00 2001 From: erdgeist Date: Tue, 21 Jul 2026 22:28:18 +0200 Subject: More PDF headline eligibility --- app/controllers/related_assets_controller.rb | 4 ++-- app/models/asset.rb | 6 ++++++ app/views/nodes/edit.html.erb | 2 +- test/models/asset_test.rb | 5 +++++ 4 files changed, 14 insertions(+), 3 deletions(-) diff --git a/app/controllers/related_assets_controller.rb b/app/controllers/related_assets_controller.rb index 2fefaf7..560df75 100644 --- a/app/controllers/related_assets_controller.rb +++ b/app/controllers/related_assets_controller.rb @@ -5,10 +5,10 @@ class RelatedAssetsController < ApplicationController def search term = params[:search_term].to_s.strip attached_ids = @node.editable_page.related_assets.pluck(:asset_id) - scope = Asset.images.where.not(id: attached_ids) + scope = Asset.headline_eligible.where.not(id: attached_ids) results = if term.present? - scope.where("name ILIKE ?", "%#{term}%").limit(10) + scope.where("name ILIKE :term OR upload_file_name ILIKE :term", term: "%#{term}%").limit(10) else scope.order(created_at: :desc).limit(5) end diff --git a/app/models/asset.rb b/app/models/asset.rb index 4a295d8..8aa5415 100644 --- a/app/models/asset.rb +++ b/app/models/asset.rb @@ -1,5 +1,6 @@ class Asset < ApplicationRecord IMAGE_CONTENT_TYPES = ["image/gif", "image/jpeg", "image/png", "image/webp"] + PDF_CONTENT_TYPE = "application/pdf" include FileAttachment @@ -9,6 +10,7 @@ class Asset < ApplicationRecord scope :images, -> { where(:upload_content_type => IMAGE_CONTENT_TYPES) } scope :documents, -> { where(:upload_content_type => ["application/pdf", "text/plain", "text/rtf"]) } scope :audio, -> { where(:upload_content_type => ["audio/mpeg", "audio/x-m4a", "audio/wav", "audio/x-wav"]) } + scope :headline_eligible, -> { where(:upload_content_type => IMAGE_CONTENT_TYPES + [PDF_CONTENT_TYPE]) } validates :license_key, inclusion: { in: -> { AssetLicense.keys } }, allow_blank: true @@ -16,6 +18,10 @@ class Asset < ApplicationRecord IMAGE_CONTENT_TYPES.include?(upload_content_type) end + def pdf? + upload_content_type == PDF_CONTENT_TYPE + end + def has_credit? creator.present? || source_url.present? || license_key.present? end diff --git a/app/views/nodes/edit.html.erb b/app/views/nodes/edit.html.erb index 07b42d4..f06ccaf 100644 --- a/app/views/nodes/edit.html.erb +++ b/app/views/nodes/edit.html.erb @@ -102,7 +102,7 @@ <% end %> -

The starred photo becomes this page's headline image on the public site. If none is starred, visitors get a link to browse all attached images instead.

+

The starred image or PDF becomes this page's headline on the public site. If none is starred, visitors get a link to browse all attached images instead.

<%= text_field_tag nil, nil, id: "related_asset_search_term", placeholder: "Search images to attach…", autocomplete: "off" %> diff --git a/test/models/asset_test.rb b/test/models/asset_test.rb index d246abe..cda0a04 100644 --- a/test/models/asset_test.rb +++ b/test/models/asset_test.rb @@ -41,4 +41,9 @@ class AssetTest < ActiveSupport::TestCase test "license_key may be blank" do assert Asset.new(:license_key => nil).valid? end + + test "pdf? is true only for application/pdf" do + assert Asset.new(:upload_content_type => "application/pdf").pdf? + assert_not Asset.new(:upload_content_type => "image/png").pdf? + end end -- cgit v1.3