Skip to content
This repository

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
tree: 31a1d711a9
Fetching contributors…

Cannot retrieve contributors at this time

file 48 lines (40 sloc) 1.355 kb
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48
module Allowy

  # This module provides the default and common context to for checking the permissions.
  # It is mixed into the Controller in Rails by default and provides an wasy way to reuse it
  # in other parts of the application (in RSpec or Cucumber) without needint a controller.
  # For example, you can use this code in your Cucumber features:
  #
  # @example
  # class CustomContext
  # include Allowy::Context
  # attr_accessor :current_user
  #
  # def initialize(user)
  # @current_user = user
  # end
  #
  # And the you can easily check the permissions simply using something like:
  #
  # @example
  # CustomContext.new(that_user).should be_able_to :create, Blog
  module Context
    extend ActiveSupport::Concern

    module InstanceMethods

      def allowy_context
        self
      end

      def current_allowy
        @current_allowy ||= ::Allowy::Registry.new(allowy_context)
      end

      def can?(action, subject, *args)
        current_allowy.access_control_for!(subject).can?(action, subject, *args)
      end

      def cannot?(*args)
        current_allowy.access_control_for!(subject).cannot?(action, subject, *args)
      end

      def authorize!(action, subject, *args)
        current_allowy.access_control_for!(subject).authorize!(action, subject, *args)
      end
    end
  end

end
Something went wrong with that request. Please try again.