-
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.
Extended format for JSONAPI
include
option.
- Loading branch information
Showing
12 changed files
with
160 additions
and
57 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
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,35 @@ | ||
module ActiveModel::Serializer::Utils | ||
module_function | ||
|
||
# Translates a comma separated list of dot separated paths (JSONAPI format) into a Hash. | ||
# Example: `'posts.author, posts.comments.upvotes, posts.comments.author'` would become `{ posts: { author: {}, comments: { author: {}, upvotes: {} } } }`. | ||
# | ||
# @param [String] included | ||
# @return [Hash] a Hash representing the same tree structure | ||
def include_string_to_hash(included) | ||
included.delete(' ').split(',').inject({}) do |hash, path| | ||
hash.deep_merge!(path.split('.').reverse_each.inject({}) { |a, e| { e.to_sym => a } }) | ||
end | ||
end | ||
|
||
# Translates the arguments passed to the include option into a Hash. The format can be either | ||
# a String (see #include_string_to_hash), an Array of Symbols and Hashes, or a mix of both. | ||
# Example: `posts: [:author, comments: [:author, :upvotes]]` would become `{ posts: { author: {}, comments: { author: {}, upvotes: {} } } }`. | ||
# | ||
# @param [Symbol, Hash, Array, String] included | ||
# @return [Hash] a Hash representing the same tree structure | ||
def include_args_to_hash(included) | ||
case included | ||
when Symbol | ||
{ included => {} } | ||
when Hash | ||
included.each_with_object({}) { |(key, value), hash| hash[key] = include_args_to_hash(value) } | ||
when Array | ||
included.inject({}) { |a, e| a.merge!(include_args_to_hash(e)) } | ||
when String | ||
include_string_to_hash(included) | ||
else | ||
{} | ||
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
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,79 @@ | ||
require 'test_helper' | ||
|
||
module ActiveModel | ||
class Serializer | ||
module Utils | ||
class IncludeArgsToHashTest < Minitest::Test | ||
def test_nil | ||
input = nil | ||
expected = {} | ||
actual = ActiveModel::Serializer::Utils.include_args_to_hash(input) | ||
assert_equal(expected, actual) | ||
end | ||
|
||
def test_empty_string | ||
input = '' | ||
expected = {} | ||
actual = ActiveModel::Serializer::Utils.include_args_to_hash(input) | ||
assert_equal(expected, actual) | ||
end | ||
|
||
def test_single_string | ||
input = 'author' | ||
expected = { author: {} } | ||
actual = ActiveModel::Serializer::Utils.include_args_to_hash(input) | ||
assert_equal(expected, actual) | ||
end | ||
|
||
def test_multiple_strings | ||
input = 'author,comments' | ||
expected = { author: {}, comments: {} } | ||
actual = ActiveModel::Serializer::Utils.include_args_to_hash(input) | ||
assert_equal(expected, actual) | ||
end | ||
|
||
def test_multiple_strings_with_space | ||
input = 'author, comments' | ||
expected = { author: {}, comments: {} } | ||
actual = ActiveModel::Serializer::Utils.include_args_to_hash(input) | ||
assert_equal(expected, actual) | ||
end | ||
|
||
def test_nested_string | ||
input = 'posts.author' | ||
expected = { posts: { author: {} } } | ||
actual = ActiveModel::Serializer::Utils.include_args_to_hash(input) | ||
assert_equal(expected, actual) | ||
end | ||
|
||
def test_multiple_nested_string | ||
input = 'posts.author,posts.comments.author,comments' | ||
expected = { posts: { author: {}, comments: { author: {} } }, comments: {} } | ||
actual = ActiveModel::Serializer::Utils.include_args_to_hash(input) | ||
assert_equal(expected, actual) | ||
end | ||
|
||
def test_empty_array | ||
input = [] | ||
expected = {} | ||
actual = ActiveModel::Serializer::Utils.include_args_to_hash(input) | ||
assert_equal(expected, actual) | ||
end | ||
|
||
def test_simple_array | ||
input = [:comments, :author] | ||
expected = { author: {}, comments: {} } | ||
actual = ActiveModel::Serializer::Utils.include_args_to_hash(input) | ||
assert_equal(expected, actual) | ||
end | ||
|
||
def test_nested_array | ||
input = [:comments, posts: [:author, comments: [:author]]] | ||
expected = { posts: { author: {}, comments: { author: {} } }, comments: {} } | ||
actual = ActiveModel::Serializer::Utils.include_args_to_hash(input) | ||
assert_equal(expected, actual) | ||
end | ||
end | ||
end | ||
end | ||
end |