Browse Source

Add activity validation

Maarten van den Berg 8 years ago
parent
commit
d0a51b00f8
1 changed files with 19 additions and 0 deletions
  1. 19 0
      app/models/activity.rb

+ 19 - 0
app/models/activity.rb

@@ -3,4 +3,23 @@ class Activity < ApplicationRecord
3 3
 
4 4
   has_many :participants
5 5
   has_many :people, through: :participants
6
+
7
+  validates :public_name, presence: true
8
+  validates :start, presence: true
9
+  validate  :deadline_before_start
10
+
11
+  def organizers
12
+    self.organizers.includes(:person).where(is_organizer: true)
13
+  end
14
+
15
+  def is_organizer(person)
16
+    Participant.exists?(person_id: person.id, activity_id: self.id, is_organizer: true)
17
+  end
18
+
19
+  private
20
+  def deadline_before_start
21
+    if self.deadline && self.deadline > self.start
22
+      errors.add(:deadline, 'must be before start')
23
+    end
24
+  end
6 25
 end