summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
-rw-r--r--.gitignore1
-rw-r--r--app/models/geo_location_service.rb2
-rw-r--r--app/models/user.rb19
-rw-r--r--app/views/devise/registrations/edit.html.erb3
-rw-r--r--app/views/devise/registrations/new.html.erb3
-rw-r--r--db/migrate/20130624041207_add_location_to_user.rb7
-rw-r--r--db/schema.rb3
-rw-r--r--spec/models/GeoLocationService_spec.rb12
8 files changed, 49 insertions, 1 deletions
diff --git a/.gitignore b/.gitignore
index dcf64d4..ceae409 100644
--- a/.gitignore
+++ b/.gitignore
@@ -17,3 +17,4 @@ public/assets
.env
script/heroku_setup.sh
database.yml
+*.swp
diff --git a/app/models/geo_location_service.rb b/app/models/geo_location_service.rb
index e5a0dc5..50f418f 100644
--- a/app/models/geo_location_service.rb
+++ b/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"]
diff --git a/app/models/user.rb b/app/models/user.rb
index 4a170b9..a471980 100644
--- a/app/models/user.rb
+++ b/app/models/user.rb
@@ -1,8 +1,25 @@
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
has_many :links
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
diff --git a/app/views/devise/registrations/edit.html.erb b/app/views/devise/registrations/edit.html.erb
index 1465dcc..ae74bdd 100644
--- a/app/views/devise/registrations/edit.html.erb
+++ b/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 %>
diff --git a/app/views/devise/registrations/new.html.erb b/app/views/devise/registrations/new.html.erb
index 9a9a71d..4d70d49 100644
--- a/app/views/devise/registrations/new.html.erb
+++ b/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>
diff --git a/db/migrate/20130624041207_add_location_to_user.rb b/db/migrate/20130624041207_add_location_to_user.rb
new file mode 100644
index 0000000..9cd333c
--- /dev/null
+++ b/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
diff --git a/db/schema.rb b/db/schema.rb
index 0fa3137..d95f634 100644
--- a/db/schema.rb
+++ b/db/schema.rb
@@ -104,6 +104,9 @@ ActiveRecord::Schema.define(:version => 20130624045542) 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
diff --git a/spec/models/GeoLocationService_spec.rb b/spec/models/GeoLocationService_spec.rb
index 6ff491a..fdc5266 100644
--- a/spec/models/GeoLocationService_spec.rb
+++ b/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