Module: Ratify::ClassMethods

Defined in:
lib/ratify.rb

Overview

The methods in this module are added to the object’s class methods when Ratify is included.

Instance Method Summary collapse

Instance Method Details

#permissionsArray<Permission>

All of the permissions for the object.

Returns:

  • (Array<Permission>)

    List of permissions.



29
30
31
# File 'lib/ratify.rb', line 29

def permissions
  @permissions ||= []
end

#permit(object, *actions, **conditions) ⇒ Array<Permission>

Creates a new permission for the object.

Examples:

Basic usage

include Ratify
permit User, :create, :read, :update, :destroy, if: :admin?

Returns:

  • (Array<Permission>)

    All of the permissions after they’re updated.



40
41
42
# File 'lib/ratify.rb', line 40

def permit(object, *actions, **conditions)
  permissions << Permission.new(object, *actions, **conditions)
end

#permits?(object, *actions, scope: nil) ⇒ true | false

Checks if the given object is allowed to perform the requested action(s).

Parameters:

  • object (Object)

    The object requesting the action(s).

  • actions (*Symbol)

    The action(s) being requested.

  • scope (Object) (defaults to: nil)

    The scope of permissible object.

Returns:

  • (true | false)

    Whether or not the object is permitted.



50
51
52
# File 'lib/ratify.rb', line 50

def permits?(object, *actions, scope: nil)
  permissions.any? { |m| m.permits?(object, *actions, scope: scope) }
end