Permalink
Browse files

Added support for S3 bucket being defined as a Proc and therefore det…

…ermined at runtime.

(cherry picked from commit 74de215ea28628af94b2c4a0d1b005395247fdc1)
  • Loading branch information...
szajbus authored and jyurek committed Feb 3, 2009
1 parent 8778382 commit 044f00eccba1c3314e904a2adcf293ba72b56a3d
Showing with 16 additions and 0 deletions.
  1. +3 −0 lib/paperclip/storage.rb
  2. +13 −0 test/storage_test.rb
View
@@ -106,6 +106,8 @@ def flush_deletes #:nodoc:
# * +bucket+: This is the name of the S3 bucket that will store your files. Remember
# that the bucket must be unique across all of Amazon S3. If the bucket does not exist
# Paperclip will attempt to create it. The bucket name will not be interpolated.
+ # You can define the bucket as a Proc if you want to determine it's name at runtime.
+ # Paperclip will call that Proc with attachment as the only argument.
# * +url+: There are two options for the S3 url. You can choose to have the bucket's name
# placed domain-style (bucket.s3.amazonaws.com) or path-style (s3.amazonaws.com/bucket).
# Normally, this won't matter in the slightest and you can leave the default (which is
@@ -122,6 +124,7 @@ def self.extended base
base.instance_eval do
@s3_credentials = parse_credentials(@options[:s3_credentials])
@bucket = @options[:bucket] || @s3_credentials[:bucket]
+ @bucket = @bucket.call(self) if @bucket.is_a?(Proc)
@s3_options = @options[:s3_options] || {}
@s3_permissions = @options[:s3_permissions] || 'public-read'
@s3_protocol = @options[:s3_protocol] || (@s3_permissions == 'public-read' ? 'http' : 'https')
View
@@ -126,6 +126,19 @@ class StorageTest < Test::Unit::TestCase
end
end
end
+
+ context "An attachment with S3 storage and bucket defined as a Proc" do
+ setup do
+ rebuild_model :storage => :s3,
+ :bucket => lambda { |attachment| "bucket_#{attachment.instance.other}" },
+ :s3_credentials => {:not => :important}
+ end
+
+ should "get the right bucket name" do
+ assert "bucket_a", Dummy.new(:other => 'a').avatar.bucket_name
+ assert "bucket_b", Dummy.new(:other => 'b').avatar.bucket_name
+ end
+ end
context "An attachment with S3 storage and specific s3 headers set" do
setup do

0 comments on commit 044f00e

Please sign in to comment.