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