cirandas.net

ref: master

lib/extensions/role_assignment.rb


 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
require_relative '../cache_counter'

class RoleAssignment

  extend CacheCounter

  after_create do |role_assignment|
    accessor = role_assignment.accessor
    resource = role_assignment.resource
    if resource.kind_of?(Organization)
      #FIXME This will only work as long as the role_assignment associations
      #happen only between profiles, due to the polymorphic column type.
      if resource.role_assignments.where(:accessor_id => accessor.id).count == 1
        RoleAssignment.update_cache_counter(:members_count, resource, 1)
      end
    end
  end

  after_destroy do |role_assignment|
    accessor = role_assignment.accessor
    resource = role_assignment.resource
    if resource.kind_of?(Organization)
      #FIXME This will only work as long as the role_assignment associations
      #happen only between profiles, due to the polymorphic column type.
      if resource.role_assignments.where(:accessor_id => accessor.id).count == 0
        RoleAssignment.update_cache_counter(:members_count, resource, -1)
      end
    end
  end

end