acts_as_paranoid 0.1.5 → 0.1.6
Sign up to get free protection for your applications and to get access to all the features.
- data/CHANGELOG +4 -0
- data/lib/acts_as_paranoid.rb +27 -4
- data/test/fixtures/activerecord_paranoid.sqlite +0 -0
- data/test/paranoid_test.rb +27 -3
- metadata +2 -2
data/CHANGELOG
CHANGED
data/lib/acts_as_paranoid.rb
CHANGED
@@ -53,6 +53,9 @@ module ActiveRecord #:nodoc:
|
|
53
53
|
|
54
54
|
module ParanoidMethods #:nodoc:
|
55
55
|
def self.included(base) # :nodoc:
|
56
|
+
class << base
|
57
|
+
alias_method :clobbering_constrain, :constrain
|
58
|
+
end
|
56
59
|
base.extend ClassMethods
|
57
60
|
end
|
58
61
|
|
@@ -75,12 +78,32 @@ module ActiveRecord #:nodoc:
|
|
75
78
|
def count(conditions = nil, joins = nil)
|
76
79
|
constrain(scope_constrains.merge(:conditions => deleted_constrain)) { count_with_deleted(conditions, joins) }
|
77
80
|
end
|
78
|
-
|
81
|
+
|
82
|
+
# Override #constrain so that nested constrains don't clobber each other.
|
83
|
+
#
|
84
|
+
# Entry.constrain(:conditions => 'published_at IS NOT NULL') do
|
85
|
+
# Entry.constrain(:conditions => 'deleted_at IS NULL') do
|
86
|
+
# Entry.find(:all)
|
87
|
+
# end
|
88
|
+
# end
|
89
|
+
def constrain(options = {}, &block)
|
90
|
+
begin
|
91
|
+
is_new_scope = scope_constrains.empty?
|
92
|
+
self.scope_constrains = options
|
93
|
+
block.call if block_given?
|
94
|
+
ensure
|
95
|
+
self.scope_constrains = nil if is_new_scope
|
96
|
+
end
|
97
|
+
end
|
98
|
+
|
79
99
|
protected
|
80
100
|
def deleted_constrain
|
81
|
-
|
82
|
-
|
83
|
-
|
101
|
+
deleted_cond = "#{table_name}.deleted_at IS NULL"
|
102
|
+
case scope_constrains[:conditions]
|
103
|
+
when /#{deleted_cond}/ then scope_constrains[:conditions]
|
104
|
+
when NilClass then deleted_cond
|
105
|
+
else "#{scope_constrains[:conditions]} AND #{deleted_cond}"
|
106
|
+
end
|
84
107
|
end
|
85
108
|
end
|
86
109
|
|
Binary file
|
data/test/paranoid_test.rb
CHANGED
@@ -85,8 +85,32 @@ class ParanoidTest < Test::Unit::TestCase
|
|
85
85
|
end
|
86
86
|
|
87
87
|
def test_should_not_override_constrains_when_finding
|
88
|
-
assert_equal [1], Widget.constrain(:conditions => "title = 'widget 1'") { Widget.find(:all) }.
|
89
|
-
assert_equal [], Widget.constrain(:conditions => "title = 'deleted widget 2'") { Widget.find(:all) }.
|
90
|
-
assert_equal [2], Widget.constrain(:conditions => "title = 'deleted widget 2'") { Widget.find_with_deleted(:all) }.
|
88
|
+
assert_equal [1], Widget.constrain(:conditions => "title = 'widget 1'") { Widget.find(:all) }.ids
|
89
|
+
assert_equal [], Widget.constrain(:conditions => "title = 'deleted widget 2'") { Widget.find(:all) }.ids
|
90
|
+
assert_equal [2], Widget.constrain(:conditions => "title = 'deleted widget 2'") { Widget.find_with_deleted(:all) }.ids
|
91
|
+
end
|
92
|
+
|
93
|
+
def test_should_allow_multiple_constrained_calls_when_finding
|
94
|
+
Widget.constrain(:conditions => "title = 'deleted widget 2'") do
|
95
|
+
assert_equal [2], Widget.find_with_deleted(:all).ids
|
96
|
+
assert_equal [2], Widget.find_with_deleted(:all).ids, "clobbers the constrain on the unmodified find"
|
97
|
+
assert_equal [], Widget.find(:all).ids
|
98
|
+
assert_equal [], Widget.find(:all).ids, 'clobbers the constrain on a paranoid find'
|
99
|
+
end
|
100
|
+
end
|
101
|
+
|
102
|
+
def test_should_allow_multiple_constrained_calls_when_counting
|
103
|
+
Widget.constrain(:conditions => "title = 'deleted widget 2'") do
|
104
|
+
assert_equal 1, Widget.count_with_deleted
|
105
|
+
assert_equal 1, Widget.count_with_deleted, "clobbers the constrain on the unmodified find"
|
106
|
+
assert_equal 0, Widget.count
|
107
|
+
assert_equal 0, Widget.count, 'clobbers the constrain on a paranoid find'
|
108
|
+
end
|
109
|
+
end
|
110
|
+
end
|
111
|
+
|
112
|
+
class Array
|
113
|
+
def ids
|
114
|
+
collect { |i| i.id }
|
91
115
|
end
|
92
116
|
end
|
metadata
CHANGED
@@ -3,8 +3,8 @@ rubygems_version: 0.8.10
|
|
3
3
|
specification_version: 1
|
4
4
|
name: acts_as_paranoid
|
5
5
|
version: !ruby/object:Gem::Version
|
6
|
-
version: 0.1.
|
7
|
-
date: 2005-09-
|
6
|
+
version: 0.1.6
|
7
|
+
date: 2005-09-25
|
8
8
|
summary: acts_as_paranoid keeps models from actually being deleted by setting a deleted_at field.
|
9
9
|
require_paths:
|
10
10
|
- lib
|