Skip to content

Commit

Permalink
Rename variable, apply i18n, iterate category_name, and improve idemp…
Browse files Browse the repository at this point in the history
…otentcy

Co-authored-by: maebeale <maebeale@gmail.com>
Co-authored-by: exbinary <exbinary@gmail.com>
  • Loading branch information
3 people committed Apr 3, 2021
1 parent d0cd87a commit 5cca2be
Showing 1 changed file with 17 additions and 18 deletions.
35 changes: 17 additions & 18 deletions app/models/importers/community_resource_importer.rb
Original file line number Diff line number Diff line change
Expand Up @@ -27,25 +27,22 @@ def process_row(row)
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
community_resource = organization.community_resources.i18n.where(name: row["name"], publish_from: row["publish_from"])
if community_resource
community_resource.description = row["description"]

row["category_name"].split(/,\s*/).each do |category|
community_resource.tag_list << category
end

community_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!(
community_resource = CommunityResource.create!(
name: row["name"],
website_url: row["website_url"],
facebook_url: row["facebook_url"],
Expand All @@ -59,9 +56,11 @@ def process_row(row)
organization: organization
)

resource.tag_list.add(row["category_name"])
row["category_name"].split(/,\s*/).each do |category|
community_resource.tag_list << category
end

resource.location = Location.create! do |location|
community_resource.location = Location.create! do |location|
location.street_address = row["street"]
location.city = row["city"]
location.state = row["state"]
Expand All @@ -71,7 +70,7 @@ def process_row(row)
end

service_location_type = LocationType.find_or_create_by(name: "service_area")
resource.service_area = ServiceArea.create! do |service_area|
community_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"]
Expand All @@ -88,7 +87,7 @@ def process_row(row)
service_area.service_area_type = "city"
end

service_area.organization = resource.organization
service_area.organization = community_resource.organization
service_area.location = Location.create! do |location|
location.location_type = service_location_type

Expand All @@ -100,6 +99,6 @@ def process_row(row)
end
end

resource.save
community_resource.save
end
end

0 comments on commit 5cca2be

Please sign in to comment.