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

Commit

Permalink
Revert "Revert "Handle interpolation of string IDs (supports mongo) a…
Browse files Browse the repository at this point in the history
…s well as integers""

I think it's OK to have the bugfix for MongoDB in the plugin.

This reverts commit cde2256.
  • Loading branch information
sikachu committed Aug 23, 2011
1 parent 6b6ce9e commit 7e1a923
Show file tree
Hide file tree
Showing 2 changed files with 13 additions and 2 deletions.
6 changes: 5 additions & 1 deletion lib/paperclip/interpolations.rb
Expand Up @@ -143,7 +143,11 @@ def hash attachment, style_name
# Returns the id of the instance in a split path form. e.g. returns
# 000/001/234 for an id of 1234.
def id_partition attachment, style_name
("%09d" % attachment.instance.id).scan(/\d{3}/).join("/")
if (id = attachment.instance.id).is_a?(Integer)
("%09d" % id).scan(/\d{3}/).join("/")
else
id.scan(/.{3}/).first(3).join("/")
end
end

# Returns the pluralized form of the attachment name. e.g.
Expand Down
9 changes: 8 additions & 1 deletion test/interpolations_test.rb
Expand Up @@ -88,13 +88,20 @@ class InterpolationsTest < Test::Unit::TestCase
assert_equal 23, Paperclip::Interpolations.id(attachment, :style)
end

should "return the partitioned id of the attachment" do
should "return the partitioned id of the attachment when the id is an integer" do
attachment = mock
attachment.expects(:id).returns(23)
attachment.expects(:instance).returns(attachment)
assert_equal "000/000/023", Paperclip::Interpolations.id_partition(attachment, :style)
end

should "return the partitioned id of the attachment when the id is a string" do
attachment = mock
attachment.expects(:id).returns("32fnj23oio2f")
attachment.expects(:instance).returns(attachment)
assert_equal "32f/nj2/3oi", Paperclip::Interpolations.id_partition(attachment, :style)
end

should "return the name of the attachment" do
attachment = mock
attachment.expects(:name).returns("file")
Expand Down

0 comments on commit 7e1a923

Please sign in to comment.