Skip to content
This repository has been archived by the owner on Jul 13, 2023. It is now read-only.

Commit

Permalink
URLGenerator should disregard a ? which isn't followed by an =
Browse files Browse the repository at this point in the history
In some case, we seriously don't want to prepend the timestamp with & if there wasn't a query string there. If the original URL doesn't have any query string, we should add the ?.
  • Loading branch information
sikachu committed Dec 12, 2011
1 parent bb22be3 commit 128d664
Show file tree
Hide file tree
Showing 2 changed files with 4 additions and 4 deletions.
4 changes: 2 additions & 2 deletions lib/paperclip/url_generator.rb
Expand Up @@ -38,7 +38,7 @@ def most_appropriate_url

def timestamp_as_needed(url, options)
if options[:timestamp] && timestamp_possible?
delimiter_char = url.include?('?') ? '&' : '?'
delimiter_char = url.match(/\?.+=/) ? '&' : '?'
"#{url}#{delimiter_char}#{@attachment.updated_at.to_s}"
else
url
Expand All @@ -58,7 +58,7 @@ def escape_url_as_needed(url, options)
end

def escape_url(url)
url.respond_to?(:escape) ? url.escape : URI.escape(url)
(url.respond_to?(:escape) ? url.escape : URI.escape(url)).gsub(/(\/.+)\?(.+\.)/, '\1%3F\2')

This comment has been minimized.

Copy link
@metaskills

metaskills Mar 13, 2012

Contributor

This line means I can no longer have a URL from an interpolation look like so, note the ? for the params. I have worked around the issue by defining a singleton method for #escape on the returned string. The subject of the commit seems to indicated this should not happen since indeed my returned URL does have an = after it.

Paperclip.interpolates(:my_authed_url) do |attachment, style|
  params = "?filename=#{attachment.original_filename}&key=#{attachment.instance.random_secret}"
  "http://example.com/downloads/get_authed#{params}".tap do |url|
    def url.escape
      url
    end
  end
end

So is this pattern what we need to do in my case of returning a self build url in an interpolation?

This comment has been minimized.

Copy link
@metaskills

metaskills Mar 13, 2012

Contributor

In fact I am wrong, even defining an escape will not work as no matter what the ? is converted. This should not be done if there is an = after it right? That line should maybe include that logic? Open an issue?

This comment has been minimized.

Copy link
@metaskills

metaskills Mar 13, 2012

Contributor

OK, figured it out. I had to pass :escape => false to my #url method. Duh :/ Thanks!

end
end
end
4 changes: 2 additions & 2 deletions test/url_generator_test.rb
Expand Up @@ -146,8 +146,8 @@ def escape
assert_equal "#{expected}?#{updated_at}", result
end

should "produce URLs with the updated_at when it exists, separated with a & if a ? already exists" do
expected = "the?expected result"
should "produce URLs with the updated_at when it exists, separated with a & if a ? follow by = already exists" do
expected = "the?expected=result"
updated_at = 1231231234
mock_interpolator = MockInterpolator.new(:result => expected)
mock_attachment = MockAttachment.new(:updated_at => updated_at)
Expand Down

0 comments on commit 128d664

Please sign in to comment.