Class: Supervision

Inherits:
ActiveRecord::Base
  • Object
show all
Defined in:
app/models/supervision.rb

Overview

Model for handling supervisions

Class Method Summary (collapse)

Instance Method Summary (collapse)

Class Method Details

+ (Hash{String => String, Number}) new_supervision(degree_year, degree_type, institution_name, person_name, supervisor_name)

Creates a new supervision.

Parameters:

  • degree_year (Number)

    year the degree was awarded

  • degree_type (String)

    type of degree awarded

  • institution_name (String)

    institution the degree was awarded

  • person_name (String)

    name of the person

  • supervisor_name (String)

    name of the supervisor

Returns:

  • (Hash{String => String, Number})

    created supervision



18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
# File 'app/models/supervision.rb', line 18

def Supervision.new_supervision(degree_year,
                                degree_type,
                                institution_name,
                                person_name,
                                supervisor_name)

  degree_id = FindId.degree(degree_year, degree_type, institution_name)
  person_id = FindId.person(person_name)
  supervisor_id = FindId.mentor_supervisor(supervisor_name, institution_name)

  supervision = Supervision.create_with(approved: false)
                          .find_or_create_by(degree_id: degree_id,
                                              person_id: person_id,
                                              supervisor_id: supervisor_id)
  return supervision
end

+ (Object) update_supervision(id, name, supervision_array_received)

Note:

Currently uses a quick-fix. Will hopefully actually update later.

Updates the supervisions and degrees that the person has

Parameters:

  • id (Number)

    id of the person

  • name (String)

    name of the person

  • supervision_array_received (Array<Hash{String => String, Number}>)

    array of supervisions that the person has



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
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
# File 'app/models/supervision.rb', line 41

def Supervision.update_supervision(id, name, supervision_array_received)

  # Creates an array of supervision ids that are connected to the person in
  # the database
  supervision_array = Array.new
  supervision_find = Supervision.where(:person_id => id)
  supervision_find.each do |single|
    supervision_array.push(single.id)
  end

  # Check that supervision_array is not nil
  unless supervision_array.nil?

    # Check that the supervision_array_received is not nil
    unless supervision_array_received.nil?

      # For each supervision, see if it already exists in the database
      # If not, create a new one
      supervision_array_received.each do |supervision|

        # This function will find or create a supervision given the required
        # attributes. It will also create a new degree if needed.
        supervision_object = Supervision.new_supervision(supervision[:year],
                                                          supervision[:type],
                                                          supervision[:institution],
                                                          name,
                                                          supervision[:supervisor],)
        supervision_id = supervision_object.id

        # If the supervision is in the supervision_array, remove from the array
        if supervision_array.include? supervision_id
          supervision_array.delete(supervision_id)
        end
      end

      # If there are any supervisions left in supervision_array, delete them
      unless supervision_array.nil?
        supervision_array.each do |supervision_id|
          Supervision.delete(supervision_id)
        end
      end

    # If the supervision_array_received is nil (person has no degrees/supervisions)
    # Then delete all supervisions in the supervision_array
    else
      supervision_array.each do |supervision_id|
        Supervision.delete(supervision_id)
      end
    end

  # If there are no supervisions connected to the person, create new
  # supervisions and degrees for the person as long as supervision_array_received
  # is not nil either
  # In this case, degree_id, degree_approved, supervision_id, and supervision_approved
  # are all nil
  else
    unless supervision_array_received.nil?
      supervision_array_received.each do |supervision|
        Degree.new_degree(supervision[:year], supervision[:type], supervision[:institution])
        Supervision.new_supervision(supervision[:year],
                                    supervision[:type],
                                    supervision[:institution],
                                    name,
                                    supervision[:supervisor])
      end
    end
  end

  # # For each supervision in the supervision_array_received, check if it
  # # exists in the supervision_array.
  # # If so, then update. If not, create a new supervision and degree
  # supervision_array_received.each do |supervision|
  #
  #   # If the supervision is new, then the supervision[:supervision_id]
  #   # should be nil
  #   # Maybe this as well: if supervision_array.include? supervision[:supervision_id]
  #   unless supervision[:supervision_id].nil?
  #
  #     # Check if the supervisor has changed. If so, then get new id.
  #     # Gets the supervision object and the supervisor person object
  #     # from the database to compare
  #     # See if there's a matching supervisor in the database and update
  #     # if it's not the same
  #     supervisor_id = FindId.person(supervision[:supervisor])
  #     supervision_object = Supervision.find(supervision[:supervision_id])
  #     if supervision_object.person_id != supervisor_id
  #       Supervision.update(supervision[:supervision_id], supervisor_id: supervisor_id)
  #     end
  #
  #     # Find or create the degree id from the database
  #     # If it is not the same as supervision[:degree_id], then update
  #     # supervision with the new id
  #     # * Since multiple people may have the same degree, it may be best
  #     # not to update the degree in the database *
  #     degree_id = FindId.degree(supervision[:year],
  #                               supervision[:type],
  #                               supervision[:institution])
  #     if degree_id != supervision[:degree_id]
  #       Supervision.update(supervision[:supervision_id], degree_id: degree_id)
  #     end
  #
  #     # Remove id from the supervision_array
  #     supervision_array.delete(supervision[:supervision_id])
  #
  #   # If the supervision[:supervision_id] is nil, then it is new so create
  #   # a new supervision and degree
  #   else
  #     Degree.new_degree(supervision[:year], supervision[:type], supervision[:institution])
  #     Supervision.new_supervision(supervision[:year],
  #                                 supervision[:type],
  #                                 supervision[:institution],
  #                                 name,
  #                                 supervision[:supervisor])
  #   end
  # end


end

Instance Method Details

- (Object) as_json(options = {})

Handles rendering a supervision in a JSON format.



183
184
185
# File 'app/models/supervision.rb', line 183

def as_json(options={})
  super(:except => [:created_at, :updated_at])
end

- (Hash{String => String, Number}) serializer_for_supervision(supervision)

Note:

Could probably take ids off if the frontend isn't using it

Makes a serialized supervision with degree information to be sent to the frontend in a JSON format.

Parameters:

  • supervision (Hash{String => String, Number})

    a person's supervision

Returns:

  • (Hash{String => String, Number})

    serialized supervision



165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
# File 'app/models/supervision.rb', line 165

def serializer_for_supervision(supervision)
  result = Api::SupervisionSerializer.new(self).serializable_hash

  degree = Degree.find_by(id: degree_id)
  result[:year] = degree.year
  result[:supervisor] = Person.find_by(:id => supervision.supervisor_id).name
  result[:institution] = Institution.find_by(id: degree.institution_id).name
  result[:type] = degree.degree_type

  result[:degree_id] = degree.id
  result[:degree_approved] = degree.approved
  result = result.except(:id, :approved)
  result[:supervision_id] = supervision.id
  result[:supervision_approved] = supervision.approved
  return result
end