paranoid2 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.
data/.gitignore ADDED
@@ -0,0 +1,17 @@
1
+ *.gem
2
+ *.rbc
3
+ .bundle
4
+ .config
5
+ .yardoc
6
+ Gemfile.lock
7
+ InstalledFiles
8
+ _yardoc
9
+ coverage
10
+ doc/
11
+ lib/bundler/man
12
+ pkg
13
+ rdoc
14
+ spec/reports
15
+ test/tmp
16
+ test/version_tmp
17
+ tmp
data/Gemfile ADDED
@@ -0,0 +1,7 @@
1
+ source 'https://rubygems.org'
2
+
3
+ gem 'rails', github: 'rails/rails'
4
+ gem 'arel', github: 'rails/arel'
5
+
6
+ # Specify your gem's dependencies in paranoid2.gemspec
7
+ gemspec
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,65 @@
1
+ # Paranoid2
2
+
3
+ [paranoia gem](https://github.com/radar/paranoia) ideas (and code) adapted for rails 4.
4
+
5
+ Rails 4 defines `ActiveRecord::Base#destroy!` so `Paranoid2` gem use `force: true` arg to force destroy.
6
+
7
+ ## Installation
8
+
9
+ Add this line to your application's Gemfile:
10
+
11
+ gem 'paranoid2'
12
+
13
+ And then execute:
14
+
15
+ $ bundle
16
+
17
+ ## Usage
18
+
19
+ Add `deleted_at: datetime` to your model.
20
+ Generate and run migrations.
21
+
22
+ ```
23
+ rails g migration AddDeletedAtToClients deleted_at:datetime
24
+ ```
25
+ ```ruby
26
+ class AddDeletedAtToClients < ActiveRecord::Migration
27
+ def change
28
+ add_column :clients, :deleted_at, :datetime
29
+ end
30
+ end
31
+ ```
32
+
33
+ ```ruby
34
+
35
+ class Client < ActiveRecord::Base
36
+ paranoid
37
+ end
38
+
39
+ c = Client.find(params[:id])
40
+
41
+ # will set destroyed_at time
42
+ c.destroy
43
+
44
+ # will restore object and all it's associations
45
+ c.restore
46
+
47
+ # will restore only this object without it's associations
48
+ c.restore(associations: false)
49
+
50
+ # will destroy object for real
51
+ c.destroy(force: true)
52
+
53
+ # also useful scopes are available
54
+ Client.with_deleted
55
+ Client.only_deleted
56
+
57
+ ```
58
+
59
+ ## Contributing
60
+
61
+ 1. Fork it
62
+ 2. Create your feature branch (`git checkout -b my-new-feature`)
63
+ 3. Commit your changes (`git commit -am 'Add some feature'`)
64
+ 4. Push to the branch (`git push origin my-new-feature`)
65
+ 5. Create new Pull Request
data/Rakefile ADDED
@@ -0,0 +1,10 @@
1
+ require 'bundler/gem_tasks'
2
+
3
+ require 'rake/testtask'
4
+
5
+ Rake::TestTask.new do |t|
6
+ t.libs.push "spec"
7
+ t.test_files = FileList['spec/**/*_spec.rb']
8
+ end
9
+
10
+ task default: :test
data/lib/paranoid2.rb ADDED
@@ -0,0 +1,50 @@
1
+ require 'paranoid2/version'
2
+
3
+ require 'active_support/concern'
4
+ require 'active_record'
5
+
6
+ require 'paranoid2/persistence'
7
+ require 'paranoid2/scoping'
8
+
9
+ module Paranoid2
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
+ def with_paranoid opts={}
33
+ forced = opts[:force] || paranoid_force
34
+ previous, self.paranoid_force = paranoid_force, forced
35
+ return yield
36
+ ensure
37
+ self.paranoid_force = previous
38
+ end
39
+
40
+ def paranoid_force= value
41
+ Thread.current['paranoid_force'] = value
42
+ end
43
+
44
+ def paranoid_force
45
+ Thread.current['paranoid_force']
46
+ end
47
+ end
48
+ end
49
+
50
+ ActiveRecord::Base.send :include, Paranoid2
@@ -0,0 +1,80 @@
1
+ module Paranoid2
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
+ update_column(:deleted_at, Time.now) if !deleted? && persisted?
16
+ if paranoid_force
17
+ self.class.unscoped { super() }
18
+ else
19
+ freeze
20
+ end
21
+ end
22
+ end
23
+
24
+ def restore(opts={})
25
+ return if !destroyed?
26
+
27
+ update_column :deleted_at, nil
28
+
29
+ if opts.fetch(:associations) { true }
30
+ restore_associations
31
+ end
32
+ end
33
+
34
+ def restore_associations
35
+ self.class.reflect_on_all_associations.each do |a|
36
+ next unless a.klass.paranoid?
37
+
38
+ if a.collection?
39
+ send(a.name).restore_all
40
+ else
41
+ a.klass.unscoped { send(a.name).try(:restore) }
42
+ end
43
+ end
44
+ end
45
+
46
+ def destroyed?
47
+ !deleted_at.nil?
48
+ end
49
+
50
+ def persisted?
51
+ !new_record?
52
+ end
53
+
54
+ alias :deleted? :destroyed?
55
+
56
+ def destroy_row
57
+ if paranoid_force
58
+ self.deleted_at = Time.now
59
+ super
60
+ else
61
+ delete
62
+ 1
63
+ end
64
+ end
65
+
66
+ module ClassMethods
67
+ def paranoid? ; true ; end
68
+
69
+ def destroy_all!(conditions = nil)
70
+ with_paranoid(force: true) do
71
+ destroy_all(conditions)
72
+ end
73
+ end
74
+
75
+ def restore_all
76
+ only_deleted.each &:restore
77
+ end
78
+ end
79
+ end
80
+ end
@@ -0,0 +1,24 @@
1
+ module Paranoid2
2
+ module Scoping
3
+ extend ActiveSupport::Concern
4
+
5
+ included do
6
+ default_scope { paranoid_scope }
7
+ end
8
+
9
+ module ClassMethods
10
+
11
+ def paranoid_scope
12
+ where(deleted_at: nil)
13
+ end
14
+
15
+ def only_deleted
16
+ with_deleted.where.not(deleted_at: nil)
17
+ end
18
+
19
+ def with_deleted
20
+ all.tap { |s| s.default_scoped = false }
21
+ end
22
+ end
23
+ end
24
+ end
@@ -0,0 +1,3 @@
1
+ module Paranoid2
2
+ VERSION = "1.0.0"
3
+ end
data/paranoid2.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 'paranoid2/version'
5
+
6
+ Gem::Specification.new do |gem|
7
+ gem.name = "paranoid2"
8
+ gem.version = Paranoid2::VERSION
9
+ gem.authors = ["yury"]
10
+ gem.email = ["yury.korolev@gmail.com"]
11
+ gem.description = %q{paranoid models for rails 4}
12
+ gem.summary = %q{paranoid models for rails 4}
13
+ gem.homepage = "https://github.com/anjlab/paranoid2"
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.0.0.beta'
22
+
23
+ gem.add_development_dependency "rake"
24
+ gem.add_development_dependency "sqlite3"
25
+ end
@@ -0,0 +1,222 @@
1
+ require_relative 'spec_helper'
2
+
3
+ describe Paranoid2 do
4
+ it 'has a version number' do
5
+ Paranoid2::VERSION.wont_be_nil
6
+ end
7
+
8
+ let(:object) { model.new }
9
+
10
+ describe PlainModel do
11
+ let(:model) { PlainModel }
12
+
13
+ before { model.unscoped.delete_all }
14
+
15
+ it 'is not paranoid' do
16
+ model.wont_be :paranoid?
17
+ end
18
+
19
+ it 'has not paranoid object' do
20
+ object.wont_be :paranoid?
21
+ end
22
+
23
+ it 'has default destroy behavior' do
24
+ model.count.must_equal 0
25
+ object.save!
26
+ model.count.must_equal 1
27
+
28
+ object.destroy
29
+
30
+ object.deleted_at.must_be_nil
31
+ object.must_be :frozen?
32
+
33
+ model.count.must_equal 0
34
+ model.unscoped.count.must_equal 0
35
+ end
36
+ end
37
+
38
+ describe ParanoidModel do
39
+ let(:model) { ParanoidModel }
40
+
41
+ before { model.unscoped.destroy_all! }
42
+
43
+ it 'is paranoid' do
44
+ model.must_be :paranoid?
45
+ end
46
+
47
+ it 'has paranoid object' do
48
+ object.must_be :paranoid?
49
+ end
50
+
51
+ it 'returns valid value with to_param' do
52
+ object.save
53
+ param = object.to_param
54
+
55
+ object.destroy
56
+
57
+ object.to_param.wont_be_nil
58
+ object.to_param.must_equal param
59
+ end
60
+
61
+ it "it doesn't actually destroy object" do
62
+ model.count.must_equal 0
63
+ object.save!
64
+ model.count.must_equal 1
65
+
66
+ object.destroy
67
+
68
+ object.deleted_at.wont_be_nil
69
+ object.must_be :frozen?
70
+
71
+ model.count.must_equal 0
72
+ model.unscoped.count.must_equal 1
73
+ end
74
+
75
+ it 'has working only_deleted scope' do
76
+ a = model.create
77
+ a.destroy
78
+ b = model.create
79
+
80
+ model.only_deleted.last.must_equal a
81
+ model.only_deleted.wont_include b
82
+ end
83
+
84
+ it 'restores' do
85
+ a = model.create
86
+ a.destroy
87
+ a.must_be :destroyed?
88
+
89
+ b = model.only_deleted.find(a.id)
90
+ b.restore
91
+ b.reload
92
+ b.wont_be :destroyed?
93
+ end
94
+
95
+ it 'can be force destroyed' do
96
+ object.save
97
+ object.destroy(force: true)
98
+
99
+ object.must_be :destroyed?
100
+
101
+ model.unscoped.count.must_equal 0
102
+ end
103
+
104
+ it 'can be force deleted' do
105
+ object.save
106
+ object.delete(force: true)
107
+
108
+ model.unscoped.count.must_equal 0
109
+ end
110
+
111
+ it 'works with relation scopes' do
112
+ parent1 = ParentModel.create
113
+ parent2 = ParentModel.create
114
+ a = model.create(parent_model: parent1)
115
+ b = model.create(parent_model: parent2)
116
+ a.destroy
117
+ b.destroy
118
+ parent1.paranoid_models.count.must_equal 0
119
+ parent1.paranoid_models.only_deleted.count.must_equal 1
120
+
121
+ c = model.create(parent_model: parent1)
122
+ parent1.paranoid_models.with_deleted.count.must_equal 2
123
+ parent1.paranoid_models.with_deleted.must_equal [a, c]
124
+ end
125
+
126
+ it 'works with has_many_through relationships' do
127
+ employer = Employer.create
128
+ employee = Employee.create
129
+
130
+ employer.jobs.count.must_equal 0
131
+ employer.employees.count.must_equal 0
132
+ employee.jobs.count.must_equal 0
133
+ employee.employers.count.must_equal 0
134
+
135
+ job = Job.create employer: employer, employee: employee
136
+
137
+ employer.jobs.count.must_equal 1
138
+ employer.employees.count.must_equal 1
139
+ employee.jobs.count.must_equal 1
140
+ employee.employers.count.must_equal 1
141
+
142
+ employee2 = Employee.create
143
+ job2 = Job.create employer: employer, employee: employee2
144
+ employee2.destroy
145
+
146
+ employer.jobs.count.must_equal 2
147
+ employer.employees.count.must_equal 1
148
+
149
+ job.destroy
150
+
151
+ employer.jobs.count.must_equal 1
152
+ employer.employees.count.must_equal 0
153
+ employee.jobs.count.must_equal 0
154
+ employee.employers.count.must_equal 0
155
+ end
156
+
157
+ it 'restores has_many associations' do
158
+ parent = ParentModel.create
159
+ a = model.create(parent_model: parent)
160
+ parent.destroy
161
+
162
+ a.reload
163
+
164
+ parent.must_be :destroyed?
165
+ a.must_be :destroyed?
166
+
167
+ parent = ParentModel.unscoped.find(parent.id)
168
+ parent.restore
169
+
170
+ a.reload
171
+
172
+ parent.wont_be :destroyed?
173
+ a.wont_be :destroyed?
174
+ end
175
+
176
+ it 'restores belongs_to associations' do
177
+ parent = ParentModel.create
178
+ a = model.create(parent_model: parent)
179
+
180
+ parent.destroy
181
+
182
+ a.reload
183
+
184
+ parent.must_be :destroyed?
185
+ a.must_be :destroyed?
186
+
187
+ a.restore
188
+
189
+ a.wont_be :destroyed?
190
+ a.parent_model.wont_be :destroyed?
191
+ end
192
+ end
193
+
194
+ describe CallbackModel do
195
+ let(:model) { CallbackModel }
196
+
197
+ it 'delete without callback' do
198
+ object.save
199
+ object.delete
200
+
201
+ object.callback_called.must_be_nil
202
+ end
203
+
204
+ it 'destroy with callback' do
205
+ object.save
206
+ object.destroy
207
+
208
+ object.callback_called.must_equal true
209
+ end
210
+ end
211
+
212
+ describe FeaturefulModel do
213
+ let(:model) { FeaturefulModel }
214
+
215
+ before { model.unscoped.destroy_all! }
216
+
217
+ it 'chains paranoid models' do
218
+ scope = model.where(name: 'foo').only_deleted
219
+ scope.where_values_hash[:name].must_equal 'foo'
220
+ end
221
+ end
222
+ end
@@ -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))',
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'
@@ -0,0 +1,58 @@
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
+ 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
+ end
19
+
20
+ class CallbackModel < ActiveRecord::Base
21
+ paranoid
22
+
23
+ attr_accessor :callback_called
24
+
25
+ before_destroy {|model| model.callback_called = true }
26
+ end
27
+
28
+ class ParentModel < ActiveRecord::Base
29
+ paranoid
30
+ has_many :related_models
31
+ end
32
+
33
+ class RelatedModel < ActiveRecord::Base
34
+ paranoid
35
+
36
+ belongs_to :parent_model
37
+ end
38
+
39
+ class Employer < ActiveRecord::Base
40
+ paranoid
41
+
42
+ has_many :jobs
43
+ has_many :employees, through: :jobs
44
+ end
45
+
46
+ class Employee < ActiveRecord::Base
47
+ paranoid
48
+
49
+ has_many :jobs
50
+ has_many :employers, through: :jobs
51
+ end
52
+
53
+ class Job < ActiveRecord::Base
54
+ paranoid
55
+
56
+ belongs_to :employer
57
+ belongs_to :employee
58
+ end
metadata ADDED
@@ -0,0 +1,116 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: paranoid2
3
+ version: !ruby/object:Gem::Version
4
+ prerelease:
5
+ version: 1.0.0
6
+ platform: ruby
7
+ authors:
8
+ - yury
9
+ autorequire:
10
+ bindir: bin
11
+ cert_chain: []
12
+ date: 2013-01-05 00:00:00.000000000 Z
13
+ dependencies:
14
+ - !ruby/object:Gem::Dependency
15
+ name: activerecord
16
+ version_requirements: !ruby/object:Gem::Requirement
17
+ none: false
18
+ requirements:
19
+ - - ! '>='
20
+ - !ruby/object:Gem::Version
21
+ version: 4.0.0.beta
22
+ requirement: !ruby/object:Gem::Requirement
23
+ none: false
24
+ requirements:
25
+ - - ! '>='
26
+ - !ruby/object:Gem::Version
27
+ version: 4.0.0.beta
28
+ prerelease: false
29
+ type: :runtime
30
+ - !ruby/object:Gem::Dependency
31
+ name: rake
32
+ version_requirements: !ruby/object:Gem::Requirement
33
+ none: false
34
+ requirements:
35
+ - - ! '>='
36
+ - !ruby/object:Gem::Version
37
+ version: '0'
38
+ requirement: !ruby/object:Gem::Requirement
39
+ none: false
40
+ requirements:
41
+ - - ! '>='
42
+ - !ruby/object:Gem::Version
43
+ version: '0'
44
+ prerelease: false
45
+ type: :development
46
+ - !ruby/object:Gem::Dependency
47
+ name: sqlite3
48
+ version_requirements: !ruby/object:Gem::Requirement
49
+ none: false
50
+ requirements:
51
+ - - ! '>='
52
+ - !ruby/object:Gem::Version
53
+ version: '0'
54
+ requirement: !ruby/object:Gem::Requirement
55
+ none: false
56
+ requirements:
57
+ - - ! '>='
58
+ - !ruby/object:Gem::Version
59
+ version: '0'
60
+ prerelease: false
61
+ type: :development
62
+ description: paranoid models for rails 4
63
+ email:
64
+ - yury.korolev@gmail.com
65
+ executables: []
66
+ extensions: []
67
+ extra_rdoc_files: []
68
+ files:
69
+ - .gitignore
70
+ - Gemfile
71
+ - LICENSE.txt
72
+ - README.md
73
+ - Rakefile
74
+ - lib/paranoid2.rb
75
+ - lib/paranoid2/persistence.rb
76
+ - lib/paranoid2/scoping.rb
77
+ - lib/paranoid2/version.rb
78
+ - paranoid2.gemspec
79
+ - spec/paranoid2_spec.rb
80
+ - spec/spec_helper.rb
81
+ - spec/spec_models.rb
82
+ homepage: https://github.com/anjlab/paranoid2
83
+ licenses:
84
+ - MIT
85
+ post_install_message:
86
+ rdoc_options: []
87
+ require_paths:
88
+ - lib
89
+ required_ruby_version: !ruby/object:Gem::Requirement
90
+ none: false
91
+ requirements:
92
+ - - ! '>='
93
+ - !ruby/object:Gem::Version
94
+ version: '0'
95
+ hash: 2778073889302418398
96
+ segments:
97
+ - 0
98
+ required_rubygems_version: !ruby/object:Gem::Requirement
99
+ none: false
100
+ requirements:
101
+ - - ! '>='
102
+ - !ruby/object:Gem::Version
103
+ version: '0'
104
+ hash: 2778073889302418398
105
+ segments:
106
+ - 0
107
+ requirements: []
108
+ rubyforge_project:
109
+ rubygems_version: 1.8.24
110
+ signing_key:
111
+ specification_version: 3
112
+ summary: paranoid models for rails 4
113
+ test_files:
114
+ - spec/paranoid2_spec.rb
115
+ - spec/spec_helper.rb
116
+ - spec/spec_models.rb