Skip to content

Commit

Permalink
Merge pull request #1013 from vyrak/root-option
Browse files Browse the repository at this point in the history
Root option with empty array support
  • Loading branch information
joaomdmoura committed Jul 17, 2015
2 parents 357d0d4 + 1b09d0e commit 7066b1f
Show file tree
Hide file tree
Showing 5 changed files with 121 additions and 21 deletions.
2 changes: 1 addition & 1 deletion lib/active_model/serializer.rb
Original file line number Diff line number Diff line change
Expand Up @@ -169,7 +169,7 @@ def initialize(object, options = {})
end

def json_key
self.class.root_name
@root || self.class.root_name
end

def id
Expand Down
14 changes: 4 additions & 10 deletions lib/active_model/serializer/array_serializer.rb
Original file line number Diff line number Diff line change
Expand Up @@ -5,9 +5,10 @@ class ArraySerializer
include Enumerable
delegate :each, to: :@objects

attr_reader :meta, :meta_key
attr_reader :root, :meta, :meta_key

def initialize(objects, options = {})
@root = options[:root]
@resource = objects
@objects = objects.map do |object|
serializer_class = options.fetch(
Expand All @@ -26,15 +27,8 @@ def initialize(objects, options = {})
end

def json_key
if @objects.first
@objects.first.json_key.pluralize
else
@resource.name.underscore.pluralize if @resource.try(:name)
end
end

def root=(root)
@objects.first.root = root if @objects.first
key = root || @objects.first.try(:json_key) || @resource.try(:name).try(:underscore)
key.try(:pluralize)
end
end
end
Expand Down
44 changes: 44 additions & 0 deletions test/action_controller/serialization_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,26 @@ def render_using_default_adapter_root
end
end

def render_array_using_custom_root
with_adapter ActiveModel::Serializer::Adapter::Json do
@profile = Profile.new({ name: 'Name 1', description: 'Description 1', comments: 'Comments 1' })
render json: [@profile], root: "custom_root"
end
end

def render_array_that_is_empty_using_custom_root
with_adapter ActiveModel::Serializer::Adapter::Json do
render json: [], root: "custom_root"
end
end

def render_object_using_custom_root
with_adapter ActiveModel::Serializer::Adapter::Json do
@profile = Profile.new({ name: 'Name 1', description: 'Description 1', comments: 'Comments 1' })
render json: @profile, root: "custom_root"
end
end

def render_array_using_implicit_serializer
array = [
Profile.new({ name: 'Name 1', description: 'Description 1', comments: 'Comments 1' }),
Expand Down Expand Up @@ -169,6 +189,30 @@ def test_render_using_default_root
assert_equal expected.to_json, @response.body
end

def test_render_array_using_custom_root
get :render_array_using_custom_root

expected = {custom_roots: [{name: "Name 1", description: "Description 1"}]}
assert_equal 'application/json', @response.content_type
assert_equal expected.to_json, @response.body
end

def test_render_array_that_is_empty_using_custom_root
get :render_array_that_is_empty_using_custom_root

expected = {custom_roots: []}
assert_equal 'application/json', @response.content_type
assert_equal expected.to_json, @response.body
end

def test_render_object_using_custom_root
get :render_object_using_custom_root

expected = {custom_root: {name: "Name 1", description: "Description 1"}}
assert_equal 'application/json', @response.content_type
assert_equal expected.to_json, @response.body
end

def test_render_json_object_without_serializer
get :render_json_object_without_serializer

Expand Down
63 changes: 53 additions & 10 deletions test/array_serializer_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,13 @@ class ArraySerializerTest < Minitest::Test
def setup
@comment = Comment.new
@post = Post.new
@serializer = ArraySerializer.new([@comment, @post], {some: :options})
@resource = build_named_collection @comment, @post
@serializer = ArraySerializer.new(@resource, {some: :options})
end

def build_named_collection(*resource)
resource.define_singleton_method(:name){ 'MeResource' }
resource
end

def test_respond_to_each
Expand Down Expand Up @@ -39,15 +45,52 @@ def test_meta_and_meta_key_attr_readers
assert_equal @serializer.meta_key, "the meta key"
end

def test_json_key_when_resource_is_empty
Array.class_eval do
def name
'PostComment'
end
end
@post_comments = []
@serializer = ArraySerializer.new(@post_comments)
assert_equal @serializer.json_key, "post_comments"
def test_root_default
@serializer = ArraySerializer.new([@comment, @post])
assert_equal @serializer.root, nil
end

def test_root
expected = 'custom_root'
@serializer = ArraySerializer.new([@comment, @post], root: expected)
assert_equal @serializer.root, expected
end

def test_root_with_no_serializers
expected = 'custom_root'
@serializer = ArraySerializer.new([], root: expected)
assert_equal @serializer.root, expected
end

def test_json_key
assert_equal @serializer.json_key, 'comments'
end

def test_json_key_with_resource_with_name_and_no_serializers
serializer = ArraySerializer.new(build_named_collection)
assert_equal serializer.json_key, 'me_resources'
end

def test_json_key_with_resource_with_nil_name_and_no_serializers
resource = []
resource.define_singleton_method(:name){ nil }
serializer = ArraySerializer.new(resource)
assert_equal serializer.json_key, nil
end

def test_json_key_with_resource_without_name_and_no_serializers
serializer = ArraySerializer.new([])
assert_equal serializer.json_key, nil
end

def test_json_key_with_root
serializer = ArraySerializer.new(@resource, root: 'custom_root')
assert_equal serializer.json_key, 'custom_roots'
end

def test_json_key_with_root_and_no_serializers
serializer = ArraySerializer.new(build_named_collection, root: 'custom_root')
assert_equal serializer.json_key, 'custom_roots'
end
end
end
Expand Down
19 changes: 19 additions & 0 deletions test/serializers/root_test.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
require 'test_helper'

module ActiveModel
class Serializer
class RootTest < Minitest::Test

def setup
@profile = Profile.new({ name: 'Name 1', description: 'Description 1', comments: 'Comments 1' })
@profile_serializer = ProfileSerializer.new(@post, {root: 'smth'})
end

def test_overwrite_root
setup
assert_equal('smth', @profile_serializer.json_key)
end

end
end
end

0 comments on commit 7066b1f

Please sign in to comment.