Commit 26f4d409 by Mike Burns

Change the default :url and :path to avoid conflicts. Closes #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"
parent 06d69afb
* 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: New in 2.7.0:
* Bug fix: Checking the existence of a file on S3 handles all AWS errors. * Bug fix: Checking the existence of a file on S3 handles all AWS errors.
......
...@@ -19,8 +19,8 @@ Feature: Rails integration ...@@ -19,8 +19,8 @@ Feature: Rails integration
And I attach the file "test/fixtures/5k.png" to "Attachment" And I attach the file "test/fixtures/5k.png" to "Attachment"
And I press "Submit" And I press "Submit"
Then I should see "Name: something" Then I should see "Name: something"
And I should see an image with a path of "/system/attachments/1/original/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/attachments/1/original/5k.png" should be the same as "test/fixtures/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 Scenario: S3 Integration test
Given I add this snippet to the User model: Given I add this snippet to the User model:
......
...@@ -25,7 +25,7 @@ module Paperclip ...@@ -25,7 +25,7 @@ module Paperclip
:source_file_options => {}, :source_file_options => {},
:storage => :filesystem, :storage => :filesystem,
:styles => {}, :styles => {},
:url => "/system/:attachment/:id/:style/:filename", :url => "/system/:class/:attachment/:id_partition/:style/:filename",
:url_generator => Paperclip::UrlGenerator, :url_generator => Paperclip::UrlGenerator,
:use_default_time_zone => true, :use_default_time_zone => true,
:use_timestamp => true, :use_timestamp => true,
......
...@@ -103,6 +103,15 @@ class AttachmentTest < Test::Unit::TestCase ...@@ -103,6 +103,15 @@ class AttachmentTest < Test::Unit::TestCase
assert_equal "#{Rails.root}/public/fake_models/1234/fake", @attachment.path assert_equal "#{Rails.root}/public/fake_models/1234/fake", @attachment.path
end 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 context "Attachment default_options" do
setup do setup do
rebuild_model rebuild_model
......
...@@ -121,7 +121,7 @@ class FakeModel ...@@ -121,7 +121,7 @@ class FakeModel
end end
def attachment options def attachment(options={})
Paperclip::Attachment.new(:avatar, FakeModel.new, options) Paperclip::Attachment.new(:avatar, FakeModel.new, options)
end end
......
...@@ -90,7 +90,7 @@ class IntegrationTest < Test::Unit::TestCase ...@@ -90,7 +90,7 @@ class IntegrationTest < Test::Unit::TestCase
context "Attachment" do context "Attachment" do
setup 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) File.delete(@thumb_path) if File.exists?(@thumb_path)
rebuild_model :styles => { :thumb => "50x50#" } rebuild_model :styles => { :thumb => "50x50#" }
@dummy = Dummy.new @dummy = Dummy.new
...@@ -119,8 +119,8 @@ class IntegrationTest < Test::Unit::TestCase ...@@ -119,8 +119,8 @@ class IntegrationTest < Test::Unit::TestCase
context "Attachment with no generated thumbnails" do context "Attachment with no generated thumbnails" do
setup do setup do
@thumb_small_path = "./test/../public/system/avatars/1/thumb_small/5k.png" @thumb_small_path = "./test/../public/system/dummies/avatars/000/000/001/thumb_small/5k.png"
@thumb_large_path = "./test/../public/system/avatars/1/thumb_large/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_small_path) if File.exists?(@thumb_small_path)
File.delete(@thumb_large_path) if File.exists?(@thumb_large_path) File.delete(@thumb_large_path) if File.exists?(@thumb_large_path)
rebuild_model :styles => { :thumb_small => "50x50#", :thumb_large => "60x60#" } rebuild_model :styles => { :thumb_small => "50x50#", :thumb_large => "60x60#" }
......
...@@ -87,12 +87,13 @@ class PaperclipTest < Test::Unit::TestCase ...@@ -87,12 +87,13 @@ class PaperclipTest < Test::Unit::TestCase
end end
should "generate warning if attachment is redefined with the same url string" do 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 Dummy.class_eval do
has_attached_file :blah has_attached_file :blah, :url => '/system/:id/:style/:filename'
end end
Dummy2.class_eval do Dummy2.class_eval do
has_attached_file :blah has_attached_file :blah, :url => '/system/:id/:style/:filename'
end end
end end
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment