class JamRuby::AffiliateReferralVisit < ActiveRecord::Base

  belongs_to :affiliate_partner, class_name: 'JamRuby::AffiliatePartner', inverse_of: :visits

  validates :affiliate_partner_id, numericality: {only_integer: true}, :allow_nil => true
  validates :visited_url, length: {maximum: 1000}
  validates :referral_url, length: {maximum: 1000}
  validates :ip_address, presence: true, length: {maximum:1000}
  validates :first_visit, inclusion: {in: [true, false]}
  validates :user_id, length: {maximum:64}

  def self.track(options = {})
    visit = AffiliateReferralVisit.new
    visit.affiliate_partner_id = options[:affiliate_id]
    visit.ip_address = options[:remote_ip]
    visit.visited_url = options[:visited_url]
    visit.referral_url = options[:referral_url]
    visit.first_visit = options[:visited].nil?
    visit.user_id = options[:current_user].id if options[:current_user]
    visit.save
    visit
  end
end
