Class: Mentorship
- Inherits:
-
ActiveRecord::Base
- Object
- ActiveRecord::Base
- Mentorship
- Defined in:
- app/models/mentorship.rb
Overview
Model for handling mentorships
Class Method Summary (collapse)
-
+ (Hash{String => String, Number}) new_mentorship(person_name, mentor_name, institution_name, start_date, end_date)
Creates a new mentorship.
-
+ (Object) update_mentorship(id, person_name, mentorship_array_received)
Updates the mentorships connected to a person.
Instance Method Summary (collapse)
-
- (Object) as_json(options = {})
Handles rendering a mentorship in a JSON format.
-
- (Hash{String => String, Number}) serializer_for_mentorship(mentorship)
Makes a serialized postdoc to be sent to the frontend in a JSON format.
Class Method Details
+ (Hash{String => String, Number}) new_mentorship(person_name, mentor_name, institution_name, start_date, end_date)
Creates a new mentorship.
19 20 21 22 23 24 25 26 27 28 29 30 31 32 |
# File 'app/models/mentorship.rb', line 19 def Mentorship.new_mentorship(person_name, mentor_name, institution_name, start_date, end_date) person_id = FindId.person(person_name) mentor_id = FindId.mentor_supervisor(mentor_name, institution_name) institution_id = FindId.institution(institution_name) mentorship = Mentorship.create_with(approved: false) .find_or_create_by(person_id: person_id, mentor_id: mentor_id, institution_id: institution_id, start: start_date, end: end_date) return mentorship end |
+ (Object) update_mentorship(id, person_name, mentorship_array_received)
Note:
Currently uses a quick-fix. Will hopefully update later.
Updates the mentorships connected to a person.
40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 |
# File 'app/models/mentorship.rb', line 40 def Mentorship.update_mentorship(id, person_name, mentorship_array_received) # Make an array of mentorship ids that are connected to the person in the database mentorship_array = Array.new mentorship_list = Mentorship.where(:person_id => id) mentorship_list.each do |mentorship_single| mentorship_array.push(mentorship_single.id) end # Check that mentorship_array is not nil unless mentorship_array.nil? # Check that mentorship_array_received is not nil unless mentorship_array_received.nil? # For each mentorship in the mentorship_array_received, see if it # already exists in the database # If it exists, don't do anything. If not, create it. mentorship_array_received.each do |mentorship| # Reminder that this function will find or create a mentorship with # the required attributes mentorship_object = Mentorship.new_mentorship(person_name, mentorship[:pdSupervisor], mentorship[:pdInstitution], mentorship[:pdStartYear], mentorship[:pdEndYear]) mentorship_id = mentorship_object.id # If the mentorship is in the mentorship_array, remove from the array if mentorship_array.include? mentorship_id mentorship_array.delete(mentorship_id) end end # If there's anything left in the mentorship_array after, remove them unless mentorship_array.nil? mentorship_array.each do |mentorship_id| Mentorship.delete(mentorship_id) end end # If the mentorship_array_received is nil, remove all mentorships # connected to the person else mentorship_array.each do |mentorship_id| Mentorship.delete(mentorship_id) end end # If mentorship_array is nil (or person has no mentorships), # then if mentorship_array_received (or the mentorships received from edit page) # is not nil, then add all of them to the table else unless mentorship_array_received.nil? mentorship_array_received.each do |mentorship| Mentorship.new_mentorship(person_name, mentorship[:pdSupervisor], mentorship[:pdInstitution], mentorship[:pdStartYear], mentorship[:pdEndYear]) end end end end |
Instance Method Details
- (Object) as_json(options = {})
Handles rendering a mentorship in a JSON format.
179 180 181 |
# File 'app/models/mentorship.rb', line 179 def as_json(={}) super(:except => [:created_at, :updated_at]) end |
- (Hash{String => String, Number}) serializer_for_mentorship(mentorship)
Note:
Could probably take id off if the frontend isn't using it
Makes a serialized postdoc to be sent to the frontend in a JSON format.
164 165 166 167 168 169 170 171 172 173 174 175 176 |
# File 'app/models/mentorship.rb', line 164 def serializer_for_mentorship(mentorship) result = Api::MentorshipSerializer.new(self).serializable_hash result[:pdStartYear] = mentorship.start result[:pdEndYear] = mentorship.end result[:pdSupervisor] = Person.find_by(id: mentorship.mentor_id).name result[:pdInstitution] = Institution.find_by(id: mentorship.institution_id).name result = result.except(:id, :approved) result[:postdoc_id] = mentorship.id result[:postdoc_approved] = mentorship.approved return result end |