Skip to content

Commit

Permalink
work on geocoding
Browse files Browse the repository at this point in the history
  • Loading branch information
mikeheft committed Jul 5, 2024
1 parent 7b85015 commit 89304c8
Show file tree
Hide file tree
Showing 8 changed files with 58 additions and 21 deletions.
6 changes: 6 additions & 0 deletions .rubocop.yml
Original file line number Diff line number Diff line change
Expand Up @@ -22,10 +22,16 @@ Layout/ArgumentAlignment:
Layout/IndentationWidth:
Enabled: true
Width: 2
Layout/DotPosition:
EnforcedStyle: leading
Layout/EndAlignment:
EnforcedStyleAlignWith: variable
Layout/MultilineAssignmentLayout:
EnforcedStyle: same_line
Layout/MultilineMethodCallIndentation:
EnforcedStyle: indented
Style/MethodCallWithArgsParentheses:
EnforcedStyle: require_parentheses
Layout/SpaceInsideBlockBraces:
EnforcedStyle: space
Lint/UnusedMethodArgument:
Expand Down
10 changes: 2 additions & 8 deletions app/models/address.rb
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ class Address < ApplicationRecord
has_many :ride_origins, class_name: "Ride", foreign_key: "from_address_id", dependent: nil, inverse_of: :from_address
has_many :ride_destinations, class_name: "Ride", foreign_key: "to_address_id", dependent: nil, inverse_of: :to_address

validates :line_1, :city, :state, :zip_code, :place_id, :latitude, :longitude, presence: true
# validates :line_1, :city, :state, :zip_code, :place_id, :latitude, :longitude, presence: true
validates :place_id, uniqueness: true
validates :zip_code, uniqueness: { scope: %i[line_1 line_2] }

Expand All @@ -17,13 +17,7 @@ class Address < ApplicationRecord
end
end

before_validation :geocode,
if: ->(obj) {
obj.full_address.present? && %i[line_1 line_2 city state zip_code place_id latitude
longitude].any? do
obj.send("#{_1}_changed?")
end
}
before_validation :geocode

def full_address
[line_1, line_2, city, state, zip_code].compact.join(", ")
Expand Down
2 changes: 1 addition & 1 deletion app/models/driver.rb
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ class Driver < ApplicationRecord
inverse_of: :driver
has_one :current_address, through: :current_driver_address, source: :address, dependent: :destroy

validates :first_name, :last_name, presence: true
validates :first_name, :last_name, :current_address, presence: true

def origin_place_id
current_address.place_id
Expand Down
16 changes: 14 additions & 2 deletions app/models/ride.rb
Original file line number Diff line number Diff line change
Expand Up @@ -14,14 +14,26 @@ class Ride < ApplicationRecord
}

scope :by_address, ->(address_id) {
where(from_address_id: address_id).or(where(to_address_id: address_id))
}
where(from_address_id: address_id).or(where(to_address_id: address_id))
}

scope :selectable, -> {
includes(:from_address, :to_address)
.select(:id, :from_address_id, :to_address_id)
.where(driver_id: nil, duration: nil, distance: nil, commute_duration: nil, amount_cents: 0)
}

scope :nearby_driver, ->(driver) {
current_driver_address = driver.current_address
# Due to the manner in which Geocoder computes and orders this, we need to first
# get the addresses within the driver's desired radius and _then_ find the rides
# with corresponding from_address_id
addresses = Address.where.not(id: current_driver_address.id)
.near([current_driver_address.latitude, current_driver_address.longitude], driver.max_radius)
binding.pry
selectable.where(id: addresses.map(&:id))
}

def origin_place_id
from_address.place_id
end
Expand Down
8 changes: 8 additions & 0 deletions db/migrate/20240704202204_add_max_radius_to_driver.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
# frozen_string_literal: true

class AddMaxRadiusToDriver < ActiveRecord::Migration[7.1]
def change
add_column :drivers, :max_radius, :integer, default: 10
add_index :drivers, :max_radius
end
end
10 changes: 3 additions & 7 deletions db/seeds.rb
Original file line number Diff line number Diff line change
Expand Up @@ -17,14 +17,10 @@
3.times do
first_name = Faker::Name.first_name
last_name = Faker::Name.last_name
Driver.create!(first_name:,last_name:)
end

# Create home address for driver
puts "Creating current Addresses for Drivers..."
Driver.find_each.with_index do |driver, index|
address = Address.create(ADDRESSES[index])
driver = Driver.build(first_name:,last_name:)
address = Address.create(ADDRESSES.sample)
driver.create_current_driver_address(address:, current: true)
driver.save!
end

puts "Creating remaining Addresses..."
Expand Down
6 changes: 3 additions & 3 deletions spec/factories/addresses.rb
Original file line number Diff line number Diff line change
Expand Up @@ -7,9 +7,9 @@
city { Faker::Address.city }
state { Faker::Address.state }
zip_code { Faker::Address.zip_code }
place_id { Faker::Internet.unique.device_token }
latitude { Faker::Address.latitude }
longitude { Faker::Address.longitude }
place_id { nil }
latitude { nil }
longitude { nil }

trait :with_out_place_id do
place_id { nil }
Expand Down
21 changes: 21 additions & 0 deletions spec/models/ride_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -14,4 +14,25 @@
it { is_expected.to monetize(:amount_cents).as(:amount) }
it { is_expected.to validate_numericality_of(:amount_cents) }
end

describe "scopes" do
it ".nearby_drivers" do
VCR.use_cassette("nearby_drivers") do
from_address = create(
:address, line_1: "4705 Weitzel Street", city: "Timnath", state: "CO",
place_id: "ChIJ0zbP73SzbocR7mXVIY-QdBM",
zip_code: "80547"
)
to_address = create(
:address, :with_out_place_id, line_1: "151 N College Ave", city: "Fort Collins", state: "CO",
place_id: "ChIJlRJnwIpKaYcRYzG0fYZOwpY",
zip_code: "80524"
)
create_list(:ride, 2, from_address:, to_address:)
driver = create(:driver, current_address: to_address)
rides = Ride.selectable.nearby_driver(driver)
binding.pry
end
end
end
end

0 comments on commit 89304c8

Please sign in to comment.