refactor more logic into AuxTableConfig
This commit is contained in:
@@ -28,8 +28,14 @@ RSpec.describe HasAuxTable do
|
||||
before(:all) do
|
||||
# Set up the database schema for testing
|
||||
ActiveRecord::Schema.define do
|
||||
create_table :vehicle_lots do |t|
|
||||
t.string :name
|
||||
t.timestamps
|
||||
end
|
||||
|
||||
create_base_table :vehicles do |t|
|
||||
t.string :name
|
||||
t.references :vehicle_lot, foreign_key: { to_table: :vehicle_lots }
|
||||
t.timestamps
|
||||
|
||||
t.create_aux :car do |t|
|
||||
@@ -92,6 +98,11 @@ RSpec.describe HasAuxTable do
|
||||
|
||||
class Vehicle < ActiveRecord::Base
|
||||
include HasAuxTable
|
||||
belongs_to :vehicle_lot
|
||||
end
|
||||
|
||||
class VehicleLot < ActiveRecord::Base
|
||||
has_many :vehicles
|
||||
end
|
||||
|
||||
class Car < Vehicle
|
||||
@@ -191,16 +202,40 @@ RSpec.describe HasAuxTable do
|
||||
car =
|
||||
Car.create!(name: "Honda Civic", fuel_type: "gasoline", engine_size: 2.0)
|
||||
expect(car.attributes).to match(
|
||||
"type" => "Car",
|
||||
"id" => car.id,
|
||||
"name" => "Honda Civic",
|
||||
"fuel_type" => "gasoline",
|
||||
"engine_size" => be_within(0.001).of(2.0),
|
||||
"created_at" => be_within(0.001).of(car.created_at),
|
||||
"updated_at" => be_within(0.001).of(car.updated_at)
|
||||
hash_including(
|
||||
"type" => "Car",
|
||||
"id" => car.id,
|
||||
"name" => "Honda Civic",
|
||||
"fuel_type" => "gasoline",
|
||||
"engine_size" => be_within(0.001).of(2.0),
|
||||
"created_at" => be_within(0.001).of(car.created_at),
|
||||
"updated_at" => be_within(0.001).of(car.updated_at)
|
||||
)
|
||||
)
|
||||
end
|
||||
|
||||
it "can be created as the base class" do
|
||||
vehicle = Vehicle.create(type: "Vehicle", name: "big tractor")
|
||||
expect(vehicle.attributes).to match(
|
||||
hash_including(
|
||||
"type" => "Vehicle",
|
||||
"id" => vehicle.id,
|
||||
"name" => "big tractor",
|
||||
"created_at" => be_within(0.001).of(vehicle.created_at),
|
||||
"updated_at" => be_within(0.001).of(vehicle.updated_at)
|
||||
)
|
||||
)
|
||||
end
|
||||
|
||||
it "can be created through an association" do
|
||||
lot = VehicleLot.create(name: "lot1")
|
||||
lot.vehicles.create { |b| b.name = "vehicle1" }
|
||||
lot.save!
|
||||
lot.reload
|
||||
expect(lot.vehicles.count).to eq(1)
|
||||
expect(lot.vehicles.first.name).to eq("vehicle1")
|
||||
end
|
||||
|
||||
describe "database integration" do
|
||||
it "provides automatic attribute accessors for auxiliary table columns" do
|
||||
vehicle = Car.create!(name: "Honda Civic")
|
||||
|
||||
Reference in New Issue
Block a user