module ActionView::Helpers::AssetTagHelper

Public Instance Methods

theme_font_include(theme_id, font_file_name, options={}) click to toggle source

theme_font_include(theme_id, font_file_name, options={}) @param1: theme_id(string) @param2: font_file_name(string) @param3: options = {

  sources: [
              {url: 'font1', format: 'format1'},
              {url: 'font2', format: 'format2'}
           ],
           font_family: 'FontFamilyName',
           font_style: normal,
           font_weight: normal,
           font_stretch: '',
           unicode_range: '',
           apply_to: ''
}
# File lib/knitkit/extensions/railties/theme_support/asset_tag_helper.rb, line 114
def theme_font_include(theme_id, font_file_name, options={})
  theme = controller.website.themes.find_by_theme_id(theme_id)
  return("could not find theme with the id #{theme_id}") unless theme

  theme_font_src_tag(theme, font_file_name, options)
end
theme_image_path(theme, source) click to toggle source
# File lib/knitkit/extensions/railties/theme_support/asset_tag_helper.rb, line 67
def theme_image_path(theme, source)
  theme = controller.website.themes.find_by_theme_id(theme) unless theme.is_a?(Theme)

  name, directory = name_and_path_from_source(source, "#{theme.url}/images")

  file = theme.files.where('name = ? and directory = ?', name, directory).first

  file.nil? ? '' : file.data.url
end
Also aliased as: theme_path_to_image
theme_image_tag(theme_id, source, options = {}) click to toggle source
# File lib/knitkit/extensions/railties/theme_support/asset_tag_helper.rb, line 79
def theme_image_tag(theme_id, source, options = {})
  theme = controller.website.themes.find_by_theme_id(theme_id)
  return("could not find theme with the id #{theme_id}") unless theme

  options.symbolize_keys!
  options[:src] = theme_path_to_image(theme, source)
  options[:alt] ||= File.basename(options[:src], '.*').split('.').first.to_s.capitalize

  if size = options.delete(:size)
    options[:width], options[:height] = size.split("x") if size =~ %r{^\d+x\d+$}
  end

  if mouseover = options.delete(:mouseover)
    options[:onmouseover] = "this.src='#{theme_image_path(theme, mouseover)}'"
    options[:onmouseout] = "this.src='#{theme_image_path(theme, options[:src])}'"
  end

  tag("img", options)
end
theme_javascript_include_tag(theme_id, *sources) click to toggle source
# File lib/knitkit/extensions/railties/theme_support/asset_tag_helper.rb, line 25
def theme_javascript_include_tag(theme_id, *sources)
  theme = controller.website.themes.find_by_theme_id(theme_id)
  return("could not find theme with the id #{theme_id}") unless theme

  options = sources.extract_options!.stringify_keys
  cache = options.delete("cache")
  recursive = options.delete("recursive")

  sources = theme_expand_javascript_sources(theme, sources, recursive).collect do |source|
    theme_javascript_src_tag(theme, source, options)
  end.join("\n")
  raw sources
  #end
end
theme_javascript_path(theme, source) click to toggle source
# File lib/knitkit/extensions/railties/theme_support/asset_tag_helper.rb, line 13
def theme_javascript_path(theme, source)
  theme = controller.website.themes.find_by_theme_id(theme) unless theme.is_a?(Theme)

  name, directory = name_and_path_from_source(source, "#{theme.url}/javascripts")

  file = theme.files.where('name = ? and directory = ?', name, directory).first

  file.nil? ? '' : file.data.url
end
Also aliased as: theme_path_to_javascript
theme_path_to_image(theme, source)
Alias for: theme_image_path
theme_path_to_javascript(theme, source)
theme_path_to_stylesheet(theme, source)
theme_stylesheet_path(theme, source) click to toggle source
# File lib/knitkit/extensions/railties/theme_support/asset_tag_helper.rb, line 40
def theme_stylesheet_path(theme, source)
  theme = controller.website.themes.find_by_theme_id(theme) unless theme.is_a?(Theme)

  name, directory = name_and_path_from_source(source, "#{theme.url}/stylesheets")

  file = theme.files.where('name = ? and directory = ?', name, directory).first

  file.nil? ? '' : file.data.url
end
Also aliased as: theme_path_to_stylesheet

Private Instance Methods

generate_font_css_code(url, options, apply_to) click to toggle source
# File lib/knitkit/extensions/railties/theme_support/asset_tag_helper.rb, line 155
def generate_font_css_code(url, options, apply_to)
  font_code = "@font-face{\n src: url('#{url}'); \n"

  options.each do |key, value|
    font_code += "#{key}: #{value}; \n"
  end
  font_code += '}'

  if apply_to.present?
    font_code += "\n#{apply_to}{ font-family: #{options['font-family']} !important;}"
  end

  font_code
end
get_theme_attributes(path, options) click to toggle source
# File lib/knitkit/extensions/railties/theme_support/asset_tag_helper.rb, line 138
def get_theme_attributes(path, options)
  font_options = {}
  font_options['src'] = get_theme_font_urls(path, options[:sources]).join(',') if options[:sources]

  %w(font-family font-weight font-style font-stretch unicode-range).each do |option_key|
    key = option_key.split('-').join('_')

    if option_key == 'font_family'
      font_options[option_key] = "'#{options[key.to_sym]}'"
    else
      font_options[option_key] = options[key.to_sym]
    end if options[key.to_sym].present?
  end

  font_options
end
get_theme_font_urls(path, sources) click to toggle source
# File lib/knitkit/extensions/railties/theme_support/asset_tag_helper.rb, line 129
def get_theme_font_urls(path, sources)
  font_urls = []
  sources.each do |source|
    url_str = "url('#{path + source[:url]}') format('#{source[:format]}')"
    font_urls << url_str
  end
  font_urls
end
name_and_path_from_source(source, base_directory) click to toggle source
# File lib/knitkit/extensions/railties/theme_support/asset_tag_helper.rb, line 280
def name_and_path_from_source(source, base_directory)
  path = source.split('/')
  name = path.last

  directory = if path.length > 1
                #remove last element
                path.pop

                "#{base_directory}/#{path.join('/')}"
              else
                base_directory
              end

  return name, directory
end
theme_asset_file_path(theme, path) click to toggle source
# File lib/knitkit/extensions/railties/theme_support/asset_tag_helper.rb, line 276
def theme_asset_file_path(theme, path)
  File.join(Theme.root_dir, path.split('?').first)
end
theme_compute_javascript_paths(theme, *args) click to toggle source
# File lib/knitkit/extensions/railties/theme_support/asset_tag_helper.rb, line 235
def theme_compute_javascript_paths(theme, *args)
  theme_expand_javascript_sources(theme, *args).collect do |source|
    theme_compute_public_path(theme, source, theme.url + '/javascripts', 'js', false)
  end
end
theme_compute_public_path(theme, source, dir, ext = nil, include_host = true) click to toggle source
# File lib/knitkit/extensions/railties/theme_support/asset_tag_helper.rb, line 183
def theme_compute_public_path(theme, source, dir, ext = nil, include_host = true)
  has_request = controller.respond_to?(:request)

  if ext && (File.extname(source).blank? || File.exist?(File.join(theme.path, dir, "#{source}.#{ext}")))
    source += ".#{ext}"
  end

  unless source =~ %r{^[-a-z]+://}
    source = "/#{dir}/#{source}" unless source[0] == ?/

    source = theme_rewrite_asset_path(theme, source)

    if has_request && include_host
      unless source =~ %r{^#{ActionController::Base.config.relative_url_root}/}
        source = "#{ActionController::Base.config.relative_url_root}#{source}"
      end
    end
  end

  source
end
theme_compute_stylesheet_paths(theme, *args) click to toggle source
# File lib/knitkit/extensions/railties/theme_support/asset_tag_helper.rb, line 241
def theme_compute_stylesheet_paths(theme, *args)
  theme_expand_stylesheet_sources(theme, *args).collect do |source|
    theme_compute_public_path(theme, source, theme.url + '/stylesheets', 'css', false)
  end
end
theme_expand_javascript_sources(theme, sources, recursive = false) click to toggle source
# File lib/knitkit/extensions/railties/theme_support/asset_tag_helper.rb, line 247
def theme_expand_javascript_sources(theme, sources, recursive = false)
  if sources.include?(:all)
    all_javascript_files = collect_asset_files(theme.path + '/javascripts', ('**' if recursive), '*.js').uniq
  else
    sources.flatten
  end
end
theme_expand_stylesheet_sources(theme, sources, recursive) click to toggle source
# File lib/knitkit/extensions/railties/theme_support/asset_tag_helper.rb, line 255
def theme_expand_stylesheet_sources(theme, sources, recursive)
  if sources.first == :all
    collect_asset_files(theme.path + '/stylesheets', ('**' if recursive), '*.css')
  else
    sources.flatten
  end
end
theme_font_path(theme, font_file_name) click to toggle source
# File lib/knitkit/extensions/railties/theme_support/asset_tag_helper.rb, line 123
def theme_font_path(theme, font_file_name)
  name, directory = name_and_path_from_source(font_file_name, "#{theme.url}/fonts")
  file = theme.files.where('name = ? and directory = ?', name, directory).first
  file.nil? ? '' : file.data.url
end
theme_font_src_tag(theme, font_file_name, options) click to toggle source
# File lib/knitkit/extensions/railties/theme_support/asset_tag_helper.rb, line 170
def theme_font_src_tag(theme, font_file_name, options)
  font_url = theme_font_path(theme, font_file_name)
  font_code = ""

  if font_url.present?
    absolute_path = font_url.split(font_file_name).first
    font_options = get_theme_attributes(absolute_path, options)
    font_code = generate_font_css_code(font_url, font_options, options[:apply_to])
  end

  content_tag("style", raw(font_code))
end
theme_javascript_src_tag(theme, source, options) click to toggle source
# File lib/knitkit/extensions/railties/theme_support/asset_tag_helper.rb, line 224
def theme_javascript_src_tag(theme, source, options)
  options = {"type" => Mime::JS, "src" => theme_path_to_javascript(theme, source)}.merge(options)
  content_tag("script", "", options)
end
theme_join_asset_file_contents(theme, paths) click to toggle source
# File lib/knitkit/extensions/railties/theme_support/asset_tag_helper.rb, line 272
def theme_join_asset_file_contents(theme, paths)
  paths.collect { |path| File.read(theme_asset_file_path(theme, path)) }.join("\n\n")
end
theme_rails_asset_id(theme, source) click to toggle source
# File lib/knitkit/extensions/railties/theme_support/asset_tag_helper.rb, line 205
def theme_rails_asset_id(theme, source)
  if asset_id = ENV["RAILS_ASSET_ID"]
    asset_id
  else
    path = File.join(theme.path, source)
    asset_id = File.exist?(path) ? File.mtime(path).to_i.to_s : ''
    asset_id
  end
end
theme_rewrite_asset_path(theme, source) click to toggle source
# File lib/knitkit/extensions/railties/theme_support/asset_tag_helper.rb, line 215
def theme_rewrite_asset_path(theme, source)
  asset_id = theme_rails_asset_id(theme, source)
  if asset_id.blank?
    source
  else
    source + "?#{asset_id}"
  end
end
theme_stylesheet_tag(theme, source, options) click to toggle source
# File lib/knitkit/extensions/railties/theme_support/asset_tag_helper.rb, line 229
def theme_stylesheet_tag(theme, source, options)
  options = {"rel" => "stylesheet", "type" => Mime::CSS, "media" => "screen",
             "href" => html_escape(theme_path_to_stylesheet(theme, source))}.merge(options)
  tag("link", options, false, false)
end
theme_write_asset_file_contents(theme, joined_asset_path, asset_paths) click to toggle source
# File lib/knitkit/extensions/railties/theme_support/asset_tag_helper.rb, line 263
def theme_write_asset_file_contents(theme, joined_asset_path, asset_paths)
  FileUtils.mkdir_p(File.dirname(joined_asset_path))
  File.open(joined_asset_path, "w+") do |cache|
    cache.write(theme_join_asset_file_contents(theme, asset_paths))
  end
  mt = asset_paths.map { |p| File.mtime(theme_asset_file_path(theme, p)) }.max
  File.utime(mt, mt, joined_asset_path)
end