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

Changeset 6539

Show
Ignore:
Timestamp:
04/19/07 22:18:03 (1 year ago)
Author:
david
Message:

Disregard namespaces from the default element name, so Highrise::Person will just try to fetch from "/people", not "/highrise/people" [DHH] Added that saves which get a body response (and not just a 201) will use that response to update themselves [DHH] Fixed constant warning when fetching the same object multiple times [DHH]

Files:

Legend:

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

    r6379 r6539  
    11*SVN* 
     2 
     3* Fixed constant warning when fetching the same object multiple times [DHH] 
     4 
     5* Added that saves which get a body response (and not just a 201) will use that response to update themselves [DHH] 
     6 
     7* Disregard namespaces from the default element name, so Highrise::Person will just try to fetch from "/people", not "/highrise/people" [DHH] 
    28 
    39* Allow array and hash query parameters.  #7756 [Greg Spurrier] 
  • trunk/activeresource/lib/active_resource/base.rb

    r6379 r6539  
    3030      end 
    3131 
    32       attr_accessor_with_default(:element_name)    { to_s.underscore } #:nodoc: 
     32      # Do not include any modules in the default element name. This makes it easier to seclude ARes objects 
     33      # in a separate namespace without having to set element_name repeatedly. 
     34      attr_accessor_with_default(:element_name)    { to_s.split("::").last.underscore } #:nodoc: 
     35 
    3336      attr_accessor_with_default(:collection_name) { element_name.pluralize } #:nodoc: 
    3437      attr_accessor_with_default(:primary_key, 'id') #:nodoc: 
     
    150153    def initialize(attributes = {}, prefix_options = {}) 
    151154      @attributes = {} 
    152       self.load attributes 
     155      load(attributes) 
    153156      @prefix_options = prefix_options 
    154157    end 
     
    185188    end 
    186189 
    187     # Delegates to +create+ if a new object, +update+ if its old. 
     190    # Delegates to +create+ if a new object, +update+ if its old. If the response to the save includes a body, 
     191    # it will be assumed that this body is XML for the final object as it looked after the save (which would include 
     192    # attributes like created_at that wasn't part of the original submit). 
    188193    def save 
    189194      new? ? create : update 
     
    207212    # Reloads the attributes of this object from the remote web service. 
    208213    def reload 
    209       self.load self.class.find(id, @prefix_options).attributes 
     214      self.load(self.class.find(id, @prefix_options).attributes) 
    210215    end 
    211216 
     
    246251        returning connection.post(collection_path, to_xml) do |response| 
    247252          self.id = id_from_response(response) 
     253           
     254          if response['Content-size'] != "0" && response.body.strip.size > 0 
     255            load(connection.xml_from_response(response)) 
     256          end 
    248257        end 
    249258      end 
     
    271280      def find_or_create_resource_for(name) 
    272281        resource_name = name.to_s.camelize 
    273         resource_name.constantize 
     282        self.class.const_get(resource_name) 
    274283      rescue NameError 
    275284        resource = self.class.const_set(resource_name, Class.new(ActiveResource::Base)) 
  • trunk/activeresource/lib/active_resource/connection.rb

    r5967 r6539  
    5353    # Used to get (find) resources. 
    5454    def get(path) 
    55       from_xml_data(Hash.from_xml(request(:get, path, build_request_headers).body).values.first
     55      xml_from_response(request(:get, path, build_request_headers)
    5656    end 
    5757 
     
    7373      request(:post, path, body, build_request_headers) 
    7474    end 
     75 
     76    def xml_from_response(response) 
     77      if response = from_xml_data(Hash.from_xml(response.body)) 
     78        response.first 
     79      else 
     80        nil 
     81      end 
     82    end 
     83 
    7584 
    7685    private 
     
    139148                when Hash  then [ from_xml_data(data.values.first) ] 
    140149                when Array then from_xml_data(data.values.first) 
    141                 else       data 
     150                else       data.values.first 
    142151              end 
    143152            else 
  • trunk/activeresource/lib/active_resource/http_mock.rb

    r5962 r6539  
    1313        module_eval <<-EOE 
    1414          def #{method}(path, request_headers = {}, body = nil, status = 200, response_headers = {}) 
    15             @responses[Request.new(:#{method}, path, nil, request_headers)] = Response.new(body || {}, status, response_headers) 
     15            @responses[Request.new(:#{method}, path, nil, request_headers)] = Response.new(body || "", status, response_headers) 
    1616          end 
    1717        EOE 
  • trunk/activeresource/test/base_test.rb

    r6379 r6539  
    1717      mock.delete "/people/1.xml",             {}, nil, 200 
    1818      mock.delete "/people/2.xml",             {}, nil, 400 
    19       mock.post   "/people.xml",               {}, nil, 201, 'Location' => '/people/5.xml' 
    2019      mock.get    "/people/99.xml",            {}, nil, 404 
     20      mock.post   "/people.xml",               {}, "<person><name>Rick</name><age type='integer'>25</age></person>", 201, 'Location' => '/people/5.xml' 
    2121      mock.get    "/people.xml",               {}, "<people>#{@matz}#{@david}</people>" 
    2222      mock.get    "/people/1/addresses.xml",   {}, "<addresses>#{@addy}</addresses>" 
     
    129129  end 
    130130 
    131   def test_nested_element_name 
    132     self.class.const_set :Actor, Class.new(Person) 
    133     assert_equal 'base_test/actor', Actor.element_name 
    134   ensure 
    135     self.class.remove_const :Actor rescue nil 
    136   end 
    137  
    138  
    139131  def test_prefix 
    140132    assert_equal "/", Person.prefix 
     
    216208 
    217209  def test_create_with_custom_prefix 
    218     matzs_house = StreetAddress.new({}, {:person_id => 1}) 
     210    matzs_house = StreetAddress.new({}, { :person_id => 1 }) 
    219211    matzs_house.save 
    220212    assert_equal '5', matzs_house.id 
     
    233225    assert !rick.new? 
    234226    assert_equal '5', rick.id 
     227 
     228    # test additional attribute returned on create 
     229    assert_equal 25, rick.age 
    235230     
    236231    # Test that save exceptions get bubbled up too 
  • trunk/activeresource/test/connection_test.rb

    r5967 r6539  
    9393  def test_get_collection_empty 
    9494    people = @conn.get("/people_empty_elements.xml") 
    95     assert_equal people, nil 
     95    assert_nil people 
    9696  end 
    9797