jsonapi-resources 0.10.5 → 0.10.7

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.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: ac8ebf6446cff655aede872afda1f5343ab32a825e7f8be26c56ae1a44a4cdc6
4
- data.tar.gz: 1069433f6a6b65ca6a6b8dd720408ad249df1c18f26858495adc17de6e1aaec1
3
+ metadata.gz: d30a2ea26f6bb9f907e0710a7e9d95a9682bfa64ab0fd7ae1e5b350c07f52924
4
+ data.tar.gz: fd883de5a6506f4aaad03fcf3ec61ec311c0e57f0cf33ea8c3d8b082c47b2671
5
5
  SHA512:
6
- metadata.gz: 9f603f9d68eb6996f439e63d2ea39c78691555fbb3fcc56c8783c5d8c44c9700c77947d81d8061a8246bb8d230bd150c6e83ef5d846472bab13cc6d10cb7377e
7
- data.tar.gz: cc5dad7fdd043aee7ce42cc51c455a29835b8cb88306a8bd15837105cb6d5ab04d33fc3d68357c85ac1e1a1cd8d11691578deb0a9da3451f02a3141eff76d9df
6
+ metadata.gz: cc53b7071fb93b2f2f5c8ff2f02b43572a56d5da8a58835cff73953100db55c0ea9dbc2a1d26b7b06ae854d8e588276619d7631a4dc509e7c6332b9ced5c6245
7
+ data.tar.gz: d9f77c39f058f3978b20ccba5d2beea4744bbda9cb1b19933ed9a3a235a5265a9fecb945a2fd3646f0135cb97059896be3ac2fa19023a94fd555480ac8483f95
@@ -147,9 +147,15 @@ module JSONAPI
147
147
  related_resource_klass = join_details[:related_resource_klass]
148
148
  join_type = relationship_details[:join_type]
149
149
 
150
+ join_options = {
151
+ relationship: relationship,
152
+ relationship_details: relationship_details,
153
+ related_resource_klass: related_resource_klass,
154
+ }
155
+
150
156
  if relationship == :root
151
157
  unless source_relationship
152
- add_join_details('', {alias: resource_klass._table_name, join_type: :root})
158
+ add_join_details('', {alias: resource_klass._table_name, join_type: :root, join_options: join_options})
153
159
  end
154
160
  next
155
161
  end
@@ -163,7 +169,7 @@ module JSONAPI
163
169
  options: options)
164
170
  }
165
171
 
166
- details = {alias: self.class.alias_from_arel_node(join_node), join_type: join_type}
172
+ details = {alias: self.class.alias_from_arel_node(join_node), join_type: join_type, join_options: join_options}
167
173
 
168
174
  if relationship == source_relationship
169
175
  if relationship.polymorphic? && relationship.belongs_to?
@@ -324,6 +324,11 @@ module JSONAPI
324
324
  records = records.joins_left(relation_name)
325
325
  end
326
326
  end
327
+
328
+ if relationship.use_related_resource_records_for_joins
329
+ records = records.merge(self.records(options))
330
+ end
331
+
327
332
  records
328
333
  end
329
334
 
@@ -51,8 +51,8 @@ module JSONAPI
51
51
  @fetchable_fields = Set.new(fetchable_fields)
52
52
 
53
53
  # Relationships left uncompiled because we'll often want to insert included ids on retrieval
54
- @relationships = relationships
55
-
54
+ # Remove the data since that should not be cached
55
+ @relationships = relationships&.transform_values {|v| v.delete_if {|k, _v| k == 'data'} }
56
56
  @links_json = CompiledJson.of(links_json)
57
57
  @attributes_json = CompiledJson.of(attributes_json)
58
58
  @meta_json = CompiledJson.of(meta_json)
@@ -39,7 +39,8 @@ module JSONAPI
39
39
  :default_resource_cache_field,
40
40
  :resource_cache_digest_function,
41
41
  :resource_cache_usage_report_function,
42
- :default_exclude_links
42
+ :default_exclude_links,
43
+ :use_related_resource_records_for_joins
43
44
 
44
45
  def initialize
45
46
  #:underscored_key, :camelized_key, :dasherized_key, or custom
@@ -158,6 +159,11 @@ module JSONAPI
158
159
  # and relationships. Accepts either `:default`, `:none`, or array containing the
159
160
  # specific default links to exclude, which may be `:self` and `:related`.
160
161
  self.default_exclude_links = :none
162
+
163
+ # Use a related resource's `records` when performing joins. This setting allows included resources to account for
164
+ # permission scopes. It can be overridden explicitly per relationship. Furthermore, specifying a `relation_name`
165
+ # on a relationship will cause this setting to be ignored.
166
+ self.use_related_resource_records_for_joins = true
161
167
  end
162
168
 
163
169
  def cache_formatters=(bool)
@@ -299,6 +305,8 @@ module JSONAPI
299
305
  attr_writer :resource_cache_usage_report_function
300
306
 
301
307
  attr_writer :default_exclude_links
308
+
309
+ attr_writer :use_related_resource_records_for_joins
302
310
  end
303
311
 
304
312
  class << self
@@ -13,9 +13,16 @@ module JSONAPI
13
13
  # :nocov:
14
14
  end
15
15
 
16
+ def requires_record_count
17
+ # :nocov:
18
+ self.class.requires_record_count
19
+ # :nocov:
20
+ end
21
+
16
22
  class << self
17
23
  def requires_record_count
18
24
  # :nocov:
25
+ # @deprecated
19
26
  false
20
27
  # :nocov:
21
28
  end
@@ -36,10 +43,15 @@ class OffsetPaginator < JSONAPI::Paginator
36
43
  verify_pagination_params
37
44
  end
38
45
 
46
+ # @deprecated
39
47
  def self.requires_record_count
40
48
  true
41
49
  end
42
50
 
51
+ def requires_record_count
52
+ true
53
+ end
54
+
43
55
  def apply(relation, _order_options)
44
56
  relation.offset(@offset).limit(@limit)
45
57
  end
@@ -127,10 +139,15 @@ class PagedPaginator < JSONAPI::Paginator
127
139
  verify_pagination_params
128
140
  end
129
141
 
142
+ # @deprecated
130
143
  def self.requires_record_count
131
144
  true
132
145
  end
133
146
 
147
+ def requires_record_count
148
+ true
149
+ end
150
+
134
151
  def calculate_page_count(record_count)
135
152
  (record_count / @size.to_f).ceil
136
153
  end
@@ -65,13 +65,13 @@ module JSONAPI
65
65
  resource_set.populate!(serializer, context, find_options)
66
66
 
67
67
  page_options = result_options
68
- if (JSONAPI.configuration.top_level_meta_include_record_count || (paginator && paginator.class.requires_record_count))
68
+ if (top_level_meta_include_record_count || (paginator && paginator.requires_record_count))
69
69
  page_options[:record_count] = resource_klass.count(verified_filters,
70
70
  context: context,
71
71
  include_directives: include_directives)
72
72
  end
73
73
 
74
- if (JSONAPI.configuration.top_level_meta_include_page_count && paginator && page_options[:record_count])
74
+ if (top_level_meta_include_page_count && paginator && page_options[:record_count])
75
75
  page_options[:page_count] = paginator ? paginator.calculate_page_count(page_options[:record_count]) : 1
76
76
  end
77
77
 
@@ -197,9 +197,9 @@ module JSONAPI
197
197
  resource_set.populate!(serializer, context, find_options)
198
198
 
199
199
  opts = result_options
200
- if ((JSONAPI.configuration.top_level_meta_include_record_count) ||
201
- (paginator && paginator.class.requires_record_count) ||
202
- (JSONAPI.configuration.top_level_meta_include_page_count))
200
+ if ((top_level_meta_include_record_count) ||
201
+ (paginator && paginator.requires_record_count) ||
202
+ (top_level_meta_include_page_count))
203
203
 
204
204
  opts[:record_count] = source_resource.class.count_related(
205
205
  source_resource.identity,
@@ -207,13 +207,13 @@ module JSONAPI
207
207
  find_options)
208
208
  end
209
209
 
210
- if (JSONAPI.configuration.top_level_meta_include_page_count && opts[:record_count])
210
+ if (top_level_meta_include_page_count && opts[:record_count])
211
211
  opts[:page_count] = paginator.calculate_page_count(opts[:record_count])
212
212
  end
213
213
 
214
214
  opts[:pagination_params] = if paginator && JSONAPI.configuration.top_level_links_include_pagination
215
215
  page_options = {}
216
- page_options[:record_count] = opts[:record_count] if paginator.class.requires_record_count
216
+ page_options[:record_count] = opts[:record_count] if paginator.requires_record_count
217
217
  paginator.links_page_params(page_options.merge(fetched_resources: resource_set))
218
218
  else
219
219
  {}
@@ -382,6 +382,14 @@ module JSONAPI
382
382
  JSONAPI::ResourceSet.new(resource_id_tree)
383
383
  end
384
384
 
385
+ def top_level_meta_include_record_count
386
+ JSONAPI.configuration.top_level_meta_include_record_count
387
+ end
388
+
389
+ def top_level_meta_include_page_count
390
+ JSONAPI.configuration.top_level_meta_include_page_count
391
+ end
392
+
385
393
  private
386
394
  def find_related_resource_id_tree(resource_klass, source_id, relationship_name, find_options, include_related)
387
395
  options = find_options.except(:include_directives)
@@ -3,7 +3,7 @@ module JSONAPI
3
3
  attr_reader :acts_as_set, :foreign_key, :options, :name,
4
4
  :class_name, :polymorphic, :always_include_optional_linkage_data,
5
5
  :parent_resource, :eager_load_on_include, :custom_methods,
6
- :inverse_relationship, :allow_include
6
+ :inverse_relationship, :allow_include, :use_related_resource_records_for_joins
7
7
 
8
8
  attr_writer :allow_include
9
9
 
@@ -23,6 +23,15 @@ module JSONAPI
23
23
  @polymorphic_types ||= options[:polymorphic_relations]
24
24
  end
25
25
 
26
+ use_related_resource_records_for_joins_default = if options[:relation_name]
27
+ false
28
+ else
29
+ JSONAPI.configuration.use_related_resource_records_for_joins
30
+ end
31
+
32
+ @use_related_resource_records_for_joins = options.fetch(:use_related_resource_records_for_joins,
33
+ use_related_resource_records_for_joins_default) == true
34
+
26
35
  @always_include_optional_linkage_data = options.fetch(:always_include_optional_linkage_data, false) == true
27
36
  @eager_load_on_include = options.fetch(:eager_load_on_include, false) == true
28
37
  @allow_include = options[:allow_include]
@@ -1,5 +1,5 @@
1
1
  module JSONAPI
2
2
  module Resources
3
- VERSION = '0.10.5'
3
+ VERSION = '0.10.7'
4
4
  end
5
5
  end
metadata CHANGED
@@ -1,15 +1,15 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: jsonapi-resources
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.10.5
4
+ version: 0.10.7
5
5
  platform: ruby
6
6
  authors:
7
7
  - Dan Gebhardt
8
8
  - Larry Gebhardt
9
- autorequire:
9
+ autorequire:
10
10
  bindir: bin
11
11
  cert_chain: []
12
- date: 2021-05-19 00:00:00.000000000 Z
12
+ date: 2022-03-09 00:00:00.000000000 Z
13
13
  dependencies:
14
14
  - !ruby/object:Gem::Dependency
15
15
  name: bundler
@@ -232,7 +232,7 @@ homepage: https://github.com/cerebris/jsonapi-resources
232
232
  licenses:
233
233
  - MIT
234
234
  metadata: {}
235
- post_install_message:
235
+ post_install_message:
236
236
  rdoc_options: []
237
237
  require_paths:
238
238
  - lib
@@ -247,8 +247,8 @@ required_rubygems_version: !ruby/object:Gem::Requirement
247
247
  - !ruby/object:Gem::Version
248
248
  version: '0'
249
249
  requirements: []
250
- rubygems_version: 3.1.4
251
- signing_key:
250
+ rubygems_version: 3.1.6
251
+ signing_key:
252
252
  specification_version: 4
253
253
  summary: Easily support JSON API in Rails.
254
254
  test_files: []