changed, changed_attributes

This commit is contained in:
Dylan Knutson
2025-07-18 17:49:26 +00:00
parent 5cdede91ec
commit ea33ffbb11
2 changed files with 60 additions and 0 deletions

View File

@@ -99,6 +99,47 @@ RSpec.describe HasAuxTable do
expect(lot.vehicles.first.name).to eq("vehicle1")
end
describe "#changed?" do
it "returns true if the main record changes" do
car = Car.create!(name: "Honda Civic")
expect(car.changed?).to be_falsey
car.name = "Toyota Camry"
expect(car.changed?).to be_truthy
end
it "returns true if the aux record changes" do
car = Car.create!(name: "Honda Civic")
expect(car.changed?).to be_falsey
car.fuel_type = "hybrid"
expect(car.changed?).to be_truthy
end
end
describe "#changed_attributes" do
# changed_attributes returns a hash with the original values of the attribute
it "returns the changed attributes of the main record" do
car = Car.create!(name: "Honda Civic")
expect(car.changed_attributes).to eq({})
car.name = "Toyota Camry"
expect(car.changed_attributes).to eq({ "name" => "Honda Civic" })
end
it "returns the changed attributes of the aux record when original is nil" do
car = Car.create!(name: "Honda Civic")
expect(car.changed_attributes).to eq({})
car.fuel_type = "hybrid"
expect(car.changed_attributes).to eq({ "fuel_type" => nil })
end
it "returns the changed attributes of the aux record when original is not nil" do
car = Car.create!(name: "Honda Civic", fuel_type: "gasoline")
expect(car.changed_attributes).to eq({})
car.fuel_type = "hybrid"
expect(car.changed_attributes).to eq({ "fuel_type" => "gasoline" })
end
end
describe "database integration" do
it "provides automatic attribute accessors for auxiliary table columns" do
vehicle = Car.create!(name: "Honda Civic")