Start moving some logic from being embedded in AV into the Rails Sprockets extensions

This commit is contained in:
wycats 2011-05-23 16:07:16 -07:00
parent 5ec23b95ba
commit 4b79029490
5 changed files with 60 additions and 62 deletions

@ -274,11 +274,7 @@ def favicon_link_tag(source='/favicon.ico', options={})
# The alias +path_to_image+ is provided to avoid that. Rails uses the alias internally, and
# plugin authors are encouraged to do so.
def image_path(source)
if config.use_sprockets
asset_path(source)
else
asset_paths.compute_public_path(source, 'images')
end
asset_paths.compute_public_path(source, 'images')
end
alias_method :path_to_image, :image_path # aliased to avoid conflicts with an image_path named route
@ -293,11 +289,7 @@ def image_path(source)
# video_path("/trailers/hd.avi") # => /trailers/hd.avi
# video_path("http://www.example.com/vid/hd.avi") # => http://www.example.com/vid/hd.avi
def video_path(source)
if config.use_sprockets
asset_path(source)
else
asset_paths.compute_public_path(source, 'videos')
end
asset_paths.compute_public_path(source, 'videos')
end
alias_method :path_to_video, :video_path # aliased to avoid conflicts with a video_path named route
@ -312,11 +304,7 @@ def video_path(source)
# audio_path("/sounds/horse.wav") # => /sounds/horse.wav
# audio_path("http://www.example.com/sounds/horse.wav") # => http://www.example.com/sounds/horse.wav
def audio_path(source)
if config.use_sprockets
asset_path(source)
else
asset_paths.compute_public_path(source, 'audios')
end
asset_paths.compute_public_path(source, 'audios')
end
alias_method :path_to_audio, :audio_path # aliased to avoid conflicts with an audio_path named route

@ -11,7 +11,7 @@ def debug_assets?
def asset_path(source, default_ext = nil, body = false)
source = source.logical_path if source.respond_to?(:logical_path)
path = sprockets_asset_paths.compute_public_path(source, 'assets', default_ext, true)
path = asset_paths.compute_public_path(source, 'assets', default_ext, true)
body ? "#{path}?body=1" : path
end
@ -19,7 +19,7 @@ def sprockets_javascript_include_tag(source, options = {})
debug = options.key?(:debug) ? options.delete(:debug) : debug_assets?
body = options.key?(:body) ? options.delete(:body) : false
if debug && asset = sprockets_asset_paths.asset_for(source, 'js')
if debug && asset = asset_paths.asset_for(source, 'js')
asset.to_a.map { |dep|
sprockets_javascript_include_tag(dep, :debug => false, :body => true)
}.join("\n").html_safe
@ -37,7 +37,7 @@ def sprockets_stylesheet_link_tag(source, options = {})
debug = options.key?(:debug) ? options.delete(:debug) : debug_assets?
body = options.key?(:body) ? options.delete(:body) : false
if debug && asset = sprockets_asset_paths.asset_for(source, 'css')
if debug && asset = asset_paths.asset_for(source, 'css')
asset.to_a.map { |dep|
sprockets_stylesheet_link_tag(dep, :debug => false, :body => true)
}.join("\n").html_safe
@ -52,50 +52,6 @@ def sprockets_stylesheet_link_tag(source, options = {})
tag 'link', options
end
end
private
def sprockets_asset_paths
@sprockets_asset_paths ||= begin
config = self.config if respond_to?(:config)
controller = self.controller if respond_to?(:controller)
SprocketsHelper::AssetPaths.new(config, controller)
end
end
class AssetPaths < ActionView::Helpers::AssetPaths #:nodoc:
def asset_for(source, ext)
source = source.to_s
return nil if is_uri?(source)
source = rewrite_extension(source, nil, ext)
assets[source]
end
def rewrite_asset_path(source, dir)
if source[0] == ?/
source
else
assets.path(source, performing_caching?, dir)
end
end
def rewrite_extension(source, dir, ext)
if ext && File.extname(source).empty?
"#{source}.#{ext}"
else
source
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 ? @config.perform_caching : Rails.application.config.action_controller.perform_caching
end
end
end
end
end

@ -0,0 +1,51 @@
module Sprockets
module Helpers
module RailsHelper
def asset_paths
@asset_paths ||= begin
config = self.config if respond_to?(:config)
controller = self.controller if respond_to?(:controller)
RailsHelper::AssetPaths.new(config, controller)
end
end
class AssetPaths < ActionView::Helpers::AssetPaths #:nodoc:
def compute_public_path(source, dir, ext=nil, include_host=true)
super(source, 'assets', ext, include_host)
end
def asset_for(source, ext)
source = source.to_s
return nil if is_uri?(source)
source = rewrite_extension(source, nil, ext)
assets[source]
end
def rewrite_asset_path(source, dir)
if source[0] == ?/
source
else
assets.path(source, performing_caching?, dir)
end
end
def rewrite_extension(source, dir, ext)
if ext && File.extname(source).empty?
"#{source}.#{ext}"
else
source
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 ? @config.perform_caching : Rails.application.config.action_controller.perform_caching
end
end
end
end
end

@ -36,6 +36,7 @@ def self.using_scss?
ActiveSupport.on_load(:action_view) do
app.assets.context_class.instance_eval do
include ::ActionView::Helpers::SprocketsHelper
include ::Sprockets::Helpers::RailsHelper
end
end

@ -1,9 +1,11 @@
require 'abstract_unit'
require 'sprockets'
require 'sprockets/helpers/rails_helper'
require 'mocha'
class SprocketsHelperTest < ActionView::TestCase
tests ActionView::Helpers::SprocketsHelper
include Sprockets::Helpers::RailsHelper
attr_accessor :assets