Skip to content

Instantly share code, notes, and snippets.

@gnufied
Created December 24, 2010 06:47
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save gnufied/753978 to your computer and use it in GitHub Desktop.
Save gnufied/753978 to your computer and use it in GitHub Desktop.
From ecb97ef1cb7f7439210d4f21a384f213947641d8 Mon Sep 17 00:00:00 2001
From: Hemant Kumar <hkumar@crri.co.in>
Date: Fri, 3 Dec 2010 23:40:29 +0530
Subject: [PATCH 1/2] ignore idea file
---
.gitignore | 1 +
1 files changed, 1 insertions(+), 0 deletions(-)
diff --git a/.gitignore b/.gitignore
index a18fba3..16eaf6e 100644
--- a/.gitignore
+++ b/.gitignore
@@ -1,6 +1,7 @@
*.gem
pkg
.bundle
+.idea
Gemfile.lock
debug.log
doc/rdoc
--
1.7.2.1
From 97d4940a8e56fe5751a4fb6e8ee741b0ccbd0930 Mon Sep 17 00:00:00 2001
From: Hemant Kumar <hkumar@crri.co.in>
Date: Fri, 24 Dec 2010 12:03:00 +0530
Subject: [PATCH 2/2] move class methods to ClassMethods module, so that plugin authors can
overwrite the methods easily
---
activeresource/lib/active_resource.rb | 1 +
activeresource/lib/active_resource/base.rb | 19 +-----
.../lib/active_resource/custom_methods.rb | 66 +++++++++-----------
activeresource/lib/active_resource/http_methods.rb | 24 +++++++
4 files changed, 56 insertions(+), 54 deletions(-)
create mode 100644 activeresource/lib/active_resource/http_methods.rb
diff --git a/activeresource/lib/active_resource.rb b/activeresource/lib/active_resource.rb
index 186865f..f17472a 100644
--- a/activeresource/lib/active_resource.rb
+++ b/activeresource/lib/active_resource.rb
@@ -37,6 +37,7 @@ module ActiveResource
autoload :Base
autoload :Connection
autoload :CustomMethods
+ autoload :HttpMethods
autoload :Formats
autoload :HttpMock
autoload :Observing
diff --git a/activeresource/lib/active_resource/base.rb b/activeresource/lib/active_resource/base.rb
index d959fd1..018a127 100644
--- a/activeresource/lib/active_resource/base.rb
+++ b/activeresource/lib/active_resource/base.rb
@@ -821,24 +821,6 @@ module ActiveResource
end
- # Deletes the resources with the ID in the +id+ parameter.
- #
- # ==== Options
- # All options specify \prefix and query parameters.
- #
- # ==== Examples
- # Event.delete(2) # sends DELETE /events/2
- #
- # Event.create(:name => 'Free Concert', :location => 'Community Center')
- # my_event = Event.find(:first) # let's assume this is event with ID 7
- # Event.delete(my_event.id) # sends DELETE /events/7
- #
- # # Let's assume a request to events/5/cancel.xml
- # Event.delete(params[:id]) # sends DELETE /events/5
- def delete(id, options = {})
- connection.delete(element_path(id, options))
- end
-
# Asserts the existence of a resource, returning <tt>true</tt> if the resource is found.
#
# ==== Examples
@@ -1427,6 +1409,7 @@ module ActiveResource
class Base
extend ActiveModel::Naming
+ include HttpMethods
include CustomMethods, Observing, Validations
include ActiveModel::Conversion
include ActiveModel::Serializers::JSON
diff --git a/activeresource/lib/active_resource/custom_methods.rb b/activeresource/lib/active_resource/custom_methods.rb
index 9879f8c..3cd9c05 100644
--- a/activeresource/lib/active_resource/custom_methods.rb
+++ b/activeresource/lib/active_resource/custom_methods.rb
@@ -35,48 +35,42 @@ module ActiveResource
module CustomMethods
extend ActiveSupport::Concern
- included do
- class << self
- alias :orig_delete :delete
-
- # Invokes a GET to a given custom REST method. For example:
- #
- # Person.get(:active) # GET /people/active.xml
- # # => [{:id => 1, :name => 'Ryan'}, {:id => 2, :name => 'Joe'}]
- #
- # Person.get(:active, :awesome => true) # GET /people/active.xml?awesome=true
- # # => [{:id => 1, :name => 'Ryan'}]
- #
- # Note: the objects returned from this method are not automatically converted
- # into ActiveResource::Base instances - they are ordinary Hashes. If you are expecting
- # ActiveResource::Base instances, use the <tt>find</tt> class method with the
- # <tt>:from</tt> option. For example:
- #
- # Person.find(:all, :from => :active)
- def get(custom_method_name, options = {})
- format.decode(connection.get(custom_method_collection_url(custom_method_name, options), headers).body)
- end
+ module ClassMethods
+ # Invokes a GET to a given custom REST method. For example:
+ #
+ # Person.get(:active) # GET /people/active.xml
+ # # => [{:id => 1, :name => 'Ryan'}, {:id => 2, :name => 'Joe'}]
+ #
+ # Person.get(:active, :awesome => true) # GET /people/active.xml?awesome=true
+ # # => [{:id => 1, :name => 'Ryan'}]
+ #
+ # Note: the objects returned from this method are not automatically converted
+ # into ActiveResource::Base instances - they are ordinary Hashes. If you are expecting
+ # ActiveResource::Base instances, use the <tt>find</tt> class method with the
+ # <tt>:from</tt> option. For example:
+ #
+ # Person.find(:all, :from => :active)
+ def get(custom_method_name, options = {})
+ format.decode(connection.get(custom_method_collection_url(custom_method_name, options), headers).body)
+ end
- def post(custom_method_name, options = {}, body = '')
- connection.post(custom_method_collection_url(custom_method_name, options), body, headers)
- end
+ def post(custom_method_name, options = {}, body = '')
+ connection.post(custom_method_collection_url(custom_method_name, options), body, headers)
+ end
- def put(custom_method_name, options = {}, body = '')
- connection.put(custom_method_collection_url(custom_method_name, options), body, headers)
- end
+ def put(custom_method_name, options = {}, body = '')
+ connection.put(custom_method_collection_url(custom_method_name, options), body, headers)
+ end
- def delete(custom_method_name, options = {})
- # Need to jump through some hoops to retain the original class 'delete' method
- if custom_method_name.is_a?(Symbol)
- connection.delete(custom_method_collection_url(custom_method_name, options), headers)
- else
- orig_delete(custom_method_name, options)
- end
+ def delete(custom_method_name, options = {})
+ # Need to jump through some hoops to retain the original class 'delete' method
+ if custom_method_name.is_a?(Symbol)
+ connection.delete(custom_method_collection_url(custom_method_name, options), headers)
+ else
+ super(custom_method_name, options)
end
end
- end
- module ClassMethods
def custom_method_collection_url(method_name, options = {})
prefix_options, query_options = split_options(options)
"#{prefix(prefix_options)}#{collection_name}/#{method_name}.#{format.extension}#{query_string(query_options)}"
diff --git a/activeresource/lib/active_resource/http_methods.rb b/activeresource/lib/active_resource/http_methods.rb
new file mode 100644
index 0000000..a20bd01
--- /dev/null
+++ b/activeresource/lib/active_resource/http_methods.rb
@@ -0,0 +1,24 @@
+module ActiveResource
+ module HttpMethods
+ extend ActiveSupport::Concern
+ module ClassMethods
+ # Deletes the resources with the ID in the +id+ parameter.
+ #
+ # ==== Options
+ # All options specify \prefix and query parameters.
+ #
+ # ==== Examples
+ # Event.delete(2) # sends DELETE /events/2
+ #
+ # Event.create(:name => 'Free Concert', :location => 'Community Center')
+ # my_event = Event.find(:first) # let's assume this is event with ID 7
+ # Event.delete(my_event.id) # sends DELETE /events/7
+ #
+ # # Let's assume a request to events/5/cancel.xml
+ # Event.delete(params[:id]) # sends DELETE /events/5
+ def delete(id, options = {})
+ connection.delete(element_path(id, options))
+ end
+ end
+ end
+end
--
1.7.2.1
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment