jsonapi-resources 0.10.0.beta9 → 0.10.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
  SHA256:
3
- metadata.gz: 46a8c66a46d4472b937170a49042d8562590625617e7cacf7ffeea41a82833f6
4
- data.tar.gz: '056962b1332867b7d5c787c896ab27b09dbc20f32fc1e393bab92926368d0d4e'
3
+ metadata.gz: f0373c84cc7d1e7fa2da86da83b30db06192824ecd5261718d167ddcbd7d3c3d
4
+ data.tar.gz: 4f2667a8a42ed1a4c91100979e047a0fafedc0467d84bd13e487fc7cb8baf60d
5
5
  SHA512:
6
- metadata.gz: 7054515d6f28adb20a7df94f9fe85a70cd7489f3e75a89eca4daa6b7c75d056a34512fe825dc9c2b12ca4c2e3e0bc0ca10f3db27586e91b757b06bd72b04dad1
7
- data.tar.gz: 99514c00fa4f01507f38a60c8c62796790116e60406bee10666c83fef3072a572b03a33a4a9867741064ed60d1337e9c73f83a4c7d52fd8483821bac0958ef9b
6
+ metadata.gz: 6d080b0c4fcd192780be59c59158149081d7873301e36870335df9e362cc025d58b471d5677c7ba53c0d1b90c299cd41dea8975239fbd0510cdf0a9eb6a39cfb
7
+ data.tar.gz: 726d7c41751b41f79e1dfa6c6e6f4cd85d35c2a4e713d6836f8e1e8e8b275c74c62053fb1e95b278e6821732e81159cacdd6424482a182bc577ff56c0af767df
@@ -153,6 +153,11 @@ module JSONAPI
153
153
  pluck_fields << Arel.sql("#{concat_table_field(resource_table_alias, model_field[:name])} AS #{resource_table_alias}_#{model_field[:name]}")
154
154
  end
155
155
 
156
+ sort_fields = options.dig(:_relation_helper_options, :sort_fields)
157
+ sort_fields.try(:each) do |field|
158
+ pluck_fields << Arel.sql(field)
159
+ end
160
+
156
161
  fragments = {}
157
162
  rows = records.pluck(*pluck_fields)
158
163
  rows.each do |row|
@@ -390,7 +395,7 @@ module JSONAPI
390
395
  sort_criteria: sort_criteria,
391
396
  filters: filters)
392
397
 
393
- paginator = options[:paginator] if source_rids.count == 1
398
+ paginator = options[:paginator]
394
399
 
395
400
  records = apply_request_settings_to_records(records: records_for_source_to_related(options),
396
401
  resource_klass: resource_klass,
@@ -445,6 +450,11 @@ module JSONAPI
445
450
  pluck_fields << Arel.sql("#{concat_table_field(resource_table_alias, model_field[:name])} AS #{resource_table_alias}_#{model_field[:name]}")
446
451
  end
447
452
 
453
+ sort_fields = options.dig(:_relation_helper_options, :sort_fields)
454
+ sort_fields.try(:each) do |field|
455
+ pluck_fields << Arel.sql(field)
456
+ end
457
+
448
458
  fragments = {}
449
459
  rows = records.distinct.pluck(*pluck_fields)
450
460
  rows.each do |row|
@@ -515,7 +525,7 @@ module JSONAPI
515
525
  relationships: linkage_relationships,
516
526
  filters: filters)
517
527
 
518
- paginator = options[:paginator] if source_rids.count == 1
528
+ paginator = options[:paginator]
519
529
 
520
530
  # Note: We will sort by the source table. Without using unions we can't sort on a polymorphic relationship
521
531
  # in any manner that makes sense
@@ -680,24 +690,23 @@ module JSONAPI
680
690
  paginator: nil,
681
691
  options: {})
682
692
 
683
- opts = options.dup
684
- records = resource_klass.apply_joins(records, join_manager, opts)
693
+ options[:_relation_helper_options] = { join_manager: join_manager, sort_fields: [] }
694
+
695
+ records = resource_klass.apply_joins(records, join_manager, options)
685
696
 
686
697
  if primary_keys
687
698
  records = records.where(_primary_key => primary_keys)
688
699
  end
689
700
 
690
- opts[:join_manager] = join_manager
691
-
692
701
  unless filters.empty?
693
- records = resource_klass.filter_records(records, filters, opts)
702
+ records = resource_klass.filter_records(records, filters, options)
694
703
  end
695
704
 
696
705
  if sort_primary
697
706
  records = records.order(_primary_key => :asc)
698
707
  else
699
708
  order_options = resource_klass.construct_order_options(sort_criteria)
700
- records = resource_klass.sort_records(records, order_options, opts)
709
+ records = resource_klass.sort_records(records, order_options, options)
701
710
  end
702
711
 
703
712
  if paginator
@@ -731,19 +740,23 @@ module JSONAPI
731
740
 
732
741
  strategy = _allowed_sort.fetch(field.to_sym, {})[:apply]
733
742
 
743
+ options[:_relation_helper_options] ||= {}
744
+ options[:_relation_helper_options][:sort_fields] ||= []
745
+
734
746
  if strategy
735
747
  records = call_method_or_proc(strategy, records, direction, context)
736
748
  else
737
- join_manager = options[:join_manager]
738
-
739
- records = records.order(Arel.sql("#{get_aliased_field(field, join_manager)} #{direction}"))
749
+ join_manager = options.dig(:_relation_helper_options, :join_manager)
750
+ sort_field = join_manager ? get_aliased_field(field, join_manager) : field
751
+ options[:_relation_helper_options][:sort_fields].push("#{sort_field}")
752
+ records = records.order(Arel.sql("#{sort_field} #{direction}"))
740
753
  end
741
754
  records
742
755
  end
743
756
 
744
757
  # Assumes ActiveRecord's counting. Override if you need a different counting method
745
758
  def count_records(records)
746
- if Rails::VERSION::MAJOR >= 5 && ActiveRecord::VERSION::MINOR >= 1
759
+ if (Rails::VERSION::MAJOR == 5 && ActiveRecord::VERSION::MINOR >= 1) || Rails::VERSION::MAJOR >= 6
747
760
  records.count(:all)
748
761
  else
749
762
  records.count
@@ -825,8 +838,9 @@ module JSONAPI
825
838
  if strategy
826
839
  records = call_method_or_proc(strategy, records, value, options)
827
840
  else
828
- join_manager = options[:join_manager]
829
- records = records.where(Arel.sql(get_aliased_field(filter, join_manager)) => value)
841
+ join_manager = options.dig(:_relation_helper_options, :join_manager)
842
+ field = join_manager ? get_aliased_field(filter, join_manager) : filter
843
+ records = records.where(Arel.sql(field) => value)
830
844
  end
831
845
 
832
846
  records
@@ -189,7 +189,7 @@ module JSONAPI
189
189
  end
190
190
  end
191
191
 
192
- def media_types_for(header)
192
+ def media_types_for(header)
193
193
  (request.headers[header] || '')
194
194
  .scan(MEDIA_TYPE_MATCHER)
195
195
  .to_a
@@ -18,7 +18,7 @@ module JSONAPI
18
18
  :default_paginator,
19
19
  :default_page_size,
20
20
  :maximum_page_size,
21
- :default_processor_klass,
21
+ :default_processor_klass_name,
22
22
  :use_text_errors,
23
23
  :top_level_links_include_pagination,
24
24
  :top_level_meta_include_record_count,
@@ -110,7 +110,7 @@ module JSONAPI
110
110
 
111
111
  # The default Operation Processor to use if one is not defined specifically
112
112
  # for a Resource.
113
- self.default_processor_klass = JSONAPI::Processor
113
+ self.default_processor_klass_name = 'JSONAPI::Processor'
114
114
 
115
115
  # Allows transactions for creating and updating records
116
116
  # Set this to false if your backend does not support transactions (e.g. Mongodb)
@@ -225,9 +225,19 @@ module JSONAPI
225
225
  end
226
226
 
227
227
  def default_processor_klass=(default_processor_klass)
228
+ ActiveSupport::Deprecation.warn('`default_processor_klass` has been replaced by `default_processor_klass_name`.')
228
229
  @default_processor_klass = default_processor_klass
229
230
  end
230
231
 
232
+ def default_processor_klass
233
+ @default_processor_klass ||= default_processor_klass_name.safe_constantize
234
+ end
235
+
236
+ def default_processor_klass_name=(default_processor_klass_name)
237
+ @default_processor_klass = nil
238
+ @default_processor_klass_name = default_processor_klass_name
239
+ end
240
+
231
241
  def allow_include=(allow_include)
232
242
  ActiveSupport::Deprecation.warn('`allow_include` has been replaced by `default_allow_include_to_one` and `default_allow_include_to_many` options.')
233
243
  @default_allow_include_to_one = allow_include
@@ -101,6 +101,7 @@ module JSONAPI
101
101
  include_directives,
102
102
  find_options)
103
103
 
104
+ fail JSONAPI::Exceptions::RecordNotFound.new(id) if resource_set.resource_klasses.empty?
104
105
  resource_set.populate!(serializer, context, find_options)
105
106
 
106
107
  return JSONAPI::ResourceSetOperationResult.new(:ok, resource_set, result_options)
@@ -391,7 +392,7 @@ module JSONAPI
391
392
  primary_resource_id_tree = PrimaryResourceIdTree.new
392
393
  primary_resource_id_tree.add_resource_fragments(fragments, include_related)
393
394
 
394
- load_included(resource_klass, primary_resource_id_tree, include_related, options.except(:filters, :sort_criteria))
395
+ load_included(resource_klass, primary_resource_id_tree, include_related, options)
395
396
 
396
397
  primary_resource_id_tree
397
398
  end
@@ -405,7 +406,7 @@ module JSONAPI
405
406
  primary_resource_id_tree = PrimaryResourceIdTree.new
406
407
  primary_resource_id_tree.add_resource_fragments(fragments, include_related)
407
408
 
408
- load_included(resource_klass, primary_resource_id_tree, include_related, options.except(:filters, :sort_criteria))
409
+ load_included(resource_klass, primary_resource_id_tree, include_related, options)
409
410
 
410
411
  primary_resource_id_tree
411
412
  end
@@ -421,7 +422,7 @@ module JSONAPI
421
422
  primary_resource_id_tree = PrimaryResourceIdTree.new
422
423
  primary_resource_id_tree.add_resource_fragments(fragments, include_related)
423
424
 
424
- load_included(resource_klass, primary_resource_id_tree, include_related, options.except(:filters, :sort_criteria))
425
+ load_included(resource_klass, primary_resource_id_tree, include_related, options)
425
426
 
426
427
  primary_resource_id_tree
427
428
  end
@@ -433,7 +434,7 @@ module JSONAPI
433
434
  relationship = resource_klass._relationship(key)
434
435
  relationship_name = relationship.name.to_sym
435
436
 
436
- find_related_resource_options = options.dup
437
+ find_related_resource_options = options.except(:filters, :sort_criteria, :paginator)
437
438
  find_related_resource_options[:sort_criteria] = relationship.resource_klass.default_sort
438
439
  find_related_resource_options[:cache] = resource_klass.caching?
439
440
 
@@ -418,7 +418,7 @@ module JSONAPI
418
418
  sorts = sort_criteria
419
419
  elsif sort_criteria.is_a?(String)
420
420
  begin
421
- raw = URI.unescape(sort_criteria)
421
+ raw = URI.decode_www_form_component(sort_criteria)
422
422
  sorts = CSV.parse_line(raw)
423
423
  rescue CSV::MalformedCSVError
424
424
  fail JSONAPI::Exceptions::InvalidSortCriteria.new(format_key(resource_klass._type), raw)
@@ -5,10 +5,10 @@ module JSONAPI
5
5
  ActionController::Rendering,
6
6
  ActionController::Renderers::All,
7
7
  ActionController::StrongParameters,
8
- ActionController::ForceSSL,
8
+ Gem::Requirement.new('< 6.1').satisfied_by?(ActionPack.gem_version) ? ActionController::ForceSSL : nil,
9
9
  ActionController::Instrumentation,
10
10
  JSONAPI::ActsAsResourceController
11
- ].freeze
11
+ ].compact.freeze
12
12
 
13
13
  # Note, the url_helpers are not loaded. This will prevent links from being generated for resources, and warnings
14
14
  # will be emitted. Link support can be added by including `Rails.application.routes.url_helpers`, and links
@@ -47,7 +47,6 @@ module JSONAPI
47
47
  )
48
48
  )
49
49
  else
50
- missed_resource_ids[resource_klass] ||= {}
51
50
  missed_resource_ids[resource_klass] = @resource_klasses[resource_klass].keys
52
51
  end
53
52
  end
@@ -1,5 +1,5 @@
1
1
  module JSONAPI
2
2
  module Resources
3
- VERSION = '0.10.0.beta9'
3
+ VERSION = '0.10.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.10.0.beta9
4
+ version: 0.10.4
5
5
  platform: ruby
6
6
  authors:
7
7
  - Dan Gebhardt
@@ -9,22 +9,22 @@ authors:
9
9
  autorequire:
10
10
  bindir: bin
11
11
  cert_chain: []
12
- date: 2019-09-17 00:00:00.000000000 Z
12
+ date: 2021-02-05 00:00:00.000000000 Z
13
13
  dependencies:
14
14
  - !ruby/object:Gem::Dependency
15
15
  name: bundler
16
16
  requirement: !ruby/object:Gem::Requirement
17
17
  requirements:
18
- - - "~>"
18
+ - - ">="
19
19
  - !ruby/object:Gem::Version
20
- version: 1.17.3
20
+ version: '1.17'
21
21
  type: :development
22
22
  prerelease: false
23
23
  version_requirements: !ruby/object:Gem::Requirement
24
24
  requirements:
25
- - - "~>"
25
+ - - ">="
26
26
  - !ruby/object:Gem::Version
27
- version: 1.17.3
27
+ version: '1.17'
28
28
  - !ruby/object:Gem::Dependency
29
29
  name: rake
30
30
  requirement: !ruby/object:Gem::Requirement
@@ -243,12 +243,11 @@ required_ruby_version: !ruby/object:Gem::Requirement
243
243
  version: '2.3'
244
244
  required_rubygems_version: !ruby/object:Gem::Requirement
245
245
  requirements:
246
- - - ">"
246
+ - - ">="
247
247
  - !ruby/object:Gem::Version
248
- version: 1.3.1
248
+ version: '0'
249
249
  requirements: []
250
- rubyforge_project:
251
- rubygems_version: 2.7.6
250
+ rubygems_version: 3.0.3
252
251
  signing_key:
253
252
  specification_version: 4
254
253
  summary: Easily support JSON API in Rails.