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

Commit

Permalink
Implemented performance improvement to interpolator
Browse files Browse the repository at this point in the history
  • Loading branch information
phene authored and Jon Yurek committed Nov 27, 2012
1 parent c0c1f1e commit 68aea16
Show file tree
Hide file tree
Showing 3 changed files with 5 additions and 9 deletions.
9 changes: 3 additions & 6 deletions lib/paperclip/interpolations.rb
Expand Up @@ -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

Expand Down
4 changes: 2 additions & 2 deletions test/attachment_test.rb
Expand Up @@ -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

Expand Down
1 change: 0 additions & 1 deletion test/interpolations_test.rb
Expand Up @@ -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
Expand Down

0 comments on commit 68aea16

Please sign in to comment.