paranoia 1.3.1 → 1.3.2

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: 07438f51e6a97736c8bbb1778bbb7b9cbe5c623c
4
- data.tar.gz: 4456e34fe1c2988dfe9cb1014ca8002e288ec45c
3
+ metadata.gz: 74da60de8afe27c56c43f9a2ad74eec282f1d0d7
4
+ data.tar.gz: fc3b097f03211f08ba1ad711e82d7c6ef20231ab
5
5
  SHA512:
6
- metadata.gz: 5d13573dc45ac55f6aaf478b20a3e74e2d209d37f6c17e826f293aec36ffc568b7952078eb42f9d63753ef42162993b127d11773e2a7afb9fe3558955faf15d1
7
- data.tar.gz: 5b84920192ef0cd5e13acf1cc433753b3f6f1cc69ce6afa7369404e6ac2bd7394b96b33b22415d5e2de41928c5b0582f9498cbb592d1eb291a651ac6f0e6bb97
6
+ metadata.gz: 1b98a9b12106d88e02d0d059143b56d7fe63dfa12d0362a83da656aef59b5940e5e9e9514caa2c6f51881f2ad306e76977d75efc22a8dcf60a2df04bebe931ce
7
+ data.tar.gz: 29a1b849c690b46339e75e5bdd2761bd6f070025c55f543abc7603a1104ebb0feb59b4cc04d70d5ed363f2f38a04de50fc630c5a26d8e31ae1b61287f73d56e4
data/.travis.yml ADDED
@@ -0,0 +1,4 @@
1
+ language: ruby
2
+ rvm:
3
+ - 1.9.3
4
+ - 2.0.0
data/README.md CHANGED
@@ -4,6 +4,8 @@ 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
11
  For Rails 3, please use version 1 of Paranoia:
@@ -23,7 +25,7 @@ Of course you can install this from GitHub as well:
23
25
  ```ruby
24
26
  gem 'paranoia', :github => 'radar/paranoia', :branch => 'master'
25
27
  # or
26
- gem 'paranoia', :github => 'radar/paranoia', :branch => 'rails-4'
28
+ gem 'paranoia', :github => 'radar/paranoia', :branch => 'rails4'
27
29
  ```
28
30
 
29
31
  Then run:
data/lib/paranoia.rb CHANGED
@@ -1,18 +1,49 @@
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
- def paranoid? ; true ; end
8
-
9
- def only_deleted
10
- scoped.tap { |x| x.default_scoped = false }.where("#{self.table_name}.deleted_at is not null")
8
+ def paranoid?
9
+ true
11
10
  end
12
11
 
12
+
13
13
  def with_deleted
14
14
  scoped.tap { |x| x.default_scoped = false }
15
15
  end
16
+
17
+ def only_deleted
18
+ with_deleted.where("#{self.table_name}.deleted_at IS NOT NULL")
19
+ end
20
+ alias :deleted :only_deleted
21
+
22
+ def restore(id)
23
+ if id.is_a?(Array)
24
+ id.map { |one_id| restore(one_id) }
25
+ else
26
+ only_deleted.find(id).restore!
27
+ end
28
+ end
29
+ end
30
+
31
+ module Callbacks
32
+ def self.extended(klazz)
33
+ klazz.define_callbacks :restore
34
+
35
+ klazz.define_singleton_method("before_restore") do |*args, &block|
36
+ set_callback(:restore, :before, *args, &block)
37
+ end
38
+
39
+ klazz.define_singleton_method("around_restore") do |*args, &block|
40
+ set_callback(:restore, :around, *args, &block)
41
+ end
42
+
43
+ klazz.define_singleton_method("after_restore") do |*args, &block|
44
+ set_callback(:restore, :after, *args, &block)
45
+ end
46
+ end
16
47
  end
17
48
 
18
49
  def destroy
@@ -20,37 +51,46 @@ module Paranoia
20
51
  end
21
52
 
22
53
  def delete
23
- return if new_record? or destroyed?
24
- update_attribute_or_column :deleted_at, Time.now
54
+ return if new_record?
55
+ destroyed? ? destroy! : update_attribute_or_column(:deleted_at, Time.now)
25
56
  end
26
57
 
27
58
  def restore!
28
- update_attribute_or_column :deleted_at, nil
59
+ run_callbacks(:restore) { update_column :deleted_at, nil }
29
60
  end
61
+ alias :restore :restore!
30
62
 
31
63
  def destroyed?
32
- !self.deleted_at.nil?
64
+ !!deleted_at
33
65
  end
66
+
34
67
  alias :deleted? :destroyed?
35
68
 
36
69
  private
37
70
 
38
71
  # Rails 3.1 adds update_column. Rails > 3.2.6 deprecates update_attribute, gone in Rails 4.
39
72
  def update_attribute_or_column(*args)
40
- respond_to?(:update_column) ? update_column(*args) : update_attribute(*args)
73
+ self.class.unscoped do
74
+ respond_to?(:update_column) ? update_column(*args) : update_attribute(*args)
75
+ end
41
76
  end
42
77
  end
43
78
 
44
79
  class ActiveRecord::Base
45
80
  def self.acts_as_paranoid
46
81
  alias :destroy! :destroy
47
- alias :delete! :delete
82
+ alias :delete! :delete
48
83
  include Paranoia
49
- default_scope { where(:deleted_at => nil) }
84
+ default_scope { where(self.quoted_table_name + '.deleted_at IS NULL') }
85
+ end
86
+
87
+ def self.paranoid?
88
+ false
50
89
  end
51
90
 
52
- def self.paranoid? ; false ; end
53
- def paranoid? ; self.class.paranoid? ; end
91
+ def paranoid?
92
+ self.class.paranoid?
93
+ end
54
94
 
55
95
  # Override the persisted method to allow for the paranoia gem.
56
96
  # If a paranoid record is selected, then we only want to check
@@ -1,3 +1,3 @@
1
1
  module Paranoia
2
- VERSION = "1.3.1"
2
+ VERSION = '1.3.2'
3
3
  end
data/paranoia.gemspec CHANGED
@@ -1,27 +1,26 @@
1
1
  # -*- encoding: utf-8 -*-
2
- require File.expand_path("../lib/paranoia/version", __FILE__)
2
+ require File.expand_path('../lib/paranoia/version', __FILE__)
3
3
 
4
4
  Gem::Specification.new do |s|
5
- s.name = "paranoia"
6
- s.version = Paranoia::VERSION
7
- s.platform = Gem::Platform::RUBY
8
- s.authors = ["radarlistener@gmail.com"]
9
- s.email = []
10
- s.homepage = "http://rubygems.org/gems/paranoia"
11
- s.summary = "Paranoia is a re-implementation of acts_as_paranoid for Rails 3, using much, much, much less code."
5
+ s.name = 'paranoia'
6
+ s.version = Paranoia::VERSION
7
+ s.platform = Gem::Platform::RUBY
8
+ s.authors = %w(radarlistener@gmail.com)
9
+ s.email = []
10
+ s.homepage = 'http://rubygems.org/gems/paranoia'
11
+ s.summary = 'Paranoia is a re-implementation of acts_as_paranoid for Rails 3, using much, much, much less code.'
12
12
  s.description = "Paranoia is a re-implementation of acts_as_paranoid for Rails 3, using much, much, much less code. 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."
13
13
 
14
- s.required_rubygems_version = ">= 1.3.6"
15
- s.rubyforge_project = "paranoia"
16
-
17
- s.add_dependency "activerecord", "~> 3.2"
14
+ s.required_rubygems_version = '>= 1.3.6'
15
+ s.rubyforge_project = 'paranoia'
18
16
 
19
- s.add_development_dependency "bundler", ">= 1.0.0"
20
- s.add_development_dependency "sqlite3"
21
- s.add_development_dependency "rake", "0.8.7"
22
- s.add_development_dependency "pry"
23
-
24
- s.files = `git ls-files`.split("\n")
25
- s.executables = `git ls-files`.split("\n").map{|f| f =~ /^bin\/(.*)/ ? $1 : nil}.compact
17
+ s.add_dependency 'activerecord', '~> 3.2'
18
+
19
+ s.add_development_dependency 'bundler', '>= 1.0.0'
20
+ s.add_development_dependency 'sqlite3'
21
+ s.add_development_dependency 'rake'
22
+
23
+ s.files = `git ls-files`.split("\n")
24
+ s.executables = `git ls-files`.split("\n").map { |f| f =~ /^bin\/(.*)/ ? $1 : nil }.compact
26
25
  s.require_path = 'lib'
27
26
  end
@@ -1,6 +1,6 @@
1
1
  require 'test/unit'
2
2
  require 'active_record'
3
- require File.expand_path(File.dirname(__FILE__) + "/../lib/paranoia")
3
+ require File.expand_path(File.dirname(__FILE__) + '/../lib/paranoia')
4
4
 
5
5
  DB_FILE = 'tmp/test_db'
6
6
 
@@ -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,9 +82,10 @@ 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
- assert_equal [p1,p3], parent1.paranoid_models.with_deleted
88
+ assert_equal [p1, p3], parent1.paranoid_models.with_deleted
88
89
  end
89
90
 
90
91
  def test_destroy_behavior_for_featureful_paranoid_models
@@ -102,9 +103,8 @@ class ParanoiaTest < Test::Unit::TestCase
102
103
 
103
104
  # Regression test for #24
104
105
  def test_chaining_for_paranoid_models
105
- scope = FeaturefulModel.where(:name => "foo").only_deleted
106
-
107
- assert_equal "foo", scope.where_values_hash["name"]
106
+ scope = FeaturefulModel.where(:name => 'foo').only_deleted
107
+ assert_equal 'foo', scope.where_values_hash['name']
108
108
  assert_equal 2, scope.where_values.count
109
109
  end
110
110
 
@@ -117,6 +117,7 @@ class ParanoiaTest < Test::Unit::TestCase
117
117
 
118
118
  assert_equal model, ParanoidModel.only_deleted.last
119
119
  assert_equal false, ParanoidModel.only_deleted.include?(model2)
120
+ assert_equal false, ParanoidModel.deleted.include?(model2)
120
121
  end
121
122
 
122
123
  def test_default_scope_for_has_many_relationships
@@ -189,25 +190,75 @@ class ParanoiaTest < Test::Unit::TestCase
189
190
  assert_equal false, model.destroyed?
190
191
  end
191
192
 
193
+ def test_destroy_twice
194
+ model = ParanoidModel.new
195
+ model.save
196
+ model.destroy
197
+ model.destroy
198
+
199
+ assert_equal 0, ParanoidModel.unscoped.where(id: model.id).count
200
+ end
201
+
202
+ def test_restore_behavior_for_callbacks
203
+ model = CallbackModel.new
204
+ model.save
205
+ id = model.id
206
+ model.destroy
207
+
208
+ assert model.destroyed?
209
+
210
+ model = CallbackModel.only_deleted.find(id)
211
+ model.restore!
212
+ model.reload
213
+
214
+ assert model.instance_variable_get(:@restore_callback_called)
215
+ end
216
+
192
217
  def test_real_destroy
193
218
  model = ParanoidModel.new
194
219
  model.save
195
220
  model.destroy!
196
221
 
197
- assert_equal false, !!ParanoidModel.unscoped.exists?(model.id)
222
+ assert_equal 0, ParanoidModel.unscoped.where(id: model.id).count
198
223
  end
199
224
 
200
225
  def test_real_delete
201
226
  model = ParanoidModel.new
202
227
  model.save
203
228
  model.delete!
229
+ assert_equal 0, ParanoidModel.unscoped.where(id: model.id).count
230
+ end
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
204
253
 
205
- assert_equal false, !!ParanoidModel.unscoped.exists?(model.id)
254
+ refute a.destroyed?
255
+ assert b.destroyed?
256
+ refute c.destroyed?
206
257
  end
207
258
 
208
259
  private
209
260
  def get_featureful_model
210
- FeaturefulModel.new(:name => "not empty")
261
+ FeaturefulModel.new(:name => 'not empty')
211
262
  end
212
263
  end
213
264
 
@@ -233,6 +284,7 @@ end
233
284
  class CallbackModel < ActiveRecord::Base
234
285
  acts_as_paranoid
235
286
  before_destroy {|model| model.instance_variable_set :@callback_called, true }
287
+ before_restore {|model| model.instance_variable_set :@restore_callback_called, true }
236
288
  end
237
289
 
238
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: 1.3.1
4
+ version: 1.3.2
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
@@ -54,20 +54,6 @@ dependencies:
54
54
  version: '0'
55
55
  - !ruby/object:Gem::Dependency
56
56
  name: rake
57
- requirement: !ruby/object:Gem::Requirement
58
- requirements:
59
- - - '='
60
- - !ruby/object:Gem::Version
61
- version: 0.8.7
62
- type: :development
63
- prerelease: false
64
- version_requirements: !ruby/object:Gem::Requirement
65
- requirements:
66
- - - '='
67
- - !ruby/object:Gem::Version
68
- version: 0.8.7
69
- - !ruby/object:Gem::Dependency
70
- name: pry
71
57
  requirement: !ruby/object:Gem::Requirement
72
58
  requirements:
73
59
  - - '>='
@@ -92,6 +78,7 @@ extensions: []
92
78
  extra_rdoc_files: []
93
79
  files:
94
80
  - .gitignore
81
+ - .travis.yml
95
82
  - Gemfile
96
83
  - LICENSE
97
84
  - README.md
@@ -119,7 +106,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
119
106
  version: 1.3.6
120
107
  requirements: []
121
108
  rubyforge_project: paranoia
122
- rubygems_version: 2.0.0
109
+ rubygems_version: 2.1.0
123
110
  signing_key:
124
111
  specification_version: 4
125
112
  summary: Paranoia is a re-implementation of acts_as_paranoid for Rails 3, using much,