Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Store reverse geo-coded address & force unique addresses #518

Merged
merged 6 commits into from
Nov 18, 2018
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
11 changes: 8 additions & 3 deletions app/models/restroom.rb
Original file line number Diff line number Diff line change
Expand Up @@ -18,13 +18,14 @@ class Restroom < ApplicationRecord
ignoring: :accents

validates :name, :street, :city, :state, presence: true
validates :street, uniqueness: {scope: [:city, :state], message: "is already registered"}

geocoded_by :full_address
after_validation :perform_geocoding
before_validation :perform_geocoding, if: require_geocoding?
before_validation :reverse_geocode, if: require_geocoding?

reverse_geocoded_by :latitude, :longitude do |obj, results|
if geo = results.first
obj.name = geo.address
obj.street = geo.address.split(',').first
obj.city = geo.city
obj.state = geo.state
Expand All @@ -47,7 +48,7 @@ class Restroom < ApplicationRecord
scope :updated_since, ->(date) { where("updated_at >= ?", date) }

def full_address
"#{street}, #{city}, #{state}, #{country}"
[street, city, state, country].compact.join(",")
end

def rated?
Expand All @@ -71,6 +72,10 @@ def self.text_search(query)

private

def require_geocoding?
full_address.present?
end

def strip_slashes
['name', 'street', 'city', 'state', 'comment', 'directions'].each do |field|
attributes[field].try(:gsub!, "\\'", "'")
Expand Down
26 changes: 26 additions & 0 deletions lib/tasks/reverse_geocode.rake
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
namespace :db do
desc "Reverse geocode each restroom's address (use db:reverse_geocode[dry_run] to preview changes)"
task :reverse_geocode, [:dry_run] => [:environment] do |t, args|
args.with_defaults(dry_run: false)

if args.dry_run
puts "Dry running reverse_geocode"
else
puts "Running reverse_geocode"
end

puts

Restroom.all.each do |restroom|
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Do we want to do this for all Restrooms, or can we just do ones that have some columns missing?

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Depends if you're particularly fussy about the display, for example casing and Street vs St.


previous = restroom.full_address
restroom.reverse_geocode

if previous != restroom.full_address
puts "ID: #{restroom.id}\t#{previous}\n"
puts "\t\t\\==> #{restroom.full_address}\n\n"
restroom.save unless args.dry_run
end
end
end
end