class User < ApplicationRecord
  # Include default devise modules. Others available are:
  # :confirmable, :lockable, :timeoutable, :trackable and :omniauthable
  devise :database_authenticatable, :registerable,
         :recoverable, :rememberable, :validatable

  has_many :user_roles, dependent: :destroy
  has_many :roles, through: :user_roles

  def can_edit?(resource)
    resource_class = resource.class.to_s == "Class" ? resource.name : resource.class.to_s
    role = roles.where(reference: resource_class)
    return false unless role

    role.map(&:editable?).any? || role.map(&:createable?).any?
  end

  def can_create?(resource)
    resource_class = resource.class.to_s == "Class" ? resource.name : resource.class.to_s
    role = roles.where(reference: resource_class)
    return false unless role

    role.map(&:createable?).any?
  end
end