workaround collections loaded as :people => { :person => [{..attrs..}, {..attrs..}] }
git-svn-id: http://svn-commit.rubyonrails.org/rails/trunk@5167 5ecf4fe2-1ee6-0310-87b1-e25e094e27de
This commit is contained in:
parent
a0c677c8e6
commit
704443dcf2
|
@ -15,7 +15,7 @@ module ActiveResource
|
|||
@connection = Connection.new(site) if refresh || @connection.nil?
|
||||
@connection
|
||||
end
|
||||
|
||||
|
||||
def element_name
|
||||
self.to_s.underscore
|
||||
end
|
||||
|
@ -30,20 +30,20 @@ module ActiveResource
|
|||
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 @@ module ActiveResource
|
|||
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 @@ module ActiveResource
|
|||
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 @@ module ActiveResource
|
|||
|
||||
attr_accessor :attributes
|
||||
attr_accessor :prefix_options
|
||||
|
||||
|
||||
def initialize(attributes = {}, prefix_options = {})
|
||||
@attributes = {}
|
||||
self.load attributes
|
||||
|
@ -109,11 +109,11 @@ module ActiveResource
|
|||
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 @@ module ActiveResource
|
|||
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 @@ module ActiveResource
|
|||
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 @@ module ActiveResource
|
|||
|
||||
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 @@ require "fixtures/street_address"
|
|||
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 @@ class BaseLoadTest < Test::Unit::TestCase
|
|||
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 @@ class BaseLoadTest < Test::Unit::TestCase
|
|||
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
|
||||
|
|
Loading…
Reference in New Issue