Class: Ramaze::Action

Inherits:
Struct::Action show all
Includes:
Helper::Link
Defined in:
lib/ramaze/action.rb,
lib/ramaze/action/render.rb,
lib/ramaze/helper/aspect.rb,
lib/ramaze/contrib/auto_params.rb,
lib/ramaze/action.rb

Overview

The Action holds information that is essential to render the action for a request.

Direct Known Subclasses

Dispatcher::ActionProfiler

Class Method Summary collapse

Instance Method Summary collapse

Methods inherited from Struct

fill, #values_at

Class Method Details

.create(hash = {}) ⇒ Object

Instantiate with given Hash, takes both string/symbol keys. Only keys that match members of the Action-Struct are used.



28
29
30
31
32
33
34
# File 'lib/ramaze/action.rb', line 28

def create(hash = {})
  i = new
  members.each do |key|
    i.send("#{key}=", (hash[key] || hash[key.to_sym]))
  end
  i
end

.currentObject

alias for stack.last, returns the instance of Action you are currently in.



38
39
40
# File 'lib/ramaze/action.rb', line 38

def current
  stack.last
end

.stackObject

Return the stacked actions for the current request



43
44
45
# File 'lib/ramaze/action.rb', line 43

def stack
  Thread.current[:action_stack] ||= []
end

Instance Method Details

#after_processObject

Hook for AspectHelper



93
94
95
# File 'lib/ramaze/helper/aspect.rb', line 93

def after_process
  common_aspect(:after)
end

#before_processObject

Hook for AspectHelper



85
86
87
# File 'lib/ramaze/helper/aspect.rb', line 85

def before_process
  common_aspect(:before)
end

#bindingObject

Returns a binding of the instance, will be cached on first access.



108
109
110
# File 'lib/ramaze/action.rb', line 108

def binding
  self[:binding] ||= instance.instance_eval{ binding }
end

#common_aspect(aspect) ⇒ Object



97
98
99
100
101
102
103
104
105
106
107
# File 'lib/ramaze/helper/aspect.rb', line 97

def common_aspect(aspect)
  return unless path

  controller.relevant_ancestors.reverse_each do |controller|
    next unless aspects = controller.trait[:aspects]

    [ aspects[aspect][name], aspects[aspect][:all] ].compact.map do |block|
      instance.instance_eval(&block) if block
    end
  end
end

#engineObject

Determines based on controller.trait and the template extensions which engine has to be used. Defaults to Template::Ezamar



86
87
88
89
90
91
92
93
94
95
96
97
98
# File 'lib/ramaze/action.rb', line 86

def engine
  return self[:engine] if self[:engine]
  default = controller.trait.fetch(:engine, Template::Ezamar)
  return default unless template

  Template::ENGINES.sort_by{|v| v.join}.each do |(engine, exts)|
    if template =~ /\.(#{Regexp.union(*exts)})$/
      return self[:engine] = engine
    end
  end

  self[:engine] = default
end

#extended_pathObject

combined path to current action, from path and params



118
119
120
# File 'lib/ramaze/action.rb', line 118

def extended_path
  Array[path, *params].join('/')
end

#full_pathObject



122
123
124
# File 'lib/ramaze/action.rb', line 122

def full_path
  self.controller.mapping/extended_path
end

#instanceObject

Returns an instance of controller, will be cached on first access.



102
103
104
# File 'lib/ramaze/action.rb', line 102

def instance
  self[:instance] ||= controller.new
end

#method=(meth) ⇒ Object

Set the method, will be converted to a string and set to nil if empty.



57
58
59
60
# File 'lib/ramaze/action.rb', line 57

def method=(meth)
  meth = meth.to_s
  self[:method] = (meth.empty? ? nil : meth)
end

#nameObject

Try to figure out a sane name for current action.



113
114
115
# File 'lib/ramaze/action.rb', line 113

def name
  File.basename((self[:method] || self[:template]).to_s).split('.').first
end

#params=(*par) ⇒ Object

runs all parameters assigned through flatten and CGI::unescape



45
46
47
48
49
50
51
52
53
54
55
56
57
58
# File 'lib/ramaze/contrib/auto_params.rb', line 45

def params=(*par)
  par = *par
  self[:params] = par.map{ |pa|
    case pa
    when Array
      pa.map{|p| CGI.unescape(p.to_s)}
    when nil
      nil
    else
      CGI.unescape(pa.to_s)
    end
  } unless par.nil?
  self[:params] ||= []
end

#relaxed_hashObject

Use this as key for caches.



70
71
72
# File 'lib/ramaze/action.rb', line 70

def relaxed_hash
  [controller, method, params, template, path].hash
end

#renderObject

Render this instance of Action, this will (eventually) pass itself to Action#engine.transform Usage, given that Foo is a Controller and has the method/template for index:

> Action(:controller => Foo).render
#> 'bar'


26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
# File 'lib/ramaze/action/render.rb', line 26

def render
  Log.dev("Action: #{self}")
  stack do
    if should_cache?
      # Ignore cache if there is flash session data as the response probably
      # expects to include it, making it unique for this user and request.
      if Global.no_cache_flash && !Current.session.flash.empty?
        Log.debug("Action caching ignored as session flash data is present.")
        uncached_render
      else
        cached_render
      end
    else
      uncached_render
    end
  end
end

#stackObject

Use your block and jump into the Action::stack - this allows you to call nested actions.



9
10
11
12
13
14
15
16
17
# File 'lib/ramaze/action/render.rb', line 9

def stack
  Action.stack << self
  yield self
rescue Object => ex
  Log.error "#{ex} in: #{self}"
  raise ex
ensure
  Action.stack.pop
end

#to_hashObject

A Hash representation of Action



76
77
78
79
80
# File 'lib/ramaze/action.rb', line 76

def to_hash
  hash = {}
  members.each{|m| hash[m.to_sym] = send(m)}
  hash
end

#to_sObject

nicer representation of the Action



50
51
52
53
# File 'lib/ramaze/action.rb', line 50

def to_s
  m, p, t = method.inspect, params.inspect, template.inspect
  %{#<Action method=#{m}, params=#{p} template=#{t}>}
end

#valid_rest?Boolean

Returns:

  • (Boolean)


136
137
138
139
140
141
142
143
144
145
# File 'lib/ramaze/action.rb', line 136

def valid_rest?
  return true unless rest = controller.trait[:REST]
  meth = Request.current.request_method

  return true if rest[:any].include?(name)

  if rest.has_key?(meth)
    rest[meth].include?(name)
  end
end