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



64
65
66
# File 'lib/jsonapi/resource_controller.rb', line 64

def create
  process_request_operations
end

#create_associationObject



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

def create_association
  process_request_operations
end

#create_operations_processorObject

Override this to use another operations processor



89
90
91
# File 'lib/jsonapi/resource_controller.rb', line 89

def create_operations_processor
  JSONAPI::ActiveRecordOperationsProcessor.new
end

#destroyObject



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

def destroy
  process_request_operations
end

#destroy_associationObject



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

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(resource_klass).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
# File 'lib/jsonapi/resource_controller.rb', line 30

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

  resources = if keys.length > 1
                resource_klass.find_by_keys(keys, context: context)
              else
                resource_klass.find_by_key(keys[0], context: context)
              end

  render json: JSONAPI::ResourceSerializer.new(resource_klass).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



49
50
51
52
53
54
55
56
57
58
59
60
61
62
# File 'lib/jsonapi/resource_controller.rb', line 49

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: { key_formatter.format(association_type) => parent_resource.send(association.foreign_key)}
rescue => e
  # :nocov:
  handle_exceptions(e)
  # :nocov:
end

#updateObject



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

def update
  process_request_operations
end

#update_associationObject



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

def update_association
  process_request_operations
end