soft_deletion 0.1.8 → 0.1.9

Sign up to get free protection for your applications and to get access to all the features.
data/Gemfile CHANGED
@@ -8,4 +8,4 @@ gem 'rake'
8
8
  gem 'shoulda'
9
9
  gem 'mocha'
10
10
  gem 'sqlite3'
11
- gem 'mynyml-redgreen'
11
+ gem 'test-unit', '2.2' # >2.2 does not show dots
data/Gemfile.lock CHANGED
@@ -1,7 +1,7 @@
1
1
  PATH
2
2
  remote: .
3
3
  specs:
4
- soft_deletion (0.1.8)
4
+ soft_deletion (0.1.9)
5
5
 
6
6
  GEM
7
7
  remote: http://rubygems.org/
@@ -25,12 +25,10 @@ GEM
25
25
  i18n (0.6.0)
26
26
  mocha (0.9.12)
27
27
  multi_json (1.3.4)
28
- mynyml-redgreen (0.7.1)
29
- term-ansicolor (>= 1.0.4)
30
28
  rake (0.9.2.2)
31
29
  shoulda (2.10.3)
32
30
  sqlite3 (1.3.6)
33
- term-ansicolor (1.0.7)
31
+ test-unit (2.2.0)
34
32
  tzinfo (0.3.33)
35
33
 
36
34
  PLATFORMS
@@ -41,8 +39,8 @@ DEPENDENCIES
41
39
  activesupport
42
40
  appraisal
43
41
  mocha
44
- mynyml-redgreen
45
42
  rake
46
43
  shoulda
47
44
  soft_deletion!
48
45
  sqlite3
46
+ test-unit (= 2.2)
@@ -7,7 +7,7 @@ gem "rake"
7
7
  gem "shoulda"
8
8
  gem "mocha"
9
9
  gem "sqlite3"
10
- gem "mynyml-redgreen"
10
+ gem "test-unit", "2.2"
11
11
  gem "activerecord", "2.3.14"
12
12
  gem "activesupport", "2.3.14"
13
13
 
@@ -1,7 +1,7 @@
1
1
  PATH
2
2
  remote: /Users/mgrosser/code/tools/soft_deletion
3
3
  specs:
4
- soft_deletion (0.1.7)
4
+ soft_deletion (0.1.8)
5
5
 
6
6
  GEM
7
7
  remote: http://rubygems.org/
@@ -13,12 +13,10 @@ GEM
13
13
  bundler
14
14
  rake
15
15
  mocha (0.9.12)
16
- mynyml-redgreen (0.7.1)
17
- term-ansicolor (>= 1.0.4)
18
16
  rake (0.9.2.2)
19
17
  shoulda (2.10.3)
20
18
  sqlite3 (1.3.6)
21
- term-ansicolor (1.0.7)
19
+ test-unit (2.2.0)
22
20
 
23
21
  PLATFORMS
24
22
  ruby
@@ -28,8 +26,8 @@ DEPENDENCIES
28
26
  activesupport (= 2.3.14)
29
27
  appraisal
30
28
  mocha
31
- mynyml-redgreen
32
29
  rake
33
30
  shoulda
34
31
  soft_deletion!
35
32
  sqlite3
33
+ test-unit (= 2.2)
@@ -7,7 +7,7 @@ gem "rake"
7
7
  gem "shoulda"
8
8
  gem "mocha"
9
9
  gem "sqlite3"
10
- gem "mynyml-redgreen"
10
+ gem "test-unit", "2.2"
11
11
  gem "activerecord", "3.2.3"
12
12
  gem "activesupport", "3.2.3"
13
13
 
@@ -1,7 +1,7 @@
1
1
  PATH
2
2
  remote: /Users/mgrosser/code/tools/soft_deletion
3
3
  specs:
4
- soft_deletion (0.1.7)
4
+ soft_deletion (0.1.8)
5
5
 
6
6
  GEM
7
7
  remote: http://rubygems.org/
@@ -25,12 +25,10 @@ GEM
25
25
  i18n (0.6.0)
26
26
  mocha (0.9.12)
27
27
  multi_json (1.3.4)
28
- mynyml-redgreen (0.7.1)
29
- term-ansicolor (>= 1.0.4)
30
28
  rake (0.9.2.2)
31
29
  shoulda (2.10.3)
32
30
  sqlite3 (1.3.6)
33
- term-ansicolor (1.0.7)
31
+ test-unit (2.2.0)
34
32
  tzinfo (0.3.33)
35
33
 
36
34
  PLATFORMS
@@ -41,8 +39,8 @@ DEPENDENCIES
41
39
  activesupport (= 3.2.3)
42
40
  appraisal
43
41
  mocha
44
- mynyml-redgreen
45
42
  rake
46
43
  shoulda
47
44
  soft_deletion!
48
45
  sqlite3
46
+ test-unit (= 2.2)
data/lib/soft_deletion.rb CHANGED
@@ -11,12 +11,10 @@ module SoftDeletion
11
11
 
12
12
  if base.respond_to?(:define_default_soft_delete_scope)
13
13
  base.define_default_soft_delete_scope
14
- else
14
+ elsif base.table_exists? && base.column_names.include?('deleted_at')
15
15
  # Avoids a bad SQL request with versions of code without the column deleted_at (for example a migration prior to the migration
16
16
  # that adds deleted_at)
17
- if base.column_names.include?('deleted_at')
18
- base.send(:default_scope, :conditions => { :deleted_at => nil })
19
- end
17
+ base.send(:default_scope, :conditions => { :deleted_at => nil })
20
18
  end
21
19
 
22
20
  # backport after_soft_delete so we can safely upgrade to rails 3
@@ -1,3 +1,3 @@
1
1
  module SoftDeletion
2
- VERSION = '0.1.8'
2
+ VERSION = '0.1.9'
3
3
  end
@@ -1,105 +1,4 @@
1
- require 'soft_deletion'
2
-
3
- require 'active_support/test_case'
4
- require 'shoulda'
5
- require 'redgreen'
6
-
7
- # connect
8
- ActiveRecord::Base.establish_connection(
9
- :adapter => "sqlite3",
10
- :database => ":memory:"
11
- )
12
-
13
- # create tables
14
- ActiveRecord::Schema.define(:version => 1) do
15
- create_table :forums do |t|
16
- t.integer :category_id
17
- t.timestamp :deleted_at
18
- end
19
-
20
- create_table :categories do |t|
21
- t.timestamp :deleted_at
22
- end
23
-
24
- create_table :original_categories do |t|
25
- end
26
- end
27
-
28
- class ActiveRecord::Base
29
- def self.silent_set_table_name(name)
30
- if ActiveRecord::VERSION::MAJOR > 2
31
- self.table_name = name
32
- else
33
- set_table_name name
34
- end
35
- end
36
- end
37
-
38
- # setup models
39
- class Forum < ActiveRecord::Base
40
- include SoftDeletion
41
- belongs_to :category
42
- end
43
-
44
- class Category < ActiveRecord::Base
45
- include SoftDeletion
46
- has_many :forums, :dependent => :destroy
47
- end
48
-
49
- # No association
50
- class NACategory < ActiveRecord::Base
51
- silent_set_table_name 'categories'
52
- include SoftDeletion
53
- end
54
-
55
- # Independent association
56
- class IDACategory < ActiveRecord::Base
57
- silent_set_table_name 'categories'
58
- include SoftDeletion
59
- has_many :forums, :dependent => :destroy, :foreign_key => :category_id
60
- end
61
-
62
- # Nullified dependent association
63
- class NDACategory < ActiveRecord::Base
64
- silent_set_table_name 'categories'
65
- include SoftDeletion
66
- has_many :forums, :dependent => :destroy, :foreign_key => :category_id
67
- end
68
-
69
- # Has ome association
70
- class HOACategory < ActiveRecord::Base
71
- silent_set_table_name 'categories'
72
- include SoftDeletion
73
- has_one :forum, :dependent => :destroy, :foreign_key => :category_id
74
- end
75
-
76
- # Class without column deleted_at
77
- class OriginalCategory < ActiveRecord::Base
78
- include SoftDeletion
79
- end
80
-
81
- # Has many destroyable association
82
- class DACategory < ActiveRecord::Base
83
- silent_set_table_name 'categories'
84
- include SoftDeletion
85
- has_many :destroyable_forums, :dependent => :destroy, :foreign_key => :category_id
86
- end
87
-
88
- # Forum that isn't soft deletable for association checing
89
- class DestroyableForum < ActiveRecord::Base
90
- silent_set_table_name 'forums'
91
- end
92
-
93
- def clear_callbacks(model, callback)
94
- if ActiveRecord::VERSION::MAJOR > 2
95
- model.reset_callbacks callback
96
- else
97
- model.class_eval do
98
- instance_variable_set "@before_#{callback}_callbacks", nil
99
- instance_variable_set "@after_#{callback}_callbacks", nil
100
- end
101
- end
102
- end
1
+ require File.expand_path '../test_helper', __FILE__
103
2
 
104
3
  class SoftDeletionTest < ActiveSupport::TestCase
105
4
  def assert_deleted(resource)
@@ -329,4 +228,19 @@ class SoftDeletionTest < ActiveSupport::TestCase
329
228
  end
330
229
  end
331
230
  end
231
+
232
+ context "overwritten default scope" do
233
+ should "find even with deleted_at" do
234
+ forum = Cat1Forum.create(:deleted_at => Time.now)
235
+ assert Cat1Forum.find_by_id(forum.id)
236
+ end
237
+
238
+ should "not find by new scope" do
239
+ # create! does not work here on rails 2
240
+ forum = Cat1Forum.new
241
+ forum.category_id = 2
242
+ forum.save!
243
+ assert_nil Cat1Forum.find_by_id(forum.id)
244
+ end
245
+ end
332
246
  end
@@ -0,0 +1,122 @@
1
+ require 'active_support/test_case'
2
+ require 'shoulda'
3
+ require 'active_record'
4
+
5
+ ENV['EMACS'] = 't' # colors for test-unit < 2.4.9
6
+
7
+ def clear_callbacks(model, callback)
8
+ if ActiveRecord::VERSION::MAJOR > 2
9
+ model.reset_callbacks callback
10
+ else
11
+ model.class_eval do
12
+ instance_variable_set "@before_#{callback}_callbacks", nil
13
+ instance_variable_set "@after_#{callback}_callbacks", nil
14
+ end
15
+ end
16
+ end
17
+
18
+ # connect
19
+ ActiveRecord::Base.establish_connection(
20
+ :adapter => "sqlite3",
21
+ :database => ":memory:"
22
+ )
23
+
24
+ # create tables
25
+ ActiveRecord::Schema.define(:version => 1) do
26
+ create_table :forums do |t|
27
+ t.integer :category_id
28
+ t.timestamp :deleted_at
29
+ end
30
+
31
+ create_table :categories do |t|
32
+ t.timestamp :deleted_at
33
+ end
34
+
35
+ create_table :original_categories do |t|
36
+ end
37
+ end
38
+
39
+ class ActiveRecord::Base
40
+ def self.silent_set_table_name(name)
41
+ if ActiveRecord::VERSION::MAJOR > 2
42
+ self.table_name = name
43
+ else
44
+ set_table_name name
45
+ end
46
+ end
47
+ end
48
+
49
+ # setup models
50
+ require 'soft_deletion'
51
+
52
+ class Forum < ActiveRecord::Base
53
+ include SoftDeletion
54
+ belongs_to :category
55
+ end
56
+
57
+ class Category < ActiveRecord::Base
58
+ include SoftDeletion
59
+ has_many :forums, :dependent => :destroy
60
+ end
61
+
62
+ # No association
63
+ class NACategory < ActiveRecord::Base
64
+ silent_set_table_name 'categories'
65
+ include SoftDeletion
66
+ end
67
+
68
+ # Independent association
69
+ class IDACategory < ActiveRecord::Base
70
+ silent_set_table_name 'categories'
71
+ include SoftDeletion
72
+ has_many :forums, :dependent => :destroy, :foreign_key => :category_id
73
+ end
74
+
75
+ # Nullified dependent association
76
+ class NDACategory < ActiveRecord::Base
77
+ silent_set_table_name 'categories'
78
+ include SoftDeletion
79
+ has_many :forums, :dependent => :destroy, :foreign_key => :category_id
80
+ end
81
+
82
+ # Has ome association
83
+ class HOACategory < ActiveRecord::Base
84
+ silent_set_table_name 'categories'
85
+ include SoftDeletion
86
+ has_one :forum, :dependent => :destroy, :foreign_key => :category_id
87
+ end
88
+
89
+ # Class without column deleted_at
90
+ class OriginalCategory < ActiveRecord::Base
91
+ include SoftDeletion
92
+ end
93
+
94
+ # Has many destroyable association
95
+ class DACategory < ActiveRecord::Base
96
+ silent_set_table_name 'categories'
97
+ include SoftDeletion
98
+ has_many :destroyable_forums, :dependent => :destroy, :foreign_key => :category_id
99
+ end
100
+
101
+ # Forum that isn't soft deletable for association checking
102
+ class DestroyableForum < ActiveRecord::Base
103
+ silent_set_table_name 'forums'
104
+ end
105
+
106
+ # test that it does not blow up when the table is not yet defined (e.g. in rake db:reset)
107
+ class NoTable < ActiveRecord::Base
108
+ include SoftDeletion
109
+ end
110
+
111
+ # Forum with other default scope
112
+ class Cat1Forum < ActiveRecord::Base
113
+ silent_set_table_name 'forums'
114
+
115
+ def self.define_default_soft_delete_scope
116
+ default_scope :conditions => {:category_id => 1}
117
+ end
118
+
119
+ include SoftDeletion
120
+ belongs_to :category
121
+ end
122
+
metadata CHANGED
@@ -1,13 +1,13 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: soft_deletion
3
3
  version: !ruby/object:Gem::Version
4
- hash: 11
4
+ hash: 9
5
5
  prerelease:
6
6
  segments:
7
7
  - 0
8
8
  - 1
9
- - 8
10
- version: 0.1.8
9
+ - 9
10
+ version: 0.1.9
11
11
  platform: ruby
12
12
  authors:
13
13
  - Zendesk
@@ -15,7 +15,7 @@ autorequire:
15
15
  bindir: bin
16
16
  cert_chain: []
17
17
 
18
- date: 2012-06-28 00:00:00 Z
18
+ date: 2012-06-29 00:00:00 Z
19
19
  dependencies: []
20
20
 
21
21
  description:
@@ -43,6 +43,7 @@ files:
43
43
  - lib/soft_deletion/version.rb
44
44
  - soft_deletion.gemspec
45
45
  - test/soft_deletion_test.rb
46
+ - test/test_helper.rb
46
47
  homepage: http://github.com/grosser/soft_deletion
47
48
  licenses:
48
49
  - MIT