-
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.
- Loading branch information
Showing
10 changed files
with
158 additions
and
2 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
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
5 changes: 5 additions & 0 deletions
5
db/migrate/20240717225736_add_gem_record_id_to_committee_members.rb
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,5 @@ | ||
class AddGemRecordIdToCommitteeMembers < ActiveRecord::Migration[7.0] | ||
def change | ||
add_column :committee_members, :gem_record_id, :integer | ||
end | ||
end |
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,19 @@ | ||
# frozen_string_literal: true | ||
|
||
require Rails.root.join('lib/tasks/load_gem_records_csv.rb') | ||
|
||
namespace :gem_records_csv do | ||
desc 'Loading Gem Records.' | ||
# Usage: cat /file/with/gem_records.txt | rake gem_records:load | ||
|
||
task :load, [:filename] => :environment do |t, args| | ||
filename = args[:filename] | ||
if filename.nil? || filename.empty? | ||
puts "Error: Filename is required" | ||
exit 1 | ||
end | ||
GemRecord.delete_all | ||
gem_load = LoadGemRecordsCSV.new | ||
gem_load.load_csv(filename) | ||
end | ||
end |
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,59 @@ | ||
# frozen_string_literal: true | ||
|
||
## | ||
# Function to load gem records from a CSV file | ||
## | ||
|
||
require 'csv' | ||
|
||
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 | ||
unless GemRecord.find_by_seqgradevent(seqgradevent) | ||
gr = GemRecord.new | ||
gr.seqgradevent = seqgradevent | ||
gr.studentname = row['studentname'].strip | ||
gr.sisid = row['sisid'].strip | ||
gr.emailaddress = row['emailaddress'].strip | ||
gr.eventtype = row['eventtype'].strip | ||
gr.eventdate = row['eventdate'].strip | ||
gr.examresult = row['examresult'].strip | ||
gr.title = row['title'].strip | ||
gr.program = row['program'].strip | ||
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 | ||
end | ||
rescue StandardError => e | ||
warn("ERROR: #{e}") | ||
warn('Hint: Possible Bad File if strip nil') | ||
end | ||
end | ||
|
||
def load_committee_members(filename) | ||
count = 0 | ||
|
||
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! | ||
end | ||
end | ||
end |
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 @@ | ||
"SEQGRADEVENT","SISID","FIRSTNAME","SURNAME","ROLE" | ||
1,12345,"John","Smith","Chair" | ||
1,12345,"Jane","Doe","Committee Member" | ||
2,654321,"Jim","Brown","Chair" | ||
2,654321,"Jake","White","Committee Member" | ||
3,789012,"Eve","Green","Chair" | ||
3,789012,"Frank","Black","Committee Member" |
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,4 @@ | ||
seqgradevent,studentname,sisid,emailaddress,eventtype,eventdate,examresult,title,program,superv,examdate | ||
1,John Doe,123456,[email protected],Exam,2023-06-15,Pass,Thesis Title,Program A,Supervisor X,2023-07-01 | ||
2,Jane Smith,654321,[email protected],Defense,2023-06-20,Pass,Thesis Title B,Program B,Supervisor Y,2023-07-02 | ||
3,Alice Johnson,789012,[email protected],Exam,2023-06-25,Fail,Thesis Title C,Program C,Supervisor Z,2023-07-03 |
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,58 @@ | ||
require 'test_helper' | ||
|
||
require Rails.root.join('lib/tasks/load_gem_records_csv.rb') | ||
class LoadGemRecordsTest < ActiveSupport::TestCase | ||
setup do | ||
gem_load = LoadGemRecordsCSV.new | ||
gem_load.load_gem_records("test/fixtures/files/gem_records.csv") | ||
gem_load.load_committee_members("test/fixtures/files/committee_members.csv") | ||
end | ||
|
||
should "parse csv" do | ||
assert_equal 3, GemRecord.count | ||
|
||
records = GemRecord.order(:seqgradevent) | ||
|
||
assert_equal 1, records[0].seqgradevent | ||
assert_equal 'John Doe', records[0].studentname | ||
assert_equal 123456, records[0].sisid | ||
assert_equal '[email protected]', records[0].emailaddress | ||
|
||
assert_equal 2, records[1].seqgradevent | ||
assert_equal 'Jane Smith', records[1].studentname | ||
assert_equal 654321, records[1].sisid | ||
assert_equal '[email protected]', records[1].emailaddress | ||
|
||
assert_equal 3, records[2].seqgradevent | ||
assert_equal 'Alice Johnson', records[2].studentname | ||
assert_equal 789012, records[2].sisid | ||
assert_equal '[email protected]', records[2].emailaddress | ||
|
||
assert_equal 2, records[0].committee_members.count | ||
assert_equal 'John', records[0].committee_members.first.first_name | ||
assert_equal 'Smith', records[0].committee_members.first.last_name | ||
assert_equal 'Chair', records[0].committee_members.first.role | ||
|
||
assert_equal 'Jane', records[0].committee_members.last.first_name | ||
assert_equal 'Doe', records[0].committee_members.last.last_name | ||
assert_equal 'Committee Member', records[0].committee_members.last.role | ||
|
||
assert_equal 2, records[1].committee_members.count | ||
assert_equal 'Jim', records[1].committee_members.first.first_name | ||
assert_equal 'Brown', records[1].committee_members.first.last_name | ||
assert_equal 'Chair', records[1].committee_members.first.role | ||
|
||
assert_equal 'Jake', records[1].committee_members.last.first_name | ||
assert_equal 'White', records[1].committee_members.last.last_name | ||
assert_equal 'Committee Member', records[1].committee_members.last.role | ||
|
||
assert_equal 2, records[2].committee_members.count | ||
assert_equal 'Eve', records[2].committee_members.first.first_name | ||
assert_equal 'Green', records[2].committee_members.first.last_name | ||
assert_equal 'Chair', records[2].committee_members.first.role | ||
|
||
assert_equal 'Frank', records[2].committee_members.last.first_name | ||
assert_equal 'Black', records[2].committee_members.last.last_name | ||
assert_equal 'Committee Member', records[2].committee_members.last.role | ||
end | ||
end |