Permalink
Browse files

Refactor AMo as_json.

  • Loading branch information...
josevalim committed Sep 22, 2011
1 parent c19c555 commit 4bfbdc133a37c6046b8854d3659ba75597d1d37e
Showing with 5 additions and 9 deletions.
  1. +5 −9 activemodel/lib/active_model/serializers/json.rb
@@ -86,16 +86,12 @@ module JSON
# "title": "Welcome to the weblog"},
# {"comments": [{"body": "Don't think too hard"}],
# "title": "So I was thinking"}]}
-
def as_json(options = nil)
- opts_root = options[:root] if options.try(:key?, :root)
- if opts_root
- custom_root = opts_root == true ? self.class.model_name.element : opts_root
- { custom_root => serializable_hash(options) }
- elsif opts_root == false
- serializable_hash(options)
- elsif include_root_in_json
- { self.class.model_name.element => serializable_hash(options) }
+ root = include_root_in_json
+ root = options[:root] if options.try(:key?, :root)
+ if root
+ root = self.class.model_name.element if root == true
@vijaydev

vijaydev Sep 23, 2011

Member

Why not move this check to the line above?

@josevalim

josevalim Sep 23, 2011

Contributor

Why not leave it here? It makes no difference.

@vijaydev

vijaydev Sep 23, 2011

Member

I meant to replace the if root with if root == true. Save one if? :)

@josevalim

josevalim Sep 23, 2011

Contributor

But then I would need to add a elsif root for the cases where it is not true, but a string/symbol. Which at the end would be the same, c/d?

@vijaydev

vijaydev Sep 23, 2011

Member

You're right. I missed that..

+ { root => serializable_hash(options) }
else
serializable_hash(options)
end

0 comments on commit 4bfbdc1

Please sign in to comment.