Module: Tension::Utils

Defined in:
lib/tension/utils.rb

Constant Summary collapse

SHARED_SUFFIX =
"_common".freeze
SHARED_REGEX =
/#{SHARED_SUFFIX}\..*\z/.freeze
EXTENSION_REGEX =
/(#{SHARED_SUFFIX})?\..*\z/.freeze
CONTROLLER_REGEX =

Matches strings like “blog/comments”.

/([\w\/]+[^\.])/.freeze

Class Method Summary collapse

Class Method Details

.action_name(path) ⇒ Object

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



26
27
28
# 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) ⇒ Object

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

ARGS: path: a String path like "admin/blog_common.css"


15
16
17
18
19
20
21
22
# 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) ⇒ Object

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


44
45
46
# File 'lib/tension/utils.rb', line 44

def find_asset(options)
  Tension.environment.assets[ logical_asset_path(options) ]
end

.global_asset(type) ⇒ Object

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

ARGS: type: Tension::JS or Tension::CSS


51
52
53
# File 'lib/tension/utils.rb', line 51

def global_asset(type)
  Tension.environment.assets[ "application.#{type}" ]
end

.shared_asset?(path) ⇒ Boolean

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

Returns:

  • (Boolean)


33
34
35
# File 'lib/tension/utils.rb', line 33

def shared_asset?(path)
  !!path.match(SHARED_SUFFIX)
end