Skip to content

Commit

Permalink
Tweaked the interpolation matching so :id won't step on :id_partition.
Browse files Browse the repository at this point in the history
git-svn-id: https://svn.thoughtbot.com/plugins/paperclip/trunk@458 7bbfaf0e-4d1d-0410-9690-a8bb5f8ef2aa
  • Loading branch information
jyurek committed Apr 17, 2008
1 parent 61d8253 commit 96dc32b
Show file tree
Hide file tree
Showing 2 changed files with 21 additions and 3 deletions.
8 changes: 5 additions & 3 deletions lib/paperclip/attachment.rb
Expand Up @@ -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,
Expand All @@ -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 },
Expand Down Expand Up @@ -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
Expand Down
16 changes: 16 additions & 0 deletions test/test_attachment.rb
Expand Up @@ -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!({
Expand Down

0 comments on commit 96dc32b

Please sign in to comment.