Last active
January 6, 2016 04:10
-
-
Save ezrarush/4579c01702dfd1dea685 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# ===================================================================================================== | |
# Template for generating a geocoder gem test app | |
# ===================================================================================================== | |
# Requirements: | |
# ------------- | |
# | |
# * Ruby >= 1.9.3 | |
# * Rails >= 4 | |
# Usage: | |
# ------ | |
# | |
# $ rails new geocoder-app --skip --skip-bundle --template this-gist.rb | |
# | |
# ===================================================================================================== | |
# ----- Use Thin ---------------------------------------------------------------------------------- | |
begin | |
require 'thin' | |
gem 'thin' | |
rescue LoadError | |
end | |
# ----- Remove CoffeeScript, Sass and "all that jazz" --------------------------------------------- | |
comment_lines 'Gemfile', /gem 'coffee/ | |
comment_lines 'Gemfile', /gem 'sass/ | |
comment_lines 'Gemfile', /gem 'uglifier/ | |
uncomment_lines 'Gemfile', /gem 'therubyracer/ | |
# ----- Add gems into Gemfile --------------------------------------------------------------------- | |
gem 'geocoder' | |
# ----- Disable asset logging in development ------------------------------------------------------ | |
environment 'config.assets.logger = false', env: 'development' | |
gem 'quiet_assets', group: "development" | |
# ----- Install gems ------------------------------------------------------------------------------ | |
run "bundle install" | |
# ----- Generate Location resource ----------------------------------------------------------------- | |
generate :scaffold, "Location address:string latitude:float longitude:float type:string" | |
route "root to: 'locations#index'" | |
rake "db:migrate" | |
# ----- Remove the input fields for auto-populated attributes and make type a select --------------- | |
run "rm -f app/views/locations/_form.html.erb" | |
file 'app/views/locations/_form.html.erb', <<-CODE | |
<%= form_for(@location) do |f| %> | |
<% if @location.errors.any? %> | |
<div id="error_explanation"> | |
<h2><%= pluralize(@location.errors.count, "error") %> prohibited this location from being saved:</h2> | |
<ul> | |
<% @location.errors.full_messages.each do |message| %> | |
<li><%= message %></li> | |
<% end %> | |
</ul> | |
</div> | |
<% end %> | |
<div class="field"> | |
<%= f.label :address %><br> | |
<%= f.text_field :address %> | |
</div> | |
<div class="field"> | |
<%= f.label :type %><br> | |
<%= f.select :type, ["", "KansasLocation"] %> | |
</div> | |
<div class="actions"> | |
<%= f.submit %> | |
</div> | |
<% end %> | |
CODE | |
# ----- Configure Geocoder ------------------------------------------------------------------------- | |
file 'config/initializers/geocoder.rb', <<-CODE | |
Geocoder.configure( | |
# geocoding service (see below for supported options): | |
:lookup => :nominatim, | |
# :lookup => :google, | |
# geocoding service request timeout, in seconds (default 3): | |
:timeout => 10 | |
) | |
CODE | |
# | |
# ----- Add Geocoder integration into the model and setup one controller and route ------------------- | |
run "rm -f app/models/location.rb" | |
file 'app/models/location.rb', <<-CODE | |
class Location < ActiveRecord::Base | |
#{'attr_accessible :address' if Rails::VERSION::STRING < '4'} | |
geocoded_by :address | |
after_validation :geocode | |
# use this superclass's controller and route for all subclasses | |
def self.inherited(child) | |
child.instance_eval do | |
def model_name | |
Location.model_name | |
end | |
end | |
super | |
end | |
end | |
CODE | |
# ------ Cache ambiguous addresses by state subclass ------------------------------------------------------------ | |
file 'app/models/kansas_location.rb', <<-CODE | |
class KansasLocation < Location | |
#{'attr_accessible :address' if Rails::VERSION::STRING < '4'} | |
geocoded_by :address do |obj,results| | |
unless results.empty? | |
(1...results.size).each do |i| | |
if results[i].state == "Kansas" | |
obj.latitude = results[i].latitude | |
obj.longitude = results[i].longitude | |
end | |
end | |
if obj.latitude.nil? | |
obj.type = nil | |
obj.latitude = results.first.latitude | |
obj.longitude = results.first.longitude | |
end | |
end | |
end | |
after_validation :geocode | |
end | |
CODE |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment