Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
Browse files

A few bugfixes and features:

 * Pass (image) path to asset_host when rewriting css. Otherwise the %d parameter would be populated incorrectly.
 * Ability to insert prefix to paths to uncompressed files.
 * Ability to define environment specific credentials in s3.yml.
  • Loading branch information...
commit 19d633f99e7b4afba805400c6d5a9ccba8bf1c76 1 parent 4254f47
@zabojnik zabojnik authored committed
View
9 lib/cloudfront_asset_host.rb
@@ -34,6 +34,9 @@ module CloudfrontAssetHost
# Key-prefix under which to store gzipped assets
mattr_accessor :gzip_prefix
+ # Key-prefix under which to store plain (unzipped) assets
+ mattr_accessor :plain_prefix
+
# Which extensions are likely to occur in css files
mattr_accessor :image_extensions
@@ -61,6 +64,8 @@ def configure
self.gzip_extensions = %w(js css)
self.gzip_prefix = "gz"
+ self.plain_prefix = ""
+
self.image_extensions = %w(jpg jpeg gif png)
yield(self)
@@ -93,7 +98,11 @@ def asset_host(source = nil, request = nil)
if gzip_accepted && gzip_allowed
host << "/#{CloudfrontAssetHost.gzip_prefix}"
+ else
+ host << "/#{CloudfrontAssetHost.plain_prefix}" if CloudfrontAssetHost.plain_prefix.present?
end
+ else
+ host << "/#{CloudfrontAssetHost.plain_prefix}" if CloudfrontAssetHost.plain_prefix.present?
end
host
View
2  lib/cloudfront_asset_host/css_rewriter.rb
@@ -34,7 +34,7 @@ def rewrite_asset_link(asset_link, stylesheet_path)
if path.present? && File.exists?(path)
key = CloudfrontAssetHost.key_for_path(path) + path.gsub(Rails.public_path, '')
- "url(#{CloudfrontAssetHost.asset_host}/#{key})"
+ "url(#{CloudfrontAssetHost.asset_host(url)}/#{key})"
else
puts "Could not extract path: #{path}"
asset_link
View
12 lib/cloudfront_asset_host/uploader.rb
@@ -65,7 +65,8 @@ def rewritten_css_path(path)
def keys_with_paths
current_paths.inject({}) do |result, path|
- key = CloudfrontAssetHost.key_for_path(path) + path.gsub(Rails.public_path, '')
+ key = CloudfrontAssetHost.plain_prefix.present? ? "#{CloudfrontAssetHost.plain_prefix}/" : ""
+ key << CloudfrontAssetHost.key_for_path(path) + path.gsub(Rails.public_path, '')
result[key] = path
result
@@ -92,7 +93,9 @@ def rewrite_all_css?
def existing_keys
@existing_keys ||= begin
keys = []
- keys.concat bucket.keys('prefix' => CloudfrontAssetHost.key_prefix).map { |key| key.name }
+ prefix = CloudfrontAssetHost.key_prefix
+ prefix = "#{CloudfrontAssetHost.plain_prefix}/#{prefix}" if CloudfrontAssetHost.plain_prefix.present?
+ keys.concat bucket.keys('prefix' => prefix).map { |key| key.name }
keys.concat bucket.keys('prefix' => CloudfrontAssetHost.gzip_prefix).map { |key| key.name }
keys
end
@@ -131,7 +134,10 @@ def s3
end
def config
- @config ||= YAML::load_file(CloudfrontAssetHost.s3_config)
+ @config ||= begin
+ config = YAML::load_file(CloudfrontAssetHost.s3_config)
+ config.has_key?(Rails.env) ? config[Rails.env] : config
+ end
end
def asset_dirs
View
6 test/app/config/s3-env.yml
@@ -0,0 +1,6 @@
+#!/bin/bash
+production:
+ access_key_id: 'access_key'
+ secret_access_key: 'secret'
+ options:
+ use_ssl: true
View
2  test/app/config/s3.yml
@@ -2,4 +2,4 @@
access_key_id: 'access_key'
secret_access_key: 'secret'
options:
- use_ssl: true
+ use_ssl: true
View
6 test/cloudfront_asset_host_test.rb
@@ -60,6 +60,11 @@ class CloudfrontAssetHostTest < Test::Unit::TestCase
assert_equal "http://bucketname.s3.amazonaws.com", CloudfrontAssetHost.asset_host(@source)
end
+ should "add plain_prefix if present" do
+ CloudfrontAssetHost.plain_prefix = "prefix"
+ assert_equal "http://assethost.com/prefix", CloudfrontAssetHost.asset_host(@source)
+ end
+
context "when taking the headers into account" do
should "not support gzip for images" do
@@ -145,4 +150,5 @@ class CloudfrontAssetHostTest < Test::Unit::TestCase
end
assert_equal %w(custom), CloudfrontAssetHost.asset_dirs
end
+
end
View
6 test/test_helper.rb
@@ -2,7 +2,7 @@
require 'bundler'
Bundler.setup
-require 'activesupport'
+require 'active_support'
require 'action_controller'
require 'test/unit'
@@ -22,6 +22,10 @@ def root
def public_path
File.join(RAILS_ROOT, 'public')
end
+
+ def env
+ "production"
+ end
end
end
View
11 test/uploader_test.rb
@@ -21,6 +21,17 @@ class UploaderTest < Test::Unit::TestCase
assert_equal 'secret', config['secret_access_key']
end
+ should "be able to retrieve enviroment-specific config" do
+ CloudfrontAssetHost.configure do |config|
+ config.enabled = false
+ config.bucket = "bucketname"
+ config.s3_config = "#{RAILS_ROOT}/config/s3-env.yml"
+ end
+ config = CloudfrontAssetHost::Uploader.config
+ assert_equal 'access_key', config['access_key_id']
+ assert_equal 'secret', config['secret_access_key']
+ end
+
should "be able to instantiate s3-interface" do
RightAws::S3.expects(:new).with('access_key', 'secret').returns(mock)
assert_not_nil CloudfrontAssetHost::Uploader.s3
Please sign in to comment.
Something went wrong with that request. Please try again.