Permalink
Browse files

Fixed when primary key was not "id" (ie: "uuid")

  • Loading branch information...
1 parent a330e84 commit e3660c8cdca0455e0f76fbaf1c66e58477fdfeb0 @thibaudgg thibaudgg committed Sep 17, 2009
Showing with 31 additions and 3 deletions.
  1. +3 −0 CHANGELOG
  2. +3 −3 lib/is_paranoid.rb
  3. +8 −0 spec/is_paranoid_spec.rb
  4. +10 −0 spec/models.rb
  5. +7 −0 spec/schema.rb
View
@@ -1,5 +1,8 @@
This will only document major changes. Please see the commit log for minor changes.
+-2009-09-17
+* fixed when primary key was not "id" (ie: "uuid") via Thibaud Guillaume-Gentil
+
-2009-09-15
* added support for has_many associations (i.e. @r2d2.components_with_destroyed) via Amiel Martin
View
@@ -60,8 +60,8 @@ def restore(id, options = {})
options.reverse_merge!({:include_destroyed_dependents => true}) unless options[:include]
with_exclusive_scope do
update_all(
- "#{destroyed_field} = #{connection.quote(field_not_destroyed)}",
- "id = #{id}"
+ "#{destroyed_field} = #{connection.quote(field_not_destroyed)}",
+ primary_key.to_sym => id
)
end
@@ -212,7 +212,7 @@ def is_paranoid_method_missing name, *args, &block
def destroy_without_callbacks
self.class.update_all(
"#{destroyed_field} = #{self.class.connection.quote(( field_destroyed.respond_to?(:call) ? field_destroyed.call : field_destroyed))}",
- "id = #{self.id}"
+ self.class.primary_key.to_sym => self.id
)
self
end
View
@@ -281,4 +281,12 @@
end
end
+
+ describe "alternate primary key" do
+ it "should destroy without problem" do
+ uuid = Uuid.create(:name => "foo")
+ uuid.destroy.should be_true
+ end
+ end
+
end
View
@@ -91,4 +91,14 @@ class UndestroyablePirate < ActiveRecord::Base #:nodoc:
def before_destroy
false
end
+end
+
+class Uuid < ActiveRecord::Base #:nodoc:
+ set_primary_key "uuid"
+
+ def before_create
+ self.uuid = "295b3430-85b8-012c-cfe4-002332cf7d5e"
+ end
+
+ is_paranoid
end
View
@@ -48,4 +48,11 @@
t.string "name"
t.boolean "alive", :default => true
end
+
+ create_table "uuids", :id => false, :force => true do |t|
+ t.string "uuid", :limit => 36, :primary => true
+ t.string "name"
+ t.datetime "deleted_at"
+ end
+
end

0 comments on commit e3660c8

Please sign in to comment.