paranoid42 1.0.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- checksums.yaml +7 -0
- data/.gitignore +17 -0
- data/Gemfile +2 -0
- data/LICENSE.txt +22 -0
- data/README.md +106 -0
- data/Rakefile +10 -0
- data/lib/paranoid42/persistence.rb +88 -0
- data/lib/paranoid42/scoping.rb +17 -0
- data/lib/paranoid42/version.rb +3 -0
- data/lib/paranoid42.rb +52 -0
- data/paranoid42.gemspec +25 -0
- data/spec/paranoid42_spec.rb +200 -0
- data/spec/spec_helper.rb +24 -0
- data/spec/spec_models.rb +61 -0
- metadata +104 -0
checksums.yaml
ADDED
@@ -0,0 +1,7 @@
|
|
1
|
+
---
|
2
|
+
SHA1:
|
3
|
+
metadata.gz: e570b02db22769fdd1f075786855bb8a0f69b86c
|
4
|
+
data.tar.gz: 50b87789f4e9598ab3ef6684746cd54692230fd8
|
5
|
+
SHA512:
|
6
|
+
metadata.gz: 9bb7953888ba744e8836b14fefa60061fe5df737ba007d5612cd9c8b3e9583cf932224cabf0b689b42ae706c01e6ce17840543717a68c75149b988e81eb5230e
|
7
|
+
data.tar.gz: 332914f9bf6b643c64cccecc22d79b992ba7f53c5aff9ac13bd17f89d935136bc762db0da835e4d7ad3a52437709932b162c58eefbd39064d8748280c4659860
|
data/.gitignore
ADDED
data/Gemfile
ADDED
data/LICENSE.txt
ADDED
@@ -0,0 +1,22 @@
|
|
1
|
+
Copyright (c) 2013 Anjlab
|
2
|
+
|
3
|
+
MIT License
|
4
|
+
|
5
|
+
Permission is hereby granted, free of charge, to any person obtaining
|
6
|
+
a copy of this software and associated documentation files (the
|
7
|
+
"Software"), to deal in the Software without restriction, including
|
8
|
+
without limitation the rights to use, copy, modify, merge, publish,
|
9
|
+
distribute, sublicense, and/or sell copies of the Software, and to
|
10
|
+
permit persons to whom the Software is furnished to do so, subject to
|
11
|
+
the following conditions:
|
12
|
+
|
13
|
+
The above copyright notice and this permission notice shall be
|
14
|
+
included in all copies or substantial portions of the Software.
|
15
|
+
|
16
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
|
17
|
+
EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
|
18
|
+
MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
|
19
|
+
NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
|
20
|
+
LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
|
21
|
+
OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
|
22
|
+
WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
|
data/README.md
ADDED
@@ -0,0 +1,106 @@
|
|
1
|
+
# Paranoid42
|
2
|
+
|
3
|
+
[paranoid2](https://github.com/anjlab/paranoid2) ideas (and code) adapted for rails 4.2
|
4
|
+
|
5
|
+
Rails 4.2 introduced adequate record, but in order to take advantage of the improvements, default_scope can't be used. Paranoid42 removes the default scope from Paranoid2. This means you have to be explicit about telling your app not to return deleted records.
|
6
|
+
|
7
|
+
Paranoid42 removes the default scope and adds a new `not_deleted` method to your paranoid classes.
|
8
|
+
|
9
|
+
So this:
|
10
|
+
|
11
|
+
```
|
12
|
+
> Special.where(name: "test")
|
13
|
+
|
14
|
+
SELECT "specials".* FROM "specials" WHERE (deleted_at IS NULL) AND "specials"."name" = $1 [["name", "test"]]
|
15
|
+
```
|
16
|
+
|
17
|
+
Turns into this:
|
18
|
+
|
19
|
+
```
|
20
|
+
> Special.not_deleted.where(name: "test")
|
21
|
+
|
22
|
+
SELECT "specials".* FROM "specials" WHERE (deleted_at IS NULL) AND "specials"."name" = $1 [["name", "test"]]
|
23
|
+
```
|
24
|
+
|
25
|
+
If you would rather use a shorter method, just create a method with your preferred name, and reference `not_deleted`:
|
26
|
+
|
27
|
+
```
|
28
|
+
def active
|
29
|
+
not_deleted
|
30
|
+
end
|
31
|
+
```
|
32
|
+
|
33
|
+
[Benchmarks](https://gist.github.com/effektz/f18e1be522a328a981b9)
|
34
|
+
|
35
|
+
## Paranoid2
|
36
|
+
|
37
|
+
Rails 4 defines `ActiveRecord::Base#destroy!` so `Paranoid42` gem use `force: true` arg to force destroy.
|
38
|
+
|
39
|
+
## Installation
|
40
|
+
|
41
|
+
Add this line to your application's Gemfile:
|
42
|
+
|
43
|
+
gem 'paranoid42'
|
44
|
+
|
45
|
+
And then execute:
|
46
|
+
|
47
|
+
$ bundle
|
48
|
+
|
49
|
+
## Usage
|
50
|
+
|
51
|
+
Add `deleted_at: datetime` to your model.
|
52
|
+
Generate and run migrations.
|
53
|
+
|
54
|
+
```
|
55
|
+
rails g migration AddDeletedAtToClients deleted_at:datetime
|
56
|
+
```
|
57
|
+
```ruby
|
58
|
+
class AddDeletedAtToClients < ActiveRecord::Migration
|
59
|
+
def change
|
60
|
+
add_column :clients, :deleted_at, :datetime
|
61
|
+
end
|
62
|
+
end
|
63
|
+
```
|
64
|
+
|
65
|
+
```ruby
|
66
|
+
|
67
|
+
class Client < ActiveRecord::Base
|
68
|
+
paranoid
|
69
|
+
end
|
70
|
+
|
71
|
+
c = Client.find(params[:id])
|
72
|
+
|
73
|
+
# will set destroyed_at time
|
74
|
+
c.destroy
|
75
|
+
|
76
|
+
# will restore object and all it's associations
|
77
|
+
c.restore
|
78
|
+
|
79
|
+
# will restore only this object without it's associations
|
80
|
+
c.restore(associations: false)
|
81
|
+
|
82
|
+
# will destroy object for real
|
83
|
+
c.destroy(force: true)
|
84
|
+
|
85
|
+
# also useful scopes are available
|
86
|
+
Client.only_deleted
|
87
|
+
|
88
|
+
```
|
89
|
+
|
90
|
+
### With paperclip
|
91
|
+
|
92
|
+
```ruby
|
93
|
+
class Listing < ActiveRecord::Base
|
94
|
+
has_attached_file :image,
|
95
|
+
# ...
|
96
|
+
preserve_files: true
|
97
|
+
end
|
98
|
+
```
|
99
|
+
|
100
|
+
## Contributing
|
101
|
+
|
102
|
+
1. Fork it
|
103
|
+
2. Create your feature branch (`git checkout -b my-new-feature`)
|
104
|
+
3. Commit your changes (`git commit -am 'Add some feature'`)
|
105
|
+
4. Push to the branch (`git push origin my-new-feature`)
|
106
|
+
5. Create new Pull Request
|
data/Rakefile
ADDED
@@ -0,0 +1,88 @@
|
|
1
|
+
module Paranoid42
|
2
|
+
module Persistence
|
3
|
+
extend ActiveSupport::Concern
|
4
|
+
|
5
|
+
def destroy(opts = {})
|
6
|
+
with_paranoid(opts) { super() }
|
7
|
+
end
|
8
|
+
|
9
|
+
def destroy!(opts = {})
|
10
|
+
with_paranoid(opts) { super() }
|
11
|
+
end
|
12
|
+
|
13
|
+
def delete(opts = {})
|
14
|
+
with_paranoid(opts) do
|
15
|
+
touch(:deleted_at) if !deleted? && persisted?
|
16
|
+
self.class.unscoped { super() } if paranoid_force
|
17
|
+
end
|
18
|
+
end
|
19
|
+
|
20
|
+
def restore(opts={})
|
21
|
+
return if !destroyed?
|
22
|
+
|
23
|
+
attrs = timestamp_attributes_for_update_in_model
|
24
|
+
current_time = current_time_from_proper_timezone
|
25
|
+
changes = {}
|
26
|
+
attrs.each do |column|
|
27
|
+
changes[column.to_s] = write_attribute(column.to_s, current_time)
|
28
|
+
end
|
29
|
+
changes['deleted_at'] = write_attribute('deleted_at', nil)
|
30
|
+
|
31
|
+
changes[self.class.locking_column] = increment_lock if locking_enabled?
|
32
|
+
|
33
|
+
@changed_attributes.except!(*changes.keys)
|
34
|
+
primary_key = self.class.primary_key
|
35
|
+
self.class.unscoped.where({ primary_key => self[primary_key] }).update_all(changes)
|
36
|
+
|
37
|
+
if opts.fetch(:associations) { true }
|
38
|
+
restore_associations
|
39
|
+
end
|
40
|
+
end
|
41
|
+
|
42
|
+
def restore_associations
|
43
|
+
self.class.reflect_on_all_associations.each do |a|
|
44
|
+
next unless a.klass.paranoid?
|
45
|
+
|
46
|
+
if a.collection?
|
47
|
+
send(a.name).restore_all
|
48
|
+
else
|
49
|
+
a.klass.unscoped { send(a.name).try(:restore) }
|
50
|
+
end
|
51
|
+
end
|
52
|
+
end
|
53
|
+
|
54
|
+
def destroyed?
|
55
|
+
!deleted_at.nil?
|
56
|
+
end
|
57
|
+
|
58
|
+
def persisted?
|
59
|
+
!new_record?
|
60
|
+
end
|
61
|
+
|
62
|
+
alias :deleted? :destroyed?
|
63
|
+
|
64
|
+
def destroy_row
|
65
|
+
if paranoid_force
|
66
|
+
self.deleted_at = Time.now
|
67
|
+
super
|
68
|
+
else
|
69
|
+
delete
|
70
|
+
1
|
71
|
+
end
|
72
|
+
end
|
73
|
+
|
74
|
+
module ClassMethods
|
75
|
+
def paranoid? ; true ; end
|
76
|
+
|
77
|
+
def destroy_all!(conditions = nil)
|
78
|
+
with_paranoid(force: true) do
|
79
|
+
destroy_all(conditions)
|
80
|
+
end
|
81
|
+
end
|
82
|
+
|
83
|
+
def restore_all
|
84
|
+
only_deleted.each &:restore
|
85
|
+
end
|
86
|
+
end
|
87
|
+
end
|
88
|
+
end
|
data/lib/paranoid42.rb
ADDED
@@ -0,0 +1,52 @@
|
|
1
|
+
require 'paranoid42/version'
|
2
|
+
|
3
|
+
require 'active_support/concern'
|
4
|
+
require 'active_record'
|
5
|
+
|
6
|
+
require 'paranoid42/persistence'
|
7
|
+
require 'paranoid42/scoping'
|
8
|
+
|
9
|
+
module Paranoid42
|
10
|
+
extend ActiveSupport::Concern
|
11
|
+
|
12
|
+
def paranoid?
|
13
|
+
self.class.paranoid?
|
14
|
+
end
|
15
|
+
|
16
|
+
def paranoid_force
|
17
|
+
self.class.paranoid_force
|
18
|
+
end
|
19
|
+
|
20
|
+
def with_paranoid value, &block
|
21
|
+
self.class.with_paranoid value, &block
|
22
|
+
end
|
23
|
+
|
24
|
+
module ClassMethods
|
25
|
+
def paranoid? ; false ; end
|
26
|
+
|
27
|
+
def paranoid
|
28
|
+
include Persistence
|
29
|
+
include Scoping
|
30
|
+
end
|
31
|
+
|
32
|
+
alias acts_as_paranoid paranoid
|
33
|
+
|
34
|
+
def with_paranoid opts={}
|
35
|
+
forced = opts[:force] || paranoid_force
|
36
|
+
previous, self.paranoid_force = paranoid_force, forced
|
37
|
+
return yield
|
38
|
+
ensure
|
39
|
+
self.paranoid_force = previous
|
40
|
+
end
|
41
|
+
|
42
|
+
def paranoid_force= value
|
43
|
+
Thread.current['paranoid_force'] = value
|
44
|
+
end
|
45
|
+
|
46
|
+
def paranoid_force
|
47
|
+
Thread.current['paranoid_force']
|
48
|
+
end
|
49
|
+
end
|
50
|
+
end
|
51
|
+
|
52
|
+
ActiveRecord::Base.send :include, Paranoid42
|
data/paranoid42.gemspec
ADDED
@@ -0,0 +1,25 @@
|
|
1
|
+
# -*- encoding: utf-8 -*-
|
2
|
+
lib = File.expand_path('../lib', __FILE__)
|
3
|
+
$LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
|
4
|
+
require 'paranoid42/version'
|
5
|
+
|
6
|
+
Gem::Specification.new do |gem|
|
7
|
+
gem.name = "paranoid42"
|
8
|
+
gem.version = Paranoid42::VERSION
|
9
|
+
gem.authors = ["effektz", "yury"]
|
10
|
+
gem.email = ["alexweidmann@gmail.com", "yury.korolev@gmail.com"]
|
11
|
+
gem.description = %q{paranoid models for rails 4.2}
|
12
|
+
gem.summary = %q{paranoid models for rails 4.2}
|
13
|
+
gem.homepage = "https://github.com/effektz/paranoid42"
|
14
|
+
gem.license = "MIT"
|
15
|
+
|
16
|
+
gem.files = `git ls-files`.split($/)
|
17
|
+
gem.executables = gem.files.grep(%r{^bin/}) { |f| File.basename(f) }
|
18
|
+
gem.test_files = gem.files.grep(%r{^(test|spec|features)/})
|
19
|
+
gem.require_paths = ["lib"]
|
20
|
+
|
21
|
+
gem.add_dependency 'activerecord', '>= 4.2.0'
|
22
|
+
|
23
|
+
gem.add_development_dependency "rake"
|
24
|
+
gem.add_development_dependency "sqlite3"
|
25
|
+
end
|
@@ -0,0 +1,200 @@
|
|
1
|
+
require_relative 'spec_helper'
|
2
|
+
|
3
|
+
describe Paranoid42 do
|
4
|
+
it 'has a version number' do
|
5
|
+
Paranoid42::VERSION.wont_be_nil
|
6
|
+
end
|
7
|
+
|
8
|
+
let(:object) { model.new }
|
9
|
+
|
10
|
+
describe PlainModel do
|
11
|
+
let(:model) { PlainModel }
|
12
|
+
before { model.unscoped.delete_all }
|
13
|
+
|
14
|
+
it 'is not paranoid' do
|
15
|
+
model.wont_be :paranoid?
|
16
|
+
end
|
17
|
+
|
18
|
+
it 'has not paranoid object' do
|
19
|
+
object.wont_be :paranoid?
|
20
|
+
end
|
21
|
+
|
22
|
+
it 'has default destroy behavior' do
|
23
|
+
model.count.must_equal 0
|
24
|
+
object.save!
|
25
|
+
model.count.must_equal 1
|
26
|
+
object.destroy
|
27
|
+
object.deleted_at.must_be_nil
|
28
|
+
object.must_be :frozen?
|
29
|
+
model.count.must_equal 0
|
30
|
+
model.unscoped.count.must_equal 0
|
31
|
+
end
|
32
|
+
end
|
33
|
+
|
34
|
+
describe ParanoidModel do
|
35
|
+
let(:model) { ParanoidModel }
|
36
|
+
before { model.unscoped.destroy_all! }
|
37
|
+
|
38
|
+
it 'is paranoid' do
|
39
|
+
model.must_be :paranoid?
|
40
|
+
end
|
41
|
+
|
42
|
+
it 'has paranoid object' do
|
43
|
+
object.must_be :paranoid?
|
44
|
+
end
|
45
|
+
|
46
|
+
it 'returns valid value with to_param' do
|
47
|
+
object.save
|
48
|
+
param = object.to_param
|
49
|
+
object.destroy
|
50
|
+
object.to_param.wont_be_nil
|
51
|
+
object.to_param.must_equal param
|
52
|
+
end
|
53
|
+
|
54
|
+
it "it doesn't actually destroy object" do
|
55
|
+
model.count.must_equal 0
|
56
|
+
object.save!
|
57
|
+
model.count.must_equal 1
|
58
|
+
object.destroy
|
59
|
+
object.deleted_at.wont_be_nil
|
60
|
+
object.must_be :frozen?
|
61
|
+
model.not_deleted.count.must_equal 0
|
62
|
+
model.count.must_equal 1
|
63
|
+
end
|
64
|
+
|
65
|
+
it 'has working only_deleted scope' do
|
66
|
+
a = model.create
|
67
|
+
a.destroy
|
68
|
+
b = model.create
|
69
|
+
model.only_deleted.last.must_equal a
|
70
|
+
model.only_deleted.wont_include b
|
71
|
+
end
|
72
|
+
|
73
|
+
it 'restores' do
|
74
|
+
a = model.create
|
75
|
+
a.destroy
|
76
|
+
a.must_be :destroyed?
|
77
|
+
b = model.only_deleted.find(a.id)
|
78
|
+
b.restore
|
79
|
+
b.reload
|
80
|
+
b.wont_be :destroyed?
|
81
|
+
end
|
82
|
+
|
83
|
+
it 'can be force destroyed' do
|
84
|
+
object.save
|
85
|
+
object.destroy(force: true)
|
86
|
+
object.must_be :destroyed?
|
87
|
+
model.unscoped.count.must_equal 0
|
88
|
+
end
|
89
|
+
|
90
|
+
it 'can be force deleted' do
|
91
|
+
object.save
|
92
|
+
object.delete(force: true)
|
93
|
+
model.unscoped.count.must_equal 0
|
94
|
+
end
|
95
|
+
|
96
|
+
it 'works with relation scopes' do
|
97
|
+
parent1 = ParentModel.create
|
98
|
+
parent2 = ParentModel.create
|
99
|
+
a = model.create(parent_model: parent1)
|
100
|
+
b = model.create(parent_model: parent2)
|
101
|
+
a.destroy
|
102
|
+
b.destroy
|
103
|
+
parent1.paranoid_models.not_deleted.count.must_equal 0
|
104
|
+
parent1.paranoid_models.only_deleted.count.must_equal 1
|
105
|
+
model.create(parent_model: parent1)
|
106
|
+
end
|
107
|
+
|
108
|
+
it 'allows "Model#includes"' do
|
109
|
+
parent1 = ParentModel.create
|
110
|
+
parent2 = ParentModel.create
|
111
|
+
model.create(parent_model: parent1)
|
112
|
+
model.create(parent_model: parent2)
|
113
|
+
model.includes(:parent_model).references(:parent_model).to_a
|
114
|
+
end
|
115
|
+
|
116
|
+
it 'works with has_many_through relationships' do
|
117
|
+
employer = Employer.create
|
118
|
+
employee = Employee.create
|
119
|
+
employer.jobs.count.must_equal 0
|
120
|
+
employer.employees.count.must_equal 0
|
121
|
+
employee.jobs.count.must_equal 0
|
122
|
+
employee.employers.count.must_equal 0
|
123
|
+
job = Job.create employer: employer, employee: employee
|
124
|
+
employer.jobs.count.must_equal 1
|
125
|
+
employer.employees.count.must_equal 1
|
126
|
+
employee.jobs.count.must_equal 1
|
127
|
+
employee.employers.count.must_equal 1
|
128
|
+
employee2 = Employee.create
|
129
|
+
Job.create employer: employer, employee: employee2
|
130
|
+
employee2.destroy
|
131
|
+
employer.jobs.count.must_equal 2
|
132
|
+
employer.employees.not_deleted.count.must_equal 1
|
133
|
+
job.destroy
|
134
|
+
employer.jobs.not_deleted.count.must_equal 1
|
135
|
+
employer.employees.not_deleted.count.must_equal 1
|
136
|
+
employee.jobs.not_deleted.count.must_equal 0
|
137
|
+
employee.employers.not_deleted.count.must_equal 1
|
138
|
+
end
|
139
|
+
|
140
|
+
it 'restores has_many associations' do
|
141
|
+
parent = ParentModel.create
|
142
|
+
a = model.create(parent_model: parent)
|
143
|
+
parent.destroy
|
144
|
+
a.reload
|
145
|
+
parent.must_be :destroyed?
|
146
|
+
a.must_be :destroyed?
|
147
|
+
parent = ParentModel.unscoped.find(parent.id)
|
148
|
+
parent.restore
|
149
|
+
a.reload
|
150
|
+
parent.wont_be :destroyed?
|
151
|
+
a.wont_be :destroyed?
|
152
|
+
end
|
153
|
+
|
154
|
+
it 'restores belongs_to associations' do
|
155
|
+
parent = ParentModel.create
|
156
|
+
a = model.create(parent_model: parent)
|
157
|
+
parent.destroy
|
158
|
+
a.reload
|
159
|
+
parent.must_be :destroyed?
|
160
|
+
a.must_be :destroyed?
|
161
|
+
a.restore
|
162
|
+
a.wont_be :destroyed?
|
163
|
+
a.parent_model.wont_be :destroyed?
|
164
|
+
end
|
165
|
+
end
|
166
|
+
|
167
|
+
describe CallbackModel do
|
168
|
+
let(:model) { CallbackModel }
|
169
|
+
|
170
|
+
it 'delete without callback' do
|
171
|
+
object.save
|
172
|
+
object.delete
|
173
|
+
object.callback_called.must_be_nil
|
174
|
+
end
|
175
|
+
|
176
|
+
it 'destroy with callback' do
|
177
|
+
object.save
|
178
|
+
object.destroy
|
179
|
+
object.callback_called.must_equal true
|
180
|
+
end
|
181
|
+
end
|
182
|
+
|
183
|
+
describe FeaturefulModel do
|
184
|
+
let(:model) { FeaturefulModel }
|
185
|
+
before { model.unscoped.destroy_all! }
|
186
|
+
|
187
|
+
it 'chains paranoid models' do
|
188
|
+
scope = model.where(name: 'foo').only_deleted
|
189
|
+
scope.where_values_hash['name'].must_equal 'foo'
|
190
|
+
end
|
191
|
+
|
192
|
+
it 'validates uniqueness with scope' do
|
193
|
+
a = model.create!(name: 'yury', phone: '9106701550')
|
194
|
+
b = model.create(name: 'effektz', phone: '3034207100')
|
195
|
+
b.wont_be :valid?
|
196
|
+
a.destroy
|
197
|
+
b.must_be :valid?
|
198
|
+
end
|
199
|
+
end
|
200
|
+
end
|
data/spec/spec_helper.rb
ADDED
@@ -0,0 +1,24 @@
|
|
1
|
+
require 'bundler'
|
2
|
+
Bundler.require
|
3
|
+
|
4
|
+
require 'minitest/autorun'
|
5
|
+
|
6
|
+
DB_FILE = 'tmp/test_db'
|
7
|
+
|
8
|
+
FileUtils.mkpath File.dirname(DB_FILE)
|
9
|
+
FileUtils.rm_f DB_FILE
|
10
|
+
|
11
|
+
ActiveRecord::Base.establish_connection adapter: 'sqlite3', :database => DB_FILE
|
12
|
+
|
13
|
+
[ 'CREATE TABLE parent_models (id INTEGER NOT NULL PRIMARY KEY, deleted_at DATETIME)',
|
14
|
+
'CREATE TABLE paranoid_models (id INTEGER NOT NULL PRIMARY KEY, parent_model_id INTEGER, deleted_at DATETIME)',
|
15
|
+
'CREATE TABLE featureful_models (id INTEGER NOT NULL PRIMARY KEY, deleted_at DATETIME, name VARCHAR(32), phone VARCHAR(20))',
|
16
|
+
'CREATE TABLE plain_models (id INTEGER NOT NULL PRIMARY KEY, deleted_at DATETIME)',
|
17
|
+
'CREATE TABLE callback_models (id INTEGER NOT NULL PRIMARY KEY, deleted_at DATETIME)',
|
18
|
+
'CREATE TABLE related_models (id INTEGER NOT NULL PRIMARY KEY, parent_model_id INTEGER NOT NULL, deleted_at DATETIME)',
|
19
|
+
'CREATE TABLE employers (id INTEGER NOT NULL PRIMARY KEY, deleted_at DATETIME)',
|
20
|
+
'CREATE TABLE employees (id INTEGER NOT NULL PRIMARY KEY, deleted_at DATETIME)',
|
21
|
+
'CREATE TABLE jobs (id INTEGER NOT NULL PRIMARY KEY, employer_id INTEGER NOT NULL, employee_id INTEGER NOT NULL, deleted_at DATETIME)'
|
22
|
+
].each {|script| ActiveRecord::Base.connection.execute script }
|
23
|
+
|
24
|
+
require_relative 'spec_models'
|
data/spec/spec_models.rb
ADDED
@@ -0,0 +1,61 @@
|
|
1
|
+
class ParentModel < ActiveRecord::Base
|
2
|
+
has_many :paranoid_models, dependent: :destroy
|
3
|
+
end
|
4
|
+
|
5
|
+
class PlainModel < ActiveRecord::Base
|
6
|
+
end
|
7
|
+
|
8
|
+
class ParanoidModel < ActiveRecord::Base
|
9
|
+
acts_as_paranoid
|
10
|
+
|
11
|
+
belongs_to :parent_model
|
12
|
+
end
|
13
|
+
|
14
|
+
class FeaturefulModel < ActiveRecord::Base
|
15
|
+
paranoid
|
16
|
+
|
17
|
+
validates :name, presence: true, uniqueness: true
|
18
|
+
validates :phone, uniqueness: {conditions: -> { not_deleted } }
|
19
|
+
|
20
|
+
scope :find_last, lambda {|name| where(name: name).last }
|
21
|
+
end
|
22
|
+
|
23
|
+
class CallbackModel < ActiveRecord::Base
|
24
|
+
paranoid
|
25
|
+
|
26
|
+
attr_accessor :callback_called
|
27
|
+
|
28
|
+
before_destroy {|model| model.callback_called = true }
|
29
|
+
end
|
30
|
+
|
31
|
+
class ParentModel < ActiveRecord::Base
|
32
|
+
paranoid
|
33
|
+
has_many :related_models
|
34
|
+
end
|
35
|
+
|
36
|
+
class RelatedModel < ActiveRecord::Base
|
37
|
+
paranoid
|
38
|
+
|
39
|
+
belongs_to :parent_model
|
40
|
+
end
|
41
|
+
|
42
|
+
class Employer < ActiveRecord::Base
|
43
|
+
paranoid
|
44
|
+
|
45
|
+
has_many :jobs
|
46
|
+
has_many :employees, through: :jobs
|
47
|
+
end
|
48
|
+
|
49
|
+
class Employee < ActiveRecord::Base
|
50
|
+
paranoid
|
51
|
+
|
52
|
+
has_many :jobs
|
53
|
+
has_many :employers, through: :jobs
|
54
|
+
end
|
55
|
+
|
56
|
+
class Job < ActiveRecord::Base
|
57
|
+
paranoid
|
58
|
+
|
59
|
+
belongs_to :employer
|
60
|
+
belongs_to :employee
|
61
|
+
end
|
metadata
ADDED
@@ -0,0 +1,104 @@
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
|
+
name: paranoid42
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
version: 1.0.0
|
5
|
+
platform: ruby
|
6
|
+
authors:
|
7
|
+
- effektz
|
8
|
+
- yury
|
9
|
+
autorequire:
|
10
|
+
bindir: bin
|
11
|
+
cert_chain: []
|
12
|
+
date: 2015-01-02 00:00:00.000000000 Z
|
13
|
+
dependencies:
|
14
|
+
- !ruby/object:Gem::Dependency
|
15
|
+
name: activerecord
|
16
|
+
requirement: !ruby/object:Gem::Requirement
|
17
|
+
requirements:
|
18
|
+
- - ">="
|
19
|
+
- !ruby/object:Gem::Version
|
20
|
+
version: 4.2.0
|
21
|
+
type: :runtime
|
22
|
+
prerelease: false
|
23
|
+
version_requirements: !ruby/object:Gem::Requirement
|
24
|
+
requirements:
|
25
|
+
- - ">="
|
26
|
+
- !ruby/object:Gem::Version
|
27
|
+
version: 4.2.0
|
28
|
+
- !ruby/object:Gem::Dependency
|
29
|
+
name: rake
|
30
|
+
requirement: !ruby/object:Gem::Requirement
|
31
|
+
requirements:
|
32
|
+
- - ">="
|
33
|
+
- !ruby/object:Gem::Version
|
34
|
+
version: '0'
|
35
|
+
type: :development
|
36
|
+
prerelease: false
|
37
|
+
version_requirements: !ruby/object:Gem::Requirement
|
38
|
+
requirements:
|
39
|
+
- - ">="
|
40
|
+
- !ruby/object:Gem::Version
|
41
|
+
version: '0'
|
42
|
+
- !ruby/object:Gem::Dependency
|
43
|
+
name: sqlite3
|
44
|
+
requirement: !ruby/object:Gem::Requirement
|
45
|
+
requirements:
|
46
|
+
- - ">="
|
47
|
+
- !ruby/object:Gem::Version
|
48
|
+
version: '0'
|
49
|
+
type: :development
|
50
|
+
prerelease: false
|
51
|
+
version_requirements: !ruby/object:Gem::Requirement
|
52
|
+
requirements:
|
53
|
+
- - ">="
|
54
|
+
- !ruby/object:Gem::Version
|
55
|
+
version: '0'
|
56
|
+
description: paranoid models for rails 4.2
|
57
|
+
email:
|
58
|
+
- alexweidmann@gmail.com
|
59
|
+
- yury.korolev@gmail.com
|
60
|
+
executables: []
|
61
|
+
extensions: []
|
62
|
+
extra_rdoc_files: []
|
63
|
+
files:
|
64
|
+
- ".gitignore"
|
65
|
+
- Gemfile
|
66
|
+
- LICENSE.txt
|
67
|
+
- README.md
|
68
|
+
- Rakefile
|
69
|
+
- lib/paranoid42.rb
|
70
|
+
- lib/paranoid42/persistence.rb
|
71
|
+
- lib/paranoid42/scoping.rb
|
72
|
+
- lib/paranoid42/version.rb
|
73
|
+
- paranoid42.gemspec
|
74
|
+
- spec/paranoid42_spec.rb
|
75
|
+
- spec/spec_helper.rb
|
76
|
+
- spec/spec_models.rb
|
77
|
+
homepage: https://github.com/effektz/paranoid42
|
78
|
+
licenses:
|
79
|
+
- MIT
|
80
|
+
metadata: {}
|
81
|
+
post_install_message:
|
82
|
+
rdoc_options: []
|
83
|
+
require_paths:
|
84
|
+
- lib
|
85
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
86
|
+
requirements:
|
87
|
+
- - ">="
|
88
|
+
- !ruby/object:Gem::Version
|
89
|
+
version: '0'
|
90
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
91
|
+
requirements:
|
92
|
+
- - ">="
|
93
|
+
- !ruby/object:Gem::Version
|
94
|
+
version: '0'
|
95
|
+
requirements: []
|
96
|
+
rubyforge_project:
|
97
|
+
rubygems_version: 2.4.5
|
98
|
+
signing_key:
|
99
|
+
specification_version: 4
|
100
|
+
summary: paranoid models for rails 4.2
|
101
|
+
test_files:
|
102
|
+
- spec/paranoid42_spec.rb
|
103
|
+
- spec/spec_helper.rb
|
104
|
+
- spec/spec_models.rb
|