Skip to content

Instantly share code, notes, and snippets.

@jcreed
Created April 28, 2016 12:45
Show Gist options
  • Save jcreed/ff2d24fc706811c9da4c763e22a15263 to your computer and use it in GitHub Desktop.
Save jcreed/ff2d24fc706811c9da4c763e22a15263 to your computer and use it in GitHub Desktop.
cas questions deleter
namespace :releases do
namespace "5.56" do
namespace :after_load do
desc "Delete invalid CAS Custom Questions"
task "UNCAS-8212" => [:environment] do |task|
display :notice, task.full_comment
require 'csv'
csv_file = IO::read(File.join(Rails.root, "db/data/2016/invalid_custom_question_ids.csv"), :encoding => 'UTF-8')
csv = CSV.new csv_file, headers: true
sup_quest_identifiers = {}
cas_form_id = nil
csv.each do |row|
if sup_quest_identifiers[row[2].to_s].nil?
sup_quest_identifiers[row[2].to_s] = [row[1].to_s]
else
sup_quest_identifiers[row[2].to_s] << row[1].to_s
end
end
sup_quest_identifiers.each do |cas_form_id, question_identifiers|
ActiveRecord::Base.transaction do
cas=Association.where(application_form_identifier: cas_form_id).first
#require 'debugger'; debugger
if cas
bad_cas_questions = SupplementalQuestion.where(owner_type: 'Association', question_identifier: question_identifiers, owner_id: cas)
if bad_cas_questions.present?
supplemental_lookups = (cas.supplemental_lookups + cas.applicant_gateway_supplemental_lookups).flatten.uniq
supplemental_question_blocks = bad_cas_questions.map(&:supplemental_question_block).compact.uniq
supplemental_question_block_field_categories = supplemental_question_blocks.map(&:field_category)
display :notice, " Destroying things for #{cas.name}-#{cas.cycle.identifier}"
field_categories_destroyed = supplemental_question_block_field_categories.count
#supplemental_question_block_field_categories.each(&:destroy)
display :success, "Destroyed #{field_categories_destroyed} field categories."
blocks_destroyed = supplemental_question_blocks.count
#supplemental_question_blocks.each(&:destroy)
display :success, "Destroyed #{blocks_destroyed} supplemental question blocks."
questions_destroyed = bad_cas_questions.count
#bad_cas_questions.each(&:destroy)
display :success, "Destroyed #{questions_destroyed} supplemental questions."
lookups_destroyed = supplemental_lookups.count
#supplemental_lookups.each(&:destroy)
display :success, "Destroyed #{lookups_destroyed} supplemental lookups."
end
end
end
end
end
end
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment