-
-
Notifications
You must be signed in to change notification settings - Fork 61
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Community resource importer #918
Draft
svileshina
wants to merge
11
commits into
main
Choose a base branch
from
community_resource_importer
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Draft
Changes from 9 commits
Commits
Show all changes
11 commits
Select commit
Hold shift + click to select a range
a87bc30
WIP: Basic scaffold for CommunityResourceImporter
cattywampus e41ddc2
created tests for website, facebook, phone, description, organization…
senadakadric b5818ff
WIP: Import location and Service Area for Community Resource
cattywampus 7615178
created tests for all fields
senadakadric 1289da6
created a new context to test for situations with multiple records
senadakadric 016de18
Avoid duplicating records when importing community resources
cattywampus 09ec702
44 tests created- most recently making sure the descriptions of comm …
senadakadric c2dec48
Merge branch 'main' into community_resource_importer
svileshina 5429bf6
typo in seed data
svileshina d0cd87a
Merge branch 'main' into community_resource_importer
Connieh1 5cca2be
Rename variable, apply i18n, iterate category_name, and improve idemp…
Connieh1 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 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,105 @@ | ||
class Importers::CommunityResourceImporter < Importers::BaseImporter | ||
def primary_import_klass_name | ||
"CommunityResource" | ||
end | ||
|
||
def process_row(row) | ||
organization = Organization.joins(:location).includes(:location) | ||
.where(name: row["organization_name"]) | ||
.where(locations: { | ||
street_address: row["street"], | ||
city: row["city"], | ||
zip: row["zip"] | ||
}).take | ||
|
||
business_location = LocationType.find_or_create_by(name: "business") | ||
|
||
unless organization | ||
organization = Organization.create! name: row["organization_name"] do |organization| | ||
organization.location = Location.create! do |location| | ||
location.street_address = row["street"] | ||
location.city = row["city"] | ||
location.state = row["state"] | ||
location.zip = row["zip"] | ||
location.county = row["county"] | ||
location.location_type = business_location | ||
end | ||
end | ||
end | ||
|
||
# Have to filter resources in memory because mobility prevents the | ||
# `name` attribute from being persisted to the `community_resources` | ||
# table. This prevents us from querying CommunityResource records by | ||
# their name using ActiveRecord. | ||
resource = organization.community_resources.detect { |resource| resource.name == row["name"] } | ||
if resource | ||
# Add breaks after the description so that if we are updating this resource with other row data there are visible | ||
# gaps between each description entry | ||
resource.description += "\n\n#{row["description"]}" | ||
resource.tag_list.add(row["category_name"]) | ||
|
||
resource.save | ||
|
||
# Resource exists so there's nothing else to update. Return early and move onto the new row. | ||
# This assumes nothing else needs to be updated for existing data. | ||
return | ||
end | ||
|
||
resource = CommunityResource.create!( | ||
name: row["name"], | ||
website_url: row["website_url"], | ||
facebook_url: row["facebook_url"], | ||
phone: row["phone"], | ||
youtube_identifier: row["youtube_identifier"], | ||
publish_from: row["publish_from"], | ||
publish_until: row["publish_until"], | ||
is_created_by_admin: row["is_created_by_admin"], | ||
is_approved: row["is_approved"], | ||
description: row["description"], | ||
organization: organization | ||
) | ||
|
||
resource.tag_list.add(row["category_name"]) | ||
|
||
resource.location = Location.create! do |location| | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. If the organization already exists and has a location, do not create a new location. |
||
location.street_address = row["street"] | ||
location.city = row["city"] | ||
location.state = row["state"] | ||
location.zip = row["zip"] | ||
location.county = row["county"] | ||
location.location_type = business_location | ||
end | ||
|
||
service_location_type = LocationType.find_or_create_by(name: "service_area") | ||
resource.service_area = ServiceArea.create! do |service_area| | ||
if row["service_area_name"] | ||
service_area.name = row["service_area_name"] | ||
elsif row["service_area_town_names"] | ||
service_area.name = row["service_area_town_names"] | ||
elsif row["service_area_counties"] | ||
service_area.name = row["service_area_counties"] | ||
else | ||
service_area.name = row["city"] | ||
end | ||
|
||
if row["service_area_counties"] | ||
service_area.service_area_type = "county" | ||
else | ||
service_area.service_area_type = "city" | ||
end | ||
|
||
service_area.organization = resource.organization | ||
service_area.location = Location.create! do |location| | ||
location.location_type = service_location_type | ||
|
||
location.city = row["service_area_town_names"] ? row["service_area_town_names"] : row["city"] | ||
|
||
location.state = row["state"] | ||
|
||
location.county = row["service_area_counties"] ? row["service_area_counties"] : row["county"] | ||
end | ||
end | ||
|
||
resource.save | ||
end | ||
end |
This file contains 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,8 @@ | ||
require 'csv' | ||
|
||
csv = "community_resources.csv" | ||
path = Rails.root.join('db', 'seeds', 'public_template_csvs', csv) | ||
importer = Importers::CommunityResourceImporter.new(User.first) | ||
importer.import(path) | ||
|
||
puts "completed community_resources.csv" |
Oops, something went wrong.
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.
This needs to iterate because category name is a comma separated list --Mae