acts_as_paranoid 0.3.1 → 0.4.0

Sign up to get free protection for your applications and to get access to all the features.
@@ -1,19 +0,0 @@
1
- category_1:
2
- id: 1
3
- widget_id: 1
4
- title: 'category 1'
5
- category_2:
6
- id: 2
7
- widget_id: 1
8
- title: 'category 2'
9
- deleted_at: '2005-01-01 00:00:00'
10
- category_3:
11
- id: 3
12
- widget_id: 2
13
- title: 'category 3'
14
- deleted_at: '2005-01-01 00:00:00'
15
- category_4:
16
- id: 4
17
- widget_id: 2
18
- title: 'category 4'
19
- deleted_at: '2005-01-01 00:00:00'
@@ -1,12 +0,0 @@
1
- cw_1:
2
- category_id: 1
3
- widget_id: 1
4
- cw_2:
5
- category_id: 2
6
- widget_id: 1
7
- cw_3:
8
- category_id: 3
9
- widget_id: 2
10
- cw_4:
11
- category_id: 4
12
- widget_id: 2
@@ -1,8 +0,0 @@
1
- widget_1:
2
- id: 1
3
- title: 'widget 1'
4
- widget_2:
5
- id: 2
6
- title: 'deleted widget 2'
7
- deleted_at: '2005-01-01 00:00:00'
8
- category_id: 3
@@ -1,151 +0,0 @@
1
- require File.join(File.dirname(__FILE__), 'test_helper')
2
-
3
- class Widget < ActiveRecord::Base
4
- acts_as_paranoid
5
- has_many :categories, :dependent => true
6
- has_and_belongs_to_many :habtm_categories, :class_name => 'Category'
7
- has_one :category
8
- belongs_to :parent_category, :class_name => 'Category'
9
- end
10
-
11
- class Category < ActiveRecord::Base
12
- belongs_to :widget
13
- acts_as_paranoid
14
- end
15
-
16
- class NonParanoidAndroid < ActiveRecord::Base
17
- end
18
-
19
- class ParanoidTest < Test::Unit::TestCase
20
- fixtures :widgets, :categories, :categories_widgets
21
-
22
- def test_should_set_deleted_at
23
- assert_equal 1, Widget.count
24
- assert_equal 1, Category.count
25
- widgets(:widget_1).destroy
26
- assert_equal 0, Widget.count
27
- assert_equal 0, Category.count
28
- assert_equal 2, Widget.count_with_deleted
29
- assert_equal 4, Category.count_with_deleted
30
- end
31
-
32
- def test_should_destroy
33
- assert_equal 1, Widget.count
34
- assert_equal 1, Category.count
35
- widgets(:widget_1).destroy!
36
- assert_equal 0, Widget.count
37
- assert_equal 0, Category.count
38
- assert_equal 1, Widget.count_with_deleted
39
- # Category doesn't get destroyed because the dependent before_destroy callback uses #destroy
40
- assert_equal 4, Category.count_with_deleted
41
- end
42
-
43
- def test_should_not_count_deleted
44
- assert_equal 1, Widget.count
45
- assert_equal 1, Widget.count(['title=?', 'widget 1'])
46
- assert_equal 2, Widget.count_with_deleted
47
- end
48
-
49
- def test_should_not_find_deleted
50
- assert_equal [widgets(:widget_1)], Widget.find(:all)
51
- assert_equal [1, 2], Widget.find_with_deleted(:all, :order => 'id').collect { |w| w.id }
52
- end
53
-
54
- def test_should_not_find_deleted_associations
55
- assert_equal 2, Category.count_with_deleted('widget_id=1')
56
-
57
- assert_equal 1, widgets(:widget_1).categories.size
58
- assert_equal [categories(:category_1)], widgets(:widget_1).categories
59
-
60
- assert_equal 1, widgets(:widget_1).habtm_categories.size
61
- assert_equal [categories(:category_1)], widgets(:widget_1).habtm_categories
62
- end
63
-
64
- def test_should_find_first_with_deleted
65
- assert_equal widgets(:widget_1), Widget.find(:first)
66
- assert_equal 2, Widget.find_with_deleted(:first, :order => 'id desc').id
67
- end
68
-
69
- def test_should_find_single_id
70
- assert Widget.find(1)
71
- assert Widget.find_with_deleted(2)
72
- assert_raises(ActiveRecord::RecordNotFound) { Widget.find(2) }
73
- end
74
-
75
- def test_should_find_multiple_ids
76
- assert_equal [1,2], Widget.find_with_deleted(1,2).sort_by { |w| w.id }.collect { |w| w.id }
77
- assert_equal [1,2], Widget.find_with_deleted([1,2]).sort_by { |w| w.id }.collect { |w| w.id }
78
- assert_raises(ActiveRecord::RecordNotFound) { Widget.find(1,2) }
79
- end
80
-
81
- def test_should_ignore_multiple_includes
82
- Widget.class_eval { acts_as_paranoid }
83
- assert Widget.find(1)
84
- end
85
-
86
- def test_should_not_override_scopes_when_counting
87
- assert_equal 1, Widget.with_scope(:find => { :conditions => "title = 'widget 1'" }) { Widget.count }
88
- assert_equal 0, Widget.with_scope(:find => { :conditions => "title = 'deleted widget 2'" }) { Widget.count }
89
- assert_equal 1, Widget.with_scope(:find => { :conditions => "title = 'deleted widget 2'" }) { Widget.count_with_deleted }
90
- end
91
-
92
- def test_should_not_override_scopes_when_finding
93
- assert_equal [1], Widget.with_scope(:find => { :conditions => "title = 'widget 1'" }) { Widget.find(:all) }.ids
94
- assert_equal [], Widget.with_scope(:find => { :conditions => "title = 'deleted widget 2'" }) { Widget.find(:all) }.ids
95
- assert_equal [2], Widget.with_scope(:find => { :conditions => "title = 'deleted widget 2'" }) { Widget.find_with_deleted(:all) }.ids
96
- end
97
-
98
- def test_should_allow_multiple_scoped_calls_when_finding
99
- Widget.with_scope(:find => { :conditions => "title = 'deleted widget 2'" }) do
100
- assert_equal [2], Widget.find_with_deleted(:all).ids
101
- assert_equal [2], Widget.find_with_deleted(:all).ids, "clobbers the constrain on the unmodified find"
102
- assert_equal [], Widget.find(:all).ids
103
- assert_equal [], Widget.find(:all).ids, 'clobbers the constrain on a paranoid find'
104
- end
105
- end
106
-
107
- def test_should_allow_multiple_scoped_calls_when_counting
108
- Widget.with_scope(:find => { :conditions => "title = 'deleted widget 2'" }) do
109
- assert_equal 1, Widget.count_with_deleted
110
- assert_equal 1, Widget.count_with_deleted, "clobbers the constrain on the unmodified find"
111
- assert_equal 0, Widget.count
112
- assert_equal 0, Widget.count, 'clobbers the constrain on a paranoid find'
113
- end
114
- end
115
-
116
- def test_should_give_paranoid_status
117
- assert Widget.paranoid?
118
- assert !NonParanoidAndroid.paranoid?
119
- end
120
-
121
- # sorry charlie, these are out!
122
- #def test_should_find_deleted_has_many_assocations_on_deleted_records_by_default
123
- # w = Widget.find_with_deleted 2
124
- # assert_equal 2, w.categories.length
125
- # assert_equal 2, w.categories(true).size
126
- #end
127
- #
128
- #def test_should_find_deleted_habtm_assocations_on_deleted_records_by_default
129
- # w = Widget.find_with_deleted 2
130
- # assert_equal 2, w.habtm_categories.length
131
- # assert_equal 2, w.habtm_categories(true).size
132
- #end
133
- #
134
- #def test_should_find_deleted_has_one_associations_on_deleted_records_by_default
135
- # c = Category.find_with_deleted 3
136
- # w = Widget.find_with_deleted 2
137
- # assert_equal c, w.category
138
- #end
139
- #
140
- #def test_should_find_deleted_belongs_to_associations_on_deleted_records_by_default
141
- # c = Category.find_with_deleted 3
142
- # w = Widget.find_with_deleted 2
143
- # assert_equal c, w.parent_category
144
- #end
145
- end
146
-
147
- class Array
148
- def ids
149
- collect { |i| i.id }
150
- end
151
- end
@@ -1,20 +0,0 @@
1
- ActiveRecord::Schema.define(:version => 1) do
2
-
3
- create_table :widgets, :force => true do |t|
4
- t.column :title, :string, :limit => 50
5
- t.column :category_id, :integer
6
- t.column :deleted_at, :timestamp
7
- end
8
-
9
- create_table :categories, :force => true do |t|
10
- t.column :widget_id, :integer
11
- t.column :title, :string, :limit => 50
12
- t.column :deleted_at, :timestamp
13
- end
14
-
15
- create_table :categories_widgets, :force => true, :id => false do |t|
16
- t.column :category_id, :integer
17
- t.column :widget_id, :integer
18
- end
19
-
20
- end
@@ -1,36 +0,0 @@
1
- $:.unshift(File.dirname(__FILE__) + '/../lib')
2
-
3
- require 'test/unit'
4
- require 'rubygems'
5
- require 'active_record'
6
- require 'active_record/fixtures'
7
- require 'active_support/binding_of_caller'
8
- require 'active_support/breakpoint'
9
- require "#{File.dirname(__FILE__)}/../init"
10
-
11
- config = YAML::load(IO.read(File.dirname(__FILE__) + '/database.yml'))
12
- ActiveRecord::Base.logger = Logger.new(File.dirname(__FILE__) + "/debug.log")
13
- ActiveRecord::Base.establish_connection(config[ENV['DB'] || 'sqlite'])
14
-
15
- load(File.dirname(__FILE__) + "/schema.rb")
16
-
17
- Test::Unit::TestCase.fixture_path = File.dirname(__FILE__) + "/fixtures/"
18
- $LOAD_PATH.unshift(Test::Unit::TestCase.fixture_path)
19
-
20
- class Test::Unit::TestCase #:nodoc:
21
- def create_fixtures(*table_names)
22
- if block_given?
23
- Fixtures.create_fixtures(Test::Unit::TestCase.fixture_path, table_names) { yield }
24
- else
25
- Fixtures.create_fixtures(Test::Unit::TestCase.fixture_path, table_names)
26
- end
27
- end
28
-
29
- # Turn off transactional fixtures if you're working with MyISAM tables in MySQL
30
- self.use_transactional_fixtures = true
31
-
32
- # Instantiated fixtures are slow, but give you @david where you otherwise would need people(:david)
33
- self.use_instantiated_fixtures = false
34
-
35
- # Add more helper methods to be used by all tests here...
36
- end