Skip to content

Commit

Permalink
allow proc for fog_directory so its possible to call model methods to…
Browse files Browse the repository at this point in the history
… set the directory
  • Loading branch information
Benjamin Hüttinger committed Jan 20, 2012
1 parent 65e0338 commit e049ec5
Show file tree
Hide file tree
Showing 2 changed files with 22 additions and 1 deletion.
8 changes: 7 additions & 1 deletion lib/paperclip/storage/fog.rb
Expand Up @@ -168,7 +168,13 @@ def connection
end

def directory
@directory ||= connection.directories.new(:key => @options[:fog_directory])
dir = if @options[:fog_directory].is_a?(Proc)
@options[:fog_directory].call(self)
else
@options[:fog_directory]
end

@directory ||= connection.directories.new(:key => dir)
end
end
end
Expand Down
15 changes: 15 additions & 0 deletions test/storage/fog_test.rb
Expand Up @@ -220,6 +220,21 @@ 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
end

end
Expand Down

0 comments on commit e049ec5

Please sign in to comment.