Skip to content

Commit dd71bc1

Browse files
committed
Introduce the ability to add meta tag for every resource in the collection
1 parent 5ff3fa9 commit dd71bc1

4 files changed

Lines changed: 58 additions & 1 deletion

File tree

README.md

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -245,6 +245,23 @@ class MovieSerializer
245245
end
246246
```
247247

248+
### Meta Per Resource
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.
251+
252+
253+
```ruby
254+
class MovieSerializer
255+
include FastJsonapi::ObjectSerializer
256+
257+
meta do |movie|
258+
{
259+
years_since_release: Date.current.year - movie.year
260+
}
261+
end
262+
end
263+
```
264+
248265
### Compound Document
249266

250267
Support for top-level and nested included associations through ` options[:include] `.

lib/fast_jsonapi/object_serializer.rb

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -120,6 +120,7 @@ def inherited(subclass)
120120
subclass.data_links = data_links
121121
subclass.cached = cached
122122
subclass.set_type(subclass.reflected_record_type) if subclass.reflected_record_type
123+
subclass.meta_to_serialize = meta_to_serialize
123124
end
124125

125126
def reflected_record_type
@@ -218,6 +219,10 @@ def belongs_to(relationship_name, options = {}, &block)
218219
add_relationship(relationship)
219220
end
220221

222+
def meta(&block)
223+
self.meta_to_serialize = block
224+
end
225+
221226
def create_relationship(base_key, relationship_type, options, block)
222227
name = base_key.to_sym
223228
if relationship_type == :has_many

lib/fast_jsonapi/serialization_core.rb

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,8 @@ class << self
2121
:cache_length,
2222
:race_condition_ttl,
2323
:cached,
24-
:data_links
24+
:data_links,
25+
:meta_to_serialize
2526
end
2627
end
2728

@@ -57,6 +58,10 @@ def relationships_hash(record, relationships = nil, fieldset = nil, params = {})
5758
end
5859
end
5960

61+
def meta_hash(record, params = {})
62+
meta_to_serialize.call(record, params)
63+
end
64+
6065
def record_hash(record, fieldset, params = {})
6166
if cached
6267
record_hash = Rails.cache.fetch(record.cache_key, expires_in: cache_length, race_condition_ttl: race_condition_ttl) do
@@ -68,12 +73,14 @@ def record_hash(record, fieldset, params = {})
6873
temp_hash
6974
end
7075
record_hash[:relationships] = record_hash[:relationships].merge(relationships_hash(record, uncachable_relationships_to_serialize, params)) if uncachable_relationships_to_serialize.present?
76+
record_hash[:meta] = meta_hash(record, params) if meta_to_serialize.present?
7177
record_hash
7278
else
7379
record_hash = id_hash(id_from_record(record), record_type, true)
7480
record_hash[:attributes] = attributes_hash(record, fieldset, params) if attributes_to_serialize.present?
7581
record_hash[:relationships] = relationships_hash(record, nil, fieldset, params) if relationships_to_serialize.present?
7682
record_hash[:links] = links_hash(record, params) if data_links.present?
83+
record_hash[:meta] = meta_hash(record, params) if meta_to_serialize.present?
7784
record_hash
7885
end
7986
end

spec/lib/object_serializer_class_methods_spec.rb

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -249,6 +249,34 @@
249249
end
250250
end
251251

252+
describe '#meta' do
253+
subject(:serializable_hash) { MovieSerializer.new(movie).serializable_hash }
254+
255+
before do
256+
movie.release_year = 2008
257+
MovieSerializer.meta do |movie|
258+
{
259+
years_since_release: year_since_release_calculator(movie.release_year)
260+
}
261+
end
262+
end
263+
264+
after do
265+
movie.release_year = nil
266+
MovieSerializer.meta_to_serialize = nil
267+
end
268+
269+
it 'returns correct hash when serializable_hash is called' do
270+
expect(serializable_hash[:data][:meta]).to eq ({ years_since_release: year_since_release_calculator(movie.release_year) })
271+
end
272+
273+
private
274+
275+
def year_since_release_calculator(release_year)
276+
Date.current.year - release_year
277+
end
278+
end
279+
252280
describe '#link' do
253281
subject(:serializable_hash) { MovieSerializer.new(movie).serializable_hash }
254282

0 commit comments

Comments
 (0)