soft_destroyable 0.1.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.
@@ -0,0 +1,395 @@
1
+ require 'test/unit'
2
+
3
+ require 'rubygems'
4
+ gem 'activerecord', '~> 3.0.0'
5
+ require 'active_record'
6
+
7
+ require "#{File.dirname(__FILE__)}/../init"
8
+
9
+ ActiveRecord::Base.establish_connection(:adapter => "sqlite3", :database => ":memory:")
10
+
11
+ def setup_db
12
+ ActiveRecord::Schema.define(:version => 1) do
13
+ create_table :parents do |t|
14
+ t.string :name
15
+ t.soft_destroyable
16
+ end
17
+
18
+ # used to test non_dependent associations
19
+ create_table :non_dependent_children do |t|
20
+ t.string :name
21
+ t.references :parent
22
+ end
23
+
24
+ # used to test has_many through
25
+ create_table :soft_parent_sports do |t|
26
+ t.references :parent
27
+ t.references :soft_sport
28
+ t.soft_destroyable
29
+ end
30
+
31
+ # used to test has_many through
32
+ create_table :parent_sports do |t|
33
+ t.references :parent
34
+ t.references :sport
35
+ end
36
+
37
+ # used to test has_many through
38
+ create_table :soft_sports do |t|
39
+ t.string :name
40
+ end
41
+
42
+ # used to test has_many through
43
+ create_table :sports do |t|
44
+ t.string :name
45
+ end
46
+
47
+ # used to test has_one through
48
+ create_table :soft_parent_nicknames do |t|
49
+ t.references :parent
50
+ t.references :soft_nickname
51
+ t.soft_destroyable
52
+ end
53
+
54
+ # used to test has_one through
55
+ create_table :parent_nicknames do |t|
56
+ t.references :parent
57
+ t.references :nickname
58
+ end
59
+
60
+ # used to test has_one through
61
+ create_table :soft_nicknames do |t|
62
+ t.string :name
63
+ end
64
+
65
+ # used to test has_one through
66
+ create_table :nicknames do |t|
67
+ t.string :name
68
+ end
69
+
70
+ # used to test has_many <soft_destroyable_model>, :dependent => :destroy
71
+ create_table :soft_children do |t|
72
+ t.string :name
73
+ t.references :parent
74
+ t.soft_destroyable
75
+ end
76
+
77
+ # used to test has_many <model>, :dependent => :destroy
78
+ create_table :children do |t|
79
+ t.string :name
80
+ t.references :parent
81
+ end
82
+
83
+ # used to test has_one <soft_destroyable_model>, :dependent => :destroy
84
+ create_table :soft_ones do |t|
85
+ t.string :name
86
+ t.references :parent
87
+ t.soft_destroyable
88
+ end
89
+
90
+ # used to test has_one <model>, :dependent => :destroy
91
+ create_table :ones do |t|
92
+ t.string :name
93
+ t.references :parent
94
+ end
95
+
96
+ # used to test has_many <soft_destroyable_model>, :dependent => :nullify
97
+ create_table :soft_nullify_children do |t|
98
+ t.string :name
99
+ t.references :parent
100
+ t.soft_destroyable
101
+ end
102
+
103
+ # used to test has_many <model>, :dependent => :nullify
104
+ create_table :nullify_children do |t|
105
+ t.string :name
106
+ t.references :parent
107
+ end
108
+
109
+ # used to test has_one <soft_destroyable_model>, :dependent => :nullify
110
+ create_table :soft_nullify_ones do |t|
111
+ t.string :name
112
+ t.references :parent
113
+ t.soft_destroyable
114
+ end
115
+
116
+ # used to test has_one <model>, :dependent => :nullify
117
+ create_table :nullify_ones do |t|
118
+ t.string :name
119
+ t.references :parent
120
+ end
121
+
122
+ # used to test has_many <soft_destroyable_model>, :dependent => :restrict
123
+ create_table :soft_restrict_children do |t|
124
+ t.string :name
125
+ t.references :parent
126
+ t.soft_destroyable
127
+ end
128
+
129
+ # used to test has_many <model>, :dependent => :restrict
130
+ create_table :restrict_children do |t|
131
+ t.string :name
132
+ t.references :parent
133
+ end
134
+
135
+ # used to test has_one <soft_destroyable_model>, :dependent => :restrict
136
+ create_table :soft_restrict_ones do |t|
137
+ t.string :name
138
+ t.references :parent
139
+ t.soft_destroyable
140
+ end
141
+
142
+ # used to test has_one <model>, :dependent => :restrict
143
+ create_table :restrict_ones do |t|
144
+ t.string :name
145
+ t.references :parent
146
+ end
147
+
148
+ # used to test has_many <soft_destroyable_model>, :dependent => :delete_all
149
+ create_table :soft_delete_all_children do |t|
150
+ t.string :name
151
+ t.references :parent
152
+ t.soft_destroyable
153
+ end
154
+
155
+ # used to test has_many <model>, :dependent => :delete_all
156
+ create_table :delete_all_children do |t|
157
+ t.string :name
158
+ t.references :parent
159
+ end
160
+
161
+ # used to test has_one <soft_destroyable_model>, :dependent => :delete
162
+ create_table :soft_delete_ones do |t|
163
+ t.string :name
164
+ t.references :parent
165
+ t.soft_destroyable
166
+ end
167
+
168
+ # used to test has_one <model>, :dependent => :delete
169
+ create_table :delete_ones do |t|
170
+ t.string :name
171
+ t.references :parent
172
+ end
173
+
174
+ # used to test callbacks
175
+ create_table :callback_parents do |t|
176
+ t.string :name
177
+ t.soft_destroyable
178
+ end
179
+
180
+ # used to test before_soft_destroy and before_destroy!
181
+ create_table :soft_callback_children do |t|
182
+ t.string :name
183
+ t.references :callback_parent
184
+ t.soft_destroyable
185
+ end
186
+
187
+ # used to test callbacks
188
+ create_table :callback_children do |t|
189
+ t.string :name
190
+ t.references :callback_parent
191
+ end
192
+
193
+ # todo: HasAndBelongsToMany?
194
+ end
195
+ end
196
+
197
+ setup_db
198
+
199
+
200
+ class Parent < ActiveRecord::Base
201
+ has_many :non_dependent_children
202
+
203
+ # dependent destroy associations
204
+ has_many :soft_children, :dependent => :destroy
205
+ has_many :children, :dependent => :destroy
206
+ has_one :soft_one, :dependent => :destroy
207
+ has_one :one, :dependent => :destroy
208
+
209
+ # used to test has_many through associations
210
+ has_many :soft_parent_sports, :dependent => :destroy
211
+ has_many :soft_sports, :through => :soft_parent_sports
212
+ has_many :parent_sports, :dependent => :destroy
213
+ has_many :sports, :through => :parent_sports
214
+
215
+ # used to test has_one through associations
216
+ has_one :soft_parent_nickname, :dependent => :destroy
217
+ has_one :soft_nickname, :through => :soft_parent_nickname
218
+ has_one :parent_nickname, :dependent => :destroy
219
+ has_one :nickname, :through => :parent_nickname
220
+
221
+ # dependent nullify associations
222
+ has_many :soft_nullify_children, :dependent => :nullify
223
+ has_many :nullify_children, :dependent => :nullify
224
+ has_one :soft_nullify_one, :dependent => :nullify
225
+ has_one :nullify_one, :dependent => :nullify
226
+
227
+ # dependent restrict associations
228
+ has_many :soft_restrict_children, :dependent => :restrict
229
+ has_many :restrict_children, :dependent => :restrict
230
+ has_one :soft_restrict_one, :dependent => :restrict
231
+ has_one :restrict_one, :dependent => :restrict
232
+
233
+ # dependent delete_all, delete associations
234
+ has_many :soft_delete_all_children, :dependent => :delete_all
235
+ has_many :delete_all_children, :dependent => :delete_all
236
+ has_one :soft_delete_one, :dependent => :delete
237
+ has_one :delete_one, :dependent => :delete
238
+
239
+ soft_destroyable
240
+ end
241
+
242
+ class NonDependentChild < ActiveRecord::Base
243
+ belongs_to :parent
244
+ end
245
+
246
+ class SoftChild < ActiveRecord::Base
247
+ belongs_to :parent
248
+ soft_destroyable
249
+ end
250
+
251
+ class Child < ActiveRecord::Base
252
+ belongs_to :parent
253
+ end
254
+
255
+ class SoftOne < ActiveRecord::Base
256
+ belongs_to :parent
257
+ soft_destroyable
258
+ end
259
+
260
+ class One < ActiveRecord::Base
261
+ belongs_to :parent
262
+ end
263
+
264
+ class SoftNullifyChild < ActiveRecord::Base
265
+ belongs_to :parent
266
+ soft_destroyable
267
+ end
268
+
269
+ class NullifyChild < ActiveRecord::Base
270
+ belongs_to :parent
271
+ end
272
+
273
+ class SoftNullifyOne < ActiveRecord::Base
274
+ belongs_to :parent
275
+ soft_destroyable
276
+ end
277
+
278
+ class NullifyOne < ActiveRecord::Base
279
+ belongs_to :parent
280
+ end
281
+
282
+ class SoftRestrictChild < ActiveRecord::Base
283
+ belongs_to :parent
284
+ soft_destroyable
285
+ end
286
+
287
+ class RestrictChild < ActiveRecord::Base
288
+ belongs_to :parent
289
+ end
290
+
291
+ class SoftRestrictOne < ActiveRecord::Base
292
+ belongs_to :parent
293
+ soft_destroyable
294
+ end
295
+
296
+ class RestrictOne < ActiveRecord::Base
297
+ belongs_to :parent
298
+ end
299
+
300
+ class SoftDeleteAllChild < ActiveRecord::Base
301
+ belongs_to :parent
302
+ soft_destroyable
303
+ end
304
+
305
+ class DeleteAllChild < ActiveRecord::Base
306
+ belongs_to :parent
307
+ end
308
+
309
+ class SoftDeleteOne < ActiveRecord::Base
310
+ belongs_to :parent
311
+ soft_destroyable
312
+ end
313
+
314
+ class DeleteOne < ActiveRecord::Base
315
+ belongs_to :parent
316
+ end
317
+
318
+ class ParentSport < ActiveRecord::Base
319
+ belongs_to :parent
320
+ belongs_to :sport
321
+ end
322
+
323
+ class SoftParentSport < ActiveRecord::Base
324
+ belongs_to :parent
325
+ belongs_to :soft_sport
326
+ soft_destroyable
327
+ end
328
+
329
+ class SoftSport < ActiveRecord::Base
330
+ has_many :soft_parent_sports
331
+ soft_destroyable
332
+ end
333
+
334
+ class Sport < ActiveRecord::Base
335
+ has_many :parent_sports
336
+ end
337
+
338
+ class ParentNickname < ActiveRecord::Base
339
+ belongs_to :parent
340
+ belongs_to :nickname
341
+ end
342
+
343
+ class SoftParentNickname < ActiveRecord::Base
344
+ belongs_to :parent
345
+ belongs_to :soft_nickname
346
+ soft_destroyable
347
+ end
348
+
349
+ class SoftNickname < ActiveRecord::Base
350
+ has_one :soft_parent_nickname
351
+ soft_destroyable
352
+ end
353
+
354
+ class Nickname < ActiveRecord::Base
355
+ has_one :parent_nickname
356
+ end
357
+
358
+ class CallbackParent < ActiveRecord::Base
359
+ has_many :soft_callback_children, :dependent => :destroy
360
+ has_many :callback_children, :dependent => :destroy
361
+ soft_destroyable
362
+ end
363
+
364
+ class PreventDestroyBangError < StandardError
365
+
366
+ end
367
+
368
+ class PreventSoftDestroyError < StandardError
369
+
370
+ end
371
+
372
+ class SoftCallbackChild < ActiveRecord::Base
373
+ belongs_to :callback_parent
374
+ soft_destroyable
375
+
376
+ def before_soft_destroy
377
+ raise PreventSoftDestroyError.new
378
+ end
379
+
380
+ def before_destroy!
381
+ raise PreventDestroyBangError.new
382
+ end
383
+ end
384
+
385
+ class CallbackChild < ActiveRecord::Base
386
+ belongs_to :callback_parent
387
+
388
+ def before_soft_destroy
389
+ raise PreventSoftDestroyError.new
390
+ end
391
+
392
+ def before_destroy!
393
+ raise PreventDestroyBangError.new
394
+ end
395
+ end
@@ -0,0 +1,85 @@
1
+ require "#{File.dirname(__FILE__)}/test_helper"
2
+
3
+ class ThroughAssociationsTest < Test::Unit::TestCase
4
+
5
+ def setup
6
+ @fred = Parent.create!(:name => "fred")
7
+ end
8
+
9
+ def teardown
10
+ Parent.delete_all
11
+ Sport.delete_all
12
+ SoftSport.delete_all
13
+ SoftParentSport.delete_all
14
+ ParentSport.delete_all
15
+ Nickname.delete_all
16
+ SoftNickname.delete_all
17
+ ParentNickname.delete_all
18
+ SoftParentNickname.delete_all
19
+ end
20
+
21
+ def test_destroy_does_not_destroy_has_many_through_associations
22
+ baseball = Sport.create!(:name => "baseball")
23
+ basketball = SoftSport.create!(:name => "basketball")
24
+ @fred.sports << baseball
25
+ @fred.soft_sports << basketball
26
+ @fred.reload
27
+ assert_equal 1, @fred.sports.count
28
+ assert_equal 1, @fred.parent_sports.count
29
+ assert_equal 1, @fred.soft_sports.count
30
+ assert_equal 1, @fred.soft_parent_sports.count
31
+ @fred.destroy
32
+ assert_equal true, @fred.reload.deleted?
33
+ assert_equal 1, @fred.sports.count
34
+ assert_equal 1, @fred.soft_sports.count
35
+ end
36
+
37
+ def test_destroy_bang_does_not_destroy_has_many_through_associations
38
+ baseball = Sport.create!(:name => "baseball")
39
+ basketball = SoftSport.create!(:name => "basketball")
40
+ @fred.sports << baseball
41
+ @fred.soft_sports << basketball
42
+ @fred.reload
43
+ assert_equal 1, @fred.sports.count
44
+ assert_equal 1, @fred.parent_sports.count
45
+ assert_equal 1, @fred.soft_sports.count
46
+ assert_equal 1, @fred.soft_parent_sports.count
47
+ @fred.destroy!
48
+ assert_nil Parent.where(:name => "fred").first
49
+ assert_equal 1, Sport.count
50
+ assert_equal 1, SoftSport.count
51
+ end
52
+
53
+ def test_destroy_does_not_destroy_has_one_through_associations
54
+ rocky = Nickname.create!(:name => "rocky")
55
+ scar = SoftNickname.create!(:name => "scar")
56
+ @fred.nickname = rocky
57
+ @fred.soft_nickname = scar
58
+ @fred.reload
59
+ assert_equal rocky, @fred.nickname
60
+ assert_equal scar, @fred.soft_nickname
61
+ assert_equal rocky, @fred.parent_nickname.nickname
62
+ assert_equal scar, @fred.soft_parent_nickname.soft_nickname
63
+ @fred.destroy
64
+ assert_equal true, @fred.reload.deleted?
65
+ assert_equal rocky, @fred.nickname
66
+ assert_equal scar, @fred.soft_nickname
67
+ end
68
+
69
+ def test_destroy_bang_does_not_destroy_has_one_through_associations
70
+ rocky = Nickname.create!(:name => "rocky")
71
+ scar = SoftNickname.create!(:name => "scar")
72
+ @fred.nickname = rocky
73
+ @fred.soft_nickname = scar
74
+ @fred.reload
75
+ assert_equal rocky, @fred.nickname
76
+ assert_equal scar, @fred.soft_nickname
77
+ assert_equal rocky, @fred.parent_nickname.nickname
78
+ assert_equal scar, @fred.soft_parent_nickname.soft_nickname
79
+ @fred.destroy!
80
+ assert_nil Parent.where(:name => "fred").first
81
+ assert_equal 1, Nickname.count
82
+ assert_equal 1, SoftNickname.count
83
+ end
84
+
85
+ end
data/uninstall.rb ADDED
@@ -0,0 +1 @@
1
+ # Uninstall hook code here
metadata ADDED
@@ -0,0 +1,129 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: soft_destroyable
3
+ version: !ruby/object:Gem::Version
4
+ hash: 27
5
+ prerelease: false
6
+ segments:
7
+ - 0
8
+ - 1
9
+ - 0
10
+ version: 0.1.0
11
+ platform: ruby
12
+ authors:
13
+ - Michael Kintzer
14
+ autorequire:
15
+ bindir: bin
16
+ cert_chain: []
17
+
18
+ date: 2011-03-05 00:00:00 -08:00
19
+ default_executable:
20
+ dependencies:
21
+ - !ruby/object:Gem::Dependency
22
+ name: rails
23
+ prerelease: false
24
+ requirement: &id001 !ruby/object:Gem::Requirement
25
+ none: false
26
+ requirements:
27
+ - - ">="
28
+ - !ruby/object:Gem::Version
29
+ hash: 7
30
+ segments:
31
+ - 3
32
+ - 0
33
+ version: "3.0"
34
+ type: :development
35
+ version_requirements: *id001
36
+ - !ruby/object:Gem::Dependency
37
+ name: sqlite3
38
+ prerelease: false
39
+ requirement: &id002 !ruby/object:Gem::Requirement
40
+ none: false
41
+ requirements:
42
+ - - ">="
43
+ - !ruby/object:Gem::Version
44
+ hash: 29
45
+ segments:
46
+ - 1
47
+ - 3
48
+ - 3
49
+ version: 1.3.3
50
+ type: :development
51
+ version_requirements: *id002
52
+ description: Rails 3 ActiveRecord compatible soft destroy implementation supporting dependent associations
53
+ email: rockrep@yahoo.com
54
+ executables: []
55
+
56
+ extensions: []
57
+
58
+ extra_rdoc_files:
59
+ - README
60
+ files:
61
+ - MIT-LICENSE
62
+ - README
63
+ - Rakefile
64
+ - VERSION
65
+ - init.rb
66
+ - install.rb
67
+ - lib/soft_destroyable.rb
68
+ - lib/soft_destroyable/is_soft_destroyable.rb
69
+ - lib/soft_destroyable/table_definition.rb
70
+ - spec/support/soft_destroy_spec_helper.rb
71
+ - test/basic_test.rb
72
+ - test/callback_test.rb
73
+ - test/class_method_test.rb
74
+ - test/dependent_delete_all_test.rb
75
+ - test/dependent_delete_test.rb
76
+ - test/dependent_destroy_test.rb
77
+ - test/dependent_nullify_test.rb
78
+ - test/dependent_restrict_test.rb
79
+ - test/non_dependent_test.rb
80
+ - test/test_helper.rb
81
+ - test/through_associations_test.rb
82
+ - uninstall.rb
83
+ has_rdoc: true
84
+ homepage: http://github.com/rockrep/soft_destroyable
85
+ licenses: []
86
+
87
+ post_install_message:
88
+ rdoc_options: []
89
+
90
+ require_paths:
91
+ - lib
92
+ required_ruby_version: !ruby/object:Gem::Requirement
93
+ none: false
94
+ requirements:
95
+ - - ">="
96
+ - !ruby/object:Gem::Version
97
+ hash: 3
98
+ segments:
99
+ - 0
100
+ version: "0"
101
+ required_rubygems_version: !ruby/object:Gem::Requirement
102
+ none: false
103
+ requirements:
104
+ - - ">="
105
+ - !ruby/object:Gem::Version
106
+ hash: 3
107
+ segments:
108
+ - 0
109
+ version: "0"
110
+ requirements: []
111
+
112
+ rubyforge_project:
113
+ rubygems_version: 1.3.7
114
+ signing_key:
115
+ specification_version: 3
116
+ summary: Rails 3 ActiveRecord compatible soft destroy implementation
117
+ test_files:
118
+ - spec/support/soft_destroy_spec_helper.rb
119
+ - test/basic_test.rb
120
+ - test/callback_test.rb
121
+ - test/class_method_test.rb
122
+ - test/dependent_delete_all_test.rb
123
+ - test/dependent_delete_test.rb
124
+ - test/dependent_destroy_test.rb
125
+ - test/dependent_nullify_test.rb
126
+ - test/dependent_restrict_test.rb
127
+ - test/non_dependent_test.rb
128
+ - test/test_helper.rb
129
+ - test/through_associations_test.rb