Ticket #10589: ares_docfixes.diff
| File ares_docfixes.diff, 12.2 kB (added by chuyeow, 7 months ago) |
|---|
-
activeresource/lib/active_resource/custom_methods.rb
old new 1 # A module to support custom REST methods and sub-resources, allowing you to break out2 # of the "default" REST methods with your own custom resource requests. For example,3 # say you use Rails to expose a REST service and configure your routes with:4 #5 # map.resources :people, :new => { :register => :post },6 # :element => { :promote => :put, :deactivate => :delete }7 # :collection => { :active => :get }8 #9 # This route set creates routes for the following http requests:10 #11 # POST /people/new/register.xml #=> PeopleController.register12 # PUT /people/1/promote.xml #=> PeopleController.promote with :id => 113 # DELETE /people/1/deactivate.xml #=> PeopleController.deactivate with :id => 114 # GET /people/active.xml #=> PeopleController.active15 #16 # Using this module, Active Resource can use these custom REST methods just like the17 # standard methods.18 #19 # class Person < ActiveResource::Base20 # self.site = "http://37s.sunrise.i:3000"21 # end22 #23 # Person.new(:name => 'Ryan).post(:register) # POST /people/new/register.xml24 # # => { :id => 1, :name => 'Ryan' }25 #26 # Person.find(1).put(:promote, :position => 'Manager') # PUT /people/1/promote.xml27 # Person.find(1).delete(:deactivate) # DELETE /people/1/deactivate.xml28 #29 # Person.get(:active) # GET /people/active.xml30 # # => [{:id => 1, :name => 'Ryan'}, {:id => 2, :name => 'Joe'}]31 #32 1 module ActiveResource 2 # A module to support custom REST methods and sub-resources, allowing you to break out 3 # of the "default" REST methods with your own custom resource requests. For example, 4 # say you use Rails to expose a REST service and configure your routes with: 5 # 6 # map.resources :people, :new => { :register => :post }, 7 # :element => { :promote => :put, :deactivate => :delete } 8 # :collection => { :active => :get } 9 # 10 # This route set creates routes for the following http requests: 11 # 12 # POST /people/new/register.xml #=> PeopleController.register 13 # PUT /people/1/promote.xml #=> PeopleController.promote with :id => 1 14 # DELETE /people/1/deactivate.xml #=> PeopleController.deactivate with :id => 1 15 # GET /people/active.xml #=> PeopleController.active 16 # 17 # Using this module, Active Resource can use these custom REST methods just like the 18 # standard methods. 19 # 20 # class Person < ActiveResource::Base 21 # self.site = "http://37s.sunrise.i:3000" 22 # end 23 # 24 # Person.new(:name => 'Ryan).post(:register) # POST /people/new/register.xml 25 # # => { :id => 1, :name => 'Ryan' } 26 # 27 # Person.find(1).put(:promote, :position => 'Manager') # PUT /people/1/promote.xml 28 # Person.find(1).delete(:deactivate) # DELETE /people/1/deactivate.xml 29 # 30 # Person.get(:active) # GET /people/active.xml 31 # # => [{:id => 1, :name => 'Ryan'}, {:id => 2, :name => 'Joe'}] 32 # 33 33 module CustomMethods 34 def self.included( within)35 within.class_eval do34 def self.included(base) 35 base.class_eval do 36 36 extend ActiveResource::CustomMethods::ClassMethods 37 37 include ActiveResource::CustomMethods::InstanceMethods 38 38 39 39 class << self 40 40 alias :orig_delete :delete 41 41 42 def get(method_name, options = {}) 43 connection.get(custom_method_collection_url(method_name, options), headers) 42 # Invokes a GET to a given custom REST method. For example: 43 # 44 # Person.get(:active) # GET /people/active.xml 45 # # => [{:id => 1, :name => 'Ryan'}, {:id => 2, :name => 'Joe'}] 46 # 47 # Person.get(:active, :awesome => true) # GET /people/active.xml?awesome=true 48 # # => [{:id => 1, :name => 'Ryan'}] 49 # 50 # Note: the objects returned from this method are not automatically converted 51 # into ActiveResource instances - they are ordinary Hashes. If you are expecting 52 # ActiveResource instances, use the <tt>find</tt> class method with the 53 # <tt>:from</tt> option. For example: 54 # 55 # Person.find(:all, :from => :active) 56 def get(custom_method_name, options = {}) 57 connection.get(custom_method_collection_url(custom_method_name, options), headers) 44 58 end 45 59 46 def post( method_name, options = {}, body = '')47 connection.post(custom_method_collection_url( method_name, options), body, headers)60 def post(custom_method_name, options = {}, body = '') 61 connection.post(custom_method_collection_url(custom_method_name, options), body, headers) 48 62 end 49 63 50 def put( method_name, options = {}, body = '')51 connection.put(custom_method_collection_url( method_name, options), body, headers)64 def put(custom_method_name, options = {}, body = '') 65 connection.put(custom_method_collection_url(custom_method_name, options), body, headers) 52 66 end 53 67 54 # Need to jump through some hoops to retain the original class 'delete' method55 68 def delete(custom_method_name, options = {}) 56 if (custom_method_name.is_a?(Symbol)) 69 # Need to jump through some hoops to retain the original class 'delete' method 70 if custom_method_name.is_a?(Symbol) 57 71 connection.delete(custom_method_collection_url(custom_method_name, options), headers) 58 72 else 59 73 orig_delete(custom_method_name, options) -
activeresource/lib/active_resource/base.rb
old new 5 5 module ActiveResource 6 6 # ActiveResource::Base is the main class for mapping RESTful resources as models in a Rails application. 7 7 # 8 # For an outline of what Active Resource is capable of, see link:files/ README.html.8 # For an outline of what Active Resource is capable of, see link:files/vendor/rails/activeresource/README.html. 9 9 # 10 10 # == Automated mapping 11 11 # … … 33 33 # ryan.exists? #=> true 34 34 # 35 35 # ryan = Person.find(1) 36 # # => Resource holding our newly create Person object36 # # => Resource holding our newly created Person object 37 37 # 38 38 # ryan.first = 'Rizzle' 39 39 # ryan.save #=> true … … 46 46 # === Custom REST methods 47 47 # 48 48 # Since simple CRUD/lifecycle methods can't accomplish every task, Active Resource also supports 49 # defining your own custom REST methods. 50 # 51 # Person.new(:name => 'Ryan).post(:register) 49 # defining your own custom REST methods. To invoke them, Active Resource provides the <tt>get</tt>, 50 # <tt>post</tt>, <tt>post</tt> and <tt>put</tt> methods where you can specify a custom REST method 51 # name to invoke. 52 # 53 # # POST to the custom 'register' REST method, i.e. POST /people/new/register.xml. 54 # Person.new(:name => 'Ryan').post(:register) 52 55 # # => { :id => 1, :name => 'Ryan', :position => 'Clerk' } 53 56 # 57 # # PUT an update by invoking the 'promote' REST method, i.e. PUT /people/1/promote.xml?position=Manager. 54 58 # Person.find(1).put(:promote, :position => 'Manager') 55 59 # # => { :id => 1, :name => 'Ryan', :position => 'Manager' } 60 # 61 # # GET all the positions available, i.e. GET /people/positions.xml. 62 # Person.get(:positions) 63 # # => [{:name => 'Manager'}, {:name => 'Clerk'}] 64 # 65 # # DELETE to 'fire' a person, i.e. DELETE /people/1/fire.xml. 66 # Person.find(1).delete(:fire) 56 67 # 57 # For more information on creating andusing custom REST methods, see the68 # For more information on using custom REST methods, see the 58 69 # ActiveResource::CustomMethods documentation. 59 70 # 60 71 # == Validations … … 87 98 # == Errors & Validation 88 99 # 89 100 # Error handling and validation is handled in much the same manner as you're used to seeing in 90 # Active Record. Both the response code in the H ttpresponse and the body of the response are used to101 # Active Record. Both the response code in the HTTP response and the body of the response are used to 91 102 # indicate that an error occurred. 92 103 # 93 104 # === Resource errors 94 105 # 95 # When a get is requested for a resource that does not exist, the HTTP +404+(Resource Not Found)106 # When a GET is requested for a resource that does not exist, the HTTP <tt>404</tt> (Resource Not Found) 96 107 # response code will be returned from the server which will raise an ActiveResource::ResourceNotFound 97 108 # exception. 98 109 # … … 100 111 # ryan = Person.find(999) # => Raises ActiveResource::ResourceNotFound 101 112 # # => Response = 404 102 113 # 103 # +404+is just one of the HTTP error response codes that ActiveResource will handle with its own exception. The114 # <tt>404</tt> is just one of the HTTP error response codes that ActiveResource will handle with its own exception. The 104 115 # following HTTP response codes will also result in these exceptions: 105 116 # 106 117 # 200 - 399:: Valid response, no exception … … 125 136 # 126 137 # Active Resource supports validations on resources and will return errors if any these validations fail 127 138 # (e.g., "First name can not be blank" and so on). These types of errors are denoted in the response by 128 # a response code of +422+and an XML representation of the validation errors. The save operation will129 # then fail (with a +false+return value) and the validation errors can be accessed on the resource in question.139 # a response code of <tt>422</tt> and an XML representation of the validation errors. The save operation will 140 # then fail (with a <tt>false</tt> return value) and the validation errors can be accessed on the resource in question. 130 141 # 131 142 # ryan = Person.find(1) 132 143 # ryan.first #=> '' … … 191 202 192 203 # An instance of ActiveResource::Connection that is the base connection to the remote service. 193 204 # The +refresh+ parameter toggles whether or not the connection is refreshed at every request 194 # or not (defaults to +false+).205 # or not (defaults to <tt>false</tt>). 195 206 def connection(refresh = false) 196 207 if defined?(@connection) || superclass == Object 197 208 @connection = Connection.new(site, format) if refresh || @connection.nil? … … 372 383 # Person.find(:one, :from => :leader) 373 384 # # => GET /people/leader.xml 374 385 # 386 # Person.find(:all, :from => :developers, :params => { :language => 'ruby' }) 387 # # => GET /people/developers.xml?language=ruby 388 # 375 389 # Person.find(:one, :from => "/companies/1/manager.xml") 376 390 # # => GET /companies/1/manager.xml 377 391 # … … 687 701 # 688 702 # indent:: Set the indent level for the XML output (default is +2+). 689 703 # dasherize:: Boolean option to determine whether or not element names should 690 # replace underscores with dashes (default is +false+).704 # replace underscores with dashes (default is <tt>false</tt>). 691 705 # skip_instruct:: Toggle skipping the +instruct!+ call on the XML builder 692 # that generates the XML declaration (default is +false+).706 # that generates the XML declaration (default is <tt>false</tt>). 693 707 # 694 708 # ==== Examples 695 709 # my_group = SubsidiaryGroup.find(:first) … … 769 783 alias_method :respond_to_without_attributes?, :respond_to? 770 784 771 785 # A method to determine if an object responds to a message (e.g., a method call). In Active Resource, a +Person+ object with a 772 # +name+ attribute can answer +true+ to +my_person.respond_to?("name")+, +my_person.respond_to?("name=")+, and773 # +my_person.respond_to?("name?")+.786 # +name+ attribute can answer <tt>true</tt> to <tt>my_person.respond_to?("name")</tt>, <tt>my_person.respond_to?("name=")</tt>, and 787 # <tt>my_person.respond_to?("name?")</tt>. 774 788 def respond_to?(method, include_priv = false) 775 789 method_name = method.to_s 776 790 if attributes.nil?