~michaelforrest/use-case-mapper/trunk

« back to all changes in this revision

Viewing changes to vendor/rails/activerecord/lib/active_record/serializers/json_serializer.rb

  • Committer: Richard Lee (Canonical)
  • Date: 2010-10-15 15:17:58 UTC
  • mfrom: (190.1.3 use-case-mapper)
  • Revision ID: richard.lee@canonical.com-20101015151758-wcvmfxrexsongf9d
Merge

Show diffs side-by-side

added added

removed removed

Lines of Context:
1
 
require 'active_support/json'
2
 
require 'active_support/core_ext/module/model_naming'
3
 
 
4
 
module ActiveRecord #:nodoc:
5
 
  module Serialization
6
 
    def self.included(base)
7
 
      base.cattr_accessor :include_root_in_json, :instance_writer => false
8
 
    end
9
 
 
10
 
    # Returns a JSON string representing the model. Some configuration is
11
 
    # available through +options+.
12
 
    #
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:
17
 
    #
18
 
    #   konata = User.find(1)
19
 
    #   ActiveRecord::Base.include_root_in_json = true
20
 
    #   konata.to_json
21
 
    #   # => { "user": {"id": 1, "name": "Konata Izumi", "age": 16,
22
 
    #                   "created_at": "2006/08/01", "awesome": true} }
23
 
    #
24
 
    #   ActiveRecord::Base.include_root_in_json = false
25
 
    #   konata.to_json
26
 
    #   # => {"id": 1, "name": "Konata Izumi", "age": 16,
27
 
    #         "created_at": "2006/08/01", "awesome": true}
28
 
    #
29
 
    # The remainder of the examples in this section assume include_root_in_json is set to
30
 
    # <tt>false</tt>.
31
 
    #
32
 
    # Without any +options+, the returned JSON string will include all
33
 
    # the model's attributes. For example:
34
 
    #
35
 
    #   konata = User.find(1)
36
 
    #   konata.to_json
37
 
    #   # => {"id": 1, "name": "Konata Izumi", "age": 16,
38
 
    #         "created_at": "2006/08/01", "awesome": true}
39
 
    #
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:
42
 
    #
43
 
    #   konata.to_json(:only => [ :id, :name ])
44
 
    #   # => {"id": 1, "name": "Konata Izumi"}
45
 
    #
46
 
    #   konata.to_json(:except => [ :id, :created_at, :age ])
47
 
    #   # => {"name": "Konata Izumi", "awesome": true}
48
 
    #
49
 
    # To include any methods on the model, use <tt>:methods</tt>.
50
 
    #
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"}
55
 
    #
56
 
    # To include associations, use <tt>:include</tt>.
57
 
    #
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"}]}
63
 
    #
64
 
    # 2nd level and higher order associations work as well:
65
 
    #
66
 
    #   konata.to_json(:include => { :posts => {
67
 
    #                                  :include => { :comments => {
68
 
    #                                                :only => :body } },
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 = {})
77
 
      super
78
 
    end
79
 
 
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
83
 
      hash
84
 
    end
85
 
 
86
 
    def from_json(json)
87
 
      self.attributes = ActiveSupport::JSON.decode(json)
88
 
      self
89
 
    end
90
 
  end
91
 
end