Browse Source

Add Person validation

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

+ 18 - 0
app/models/person.rb

@@ -1,2 +1,20 @@
1 1
 class Person < ApplicationRecord
2
+  validates :email, presence: true, uniqueness: true
3
+  validates :first_name, presence: true
4
+  validates :last_name, presence: true
5
+  validates :is_admin, presence: true
6
+
7
+  validate :birth_date_cannot_be_in_future
8
+
9
+  before_validation :not_admin_if_nil
10
+
11
+  def birth_date_cannot_be_in_future
12
+    if self.birth_date > Date.today
13
+      errors.add(:birth_date, "can't be in the future.")
14
+    end
15
+  end
16
+
17
+  def not_admin_if_nil
18
+    self.is_admin ||= false
19
+  end
2 20
 end