Skip to content

Commit

Permalink
Always provide format with attachment URLs
Browse files Browse the repository at this point in the history
When linking to an attachment without the format, the browser does not
necessarily know what to do with the file. Adding the suffix by default
makes that better: Firefox won't, for example, try rendering a PDF file
as HTML.
  • Loading branch information
mamhoff committed Oct 22, 2022
1 parent 9d1505b commit ea99ab0
Show file tree
Hide file tree
Showing 2 changed files with 4 additions and 3 deletions.
1 change: 1 addition & 0 deletions app/models/alchemy/attachment/url.rb
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ def initialize(attachment)
# @return [String]
#
def call(options = {})
options[:format] ||= @attachment.suffix
if options.delete(:download)
routes.download_attachment_path(@attachment, options)
else
Expand Down
6 changes: 3 additions & 3 deletions spec/models/alchemy/attachment_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -63,14 +63,14 @@ module Alchemy
let(:attachment) { create(:alchemy_attachment) }

it "returns local path" do
is_expected.to eq "/attachment/#{attachment.id}/show"
is_expected.to eq "/attachment/#{attachment.id}/show.png"
end

context "with download enabled" do
subject { attachment.url(download: true) }

it "returns local download path" do
is_expected.to eq "/attachment/#{attachment.id}/download"
is_expected.to eq "/attachment/#{attachment.id}/download.png"
end

context "with extra params given" do
Expand All @@ -88,7 +88,7 @@ module Alchemy
subject { attachment.url(download: false) }

it "returns local path" do
is_expected.to eq "/attachment/#{attachment.id}/show"
is_expected.to eq "/attachment/#{attachment.id}/show.png"
end

context "with extra params given" do
Expand Down

0 comments on commit ea99ab0

Please sign in to comment.