mirror of
https://github.com/ditkrg/active_model_serializers.git
synced 2026-01-24 14:56:50 +00:00
Implement ArraySerializer
This commit is contained in:
parent
61a1669a86
commit
7e83f0c29d
19
lib/active_model/array_serializer.rb
Normal file
19
lib/active_model/array_serializer.rb
Normal file
@ -0,0 +1,19 @@
|
|||||||
|
module ActiveModel
|
||||||
|
class ArraySerializer
|
||||||
|
def initialize(object, options={})
|
||||||
|
@object = object
|
||||||
|
@options = options
|
||||||
|
end
|
||||||
|
|
||||||
|
def serializable_array
|
||||||
|
@object.map do |item|
|
||||||
|
if serializer = Serializer.serializer_for(item)
|
||||||
|
serializer.new(item).serializable_object(@options.merge(root: nil))
|
||||||
|
else
|
||||||
|
item.as_json
|
||||||
|
end
|
||||||
|
end
|
||||||
|
end
|
||||||
|
alias serializable_object serializable_array
|
||||||
|
end
|
||||||
|
end
|
||||||
@ -9,8 +9,12 @@ module ActiveModel
|
|||||||
end
|
end
|
||||||
|
|
||||||
def serializer_for(resource)
|
def serializer_for(resource)
|
||||||
|
if resource.respond_to?(:to_ary)
|
||||||
|
ArraySerializer
|
||||||
|
else
|
||||||
"#{resource.class.name}Serializer".safe_constantize
|
"#{resource.class.name}Serializer".safe_constantize
|
||||||
end
|
end
|
||||||
|
end
|
||||||
|
|
||||||
attr_accessor :_root, :_attributes, :_associations
|
attr_accessor :_root, :_attributes, :_associations
|
||||||
|
|
||||||
@ -114,6 +118,7 @@ module ActiveModel
|
|||||||
hash = attributes
|
hash = attributes
|
||||||
hash.merge! associations
|
hash.merge! associations
|
||||||
end
|
end
|
||||||
|
alias serializable_object serializable_hash
|
||||||
|
|
||||||
def as_json(options={})
|
def as_json(options={})
|
||||||
if root = options[:root] || self.root
|
if root = options[:root] || self.root
|
||||||
|
|||||||
@ -1,5 +1,6 @@
|
|||||||
require 'active_model'
|
require 'active_model'
|
||||||
require 'active_model/serializer'
|
require 'active_model/serializer'
|
||||||
|
require 'active_model/array_serializer'
|
||||||
|
|
||||||
begin
|
begin
|
||||||
require 'action_controller'
|
require 'action_controller'
|
||||||
|
|||||||
32
test/unit/active_model/array_serializer_test.rb
Normal file
32
test/unit/active_model/array_serializer_test.rb
Normal file
@ -0,0 +1,32 @@
|
|||||||
|
require 'test_helper'
|
||||||
|
require 'active_model/serializer'
|
||||||
|
|
||||||
|
module ActiveModel
|
||||||
|
class ArraySerializer
|
||||||
|
class Test < ActiveModel::TestCase
|
||||||
|
def setup
|
||||||
|
array = [1, 2, 3]
|
||||||
|
@serializer = ActiveModel::Serializer.serializer_for(array).new(array)
|
||||||
|
end
|
||||||
|
|
||||||
|
def test_serializer_for_array_returns_appropriate_type
|
||||||
|
assert_kind_of ArraySerializer, @serializer
|
||||||
|
end
|
||||||
|
|
||||||
|
def test_array_serializer_serializes_simple_objects
|
||||||
|
assert_equal [1, 2, 3], @serializer.serializable_array
|
||||||
|
end
|
||||||
|
|
||||||
|
def test_array_serializer_serializes_models
|
||||||
|
array = [Profile.new({ name: 'Name 1', description: 'Description 1', comments: 'Comments 1' }),
|
||||||
|
Profile.new({ name: 'Name 2', description: 'Description 2', comments: 'Comments 2' })]
|
||||||
|
serializer = ArraySerializer.new(array)
|
||||||
|
|
||||||
|
expected = [{'name' => 'Name 1', 'description' => 'Description 1'},
|
||||||
|
{'name' => 'Name 2', 'description' => 'Description 2'}]
|
||||||
|
|
||||||
|
assert_equal expected, serializer.serializable_array
|
||||||
|
end
|
||||||
|
end
|
||||||
|
end
|
||||||
|
end
|
||||||
Loading…
Reference in New Issue
Block a user