Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Interpolating id_partition bug for new records #1

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
6 changes: 3 additions & 3 deletions lib/paperclip/interpolations.rb
Expand Up @@ -143,10 +143,10 @@ 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
if (id = attachment.instance.id).is_a?(Integer)
("%09d" % id).scan(/\d{3}/).join("/")
else
if (id = attachment.instance.id).is_a?(String)
id.scan(/.{3}/).first(3).join("/")
else
("%09d" % id).scan(/\d{3}/).join("/")
end
end

Expand Down
8 changes: 8 additions & 0 deletions test/interpolations_test.rb
Expand Up @@ -102,6 +102,14 @@ class InterpolationsTest < Test::Unit::TestCase
assert_equal "32f/nj2/3oi", Paperclip::Interpolations.id_partition(attachment, :style)
end

should "return the fake partitioned id of the attachment when the id is nil for new record" do
attachment = mock
attachment.expects(:id).returns(nil)
attachment.expects(:instance).returns(attachment)
assert_equal "000/000/000", Paperclip::Interpolations.id_partition(attachment, :style)
end


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