Commit de3c7de
Changed files (7)
app
models
views
devise
registrations
db
spec
models
app/models/geo_location_service.rb
@@ -23,6 +23,8 @@ class GeoLocationService
def self.ExtractGeoLocationFromJson( jsonString)
parsed = JSON.parse(jsonString)
+ return nil if parsed["locations"].length == 0
+
# need to handle more than one match
x = parsed["locations"][0]["feature"]["geometry"]["x"]
y = parsed["locations"][0]["feature"]["geometry"]["y"]
app/models/user.rb
@@ -1,7 +1,24 @@
class User < ActiveRecord::Base
devise :database_authenticatable, :registerable, :recoverable, :rememberable, :trackable, :validatable
+ after_save :get_location
- attr_accessible :name, :email, :password, :password_confirmation, :remember_me
+ attr_accessible :name, :email, :password, :password_confirmation, :remember_me, :postal_code, :latitude, :longitude
has_many :needs
acts_as_tagger
+
+private
+
+ def get_location
+
+ return if self.postal_code.nil?
+
+ location = GeoLocationService.GetGeoLocation(self.postal_code)
+
+ return if location.nil?
+
+ update_column :latitude, location.y
+ update_column :longitude, location.x
+
+ end
+
end
app/views/devise/registrations/edit.html.erb
@@ -9,6 +9,9 @@
<div><%= f.label :email %><br />
<%= f.email_field :email %></div>
+ <div><%= f.label :postal_code %><br />
+ <%= f.text_field :postal_code %></div>
+
<% if devise_mapping.confirmable? && resource.pending_reconfirmation? %>
<div>Currently waiting confirmation for: <%= resource.unconfirmed_email %></div>
<% end %>
app/views/devise/registrations/new.html.erb
@@ -9,6 +9,9 @@
<div><%= f.label :email %><br />
<%= f.email_field :email %></div>
+ <div><%= f.label :postal_code %><br />
+ <%= f.text_field :postal_code %></div>
+
<div><%= f.label :password %><br />
<%= f.password_field :password %></div>
db/migrate/20130624041207_add_location_to_user.rb
@@ -0,0 +1,7 @@
+class AddLocationToUser < ActiveRecord::Migration
+ def change
+ add_column :users, :postal_code, :string
+ add_column :users, :latitude, :float
+ add_column :users, :longitude, :float
+ end
+end
db/schema.rb
@@ -11,7 +11,7 @@
#
# It's strongly recommended to check this file into your version control system.
-ActiveRecord::Schema.define(:version => 20130624022039) do
+ActiveRecord::Schema.define(:version => 20130624041207) do
create_table "active_admin_comments", :force => true do |t|
t.string "resource_id", :null => false
@@ -102,6 +102,9 @@ ActiveRecord::Schema.define(:version => 20130624022039) do
t.datetime "created_at", :null => false
t.datetime "updated_at", :null => false
t.string "name"
+ t.string "postal_code"
+ t.float "latitude"
+ t.float "longitude"
end
add_index "users", ["email"], :name => "index_users_on_email", :unique => true
spec/models/GeoLocationService_spec.rb
@@ -39,4 +39,16 @@ describe GeoLocationService do
result.x.should be_within(0.0001).of(-87.6221405679)
result.y.should be_within(0.0001).of(41.8845104628)
end
+
+ it "can look up by postal code" do
+ result = GeoLocationService.GetGeoLocation("T2J0A3")
+ result.should be_an_instance_of(GeoLocation)
+ result.x.should be_within(0.0001).of(-114.06582942278891)
+ result.y.should be_within(0.0001).of(50.97218089773139)
+ end
+
+ it "will return nil with an invalid postal code" do
+ result = GeoLocationService.GetGeoLocation("T2J0A")
+ result.should be_nil
+ end
end