Class: Person

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

Overview

Model for handling people

Class Method Summary (collapse)

Instance Method Summary (collapse)

Class Method Details

+ (Hash{String => String}) new_person(name, position, institution_name)

Note:

We have not decided how to handle people with same names

Creates a new person.

Parameters:

  • name (String)

    name of the person

  • position (String)

    current position of the person

  • institution_name (String)

    current institution of the person

Returns:

  • (Hash{String => String})

    created person



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

def Person.new_person(name, position, institution_name)
  name = name.downcase

  unless position.nil?
    position = position.downcase
  end

  unless institution_name.nil?
    institution_id = FindId.institution(institution_name)
  end

  person = Person.create_with(position: position,
                              institution_id: institution_id,
                              approved: false)
                  .find_or_create_by(name: name)
  return person
end

Instance Method Details

- (Object) as_json(options = {})

Handles rendering a person in a JSON format.



73
74
75
# File 'app/models/person.rb', line 73

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

- (Hash{String => String, Array<Hash{String => String, Number}>}) serializer_for_person(person_object)

Makes a serializable hash for a person to be sent to the frontend in a JSON format.

Parameters:

  • person_object (Hash{String => String})

    a person's basic information

Returns:

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

    a person's information in JSON format



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
# File 'app/models/person.rb', line 40

def serializer_for_person(person_object)
  unless person_object.institution_id.nil?
    institution_object = Institution.find(person_object.institution_id)
    institution_name = institution_object.name
  end

  result = Api::PersonSerializer.new(self).serializable_hash
  result[:currentInstitutionName] = institution_name
  result[:currentPositionTitle] = person_object.position

  person_id = person_object.id

  postdoc_array = Array.new
  postdoc_list = Mentorship.where(:person_id => person_id)
  postdoc_list.each do |single|
    postdoc = single.serializer_for_mentorship(single)
    postdoc_array.push(postdoc)
  end
  result[:postDocInformation] = postdoc_array

  supervision_array = Array.new
  supervision_list = Supervision.where(:person_id => person_id)

  supervision_list.each do |single|
    supervision = single.serializer_for_supervision(single)
    supervision_array.push(supervision)
  end
  result[:degreeInformation] = supervision_array

  return result.to_json
end