Class: Lanes::Access::RoleCollection

Inherits:
Object
  • Object
show all
Includes:
Enumerable
Defined in:
lib/lanes/access/role_collection.rb

Instance Method Summary collapse

Constructor Details

#initialize(user) ⇒ RoleCollection

Returns a new instance of RoleCollection.



7
8
9
10
11
12
# File 'lib/lanes/access/role_collection.rb', line 7

def initialize(user)
    @role_names = user.role_names
    @roles = user.role_names.map{ |name|
        "Lanes::Access::Roles::#{name.classify}".safe_constantize
    }.compact.map{ |klass| klass.new(user) }
end

Instance Method Details

#can_delete?(model, id) ⇒ Boolean

Returns Can the User delete the model?.

Parameters:

  • model (Lanes::Model)
  • id (Fixnum)

    the id of the record to remove

Returns:

  • (Boolean)

    Can the User delete the model?



43
44
45
46
# File 'lib/lanes/access/role_collection.rb', line 43

def can_delete?(model,id)
    klass=model_to_class(model)
    @roles.each{ |role| role.can_delete?(klass) }
end

#can_read?(model, attribute = nil) ⇒ Boolean

Returns Can the User view the model?.

Parameters:

  • model (Lanes::Model)
  • attribute (Symbol) (defaults to: nil)

Returns:

  • (Boolean)

    Can the User view the model?



27
28
29
30
# File 'lib/lanes/access/role_collection.rb', line 27

def can_read?(model, attribute = nil)
    klass=model_to_class(model)
    test_access(klass, attribute, :read){ |role| role.can_read?(klass) }
end

#can_write?(model, attribute = nil) ⇒ Boolean

Returns Can the User create and update the model?.

Parameters:

  • model (Lanes::Model)
  • attribute (Symbol) (defaults to: nil)

Returns:

  • (Boolean)

    Can the User create and update the model?



35
36
37
38
# File 'lib/lanes/access/role_collection.rb', line 35

def can_write?(model, attribute = nil)
    klass=model_to_class(model)
    test_access(klass, attribute, :write){ |role| role.can_write?(klass) }
end

#eachObject



53
54
55
# File 'lib/lanes/access/role_collection.rb', line 53

def each
    @roles.each{|r| yield r}
end

#exposed_dataObject



14
15
16
# File 'lib/lanes/access/role_collection.rb', line 14

def exposed_data
    @role_names
end

#include?(role) ⇒ Boolean

Returns Does a role with the given id exist?.

Parameters:

  • role (String)

Returns:

  • (Boolean)

    Does a role with the given id exist?



20
21
22
# File 'lib/lanes/access/role_collection.rb', line 20

def include?(role)
    @role_names.include?(role)
end

#to_symArray<symbol>

Returns list of roles.

Returns:

  • (Array<symbol>)

    list of roles



49
50
51
# File 'lib/lanes/access/role_collection.rb', line 49

def to_sym
    @roles.map{ |r| r.class.to_s.demodulize.downcase.to_sym }
end