Class: Search

Inherits:
Object
  • Object
show all
Defined in:
app/lib/search.rb

Overview

This class is used to find all relations with respect to a person. That is to find the: postdocs mentored by the person, the postdoc mentors for the person, the degrees supervised by the person and the supervisors for the persons degrees. There is also the case where it finds ALL available information about a person and also the people related to the person's information.

Author:

Class Method Summary (collapse)

Class Method Details

+ (Object) person(id, approved)

this class is different from relations_by_id, because it not only gathers the relations, but also the information required to “fill” out these relations. used to detail or “view” a person on the frontend.



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
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
# File 'app/lib/search.rb', line 116

def self.person(id, approved)
  unless Person.exists?(id) then return nil end
  @person_info = self.person_info(id)
  if @person_info["person"] == nil
    return nil
  end

  # be warned: the following nested if-elsif statements are ugly as sin
  # but i'm experiencing a bug and don't have time for elegance

  # TODO: the problem is that the approved value is a string and not a boolean
  # so, write a helper method that returns true/false depending on the value
  # of the string and then use the returned boolean value to query database
  # instead of these nested if/elsif blocks of nasty

  @mentorships_array = Array.new
  unless @person_info["person"].mentorships.blank?
    @person_info["person"].mentorships.each do |m|
      if m.approved && approved == 'true'
        @mentorship = {
          'id' => m.id,
          'start' => m.start,
          'end' => m.end,
          'institution' => m.institution,
          'mentor' => {
            'data' => m.mentor,
            'institution' => m.mentor.institution
          }
        }
        @mentorships_array.push(@mentorship)
      elsif approved == 'false'
        @mentorship = {
          'id' => m.id,
          'start' => m.start,
          'end' => m.end,
          'institution' => m.institution,
          'mentor' => {
            'data' => m.mentor,
            'institution' => m.mentor.institution
          }
        }
        @mentorships_array.push(@mentorship)
      end
    end
  end

  @supervision_array = Array.new
  unless @person_info["person"].supervisions.blank?
     @person_info["person"].supervisions.each do |s|
      if s.approved && approved == 'true'
        @supervision = {
          'id' => s.id,
          'degree' => {
            'data' => s.degree,
            'institution' => s.degree.institution
          },
          'supervisor' => {
            'person' => s.supervisor,
            'institution' => s.supervisor.institution
          }
        }
        @supervision_array.push(@supervision)
      elsif approved == 'false'
        @supervision = {
          'id' => s.id,
          'degree' => {
            'data' => s.degree,
            'institution' => s.degree.institution
          },
          'supervisor' => {
            'person' => s.supervisor,
            'institution' => s.supervisor.institution
          }
        }
        @supervision_array.push(@supervision)
      end
    end
  end

  @mentored_array = Array.new
  unless @person_info["mentored"].blank?
    @person_info["mentored"].each do |m|
      if m.approved && approved == 'true'
        @mentored_obj = {
          'id' => m.id,
          'start' => m.start,
          'end' => m.end,
          'institution' => m.institution,
          'mentored' => {
            'person' => m.person,
            'institution' => m.person.institution
            }
          }
        @mentored_array.push(@mentored_obj)
      elsif approved == 'false'
        @mentored_obj = {
          'id' => m.id,
          'start' => m.start,
          'end' => m.end,
          'institution' => m.institution,
          'mentored' => {
            'person' => m.person,
            'institution' => m.person.institution
            }
          }
        @mentored_array.push(@mentored_obj)
      end
    end
  end

  @supervised_array = Array.new
  unless @person_info["supervised"].blank?
    @person_info["supervised"].each do |s|
      if s.approved && approved == 'true'
        @supervised_obj = {
          'id' => s.id,
          'degree' => {
            'data' => s.degree,
            'institution' => s.degree.institution
          },
          'person' => {
            'data' => s.person,
            'institution' => s.person.institution
          }
        }
        @supervised_array.push(@supervised_obj)
      elsif approved == 'false'
        @supervised_obj = {
          'id' => s.id,
          'degree' => {
            'data' => s.degree,
            'institution' => s.degree.institution
          },
          'person' => {
            'data' => s.person,
            'institution' => s.person.institution
          }
        }
        @supervised_array.push(@supervised_obj)
      end
    end
  end

  @data = {
    'person' => {
      'data' => @person_info["person"],
      'institution' => @person_info["person"].institution
    },
    'mentors' => @mentorships_array,
    'mentored' => @mentored_array,
    'supervisors' => @supervision_array,
    'supervised' => @supervised_array
  }
  return @data.as_json
end

+ (Object) person_info(id)

this function was extracted from self.person(id) and is called by Notifier and Deleter. It is used to retrieve the relationships w.r.t if approved == nil then we will gather all information, good for notifications

Parameters:

  • id

Returns:

  • hash contating the relationships



91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
# File 'app/lib/search.rb', line 91

def self.person_info(id)
  unless Person.exists?(id) then return nil end
  @person = Person.includes(:institution)
            .includes( {mentorships: [:institution, {mentor: [:institution]}] })
            .includes( {supervisions: [ {degree:  [:institution] }, {supervisor: [:institution]}] })
            .where(:id => id).first

  @mentored = Mentorship.where(:mentor_id => id)
              .includes(:institution)
              .includes(person: :institution)

  @supervised = Supervision.where(:supervisor_id => id)
                .includes(degree: :institution)
                .includes(person: :institution)

  return {
    'person' => @person,
    'mentored' => @mentored,
    'supervised' => @supervised
  }
end

+ (Object) relations_by_id(person_id)

returns a hash that contains the relations to a person and the person/institution records. it is used in conjunction with search on the frontend, and it is likely that this class can be optimized TODO: investigate optimizations



25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
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
# File 'app/lib/search.rb', line 25

def self.relations_by_id(person_id)
  @persons = Set.new
  @institutions = Set.new

  #this code could be cleaner still reference notifier.rb
  @search_target = Person.where('id' => person_id)
                   .where('approved' => true)
                   .includes(:institution).first
  if @search_target.blank?
    return  {
              'target' => Array.new,
              'mentors' => Array.new, 'mentored' => Array.new,
              'supervisors' => Array.new, 'supervised' => Array.new,
              'institutions' => Array.new
            }
  end

  @mentors = Person.joins('LEFT OUTER JOIN mentorships ON mentorships.mentor_id = people.id')
             .where('mentorships.person_id' => person_id)
             .where('approved' => true)
             .where('mentorships.approved' => true)
             .includes(:institution)

  unless @mentors.blank? then @persons.add(@mentors) end

  @mentored = Person.joins(:mentorships)
              .where('mentorships.mentor_id' => person_id)
              .where('approved' => true)
              .where('mentorships.approved' => true)
              .includes(:institution)
  unless @mentored.blank? then @persons.add(@mentored) end

  @supervisors = Person.joins('LEFT OUTER JOIN supervisions ON supervisions.supervisor_id = people.id')
                 .where('supervisions.person_id' => person_id)
                 .where('approved' => true)
                 .where('supervisions.approved' => true)
                 .includes(:institution)
  unless @supervisors.blank? then @persons.add(@supervisors) end

  @supervised = Person.joins(:supervisions)
                .where('supervisions.supervisor_id' => person_id)
                .where('approved' => true)
                .where('supervisions.approved' => true)
                .includes(:institution)
  unless @supervised.blank? then @persons.add(@supervised) end

  @persons.each do |p|
    p.each do |person|
      unless person.institution.blank? then @institutions.add(person.institution) end
    end
  end


  return  {
            'target' => @search_target,
            'mentors' => @mentors, 'mentored' => @mentored,
            'supervisors' => @supervisors, 'supervised' => @supervised,
            'institutions' => @institutions
          }
end

+ (Object) relations_by_name(name)

if called will find the id associated with the name and then call self.relations_by_id



12
13
14
15
16
17
18
# File 'app/lib/search.rb', line 12

def self.relations_by_name(name)
  if Person.exists?(name: name)
    @person_id = Person.find_by(name: name).id
    return self.relations_by_id(@person_id)
  end
  return {}
end