-
Notifications
You must be signed in to change notification settings - Fork 1.4k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Add support for resource-level meta. #1340
Merged
Merged
Changes from all commits
Commits
Show all changes
3 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,29 @@ | ||
module ActiveModel | ||
class Serializer | ||
module Adapter | ||
class JsonApi | ||
class Meta | ||
def initialize(serializer) | ||
@object = serializer.object | ||
@scope = serializer.scope | ||
|
||
# Use the return value of the block unless it is nil. | ||
if serializer._meta.respond_to?(:call) | ||
@value = instance_eval(&serializer._meta) | ||
else | ||
@value = serializer._meta | ||
end | ||
end | ||
|
||
def as_json | ||
@value | ||
end | ||
|
||
protected | ||
|
||
attr_reader :object, :scope | ||
end | ||
end | ||
end | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,29 @@ | ||
module ActiveModel | ||
class Serializer | ||
module Meta | ||
extend ActiveSupport::Concern | ||
|
||
included do | ||
with_options instance_writer: false, instance_reader: true do |serializer| | ||
serializer.class_attribute :_meta # @api private | ||
end | ||
|
||
extend ActiveSupport::Autoload | ||
end | ||
|
||
module ClassMethods | ||
# Set the JSON API meta attribute of a serializer. | ||
# @example | ||
# class AdminAuthorSerializer < ActiveModel::Serializer | ||
# meta { stuff: 'value' } | ||
# @example | ||
# meta do | ||
# { comment_count: object.comments.count } | ||
# end | ||
def meta(value = nil, &block) | ||
self._meta = block || value | ||
end | ||
end | ||
end | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,63 @@ | ||
require 'test_helper' | ||
|
||
module ActiveModel | ||
class Serializer | ||
module Adapter | ||
class JsonApi | ||
class ResourceMetaTest < Minitest::Test | ||
class MetaHashPostSerializer < ActiveModel::Serializer | ||
attributes :id | ||
meta stuff: 'value' | ||
end | ||
|
||
class MetaBlockPostSerializer < ActiveModel::Serializer | ||
attributes :id | ||
meta do | ||
{ comments_count: object.comments.count } | ||
end | ||
end | ||
|
||
def setup | ||
@post = Post.new(id: 1337, comments: [], author: nil) | ||
end | ||
|
||
def test_meta_hash_object_resource | ||
hash = ActiveModel::SerializableResource.new( | ||
@post, | ||
serializer: MetaHashPostSerializer, | ||
adapter: :json_api | ||
).serializable_hash | ||
expected = { | ||
stuff: 'value' | ||
} | ||
assert_equal(expected, hash[:data][:meta]) | ||
end | ||
|
||
def test_meta_block_object_resource | ||
hash = ActiveModel::SerializableResource.new( | ||
@post, | ||
serializer: MetaBlockPostSerializer, | ||
adapter: :json_api | ||
).serializable_hash | ||
expected = { | ||
comments_count: @post.comments.count | ||
} | ||
assert_equal(expected, hash[:data][:meta]) | ||
end | ||
|
||
def test_meta_object_resource_in_array | ||
hash = ActiveModel::SerializableResource.new( | ||
[@post, @post], | ||
each_serializer: MetaBlockPostSerializer, | ||
adapter: :json_api | ||
).serializable_hash | ||
expected = { | ||
comments_count: @post.comments.count | ||
} | ||
assert_equal([expected, expected], hash[:data].map { |obj| obj[:meta] }) | ||
end | ||
end | ||
end | ||
end | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -3,6 +3,8 @@ | |
module ActiveModel | ||
class Serializer | ||
class MetaTest < ActiveSupport::TestCase | ||
MetaBlogSerializer = Class.new(ActiveModel::Serializer) | ||
|
||
def setup | ||
@blog = Blog.new(id: 1, | ||
name: 'AMS Hints', | ||
|
@@ -125,6 +127,20 @@ def test_meta_is_present_on_arrays_with_root | |
} | ||
assert_equal(expected, actual) | ||
end | ||
|
||
def test_meta_is_set_with_direct_attributes | ||
MetaBlogSerializer.meta stuff: 'value' | ||
blog_meta_serializer = MetaBlogSerializer.new(@blog) | ||
assert_equal(blog_meta_serializer.meta, stuff: 'value') | ||
end | ||
|
||
def test_meta_is_set_with_block | ||
MetaBlogSerializer.meta do | ||
{ articles_count: object.articles.count } | ||
end | ||
blog_meta_serializer = MetaBlogSerializer.new(@blog) | ||
assert_equal(blog_meta_serializer.meta, articles_count: @blog.articles.count) | ||
end | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I'd like to validate that meta creates valid JSON API objects. Do you think this PR is a good spot for that? |
||
end | ||
end | ||
end |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
fwiw, there's a test support helper
serializable
but it wouldn't make things much shorter. Do you think we should test Ruby hashes made fromas_json
instead of serializable_hash in the future?