Class: Authorizer::ObjectObserver

Inherits:
ActiveRecord::Observer
  • Object
show all
Defined in:
lib/authorizer/object_observer.rb

Overview

Observes users and deleted any associated ObjectRole objects when the user gets deleted.

Instance Method Summary collapse

Instance Method Details

#after_destroy(object) ⇒ Object

W DONT DO DIZ let’s use before_destroy instead of after_destroy. More chance it will still have an ID >:)))))))))) :‘) :DDDDDDDDDDDDDDDDDDDDDDD W DONT DO DIZ



11
12
13
14
15
16
17
18
19
# File 'lib/authorizer/object_observer.rb', line 11

def after_destroy(object)
  return nil if object.is_a?(User) # Users are covered by the other observer class.
  # Find all ObjectRole records that point to this object.
  object_roles = ObjectRole.find_all_by_object(object)
  # Walk through 'em
  for object_role in object_roles
    object_role.destroy
  end
end

#after_update(object) ⇒ Object

This is a Rails only feature: Single Table Inheritance (STI) is implemented using the Type column. The ‘type’ column contains the name of the subclass of the table the record is in. For example, Dog is a subclass of Animal. If we have a Dog object here that changes its ‘type’ to Animal, the ObjectRole must be updated to reflect the new class name.



26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
# File 'lib/authorizer/object_observer.rb', line 26

def after_update(object)
  type = object.try(:read_attribute, :type)
  # Big chance the object doesn't even have the 'type' attribute. In that case, do nothing.
  unless type.blank?
    object_class_name = object.class.to_s
    # This is how we gonna detect that the type has changed:
    # object_class_name should be different than type.
    unless type.eql?(object_class_name)
      object_roles = ObjectRole.find_all_by_object(object)
      # Walk through the object roles associated with this object
      for object_role in object_roles
        # update it!
        # it should reflect the new type.
        object_role.update_attributes!( :klazz_name => type )
      end
    end
  end
end