From 9d432754a9c76cfa4e8bf5ac771900d7d5a7c787 Mon Sep 17 00:00:00 2001 From: Pablo Brasero Date: Thu, 24 Oct 2024 10:12:32 +0100 Subject: [PATCH] Better experience for example app --- spec/example_app/app/controllers/files_controller.rb | 12 ++++++++++++ spec/example_app/config/routes.rb | 2 ++ .../lib/administrate/field/receipt_link.rb | 2 +- spec/features/payments_index_spec.rb | 12 +++++++++++- 4 files changed, 26 insertions(+), 2 deletions(-) create mode 100644 spec/example_app/app/controllers/files_controller.rb diff --git a/spec/example_app/app/controllers/files_controller.rb b/spec/example_app/app/controllers/files_controller.rb new file mode 100644 index 0000000000..e6f6b2ea15 --- /dev/null +++ b/spec/example_app/app/controllers/files_controller.rb @@ -0,0 +1,12 @@ +class FilesController < ApplicationController + def download + filename = params[:filename] + match = %r{receipt-(\d+)}.match(filename) + if match + payment_id = match[1] + send_data("This is the receipt for payment ##{payment_id}", filename: "#{filename}.txt") + else + render status: 404, layout: false, file: Rails.root.join("public/404.html") + end + end +end diff --git a/spec/example_app/config/routes.rb b/spec/example_app/config/routes.rb index f8fc984e8b..53ce954aec 100644 --- a/spec/example_app/config/routes.rb +++ b/spec/example_app/config/routes.rb @@ -24,6 +24,8 @@ root to: "customers#index" end + get "/files/receipts/*filename.txt", to: "files#download" + get "/*page", to: "docs#show", constraints: ->(request) { !request.path.start_with?("/rails/") } root to: "docs#index" end diff --git a/spec/example_app/lib/administrate/field/receipt_link.rb b/spec/example_app/lib/administrate/field/receipt_link.rb index 0756b259ce..bbe7192109 100644 --- a/spec/example_app/lib/administrate/field/receipt_link.rb +++ b/spec/example_app/lib/administrate/field/receipt_link.rb @@ -8,7 +8,7 @@ def data end def filename - "receipt-#{resource.id}.pdf" + "receipt-#{resource.id}.txt" end end end diff --git a/spec/features/payments_index_spec.rb b/spec/features/payments_index_spec.rb index 1d16401767..b5365ff142 100644 --- a/spec/features/payments_index_spec.rb +++ b/spec/features/payments_index_spec.rb @@ -8,7 +8,17 @@ expect(page).to have_header("Payments") expect(page).to have_content(payment.id) - expect(page).to have_content("receipt-#{payment.id}.pdf") + expect(page).to have_content("receipt-#{payment.id}.txt") + end + + it "allows downloading the receipt" do + payment = create(:payment) + + visit admin_payments_path + click_on("receipt-#{payment.id}.txt") + + expect(page.body).to eq("This is the receipt for payment ##{payment.id}") + expect(response_headers["Content-Disposition"]).to match(%r{^attachment; filename=}) end it "links to the payment show page", :js do