Skip to content
Browse files

Fixing asset_host numbering, adding asset_host_count config option

  • Loading branch information...
1 parent c9557c8 commit 26a5146735967ca2da2d208de1d511b4c905e68f @winton committed Dec 7, 2010
Showing with 105 additions and 22 deletions.
  1. +26 −9 lib/smart_asset.rb
  2. +2 −2 lib/smart_asset/helper.rb
  3. +2 −1 spec/fixtures/assets.yml
  4. +75 −10 spec/smart_asset_spec.rb
View
35 lib/smart_asset.rb
@@ -14,7 +14,7 @@
class SmartAsset
class <<self
- attr_accessor :asset_host, :cache, :config, :dest, :env, :envs, :pub, :root, :sources
+ attr_accessor :asset_host, :asset_counter, :cache, :config, :dest, :env, :envs, :pub, :root, :sources
BIN = File.expand_path(File.dirname(__FILE__) + '/../bin')
CLOSURE_COMPILER = BIN + '/closure_compiler.jar'
@@ -84,6 +84,7 @@ def load_config(root, relative_config=nil)
@root = File.expand_path(root)
@config = YAML::load(File.read("#{@root}/#{relative_config}"))
+ @config['asset_host_count'] ||= 4
@config['asset_host'] ||= ActionController::Base.asset_host rescue nil
@config['environments'] ||= %w(production)
@config['public'] ||= 'public'
@@ -124,19 +125,14 @@ def paths(type, match)
return @cache[type][match]
end
- host =
- @asset_host.respond_to?(:keys) ?
- @asset_host[@env.to_s] :
- @asset_host
-
dest = @dest[type]
ext = ext_from_type type
if @envs.include?(@env.to_s)
match = match.gsub('/', '_')
@cache[type][match] =
if result = Dir["#{dest}/*_#{match}.#{ext}"].sort.last
- [ "#{host}#{result.gsub(@pub, '')}" ]
+ [ result.gsub(@pub, '') ]
else
[]
end
@@ -145,13 +141,13 @@ def paths(type, match)
if package.to_s == match
files.collect do |file|
file = "/#{@sources[type]}/#{file}.#{ext}"
- "#{host}#{file}" if File.exists?("#{@pub}/#{file}")
+ file if File.exists?("#{@pub}/#{file}")
end
elsif files
files.collect do |file|
if file.to_s == match
file = "/#{@sources[type]}/#{file}.#{ext}"
- "#{host}#{file}" if File.exists?("#{@pub}/#{file}")
+ file if File.exists?("#{@pub}/#{file}")
end
end
end
@@ -160,6 +156,27 @@ def paths(type, match)
end
end
+ def prepend_asset_host(path)
+ if @asset_host.respond_to?(:keys)
+ host = @asset_host[@env.to_s]
+ else
+ host = @asset_host
+ end
+
+ if host
+ if !@asset_counter || @asset_counter == @config['asset_host_count']
+ @asset_counter = 0
+ end
+
+ count = @asset_counter.to_s
+ @asset_counter += 1
+
+ host.gsub('%d', count) + path
+ else
+ path
+ end
+ end
+
private
def ext_from_type(type)
View
4 lib/smart_asset/helper.rb
@@ -3,14 +3,14 @@ module Helper
def javascript_include_merged(*javascripts)
output = javascript_paths(*javascripts).collect { |js|
- "<script src=\"#{js}\" type=\"text/javascript\"></script>"
+ "<script src=\"#{SmartAsset.prepend_asset_host js}\" type=\"text/javascript\"></script>"
}.join("\n")
defined?(Rails) && Rails.version[0..0] == '3' ? output.html_safe : output
end
def stylesheet_link_merged(*stylesheets)
output = stylesheet_paths(*stylesheets).collect { |css|
- "<link href=\"#{css}\" media=\"screen\" rel=\"Stylesheet\" type=\"text/css\" />"
+ "<link href=\"#{SmartAsset.prepend_asset_host css}\" media=\"screen\" rel=\"Stylesheet\" type=\"text/css\" />"
}.join("\n")
defined?(Rails) && Rails.version[0..0] == '3' ? output.html_safe : output
end
View
3 spec/fixtures/assets.yml
@@ -1,5 +1,6 @@
+asset_host_count: 2
asset_host:
- production: http://asset-host.com
+ production: http://assets%d.host.com
public: spec/fixtures/assets
destination:
javascripts: compressed
View
85 spec/smart_asset_spec.rb
@@ -2,6 +2,8 @@
unless FrameworkFixture.framework
describe SmartAsset do
+
+ include SmartAsset::Helper
before(:all) do
@config = "spec/fixtures/assets.yml"
@@ -19,7 +21,8 @@
it "should populate @config" do
SmartAsset.config.should == {
- "asset_host"=>{"production"=>"http://asset-host.com"},
+ "asset_host_count"=>2,
+ "asset_host"=>{"production"=>"http://assets%d.host.com"},
"destination"=>{"javascripts"=>"compressed", "stylesheets"=>"compressed"},
"environments"=>["production"],
"public"=>"spec/fixtures/assets",
@@ -35,7 +38,7 @@
end
it "should populate @asset_host" do
- SmartAsset.asset_host.should == {"production"=>"http://asset-host.com"}
+ SmartAsset.asset_host.should == {"production"=>"http://assets%d.host.com"}
end
it "should populate @dest" do
@@ -209,26 +212,88 @@
it "should return compressed paths" do
SmartAsset.paths('javascripts', :package).should == [
- "http://asset-host.com/compressed/20101130061253_package.js"
+ "/compressed/20101130061253_package.js"
]
SmartAsset.paths('javascripts', 'jquery/jquery').should == [
- "http://asset-host.com/compressed/20101130061253_package_jquery_jquery.js"
+ "/compressed/20101130061253_package_jquery_jquery.js"
]
SmartAsset.paths('stylesheets', :package).should == [
- "http://asset-host.com/compressed/20101130061253_package.css"
+ "/compressed/20101130061253_package.css"
]
SmartAsset.paths('stylesheets', 960).should == [
- "http://asset-host.com/compressed/20101128112833_package_960.css"
+ "/compressed/20101128112833_package_960.css"
]
end
it "should populate @cache" do
SmartAsset.cache.should == {"javascripts"=>
- {"package"=>["http://asset-host.com/compressed/20101130061253_package.js"],
- "jquery_jquery"=>["http://asset-host.com/compressed/20101130061253_package_jquery_jquery.js"]},
+ {"package"=>["/compressed/20101130061253_package.js"],
+ "jquery_jquery"=>["/compressed/20101130061253_package_jquery_jquery.js"]},
"stylesheets"=>
- {"package"=>["http://asset-host.com/compressed/20101130061253_package.css"],
- "960"=>["http://asset-host.com/compressed/20101128112833_package_960.css"]}}
+ {"package"=>["/compressed/20101130061253_package.css"],
+ "960"=>["/compressed/20101128112833_package_960.css"]}}
+ end
+ end
+ end
+
+ describe :helper do
+ describe "production" do
+
+ before(:all) do
+ SmartAsset.env = 'production'
+ SmartAsset.asset_counter = nil
+ end
+
+ before(:each) do
+ SmartAsset.cache = nil
+ end
+
+ it "should output correct script tags" do
+ javascript_include_merged(:package, 'jquery/jquery', :underscore, :unknown).split("\n").should == [
+ "<script src=\"http://assets0.host.com/compressed/20101130061253_package.js\" type=\"text/javascript\"></script>",
+ "<script src=\"http://assets1.host.com/compressed/20101130061253_package_jquery_jquery.js\" type=\"text/javascript\"></script>",
+ "<script src=\"http://assets0.host.com/compressed/20101128112833_package_underscore.js\" type=\"text/javascript\"></script>"
+ ]
+ end
+
+ it "should output correct style tags" do
+ stylesheet_link_merged(:package, 960, 'blueprint/blueprint', :unknown).split("\n").should == [
+ "<link href=\"http://assets1.host.com/compressed/20101130061253_package.css\" media=\"screen\" rel=\"Stylesheet\" type=\"text/css\" />",
+ "<link href=\"http://assets0.host.com/compressed/20101128112833_package_960.css\" media=\"screen\" rel=\"Stylesheet\" type=\"text/css\" />",
+ "<link href=\"http://assets1.host.com/compressed/20101130061253_package_blueprint_blueprint.css\" media=\"screen\" rel=\"Stylesheet\" type=\"text/css\" />"
+ ]
+ end
+ end
+
+ describe "development" do
+
+ before(:all) do
+ SmartAsset.env = 'development'
+ end
+
+ before(:each) do
+ SmartAsset.cache = nil
+ end
+
+ it "should output correct script tags for a package" do
+ javascript_include_merged(:package, :unknown).split("\n").should == [
+ "<script src=\"/javascripts/jquery/jquery.js\" type=\"text/javascript\"></script>",
+ "<script src=\"/javascripts/underscore.js\" type=\"text/javascript\"></script>"
+ ]
+ end
+
+ it "should output correct script tags for individual files" do
+ javascript_include_merged(:underscore, 'jquery/jquery', :unknown).split("\n").should == [
+ "<script src=\"/javascripts/underscore.js\" type=\"text/javascript\"></script>",
+ "<script src=\"/javascripts/jquery/jquery.js\" type=\"text/javascript\"></script>"
+ ]
+ end
+
+ it "should output correct style tags" do
+ stylesheet_link_merged(:package, 960, 'blueprint/blueprint', :unknown).split("\n").should == [
+ "<link href=\"/stylesheets/blueprint/blueprint.css\" media=\"screen\" rel=\"Stylesheet\" type=\"text/css\" />",
+ "<link href=\"/stylesheets/960.css\" media=\"screen\" rel=\"Stylesheet\" type=\"text/css\" />"
+ ]
end
end
end

0 comments on commit 26a5146

Please sign in to comment.
Something went wrong with that request. Please try again.