Class: ActionPermission::Base

Inherits:
Object
  • Object
show all
Defined in:
lib/action_permission/base.rb

Direct Known Subclasses

ApplicationPermission

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(membership) ⇒ Base

Returns a new instance of Base.



18
19
20
# File 'lib/action_permission/base.rb', line 18

def initialize(membership)
  load membership
end

Instance Attribute Details

#allowed_actionsObject (readonly)

Returns the value of attribute allowed_actions.



16
17
18
# File 'lib/action_permission/base.rb', line 16

def allowed_actions
  @allowed_actions
end

#allowed_paramsObject (readonly)

Returns the value of attribute allowed_params.



16
17
18
# File 'lib/action_permission/base.rb', line 16

def allowed_params
  @allowed_params
end

#membershipObject

Returns the value of attribute membership.



15
16
17
# File 'lib/action_permission/base.rb', line 15

def membership
  @membership
end

Class Method Details

.match_with(source, to) ⇒ Object



7
8
9
10
11
# File 'lib/action_permission/base.rb', line 7

def match_with source,to
  Array(to).each do |role|
    alias_method role, source
  end
end

Instance Method Details

#allow(actions, &block) ⇒ Object



35
36
37
38
39
40
# File 'lib/action_permission/base.rb', line 35

def allow(actions, &block)
  @allowed_actions ||= {}
  Array(actions).each do |action|
    @allowed_actions[action.to_s] = block || true
  end
end

#allow?(action, resource = nil) ⇒ Boolean

Returns:

  • (Boolean)


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

def allow?(action, resource = nil)
  allowed = @allowed_actions[action.to_s] if @allowed_actions
  allowed && (allowed == true || resource && allowed.call(resource))
end

#allow_params(options = nil) ⇒ Object



50
51
52
53
54
55
56
57
58
# File 'lib/action_permission/base.rb', line 50

def allow_params options=nil
  @allowed_params ||= []

  if options
    @allowed_params = allow_params_with_options options
  else
    @allowed_params = Array(params)
  end
end

#allow_rest_actions(&block) ⇒ Object



42
43
44
# File 'lib/action_permission/base.rb', line 42

def allow_rest_actions(&block)
  allow [:index, :new, :create, :show, :edit, :update, :destroy], &block
end

#load(membership) ⇒ Object



22
23
24
25
26
27
28
# File 'lib/action_permission/base.rb', line 22

def load(membership)
  @membership = membership
  role = @membership.identify
  if role && respond_to?(role)
    send(role)
  end
end

#paramsObject



46
47
48
# File 'lib/action_permission/base.rb', line 46

def params
  []
end