Skip to content

Instantly share code, notes, and snippets.

@garethrees
Created November 26, 2020 16:03
Show Gist options
  • Save garethrees/81f2978395d4b43a1aac40be65ead30d to your computer and use it in GitHub Desktop.
Save garethrees/81f2978395d4b43a1aac40be65ead30d to your computer and use it in GitHub Desktop.
Schools Update 2020
class UpdateDefunct
GLOABL_TAG = 'school_defunct_nov2020'
def initialize(csv, dryrun: true)
@csv = csv
@dryrun = dryrun
@errors = []
@missing_bodies = []
end
attr_reader :errors, :missing_bodies
def update_all
rows.each do |row|
body = PublicBody.find_by(name: row[:name])
if body
update_body(body, row)
else
missing_body(row)
end
end
end
def check_missing
rows.reject { |row| PublicBody.where(name: row[:name]).exists? }
end
private
attr_reader :csv, :dryrun
def update_body(body, row)
puts %Q(Marking "#{body.name}" as "#{row[:tag_string]}")
return if dryrun
body.last_edit_comment = GLOABL_TAG
body.last_edit_editor = GLOABL_TAG
add_tag(body, row[:tag_string])
add_tag(body, GLOABL_TAG)
body.save!
rescue StandardError => e
@errors << [body, row, e]
end
def missing_body(row)
@missing_bodies << row
end
def rows
CSV.read(csv, headers: true, header_converters: :symbol)
end
def add_tag(body, tag)
return if body.tag_string.include?(tag)
body.tag_string = body.tag_string + ' ' + tag
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment