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



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

def create
  process_request_operations
end

#create_associationObject



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

def create_association
  process_request_operations
end

#create_operations_processorObject

Override this to use another operations processor



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

def create_operations_processor
  JSONAPI::ActiveRecordOperationsProcessor.new
end

#destroyObject



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

def destroy
  process_request_operations
end

#destroy_associationObject



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

def destroy_association
  process_request_operations
end

#indexObject



18
19
20
21
22
23
24
25
26
27
# 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),
      include: @request.include,
      fields: @request.fields,
      attribute_formatters: attribute_formatters,
      key_formatter: key_formatter)
rescue => e
  handle_exceptions(e)
end

#showObject



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

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))
    end
  else
    resources = resource_klass.find_by_key(keys[0], 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



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

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)

  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



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

def update
  process_request_operations
end

#update_associationObject



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

def update_association
  process_request_operations
end