Skip to content

Commit

Permalink
close #50
Browse files Browse the repository at this point in the history
  • Loading branch information
amtuannguyen committed Jul 19, 2024
2 parents adea974 + 7d483a5 commit 0fca169
Show file tree
Hide file tree
Showing 10 changed files with 158 additions and 2 deletions.
2 changes: 1 addition & 1 deletion app/controllers/committee_members_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,6 @@ def destroy
private

def committee_member_params
params.require(:committee_member).permit(:first_name, :last_name, :role, :thesis_id)
params.require(:committee_member).permit(:first_name, :last_name, :role, :thesis_id, :gem_record_id)
end
end
1 change: 1 addition & 0 deletions app/models/committee_member.rb
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ class CommitteeMember < ApplicationRecord

## RELATIONS ##
belongs_to :thesis
belongs_to :gem_record

## VALIDATIONS ##
validates_presence_of :first_name, message: 'Please choose a name.'
Expand Down
2 changes: 2 additions & 0 deletions app/models/gem_record.rb
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,8 @@ class GemRecord < ApplicationRecord
MASTERS_EXAM = "Master's Thesis Exam"
ACCEPTED = 'Accepted'

has_many :committee_members

scope :completed, lambda {
where('eventtype = ? OR eventtype = ?', GemRecord::PHD_COMPLETED, GemRecord::MASTERS_COMPLETED)
}
Expand Down
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
3 changes: 2 additions & 1 deletion db/schema.rb
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@
#
# It's strongly recommended that you check this file into your version control system.

ActiveRecord::Schema[7.0].define(version: 2024_06_11_142734) do
ActiveRecord::Schema[7.0].define(version: 2024_07_17_225736) do
create_table "action_text_rich_texts", charset: "utf8mb3", force: :cascade do |t|
t.string "name", null: false
t.text "body", size: :long
Expand Down Expand Up @@ -79,6 +79,7 @@
t.datetime "updated_at", precision: nil, null: false
t.string "first_name"
t.string "last_name"
t.integer "gem_record_id"
end

create_table "delayed_jobs", id: :integer, charset: "utf8mb3", force: :cascade do |t|
Expand Down
19 changes: 19 additions & 0 deletions lib/tasks/gem_records_csv.rake
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
59 changes: 59 additions & 0 deletions lib/tasks/load_gem_records_csv.rb
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
7 changes: 7 additions & 0 deletions test/fixtures/files/committee_members.csv
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"
4 changes: 4 additions & 0 deletions test/fixtures/files/gem_records.csv
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
58 changes: 58 additions & 0 deletions test/lib/load_gem_records_test.rb
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

0 comments on commit 0fca169

Please sign in to comment.