summaryrefslogtreecommitdiff
path: root/app/models/conference.rb
diff options
context:
space:
mode:
Diffstat (limited to 'app/models/conference.rb')
-rw-r--r--app/models/conference.rb63
1 files changed, 63 insertions, 0 deletions
diff --git a/app/models/conference.rb b/app/models/conference.rb
new file mode 100644
index 0000000..8be9f21
--- /dev/null
+++ b/app/models/conference.rb
@@ -0,0 +1,63 @@
+class Conference < ActiveRecord::Base
+ attr_accessible :name, :start, :end, :description, :pin,
+ :open_for_anybody, :max_members, :announce_new_member_by_name,
+ :announce_left_member_by_name
+
+ belongs_to :conferenceable, :polymorphic => true
+ has_many :conference_invitees, :dependent => :destroy
+ has_many :phone_numbers, :as => :phone_numberable, :dependent => :destroy
+
+ validates_presence_of :conferenceable_type, :conferenceable_id
+ validates_presence_of :conferenceable
+ validates_presence_of :name
+ validates_presence_of :start, :if => Proc.new { |conference| !conference.end.blank? }
+ validates_presence_of :end, :if => Proc.new { |conference| !conference.start.blank? }
+ validates_presence_of :max_members
+ validates_numericality_of :max_members, :only_integer => true,
+ :greater_than => 0,
+ :less_than => (MAXIMUM_NUMBER_OF_PEOPLE_IN_A_CONFERENCE + 1),
+ :allow_nil => false,
+ :allow_blank => false
+
+ validates_inclusion_of :open_for_anybody, :in => [true, false]
+
+ validates_numericality_of :pin, :only_integer => true,
+ :greater_than => 0,
+ :allow_nil => true,
+ :allow_blank => true
+ validates_length_of :pin, :minimum => MINIMUM_PIN_LENGTH,
+ :allow_nil => true,
+ :allow_blank => true
+
+ validate :start_and_end_dates_must_make_sense, :if => Proc.new { |conference| !conference.start.blank? && !conference.end.blank? }
+
+ after_save :send_pin_email_when_pin_has_changed
+
+ default_scope where(:state => 'active').order(:start)
+
+ # State Machine stuff
+ state_machine :initial => :active do
+ end
+
+ def sip_domain
+ self.conferenceable.try(:sip_domain)
+ end
+
+ def to_s
+ name
+ end
+
+ private
+
+ def start_and_end_dates_must_make_sense
+ errors.add(:start, 'must be in the future') if self.start < Time.now - 10.minutes
+ errors.add(:end, 'must be later than the start') if self.end < self.start
+ end
+
+ def send_pin_email_when_pin_has_changed
+ if self.conferenceable.class == User && self.pin_changed?
+ Notifications.new_pin(self).deliver
+ end
+ end
+
+end