diff options
author | Jose and Yehuda <wycats@gmail.com> | 2011-10-15 18:54:20 +0200 |
---|---|---|
committer | Jose and Yehuda <wycats@gmail.com> | 2011-10-15 18:56:47 +0200 |
commit | a230f040ff61f069d46a9b86417a8e251016d5db (patch) | |
tree | 0b05a059be5cdce5a057fee1f405a8276fe3046b /activemodel/lib/active_model/serializer.rb | |
parent | 7a28498b55913aa0fd1d3529909ab57eaf64af0e (diff) | |
download | rails-a230f040ff61f069d46a9b86417a8e251016d5db.tar.gz rails-a230f040ff61f069d46a9b86417a8e251016d5db.tar.bz2 rails-a230f040ff61f069d46a9b86417a8e251016d5db.zip |
Add support for the root attribute
Diffstat (limited to 'activemodel/lib/active_model/serializer.rb')
-rw-r--r-- | activemodel/lib/active_model/serializer.rb | 13 |
1 files changed, 12 insertions, 1 deletions
diff --git a/activemodel/lib/active_model/serializer.rb b/activemodel/lib/active_model/serializer.rb index 1d11870bb4..98801ae633 100644 --- a/activemodel/lib/active_model/serializer.rb +++ b/activemodel/lib/active_model/serializer.rb @@ -46,6 +46,8 @@ module ActiveModel class_attribute :_associations self._associations = [] + class_attribute :_root + class << self def attributes(*attrs) self._attributes += attrs @@ -71,6 +73,10 @@ module ActiveModel associate(Associations::HasOne, attrs) end + def root(name) + self._root = name + end + def inherited(klass) return if klass.anonymous? @@ -78,6 +84,7 @@ module ActiveModel klass.class_eval do alias_method name.to_sym, :object + root name.to_sym unless self._root == false end end end @@ -89,7 +96,11 @@ module ActiveModel end def as_json(*) - serializable_hash + if _root + { _root => serializable_hash } + else + serializable_hash + end end def serializable_hash |