Module: Restful::Base::ClassMethods

Defined in:
lib/restful/base.rb

Overview

Class macros to setup restful functionality

Constant Summary collapse

ACTIONS =

List of REST actions

[:index, :show, :edit, :update, :new, :create, :destroy]

Instance Method Summary collapse

Instance Method Details

#restful(model: nil, route_prefix: nil, actions: :all) ⇒ Object

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.


333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
# File 'lib/restful/base.rb', line 333

def restful(model: nil, route_prefix: nil, actions: :all)
  self.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