pg_serializable 1.1.0 → 1.2.0

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
- SHA1:
3
- metadata.gz: 52b59e980da06293df94c9f2b794587960a3a1bf
4
- data.tar.gz: c8b3189b569853a8f0adc30b8f4b1037f0016361
2
+ SHA256:
3
+ metadata.gz: 2412f2861f08e20be5822ca7db5557e39017a451c1736bf3833aa2358e20812e
4
+ data.tar.gz: 5e696c9b943484d1eb2b3f19f1797212ac3593a03cd74c191b9ec410c68000ee
5
5
  SHA512:
6
- metadata.gz: 5dff0d8070efa302ed38f03c081851dcfbb9c0a933e525870d87a75eccd4ec6861676ee9a8771c062df6b11b62ac57eb7d536318a1ea4020511f22501e730de0
7
- data.tar.gz: 7d0e69567b8be5aa95038456598f9669cef49410d4acc8bb5cd969768f1e6c856dcc61f43f2940e12f2d3b57b0d3432295a62a827e3fd50f47f6fe1f1a00a2e5
6
+ metadata.gz: 2885d036b4e2b81e23444aac10b5edadc5f9a2fab31818b1fdea666a600bc026e199e1d856134045782006512bd24abf4fda0dfb6ae0a69ff2eae0078a5f0741
7
+ data.tar.gz: 13914aff5bc7e29849b94022f84a8af6b70ea3637cb1034f8848c9192d2ce9c84cc4e56d10e9c59c4767cc4abcb5b2a4708d00819c47daa642b7c1ee5b735b79
data/.gitignore CHANGED
@@ -9,3 +9,4 @@
9
9
 
10
10
  # rspec failure tracking
11
11
  .rspec_status
12
+ *.gem
data/README.md CHANGED
@@ -41,7 +41,7 @@ class Api::ProductsController < ApplicationController
41
41
  end
42
42
  ```
43
43
  ```shell
44
- Completed 200 OK in 2521ms (Views: 2431.8ms | ActiveRecord: 45.7ms)
44
+ Completed 200 OK in 2975ms (Views: 2944.2ms | ActiveRecord: 29.9ms)
45
45
  ```
46
46
 
47
47
  Using fast_jsonapi:
@@ -51,12 +51,16 @@ class Api::ProductsController < ApplicationController
51
51
  @products = Product.limit(200)
52
52
  .order(updated_at: :desc)
53
53
  .includes(:categories, :label, variations: :color)
54
- render json: ProductSerializer.new(@products).serialized_json
54
+ options = {
55
+ include: [:categories, :variations, :label, :'variations.color']
56
+ }
57
+
58
+ render json: ProductSerializer.new(@products, options).serialized_json
55
59
  end
56
60
  end
57
61
  ```
58
62
  ```shell
59
- Completed 200 OK in 315ms (Views: 0.2ms | ActiveRecord: 50.3ms)
63
+ Completed 200 OK in 542ms (Views: 0.5ms | ActiveRecord: 29.0ms)
60
64
  ```
61
65
 
62
66
  Using PgSerializable:
@@ -68,14 +72,15 @@ class Api::ProductsController < ApplicationController
68
72
  end
69
73
  ```
70
74
  ```shell
71
- Completed 200 OK in 109ms (Views: 0.2ms | ActiveRecord: 87.1ms)
75
+ Completed 200 OK in 54ms (Views: 0.1ms | ActiveRecord: 43.0ms)
72
76
  ```
73
77
 
74
78
  Benchmarking `fast_jsonapi` against `pg_serializable` on 100 requests:
75
79
  ```shell
76
80
  user system total real
77
- fast_jsonapi 21.510000 0.700000 22.210000 ( 26.994325)
78
- pg_serializable 1.470000 0.170000 1.640000 ( 9.187275)
81
+ jbuilder 175.620000 70.750000 246.370000 (282.967300)
82
+ fast_jsonapi 37.880000 0.720000 38.600000 ( 48.234853)
83
+ pg_serializable 1.180000 0.080000 1.260000 ( 4.150280)
79
84
  ```
80
85
 
81
86
  You'll see the greatest benefits from PgSerializable for deeply nested json objects.
@@ -133,7 +138,7 @@ attributes :name, :id
133
138
  ```
134
139
  results in:
135
140
  ```json
136
- [
141
+ [
137
142
  {
138
143
  "id": 503,
139
144
  "name": "Direct Viewer"
@@ -233,6 +238,7 @@ Supported associations:
233
238
  - `belongs_to`
234
239
  - `has_many`
235
240
  - `has_many :through`
241
+ - `has_and_belongs_to_many`
236
242
  - `has_one`
237
243
 
238
244
  #### belongs_to
@@ -240,7 +246,7 @@ Supported associations:
240
246
  serializable do
241
247
  default do
242
248
  attributes :id, :name
243
- belongs_to: :label
249
+ belongs_to :label
244
250
  end
245
251
  end
246
252
  ```
@@ -270,7 +276,7 @@ class Product < ApplicationRecord
270
276
  serializable do
271
277
  default do
272
278
  attributes :id, :name
273
- has_many: :variations
279
+ has_many :variations
274
280
  end
275
281
  end
276
282
  end
@@ -279,7 +285,7 @@ class Variation < ApplicationRecord
279
285
  serializable do
280
286
  default do
281
287
  attributes :id, :name
282
- belongs_to: :color
288
+ belongs_to :color
283
289
  end
284
290
  end
285
291
  end
@@ -392,8 +398,103 @@ end
392
398
  }
393
399
  ]
394
400
  ```
401
+
402
+ #### has_many_and_belongs_to_many
403
+ ```ruby
404
+ class Product < ApplicationRecord
405
+ has_and_belongs_to_many :categories
406
+
407
+ serializable do
408
+ default do
409
+ attributes :id
410
+ has_and_belongs_to_many :categories
411
+ end
412
+ end
413
+ end
414
+
415
+ class Category < ApplicationRecord
416
+ serializable do
417
+ default do
418
+ attributes :name, :id
419
+ end
420
+ end
421
+ end
422
+ ```
423
+
424
+ ```json
425
+ [
426
+ {
427
+ "id": 503,
428
+ "categories": [
429
+ {
430
+ "name": "Juliann",
431
+ "id": 13
432
+ },
433
+ {
434
+ "name": "Teressa",
435
+ "id": 176
436
+ },
437
+ {
438
+ "name": "Garret",
439
+ "id": 294
440
+ }
441
+ ]
442
+ },
443
+ {
444
+ "id": 502,
445
+ "categories": [
446
+ {
447
+ "name": "Rossana",
448
+ "id": 254
449
+ }
450
+ ]
451
+ }
452
+ ]
453
+ ```
454
+
395
455
  #### has_one
396
- TODO: write examples
456
+
457
+ ```ruby
458
+ class Product < ApplicationRecord
459
+ has_one :variation
460
+
461
+ serializable do
462
+ default do
463
+ attributes :name, :id
464
+ has_one :variation
465
+ end
466
+ end
467
+ end
468
+ ```
469
+
470
+ ```json
471
+ [
472
+ {
473
+ "name": "GPS Kit",
474
+ "id": 1003,
475
+ "variation": {
476
+ "name": "Gottlieb",
477
+ "id": 4544,
478
+ "color": {
479
+ "id": 756,
480
+ "hex": "67809b"
481
+ }
482
+ }
483
+ },
484
+ {
485
+ "name": "Video Transmitter",
486
+ "id": 1002,
487
+ "variation": {
488
+ "name": "Hessel",
489
+ "id": 4535,
490
+ "color": {
491
+ "id": 111,
492
+ "hex": "144f9e"
493
+ }
494
+ }
495
+ }
496
+ ]
497
+ ```
397
498
 
398
499
  ### Association Traits
399
500
  Models:
@@ -431,7 +532,7 @@ Controller:
431
532
  ```ruby
432
533
  render json: Product.limit(3).json(trait: :with_variations)
433
534
  ```
434
-
535
+ Response:
435
536
  ```json
436
537
  [
437
538
  {
@@ -28,5 +28,9 @@ module PgSerializable
28
28
  def has_one(association, label: nil, trait: :default)
29
29
  @attribute_nodes << Nodes::Association.new(klass, association, :has_one, label: label, trait: trait)
30
30
  end
31
+
32
+ def has_and_belongs_to_many(association, label: nil, trait: :default)
33
+ @attribute_nodes << Nodes::Association.new(klass, association, :has_and_belongs_to_many, label: label, trait: trait)
34
+ end
31
35
  end
32
36
  end
@@ -1,3 +1,3 @@
1
1
  module PgSerializable
2
- VERSION = "1.1.0"
2
+ VERSION = "1.2.0"
3
3
  end
@@ -102,6 +102,22 @@ module PgSerializable
102
102
  "\'#{subject.label}\', (#{query})"
103
103
  end
104
104
 
105
+ def visit_has_and_belongs_to_many(subject, table_alias:, **kwargs)
106
+ current_alias = next_alias!
107
+ association = subject.association
108
+ join_table = association.join_table
109
+ source = association.source_reflection
110
+
111
+ query = visit(association
112
+ .klass
113
+ .select("#{source.table_name}.*, #{join_table}.#{source.association_foreign_key}, #{join_table}.#{association.join_primary_key}")
114
+ .joins("INNER JOIN #{join_table} ON #{join_table}.#{source.association_foreign_key}=#{source.table_name}.#{source.association_primary_key}"), table_alias: current_alias)
115
+ .where("#{current_alias}.#{association.join_primary_key}=#{table_alias}.#{subject.foreign_key}")
116
+ .to_sql
117
+
118
+ "\'#{subject.label}\', (#{query})"
119
+ end
120
+
105
121
  def visit_has_one(subject, table_alias:, **kwargs)
106
122
  current_alias = next_alias!
107
123
  subquery_alias = next_alias!
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: pg_serializable
3
3
  version: !ruby/object:Gem::Version
4
- version: 1.1.0
4
+ version: 1.2.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - matthewjf
8
8
  autorequire:
9
9
  bindir: exe
10
10
  cert_chain: []
11
- date: 2018-10-15 00:00:00.000000000 Z
11
+ date: 2019-04-11 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: bundler
@@ -152,7 +152,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
152
152
  version: '0'
153
153
  requirements: []
154
154
  rubyforge_project:
155
- rubygems_version: 2.5.2
155
+ rubygems_version: 2.7.6
156
156
  signing_key:
157
157
  specification_version: 4
158
158
  summary: serializes rails models from postgres (9.5+)