Module: Waves::Controllers::Mixin
Overview
Waves::Controllers::Mixin adapts a controller class for use in mappings and provides utility methods.
It is included in controllers autocreated by the Default foundation, so you do not need to include it in subclasses of the same.
The utility methods include simple reflection to allow controller methods to be written generically (i.e., without reference to a specific model) and parameter destructuring for the request parameters.
Instance Attribute Summary collapse
-
#request ⇒ Object
readonly
Returns the value of attribute request.
Class Method Summary collapse
-
.included(mod) ⇒ Object
When this mixin is included it adds a class method named
process
, which accepts a request object and a block.
Instance Method Summary collapse
- #initialize(request) ⇒ Object
-
#model ⇒ Object
Returns the model corresponding to this controller by naively assuming that
model_name
must be correct. -
#model_name ⇒ Object
Returns the name of the model corresponding to this controller by taking the basename of the module and converting it to snake case.
-
#params ⇒ Object
The params variable is taken from the request object and “destructured”, so that a parameter named ‘blog.title’ becomes:.
Methods included from ResponseMixin
#blackboard, #controllers, #domain, #log, #models, #not_found, #path, #redirect, #response, #session, #url, #views
Instance Attribute Details
#request ⇒ Object (readonly)
Returns the value of attribute request.
90 91 92 |
# File 'lib/controllers/mixin.rb', line 90 def request @request end |
Class Method Details
.included(mod) ⇒ Object
When this mixin is included it adds a class method named process
, which accepts a request object and a block. The process
method initializes the controller with the request, then evaluates the block using instance_eval
. This allows the controller to be used from within a mapping lambda (i.e. a ResponseProxy).
99 100 101 102 103 |
# File 'lib/controllers/mixin.rb', line 99 def self.included( mod ) def mod.process( request, &block ) self.new( request ).instance_eval( &block ) end end |
Instance Method Details
#initialize(request) ⇒ Object
105 106 107 |
# File 'lib/controllers/mixin.rb', line 105 def initialize( request ) @request = request end |
#model ⇒ Object
Returns the model corresponding to this controller by naively assuming that model_name
must be correct. This allows you to write generic controller methods such as:
model.find( name )
to find an instance of a given model. Again, the plurality of the controller and model must be the same for this to work.
130 |
# File 'lib/controllers/mixin.rb', line 130 def model; Waves.application.models[ model_name.intern ]; end |
#model_name ⇒ Object
Returns the name of the model corresponding to this controller by taking the basename of the module and converting it to snake case. If the model plurality is different than the controller, this will not, in fact, be the model name.
121 |
# File 'lib/controllers/mixin.rb', line 121 def model_name; self.class.basename.snake_case; end |
#params ⇒ Object
The params variable is taken from the request object and “destructured”, so that a parameter named ‘blog.title’ becomes:
params['blog']['title']
If you want to access the original parameters object, you can still do so using request.params
instead of simply params
.
116 |
# File 'lib/controllers/mixin.rb', line 116 def params; @params ||= destructure(request.params); end |