Skip to content

Provide a hardcoded IP to `development` and `test` environments

Yaro edited this page May 17, 2020 · 4 revisions

At the bottom of your config/initializers/geocoder.rb, copy/paste the following code:

# Provide a hardcoded ip of 1.2.3.4 when in developmnt/test and the ip address resolves as localhost
if %w(development test).include? Rails.env
  module Geocoder
    module Request
      def geocoder_spoofable_ip_with_localhost_override
        ip_candidate = geocoder_spoofable_ip_without_localhost_override
        if ip_candidate == '127.0.0.1'
          '1.2.3.4'
        else
          ip_candidate
        end
      end
      
      # alias_method_chain is deprecated
      # alias_method_chain :geocoder_spoofable_ip, :localhost_override

      alias_method :geocoder_spoofable_ip_without_localhost_override, :geocoder_spoofable_ip
      alias_method :geocoder_spoofable_ip, :geocoder_spoofable_ip_with_localhost_override
    end
  end
end
Clone this wiki locally