class ClassAction::Action
Base class for controller actions.
Attributes
_responders[R]
Responders
_responses[R]
Responders
helpers[RW]
Helpers
Public Class Methods
_action_methods()
click to toggle source
# File lib/class_action/action.rb, line 68 def _action_methods methods = public_instance_methods methods -= [ :_execute, :available? ] methods -= Object.public_instance_methods methods end
_controller_method(method)
click to toggle source
Exposes the given controller methods into the action.
# File lib/class_action/action.rb, line 35 def _controller_method(method) class_eval <<-RUBY, __FILE__, __LINE__+1 def #{method}(*args, &block) copy_assigns_to_controller controller.send :#{method}, *args, &block ensure copy_assigns_from_controller end protected :#{method} RUBY end
new(controller)
click to toggle source
Initialization
# File lib/class_action/action.rb, line 12 def initialize(controller) @_controller = controller @_controller.singleton_class.send :include, self.class.helpers end
Private Class Methods
helper_method(*methods)
click to toggle source
# File lib/class_action/action.rb, line 149 def helper_method(*methods) methods.each do |method| helpers.class_eval <<-RUBY, __FILE__, __LINE__+1 def #{method}(*args, &block) controller = if respond_to?(:class_action) self else self.controller end controller.class_action.send(:#{method}, *args, &block) end RUBY end end
respond_to(*formats, on: nil, &block)
click to toggle source
Defines a response block for the given format(s). Specify an optional precondition in the `on` parameter.
# File lib/class_action/action.rb, line 195 def respond_to(*formats, on: nil, &block) formats.each do |format| _responders[ [format.to_sym, on.try(:to_sym)] ] = block end end
respond_to_any(on: nil, &block)
click to toggle source
Defines a response block for any remaining format. Specify an optional precondition in the `on` parameter.
# File lib/class_action/action.rb, line 202 def respond_to_any(on: nil, &block) respond_to :any, on: on, &block end
respond_with(method, on: nil)
click to toggle source
Defines a method that returns the response. Specify an optional precondition in the `on` parameter.
# File lib/class_action/action.rb, line 190 def respond_with(method, on: nil) _responses[on.try(:to_sym)] = method end
Public Instance Methods
_execute()
click to toggle source
# File lib/class_action/action.rb, line 77 def _execute raise ActionNotAvailable unless available? # Execute the action by running all public methods in order. self.class._action_methods.each do |method| next if self.method(method).arity != 0 send method # Break execution of the action when some response body is set. # E.g. when the action decides to redirect halfway. break if controller.response_body end # Perform a default response if not done so yet. _respond unless controller.response_body end
available?()
click to toggle source
# File lib/class_action/action.rb, line 25 def available? true end
respond_to?(method, include_private = false)
click to toggle source
Calls superclass method
# File lib/class_action/action.rb, line 49 def respond_to?(method, include_private = false) super || (include_private && controller.respond_to?(method, true)) end
Protected Instance Methods
controller()
click to toggle source
Attributes
# File lib/class_action/action.rb, line 20 def controller @_controller end
Private Instance Methods
_respond()
click to toggle source
# File lib/class_action/action.rb, line 97 def _respond copy_assigns_to_controller response = self.class._responses.find do |on, response| !on || send(:"#{on}?") end.try(:last) if response response_object = if response =~ /^@/ instance_variable_get(response) else send(response) end controller.respond_with response_object, &_respond_block elsif _respond_block controller.respond_to &_respond_block end end
_respond_block()
click to toggle source
# File lib/class_action/action.rb, line 117 def _respond_block responders = {} self.class._responders.each do |(format, on), block| # Select only those responders that have a block, and for which no precondition is set, or # one that matches the current action state. responders[format] ||= block if block && (!on || send(:"#{on}?")) end return if responders.empty? action = self proc do |collector| responders.each do |format, block| collector.send(format) do action.instance_exec &block copy_assigns_to_controller end end end end
copy_assigns_from_controller()
click to toggle source
Assigns
# File lib/class_action/action.rb, line 213 def copy_assigns_from_controller controller.view_assigns.each do |key, value| instance_variable_set "@#{key}", value end end
copy_assigns_to_controller()
click to toggle source
# File lib/class_action/action.rb, line 219 def copy_assigns_to_controller ivars = instance_variables ivars -= [ :@_controller, :@_responders, :@_default_responder ] ivars.each do |ivar| controller.instance_variable_set ivar, instance_variable_get(ivar) end end
method_missing(method, *args, &block)
click to toggle source
Calls superclass method
# File lib/class_action/action.rb, line 53 def method_missing(method, *args, &block) if controller.respond_to?(method, true) self.class._controller_method method send method, *args, &block else super end end