123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113 |
- class Person < ApplicationRecord
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
- has_one :user
- has_many :members,
- dependent: :destroy
- has_many :participants,
- dependent: :destroy
- has_many :groups, through: :members
- has_many :activities, through: :participants
- validates :email, uniqueness: true
- validates :first_name, presence: true
- validates :last_name, presence: true
- validate :birth_date_cannot_be_in_future
- before_validation :not_admin_if_nil
- before_save :update_user_email, if: :email_changed?
-
- def full_name
- if self.infix
- [self.first_name, self.infix, self.last_name].join(' ')
- else
- [self.first_name, self.last_name].join(' ')
- end
- end
-
- def reversed_name
- if self.infix
- [self.last_name, self.infix, self.first_name].join(', ')
- else
- [self.last_name, self.first_name].join(', ')
- end
- end
-
- def organized_activities
- self.participants.includes(:activity).where(is_organizer: true)
- end
-
- def self.from_csv(content)
- reader = CSV.parse(content, {headers: true, skip_blanks: true})
- result = []
- reader.each do |row|
- p = Person.find_by(email: row['email'])
- if not p
- p = Person.new
- p.first_name = row['first_name']
- p.infix = row['infix']
- p.last_name = row['last_name']
- p.email = row['email']
- p.birth_date = Date.strptime(row['birth_date']) unless row['birth_date'].blank?
- p.save!
- end
- result << p
- end
- return result
- end
- private
-
- def birth_date_cannot_be_in_future
- if self.birth_date && self.birth_date > Date.today
- errors.add(:birth_date, I18n.t('person.errors.cannot_future'))
- end
- end
-
- def not_admin_if_nil
- self.is_admin ||= false
- end
-
-
- def update_user_email
- if not self.user.nil?
- self.user.update!(email: self.email)
- end
- end
- end
|