module Eldr::Assets::Helpers

Constants

ABSOLUTE_URL_PATTERN
APPEND_ASSET_EXTENSIONS

Public Instance Methods

asset_path(kind, source = nil) click to toggle source
# File lib/eldr/assets/helpers.rb, line 30
def asset_path(kind, source = nil)
  kind, source = source, kind if source.nil?
  source = asset_normalize_extension(kind, URI.escape(source.to_s))

  return source if source =~ ABSOLUTE_URL_PATTERN || source =~ /^\//

  source      = File.join(asset_folder_name(kind).to_s, source)
  timestamp   = asset_timestamp(source)
  result_path = uri_root_path(source)

  "#{result_path}#{timestamp}"
end
css(*sources) click to toggle source
# File lib/eldr/assets/helpers.rb, line 11
def css(*sources)
  options = {
    rel: 'stylesheet',
    type: 'text/css'
  }.update(sources.extract_options!.symbolize_keys)
  sources.flatten.inject(ActiveSupport::SafeBuffer.new) do |all,source|
    all << tag(:link, { href: asset_path(:css, source) }.update(options))
  end
end
js(*sources) click to toggle source
# File lib/eldr/assets/helpers.rb, line 21
def js(*sources)
  options = {
    type: 'text/javascript'
  }.update(sources.extract_options!.symbolize_keys)
  sources.flatten.inject(ActiveSupport::SafeBuffer.new) do |all,source|
    all << content_tag(:script, nil, { src: asset_path(:js, source) }.update(options))
  end
end
uri_root_path(*paths) click to toggle source
# File lib/eldr/assets/helpers.rb, line 43
def uri_root_path(*paths)
  root_uri   = self.configuration.uri_root
  root_uri ||= ENV['ROOT_URI'] if ENV.include? 'ROOT_URI'
  File.join(ENV['RACK_BASE_URI'].to_s, root_uri || '/', *paths)
end

Private Instance Methods

asset_folder_name(kind) click to toggle source
# File lib/eldr/assets/helpers.rb, line 67
def asset_folder_name(kind)
  asset_folder   = self.configuration.send("#{kind}_assets_folder")
  asset_folder ||= kind
end
asset_normalize_extension(kind, source) click to toggle source
# File lib/eldr/assets/helpers.rb, line 72
def asset_normalize_extension(kind, source)
  ignore_extension = !APPEND_ASSET_EXTENSIONS.include?(kind.to_s)
  source << ".#{kind}" unless ignore_extension || source =~ /\.#{kind}/ || source =~ ABSOLUTE_URL_PATTERN
  source
end
asset_timestamp(file_path) click to toggle source
# File lib/eldr/assets/helpers.rb, line 51
def asset_timestamp(file_path)
  return nil if file_path =~ /\?/ || (self.configuration.asset_stamp == false)

  assets_path   = self.configuration.assets_path
  assets_path ||= ENV['ASSETS_PATH']
  assets_path ||= "#{ENV['APP_ROOT']}/assets" if ENV['APP_ROOT']
  assets_path ||= "#{self.configuration.app_root}/assets"

  asset_file_path = File.join(assets_path, file_path) if assets_path

  stamp   = File.mtime(asset_file_path).to_i if asset_file_path && File.exist?(asset_file_path)
  stamp ||= Time.now.to_i

  "?#{stamp}"
end