diff --git a/lib/active_resource/base.rb b/lib/active_resource/base.rb index f9a66bd7f9..1189971966 100644 --- a/lib/active_resource/base.rb +++ b/lib/active_resource/base.rb @@ -998,12 +998,15 @@ def instantiate_collection(collection, original_params = {}, prefix_options = {} end def instantiate_record(record, prefix_options = {}) - new(record, true).tap do |resource| + instance_klass = self + if defined?(self::SUBCLASSES) && self::SUBCLASSES.include?(record["type"]) + instance_klass = record["type"].constantize + end + instance_klass.new(record, true).tap do |resource| resource.prefix_options = prefix_options end end - # Accepts a URI and creates the site URI from that. def create_site_uri_from(site) site.is_a?(URI) ? site.dup : URI.parse(site) diff --git a/test/fixtures/product.rb b/test/fixtures/product.rb index 6784cfb52a..694ca390f1 100644 --- a/test/fixtures/product.rb +++ b/test/fixtures/product.rb @@ -1,3 +1,4 @@ class Product < ActiveResource::Base self.site = 'http://37s.sunrise.i:3000' + SUBCLASSES = ["SubProduct"] end diff --git a/test/fixtures/sub_product.rb b/test/fixtures/sub_product.rb new file mode 100644 index 0000000000..b3a9388550 --- /dev/null +++ b/test/fixtures/sub_product.rb @@ -0,0 +1,2 @@ +class SubProduct < Product +end \ No newline at end of file diff --git a/test/singleton_test.rb b/test/singleton_test.rb index bfad3510ca..d0cb4a509b 100644 --- a/test/singleton_test.rb +++ b/test/singleton_test.rb @@ -1,6 +1,8 @@ require 'abstract_unit' require 'fixtures/weather' require 'fixtures/inventory' +require 'fixtures/product' +require 'fixtures/sub_product' class SingletonTest < ActiveSupport::TestCase def setup_weather @@ -59,6 +61,16 @@ def test_singleton_path_with_parameters assert_equal '/products/5/inventory.json?sold=true', path end + def test_subclass_instantiation + product = { :id => 1, :type => "SubProduct" } + ActiveResource::HttpMock.respond_to do |mock| + mock.get '/products/1.json', {}, product.to_json + end + sub_product = Product.find 1 + assert_not_nil sub_product + assert_equal 'SubProduct', sub_product.class.name + end + def test_find_singleton setup_weather weather = Weather.send(:find_singleton, Hash.new)