Class: Api::CustomActionDispatcher

Inherits:
Object
  • Object
show all
Defined in:
lib/api/custom_action_dispatcher.rb

Class Method Summary collapse

Class Method Details

.call(model, params, request) ⇒ Object

Dispatch a custom action if the request signals one. Returns false if this is not a custom action call. Returns [true, body, status] when dispatched. Raises NoMethodError if the action name is present but not found.



7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
# File 'lib/api/custom_action_dispatcher.rb', line 7

def self.call(model, params, request)
  custom_action = if !params[:do].blank?
      params[:do]
    elsif request.url.include?("/custom_action/")
      params[:action_name]
    end
  return false unless custom_action

  params[:request_url] = request.url
  params[:remote_ip] = request.remote_ip
  params[:request_verb] = request.request_method
  params[:token] = extract_bearer(request)

  Rails.logger.debug("CustomActionDispatcher: #{custom_action} on #{model}")

  if model.respond_to?("custom_action_#{custom_action}")
    body, status = model.send("custom_action_#{custom_action}", params)
  elsif ("Endpoints::#{model}".constantize rescue false) &&
        "Endpoints::#{model}".constantize.instance_methods.include?(custom_action.to_sym)
    body, status = "Endpoints::#{model}".constantize.new(custom_action, params).result
  else
    raise NoMethodError
  end

  [true, body, status]
end