Skip to content

Commit

Permalink
Change the default :url and :path to avoid conflicts. Closes thoughtb…
Browse files Browse the repository at this point in the history
…ot#727.

The new default :path and :include include the name of the model and
also nests the model ID under a series of subdirectories, improving
filesystem access speed when more than 1024 models have saved
attachments.

The easiest way to upgrade is to add an explicit :url and :path to your
has_attached_file calls:

    has_attached_file :avatar,
      :path => ":rails_root/public/system/:attachment/:id/:style/:filename",
      :url => "/system/:attachment/:id/:style/:filename"
  • Loading branch information
mike-burns committed Mar 9, 2012
1 parent 06d69af commit 26f4d40
Show file tree
Hide file tree
Showing 7 changed files with 30 additions and 10 deletions.
10 changes: 10 additions & 0 deletions NEWS
Original file line number Diff line number Diff line change
@@ -1,3 +1,13 @@
* API CHANGE: The default :url and :path have changed. The new scheme avoids
filesystem conflicts and scales to handle larger numbers of uploads.

The easiest way to upgrade is to add an explicit :url and :path to your
has_attached_file calls:

has_attached_file :avatar,
:path => ":rails_root/public/system/:attachment/:id/:style/:filename",
:url => "/system/:attachment/:id/:style/:filename"

New in 2.7.0:

* Bug fix: Checking the existence of a file on S3 handles all AWS errors.
Expand Down
4 changes: 2 additions & 2 deletions features/basic_integration.feature
Original file line number Diff line number Diff line change
Expand Up @@ -19,8 +19,8 @@ Feature: Rails integration
And I attach the file "test/fixtures/5k.png" to "Attachment"
And I press "Submit"
Then I should see "Name: something"
And I should see an image with a path of "/system/attachments/1/original/5k.png"
And the file at "/system/attachments/1/original/5k.png" should be the same as "test/fixtures/5k.png"
And I should see an image with a path of "/system/users/attachments/000/000/001/original/5k.png"
And the file at "/system/users/attachments/000/000/001/original/5k.png" should be the same as "test/fixtures/5k.png"

Scenario: S3 Integration test
Given I add this snippet to the User model:
Expand Down
2 changes: 1 addition & 1 deletion lib/paperclip/attachment.rb
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ def self.default_options
:source_file_options => {},
:storage => :filesystem,
:styles => {},
:url => "/system/:attachment/:id/:style/:filename",
:url => "/system/:class/:attachment/:id_partition/:style/:filename",
:url_generator => Paperclip::UrlGenerator,
:use_default_time_zone => true,
:use_timestamp => true,
Expand Down
9 changes: 9 additions & 0 deletions test/attachment_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -103,6 +103,15 @@ class AttachmentTest < Test::Unit::TestCase
assert_equal "#{Rails.root}/public/fake_models/1234/fake", @attachment.path
end

should "default to a path that scales" do
avatar_attachment = attachment
model = avatar_attachment.instance
model.id = 1234
model.avatar_file_name = "fake.jpg"
expected_path = "#{Rails.root}/public/system/fake_models/avatars/000/001/234/original/fake.jpg"
assert_equal expected_path, avatar_attachment.path
end

context "Attachment default_options" do
setup do
rebuild_model
Expand Down
2 changes: 1 addition & 1 deletion test/helper.rb
Original file line number Diff line number Diff line change
Expand Up @@ -121,7 +121,7 @@ def run_paperclip_callbacks name, *args

end

def attachment options
def attachment(options={})
Paperclip::Attachment.new(:avatar, FakeModel.new, options)
end

Expand Down
6 changes: 3 additions & 3 deletions test/integration_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -90,7 +90,7 @@ class IntegrationTest < Test::Unit::TestCase

context "Attachment" do
setup do
@thumb_path = "./test/../public/system/avatars/1/thumb/5k.png"
@thumb_path = "./test/../public/system/dummies/avatars/000/000/001/thumb/5k.png"
File.delete(@thumb_path) if File.exists?(@thumb_path)
rebuild_model :styles => { :thumb => "50x50#" }
@dummy = Dummy.new
Expand Down Expand Up @@ -119,8 +119,8 @@ class IntegrationTest < Test::Unit::TestCase

context "Attachment with no generated thumbnails" do
setup do
@thumb_small_path = "./test/../public/system/avatars/1/thumb_small/5k.png"
@thumb_large_path = "./test/../public/system/avatars/1/thumb_large/5k.png"
@thumb_small_path = "./test/../public/system/dummies/avatars/000/000/001/thumb_small/5k.png"
@thumb_large_path = "./test/../public/system/dummies/avatars/000/000/001/thumb_large/5k.png"
File.delete(@thumb_small_path) if File.exists?(@thumb_small_path)
File.delete(@thumb_large_path) if File.exists?(@thumb_large_path)
rebuild_model :styles => { :thumb_small => "50x50#", :thumb_large => "60x60#" }
Expand Down
7 changes: 4 additions & 3 deletions test/paperclip_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -87,12 +87,13 @@ class Dummy2 < ActiveRecord::Base
end

should "generate warning if attachment is redefined with the same url string" do
Paperclip.expects(:log).with("Duplicate URL for blah with /system/:attachment/:id/:style/:filename. This will clash with attachment defined in Dummy class")
expected_log_msg = "Duplicate URL for blah with /system/:id/:style/:filename. This will clash with attachment defined in Dummy class"
Paperclip.expects(:log).with(expected_log_msg)
Dummy.class_eval do
has_attached_file :blah
has_attached_file :blah, :url => '/system/:id/:style/:filename'
end
Dummy2.class_eval do
has_attached_file :blah
has_attached_file :blah, :url => '/system/:id/:style/:filename'
end
end

Expand Down

0 comments on commit 26f4d40

Please sign in to comment.