diff --git a/app/models/university/person.rb b/app/models/university/person.rb index 0a8223ff4d9a1449b8d16df3446a2f7e44009083..481511e1b0f01776bc614e3b9de1996637e61446 100644 --- a/app/models/university/person.rb +++ b/app/models/university/person.rb @@ -80,6 +80,7 @@ class University::Person < ApplicationRecord allow_blank: true, if: :will_save_change_to_email? + before_validation :sanitize_email scope :ordered, -> { order(:last_name, :first_name) } scope :administratives, -> { where(is_administrative: true) } @@ -164,4 +165,10 @@ class University::Person < ApplicationRecord def git_dependencies_administrator [] end + + protected + + def sanitize_email + self.email = self.email.downcase.strip + end end diff --git a/app/models/user.rb b/app/models/user.rb index a33738c3ce346b96308179786753edab5402aa82..602fa6c6fbeacd71974cace3381ced6872fd5fa9 100644 --- a/app/models/user.rb +++ b/app/models/user.rb @@ -55,12 +55,12 @@ class User < ApplicationRecord has_one_attached_deletable :picture # In this order, "resize avatar" callback will be fired after the others. include WithAuthentication + include WithPerson include WithRoles include WithSyncBetweenUniversities belongs_to :university belongs_to :language - has_one :person, class_name: 'University::Person', dependent: :nullify scope :ordered, -> { order(:last_name, :first_name) } diff --git a/app/models/user/with_person.rb b/app/models/user/with_person.rb new file mode 100644 index 0000000000000000000000000000000000000000..5ab95727d620865fb9fc1054ab6be316c5b7bf31 --- /dev/null +++ b/app/models/user/with_person.rb @@ -0,0 +1,21 @@ +module User::WithPerson + extend ActiveSupport::Concern + + included do + has_one :person, class_name: 'University::Person', dependent: :nullify + + after_create_commit :find_or_create_person + end + + protected + + def find_or_create_person + person = university.people.where(email: email).first_or_initialize do |person| + person.first_name = first_name + person.last_name = last_name + person.phone = mobile_phone + end + person.user = self + person.save + end +end