jsonapi-resources 0.10.4 → 0.10.5

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: f0373c84cc7d1e7fa2da86da83b30db06192824ecd5261718d167ddcbd7d3c3d
4
- data.tar.gz: 4f2667a8a42ed1a4c91100979e047a0fafedc0467d84bd13e487fc7cb8baf60d
3
+ metadata.gz: ac8ebf6446cff655aede872afda1f5343ab32a825e7f8be26c56ae1a44a4cdc6
4
+ data.tar.gz: 1069433f6a6b65ca6a6b8dd720408ad249df1c18f26858495adc17de6e1aaec1
5
5
  SHA512:
6
- metadata.gz: 6d080b0c4fcd192780be59c59158149081d7873301e36870335df9e362cc025d58b471d5677c7ba53c0d1b90c299cd41dea8975239fbd0510cdf0a9eb6a39cfb
7
- data.tar.gz: 726d7c41751b41f79e1dfa6c6e6f4cd85d35c2a4e713d6836f8e1e8e8b275c74c62053fb1e95b278e6821732e81159cacdd6424482a182bc577ff56c0af767df
6
+ metadata.gz: 9f603f9d68eb6996f439e63d2ea39c78691555fbb3fcc56c8783c5d8c44c9700c77947d81d8061a8246bb8d230bd150c6e83ef5d846472bab13cc6d10cb7377e
7
+ data.tar.gz: cc5dad7fdd043aee7ce42cc51c455a29835b8cb88306a8bd15837105cb6d5ab04d33fc3d68357c85ac1e1a1cd8d11691578deb0a9da3451f02a3141eff76d9df
@@ -114,11 +114,11 @@ module JSONAPI
114
114
  # This alias is going to be resolve down to the model's table name and will not actually be an alias
115
115
  resource_table_alias = resource_klass._table_name
116
116
 
117
- pluck_fields = [Arel.sql("#{concat_table_field(resource_table_alias, resource_klass._primary_key)} AS #{resource_table_alias}_#{resource_klass._primary_key}")]
117
+ pluck_fields = [sql_field_with_alias(resource_table_alias, resource_klass._primary_key)]
118
118
 
119
119
  cache_field = attribute_to_model_field(:_cache_field) if options[:cache]
120
120
  if cache_field
121
- pluck_fields << Arel.sql("#{concat_table_field(resource_table_alias, cache_field[:name])} AS #{resource_table_alias}_#{cache_field[:name]}")
121
+ pluck_fields << sql_field_with_alias(resource_table_alias, cache_field[:name])
122
122
  end
123
123
 
124
124
  linkage_fields = []
@@ -133,7 +133,7 @@ module JSONAPI
133
133
 
134
134
  linkage_table_alias = join_manager.join_details_by_polymorphic_relationship(linkage_relationship, resource_type)[:alias]
135
135
  primary_key = klass._primary_key
136
- pluck_fields << Arel.sql("#{concat_table_field(linkage_table_alias, primary_key)} AS #{linkage_table_alias}_#{primary_key}")
136
+ pluck_fields << sql_field_with_alias(linkage_table_alias, primary_key)
137
137
  end
138
138
  else
139
139
  klass = linkage_relationship.resource_klass
@@ -141,7 +141,7 @@ module JSONAPI
141
141
 
142
142
  linkage_table_alias = join_manager.join_details_by_relationship(linkage_relationship)[:alias]
143
143
  primary_key = klass._primary_key
144
- pluck_fields << Arel.sql("#{concat_table_field(linkage_table_alias, primary_key)} AS #{linkage_table_alias}_#{primary_key}")
144
+ pluck_fields << sql_field_with_alias(linkage_table_alias, primary_key)
145
145
  end
146
146
  end
147
147
 
@@ -150,7 +150,7 @@ module JSONAPI
150
150
  attributes.try(:each) do |attribute|
151
151
  model_field = resource_klass.attribute_to_model_field(attribute)
152
152
  model_fields[attribute] = model_field
153
- pluck_fields << Arel.sql("#{concat_table_field(resource_table_alias, model_field[:name])} AS #{resource_table_alias}_#{model_field[:name]}")
153
+ pluck_fields << sql_field_with_alias(resource_table_alias, model_field[:name])
154
154
  end
155
155
 
156
156
  sort_fields = options.dig(:_relation_helper_options, :sort_fields)
@@ -409,13 +409,13 @@ module JSONAPI
409
409
  resource_table_alias = join_manager.join_details_by_relationship(relationship)[:alias]
410
410
 
411
411
  pluck_fields = [
412
- Arel.sql("#{_table_name}.#{_primary_key} AS source_id"),
413
- Arel.sql("#{concat_table_field(resource_table_alias, resource_klass._primary_key)} AS #{resource_table_alias}_#{resource_klass._primary_key}")
412
+ Arel.sql("#{_table_name}.#{_primary_key} AS \"source_id\""),
413
+ sql_field_with_alias(resource_table_alias, resource_klass._primary_key)
414
414
  ]
415
415
 
416
416
  cache_field = resource_klass.attribute_to_model_field(:_cache_field) if options[:cache]
417
417
  if cache_field
418
- pluck_fields << Arel.sql("#{concat_table_field(resource_table_alias, cache_field[:name])} AS #{resource_table_alias}_#{cache_field[:name]}")
418
+ pluck_fields << sql_field_with_alias(resource_table_alias, cache_field[:name])
419
419
  end
420
420
 
421
421
  linkage_fields = []
@@ -430,7 +430,7 @@ module JSONAPI
430
430
 
431
431
  linkage_table_alias = join_manager.join_details_by_polymorphic_relationship(linkage_relationship, resource_type)[:alias]
432
432
  primary_key = klass._primary_key
433
- pluck_fields << Arel.sql("#{concat_table_field(linkage_table_alias, primary_key)} AS #{linkage_table_alias}_#{primary_key}")
433
+ pluck_fields << sql_field_with_alias(linkage_table_alias, primary_key)
434
434
  end
435
435
  else
436
436
  klass = linkage_relationship.resource_klass
@@ -438,7 +438,7 @@ module JSONAPI
438
438
 
439
439
  linkage_table_alias = join_manager.join_details_by_relationship(linkage_relationship)[:alias]
440
440
  primary_key = klass._primary_key
441
- pluck_fields << Arel.sql("#{concat_table_field(linkage_table_alias, primary_key)} AS #{linkage_table_alias}_#{primary_key}")
441
+ pluck_fields << sql_field_with_alias(linkage_table_alias, primary_key)
442
442
  end
443
443
  end
444
444
 
@@ -447,7 +447,7 @@ module JSONAPI
447
447
  attributes.try(:each) do |attribute|
448
448
  model_field = resource_klass.attribute_to_model_field(attribute)
449
449
  model_fields[attribute] = model_field
450
- pluck_fields << Arel.sql("#{concat_table_field(resource_table_alias, model_field[:name])} AS #{resource_table_alias}_#{model_field[:name]}")
450
+ pluck_fields << sql_field_with_alias(resource_table_alias, model_field[:name])
451
451
  end
452
452
 
453
453
  sort_fields = options.dig(:_relation_helper_options, :sort_fields)
@@ -543,9 +543,9 @@ module JSONAPI
543
543
  related_type = concat_table_field(_table_name, relationship.polymorphic_type)
544
544
 
545
545
  pluck_fields = [
546
- Arel.sql("#{primary_key} AS #{_table_name}_#{_primary_key}"),
547
- Arel.sql("#{related_key} AS #{_table_name}_#{relationship.foreign_key}"),
548
- Arel.sql("#{related_type} AS #{_table_name}_#{relationship.polymorphic_type}")
546
+ Arel.sql("#{primary_key} AS #{alias_table_field(_table_name, _primary_key)}"),
547
+ Arel.sql("#{related_key} AS #{alias_table_field(_table_name, relationship.foreign_key)}"),
548
+ Arel.sql("#{related_type} AS #{alias_table_field(_table_name, relationship.polymorphic_type)}")
549
549
  ]
550
550
 
551
551
  # Get the additional fields from each relation. There's a limitation that the fields must exist in each relation
@@ -570,7 +570,7 @@ module JSONAPI
570
570
 
571
571
  cache_offset = relation_index
572
572
  if cache_field
573
- pluck_fields << Arel.sql("#{concat_table_field(table_alias, cache_field[:name])} AS cache_#{type}_#{cache_field[:name]}")
573
+ pluck_fields << sql_field_with_alias(table_alias, cache_field[:name])
574
574
  relation_index+= 1
575
575
  end
576
576
 
@@ -579,7 +579,7 @@ module JSONAPI
579
579
  attributes.try(:each) do |attribute|
580
580
  model_field = related_klass.attribute_to_model_field(attribute)
581
581
  model_fields[attribute] = model_field
582
- pluck_fields << Arel.sql("#{concat_table_field(table_alias, model_field[:name])} AS #{table_alias}_#{model_field[:name]}")
582
+ pluck_fields << sql_field_with_alias(table_alias, model_field[:name])
583
583
  relation_index+= 1
584
584
  end
585
585
 
@@ -616,7 +616,7 @@ module JSONAPI
616
616
 
617
617
  linkage_table_alias = join_manager.join_details_by_polymorphic_relationship(linkage_relationship, resource_type)[:alias]
618
618
  primary_key = klass._primary_key
619
- pluck_fields << Arel.sql("#{concat_table_field(linkage_table_alias, primary_key)} AS #{linkage_table_alias}_#{primary_key}")
619
+ pluck_fields << sql_field_with_alias(linkage_table_alias, primary_key)
620
620
  end
621
621
  else
622
622
  klass = linkage_relationship.resource_klass
@@ -624,7 +624,7 @@ module JSONAPI
624
624
 
625
625
  linkage_table_alias = join_manager.join_details_by_relationship(linkage_relationship)[:alias]
626
626
  primary_key = klass._primary_key
627
- pluck_fields << Arel.sql("#{concat_table_field(linkage_table_alias, primary_key)} AS #{linkage_table_alias}_#{primary_key}")
627
+ pluck_fields << sql_field_with_alias(linkage_table_alias, primary_key)
628
628
  end
629
629
  end
630
630
 
@@ -649,7 +649,7 @@ module JSONAPI
649
649
  end
650
650
  end
651
651
 
652
- relation_position = relation_positions[row[2].downcase.pluralize]
652
+ relation_position = relation_positions[row[2].underscore.pluralize]
653
653
  model_fields = relation_position[:model_fields]
654
654
  cache_field = relation_position[:cache_field]
655
655
  cache_offset = relation_position[:cache_offset]
@@ -790,22 +790,51 @@ module JSONAPI
790
790
  if table.blank? || field.to_s.include?('.')
791
791
  # :nocov:
792
792
  if quoted
793
- "\"#{field.to_s}\""
793
+ quote(field)
794
794
  else
795
795
  field.to_s
796
796
  end
797
797
  # :nocov:
798
798
  else
799
799
  if quoted
800
+ "#{quote(table)}.#{quote(field)}"
801
+ else
800
802
  # :nocov:
801
- "\"#{table.to_s}\".\"#{field.to_s}\""
803
+ "#{table.to_s}.#{field.to_s}"
802
804
  # :nocov:
805
+ end
806
+ end
807
+ end
808
+
809
+ def sql_field_with_alias(table, field, quoted = true)
810
+ Arel.sql("#{concat_table_field(table, field, quoted)} AS #{alias_table_field(table, field, quoted)}")
811
+ end
812
+
813
+ def alias_table_field(table, field, quoted = false)
814
+ if table.blank? || field.to_s.include?('.')
815
+ # :nocov:
816
+ if quoted
817
+ quote(field)
803
818
  else
804
- "#{table.to_s}.#{field.to_s}"
819
+ field.to_s
820
+ end
821
+ # :nocov:
822
+ else
823
+ if quoted
824
+ # :nocov:
825
+ quote("#{table.to_s}_#{field.to_s}")
826
+ # :nocov:
827
+ else
828
+ "#{table.to_s}_#{field.to_s}"
805
829
  end
806
830
  end
807
831
  end
808
832
 
833
+ def quote(field)
834
+ "\"#{field.to_s}\""
835
+ end
836
+
837
+
809
838
  def apply_filters(records, filters, options = {})
810
839
  if filters
811
840
  filters.each do |filter, value|
@@ -873,7 +873,7 @@ module JSONAPI
873
873
  if !_polymorphic
874
874
  ''
875
875
  else
876
- @_polymorphic_name ||= _model_name.to_s.downcase
876
+ @_polymorphic_name ||= _model_name.to_s.underscore
877
877
  end
878
878
  end
879
879
 
@@ -927,7 +927,7 @@ module JSONAPI
927
927
  next unless Module === klass
928
928
  if klass < ActiveRecord::Base
929
929
  klass.reflect_on_all_associations(:has_many).select{|r| r.options[:as] }.each do |reflection|
930
- (hash[reflection.options[:as]] ||= []) << klass.name.downcase
930
+ (hash[reflection.options[:as]] ||= []) << klass.name.underscore
931
931
  end
932
932
  end
933
933
  end
@@ -1058,7 +1058,7 @@ module JSONAPI
1058
1058
  end
1059
1059
 
1060
1060
  def construct_order_options(sort_params)
1061
- sort_params ||= default_sort
1061
+ sort_params = default_sort if sort_params.blank?
1062
1062
 
1063
1063
  return {} unless sort_params
1064
1064
 
@@ -30,7 +30,7 @@ module JSONAPI
30
30
  end
31
31
 
32
32
  def eql?(other)
33
- relationship == other.relationship && resource_klass == other.resource_klass
33
+ other.is_a?(JSONAPI::PathSegment::Relationship) && relationship == other.relationship && resource_klass == other.resource_klass
34
34
  end
35
35
 
36
36
  def hash
@@ -59,7 +59,7 @@ module JSONAPI
59
59
  end
60
60
 
61
61
  def eql?(other)
62
- field_name == other.field_name && resource_klass == other.resource_klass
62
+ other.is_a?(JSONAPI::PathSegment::Field) && field_name == other.field_name && resource_klass == other.resource_klass
63
63
  end
64
64
 
65
65
  def delegated_field_name
@@ -63,7 +63,7 @@ module JSONAPI
63
63
  next unless Module === klass
64
64
  if ActiveRecord::Base > klass
65
65
  klass.reflect_on_all_associations(:has_many).select{|r| r.options[:as] }.each do |reflection|
66
- (hash[reflection.options[:as]] ||= []) << klass.name.downcase
66
+ (hash[reflection.options[:as]] ||= []) << klass.name.underscore
67
67
  end
68
68
  end
69
69
  end
@@ -1,5 +1,5 @@
1
1
  module JSONAPI
2
2
  module Resources
3
- VERSION = '0.10.4'
3
+ VERSION = '0.10.5'
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.4
4
+ version: 0.10.5
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: 2021-02-05 00:00:00.000000000 Z
12
+ date: 2021-05-19 00:00:00.000000000 Z
13
13
  dependencies:
14
14
  - !ruby/object:Gem::Dependency
15
15
  name: bundler
@@ -247,7 +247,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
247
247
  - !ruby/object:Gem::Version
248
248
  version: '0'
249
249
  requirements: []
250
- rubygems_version: 3.0.3
250
+ rubygems_version: 3.1.4
251
251
  signing_key:
252
252
  specification_version: 4
253
253
  summary: Easily support JSON API in Rails.