Class: JSONAPI::ResourceController

Inherits:
ActionController::Base
  • Object
show all
Includes:
ResourceFor
Defined in:
lib/jsonapi/resource_controller.rb

Instance Method Summary collapse

Methods included from ResourceFor

included

Instance Method Details

#createObject



67
68
69
# File 'lib/jsonapi/resource_controller.rb', line 67

def create
  process_request_operations
end

#create_associationObject



71
72
73
# File 'lib/jsonapi/resource_controller.rb', line 71

def create_association
  process_request_operations
end

#create_operations_processorObject

Override this to use another operations processor



92
93
94
# File 'lib/jsonapi/resource_controller.rb', line 92

def create_operations_processor
  JSONAPI::ActiveRecordOperationsProcessor.new
end

#destroyObject



83
84
85
# File 'lib/jsonapi/resource_controller.rb', line 83

def destroy
  process_request_operations
end

#destroy_associationObject



87
88
89
# File 'lib/jsonapi/resource_controller.rb', line 87

def destroy_association
  process_request_operations
end

#indexObject



18
19
20
21
22
23
24
25
26
27
28
# File 'lib/jsonapi/resource_controller.rb', line 18

def index
  render json: JSONAPI::ResourceSerializer.new.serialize_to_hash(
      resource_klass.find(resource_klass.verify_filters(@request.filters, context),
        context: context, sort_params: @request.sort_params),
      include: @request.include,
      fields: @request.fields,
      attribute_formatters: attribute_formatters,
      key_formatter: key_formatter)
rescue => e
  handle_exceptions(e)
end

#showObject



30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
# File 'lib/jsonapi/resource_controller.rb', line 30

def show
  keys = parse_key_array(params[resource_klass._primary_key])

  if keys.length > 1
    resources = []
    keys.each do |key|
      resources.push(resource_klass.find_by_key(key, context: context))
    end
  else
    resources = resource_klass.find_by_key(keys[0], context: context)
  end

  render json: JSONAPI::ResourceSerializer.new.serialize_to_hash(
      resources,
      include: @request.include,
      fields: @request.fields,
      attribute_formatters: attribute_formatters,
      key_formatter: key_formatter)
rescue => e
  handle_exceptions(e)
end

#show_associationObject



52
53
54
55
56
57
58
59
60
61
62
63
64
65
# File 'lib/jsonapi/resource_controller.rb', line 52

def show_association
  association_type = params[:association]

  parent_key = params[resource_klass._as_parent_key]

  parent_resource = resource_klass.find_by_key(parent_key, context: context)

  association = resource_klass._association(association_type)
  render json: { association_type => parent_resource.send(association.foreign_key)}
rescue => e
  # :nocov:
  handle_exceptions(e)
  # :nocov:
end

#updateObject



79
80
81
# File 'lib/jsonapi/resource_controller.rb', line 79

def update
  process_request_operations
end

#update_associationObject



75
76
77
# File 'lib/jsonapi/resource_controller.rb', line 75

def update_association
  process_request_operations
end