Sha256: 4632307253c866c1d9460ba203b60ba7f95c93ba9e1ce4de9b08770e9df6107c

Contents?: true

Size: 1.07 KB

Versions: 5

Compression:

Stored size: 1.07 KB

Contents

class User < ActiveRecord::Base
  # Include default devise modules. Others available are:
  # :token_authenticatable, :confirmable, :lockable and :timeoutable
  devise :database_authenticatable, :recoverable, :rememberable, :trackable, :validatable

  # Setup accessible (or protected) attributes for your model
  attr_accessible :email, :password, :password_confirmation, :remember_me
  
  acts_as_authorization_subject :association_name => :roles
  
  scope :admins, where("roles.name = ?" , "admin").includes(:roles)
  
  def role_list
    roles = Role.all
    user_roles = roles.map{|r| r if self.has_role?(r.name) }
  end
  
  def last_admin?
    self.has_role?("admin") and User.admins.size == 1 ? true : false
  end
  
  def set_roles(roles)    
    if self.last_admin?
      self.has_no_roles!
      self.has_role!("admin")
    else
      self.has_no_roles!
    end
    
    return unless roles
    for role in roles
      self.has_role!(role)
    end
  end  
  
  protected
  
  def password_required?
    !persisted? || password.present? || password_confirmation.present?
  end
  
end

Version data entries

5 entries across 5 versions & 1 rubygems

Version Path
merrycms-0.1.7 app/models/user.rb
merrycms-0.1.4 app/models/user.rb
merrycms-0.1.2 app/models/user.rb
merrycms-0.1.1 app/models/user.rb
merrycms-0.1.0 app/models/user.rb