acts_as_paranoid 0.1.5 → 0.1.6

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/CHANGELOG CHANGED
@@ -1,3 +1,7 @@
1
+ *0.1.6* (25 Sep 2005)
2
+
3
+ * Fixed bug where nested constrains would get clobbered after multiple queries
4
+
1
5
  *0.1.5* (22 Sep 2005)
2
6
 
3
7
  * Fixed bug where acts_as_paranoid would clobber other constrains
@@ -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
- constrain = "#{table_name}.deleted_at IS NULL"
82
- constrains = (scope_constrains.nil? or scope_constrains[:conditions].nil? or scope_constrains[:conditions] == constrain) ?
83
- constrain : "#{scope_constrains[:conditions]} AND #{constrain}"
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
 
@@ -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) }.collect { |w| w.id }
89
- assert_equal [], Widget.constrain(:conditions => "title = 'deleted widget 2'") { Widget.find(:all) }.collect { |w| w.id }
90
- assert_equal [2], Widget.constrain(:conditions => "title = 'deleted widget 2'") { Widget.find_with_deleted(:all) }.collect { |w| w.id }
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.5
7
- date: 2005-09-22
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