Skip to content
This repository
Browse code

Add support for the root attribute

  • Loading branch information...
commit a230f040ff61f069d46a9b86417a8e251016d5db 1 parent 7a28498
Yehuda Katz wycats authored
13 activemodel/lib/active_model/serializer.rb
@@ -46,6 +46,8 @@ def serialize_ids(object, scope)
46 46 class_attribute :_associations
47 47 self._associations = []
48 48
  49 + class_attribute :_root
  50 +
49 51 class << self
50 52 def attributes(*attrs)
51 53 self._attributes += attrs
@@ -71,6 +73,10 @@ def has_one(*attrs)
71 73 associate(Associations::HasOne, attrs)
72 74 end
73 75
  76 + def root(name)
  77 + self._root = name
  78 + end
  79 +
74 80 def inherited(klass)
75 81 return if klass.anonymous?
76 82
@@ -78,6 +84,7 @@ def inherited(klass)
78 84
79 85 klass.class_eval do
80 86 alias_method name.to_sym, :object
  87 + root name.to_sym unless self._root == false
81 88 end
82 89 end
83 90 end
@@ -89,7 +96,11 @@ def initialize(object, scope)
89 96 end
90 97
91 98 def as_json(*)
92   - serializable_hash
  99 + if _root
  100 + { _root => serializable_hash }
  101 + else
  102 + serializable_hash
  103 + end
93 104 end
94 105
95 106 def serializable_hash
76 activemodel/test/cases/serializer_test.rb
@@ -77,7 +77,9 @@ def test_attributes
77 77
78 78 hash = user_serializer.as_json
79 79
80   - assert_equal({ :first_name => "Jose", :last_name => "Valim" }, hash)
  80 + assert_equal({
  81 + :user => { :first_name => "Jose", :last_name => "Valim" }
  82 + }, hash)
81 83 end
82 84
83 85 def test_attributes_method
@@ -86,7 +88,9 @@ def test_attributes_method
86 88
87 89 hash = user_serializer.as_json
88 90
89   - assert_equal({ :first_name => "Jose", :last_name => "Valim", :ok => true }, hash)
  91 + assert_equal({
  92 + :user2 => { :first_name => "Jose", :last_name => "Valim", :ok => true }
  93 + }, hash)
90 94 end
91 95
92 96 def test_serializer_receives_scope
@@ -95,7 +99,14 @@ def test_serializer_receives_scope
95 99
96 100 hash = user_serializer.as_json
97 101
98   - assert_equal({ :first_name => "Jose", :last_name => "Valim", :ok => true, :scope => true }, hash)
  102 + assert_equal({
  103 + :user2 => {
  104 + :first_name => "Jose",
  105 + :last_name => "Valim",
  106 + :ok => true,
  107 + :scope => true
  108 + }
  109 + }, hash)
99 110 end
100 111
101 112 def test_pretty_accessors
@@ -105,7 +116,11 @@ def test_pretty_accessors
105 116
106 117 hash = user_serializer.as_json
107 118
108   - assert_equal({ :first_name => "Jose", :last_name => "Valim", :super_user => true }, hash)
  119 + assert_equal({
  120 + :my_user => {
  121 + :first_name => "Jose", :last_name => "Valim", :super_user => true
  122 + }
  123 + }, hash)
109 124 end
110 125
111 126 def test_has_many
@@ -118,12 +133,14 @@ def test_has_many
118 133 post_serializer = PostSerializer.new(post, user)
119 134
120 135 assert_equal({
121   - :title => "New Post",
122   - :body => "Body of new post",
123   - :comments => [
124   - { :title => "Comment1" },
125   - { :title => "Comment2" }
126   - ]
  136 + :post => {
  137 + :title => "New Post",
  138 + :body => "Body of new post",
  139 + :comments => [
  140 + { :title => "Comment1" },
  141 + { :title => "Comment2" }
  142 + ]
  143 + }
127 144 }, post_serializer.as_json)
128 145 end
129 146
@@ -146,9 +163,11 @@ def test_has_one
146 163
147 164 json = BlogSerializer.new(blog, user).as_json
148 165 assert_equal({
149   - :author => {
150   - :first_name => "Jose",
151   - :last_name => "Valim"
  166 + :blog => {
  167 + :author => {
  168 + :first_name => "Jose",
  169 + :last_name => "Valim"
  170 + }
152 171 }
153 172 }, json)
154 173 end
@@ -266,15 +285,44 @@ def test_associations_with_nil_association
266 285
267 286 json = BlogSerializer.new(blog, user).as_json
268 287 assert_equal({
269   - :author => nil
  288 + :blog => { :author => nil }
270 289 }, json)
271 290
272 291 serializer = Class.new(BlogSerializer) do
  292 + root :blog
  293 +
273 294 def serializable_hash
274 295 attributes.merge(association_ids)
275 296 end
276 297 end
277 298
  299 + json = serializer.new(blog, user).as_json
  300 + assert_equal({ :blog => { :author => nil } }, json)
  301 + end
  302 +
  303 + def test_custom_root
  304 + user = User.new
  305 + blog = Blog.new
  306 +
  307 + serializer = Class.new(BlogSerializer) do
  308 + root :my_blog
  309 + end
  310 +
  311 + assert_equal({ :my_blog => { :author => nil } }, serializer.new(blog, user).as_json)
  312 + end
  313 +
  314 + def test_false_root
  315 + user = User.new
  316 + blog = Blog.new
  317 +
  318 + serializer = Class.new(BlogSerializer) do
  319 + root false
  320 + end
  321 +
  322 + assert_equal({ :author => nil }, serializer.new(blog, user).as_json)
  323 +
  324 + # test inherited false root
  325 + serializer = Class.new(serializer)
278 326 assert_equal({ :author => nil }, serializer.new(blog, user).as_json)
279 327 end
280 328 end

0 comments on commit a230f04

Please sign in to comment.
Something went wrong with that request. Please try again.