README.md in pg_serializable-1.1.0 vs README.md in pg_serializable-1.2.0
- old
+ new
@@ -39,26 +39,30 @@
render 'api/products/index.json.jbuilder'
end
end
```
```shell
-Completed 200 OK in 2521ms (Views: 2431.8ms | ActiveRecord: 45.7ms)
+Completed 200 OK in 2975ms (Views: 2944.2ms | ActiveRecord: 29.9ms)
```
Using fast_jsonapi:
```ruby
class Api::ProductsController < ApplicationController
def index
@products = Product.limit(200)
.order(updated_at: :desc)
.includes(:categories, :label, variations: :color)
- render json: ProductSerializer.new(@products).serialized_json
+ options = {
+ include: [:categories, :variations, :label, :'variations.color']
+ }
+
+ render json: ProductSerializer.new(@products, options).serialized_json
end
end
```
```shell
-Completed 200 OK in 315ms (Views: 0.2ms | ActiveRecord: 50.3ms)
+Completed 200 OK in 542ms (Views: 0.5ms | ActiveRecord: 29.0ms)
```
Using PgSerializable:
```ruby
class Api::ProductsController < ApplicationController
@@ -66,18 +70,19 @@
render json: Product.limit(200).order(updated_at: :desc).json
end
end
```
```shell
-Completed 200 OK in 109ms (Views: 0.2ms | ActiveRecord: 87.1ms)
+Completed 200 OK in 54ms (Views: 0.1ms | ActiveRecord: 43.0ms)
```
Benchmarking `fast_jsonapi` against `pg_serializable` on 100 requests:
```shell
user system total real
-fast_jsonapi 21.510000 0.700000 22.210000 ( 26.994325)
-pg_serializable 1.470000 0.170000 1.640000 ( 9.187275)
+jbuilder 175.620000 70.750000 246.370000 (282.967300)
+fast_jsonapi 37.880000 0.720000 38.600000 ( 48.234853)
+pg_serializable 1.180000 0.080000 1.260000 ( 4.150280)
```
You'll see the greatest benefits from PgSerializable for deeply nested json objects.
## Installation
@@ -131,11 +136,11 @@
```ruby
attributes :name, :id
```
results in:
```json
-[
+[
{
"id": 503,
"name": "Direct Viewer"
},
{
@@ -231,18 +236,19 @@
### Associations
Supported associations:
- `belongs_to`
- `has_many`
- `has_many :through`
+- `has_and_belongs_to_many`
- `has_one`
#### belongs_to
```ruby
serializable do
default do
attributes :id, :name
- belongs_to: :label
+ belongs_to :label
end
end
```
```json
[
@@ -268,20 +274,20 @@
```ruby
class Product < ApplicationRecord
serializable do
default do
attributes :id, :name
- has_many: :variations
+ has_many :variations
end
end
end
class Variation < ApplicationRecord
serializable do
default do
attributes :id, :name
- belongs_to: :color
+ belongs_to :color
end
end
end
class Color < ApplicationRecord
@@ -390,13 +396,108 @@
}
]
}
]
```
+
+#### has_many_and_belongs_to_many
+```ruby
+class Product < ApplicationRecord
+ has_and_belongs_to_many :categories
+
+ serializable do
+ default do
+ attributes :id
+ has_and_belongs_to_many :categories
+ end
+ end
+end
+
+class Category < ApplicationRecord
+ serializable do
+ default do
+ attributes :name, :id
+ end
+ end
+end
+```
+
+```json
+[
+ {
+ "id": 503,
+ "categories": [
+ {
+ "name": "Juliann",
+ "id": 13
+ },
+ {
+ "name": "Teressa",
+ "id": 176
+ },
+ {
+ "name": "Garret",
+ "id": 294
+ }
+ ]
+ },
+ {
+ "id": 502,
+ "categories": [
+ {
+ "name": "Rossana",
+ "id": 254
+ }
+ ]
+ }
+]
+```
+
#### has_one
-TODO: write examples
+```ruby
+class Product < ApplicationRecord
+ has_one :variation
+
+ serializable do
+ default do
+ attributes :name, :id
+ has_one :variation
+ end
+ end
+end
+```
+
+```json
+[
+ {
+ "name": "GPS Kit",
+ "id": 1003,
+ "variation": {
+ "name": "Gottlieb",
+ "id": 4544,
+ "color": {
+ "id": 756,
+ "hex": "67809b"
+ }
+ }
+ },
+ {
+ "name": "Video Transmitter",
+ "id": 1002,
+ "variation": {
+ "name": "Hessel",
+ "id": 4535,
+ "color": {
+ "id": 111,
+ "hex": "144f9e"
+ }
+ }
+ }
+]
+```
+
### Association Traits
Models:
```ruby
class Product < ApplicationRecord
has_many :variations
@@ -429,10 +530,10 @@
Controller:
```ruby
render json: Product.limit(3).json(trait: :with_variations)
```
-
+Response:
```json
[
{
"id":1,
"variations":[