Report abuse

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
# == Schema Information
# Schema version: 3
#
# Table name: events
#
#  id          :integer(11)     not null, primary key
#  name        :string(30)      
#  description :text            
#  flyer_id    :integer(11)     
#  user_id     :integer(11)     
#  start       :datetime        
#  end         :datetime        
#  created_at  :datetime        
#  updated_at  :datetime        
#

class Event < ActiveRecord::Base
  belongs_to :user
  
  def length= time
    if self.start
      self.end = self.start + time
    else
      return false
    end
  end
  
  def self.left_in date, user
    month = {:start => date.beginning_of_month, :end => date.end_of_month}
    count = user.events.count(:conditions => "`start` > #{month[:start]} AND `start` < #{month[:end]}")
    user.allowed_events - count
  end
  
  def length
    self.end - self.start
  end
end