class PetitionSignator < ActiveRecord::Base
  belongs_to :petition
  belongs_to :lang_site

  
  has_one :registrant
  attr_accessor :email_confirmation
  #attr_accessible :address, :address2, :city, :enabled, :token, :country, :cp, :email, :email_confirmation, :firstname, :name, :petition_id
  
  
  
  belongs_to :mail_profile
  
  accepts_nested_attributes_for :mail_profile
  
  
  
  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, :petition_signator_id => self.id, :lang_site_id => self.lang_site_id) if self.newsletter
  end
  
  def export_lang
    if self.lang_site_id
      if self.lang_site_id == 1
        "fr"
      elsif self.lang_site_id == 2
        "en"
      end

    end
  end

  def address_long
    address.to_s + " " + address2.to_s
  end

  def export_petition
    self.petition.name if self.petition
  end
  
  
end
