Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
Browse files

Merge pull request #711 from maxigs/dynamic-fog-configuration

Simple addition to allow procs in the fog_config_options (bucket-name, authentication, and host)
  • Loading branch information...
commit 5ffcc855d5baed2278af437d60adccf2cc759512 2 parents 3737484 + 1c88a72
@sikachu sikachu authored
Showing with 66 additions and 3 deletions.
  1. +18 −3 lib/paperclip/storage/fog.rb
  2. +48 −0 test/storage/fog_test.rb
View
21 lib/paperclip/storage/fog.rb
@@ -126,7 +126,12 @@ def to_file(style = default_style)
def public_url(style = default_style)
if @options[:fog_host]
- host = (@options[:fog_host] =~ /%d/) ? @options[:fog_host] % (path(style).hash % 4) : @options[:fog_host]
+ host = if @options[:fog_host].respond_to?(:call)
+ @options[:fog_host].call(self)
+ else
+ (@options[:fog_host] =~ /%d/) ? @options[:fog_host] % (path(style).hash % 4) : @options[:fog_host]
+ end
+
"#{host}/#{path(style)}"
else
if fog_credentials[:provider] == 'AWS'
@@ -159,7 +164,11 @@ def find_credentials(creds)
when Hash
creds
else
- raise ArgumentError, "Credentials are not a path, file, or hash."
+ if creds.respond_to?(:call)
+ creds.call(self)
+ else
+ raise ArgumentError, "Credentials are not a path, file, hash or proc."
+ end
end
end
@@ -168,7 +177,13 @@ def connection
end
def directory
- @directory ||= connection.directories.new(:key => @options[:fog_directory])
+ dir = if @options[:fog_directory].respond_to?(:call)
+ @options[:fog_directory].call(self)
+ else
+ @options[:fog_directory]
+ end
+
+ @directory ||= connection.directories.new(:key => dir)
end
end
end
View
48 test/storage/fog_test.rb
@@ -220,6 +220,54 @@ class FogTest < Test::Unit::TestCase
end
+ context "with a proc for a bucket name evaluating a model method" do
+ setup do
+ @dynamic_fog_directory = 'dynamicpaperclip'
+ rebuild_model(@options.merge(:fog_directory => lambda { |attachment| attachment.instance.bucket_name }))
+ @dummy = Dummy.new
+ @dummy.stubs(:bucket_name).returns(@dynamic_fog_directory)
+ @dummy.avatar = @file
+ @dummy.save
+ end
+
+ should "have created the bucket" do
+ assert @connection.directories.get(@dynamic_fog_directory).inspect
+ end
+
+ end
+
+ context "with a proc for the fog_host evaluating a model method" do
+ setup do
+ rebuild_model(@options.merge(:fog_host => lambda { |attachment| attachment.instance.fog_host }))
+ @dummy = Dummy.new
+ @dummy.stubs(:fog_host).returns('http://dynamicfoghost.com')
+ @dummy.avatar = @file
+ @dummy.save
+ end
+
+ should "provide a public url" do
+ assert_match /http:\/\/dynamicfoghost\.com/, @dummy.avatar.url
+ end
+ end
+
+ context "with a proc for the fog_credentials evaluating a model method" do
+ setup do
+ @dynamic_fog_credentials = {
+ :provider => 'AWS',
+ :aws_access_key_id => 'DYNAMIC_ID',
+ :aws_secret_access_key => 'DYNAMIC_SECRET'
+ }
+ rebuild_model(@options.merge(:fog_credentials => lambda { |attachment| attachment.instance.fog_credentials }))
+ @dummy = Dummy.new
+ @dummy.stubs(:fog_credentials).returns(@dynamic_fog_credentials)
+ @dummy.avatar = @file
+ @dummy.save
+ end
+
+ should "provide a public url" do
+ assert_equal @dummy.avatar.fog_credentials, @dynamic_fog_credentials
+ end
+ end
end
end
Please sign in to comment.
Something went wrong with that request. Please try again.