Ruby on Rails | Screencasts | Download | Documentation | Weblog | Community | Source

Changeset 9202

Show
Ignore:
Timestamp:
04/01/08 20:09:45 (8 months ago)
Author:
rick
Message:

Tweak ActiveRecord::Base#to_json to include a root value in the returned hash: {post: {title: ...}} [rick]

Files:

Legend:

Unmodified
Added
Removed
Modified
Copied
Moved
  • trunk/activerecord/CHANGELOG

    r9200 r9202  
    11*SVN* 
     2 
     3* Tweak ActiveRecord::Base#to_json to include a root value in the returned hash: {"post": {"title": ...}} [rick] 
     4 
     5  Post.find(1).to_json # => {"title": ...} 
     6  config.active_record.include_root_in_json = true 
     7  Post.find(1).to_json # => {"post": {"title": ...}} 
    28 
    39* Add efficient #include? to AssociationCollection (for has_many/has_many :through/habtm).  [stopdropandrew] 
  • trunk/activerecord/lib/active_record/serializers/json_serializer.rb

    r9093 r9202  
    11module ActiveRecord #:nodoc: 
    22  module Serialization 
     3    def self.included(base) 
     4      base.cattr_accessor :include_root_in_json, :instance_writer => false 
     5      base.extend ClassMethods 
     6    end 
     7 
    38    # Returns a JSON string representing the model. Some configuration is 
    49    # available through +options+. 
     
    4954    #                    "title": "So I was thinking"}]} 
    5055    def to_json(options = {}) 
    51       JsonSerializer.new(self, options).to_s 
     56      if include_root_in_json 
     57        "{#{self.class.json_class_name}: #{JsonSerializer.new(self, options).to_s}}" 
     58      else 
     59        JsonSerializer.new(self, options).to_s 
     60      end 
    5261    end 
    5362 
     
    6271      end 
    6372    end 
     73 
     74    module ClassMethods 
     75      def json_class_name 
     76        @json_class_name ||= name.demodulize.underscore.inspect 
     77      end 
     78    end 
    6479  end 
    6580end 
  • trunk/activerecord/test/cases/json_serialization_test.rb

    r8681 r9202  
    88 
    99class JsonSerializationTest < ActiveRecord::TestCase 
     10  class NamespacedContact < Contact 
     11    column :name,        :string 
     12  end 
     13 
    1014  def setup 
    1115    @contact = Contact.new( 
     
    1721      :preferences => { :shows => 'anime' } 
    1822    ) 
     23  end 
     24 
     25  def test_should_demodulize_root_in_json 
     26    NamespacedContact.include_root_in_json = true 
     27    @contact = NamespacedContact.new :name => 'whatever' 
     28    json = @contact.to_json 
     29    assert_match %r{^\{"namespaced_contact": \{}, json 
     30  end 
     31 
     32  def test_should_include_root_in_json 
     33    Contact.include_root_in_json = true 
     34    json = @contact.to_json 
     35 
     36    assert_match %r{^\{"contact": \{}, json 
     37    assert_match %r{"name": "Konata Izumi"}, json 
     38    assert_match %r{"age": 16}, json 
     39    assert json.include?(%("created_at": #{ActiveSupport::JSON.encode(Time.utc(2006, 8, 1))})) 
     40    assert_match %r{"awesome": true}, json 
     41    assert_match %r{"preferences": \{"shows": "anime"\}}, json 
     42  ensure 
     43    Contact.include_root_in_json = false 
    1944  end 
    2045