Permalink
Browse files

Tweaked the interpolation matching so :id won't step on :id_partition.

git-svn-id: https://svn.thoughtbot.com/plugins/paperclip/trunk@458 7bbfaf0e-4d1d-0410-9690-a8bb5f8ef2aa
  • Loading branch information...
1 parent 61d8253 commit 96dc32b2b4d1dadc936217f7367e44bccb00b0b6 jyurek committed Apr 17, 2008
Showing with 21 additions and 3 deletions.
  1. +5 −3 lib/paperclip/attachment.rb
  2. +16 −0 test/test_attachment.rb
@@ -148,7 +148,9 @@ def original_filename
def self.interpolations
@interpolations ||= {
:rails_root => lambda{|attachment,style| RAILS_ROOT },
- :class => lambda{|attachment,style| attachment.instance.class.to_s.downcase.pluralize },
+ :class => lambda do |attachment,style|
+ attachment.instance.class.to_s.downcase.pluralize
+ end,
:basename => lambda do |attachment,style|
attachment.original_filename.gsub(/\.(.*?)$/, "")
end,
@@ -157,7 +159,7 @@ def self.interpolations
File.extname(attachment.original_filename).gsub(/^\.+/, "")
end,
:id => lambda{|attachment,style| attachment.instance.id },
- :partition_id => lambda do |attachment, style|
+ :id_partition => lambda do |attachment, style|
("%09d" % attachment.instance.id).scan(/\d{3}/).join("/")
end,
:attachment => lambda{|attachment,style| attachment.name.to_s.downcase.pluralize },
@@ -215,7 +217,7 @@ def interpolate pattern, style = nil #:nodoc:
style ||= default_style
pattern = pattern.dup
self.class.interpolations.each do |tag, l|
- pattern.gsub!(/:#{tag}/) do |match|
+ pattern.gsub!(/:\b#{tag}\b/) do |match|
l.call( self, style )
end
end
View
@@ -60,6 +60,22 @@ class AttachmentTest < Test::Unit::TestCase
end
end
+ context "An attachment with similarly named interpolations" do
+ setup do
+ rebuild_model :path => ":id.omg/:id-bbq/:idwhat/:id_partition.wtf"
+ @dummy = Dummy.new
+ @dummy.stubs(:id).returns(1024)
+ @file = File.new(File.join(File.dirname(__FILE__),
+ "fixtures",
+ "5k.png"))
+ @dummy.avatar = @file
+ end
+
+ should "make sure that they are interpolated correctly" do
+ assert_equal "1024.omg/1024-bbq/:idwhat/000/001/024.wtf", @dummy.avatar.path
+ end
+ end
+
context "An attachment" do
setup do
Paperclip::Attachment.default_options.merge!({

0 comments on commit 96dc32b

Please sign in to comment.