-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Work on relationship to allow for to/from addresses on rides
- Loading branch information
Showing
13 changed files
with
88 additions
and
19 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
7 changes: 7 additions & 0 deletions
7
db/migrate/20240702210854_add_uniq_index_to_driver_addresses.rb
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters