diff options
author | Jose and Yehuda <wycats@gmail.com> | 2011-10-15 19:22:16 +0200 |
---|---|---|
committer | Jose and Yehuda <wycats@gmail.com> | 2011-10-15 19:22:16 +0200 |
commit | 2abb2e617af8e3353d4411a8bd51d03256e0274a (patch) | |
tree | fcfbe4d7c40d476ffebb456e39aab89a7c21aa64 /activemodel/lib | |
parent | a230f040ff61f069d46a9b86417a8e251016d5db (diff) | |
download | rails-2abb2e617af8e3353d4411a8bd51d03256e0274a.tar.gz rails-2abb2e617af8e3353d4411a8bd51d03256e0274a.tar.bz2 rails-2abb2e617af8e3353d4411a8bd51d03256e0274a.zip |
Add initial support for embed API
Diffstat (limited to 'activemodel/lib')
-rw-r--r-- | activemodel/lib/active_model/serializer.rb | 22 |
1 files changed, 19 insertions, 3 deletions
diff --git a/activemodel/lib/active_model/serializer.rb b/activemodel/lib/active_model/serializer.rb index 98801ae633..6d0746a3e8 100644 --- a/activemodel/lib/active_model/serializer.rb +++ b/activemodel/lib/active_model/serializer.rb @@ -21,7 +21,7 @@ module ActiveModel def serialize_ids(collection, scope) # use named scopes if they are present - return collection.ids if collection.respond_to?(:ids) + #return collection.ids if collection.respond_to?(:ids) collection.map do |item| item.read_attribute_for_serialization(:id) @@ -47,6 +47,9 @@ module ActiveModel self._associations = [] class_attribute :_root + class_attribute :_embed + self._embed = :objects + class_attribute :_root_embed class << self def attributes(*attrs) @@ -73,6 +76,11 @@ module ActiveModel associate(Associations::HasOne, attrs) end + def embed(type, options={}) + self._embed = type + self._root_embed = true if options[:include] + end + def root(name) self._root = name end @@ -97,14 +105,22 @@ module ActiveModel def as_json(*) if _root - { _root => serializable_hash } + hash = { _root => serializable_hash } + hash.merge!(associations) if _root_embed + hash else serializable_hash end end def serializable_hash - attributes.merge(associations) + if _embed == :ids + attributes.merge(association_ids) + elsif _embed == :objects + attributes.merge(associations) + else + attributes + end end def associations |