Skip to content

Instantly share code, notes, and snippets.

@Epigene
Created January 29, 2021 14:14
Show Gist options
  • Save Epigene/8fb4b97541539d2860446e2ef78c511b to your computer and use it in GitHub Desktop.
Save Epigene/8fb4b97541539d2860446e2ef78c511b to your computer and use it in GitHub Desktop.
describe "#current_agreement_vehicle assoc", :slow, travel_to: "2021-01-01" do
let!(:vehicle1) do
Timecop.freeze(1.day.ago) { create(:vehicle) }
end
let!(:vehicle2) do
Timecop.freeze(2.days.ago) { create(:vehicle) }
end
let(:av1_2) do
Timecop.freeze(18.seconds.ago) do
create(:agreement_vehicle, owner: create(:admin), vehicle: vehicle1, state: Vehicle::STATE_ACTIVE_LOAN)
end
end
let(:av2_2) do
Timecop.freeze(10.seconds.ago) do
create(:agreement_vehicle, owner: create(:admin), vehicle: vehicle2, state: Vehicle::STATE_REGISTERED)
end
end
# This produces a 2x2 matrix of agreement_vehicle records
# veh1 old = "registered"
# veh1 new = "active"
# veh2 old = "active"
# veh2 new = "registered"
#
# So if we query for "latest" that are active only veh1 should get selected
before do
vehicle2.agreement_vehicles.update_all(state: Vehicle::STATE_ACTIVE_LOAN)
av1_2
av2_2
end
it "returns the correct association when used as a method, and correcly joins" do
expect(vehicle1.reload.latest_agreement_vehicle).to eq(av1_2)
expect(vehicle2.reload.latest_agreement_vehicle).to eq(av2_2)
vehicles_with_currently_active_state = Vehicle.
joins(:latest_agreement_vehicle).
merge(AgreementVehicle.where(state: Vehicle::STATE_ACTIVE_LOAN))
expect(vehicles_with_currently_active_state).to(
contain(vehicle1).
and exclude(vehicle2)
)
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment