GeekPark/gpk_account

View on GitHub
app/models/concerns/has_role.rb

Summary

Maintainability
A
0 mins
Test Coverage
module HasRole
  extend ActiveSupport::Concern

  included do
    before_save :ensure_roles_existing
    before_save :ensure_roles_unique

    scope :filter_role,  ->(x) { where('? = ANY(roles)', x)  }
    scope :exclude_role, ->(x) { where('? <> ALL(roles)', x) }
  end

  def admin?
    'admin'.in? roles
  end

  def ensure_roles_existing
    return if roles.present?
    roles << Role.default
  end

  def ensure_roles_unique
    roles.uniq!
  end
end