mirror of
https://github.com/ditkrg/active_model_serializers.git
synced 2026-01-22 22:06:50 +00:00
Merge pull request #293 from goshakkk/mongoid-support
add support of mongoid collection serialization
This commit is contained in:
commit
da779c259f
12
README.md
12
README.md
@ -48,10 +48,14 @@ $ rails g serializer post
|
||||
### Support for PORO's and other ORM's.
|
||||
|
||||
Currently `ActiveModel::Serializers` adds serialization support to all models
|
||||
that descend from `ActiveRecord`. If you are using another ORM, or if you are
|
||||
using objects that are `ActiveModel` compliant but do not descend from
|
||||
`ActiveRecord`, you must add an include statement for
|
||||
`ActiveModel::SerializerSupport`.
|
||||
that descend from `ActiveRecord` or include `Mongoid::Document`. If you are
|
||||
using another ORM, or if you are using objects that are `ActiveModel`
|
||||
compliant but do not descend from `ActiveRecord` or include
|
||||
`Mongoid::Document`, you must add an include statement for
|
||||
`ActiveModel::SerializerSupport` to make models serializable. If you
|
||||
also want to make collections serializable, you should include
|
||||
`ActiveModel::ArraySerializationSupport` into your ORM's
|
||||
relation/criteria class.
|
||||
|
||||
# ActiveModel::Serializer
|
||||
|
||||
|
||||
@ -64,12 +64,6 @@ module ActiveModel::SerializerSupport
|
||||
alias :read_attribute_for_serialization :send
|
||||
end
|
||||
|
||||
[:active_record, :mongoid].each do |orm|
|
||||
ActiveSupport.on_load(orm) do
|
||||
include ActiveModel::SerializerSupport
|
||||
end
|
||||
end
|
||||
|
||||
module ActiveModel::ArraySerializerSupport
|
||||
def active_model_serializer
|
||||
ActiveModel::ArraySerializer
|
||||
@ -79,8 +73,14 @@ end
|
||||
Array.send(:include, ActiveModel::ArraySerializerSupport)
|
||||
Set.send(:include, ActiveModel::ArraySerializerSupport)
|
||||
|
||||
ActiveSupport.on_load(:active_record) do
|
||||
ActiveRecord::Relation.send(:include, ActiveModel::ArraySerializerSupport)
|
||||
{
|
||||
:active_record => 'ActiveRecord::Relation',
|
||||
:mongoid => 'Mongoid::Criteria'
|
||||
}.each do |orm, rel_class|
|
||||
ActiveSupport.on_load(orm) do
|
||||
include ActiveModel::SerializerSupport
|
||||
rel_class.constantize.send(:include, ActiveModel::ArraySerializerSupport)
|
||||
end
|
||||
end
|
||||
|
||||
begin
|
||||
|
||||
@ -20,6 +20,11 @@ module ActiveRecord
|
||||
end
|
||||
end
|
||||
|
||||
module Mongoid
|
||||
class Criteria
|
||||
end
|
||||
end
|
||||
|
||||
class SerializerSupportTest < ActiveModel::TestCase
|
||||
test "it returns nil if no serializer exists" do
|
||||
assert_equal nil, RandomModel.new.active_model_serializer
|
||||
@ -37,5 +42,10 @@ class SerializerSupportTest < ActiveModel::TestCase
|
||||
ActiveSupport.run_load_hooks(:active_record)
|
||||
assert_equal ActiveModel::ArraySerializer, ActiveRecord::Relation.new.active_model_serializer
|
||||
end
|
||||
|
||||
test "it automatically includes array_serializer in mongoid/criteria" do
|
||||
ActiveSupport.run_load_hooks(:mongoid)
|
||||
assert_equal ActiveModel::ArraySerializer, Mongoid::Criteria.new.active_model_serializer
|
||||
end
|
||||
end
|
||||
|
||||
|
||||
Loading…
Reference in New Issue
Block a user