Class: Policy

Inherits:
Object
  • Object
show all
Defined in:
lib/adapters/model.rb,
lib/egoist/base.rb,
lib/egoist/error.rb,
lib/egoist/error.rb,
lib/egoist/proxy.rb,
lib/adapters/controller.rb

Overview

include Policy::Controller

Defined Under Namespace

Modules: Controller, Model Classes: Error, Proxy

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(model:, user: nil) ⇒ Policy

Returns a new instance of Policy.



4
5
6
7
# File 'lib/egoist/base.rb', line 4

def initialize model:, user: nil
  @model = model
  @user  = user || Policy.current_user
end

Instance Attribute Details

#actionObject (readonly)

Returns the value of attribute action.



2
3
4
# File 'lib/egoist/base.rb', line 2

def action
  @action
end

#modelObject (readonly)

Returns the value of attribute model.



2
3
4
# File 'lib/egoist/base.rb', line 2

def model
  @model
end

#userObject (readonly)

Returns the value of attribute user.



2
3
4
# File 'lib/egoist/base.rb', line 2

def user
  @user
end

Class Method Details

.can(model = nil, user = nil) ⇒ Object



16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
# File 'lib/egoist/proxy.rb', line 16

def can model = nil, user = nil
  if model.is_a?(Hash)
    model, user = model[:model], model[:user]
  end

  klass = self

  # if we are calling can on Policy class, figure out policy class
  if self == Policy
    klass = ('%s_policy' % model.class).classify
    klass = Object.const_defined?('::%s' % klass) ? klass.constantize : raise('Policy class %s not defined' % klass)
  end

  klass.new(user: user, model: model).can
end

.current_userObject

try to load current user



4
5
6
7
8
9
10
11
12
13
14
# File 'lib/egoist/proxy.rb', line 4

def current_user
  if Thread.current.key?(:current_user)
    Thread.current[:current_user]
  elsif defined?(User) && User.respond_to?(:current)
    User.current
  elsif defined?(Current) && Current.respond_to?(:user)
    Current.user
  else
    raise RuntimeError.new('Current user not found in Policy#current_user')
  end
end

.error(msg) ⇒ Object

Raises:



8
9
10
# File 'lib/egoist/error.rb', line 8

def error msg
  raise ::Policy::Error.new(msg)
end

Instance Method Details

#canObject



30
31
32
# File 'lib/egoist/base.rb', line 30

def can
  Proxy.new self
end

#can?(action, *args, &block) ⇒ Boolean

pass block if you want to handle errors yourself return true if false if block is passed

Returns:

  • (Boolean)


11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
# File 'lib/egoist/base.rb', line 11

def can? action, *args, &block
  @action = action
    .to_s
    .gsub(/[^\w+]/, '')
    .concat('?')
    .to_sym

  # pre check
  if %i(can).index(@action)
    raise RuntimeError.new('Method name not allowed')
  end

  unless respond_to?(@action)
    raise NoMethodError.new(%[Policy check "#{@action}" not found in #{self.class}])
  end

  call *args, &block
end

#error(message) ⇒ Object

Raises:



15
16
17
# File 'lib/egoist/error.rb', line 15

def error message
  raise Policy::Error.new(message)
end