Module: CarrotRpc::RpcServer::JSONAPIResources::Actions

Defined in:
lib/carrot_rpc/rpc_server/jsonapi_resources/actions.rb

Overview

The common CRUD actions for CarrotRpc::RpcServer::JSONAPIResources

Constant Summary collapse

NAME_SET =

Set of allowed actions for JSONAPI::Resources

Set.new(
  [
    # Mimic behaviour of `POST <collection>` routes
    :create,
    # Mimic behaviour of `POST <collection>/<id>/relationships/<relation>` routes
    :create_relationship,
    # Mimic behavior of `DELETE <collection>/<id>` routes
    :destroy,
    # Mimic behavior of `DELETE <collection>/<id>/relationships/<relationship>` routes
    :destroy_relationship,
    # Mimics behavior of `GET <collection>/<id>/<relationship>` routes
    :get_related_resource,
    # Mimic behavior of `GET <collection>` routes
    :index,
    # Mimic behavior of `GET <collection>/<id>` routes
    :show,
    # Mimic behavior of `GET <collection>/<id>/relationships/<relationship>` routes
    :show_relationship,
    # Mimic behavior of `PATCH|PUT <collection>/<id>` routes
    :update,
    # Mimic behavior of `PATCH|PUT <collection>/<id>/relationships/<relationship>` routes
    :update_relationship
  ]
).freeze

Class Method Summary collapse

Instance Method Summary collapse

Class Method Details

.define_action_method(action_module, name) ⇒ void

This method returns an undefined value.

Defines an action method, ‘name` on `action_module`.

Parameters:

  • action_module (Module)

    Module where action methods are defined so that they can be called with ‘super` if overridden.

  • name (Symbol)

    an element of ‘NAME_SET`.



43
44
45
46
47
48
49
50
51
52
53
54
# File 'lib/carrot_rpc/rpc_server/jsonapi_resources/actions.rb', line 43

def self.define_action_method(action_module, name)
  action_module.send(:define_method, name) do |params|
    process_request_params(
      ActionController::Parameters.new(
        params.merge(
          action: name,
          controller: controller
        )
      )
    )
  end
end

Instance Method Details

#actions(*names) ⇒ void

This method returns an undefined value.

Adds actions in ‘names` to the current class.

The actions are added to a mixin module ‘self::Actions`, so that the action methods can be overridden and `super` will work.

Examples:

Adding only show actions

extend RpcServer::JSONAPIResources::Actions
include RpcServer::JSONAPIResources

actions :create,
        :destroy,
        :index,
        :show,
        :update

Parameters:

  • names (Array<Symbol>)

    a array of a subset of NAME_SET.



78
79
80
81
82
83
84
85
86
87
88
89
90
91
# File 'lib/carrot_rpc/rpc_server/jsonapi_resources/actions.rb', line 78

def actions(*names)
  valid_actions!(names)

  # an include module so that `super` works if the method is overridden
  action_module = Module.new

  names.each do |name|
    CarrotRpc::RpcServer::JSONAPIResources::Actions.define_action_method(action_module, name)
  end

  const_set(:Actions, action_module)

  include action_module
end