Created
November 26, 2020 16:03
-
-
Save garethrees/81f2978395d4b43a1aac40be65ead30d to your computer and use it in GitHub Desktop.
Revisions
-
garethrees created this gist
Nov 26, 2020 .There are no files selected for viewing
This file contains hidden or 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 charactersOriginal file line number Diff line number Diff line change @@ -0,0 +1,60 @@ 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