1
require 'active_support/json'
2
require 'active_support/core_ext/module/model_naming'
4
module ActiveRecord #:nodoc:
6
def self.included(base)
7
base.cattr_accessor :include_root_in_json, :instance_writer => false
10
# Returns a JSON string representing the model. Some configuration is
11
# available through +options+.
13
# The option <tt>ActiveRecord::Base.include_root_in_json</tt> controls the
14
# top-level behavior of to_json. In a new Rails application, it is set to
15
# <tt>true</tt> in initializers/new_rails_defaults.rb. When it is <tt>true</tt>,
16
# to_json will emit a single root node named after the object's type. For example:
18
# konata = User.find(1)
19
# ActiveRecord::Base.include_root_in_json = true
21
# # => { "user": {"id": 1, "name": "Konata Izumi", "age": 16,
22
# "created_at": "2006/08/01", "awesome": true} }
24
# ActiveRecord::Base.include_root_in_json = false
26
# # => {"id": 1, "name": "Konata Izumi", "age": 16,
27
# "created_at": "2006/08/01", "awesome": true}
29
# The remainder of the examples in this section assume include_root_in_json is set to
32
# Without any +options+, the returned JSON string will include all
33
# the model's attributes. For example:
35
# konata = User.find(1)
37
# # => {"id": 1, "name": "Konata Izumi", "age": 16,
38
# "created_at": "2006/08/01", "awesome": true}
40
# The <tt>:only</tt> and <tt>:except</tt> options can be used to limit the attributes
41
# included, and work similar to the +attributes+ method. For example:
43
# konata.to_json(:only => [ :id, :name ])
44
# # => {"id": 1, "name": "Konata Izumi"}
46
# konata.to_json(:except => [ :id, :created_at, :age ])
47
# # => {"name": "Konata Izumi", "awesome": true}
49
# To include any methods on the model, use <tt>:methods</tt>.
51
# konata.to_json(:methods => :permalink)
52
# # => {"id": 1, "name": "Konata Izumi", "age": 16,
53
# "created_at": "2006/08/01", "awesome": true,
54
# "permalink": "1-konata-izumi"}
56
# To include associations, use <tt>:include</tt>.
58
# konata.to_json(:include => :posts)
59
# # => {"id": 1, "name": "Konata Izumi", "age": 16,
60
# "created_at": "2006/08/01", "awesome": true,
61
# "posts": [{"id": 1, "author_id": 1, "title": "Welcome to the weblog"},
62
# {"id": 2, author_id: 1, "title": "So I was thinking"}]}
64
# 2nd level and higher order associations work as well:
66
# konata.to_json(:include => { :posts => {
67
# :include => { :comments => {
69
# :only => :title } })
70
# # => {"id": 1, "name": "Konata Izumi", "age": 16,
71
# "created_at": "2006/08/01", "awesome": true,
72
# "posts": [{"comments": [{"body": "1st post!"}, {"body": "Second!"}],
73
# "title": "Welcome to the weblog"},
74
# {"comments": [{"body": "Don't think too hard"}],
75
# "title": "So I was thinking"}]}
76
def to_json(options = {})
80
def as_json(options = nil) #:nodoc:
81
hash = Serializer.new(self, options).serializable_record
82
hash = { self.class.model_name.element => hash } if include_root_in_json
87
self.attributes = ActiveSupport::JSON.decode(json)