jsonapi-resources 0.9.3 → 0.9.4

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: 1b9c37e4c95b76725a1cd1c63fe6f065dc56eb04
4
- data.tar.gz: c206e1fec888ce0031ad4c2c5d26a2519a82791c
3
+ metadata.gz: a96ed78c48c0357091b76f35918aa4dbb3d21d55
4
+ data.tar.gz: d9ac26a835068ae65a3d567b642cdf0f705eac63
5
5
  SHA512:
6
- metadata.gz: c8f727dc37c485ba63ccb9de5abfa3903ea3369c4ea1a7175d796d00a63b971913672d5c33b0685b87b873ba586e1caba4b1a5ce5761961151a7bf4287c6ff63
7
- data.tar.gz: c5c6a4dc06ad88949d1d18ffbc23b0447a8843c9e658ae386d3eb510af36448de75f749b6f20076b46f1b294802dbc1366f25ed0bbfd5fb6a6861676cb464d16
6
+ metadata.gz: 10d478f7821349ef9618ad3378b177b1237ffe91110779c66a8f2e6ed7d8ad93f8da87cfaddab0e5b167222a290c27047af02e6a652dfed8927c69c2ee9ea57e
7
+ data.tar.gz: 9436fd76c957786a7faa1abfa1e8d0c05fdaec3530a9c0e456687e67a1a411ac5a4df4e3164551610d1fea7e8c6dc0dca21f6a3fae9f8b29bc1364f6d3f8943a
@@ -40,6 +40,16 @@ module JSONAPI
40
40
  delve_paths(get_includes(@include_directives_hash, false))
41
41
  end
42
42
 
43
+ def merge_filter(relation, filter)
44
+ config = include_config(relation.to_sym)
45
+ config[:include_filters] ||= {}
46
+ config[:include_filters].merge!(filter)
47
+ end
48
+
49
+ def include_config(relation)
50
+ @include_directives_hash[:include_related][relation]
51
+ end
52
+
43
53
  private
44
54
 
45
55
  def get_related(current_path)
@@ -197,7 +197,7 @@ module JSONAPI
197
197
  (paginator && paginator.class.requires_record_count) ||
198
198
  (JSONAPI.configuration.top_level_meta_include_page_count))
199
199
  related_resource_records = source_resource.public_send("records_for_" + relationship_type)
200
- records = resource_klass.filter_records(filters, {},
200
+ records = resource_klass.filter_records(verified_filters, {},
201
201
  related_resource_records)
202
202
 
203
203
  record_count = resource_klass.count_records(records)
@@ -76,6 +76,8 @@ module JSONAPI
76
76
  def setup_show_action(params)
77
77
  parse_fields(params[:fields])
78
78
  parse_include_directives(params[:include])
79
+ parse_filters(params[:filter])
80
+
79
81
  @id = params[:id]
80
82
  add_show_operation
81
83
  end
@@ -232,7 +234,7 @@ module JSONAPI
232
234
  @include_directives = JSONAPI::IncludeDirectives.new(@resource_klass, result)
233
235
  rescue JSONAPI::Exceptions::InvalidInclude => e
234
236
  @errors.concat(e.errors)
235
- @include_directives = {}
237
+ @include_directives = JSONAPI::IncludeDirectives.new(@resource_klass, [])
236
238
  end
237
239
  end
238
240
 
@@ -249,11 +251,33 @@ module JSONAPI
249
251
  end
250
252
 
251
253
  filters.each do |key, value|
252
- filter = unformat_key(key)
253
- if @resource_klass._allowed_filter?(filter)
254
- @filters[filter] = value
254
+ filter_method, included_resource_name =
255
+ key.to_s.split('.').map { |k| unformat_key(k) }.reverse
256
+
257
+ if included_resource_name
258
+ relationship = resource_klass._relationship(included_resource_name || '')
259
+
260
+ unless relationship
261
+ return @errors.concat(Exceptions::FilterNotAllowed.new(filter_method).errors)
262
+ end
263
+
264
+ unless relationship.resource_klass._allowed_filter?(filter_method)
265
+ return @errors.concat(Exceptions::FilterNotAllowed.new(filter_method).errors)
266
+ end
267
+
268
+ unless @include_directives.include_config(relationship.name.to_sym).present?
269
+ return @errors.concat(Exceptions::FilterNotAllowed.new(filter_method).errors)
270
+ end
271
+
272
+ verified_filter = relationship.resource_klass.verify_filters(filter_method => value)
273
+ @include_directives.merge_filter(relationship.name, verified_filter)
274
+ next
255
275
  else
256
- fail JSONAPI::Exceptions::FilterNotAllowed.new(filter)
276
+ unless resource_klass._allowed_filter?(filter_method)
277
+ return @errors.concat(Exceptions::FilterNotAllowed.new(filter_method).errors)
278
+ end
279
+
280
+ @filters[filter_method] = value
257
281
  end
258
282
  end
259
283
  end
@@ -732,9 +732,30 @@ module JSONAPI
732
732
  records
733
733
  end
734
734
 
735
+ def apply_included_resources_filters(records, options = {})
736
+ include_directives = options[:include_directives]
737
+ return records unless include_directives
738
+ related_directives = include_directives.include_directives.fetch(:include_related)
739
+ related_directives.reduce(records) do |memo, (relationship_name, config)|
740
+ relationship = _relationship(relationship_name)
741
+ next memo unless relationship && relationship.is_a?(JSONAPI::Relationship::ToMany)
742
+ filtering_resource = relationship.resource_klass
743
+
744
+ # Don't try to merge where clauses when relation isn't already being joined to query.
745
+ next memo unless config[:include_in_join]
746
+
747
+ filters = config[:include_filters]
748
+ next memo unless filters
749
+
750
+ rel_records = filtering_resource.apply_filters(filtering_resource.records(options), filters, options).references(relationship_name)
751
+ memo.merge(rel_records)
752
+ end
753
+ end
754
+
735
755
  def filter_records(filters, options, records = records(options))
736
756
  records = apply_filters(records, filters, options)
737
- apply_includes(records, options)
757
+ records = apply_includes(records, options)
758
+ apply_included_resources_filters(records, options)
738
759
  end
739
760
 
740
761
  def sort_records(records, order_options, context = {})
@@ -1256,7 +1277,7 @@ module JSONAPI
1256
1277
  quoted_attrs = attrs.map do |attr|
1257
1278
  quoted_table = conn.quote_table_name(attr.relation.table_alias || attr.relation.name)
1258
1279
  quoted_column = conn.quote_column_name(attr.name)
1259
- "#{quoted_table}.#{quoted_column}"
1280
+ Arel.sql("#{quoted_table}.#{quoted_column}")
1260
1281
  end
1261
1282
  relation.pluck(*quoted_attrs)
1262
1283
  end
@@ -287,6 +287,7 @@ module JSONAPI
287
287
  include_linkage = ia && ia[:include]
288
288
  include_linked_children = ia && !ia[:include_related].empty?
289
289
 
290
+ options = { filters: ia && ia[:include_filters] || {} }
290
291
  if field_set.include?(name)
291
292
  hash[format_key(name)] = link_object(source, relationship, include_linkage)
292
293
  end
@@ -298,7 +299,7 @@ module JSONAPI
298
299
  resources = if source.preloaded_fragments.has_key?(format_key(name))
299
300
  source.preloaded_fragments[format_key(name)].values
300
301
  else
301
- [source.public_send(name)].flatten(1).compact
302
+ [source.public_send(name, options)].flatten(1).compact
302
303
  end
303
304
  resources.each do |resource|
304
305
  next if self_referential_and_already_in_source(resource)
@@ -410,7 +411,10 @@ module JSONAPI
410
411
  end
411
412
  end
412
413
  else
413
- source.public_send(relationship.name).map do |value|
414
+ include_config = include_directives.include_config(relationship.name.to_sym) if include_directives
415
+ include_filters = include_config[:include_filters] if include_config
416
+ options = { filters: include_filters || {} }
417
+ source.public_send(relationship.name, options).map do |value|
414
418
  [relationship.type, value.id]
415
419
  end
416
420
  end
@@ -1,5 +1,5 @@
1
1
  module JSONAPI
2
2
  module Resources
3
- VERSION = '0.9.3'
3
+ VERSION = '0.9.4'
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.9.3
4
+ version: 0.9.4
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: 2018-08-31 00:00:00.000000000 Z
12
+ date: 2019-01-03 00:00:00.000000000 Z
13
13
  dependencies:
14
14
  - !ruby/object:Gem::Dependency
15
15
  name: bundler
@@ -217,7 +217,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
217
217
  version: '0'
218
218
  requirements: []
219
219
  rubyforge_project:
220
- rubygems_version: 2.5.2.3
220
+ rubygems_version: 2.6.14.1
221
221
  signing_key:
222
222
  specification_version: 4
223
223
  summary: Easily support JSON API in Rails.