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



102
103
104
105
106
107
108
109
110
# File 'lib/mongoid/audit_log/entry.rb', line 102

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



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

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)


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

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

#modifierObject



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

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

#modifier=(modifier) ⇒ Object



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

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)


97
98
99
100
# File 'lib/mongoid/audit_log/entry.rb', line 97

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

#restorable?Boolean

Returns:

  • (Boolean)


93
94
95
# File 'lib/mongoid/audit_log/entry.rb', line 93

def restorable?
  destroy? && !restored? && Restore.new(self).valid?
end

#restore!Object



86
87
88
89
90
91
# File 'lib/mongoid/audit_log/entry.rb', line 86

def restore!
  raise Restore::InvalidRestore if restored? || !destroy?

  Restore.new(self).perform
  update_attributes!(:restored => true)
end

#rootObject



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

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

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

#valid?Boolean

Returns:

  • (Boolean)


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

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