Commit f033d952 by Mikel Lindsaar Committed by Prem Sichanugrist

Allow :s3_host_alias to be a proc for dynamic cdn generation

parent 4e6fac4c
......@@ -76,6 +76,7 @@ module Paperclip
@s3_protocol = @options[:s3_protocol] || (@s3_permissions == :public_read ? 'http' : 'https')
@s3_headers = @options[:s3_headers] || {}
@s3_host_alias = @options[:s3_host_alias]
@s3_host_alias = @s3_host_alias.call(self) if @s3_host_alias.is_a?(Proc)
unless @url.to_s.match(/^:s3.*url$/)
@path = @path.gsub(/:url/, @url)
@url = ":s3_path_url"
......
......@@ -141,6 +141,28 @@ class StorageTest < Test::Unit::TestCase
end
end
context "generating a url with a proc as the host alias" do
setup do
AWS::S3::Base.stubs(:establish_connection!)
rebuild_model :storage => :s3,
:s3_credentials => { :bucket => "prod_bucket" },
:s3_host_alias => Proc.new { |image| "cdn#{image.size.to_i % 4}.example.com" },
:path => ":attachment/:basename.:extension",
:url => ":s3_alias_url"
@dummy = Dummy.new
@dummy.avatar = StringIO.new(".")
end
should "return a url based on the host_alias" do
assert_match %r{^http://cdn0.example.com/avatars/stringio.txt}, @dummy.avatar.url
end
should "still return the bucket name" do
assert_equal "prod_bucket", @dummy.avatar.bucket_name
end
end
context "" do
setup do
AWS::S3::Base.stubs(:establish_connection!)
......
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