Sinatra 1.0 based prerelease
This commit is contained in:
parent
ce6deb4c9f
commit
af8f1c4f26
12
features/asset_host.feature
Normal file
12
features/asset_host.feature
Normal file
|
@ -0,0 +1,12 @@
|
||||||
|
Feature: Alternate between multiple asset hosts
|
||||||
|
In order to speed up page loading
|
||||||
|
|
||||||
|
Scenario: Rendering css with the feature enabled
|
||||||
|
Given I am using an asset host
|
||||||
|
When I go to "/stylesheets/asset_host.css"
|
||||||
|
Then I should see "http://assets"
|
||||||
|
|
||||||
|
Scenario: Rendering html with the feature enabled
|
||||||
|
Given I am using an asset host
|
||||||
|
When I go to "/asset_host.html"
|
||||||
|
Then I should see "http://assets"
|
6
features/step_definitions/asset_host_steps.rb
Normal file
6
features/step_definitions/asset_host_steps.rb
Normal file
|
@ -0,0 +1,6 @@
|
||||||
|
Given /^I am using an asset host$/ do
|
||||||
|
Middleman::Base.set :asset_host do |asset|
|
||||||
|
"http://assets%d.example.com" % (asset.hash % 4)
|
||||||
|
end
|
||||||
|
@browser = Rack::Test::Session.new(Rack::MockSession.new(Middleman::Base.new))
|
||||||
|
end
|
|
@ -39,6 +39,13 @@ module Middleman
|
||||||
super
|
super
|
||||||
end
|
end
|
||||||
|
|
||||||
|
def self.set(option, value=self, &block)
|
||||||
|
if block_given?
|
||||||
|
value = Proc.new { block }
|
||||||
|
end
|
||||||
|
super(option, value)
|
||||||
|
end
|
||||||
|
|
||||||
@@afters = []
|
@@afters = []
|
||||||
def self.after_feature_init(&block)
|
def self.after_feature_init(&block)
|
||||||
@@afters << block
|
@@afters << block
|
||||||
|
@ -134,6 +141,7 @@ class Middleman::Base
|
||||||
helpers Middleman::Helpers
|
helpers Middleman::Helpers
|
||||||
|
|
||||||
# Features disabled by default
|
# Features disabled by default
|
||||||
|
enable :asset_host
|
||||||
disable :slickmap
|
disable :slickmap
|
||||||
disable :cache_buster
|
disable :cache_buster
|
||||||
disable :minify_css
|
disable :minify_css
|
||||||
|
|
28
lib/middleman/features/asset_host.rb
Normal file
28
lib/middleman/features/asset_host.rb
Normal file
|
@ -0,0 +1,28 @@
|
||||||
|
class Middleman::Base
|
||||||
|
after_feature_init do
|
||||||
|
::Compass.configuration do |config|
|
||||||
|
config.asset_host(&self.asset_host)
|
||||||
|
end
|
||||||
|
end if self.enabled?(:asset_host)
|
||||||
|
end
|
||||||
|
|
||||||
|
class << Middleman::Base
|
||||||
|
alias_method :pre_asset_host_asset_url, :asset_url
|
||||||
|
def asset_url(path, prefix="", request=nil)
|
||||||
|
original_output = pre_asset_host_asset_url(path, prefix, request)
|
||||||
|
|
||||||
|
valid_extensions = %w(.png .gif .jpg .jpeg .js .css)
|
||||||
|
|
||||||
|
if !self.enabled?(:asset_host) || path.include?("://") || !valid_extensions.include?(File.extname(path)) || !self.asset_host
|
||||||
|
return original_output
|
||||||
|
end
|
||||||
|
|
||||||
|
asset_prefix = if self.asset_host.is_a?(Proc) || self.asset_host.respond_to?(:call)
|
||||||
|
self.asset_host.call(original_output)
|
||||||
|
else
|
||||||
|
(self.asset_host =~ /%d/) ? self.asset_host % (original_output.hash % 4) : self.asset_host
|
||||||
|
end
|
||||||
|
|
||||||
|
return File.join(asset_prefix, original_output)
|
||||||
|
end
|
||||||
|
end
|
1
spec/fixtures/sample/views/asset_host.html.haml
vendored
Executable file
1
spec/fixtures/sample/views/asset_host.html.haml
vendored
Executable file
|
@ -0,0 +1 @@
|
||||||
|
= image_tag "blank.gif"
|
3
spec/fixtures/sample/views/stylesheets/asset_host.css.sass
vendored
Executable file
3
spec/fixtures/sample/views/stylesheets/asset_host.css.sass
vendored
Executable file
|
@ -0,0 +1,3 @@
|
||||||
|
@import compass.sass
|
||||||
|
h1
|
||||||
|
background= image_url("blank.gif")
|
Loading…
Reference in a new issue