diff options
Diffstat (limited to 'actionpack/lib/sprockets')
-rw-r--r-- | actionpack/lib/sprockets/compressors.rb | 21 | ||||
-rw-r--r-- | actionpack/lib/sprockets/helpers/rails_helper.rb | 45 | ||||
-rw-r--r-- | actionpack/lib/sprockets/railtie.rb | 10 |
3 files changed, 57 insertions, 19 deletions
diff --git a/actionpack/lib/sprockets/compressors.rb b/actionpack/lib/sprockets/compressors.rb new file mode 100644 index 0000000000..6544953df4 --- /dev/null +++ b/actionpack/lib/sprockets/compressors.rb @@ -0,0 +1,21 @@ +module Sprockets + class NullCompressor + def compress(content) + content + end + end + + class LazyCompressor + def initialize(&block) + @block = block + end + + def compressor + @compressor ||= @block.call || NullCompressor.new + end + + def compress(content) + compressor.compress(content) + end + end +end
\ No newline at end of file diff --git a/actionpack/lib/sprockets/helpers/rails_helper.rb b/actionpack/lib/sprockets/helpers/rails_helper.rb index 63820cc76c..a95e7c0649 100644 --- a/actionpack/lib/sprockets/helpers/rails_helper.rb +++ b/actionpack/lib/sprockets/helpers/rails_helper.rb @@ -1,4 +1,4 @@ -require "action_view/helpers" +require "action_view" module Sprockets module Helpers @@ -9,14 +9,12 @@ module Sprockets def asset_paths @asset_paths ||= begin config = self.config if respond_to?(:config) + config ||= Rails.application.config controller = self.controller if respond_to?(:controller) - config ||= Rails.application.config - if config.action_controller.present? - config.action_controller.default_asset_host_protocol ||= :relative - else - config.default_asset_host_protocol ||= :relative - end - RailsHelper::AssetPaths.new(config, controller) + paths = RailsHelper::AssetPaths.new(config, controller) + paths.asset_environment = asset_environment + paths.asset_prefix = asset_prefix + paths end end @@ -76,9 +74,28 @@ module Sprockets params[:debug_assets] == 'true' end + # Override to specify an alternative prefix for asset path generation. + # When combined with a custom +asset_environment+, this can be used to + # implement themes that can take advantage of the asset pipeline. + # + # If you only want to change where the assets are mounted, refer to + # +config.assets.prefix+ instead. + def asset_prefix + Rails.application.config.assets.prefix + end + + # Override to specify an alternative asset environment for asset + # path generation. The environment should already have been mounted + # at the prefix returned by +asset_prefix+. + def asset_environment + Rails.application.assets + end + class AssetPaths < ::ActionView::AssetPaths #:nodoc: - def compute_public_path(source, dir, ext=nil, include_host=true, protocol = nil) - super(source, Rails.application.config.assets.prefix, ext, include_host, protocol) + attr_accessor :asset_environment, :asset_prefix + + def compute_public_path(source, dir, ext=nil, include_host=true, protocol=nil) + super(source, asset_prefix, ext, include_host, protocol) end # Return the filesystem path for the source @@ -90,14 +107,14 @@ module Sprockets source = source.to_s return nil if is_uri?(source) source = rewrite_extension(source, nil, ext) - assets[source] + asset_environment[source] end def rewrite_asset_path(source, dir) if source[0] == ?/ source else - assets.path(source, performing_caching?, dir) + asset_environment.path(source, performing_caching?, dir) end end @@ -109,10 +126,6 @@ module Sprockets end end - def assets - Rails.application.assets - end - # When included in Sprockets::Context, we need to ask the top-level config as the controller is not available def performing_caching? config.action_controller.present? ? config.action_controller.perform_caching : config.perform_caching diff --git a/actionpack/lib/sprockets/railtie.rb b/actionpack/lib/sprockets/railtie.rb index d1e97c594e..c28bdc3061 100644 --- a/actionpack/lib/sprockets/railtie.rb +++ b/actionpack/lib/sprockets/railtie.rb @@ -1,8 +1,12 @@ module Sprockets autoload :Helpers, "sprockets/helpers" + autoload :LazyCompressor, "sprockets/compressors" + autoload :NullCompressor, "sprockets/compressors" # TODO: Get rid of config.assets.enabled class Railtie < ::Rails::Railtie + config.default_asset_host_protocol = :relative + rake_tasks do load "sprockets/assets.rake" end @@ -60,14 +64,14 @@ module Sprockets env.logger = Rails.logger if env.respond_to?(:cache) - env.cache = Rails.cache + env.cache = assets.cache_store || Rails.cache end if assets.compress # temporarily hardcode default JS compressor to uglify. Soon, it will work # the same as SCSS, where a default plugin sets the default. - env.js_compressor = expand_js_compressor(assets.js_compressor || :uglifier) - env.css_compressor = expand_css_compressor(assets.css_compressor) + env.js_compressor = LazyCompressor.new { expand_js_compressor(assets.js_compressor || :uglifier) } + env.css_compressor = LazyCompressor.new { expand_css_compressor(assets.css_compressor) } end env |