jcnetdev-acts_as_paranoid 1.1.20080706 → 1.2.20080706
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.
- data/acts_as_paranoid.gemspec +1 -1
- data/lib/caboose/acts/paranoid.rb +12 -1
- data/rails/init.rb +1 -30
- data/test/paranoid_test.rb +23 -0
- data/test/test_helper.rb +2 -0
- metadata +1 -1
data/acts_as_paranoid.gemspec
CHANGED
@@ -51,6 +51,10 @@ module Caboose #:nodoc:
|
|
51
51
|
module ClassMethods
|
52
52
|
def acts_as_paranoid(options = {})
|
53
53
|
unless paranoid? # don't let AR call this twice
|
54
|
+
class << self
|
55
|
+
VALID_FIND_OPTIONS << :with_deleted unless VALID_FIND_OPTIONS.include?(:with_deleted)
|
56
|
+
end
|
57
|
+
ActiveRecord::Calculations::CALCULATIONS_OPTIONS << :with_deleted unless ActiveRecord::Calculations::CALCULATIONS_OPTIONS.include?(:with_deleted)
|
54
58
|
cattr_accessor :deleted_attribute
|
55
59
|
self.deleted_attribute = options[:with] || :deleted_at
|
56
60
|
alias_method :destroy_without_callbacks!, :destroy_without_callbacks
|
@@ -59,6 +63,9 @@ module Caboose #:nodoc:
|
|
59
63
|
alias_method :calculate_with_deleted, :calculate
|
60
64
|
alias_method :delete_all!, :delete_all
|
61
65
|
end
|
66
|
+
if ActiveRecord::Base.respond_to?(:named_scope)
|
67
|
+
named_scope :with_deleted, lambda { |with_deleted| (with_deleted == true) ? { :with_deleted => true } : {} }
|
68
|
+
end
|
62
69
|
end
|
63
70
|
include InstanceMethods
|
64
71
|
end
|
@@ -113,7 +120,11 @@ module Caboose #:nodoc:
|
|
113
120
|
end
|
114
121
|
|
115
122
|
def with_deleted_scope(&block)
|
116
|
-
|
123
|
+
unless current_scoped_methods.is_a?(Hash) && current_scoped_methods[:find].is_a?(Hash) && current_scoped_methods[:find][:with_deleted] == true
|
124
|
+
with_scope({:find => { :conditions => ["#{table_name}.#{deleted_attribute} IS NULL OR #{table_name}.#{deleted_attribute} > ?", current_time] } }, :merge, &block)
|
125
|
+
else
|
126
|
+
with_scope(&block)
|
127
|
+
end
|
117
128
|
end
|
118
129
|
|
119
130
|
private
|
data/rails/init.rb
CHANGED
@@ -1,30 +1 @@
|
|
1
|
-
|
2
|
-
def belongs_to_with_deleted(association_id, options = {})
|
3
|
-
with_deleted = options.delete :with_deleted
|
4
|
-
returning belongs_to_without_deleted(association_id, options) do
|
5
|
-
if with_deleted
|
6
|
-
reflection = reflect_on_association(association_id)
|
7
|
-
association_accessor_methods(reflection, Caboose::Acts::BelongsToWithDeletedAssociation)
|
8
|
-
association_constructor_method(:build, reflection, Caboose::Acts::BelongsToWithDeletedAssociation)
|
9
|
-
association_constructor_method(:create, reflection, Caboose::Acts::BelongsToWithDeletedAssociation)
|
10
|
-
end
|
11
|
-
end
|
12
|
-
end
|
13
|
-
|
14
|
-
def has_many_without_deleted(association_id, options = {}, &extension)
|
15
|
-
with_deleted = options.delete :with_deleted
|
16
|
-
returning has_many_with_deleted(association_id, options, &extension) do
|
17
|
-
if options[:through] && !with_deleted
|
18
|
-
reflection = reflect_on_association(association_id)
|
19
|
-
collection_reader_method(reflection, Caboose::Acts::HasManyThroughWithoutDeletedAssociation)
|
20
|
-
collection_accessor_methods(reflection, Caboose::Acts::HasManyThroughWithoutDeletedAssociation, false)
|
21
|
-
end
|
22
|
-
end
|
23
|
-
end
|
24
|
-
|
25
|
-
alias_method_chain :belongs_to, :deleted
|
26
|
-
alias_method :has_many_with_deleted, :has_many
|
27
|
-
alias_method :has_many, :has_many_without_deleted
|
28
|
-
alias_method :exists_with_deleted?, :exists?
|
29
|
-
end
|
30
|
-
ActiveRecord::Base.send :include, Caboose::Acts::Paranoid
|
1
|
+
require 'acts_as_paranoid'
|
data/test/paranoid_test.rb
CHANGED
@@ -45,12 +45,21 @@ class ParanoidTest < Test::Unit::TestCase
|
|
45
45
|
def test_should_exists_with_deleted
|
46
46
|
assert Widget.exists_with_deleted?(2)
|
47
47
|
assert !Widget.exists?(2)
|
48
|
+
if NAMED_SCOPE_TESTS
|
49
|
+
assert Widget.with_deleted(true).exists?(2)
|
50
|
+
assert !Widget.with_deleted(false).exists?(2)
|
51
|
+
end
|
48
52
|
end
|
49
53
|
|
50
54
|
def test_should_count_with_deleted
|
51
55
|
assert_equal 1, Widget.count
|
52
56
|
assert_equal 2, Widget.count_with_deleted
|
53
57
|
assert_equal 2, Widget.calculate_with_deleted(:count, :all)
|
58
|
+
if NAMED_SCOPE_TESTS
|
59
|
+
assert_equal 1, Widget.with_deleted(false).count
|
60
|
+
assert_equal 2, Widget.with_deleted(true).count
|
61
|
+
assert_equal 2, Widget.with_deleted(true).calculate_with_deleted(:count, :all)
|
62
|
+
end
|
54
63
|
end
|
55
64
|
|
56
65
|
def test_should_set_deleted_at
|
@@ -174,12 +183,20 @@ class ParanoidTest < Test::Unit::TestCase
|
|
174
183
|
assert_equal 1, Widget.send(:with_scope, :find => { :conditions => "title = 'widget 1'" }) { Widget.count }
|
175
184
|
assert_equal 0, Widget.send(:with_scope, :find => { :conditions => "title = 'deleted widget 2'" }) { Widget.count }
|
176
185
|
assert_equal 1, Widget.send(:with_scope, :find => { :conditions => "title = 'deleted widget 2'" }) { Widget.calculate_with_deleted(:count, :all) }
|
186
|
+
if NAMED_SCOPE_TESTS
|
187
|
+
assert_equal 1, Widget.send(:with_scope, :find => { :conditions => "title = 'deleted widget 2'" }) { Widget.with_deleted(true).count }
|
188
|
+
assert_equal 0, Widget.send(:with_scope, :find => { :conditions => "title = 'deleted widget 2'" }) { Widget.with_deleted(false).count }
|
189
|
+
end
|
177
190
|
end
|
178
191
|
|
179
192
|
def test_should_not_override_scopes_when_finding
|
180
193
|
assert_equal [1], Widget.send(:with_scope, :find => { :conditions => "title = 'widget 1'" }) { Widget.find(:all) }.ids
|
181
194
|
assert_equal [], Widget.send(:with_scope, :find => { :conditions => "title = 'deleted widget 2'" }) { Widget.find(:all) }.ids
|
182
195
|
assert_equal [2], Widget.send(:with_scope, :find => { :conditions => "title = 'deleted widget 2'" }) { Widget.find_with_deleted(:all) }.ids
|
196
|
+
if NAMED_SCOPE_TESTS
|
197
|
+
assert_equal [2], Widget.send(:with_scope, :find => { :conditions => "title = 'deleted widget 2'" }) { Widget.with_deleted(true).find(:all) }.ids
|
198
|
+
assert_equal [], Widget.send(:with_scope, :find => { :conditions => "title = 'deleted widget 2'" }) { Widget.with_deleted(false).find(:all) }.ids
|
199
|
+
end
|
183
200
|
end
|
184
201
|
|
185
202
|
def test_should_allow_multiple_scoped_calls_when_finding
|
@@ -197,6 +214,12 @@ class ParanoidTest < Test::Unit::TestCase
|
|
197
214
|
assert_equal 1, Widget.calculate_with_deleted(:count, :all), "clobbers the constrain on the unmodified find"
|
198
215
|
assert_equal 0, Widget.count
|
199
216
|
assert_equal 0, Widget.count, 'clobbers the constrain on a paranoid find'
|
217
|
+
if NAMED_SCOPE_TESTS
|
218
|
+
assert_equal 1, Widget.with_deleted(true).calculate(:count, :all)
|
219
|
+
assert_equal 1, Widget.with_deleted(true).calculate(:count, :all), "clobbers the constrain on the unmodified find"
|
220
|
+
assert_equal 0, Widget.count
|
221
|
+
assert_equal 0, Widget.count, 'clobbers the constrain on a paranoid find'
|
222
|
+
end
|
200
223
|
end
|
201
224
|
end
|
202
225
|
|
data/test/test_helper.rb
CHANGED
@@ -9,6 +9,8 @@ config = YAML::load(IO.read(File.dirname(__FILE__) + '/database.yml'))
|
|
9
9
|
ActiveRecord::Base.logger = Logger.new(File.dirname(__FILE__) + "/debug.log")
|
10
10
|
ActiveRecord::Base.establish_connection(config[ENV['DB'] || 'sqlite'])
|
11
11
|
|
12
|
+
NAMED_SCOPE_TESTS = ActiveRecord::Base.respond_to?(:named_scope)
|
13
|
+
|
12
14
|
load(File.dirname(__FILE__) + "/schema.rb")
|
13
15
|
|
14
16
|
Test::Unit::TestCase.fixture_path = File.dirname(__FILE__) + "/fixtures/"
|