ruby-on-railsrolify

How to check for roles using rolify gem?


I have a user model, where I am having methods to check for roles. I have total 5 to 6 roles. Super administrator should have access to view users with all the roles. I am using rolify gem (has_role?) for checking admin role. Can someone guide me on how to use it? As of now, I am getting undefined method include? error.

class User < ActiveRecord::Base
  extend FriendlyId
  self.table_name = "DIM_USER"
  self.primary_key = "user_id"
  self.sequence_name = 'DIM_USER_ID_SEQ'

  rolify
  devise :database_authenticatable,
         :recoverable, :rememberable, :trackable, :validatable, :timeoutable

  before_validation :strip_whitespace, :only => [:email]
  default_scope {where(clean_up_flag: false)}
  has_many :store_user_assignments
  has_many :stores, through: :store_user_assignments
  has_and_belongs_to_many :clients, join_table: :clients_users
  has_many :store_user_assignments
  has_many :stores, through: :store_user_assignments
  belongs_to :role

  before_save {self.email = email.downcase}
  before_save :update_full_name
  after_create :create_slug

  friendly_id :slug, use: :slugged

  NAME_MIN_LENGTH = 1
  NAME_MAX_LENGTH = 100
  EMAIL_MAX_LENGTH = 100
  NAME_RANGE = NAME_MIN_LENGTH..NAME_MAX_LENGTH

  validate :password_check
  validates :encrypted_password, presence: true

  scope :admin, -> {joins(:users_roles, :DIM_ROLE).where("users_roles.role_id = DIM_ROLE.role_id AND DIM_ROLE.name = 'super_administrator'").order(:last_name)}
  scope :pmt_ptl_accnt_manager, -> {joins(:users_roles, :DIM_ROLE).where("users_roles.role_id = DIM_ROLE.role_id AND DIM_ROLE.name = 'Portal-Account-Manager-Client'").order(:last_name)}
  scope :inactive_pmt_ptl_accnt_manager_with_no_stores, -> {joins(:users_roles, :DIM_ROLE).where("users.active=? AND users_roles.role_id = DIM_ROLE.role_id AND DIM_ROLE.name = ? AND users.user_id NOT IN (select user_id from stores_users)", false, 'Portal-Account-Manager-Client').order(:last_name)}

  def active_for_authentication?
    super && self.active?
  end

  def inactive_message
    :invalid
  end

  def update_full_name
    self.full_name = "#{first_name} #{last_name}"
  end

  def admin?
    self.has_role?(:super_administrator)
  end

  def vt_user?
    self.has_role?(:Virtual-Terminal-User)
  end


  def pmt_ptl_accnt_manager?
    self.role.include?(Role.where(:name => 'Portal-Account-Manager-Client').first) ||
        self.role.include?(Role.where(:name => 'Radial-Account-Manager').first)
  end

  def radial_account_manager?
    self.role.include?(Role.where(:name => 'Radial-Account-Manager').first)
  end

  def payments_portal_readonly?
    self.role.include?(Role.where(:name => 'Portal-Account-Manager-Read-Only-Client').first)
  end

  def search_user?
    self.role.include?(Role.where(:name => 'Transaction-Search-Only').first)
  end

  def radial_readonly?
    self.role.include?(Role.where(:name => 'Radial_ReadOnly').first)
  end

end

Solution

  • According to the documentation, you should call has_role? method.

     def pmt_ptl_accnt_manager?
        self.has_role?('Portal-Account-Manager-Client') || self.has_role?('Radial-Account-Manager')
     end