class TestDefaultAutosaveAssociationOnAHasManyAssociation

Public Instance Methods

test_adding_before_save() click to toggle source
# File activerecord/test/cases/autosave_association_test.rb, line 558
def test_adding_before_save
  no_of_firms = Firm.count
  no_of_clients = Client.count

  new_firm = Firm.new("name" => "A New Firm, Inc")
  c = Client.new("name" => "Apple")

  new_firm.clients_of_firm.push Client.new("name" => "Natural Company")
  assert_equal 1, new_firm.clients_of_firm.size
  new_firm.clients_of_firm << c
  assert_equal 2, new_firm.clients_of_firm.size

  assert_equal no_of_firms, Firm.count      # Firm was not saved to database.
  assert_equal no_of_clients, Client.count  # Clients were not saved to database.
  assert new_firm.save
  assert new_firm.persisted?
  assert c.persisted?
  assert_equal new_firm, c.firm
  assert_equal no_of_firms + 1, Firm.count      # Firm was saved to database.
  assert_equal no_of_clients + 2, Client.count  # Clients were saved to database.

  assert_equal 2, new_firm.clients_of_firm.size
  assert_equal 2, new_firm.clients_of_firm.reload.size
end
test_assign_ids() click to toggle source
# File activerecord/test/cases/autosave_association_test.rb, line 583
def test_assign_ids
  firm = Firm.new("name" => "Apple")
  firm.client_ids = [companies(:first_client).id, companies(:second_client).id]
  firm.save
  firm.reload
  assert_equal 2, firm.clients.length
  assert_includes firm.clients, companies(:second_client)
end
test_assign_ids_for_through_a_belongs_to() click to toggle source
# File activerecord/test/cases/autosave_association_test.rb, line 592
def test_assign_ids_for_through_a_belongs_to
  firm = Firm.new("name" => "Apple")
  firm.developer_ids = [developers(:david).id, developers(:jamis).id]
  firm.save
  firm.reload
  assert_equal 2, firm.developers.length
  assert_includes firm.developers, developers(:david)
end
test_build_before_save() click to toggle source
# File activerecord/test/cases/autosave_association_test.rb, line 601
def test_build_before_save
  company = companies(:first_firm)
  new_client = assert_no_queries(ignore_none: false) { company.clients_of_firm.build("name" => "Another Client") }
  assert !company.clients_of_firm.loaded?

  company.name += "-changed"
  assert_queries(2) { assert company.save }
  assert new_client.persisted?
  assert_equal 3, company.clients_of_firm.reload.size
end
test_build_many_before_save() click to toggle source
# File activerecord/test/cases/autosave_association_test.rb, line 612
def test_build_many_before_save
  company = companies(:first_firm)
  assert_no_queries(ignore_none: false) { company.clients_of_firm.build([{ "name" => "Another Client" }, { "name" => "Another Client II" }]) }

  company.name += "-changed"
  assert_queries(3) { assert company.save }
  assert_equal 4, company.clients_of_firm.reload.size
end
test_build_many_via_block_before_save() click to toggle source
# File activerecord/test/cases/autosave_association_test.rb, line 632
def test_build_many_via_block_before_save
  company = companies(:first_firm)
  assert_no_queries(ignore_none: false) do
    company.clients_of_firm.build([{ "name" => "Another Client" }, { "name" => "Another Client II" }]) do |client|
      client.name = "changed"
    end
  end

  company.name += "-changed"
  assert_queries(3) { assert company.save }
  assert_equal 4, company.clients_of_firm.reload.size
end
test_build_via_block_before_save() click to toggle source
# File activerecord/test/cases/autosave_association_test.rb, line 621
def test_build_via_block_before_save
  company = companies(:first_firm)
  new_client = assert_no_queries(ignore_none: false) { company.clients_of_firm.build { |client| client.name = "Another Client" } }
  assert !company.clients_of_firm.loaded?

  company.name += "-changed"
  assert_queries(2) { assert company.save }
  assert new_client.persisted?
  assert_equal 3, company.clients_of_firm.reload.size
end
test_invalid_adding() click to toggle source
# File activerecord/test/cases/autosave_association_test.rb, line 500
def test_invalid_adding
  firm = Firm.find(1)
  assert !(firm.clients_of_firm << c = Client.new)
  assert !c.persisted?
  assert !firm.valid?
  assert !firm.save
  assert !c.persisted?
end
test_invalid_adding_before_save() click to toggle source
# File activerecord/test/cases/autosave_association_test.rb, line 509
def test_invalid_adding_before_save
  new_firm = Firm.new("name" => "A New Firm, Inc")
  new_firm.clients_of_firm.concat([c = Client.new, Client.new("name" => "Apple")])
  assert !c.persisted?
  assert !c.valid?
  assert !new_firm.valid?
  assert !new_firm.save
  assert !c.persisted?
  assert !new_firm.persisted?
end
test_invalid_adding_with_validate_false() click to toggle source
# File activerecord/test/cases/autosave_association_test.rb, line 520
def test_invalid_adding_with_validate_false
  firm = Firm.first
  client = Client.new
  firm.unvalidated_clients_of_firm << client

  assert firm.valid?
  assert !client.valid?
  assert firm.save
  assert !client.persisted?
end
test_invalid_build() click to toggle source
# File activerecord/test/cases/autosave_association_test.rb, line 548
def test_invalid_build
  new_client = companies(:first_firm).clients_of_firm.build
  assert !new_client.persisted?
  assert !new_client.valid?
  assert_equal new_client, companies(:first_firm).clients_of_firm.last
  assert !companies(:first_firm).save
  assert !new_client.persisted?
  assert_equal 2, companies(:first_firm).clients_of_firm.reload.size
end
test_replace_on_new_object() click to toggle source
# File activerecord/test/cases/autosave_association_test.rb, line 645
def test_replace_on_new_object
  firm = Firm.new("name" => "New Firm")
  firm.clients = [companies(:second_client), Client.new("name" => "New Client")]
  assert firm.save
  firm.reload
  assert_equal 2, firm.clients.length
  assert_includes firm.clients, Client.find_by_name("New Client")
end
test_valid_adding_with_validate_false() click to toggle source
# File activerecord/test/cases/autosave_association_test.rb, line 531
def test_valid_adding_with_validate_false
  no_of_clients = Client.count

  firm = Firm.first
  client = Client.new("name" => "Apple")

  assert firm.valid?
  assert client.valid?
  assert !client.persisted?

  firm.unvalidated_clients_of_firm << client

  assert firm.save
  assert client.persisted?
  assert_equal no_of_clients + 1, Client.count
end