Skip to content
This repository was archived by the owner on Dec 3, 2019. It is now read-only.
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 4 additions & 0 deletions Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,10 @@ db_create:
db_migrate:
docker-compose run ${RAILS_CONTAINER} rake db:migrate

.PHONY: db_rollback
db_rollback:
docker-compose run ${RAILS_CONTAINER} rake db:rollback

.PHONY: test
test: bg
docker-compose run operationcode-psql bash -c "while ! psql --host=operationcode-psql --username=postgres -c 'SELECT 1'; do sleep 5; done;"
Expand Down
2 changes: 1 addition & 1 deletion app/controllers/api/v1/users_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@ def by_location
private

def user_params
params.require(:user).permit(:email, :zip, :password, :first_name, :last_name, :mentor, :slack_name, :verified)
params.require(:user).permit(:email, :zip, :password, :first_name, :last_name, :mentor, :slack_name, :verified, :state)
end
end
end
Expand Down
11 changes: 10 additions & 1 deletion app/models/user.rb
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,16 @@ class User < ApplicationRecord
# :confirmable, :lockable, :timeoutable and :omniauthable
devise :database_authenticatable, :registerable,
:recoverable, :rememberable, :trackable, :validatable
geocoded_by :zip

geocoded_by :zip do |user, results|

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@rickr - Not terribly pleased that this is a block being passed, at the top of the user.rb file, versus a method being called. However, was not able to find an alternative solution in the Geocoder docs.

Let me know if you know of one/want it changed.

geocoded_object = results.first

if geocoded_object.present?
user.latitude = geocoded_object.latitude
user.longitude = geocoded_object.longitude
user.state = geocoded_object.state_code
end
end

# These attributes are what we send to sendgrid as custom fields
SENDGRID_ATTRIBUTES = %w[first_name last_name email]
Expand Down
5 changes: 5 additions & 0 deletions db/migrate/20170720215043_add_state_to_user.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
class AddStateToUser < ActiveRecord::Migration[5.0]
def change
add_column :users, :state, :string
end
end
3 changes: 2 additions & 1 deletion db/schema.rb
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@
#
# It's strongly recommended that you check this file into your version control system.

ActiveRecord::Schema.define(version: 20170701194821) do
ActiveRecord::Schema.define(version: 20170720215043) do

# These are extensions that must be enabled in order to support this database
enable_extension "plpgsql"
Expand Down Expand Up @@ -154,6 +154,7 @@
t.string "timezone"
t.text "bio"
t.boolean "verified", default: false, null: false
t.string "state"
t.index ["email"], name: "index_users_on_email", unique: true, using: :btree
t.index ["reset_password_token"], name: "index_users_on_reset_password_token", unique: true, using: :btree
end
Expand Down
36 changes: 36 additions & 0 deletions lib/tasks/users.rake
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
namespace :users do
desc "Sets the user's :state based on user's :zip, for any any where user.state == nil"
task populate_states: :environment do
# https://developers.google.com/maps/documentation/geocoding/usage-limits
GEOCODING_DAILY_MAX = 2500
GEOCODING_PER_S_MAX = 50

users = User.where.not(latitude: nil, longitude: nil).where(state: nil).limit(GEOCODING_DAILY_MAX)
user_count = users.count

return "0 eligible users to be updated" unless users.present?
p "#{user_count} users are eligible to be updated."

users.in_batches(of: GEOCODING_PER_S_MAX).each_with_index do |batch, batch_index|
batch.each_with_index do |user, index|
p "Updating #{(batch_index * GEOCODING_PER_S_MAX) + index + 1} of #{user_count}"

begin
results = Geocoder.search([user.latitude, user.longitude]).try(:first)

raise "Could not geocode User id #{user.id}" unless results.present?

user.update! state: results.state_code
rescue => e
p "When adding the :state for User id #{user.id}, experienced this error: #{e}"
Rails.logger.info "When adding the :state for User id #{user.id}, experienced this error: #{e}"
end
end

sleep 2
end

remaining = User.where.not(latitude: nil, longitude: nil).where(state: nil).count
p "#{remaining} users left to be updated. Task can be reran in 24 hours."
end
end
1 change: 1 addition & 0 deletions test/factories/users.rb
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@
timezone 'EST'
mentor false
verified false
state { Faker::Address.state_abbr }

factory :mentor do
mentor true
Expand Down
1 change: 1 addition & 0 deletions test/models/user_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,7 @@ def setup
u.update_attributes(zip: '80203')
assert_equal 20.7143528, u.latitude
assert_equal -174.0059731, u.longitude
assert_equal 'CO', u.state
end

test 'only geocodes if zip is updated' do
Expand Down