Changeset 6539
- Timestamp:
- 04/19/07 22:18:03 (1 year ago)
- Files:
-
- trunk/activeresource/CHANGELOG (modified) (1 diff)
- trunk/activeresource/lib/active_resource/base.rb (modified) (6 diffs)
- trunk/activeresource/lib/active_resource/connection.rb (modified) (3 diffs)
- trunk/activeresource/lib/active_resource/http_mock.rb (modified) (1 diff)
- trunk/activeresource/test/base_test.rb (modified) (4 diffs)
- trunk/activeresource/test/connection_test.rb (modified) (1 diff)
Legend:
- Unmodified
- Added
- Removed
- Modified
- Copied
- Moved
trunk/activeresource/CHANGELOG
r6379 r6539 1 1 *SVN* 2 3 * Fixed constant warning when fetching the same object multiple times [DHH] 4 5 * Added that saves which get a body response (and not just a 201) will use that response to update themselves [DHH] 6 7 * Disregard namespaces from the default element name, so Highrise::Person will just try to fetch from "/people", not "/highrise/people" [DHH] 2 8 3 9 * Allow array and hash query parameters. #7756 [Greg Spurrier] trunk/activeresource/lib/active_resource/base.rb
r6379 r6539 30 30 end 31 31 32 attr_accessor_with_default(:element_name) { to_s.underscore } #:nodoc: 32 # Do not include any modules in the default element name. This makes it easier to seclude ARes objects 33 # in a separate namespace without having to set element_name repeatedly. 34 attr_accessor_with_default(:element_name) { to_s.split("::").last.underscore } #:nodoc: 35 33 36 attr_accessor_with_default(:collection_name) { element_name.pluralize } #:nodoc: 34 37 attr_accessor_with_default(:primary_key, 'id') #:nodoc: … … 150 153 def initialize(attributes = {}, prefix_options = {}) 151 154 @attributes = {} 152 self.load attributes155 load(attributes) 153 156 @prefix_options = prefix_options 154 157 end … … 185 188 end 186 189 187 # Delegates to +create+ if a new object, +update+ if its old. 190 # Delegates to +create+ if a new object, +update+ if its old. If the response to the save includes a body, 191 # it will be assumed that this body is XML for the final object as it looked after the save (which would include 192 # attributes like created_at that wasn't part of the original submit). 188 193 def save 189 194 new? ? create : update … … 207 212 # Reloads the attributes of this object from the remote web service. 208 213 def reload 209 self.load self.class.find(id, @prefix_options).attributes214 self.load(self.class.find(id, @prefix_options).attributes) 210 215 end 211 216 … … 246 251 returning connection.post(collection_path, to_xml) do |response| 247 252 self.id = id_from_response(response) 253 254 if response['Content-size'] != "0" && response.body.strip.size > 0 255 load(connection.xml_from_response(response)) 256 end 248 257 end 249 258 end … … 271 280 def find_or_create_resource_for(name) 272 281 resource_name = name.to_s.camelize 273 resource_name.constantize282 self.class.const_get(resource_name) 274 283 rescue NameError 275 284 resource = self.class.const_set(resource_name, Class.new(ActiveResource::Base)) trunk/activeresource/lib/active_resource/connection.rb
r5967 r6539 53 53 # Used to get (find) resources. 54 54 def get(path) 55 from_xml_data(Hash.from_xml(request(:get, path, build_request_headers).body).values.first)55 xml_from_response(request(:get, path, build_request_headers)) 56 56 end 57 57 … … 73 73 request(:post, path, body, build_request_headers) 74 74 end 75 76 def xml_from_response(response) 77 if response = from_xml_data(Hash.from_xml(response.body)) 78 response.first 79 else 80 nil 81 end 82 end 83 75 84 76 85 private … … 139 148 when Hash then [ from_xml_data(data.values.first) ] 140 149 when Array then from_xml_data(data.values.first) 141 else data 150 else data.values.first 142 151 end 143 152 else trunk/activeresource/lib/active_resource/http_mock.rb
r5962 r6539 13 13 module_eval <<-EOE 14 14 def #{method}(path, request_headers = {}, body = nil, status = 200, response_headers = {}) 15 @responses[Request.new(:#{method}, path, nil, request_headers)] = Response.new(body || {}, status, response_headers)15 @responses[Request.new(:#{method}, path, nil, request_headers)] = Response.new(body || "", status, response_headers) 16 16 end 17 17 EOE trunk/activeresource/test/base_test.rb
r6379 r6539 17 17 mock.delete "/people/1.xml", {}, nil, 200 18 18 mock.delete "/people/2.xml", {}, nil, 400 19 mock.post "/people.xml", {}, nil, 201, 'Location' => '/people/5.xml'20 19 mock.get "/people/99.xml", {}, nil, 404 20 mock.post "/people.xml", {}, "<person><name>Rick</name><age type='integer'>25</age></person>", 201, 'Location' => '/people/5.xml' 21 21 mock.get "/people.xml", {}, "<people>#{@matz}#{@david}</people>" 22 22 mock.get "/people/1/addresses.xml", {}, "<addresses>#{@addy}</addresses>" … … 129 129 end 130 130 131 def test_nested_element_name132 self.class.const_set :Actor, Class.new(Person)133 assert_equal 'base_test/actor', Actor.element_name134 ensure135 self.class.remove_const :Actor rescue nil136 end137 138 139 131 def test_prefix 140 132 assert_equal "/", Person.prefix … … 216 208 217 209 def test_create_with_custom_prefix 218 matzs_house = StreetAddress.new({}, { :person_id => 1})210 matzs_house = StreetAddress.new({}, { :person_id => 1 }) 219 211 matzs_house.save 220 212 assert_equal '5', matzs_house.id … … 233 225 assert !rick.new? 234 226 assert_equal '5', rick.id 227 228 # test additional attribute returned on create 229 assert_equal 25, rick.age 235 230 236 231 # Test that save exceptions get bubbled up too trunk/activeresource/test/connection_test.rb
r5967 r6539 93 93 def test_get_collection_empty 94 94 people = @conn.get("/people_empty_elements.xml") 95 assert_ equal people, nil95 assert_nil people 96 96 end 97 97