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

Ticket #10537: add_new_record_method_to_ares.diff

File add_new_record_method_to_ares.diff, 2.0 kB (added by EmmanuelOga, 8 months ago)

Patch to add new_record? method as an alias for new? for compatibility with ActiveRecord and (fixes a form_for issue with ARes)

  • test/base_test.rb

    old new  
    301301    assert_equal true, rick.save 
    302302    assert_equal '5', rick.id 
    303303  end 
     304   
     305  def test_new_record 
     306    rick = Person.new 
     307     
     308    # new_record? is an alias for new? to improve compatibility with ActionRecord 
     309    assert rick.new? && (rick.new? == rick.new_record?) 
     310     
     311    assert_equal true, rick.save 
     312    assert !rick.new? && (rick.new? == rick.new_record?) 
     313  end   
    304314 
    305315  def test_id_from_response 
    306316    p = Person.new 
     
    350360    end     
    351361    assert_raises(ActiveResource::ResourceConflict) { Person.create(:name => 'Rick') } 
    352362  end 
    353  
     363   
    354364  def test_update 
    355365    matz = Person.find(:first) 
    356366    matz.name = "David" 
  • lib/active_resource/base.rb

    old new  
    553553    def new? 
    554554      id.nil? 
    555555    end 
     556     
     557    # Alias for +new?+ 
     558    # 
     559    # This method is added for compatibility with ActiveRecord. 
     560    def new_record? 
     561      new? 
     562    end 
    556563 
    557564    # Get the +id+ attribute of the resource. 
    558565    def id 
  • README

    old new  
    123123  ryan.new?  #=> false 
    124124  ryan.id    #=> 2 
    125125 
     126In the previous example, +new?+ is used to verify the state of the record before and after saving it. 
     127The same could be accomplished by using the +new_record?+ method instead. 
     128+new_record?+ is an alias for +new?+ included for compatibility with ActiveRecord. 
     129 
     130  ryan = Person.new(:first => 'Ryan') 
     131  ryan.new_record?  #=> true 
     132  ryan.save         #=> true 
     133  ryan.new_record?  #=> false 
     134  ryan.id           #=> 2 
     135 
    126136==== Update 
    127137 
    128138'save' is also used to update an existing resource - and follows the same protocol as creating a resource