Skip to content

Commit

Permalink
Merge pull request #984 from Rodrigora/add-key-option-to-associations
Browse files Browse the repository at this point in the history
Add option "key" to serializer associations
  • Loading branch information
joaomdmoura committed Jul 6, 2015
2 parents ac1991f + df63b59 commit 90fb1cf
Show file tree
Hide file tree
Showing 8 changed files with 109 additions and 29 deletions.
6 changes: 6 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -264,6 +264,12 @@ You may also use the `:serializer` option to specify a custom serializer class,
has_many :comments, serializer: CommentPreviewSerializer
```

And you can change the JSON key that the serializer should use for a particular association:

```ruby
has_many :comments, key: :reviews
```

The `url` declaration describes which named routes to use while generating URLs
for your JSON. Not every adapter will require URLs.

Expand Down
3 changes: 2 additions & 1 deletion lib/active_model/serializer.rb
Original file line number Diff line number Diff line change
Expand Up @@ -220,8 +220,9 @@ def each_association(&block)
association_options[:association_options][:virtual_value] = association_value
end

association_key = association_options[:association_options][:key] || name
if block_given?
block.call(name, serializer, association_options[:association_options])
block.call(association_key, serializer, association_options[:association_options])
end
end
end
Expand Down
10 changes: 5 additions & 5 deletions lib/active_model/serializer/adapter/json.rb
Original file line number Diff line number Diff line change
Expand Up @@ -15,23 +15,23 @@ def serializable_hash(options = nil)
serializer.attributes(options)
end

serializer.each_association do |name, association, opts|
serializer.each_association do |key, association, opts|
if association.respond_to?(:each)
array_serializer = association
@hash[name] = array_serializer.map do |item|
@hash[key] = array_serializer.map do |item|
cache_check(item) do
item.attributes(opts)
end
end
else
if association && association.object
@hash[name] = cache_check(association) do
@hash[key] = cache_check(association) do
association.attributes(options)
end
elsif opts[:virtual_value]
@hash[name] = opts[:virtual_value]
@hash[key] = opts[:virtual_value]
else
@hash[name] = nil
@hash[key] = nil
end
end
end
Expand Down
14 changes: 7 additions & 7 deletions lib/active_model/serializer/adapter/json_api.rb
Original file line number Diff line number Diff line change
Expand Up @@ -75,8 +75,8 @@ def add_included(resource_name, serializers, parent = nil)
end

serializers.each do |serializer|
serializer.each_association do |name, association, opts|
add_included(name, association, resource_path) if association
serializer.each_association do |key, association, opts|
add_included(key, association, resource_path) if association
end if include_nested_assoc? resource_path
end
end
Expand Down Expand Up @@ -131,22 +131,22 @@ def check_assoc(assoc)
def add_resource_relationships(attrs, serializer, options = {})
options[:add_included] = options.fetch(:add_included, true)

serializer.each_association do |name, association, opts|
serializer.each_association do |key, association, opts|
attrs[:relationships] ||= {}

if association.respond_to?(:each)
add_relationships(attrs, name, association)
add_relationships(attrs, key, association)
else
if opts[:virtual_value]
add_relationship(attrs, name, nil, opts[:virtual_value])
add_relationship(attrs, key, nil, opts[:virtual_value])
else
add_relationship(attrs, name, association)
add_relationship(attrs, key, association)
end
end

if options[:add_included]
Array(association).each do |association|
add_included(name, association)
add_included(key, association)
end
end
end
Expand Down
38 changes: 38 additions & 0 deletions test/adapter/json_api/json_api_test.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
require 'test_helper'

module ActiveModel
class Serializer
class Adapter
class JsonApiTest < Minitest::Test
def setup
ActionController::Base.cache_store.clear
@author = Author.new(id: 1, name: 'Steve K.')
@post = Post.new(id: 1, title: 'New Post', body: 'Body')
@first_comment = Comment.new(id: 1, body: 'ZOMG A COMMENT')
@second_comment = Comment.new(id: 2, body: 'ZOMG ANOTHER COMMENT')
@post.comments = [@first_comment, @second_comment]
@first_comment.post = @post
@second_comment.post = @post
@post.author = @author
@blog = Blog.new(id: 1, name: "My Blog!!")
@post.blog = @blog

end

def test_custom_keys
serializer = PostWithCustomKeysSerializer.new(@post)
adapter = ActiveModel::Serializer::Adapter::JsonApi.new(serializer)

assert_equal({
reviews: { data: [
{type: "comments", id: "1"},
{type: "comments", id: "2"}
]},
writer: { data: {type: "authors", id: "1"} },
site: { data: {type: "blogs", id: "1" } }
}, adapter.serializable_hash[:data][:relationships])
end
end
end
end
end
16 changes: 15 additions & 1 deletion test/adapter/json_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ class JsonTest < Minitest::Test
def setup
ActionController::Base.cache_store.clear
@author = Author.new(id: 1, name: 'Steve K.')
@post = Post.new(title: 'New Post', body: 'Body')
@post = Post.new(id: 1, title: 'New Post', body: 'Body')
@first_comment = Comment.new(id: 1, body: 'ZOMG A COMMENT')
@second_comment = Comment.new(id: 2, body: 'ZOMG ANOTHER COMMENT')
@post.comments = [@first_comment, @second_comment]
Expand All @@ -27,6 +27,20 @@ def test_has_many
{id: 2, body: 'ZOMG ANOTHER COMMENT'}
], @adapter.serializable_hash[:post][:comments])
end

def test_custom_keys
serializer = PostWithCustomKeysSerializer.new(@post)
adapter = ActiveModel::Serializer::Adapter::Json.new(serializer)

assert_equal({
id: 1,
reviews: [{id: 1, body: "ZOMG A COMMENT"},
{id: 2, body: "ZOMG ANOTHER COMMENT"}
],
writer: {id: 1, name: "Steve K."},
site: {id: 1, name: "My Blog!!"}
}, adapter.serializable_hash[:post_with_custom_keys])
end
end
end
end
Expand Down
8 changes: 8 additions & 0 deletions test/fixtures/poro.rb
Original file line number Diff line number Diff line change
Expand Up @@ -228,6 +228,14 @@ def self.root_name
has_many :tags
end

PostWithCustomKeysSerializer = Class.new(ActiveModel::Serializer) do
attributes :id

has_many :comments, key: :reviews
belongs_to :author, key: :writer
has_one :blog, key: :site
end

VirtualValueSerializer = Class.new(ActiveModel::Serializer) do
attributes :id

Expand Down
43 changes: 28 additions & 15 deletions test/serializers/associations_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -51,33 +51,33 @@ def test_has_many_and_has_one
bio: { type: :has_one, association_options: {} } },
@author_serializer.class._associations
)
@author_serializer.each_association do |name, serializer, options|
if name == :posts
@author_serializer.each_association do |key, serializer, options|
if key == :posts
assert_equal({embed: :ids}, options)
assert_kind_of(ActiveModel::Serializer.config.array_serializer, serializer)
elsif name == :bio
elsif key == :bio
assert_equal({}, options)
assert_nil serializer
elsif name == :roles
elsif key == :roles
assert_equal({embed: :ids}, options)
assert_kind_of(ActiveModel::Serializer.config.array_serializer, serializer)
else
flunk "Unknown association: #{name}"
flunk "Unknown association: #{key}"
end
end
end

def test_has_many_with_no_serializer
PostWithTagsSerializer.new(@post).each_association do |name, serializer, options|
assert_equal name, :tags
PostWithTagsSerializer.new(@post).each_association do |key, serializer, options|
assert_equal key, :tags
assert_equal serializer, nil
assert_equal [{ attributes: { name: "#hashtagged" }}].to_json, options[:virtual_value].to_json
end
end

def test_serializer_options_are_passed_into_associations_serializers
@post_serializer.each_association do |name, association|
if name == :comments
@post_serializer.each_association do |key, association|
if key == :comments
assert association.first.custom_options[:custom_options]
end
end
Expand All @@ -89,24 +89,24 @@ def test_belongs_to
author: { type: :belongs_to, association_options: {} } },
@comment_serializer.class._associations
)
@comment_serializer.each_association do |name, serializer, options|
if name == :post
@comment_serializer.each_association do |key, serializer, options|
if key == :post
assert_equal({}, options)
assert_kind_of(PostSerializer, serializer)
elsif name == :author
elsif key == :author
assert_equal({}, options)
assert_nil serializer
else
flunk "Unknown association: #{name}"
flunk "Unknown association: #{key}"
end
end
end

def test_belongs_to_with_custom_method
blog_is_present = false

@post_serializer.each_association do |name, serializer, options|
blog_is_present = true if name == :blog
@post_serializer.each_association do |key, serializer, options|
blog_is_present = true if key == :blog
end

assert blog_is_present
Expand All @@ -132,6 +132,19 @@ def test_associations_inheritance_with_new_association
)
assert_equal(inherited_klass._associations, expected_associations)
end

def test_associations_custom_keys
serializer = PostWithCustomKeysSerializer.new(@post)

expected_association_keys = []
serializer.each_association do |key, serializer, options|
expected_association_keys << key
end

assert expected_association_keys.include? :reviews
assert expected_association_keys.include? :writer
assert expected_association_keys.include? :site
end
end
end
end

0 comments on commit 90fb1cf

Please sign in to comment.