Skip to content

Commit

Permalink
Add specs
Browse files Browse the repository at this point in the history
  • Loading branch information
mikeheft committed Jul 2, 2024
1 parent 6f56dc6 commit 5a042db
Show file tree
Hide file tree
Showing 4 changed files with 18 additions and 0 deletions.
2 changes: 2 additions & 0 deletions .rubocop.yml
Original file line number Diff line number Diff line change
Expand Up @@ -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:
Expand Down
6 changes: 6 additions & 0 deletions app/models/address.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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
4 changes: 4 additions & 0 deletions app/models/ride.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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
6 changes: 6 additions & 0 deletions spec/models/address_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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

0 comments on commit 5a042db

Please sign in to comment.