Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP

Comparing changes

Choose two branches to see what's changed or to start a new pull request. If you need to, you can also compare across forks.

Open a pull request

Create a new pull request by comparing changes across two branches. If you need to, you can also compare across forks.
base fork: thoughtbot/paperclip
...
head fork: liveline/paperclip
Checking mergeability… Don't worry, you can still create the pull request.
  • 3 commits
  • 4 files changed
  • 0 commit comments
  • 1 contributor
Commits on Apr 07, 2011
@nickgrim nickgrim Whitespace cleanup. da3cea0
@nickgrim nickgrim Added an additional test around disabling post-processing. a8a236b
@nickgrim nickgrim Enable loading all S3-config from a file, not just credentials.
Added :s3_config config-parameter to override :s3_credentials.
Refactored #{find,parse}_credentials to #{find,parse}_config.
Updated StorageTest.
4da13be
View
39 lib/paperclip/storage/s3.rb
@@ -66,21 +66,22 @@ def self.extended base
end unless defined?(AWS::S3)
base.instance_eval do
- @s3_credentials = parse_credentials(@options[:s3_credentials])
- @bucket = @options[:bucket] || @s3_credentials[:bucket]
+ @s3_config = parse_config(@options[:s3_config] || @options[:s3_credentials])
+ @bucket = @options[:bucket] || @s3_config[:bucket]
@bucket = @bucket.call(self) if @bucket.is_a?(Proc)
- @s3_options = @options[:s3_options] || {}
- @s3_permissions = @options[:s3_permissions] || :public_read
- @s3_protocol = @options[:s3_protocol] || (@s3_permissions == :public_read ? 'http' : 'https')
- @s3_headers = @options[:s3_headers] || {}
- @s3_host_alias = @options[:s3_host_alias]
+ @s3_options = @options[:s3_options] || @s3_config[:options] || {}
+ @s3_options = @s3_options.symbolize_keys
+ @s3_permissions = @options[:s3_permissions] || @s3_config[:permissions] || :public_read
+ @s3_protocol = @options[:s3_protocol] || @s3_config[:protocol] || (@s3_permissions == :public_read ? 'http' : 'https')
+ @s3_headers = @options[:s3_headers] || @s3_config[:headers] || {}
+ @s3_host_alias = @options[:s3_host_alias] || @s3_config[:host_alias]
unless @url.to_s.match(/^:s3.*url$/)
@path = @path.gsub(/:url/, @url)
@url = ":s3_path_url"
end
AWS::S3::Base.establish_connection!( @s3_options.merge(
- :access_key_id => @s3_credentials[:access_key_id],
- :secret_access_key => @s3_credentials[:secret_access_key]
+ :access_key_id => @s3_config[:access_key_id],
+ :secret_access_key => @s3_config[:secret_access_key]
))
end
Paperclip.interpolates(:s3_alias_url) do |attachment, style|
@@ -106,9 +107,9 @@ def s3_host_alias
@s3_host_alias
end
- def parse_credentials creds
- creds = find_credentials(creds).stringify_keys
- (creds[Rails.env] || creds).symbolize_keys
+ def parse_config config
+ config = find_config(config).stringify_keys
+ (config[Rails.env] || config).symbolize_keys
end
def exists?(style = default_style)
@@ -173,19 +174,19 @@ def flush_deletes #:nodoc:
@queued_for_delete = []
end
- def find_credentials creds
- case creds
+ def find_config config
+ case config
when File
- YAML::load(ERB.new(File.read(creds.path)).result)
+ YAML::load(ERB.new(File.read(config.path)).result)
when String, Pathname
- YAML::load(ERB.new(File.read(creds)).result)
+ YAML::load(ERB.new(File.read(config)).result)
when Hash
- creds
+ config
else
- raise ArgumentError, "Credentials are not a path, file, or hash."
+ raise ArgumentError, "Config is not a path, file, or hash."
end
end
- private :find_credentials
+ private :find_config
end
end
View
7 test/fixtures/s3.yml
@@ -6,3 +6,10 @@ test:
bucket: <%= ENV['S3_BUCKET'] %>
access_key_id: <%= ENV['S3_KEY'] %>
secret_access_key: <%= ENV['S3_SECRET'] %>
+ options:
+ server: test.example.com
+ permissions: <%= ENV['S3_PERMISSIONS'] %>
+ protocol: <%= ENV['S3_PROTOCOL'] %>
+ headers:
+ X-Testing: test
+ host_alias: <%= ENV['S3_HOST_ALIAS'] %>
View
29 test/integration_test.rb
@@ -34,20 +34,20 @@ class IntegrationTest < Test::Unit::TestCase
should "create its thumbnails properly" do
assert_match /\b50x50\b/, `identify "#{@dummy.avatar.path(:thumb)}"`
end
-
+
context 'reprocessing with unreadable original' do
setup { File.chmod(0000, @dummy.avatar.path) }
-
+
should "not raise an error" do
assert_nothing_raised do
@dummy.avatar.reprocess!
end
end
-
+
should "return false" do
assert ! @dummy.avatar.reprocess!
end
-
+
teardown { File.chmod(0644, @dummy.avatar.path) }
end
@@ -83,11 +83,21 @@ class IntegrationTest < Test::Unit::TestCase
teardown { @file.close }
- should "not create the thumbnails upon saving when post-processing is disabled" do
- @dummy.avatar.post_processing = false
- @dummy.avatar = @file
- assert @dummy.save
- assert !File.exists?(@thumb_path)
+ context "when post-processing is disabled" do
+ setup do
+ @dummy.avatar.post_processing = false
+ @dummy.avatar = @file
+ assert @dummy.save
+ end
+
+ should "not create the thumbnails upon save" do
+ assert !File.exists?(@thumb_path)
+ end
+
+ should "create the thumbnails when reprocess! is called" do
+ @dummy.avatar.reprocess!
+ assert File.exists?(@thumb_path)
+ end
end
should "create the thumbnails upon saving when post_processing is enabled" do
@@ -567,4 +577,3 @@ def s3_headers_for attachment, style
end
end
end
-
View
79 test/storage_test.rb
@@ -7,7 +7,7 @@ def rails_env(env)
Object.const_set(:Rails, stub('Rails', :env => env))
end
end
-
+
context "filesystem" do
setup do
rebuild_model :styles => { :thumbnail => "25x25#" }
@@ -15,23 +15,23 @@ def rails_env(env)
@dummy.avatar = File.open(File.join(File.dirname(__FILE__), "fixtures", "5k.png"))
end
-
+
should "allow file assignment" do
assert @dummy.save
end
-
+
should "store the original" do
@dummy.save
assert File.exists?(@dummy.avatar.path)
end
-
+
should "store the thumbnail" do
@dummy.save
assert File.exists?(@dummy.avatar.path(:thumbnail))
end
end
- context "Parsing S3 credentials" do
+ context "Parsing S3 config" do
setup do
AWS::S3::Base.stubs(:establish_connection!)
rebuild_model :storage => :s3,
@@ -42,23 +42,66 @@ def rails_env(env)
@avatar = @dummy.avatar
end
- should "get the correct credentials when RAILS_ENV is production" do
+ should "get the correct config when RAILS_ENV is production" do
rails_env("production")
assert_equal({:key => "12345"},
- @avatar.parse_credentials('production' => {:key => '12345'},
+ @avatar.parse_config('production' => {:key => '12345'},
:development => {:key => "54321"}))
end
- should "get the correct credentials when RAILS_ENV is development" do
+ should "get the correct config when RAILS_ENV is development" do
rails_env("development")
assert_equal({:key => "54321"},
- @avatar.parse_credentials('production' => {:key => '12345'},
+ @avatar.parse_config('production' => {:key => '12345'},
:development => {:key => "54321"}))
end
should "return the argument if the key does not exist" do
rails_env("not really an env")
- assert_equal({:test => "12345"}, @avatar.parse_credentials(:test => "12345"))
+ assert_equal({:test => "12345"}, @avatar.parse_config(:test => "12345"))
+ end
+ end
+
+ context "When locating which config to use" do
+ setup do
+ AWS::S3::Base.stubs(:establish_connection!)
+ end
+
+ context "if only :s3_config is supplied" do
+ setup do
+ rebuild_class :storage => :s3,
+ :s3_config => {:bucket => 's3_config'}
+ @dummy = Dummy.new
+ end
+
+ should "use :s3_config for configuration" do
+ assert_equal "s3_config", @dummy.avatar.bucket_name
+ end
+ end
+
+ context "if only :s3_credentials is supplied" do
+ setup do
+ rebuild_class :storage => :s3,
+ :s3_credentials => {:bucket => 's3_credentials'}
+ @dummy = Dummy.new
+ end
+
+ should "use :s3_credentials for configuration" do
+ assert_equal "s3_credentials", @dummy.avatar.bucket_name
+ end
+ end
+
+ context "if both :s3_config and :s3_credentials are supplied" do
+ setup do
+ rebuild_class :storage => :s3,
+ :s3_config => {:bucket => 's3_config'},
+ :s3_credentials => {:bucket => 's3_credentials'}
+ @dummy = Dummy.new
+ end
+
+ should "use :s3_config for configuration" do
+ assert_equal "s3_config", @dummy.avatar.bucket_name
+ end
end
end
@@ -316,9 +359,12 @@ class AWS::S3::NoSuchBucket < AWS::S3::ResponseError
context "with S3 credentials in a YAML file" do
setup do
- ENV['S3_KEY'] = 'env_key'
- ENV['S3_BUCKET'] = 'env_bucket'
- ENV['S3_SECRET'] = 'env_secret'
+ ENV['S3_KEY'] = 'env_key'
+ ENV['S3_BUCKET'] = 'env_bucket'
+ ENV['S3_SECRET'] = 'env_secret'
+ ENV['S3_PERMISSIONS'] = 'env_permissions'
+ ENV['S3_PROTOCOL'] = 'env_protocol'
+ ENV['S3_HOST_ALIAS'] = 'env_host_alias'
rails_env('test')
@@ -328,12 +374,17 @@ class AWS::S3::NoSuchBucket < AWS::S3::ResponseError
Dummy.delete_all
@dummy = Dummy.new
+ @avatar = @dummy.avatar
end
- should "run it the file through ERB" do
+ should "run the file through ERB" do
assert_equal 'env_bucket', @dummy.avatar.bucket_name
assert_equal 'env_key', AWS::S3::Base.connection.options[:access_key_id]
assert_equal 'env_secret', AWS::S3::Base.connection.options[:secret_access_key]
+ assert_equal 'test.example.com', AWS::S3::Base.connection.options[:server]
+ assert_equal 'env_permissions', @dummy.avatar.instance_variable_get('@s3_permissions')
+ assert_equal 'env_protocol', @dummy.avatar.s3_protocol
+ assert_equal 'env_host_alias', @dummy.avatar.s3_host_alias
end
end

No commit comments for this range

Something went wrong with that request. Please try again.