alexreisner / geocoder

Complete Ruby geocoding solution.
http://www.rubygeocoder.com
MIT License
6.35k stars 1.19k forks source link

geocoded_by not being called #1660

Closed mikeheft closed 4 months ago

mikeheft commented 4 months ago

BEFORE POSTING AN ISSUE, PLEASE MAKE SURE THE PROBLEM IS NOT ADDRESSED IN THE README!

Expected behavior

class Address
  def full_address
    [line_1, line_2, city, state, zip_code].compact.join(", ")
  end

  geocoded_by :full_address do |obj, results|
    if (geo = results.first)
      obj.latitude = geo.latitude
      obj.longitude = geo.longitude
      obj.place_id = geo.place_id
    end
  end

  before_validation :geocode
end

address = Address.create!(...)
address.latitude != nil
address.longitude != nil
address.place_id != nil

Actual behavior

address = Address.create!(...)
address.latitude == nil
address.longitude == nil
address.place_id == nil

Steps to reproduce

#config/initializers/geocoder.rb
# config/initializers/geocoder.rb
Geocoder.configure(
  # street address geocoding service (default :nominatim)
  lookup: :google,

  # IP address geocoding service (default :ipinfo_io)
  ip_lookup: :maxmind,

  # to use an API key:
  api_key: ENV["GOOGLE_API_KEY"],

  # geocoding service request timeout, in seconds (default 3):
  timeout: 15,

  # set default units to kilometers:
  units: :mi,

  # caching (see Caching section below for details):
  cache: Redis.new,
  cache_options: {
    expiration: 1.day, # Defaults to `nil`
  }
)

      VCR.use_cassette("first_commute") do
        from_address = create(
          :address, line_1: "4705 Weitzel Street", city: "Timnath", state: "CO",
          zip_code: "80547"
        )
        to_address = create(
          :address, :with_out_place_id, line_1: "151 N College Ave", city: "Fort Collins", state: "CO",
          zip_code: "80524"
        )
        create_list(:ride, 2, from_address:, to_address:)
        driver = create(:driver, current_address: to_address)
        rides = Ride.selectable.nearby_driver(driver)
        expect(rides.length).to eq(1)
      end

Just create a 'geocodable' object. I have tried debugging with puts statements/prys in a before/after validation method and it's getting called. But even calling address.geocode manually doesn't work

Environment info

mikeheft commented 4 months ago

nvm, I'm an idiot. Not enough coffee. I had forgot that I had stubbed it somewhere else and it was still getting called in my request test somehow. It was my first time using rspec metadata and it I guess I wasn't using it properly