Ruby on Rails | Screencasts | Download | Documentation | Weblog | Community | Source

Changeset 6595

Show
Ignore:
Timestamp:
04/27/07 20:54:53 (1 year ago)
Author:
david
Message:

Added find-by-path options to ActiveResource::Base.find [DHH]

Files:

Legend:

Unmodified
Added
Removed
Modified
Copied
Moved
  • trunk/activeresource/CHANGELOG

    r6587 r6595  
    11*SVN* 
     2 
     3* Added find-by-path options to ActiveResource::Base.find [DHH]. Examples: 
     4 
     5    employees = Person.find(:all, :from => "/companies/1/people.xml") # => GET /companies/1/people.xml 
     6    manager   = Person.find("/companies/1/manager.xml")               # => GET /companies/1/manager.xml 
     7 
    28 
    39* Added support for using classes from within a single nested module [DHH]. Example: 
  • trunk/activeresource/lib/active_resource/base.rb

    r6587 r6595  
    114114 
    115115      # Core method for finding resources.  Used similarly to Active Record's find method. 
    116       #  Person.find(1)                     # => GET /people/1.xml 
    117       #  Person.find(:all)                  # => GET /people.xml 
    118       #  Person.find(:all, :title => "CEO") # => GET /people.xml?title=CEO 
    119       #  Person.find(:managers)             # => GET /people/managers.xml 
    120       #  StreetAddress.find(1, :person_id => 1) # => GET /people/1/street_addresses/1.xml 
     116      #  Person.find(1)                                        # => GET /people/1.xml 
     117      #  Person.find(:all)                                     # => GET /people.xml 
     118      #  Person.find(:all, :title => "CEO")                    # => GET /people.xml?title=CEO 
     119      #  Person.find(:managers)                                # => GET /people/managers.xml 
     120      #  Person.find(:all, :from => "/companies/1/people.xml") # => GET /companies/1/people.xml 
     121      #  Person.find("/companies/1/manager.xml")               # => GET /companies/1/manager.xml 
     122      #  StreetAddress.find(1, :person_id => 1)                # => GET /people/1/street_addresses/1.xml 
    121123      def find(*arguments) 
    122124        scope   = arguments.slice!(0) 
     
    145147        # Find every resource. 
    146148        def find_every(options) 
     149          from = options.delete(:from) 
    147150          prefix_options, query_options = split_options(options) 
    148           instantiate_collection(connection.get(collection_path(prefix_options, query_options)) || []) 
     151          from ||= collection_path(prefix_options, query_options) 
     152 
     153          instantiate_collection(connection.get(from) || []) 
    149154        end 
    150155         
     
    161166        def find_single(scope, options) 
    162167          prefix_options, query_options = split_options(options) 
    163           returning new(connection.get(element_path(scope, prefix_options, query_options))) do |resource| 
     168          from = scope.to_s.include?("/") ? scope : element_path(scope, prefix_options, query_options) 
     169 
     170          returning new(connection.get(from)) do |resource| 
    164171            resource.prefix_options = prefix_options 
    165172          end 
  • trunk/activeresource/test/base_test.rb

    r6584 r6595  
    204204  end 
    205205 
     206  def test_find_all_by_from 
     207    ActiveResource::HttpMock.respond_to { |m| m.get "/companies/1/people.xml", {}, "<people>#{@david}</people>" } 
     208   
     209    people = Person.find(:all, :from => "/companies/1/people.xml") 
     210    assert_equal 1, people.size 
     211    assert_equal "David", people.first.name 
     212  end 
     213 
     214  def test_find_single_by_from 
     215    ActiveResource::HttpMock.respond_to { |m| m.get "/companies/1/manager.xml", {}, @david } 
     216 
     217    david = Person.find("/companies/1/manager.xml") 
     218    assert_equal "David", david.name 
     219  end 
     220 
    206221  def test_save 
    207222    rick = Person.new