Skip to content

Commit

Permalink
Work on relationship to allow for to/from addresses on rides
Browse files Browse the repository at this point in the history
  • Loading branch information
mikeheft committed Jul 2, 2024
1 parent 9cfad83 commit 7d95926
Show file tree
Hide file tree
Showing 13 changed files with 88 additions and 19 deletions.
6 changes: 4 additions & 2 deletions app/models/address.rb
Original file line number Diff line number Diff line change
@@ -1,7 +1,9 @@
# frozen_string_literal: true

class Address < ApplicationRecord
belongs_to :owner, polymorphic: true, optional: false
has_many :driver_addresses, dependent: :destroy
has_one :current_driver_address, -> { where(current: true) }, class_name: "DriverAddress", dependent: :destroy
has_one :current_driver, through: :current_driver_address, source: :address, dependent: :destroy

validates :line_1, :city, :state, :zip_code, :latitude, :longitude, presence: true
validates :line_1, :city, :state, :zip_code, :place_id, :latitude, :longitude, presence: true
end
4 changes: 3 additions & 1 deletion app/models/driver.rb
Original file line number Diff line number Diff line change
@@ -1,7 +1,9 @@
# frozen_string_literal: true

class Driver < ApplicationRecord
has_one :address, as: :owner, dependent: :destroy
has_many :driver_addresses, dependent: :destroy
has_one :current_driver_address, -> { where(current: true) }, class_name: "DriverAddress", dependent: :destroy
has_one :current_address, through: :current_driver_address, source: :driver, dependent: :destroy

validates :first_name, :last_name, presence: true
end
9 changes: 9 additions & 0 deletions app/models/driver_address.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
class DriverAddress < ApplicationRecord
belongs_to :driver
belongs_to :address

validates :current,
if: -> { current },
uniqueness: { scope: :driver_id, message: "can only have one current Driver" },
inclusion: { in: [true, false] }
end
2 changes: 2 additions & 0 deletions app/models/ride.rb
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@

class Ride < ApplicationRecord
belongs_to :driver, optional: true
belongs_to :from_address, class_name: "Address"
belongs_to :to_address, class_name: "Address"

validates :duration, :distance, :commute_duration, :amount_cents, presence: true
monetize :amount_cents,
Expand Down
1 change: 0 additions & 1 deletion db/migrate/20240702182238_create_addresses.rb
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,6 @@ def change
t.float :latitude, null: false
t.float :longitude, null: false
t.string :place_id, null: true, index: true
t.references :owner, polymorphic: true, null: false, index: true

t.timestamps
end
Expand Down
2 changes: 2 additions & 0 deletions db/migrate/20240702193305_create_rides.rb
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,8 @@ def change
t.float :commute_duration, index: true, null: false
t.monetize :amount
t.references :driver, null: true, index: true
t.references :from_address, foreign_key: {to_table: :addresses}, index: true, null: false
t.references :to_address, foreign_key: {to_table: :addresses}, index: true, null: false
t.timestamps
end
end
Expand Down
11 changes: 11 additions & 0 deletions db/migrate/20240702210002_create_driver_addresses.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
class CreateDriverAddresses < ActiveRecord::Migration[7.1]
def change
create_table :driver_addresses do |t|
t.boolean :current, null: false, default: false
t.references :driver, null: false, foreign_key: true
t.references :address, null: false, foreign_key: true

t.timestamps
end
end
end
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
# frozen_string_literal: true

class AddUniqIndexToDriverAddresses < ActiveRecord::Migration[7.1]
def change
add_index :driver_addresses, %i[current driver_id], unique: true, where: "(current IS TRUE)"
end
end
40 changes: 28 additions & 12 deletions db/schema.rb

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 0 additions & 1 deletion spec/factories/addresses.rb
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,5 @@
place_id { Faker::Internet.unique.device_token }
latitude { Faker::Address.latitude }
longitude { Faker::Address.longitude }
association :owner, factory: [:driver]
end
end
1 change: 0 additions & 1 deletion spec/factories/drivers.rb
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,5 @@
factory :driver do
first_name { Faker::Name.first_name }
last_name { Faker::Name.last_name }
association :address, factory: :address
end
end
15 changes: 15 additions & 0 deletions spec/factories/rides.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
# frozen_string_literal: true

FactoryBot.define do
factory :ride do
duration { 2.3 }
commute_duration { 1.0 }
distance { 30.1 }
amount_cents { 1200 }

driver { nil }

association :from_address, factory: :address
association :to_address, factory: :address
end
end
8 changes: 7 additions & 1 deletion spec/models/address_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -3,11 +3,17 @@
require "rails_helper"

RSpec.describe Address, type: :model do
it { is_expected.to belong_to(:owner) }
it { is_expected.to have_one(:current_driver) }

it { is_expected.to validate_presence_of(:line_1) }
it { is_expected.to validate_presence_of(:city) }
it { is_expected.to validate_presence_of(:state) }
it { is_expected.to validate_presence_of(:zip_code) }
it { is_expected.to validate_presence_of(:latitude) }
it { is_expected.to validate_presence_of(:longitude) }

describe "instance_methods" do
it "can query for associated rides" do
end
end
end

0 comments on commit 7d95926

Please sign in to comment.