Module: BravoGuard::Model::ObjectMethods

Defined in:
lib/bravo_guard/model.rb

Overview

ObjectMethods to avoid deprecation warnings on InstanceMethods

Instance Method Summary collapse

Instance Method Details

#allows?(actor, *permissions) ⇒ Boolean

nodoc

Returns:

  • (Boolean)


46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
# File 'lib/bravo_guard/model.rb', line 46

def allows?(actor, *permissions)
  begin
    permission = permission_name(permissions.join('_'))
    method_name = [:allows, permission].join('_') + '?'
    self.actor = actor
    check_anon_permissions!(permission)
    send method_name
  rescue BravoGuard::PermissionDenied
    return false
  rescue BravoGuard::PermissionGranted
    return true
  ensure
    self.actor = nil
  end
end

#anon_permissionsObject

nodoc



65
66
67
# File 'lib/bravo_guard/model.rb', line 65

def anon_permissions
  []
end

#check_anon_permissions!(permission) ⇒ Object

auto-reject anons



71
72
73
74
# File 'lib/bravo_guard/model.rb', line 71

def check_anon_permissions!(permission)
  return unless actor.nil?
  no! unless anon_permissions.include?(permission)
end

#no!Object

shorthand for ‘return false’. allows?() will catch this and return false



78
79
80
# File 'lib/bravo_guard/model.rb', line 78

def no!
  raise BravoGuard::PermissionDenied
end

#permission_name(permission) ⇒ Object

nodoc



90
91
92
93
94
95
96
97
98
99
# File 'lib/bravo_guard/model.rb', line 90

def permission_name(permission)
  case permission
    when 'new' then :create
    when 'edit' then :update
    when 'delete' then :destroy
    when 'show' then :read
    when 'index' then :list
    else permission.to_s.to_sym
  end
end

#yes!Object

see no!()



84
85
86
# File 'lib/bravo_guard/model.rb', line 84

def yes!
  raise BravoGuard::PermissionGranted
end