Module: JSONAPI::ActsAsResourceController

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

Defined Under Namespace

Modules: ClassMethods

Constant Summary collapse

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

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Instance Attribute Details

#response_documentObject (readonly)

Returns the value of attribute response_document.



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

def response_document
  @response_document
end

Class Method Details

.included(base) ⇒ Object



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

def self.included(base)
  base.extend ClassMethods
  base.include Callbacks
  base.cattr_reader :server_error_callbacks
  base.define_jsonapi_resources_callbacks :process_operations,
                                          :transaction
end

Instance Method Details

#createObject



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

def create
  process_request
end

#create_relationshipObject



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

def create_relationship
  process_request
end

#destroyObject



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

def destroy
  process_request
end

#destroy_relationshipObject



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

def destroy_relationship
  process_request
end


62
63
64
65
66
67
68
# File 'lib/jsonapi/acts_as_resource_controller.rb', line 62

def get_related_resource
  # :nocov:
ActiveSupport::Deprecation.warn "In #{self.class.name} you exposed a `get_related_resource`"\
                                  " action. Please use `show_related_resource` instead."
  show_related_resource
  # :nocov:
end


70
71
72
73
74
75
76
# File 'lib/jsonapi/acts_as_resource_controller.rb', line 70

def get_related_resources
  # :nocov:
  ActiveSupport::Deprecation.warn "In #{self.class.name} you exposed a `get_related_resources`"\
                                  " action. Please use `index_related_resources` instead."
  index_related_resources
  # :nocov:
end

#indexObject



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

def index
  process_request
end


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

def index_related_resources
  process_request
end

#process_operation(operation) ⇒ Object



139
140
141
142
# File 'lib/jsonapi/acts_as_resource_controller.rb', line 139

def process_operation(operation)
  result = operation.process
  response_document.add_result(result, operation)
end

#process_operations(transactional) ⇒ Object



123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
# File 'lib/jsonapi/acts_as_resource_controller.rb', line 123

def process_operations(transactional)
  if transactional
    run_callbacks :transaction do
      ActiveRecord::Base.transaction do
        yield
      end
    end
  else
    begin
      yield
    rescue ActiveRecord::Rollback
      # Can't rollback without transaction, so just ignore it
    end
  end
end

#process_requestObject



78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
# File 'lib/jsonapi/acts_as_resource_controller.rb', line 78

def process_request
  @response_document = create_response_document

  unless verify_content_type_header && verify_accept_header
    render_response_document
    return
  end

  request_parser = JSONAPI::RequestParser.new(
      params,
      context: context,
      key_formatter: key_formatter,
      server_error_callbacks: (self.class.server_error_callbacks || []))

  transactional = request_parser.transactional?

  begin
    process_operations(transactional) do
      run_callbacks :process_operations do
        request_parser.each(response_document) do |op|
          op.options[:serializer] = resource_serializer_klass.new(
              op.resource_klass,
              include_directives: op.options[:include_directives],
              fields: op.options[:fields],
              base_url: base_url,
              key_formatter: key_formatter,
              route_formatter: route_formatter,
              serialization_options: serialization_options,
              controller: self
          )
          op.options[:cache_serializer_output] = !JSONAPI.configuration.resource_cache.nil?

          process_operation(op)
        end
      end
      if response_document.has_errors?
        raise ActiveRecord::Rollback
      end
    end
  rescue => e
    handle_exceptions(e)
  end
  render_response_document
end

#showObject



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

def show
  process_request
end


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

def show_related_resource
  process_request
end

#show_relationshipObject



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

def show_relationship
  process_request
end

#updateObject



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

def update
  process_request
end

#update_relationshipObject



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

def update_relationship
  process_request
end