diff --git a/app/models/resource.rb b/app/models/resource.rb index 3fdb23470c..f8782f0c25 100644 --- a/app/models/resource.rb +++ b/app/models/resource.rb @@ -12,11 +12,6 @@ def self.mentionable_rich_text_fields PUBLISHED_KINDS = [ "Handout", "Template", "Toolkit", "Form" ] KINDS = PUBLISHED_KINDS + [ "Resource", "Story", "LeaderSpotlight", "SectorImpact", "Theme", "Scholarship" ] - # Titles whose downloadable PDF is a single page. Every other resource PDF is - # multi-page, so its first-page preview needs a "download for all pages" note. - # Hardcoded for now — replace with real PDF introspection later. - SINGLE_PAGE_PDF_TITLES = [ "Letter to Supervisors", "W-9" ].freeze - has_rich_text :rhino_body belongs_to :created_by, class_name: "User" @@ -125,12 +120,6 @@ def story? [ "Story", "LeaderSpotlight" ].include? self.kind end - # A multi-page PDF only previews its first page, so the viewer needs a prompt - # to download for the rest. See SINGLE_PAGE_PDF_TITLES. - def single_page_pdf? - SINGLE_PAGE_PDF_TITLES.include?(title) - end - def custom_label_list "#{self.title} (#{self.kind.upcase})" unless self.kind.nil? end diff --git a/app/views/assets/_resource_display.html.erb b/app/views/assets/_resource_display.html.erb new file mode 100644 index 0000000000..a3930a740d --- /dev/null +++ b/app/views/assets/_resource_display.html.erb @@ -0,0 +1,17 @@ +<%# Renders a resource's main display. A PDF opens in the browser's built-in + inline viewer (scrollable, all pages) — every major browser ships one — with + the first-page/image preview as the fallback child for the rare client that + can't. Anything else (images, etc.) renders through the shared asset + pipeline. `resource` is a decorated Resource. %> +<% display = resource.display_image %> +<% if display.respond_to?(:attached?) && display.attached? && display.content_type == "application/pdf" %> + +<% else %> + <%= render "assets/display_assets", + resource: resource, file: display, variant: :hero, link: true %> +<% end %> diff --git a/app/views/events/callouts/_resource_body.html.erb b/app/views/events/callouts/_resource_body.html.erb index 4f2f0b8414..3e37b0cedc 100644 --- a/app/views/events/callouts/_resource_body.html.erb +++ b/app/views/events/callouts/_resource_body.html.erb @@ -1,13 +1,11 @@ <%# Renders a resource inside a callout page: a top-right download button (when a - downloadable file is attached) plus the resource display (PDF first-page preview - etc., via the same pipeline as resources/show). `resource` is a decorated - Resource. Shared by the registrant resource viewer and admin callouts that link - a resource. %> + downloadable file is attached) plus the resource display. PDFs show in the + browser's inline viewer; everything else renders via the shared asset + pipeline (see assets/resource_display). `resource` is a decorated Resource. + Shared by the registrant resource viewer and admin callouts that link a + resource. %> <% if resource.downloadable_asset&.file&.attached? %>
The preview below may take a moment to load.
-<%= render "assets/display_assets", - resource: resource, file: resource.display_image, - variant: :hero, link: true %> +<%= render "assets/resource_display", resource: resource %> diff --git a/app/views/resources/show.html.erb b/app/views/resources/show.html.erb index 9c00e601c6..f52c675d7c 100644 --- a/app/views/resources/show.html.erb +++ b/app/views/resources/show.html.erb @@ -55,7 +55,7 @@#{body_content}
", - author_id: User.all.sample&.id, + author_id: Person.all.sample&.id, + user_author_id: User.all.sample&.id, created_by_id: User.first&.id, updated_by_id: User.first&.id, organization_id: Organization.all.sample&.id, diff --git a/db/seeds/dev/resources.rb b/db/seeds/dev/resources.rb index 5a35ece530..4490d3cf73 100644 --- a/db/seeds/dev/resources.rb +++ b/db/seeds/dev/resources.rb @@ -22,7 +22,8 @@ Resource.where(title: Faker::Book.title).first_or_create!( body: resource_body, rhino_body: resource_body, - author: [ Faker::Name.name, nil ].sample, + author: [ Person.all.sample, nil ].sample, + legacy_author_name: [ Faker::Name.name, nil ].sample, agency: [ Faker::Company.name, nil ].sample, kind: kind, url: [ "https://example.com/resource/#{SecureRandom.hex(4)}", nil ].sample, diff --git a/spec/models/resource_spec.rb b/spec/models/resource_spec.rb index d428bdce4b..f391676042 100644 --- a/spec/models/resource_spec.rb +++ b/spec/models/resource_spec.rb @@ -80,15 +80,4 @@ end end end - - describe "#single_page_pdf?" do - it "is true for the known single-page titles" do - expect(build(:resource, title: "W-9").single_page_pdf?).to be(true) - expect(build(:resource, title: "Letter to Supervisors").single_page_pdf?).to be(true) - end - - it "is false for any other title" do - expect(build(:resource, title: "AHA Moments").single_page_pdf?).to be(false) - end - end end diff --git a/spec/requests/events/registration_ticket_callouts_spec.rb b/spec/requests/events/registration_ticket_callouts_spec.rb index 7a81040211..11db7ed85a 100644 --- a/spec/requests/events/registration_ticket_callouts_spec.rb +++ b/spec/requests/events/registration_ticket_callouts_spec.rb @@ -72,6 +72,35 @@ end end + context "when linked to a PDF resource" do + let(:resource) { create(:resource) } + let(:callout) { create(:registration_ticket_callout, event:, resource:, description: "") } + + before { create(:downloadable_asset, owner: resource) } + + it "shows the PDF in the browser's inline viewer" do + get event_registration_ticket_callout_path(event, callout) + + expect(response).to have_http_status(:ok) + expect(response.body).to include("type=\"application/pdf\"") + expect(response.body).to include(rails_blob_path(resource.downloadable_asset.file, disposition: :inline)) + end + end + + context "when linked to a non-PDF resource" do + let(:resource) { create(:resource) } + let(:callout) { create(:registration_ticket_callout, event:, resource:, description: "") } + + before { create(:downloadable_asset, :with_image, owner: resource) } + + it "renders the preview instead of an inline PDF viewer" do + get event_registration_ticket_callout_path(event, callout) + + expect(response).to have_http_status(:ok) + expect(response.body).not_to include("type=\"application/pdf\"") + end + end + context "when linked to a resource without a downloadable file" do let(:resource) { create(:resource) } let(:callout) do diff --git a/spec/requests/events/registrations_spec.rb b/spec/requests/events/registrations_spec.rb index b81bc6b97a..1814803521 100644 --- a/spec/requests/events/registrations_spec.rb +++ b/spec/requests/events/registrations_spec.rb @@ -358,24 +358,6 @@ expect(response.body).to include(rails_blob_path(resource.downloadable_asset.file, only_path: true)) end - it "prompts to download for all pages on a multi-page resource" do - resource = create(:resource, title: "Aha Moments", kind: "Handout") - create(:downloadable_asset, owner: resource) - - get registration_resource_path(registration.slug, resource) - - expect(response.body).to include("download to get all pages") - end - - it "omits the all-pages prompt on a known single-page resource" do - resource = create(:resource, title: "W-9", kind: "Form") - create(:downloadable_asset, owner: resource) - - get registration_resource_path(registration.slug, resource) - - expect(response.body).not_to include("download to get all pages") - end - it "is reachable by slug without logging in" do resource = create(:resource, title: "Aha Moments", kind: "Handout")