Class: Motion::Authorization

Inherits:
Object
  • Object
show all
Defined in:
lib/motion/authorization.rb

Defined Under Namespace

Modules: Methods

Class Attribute Summary collapse

Class Method Summary collapse

Class Attribute Details

.current_userObject

Returns the value of attribute current_user.



5
6
7
# File 'lib/motion/authorization.rb', line 5

def current_user
  @current_user
end

Class Method Details

.can?(action, object) ⇒ Boolean

Returns:

  • (Boolean)


19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
# File 'lib/motion/authorization.rb', line 19

def can?(action, object)
  policy_class_name = "#{object.class}Policy"

  unless Object.const_defined?(policy_class_name)
    puts "Undefined permissions policy class #{policy_class_name}"
    return false
  end

  policy = policy_class_name.constantize.new(current_user, object)

  unless policy.respond_to?("#{action}?")
    puts "No #{action}? method found in #{policy_class_name}"
    return false
  end

  policy.send "#{action}?"
end

.current_user_method(&block) ⇒ Object



7
8
9
# File 'lib/motion/authorization.rb', line 7

def current_user_method(&block)
  @current_user_block = block
end