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

Changeset 8502

Show
Ignore:
Timestamp:
12/28/07 17:03:58 (2 years ago)
Author:
rick
Message:

Support agnostic formats when calling custom methods. Closes #10635 [joerichsen]

Files:

Legend:

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

    r8472 r8502  
    11*SVN* 
     2 
     3* Support agnostic formats when calling custom methods.  Closes #10635 [joerichsen] 
    24 
    35* Document custom methods.  #10589 [Cheah Chu Yeow] 
  • trunk/activeresource/lib/active_resource.rb

    r7518 r8502  
    2828  begin 
    2929    $:.unshift(File.dirname(__FILE__) + "/../../activesupport/lib")   
    30     require 'active_support'   
     30    require 'active_support' 
    3131  rescue LoadError 
    3232    require 'rubygems' 
  • trunk/activeresource/lib/active_resource/custom_methods.rb

    r8472 r8502  
    8181      def custom_method_collection_url(method_name, options = {}) 
    8282        prefix_options, query_options = split_options(options) 
    83         "#{prefix(prefix_options)}#{collection_name}/#{method_name}.xml#{query_string(query_options)}" 
     83        "#{prefix(prefix_options)}#{collection_name}/#{method_name}.#{format.extension}#{query_string(query_options)}" 
    8484      end 
    8585    end 
     
    109109      private 
    110110        def custom_method_element_url(method_name, options = {}) 
    111           "#{self.class.prefix(prefix_options)}#{self.class.collection_name}/#{id}/#{method_name}.xml#{self.class.send!(:query_string, options)}" 
     111          "#{self.class.prefix(prefix_options)}#{self.class.collection_name}/#{id}/#{method_name}.#{self.class.format.extension}#{self.class.send!(:query_string, options)}" 
    112112        end 
    113113       
    114114        def custom_method_new_element_url(method_name, options = {}) 
    115           "#{self.class.prefix(prefix_options)}#{self.class.collection_name}/new/#{method_name}.xml#{self.class.send!(:query_string, options)}" 
     115          "#{self.class.prefix(prefix_options)}#{self.class.collection_name}/new/#{method_name}.#{self.class.format.extension}#{self.class.send!(:query_string, options)}" 
    116116        end 
    117117    end 
  • trunk/activeresource/test/format_test.rb

    r7518 r8502  
    3030  end 
    3131 
     32  def test_formats_on_custom_collection_method 
     33    for format in [ :json, :xml ] 
     34      using_format(Person, format) do 
     35        ActiveResource::HttpMock.respond_to.get "/people/retrieve.#{format}?name=David", {}, ActiveResource::Formats[format].encode([@david]) 
     36        remote_programmers = Person.get(:retrieve, :name => 'David') 
     37        assert_equal 1, remote_programmers.size 
     38        assert_equal @david[:id], remote_programmers[0]['id'] 
     39        assert_equal @david[:name], remote_programmers[0]['name'] 
     40      end 
     41    end 
     42  end 
     43   
     44  def test_formats_on_custom_element_method 
     45    for format in [ :json, :xml ] 
     46      using_format(Person, format) do 
     47        ActiveResource::HttpMock.respond_to do |mock| 
     48          mock.get "/people/2.#{format}", {}, ActiveResource::Formats[format].encode(@david) 
     49          mock.get "/people/2/shallow.#{format}", {}, ActiveResource::Formats[format].encode(@david) 
     50        end 
     51        remote_programmer = Person.find(2).get(:shallow) 
     52        assert_equal @david[:id], remote_programmer['id'] 
     53        assert_equal @david[:name], remote_programmer['name'] 
     54      end 
     55    end 
     56 
     57    for format in [ :json, :xml ] 
     58      ryan = ActiveResource::Formats[format].encode({ :name => 'Ryan' }) 
     59      using_format(Person, format) do 
     60        ActiveResource::HttpMock.respond_to.post "/people/new/register.#{format}", {}, ryan, 201, 'Location' => "/people/5.#{format}" 
     61        remote_ryan = Person.new(:name => 'Ryan') 
     62        assert_equal ActiveResource::Response.new(ryan, 201, {'Location' => "/people/5.#{format}"}), remote_ryan.post(:register) 
     63      end 
     64    end 
     65  end 
    3266   
    3367  private