diff options
author | Jacques Crocker <railsjedi@gmail.com> | 2010-09-18 03:09:37 -0700 |
---|---|---|
committer | José Valim <jose.valim@gmail.com> | 2010-09-25 13:54:09 +0200 |
commit | 51f1f550dab47c6ec3dcdba7b153258e2a0feb69 (patch) | |
tree | 8502e35b46ddff65795db6c20edcf0768b56dc7f /activeresource/lib | |
parent | ee5ef67c443407f616feef3a8cade8ba3a9d6ef0 (diff) | |
download | rails-51f1f550dab47c6ec3dcdba7b153258e2a0feb69.tar.gz rails-51f1f550dab47c6ec3dcdba7b153258e2a0feb69.tar.bz2 rails-51f1f550dab47c6ec3dcdba7b153258e2a0feb69.zip |
Allow per Resource format settings
Previously, ActiveResource was using the connection level formatter for get requests. This made it impossible to use custom formatters per resource.
Additionally this commit makes the Connection request methods more consistent. It always returns a Response. The base will then decode it each the response using its format setting.
Merging this commit will allow users to add custom formatters on a per Resource basis. This enables handling pagination responses from the server side, a very common use case that was previously impossible without monkeypatching XmlFormat.
Signed-off-by: José Valim <jose.valim@gmail.com>
Diffstat (limited to 'activeresource/lib')
-rw-r--r-- | activeresource/lib/active_resource/base.rb | 10 | ||||
-rw-r--r-- | activeresource/lib/active_resource/connection.rb | 2 | ||||
-rw-r--r-- | activeresource/lib/active_resource/custom_methods.rb | 4 |
3 files changed, 8 insertions, 8 deletions
diff --git a/activeresource/lib/active_resource/base.rb b/activeresource/lib/active_resource/base.rb index 915021a7b3..d31db9f0ba 100644 --- a/activeresource/lib/active_resource/base.rb +++ b/activeresource/lib/active_resource/base.rb @@ -678,7 +678,7 @@ module ActiveResource # Returns the new resource instance. # def build(attributes = {}) - attrs = connection.get("#{new_element_path}").merge(attributes) + attrs = self.format.decode(connection.get("#{new_element_path}").body).merge(attributes) self.new(attrs) end @@ -850,11 +850,11 @@ module ActiveResource instantiate_collection(get(from, options[:params])) when String path = "#{from}#{query_string(options[:params])}" - instantiate_collection(connection.get(path, headers) || []) + instantiate_collection(format.decode(connection.get(path, headers).body) || []) else prefix_options, query_options = split_options(options[:params]) path = collection_path(prefix_options, query_options) - instantiate_collection( (connection.get(path, headers) || []), prefix_options ) + instantiate_collection( (format.decode(connection.get(path, headers).body) || []), prefix_options ) end rescue ActiveResource::ResourceNotFound # Swallowing ResourceNotFound exceptions and return nil - as per @@ -870,7 +870,7 @@ module ActiveResource instantiate_record(get(from, options[:params])) when String path = "#{from}#{query_string(options[:params])}" - instantiate_record(connection.get(path, headers)) + instantiate_record(format.decode(connection.get(path, headers).body)) end end @@ -878,7 +878,7 @@ module ActiveResource def find_single(scope, options) prefix_options, query_options = split_options(options[:params]) path = element_path(scope, prefix_options, query_options) - instantiate_record(connection.get(path, headers), prefix_options) + instantiate_record(format.decode(connection.get(path, headers).body), prefix_options) end def instantiate_collection(collection, prefix_options = {}) diff --git a/activeresource/lib/active_resource/connection.rb b/activeresource/lib/active_resource/connection.rb index b7befe110d..c587225e24 100644 --- a/activeresource/lib/active_resource/connection.rb +++ b/activeresource/lib/active_resource/connection.rb @@ -76,7 +76,7 @@ module ActiveResource # Executes a GET request. # Used to get (find) resources. def get(path, headers = {}) - with_auth { format.decode(request(:get, path, build_request_headers(headers, :get, self.site.merge(path))).body) } + with_auth { request(:get, path, build_request_headers(headers, :get, self.site.merge(path))) } end # Executes a DELETE request (see HTTP protocol documentation if unfamiliar). diff --git a/activeresource/lib/active_resource/custom_methods.rb b/activeresource/lib/active_resource/custom_methods.rb index dd3e35dfc7..9879f8cded 100644 --- a/activeresource/lib/active_resource/custom_methods.rb +++ b/activeresource/lib/active_resource/custom_methods.rb @@ -54,7 +54,7 @@ module ActiveResource # # Person.find(:all, :from => :active) def get(custom_method_name, options = {}) - connection.get(custom_method_collection_url(custom_method_name, options), headers) + format.decode(connection.get(custom_method_collection_url(custom_method_name, options), headers).body) end def post(custom_method_name, options = {}, body = '') @@ -85,7 +85,7 @@ module ActiveResource module InstanceMethods def get(method_name, options = {}) - connection.get(custom_method_element_url(method_name, options), self.class.headers) + self.class.format.decode(connection.get(custom_method_element_url(method_name, options), self.class.headers).body) end def post(method_name, options = {}, body = nil) |