Module: JSONAPI::ActsAsResourceController

Included in:
ResourceController
Defined in:
lib/jsonapi/acts_as_resource_controller.rb

Defined Under Namespace

Modules: ClassMethods

Constant Summary collapse

MEDIA_TYPE_MATCHER =
/(.+".+"[^,]*|[^,]+)/
ALL_MEDIA_TYPES =
'*/*'

Class Method Summary collapse

Instance Method Summary collapse

Class Method Details

.included(base) ⇒ Object


8
9
10
11
12
13
14
15
# File 'lib/jsonapi/acts_as_resource_controller.rb', line 8

def self.included(base)
  base.extend ClassMethods
  base.include Callbacks
  base.before_action :ensure_correct_media_type, only: [:create, :update, :create_relationship, :update_relationship]
  base.before_action :ensure_valid_accept_media_type
  base.cattr_reader :server_error_callbacks
  base.define_jsonapi_resources_callbacks :process_operations
end

Instance Method Details

#createObject


29
30
31
# File 'lib/jsonapi/acts_as_resource_controller.rb', line 29

def create
  process_request
end

#create_relationshipObject


33
34
35
# File 'lib/jsonapi/acts_as_resource_controller.rb', line 33

def create_relationship
  process_request
end

#destroyObject


45
46
47
# File 'lib/jsonapi/acts_as_resource_controller.rb', line 45

def destroy
  process_request
end

#destroy_relationshipObject


49
50
51
# File 'lib/jsonapi/acts_as_resource_controller.rb', line 49

def destroy_relationship
  process_request
end

53
54
55
# File 'lib/jsonapi/acts_as_resource_controller.rb', line 53

def get_related_resource
  process_request
end

57
58
59
# File 'lib/jsonapi/acts_as_resource_controller.rb', line 57

def get_related_resources
  process_request
end

#indexObject


17
18
19
# File 'lib/jsonapi/acts_as_resource_controller.rb', line 17

def index
  process_request
end

#operation_dispatcherObject


96
97
98
99
100
# File 'lib/jsonapi/acts_as_resource_controller.rb', line 96

def operation_dispatcher
  @operation_dispatcher ||= JSONAPI::OperationDispatcher.new(transaction: transaction,
                                                             rollback: rollback,
                                                             server_error_callbacks: @request.server_error_callbacks)
end

#process_operationsObject


76
77
78
79
80
# File 'lib/jsonapi/acts_as_resource_controller.rb', line 76

def process_operations
  run_callbacks :process_operations do
    @operation_results = operation_dispatcher.process(@request.operations)
  end
end

#process_requestObject


61
62
63
64
65
66
67
68
69
70
71
72
73
74
# File 'lib/jsonapi/acts_as_resource_controller.rb', line 61

def process_request
  @request = JSONAPI::RequestParser.new(params, context: context,
                                        key_formatter: key_formatter,
                                        server_error_callbacks: (self.class.server_error_callbacks || []))
  unless @request.errors.empty?
    render_errors(@request.errors)
  else
    process_operations
    render_results(@operation_results)
  end

rescue => e
  handle_exceptions(e)
end

#rollbackObject


90
91
92
93
94
# File 'lib/jsonapi/acts_as_resource_controller.rb', line 90

def rollback
  lambda {
    fail ActiveRecord::Rollback
  }
end

#showObject


21
22
23
# File 'lib/jsonapi/acts_as_resource_controller.rb', line 21

def show
  process_request
end

#show_relationshipObject


25
26
27
# File 'lib/jsonapi/acts_as_resource_controller.rb', line 25

def show_relationship
  process_request
end

#transactionObject


82
83
84
85
86
87
88
# File 'lib/jsonapi/acts_as_resource_controller.rb', line 82

def transaction
  lambda { |&block|
    ActiveRecord::Base.transaction do
      block.yield
    end
  }
end

#updateObject


41
42
43
# File 'lib/jsonapi/acts_as_resource_controller.rb', line 41

def update
  process_request
end

#update_relationshipObject


37
38
39
# File 'lib/jsonapi/acts_as_resource_controller.rb', line 37

def update_relationship
  process_request
end