jsonapi-resources 0.10.0.beta1 → 0.10.0.beta2

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -42,7 +42,7 @@ module JSONAPI
42
42
  def find
43
43
  filters = params[:filters]
44
44
  include_directives = params[:include_directives]
45
- sort_criteria = params.fetch(:sort_criteria, [])
45
+ sort_criteria = params[:sort_criteria]
46
46
  paginator = params[:paginator]
47
47
  fields = params[:fields]
48
48
  serializer = params[:serializer]
@@ -93,7 +93,8 @@ module JSONAPI
93
93
  find_options = {
94
94
  context: context,
95
95
  fields: fields,
96
- filters: { resource_klass._primary_key => key }
96
+ filters: { resource_klass._primary_key => key },
97
+ include_directives: include_directives
97
98
  }
98
99
 
99
100
  resource_set = find_resource_set(resource_klass,
@@ -109,7 +110,7 @@ module JSONAPI
109
110
  parent_key = params[:parent_key]
110
111
  relationship_type = params[:relationship_type].to_sym
111
112
  paginator = params[:paginator]
112
- sort_criteria = params.fetch(:sort_criteria, [])
113
+ sort_criteria = params[:sort_criteria]
113
114
  include_directives = params[:include_directives]
114
115
  fields = params[:fields]
115
116
 
@@ -119,7 +120,8 @@ module JSONAPI
119
120
  context: context,
120
121
  sort_criteria: sort_criteria,
121
122
  paginator: paginator,
122
- fields: fields
123
+ fields: fields,
124
+ include_directives: include_directives
123
125
  }
124
126
 
125
127
  resource_id_tree = find_related_resource_id_tree(resource_klass,
@@ -146,7 +148,8 @@ module JSONAPI
146
148
  find_options = {
147
149
  context: context,
148
150
  fields: fields,
149
- filters: {}
151
+ filters: {},
152
+ include_directives: include_directives
150
153
  }
151
154
 
152
155
  source_resource = source_klass.find_by_key(source_id, context: context, fields: fields)
@@ -166,7 +169,7 @@ module JSONAPI
166
169
  source_id = params[:source_id]
167
170
  relationship_type = params[:relationship_type]
168
171
  filters = params[:filters]
169
- sort_criteria = params.fetch(:sort_criteria, resource_klass.default_sort)
172
+ sort_criteria = params[:sort_criteria]
170
173
  paginator = params[:paginator]
171
174
  fields = params[:fields]
172
175
  include_directives = params[:include_directives]
@@ -179,7 +182,8 @@ module JSONAPI
179
182
  sort_criteria: sort_criteria,
180
183
  paginator: paginator,
181
184
  fields: fields,
182
- context: context
185
+ context: context,
186
+ include_directives: include_directives
183
187
  }
184
188
 
185
189
  source_resource = source_klass.find_by_key(source_id, context: context, fields: fields)
@@ -233,7 +237,8 @@ module JSONAPI
233
237
  find_options = {
234
238
  context: context,
235
239
  fields: fields,
236
- filters: { resource_klass._primary_key => resource.id }
240
+ filters: { resource_klass._primary_key => resource.id },
241
+ include_directives: include_directives
237
242
  }
238
243
 
239
244
  resource_set = find_resource_set(resource_klass,
@@ -269,7 +274,8 @@ module JSONAPI
269
274
  find_options = {
270
275
  context: context,
271
276
  fields: fields,
272
- filters: { resource_klass._primary_key => resource.id }
277
+ filters: { resource_klass._primary_key => resource.id },
278
+ include_directives: include_directives
273
279
  }
274
280
 
275
281
  resource_set = find_resource_set(resource_klass,
@@ -1,7 +1,7 @@
1
1
  module JSONAPI
2
2
  class Relationship
3
3
  attr_reader :acts_as_set, :foreign_key, :options, :name,
4
- :class_name, :polymorphic, :always_include_linkage_data,
4
+ :class_name, :polymorphic, :always_include_optional_linkage_data,
5
5
  :parent_resource, :eager_load_on_include, :custom_methods,
6
6
  :inverse_relationship, :allow_include
7
7
 
@@ -15,8 +15,13 @@ module JSONAPI
15
15
  @parent_resource = options[:parent_resource]
16
16
  @relation_name = options.fetch(:relation_name, @name)
17
17
  @polymorphic = options.fetch(:polymorphic, false) == true
18
- @polymorphic_relations = options[:polymorphic_relations]
19
- @always_include_linkage_data = options.fetch(:always_include_linkage_data, false) == true
18
+ @polymorphic_types = options[:polymorphic_types]
19
+ if options[:polymorphic_relations]
20
+ ActiveSupport::Deprecation.warn('Use polymorphic_types instead of polymorphic_relations')
21
+ @polymorphic_types ||= options[:polymorphic_relations]
22
+ end
23
+
24
+ @always_include_optional_linkage_data = options.fetch(:always_include_optional_linkage_data, false) == true
20
25
  @eager_load_on_include = options.fetch(:eager_load_on_include, false) == true
21
26
  @allow_include = options[:allow_include]
22
27
  @class_name = nil
@@ -58,8 +63,12 @@ module JSONAPI
58
63
  @poly_hash[name.to_sym]
59
64
  end
60
65
 
61
- def polymorphic_relations
62
- @polymorphic_relations ||= self.class.polymorphic_types(@relation_name)
66
+ def resource_types
67
+ if polymorphic? && belongs_to?
68
+ @polymorphic_types ||= self.class.polymorphic_types(@relation_name).collect {|t| t.pluralize}
69
+ else
70
+ [resource_klass._type.to_s.pluralize]
71
+ end
63
72
  end
64
73
 
65
74
  def type
@@ -102,6 +111,12 @@ module JSONAPI
102
111
  end
103
112
  end
104
113
 
114
+ def to_s
115
+ # :nocov: useful for debugging
116
+ "#{parent_resource._type}.#{name} => (#{belongs_to? ? 'ToOne' : 'BelongsToOne'}) #{resource_klass._type}"
117
+ # :nocov:
118
+ end
119
+
105
120
  def belongs_to?
106
121
  # :nocov:
107
122
  foreign_key_on == :self
@@ -112,6 +127,10 @@ module JSONAPI
112
127
  "#{name}_type" if polymorphic?
113
128
  end
114
129
 
130
+ def include_optional_linkage_data?
131
+ @always_include_optional_linkage_data || JSONAPI::configuration.always_include_to_one_linkage_data
132
+ end
133
+
115
134
  def allow_include?(context = nil)
116
135
  strategy = if @allow_include.nil?
117
136
  JSONAPI.configuration.default_allow_include_to_one
@@ -142,6 +161,18 @@ module JSONAPI
142
161
  end
143
162
  end
144
163
 
164
+ def to_s
165
+ # :nocov: useful for debugging
166
+ "#{parent_resource._type}.#{name} => (ToMany) #{resource_klass._type}"
167
+ # :nocov:
168
+ end
169
+
170
+ def include_optional_linkage_data?
171
+ # :nocov:
172
+ @always_include_optional_linkage_data || JSONAPI::configuration.always_include_to_many_linkage_data
173
+ # :nocov:
174
+ end
175
+
145
176
  def allow_include?(context = nil)
146
177
  strategy = if @allow_include.nil?
147
178
  JSONAPI.configuration.default_allow_include_to_many
@@ -156,8 +187,8 @@ module JSONAPI
156
187
  else
157
188
  strategy.call(context)
158
189
  end
159
-
160
190
  end
191
+
161
192
  end
162
193
  end
163
194
  end
@@ -345,7 +345,7 @@ module JSONAPI
345
345
  end
346
346
 
347
347
  def parse_include_directives(resource_klass, raw_include)
348
- return unless raw_include
348
+ raw_include ||= ''
349
349
 
350
350
  included_resources = []
351
351
  begin
@@ -354,8 +354,6 @@ module JSONAPI
354
354
  fail JSONAPI::Exceptions::InvalidInclude.new(format_key(resource_klass._type), raw_include)
355
355
  end
356
356
 
357
- return if included_resources.nil?
358
-
359
357
  begin
360
358
  result = included_resources.compact.map do |included_resource|
361
359
  check_include(resource_klass, included_resource.partition('.'))
@@ -400,6 +400,7 @@ module JSONAPI
400
400
  subclass.caching(_caching)
401
401
  subclass.paginator(_paginator)
402
402
  subclass._attributes = (_attributes || {}).dup
403
+ subclass.polymorphic(false)
403
404
 
404
405
  subclass._model_hints = (_model_hints || {}).dup
405
406
 
@@ -439,53 +440,53 @@ module JSONAPI
439
440
  # }
440
441
  #
441
442
  # begin ResourceFinder Abstract methods
442
- def find(_filters, _options = {})
443
- # :nocov:
444
- raise 'Abstract ResourceFinder method called. Ensure that a ResourceFinder has been set.'
445
- # :nocov:
446
- end
443
+ def find(_filters, _options = {})
444
+ # :nocov:
445
+ raise 'Abstract ResourceFinder method called. Ensure that a ResourceFinder has been set.'
446
+ # :nocov:
447
+ end
447
448
 
448
- def count(_filters, _options = {})
449
- # :nocov:
450
- raise 'Abstract ResourceFinder method called. Ensure that a ResourceFinder has been set.'
451
- # :nocov:
452
- end
449
+ def count(_filters, _options = {})
450
+ # :nocov:
451
+ raise 'Abstract ResourceFinder method called. Ensure that a ResourceFinder has been set.'
452
+ # :nocov:
453
+ end
453
454
 
454
- def find_by_keys(_keys, _options = {})
455
- # :nocov:
456
- raise 'Abstract ResourceFinder method called. Ensure that a ResourceFinder has been set.'
457
- # :nocov:
458
- end
455
+ def find_by_keys(_keys, _options = {})
456
+ # :nocov:
457
+ raise 'Abstract ResourceFinder method called. Ensure that a ResourceFinder has been set.'
458
+ # :nocov:
459
+ end
459
460
 
460
- def find_by_key(_key, _options = {})
461
- # :nocov:
462
- raise 'Abstract ResourceFinder method called. Ensure that a ResourceFinder has been set.'
463
- # :nocov:
464
- end
461
+ def find_by_key(_key, _options = {})
462
+ # :nocov:
463
+ raise 'Abstract ResourceFinder method called. Ensure that a ResourceFinder has been set.'
464
+ # :nocov:
465
+ end
465
466
 
466
- def find_fragments(_filters, _options = {})
467
- # :nocov:
468
- raise 'Abstract ResourceFinder method called. Ensure that a ResourceFinder has been set.'
469
- # :nocov:
470
- end
467
+ def find_fragments(_filters, _options = {})
468
+ # :nocov:
469
+ raise 'Abstract ResourceFinder method called. Ensure that a ResourceFinder has been set.'
470
+ # :nocov:
471
+ end
471
472
 
472
- def find_included_fragments(_source_rids, _relationship_name, _options = {})
473
- # :nocov:
474
- raise 'Abstract ResourceFinder method called. Ensure that a ResourceFinder has been set.'
475
- # :nocov:
476
- end
473
+ def find_included_fragments(_source_rids, _relationship_name, _options = {})
474
+ # :nocov:
475
+ raise 'Abstract ResourceFinder method called. Ensure that a ResourceFinder has been set.'
476
+ # :nocov:
477
+ end
477
478
 
478
- def find_related_fragments(_source_rids, _relationship_name, _options = {})
479
- # :nocov:
480
- raise 'Abstract ResourceFinder method called. Ensure that a ResourceFinder has been set.'
481
- # :nocov:
482
- end
479
+ def find_related_fragments(_source_rids, _relationship_name, _options = {})
480
+ # :nocov:
481
+ raise 'Abstract ResourceFinder method called. Ensure that a ResourceFinder has been set.'
482
+ # :nocov:
483
+ end
483
484
 
484
- def count_related(_source_rid, _relationship_name, _options = {})
485
- # :nocov:
486
- raise 'Abstract ResourceFinder method called. Ensure that a ResourceFinder has been set.'
487
- # :nocov:
488
- end
485
+ def count_related(_source_rid, _relationship_name, _options = {})
486
+ # :nocov:
487
+ raise 'Abstract ResourceFinder method called. Ensure that a ResourceFinder has been set.'
488
+ # :nocov:
489
+ end
489
490
 
490
491
  #end ResourceFinder Abstract methods
491
492
 
@@ -710,14 +711,6 @@ module JSONAPI
710
711
  _relationships.keys | _attributes.keys
711
712
  end
712
713
 
713
- def records(options = {})
714
- _model_class.all
715
- end
716
-
717
- def retrieve_records(ids, options = {})
718
- _model_class.where(_primary_key => ids)
719
- end
720
-
721
714
  def resources_for(records, context)
722
715
  records.collect do |record|
723
716
  resource_for(record, context)
@@ -850,19 +843,28 @@ module JSONAPI
850
843
  end
851
844
 
852
845
  def _relationship(type)
846
+ return nil unless type
853
847
  type = type.to_sym
854
848
  @_relationships[type]
855
849
  end
856
850
 
857
851
  def _model_name
858
852
  if _abstract
859
- return ''
853
+ ''
860
854
  else
861
855
  return @_model_name.to_s if defined?(@_model_name)
862
856
  class_name = self.name
863
857
  return '' if class_name.nil?
864
858
  @_model_name = class_name.demodulize.sub(/Resource$/, '')
865
- return @_model_name.to_s
859
+ @_model_name.to_s
860
+ end
861
+ end
862
+
863
+ def _polymorphic_name
864
+ if !_polymorphic
865
+ ''
866
+ else
867
+ @_polymorphic_name ||= _model_name.to_s.downcase
866
868
  end
867
869
  end
868
870
 
@@ -902,6 +904,34 @@ module JSONAPI
902
904
  @_paginator = paginator
903
905
  end
904
906
 
907
+ def _polymorphic
908
+ @_polymorphic
909
+ end
910
+
911
+ def polymorphic(polymorphic = true)
912
+ @_polymorphic = polymorphic
913
+ end
914
+
915
+ def _polymorphic_types
916
+ @poly_hash ||= {}.tap do |hash|
917
+ ObjectSpace.each_object do |klass|
918
+ next unless Module === klass
919
+ if klass < ActiveRecord::Base
920
+ klass.reflect_on_all_associations(:has_many).select{|r| r.options[:as] }.each do |reflection|
921
+ (hash[reflection.options[:as]] ||= []) << klass.name.downcase
922
+ end
923
+ end
924
+ end
925
+ end
926
+ @poly_hash[_polymorphic_name.to_sym]
927
+ end
928
+
929
+ def _polymorphic_resource_klasses
930
+ @_polymorphic_resource_klasses ||= _polymorphic_types.collect do |type|
931
+ resource_klass_for(type)
932
+ end
933
+ end
934
+
905
935
  def abstract(val = true)
906
936
  @abstract = val
907
937
  end
@@ -48,12 +48,11 @@ module JSONAPI
48
48
 
49
49
  # fill in any missed resources
50
50
  unless missed_ids.empty?
51
- filters = {resource_klass._primary_key => missed_ids}
52
51
  find_opts = {
53
52
  context: context,
54
53
  fields: find_options[:fields] }
55
54
 
56
- found_resources = resource_klass.find(filters, find_opts)
55
+ found_resources = resource_klass.find_by_keys(missed_ids, find_opts)
57
56
 
58
57
  found_resources.each do |resource|
59
58
  relationship_data = @resource_klasses[resource_klass][resource.id][:relationships]
@@ -1,5 +1,5 @@
1
1
  module JSONAPI
2
2
  module Resources
3
- VERSION = '0.10.0.beta1'
3
+ VERSION = '0.10.0.beta2'
4
4
  end
5
5
  end
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: jsonapi-resources
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.10.0.beta1
4
+ version: 0.10.0.beta2
5
5
  platform: ruby
6
6
  authors:
7
7
  - Dan Gebhardt
@@ -9,7 +9,7 @@ authors:
9
9
  autorequire:
10
10
  bindir: bin
11
11
  cert_chain: []
12
- date: 2019-01-03 00:00:00.000000000 Z
12
+ date: 2019-02-11 00:00:00.000000000 Z
13
13
  dependencies:
14
14
  - !ruby/object:Gem::Dependency
15
15
  name: bundler
@@ -177,6 +177,7 @@ files:
177
177
  - lib/generators/jsonapi/templates/jsonapi_resource.rb
178
178
  - lib/jsonapi-resources.rb
179
179
  - lib/jsonapi/active_relation_resource_finder.rb
180
+ - lib/jsonapi/active_relation_resource_finder/adapters/join_left_active_record_adapter.rb
180
181
  - lib/jsonapi/active_relation_resource_finder/join_tree.rb
181
182
  - lib/jsonapi/acts_as_resource_controller.rb
182
183
  - lib/jsonapi/cached_response_fragment.rb
@@ -194,6 +195,8 @@ files:
194
195
  - lib/jsonapi/operation.rb
195
196
  - lib/jsonapi/operation_result.rb
196
197
  - lib/jsonapi/paginator.rb
198
+ - lib/jsonapi/path.rb
199
+ - lib/jsonapi/path_segment.rb
197
200
  - lib/jsonapi/processor.rb
198
201
  - lib/jsonapi/relationship.rb
199
202
  - lib/jsonapi/request_parser.rb
@@ -228,7 +231,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
228
231
  version: 1.3.1
229
232
  requirements: []
230
233
  rubyforge_project:
231
- rubygems_version: 2.6.14.1
234
+ rubygems_version: 2.6.14.3
232
235
  signing_key:
233
236
  specification_version: 4
234
237
  summary: Easily support JSON API in Rails.