jchupp-is_paranoid 0.3.0 → 0.4.0
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/README.textile +12 -3
- data/VERSION.yml +1 -1
- data/spec/is_paranoid_spec.rb +18 -3
- metadata +2 -2
data/README.textile
CHANGED
@@ -45,7 +45,7 @@ Now our automobiles are now soft-deleteable.
|
|
45
45
|
Automobile.count # => 1
|
46
46
|
</pre>
|
47
47
|
|
48
|
-
One thing to note, destroying is always undo-able, but deleting is not.
|
48
|
+
One thing to note, destroying is always undo-able, but deleting is not. This is a behavior difference between acts_as_paranoid and is_paranoid.
|
49
49
|
|
50
50
|
<pre>
|
51
51
|
Automobile.destroy_all
|
@@ -76,9 +76,18 @@ h3. Specifying alternate rules for what should be considered destroyed
|
|
76
76
|
|
77
77
|
These two models share the same table, but when we are finding Pirates, we're only interested in those that are alive. To break it down, we specify :alive as our field to check, false as what the model field should be marked at when destroyed and true to what the field should be if they're not destroyed. DeadPirates are specified as the opposite. Check out the specs if you're still confused.
|
78
78
|
|
79
|
-
h3. Note:
|
79
|
+
h3. Note about validates_uniqueness_of:
|
80
80
|
|
81
|
-
validates_uniqueness_of does not ignore items marked with a deleted_at flag. This is a behavior difference between is_paranoid and acts_as_paranoid.
|
81
|
+
validates_uniqueness_of does not, by default, ignore items marked with a deleted_at (or other field name) flag. This is a behavior difference between is_paranoid and acts_as_paranoid. You can overcome this by specifying the field name you are using to mark destroyed items as your scope. Example:
|
82
|
+
|
83
|
+
<pre>
|
84
|
+
class Android < ActiveRecord::Base
|
85
|
+
validates_uniqueness_of :name, :scope => :deleted_at
|
86
|
+
is_paranoid
|
87
|
+
end
|
88
|
+
</pre>
|
89
|
+
|
90
|
+
And now the validates_uniqueness_of will ignore items that are destroyed.
|
82
91
|
|
83
92
|
h3. and you may ask yourself, where does that highway go to?
|
84
93
|
|
data/VERSION.yml
CHANGED
data/spec/is_paranoid_spec.rb
CHANGED
@@ -1,6 +1,7 @@
|
|
1
1
|
require File.expand_path(File.dirname(__FILE__) + '/spec_helper')
|
2
2
|
|
3
3
|
class Person < ActiveRecord::Base
|
4
|
+
validates_uniqueness_of :name
|
4
5
|
has_many :androids, :foreign_key => :owner_id, :dependent => :destroy
|
5
6
|
end
|
6
7
|
|
@@ -9,11 +10,18 @@ class Android < ActiveRecord::Base
|
|
9
10
|
is_paranoid
|
10
11
|
end
|
11
12
|
|
13
|
+
class AndroidWithScopedUniqueness < ActiveRecord::Base
|
14
|
+
set_table_name :androids
|
15
|
+
validates_uniqueness_of :name, :scope => :deleted_at
|
16
|
+
is_paranoid
|
17
|
+
end
|
18
|
+
|
12
19
|
class NoCalculation < ActiveRecord::Base
|
13
20
|
is_paranoid
|
14
21
|
end
|
15
22
|
|
16
23
|
class Ninja < ActiveRecord::Base
|
24
|
+
validates_uniqueness_of :name, :scope => :visible
|
17
25
|
is_paranoid :field => [:visible, false, true]
|
18
26
|
end
|
19
27
|
|
@@ -106,13 +114,20 @@ describe Android do
|
|
106
114
|
Android.average_with_destroyed('id').should == (@r2d2.id + @c3p0.id) / 2.0
|
107
115
|
end
|
108
116
|
|
109
|
-
|
110
|
-
|
111
|
-
it "should not ignore deleted items in validation checks" do
|
117
|
+
it "should not ignore deleted items in validation checks unless scoped" do
|
118
|
+
# Androids are not validates_uniqueness_of scoped
|
112
119
|
@r2d2.destroy
|
113
120
|
lambda{
|
114
121
|
Android.create!(:name => 'R2D2')
|
115
122
|
}.should raise_error(ActiveRecord::RecordInvalid)
|
123
|
+
|
124
|
+
lambda{
|
125
|
+
# creating shouldn't raise an error
|
126
|
+
another_r2d2 = AndroidWithScopedUniqueness.create!(:name => 'R2D2')
|
127
|
+
# neither should destroying the second incarnation since the
|
128
|
+
# validates_uniqueness_of is only applied on create
|
129
|
+
another_r2d2.destroy
|
130
|
+
}.should_not raise_error
|
116
131
|
end
|
117
132
|
|
118
133
|
it "should allow specifying alternate fields and field values" do
|
metadata
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: jchupp-is_paranoid
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.
|
4
|
+
version: 0.4.0
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Jeffrey Chupp
|
@@ -9,7 +9,7 @@ autorequire:
|
|
9
9
|
bindir: bin
|
10
10
|
cert_chain: []
|
11
11
|
|
12
|
-
date: 2009-03-
|
12
|
+
date: 2009-03-29 00:00:00 -07:00
|
13
13
|
default_executable:
|
14
14
|
dependencies: []
|
15
15
|
|