mongoid-paranoid 1.0.0.alpha

Sign up to get free protection for your applications and to get access to all the features.
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA256:
3
+ metadata.gz: 28bbc803d6c6b67fea2a8527ebec4ea8bb3b817be73916b09f0abce81ce1c7b7
4
+ data.tar.gz: b3e778365975307e088f7c85537ca92e3f7618f5b23e714823a744515d4b09cf
5
+ SHA512:
6
+ metadata.gz: 1c59874463392caf7582133963fdbcd62de2f9a9a180717e3c6cfe6da471d0d21e204d04b9af96b87b93a8d207ce0ad082a19ce26002697b712fa963246d6a44
7
+ data.tar.gz: aefa367bfc5e53a0960b9c3651b5c65d16aaed00f7b8e60d4601ce27e00a117ca6b6bb55605e3a7724de380c18a3b7fbf52c5a20d7ff10a7e6bd3839fcaf4eac
data/LICENSE ADDED
@@ -0,0 +1,21 @@
1
+ The MIT License (MIT)
2
+
3
+ Copyright (c) 2018 Yuri S.
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining a copy
6
+ of this software and associated documentation files (the "Software"), to deal
7
+ in the Software without restriction, including without limitation the rights
8
+ to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9
+ copies of the Software, and to permit persons to whom the Software is
10
+ furnished to do so, subject to the following conditions:
11
+
12
+ The above copyright notice and this permission notice shall be included in
13
+ all copies or substantial portions of the Software.
14
+
15
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16
+ IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17
+ FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18
+ AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19
+ LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20
+ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
21
+ THE SOFTWARE.
@@ -0,0 +1,39 @@
1
+ # Mongoid::Paranoid
2
+
3
+ Welcome to your new gem! In this directory, you'll find the files you need to be able to package up your Ruby library into a gem. Put your Ruby code in the file `lib/mongoid/paranoid`. To experiment with that code, run `bin/console` for an interactive prompt.
4
+
5
+ TODO: Delete this and the text above, and describe your gem
6
+
7
+ ## Installation
8
+
9
+ Add this line to your application's Gemfile:
10
+
11
+ ```ruby
12
+ gem 'mongoid-paranoid'
13
+ ```
14
+
15
+ And then execute:
16
+
17
+ $ bundle
18
+
19
+ Or install it yourself as:
20
+
21
+ $ gem install mongoid-paranoid
22
+
23
+ ## Usage
24
+
25
+ TODO: Write usage instructions here
26
+
27
+ ## Development
28
+
29
+ After checking out the repo, run `bin/setup` to install dependencies. Then, run `rake test` to run the tests. You can also run `bin/console` for an interactive prompt that will allow you to experiment.
30
+
31
+ To install this gem onto your local machine, run `bundle exec rake install`. To release a new version, update the version number in `version.rb`, and then run `bundle exec rake release`, which will create a git tag for the version, push git commits and tags, and push the `.gem` file to [rubygems.org](https://rubygems.org).
32
+
33
+ ## Contributing
34
+
35
+ Bug reports and pull requests are welcome on GitHub at https://github.com/[USERNAME]/mongoid-paranoid.
36
+
37
+ ## License
38
+
39
+ The gem is available as open source under the terms of the [MIT License](https://opensource.org/licenses/MIT).
@@ -0,0 +1,80 @@
1
+ # frozen_string_literal: true
2
+
3
+ # @example Make a document paranoid
4
+ # class User
5
+ # include Mongoid::Document
6
+ # include Paranoid.new default_scope: false, field: :d_at
7
+ # end
8
+ class Paranoid
9
+ def self.new(options = {}) # rubocop:disable Metrics/AbcSize, MethodLength
10
+ Module.new do # rubocop:disable Metrics/BlockLength
11
+ extend ActiveSupport::Concern
12
+
13
+ included do # rubocop:disable Metrics/BlockLength
14
+ class_attribute :paranoid, :paranoid_default_scope, :paranoid_field
15
+ options = { default_scope: true, field: :deleted_at }.merge! options
16
+
17
+ self.paranoid = true
18
+ self.paranoid_default_scope = options[:default_scope]
19
+ self.paranoid_field = options[:field]
20
+
21
+ scope :deleted, -> { ne deleted_at: nil }
22
+ default_scope -> { where deleted_at: nil } if paranoid_default_scope
23
+
24
+ define_model_callbacks :restore
25
+ define_model_callbacks :remove
26
+
27
+ field paranoid_field, as: :deleted_at, type: Time
28
+
29
+ alias_method :orig_remove, :remove
30
+
31
+ def remove(_ = {})
32
+ time = self.deleted_at = Time.now.utc
33
+ paranoid_update '$set' => { atomic_field => time }
34
+ @destroyed = true
35
+ true
36
+ end
37
+
38
+ alias_method :delete, :remove
39
+ alias_method :delete!, :orig_remove
40
+
41
+ def destroy!
42
+ run_callbacks :destroy do
43
+ run_callbacks :remove do
44
+ orig_remove
45
+ end
46
+ end
47
+ end
48
+
49
+ def destroyed?
50
+ (@destroyed ||= false) || deleted_at?
51
+ end
52
+
53
+ def restore
54
+ run_callbacks :restore do
55
+ paranoid_update '$unset' => { atomic_field => true }
56
+ attributes.delete 'deleted_at'
57
+ @destroyed = false
58
+ true
59
+ end
60
+ end
61
+
62
+ private :paranoid_default_scope, :paranoid_field
63
+
64
+ private
65
+
66
+ def atomic_field
67
+ embedded? ? "#{atomic_position}.#{paranoid_field}" : paranoid_field
68
+ end
69
+
70
+ def paranoid_collection
71
+ embedded? ? _root.collection : collection
72
+ end
73
+
74
+ def paranoid_update(atomic_operation)
75
+ paranoid_collection.find(atomic_selector).update_one atomic_operation
76
+ end
77
+ end
78
+ end
79
+ end
80
+ end
@@ -0,0 +1,7 @@
1
+ # frozen_string_literal: true
2
+
3
+ module Mongoid
4
+ module Paranoid
5
+ VERSION = '1.0.0.alpha'
6
+ end
7
+ end
@@ -0,0 +1,3 @@
1
+ # frozen_string_literal: true
2
+
3
+ require 'mongoid/paranoid'
metadata ADDED
@@ -0,0 +1,104 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: mongoid-paranoid
3
+ version: !ruby/object:Gem::Version
4
+ version: 1.0.0.alpha
5
+ platform: ruby
6
+ authors:
7
+ - Yuri S.
8
+ autorequire:
9
+ bindir: bin
10
+ cert_chain: []
11
+ date: 2018-01-23 00:00:00.000000000 Z
12
+ dependencies:
13
+ - !ruby/object:Gem::Dependency
14
+ name: mongoid
15
+ requirement: !ruby/object:Gem::Requirement
16
+ requirements:
17
+ - - ">="
18
+ - !ruby/object:Gem::Version
19
+ version: 7.0.0.beta
20
+ type: :runtime
21
+ prerelease: false
22
+ version_requirements: !ruby/object:Gem::Requirement
23
+ requirements:
24
+ - - ">="
25
+ - !ruby/object:Gem::Version
26
+ version: 7.0.0.beta
27
+ - !ruby/object:Gem::Dependency
28
+ name: minitest
29
+ requirement: !ruby/object:Gem::Requirement
30
+ requirements:
31
+ - - "~>"
32
+ - !ruby/object:Gem::Version
33
+ version: '5.11'
34
+ type: :development
35
+ prerelease: false
36
+ version_requirements: !ruby/object:Gem::Requirement
37
+ requirements:
38
+ - - "~>"
39
+ - !ruby/object:Gem::Version
40
+ version: '5.11'
41
+ - !ruby/object:Gem::Dependency
42
+ name: rake
43
+ requirement: !ruby/object:Gem::Requirement
44
+ requirements:
45
+ - - ">="
46
+ - !ruby/object:Gem::Version
47
+ version: '11.1'
48
+ type: :development
49
+ prerelease: false
50
+ version_requirements: !ruby/object:Gem::Requirement
51
+ requirements:
52
+ - - ">="
53
+ - !ruby/object:Gem::Version
54
+ version: '11.1'
55
+ - !ruby/object:Gem::Dependency
56
+ name: rubocop
57
+ requirement: !ruby/object:Gem::Requirement
58
+ requirements:
59
+ - - ">="
60
+ - !ruby/object:Gem::Version
61
+ version: '0.52'
62
+ type: :development
63
+ prerelease: false
64
+ version_requirements: !ruby/object:Gem::Requirement
65
+ requirements:
66
+ - - ">="
67
+ - !ruby/object:Gem::Version
68
+ version: '0.52'
69
+ description: Add soft-destroy and soft-delete for documents
70
+ email: fudoshiki.ari@gmail.com
71
+ executables: []
72
+ extensions: []
73
+ extra_rdoc_files: []
74
+ files:
75
+ - LICENSE
76
+ - README.md
77
+ - lib/mongoid/paranoid.rb
78
+ - lib/mongoid/paranoid/version.rb
79
+ - lib/mongoid_paranoid.rb
80
+ homepage: https://github.com/Fudoshiki/mongoid-paranoid
81
+ licenses:
82
+ - MIT
83
+ metadata: {}
84
+ post_install_message:
85
+ rdoc_options: []
86
+ require_paths:
87
+ - lib
88
+ required_ruby_version: !ruby/object:Gem::Requirement
89
+ requirements:
90
+ - - ">="
91
+ - !ruby/object:Gem::Version
92
+ version: '0'
93
+ required_rubygems_version: !ruby/object:Gem::Requirement
94
+ requirements:
95
+ - - ">"
96
+ - !ruby/object:Gem::Version
97
+ version: 1.3.1
98
+ requirements: []
99
+ rubyforge_project:
100
+ rubygems_version: 2.7.3
101
+ signing_key:
102
+ specification_version: 4
103
+ summary: Make mongoid is paranoid
104
+ test_files: []