diff --git a/.rubocop.yml b/.rubocop.yml index d1abbf4..397c700 100644 --- a/.rubocop.yml +++ b/.rubocop.yml @@ -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: diff --git a/app/models/address.rb b/app/models/address.rb index 7bfc420..449303e 100644 --- a/app/models/address.rb +++ b/app/models/address.rb @@ -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] } @@ -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(", ") diff --git a/app/models/driver.rb b/app/models/driver.rb index 145f47a..3b362ab 100644 --- a/app/models/driver.rb +++ b/app/models/driver.rb @@ -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 diff --git a/app/models/ride.rb b/app/models/ride.rb index fda9e66..4033d64 100644 --- a/app/models/ride.rb +++ b/app/models/ride.rb @@ -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 diff --git a/db/migrate/20240704202204_add_max_radius_to_driver.rb b/db/migrate/20240704202204_add_max_radius_to_driver.rb new file mode 100644 index 0000000..a91792b --- /dev/null +++ b/db/migrate/20240704202204_add_max_radius_to_driver.rb @@ -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 diff --git a/db/seeds.rb b/db/seeds.rb index 15741d2..e0c778a 100644 --- a/db/seeds.rb +++ b/db/seeds.rb @@ -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..." diff --git a/spec/factories/addresses.rb b/spec/factories/addresses.rb index b3a179d..bef385a 100644 --- a/spec/factories/addresses.rb +++ b/spec/factories/addresses.rb @@ -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 } diff --git a/spec/models/ride_spec.rb b/spec/models/ride_spec.rb index 7d42a82..b760c4e 100644 --- a/spec/models/ride_spec.rb +++ b/spec/models/ride_spec.rb @@ -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