kaps_app/app/models/p_friend.rb
2020-01-11 15:28:56 +01:00

40 lines
1.2 KiB
Ruby

class PFriend < ActiveRecord::Base
belongs_to :p_customer
belongs_to :p_friend_customer, :class_name => "PCustomer"
attr_accessor :email
validates :email, :presence => true, on: :create
validates :p_customer_id, :presence => true
validates :p_friend_customer_id, :presence => true
before_validation do
if !self.id
p_friend_customer = PCustomer.where(:email => self.email).first
if p_friend_customer
self.p_friend_customer = p_friend_customer
if self.p_customer.p_friends.where(:p_friend_customer_id => p_friend_customer.id).first or p_friend_customer.id == self.p_customer.id
errors.add(:email, 'Vous avez déjà ajouté cet ami.')
end
else
errors.add(:email, 'Aucun compte avec cet email n\'a été trouvé.')
end
end
end
after_create do
PFriend.create(:p_customer_id => self.p_friend_customer_id, :p_friend_customer_id => self.p_customer_id, :email => self.p_customer.email)
end
def confirm
self.enabled = true
self.save
PFriend.where(:p_customer_id => self.p_friend_customer_id, :p_friend_customer_id => self.p_customer_id).update_all(:enabled => true)
end
end