From 5a042db7aec136a71890b5538e1916b9fb9db6f5 Mon Sep 17 00:00:00 2001 From: Mike Heft Date: Tue, 2 Jul 2024 16:56:49 -0600 Subject: [PATCH] Add specs --- .rubocop.yml | 2 ++ app/models/address.rb | 6 ++++++ app/models/ride.rb | 4 ++++ spec/models/address_spec.rb | 6 ++++++ 4 files changed, 18 insertions(+) diff --git a/.rubocop.yml b/.rubocop.yml index 31ec9a4..a8841c9 100644 --- a/.rubocop.yml +++ b/.rubocop.yml @@ -40,6 +40,8 @@ Metrics/PerceivedComplexity: - "lib/rubocop/cop/custom/*.rb" Naming/VariableNumber: EnforcedStyle: snake_case +Rails/InverseOf: + Enabled: false Style/AccessModifierDeclarations: EnforcedStyle: inline Style/Documentation: diff --git a/app/models/address.rb b/app/models/address.rb index b2523c5..80eb353 100644 --- a/app/models/address.rb +++ b/app/models/address.rb @@ -2,6 +2,12 @@ class Address < ApplicationRecord has_many :driver_addresses, dependent: :destroy + 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 + + def rides + Ride.by_address(id) + end end diff --git a/app/models/ride.rb b/app/models/ride.rb index 618ab04..eea732c 100644 --- a/app/models/ride.rb +++ b/app/models/ride.rb @@ -12,4 +12,8 @@ class Ride < ApplicationRecord numericality: { greater_than_or_equal_to: 0 } + + scope :by_address, ->(address_id) { + where(from_address_id: address_id).or(where(to_address_id: address_id)) + } end diff --git a/spec/models/address_spec.rb b/spec/models/address_spec.rb index 344db8e..1cab9ba 100644 --- a/spec/models/address_spec.rb +++ b/spec/models/address_spec.rb @@ -18,6 +18,12 @@ describe "instance_methods" do it "can query for associated rides" do + ride = create(:ride) + from_address = ride.from_address + to_address = ride.to_address + + expect(from_address.rides).to include(ride) + expect(to_address.rides).to include(ride) end end end