From 137d8e0b2fe9fcc4fdac6cbbd44ca010784e5972 Mon Sep 17 00:00:00 2001 From: Santiago Pastorino Date: Mon, 19 Apr 2010 01:46:30 -0300 Subject: Make this test pass on > 1.9 --- activeresource/test/cases/base_test.rb | 6 +++++- 1 file changed, 5 insertions(+), 1 deletion(-) (limited to 'activeresource') diff --git a/activeresource/test/cases/base_test.rb b/activeresource/test/cases/base_test.rb index 31e0dc0abc..a424b2baf3 100644 --- a/activeresource/test/cases/base_test.rb +++ b/activeresource/test/cases/base_test.rb @@ -1022,7 +1022,11 @@ class BaseTest < Test::Unit::TestCase joe = Person.find(6) json = joe.encode Person.format = :xml - assert_equal json, '{"person":{"person":{"name":"Joe","id":6}}}' + + assert_match %r{^\{"person":\{"person":\{}, json + assert_match %r{"id":6}, json + assert_match %r{"name":"Joe"}, json + assert_match %r{\}\}\}$}, json end def test_to_param_quacks_like_active_record -- cgit v1.2.3 From 2a6e0f34ad6b48dcf41989e0d7555cda46492b34 Mon Sep 17 00:00:00 2001 From: Jeremy Kemper Date: Sun, 25 Apr 2010 21:02:35 -0700 Subject: Revert "create option to include_root_in_json for ActiveResource [#2584 state:committed]" This reverts commits 72f89b5d971b48a133c4c0af56fbeda35d738dae, 137d8e0b2fe9fcc4fdac6cbbd44ca010784e5972. Should reuse Active Model. [#2584 state:incomplete] --- activeresource/lib/active_resource/base.rb | 9 --------- activeresource/test/cases/base_test.rb | 29 ++++++----------------------- 2 files changed, 6 insertions(+), 32 deletions(-) (limited to 'activeresource') diff --git a/activeresource/lib/active_resource/base.rb b/activeresource/lib/active_resource/base.rb index 1dd5af8098..bf775a14c8 100644 --- a/activeresource/lib/active_resource/base.rb +++ b/activeresource/lib/active_resource/base.rb @@ -251,9 +251,6 @@ module ActiveResource # The logger for diagnosing and tracing Active Resource calls. cattr_accessor :logger - # Controls the top-level behavior of JSON serialization - cattr_accessor :include_root_in_json, :instance_writer => false - class << self # Creates a schema for this resource - setting the attributes that are # known prior to fetching an instance from the remote system. @@ -1243,12 +1240,6 @@ module ActiveResource case self.class.format when ActiveResource::Formats::XmlFormat self.class.format.encode(attributes, {:root => self.class.element_name}.merge(options)) - when ActiveResource::Formats::JsonFormat - if ActiveResource::Base.include_root_in_json - self.class.format.encode({self.class.element_name => attributes}, options) - else - self.class.format.encode(attributes, options) - end else self.class.format.encode(attributes, options) end diff --git a/activeresource/test/cases/base_test.rb b/activeresource/test/cases/base_test.rb index a424b2baf3..0f10a0e4d7 100644 --- a/activeresource/test/cases/base_test.rb +++ b/activeresource/test/cases/base_test.rb @@ -4,22 +4,20 @@ require "fixtures/customer" require "fixtures/street_address" require "fixtures/beast" require "fixtures/proxy" -require 'active_support/json' require 'active_support/core_ext/hash/conversions' require 'mocha' class BaseTest < Test::Unit::TestCase def setup + @matz = { :id => 1, :name => 'Matz' }.to_xml(:root => 'person') + @david = { :id => 2, :name => 'David' }.to_xml(:root => 'person') + @greg = { :id => 3, :name => 'Greg' }.to_xml(:root => 'person') + @addy = { :id => 1, :street => '12345 Street', :country => 'Australia' }.to_xml(:root => 'address') @default_request_headers = { 'Content-Type' => 'application/xml' } - @matz = { :id => 1, :name => 'Matz' }.to_xml(:root => 'person') - @david = { :id => 2, :name => 'David' }.to_xml(:root => 'person') - @greg = { :id => 3, :name => 'Greg' }.to_xml(:root => 'person') - @addy = { :id => 1, :street => '12345 Street', :country => 'Australia' }.to_xml(:root => 'address') - @rick = { :name => "Rick", :age => 25 }.to_xml(:root => "person") - @joe = { 'person' => { :id => 6, :name => 'Joe' }}.to_json + @rick = { :name => "Rick", :age => 25 }.to_xml(:root => "person") @people = [{ :id => 1, :name => 'Matz' }, { :id => 2, :name => 'David' }].to_xml(:root => 'people') @people_david = [{ :id => 2, :name => 'David' }].to_xml(:root => 'people') - @addresses = [{ :id => 1, :street => '12345 Street', :country => 'Australia' }].to_xml(:root => 'addresses') + @addresses = [{ :id => 1, :street => '12345 Street', :country => 'Australia' }].to_xml(:root => 'addresses') # - deep nested resource - # - Luis (Customer) @@ -68,7 +66,6 @@ class BaseTest < Test::Unit::TestCase ActiveResource::HttpMock.respond_to do |mock| mock.get "/people/1.xml", {}, @matz mock.get "/people/2.xml", {}, @david - mock.get "/people/6.json", {}, @joe mock.get "/people/5.xml", {}, @marty mock.get "/people/Greg.xml", {}, @greg mock.get "/people/4.xml", {'key' => 'value'}, nil, 404 @@ -1015,20 +1012,6 @@ class BaseTest < Test::Unit::TestCase assert xml.include?('1') end - - def test_to_json_including_root - Person.include_root_in_json = true - Person.format = :json - joe = Person.find(6) - json = joe.encode - Person.format = :xml - - assert_match %r{^\{"person":\{"person":\{}, json - assert_match %r{"id":6}, json - assert_match %r{"name":"Joe"}, json - assert_match %r{\}\}\}$}, json - end - def test_to_param_quacks_like_active_record new_person = Person.new assert_nil new_person.to_param -- cgit v1.2.3 From 7cd1d37a51f5f53f8fc1360f886d26cabf12d969 Mon Sep 17 00:00:00 2001 From: Santiago Pastorino Date: Mon, 26 Apr 2010 18:27:16 -0300 Subject: Reuse Active Model serialization in Active Resource. [#2584 state:committed] Signed-off-by: Jeremy Kemper --- activeresource/lib/active_resource/base.rb | 66 ++---------------------------- activeresource/test/cases/base_test.rb | 28 ++++++++++--- 2 files changed, 25 insertions(+), 69 deletions(-) (limited to 'activeresource') diff --git a/activeresource/lib/active_resource/base.rb b/activeresource/lib/active_resource/base.rb index bf775a14c8..ad994214f6 100644 --- a/activeresource/lib/active_resource/base.rb +++ b/activeresource/lib/active_resource/base.rb @@ -1176,73 +1176,11 @@ module ActiveResource !new? && self.class.exists?(to_param, :params => prefix_options) end - # Converts the resource to an XML string representation. - # - # ==== Options - # The +options+ parameter is handed off to the +to_xml+ method on each - # attribute, so it has the same options as the +to_xml+ methods in - # Active Support. - # - # * :indent - Set the indent level for the XML output (default is +2+). - # * :dasherize - Boolean option to determine whether or not element names should - # replace underscores with dashes (default is false). - # * :skip_instruct - Toggle skipping the +instruct!+ call on the XML builder - # that generates the XML declaration (default is false). - # - # ==== Examples - # my_group = SubsidiaryGroup.find(:first) - # my_group.to_xml - # # => - # # [...] - # - # my_group.to_xml(:dasherize => true) - # # => - # # [...] - # - # my_group.to_xml(:skip_instruct => true) - # # => [...] - def to_xml(options={}) - attributes.to_xml({:root => self.class.element_name}.merge(options)) - end - - # Coerces to a hash for JSON encoding. - # - # ==== Options - # The +options+ are passed to the +to_json+ method on each - # attribute, so the same options as the +to_json+ methods in - # Active Support. - # - # * :only - Only include the specified attribute or list of - # attributes in the serialized output. Attribute names must be specified - # as strings. - # * :except - Do not include the specified attribute or list of - # attributes in the serialized output. Attribute names must be specified - # as strings. - # - # ==== Examples - # person = Person.new(:first_name => "Jim", :last_name => "Smith") - # person.to_json - # # => {"first_name": "Jim", "last_name": "Smith"} - # - # person.to_json(:only => ["first_name"]) - # # => {"first_name": "Jim"} - # - # person.to_json(:except => ["first_name"]) - # # => {"last_name": "Smith"} - def as_json(options = nil) - attributes.as_json(options) - end - # Returns the serialized string representation of the resource in the configured # serialization format specified in ActiveResource::Base.format. The options # applicable depend on the configured encoding format. def encode(options={}) - case self.class.format - when ActiveResource::Formats::XmlFormat - self.class.format.encode(attributes, {:root => self.class.element_name}.merge(options)) - else - self.class.format.encode(attributes, options) - end + send("to_#{self.class.format.extension}", options) end # A method to \reload the attributes of this object from the remote web service. @@ -1466,5 +1404,7 @@ module ActiveResource class Base extend ActiveModel::Naming include CustomMethods, Observing, Validations + include ActiveModel::Serializers::JSON + include ActiveModel::Serializers::Xml end end diff --git a/activeresource/test/cases/base_test.rb b/activeresource/test/cases/base_test.rb index 0f10a0e4d7..2ed7e1c95f 100644 --- a/activeresource/test/cases/base_test.rb +++ b/activeresource/test/cases/base_test.rb @@ -4,20 +4,22 @@ require "fixtures/customer" require "fixtures/street_address" require "fixtures/beast" require "fixtures/proxy" +require 'active_support/json' require 'active_support/core_ext/hash/conversions' require 'mocha' class BaseTest < Test::Unit::TestCase def setup - @matz = { :id => 1, :name => 'Matz' }.to_xml(:root => 'person') - @david = { :id => 2, :name => 'David' }.to_xml(:root => 'person') - @greg = { :id => 3, :name => 'Greg' }.to_xml(:root => 'person') - @addy = { :id => 1, :street => '12345 Street', :country => 'Australia' }.to_xml(:root => 'address') @default_request_headers = { 'Content-Type' => 'application/xml' } - @rick = { :name => "Rick", :age => 25 }.to_xml(:root => "person") + @matz = { :id => 1, :name => 'Matz' }.to_xml(:root => 'person') + @david = { :id => 2, :name => 'David' }.to_xml(:root => 'person') + @greg = { :id => 3, :name => 'Greg' }.to_xml(:root => 'person') + @addy = { :id => 1, :street => '12345 Street', :country => 'Australia' }.to_xml(:root => 'address') + @rick = { :name => "Rick", :age => 25 }.to_xml(:root => "person") + @joe = { 'person' => { :id => 6, :name => 'Joe' }}.to_json @people = [{ :id => 1, :name => 'Matz' }, { :id => 2, :name => 'David' }].to_xml(:root => 'people') @people_david = [{ :id => 2, :name => 'David' }].to_xml(:root => 'people') - @addresses = [{ :id => 1, :street => '12345 Street', :country => 'Australia' }].to_xml(:root => 'addresses') + @addresses = [{ :id => 1, :street => '12345 Street', :country => 'Australia' }].to_xml(:root => 'addresses') # - deep nested resource - # - Luis (Customer) @@ -66,6 +68,7 @@ class BaseTest < Test::Unit::TestCase ActiveResource::HttpMock.respond_to do |mock| mock.get "/people/1.xml", {}, @matz mock.get "/people/2.xml", {}, @david + mock.get "/people/6.json", {}, @joe mock.get "/people/5.xml", {}, @marty mock.get "/people/Greg.xml", {}, @greg mock.get "/people/4.xml", {'key' => 'value'}, nil, 404 @@ -1012,6 +1015,19 @@ class BaseTest < Test::Unit::TestCase assert xml.include?('1') end + def test_to_json + Person.include_root_in_json = true + Person.format = :json + joe = Person.find(6) + json = joe.encode + Person.format = :xml + + assert_match %r{^\{"person":\{"person":\{}, json + assert_match %r{"id":6}, json + assert_match %r{"name":"Joe"}, json + assert_match %r{\}\}\}$}, json + end + def test_to_param_quacks_like_active_record new_person = Person.new assert_nil new_person.to_param -- cgit v1.2.3 From 43e2fd93b4fa92ca23d8bc8e68e1bf5a94038461 Mon Sep 17 00:00:00 2001 From: Jeremy Kemper Date: Mon, 26 Apr 2010 15:21:04 -0700 Subject: Update CHANGELOG for include_root_in_json. --- activeresource/CHANGELOG | 5 +++++ 1 file changed, 5 insertions(+) (limited to 'activeresource') diff --git a/activeresource/CHANGELOG b/activeresource/CHANGELOG index 91dccb9671..bd97b2d549 100644 --- a/activeresource/CHANGELOG +++ b/activeresource/CHANGELOG @@ -1,3 +1,8 @@ +*Rails 3.0.0 [beta 4/release candidate] (unreleased)* + +* JSON: set Base.include_root_in_json = true to include a root value in the JSON: {"post": {"title": ...}}. Mirrors the Active Record option. [Santiago Pastorino] + + *Rails 3.0.0 [beta 3] (April 13th, 2010)* * No changes -- cgit v1.2.3