immortal 2.0.0 → 3.0.0
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/.gitignore +3 -0
- data/.rspec +3 -0
- data/.rubocop.yml +1 -0
- data/CHANGELOG.md +114 -0
- data/CODE_OF_CONDUCT.md +74 -0
- data/Gemfile +1 -1
- data/LICENSE.txt +21 -0
- data/README.md +21 -44
- data/Rakefile +2 -2
- data/immortal.gemspec +28 -21
- data/lib/immortal.rb +45 -39
- data/lib/immortal/belongs_to.rb +4 -3
- data/lib/immortal/belongs_to_builder.rb +25 -30
- data/lib/immortal/singular_association.rb +91 -109
- data/lib/immortal/version.rb +3 -0
- metadata +69 -10
- data/LICENSE +0 -9
- data/spec/immortal_spec.rb +0 -383
- data/spec/spec_helper.rb +0 -145
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA1:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 487cc9848fc342f7f15e64970c3f470462396991
|
4
|
+
data.tar.gz: 271bf192210787e34fbb19c642178ddde59f587c
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 0edc1fa5dba903469dbc21284009b4edf3f62dbb1eeaaec32078d6ba591705da2d40de0ae6eab9d92d6a55b8a2d35a7f6282001e9cd492dc9808416f24d8768a
|
7
|
+
data.tar.gz: 5deeee14ae09920532785f3b64d98d6579436274982ae2772508c779ac992a6973028b407dab0af4fd5ccaa1f2a9d76b6c08ba4b6ece4a5b90de11ef93473f3a
|
data/.gitignore
CHANGED
data/.rspec
ADDED
data/.rubocop.yml
ADDED
@@ -0,0 +1 @@
|
|
1
|
+
require: 'rubocop-rspec'
|
data/CHANGELOG.md
ADDED
@@ -0,0 +1,114 @@
|
|
1
|
+
# Change Log
|
2
|
+
All notable changes to this project will be documented in this file.
|
3
|
+
|
4
|
+
The format is based on [Keep a Changelog](http://keepachangelog.com/)
|
5
|
+
and this project adheres to [Semantic Versioning](http://semver.org/).
|
6
|
+
|
7
|
+
## [3.0.0]
|
8
|
+
### Added
|
9
|
+
|
10
|
+
- Modern gem management
|
11
|
+
- Contributor Covenant CoC
|
12
|
+
|
13
|
+
### Changed
|
14
|
+
|
15
|
+
- Upgraded rspec and tests syntax
|
16
|
+
|
17
|
+
### Deprecated
|
18
|
+
|
19
|
+
- Rails versions smaller than 4.1.x
|
20
|
+
|
21
|
+
### Removed
|
22
|
+
|
23
|
+
- Support for rails versions below 4.1.x
|
24
|
+
|
25
|
+
### Fixed
|
26
|
+
### Security
|
27
|
+
|
28
|
+
## [2.0.0]
|
29
|
+
### Deprecated
|
30
|
+
|
31
|
+
- Rails versions smaller than 4.0.x
|
32
|
+
|
33
|
+
### Removed
|
34
|
+
|
35
|
+
- Support for rails versions below 4.0.x
|
36
|
+
|
37
|
+
## [1.0.5]
|
38
|
+
|
39
|
+
### Changed
|
40
|
+
|
41
|
+
- Use separate internal accessors for with/only_deleted singular association readers
|
42
|
+
|
43
|
+
## [1.0.4]
|
44
|
+
### Changed
|
45
|
+
|
46
|
+
- Extract with_deleted singular assoc readers to separate module
|
47
|
+
|
48
|
+
## [1.0.3]
|
49
|
+
### Added
|
50
|
+
|
51
|
+
- Added back feature where using immortal finders doesn't unscope association scopes.
|
52
|
+
|
53
|
+
## [1.0.2]
|
54
|
+
### Added
|
55
|
+
|
56
|
+
- Added with/only_deleted singular association readers (see specs)
|
57
|
+
|
58
|
+
## [1.0.1]
|
59
|
+
### Added
|
60
|
+
|
61
|
+
- Made compatible with Rails 3.1.X
|
62
|
+
|
63
|
+
## [1.0.0]
|
64
|
+
### Changed
|
65
|
+
|
66
|
+
- Changed the API, made it compatible with Rails 3.1, removed functionality
|
67
|
+
|
68
|
+
## [0.1.6]
|
69
|
+
### Fixed
|
70
|
+
|
71
|
+
- issue 2: with_deleted breaks associations
|
72
|
+
|
73
|
+
## [0.1.5]
|
74
|
+
### Added
|
75
|
+
|
76
|
+
- "without deleted" scope to join model by overriding
|
77
|
+
HasManyThroughAssociation#construct_conditions rather than simply adding to
|
78
|
+
has_many conditions.
|
79
|
+
|
80
|
+
## [0.1.4]
|
81
|
+
### Fixed
|
82
|
+
|
83
|
+
- Bug where ALL records of any dependent associations were immortally deleted if
|
84
|
+
assocation has `:dependant => :delete_all` option set
|
85
|
+
|
86
|
+
## [0.1.3]
|
87
|
+
### Fixed
|
88
|
+
|
89
|
+
- Bug where join model is not immortal
|
90
|
+
|
91
|
+
## [0.1.2]
|
92
|
+
### Fixed
|
93
|
+
|
94
|
+
- Loading issue when the `deleted` column doesn't exist (or even the table)
|
95
|
+
|
96
|
+
## [0.1.1]
|
97
|
+
### Fixed
|
98
|
+
|
99
|
+
- Behavior with `has_many :through` associations
|
100
|
+
|
101
|
+
[Unreleased]: https://github.com/teambox/immortal/compare/v3.0.0...HEAD
|
102
|
+
[3.0.0]: https://github.com/teambox/immortal/compare/v2.0.0...v3.0.0
|
103
|
+
[2.0.0]: https://github.com/teambox/immortal/compare/v1.0.5...v2.0.0
|
104
|
+
[1.0.5]: https://github.com/teambox/immortal/compare/v1.0.4...v1.0.5
|
105
|
+
[1.0.4]: https://github.com/teambox/immortal/compare/v1.0.3...v1.0.4
|
106
|
+
[1.0.3]: https://github.com/teambox/immortal/compare/v1.0.2...v1.0.3
|
107
|
+
[1.0.2]: https://github.com/teambox/immortal/compare/v1.0.1...v1.0.2
|
108
|
+
[1.0.1]: https://github.com/teambox/immortal/compare/v1.0.0...v1.0.1
|
109
|
+
[1.0.0]: https://github.com/teambox/immortal/compare/v0.1.6...v1.0.0
|
110
|
+
[0.1.6]: https://github.com/teambox/immortal/compare/v0.1.5...v0.1.6
|
111
|
+
[0.1.5]: https://github.com/teambox/immortal/compare/v0.1.4...v0.1.5
|
112
|
+
[0.1.4]: https://github.com/teambox/immortal/compare/v0.1.3...v0.1.4
|
113
|
+
[0.1.3]: https://github.com/teambox/immortal/compare/v0.1.2...v0.1.3
|
114
|
+
[0.1.2]: https://github.com/teambox/immortal/compare/v0.1.1...v0.1.2
|
data/CODE_OF_CONDUCT.md
ADDED
@@ -0,0 +1,74 @@
|
|
1
|
+
# Contributor Covenant Code of Conduct
|
2
|
+
|
3
|
+
## Our Pledge
|
4
|
+
|
5
|
+
In the interest of fostering an open and welcoming environment, we as
|
6
|
+
contributors and maintainers pledge to making participation in our project and
|
7
|
+
our community a harassment-free experience for everyone, regardless of age, body
|
8
|
+
size, disability, ethnicity, gender identity and expression, level of experience,
|
9
|
+
nationality, personal appearance, race, religion, or sexual identity and
|
10
|
+
orientation.
|
11
|
+
|
12
|
+
## Our Standards
|
13
|
+
|
14
|
+
Examples of behavior that contributes to creating a positive environment
|
15
|
+
include:
|
16
|
+
|
17
|
+
* Using welcoming and inclusive language
|
18
|
+
* Being respectful of differing viewpoints and experiences
|
19
|
+
* Gracefully accepting constructive criticism
|
20
|
+
* Focusing on what is best for the community
|
21
|
+
* Showing empathy towards other community members
|
22
|
+
|
23
|
+
Examples of unacceptable behavior by participants include:
|
24
|
+
|
25
|
+
* The use of sexualized language or imagery and unwelcome sexual attention or
|
26
|
+
advances
|
27
|
+
* Trolling, insulting/derogatory comments, and personal or political attacks
|
28
|
+
* Public or private harassment
|
29
|
+
* Publishing others' private information, such as a physical or electronic
|
30
|
+
address, without explicit permission
|
31
|
+
* Other conduct which could reasonably be considered inappropriate in a
|
32
|
+
professional setting
|
33
|
+
|
34
|
+
## Our Responsibilities
|
35
|
+
|
36
|
+
Project maintainers are responsible for clarifying the standards of acceptable
|
37
|
+
behavior and are expected to take appropriate and fair corrective action in
|
38
|
+
response to any instances of unacceptable behavior.
|
39
|
+
|
40
|
+
Project maintainers have the right and responsibility to remove, edit, or
|
41
|
+
reject comments, commits, code, wiki edits, issues, and other contributions
|
42
|
+
that are not aligned to this Code of Conduct, or to ban temporarily or
|
43
|
+
permanently any contributor for other behaviors that they deem inappropriate,
|
44
|
+
threatening, offensive, or harmful.
|
45
|
+
|
46
|
+
## Scope
|
47
|
+
|
48
|
+
This Code of Conduct applies both within project spaces and in public spaces
|
49
|
+
when an individual is representing the project or its community. Examples of
|
50
|
+
representing a project or community include using an official project e-mail
|
51
|
+
address, posting via an official social media account, or acting as an appointed
|
52
|
+
representative at an online or offline event. Representation of a project may be
|
53
|
+
further defined and clarified by project maintainers.
|
54
|
+
|
55
|
+
## Enforcement
|
56
|
+
|
57
|
+
Instances of abusive, harassing, or otherwise unacceptable behavior may be
|
58
|
+
reported by contacting the project team at nflamel@gmail.com. All
|
59
|
+
complaints will be reviewed and investigated and will result in a response that
|
60
|
+
is deemed necessary and appropriate to the circumstances. The project team is
|
61
|
+
obligated to maintain confidentiality with regard to the reporter of an incident.
|
62
|
+
Further details of specific enforcement policies may be posted separately.
|
63
|
+
|
64
|
+
Project maintainers who do not follow or enforce the Code of Conduct in good
|
65
|
+
faith may face temporary or permanent repercussions as determined by other
|
66
|
+
members of the project's leadership.
|
67
|
+
|
68
|
+
## Attribution
|
69
|
+
|
70
|
+
This Code of Conduct is adapted from the [Contributor Covenant][homepage], version 1.4,
|
71
|
+
available at [http://contributor-covenant.org/version/1/4][version]
|
72
|
+
|
73
|
+
[homepage]: http://contributor-covenant.org
|
74
|
+
[version]: http://contributor-covenant.org/version/1/4/
|
data/Gemfile
CHANGED
data/LICENSE.txt
ADDED
@@ -0,0 +1,21 @@
|
|
1
|
+
The MIT License (MIT)
|
2
|
+
|
3
|
+
Copyright (c) 2017 Redbooth
|
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.
|
data/README.md
CHANGED
@@ -1,30 +1,38 @@
|
|
1
1
|
# Immortal
|
2
2
|
|
3
|
-
Make any ActiveRecord model paranoid by just including `Immortal`, and instead
|
3
|
+
Make any ActiveRecord model paranoid by just including `Immortal`, and instead
|
4
|
+
of being deleted from the database, the object will just marked as 'deleted'
|
5
|
+
with a boolean field in the database.
|
4
6
|
|
5
7
|
## Installation
|
6
8
|
|
7
9
|
Add the gem dependency to your Gemfile:
|
8
10
|
|
9
|
-
|
11
|
+
```ruby
|
12
|
+
gem 'immortal'
|
13
|
+
```
|
10
14
|
|
11
15
|
## Usage
|
12
16
|
|
13
|
-
|
14
|
-
|
15
|
-
|
17
|
+
```ruby
|
18
|
+
class User < ActiveRecord::Base
|
19
|
+
include Immortal
|
20
|
+
end
|
21
|
+
```
|
16
22
|
|
17
23
|
And add a boolean field called `deleted` to that model:
|
18
24
|
|
19
|
-
|
20
|
-
|
21
|
-
|
22
|
-
|
25
|
+
```ruby
|
26
|
+
class AddDeletedToUsers < ActiveRecord::Migration
|
27
|
+
def self.up
|
28
|
+
add_column :users, :deleted, :boolean
|
29
|
+
end
|
23
30
|
|
24
|
-
|
25
|
-
|
26
|
-
|
27
|
-
|
31
|
+
def self.down
|
32
|
+
remove_column :users, :deleted
|
33
|
+
end
|
34
|
+
end
|
35
|
+
```
|
28
36
|
|
29
37
|
## TODO
|
30
38
|
|
@@ -38,34 +46,3 @@ If you want to improve immortal
|
|
38
46
|
2. Create a topic branch `git checkout -b my_feature`
|
39
47
|
3. Push it! `git push origin my_feature`
|
40
48
|
4. Open a pull request
|
41
|
-
|
42
|
-
## CHANGELOG
|
43
|
-
|
44
|
-
- 1.0.5 Use separate internal accessors for with/only_deleted singular association readers
|
45
|
-
- 1.0.4 Extract with_deleted singular assoc readers to separate module
|
46
|
-
- 1.0.3 Added back feature where using immortal finders doesn't unscope association scopes.
|
47
|
-
- 1.0.2 Added with/only_deleted singular association readers (see specs)
|
48
|
-
- 1.0.1 Made compatible with Rails 3.1.X
|
49
|
-
- 1.0.0 Changed the API, made it compatible with Rails 3.1, removed
|
50
|
-
functionality
|
51
|
-
- 0.1.6 Fixing immortal issue 2: with_deleted breaks associations
|
52
|
-
- 0.1.5 Add "without deleted" scope to join model by overriding HasManyThroughAssociation#construct_conditions
|
53
|
-
rather than simply adding to has_many conditions.
|
54
|
-
- 0.1.4 fix bug where ALL records of any dependent associations were
|
55
|
-
immortally deleted if assocation has :dependant => :delete_all option
|
56
|
-
set
|
57
|
-
- 0.1.3 fix bug where join model is not immortal
|
58
|
-
- 0.1.2 fix loading issue when the `deleted` column doesn't exist (or even the table)
|
59
|
-
- 0.1.1 fix behavior with `has_many :through` associations
|
60
|
-
|
61
|
-
## License
|
62
|
-
|
63
|
-
(The MIT License)
|
64
|
-
|
65
|
-
Copyright (c) Teambox
|
66
|
-
|
67
|
-
Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the 'Software'), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:
|
68
|
-
|
69
|
-
The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.
|
70
|
-
|
71
|
-
THE SOFTWARE IS PROVIDED 'AS IS', WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
|
data/Rakefile
CHANGED
@@ -1,2 +1,2 @@
|
|
1
|
-
require
|
2
|
-
|
1
|
+
require "bundler/gem_tasks"
|
2
|
+
task :default => :spec
|
data/immortal.gemspec
CHANGED
@@ -1,28 +1,35 @@
|
|
1
|
-
#
|
2
|
-
|
1
|
+
# coding: utf-8
|
2
|
+
lib = File.expand_path('../lib', __FILE__)
|
3
|
+
$LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
|
4
|
+
require 'immortal/version'
|
3
5
|
|
4
|
-
Gem::Specification.new do |
|
5
|
-
|
6
|
-
|
7
|
-
|
6
|
+
Gem::Specification.new do |spec|
|
7
|
+
spec.name = 'immortal'
|
8
|
+
spec.version = Immortal::VERSION
|
9
|
+
spec.authors = [
|
8
10
|
'Jordi Romero', 'Saimon Moore', 'Pau Ramon', 'Carlos Saura', 'Andres Bravo',
|
9
11
|
'Fran Casas', 'Pau Perez'
|
10
12
|
]
|
11
|
-
|
12
|
-
|
13
|
-
|
14
|
-
|
15
|
-
|
16
|
-
|
13
|
+
spec.email = ['jordi@jrom.net', 'saimon@saimonmoore.net']
|
14
|
+
spec.homepage = 'http://github.com/teambox/immortal'
|
15
|
+
spec.summary = 'Replacement for acts_as_paranoid for Rails 4'
|
16
|
+
spec.description = 'Typical paranoid gem built for Rails 4 and with the ' \
|
17
|
+
"minimum code needed to satisfy acts_as_paranoid's API"
|
18
|
+
spec.license = 'MIT'
|
17
19
|
|
18
|
-
|
19
|
-
|
20
|
-
|
21
|
-
|
20
|
+
spec.files = `git ls-files -z`.split("\x0").reject do |f|
|
21
|
+
f.match(%r{^(test|spec|features)/})
|
22
|
+
end
|
23
|
+
spec.executables = spec.files.grep(%r{^exe/}) { |f| File.basename(f) }
|
24
|
+
spec.require_paths = %w[lib]
|
22
25
|
|
23
|
-
|
24
|
-
|
25
|
-
|
26
|
-
|
27
|
-
|
26
|
+
spec.add_dependency 'activerecord', '~> 4.1.0'
|
27
|
+
spec.add_development_dependency 'rspec', '~> 3.6.0'
|
28
|
+
spec.add_development_dependency 'rspec-its'
|
29
|
+
spec.add_development_dependency 'sqlite3'
|
30
|
+
spec.add_development_dependency 'byebug'
|
31
|
+
spec.add_development_dependency 'rake', '~> 10.0'
|
32
|
+
spec.add_development_dependency 'rubocop'
|
33
|
+
spec.add_development_dependency 'rubocop-rspec'
|
34
|
+
spec.add_development_dependency 'reek'
|
28
35
|
end
|
data/lib/immortal.rb
CHANGED
@@ -1,31 +1,34 @@
|
|
1
1
|
require 'immortal/belongs_to'
|
2
2
|
|
3
|
+
# Include +Immortal+ module to activate soft delete on your model.
|
3
4
|
module Immortal
|
5
|
+
COLUMN_NAME = 'deleted'.freeze
|
4
6
|
|
5
7
|
def self.included(base)
|
6
8
|
base.send :extend, ClassMethods
|
7
9
|
base.send :include, InstanceMethods
|
8
10
|
base.send :include, BelongsTo
|
11
|
+
|
9
12
|
base.class_eval do
|
10
13
|
class << self
|
11
|
-
|
12
|
-
|
14
|
+
alias_method :mortal_delete_all, :delete_all
|
15
|
+
alias_method :delete_all, :immortal_delete_all
|
13
16
|
end
|
14
17
|
end
|
15
18
|
end
|
16
19
|
|
17
20
|
module ClassMethods
|
18
|
-
|
21
|
+
# @return [Boolean] whether the model supports immortal or not
|
19
22
|
def immortal?
|
20
|
-
|
23
|
+
included_modules.include?(::Immortal::InstanceMethods)
|
21
24
|
end
|
22
25
|
|
23
26
|
def without_default_scope
|
24
|
-
new_scope =
|
25
|
-
our_scope =
|
27
|
+
new_scope = unscoped
|
28
|
+
our_scope = current_scope || unscoped
|
26
29
|
|
27
30
|
non_immortal_constraints_sql = our_scope.arel.constraints.to_a.map do |constraint|
|
28
|
-
constraint.to_sql.split('AND').reject{|clause| clause.include?(
|
31
|
+
constraint.to_sql.split('AND').reject { |clause| clause.include?(COLUMN_NAME) }
|
29
32
|
end.flatten.join(' AND ')
|
30
33
|
|
31
34
|
new_scope = new_scope.merge(our_scope.except(:where))
|
@@ -37,7 +40,7 @@ module Immortal
|
|
37
40
|
end
|
38
41
|
|
39
42
|
def exists?(id = false)
|
40
|
-
|
43
|
+
mortal.exists?(id)
|
41
44
|
end
|
42
45
|
|
43
46
|
def count_with_deleted(*args)
|
@@ -48,38 +51,24 @@ module Immortal
|
|
48
51
|
|
49
52
|
def count_only_deleted(*args)
|
50
53
|
without_default_scope do
|
51
|
-
|
54
|
+
immortal.count(*args)
|
52
55
|
end
|
53
56
|
end
|
54
57
|
|
55
|
-
def where_with_deleted(
|
58
|
+
def where_with_deleted(conditions)
|
56
59
|
without_default_scope do
|
57
|
-
where(
|
60
|
+
where(conditions)
|
58
61
|
end
|
59
62
|
end
|
60
63
|
|
61
|
-
def
|
62
|
-
ActiveSupport::Deprecation.warn('[immortal] we are deprecating #find_with_deleted use where_with_deleted instead')
|
64
|
+
def where_only_deleted(conditions)
|
63
65
|
without_default_scope do
|
64
|
-
|
65
|
-
end
|
66
|
-
end
|
67
|
-
|
68
|
-
def where_only_deleted(*args)
|
69
|
-
without_default_scope do
|
70
|
-
where(deleted: true).where(args)
|
71
|
-
end
|
72
|
-
end
|
73
|
-
|
74
|
-
def find_only_deleted(*args)
|
75
|
-
ActiveSupport::Deprecation.warn('[immortal] we are deprecating #find_only_deleted use where_only_deleted instead')
|
76
|
-
without_default_scope do
|
77
|
-
where(deleted: true).find(*args)
|
66
|
+
immortal.where(conditions)
|
78
67
|
end
|
79
68
|
end
|
80
69
|
|
81
70
|
def immortal_delete_all(conditions = nil)
|
82
|
-
unscoped.where(conditions).update_all(
|
71
|
+
unscoped.where(conditions).update_all(COLUMN_NAME => 1)
|
83
72
|
end
|
84
73
|
|
85
74
|
def delete_all!(*args)
|
@@ -87,26 +76,31 @@ module Immortal
|
|
87
76
|
end
|
88
77
|
|
89
78
|
def undeleted_clause_sql
|
90
|
-
unscoped.
|
79
|
+
unscoped.mortal.constraints.first.to_sql
|
91
80
|
end
|
92
81
|
|
93
82
|
def deleted_clause_sql
|
94
|
-
unscoped.where(arel_table[
|
83
|
+
unscoped.where(arel_table[COLUMN_NAME].eq(true)).constraints.first.to_sql
|
95
84
|
end
|
96
|
-
|
97
85
|
end
|
98
86
|
|
99
87
|
module InstanceMethods
|
100
88
|
def self.included(base)
|
101
|
-
unless base.table_exists? && base.columns_hash[
|
102
|
-
Kernel.warn
|
89
|
+
unless base.table_exists? && base.columns_hash[COLUMN_NAME] && !base.columns_hash[COLUMN_NAME].null
|
90
|
+
Kernel.warn(
|
91
|
+
"[Immortal] The '#{COLUMN_NAME}' column in #{base} is nullable, " \
|
92
|
+
'change the column to not accept NULL values'
|
93
|
+
)
|
103
94
|
end
|
104
95
|
|
105
96
|
base.class_eval do
|
106
|
-
|
97
|
+
scope(:mortal, -> { where(COLUMN_NAME => false) })
|
98
|
+
scope(:immortal, -> { where(COLUMN_NAME => true) })
|
107
99
|
|
108
|
-
|
109
|
-
|
100
|
+
default_scope { -> { mortal } } if arel_table[COLUMN_NAME]
|
101
|
+
|
102
|
+
alias_method :mortal_destroy, :destroy
|
103
|
+
alias_method :destroy, :immortal_destroy
|
110
104
|
end
|
111
105
|
end
|
112
106
|
|
@@ -123,23 +117,35 @@ module Immortal
|
|
123
117
|
end
|
124
118
|
|
125
119
|
def destroy_without_callbacks
|
126
|
-
|
120
|
+
scoped_record.update_all(
|
121
|
+
COLUMN_NAME => true,
|
122
|
+
updated_at: current_time_from_proper_timezone
|
123
|
+
)
|
124
|
+
|
127
125
|
@destroyed = true
|
128
126
|
reload
|
129
127
|
freeze
|
130
128
|
end
|
131
129
|
|
132
130
|
def recover!
|
133
|
-
|
131
|
+
scoped_record.update_all(
|
132
|
+
COLUMN_NAME => false,
|
133
|
+
updated_at: current_time_from_proper_timezone
|
134
|
+
)
|
135
|
+
|
134
136
|
@destroyed = false
|
135
137
|
reload
|
136
138
|
end
|
137
139
|
|
138
140
|
private
|
139
141
|
|
142
|
+
# @return [ActiveRecord::Relation]
|
143
|
+
def scoped_record
|
144
|
+
self.class.unscoped.where(id: id)
|
145
|
+
end
|
146
|
+
|
140
147
|
def current_time_from_proper_timezone
|
141
148
|
ActiveRecord::Base.default_timezone == :utc ? Time.now.utc : Time.now
|
142
149
|
end
|
143
|
-
|
144
150
|
end
|
145
151
|
end
|