class AbstractController::Base
the context.
their own ‘render` method, since rendering means different things depending on
directly, and subclasses (like ActionController::Base) are expected to provide
AbstractController::Base is a low-level API. Nobody should be using it
# Abstract Controller Base
def self.supports_path?
subclass of `AbstractController::Base` may return false. An Email controller
Returns true if the given controller is capable of rendering a path. A
def self.supports_path? true end
def _find_action_name(action_name)
* false - No valid method name could be found.
* `string` - The name of the method that handles the action
#### Returns
* `action_name` - An action name to find a method name for
#### Parameters
See method_for_action for more information.
It checks if the action name is valid and returns false otherwise.
action.
Takes an action name and returns the name of the method that will handle the
def _find_action_name(action_name) _valid_action_name?(action_name) && method_for_action(action_name) end
def _handle_action_missing(*args)
found, #method_for_action will return "_handle_action_missing". This method
If the action name was not found, but a method called "action_missing" was
def _handle_action_missing(*args) action_missing(@_action_name, *args) end
def _valid_action_name?(action_name)
def _valid_action_name?(action_name) !action_name.to_s.include? File::SEPARATOR end
def abstract!
def abstract! @abstract = true end
def action_method?(name)
* `name` - The name of an action to be tested
#### Parameters
defined in the controller.
Returns true if the name can be considered an action because it has a method
def action_method?(name) self.class.action_methods.include?(name) end
def action_methods
* `Set` - A set of all methods that should be considered actions.
#### Returns
exist on the class itself.
internal_methods), adding back in any methods that are internal, but still
public instance methods on a controller, less any internal methods (see
A list of method names that should be considered actions. This includes all
def action_methods @action_methods ||= begin # All public instance methods of this class, including ancestors except for # public instance methods of Base and its ancestors. methods = public_instance_methods(true) - internal_methods # Be sure to include shadowed public instance methods of this class. methods.concat(public_instance_methods(false)) methods.map!(&:to_s) methods.to_set end end
def action_methods
def action_methods self.class.action_methods end
def available_action?(action_name)
* `action_name` - The name of an action to be tested
#### Parameters
ones.
that are also available through other means, for example, implicit render
`available_action?("foo")` returns true because this method considers actions
Notice that `action_methods.include?("foo")` may return false and
false otherwise.
Returns true if a method for the action is available and can be dispatched,
def available_action?(action_name) _find_action_name(action_name) end
def clear_action_methods!
::clear_action_methods! allows you to do that, so next time you run
action_methods are cached and there is sometimes a need to refresh them.
def clear_action_methods! @action_methods = nil end
def controller_path
* `String`
#### Returns
MyApp::MyPostsController.controller_path # => "my_app/my_posts"
end
class MyApp::MyPostsController < AbstractController::Base
Returns the full controller name, underscored, without the ending Controller.
def controller_path @controller_path ||= name.delete_suffix("Controller").underscore unless anonymous? end
def controller_path
def controller_path self.class.controller_path end
def eager_load! # :nodoc:
def eager_load! # :nodoc: action_methods nil end
def inherited(klass) # :nodoc:
def inherited(klass) # :nodoc: # Define the abstract ivar on subclasses so that we don't get uninitialized ivar # warnings unless klass.instance_variable_defined?(:@abstract) klass.instance_variable_set(:@abstract, false) end super end
def inspect # :nodoc:
def inspect # :nodoc: "#<#{self.class.name}:#{'%#016x' % (object_id << 1)}>" end
def internal_methods
removed. (ActionController::Metal and ActionController::Base are defined as
considered action methods, so methods declared on abstract classes are being
that abstract class. Public instance methods of a controller would normally be
superclass of a controller, and gets a list of all public instance methods on
A list of all internal methods for a controller. This finds the first abstract
def internal_methods controller = self methods = [] until controller.abstract? methods += controller.public_instance_methods(false) controller = controller.superclass end controller.public_instance_methods(true) - methods end
def method_added(name)
def method_added(name) super clear_action_methods! end
def method_for_action(action_name)
* `nil` - No method name could be found.
* `string` - The name of the method that handles the action
#### Returns
* `action_name` - An action name to find a method name for
#### Parameters
an `AbstractController::ActionNotFound` exception will be raised.
If none of these conditions are true, and `method_for_action` returns `nil`,
method (like `_handle_method_missing`) to handle the case.
If you override this method to handle additional cases, you may also provide a
matching the action name is considered to exist.
be considered an action. For instance, an HTTP controller with a template
Subclasses may override this method to add additional conditions that should
is found.
look for an #action_missing method and return "_handle_action_missing" if one
default, if #method_for_action receives a name that is not an action, it will
action. In normal cases, this method returns the same name as it receives. By
Takes an action name and returns the name of the method that will handle the
def method_for_action(action_name) if action_method?(action_name) action_name elsif respond_to?(:action_missing, true) "_handle_action_missing" end end
def performed?
Tests if a response body is set. Used to determine if the `process_action`
def performed? response_body end
def process(action, ...)
* `self`
#### Returns
error is raised.
If no method can handle the action, then an AbstractController::ActionNotFound
The actual method that is called is determined by calling #method_for_action.
Calls the action going through the entire Action Dispatch stack.
def process(action, ...) @_action_name = action.to_s unless action_name = _find_action_name(@_action_name) raise ActionNotFound.new("The action '#{action}' could not be found for #{self.class.name}", self, action) end @_response_body = nil process_action(action_name, ...) end
def process_action(...)
Notice that the first argument is the method to be dispatched which is **not**
action dispatching.
processing an action. This, and not #process, is the intended way to override
Call the action. Override this in a subclass to modify the behavior around
def process_action(...) send_action(...) end