Sha256: 69afa12538553f15e42236a0631470caa86d1b58e2f1d2e707739c8894a05ee6

Contents?: true

Size: 871 Bytes

Versions: 2

Compression:

Stored size: 871 Bytes

Contents

# frozen_string_literal: true

require 'i_am_i_can/role/definition'
require 'i_am_i_can/role/assignment'
require 'i_am_i_can/role/grouping'
require 'i_am_i_can/role_group/definition'

module IAmICan
  module Role
    extend ActiveSupport::Concern

    class_methods do
      def which(name:, **conditions)
        find_by!(name: name, **conditions)
      end

      def names
        self.pluck(:name).map(&:to_sym)
      end

      def can?(action, obj)
        send("with_#{_reflect_of('permission')}?",
             i_am_i_can.permission_model.matched(action, obj).ids)
      end
    end

    included do
      define_model_callbacks :permission_assign, :cancel_permission_assign, :permission_update

      # `can? :manage, User` / `can? :manage, obj: User`
      def can? action, o = nil, obj: o
        _permissions.matched?(action, obj)
      end
    end
  end
end

Version data entries

2 entries across 2 versions & 1 rubygems

Version Path
i_am_i_can-4.4.1 lib/i_am_i_can/role.rb
i_am_i_can-4.4.0 lib/i_am_i_can/role.rb