Permalink
Browse files

Disregard namespaces from the default element name, so Highrise::Pers…

…on will just try to fetch from "/people", not "/highrise/people" [DHH] Added that saves which get a body response (and not just a 201) will use that response to update themselves [DHH] Fixed constant warning when fetching the same object multiple times [DHH]

git-svn-id: http://svn-commit.rubyonrails.org/rails/trunk@6539 5ecf4fe2-1ee6-0310-87b1-e25e094e27de
  • Loading branch information...
1 parent a0e78f7 commit c97f329b2a76439cd4fe71debdd64c71e9890b48 @dhh dhh committed Apr 19, 2007
@@ -1,5 +1,11 @@
*SVN*
+* Fixed constant warning when fetching the same object multiple times [DHH]
+
+* Added that saves which get a body response (and not just a 201) will use that response to update themselves [DHH]
+
+* Disregard namespaces from the default element name, so Highrise::Person will just try to fetch from "/people", not "/highrise/people" [DHH]
+
* Allow array and hash query parameters. #7756 [Greg Spurrier]
* Loading a resource preserves its prefix_options. #7353 [Ryan Daigle]
@@ -29,7 +29,10 @@ def connection(refresh = false)
@connection
end
- attr_accessor_with_default(:element_name) { to_s.underscore } #:nodoc:
+ # Do not include any modules in the default element name. This makes it easier to seclude ARes objects
+ # in a separate namespace without having to set element_name repeatedly.
+ attr_accessor_with_default(:element_name) { to_s.split("::").last.underscore } #:nodoc:
+
attr_accessor_with_default(:collection_name) { element_name.pluralize } #:nodoc:
attr_accessor_with_default(:primary_key, 'id') #:nodoc:
@@ -149,7 +152,7 @@ def query_string(options)
def initialize(attributes = {}, prefix_options = {})
@attributes = {}
- self.load attributes
+ load(attributes)
@prefix_options = prefix_options
end
@@ -184,7 +187,9 @@ def hash
id.hash
end
- # Delegates to +create+ if a new object, +update+ if its old.
+ # Delegates to +create+ if a new object, +update+ if its old. If the response to the save includes a body,
+ # it will be assumed that this body is XML for the final object as it looked after the save (which would include
+ # attributes like created_at that wasn't part of the original submit).
def save
new? ? create : update
end
@@ -206,7 +211,7 @@ def to_xml(options={})
# Reloads the attributes of this object from the remote web service.
def reload
- self.load self.class.find(id, @prefix_options).attributes
+ self.load(self.class.find(id, @prefix_options).attributes)
end
# Manually load attributes from a hash. Recursively loads collections of
@@ -245,6 +250,10 @@ def update
def create
returning connection.post(collection_path, to_xml) do |response|
self.id = id_from_response(response)
+
+ if response['Content-size'] != "0" && response.body.strip.size > 0
+ load(connection.xml_from_response(response))
+ end
end
end
@@ -270,7 +279,7 @@ def find_or_create_resource_for_collection(name)
# Tries to find a resource for a given name; if it fails, then the resource is created
def find_or_create_resource_for(name)
resource_name = name.to_s.camelize
- resource_name.constantize
+ self.class.const_get(resource_name)
rescue NameError
resource = self.class.const_set(resource_name, Class.new(ActiveResource::Base))
resource.prefix = self.class.prefix
@@ -52,7 +52,7 @@ def site=(site)
# Execute a GET request.
# Used to get (find) resources.
def get(path)
- from_xml_data(Hash.from_xml(request(:get, path, build_request_headers).body).values.first)
+ xml_from_response(request(:get, path, build_request_headers))
end
# Execute a DELETE request (see HTTP protocol documentation if unfamiliar).
@@ -73,6 +73,15 @@ def post(path, body = '')
request(:post, path, body, build_request_headers)
end
+ def xml_from_response(response)
+ if response = from_xml_data(Hash.from_xml(response.body))
+ response.first
+ else
+ nil
+ end
+ end
+
+
private
# Makes request to remote service.
def request(method, path, *arguments)
@@ -138,7 +147,7 @@ def from_xml_data(data)
case data.values.first
when Hash then [ from_xml_data(data.values.first) ]
when Array then from_xml_data(data.values.first)
- else data
+ else data.values.first
end
else
data.each_key { |key| data[key] = from_xml_data(data[key]) }
@@ -12,7 +12,7 @@ def initialize(responses)
for method in [ :post, :put, :get, :delete ]
module_eval <<-EOE
def #{method}(path, request_headers = {}, body = nil, status = 200, response_headers = {})
- @responses[Request.new(:#{method}, path, nil, request_headers)] = Response.new(body || {}, status, response_headers)
+ @responses[Request.new(:#{method}, path, nil, request_headers)] = Response.new(body || "", status, response_headers)
end
EOE
end
@@ -16,8 +16,8 @@ def setup
mock.put "/people/1.xml", {}, nil, 204
mock.delete "/people/1.xml", {}, nil, 200
mock.delete "/people/2.xml", {}, nil, 400
- mock.post "/people.xml", {}, nil, 201, 'Location' => '/people/5.xml'
mock.get "/people/99.xml", {}, nil, 404
+ mock.post "/people.xml", {}, "<person><name>Rick</name><age type='integer'>25</age></person>", 201, 'Location' => '/people/5.xml'
mock.get "/people.xml", {}, "<people>#{@matz}#{@david}</people>"
mock.get "/people/1/addresses.xml", {}, "<addresses>#{@addy}</addresses>"
mock.get "/people/1/addresses/1.xml", {}, @addy
@@ -128,14 +128,6 @@ def test_custom_collection_name
assert_equal 'addresses', StreetAddress.collection_name
end
- def test_nested_element_name
- self.class.const_set :Actor, Class.new(Person)
- assert_equal 'base_test/actor', Actor.element_name
- ensure
- self.class.remove_const :Actor rescue nil
- end
-
-
def test_prefix
assert_equal "/", Person.prefix
assert_equal Set.new, Person.send(:prefix_parameters)
@@ -215,7 +207,7 @@ def test_id_from_response
end
def test_create_with_custom_prefix
- matzs_house = StreetAddress.new({}, {:person_id => 1})
+ matzs_house = StreetAddress.new({}, { :person_id => 1 })
matzs_house.save
assert_equal '5', matzs_house.id
end
@@ -232,6 +224,9 @@ def test_create
assert rick.valid?
assert !rick.new?
assert_equal '5', rick.id
+
+ # test additional attribute returned on create
+ assert_equal 25, rick.age
# Test that save exceptions get bubbled up too
ActiveResource::HttpMock.respond_to do |mock|
@@ -92,7 +92,7 @@ def test_get_collection_single
def test_get_collection_empty
people = @conn.get("/people_empty_elements.xml")
- assert_equal people, nil
+ assert_nil people
end
def test_post

0 comments on commit c97f329

Please sign in to comment.