paranoia 1.0.3 → 1.1.0

Sign up to get free protection for your applications and to get access to all the features.
@@ -0,0 +1,34 @@
1
+ PATH
2
+ remote: .
3
+ specs:
4
+ paranoia (1.1.0)
5
+ activerecord (>= 3.0.0)
6
+
7
+ GEM
8
+ remote: http://rubygems.org/
9
+ specs:
10
+ activemodel (3.0.8)
11
+ activesupport (= 3.0.8)
12
+ builder (~> 2.1.2)
13
+ i18n (~> 0.5.0)
14
+ activerecord (3.0.8)
15
+ activemodel (= 3.0.8)
16
+ activesupport (= 3.0.8)
17
+ arel (~> 2.0.10)
18
+ tzinfo (~> 0.3.23)
19
+ activesupport (3.0.8)
20
+ arel (2.0.10)
21
+ builder (2.1.2)
22
+ i18n (0.5.0)
23
+ rake (0.8.7)
24
+ sqlite3 (1.3.3)
25
+ tzinfo (0.3.28)
26
+
27
+ PLATFORMS
28
+ ruby
29
+
30
+ DEPENDENCIES
31
+ bundler (>= 1.0.0)
32
+ paranoia!
33
+ rake (= 0.8.7)
34
+ sqlite3
data/README.md CHANGED
@@ -34,13 +34,25 @@ Then run:
34
34
 
35
35
  rake gems:install
36
36
 
37
+ #### Run your migrations for the desired models
38
+
39
+ class AddDeletedAtToClient < ActiveRecord::Migration
40
+ def self.up
41
+ add_column :clients, :deleted_at, :datetime
42
+ end
43
+
44
+ def self.down
45
+ remove_column :clients, :deleted_at
46
+ end
47
+ end
48
+
37
49
  ### Usage
38
50
 
39
51
  #### In your model:
40
52
 
41
53
  class Client < ActiveRecord::Base
42
54
  acts_as_paranoid
43
-
55
+
44
56
  ...
45
57
  end
46
58
 
@@ -60,6 +72,16 @@ If you want a method to be called on destroy, simply provide a _before\_destroy_
60
72
  ...
61
73
  end
62
74
 
75
+ You can replace the older acts_as_paranoid methods as follows:
76
+
77
+ find_with_deleted(:all) # => unscoped
78
+ find_with_deleted(:first) # => unscoped.first
79
+ find_with_deleted(id) # => unscoped.find(id)
80
+
81
+ find_only_deleted(:all) # => only_deleted
82
+ find_only_deleted(:first) # => only_deleted.first
83
+ find_only_deleted(id) # => only_deleted.find(id)
84
+
63
85
  ## License
64
86
 
65
87
  This gem is released under the MIT license.
data/Rakefile CHANGED
@@ -3,6 +3,6 @@ Bundler::GemHelper.install_tasks
3
3
 
4
4
  task :test do
5
5
  Dir['test/*_test.rb'].each do |testfile|
6
- load testfile
6
+ load testfile
7
7
  end
8
8
  end
@@ -5,24 +5,38 @@ module Paranoia
5
5
 
6
6
  module Query
7
7
  def paranoid? ; true ; end
8
+
9
+ def only_deleted
10
+ unscoped {
11
+ where("deleted_at is not null")
12
+ }
13
+ end
8
14
  end
9
15
 
10
16
  def destroy
11
- _run_destroy_callbacks
17
+ _run_destroy_callbacks { delete }
18
+ end
19
+
20
+ def delete
12
21
  self.update_attribute(:deleted_at, Time.now) if !deleted? && persisted?
13
22
  freeze
14
23
  end
15
- alias :delete :destroy
24
+
25
+ def restore!
26
+ update_attribute :deleted_at, nil
27
+ end
16
28
 
17
29
  def destroyed?
18
- !self[:deleted_at].nil?
30
+ !self.deleted_at.nil?
19
31
  end
20
32
  alias :deleted? :destroyed?
21
33
  end
22
34
 
23
35
  class ActiveRecord::Base
24
36
  def self.acts_as_paranoid
25
- self.send(:include, Paranoia)
37
+ alias_method :destroy!, :destroy
38
+ alias_method :delete!, :delete
39
+ include Paranoia
26
40
  default_scope :conditions => { :deleted_at => nil }
27
41
  end
28
42
 
@@ -1,3 +1,3 @@
1
1
  module Paranoia
2
- VERSION = "1.0.3"
2
+ VERSION = "1.1.0"
3
3
  end
@@ -5,7 +5,7 @@ Gem::Specification.new do |s|
5
5
  s.name = "paranoia"
6
6
  s.version = Paranoia::VERSION
7
7
  s.platform = Gem::Platform::RUBY
8
- s.authors = []
8
+ s.authors = ["radarlistener@gmail.com"]
9
9
  s.email = []
10
10
  s.homepage = "http://rubygems.org/gems/paranoia"
11
11
  s.summary = "acts_as_paranoid, without the clusterfuck"
@@ -11,6 +11,7 @@ ActiveRecord::Base.establish_connection :adapter => 'sqlite3', :database => DB_F
11
11
  ActiveRecord::Base.connection.execute 'CREATE TABLE paranoid_models (id INTEGER NOT NULL PRIMARY KEY, deleted_at DATETIME)'
12
12
  ActiveRecord::Base.connection.execute 'CREATE TABLE featureful_models (id INTEGER NOT NULL PRIMARY KEY, deleted_at DATETIME, name VARCHAR(32))'
13
13
  ActiveRecord::Base.connection.execute 'CREATE TABLE plain_models (id INTEGER NOT NULL PRIMARY KEY, deleted_at DATETIME)'
14
+ ActiveRecord::Base.connection.execute 'CREATE TABLE callback_models (id INTEGER NOT NULL PRIMARY KEY, deleted_at DATETIME)'
14
15
 
15
16
  class ParanoiaTest < Test::Unit::TestCase
16
17
  def test_plain_model_class_is_not_paranoid
@@ -29,26 +30,26 @@ class ParanoiaTest < Test::Unit::TestCase
29
30
  assert_equal true, ParanoidModel.new.paranoid?
30
31
  end
31
32
 
32
- def test_delete_behavior_for_plain_models
33
+ def test_destroy_behavior_for_plain_models
33
34
  model = PlainModel.new
34
35
  assert_equal 0, model.class.count
35
36
  model.save!
36
37
  assert_equal 1, model.class.count
37
- model.delete
38
+ model.destroy
38
39
 
39
40
  assert_equal true, model.deleted_at.nil?
40
41
  assert model.frozen?
41
-
42
+
42
43
  assert_equal 0, model.class.count
43
44
  assert_equal 0, model.class.unscoped.count
44
45
  end
45
46
 
46
- def test_delete_behavior_for_paranoid_models
47
+ def test_destroy_behavior_for_paranoid_models
47
48
  model = ParanoidModel.new
48
49
  assert_equal 0, model.class.count
49
50
  model.save!
50
51
  assert_equal 1, model.class.count
51
- model.delete
52
+ model.destroy
52
53
 
53
54
  assert_equal false, model.deleted_at.nil?
54
55
  assert model.frozen?
@@ -58,19 +59,74 @@ class ParanoiaTest < Test::Unit::TestCase
58
59
 
59
60
  end
60
61
 
61
- def test_delete_behavior_for_featureful_paranoid_models
62
+ def test_destroy_behavior_for_featureful_paranoid_models
62
63
  model = get_featureful_model
63
64
  assert_equal 0, model.class.count
64
65
  model.save!
65
66
  assert_equal 1, model.class.count
66
- model.delete
67
+ model.destroy
67
68
 
68
69
  assert_equal false, model.deleted_at.nil?
69
-
70
+
70
71
  assert_equal 0, model.class.count
71
72
  assert_equal 1, model.class.unscoped.count
72
73
  end
73
74
 
75
+ def test_only_destroyed_scope_for_paranoid_models
76
+ model = ParanoidModel.new
77
+ model.save
78
+ model.destroy
79
+ model2 = ParanoidModel.new
80
+ model2.save
81
+
82
+ assert_equal model, ParanoidModel.only_deleted.last
83
+ assert_equal false, ParanoidModel.only_deleted.include?(model2)
84
+ end
85
+
86
+ def test_delete_behavior_for_callbacks
87
+ model = CallbackModel.new
88
+ model.save
89
+ model.delete
90
+ assert_equal nil, model.instance_variable_get(:@callback_called)
91
+ end
92
+
93
+ def test_destroy_behavior_for_callbacks
94
+ model = CallbackModel.new
95
+ model.save
96
+ model.destroy
97
+ assert model.instance_variable_get(:@callback_called)
98
+ end
99
+
100
+ def test_restore
101
+ model = ParanoidModel.new
102
+ model.save
103
+ id = model.id
104
+ model.destroy
105
+
106
+ assert model.destroyed?
107
+
108
+ model = ParanoidModel.only_deleted.find(id)
109
+ model.restore!
110
+
111
+ assert_equal false, model.destroyed?
112
+ end
113
+
114
+ def test_real_destroy
115
+ model = ParanoidModel.new
116
+ model.save
117
+ model.destroy!
118
+
119
+ assert_equal false, ParanoidModel.unscoped.exists?(model.id)
120
+ end
121
+
122
+ def test_real_delete
123
+ model = ParanoidModel.new
124
+ model.save
125
+ model.delete!
126
+
127
+ assert_equal false, ParanoidModel.unscoped.exists?(model.id)
128
+ end
129
+
74
130
  private
75
131
  def get_featureful_model
76
132
  FeaturefulModel.new(:name => "not empty")
@@ -90,3 +146,8 @@ end
90
146
 
91
147
  class PlainModel < ActiveRecord::Base
92
148
  end
149
+
150
+ class CallbackModel < ActiveRecord::Base
151
+ acts_as_paranoid
152
+ before_destroy {|model| model.instance_variable_set :@callback_called, true }
153
+ end
metadata CHANGED
@@ -2,15 +2,15 @@
2
2
  name: paranoia
3
3
  version: !ruby/object:Gem::Version
4
4
  prerelease:
5
- version: 1.0.3
5
+ version: 1.1.0
6
6
  platform: ruby
7
- authors: []
8
-
7
+ authors:
8
+ - radarlistener@gmail.com
9
9
  autorequire:
10
10
  bindir: bin
11
11
  cert_chain: []
12
12
 
13
- date: 2011-05-26 00:00:00 +10:00
13
+ date: 2011-07-11 00:00:00 +10:00
14
14
  default_executable:
15
15
  dependencies:
16
16
  - !ruby/object:Gem::Dependency
@@ -69,6 +69,7 @@ extra_rdoc_files: []
69
69
  files:
70
70
  - .gitignore
71
71
  - Gemfile
72
+ - Gemfile.lock
72
73
  - README.md
73
74
  - Rakefile
74
75
  - lib/paranoia.rb