Browse Source

Update User email if Person changes email

Maarten van den Berg 8 years ago
parent
commit
5b3df5b45d
1 changed files with 8 additions and 0 deletions
  1. 8 0
      app/models/person.rb

+ 8 - 0
app/models/person.rb

@@ -1,4 +1,6 @@
1 1
 class Person < ApplicationRecord
2
+  has_one :user
3
+
2 4
   validates :email, presence: true, uniqueness: true
3 5
   validates :first_name, presence: true
4 6
   validates :last_name, presence: true
@@ -7,7 +9,9 @@ class Person < ApplicationRecord
7 9
   validate :birth_date_cannot_be_in_future
8 10
 
9 11
   before_validation :not_admin_if_nil
12
+  before_save :update_user_email, if: :email_changed?
10 13
 
14
+  private
11 15
   def birth_date_cannot_be_in_future
12 16
     if self.birth_date > Date.today
13 17
       errors.add(:birth_date, "can't be in the future.")
@@ -17,4 +21,8 @@ class Person < ApplicationRecord
17 21
   def not_admin_if_nil
18 22
     self.is_admin ||= false
19 23
   end
24
+
25
+  def update_user_email
26
+    self.user.update!(email: self.email)
27
+  end
20 28
 end