Skip to content

Instantly share code, notes, and snippets.

@KonnorRogers
Last active April 16, 2022 10:51
Show Gist options
  • Save KonnorRogers/bd05ae76bcc331465e87092011973829 to your computer and use it in GitHub Desktop.
Save KonnorRogers/bd05ae76bcc331465e87092011973829 to your computer and use it in GitHub Desktop.
async geocoding with Ruby-Geocoder
# app/jobs/geocode_job.rb
class GeocodeJob < ApplicationJob
def perform(model)
model.geocode
end
end
# app/models/concerns/geocodeable.rb
module Geocodeable
extend ActiveSupport::Concern
included do
def async_geocode
GeocodeJob.perform_later(self)
end
end
end
# app/models/place.rb
class Place < ApplicationRecord
include Geocodeable
geocoded_by :address
after_commit :async_geocode,
on: [:create, :update],
if: ->(obj) { obj.address.present? and obj.address_changed? }
def address
[street, city, state, country].compact.join(', ')
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment