mongoid-archivable 1.5.2 → 1.6.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: 6196888bed37db1654169604ef259efd32fcf17c
4
- data.tar.gz: 510d60a14521bffa9a7fc0c1f128ddb2549702fa
3
+ metadata.gz: 39ea08e10786da69fd02888d73ff5311550115d4
4
+ data.tar.gz: 763442ac4b4451e46119ff3574a6c64d52cf96bd
5
5
  SHA512:
6
- metadata.gz: 1f0176ff51fca46ffbbe08af76c0f7d3feadcee54cd1d3445844bd02b21449fcd029cbab63327b196129eeb21e2078b860238e2908401657c148866ad281db43
7
- data.tar.gz: 63262b2557d0f08feb5a138c6281f0dc416cac19fd9415b9b70413bea3044955178df39fcd227cf13dd4b86a6896d6336617e2e555f1aed4bded9900fb238d78
6
+ metadata.gz: e42df539cbf0d48e580c7e4fd10226cdb66f14bb6e799118e00581c2f06699c83bc254a17c23987b970a188b970243f9aaed169fc3189859a6e0703b208fe8a9
7
+ data.tar.gz: 531755776b89e8dbbbd7363d11bfc9304fc23d66ef9f473523c807f683e0c7ae465af0140131c6dbe670234ae5a56781c1cf0913ffcfad3ac18adab7baa45c15
data/README.md CHANGED
@@ -48,6 +48,50 @@ User.count # => 1
48
48
  User::Archive.count # => 1
49
49
  ```
50
50
 
51
+ ## Standalone Storage
52
+
53
+ By default, the archived documents will be stored in primary client and database. If you want to use different database or client for all archived documents, you can create the following middleware:
54
+
55
+ ```ruby
56
+ # Rails : config/initializers/mongoid_archivable.rb
57
+ # Ruby : config/mongoid_archivable.rb
58
+
59
+ Mongoid::Archivable.configure do |config|
60
+ config.database = "archives"
61
+ config.client = "secondary"
62
+ end
63
+ ```
64
+
65
+ But if you only want to use different database or client in spesific Mongoid document, you can use this approach:
66
+
67
+ ```ruby
68
+ class User
69
+ include Mongoid::Document
70
+ include Mongoid::Archive
71
+ archive_in database: 'achives', client: 'secondary'
72
+ end
73
+ ```
74
+
75
+ In your mongoid.yml will show like this:
76
+
77
+ ```yaml
78
+ development:
79
+ clients:
80
+ default:
81
+ database: project_development
82
+ hosts:
83
+ - localhost:27017
84
+ options:
85
+ <<: *client_options
86
+ secondary:
87
+ database: archives
88
+ hosts:
89
+ - localhost:27018
90
+ options:
91
+ <<: *client_options
92
+ ```
93
+
94
+
51
95
  ## Development
52
96
 
53
97
  Please report any issues to the [GitHub issue tracker](https://github.com/Sign2Pay/mongoid-archivable/issues).
@@ -1,3 +1,4 @@
1
1
  require 'mongoid/archivable/version'
2
+ require 'mongoid/archivable/config'
2
3
  require 'mongoid/archivable/process_localized_fields'
3
4
  require 'mongoid/archivable'
@@ -1,55 +1,41 @@
1
1
  require 'active_support/concern'
2
2
  require 'mongoid/archivable/process_localized_fields'
3
+ require 'mongoid/archivable/restoration'
4
+ require 'mongoid/archivable/config'
5
+ require 'mongoid/archivable/gluten'
6
+ require 'mongoid/archivable/depot'
3
7
 
4
8
  module Mongoid
5
9
  module Archivable
6
10
  extend ActiveSupport::Concern
7
11
 
8
- module Restoration
9
- # Restores the archived document to its former glory.
10
- def restore
11
- original_document.save
12
- original_document
12
+ class << self
13
+ def config
14
+ @config ||= Config.new
15
+ @config
13
16
  end
14
17
 
15
- def original_document
16
- @original_document ||= begin
17
- excluded_attributes = %w(_id original_id original_type archived_at)
18
- attrs = attributes.except(*excluded_attributes)
19
- attrs = Mongoid::Archivable::ProcessLocalizedFields.call(original_class, attrs)
20
-
21
- original_class.new(attrs) do |doc|
22
- doc.id = original_id
23
- end
24
- end
25
- end
26
-
27
- # first, try to retrieve the original_class from the stored :original_type
28
- # since previous versions of this gem did not use this field, fall back
29
- # to previous method -- removing the '::Archive' from archive class name
30
- def original_class_name
31
- if respond_to?(:original_type) && original_type.present? # gem version >= 1.3.0, stored as a field.
32
- original_type
33
- else
34
- self.class.to_s.gsub(/::Archive\z/, '') # gem version < 1.3.0, turns "User::Archive" into "User".
35
- end
36
- end
37
-
38
- def original_class
39
- original_class_name.constantize
18
+ def configure(&proc)
19
+ yield config
40
20
  end
41
21
  end
42
22
 
43
23
  included do
24
+ mattr_accessor :archive_storage
25
+ include Mongoid::Archivable::Gluten
26
+
44
27
  const_set('Archive', Class.new)
45
28
  const_get('Archive').class_eval do
46
29
  include Mongoid::Document
47
30
  include Mongoid::Attributes::Dynamic
48
31
  include Mongoid::Archivable::Restoration
32
+ include Mongoid::Archivable::Depot
33
+ store_in database: ->{ archive_database_name }, client: ->{ archive_client_name }
49
34
 
50
35
  field :archived_at, type: Time
51
36
  field :original_id, type: String
52
37
  field :original_type, type: String
38
+
53
39
  end
54
40
 
55
41
  before_destroy :archive
@@ -0,0 +1,16 @@
1
+ module Mongoid
2
+ module Archivable
3
+ class Config
4
+ attr_accessor :database
5
+ attr_accessor :client
6
+
7
+ def get_database
8
+ database || Mongoid::Config.clients[get_client][:database]
9
+ end
10
+
11
+ def get_client
12
+ client || :default
13
+ end
14
+ end
15
+ end
16
+ end
@@ -0,0 +1,40 @@
1
+ module Mongoid
2
+ module Archivable
3
+ module Depot
4
+ extend ActiveSupport::Concern
5
+ included do
6
+ include ClassMethods
7
+ end
8
+
9
+ module ClassMethods
10
+ def has_archive_storage?
11
+ !parent.archive_storage.nil?
12
+ end
13
+
14
+ def has_archive_client?
15
+ has_archive_storage? && !parent.archive_storage[:client].nil?
16
+ end
17
+
18
+ def has_archive_database?
19
+ has_archive_storage? && !parent.archive_storage[:client].nil?
20
+ end
21
+
22
+ def archive_database_name
23
+ if has_archive_database?
24
+ parent.archive_storage[:database]
25
+ else
26
+ Mongoid::Archivable.config.get_database
27
+ end
28
+ end
29
+
30
+ def archive_client_name
31
+ if has_archive_client?
32
+ parent.archive_storage[:client]
33
+ else
34
+ Mongoid::Archivable.config.get_client
35
+ end
36
+ end
37
+ end
38
+ end
39
+ end
40
+ end
@@ -0,0 +1,19 @@
1
+ module Mongoid
2
+ module Archivable
3
+ module Gluten
4
+ extend ActiveSupport::Concern
5
+ included do
6
+ include ClassMethods
7
+ end
8
+ module ClassMethods
9
+ def archive_in args
10
+ @@archive_storage = args
11
+ end
12
+
13
+ def archive_storage
14
+ @@archive_storage
15
+ end
16
+ end
17
+ end
18
+ end
19
+ end
@@ -0,0 +1,38 @@
1
+ module Mongoid
2
+ module Archivable
3
+ module Restoration
4
+ # Restores the archived document to its former glory.
5
+ def restore
6
+ original_document.save
7
+ original_document
8
+ end
9
+
10
+ def original_document
11
+ @original_document ||= begin
12
+ excluded_attributes = %w(_id original_id original_type archived_at)
13
+ attrs = attributes.except(*excluded_attributes)
14
+ attrs = Mongoid::Archivable::ProcessLocalizedFields.call(original_class, attrs)
15
+
16
+ original_class.new(attrs) do |doc|
17
+ doc.id = original_id
18
+ end
19
+ end
20
+ end
21
+
22
+ # first, try to retrieve the original_class from the stored :original_type
23
+ # since previous versions of this gem did not use this field, fall back
24
+ # to previous method -- removing the '::Archive' from archive class name
25
+ def original_class_name
26
+ if respond_to?(:original_type) && original_type.present? # gem version >= 1.3.0, stored as a field.
27
+ original_type
28
+ else
29
+ self.class.to_s.gsub(/::Archive\z/, '') # gem version < 1.3.0, turns "User::Archive" into "User".
30
+ end
31
+ end
32
+
33
+ def original_class
34
+ original_class_name.constantize
35
+ end
36
+ end
37
+ end
38
+ end
@@ -1,5 +1,5 @@
1
1
  module Mongoid
2
2
  module Archivable
3
- VERSION = '1.5.2'.freeze
3
+ VERSION = '1.6.0'.freeze
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.5.2
4
+ version: 1.6.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: 2017-02-03 00:00:00.000000000 Z
11
+ date: 2017-03-06 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: mongoid
@@ -101,7 +101,11 @@ files:
101
101
  - bin/setup
102
102
  - lib/mongoid-archivable.rb
103
103
  - lib/mongoid/archivable.rb
104
+ - lib/mongoid/archivable/config.rb
105
+ - lib/mongoid/archivable/depot.rb
106
+ - lib/mongoid/archivable/gluten.rb
104
107
  - lib/mongoid/archivable/process_localized_fields.rb
108
+ - lib/mongoid/archivable/restoration.rb
105
109
  - lib/mongoid/archivable/version.rb
106
110
  - mongoid-archivable.gemspec
107
111
  homepage: https://github.com/Sign2Pay/mongoid-archivable