Module: Hippo::Access

Defined in:
lib/hippo/access.rb,
lib/hippo/access/role.rb,
lib/hippo/access/version.rb,
lib/hippo/access/locked_fields.rb,
lib/hippo/access/roles/support.rb,
lib/hippo/access/role_collection.rb,
lib/hippo/access/roles/basic_user.rb,
lib/hippo/access/roles/administrator.rb

Defined Under Namespace

Modules: Roles Classes: LockedFields, Role, RoleCollection

Constant Summary collapse

VERSION =
0.2

Class Method Summary collapse

Class Method Details

.for_user(user) ⇒ Object


17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
# File 'lib/hippo/access.rb', line 17

def for_user( user )
    {
        :roles => user.roles.map{ | role |
            {
                type: type_to_client_id(role),
                read: role.read_types.map{ |klass| type_to_client(klass) },
                write: role.write_types.map{ |klass| type_to_client(klass) },
                delete: role.delete_types.map{ |klass| type_to_client(klass) }
            }
        },
        :locked_fields => LockedFields.definitions.map{ | klass, locks |
            {
                type: type_to_client(klass),
                locks: locks.each_with_object({}) do |(field, grants), result|
                    result[field] = grants.map do |grant|
                        { role: type_to_client_id(grant[:role]), only: grant[:only] }
                    end
                end
            }
        }
    }
end

.type_to_client(klass) ⇒ Object


9
10
11
# File 'lib/hippo/access.rb', line 9

def type_to_client(klass)
    klass.to_s.sub(/^(\w+).*?(\w+)$/,'\1.Models.\2')
end

.type_to_client_id(klass) ⇒ Object


13
14
15
# File 'lib/hippo/access.rb', line 13

def type_to_client_id(klass)
    ( klass.is_a?(Class) ? klass : klass.class ).to_s.demodulize.underscore
end