Permalink
Browse files

Sidestep the whole escaping issue by telling url not to escape in int…

…erpolations
  • Loading branch information...
jyurek committed Oct 21, 2011
1 parent cd577da commit 4917ab2fba921e8d3e62bb834b3fc2f97d0356eb
@@ -1,33 +0,0 @@
-require 'uri'
-
-module Paperclip
- class InterpolatedString < String
- def escaped?
- !!@escaped
- end
-
- def escape
- if !escaped?
- escaped_string = self.class.new(URI.escape(self))
- escaped_string.instance_variable_set(:@escaped, true)
- escaped_string
- else
- self
- end
- end
-
- def unescape
- if escaped?
- escaped_string = self.class.new(URI.unescape(self))
- escaped_string.instance_variable_set(:@escaped, false)
- escaped_string
- else
- self
- end
- end
-
- def force_escape
- @escaped = true
- end
- end
-end
@@ -31,13 +31,11 @@ def self.all
# an interpolation pattern for Paperclip to use.
def self.interpolate pattern, *args
pattern = args.first.instance.send(pattern) if pattern.kind_of? Symbol
- interpolated_string = all.reverse.inject(InterpolatedString.new(pattern)) do |result, tag|
+ all.reverse.inject(pattern) do |result, tag|
result.gsub(/:#{tag}/) do |match|
send( tag, *args )
end
end
- interpolated_string.force_escape if pattern =~ /:url/
- interpolated_string
end
# Returns the filename, the same way as ":basename.:extension" would.
@@ -51,7 +49,7 @@ def filename attachment, style_name
RIGHT_HERE = "#{__FILE__.gsub(%r{^\./}, "")}:#{__LINE__ + 3}"
def url attachment, style_name
raise InfiniteInterpolationError if caller.any?{|b| b.index(RIGHT_HERE) }
- attachment.url(style_name, false)
+ attachment.url(style_name, :timestamp => false, :escape => false)
end
# Returns the timestamp as defined by the <attachment>_updated_at field
@@ -1,62 +0,0 @@
-require './test/helper'
-
-class InterpolatedStringTest < Test::Unit::TestCase
- context "inheritance" do
- should "inherited from String" do
- assert Paperclip::InterpolatedString.new("paperclip").is_a? String
- end
- end
-
- context "#escape" do
- subject { Paperclip::InterpolatedString.new("paperclip foo").escape }
-
- should "returns an InterpolatedString object" do
- assert subject.is_a? Paperclip::InterpolatedString
- end
-
- should "escape the output string" do
- assert_equal "paperclip%20foo", subject
- end
-
- should "not double escape output string" do
- assert_equal "paperclip%20foo", subject.escape
- end
- end
-
- context "#unescape" do
- subject { Paperclip::InterpolatedString.new("paperclip%20foo").escape.unescape }
-
- should "returns an InterpolatedString object" do
- assert subject.is_a? Paperclip::InterpolatedString
- end
-
- should "unescape the output string" do
- assert_equal "paperclip%20foo", subject
- end
-
- should "not double unescape output string" do
- assert_equal "paperclip%20foo", subject.unescape
- end
- end
-
- context "#escaped?" do
- subject { Paperclip::InterpolatedString.new("paperclip") }
-
- should "returns true if string was escaped" do
- assert subject.escape.escaped?
- end
-
- should "returns false if string wasn't escaped" do
- assert !subject.escaped?
- end
- end
-
- context "#force_escape" do
- subject { Paperclip::InterpolatedString.new("paperclip") }
- setup { subject.force_escape }
-
- should "sets escaped flag to true" do
- assert subject.escaped?
- end
- end
-end
@@ -120,7 +120,7 @@ class InterpolationsTest < Test::Unit::TestCase
should "reinterpolate :url" do
attachment = mock
- attachment.expects(:url).with(:style, false).returns("1234")
+ attachment.expects(:url).with(:style, :timestamp => false, :escape => false).returns("1234")
assert_equal "1234", Paperclip::Interpolations.url(attachment, :style)
end

0 comments on commit 4917ab2

Please sign in to comment.