13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
|
# File 'lib/sunrise/models/user.rb', line 13
def self.extended(base)
base.class_eval do
has_many :roles, :dependent => :delete_all, :autosave => true
has_one :avatar, :as => :assetable, :dependent => :destroy, :autosave => true
before_validation :generate_login, :if => :has_login?
before_create :set_default_role, :if => :roles_empty?
validates_presence_of :name
scope :with_email, lambda {|email| where(["email LIKE ?", "#{email}%"]) }
scope :with_name, lambda {|name| where(["name LIKE ?", "#{name}%"]) }
scope :with_role, lambda {|role_id| joins(:roles).merge(::Role.with_type(role_id)) }
scope :defaults, with_role(::RoleType.default.id)
scope :moderators, with_role(::RoleType.moderator.id)
scope :admins, with_role(::RoleType.admin.id)
end
end
|