Class: Ramaze::Action
- Includes:
- Helper::Link
- Defined in:
- lib/ramaze/action.rb,
lib/ramaze/action.rb,
lib/ramaze/action/render.rb,
lib/ramaze/helper/aspect.rb
Overview
The Action holds information that is essential to render the action for a request.
Direct Known Subclasses
Class Method Summary collapse
-
.create(hash = {}) ⇒ Object
Instantiate with given Hash, takes both string/symbol keys.
-
.current ⇒ Object
alias for stack.last, returns the instance of Action you are currently in.
-
.stack ⇒ Object
Return the stacked actions for the current request.
Instance Method Summary collapse
-
#after_process ⇒ Object
overwrites the default Action hook and runs the neccesary blocks in its scope after actions are run, starting from Ramaze::Controller down the ancestor chain.
-
#before_process ⇒ Object
overwrites the default Action hook and runs the neccesary blocks in its scope before actions are run, starting from Ramaze::Controller down the ancestor chain.
-
#binding ⇒ Object
Returns a binding of the instance, will be cached on first access.
- #common_aspect(aspect) ⇒ Object
-
#engine ⇒ Object
Determines based on controller.trait and the template extensions which engine has to be used.
-
#extended_path ⇒ Object
combined path to current action, from path and params.
-
#full_path ⇒ Object
same as Ramaze::Action#extended_path, with mapping of the current controller prepended.
-
#instance ⇒ Object
Returns an instance of controller, will be cached on first access.
-
#method=(meth) ⇒ Object
Set the method, will be converted to a string and set to nil if empty.
-
#name ⇒ Object
Try to figure out a sane name for current action.
-
#params=(*par) ⇒ Object
runs all parameters assigned through flatten and unescape.
-
#relaxed_hash ⇒ Object
Use this as key for caches.
-
#render ⇒ Object
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’.
-
#stack ⇒ Object
Use your block and jump into the Action::stack - this allows you to call nested actions.
-
#to_hash ⇒ Object
A Hash representation of Action.
-
#to_s ⇒ Object
nicer representation of the Action.
-
#valid_rest? ⇒ Boolean
Returns true if current request is valid REST request.
Methods included from Helper::Link
#A, #R, #Rs, #breadcrumbs
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 |
.current ⇒ Object
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 |
Instance Method Details
#after_process ⇒ Object
overwrites the default Action hook and runs the neccesary blocks in its scope after actions are run, starting from Ramaze::Controller down the ancestor chain.
137 138 |
# File 'lib/ramaze/action.rb', line 137 def after_process end |
#before_process ⇒ Object
overwrites the default Action hook and runs the neccesary blocks in its scope before actions are run, starting from Ramaze::Controller down the ancestor chain.
132 133 |
# File 'lib/ramaze/action.rb', line 132 def before_process end |
#binding ⇒ Object
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
93 94 95 96 97 98 99 100 101 102 103 |
# File 'lib/ramaze/helper/aspect.rb', line 93 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 |
#engine ⇒ Object
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_path ⇒ Object
combined path to current action, from path and params
120 121 122 |
# File 'lib/ramaze/action.rb', line 120 def extended_path (path == "index" && !params.empty? ? params : Array[path, *params]).join('/') end |
#full_path ⇒ Object
same as Ramaze::Action#extended_path, with mapping of the current controller prepended.
126 127 128 |
# File 'lib/ramaze/action.rb', line 126 def full_path File.join(self.controller.mapping, extended_path) end |
#instance ⇒ Object
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 |
#name ⇒ Object
Try to figure out a sane name for current action.
114 115 116 |
# File 'lib/ramaze/action.rb', line 114 def name File.basename((self[:method] || self[:template]).to_s).split('.').first end |
#params=(*par) ⇒ Object
runs all parameters assigned through flatten and unescape
64 65 66 |
# File 'lib/ramaze/action.rb', line 64 def params=(*par) self[:params] = par.flatten.compact.map{|pa| Rack::Utils.unescape(pa.to_s) } end |
#relaxed_hash ⇒ Object
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 |
#render ⇒ Object
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 |
#stack ⇒ Object
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_hash ⇒ Object
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_s ⇒ Object
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 true if current request is valid REST request.
142 143 144 145 146 147 148 149 150 151 |
# File 'lib/ramaze/action.rb', line 142 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 |