Skip to content
This repository has been archived by the owner on Jul 13, 2023. It is now read-only.

Commit

Permalink
Merge pull request #711 from maxigs/dynamic-fog-configuration
Browse files Browse the repository at this point in the history
Simple addition to allow procs in the fog_config_options (bucket-name, authentication, and host)
  • Loading branch information
sikachu committed Jan 27, 2012
2 parents 3737484 + 1c88a72 commit 5ffcc85
Show file tree
Hide file tree
Showing 2 changed files with 66 additions and 3 deletions.
21 changes: 18 additions & 3 deletions lib/paperclip/storage/fog.rb
Expand Up @@ -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'
Expand Down Expand Up @@ -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

Expand All @@ -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
Expand Down
48 changes: 48 additions & 0 deletions test/storage/fog_test.rb
Expand Up @@ -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
Expand Down

0 comments on commit 5ffcc85

Please sign in to comment.