From 347c03ccd724c04f8d95b544685d9555b26b3004 Mon Sep 17 00:00:00 2001 From: Janell-Huyck Date: Mon, 20 Nov 2023 18:05:29 -0500 Subject: [PATCH 1/3] Modify college_name(id) to account for destroyed colleges --- app/helpers/colleges_helper.rb | 9 ++++----- app/models/csv.rb | 9 ++++----- 2 files changed, 8 insertions(+), 10 deletions(-) diff --git a/app/helpers/colleges_helper.rb b/app/helpers/colleges_helper.rb index bfca878d..d90daeac 100644 --- a/app/helpers/colleges_helper.rb +++ b/app/helpers/colleges_helper.rb @@ -22,11 +22,10 @@ def check_other_college(publication) end def college_name(id) - if id.nil? - '' - else - College.find(id).name - end + return '' if id.nil? + + college = College.find_by(id:) + college&.name.to_s end def college_array(publication) diff --git a/app/models/csv.rb b/app/models/csv.rb index dbb25f11..d707acb0 100644 --- a/app/models/csv.rb +++ b/app/models/csv.rb @@ -20,11 +20,10 @@ def author_name(position) end def college_name(id) - if id.nil? - '' - else - College.find(id).name - end + return '' if id.nil? + + college = College.find_by(id:) + college&.name.to_s end def colleges From 699391a026a127a2153ec441cb1d4732fc328d4c Mon Sep 17 00:00:00 2001 From: Janell-Huyck Date: Mon, 20 Nov 2023 18:18:11 -0500 Subject: [PATCH 2/3] move college name functionality to college model --- app/helpers/colleges_helper.rb | 5 +---- app/models/college.rb | 7 +++++++ app/models/csv.rb | 5 +---- 3 files changed, 9 insertions(+), 8 deletions(-) diff --git a/app/helpers/colleges_helper.rb b/app/helpers/colleges_helper.rb index d90daeac..bc7166b7 100644 --- a/app/helpers/colleges_helper.rb +++ b/app/helpers/colleges_helper.rb @@ -22,10 +22,7 @@ def check_other_college(publication) end def college_name(id) - return '' if id.nil? - - college = College.find_by(id:) - college&.name.to_s + College.name_for(id) end def college_array(publication) diff --git a/app/models/college.rb b/app/models/college.rb index 1bfa14f3..900c2092 100644 --- a/app/models/college.rb +++ b/app/models/college.rb @@ -4,4 +4,11 @@ class College < ApplicationRecord has_and_belongs_to_many :books, dependent: :destroy has_and_belongs_to_many :other_publications, dependent: :destroy validates :name, presence: true + + def self.name_for(id) + return '' if id.nil? + + college = College.find_by(id:) + college&.name.to_s + end end diff --git a/app/models/csv.rb b/app/models/csv.rb index d707acb0..e110a82f 100644 --- a/app/models/csv.rb +++ b/app/models/csv.rb @@ -20,10 +20,7 @@ def author_name(position) end def college_name(id) - return '' if id.nil? - - college = College.find_by(id:) - college&.name.to_s + College.name_for(id) end def colleges From 7fbf7e53f675aeaf27329bddc31c2f17b79f1285 Mon Sep 17 00:00:00 2001 From: Janell-Huyck Date: Mon, 20 Nov 2023 18:36:33 -0500 Subject: [PATCH 3/3] Protect 'set_college' function --- app/controllers/colleges_controller.rb | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/app/controllers/colleges_controller.rb b/app/controllers/colleges_controller.rb index a08d246a..7c19598d 100644 --- a/app/controllers/colleges_controller.rb +++ b/app/controllers/colleges_controller.rb @@ -68,7 +68,7 @@ def destroy # Use callbacks to share common setup or constraints between actions. def set_college - @college = College.find(params[:id]) + @college = College.find_by(id: params[:id]) end # Never trust parameters from the scary internet, only allow the white list through.