class MailProfile < ActiveRecord::Base



  validates :email, :presence => true, :format => /\A([^@\s]+)@((?:[-a-z0-9]+\.)+[a-z]{2,})\z/i, :if => :force_email?#, :uniqueness => { :scope => :petition_id, :message => "Vous avez déjà signé cette pétition avec cette adresse email" }

  validates :name, :presence => true, :if => :force_particular?
  validates :firstname, :presence => true, :if => :force_particular?
 # validates :address, :presence => true
  validates :cp, :presence => true, :if => :force_particular?
  validates :city, :presence => true, :if => :force_particular?
  validates :country, :presence => true, :if => :force_particular?
  #validates :phone, :presence => true
  
  attr_accessor :force_particular, :skip_email
  
  
  def force_email?
    true if !self.skip_email
  end
  def force_particular?
    true if self.force_particular
  end
  
  before_create do
    self.token = (Digest::MD5.hexdigest "#{SecureRandom.hex(10)}-#{DateTime.now.to_s}")
  end
  
  after_create do
    #Registrant.create(:email => self.email, :petition_signator_id => self.id, :lang_site_id => self.lang_site_id) if self.newsletter
  end

end
