diff options
author | Jeremy Kemper <jeremy@bitsweat.net> | 2006-09-22 21:29:54 +0000 |
---|---|---|
committer | Jeremy Kemper <jeremy@bitsweat.net> | 2006-09-22 21:29:54 +0000 |
commit | 704443dcf2b61cedfa91d4352bbb351e17997b7c (patch) | |
tree | fb0908fec783175623ed33603e15ae05768fd2d6 /activeresource/lib | |
parent | a0c677c8e630fb4681153290494ae7abfb1675a2 (diff) | |
download | rails-704443dcf2b61cedfa91d4352bbb351e17997b7c.tar.gz rails-704443dcf2b61cedfa91d4352bbb351e17997b7c.tar.bz2 rails-704443dcf2b61cedfa91d4352bbb351e17997b7c.zip |
workaround collections loaded as :people => { :person => [{..attrs..}, {..attrs..}] }
git-svn-id: http://svn-commit.rubyonrails.org/rails/trunk@5167 5ecf4fe2-1ee6-0310-87b1-e25e094e27de
Diffstat (limited to 'activeresource/lib')
-rw-r--r-- | activeresource/lib/active_resource/base.rb | 41 |
1 files changed, 25 insertions, 16 deletions
diff --git a/activeresource/lib/active_resource/base.rb b/activeresource/lib/active_resource/base.rb index 647fd90bb5..cb520eef5c 100644 --- a/activeresource/lib/active_resource/base.rb +++ b/activeresource/lib/active_resource/base.rb @@ -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 |