diff options
| author | mendicantx <mendicant@beigesunshine.com> | 2013-06-23 22:53:12 -0600 |
|---|---|---|
| committer | mendicantx <mendicant@beigesunshine.com> | 2013-06-23 22:53:12 -0600 |
| commit | de3c7de2bac4f9f27ee7b74567c234177cd41d48 (patch) | |
| tree | a9ed16abb63a635eddd4a125667681c2d71316dd | |
| parent | 3dca0ad6b75ea615fdc0c72a72f355e2b725fc37 (diff) | |
Add location to a user via a postal code.
| -rw-r--r-- | app/models/geo_location_service.rb | 2 | ||||
| -rw-r--r-- | app/models/user.rb | 19 | ||||
| -rw-r--r-- | app/views/devise/registrations/edit.html.erb | 3 | ||||
| -rw-r--r-- | app/views/devise/registrations/new.html.erb | 3 | ||||
| -rw-r--r-- | db/migrate/20130624041207_add_location_to_user.rb | 7 | ||||
| -rw-r--r-- | db/schema.rb | 5 | ||||
| -rw-r--r-- | spec/models/GeoLocationService_spec.rb | 12 |
7 files changed, 49 insertions, 2 deletions
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 @@ <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 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 |
