Class: Mongoid::AuditLog::Restore

Inherits:
Object
  • Object
show all
Defined in:
lib/mongoid/audit_log/restore.rb

Defined Under Namespace

Classes: DuplicateError, InvalidRestore

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(entry) ⇒ Restore

Returns a new instance of Restore.



11
12
13
# File 'lib/mongoid/audit_log/restore.rb', line 11

def initialize(entry)
  @entry = entry
end

Instance Attribute Details

#entryObject (readonly)

Returns the value of attribute entry.



7
8
9
# File 'lib/mongoid/audit_log/restore.rb', line 7

def entry
  @entry
end

Instance Method Details

#attributesObject



24
25
26
27
28
29
30
31
# File 'lib/mongoid/audit_log/restore.rb', line 24

def attributes
  @attributes ||=
    begin
      attrs = entry.model_attributes.deep_dup
      restored.send(:process_localized_attributes, model_class, attrs)
      attrs
    end
end

#document_pathObject



84
85
86
87
# File 'lib/mongoid/audit_log/restore.rb', line 84

def document_path
  # don't need last because that entry represents the deleted doc
  entry.document_path[0..-2]
end

#document_path_matches?(path, object) ⇒ Boolean

Returns:

  • (Boolean)


89
90
91
# File 'lib/mongoid/audit_log/restore.rb', line 89

def document_path_matches?(path, object)
  object.class.name == path['class_name'] && object.id == path['id']
end

#find_embedded_restoredObject

Raises:



50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
# File 'lib/mongoid/audit_log/restore.rb', line 50

def find_embedded_restored
  raise InvalidRestore if restored_root.blank?

  last_path = document_path.last
   = restored_root.class.reflect_on_association(last_path['relation'])
  relation = restored_root.send(last_path['relation'])

  if .is_a?(Association::Embedded::EmbedsMany)
    relation.build
  elsif relation.present?
    raise DuplicateError
  else
    restored_root.send("build_#{.name}")
  end
end

#find_root_restoredObject



46
47
48
# File 'lib/mongoid/audit_log/restore.rb', line 46

def find_root_restored
  model_class.new
end

#model_classObject



33
34
35
# File 'lib/mongoid/audit_log/restore.rb', line 33

def model_class
  entry.audited_type.constantize
end

#performObject



19
20
21
22
# File 'lib/mongoid/audit_log/restore.rb', line 19

def perform
  restored.attributes = attributes
  restored.save!
end

#restoredObject



37
38
39
40
41
42
43
44
# File 'lib/mongoid/audit_log/restore.rb', line 37

def restored
  @restored ||=
    if entry.for_embedded_doc?
      find_embedded_restored
    else
      find_root_restored
    end
end

#restored_rootObject



66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
# File 'lib/mongoid/audit_log/restore.rb', line 66

def restored_root
  document_path.reduce(entry.root) do |current, path|
    match = if document_path_matches?(path, current)
              current
            elsif current.respond_to?(:detect)
              current.detect do |model|
                document_path_matches?(path, model)
              end
            end

    if path == document_path.last
      return match
    else
      match.send(path['relation'])
    end
  end
end

#valid?Boolean

Returns:

  • (Boolean)


15
16
17
# File 'lib/mongoid/audit_log/restore.rb', line 15

def valid?
  !entry.for_embedded_doc? || restored_root.present?
end