class PetitionSignator < ActiveRecord::Base
  belongs_to :petition
  belongs_to :lang_site
  attr_accessor :email_confirmation
  #attr_accessible :address, :address2, :city, :enabled, :token, :country, :cp, :email, :email_confirmation, :firstname, :name, :petition_id
  
  validates :email, :presence => true, :uniqueness => { :scope => :petition_id, :message => "Vous avez déjà signé cette pétition avec cette adresse email" }

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

end
