no_brainer_soft_delete 0.1.0
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +7 -0
- data/.gitignore +9 -0
- data/CHANGELOG.md +4 -0
- data/CONTRIBUTING.md +35 -0
- data/Gemfile +11 -0
- data/LICENSE.md +19 -0
- data/README.md +76 -0
- data/Rakefile +10 -0
- data/lib/no_brainer_soft_delete/version.rb +3 -0
- data/lib/no_brainer_soft_delete.rb +53 -0
- data/no_brainer_soft_delete.gemspec +36 -0
- metadata +125 -0
checksums.yaml
ADDED
@@ -0,0 +1,7 @@
|
|
1
|
+
---
|
2
|
+
SHA1:
|
3
|
+
metadata.gz: 044d0cb771a1ae6291d91dd63763d9e2361f30ed
|
4
|
+
data.tar.gz: bd57665a669a82646c38c141eddd5f55a3904639
|
5
|
+
SHA512:
|
6
|
+
metadata.gz: 49c98211a5ea03bbbb9b2d68e235e6a371c41e07ccad9d6eb63ee3c8cad665c598ef1d4d16742679a25ff603f31dd23ed93c618a4b04f8e61ad07b6ae8897952
|
7
|
+
data.tar.gz: 6e7c7970d59f43807668678b6e30b90d7108f6ac9be97aa688c143f4522644e34dbafd76ff3a401a951aa65411d9056601bf761f4b260e24965e9e4bc36c87d2
|
data/.gitignore
ADDED
data/CHANGELOG.md
ADDED
data/CONTRIBUTING.md
ADDED
@@ -0,0 +1,35 @@
|
|
1
|
+
# NoBrainer Soft Delete
|
2
|
+
|
3
|
+
This is an open source project. Contributions are encouraged. Please refer to the guidelines below prior to contributing.
|
4
|
+
|
5
|
+
## Filing an issue
|
6
|
+
|
7
|
+
When filing an issue please provide the following details:
|
8
|
+
|
9
|
+
- What you're expecting to happen compared with what's actually happening.
|
10
|
+
- A comprehensive list of steps to reproduce the issue.
|
11
|
+
- Your application's complete `Gemfile.lock`, and `Gemfile.lock` as text in a [Gist](https://gist.github.com) (*not as an image*)
|
12
|
+
- Any relevant stack traces ("Full trace" preferred)
|
13
|
+
|
14
|
+
Please remember to format code using triple backticks (\`) so that it is neatly
|
15
|
+
formatted when the issue is posted.
|
16
|
+
|
17
|
+
## Pull requests
|
18
|
+
|
19
|
+
We gladly accept pull requests to add documentation, fix bugs, and
|
20
|
+
add new features.
|
21
|
+
|
22
|
+
Here's a quick guide:
|
23
|
+
|
24
|
+
1. Fork the repo.
|
25
|
+
|
26
|
+
2. Run the tests. We only take pull requests with passing tests, and it's great
|
27
|
+
to know that you have a clean slate.
|
28
|
+
|
29
|
+
3. Create new feature branch, make your changes, and add tests for your changes. Only
|
30
|
+
refactoring and documentation changes require no new tests. If you are adding
|
31
|
+
functionality or fixing a bug, we need tests!
|
32
|
+
|
33
|
+
4. Push to your fork and submit a pull request.
|
34
|
+
|
35
|
+
The entire test suite must pass before your feature branch will be merged in.
|
data/Gemfile
ADDED
data/LICENSE.md
ADDED
@@ -0,0 +1,19 @@
|
|
1
|
+
# The MIT License (MIT)
|
2
|
+
|
3
|
+
Permission is hereby granted, free of charge, to any person obtaining a copy
|
4
|
+
of this software and associated documentation files (the "Software"), to deal
|
5
|
+
in the Software without restriction, including without limitation the rights
|
6
|
+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
7
|
+
copies of the Software, and to permit persons to whom the Software is
|
8
|
+
furnished to do so, subject to the following conditions:
|
9
|
+
|
10
|
+
The above copyright notice and this permission notice shall be included in
|
11
|
+
all copies or substantial portions of the Software.
|
12
|
+
|
13
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
14
|
+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
15
|
+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
16
|
+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
17
|
+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
18
|
+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
|
19
|
+
THE SOFTWARE.
|
data/README.md
ADDED
@@ -0,0 +1,76 @@
|
|
1
|
+
# NoBrainer Soft Delete
|
2
|
+
|
3
|
+
This gem adds soft delete functionality inspired by [Paranoia](https://github.com/rubysherpas/paranoia) to NoBrainer for the NoSQL database RethinkDB.
|
4
|
+
|
5
|
+
## Installation & Use
|
6
|
+
|
7
|
+
This gem requires you to use [RethinkDB](https://rethinkdb.com/) and the gem [NoBrainer](https://github.com/nviennot/nobrainer) in your application.
|
8
|
+
|
9
|
+
To install, add this line to your application's Gemfile and run bundle:
|
10
|
+
|
11
|
+
```ruby
|
12
|
+
gem 'no_brainer_soft_delete'
|
13
|
+
```
|
14
|
+
|
15
|
+
In the model that you would like to add soft delete functionality to, add this line of code directly below your NoBrainer includes:
|
16
|
+
|
17
|
+
```ruby
|
18
|
+
include NoBrainerSoftDelete
|
19
|
+
```
|
20
|
+
|
21
|
+
That's all there is to it. This single line of code does several things for you:
|
22
|
+
- It adds a `deleted_at` field to your model (with an index)
|
23
|
+
- It scopes your default queries to documents that do not have a `deleted_at` value
|
24
|
+
- It provides access to several class and instance level methods.
|
25
|
+
|
26
|
+
__Methods available from NoBrainerSoftDelete:__
|
27
|
+
|
28
|
+
`Model.with_deleted`
|
29
|
+
|
30
|
+
This method removes *all* scopes, then applies a scope reflecting that documents should be included regardless of the value held at `deleted_at`. It is important to note, this should be the first method called in a query chain as it will override anything prior to it.
|
31
|
+
|
32
|
+
```ruby
|
33
|
+
# Good
|
34
|
+
Model.with_deleted.where(value: 'query_for')
|
35
|
+
|
36
|
+
# Bad
|
37
|
+
Model.where(value: 'query_for').with_deleted
|
38
|
+
```
|
39
|
+
|
40
|
+
`Model.only_deleted`
|
41
|
+
|
42
|
+
This method removes *all* scopes,, then applies a scope reflecting that documents only be included in the query if the have a value saved to `deleted_at`. It is important to note, this should be the first method called in a query chain as it will override anything prior to it.
|
43
|
+
|
44
|
+
`Model.restore(id)`
|
45
|
+
|
46
|
+
This method locates a document and restores it.
|
47
|
+
|
48
|
+
`instance.deleted?`
|
49
|
+
|
50
|
+
Returns a boolean indicating whether or not the document has been soft deleted.
|
51
|
+
|
52
|
+
`instance.destroy`
|
53
|
+
|
54
|
+
Saves the value of `Time.now` to the `deleted_at` field and renders the document soft deleted. Also performs before/ after destroy callbacks. Returns a boolean indicating whether the operation was successful.
|
55
|
+
|
56
|
+
`instance.really_destroy!`
|
57
|
+
|
58
|
+
Performs the original destroy operation and completely removes the document from the database. Any instance that has this method called will no longer exist. Use with caution.
|
59
|
+
|
60
|
+
`instance.restore`
|
61
|
+
|
62
|
+
Erases the value saved to `deleted_at` by setting it to nil. The effectively "un-deletes" the document, and makes it readily accessible again.
|
63
|
+
|
64
|
+
## Development
|
65
|
+
|
66
|
+
To install the dependencies for this gem on your local machine, run `$ bundle install`.
|
67
|
+
|
68
|
+
To run the specs use first start your rethinkdb server, then run `$ bundle exec rspec`.
|
69
|
+
|
70
|
+
## Contributing
|
71
|
+
|
72
|
+
Bug reports and pull requests are welcome on GitHub at https://github.com/andrewtpoe/no_brainer_soft_delete. Please refer to the contributing guide for best practices.
|
73
|
+
|
74
|
+
## License
|
75
|
+
|
76
|
+
The gem is available as open source under the terms of the [MIT License](http://opensource.org/licenses/MIT).
|
data/Rakefile
ADDED
@@ -0,0 +1,53 @@
|
|
1
|
+
require 'active_support'
|
2
|
+
require 'nobrainer'
|
3
|
+
require "no_brainer_soft_delete/version"
|
4
|
+
|
5
|
+
module NoBrainerSoftDelete
|
6
|
+
extend ActiveSupport::Concern
|
7
|
+
|
8
|
+
included do
|
9
|
+
# Set the default scope to only retrieve documents that have not been soft deleted
|
10
|
+
default_scope { where(:deleted_at.undefined => true) }
|
11
|
+
|
12
|
+
# Add a deleted_at field to the document, for use with NoBrainerSoftDelete
|
13
|
+
field :deleted_at, index: true, type: Time
|
14
|
+
end
|
15
|
+
|
16
|
+
class_methods do
|
17
|
+
# Returns documents regardless of the value held in deleted_at
|
18
|
+
def with_deleted
|
19
|
+
unscoped.where(:or => [{ :deleted_at.defined => true }, { :deleted_at.undefined => true }])
|
20
|
+
end
|
21
|
+
|
22
|
+
# Returns only documents that have been soft deleted
|
23
|
+
def only_deleted
|
24
|
+
unscoped.where(:deleted_at.defined => true)
|
25
|
+
end
|
26
|
+
|
27
|
+
# Searches for a document and restores it
|
28
|
+
def restore(id, options = {})
|
29
|
+
with_deleted.where(id: id).first.restore(options)
|
30
|
+
end
|
31
|
+
end
|
32
|
+
|
33
|
+
# Returns a boolean indicating whether or not a document has been soft deleted
|
34
|
+
def deleted?
|
35
|
+
!!deleted_at
|
36
|
+
end
|
37
|
+
|
38
|
+
# Sets the deleted_at value to the current time, thus marking the document as deleted
|
39
|
+
def destroy
|
40
|
+
update(deleted_at: Time.now.utc)
|
41
|
+
end
|
42
|
+
|
43
|
+
# Completely removes the document from the database, and runs the callbacks.
|
44
|
+
def really_destroy!
|
45
|
+
_run_destroy_callbacks
|
46
|
+
delete
|
47
|
+
end
|
48
|
+
|
49
|
+
# Sets the deleted_at value to nil, thus no longer marking the document as deleted
|
50
|
+
def restore(options = {})
|
51
|
+
update(deleted_at: nil)
|
52
|
+
end
|
53
|
+
end
|
@@ -0,0 +1,36 @@
|
|
1
|
+
# coding: utf-8
|
2
|
+
lib = File.expand_path('../lib', __FILE__)
|
3
|
+
$LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
|
4
|
+
|
5
|
+
require 'no_brainer_soft_delete/version'
|
6
|
+
|
7
|
+
Gem::Specification.new do |spec|
|
8
|
+
spec.name = "no_brainer_soft_delete"
|
9
|
+
spec.version = NoBrainerSoftDelete::VERSION
|
10
|
+
spec.authors = ["Andrew T. poe"]
|
11
|
+
spec.email = ["andrewtpoe@gmail.com"]
|
12
|
+
spec.summary = "Adds soft delete capabilities to NoBrainer"
|
13
|
+
spec.description = "Extends the NoBrainer ORM to include soft delete capabilities similar to Paranoia for Active Record."
|
14
|
+
spec.homepage = "https://github.com/andrewtpoe/no_brainer_soft_delete"
|
15
|
+
spec.license = "MIT"
|
16
|
+
|
17
|
+
# Prevent pushing this gem to RubyGems.org by setting 'allowed_push_host', or
|
18
|
+
# delete this section to allow pushing this gem to any host.
|
19
|
+
# if spec.respond_to?(:metadata)
|
20
|
+
# spec.metadata['allowed_push_host'] = "TODO: Set to 'http://mygemserver.com'"
|
21
|
+
# else
|
22
|
+
# raise "RubyGems 2.0 or newer is required to protect against public gem pushes."
|
23
|
+
# end
|
24
|
+
|
25
|
+
spec.files = `git ls-files -z`.split("\x0").reject { |f| f.match(%r{^(test|spec|features)/}) }
|
26
|
+
|
27
|
+
spec.require_paths = ["lib"]
|
28
|
+
spec.required_ruby_version = ">= 2.2.2"
|
29
|
+
|
30
|
+
spec.add_dependency "activesupport", ">= 4.1.0"
|
31
|
+
spec.add_dependency "nobrainer", ">=0.32.0"
|
32
|
+
spec.add_dependency "rethinkdb", ">= 2.3.0"
|
33
|
+
|
34
|
+
spec.add_development_dependency "bundler", "~> 1.11"
|
35
|
+
spec.add_development_dependency "rake", "~> 10.0"
|
36
|
+
end
|
metadata
ADDED
@@ -0,0 +1,125 @@
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
|
+
name: no_brainer_soft_delete
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
version: 0.1.0
|
5
|
+
platform: ruby
|
6
|
+
authors:
|
7
|
+
- Andrew T. poe
|
8
|
+
autorequire:
|
9
|
+
bindir: bin
|
10
|
+
cert_chain: []
|
11
|
+
date: 2016-07-24 00:00:00.000000000 Z
|
12
|
+
dependencies:
|
13
|
+
- !ruby/object:Gem::Dependency
|
14
|
+
name: activesupport
|
15
|
+
requirement: !ruby/object:Gem::Requirement
|
16
|
+
requirements:
|
17
|
+
- - ">="
|
18
|
+
- !ruby/object:Gem::Version
|
19
|
+
version: 4.1.0
|
20
|
+
type: :runtime
|
21
|
+
prerelease: false
|
22
|
+
version_requirements: !ruby/object:Gem::Requirement
|
23
|
+
requirements:
|
24
|
+
- - ">="
|
25
|
+
- !ruby/object:Gem::Version
|
26
|
+
version: 4.1.0
|
27
|
+
- !ruby/object:Gem::Dependency
|
28
|
+
name: nobrainer
|
29
|
+
requirement: !ruby/object:Gem::Requirement
|
30
|
+
requirements:
|
31
|
+
- - ">="
|
32
|
+
- !ruby/object:Gem::Version
|
33
|
+
version: 0.32.0
|
34
|
+
type: :runtime
|
35
|
+
prerelease: false
|
36
|
+
version_requirements: !ruby/object:Gem::Requirement
|
37
|
+
requirements:
|
38
|
+
- - ">="
|
39
|
+
- !ruby/object:Gem::Version
|
40
|
+
version: 0.32.0
|
41
|
+
- !ruby/object:Gem::Dependency
|
42
|
+
name: rethinkdb
|
43
|
+
requirement: !ruby/object:Gem::Requirement
|
44
|
+
requirements:
|
45
|
+
- - ">="
|
46
|
+
- !ruby/object:Gem::Version
|
47
|
+
version: 2.3.0
|
48
|
+
type: :runtime
|
49
|
+
prerelease: false
|
50
|
+
version_requirements: !ruby/object:Gem::Requirement
|
51
|
+
requirements:
|
52
|
+
- - ">="
|
53
|
+
- !ruby/object:Gem::Version
|
54
|
+
version: 2.3.0
|
55
|
+
- !ruby/object:Gem::Dependency
|
56
|
+
name: bundler
|
57
|
+
requirement: !ruby/object:Gem::Requirement
|
58
|
+
requirements:
|
59
|
+
- - "~>"
|
60
|
+
- !ruby/object:Gem::Version
|
61
|
+
version: '1.11'
|
62
|
+
type: :development
|
63
|
+
prerelease: false
|
64
|
+
version_requirements: !ruby/object:Gem::Requirement
|
65
|
+
requirements:
|
66
|
+
- - "~>"
|
67
|
+
- !ruby/object:Gem::Version
|
68
|
+
version: '1.11'
|
69
|
+
- !ruby/object:Gem::Dependency
|
70
|
+
name: rake
|
71
|
+
requirement: !ruby/object:Gem::Requirement
|
72
|
+
requirements:
|
73
|
+
- - "~>"
|
74
|
+
- !ruby/object:Gem::Version
|
75
|
+
version: '10.0'
|
76
|
+
type: :development
|
77
|
+
prerelease: false
|
78
|
+
version_requirements: !ruby/object:Gem::Requirement
|
79
|
+
requirements:
|
80
|
+
- - "~>"
|
81
|
+
- !ruby/object:Gem::Version
|
82
|
+
version: '10.0'
|
83
|
+
description: Extends the NoBrainer ORM to include soft delete capabilities similar
|
84
|
+
to Paranoia for Active Record.
|
85
|
+
email:
|
86
|
+
- andrewtpoe@gmail.com
|
87
|
+
executables: []
|
88
|
+
extensions: []
|
89
|
+
extra_rdoc_files: []
|
90
|
+
files:
|
91
|
+
- ".gitignore"
|
92
|
+
- CHANGELOG.md
|
93
|
+
- CONTRIBUTING.md
|
94
|
+
- Gemfile
|
95
|
+
- LICENSE.md
|
96
|
+
- README.md
|
97
|
+
- Rakefile
|
98
|
+
- lib/no_brainer_soft_delete.rb
|
99
|
+
- lib/no_brainer_soft_delete/version.rb
|
100
|
+
- no_brainer_soft_delete.gemspec
|
101
|
+
homepage: https://github.com/andrewtpoe/no_brainer_soft_delete
|
102
|
+
licenses:
|
103
|
+
- MIT
|
104
|
+
metadata: {}
|
105
|
+
post_install_message:
|
106
|
+
rdoc_options: []
|
107
|
+
require_paths:
|
108
|
+
- lib
|
109
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
110
|
+
requirements:
|
111
|
+
- - ">="
|
112
|
+
- !ruby/object:Gem::Version
|
113
|
+
version: 2.2.2
|
114
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
115
|
+
requirements:
|
116
|
+
- - ">="
|
117
|
+
- !ruby/object:Gem::Version
|
118
|
+
version: '0'
|
119
|
+
requirements: []
|
120
|
+
rubyforge_project:
|
121
|
+
rubygems_version: 2.5.1
|
122
|
+
signing_key:
|
123
|
+
specification_version: 4
|
124
|
+
summary: Adds soft delete capabilities to NoBrainer
|
125
|
+
test_files: []
|