-
Notifications
You must be signed in to change notification settings - Fork 1.4k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #768 from rails-api/adds-meta
Adds support for meta attribute
- Loading branch information
Showing
7 changed files
with
170 additions
and
15 deletions.
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
### 0.10.0 | ||
|
||
* adds support for `meta` and `meta_key` [@kurko] |
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
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,77 @@ | ||
require 'test_helper' | ||
|
||
module ActiveModel | ||
class Serializer | ||
class MetaTest < Minitest::Test | ||
def setup | ||
@blog = Blog.new(id: 1, | ||
name: 'AMS Hints', | ||
writer: Author.new(id: 2, name: "Steve"), | ||
articles: [Post.new(id: 3, title: "AMS")]) | ||
end | ||
|
||
def test_meta_is_present_with_root | ||
adapter = load_adapter(root: "blog", meta: {total: 10}) | ||
expected = { | ||
"blog" => { | ||
id: 1, | ||
title: "AMS Hints" | ||
}, | ||
"meta" => { | ||
total: 10 | ||
} | ||
} | ||
assert_equal expected, adapter.as_json | ||
end | ||
|
||
def test_meta_is_not_included_when_root_is_missing | ||
adapter = load_adapter(meta: {total: 10}) | ||
expected = { | ||
id: 1, | ||
title: "AMS Hints" | ||
} | ||
assert_equal expected, adapter.as_json | ||
end | ||
|
||
def test_meta_key_is_used | ||
adapter = load_adapter(root: "blog", meta: {total: 10}, meta_key: "haha_meta") | ||
expected = { | ||
"blog" => { | ||
id: 1, | ||
title: "AMS Hints" | ||
}, | ||
"haha_meta" => { | ||
total: 10 | ||
} | ||
} | ||
assert_equal expected, adapter.as_json | ||
end | ||
|
||
def test_meta_is_not_used_on_arrays | ||
serializer = ArraySerializer.new([@blog], root: "blog", meta: {total: 10}, meta_key: "haha_meta") | ||
adapter = ActiveModel::Serializer::Adapter::Json.new(serializer) | ||
expected = [{ | ||
id: 1, | ||
name: "AMS Hints", | ||
writer: { | ||
id: 2, | ||
name: "Steve" | ||
}, | ||
articles: [{ | ||
id: 3, | ||
title: "AMS", | ||
body: nil | ||
}] | ||
}] | ||
assert_equal expected, adapter.as_json | ||
end | ||
|
||
private | ||
|
||
def load_adapter(options) | ||
serializer = AlternateBlogSerializer.new(@blog, options) | ||
ActiveModel::Serializer::Adapter::Json.new(serializer) | ||
end | ||
end | ||
end | ||
end |