Class: Mongoid::AuditLog::Entry

Inherits:
Object
  • Object
show all
Includes:
Document, Timestamps::Created
Defined in:
lib/mongoid/audit_log/entry.rb

Instance Method Summary collapse

Dynamic Method Handling

This class handles dynamic methods through the method_missing method

#method_missing(sym, *args, &block) ⇒ Object



90
91
92
93
94
95
96
97
98
# File 'lib/mongoid/audit_log/entry.rb', line 90

def method_missing(sym, *args, &block)
  key = sym.to_s

  if model_attributes.present? && model_attributes.has_key?(key)
    model_attributes[key]
  else
    super
  end
end

Instance Method Details

#auditedObject



64
65
66
67
68
69
70
71
72
# File 'lib/mongoid/audit_log/entry.rb', line 64

def audited
  return nil if audited_type.blank? || audited_id.blank?

  if for_embedded_doc?
    lookup_from_document_path
  else
    audited_type.constantize.where(id: audited_id).first
  end
end

#for_embedded_doc?Boolean

Returns:

  • (Boolean)


60
61
62
# File 'lib/mongoid/audit_log/entry.rb', line 60

def for_embedded_doc?
  document_path.try(:length).to_i > 1
end

#modifierObject



41
42
43
44
45
46
47
48
# File 'lib/mongoid/audit_log/entry.rb', line 41

def modifier
  @modifier ||= if modifier_id.blank?
                  nil
                else
                  klass = Mongoid::AuditLog.modifier_class_name.constantize
                  klass.find(modifier_id)
                end
end

#modifier=(modifier) ⇒ Object



50
51
52
53
54
55
56
57
58
# File 'lib/mongoid/audit_log/entry.rb', line 50

def modifier=(modifier)
  self.modifier_id = if modifier.present? && modifier.respond_to?(:id)
                       modifier.id
                     else
                       modifier
                     end

  @modifier = modifier
end

#respond_to?(sym, *args) ⇒ Boolean

Returns:

  • (Boolean)


85
86
87
88
# File 'lib/mongoid/audit_log/entry.rb', line 85

def respond_to?(sym, *args)
  key = sym.to_s
  (model_attributes.present? && model_attributes.has_key?(key)) || super
end

#rootObject



74
75
76
77
78
79
80
81
82
83
# File 'lib/mongoid/audit_log/entry.rb', line 74

def root
  root = document_path.first
  return audited if root.blank?

  if for_embedded_doc?
    root['class_name'].constantize.find(root['id'])
  else
    audited
  end
end

#valid?Boolean

Returns:

  • (Boolean)


33
34
35
36
37
38
39
# File 'lib/mongoid/audit_log/entry.rb', line 33

def valid?(*)
  result = super
  if result && modifier.blank?
    self.modifier = Mongoid::AuditLog.current_modifier
  end
  result
end