This repository was archived by the owner on Dec 3, 2019. It is now read-only.
Repository navigation
Feature/105 save users state #108
Merged
Merged
Changes from all commits
Commits
Show all changes
6 commits
Select commit
Hold shift + click to select a range
3245319
Adds :state column to User
hpjaj fbfa4c2
Adds make command for rake db:rollback
hpjaj 1ab6184
Saves user’s lat, long, and state abbreviation on before_save
hpjaj 20b5b80
Rake task to set eligible user.state based on user.zip
hpjaj 3537de9
Raises error if no geocoding results are returned
hpjaj 6393bdd
Fixes CC issue with assignment in conditional
hpjaj File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| 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 |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| 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 |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
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.rbfile, 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.