Class: Tension::Context

Inherits:
Object
  • Object
show all
Defined in:
lib/tension/context.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(controller_path) ⇒ Context

Returns a new instance of Context.



5
6
7
# File 'lib/tension/context.rb', line 5

def initialize(controller_path)
  @controller = "#{ controller_path }_controller".classify.constantize
end

Dynamic Method Handling

This class handles dynamic methods through the method_missing method

#method_missing(method_sym, *args) ⇒ Object (private)



55
56
57
58
59
60
61
62
63
64
# File 'lib/tension/context.rb', line 55

def method_missing(method_sym, *args)
  action = method_sym.to_s
  type   = args.first

  if has_action?(action)
    best_asset(action, type)
  else
    super
  end
end

Instance Attribute Details

#actionObject (readonly)

Returns the value of attribute action.



3
4
5
# File 'lib/tension/context.rb', line 3

def action
  @action
end

#controllerObject (readonly)

Returns the value of attribute controller.



3
4
5
# File 'lib/tension/context.rb', line 3

def controller
  @controller
end

Instance Method Details

#action_asset(action, type) ⇒ Object

Returns the action-level asset for the given action name and type, or nil.



26
27
28
# File 'lib/tension/context.rb', line 26

def action_asset(action, type)
  Tension::Utils.find_asset( controller: controller.controller_path, action: action, type: type )
end

#controller_asset(type) ⇒ Object

Returns the controller-level asset for the given type, or nil.



32
33
34
# File 'lib/tension/context.rb', line 32

def controller_asset(type)
  Tension::Utils.find_asset( controller: controller.controller_path, type: type )
end

#css(action) ⇒ Object

Locates the best stylesheet for the given action name. Aliased as, for example, ‘#index( Tension::CSS )` or `#show(:css)`.



12
13
14
# File 'lib/tension/context.rb', line 12

def css(action)
  best_asset( action, Tension::CSS )
end

#global_asset(type) ⇒ Object

Returns the global asset for the given type (application.css,js).



38
39
40
# File 'lib/tension/context.rb', line 38

def global_asset(type)
  Tension::Utils.global_asset(type)
end

#has_action?(action_name) ⇒ Boolean

Returns:

  • (Boolean)


42
43
44
# File 'lib/tension/context.rb', line 42

def has_action?(action_name)
  controller.action_methods.include?(action_name)
end

#js(action) ⇒ Object

Locates the best JavaScript for the given action name. Aliased as, for example, ‘#index( Tension::JS )` or `#show(:js)`.



19
20
21
# File 'lib/tension/context.rb', line 19

def js(action)
  best_asset( action, Tension::JS )
end