Skip to content

Commit

Permalink
Deprecate Attachment#urlname
Browse files Browse the repository at this point in the history
And use slug instead.
  • Loading branch information
tvdeyen committed May 25, 2020
1 parent 4b5ecc1 commit 7253523
Show file tree
Hide file tree
Showing 9 changed files with 19 additions and 16 deletions.
2 changes: 1 addition & 1 deletion app/controllers/alchemy/admin/pages_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -140,7 +140,7 @@ def destroy

def link
@attachments = Attachment.all.collect { |f|
[f.name, download_attachment_path(id: f.id, name: f.urlname)]
[f.name, download_attachment_path(id: f.id, name: f.slug)]
}
@url_prefix = prefix_locale? ? "#{Language.current.code}/" : ""
end
Expand Down
4 changes: 2 additions & 2 deletions app/helpers/alchemy/url_helper.rb
Original file line number Diff line number Diff line change
Expand Up @@ -25,12 +25,12 @@ def show_page_path_params(page, optional_params = {})

# Returns the path for downloading an alchemy attachment
def download_alchemy_attachment_path(attachment)
alchemy.download_attachment_path(attachment, attachment.urlname)
alchemy.download_attachment_path(attachment, attachment.slug)
end

# Returns the url for downloading an alchemy attachment
def download_alchemy_attachment_url(attachment)
alchemy.download_attachment_url(attachment, attachment.urlname)
alchemy.download_attachment_url(attachment, attachment.slug)
end

# Returns the full url containing host, page and anchor for the given element
Expand Down
7 changes: 5 additions & 2 deletions app/models/alchemy/attachment.rb
Original file line number Diff line number Diff line change
Expand Up @@ -76,10 +76,13 @@ def to_jq_upload
end

# An url save filename without format suffix
def urlname
CGI.escape(file_name.gsub(/\.#{extension}$/, '').tr('.', ' '))
def slug
CGI.escape(file_name.gsub(/\.#{extension}$/, "").tr(".", " "))
end

alias_method :urlname, :slug
deprecate urlname: :slug, deprecator: Alchemy::Deprecation

# Checks if the attachment is restricted, because it is attached on restricted pages only
def restricted?
pages.any? && pages.not_restricted.blank?
Expand Down
2 changes: 1 addition & 1 deletion app/models/alchemy/essence_file.rb
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ def attachment_url
return if attachment.nil?
routes.download_attachment_path(
id: attachment.id,
name: attachment.urlname,
name: attachment.slug,
format: attachment.suffix
)
end
Expand Down
2 changes: 1 addition & 1 deletion app/views/alchemy/essences/_essence_file_view.html.erb
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
attachment.name,
alchemy.download_attachment_path(
attachment,
name: attachment.urlname,
name: attachment.slug,
format: attachment.suffix
),
{
Expand Down
6 changes: 3 additions & 3 deletions spec/helpers/alchemy/url_helper_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -131,16 +131,16 @@ module Alchemy
end

context 'attachment path helpers' do
let(:attachment) { mock_model(Attachment, urlname: 'test-attachment.pdf') }
let(:attachment) { mock_model(Attachment, slug: "test-attachment.pdf") }

it 'should return the correct relative path to download an attachment' do
expect(helper.download_alchemy_attachment_path(attachment)).to \
eq("/attachment/#{attachment.id}/download/#{attachment.urlname}")
eq("/attachment/#{attachment.id}/download/#{attachment.slug}")
end

it 'should return the correct url to download an attachment' do
expect(helper.download_alchemy_attachment_url(attachment)).to \
eq("http://#{helper.request.host}/attachment/#{attachment.id}/download/#{attachment.urlname}")
eq("http://#{helper.request.host}/attachment/#{attachment.id}/download/#{attachment.slug}")
end
end

Expand Down
8 changes: 4 additions & 4 deletions spec/models/alchemy/attachment_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -54,22 +54,22 @@ module Alchemy
describe 'urlname sanitizing' do
it "escapes unsafe url characters" do
attachment.file_name = 'f#%&cking cute kitten pic.png'
expect(attachment.urlname).to eq('f%23%25%26cking+cute+kitten+pic')
expect(attachment.slug).to eq("f%23%25%26cking+cute+kitten+pic")
end

it "removes format suffix from end of file name" do
attachment.file_name = 'pic.png.png'
expect(attachment.urlname).to eq('pic+png')
expect(attachment.slug).to eq("pic+png")
end

it "converts dots into escaped spaces" do
attachment.file_name = 'cute.kitten.pic.png'
expect(attachment.urlname).to eq('cute+kitten+pic')
expect(attachment.slug).to eq("cute+kitten+pic")
end

it "escapes umlauts in the name" do
attachment.file_name = 'süßes katzenbild.png'
expect(attachment.urlname).to eq('s%C3%BC%C3%9Fes+katzenbild')
expect(attachment.slug).to eq("s%C3%BC%C3%9Fes+katzenbild")
end
end

Expand Down
2 changes: 1 addition & 1 deletion spec/models/alchemy/essence_file_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ module Alchemy
subject { essence.attachment_url }

it "returns the download attachment url." do
is_expected.to match(/\/attachment\/#{attachment.id}\/download\/#{attachment.urlname}\.#{attachment.suffix}/)
is_expected.to match(/\/attachment\/#{attachment.id}\/download\/#{attachment.slug}\.#{attachment.suffix}/)
end

context 'without attachment assigned' do
Expand Down
2 changes: 1 addition & 1 deletion spec/views/essences/essence_file_view_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@
it "renders a link to download the attachment" do
render content, content: content
expect(rendered).to have_selector(
"a[href='/attachment/#{attachment.id}/download/#{attachment.urlname}.#{attachment.suffix}']"
"a[href='/attachment/#{attachment.id}/download/#{attachment.slug}.#{attachment.suffix}']"
)
end

Expand Down

0 comments on commit 7253523

Please sign in to comment.