class Mongoid::AuditLog::Restore

Attributes

entry[R]

Public Class Methods

new(entry) click to toggle source
# File lib/mongoid/audit_log/restore.rb, line 11
def initialize(entry)
  @entry = entry
end

Public Instance Methods

attributes() click to toggle source
# 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_path() click to toggle source
# 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) click to toggle source
# 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_restored() click to toggle source
# File lib/mongoid/audit_log/restore.rb, line 50
def find_embedded_restored
  raise InvalidRestore if restored_root.blank?

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

  if metadata.is_a?(Association::Embedded::EmbedsMany)
    relation.build
  elsif relation.present?
    raise DuplicateError
  else
    restored_root.send("build_#{metadata.name}")
  end
end
find_root_restored() click to toggle source
# File lib/mongoid/audit_log/restore.rb, line 46
def find_root_restored
  model_class.new
end
model_class() click to toggle source
# File lib/mongoid/audit_log/restore.rb, line 33
def model_class
  entry.audited_type.constantize
end
perform() click to toggle source
# File lib/mongoid/audit_log/restore.rb, line 19
def perform
  restored.attributes = attributes
  restored.save!
end
restored() click to toggle source
# 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_root() click to toggle source
# 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?() click to toggle source
# File lib/mongoid/audit_log/restore.rb, line 15
def valid?
  !entry.for_embedded_doc? || restored_root.present?
end