-
Notifications
You must be signed in to change notification settings - Fork 19
/
Copy pathperson.rb
116 lines (97 loc) · 3.69 KB
/
person.rb
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
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
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
# frozen_string_literal: true
# == Schema Information
#
# Table name: people
#
# id :integer not null, primary key
# birthdate :datetime
# location :string
# updated_by :string
# name :string
# title :string
# created_at :datetime not null
# updated_at :datetime not null
# picture :string
# competence_notes :string
# company_id :bigint(8)
# associations_updatet_at :datetime
# nationality :string
# nationality2 :string
# marital_status :integer default("single"), not null
# email :string
# department_id :integer
# shortname :string
#
class Person < ApplicationRecord
include PgSearch::Model
after_initialize :set_default_languages
belongs_to :company
belongs_to :department, optional: true
mount_uploader :picture, PictureUploader
has_many :projects, dependent: :destroy
has_many :activities, dependent: :destroy
has_many :advanced_trainings, dependent: :destroy
has_many :educations, dependent: :destroy
has_many :expertise_topic_skill_values, dependent: :destroy
has_many :expertise_topics, through: :expertise_topic_skill_values
has_many :language_skills, dependent: :delete_all
accepts_nested_attributes_for :language_skills, allow_destroy: true
has_many :person_roles, dependent: :destroy
accepts_nested_attributes_for :person_roles, allow_destroy: true
has_many :people_skills, dependent: :destroy
accepts_nested_attributes_for :people_skills
has_many :skills, through: :people_skills
has_many :roles, through: :person_roles
accepts_nested_attributes_for :advanced_trainings, allow_destroy: true
validates :birthdate, :location, :name, :nationality,
:title, :marital_status, :email, presence: true
validates :location, :name, :title,
:email, :shortname, length: { maximum: 100 }
validates :email,
format: { with: /\A[a-zA-Z0-9._%+-]+@[a-zA-Z0-9.-]+\.[a-zA-Z]{2,}\z/,
message: 'Format nicht gültig' }
validates :nationality,
inclusion: { in: ISO3166::Country.all.collect(&:alpha2) }
validates :nationality2,
inclusion: { in: ISO3166::Country.all.collect(&:alpha2) },
allow_blank: true
validate :picture_size
scope :list, -> { order(:name) }
enum marital_status: { single: 0, married: 1, widowed: 2, registered_partnership: 3,
divorced: 4 }
pg_search_scope :search,
against: [
:name,
:title,
:competence_notes
],
associated_against: {
department: :name,
roles: :name,
projects: [:description, :title, :role, :technology],
activities: [:description, :role],
educations: [:location, :title],
advanced_trainings: :description,
skills: [:title]
},
using: {
tsearch: {
prefix: true
}
}
def last_updated_at
[associations_updatet_at, updated_at].compact.max
end
private
def picture_size
return if picture.nil? || picture.size < 10.megabytes
errors.add(:picture, 'grösse kann maximal 10MB sein')
end
def set_default_languages
if new_record?
%w[DE EN FR].each do |language|
language_skills.push(LanguageSkill.new({ language: language, level: 'Keine' }))
end
end
end
end