Skip to content

Commit

Permalink
Subclasses support
Browse files Browse the repository at this point in the history
  • Loading branch information
shamne committed Aug 29, 2013
1 parent db66c2e commit bd2fe73
Show file tree
Hide file tree
Showing 4 changed files with 20 additions and 2 deletions.
7 changes: 5 additions & 2 deletions lib/active_resource/base.rb
Expand Up @@ -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)
Expand Down
1 change: 1 addition & 0 deletions test/fixtures/product.rb
@@ -1,3 +1,4 @@
class Product < ActiveResource::Base
self.site = 'http://37s.sunrise.i:3000'
SUBCLASSES = ["SubProduct"]
end
2 changes: 2 additions & 0 deletions test/fixtures/sub_product.rb
@@ -0,0 +1,2 @@
class SubProduct < Product
end
12 changes: 12 additions & 0 deletions 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
Expand Down Expand Up @@ -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)
Expand Down

0 comments on commit bd2fe73

Please sign in to comment.