Permalink
Browse files

Ensure ActiveResource#load works with numeric arrays [Grzegorz Forysi…

…nski, Elad Meidar]

[#2305 state:resolved]

Signed-off-by: Pratik Naik <pratiknaik@gmail.com>
  • Loading branch information...
1 parent be017fd commit 59c3b0d0deb8ddd83f20c8a66fde188aed74c51c @GrzesF GrzesF committed with lifo Aug 9, 2009
Showing with 23 additions and 2 deletions.
  1. +7 −1 activeresource/lib/active_resource/base.rb
  2. +16 −1 activeresource/test/base/load_test.rb
View
8 activeresource/lib/active_resource/base.rb
@@ -1016,7 +1016,13 @@ def load(attributes)
case value
when Array
resource = find_or_create_resource_for_collection(key)
- value.map { |attrs| attrs.is_a?(String) ? attrs.dup : resource.new(attrs) }
+ value.map do |attrs|
+ if attrs.is_a?(String) || attrs.is_a?(Numeric)
+ attrs.duplicable? ? attrs.dup : attrs
+ else
+ resource.new(attrs)
+ end
+ end
when Hash
resource = find_or_create_resource_for(key)
resource.new(value)
View
17 activeresource/test/base/load_test.rb
@@ -49,7 +49,9 @@ def setup
:id => 1, :state => { :id => 1, :name => 'Oregon',
:notable_rivers => [
{ :id => 1, :name => 'Willamette' },
- { :id => 2, :name => 'Columbia', :rafted_by => @matz }] }}}
+ { :id => 2, :name => 'Columbia', :rafted_by => @matz }],
+ :postal_codes => [97018,1234567890],
+ :places => ["Columbia City", "Unknown"]}}}
@person = Person.new
end
@@ -125,6 +127,19 @@ def test_recursively_loaded_collections
assert_kind_of Person::Street::State::NotableRiver, rivers.first
assert_equal @deep[:street][:state][:notable_rivers].first[:id], rivers.first.id
assert_equal @matz[:id], rivers.last.rafted_by.id
+
+ postal_codes = state.postal_codes
+ assert_kind_of Array, postal_codes
+ assert_equal 2, postal_codes.size
+ assert_kind_of Fixnum, postal_codes.first
+ assert_equal @deep[:street][:state][:postal_codes].first, postal_codes.first
+ assert_kind_of Bignum, postal_codes.last
+ assert_equal @deep[:street][:state][:postal_codes].last, postal_codes.last
+
+ places = state.places
+ assert_kind_of Array, places
+ assert_kind_of String, places.first
+ assert_equal @deep[:street][:state][:places].first, places.first
end
def test_nested_collections_within_the_same_namespace

0 comments on commit 59c3b0d

Please sign in to comment.