Skip to content

Commit

Permalink
Geocode HousingForms on the backend
Browse files Browse the repository at this point in the history
This replaces housing_locations.js.coffee with the geocoder gem.

I'm using the geocoder gem as described in
http://railscasts.com/episodes/273-geocoder?view=comments

Fixes civictechdc#341.
  • Loading branch information
jrunningen committed Aug 25, 2015
1 parent ba8e09f commit 35e2e4b
Show file tree
Hide file tree
Showing 7 changed files with 14 additions and 68 deletions.
2 changes: 1 addition & 1 deletion app/assets/javascripts/housing_locations.js.coffee
Original file line number Diff line number Diff line change
Expand Up @@ -104,7 +104,7 @@ $ ->
$('#housing-location-modal .modal-title').text(housing_data.name)
name = housing_data.name
address = housing_data.location
coordinates = [housing_data.lat, housing_data.long]
coordinates = [housing_data.latitude, housing_data.longitude]

addMarker coordinates,name,map
$('#housing-location-modal').modal()
Expand Down
12 changes: 4 additions & 8 deletions app/controllers/housing_forms_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -40,10 +40,9 @@ def create
h.updated_locally = true
end
end
# IF long/lat update coordinates.
# IF longitude/latitude update coordinates.
respond_to do |format|
if @housing_form.save
format.js {render 'update_location.js'}
format.html {redirect_to @housing_form, notice: "Housing Form created"}
format.json { }
else
Expand All @@ -69,12 +68,9 @@ def update
end

if @housing_form.update(housing_form_params)
puts "update call"
puts "here is @housing form: #{@housing_form}"
respond_to do |format|
format.js {render 'update_location.js'}
format.html {redirect_to @housing_form, notice: "Housing Form updated"}
end
respond_to do |format|
format.html {redirect_to @housing_form, notice: "Housing Form updated"}
end
else
render :edit
end
Expand Down
3 changes: 3 additions & 0 deletions app/models/housing_form.rb
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,9 @@ class HousingForm < ActiveRecord::Base
after_create { initialize_from_disk! }
after_update { read_fields! }

geocoded_by :location
# FIXME: On `rake pull_pdfs`, #geocode gets called twice per form. Why?
after_validation :geocode, if: :location_changed?

def initialize_from_disk!
update(name: name)
Expand Down
4 changes: 2 additions & 2 deletions app/views/housing_forms/_location_table.html.erb
Original file line number Diff line number Diff line change
Expand Up @@ -38,8 +38,8 @@
id: housing_form.id,
name: housing_form.name,
location: housing_form.location,
lat: housing_form.lat,
long: housing_form.long
latitude: housing_form.latitude,
longitude: housing_form.longitude
}.to_json
%>">
<span class="glyphicon glyphicon-map-marker" title="View on map"></span>
Expand Down
2 changes: 1 addition & 1 deletion app/views/housing_forms/edit.html.erb
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
<h1>Editing Housing Form</h1>

<%= bootstrap_form_for @housing_form, remote: true do |f| %>
<%= bootstrap_form_for @housing_form do |f| %>
<% if @housing_form.errors.any? %>
<div id="error_explanation">
<h2><%= pluralize(@housing_form.errors.count, "error") %> prohibited this housing_form from being saved:</h2>
Expand Down
56 changes: 0 additions & 56 deletions app/views/housing_forms/update_location.js.erb

This file was deleted.

3 changes: 3 additions & 0 deletions lib/tasks/seed_pdfs.rake
Original file line number Diff line number Diff line change
Expand Up @@ -72,6 +72,9 @@ task pull_pdfs: :environment do
# 'url' is not a HousingForm database field. Remove it so we can use this
# hash to update or create.
housing_form.delete('url')
# 'lat' and 'long' are removed in favor of 'latitude' and 'longitude'
housing_form.delete('lat')
housing_form.delete('long')

if existing_form.nil?
puts "New PDF: #{housing_form['name']}".green
Expand Down

0 comments on commit 35e2e4b

Please sign in to comment.