Commit 5ffcc855 by Prem Sichanugrist

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

Simple addition to allow procs in the fog_config_options (bucket-name, authentication, and host)
parents 37374842 1c88a725
...@@ -126,7 +126,12 @@ module Paperclip ...@@ -126,7 +126,12 @@ module Paperclip
def public_url(style = default_style) def public_url(style = default_style)
if @options[:fog_host] 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)}" "#{host}/#{path(style)}"
else else
if fog_credentials[:provider] == 'AWS' if fog_credentials[:provider] == 'AWS'
...@@ -159,7 +164,11 @@ module Paperclip ...@@ -159,7 +164,11 @@ module Paperclip
when Hash when Hash
creds creds
else 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
end end
...@@ -168,7 +177,13 @@ module Paperclip ...@@ -168,7 +177,13 @@ module Paperclip
end end
def directory 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 end
end end
......
...@@ -220,6 +220,54 @@ class FogTest < Test::Unit::TestCase ...@@ -220,6 +220,54 @@ class FogTest < Test::Unit::TestCase
end 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
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