Commit 68aea16f by Geoffrey Hichborn Committed by Jon Yurek

Implemented performance improvement to interpolator

parent c0c1f1ed
...@@ -27,12 +27,9 @@ module Paperclip ...@@ -27,12 +27,9 @@ module Paperclip
# and the arguments to pass, which are the attachment and style name. # and the arguments to pass, which are the attachment and style name.
# You can pass a method name on your record as a symbol, which should turn # You can pass a method name on your record as a symbol, which should turn
# an interpolation pattern for Paperclip to use. # an interpolation pattern for Paperclip to use.
def self.interpolate pattern, *args def self.interpolate(pattern, *args)
pattern = args.first.instance.send(pattern) if pattern.kind_of? Symbol pattern.gsub /:([[:word:]]+)/ do
all.reverse.inject(pattern) do |result, tag| Paperclip::Interpolations.respond_to?($1) ? Paperclip::Interpolations.send($1, *args) : ":#{$1}"
result.gsub(/:#{tag}/) do |match|
send( tag, *args )
end
end end
end end
......
...@@ -282,8 +282,8 @@ class AttachmentTest < Test::Unit::TestCase ...@@ -282,8 +282,8 @@ class AttachmentTest < Test::Unit::TestCase
teardown { @file.close } teardown { @file.close }
should "make sure that they are interpolated correctly" do should "make sure that they are interpolated correctly as long non-word characters separate them" do
assert_equal "1024.omg/1024-bbq/1024what/000/001/024.wtf", @dummy.avatar.path assert_equal "1024.omg/1024-bbq/:idwhat/000/001/024.wtf", @dummy.avatar.path
end end
end end
......
...@@ -231,7 +231,6 @@ class InterpolationsTest < Test::Unit::TestCase ...@@ -231,7 +231,6 @@ class InterpolationsTest < Test::Unit::TestCase
should "call all expected interpolations with the given arguments" do should "call all expected interpolations with the given arguments" do
Paperclip::Interpolations.expects(:id).with(:attachment, :style).returns(1234) Paperclip::Interpolations.expects(:id).with(:attachment, :style).returns(1234)
Paperclip::Interpolations.expects(:attachment).with(:attachment, :style).returns("attachments") Paperclip::Interpolations.expects(:attachment).with(:attachment, :style).returns("attachments")
Paperclip::Interpolations.expects(:notreal).never
value = Paperclip::Interpolations.interpolate(":notreal/:id/:attachment", :attachment, :style) value = Paperclip::Interpolations.interpolate(":notreal/:id/:attachment", :attachment, :style)
assert_equal ":notreal/1234/attachments", value assert_equal ":notreal/1234/attachments", value
end end
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment