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

Ticket #5780: collection_singular_ids.2.diff

File collection_singular_ids.2.diff, 3.0 kB (added by mschuerig, 3 years ago)

singular_ids and unit tests

  • test/associations_test.rb

    old new  
    934934    assert firm.clients.include?(Client.find_by_name("New Client")) 
    935935  end 
    936936   
     937  def test_get_ids 
     938    assert_equal [companies(:first_client).id, companies(:second_client).id], companies(:first_firm).client_ids 
     939  end 
     940   
    937941  def test_assign_ids 
    938942    firm = Firm.new("name" => "Apple") 
    939943    firm.client_ids = [companies(:first_client).id, companies(:second_client).id] 
     
    942946    assert_equal 2, firm.clients.length 
    943947    assert firm.clients.include?(companies(:second_client)) 
    944948  end 
     949 
     950  def test_assign_ids_ignoring_blanks 
     951    firm = Firm.new("name" => "Apple") 
     952    firm.client_ids = [companies(:first_client).id, nil, companies(:second_client).id, ''] 
     953    firm.save 
     954    firm.reload 
     955    assert_equal 2, firm.clients.length 
     956    assert firm.clients.include?(companies(:second_client)) 
     957  end 
     958 
    945959end 
    946960 
    947961class BelongsToAssociationsTest < Test::Unit::TestCase 
     
    17351749 
    17361750    assert_equal 3, Developer.find(:all, :include => {:projects => :developers}, :conditions => 'developers_projects_join.joined_on IS NOT NULL', :group => group.join(",")).size 
    17371751  end 
     1752 
     1753  def test_get_ids 
     1754    assert_equal [projects(:active_record).id, projects(:action_controller).id], developers(:david).project_ids 
     1755    assert_equal [projects(:active_record).id], developers(:jamis).project_ids 
     1756  end 
     1757 
     1758  def test_assign_ids 
     1759    developer = Developer.new("name" => "Joe") 
     1760    developer.project_ids = [projects(:active_record).id, projects(:action_controller).id] 
     1761    developer.save 
     1762    developer.reload 
     1763    assert_equal 2, developer.projects.length 
     1764    assert_equal projects(:active_record), developer.projects[0]  
     1765    assert_equal projects(:action_controller), developer.projects[1]  
     1766  end 
     1767 
     1768  def test_assign_ids_ignoring_blanks 
     1769    developer = Developer.new("name" => "Joe") 
     1770    developer.project_ids = [projects(:active_record).id, nil, projects(:action_controller).id, ''] 
     1771    developer.save 
     1772    developer.reload 
     1773    assert_equal 2, developer.projects.length 
     1774    assert_equal projects(:active_record), developer.projects[0]  
     1775    assert_equal projects(:action_controller), developer.projects[1]  
     1776  end 
    17381777end 
  • lib/active_record/associations.rb

    old new  
    916916            association 
    917917          end 
    918918 
     919          define_method("#{reflection.name.to_s.singularize}_ids") do 
     920            send(reflection.name).map(&:id) 
     921          end 
     922 
    919923          define_method("#{reflection.name.to_s.singularize}_ids=") do |new_value| 
    920             send("#{reflection.name}=", reflection.class_name.constantize.find(new_value)) 
     924            ids = (new_value || []).reject { |nid| nid.blank? } 
     925            send("#{reflection.name}=", reflection.class_name.constantize.find(ids)) 
    921926          end 
    922927        end 
    923928