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



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

def create
  process_request_operations
end

#create_associationObject



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

def create_association
  process_request_operations
end

#create_operations_processorObject

Override this to use another operations processor



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

def create_operations_processor
  JSONAPI::ActiveRecordOperationsProcessor.new
end

#destroyObject



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

def destroy
  process_request_operations
end

#destroy_associationObject



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

def destroy_association
  process_request_operations
end

#indexObject



25
26
27
28
29
30
31
32
33
# File 'lib/jsonapi/resource_controller.rb', line 25

def index
  render json: JSONAPI::ResourceSerializer.new.serialize(
      resource_klass.find(resource_klass.verify_filters(@request.filters, context), context),
      @request.includes,
      @request.fields,
      context)
rescue => e
  handle_exceptions(e)
end

#showObject



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

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

  resources = []
  keys.each do |key|
    resources.push(resource_klass.find_by_key(key, context))
  end

  render json: JSONAPI::ResourceSerializer.new.serialize(
      resources,
      @request.includes,
      @request.fields,
      context)
rescue => e
  handle_exceptions(e)
end

#show_associationObject



52
53
54
55
56
57
58
59
60
61
62
63
# 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)

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

#updateObject



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

def update
  process_request_operations
end