diff --git a/app/controllers/committee_members_controller.rb b/app/controllers/committee_members_controller.rb index 7dc631e..a5ecb9b 100644 --- a/app/controllers/committee_members_controller.rb +++ b/app/controllers/committee_members_controller.rb @@ -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 diff --git a/app/models/committee_member.rb b/app/models/committee_member.rb index 3c755b3..91ebd7a 100644 --- a/app/models/committee_member.rb +++ b/app/models/committee_member.rb @@ -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.' diff --git a/app/models/gem_record.rb b/app/models/gem_record.rb index baba38b..87d02c9 100644 --- a/app/models/gem_record.rb +++ b/app/models/gem_record.rb @@ -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) } diff --git a/db/migrate/20240717225736_add_gem_record_id_to_committee_members.rb b/db/migrate/20240717225736_add_gem_record_id_to_committee_members.rb new file mode 100644 index 0000000..18508fb --- /dev/null +++ b/db/migrate/20240717225736_add_gem_record_id_to_committee_members.rb @@ -0,0 +1,5 @@ +class AddGemRecordIdToCommitteeMembers < ActiveRecord::Migration[7.0] + def change + add_column :committee_members, :gem_record_id, :integer + end +end diff --git a/db/schema.rb b/db/schema.rb index ac53eec..d36be1a 100644 --- a/db/schema.rb +++ b/db/schema.rb @@ -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 @@ -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| diff --git a/lib/tasks/gem_records_csv.rake b/lib/tasks/gem_records_csv.rake new file mode 100644 index 0000000..0474dfa --- /dev/null +++ b/lib/tasks/gem_records_csv.rake @@ -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 diff --git a/lib/tasks/load_gem_records_csv.rb b/lib/tasks/load_gem_records_csv.rb new file mode 100644 index 0000000..1ee16dc --- /dev/null +++ b/lib/tasks/load_gem_records_csv.rb @@ -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 diff --git a/test/fixtures/files/committee_members.csv b/test/fixtures/files/committee_members.csv new file mode 100644 index 0000000..edc6928 --- /dev/null +++ b/test/fixtures/files/committee_members.csv @@ -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" diff --git a/test/fixtures/files/gem_records.csv b/test/fixtures/files/gem_records.csv new file mode 100644 index 0000000..6b3c2f0 --- /dev/null +++ b/test/fixtures/files/gem_records.csv @@ -0,0 +1,4 @@ +seqgradevent,studentname,sisid,emailaddress,eventtype,eventdate,examresult,title,program,superv,examdate +1,John Doe,123456,johndoe@example.com,Exam,2023-06-15,Pass,Thesis Title,Program A,Supervisor X,2023-07-01 +2,Jane Smith,654321,janesmith@example.com,Defense,2023-06-20,Pass,Thesis Title B,Program B,Supervisor Y,2023-07-02 +3,Alice Johnson,789012,alicejohnson@example.com,Exam,2023-06-25,Fail,Thesis Title C,Program C,Supervisor Z,2023-07-03 \ No newline at end of file diff --git a/test/lib/load_gem_records_test.rb b/test/lib/load_gem_records_test.rb new file mode 100644 index 0000000..9bbfd55 --- /dev/null +++ b/test/lib/load_gem_records_test.rb @@ -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 'johndoe@example.com', records[0].emailaddress + + assert_equal 2, records[1].seqgradevent + assert_equal 'Jane Smith', records[1].studentname + assert_equal 654321, records[1].sisid + assert_equal 'janesmith@example.com', records[1].emailaddress + + assert_equal 3, records[2].seqgradevent + assert_equal 'Alice Johnson', records[2].studentname + assert_equal 789012, records[2].sisid + assert_equal 'alicejohnson@example.com', 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