Created
March 8, 2012 16:00
-
-
Save anonymous/2001689 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# encoding: utf-8 | |
class User < ActiveRecord::Base | |
# Include default devise modules. Others available are: | |
# :token_authenticatable, :encryptable, :confirmable, :lockable, :timeoutable and :omniauthable | |
devise :database_authenticatable, :registerable, | |
:recoverable, :rememberable, :trackable#, :validatable | |
has_many :user_roles | |
has_many :roles, :through => :user_roles | |
has_many :orders | |
after_create :assign_affiliate_role, :send_account_data_to_user | |
# Role checking | |
def has_role?(*role_names) | |
self.roles.where(:name => role_names).present? | |
end | |
def client? | |
self.has_role? 'client' | |
end | |
def affiliate? | |
self.has_role? 'affiliate' | |
end | |
def admin? | |
self.has_role? 'admin' | |
end | |
def master? | |
self.has_role? 'master' | |
end | |
def pending_orders? | |
if Order.where(:status => Order::PENDING, :user_id => self.id).first | |
return true | |
else | |
return false | |
end | |
end | |
private | |
def assign_affiliate_role | |
# If the user has no orders should be an affiliate | |
if self.orders.empty? | |
# Assign afiliate role | |
role_asignation = UserRole.new | |
role_asignation.role_id = 2 | |
role_asignation.user = self | |
role_asignation.save | |
end | |
end | |
def send_account_data_to_user | |
if self.orders.empty? | |
AffiliateMailer.access_data(self).deliver | |
else | |
ClientMailer.access_data(self).deliver | |
end | |
end | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment