Skip to content

Commit 997187b

Browse files
committed
Rename variable, apply i18n, iterate category_name, and make idempotent
1 parent d0cd87a commit 997187b

File tree

1 file changed

+17
-18
lines changed

1 file changed

+17
-18
lines changed

app/models/importers/community_resource_importer.rb

Lines changed: 17 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -27,25 +27,22 @@ def process_row(row)
2727
end
2828
end
2929

30-
# Have to filter resources in memory because mobility prevents the
31-
# `name` attribute from being persisted to the `community_resources`
32-
# table. This prevents us from querying CommunityResource records by
33-
# their name using ActiveRecord.
34-
resource = organization.community_resources.detect { |resource| resource.name == row["name"] }
35-
if resource
36-
# Add breaks after the description so that if we are updating this resource with other row data there are visible
37-
# gaps between each description entry
38-
resource.description += "\n\n#{row["description"]}"
39-
resource.tag_list.add(row["category_name"])
40-
41-
resource.save
30+
community_resource = organization.community_resources.i18n.where(name: row["name"], publish_from: row["publish_from"])
31+
if community_resource
32+
community_resource.description = row["description"]
33+
34+
row["category_name"].split(/,\s*/).each do |category|
35+
community_resource.tag_list << category
36+
end
37+
38+
community_resource.save
4239

4340
# Resource exists so there's nothing else to update. Return early and move onto the new row.
4441
# This assumes nothing else needs to be updated for existing data.
4542
return
4643
end
4744

48-
resource = CommunityResource.create!(
45+
community_resource = CommunityResource.create!(
4946
name: row["name"],
5047
website_url: row["website_url"],
5148
facebook_url: row["facebook_url"],
@@ -59,9 +56,11 @@ def process_row(row)
5956
organization: organization
6057
)
6158

62-
resource.tag_list.add(row["category_name"])
59+
row["category_name"].split(/,\s*/).each do |category|
60+
community_resource.tag_list << category
61+
end
6362

64-
resource.location = Location.create! do |location|
63+
community_resource.location = Location.create! do |location|
6564
location.street_address = row["street"]
6665
location.city = row["city"]
6766
location.state = row["state"]
@@ -71,7 +70,7 @@ def process_row(row)
7170
end
7271

7372
service_location_type = LocationType.find_or_create_by(name: "service_area")
74-
resource.service_area = ServiceArea.create! do |service_area|
73+
community_resource.service_area = ServiceArea.create! do |service_area|
7574
if row["service_area_name"]
7675
service_area.name = row["service_area_name"]
7776
elsif row["service_area_town_names"]
@@ -88,7 +87,7 @@ def process_row(row)
8887
service_area.service_area_type = "city"
8988
end
9089

91-
service_area.organization = resource.organization
90+
service_area.organization = community_resource.organization
9291
service_area.location = Location.create! do |location|
9392
location.location_type = service_location_type
9493

@@ -100,6 +99,6 @@ def process_row(row)
10099
end
101100
end
102101

103-
resource.save
102+
community_resource.save
104103
end
105104
end

0 commit comments

Comments
 (0)