Skip to content

Commit

Permalink
Allow unaltered shelters to be updated during import
Browse files Browse the repository at this point in the history
  • Loading branch information
omnilord committed Sep 4, 2019
1 parent a829f3e commit ec1d4e3
Show file tree
Hide file tree
Showing 3 changed files with 49 additions and 4 deletions.
2 changes: 1 addition & 1 deletion app/importers/fema_importer.rb
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,7 @@ def self.shelters

# If not closed, the sheter will likely have staff available to answer Questions
# if shelter-seekers do show up even if they are not accepting at this time.
active: status != 'CLOSED',
archived: !(status != 'CLOSED'), # Yes, double negative
accepting: accepting
}

Expand Down
9 changes: 6 additions & 3 deletions app/jobs/import_fema_shelters_job.rb
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ def deduplicated_import!(data)
Shelter.create!(data)
1
else
unarchive!(shelter)
update!(shelter, data)
0
end
end
Expand Down Expand Up @@ -66,10 +66,13 @@ def find_by_source(data)
Shelter.unscope(:where).where('LOWER(TRIM(source)) = ?', data[:source].strip.downcase)
end

def unarchive!(shelter)
def update!(shelter, data)
_data = shelter.updated_by.blank? ? data : {}
if shelter.archived
logger.info "Unarchiving pre-existing shelter with ID #{shelter.id}"
shelter.update_columns(archived: false, updated_at: Time.now)
_data[:archived] = false
end
shelter.assign_attributes(_data)
shelter.save if shelter.changed?
end
end
42 changes: 42 additions & 0 deletions test/jobs/import_fema_shelters_job_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -29,4 +29,46 @@ class ImportFemaSheltersJobTest < ActiveJob::TestCase
assert_equal shelters.count, Shelter.count
end
end

test 'import update unaltered shelter records' do
VCR.use_cassette('fema_shelters') do
# setup
shelters = FemaImporter.shelters
ImportFemaSheltersJob.perform_now

# modify a shelter
shelter = Shelter.first
correct_value = shelter.shelter
shelter.update_column(:shelter, 'This Shelter will be modified')

# reimport
ImportFemaSheltersJob.perform_now

shelter.reload
assert_equal correct_value, shelter.shelter
end
end

test 'import does not update user-updated shelter records' do
VCR.use_cassette('fema_shelters') do
# setup
shelters = FemaImporter.shelters
ImportFemaSheltersJob.perform_now

# mock a user edit
shelter = Shelter.first
correct_value = 'This Shelter was user-updated'
shelter.assign_attributes(
shelter: correct_value,
updated_by: users(:admin),
)
shelter.save

# reimport
ImportFemaSheltersJob.perform_now

shelter.reload
assert_equal correct_value, shelter.shelter
end
end
end

0 comments on commit ec1d4e3

Please sign in to comment.