Sha256: ec21b6e36f0a454068d0bc1337ce08de29d38de74721d89d48d93aa3732eb3d0

Contents?: true

Size: 1.37 KB

Versions: 7

Compression:

Stored size: 1.37 KB

Contents

module IAmICan
  module Subject
    module RoleQuerying

      # === Role Querying ===

      def is? role_name
        roles.find_by(name: role_name).present?
      end

      alias is_role?  is?
      alias has_role? is?

      def isnt? role
      !is? role
      end

      def is! role
        raise VerificationFailed if isnt? role
        true
      end

      alias is_role!  is!
      alias has_role! is!

      def is_one_of? *role_names
        roles.where(name: role_names).present?
      end

      alias is_one_of_roles? is_one_of?

      def is_one_of! *roles
        raise VerificationFailed unless is_one_of? *roles
      end

      alias is_one_of_roles! is_one_of!

      def is_every? *role_names
        roles.where(name: role_names).count == role_names.count
      end

      alias is_every_role_in? is_every?

      def is_every! *roles
        roles.each { |role| is! role } && true
      end

      alias is_every_role_in! is_every!

      # === Group Querying ===

      def is_in_role_group? name
        group_members = i_am_i_can.role_group_model.find_by!(name: name)._roles.names
        (roles.names & group_members).present?
      end

      alias in_role_group? is_in_role_group?

      def is_in_one_of? *group_names
        group_names.each { |name| return true if is_in_role_group? name } && false
      end

      alias in_one_of? is_in_one_of?
    end
  end
end

Version data entries

7 entries across 7 versions & 1 rubygems

Version Path
i_am_i_can-4.3.3 lib/i_am_i_can/subject/role_querying.rb
i_am_i_can-4.3.2 lib/i_am_i_can/subject/role_querying.rb
i_am_i_can-4.3.1 lib/i_am_i_can/subject/role_querying.rb
i_am_i_can-4.3.0 lib/i_am_i_can/subject/role_querying.rb
i_am_i_can-4.2.0 lib/i_am_i_can/subject/role_querying.rb
i_am_i_can-4.1.0 lib/i_am_i_can/subject/role_querying.rb
i_am_i_can-4.0.0 lib/i_am_i_can/subject/role_querying.rb