class ProductCustomer < ActiveRecord::Base
  
  has_secure_password
  validates :email, :presence => true, :uniqueness => true, :format => /\A([^@\s]+)@((?:[-a-z0-9]+\.)+[a-z]{2,})\z/i
   validates_presence_of :password, :on => :create
  
  
  belongs_to :particular_bill, :class_name => "Particular"
  
  accepts_nested_attributes_for :particular_bill
  
  belongs_to :particular_send, :class_name => "Particular"
  
  accepts_nested_attributes_for :particular_send
  
  
  before_create do
    self.generate_token
    
    self.particular_bill = Particular.create(:particular_type => "facturation", :skip_validation => true)
    self.particular_idem = true
    self.particular_send = Particular.create(:particular_type => "expedition", :skip_validation => true)
    
       
  end
    
  def send_password_reset
    
    begin
      self[:reset_password_token] = SecureRandom.urlsafe_base64
    end while ProductCustomer.exists?(:reset_password_token => self[:reset_password_token])
    
      self.reset_password_sent_at = Time.now
      self.save(:validate => false)
      ProductCustomerMailer.password_reset(self).deliver
  end
  
  
  def generate_token
    begin
      self.token = SecureRandom.urlsafe_base64
      puts self.token
    end while ProductCustomer.exists?(:token => self.token)
  end
  

end
