test/unit/serializer/serializer_test.rb in jsonapi-resources-0.6.1 vs test/unit/serializer/serializer_test.rb in jsonapi-resources-0.6.2
- old
+ new
@@ -1735,9 +1735,80 @@
},
serialized
)
end
+ def test_serializer_resource_meta_fixed_value
+ Api::V5::AuthorResource.class_eval do
+ def meta(options)
+ {
+ fixed: 'Hardcoded value',
+ computed: "#{self.class._type.to_s}: #{options[:serializer].url_generator.self_link(self)}"
+ }
+ end
+ end
+
+ serialized = JSONAPI::ResourceSerializer.new(
+ Api::V5::AuthorResource,
+ include: ['author_detail']
+ ).serialize_to_hash(Api::V5::AuthorResource.new(Person.find(1), nil))
+
+ assert_hash_equals(
+ {
+ data: {
+ type: 'authors',
+ id: '1',
+ attributes: {
+ name: 'Joe Author',
+ },
+ links: {
+ self: '/api/v5/authors/1'
+ },
+ relationships: {
+ posts: {
+ links: {
+ self: '/api/v5/authors/1/relationships/posts',
+ related: '/api/v5/authors/1/posts'
+ }
+ },
+ authorDetail: {
+ links: {
+ self: '/api/v5/authors/1/relationships/authorDetail',
+ related: '/api/v5/authors/1/authorDetail'
+ },
+ data: {type: 'authorDetails', id: '1'}
+ }
+ },
+ meta: {
+ fixed: 'Hardcoded value',
+ computed: 'authors: /api/v5/authors/1'
+ }
+ },
+ included: [
+ {
+ type: 'authorDetails',
+ id: '1',
+ attributes: {
+ authorStuff: 'blah blah'
+ },
+ links: {
+ self: '/api/v5/authorDetails/1'
+ }
+ }
+ ]
+ },
+ serialized
+ )
+ ensure
+ Api::V5::AuthorResource.class_eval do
+ def meta(options)
+ # :nocov:
+ { }
+ # :nocov:
+ end
+ end
+ end
+
def test_serialize_model_attr
@make = Make.first
serialized = JSONAPI::ResourceSerializer.new(
MakeResource,
).serialize_to_hash(MakeResource.new(@make, nil))