active_model_serializers_pg 0.0.8 → 0.1.0

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: ee3c698a1508537d359551eb125a45c38bd0921dfd059ae49422dd7d114aa7f2
4
- data.tar.gz: 677ac55b0c35a51ba03b042b34ed3fd560b57d223b6687ca020eaa3b131aabda
3
+ metadata.gz: e76f115e2f83261f48bd94bd4bf72a14ba90e75b0e8cf8bd5f141122950424c4
4
+ data.tar.gz: df09967cbaa3a0e594fd8357e12ac8a4db43b53a06e77d259e5c7e3d2ac24931
5
5
  SHA512:
6
- metadata.gz: 82f91e03d0b6440e6b6002909b51e346c5e21293659ca0fc80b976df30bcfc089a294b1c4f42a7ca20c01b20d612203ead218ec3c268d5bd5bb4832652df923d
7
- data.tar.gz: d906a81cde8076d6150f6466263619f5283d3f5e1e9bbcedaef938f8f5633e2fdd2d1366ef3ca18343669773ef09809cc837df3cc99719ecaefbdae8a94ff2a7
6
+ metadata.gz: daed8e7d80f643cfe961eeb162829f941c73c3e015b9e32fe68d5bf23aa0444f348ae2714847e282603a0a0676a11c29e9b4c20d8d413ac1a2e4b4dee2b96fc4
7
+ data.tar.gz: 18709557dca29fed95387c97fb684926e3c68593692fd62f14dc600af46e42c173dba2a1543787a79c942a47ddc1386fd12d7507489067b45a877010eb782ca8
@@ -173,6 +173,20 @@ class JsonThing
173
173
  (@sql_methods[field] ||= _sql_method(field))[0]
174
174
  end
175
175
 
176
+ # Returns the primary key column as a string,
177
+ # but if there is a "#{primary_key}__sql" method,
178
+ # then call that and return it instead.
179
+ # We use this for the id reported in the jsonapi output,
180
+ # but not for foreign key relationships.
181
+ def primary_key_attr
182
+ pk = primary_key
183
+ if has_sql_method?(pk)
184
+ sql_method(pk)
185
+ else
186
+ pk
187
+ end
188
+ end
189
+
176
190
  private
177
191
 
178
192
  # This needs to be globally unique within the SQL query,
@@ -494,7 +508,7 @@ class JsonApiPgSql
494
508
  def select_resource_relationship_links(resource, reflection)
495
509
  reflection.links.map {|link_name, link_parts|
496
510
  <<~EOQ
497
- '#{link_name}', CONCAT(#{link_parts.join(%Q{, "#{resource.parent.table_name}"."#{resource.parent.primary_key}", })})
511
+ '#{link_name}', CONCAT(#{link_parts.join(%Q{, "#{resource.parent.table_name}"."#{resource.parent.primary_key_attr}", })})
498
512
  EOQ
499
513
  }.join(",\n")
500
514
  end
@@ -545,7 +559,6 @@ class JsonApiPgSql
545
559
  # or from the class's default scope.
546
560
  # TODO: preserve the whole custom relation, not just ordering
547
561
  p = refl.ar_class.new
548
- ordering = nil
549
562
  ordering = p.send(refl.name).arel.orders
550
563
  ordering = child_resource.ar_class.default_scoped.arel.orders if ordering.empty?
551
564
  ordering = ordering.map{|o|
@@ -562,7 +575,7 @@ class JsonApiPgSql
562
575
  ordering = "ORDER BY #{ordering}" if ordering
563
576
  <<~EOQ
564
577
  LEFT OUTER JOIN LATERAL (
565
- SELECT coalesce(jsonb_agg(jsonb_build_object('id', rel."#{child_resource.primary_key}"::text,
578
+ SELECT coalesce(jsonb_agg(jsonb_build_object('id', rel."#{child_resource.primary_key_attr}"::text,
566
579
  'type', '#{child_resource.json_type}') #{ordering}), '[]') AS j
567
580
  FROM "#{child_resource.table_name}" rel
568
581
  WHERE rel."#{child_resource.foreign_key}" = "#{resource.table_name}"."#{resource.primary_key}"
@@ -575,7 +588,7 @@ class JsonApiPgSql
575
588
  when ActiveRecord::Relation
576
589
  rel = refl.reflection_sql
577
590
  sql = rel.select(<<~EOQ).to_sql
578
- coalesce(jsonb_agg(jsonb_build_object('id', "#{child_resource.table_name}"."#{child_resource.primary_key}"::text,
591
+ coalesce(jsonb_agg(jsonb_build_object('id', "#{child_resource.table_name}"."#{child_resource.primary_key_attr}"::text,
579
592
  'type', '#{child_resource.json_type}')), '[]') AS j
580
593
  EOQ
581
594
  <<~EOQ
@@ -588,7 +601,7 @@ class JsonApiPgSql
588
601
  elsif refl.has_one?
589
602
  <<~EOQ
590
603
  LEFT OUTER JOIN LATERAL (
591
- SELECT jsonb_build_object('id', rel."#{child_resource.primary_key}"::text,
604
+ SELECT jsonb_build_object('id', rel."#{child_resource.primary_key_attr}"::text,
592
605
  'type', '#{child_resource.json_type}') AS j
593
606
  FROM "#{child_resource.table_name}" rel
594
607
  WHERE rel."#{child_resource.foreign_key}" = "#{resource.table_name}"."#{resource.primary_key}"
@@ -704,7 +717,7 @@ class JsonApiPgSql
704
717
  def select_resource(resource)
705
718
  fields = fields_for(resource)
706
719
  <<~EOQ
707
- jsonb_build_object('id', "#{resource.table_name}"."#{resource.primary_key}"::text,
720
+ jsonb_build_object('id', "#{resource.table_name}"."#{resource.primary_key_attr}"::text,
708
721
  'type', '#{resource.json_type}',
709
722
  'attributes', #{select_resource_attributes(resource)}
710
723
  #{maybe_select_resource_relationships(resource)})
@@ -1,3 +1,3 @@
1
1
  module ActiveModelSerializersPg
2
- VERSION = '0.0.8'
2
+ VERSION = '0.1.0'
3
3
  end
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: active_model_serializers_pg
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.0.8
4
+ version: 0.1.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - Paul A. Jungwirth
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2021-03-03 00:00:00.000000000 Z
11
+ date: 2021-07-07 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: active_model_serializers