mirror of
https://github.com/github/rails.git
synced 2026-04-26 03:00:59 -04:00
reduced duplication between the javascript and stylesheet asset tag methods, also split the asset id caching methods into a separate module for easy inclusion and use by the asset include tag class and base asset tag helpers
This commit is contained in:
committed by
José Valim
parent
7a38c8b9f5
commit
6747ae2708
@@ -1,14 +1,7 @@
|
||||
require 'thread'
|
||||
require 'cgi'
|
||||
require 'action_view/helpers/url_helper'
|
||||
require 'action_view/helpers/tag_helper'
|
||||
require 'action_view/helpers/asset_tag_helpers/base_asset_helpers'
|
||||
require 'action_view/helpers/asset_tag_helpers/javascript_tag_helpers'
|
||||
require 'action_view/helpers/asset_tag_helpers/stylesheet_tag_helpers'
|
||||
require 'action_view/helpers/asset_tag_helpers/asset_id_caching'
|
||||
require 'active_support/core_ext/file'
|
||||
require 'active_support/core_ext/object/blank'
|
||||
require 'active_support/core_ext/string/output_safety'
|
||||
|
||||
module ActionView
|
||||
# = Action View Asset Tag Helpers
|
||||
@@ -200,9 +193,9 @@ module ActionView
|
||||
# RewriteRule ^/release-\d+/(images|javascripts|stylesheets)/(.*)$ /$1/$2 [L]
|
||||
module AssetTagHelper
|
||||
include BaseAssetHelpers
|
||||
include AssetIdCaching
|
||||
include JavascriptTagHelpers
|
||||
include StylesheetTagHelpers
|
||||
include AssetIdCaching
|
||||
end
|
||||
end
|
||||
end
|
||||
|
||||
@@ -1,3 +1,5 @@
|
||||
require 'thread'
|
||||
require 'active_support/core_ext/file'
|
||||
require 'active_support/concern'
|
||||
|
||||
module ActionView
|
||||
|
||||
@@ -0,0 +1,132 @@
|
||||
require 'active_support/core_ext/module/attribute_accessors'
|
||||
require 'active_support/core_ext/string/inflections'
|
||||
require 'active_support/core_ext/file'
|
||||
require 'action_view/helpers/tag_helper'
|
||||
require 'action_view/helpers/asset_tag_helpers/common_asset_helpers'
|
||||
require 'action_view/helpers/asset_tag_helpers/asset_id_caching'
|
||||
|
||||
|
||||
module ActionView
|
||||
module Helpers
|
||||
module AssetTagHelper
|
||||
|
||||
class AssetIncludeTag
|
||||
include AssetIdCaching
|
||||
include CommonAssetHelpers
|
||||
|
||||
class_attribute :asset_name
|
||||
class_attribute :extension
|
||||
|
||||
attr_reader :config, :controller, :expansions
|
||||
|
||||
def initialize(config, controller, expansions)
|
||||
@config = config
|
||||
@controller = controller
|
||||
@expansions = expansions
|
||||
end
|
||||
|
||||
def custom_dir
|
||||
raise NotImplementedError
|
||||
end
|
||||
|
||||
def asset_tag(source, options)
|
||||
raise NotImplementedError
|
||||
end
|
||||
|
||||
def include_tag(*sources)
|
||||
options = sources.extract_options!.stringify_keys
|
||||
concat = options.delete("concat")
|
||||
cache = concat || options.delete("cache")
|
||||
recursive = options.delete("recursive")
|
||||
|
||||
if concat || (config.perform_caching && cache)
|
||||
joined_name = (cache == true ? "all" : cache) + ".#{extension}"
|
||||
joined_path = File.join((joined_name[/^#{File::SEPARATOR}/] ? config.assets_dir : custom_dir), joined_name)
|
||||
unless config.perform_caching && File.exists?(joined_path)
|
||||
write_asset_file_contents(joined_path, compute_paths(sources, recursive))
|
||||
end
|
||||
asset_tag(joined_name, options)
|
||||
else
|
||||
sources = expand_sources(sources, recursive)
|
||||
ensure_sources!(sources) if cache
|
||||
sources.collect { |source| asset_tag(source, options) }.join("\n").html_safe
|
||||
end
|
||||
end
|
||||
|
||||
|
||||
private
|
||||
|
||||
def path_to_asset(source)
|
||||
compute_public_path(source, asset_name.to_s.pluralize, extension)
|
||||
end
|
||||
|
||||
def compute_paths(*args)
|
||||
expand_sources(*args).collect { |source| compute_public_path(source, asset_name.pluralize, extension, false) }
|
||||
end
|
||||
|
||||
def expand_sources(sources, recursive)
|
||||
if sources.first == :all
|
||||
collect_asset_files(custom_dir, ('**' if recursive), "*.#{extension}")
|
||||
else
|
||||
sources.collect do |source|
|
||||
determine_source(source, expansions)
|
||||
end.flatten
|
||||
end
|
||||
end
|
||||
|
||||
def ensure_sources!(sources)
|
||||
sources.each do |source|
|
||||
asset_file_path!(compute_public_path(source, asset_name.pluralize, extension))
|
||||
end
|
||||
return sources
|
||||
end
|
||||
|
||||
def collect_asset_files(*path)
|
||||
dir = path.first
|
||||
|
||||
Dir[File.join(*path.compact)].collect do |file|
|
||||
file[-(file.size - dir.size - 1)..-1].sub(/\.\w+$/, '')
|
||||
end.sort
|
||||
end
|
||||
|
||||
def determine_source(source, collection)
|
||||
case source
|
||||
when Symbol
|
||||
collection[source] || raise(ArgumentError, "No expansion found for #{source.inspect}")
|
||||
else
|
||||
source
|
||||
end
|
||||
end
|
||||
|
||||
def join_asset_file_contents(paths)
|
||||
paths.collect { |path| File.read(asset_file_path!(path, true)) }.join("\n\n")
|
||||
end
|
||||
|
||||
def write_asset_file_contents(joined_asset_path, asset_paths)
|
||||
FileUtils.mkdir_p(File.dirname(joined_asset_path))
|
||||
File.atomic_write(joined_asset_path) { |cache| cache.write(join_asset_file_contents(asset_paths)) }
|
||||
|
||||
# Set mtime to the latest of the combined files to allow for
|
||||
# consistent ETag without a shared filesystem.
|
||||
mt = asset_paths.map { |p| File.mtime(asset_file_path(p)) }.max
|
||||
File.utime(mt, mt, joined_asset_path)
|
||||
end
|
||||
|
||||
def asset_file_path(path)
|
||||
File.join(config.assets_dir, path.split('?').first)
|
||||
end
|
||||
|
||||
def asset_file_path!(path, error_if_file_is_uri = false)
|
||||
if is_uri?(path)
|
||||
raise(Errno::ENOENT, "Asset file #{path} is uri and cannot be merged into single file") if error_if_file_is_uri
|
||||
else
|
||||
absolute_path = asset_file_path(path)
|
||||
raise(Errno::ENOENT, "Asset file not found at '#{absolute_path}'" ) unless File.exist?(absolute_path)
|
||||
return absolute_path
|
||||
end
|
||||
end
|
||||
end
|
||||
|
||||
end
|
||||
end
|
||||
end
|
||||
@@ -1,12 +1,16 @@
|
||||
require 'active_support/concern'
|
||||
require 'action_view/helpers/asset_tag_helpers/helper_methods'
|
||||
require 'active_support/hash_with_indifferent_access'
|
||||
require 'action_view/helpers/url_helper'
|
||||
require 'action_view/helpers/tag_helper'
|
||||
require 'action_view/helpers/asset_tag_helpers/common_asset_helpers'
|
||||
|
||||
module ActionView
|
||||
module Helpers
|
||||
module AssetTagHelper
|
||||
|
||||
module BaseAssetHelpers
|
||||
extend HelperMethods
|
||||
extend HelperMacros
|
||||
include CommonAssetHelpers
|
||||
# Returns a link tag that browsers and news readers can use to auto-detect
|
||||
# an RSS or ATOM feed. The +type+ can either be <tt>:rss</tt> (default) or
|
||||
# <tt>:atom</tt>. Control the link options in url_for format using the
|
||||
@@ -235,94 +239,6 @@ module ActionView
|
||||
options[:src] = path_to_audio(source)
|
||||
tag("audio", options)
|
||||
end
|
||||
|
||||
private
|
||||
|
||||
def rewrite_extension(source, dir, ext)
|
||||
source_ext = File.extname(source)
|
||||
|
||||
if source_ext.empty?
|
||||
"#{source}.#{ext}"
|
||||
elsif ext != source_ext[1..-1]
|
||||
with_ext = "#{source}.#{ext}"
|
||||
with_ext if File.exist?(File.join(config.assets_dir, dir, with_ext))
|
||||
end || source
|
||||
end
|
||||
|
||||
def rewrite_host_and_protocol(source, has_request)
|
||||
host = compute_asset_host(source)
|
||||
if has_request && host && !is_uri?(host)
|
||||
host = "#{controller.request.protocol}#{host}"
|
||||
end
|
||||
"#{host}#{source}"
|
||||
end
|
||||
|
||||
def rewrite_relative_url_root(source, relative_url_root)
|
||||
relative_url_root && !source.starts_with?("#{relative_url_root}/") ? "#{relative_url_root}#{source}" : source
|
||||
end
|
||||
|
||||
# Add the the extension +ext+ if not present. Return full URLs otherwise untouched.
|
||||
# Prefix with <tt>/dir/</tt> if lacking a leading +/+. Account for relative URL
|
||||
# roots. Rewrite the asset path for cache-busting asset ids. Include
|
||||
# asset host, if configured, with the correct request protocol.
|
||||
def compute_public_path(source, dir, ext = nil, include_host = true)
|
||||
return source if is_uri?(source)
|
||||
|
||||
source = rewrite_extension(source, dir, ext) if ext
|
||||
source = "/#{dir}/#{source}" unless source[0] == ?/
|
||||
if controller.respond_to?(:env) && controller.env["action_dispatch.asset_path"]
|
||||
source = rewrite_asset_path(source, controller.env["action_dispatch.asset_path"])
|
||||
end
|
||||
source = rewrite_asset_path(source, config.asset_path)
|
||||
|
||||
has_request = controller.respond_to?(:request)
|
||||
source = rewrite_relative_url_root(source, controller.config.relative_url_root) if has_request && include_host
|
||||
source = rewrite_host_and_protocol(source, has_request) if include_host
|
||||
|
||||
source
|
||||
end
|
||||
|
||||
def is_uri?(path)
|
||||
path =~ %r{^[-a-z]+://|^cid:}
|
||||
end
|
||||
|
||||
# Pick an asset host for this source. Returns +nil+ if no host is set,
|
||||
# the host if no wildcard is set, the host interpolated with the
|
||||
# numbers 0-3 if it contains <tt>%d</tt> (the number is the source hash mod 4),
|
||||
# or the value returned from invoking the proc if it's a proc or the value from
|
||||
# invoking call if it's an object responding to call.
|
||||
def compute_asset_host(source)
|
||||
if host = config.asset_host
|
||||
if host.is_a?(Proc) || host.respond_to?(:call)
|
||||
case host.is_a?(Proc) ? host.arity : host.method(:call).arity
|
||||
when 2
|
||||
request = controller.respond_to?(:request) && controller.request
|
||||
host.call(source, request)
|
||||
else
|
||||
host.call(source)
|
||||
end
|
||||
else
|
||||
(host =~ /%d/) ? host % (source.hash % 4) : host
|
||||
end
|
||||
end
|
||||
end
|
||||
|
||||
# Break out the asset path rewrite in case plugins wish to put the asset id
|
||||
# someplace other than the query string.
|
||||
def rewrite_asset_path(source, path = nil)
|
||||
if path && path.respond_to?(:call)
|
||||
return path.call(source)
|
||||
elsif path && path.is_a?(String)
|
||||
return path % [source]
|
||||
else
|
||||
handle_asset_id(source)
|
||||
end
|
||||
end
|
||||
|
||||
# This is the default implementation
|
||||
def handle_asset_id(source)
|
||||
source
|
||||
end
|
||||
end
|
||||
|
||||
end
|
||||
|
||||
@@ -0,0 +1,110 @@
|
||||
require 'active_support/core_ext/file'
|
||||
|
||||
module ActionView
|
||||
module Helpers
|
||||
module AssetTagHelper
|
||||
|
||||
module HelperMacros
|
||||
private
|
||||
def asset_path(asset_type, extension = nil)
|
||||
define_method("#{asset_type}_path") do |source|
|
||||
compute_public_path(source, asset_type.to_s.pluralize, extension)
|
||||
end
|
||||
alias_method :"path_to_#{asset_type}", :"#{asset_type}_path" # aliased to avoid conflicts with a *_path named route
|
||||
end
|
||||
end
|
||||
|
||||
module CommonAssetHelpers
|
||||
private
|
||||
# Add the the extension +ext+ if not present. Return full URLs otherwise untouched.
|
||||
# Prefix with <tt>/dir/</tt> if lacking a leading +/+. Account for relative URL
|
||||
# roots. Rewrite the asset path for cache-busting asset ids. Include
|
||||
# asset host, if configured, with the correct request protocol.
|
||||
def compute_public_path(source, dir, ext = nil, include_host = true)
|
||||
return source if is_uri?(source)
|
||||
|
||||
source = rewrite_extension(source, dir, ext) if ext
|
||||
source = "/#{dir}/#{source}" unless source[0] == ?/
|
||||
if controller.respond_to?(:env) && controller.env["action_dispatch.asset_path"]
|
||||
source = rewrite_asset_path(source, controller.env["action_dispatch.asset_path"])
|
||||
end
|
||||
source = rewrite_asset_path(source, config.asset_path)
|
||||
|
||||
has_request = controller.respond_to?(:request)
|
||||
source = rewrite_relative_url_root(source, controller.config.relative_url_root) if has_request && include_host
|
||||
source = rewrite_host_and_protocol(source, has_request) if include_host
|
||||
|
||||
source
|
||||
end
|
||||
|
||||
def is_uri?(path)
|
||||
path =~ %r{^[-a-z]+://|^cid:}
|
||||
end
|
||||
|
||||
def rewrite_extension(source, dir, ext)
|
||||
source_ext = File.extname(source)
|
||||
|
||||
source_with_ext = if source_ext.empty?
|
||||
"#{source}.#{ext}"
|
||||
elsif ext != source_ext[1..-1]
|
||||
with_ext = "#{source}.#{ext}"
|
||||
with_ext if File.exist?(File.join(config.assets_dir, dir, with_ext))
|
||||
end
|
||||
|
||||
source_with_ext || source
|
||||
end
|
||||
|
||||
# Break out the asset path rewrite in case plugins wish to put the asset id
|
||||
# someplace other than the query string.
|
||||
def rewrite_asset_path(source, path = nil)
|
||||
if path && path.respond_to?(:call)
|
||||
return path.call(source)
|
||||
elsif path && path.is_a?(String)
|
||||
return path % [source]
|
||||
else
|
||||
handle_asset_id(source)
|
||||
end
|
||||
end
|
||||
|
||||
# This is the default implementation
|
||||
def handle_asset_id(source)
|
||||
source
|
||||
end
|
||||
|
||||
def rewrite_relative_url_root(source, relative_url_root)
|
||||
relative_url_root && !source.starts_with?("#{relative_url_root}/") ? "#{relative_url_root}#{source}" : source
|
||||
end
|
||||
|
||||
def rewrite_host_and_protocol(source, has_request)
|
||||
host = compute_asset_host(source)
|
||||
if has_request && host && !is_uri?(host)
|
||||
host = "#{controller.request.protocol}#{host}"
|
||||
end
|
||||
"#{host}#{source}"
|
||||
end
|
||||
|
||||
# Pick an asset host for this source. Returns +nil+ if no host is set,
|
||||
# the host if no wildcard is set, the host interpolated with the
|
||||
# numbers 0-3 if it contains <tt>%d</tt> (the number is the source hash mod 4),
|
||||
# or the value returned from invoking the proc if it's a proc or the value from
|
||||
# invoking call if it's an object responding to call.
|
||||
def compute_asset_host(source)
|
||||
if host = config.asset_host
|
||||
if host.is_a?(Proc) || host.respond_to?(:call)
|
||||
case host.is_a?(Proc) ? host.arity : host.method(:call).arity
|
||||
when 2
|
||||
request = controller.respond_to?(:request) && controller.request
|
||||
host.call(source, request)
|
||||
else
|
||||
host.call(source)
|
||||
end
|
||||
else
|
||||
(host =~ /%d/) ? host % (source.hash % 4) : host
|
||||
end
|
||||
end
|
||||
end
|
||||
end
|
||||
|
||||
end
|
||||
end
|
||||
end
|
||||
@@ -1,67 +0,0 @@
|
||||
require 'active_support/concern'
|
||||
require 'active_support/inflector'
|
||||
|
||||
module ActionView
|
||||
module Helpers
|
||||
module AssetTagHelper
|
||||
|
||||
module HelperMethods
|
||||
private
|
||||
def asset_path(asset_type, extension = nil)
|
||||
define_method("#{asset_type}_path") do |source|
|
||||
compute_public_path(source, asset_type.to_s.pluralize, extension)
|
||||
end
|
||||
alias_method :"path_to_#{asset_type}", :"#{asset_type}_path" # aliased to avoid conflicts with a *_path named route
|
||||
end
|
||||
end
|
||||
|
||||
module SharedHelpers
|
||||
def determine_source(source, collection)
|
||||
case source
|
||||
when Symbol
|
||||
collection[source] || raise(ArgumentError, "No expansion found for #{source.inspect}")
|
||||
else
|
||||
source
|
||||
end
|
||||
end
|
||||
|
||||
def join_asset_file_contents(paths)
|
||||
paths.collect { |path| File.read(asset_file_path!(path, true)) }.join("\n\n")
|
||||
end
|
||||
|
||||
def write_asset_file_contents(joined_asset_path, asset_paths)
|
||||
FileUtils.mkdir_p(File.dirname(joined_asset_path))
|
||||
File.atomic_write(joined_asset_path) { |cache| cache.write(join_asset_file_contents(asset_paths)) }
|
||||
|
||||
# Set mtime to the latest of the combined files to allow for
|
||||
# consistent ETag without a shared filesystem.
|
||||
mt = asset_paths.map { |p| File.mtime(asset_file_path(p)) }.max
|
||||
File.utime(mt, mt, joined_asset_path)
|
||||
end
|
||||
|
||||
def asset_file_path(path)
|
||||
File.join(config.assets_dir, path.split('?').first)
|
||||
end
|
||||
|
||||
def asset_file_path!(path, error_if_file_is_uri = false)
|
||||
if is_uri?(path)
|
||||
raise(Errno::ENOENT, "Asset file #{path} is uri and cannot be merged into single file") if error_if_file_is_uri
|
||||
else
|
||||
absolute_path = asset_file_path(path)
|
||||
raise(Errno::ENOENT, "Asset file not found at '#{absolute_path}'" ) unless File.exist?(absolute_path)
|
||||
return absolute_path
|
||||
end
|
||||
end
|
||||
|
||||
def collect_asset_files(*path)
|
||||
dir = path.first
|
||||
|
||||
Dir[File.join(*path.compact)].collect do |file|
|
||||
file[-(file.size - dir.size - 1)..-1].sub(/\.\w+$/, '')
|
||||
end.sort
|
||||
end
|
||||
end
|
||||
|
||||
end
|
||||
end
|
||||
end
|
||||
@@ -1,14 +1,47 @@
|
||||
require 'active_support/concern'
|
||||
require 'action_view/helpers/asset_tag_helpers/helper_methods'
|
||||
require 'active_support/core_ext/file'
|
||||
require 'action_view/helpers/tag_helper'
|
||||
require 'action_view/helpers/asset_tag_helpers/common_asset_helpers'
|
||||
require 'action_view/helpers/asset_tag_helpers/asset_include_tag'
|
||||
|
||||
module ActionView
|
||||
module Helpers
|
||||
module AssetTagHelper
|
||||
|
||||
class JavascriptIncludeTag < AssetIncludeTag
|
||||
include TagHelper
|
||||
|
||||
self.asset_name = 'javascript'
|
||||
self.extension = 'js'
|
||||
|
||||
def asset_tag(source, options)
|
||||
content_tag("script", "", { "type" => Mime::JS, "src" => path_to_asset(source) }.merge(options))
|
||||
end
|
||||
|
||||
def custom_dir
|
||||
config.javascripts_dir
|
||||
end
|
||||
|
||||
private
|
||||
|
||||
def expand_sources(sources, recursive = false)
|
||||
if sources.include?(:all)
|
||||
all_asset_files = (collect_asset_files(custom_dir, ('**' if recursive), "*.#{extension}") - ['application']) << 'application'
|
||||
((determine_source(:defaults, expansions).dup & all_asset_files) + all_asset_files).uniq
|
||||
else
|
||||
expanded_sources = sources.collect do |source|
|
||||
determine_source(source, expansions)
|
||||
end.flatten
|
||||
expanded_sources << "application" if sources.include?(:defaults) && File.exist?(File.join(custom_dir, "application.#{extension}"))
|
||||
expanded_sources
|
||||
end
|
||||
end
|
||||
end
|
||||
|
||||
module JavascriptTagHelpers
|
||||
extend ActiveSupport::Concern
|
||||
extend HelperMethods
|
||||
include SharedHelpers
|
||||
extend HelperMacros
|
||||
include CommonAssetHelpers
|
||||
|
||||
included do
|
||||
mattr_accessor :javascript_expansions
|
||||
@@ -128,56 +161,10 @@ module ActionView
|
||||
#
|
||||
# javascript_include_tag :all, :cache => true, :recursive => true
|
||||
def javascript_include_tag(*sources)
|
||||
options = sources.extract_options!.stringify_keys
|
||||
concat = options.delete("concat")
|
||||
cache = concat || options.delete("cache")
|
||||
recursive = options.delete("recursive")
|
||||
|
||||
if concat || (config.perform_caching && cache)
|
||||
joined_javascript_name = (cache == true ? "all" : cache) + ".js"
|
||||
joined_javascript_path = File.join(joined_javascript_name[/^#{File::SEPARATOR}/] ? config.assets_dir : config.javascripts_dir, joined_javascript_name)
|
||||
|
||||
unless config.perform_caching && File.exists?(joined_javascript_path)
|
||||
write_asset_file_contents(joined_javascript_path, compute_javascript_paths(sources, recursive))
|
||||
end
|
||||
javascript_src_tag(joined_javascript_name, options)
|
||||
else
|
||||
sources = expand_javascript_sources(sources, recursive)
|
||||
ensure_javascript_sources!(sources) if cache
|
||||
sources.collect { |source| javascript_src_tag(source, options) }.join("\n").html_safe
|
||||
end
|
||||
@javascript_include ||= JavascriptIncludeTag.new(config, controller, self.javascript_expansions)
|
||||
@javascript_include.include_tag(*sources)
|
||||
end
|
||||
|
||||
private
|
||||
|
||||
def javascript_src_tag(source, options)
|
||||
content_tag("script", "", { "type" => Mime::JS, "src" => path_to_javascript(source) }.merge(options))
|
||||
end
|
||||
|
||||
def compute_javascript_paths(*args)
|
||||
expand_javascript_sources(*args).collect { |source| compute_public_path(source, 'javascripts', 'js', false) }
|
||||
end
|
||||
|
||||
def expand_javascript_sources(sources, recursive = false)
|
||||
if sources.include?(:all)
|
||||
all_javascript_files = (collect_asset_files(config.javascripts_dir, ('**' if recursive), '*.js') - ['application']) << 'application'
|
||||
((determine_source(:defaults, self.javascript_expansions).dup & all_javascript_files) + all_javascript_files).uniq
|
||||
else
|
||||
expanded_sources = sources.collect do |source|
|
||||
determine_source(source, self.javascript_expansions)
|
||||
end.flatten
|
||||
expanded_sources << "application" if sources.include?(:defaults) && File.exist?(File.join(config.javascripts_dir, "application.js"))
|
||||
expanded_sources
|
||||
end
|
||||
end
|
||||
|
||||
def ensure_javascript_sources!(sources)
|
||||
sources.each do |source|
|
||||
asset_file_path!(path_to_javascript(source))
|
||||
end
|
||||
return sources
|
||||
end
|
||||
|
||||
end
|
||||
|
||||
end
|
||||
|
||||
@@ -1,14 +1,32 @@
|
||||
require 'active_support/concern'
|
||||
require 'action_view/helpers/asset_tag_helpers/helper_methods'
|
||||
require 'active_support/core_ext/file'
|
||||
require 'action_view/helpers/tag_helper'
|
||||
require 'action_view/helpers/asset_tag_helpers/common_asset_helpers'
|
||||
require 'action_view/helpers/asset_tag_helpers/asset_include_tag'
|
||||
|
||||
module ActionView
|
||||
module Helpers
|
||||
module AssetTagHelper
|
||||
|
||||
class StylesheetIncludeTag < AssetIncludeTag
|
||||
include TagHelper
|
||||
|
||||
self.asset_name = 'stylesheet'
|
||||
self.extension = 'css'
|
||||
|
||||
def asset_tag(source, options)
|
||||
tag("link", { "rel" => "stylesheet", "type" => Mime::CSS, "media" => "screen", "href" => ERB::Util.html_escape(path_to_asset(source)) }.merge(options), false, false)
|
||||
end
|
||||
|
||||
def custom_dir
|
||||
config.stylesheets_dir
|
||||
end
|
||||
end
|
||||
|
||||
module StylesheetTagHelpers
|
||||
extend ActiveSupport::Concern
|
||||
extend HelperMethods
|
||||
include SharedHelpers
|
||||
extend HelperMacros
|
||||
include CommonAssetHelpers
|
||||
|
||||
included do
|
||||
mattr_accessor :stylesheet_expansions
|
||||
@@ -115,53 +133,10 @@ module ActionView
|
||||
# stylesheet_link_tag :all, :concat => true
|
||||
#
|
||||
def stylesheet_link_tag(*sources)
|
||||
options = sources.extract_options!.stringify_keys
|
||||
concat = options.delete("concat")
|
||||
cache = concat || options.delete("cache")
|
||||
recursive = options.delete("recursive")
|
||||
|
||||
if concat || (config.perform_caching && cache)
|
||||
joined_stylesheet_name = (cache == true ? "all" : cache) + ".css"
|
||||
joined_stylesheet_path = File.join(joined_stylesheet_name[/^#{File::SEPARATOR}/] ? config.assets_dir : config.stylesheets_dir, joined_stylesheet_name)
|
||||
|
||||
unless config.perform_caching && File.exists?(joined_stylesheet_path)
|
||||
write_asset_file_contents(joined_stylesheet_path, compute_stylesheet_paths(sources, recursive))
|
||||
end
|
||||
stylesheet_tag(joined_stylesheet_name, options)
|
||||
else
|
||||
sources = expand_stylesheet_sources(sources, recursive)
|
||||
ensure_stylesheet_sources!(sources) if cache
|
||||
sources.collect { |source| stylesheet_tag(source, options) }.join("\n").html_safe
|
||||
end
|
||||
@stylesheet_include ||= StylesheetIncludeTag.new(config, controller, self.stylesheet_expansions)
|
||||
@stylesheet_include.include_tag(*sources)
|
||||
end
|
||||
|
||||
private
|
||||
|
||||
def stylesheet_tag(source, options)
|
||||
tag("link", { "rel" => "stylesheet", "type" => Mime::CSS, "media" => "screen", "href" => ERB::Util.html_escape(path_to_stylesheet(source)) }.merge(options), false, false)
|
||||
end
|
||||
|
||||
def compute_stylesheet_paths(*args)
|
||||
expand_stylesheet_sources(*args).collect { |source| compute_public_path(source, 'stylesheets', 'css', false) }
|
||||
end
|
||||
|
||||
def expand_stylesheet_sources(sources, recursive)
|
||||
if sources.first == :all
|
||||
collect_asset_files(config.stylesheets_dir, ('**' if recursive), '*.css')
|
||||
else
|
||||
sources.collect do |source|
|
||||
determine_source(source, self.stylesheet_expansions)
|
||||
end.flatten
|
||||
end
|
||||
end
|
||||
|
||||
def ensure_stylesheet_sources!(sources)
|
||||
sources.each do |source|
|
||||
asset_file_path!(path_to_stylesheet(source))
|
||||
end
|
||||
return sources
|
||||
end
|
||||
|
||||
end
|
||||
|
||||
end
|
||||
|
||||
Reference in New Issue
Block a user