class ActionView::Helpers::AssetTagHelper::AssetIncludeTag

Attributes

asset_paths[R]
config[R]

Public Class Methods

inherited(base) click to toggle source
# File lib/action_view/helpers/asset_tag_helpers/asset_include_tag.rb, line 16
def self.inherited(base)
  base.expansions = { }
end
new(config, asset_paths) click to toggle source
# File lib/action_view/helpers/asset_tag_helpers/asset_include_tag.rb, line 20
def initialize(config, asset_paths)
  @config = config
  @asset_paths = asset_paths
end

Public Instance Methods

asset_name() click to toggle source
# File lib/action_view/helpers/asset_tag_helpers/asset_include_tag.rb, line 25
def asset_name
  raise NotImplementedError
end
asset_tag(source, options) click to toggle source
# File lib/action_view/helpers/asset_tag_helpers/asset_include_tag.rb, line 37
def asset_tag(source, options)
  raise NotImplementedError
end
custom_dir() click to toggle source
# File lib/action_view/helpers/asset_tag_helpers/asset_include_tag.rb, line 33
def custom_dir
  raise NotImplementedError
end
extension() click to toggle source
# File lib/action_view/helpers/asset_tag_helpers/asset_include_tag.rb, line 29
def extension
  raise NotImplementedError
end
include_tag(*sources) click to toggle source
# File lib/action_view/helpers/asset_tag_helpers/asset_include_tag.rb, line 41
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 Instance Methods

asset_file_path!(absolute_path, error_if_file_is_uri = false) click to toggle source
# File lib/action_view/helpers/asset_tag_helpers/asset_include_tag.rb, line 134
def asset_file_path!(absolute_path, error_if_file_is_uri = false)
  if asset_paths.is_uri?(absolute_path)
    raise(Errno::ENOENT, "Asset file #{path} is uri and cannot be merged into single file") if error_if_file_is_uri
  else
    raise(Errno::ENOENT, "Asset file not found at '#{absolute_path}'" ) unless File.exist?(absolute_path)
    return absolute_path
  end
end
collect_asset_files(*path) click to toggle source
# File lib/action_view/helpers/asset_tag_helpers/asset_include_tag.rb, line 103
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
compute_paths(*args) click to toggle source
# File lib/action_view/helpers/asset_tag_helpers/asset_include_tag.rb, line 71
def compute_paths(*args)
  expand_sources(*args).collect { |source| path_to_asset_source(source) }
end
determine_source(source, collection) click to toggle source
# File lib/action_view/helpers/asset_tag_helpers/asset_include_tag.rb, line 111
def determine_source(source, collection)
  case source
  when Symbol
    collection[source] || raise(ArgumentError, "No expansion found for #{source.inspect}")
  else
    source
  end
end
ensure_sources!(sources) click to toggle source
# File lib/action_view/helpers/asset_tag_helpers/asset_include_tag.rb, line 97
def ensure_sources!(sources)
  sources.each do |source|
    asset_file_path!(path_to_asset_source(source))
  end
end
expand_sources(sources, recursive) click to toggle source
# File lib/action_view/helpers/asset_tag_helpers/asset_include_tag.rb, line 75
def expand_sources(sources, recursive)
  if sources.first == :all
    collect_asset_files(custom_dir, ('**' if recursive), "*.#{extension}")
  else
    sources.inject([]) do |list, source|
      determined_source = determine_source(source, expansions)
      update_source_list(list, determined_source)
    end
  end
end
join_asset_file_contents(paths) click to toggle source
# File lib/action_view/helpers/asset_tag_helpers/asset_include_tag.rb, line 120
def join_asset_file_contents(paths)
  paths.collect { |path| File.read(asset_file_path!(path, true)) }.join("\n\n")
end
path_to_asset(source, options = {}) click to toggle source
# File lib/action_view/helpers/asset_tag_helpers/asset_include_tag.rb, line 63
def path_to_asset(source, options = {})
  asset_paths.compute_public_path(source, asset_name.to_s.pluralize, options.merge(:ext => extension))
end
path_to_asset_source(source) click to toggle source
# File lib/action_view/helpers/asset_tag_helpers/asset_include_tag.rb, line 67
def path_to_asset_source(source)
  asset_paths.compute_source_path(source, asset_name.to_s.pluralize, extension)
end
update_source_list(list, source) click to toggle source
# File lib/action_view/helpers/asset_tag_helpers/asset_include_tag.rb, line 86
def update_source_list(list, source)
  case source
  when String
    list.delete(source)
    list << source
  when Array
    updated_sources = source - list
    list.concat(updated_sources)
  end
end
write_asset_file_contents(joined_asset_path, asset_paths) click to toggle source
# File lib/action_view/helpers/asset_tag_helpers/asset_include_tag.rb, line 124
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