Permalink
Browse files

workaround collections loaded as :people => { :person => [{..attrs..}…

…, {..attrs..}] }

git-svn-id: http://svn-commit.rubyonrails.org/rails/trunk@5167 5ecf4fe2-1ee6-0310-87b1-e25e094e27de
  • Loading branch information...
1 parent a0c677c commit 704443dcf2b61cedfa91d4352bbb351e17997b7c @jeremy jeremy committed Sep 22, 2006
Showing with 44 additions and 28 deletions.
  1. +25 −16 activeresource/lib/active_resource/base.rb
  2. +19 −12 activeresource/test/base/load_test.rb
@@ -15,7 +15,7 @@ def connection(refresh = false)
@connection = Connection.new(site) if refresh || @connection.nil?
@connection
end
-
+
def element_name
self.to_s.underscore
end
@@ -30,20 +30,20 @@ def prefix(options={})
self.prefix = default
prefix(options)
end
-
+
def prefix=(value = '/')
prefix_call = value.gsub(/:\w+/) { |s| "\#{options[#{s}]}" }
method_decl = %(def self.prefix(options={}) "#{prefix_call}" end)
eval method_decl
end
alias_method :set_prefix, :prefix=
-
+
def element_name=(value)
class << self ; attr_reader :element_name ; end
@element_name = value
end
alias_method :set_element_name, :element_name=
-
+
def collection_name=(value)
class << self ; attr_reader :collection_name ; end
@collection_name = value
@@ -53,21 +53,21 @@ class << self ; attr_reader :collection_name ; end
def element_path(id, options = {})
"#{prefix(options)}#{collection_name}/#{id}.xml"
end
-
+
def collection_path(options = {})
"#{prefix(options)}#{collection_name}.xml"
end
-
+
def primary_key
self.primary_key = 'id'
end
-
+
def primary_key=(value)
class << self ; attr_reader :primary_key ; end
@primary_key = value
end
alias_method :set_primary_key, :primary_key=
-
+
# Person.find(1) # => GET /people/1.xml
# StreetAddress.find(1, :person_id => 1) # => GET /people/1/street_addresses/1.xml
def find(*arguments)
@@ -86,7 +86,7 @@ def find(*arguments)
def find_every(options)
connection.get(collection_path(options)).values.first.values.first.collect { |element| new(element, options) }
end
-
+
# { :person => person1 }
def find_single(scope, options)
new(connection.get(element_path(scope, options)).values.first, options)
@@ -95,7 +95,7 @@ def find_single(scope, options)
attr_accessor :attributes
attr_accessor :prefix_options
-
+
def initialize(attributes = {}, prefix_options = {})
@attributes = {}
self.load attributes
@@ -109,11 +109,11 @@ def new?
def id
attributes[self.class.primary_key]
end
-
+
def id=(id)
attributes[self.class.primary_key] = id
end
-
+
def save
new? ? create : update
end
@@ -142,8 +142,17 @@ def load(attributes)
resource = find_or_create_resource_for_collection(key)
value.map { |attrs| resource.new(attrs) }
when Hash
- resource = find_or_create_resource_for(key)
- resource.new(value)
+ # Workaround collections loaded as Hash
+ # :persons => { :person => [
+ # { :id => 1, :name => 'a' },
+ # { :id => 2, :name => 'b' } ]}
+ if value.keys.size == 1 and value.values.first.is_a?(Array)
+ resource = find_or_create_resource_for(value.keys.first)
+ value.values.first.map { |attrs| resource.new(attrs) }
+ else
+ resource = find_or_create_resource_for(key)
+ resource.new(value)
+ end
when ActiveResource::Base
value.class.new(value.attributes)
else
@@ -157,7 +166,7 @@ def load(attributes)
def connection(refresh = false)
self.class.connection(refresh)
end
-
+
def update
connection.put(self.class.element_path(id, prefix_options), to_xml)
end
@@ -190,7 +199,7 @@ def find_or_create_resource_for(name)
def method_missing(method_symbol, *arguments)
method_name = method_symbol.to_s
-
+
case method_name.last
when "="
attributes[method_name.first(-1)] = arguments.first
@@ -5,11 +5,16 @@
class BaseLoadTest < Test::Unit::TestCase
def setup
@matz = { :id => 1, :name => 'Matz' }
- @addys = [{ :id => 1, :street => '12345 Street' }, { :id => 2, :street => '67890 Street' }]
+
+ @first_address = { :id => 1, :street => '12345 Street' }
+ @addresses = [@first_address, { :id => 2, :street => '67890 Street' }]
+ @addresses_from_xml = { :street_addresses => { :street_address => @addresses }}
+
@deep = { :id => 1, :street => {
:id => 1, :state => { :id => 1, :name => 'Oregon',
- :notable_rivers => [{ :id => 1, :name => 'Willamette' },
- { :id => 2, :name => 'Columbia', :rafted_by => @matz }] }}}
+ :notable_rivers => { :notable_river => [
+ { :id => 1, :name => 'Willamette' },
+ { :id => 2, :name => 'Columbia', :rafted_by => @matz }] }}}}
@person = Person.new
end
@@ -25,29 +30,30 @@ def test_load_simple_hash
end
def test_load_one_with_existing_resource
- address = @person.load(:street_address => @addys.first).street_address
+ address = @person.load(:street_address => @first_address).street_address
assert_kind_of StreetAddress, address
- assert_equal @addys.first.stringify_keys, address.attributes
+ assert_equal @first_address.stringify_keys, address.attributes
end
def test_load_one_with_unknown_resource
- address = silence_warnings { @person.load(:address => @addys.first).address }
+ address = silence_warnings { @person.load(:address => @first_address).address }
assert_kind_of Person::Address, address
- assert_equal @addys.first.stringify_keys, address.attributes
+ assert_equal @first_address.stringify_keys, address.attributes
end
def test_load_collection_with_existing_resource
- addresses = @person.load(:street_addresses => @addys).street_addresses
+ addresses = @person.load(@addresses_from_xml).street_addresses
+ assert_kind_of Array, addresses
addresses.each { |address| assert_kind_of StreetAddress, address }
- assert_equal @addys.map(&:stringify_keys), addresses.map(&:attributes)
+ assert_equal @addresses.map(&:stringify_keys), addresses.map(&:attributes)
end
def test_load_collection_with_unknown_resource
assert !Person.const_defined?(:Address), "Address shouldn't exist until autocreated"
- addresses = silence_warnings { @person.load(:addresses => @addys).addresses }
+ addresses = silence_warnings { @person.load(:addresses => @addresses).addresses }
assert Person.const_defined?(:Address), "Address should have been autocreated"
addresses.each { |address| assert_kind_of Person::Address, address }
- assert_equal @addys.map(&:stringify_keys), addresses.map(&:attributes)
+ assert_equal @addresses.map(&:stringify_keys), addresses.map(&:attributes)
end
def test_recursively_loaded_collections
@@ -63,8 +69,9 @@ def test_recursively_loaded_collections
assert_equal @deep[:street][:state][:id], state.id
rivers = state.notable_rivers
+ assert_kind_of Array, rivers
assert_kind_of Person::Street::State::NotableRiver, rivers.first
- assert_equal @deep[:street][:state][:notable_rivers].first[:id], rivers.first.id
+ assert_equal @deep[:street][:state][:notable_rivers][:notable_river].first[:id], rivers.first.id
assert_equal @matz[:id], rivers.last.rafted_by.id
end
end

0 comments on commit 704443d

Please sign in to comment.