Class: JSONAPI::Processor

Inherits:
Object
  • Object
show all
Includes:
Callbacks
Defined in:
lib/jsonapi/processor.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Methods included from Callbacks

included

Constructor Details

#initialize(resource_klass, operation_type, params) ⇒ Processor

Returns a new instance of Processor.



22
23
24
25
26
27
28
29
# File 'lib/jsonapi/processor.rb', line 22

def initialize(resource_klass, operation_type, params)
  @resource_klass = resource_klass
  @operation_type = operation_type
  @params = params
  @context = params[:context]
  @result = nil
  @result_options = {}
end

Instance Attribute Details

#contextObject (readonly)

Returns the value of attribute context.



20
21
22
# File 'lib/jsonapi/processor.rb', line 20

def context
  @context
end

#operation_typeObject (readonly)

Returns the value of attribute operation_type.



20
21
22
# File 'lib/jsonapi/processor.rb', line 20

def operation_type
  @operation_type
end

#paramsObject (readonly)

Returns the value of attribute params.



20
21
22
# File 'lib/jsonapi/processor.rb', line 20

def params
  @params
end

#resource_klassObject (readonly)

Returns the value of attribute resource_klass.



20
21
22
# File 'lib/jsonapi/processor.rb', line 20

def resource_klass
  @resource_klass
end

#resultObject (readonly)

Returns the value of attribute result.



20
21
22
# File 'lib/jsonapi/processor.rb', line 20

def result
  @result
end

#result_optionsObject (readonly)

Returns the value of attribute result_options.



20
21
22
# File 'lib/jsonapi/processor.rb', line 20

def result_options
  @result_options
end

Instance Method Details

#create_resourceObject



229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
# File 'lib/jsonapi/processor.rb', line 229

def create_resource
  include_directives = params[:include_directives]
  fields = params[:fields]
  serializer = params[:serializer]

  data = params[:data]
  resource = resource_klass.create(context)
  result = resource.replace_fields(data)

  find_options = {
      context: context,
      fields: fields,
      filters: { resource_klass._primary_key => resource.id },
      include_directives: include_directives
  }

  resource_set = find_resource_set(resource_klass,
                                   include_directives,
                                   find_options)

  resource_set.populate!(serializer, context, find_options)

  return JSONAPI::ResourceSetOperationResult.new((result == :completed ? :created : :accepted), resource_set, result_options)
end

#create_to_many_relationshipsObject



314
315
316
317
318
319
320
321
322
323
# File 'lib/jsonapi/processor.rb', line 314

def create_to_many_relationships
  resource_id = params[:resource_id]
  relationship_type = params[:relationship_type].to_sym
  data = params[:data]

  resource = resource_klass.find_by_key(resource_id, context: context)
  result = resource.create_to_many_links(relationship_type, data)

  return JSONAPI::OperationResult.new(result == :completed ? :no_content : :accepted, result_options)
end

#findObject



42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
# File 'lib/jsonapi/processor.rb', line 42

def find
  filters = params[:filters]
  include_directives = params[:include_directives]
  sort_criteria = params[:sort_criteria]
  paginator = params[:paginator]
  fields = params[:fields]
  serializer = params[:serializer]

  verified_filters = resource_klass.verify_filters(filters, context)

  find_options = {
    context: context,
    sort_criteria: sort_criteria,
    paginator: paginator,
    fields: fields,
    filters: verified_filters,
    include_directives: include_directives
  }

  resource_set = find_resource_set(resource_klass,
                                   include_directives,
                                   find_options)

  resource_set.populate!(serializer, context, find_options)

  page_options = result_options
  if (top_level_meta_include_record_count || (paginator && paginator.requires_record_count))
    page_options[:record_count] = resource_klass.count(verified_filters,
                                                       context: context,
                                                       include_directives: include_directives)
  end

  if (top_level_meta_include_page_count && paginator && page_options[:record_count])
    page_options[:page_count] = paginator ? paginator.calculate_page_count(page_options[:record_count]) : 1
  end

  if JSONAPI.configuration.top_level_links_include_pagination && paginator
    page_options[:pagination_params] = paginator.links_page_params(page_options.merge(fetched_resources: resource_set))
  end

  return JSONAPI::ResourcesSetOperationResult.new(:ok, resource_set, page_options)
end


377
378
379
380
381
382
383
# File 'lib/jsonapi/processor.rb', line 377

def find_related_resource_set(resource, relationship_name, include_directives, options)
  include_related = include_directives.include_directives[:include_related] if include_directives

  resource_id_tree = find_resource_id_tree_from_resource_relationship(resource, relationship_name, options, include_related)

  JSONAPI::ResourceSet.new(resource_id_tree)
end

#find_resource_set(resource_klass, include_directives, options) ⇒ Object



369
370
371
372
373
374
375
# File 'lib/jsonapi/processor.rb', line 369

def find_resource_set(resource_klass, include_directives, options)
  include_related = include_directives.include_directives[:include_related] if include_directives

  resource_id_tree = find_resource_id_tree(resource_klass, options, include_related)

  JSONAPI::ResourceSet.new(resource_id_tree)
end

#processObject



31
32
33
34
35
36
37
38
39
40
# File 'lib/jsonapi/processor.rb', line 31

def process
  run_callbacks :operation do
    run_callbacks operation_type do
      @result = send(operation_type)
    end
  end

rescue JSONAPI::Exceptions::Error => e
  @result = JSONAPI::ErrorsOperationResult.new(e.errors[0].code, e.errors)
end

#remove_resourceObject



254
255
256
257
258
259
260
261
# File 'lib/jsonapi/processor.rb', line 254

def remove_resource
  resource_id = params[:resource_id]

  resource = resource_klass.find_by_key(resource_id, context: context)
  result = resource.remove

  return JSONAPI::OperationResult.new(result == :completed ? :no_content : :accepted, result_options)
end

#remove_to_many_relationshipsObject



336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
# File 'lib/jsonapi/processor.rb', line 336

def remove_to_many_relationships
  resource_id = params[:resource_id]
  relationship_type = params[:relationship_type].to_sym
  associated_keys = params[:associated_keys]

  resource = resource_klass.find_by_key(resource_id, context: context)

  complete = true
  associated_keys.each do |key|
    result = resource.remove_to_many_link(relationship_type, key)
    if complete && result != :completed
      complete = false
    end
  end
  return JSONAPI::OperationResult.new(complete ? :no_content : :accepted, result_options)
end

#remove_to_one_relationshipObject



353
354
355
356
357
358
359
360
361
# File 'lib/jsonapi/processor.rb', line 353

def remove_to_one_relationship
  resource_id = params[:resource_id]
  relationship_type = params[:relationship_type].to_sym

  resource = resource_klass.find_by_key(resource_id, context: context)
  result = resource.remove_to_one_link(relationship_type)

  return JSONAPI::OperationResult.new(result == :completed ? :no_content : :accepted, result_options)
end

#replace_fieldsObject



263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
# File 'lib/jsonapi/processor.rb', line 263

def replace_fields
  resource_id = params[:resource_id]
  include_directives = params[:include_directives]
  fields = params[:fields]
  serializer = params[:serializer]

  data = params[:data]

  resource = resource_klass.find_by_key(resource_id, context: context)

  result = resource.replace_fields(data)

  find_options = {
      context: context,
      fields: fields,
      filters: { resource_klass._primary_key => resource.id },
      include_directives: include_directives
  }

  resource_set = find_resource_set(resource_klass,
                                   include_directives,
                                   find_options)

  resource_set.populate!(serializer, context, find_options)

  return JSONAPI::ResourceSetOperationResult.new((result == :completed ? :ok : :accepted), resource_set, result_options)
end

#replace_polymorphic_to_one_relationshipObject



302
303
304
305
306
307
308
309
310
311
312
# File 'lib/jsonapi/processor.rb', line 302

def replace_polymorphic_to_one_relationship
  resource_id = params[:resource_id]
  relationship_type = params[:relationship_type].to_sym
  key_value = params[:key_value]
  key_type = params[:key_type]

  resource = resource_klass.find_by_key(resource_id, context: context)
  result = resource.replace_polymorphic_to_one_link(relationship_type, key_value, key_type)

  return JSONAPI::OperationResult.new(result == :completed ? :no_content : :accepted, result_options)
end

#replace_to_many_relationshipsObject



325
326
327
328
329
330
331
332
333
334
# File 'lib/jsonapi/processor.rb', line 325

def replace_to_many_relationships
  resource_id = params[:resource_id]
  relationship_type = params[:relationship_type].to_sym
  data = params.fetch(:data)

  resource = resource_klass.find_by_key(resource_id, context: context)
  result = resource.replace_to_many_links(relationship_type, data)

  return JSONAPI::OperationResult.new(result == :completed ? :no_content : :accepted, result_options)
end

#replace_to_one_relationshipObject



291
292
293
294
295
296
297
298
299
300
# File 'lib/jsonapi/processor.rb', line 291

def replace_to_one_relationship
  resource_id = params[:resource_id]
  relationship_type = params[:relationship_type].to_sym
  key_value = params[:key_value]

  resource = resource_klass.find_by_key(resource_id, context: context)
  result = resource.replace_to_one_link(relationship_type, key_value)

  return JSONAPI::OperationResult.new(result == :completed ? :no_content : :accepted, result_options)
end

#showObject



85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
# File 'lib/jsonapi/processor.rb', line 85

def show
  include_directives = params[:include_directives]
  fields = params[:fields]
  id = params[:id]
  serializer = params[:serializer]

  key = resource_klass.verify_key(id, context)

  find_options = {
    context: context,
    fields: fields,
    filters: { resource_klass._primary_key => key },
    include_directives: include_directives
  }

  resource_set = find_resource_set(resource_klass,
                                   include_directives,
                                   find_options)

  fail JSONAPI::Exceptions::RecordNotFound.new(id) if resource_set.resource_klasses.empty?
  resource_set.populate!(serializer, context, find_options)

  return JSONAPI::ResourceSetOperationResult.new(:ok, resource_set, result_options)
end


141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
# File 'lib/jsonapi/processor.rb', line 141

def show_related_resource
  include_directives = params[:include_directives]
  source_klass = params[:source_klass]
  source_id = params[:source_id]
  relationship_type = params[:relationship_type]
  serializer = params[:serializer]
  fields = params[:fields]

  find_options = {
      context: context,
      fields: fields,
      filters: {},
      include_directives: include_directives
  }

  source_resource = source_klass.find_by_key(source_id, context: context, fields: fields)

  resource_set = find_related_resource_set(source_resource,
                                           relationship_type,
                                           include_directives,
                                           find_options)

  resource_set.populate!(serializer, context, find_options)

  return JSONAPI::ResourceSetOperationResult.new(:ok, resource_set, result_options)
end


168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
# File 'lib/jsonapi/processor.rb', line 168

def show_related_resources
  source_klass = params[:source_klass]
  source_id = params[:source_id]
  relationship_type = params[:relationship_type]
  filters = params[:filters]
  sort_criteria = params[:sort_criteria]
  paginator = params[:paginator]
  fields = params[:fields]
  include_directives = params[:include_directives]
  serializer = params[:serializer]

  verified_filters = resource_klass.verify_filters(filters, context)

  find_options = {
    filters:  verified_filters,
    sort_criteria: sort_criteria,
    paginator: paginator,
    fields: fields,
    context: context,
    include_directives: include_directives
  }

  source_resource = source_klass.find_by_key(source_id, context: context, fields: fields)

  resource_set = find_related_resource_set(source_resource,
                                           relationship_type,
                                           include_directives,
                                           find_options)

  resource_set.populate!(serializer, context, find_options)

  opts = result_options
  if ((top_level_meta_include_record_count) ||
      (paginator && paginator.requires_record_count) ||
      (top_level_meta_include_page_count))

    opts[:record_count] = source_resource.class.count_related(
        source_resource.identity,
        relationship_type,
        find_options)
  end

  if (top_level_meta_include_page_count && opts[:record_count])
    opts[:page_count] = paginator.calculate_page_count(opts[:record_count])
  end

  opts[:pagination_params] = if paginator && JSONAPI.configuration.top_level_links_include_pagination
                               page_options = {}
                               page_options[:record_count] = opts[:record_count] if paginator.requires_record_count
                               paginator.links_page_params(page_options.merge(fetched_resources: resource_set))
                             else
                               {}
                             end

  return JSONAPI::RelatedResourcesSetOperationResult.new(:ok,
                                                         source_resource,
                                                         relationship_type,
                                                         resource_set,
                                                         opts)
end

#show_relationshipObject



110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
# File 'lib/jsonapi/processor.rb', line 110

def show_relationship
  parent_key = params[:parent_key]
  relationship_type = params[:relationship_type].to_sym
  paginator = params[:paginator]
  sort_criteria = params[:sort_criteria]
  include_directives = params[:include_directives]
  fields = params[:fields]

  parent_resource = resource_klass.find_by_key(parent_key, context: context)

  find_options = {
      context: context,
      sort_criteria: sort_criteria,
      paginator: paginator,
      fields: fields,
      include_directives: include_directives
  }

  resource_id_tree = find_related_resource_id_tree(resource_klass,
                                                   JSONAPI::ResourceIdentity.new(resource_klass, parent_key),
                                                   relationship_type,
                                                   find_options,
                                                   nil)

  return JSONAPI::RelationshipOperationResult.new(:ok,
                                                  parent_resource,
                                                  resource_klass._relationship(relationship_type),
                                                  resource_id_tree.fragments.keys,
                                                  result_options)
end

#top_level_meta_include_page_countObject



389
390
391
# File 'lib/jsonapi/processor.rb', line 389

def top_level_meta_include_page_count
  JSONAPI.configuration.top_level_meta_include_page_count
end

#top_level_meta_include_record_countObject



385
386
387
# File 'lib/jsonapi/processor.rb', line 385

def top_level_meta_include_record_count
  JSONAPI.configuration.top_level_meta_include_record_count
end