module Restful::Base::ClassMethods
Class macros to setup restful functionality
Constants
- ACTIONS
List of REST actions
Public Instance Methods
Restful
is the macro that setup a controller to become restful. This macro accepts 3 params:
Params¶ ↑
-
model: A required parameter which is a symbol of the model name.
-
route_prefix: A prefix string to be used with controller's url
helper.
-
actions: An array of actions that a controller should implement, if
none is passed then all seven REST actions are defined.
Examples¶ ↑
Simple:
class DocumentsController < ApplicationController include Restful::Base respond_to :html restful model: :document end
This definition will create the seven REST actions for Document model, this setup a single object instance variable @document and a collection variable @documents.
Route prefix:
class DocumentsController < ApplicationController include Restful::Base respond_to :html restful model: :document, route_prefix: 'admin' end
With route_prefix param every URL helper method in our controller will have the defined prefix.
`edit_resource_path` helper method will call internally `admin_edit_resource_path`.
Listed actions variation:
The last parameter actions allows you to list in an array the actions that you want your controller to have:
class DocumentsController < ApplicationController include Restful::Base respond_to :html restful model: :document, actions: [:index, :show] end
In this case our controller will only respond to those 2 actions. We can do it the other way, indicate list of actions that shouldn't be defined:
class DocumentsController < ApplicationController include Restful::Base respond_to :html restful model: :document, actions: [:all, except: [:destroy, :show]] end
For this last example all seven REST actions will be defined but :destroy and :show
Strong params Restful
provides 3 hooks for you to implement in your controller, two of these hooks will be called depending on the action that is being executed: :create_secure_params and :update_secure_params.
if you don't require a specific strong params definition for each action, then just implement :secure_params method and this will be called.
Considerations¶ ↑
From previous examples you must have notice by now that the respond_to macro es need it. This is because all REST actions call the respont_with method, which works with the respond_to macro. Just include it in your controllers and list the formats do you wish your controller to respond.
# File lib/restful/base.rb, line 334 def restful(model: nil, route_prefix: nil, actions: :all) class_attribute :model_name, :class_name, :route_prefix, instance_writer: false self.model_name = model self.class_name = class_from_name self.route_prefix = route_prefix include InstanceMethods include Restful::Actions setup_actions actions unless actions == :all if respond_to?(:helper_method) helper_method :collection, :resource, :resource_class, :edit_resource_path, :edit_resource_url, :new_resource_path, :new_resource_url, :collection_path, :collection_url end end
Protected Instance Methods
Method that gets the model class name from the passed symbol.
# File lib/restful/base.rb, line 378 def class_from_name if model_name.to_s.include? "_" ns, *klass = model_name.to_s.split("_").collect(&:camelize) begin "#{ns}::#{klass.join("")}".constantize rescue NameError "#{ns}#{klass.join("")}".constantize end else model_name.to_s.camelize.constantize end end
Method that calculates the actions to which our controller should respond to.
# File lib/restful/base.rb, line 360 def setup_actions(actions) keep_actions = actions if actions.include?(:all) keep_actions = ACTIONS end options = actions.extract_options! except_actions = options[:except] || [] keep_actions -= except_actions (ACTIONS - keep_actions).uniq.each do |action| undef_method action.to_sym, "#{action.to_sym}!" end end