diff --git a/lib/tasks/load_gem_records_csv.rb b/lib/tasks/load_gem_records_csv.rb index 69e9f02..7ec3ec4 100644 --- a/lib/tasks/load_gem_records_csv.rb +++ b/lib/tasks/load_gem_records_csv.rb @@ -9,8 +9,6 @@ class LoadGemRecordsCSV def load_gem_records(filename) - count = 0 - converter = lambda { |header| header.downcase } CSV.foreach(filename, headers: true, header_converters: converter) do |row| seqgradevent = row['seqgradevent'].strip @@ -29,15 +27,7 @@ def load_gem_records(filename) gr.superv = row['superv'].strip gr.examdate = row['examdate'].strip - if gr.save!(validate: false) - count += 1 - else - warn('Error: Load Gem Records Save Failed!') - warn("Error: #{gr.errors.inspect}") - end - rescue StandardError => e - warn("ERROR: #{e}") - warn('Hint: Possible Bad File if strip nil') + gr.save! end end @@ -47,13 +37,21 @@ def load_committee_members(filename) converter = lambda { |header| header.downcase } CSV.foreach(filename, headers: true, header_converters: converter) do |row| seqgradevent = row['seqgradevent'].strip - gr = GemRecord.find_by_seqgradevent(seqgradevent) - cm = CommitteeMember.new - cm.gem_record = gr - cm.first_name = row['firstname'].strip - cm.last_name = row['surname'].strip - cm.role = row['role'].strip - cm.save! + sisid = row['sisid'].strip + first_name = row['firstname'].strip + last_name = row['surname'].strip + role = row['role'].strip + + if gr = GemRecord.find_by_seqgradevent(seqgradevent) + unless cm = CommitteeMember.find_by(gem_record_id: gr.id, first_name: first_name, last_name: last_name, role: role) + cm = CommitteeMember.new + cm.gem_record = gr + cm.first_name = first_name + cm.last_name = last_name + cm.role = role + cm.save! + end + end end end end diff --git a/test/lib/load_gem_records_test.rb b/test/lib/load_gem_records_test.rb index 2741b3b..092ecfe 100644 --- a/test/lib/load_gem_records_test.rb +++ b/test/lib/load_gem_records_test.rb @@ -71,6 +71,9 @@ class LoadGemRecordsTest < ActiveSupport::TestCase # there should be 8 GEM records at this point assert_equal 8, GemRecord.count + # there should be 6 CommitteeMember + assert_equal 6, CommitteeMember.count + # load 3 GEM records from the exact same CSV files AGAIN gem_load = LoadGemRecordsCSV.new gem_load.load_gem_records(LoadGemRecordsTest::GEM_RECORDS_FILE) @@ -78,6 +81,9 @@ class LoadGemRecordsTest < ActiveSupport::TestCase # there should STILL be 8 GEM records at this point assert_equal 8, GemRecord.count + + # there should STILL be 6 CommitteeMember + assert_equal 6, CommitteeMember.count end should "update existing and matching GEM records when loading GEM records from CSV" do @@ -121,6 +127,9 @@ class LoadGemRecordsTest < ActiveSupport::TestCase # there should STILL be 3 GEM records at this point assert_equal 3, GemRecord.count + + # there should STILL be 6 CommitteeMember + assert_equal 6, CommitteeMember.count # verify fields are updated from the CSV assert_equal 1, records[0].seqgradevent