module Tension::Utils

Constants

CONTROLLER_REGEX

Matches strings like “blog/comments”.

EXTENSION_REGEX
SHARED_REGEX
SHARED_SUFFIX

Public Class Methods

action_name(path) click to toggle source

Attempts to find an action name for a given asset path.

# File lib/tension/utils.rb, line 26
def action_name(path)
  strip_file_extension( path.split("/").last ) if asset_path?(path)
end
controller_path(path) click to toggle source

Attempts to build a valid controller path from the given path String.

ARGS: path: a String path like "admin/blog_common.css"
# File lib/tension/utils.rb, line 15
def controller_path(path)
  if asset_path?(path)
    return controller_for_asset_path(path)

  elsif matches = path.split("#").first.match(CONTROLLER_REGEX)
    return matches[0]
  end
end
find_asset(options) click to toggle source

Attempts to load an Asset from the Sprockets index. Uses the given Hash argument to build an asset path.

ARGS: options: a Hash containing e.g.
  controller: "blog/comments"
  action: "index"
  type: Tension::CSS
# File lib/tension/utils.rb, line 44
def find_asset(options)
  Tension.environment.find_asset( logical_asset_path(options) )
end
global_asset(type) click to toggle source

Returns the application-wide Sprockets Asset for the given type.

ARGS: type: Tension::JS or Tension::CSS
# File lib/tension/utils.rb, line 51
def global_asset(type)
  Tension.environment.find_asset( "application.#{type}" )
end
shared_asset?(path) click to toggle source

Determines whether an asset path is shared amongst all actions for its controller.

# File lib/tension/utils.rb, line 33
def shared_asset?(path)
  !!path.match(SHARED_SUFFIX)
end
strip_file_extension(path) click to toggle source
# File lib/tension/utils.rb, line 55
def strip_file_extension(path)
  path.gsub(EXTENSION_REGEX, "")
end

Private Class Methods

asset_path?(path) click to toggle source

Returns whether or not the given path represents an asset file for which Tension is useful: JavaScript or CSS.

ARGS: path: a String path like "admin/blog_common.css"
# File lib/tension/utils.rb, line 95
def asset_path?(path)
  path.match(/\.(#{ Tension::CSS }|#{ Tension::JS })\z/)
end
controller_for_asset_path(path) click to toggle source

Takes an asset path like “comments/base/index.js” and determines the controller (“comments/base”) that should serve that asset.

ARGS: path: a String path like "admin/blog_common.css"
# File lib/tension/utils.rb, line 79
def controller_for_asset_path(path)
  parts = path.split("/")

  if shared_asset?(parts.last)
    parts.push(strip_file_extension( parts.pop ))
  else
    parts.pop
  end

  return parts.any? ? parts.join("/") : nil
end
logical_asset_path(options) click to toggle source

Builds a String path for an asset based on the given hash params.

ARGS: options: a Hash containing e.g.
  controller: "blog/comments"
  action: "index"
  type: Tension::CSS
# File lib/tension/utils.rb, line 67
def logical_asset_path(options)
  if options[:action].nil?
    "#{options[:controller]}#{SHARED_SUFFIX}.#{options[:type]}"
  else
    "#{options[:controller]}/#{options[:action]}.#{options[:type]}"
  end
end