paranoia 2.0.0 → 2.0.1

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: 9e9ddf3be1c331ce833d233557bdf81876c68768
4
- data.tar.gz: 8fb6cc8d861471436bbddda7331923395650eadb
3
+ metadata.gz: 3024de08e3ab1e77b80e2f9ad7b975f0b642d204
4
+ data.tar.gz: 05e1fa475bbc9a4db39cec410375d7f33d8f09b4
5
5
  SHA512:
6
- metadata.gz: 086ef0efc989048c0c41c1f00127664cfedf49a4c356e3315f1e87bf7b5d4bdd48bf1d299580d31126e8fc54de84ca231318511c199ec1c4be502068a44dc060
7
- data.tar.gz: ce2574eaf67cf0d570e7139f3ea5f3bd09090449f55d577f955182661cc6204b62a7188d24dbf44422ca423ed6da126e654a6bd5bf1a005ab978f33e0a092c48
6
+ metadata.gz: f03869fc8a9c06d6fe005db5deaea540240b05aa65b707698e4a47bcaabef77fc24541777c90e3401060340afa1fc26940ef49e2ac3d0df66b8f6d2d7bf56389
7
+ data.tar.gz: d60317d533c98f1ca9cf157467fb122a969dc530f022f59a1a98006f4a6b2adef0e741993dd65ab84fb61d3fd63abed36bacffddded47dcfdc6c8a1eb3b13e5c
data/README.md CHANGED
@@ -4,12 +4,28 @@ Paranoia is a re-implementation of [acts\_as\_paranoid](http://github.com/techno
4
4
 
5
5
  You would use either plugin / gem if you wished that when you called `destroy` on an Active Record object that it didn't actually destroy it, but just "hid" the record. Paranoia does this by setting a `deleted_at` field to the current time when you `destroy` a record, and hides it by scoping all queries on your model to only include records which do not have a `deleted_at` field.
6
6
 
7
+ If you wish to actually destroy an object you may call destroy! on it or simply call destroy twice on the same object.
8
+
7
9
  ## Installation & Usage
8
10
 
9
- Put this in your Gemfile:
11
+ For Rails 3, please use version 1 of Paranoia:
12
+
13
+ ```ruby
14
+ gem 'paranoia', '~> 1.0'
15
+ ```
16
+
17
+ For Rails 4, please use version 2 of Paranoia:
18
+
19
+ ```ruby
20
+ gem 'paranoia', '~> 2.0'
21
+ ```
22
+
23
+ Of course you can install this from GitHub as well:
10
24
 
11
25
  ```ruby
12
- gem 'paranoia'
26
+ gem 'paranoia', :github => 'radar/paranoia', :branch => 'master'
27
+ # or
28
+ gem 'paranoia', :github => 'radar/paranoia', :branch => 'rails4'
13
29
  ```
14
30
 
15
31
  Then run:
@@ -1,17 +1,45 @@
1
1
  module Paranoia
2
2
  def self.included(klazz)
3
3
  klazz.extend Query
4
+ klazz.extend Callbacks
4
5
  end
5
6
 
6
7
  module Query
7
8
  def paranoid? ; true ; end
8
9
 
10
+ def with_deleted
11
+ all.tap { |x| x.default_scoped = false }
12
+ end
13
+
9
14
  def only_deleted
10
- all.tap { |x| x.default_scoped = false }.where.not(deleted_at: nil)
15
+ with_deleted.where.not(deleted_at: nil)
11
16
  end
17
+ alias :deleted :only_deleted
12
18
 
13
- def with_deleted
14
- all.tap { |x| x.default_scoped = false }
19
+ def restore(id)
20
+ if id.is_a?(Array)
21
+ id.map { |one_id| restore(one_id) }
22
+ else
23
+ only_deleted.find(id).restore!
24
+ end
25
+ end
26
+ end
27
+
28
+ module Callbacks
29
+ def self.extended(klazz)
30
+ klazz.define_callbacks :restore
31
+
32
+ klazz.define_singleton_method("before_restore") do |*args, &block|
33
+ set_callback(:restore, :before, *args, &block)
34
+ end
35
+
36
+ klazz.define_singleton_method("around_restore") do |*args, &block|
37
+ set_callback(:restore, :around, *args, &block)
38
+ end
39
+
40
+ klazz.define_singleton_method("after_restore") do |*args, &block|
41
+ set_callback(:restore, :after, *args, &block)
42
+ end
15
43
  end
16
44
  end
17
45
 
@@ -20,16 +48,16 @@ module Paranoia
20
48
  end
21
49
 
22
50
  def delete
23
- return if new_record? or destroyed?
24
- update_column :deleted_at, Time.now
51
+ return if new_record?
52
+ destroyed? ? destroy! : update_column(:deleted_at, Time.now)
25
53
  end
26
54
 
27
55
  def restore!
28
- update_column :deleted_at, nil
56
+ run_callbacks(:restore) { update_column :deleted_at, nil }
29
57
  end
30
58
 
31
59
  def destroyed?
32
- !self.deleted_at.nil?
60
+ !!deleted_at
33
61
  end
34
62
  alias :deleted? :destroyed?
35
63
  end
@@ -39,7 +67,7 @@ class ActiveRecord::Base
39
67
  alias :destroy! :destroy
40
68
  alias :delete! :delete
41
69
  include Paranoia
42
- default_scope { where(:deleted_at => nil) }
70
+ default_scope { where(self.quoted_table_name + '.deleted_at IS NULL') }
43
71
  end
44
72
 
45
73
  def self.paranoid? ; false ; end
@@ -1,3 +1,3 @@
1
1
  module Paranoia
2
- VERSION = "2.0.0"
2
+ VERSION = "2.0.1"
3
3
  end
@@ -71,7 +71,7 @@ class ParanoiaTest < Test::Unit::TestCase
71
71
  assert_equal 0, model.class.count
72
72
  assert_equal 1, model.class.unscoped.count
73
73
  end
74
-
74
+
75
75
  def test_scoping_behavior_for_paranoid_models
76
76
  ParanoidModel.unscoped.delete_all
77
77
  parent1 = ParentModel.create
@@ -82,6 +82,7 @@ class ParanoiaTest < Test::Unit::TestCase
82
82
  p2.destroy
83
83
  assert_equal 0, parent1.paranoid_models.count
84
84
  assert_equal 1, parent1.paranoid_models.only_deleted.count
85
+ assert_equal 1, parent1.paranoid_models.deleted.count
85
86
  p3 = ParanoidModel.create(:parent_model => parent1)
86
87
  assert_equal 2, parent1.paranoid_models.with_deleted.count
87
88
  assert_equal [p1,p3], parent1.paranoid_models.with_deleted
@@ -188,6 +189,30 @@ class ParanoiaTest < Test::Unit::TestCase
188
189
  assert_equal false, model.destroyed?
189
190
  end
190
191
 
192
+ def test_destroy_twice
193
+ model = ParanoidModel.new
194
+ model.save
195
+ model.destroy
196
+ model.destroy
197
+
198
+ assert_equal 0, ParanoidModel.unscoped.where(id: model.id).count
199
+ end
200
+
201
+ def test_restore_behavior_for_callbacks
202
+ model = CallbackModel.new
203
+ model.save
204
+ id = model.id
205
+ model.destroy
206
+
207
+ assert model.destroyed?
208
+
209
+ model = CallbackModel.only_deleted.find(id)
210
+ model.restore!
211
+ model.reload
212
+
213
+ assert model.instance_variable_get(:@restore_callback_called)
214
+ end
215
+
191
216
  def test_real_destroy
192
217
  model = ParanoidModel.new
193
218
  model.save
@@ -204,6 +229,33 @@ class ParanoiaTest < Test::Unit::TestCase
204
229
  refute ParanoidModel.unscoped.exists?(model.id)
205
230
  end
206
231
 
232
+ def test_multiple_restore
233
+ a = ParanoidModel.new
234
+ a.save
235
+ a_id = a.id
236
+ a.destroy
237
+
238
+ b = ParanoidModel.new
239
+ b.save
240
+ b_id = b.id
241
+ b.destroy
242
+
243
+ c = ParanoidModel.new
244
+ c.save
245
+ c_id = c.id
246
+ c.destroy
247
+
248
+ ParanoidModel.restore([a_id, c_id])
249
+
250
+ a.reload
251
+ b.reload
252
+ c.reload
253
+
254
+ refute a.destroyed?
255
+ assert b.destroyed?
256
+ refute c.destroyed?
257
+ end
258
+
207
259
  private
208
260
  def get_featureful_model
209
261
  FeaturefulModel.new(:name => "not empty")
@@ -232,6 +284,7 @@ end
232
284
  class CallbackModel < ActiveRecord::Base
233
285
  acts_as_paranoid
234
286
  before_destroy {|model| model.instance_variable_set :@callback_called, true }
287
+ before_restore {|model| model.instance_variable_set :@restore_callback_called, true }
235
288
  end
236
289
 
237
290
  class ParentModel < ActiveRecord::Base
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: paranoia
3
3
  version: !ruby/object:Gem::Version
4
- version: 2.0.0
4
+ version: 2.0.1
5
5
  platform: ruby
6
6
  authors:
7
7
  - radarlistener@gmail.com
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2013-07-08 00:00:00.000000000 Z
11
+ date: 2013-10-24 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: activerecord
@@ -105,7 +105,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
105
105
  version: 1.3.6
106
106
  requirements: []
107
107
  rubyforge_project: paranoia
108
- rubygems_version: 2.0.0
108
+ rubygems_version: 2.1.0
109
109
  signing_key:
110
110
  specification_version: 4
111
111
  summary: Paranoia is a re-implementation of acts_as_paranoid for Rails 3, using much,