From 68aea16fe8e2d1a19d6fc3adbc4e9d5fc74f5686 Mon Sep 17 00:00:00 2001 From: Geoffrey Hichborn Date: Fri, 15 Jun 2012 17:24:05 -0700 Subject: [PATCH] Implemented performance improvement to interpolator --- lib/paperclip/interpolations.rb | 9 +++------ test/attachment_test.rb | 4 ++-- test/interpolations_test.rb | 1 - 3 files changed, 5 insertions(+), 9 deletions(-) diff --git a/lib/paperclip/interpolations.rb b/lib/paperclip/interpolations.rb index a14b5979d..70f43f22f 100644 --- a/lib/paperclip/interpolations.rb +++ b/lib/paperclip/interpolations.rb @@ -27,12 +27,9 @@ def self.all # 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 # an interpolation pattern for Paperclip to use. - def self.interpolate pattern, *args - pattern = args.first.instance.send(pattern) if pattern.kind_of? Symbol - all.reverse.inject(pattern) do |result, tag| - result.gsub(/:#{tag}/) do |match| - send( tag, *args ) - end + def self.interpolate(pattern, *args) + pattern.gsub /:([[:word:]]+)/ do + Paperclip::Interpolations.respond_to?($1) ? Paperclip::Interpolations.send($1, *args) : ":#{$1}" end end diff --git a/test/attachment_test.rb b/test/attachment_test.rb index 02dd6255c..1d8bd910a 100644 --- a/test/attachment_test.rb +++ b/test/attachment_test.rb @@ -282,8 +282,8 @@ class AttachmentTest < Test::Unit::TestCase teardown { @file.close } - should "make sure that they are interpolated correctly" do - assert_equal "1024.omg/1024-bbq/1024what/000/001/024.wtf", @dummy.avatar.path + should "make sure that they are interpolated correctly as long non-word characters separate them" do + assert_equal "1024.omg/1024-bbq/:idwhat/000/001/024.wtf", @dummy.avatar.path end end diff --git a/test/interpolations_test.rb b/test/interpolations_test.rb index 6c7f4af3e..b7d69c00c 100644 --- a/test/interpolations_test.rb +++ b/test/interpolations_test.rb @@ -231,7 +231,6 @@ def url(*args) should "call all expected interpolations with the given arguments" do Paperclip::Interpolations.expects(:id).with(:attachment, :style).returns(1234) Paperclip::Interpolations.expects(:attachment).with(:attachment, :style).returns("attachments") - Paperclip::Interpolations.expects(:notreal).never value = Paperclip::Interpolations.interpolate(":notreal/:id/:attachment", :attachment, :style) assert_equal ":notreal/1234/attachments", value end