Module: Mongoid::Archivable::Restoration

Defined in:
lib/mongoid/archivable/restoration.rb

Instance Method Summary collapse

Instance Method Details

#original_classObject



39
40
41
# File 'lib/mongoid/archivable/restoration.rb', line 39

def original_class
  original_class_name.constantize
end

#original_class_nameObject

first, try to retrieve the original_class from the stored :original_type since previous versions of this gem did not use this field, fall back to previous method – removing the ‘::Archive’ from archive class name



31
32
33
34
35
36
37
# File 'lib/mongoid/archivable/restoration.rb', line 31

def original_class_name
  if respond_to?(:original_type) && original_type.present? # gem version >= 1.3.0, stored as a field.
    original_type
  else
    self.class.to_s.gsub(/::Archive\z/, '') # gem version < 1.3.0, turns "User::Archive" into "User".
  end
end

#original_documentObject



16
17
18
19
20
21
22
23
24
25
26
# File 'lib/mongoid/archivable/restoration.rb', line 16

def original_document
  @original_document ||= begin
    excluded_attributes = %w(_id original_id original_type archived_at)
    attrs = attributes.except(*excluded_attributes)
    attrs = Mongoid::Archivable::ProcessLocalizedFields.call(original_class, attrs)

    original_class.new(attrs) do |doc|
      doc.id = original_id
    end
  end
end

#restoreObject

Restores the archived document to its former glory.



5
6
7
8
9
# File 'lib/mongoid/archivable/restoration.rb', line 5

def restore
  if original_document.save
    original_document
  end
end

#restore!Object



11
12
13
14
# File 'lib/mongoid/archivable/restoration.rb', line 11

def restore!
  original_document.save!
  original_document
end