-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #124 from unepwcmc/feat/rake-task-to-correct-inval…
…id-data feat: rake task to update records with invalid data (WIP)
- Loading branch information
Showing
3 changed files
with
65 additions
and
0 deletions.
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,7 @@ | ||
# 1.0.0 | ||
|
||
Site has been released for a long time, but starting at 1.0.0 for the purposes of adding a change log | ||
|
||
- use date_discovery for date filtering instead of created_at | ||
- rake task to fix non-breaking spaces and  character in database | ||
- process strings during import to prevent the above issue |
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
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,49 @@ | ||
desc 'Iterate over bjson data structure and replace all non-breaking spaces with spaces and remove all  characters' | ||
task fix_invalid_data_from_imports: :environment do | ||
Report.find_each do |report| | ||
data = report.data | ||
hash_iterator(data, report.id) | ||
report.update(data: data) | ||
end | ||
end | ||
|
||
def hash_iterator(hash, report_id) | ||
keys = hash.keys | ||
keys.each do |key| | ||
value = hash[key] | ||
|
||
if value.is_a?(Hash) | ||
hash_iterator(value, report_id) | ||
elsif value.is_a?(Array) | ||
array_iterator(value, report_id) | ||
elsif value.is_a?(String) | ||
clean_string(value, report_id) | ||
end | ||
end | ||
end | ||
|
||
def array_iterator(value_array, report_id) | ||
value_array.each do |value| | ||
if value.is_a?(String) | ||
clean_string(value, report_id) | ||
elsif value.is_a?(Hash) | ||
hash_iterator(value, report_id) | ||
elsif value.is_a?(Array) | ||
array_iterator(value, report_id) | ||
end | ||
end | ||
end | ||
|
||
def clean_string(value, report_id) | ||
non_breaking_space = value =~ /\u00a0/ | ||
if non_breaking_space | ||
puts "removing non-breaking space from #{value} on Report ##{report_id}" | ||
value.gsub!(/\u00a0/," ") | ||
end | ||
|
||
invalid_character_index = value =~ /[Â]/ | ||
if invalid_character_index | ||
puts "removing  from #{value} on Report ##{report_id}" | ||
value.slice!(invalid_character_index) | ||
end | ||
end |