public
Description: ActiveRecord plugin allowing you to hide and restore records without actually deleting them.
Clone URL: git://github.com/technoweenie/acts_as_paranoid.git
Added HasManyThroughWithoutDeletedAssociation so that has_many :though 
associations don't include deleted records by default.
georg (author)
Mon Apr 14 02:04:17 -0700 2008
commit  64e54fea5c8a780f16bc8c3d70b6974b4d915acb
tree    791c308c42674b2ab1c5d7f5043b3dbdaff843be
parent  a3fdbc0637c76d37e6f9743e212f58005ee7e81c
...
11
12
13
 
 
 
 
 
 
 
 
 
 
 
14
 
 
15
16
17
...
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
0
@@ -11,6 +11,19 @@ class << ActiveRecord::Base
0
     end
0
   end
0
   
0
+ def has_many_without_deleted(association_id, options = {}, &extension)
0
+ with_deleted = options.delete :with_deleted
0
+ returning has_many_with_deleted(association_id, options, &extension) do
0
+ if options[:through] && !with_deleted
0
+ reflection = reflect_on_association(association_id)
0
+ collection_reader_method(reflection, Caboose::Acts::HasManyThroughWithoutDeletedAssociation)
0
+ collection_accessor_methods(reflection, Caboose::Acts::HasManyThroughWithoutDeletedAssociation, false)
0
+ end
0
+ end
0
+ end
0
+
0
   alias_method_chain :belongs_to, :deleted
0
+ alias_method :has_many_with_deleted, :has_many
0
+ alias_method :has_many, :has_many_without_deleted
0
 end
0
 ActiveRecord::Base.send :include, Caboose::Acts::Paranoid
0
\ No newline at end of file
...
6
7
8
 
 
 
9
10
11
...
22
23
24
 
 
 
 
 
 
 
 
 
 
 
25
26
27
28
29
 
30
31
32
...
111
112
113
 
 
 
 
 
 
 
 
 
 
114
115
116
...
6
7
8
9
10
11
12
13
14
...
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
 
43
44
45
46
...
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
0
@@ -6,6 +6,9 @@ class Widget < ActiveRecord::Base
0
   has_and_belongs_to_many :habtm_categories, :class_name => 'Category'
0
   has_one :category
0
   belongs_to :parent_category, :class_name => 'Category'
0
+ has_many :taggings
0
+ has_many :tags, :through => :taggings
0
+ has_many :any_tags, :through => :taggings, :class_name => 'Tag', :source => :tag, :with_deleted => true
0
 end
0
 
0
 class Category < ActiveRecord::Base
0
@@ -22,11 +25,22 @@ class Category < ActiveRecord::Base
0
   end
0
 end
0
 
0
+class Tag < ActiveRecord::Base
0
+ has_many :taggings
0
+ has_many :widgets, :through => :taggings
0
+end
0
+
0
+class Tagging < ActiveRecord::Base
0
+ belongs_to :tag
0
+ belongs_to :widget
0
+ acts_as_paranoid
0
+end
0
+
0
 class NonParanoidAndroid < ActiveRecord::Base
0
 end
0
 
0
 class ParanoidTest < Test::Unit::TestCase
0
- fixtures :widgets, :categories, :categories_widgets
0
+ fixtures :widgets, :categories, :categories_widgets, :tags, :taggings
0
 
0
   def test_should_count_with_deleted
0
     assert_equal 1, Widget.count
0
@@ -111,6 +125,16 @@ class ParanoidTest < Test::Unit::TestCase
0
     assert_equal [categories(:category_1)], widgets(:widget_1).habtm_categories
0
   end
0
   
0
+ def test_should_not_find_deleted_has_many_through_associations
0
+ assert_equal 1, widgets(:widget_1).tags.size
0
+ assert_equal [tags(:tag_2)], widgets(:widget_1).tags
0
+ end
0
+
0
+ def test_should_find_has_many_through_associations_with_deleted
0
+ assert_equal 2, widgets(:widget_1).any_tags.size
0
+ assert_equal Tag.find(:all), widgets(:widget_1).any_tags
0
+ end
0
+
0
   def test_should_not_find_deleted_belongs_to_associations
0
     assert_nil Category.find_with_deleted(3).widget
0
   end
...
16
17
18
 
 
 
 
 
 
 
 
 
 
19
20
21
...
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
0
@@ -16,5 +16,15 @@ ActiveRecord::Schema.define(:version => 1) do
0
     t.column :category_id, :integer
0
     t.column :widget_id, :integer
0
   end
0
+
0
+ create_table :tags, :force => true do |t|
0
+ t.column :name, :string, :limit => 50
0
+ end
0
+
0
+ create_table :taggings, :force => true do |t|
0
+ t.column :tag_id, :integer
0
+ t.column :widget_id, :integer
0
+ t.column :deleted_at, :timestamp
0
+ end
0
 
0
 end
0
\ No newline at end of file

Comments

    No one has commented yet.