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

Ticket #7543: load_nils.diff

File load_nils.diff, 1.6 kB (added by sneakin, 2 years ago)

Lets ActiveResource "load" nils

  • test/base/load_test.rb

    old new  
    2121  end 
    2222 
    2323  def test_load_expects_hash 
    24     assert_raise(ArgumentError) { @person.load nil } 
     24    assert_nothing_raised(ArgumentError) { @person.load nil } 
    2525    assert_raise(ArgumentError) { @person.load '<person id="1"/>' } 
    2626  end 
    2727 
  • lib/active_resource/base.rb

    old new  
    228228    # Manually load attributes from a hash. Recursively loads collections of 
    229229    # resources. 
    230230    def load(attributes) 
    231       raise ArgumentError, "expected an attributes Hash, got #{attributes.inspect}" unless attributes.is_a?(Hash) 
    232       attributes.each do |key, value| 
    233         @attributes[key.to_s] = 
    234           case value 
     231      if attributes 
     232        raise ArgumentError, "expected an attributes Hash, got #{attributes.inspect}" unless attributes.is_a?(Hash) 
     233        attributes.each do |key, value| 
     234          @attributes[key.to_s] = 
     235            case value 
    235236            when Array 
    236237              resource = find_or_create_resource_for_collection(key) 
    237238              value.map { |attrs| resource.new(attrs) } 
     
    242243              value.class.new(value.attributes, value.prefix_options) 
    243244            else 
    244245              value.dup rescue value 
    245           end 
     246            end 
     247        end 
    246248      end 
     249       
    247250      self 
    248251    end 
    249252