fast_jsonapi 1.4 → 1.5
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 +5 -5
- data/README.md +29 -6
- data/lib/fast_jsonapi/object_serializer.rb +14 -5
- data/lib/fast_jsonapi/relationship.rb +18 -5
- data/lib/fast_jsonapi/serialization_core.rb +4 -3
- data/lib/fast_jsonapi/version.rb +1 -1
- metadata +3 -3
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
|
-
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
2
|
+
SHA256:
|
3
|
+
metadata.gz: 2b436b8a9f4939508f4bc4c663c4436d7d5502c43593aef8ee4f54f0a09aa8ea
|
4
|
+
data.tar.gz: 5edb4a0b7e4d3f17029751ced90b3e85ef4ff5b3f5021f05463130094a044ebb
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: db6b30cdf34b23db688f16bba796439ce2b70040fa9b262d4e1c62ad7c415a11b9c3a7fb7abd3e647cf030c9c73265ffb02db52bf0d1971cb0b41a130caa281c
|
7
|
+
data.tar.gz: 8e40ad82d86ed65532bb6b64844b7755af6e350f8979b818c1f71861b20d791e3f32b143bf2ce94f2bf63095af30ca4be12ec1a8dd286996c93d6ef6f5898c6b
|
data/README.md
CHANGED
@@ -221,7 +221,7 @@ end
|
|
221
221
|
```
|
222
222
|
|
223
223
|
### Links Per Object
|
224
|
-
Links are defined in FastJsonapi using the `link` method. By default,
|
224
|
+
Links are defined in FastJsonapi using the `link` method. By default, links are read directly from the model property of the same name. In this example, `public_url` is expected to be a property of the object being serialized.
|
225
225
|
|
226
226
|
You can configure the method to use on the object for example a link with key `self` will get set to the value returned by a method called `url` on the movie object.
|
227
227
|
|
@@ -245,15 +245,38 @@ class MovieSerializer
|
|
245
245
|
end
|
246
246
|
```
|
247
247
|
|
248
|
-
|
249
|
-
|
250
|
-
For every resource in the collection, you can include a meta object containing non-standard meta-information about a resource that can not be represented as an attribute or relationship.
|
248
|
+
#### Links on a Relationship
|
251
249
|
|
250
|
+
You can specify [relationship links](http://jsonapi.org/format/#document-resource-object-relationships) by using the `links:` option on the serializer. Relationship links in JSON API are useful if you want to load a parent document and then load associated documents later due to size constraints (see [related resource links](http://jsonapi.org/format/#document-resource-object-related-resource-links))
|
252
251
|
|
253
252
|
```ruby
|
254
253
|
class MovieSerializer
|
255
254
|
include FastJsonapi::ObjectSerializer
|
256
255
|
|
256
|
+
has_many :actors, links: {
|
257
|
+
self: :url,
|
258
|
+
related: -> (object) {
|
259
|
+
"https://movies.com/#{object.id}/actors"
|
260
|
+
}
|
261
|
+
}
|
262
|
+
end
|
263
|
+
```
|
264
|
+
|
265
|
+
This will create a `self` reference for the relationship, and a `related` link for loading the actors relationship later. NB: This will not automatically disable loading the data in the relationship, you'll need to do that using the `lazy_load_data` option:
|
266
|
+
|
267
|
+
```ruby
|
268
|
+
has_many :actors, lazy_load_data: true, links: {
|
269
|
+
self: :url,
|
270
|
+
related: -> (object) {
|
271
|
+
"https://movies.com/#{object.id}/actors"
|
272
|
+
}
|
273
|
+
}
|
274
|
+
```
|
275
|
+
|
276
|
+
### Meta Per Resource
|
277
|
+
|
278
|
+
For every resource in the collection, you can include a meta object containing non-standard meta-information about a resource that can not be represented as an attribute or relationship.
|
279
|
+
```ruby
|
257
280
|
meta do |movie|
|
258
281
|
{
|
259
282
|
years_since_release: Date.current.year - movie.year
|
@@ -427,9 +450,9 @@ Option | Purpose | Example
|
|
427
450
|
------------ | ------------- | -------------
|
428
451
|
set_type | Type name of Object | ```set_type :movie ```
|
429
452
|
key | Key of Object | ```belongs_to :owner, key: :user ```
|
430
|
-
set_id | ID of Object | ```set_id :owner_id ```
|
453
|
+
set_id | ID of Object | ```set_id :owner_id ``` or ```set_id { |record| "#{record.name.downcase}-#{record.id}" }```
|
431
454
|
cache_options | Hash to enable caching and set cache length | ```cache_options enabled: true, cache_length: 12.hours, race_condition_ttl: 10.seconds```
|
432
|
-
id_method_name | Set custom method name to get ID of an object | ```has_many :locations, id_method_name: :place_ids ```
|
455
|
+
id_method_name | Set custom method name to get ID of an object (If block is provided for the relationship, `id_method_name` is invoked on the return value of the block instead of the resource object) | ```has_many :locations, id_method_name: :place_ids ```
|
433
456
|
object_method_name | Set custom method name to get related objects | ```has_many :locations, object_method_name: :places ```
|
434
457
|
record_type | Set custom Object Type for a relationship | ```belongs_to :owner, record_type: :user```
|
435
458
|
serializer | Set custom Serializer for a relationship | ```has_many :actors, serializer: :custom_actor``` or ```has_many :actors, serializer: MyApp::Api::V1::ActorSerializer```
|
@@ -1,8 +1,10 @@
|
|
1
1
|
# frozen_string_literal: true
|
2
2
|
|
3
|
+
require 'active_support/time'
|
3
4
|
require 'active_support/json'
|
4
5
|
require 'active_support/concern'
|
5
6
|
require 'active_support/inflector'
|
7
|
+
require 'active_support/core_ext/numeric/time'
|
6
8
|
require 'fast_jsonapi/attribute'
|
7
9
|
require 'fast_jsonapi/relationship'
|
8
10
|
require 'fast_jsonapi/link'
|
@@ -72,6 +74,7 @@ module FastJsonapi
|
|
72
74
|
|
73
75
|
def process_options(options)
|
74
76
|
@fieldsets = deep_symbolize(options[:fields].presence || {})
|
77
|
+
@params = {}
|
75
78
|
|
76
79
|
return if options.blank?
|
77
80
|
|
@@ -117,7 +120,7 @@ module FastJsonapi
|
|
117
120
|
subclass.transform_method = transform_method
|
118
121
|
subclass.cache_length = cache_length
|
119
122
|
subclass.race_condition_ttl = race_condition_ttl
|
120
|
-
subclass.data_links = data_links
|
123
|
+
subclass.data_links = data_links.dup if data_links.present?
|
121
124
|
subclass.cached = cached
|
122
125
|
subclass.set_type(subclass.reflected_record_type) if subclass.reflected_record_type
|
123
126
|
subclass.meta_to_serialize = meta_to_serialize
|
@@ -143,7 +146,11 @@ module FastJsonapi
|
|
143
146
|
self.transform_method = mapping[transform_name.to_sym]
|
144
147
|
|
145
148
|
# ensure that the record type is correctly transformed
|
146
|
-
|
149
|
+
if record_type
|
150
|
+
set_type(record_type)
|
151
|
+
elsif reflected_record_type
|
152
|
+
set_type(reflected_record_type)
|
153
|
+
end
|
147
154
|
end
|
148
155
|
|
149
156
|
def run_key_transform(input)
|
@@ -163,8 +170,8 @@ module FastJsonapi
|
|
163
170
|
self.record_type = run_key_transform(type_name)
|
164
171
|
end
|
165
172
|
|
166
|
-
def set_id(id_name)
|
167
|
-
self.record_id = id_name
|
173
|
+
def set_id(id_name = nil, &block)
|
174
|
+
self.record_id = block || id_name
|
168
175
|
end
|
169
176
|
|
170
177
|
def cache_options(cache_options)
|
@@ -250,7 +257,9 @@ module FastJsonapi
|
|
250
257
|
cached: options[:cached],
|
251
258
|
polymorphic: fetch_polymorphic_option(options),
|
252
259
|
conditional_proc: options[:if],
|
253
|
-
transform_method: @transform_method
|
260
|
+
transform_method: @transform_method,
|
261
|
+
links: options[:links],
|
262
|
+
lazy_load_data: options[:lazy_load_data]
|
254
263
|
)
|
255
264
|
end
|
256
265
|
|
@@ -1,6 +1,6 @@
|
|
1
1
|
module FastJsonapi
|
2
2
|
class Relationship
|
3
|
-
attr_reader :key, :name, :id_method_name, :record_type, :object_method_name, :object_block, :serializer, :relationship_type, :cached, :polymorphic, :conditional_proc, :transform_method
|
3
|
+
attr_reader :key, :name, :id_method_name, :record_type, :object_method_name, :object_block, :serializer, :relationship_type, :cached, :polymorphic, :conditional_proc, :transform_method, :links, :lazy_load_data
|
4
4
|
|
5
5
|
def initialize(
|
6
6
|
key:,
|
@@ -14,7 +14,9 @@ module FastJsonapi
|
|
14
14
|
cached: false,
|
15
15
|
polymorphic:,
|
16
16
|
conditional_proc:,
|
17
|
-
transform_method
|
17
|
+
transform_method:,
|
18
|
+
links:,
|
19
|
+
lazy_load_data: false
|
18
20
|
)
|
19
21
|
@key = key
|
20
22
|
@name = name
|
@@ -28,14 +30,19 @@ module FastJsonapi
|
|
28
30
|
@polymorphic = polymorphic
|
29
31
|
@conditional_proc = conditional_proc
|
30
32
|
@transform_method = transform_method
|
33
|
+
@links = links || {}
|
34
|
+
@lazy_load_data = lazy_load_data
|
31
35
|
end
|
32
36
|
|
33
37
|
def serialize(record, serialization_params, output_hash)
|
34
38
|
if include_relationship?(record, serialization_params)
|
35
39
|
empty_case = relationship_type == :has_many ? [] : nil
|
36
|
-
|
37
|
-
|
38
|
-
|
40
|
+
|
41
|
+
output_hash[key] = {}
|
42
|
+
unless lazy_load_data
|
43
|
+
output_hash[key][:data] = ids_hash_from_record_and_relationship(record, serialization_params) || empty_case
|
44
|
+
end
|
45
|
+
add_links_hash(record, serialization_params, output_hash) if links.present?
|
39
46
|
end
|
40
47
|
end
|
41
48
|
|
@@ -96,6 +103,12 @@ module FastJsonapi
|
|
96
103
|
record.public_send(id_method_name)
|
97
104
|
end
|
98
105
|
|
106
|
+
def add_links_hash(record, params, output_hash)
|
107
|
+
output_hash[key][:links] = links.each_with_object({}) do |(key, method), hash|
|
108
|
+
Link.new(key: key, method: method).serialize(record, params, hash)\
|
109
|
+
end
|
110
|
+
end
|
111
|
+
|
99
112
|
def run_key_transform(input)
|
100
113
|
if self.transform_method.present?
|
101
114
|
input.to_s.send(*self.transform_method).to_sym
|
@@ -86,9 +86,10 @@ module FastJsonapi
|
|
86
86
|
end
|
87
87
|
|
88
88
|
def id_from_record(record)
|
89
|
-
|
90
|
-
|
91
|
-
|
89
|
+
return record_id.call(record) if record_id.is_a?(Proc)
|
90
|
+
return record.send(record_id) if record_id
|
91
|
+
raise MandatoryField, 'id is a mandatory field in the jsonapi spec' unless record.respond_to?(:id)
|
92
|
+
record.id
|
92
93
|
end
|
93
94
|
|
94
95
|
# Override #to_json for alternative implementation
|
data/lib/fast_jsonapi/version.rb
CHANGED
metadata
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: fast_jsonapi
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: '1.
|
4
|
+
version: '1.5'
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Shishir Kakaraddi
|
@@ -10,7 +10,7 @@ authors:
|
|
10
10
|
autorequire:
|
11
11
|
bindir: bin
|
12
12
|
cert_chain: []
|
13
|
-
date: 2018-
|
13
|
+
date: 2018-11-03 00:00:00.000000000 Z
|
14
14
|
dependencies:
|
15
15
|
- !ruby/object:Gem::Dependency
|
16
16
|
name: activesupport
|
@@ -232,7 +232,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
|
|
232
232
|
version: '0'
|
233
233
|
requirements: []
|
234
234
|
rubyforge_project:
|
235
|
-
rubygems_version: 2.
|
235
|
+
rubygems_version: 2.7.7
|
236
236
|
signing_key:
|
237
237
|
specification_version: 4
|
238
238
|
summary: fast JSON API(jsonapi.org) serializer
|