From 8214c4bf23a3f95b2172d942196ade7d48186905 Mon Sep 17 00:00:00 2001 From: Neeraj Singh Date: Fri, 16 Jul 2010 15:19:18 -0400 Subject: [PATCH] included associated records while building to_json should respect as_json definition of the associated records [#3087 state:resolved] --- activerecord/lib/active_record/serialization.rb | 9 +++++++-- activerecord/test/cases/json_serialization_test.rb | 12 +++++++++++- activerecord/test/models/engine.rb | 5 +++++ activerecord/test/schema/schema.rb | 1 + 4 files changed, 24 insertions(+), 3 deletions(-) diff --git a/activerecord/lib/active_record/serialization.rb b/activerecord/lib/active_record/serialization.rb index 2d8bd18..f1f01cf 100644 --- a/activerecord/lib/active_record/serialization.rb +++ b/activerecord/lib/active_record/serialization.rb @@ -14,14 +14,19 @@ module ActiveRecord #:nodoc: serializable_add_includes(options) do |association, records, opts| hash[association] = records.is_a?(Enumerable) ? - records.map { |r| r.serializable_hash(opts) } : - records.serializable_hash(opts) + records.map { |r| r.serializable_hash(build_options(r, opts)) } : + records.serializable_hash(build_options(records, opts)) end hash end private + + def build_options(record, opts) + (output = record.as_json).is_a?(Hash) ? opts.merge(output) : opts + end + # Add associations specified via the :includes option. # # Expects a block that takes as arguments: diff --git a/activerecord/test/cases/json_serialization_test.rb b/activerecord/test/cases/json_serialization_test.rb index 2bc746c..02ab64e 100644 --- a/activerecord/test/cases/json_serialization_test.rb +++ b/activerecord/test/cases/json_serialization_test.rb @@ -5,6 +5,8 @@ require 'models/author' require 'models/tagging' require 'models/tag' require 'models/comment' +require 'models/car' +require 'models/engine' class JsonSerializationTest < ActiveRecord::TestCase class NamespacedContact < Contact @@ -85,7 +87,7 @@ class JsonSerializationTest < ActiveRecord::TestCase end class DatabaseConnectedJsonEncodingTest < ActiveRecord::TestCase - fixtures :authors, :posts, :comments, :tags, :taggings + fixtures :authors, :posts, :comments, :tags, :taggings, :cars def setup @david = authors(:david) @@ -208,4 +210,12 @@ class DatabaseConnectedJsonEncodingTest < ActiveRecord::TestCase json = ActiveSupport::JSON.encode authors_relation, :only => :name assert_equal '[{"author":{"name":"David"}},{"author":{"name":"Mary"}}]', json end + + def test_association_object_should_obey_as_json + car = Car.find(1) + car.engines.create(:size => 'medium') + expected = %({"car":{"engines":[{"size":"medium"}],"name":"honda","wheels_count":null,"id":1,"engines_count":0}}) + assert_equal expected, car.to_json(:include => :engines) + end + end diff --git a/activerecord/test/models/engine.rb b/activerecord/test/models/engine.rb index 751c3f0..286e85e 100644 --- a/activerecord/test/models/engine.rb +++ b/activerecord/test/models/engine.rb @@ -1,3 +1,8 @@ class Engine < ActiveRecord::Base belongs_to :my_car, :class_name => 'Car', :foreign_key => 'car_id', :counter_cache => :engines_count + + def as_json + {:only => [:size]} + end + end diff --git a/activerecord/test/schema/schema.rb b/activerecord/test/schema/schema.rb index 641726b..4304d78 100644 --- a/activerecord/test/schema/schema.rb +++ b/activerecord/test/schema/schema.rb @@ -192,6 +192,7 @@ ActiveRecord::Schema.define do create_table :engines, :force => true do |t| t.integer :car_id + t.string :size end create_table :entrants, :force => true do |t| -- 1.6.5.2