From 3dca0ad6b75ea615fdc0c72a72f355e2b725fc37 Mon Sep 17 00:00:00 2001 From: mendicantx Date: Sun, 23 Jun 2013 21:58:36 -0600 Subject: add *.swp to gitignore --- .gitignore | 1 + 1 file changed, 1 insertion(+) 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 -- cgit v1.2.3 From de3c7de2bac4f9f27ee7b74567c234177cd41d48 Mon Sep 17 00:00:00 2001 From: mendicantx Date: Sun, 23 Jun 2013 22:53:12 -0600 Subject: Add location to a user via a postal code. --- app/models/geo_location_service.rb | 2 ++ app/models/user.rb | 19 ++++++++++++++++++- app/views/devise/registrations/edit.html.erb | 3 +++ app/views/devise/registrations/new.html.erb | 3 +++ db/migrate/20130624041207_add_location_to_user.rb | 7 +++++++ db/schema.rb | 5 ++++- spec/models/GeoLocationService_spec.rb | 12 ++++++++++++ 7 files changed, 49 insertions(+), 2 deletions(-) create mode 100644 db/migrate/20130624041207_add_location_to_user.rb 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 d6b1339..c26babf 100644 --- a/app/models/user.rb +++ b/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 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 @@
<%= f.label :email %>
<%= f.email_field :email %>
+
<%= f.label :postal_code %>
+ <%= f.text_field :postal_code %>
+ <% if devise_mapping.confirmable? && resource.pending_reconfirmation? %>
Currently waiting confirmation for: <%= resource.unconfirmed_email %>
<% 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 @@
<%= f.label :email %>
<%= f.email_field :email %>
+
<%= f.label :postal_code %>
+ <%= f.text_field :postal_code %>
+
<%= f.label :password %>
<%= f.password_field :password %>
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 9e6af99..77ce335 100644 --- a/db/schema.rb +++ b/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 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 -- cgit v1.2.3