mongoid-archivable 1.2.0 → 1.3.0

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: 971dca70add99ed3cc798f9e4ed83daaccc69aaf
4
- data.tar.gz: 043e3d3d71ad79494169137ad2920a17efed9111
3
+ metadata.gz: ddee3e043b9d4b417645ca3bc5ee3afc4372b4f2
4
+ data.tar.gz: 82addf9eb775e8218bdb7ef90271de14ebd6e3c7
5
5
  SHA512:
6
- metadata.gz: 4aad631225297e7e89130f4a919370935e7250c2d8ccf611203505323e274b2aa84f850bb521f6185e5e3244844cfa1cb446b620b939c1007cc374ef3a774fac
7
- data.tar.gz: aae341b6b79edcad33457008ce696b80dd870ae473d90bdb2260ddc935b4a3cbf15d434d14f76bdf2b432e9ad61e0a1881908f73e752683c0e23949afba559b7
6
+ metadata.gz: 2b0dec185d816f6c7e344cb8d833e782037842da01d7fdcdd4d90054d9c0497d9fe0b97661e94a00bbb9a8382c454193abb9aab72114f3afd02e6b0a4a4daecf
7
+ data.tar.gz: 577e54c3a9cab48f1bbfa0e95dae6ddcce202ebefbd3dea4d4577537ca87e0906a24830f90faa8139885bb0ac406a0e6be3cf2c91d1a376fc009fb9a57a37ea7
@@ -8,12 +8,26 @@ module Mongoid
8
8
  module Restoration
9
9
  # Restores the archived document to its former glory.
10
10
  def restore
11
- document = self.class.to_s.gsub(/::Archive\z/, '') # Turns User::Archive into User.
11
+ original_document.save
12
+ original_document
13
+ end
12
14
 
13
- self.class.const_get(document).create(attributes.except("_id", "original_id", "archived_at")) do |doc|
15
+ def original_document
16
+ self.original_class_name.constantize.new(attributes.except("_id", "original_id", "original_type", "archived_at")) do |doc|
14
17
  doc.id = self.original_id
15
18
  end
16
19
  end
20
+
21
+ # first, try to retrieve the original_class from the stored :original_type
22
+ # since previous versions of this gem did not use this field, fall back
23
+ # to previous method -- removing the '::Archive' from archive class name
24
+ def original_class_name
25
+ if self.respond_to?(:original_type) && self.original_type.present? # gem version >= 1.3.0, stored as a field.
26
+ self.original_type
27
+ else
28
+ self.class.to_s.gsub(/::Archive\z/, '') # gem version < 1.3.0, turns "User::Archive" into "User".
29
+ end
30
+ end
17
31
  end
18
32
 
19
33
  included do
@@ -24,6 +38,7 @@ module Mongoid
24
38
  include Mongoid::Archivable::Restoration
25
39
  field :archived_at, type: Time
26
40
  field :original_id, type: String
41
+ field :original_type, type: String
27
42
  end
28
43
 
29
44
  before_destroy :archive
@@ -32,8 +47,9 @@ module Mongoid
32
47
  private
33
48
 
34
49
  def archive
35
- self.class.const_get("Archive").create(attributes.except("_id")) do |doc|
50
+ self.class.const_get("Archive").create(attributes.except("_id", "_type")) do |doc|
36
51
  doc.original_id = self.id
52
+ doc.original_type = self.class.to_s
37
53
  doc.archived_at = Time.now.utc
38
54
  end
39
55
  end
@@ -1,5 +1,5 @@
1
1
  module Mongoid
2
2
  module Archivable
3
- VERSION = "1.2.0"
3
+ VERSION = "1.3.0"
4
4
  end
5
5
  end
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: mongoid-archivable
3
3
  version: !ruby/object:Gem::Version
4
- version: 1.2.0
4
+ version: 1.3.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - Joost Baaij
8
8
  autorequire:
9
9
  bindir: exe
10
10
  cert_chain: []
11
- date: 2015-05-15 00:00:00.000000000 Z
11
+ date: 2015-08-26 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: mongoid