Class: Aegis::Action

Inherits:
Object
  • Object
show all
Defined in:
lib/aegis/action.rb

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(name, options) ⇒ Action

Returns a new instance of Action.



6
7
8
9
10
# File 'lib/aegis/action.rb', line 6

def initialize(name, options)
  @name = name.to_s
  @sieves = []
  update(options, true)
end

Instance Attribute Details

#nameObject (readonly)

Returns the value of attribute name.



4
5
6
# File 'lib/aegis/action.rb', line 4

def name
  @name
end

#pluralize_resourceObject (readonly)

Returns the value of attribute pluralize_resource.



4
5
6
# File 'lib/aegis/action.rb', line 4

def pluralize_resource
  @pluralize_resource
end

#sievesObject (readonly)

Returns the value of attribute sieves.



4
5
6
# File 'lib/aegis/action.rb', line 4

def sieves
  @sieves
end

#takes_objectObject (readonly)

Returns the value of attribute takes_object.



4
5
6
# File 'lib/aegis/action.rb', line 4

def takes_object
  @takes_object
end

#takes_parent_objectObject (readonly)

Returns the value of attribute takes_parent_object.



4
5
6
# File 'lib/aegis/action.rb', line 4

def takes_parent_object
  @takes_parent_object
end

#writingObject (readonly)

Returns the value of attribute writing.



4
5
6
# File 'lib/aegis/action.rb', line 4

def writing
  @writing
end

Class Method Details

.allow_to_allObject



60
61
62
63
64
# File 'lib/aegis/action.rb', line 60

def self.allow_to_all
  action = undefined
  action.sieves << Aegis::Sieve.allow_to_all
  action
end

.create(options = {}) ⇒ Object



48
49
50
# File 'lib/aegis/action.rb', line 48

def self.create(options = {})
  new('create', options.reverse_merge(:takes_object => false, :writing => true))
end

.deny_to_allObject



66
67
68
69
70
# File 'lib/aegis/action.rb', line 66

def self.deny_to_all
  action = undefined
  action.sieves << Aegis::Sieve.deny_to_all
  action
end

.destroy(options = {}) ⇒ Object



52
53
54
# File 'lib/aegis/action.rb', line 52

def self.destroy(options = {})
  new('destroy', options.reverse_merge(:takes_object => true, :writing => true))
end

.index(options = {}) ⇒ Object



36
37
38
# File 'lib/aegis/action.rb', line 36

def self.index(options = {})
  new('index', options.reverse_merge(:takes_object => false, :pluralize_resource => true, :writing => false))
end

.show(options = {}) ⇒ Object



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

def self.show(options = {})
  new('show', options.reverse_merge(:takes_object => true, :writing => false))
end

.undefinedObject



56
57
58
# File 'lib/aegis/action.rb', line 56

def self.undefined
  new(nil, :takes_object => false, :writing => true)
end

.update(options = {}) ⇒ Object



44
45
46
# File 'lib/aegis/action.rb', line 44

def self.update(options = {})
  new('update', options.reverse_merge(:takes_object => true, :writing => true))
end

Instance Method Details

#abstract?Boolean

Returns:

  • (Boolean)


72
73
74
# File 'lib/aegis/action.rb', line 72

def abstract?
  name.blank?
end

#inspectObject



76
77
78
# File 'lib/aegis/action.rb', line 76

def inspect
  "Action(#{{ :name => name, :takes_object => takes_object, :takes_parent_object => takes_parent_object,  :sieves => sieves }.inspect})"
end

#may!(user, *args) ⇒ Object



32
33
34
# File 'lib/aegis/action.rb', line 32

def may!(user, *args)
  may?(user, *args) or raise Aegis::AccessDenied, "Access denied: #{args.inspect}"
end

#may?(user, *args) ⇒ Boolean

Returns:

  • (Boolean)


25
26
27
28
29
30
# File 'lib/aegis/action.rb', line 25

def may?(user, *args)
  context = extract_context(user, args)
  user.roles.any? do |role|
    may_as_role?(role, context, *args)
  end
end

#update(options, use_defaults = false) ⇒ Object



12
13
14
15
16
17
# File 'lib/aegis/action.rb', line 12

def update(options, use_defaults = false)
  update_attribute(options, :takes_object, use_defaults, true)
  update_attribute(options, :takes_parent_object, use_defaults, false)
  update_attribute(options, :writing, use_defaults, true)
  update_attribute(options, :pluralize_resource, use_defaults, false)
end

#update_attribute(options, key, use_defaults, default) ⇒ Object



19
20
21
22
23
# File 'lib/aegis/action.rb', line 19

def update_attribute(options, key, use_defaults, default)
  value = options[key]
  value = default if value.nil? && use_defaults
  instance_variable_set("@#{key}", value) unless value.nil?
end