diff options
author | Pratik Naik <pratiknaik@gmail.com> | 2008-03-26 12:27:52 +0000 |
---|---|---|
committer | Pratik Naik <pratiknaik@gmail.com> | 2008-03-26 12:27:52 +0000 |
commit | ca9413674ea70dc67ab517734af2e40dac21beef (patch) | |
tree | 86cbf305a2b1b4688a5b6f7cfbce8a9aa505c5f7 /activerecord/lib/active_record/serializers | |
parent | 5c47ceb30b940a8cd8eb681a898895bce46f79dd (diff) | |
download | rails-ca9413674ea70dc67ab517734af2e40dac21beef.tar.gz rails-ca9413674ea70dc67ab517734af2e40dac21beef.tar.bz2 rails-ca9413674ea70dc67ab517734af2e40dac21beef.zip |
Improve documentation.
git-svn-id: http://svn-commit.rubyonrails.org/rails/trunk@9093 5ecf4fe2-1ee6-0310-87b1-e25e094e27de
Diffstat (limited to 'activerecord/lib/active_record/serializers')
-rw-r--r-- | activerecord/lib/active_record/serializers/json_serializer.rb | 40 | ||||
-rw-r--r-- | activerecord/lib/active_record/serializers/xml_serializer.rb | 57 |
2 files changed, 57 insertions, 40 deletions
diff --git a/activerecord/lib/active_record/serializers/json_serializer.rb b/activerecord/lib/active_record/serializers/json_serializer.rb index cf44309de7..c0a5850d5d 100644 --- a/activerecord/lib/active_record/serializers/json_serializer.rb +++ b/activerecord/lib/active_record/serializers/json_serializer.rb @@ -8,37 +8,32 @@ module ActiveRecord #:nodoc: # # konata = User.find(1) # konata.to_json - # - # {"id": 1, "name": "Konata Izumi", "age": 16, - # "created_at": "2006/08/01", "awesome": true} + # # => {"id": 1, "name": "Konata Izumi", "age": 16, + # "created_at": "2006/08/01", "awesome": true} # # The :only and :except options can be used to limit the attributes # included, and work similar to the #attributes method. For example: # # konata.to_json(:only => [ :id, :name ]) - # - # {"id": 1, "name": "Konata Izumi"} + # # => {"id": 1, "name": "Konata Izumi"} # # konata.to_json(:except => [ :id, :created_at, :age ]) - # - # {"name": "Konata Izumi", "awesome": true} + # # => {"name": "Konata Izumi", "awesome": true} # # To include any methods on the model, use :methods. # # konata.to_json(:methods => :permalink) - # - # {"id": 1, "name": "Konata Izumi", "age": 16, - # "created_at": "2006/08/01", "awesome": true, - # "permalink": "1-konata-izumi"} + # # => {"id": 1, "name": "Konata Izumi", "age": 16, + # "created_at": "2006/08/01", "awesome": true, + # "permalink": "1-konata-izumi"} # # To include associations, use :include. # # konata.to_json(:include => :posts) - # - # {"id": 1, "name": "Konata Izumi", "age": 16, - # "created_at": "2006/08/01", "awesome": true, - # "posts": [{"id": 1, "author_id": 1, "title": "Welcome to the weblog"}, - # {"id": 2, author_id: 1, "title": "So I was thinking"}]} + # # => {"id": 1, "name": "Konata Izumi", "age": 16, + # "created_at": "2006/08/01", "awesome": true, + # "posts": [{"id": 1, "author_id": 1, "title": "Welcome to the weblog"}, + # {"id": 2, author_id: 1, "title": "So I was thinking"}]} # # 2nd level and higher order associations work as well: # @@ -46,13 +41,12 @@ module ActiveRecord #:nodoc: # :include => { :comments => { # :only => :body } }, # :only => :title } }) - # - # {"id": 1, "name": "Konata Izumi", "age": 16, - # "created_at": "2006/08/01", "awesome": true, - # "posts": [{"comments": [{"body": "1st post!"}, {"body": "Second!"}], - # "title": "Welcome to the weblog"}, - # {"comments": [{"body": "Don't think too hard"}], - # "title": "So I was thinking"}]} + # # => {"id": 1, "name": "Konata Izumi", "age": 16, + # "created_at": "2006/08/01", "awesome": true, + # "posts": [{"comments": [{"body": "1st post!"}, {"body": "Second!"}], + # "title": "Welcome to the weblog"}, + # {"comments": [{"body": "Don't think too hard"}], + # "title": "So I was thinking"}]} def to_json(options = {}) JsonSerializer.new(self, options).to_s end diff --git a/activerecord/lib/active_record/serializers/xml_serializer.rb b/activerecord/lib/active_record/serializers/xml_serializer.rb index 84b1a53470..fbd15e06dc 100644 --- a/activerecord/lib/active_record/serializers/xml_serializer.rb +++ b/activerecord/lib/active_record/serializers/xml_serializer.rb @@ -1,11 +1,11 @@ module ActiveRecord #:nodoc: module Serialization - # Builds an XML document to represent the model. Some configuration is - # available through +options+, however more complicated cases should - # override ActiveRecord's to_xml. + # Builds an XML document to represent the model. Some configuration is + # available through +options+. However more complicated cases should + # override ActiveRecord's to_xml method. # # By default the generated XML document will include the processing - # instruction and all object's attributes. For example: + # instruction and all the object's attributes. For example: # # <?xml version="1.0" encoding="UTF-8"?> # <topic> @@ -23,10 +23,10 @@ module ActiveRecord #:nodoc: # </topic> # # This behavior can be controlled with :only, :except, - # :skip_instruct, :skip_types and :dasherize. The :only and + # :skip_instruct, :skip_types and :dasherize. The :only and # :except options are the same as for the #attributes method. # The default is to dasherize all column names, to disable this, - # set :dasherize to false. To not have the column type included + # set :dasherize to false. To not have the column type included # in the XML output, set :skip_types to true. # # For instance: @@ -68,6 +68,36 @@ module ActiveRecord #:nodoc: # </account> # </firm> # + # To include deeper levels of associations pass a hash like this: + # + # firm.to_xml :include => {:account => {}, :clients => {:include => :address}} + # <?xml version="1.0" encoding="UTF-8"?> + # <firm> + # <id type="integer">1</id> + # <rating type="integer">1</rating> + # <name>37signals</name> + # <clients type="array"> + # <client> + # <rating type="integer">1</rating> + # <name>Summit</name> + # <address> + # ... + # </address> + # </client> + # <client> + # <rating type="integer">1</rating> + # <name>Microsoft</name> + # <address> + # ... + # </address> + # </client> + # </clients> + # <account> + # <id type="integer">1</id> + # <credit-limit type="integer">50</credit-limit> + # </account> + # </firm> + # # To include any methods on the object(s) being called use :methods # # firm.to_xml :methods => [ :calculated_earnings, :real_earnings ] @@ -78,7 +108,7 @@ module ActiveRecord #:nodoc: # <real-earnings>5</real-earnings> # </firm> # - # To call any Proc's on the object(s) use :procs. The Proc's + # To call any Procs on the object(s) use :procs. The Procs # are passed a modified version of the options hash that was # given to #to_xml. # @@ -90,7 +120,7 @@ module ActiveRecord #:nodoc: # <abc>def</abc> # </firm> # - # Alternatively, you can also just yield the builder object as part of the to_xml call: + # Alternatively, you can yield the builder object as part of the to_xml call: # # firm.to_xml do |xml| # xml.creator do @@ -108,7 +138,7 @@ module ActiveRecord #:nodoc: # </firm> # # You can override the to_xml method in your ActiveRecord::Base - # subclasses if you need to. The general form of doing this is + # subclasses if you need to. The general form of doing this is: # # class IHaveMyOwnXML < ActiveRecord::Base # def to_xml(options = {}) @@ -155,13 +185,6 @@ module ActiveRecord #:nodoc: !options.has_key?(:dasherize) || options[:dasherize] end - - # To replicate the behavior in ActiveRecord#attributes, - # :except takes precedence over :only. If :only is not set - # for a N level model but is set for the N+1 level models, - # then because :except is set to a default value, the second - # level model can have both :except and :only set. So if - # :only is set, always delete :except. def serializable_attributes serializable_attribute_names.collect { |name| Attribute.new(name, @record) } end @@ -251,7 +274,7 @@ module ActiveRecord #:nodoc: # There is a significant speed improvement if the value # does not need to be escaped, as #tag! escapes all values - # to ensure that valid XML is generated. For known binary + # to ensure that valid XML is generated. For known binary # values, it is at least an order of magnitude faster to # Base64 encode binary values and directly put them in the # output XML than to pass the original value or the Base64 |