class Kapsme < ActiveRecord::Base
  belongs_to :kap
  belongs_to :p_customer
  
  has_many :kaps_rappels, :through => :kap
  
  def kaps_type
    if self.kaps_type_id == 1
      "Journalier"
    elsif self.kaps_type_id == 2
      "Hebdomadaire"
    else
      "Mensuel"
    end
    
    
  end
  
  
  before_save do
    if self.paused
      self.point_to_count = 0
    else
      self.point_to_count = self.points
    end
    
    if !self.token
      self.generate_token
    end
    
  end
  
  def generate_token
        
    begin
      self[:token] = SecureRandom.urlsafe_base64
    end while Kapsme.exists?(:token => self[:token])
    
    
  end
    
    
  
  
  belongs_to :kaps_day
  
end
