Permalink
Browse files

play nice with habtm relationships

  • Loading branch information...
1 parent 3ea1239 commit 97257f49fa99ec0d2872d4badfb43330a24ded40 @semanticart committed Sep 18, 2009
Showing with 36 additions and 4 deletions.
  1. +4 −1 CHANGELOG
  2. +2 −2 lib/is_paranoid.rb
  3. +10 −0 spec/is_paranoid_spec.rb
  4. +10 −1 spec/models.rb
  5. +10 −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-18
+* making is_paranoid play nice with habtm relationships
+
-2009-09-17
* fixed when primary key was not "id" (ie: "uuid") via Thibaud Guillaume-Gentil
@@ -34,4 +37,4 @@ This will only document major changes. Please see the commit log for minor chan
* requiring specific syntax to include calculations
-2009-03-21
-* initial release
+* initial release
View
@@ -209,7 +209,7 @@ def is_paranoid_method_missing name, *args, &block
end
# Mark the model deleted_at as now.
- def destroy_without_callbacks
+ def alt_destroy_without_callbacks
self.class.update_all(
"#{destroyed_field} = #{self.class.connection.quote(( field_destroyed.respond_to?(:call) ? field_destroyed.call : field_destroyed))}",
self.class.primary_key.to_sym => self.id
@@ -224,7 +224,7 @@ def destroy_without_callbacks
# separately.
def destroy
return false if callback(:before_destroy) == false
- result = destroy_without_callbacks
+ result = alt_destroy_without_callbacks
callback(:after_destroy)
self
end
View
@@ -17,6 +17,8 @@
@r2d2.memories.create(:name => 'A pretty sunset')
@c3p0.sticker = Sticker.create(:name => 'OMG, PONIES!')
+ @tatooine = Place.create(:name => "Tatooine")
+ @r2d2.places << @tatooine
end
describe 'non-is_paranoid models' do
@@ -69,6 +71,14 @@
}.should change(Android, :count).from(2).to(0)
Android.count_with_destroyed.should == 2
end
+
+ it "should not choke has_and_belongs_to_many relationships" do
+ @r2d2.places.should include(@tatooine)
+ @tatooine.destroy
+ @r2d2.reload
+ @r2d2.places.should_not include(@tatooine)
+ Place.all_with_destroyed.should include(@tatooine)
+ end
end
end
View
@@ -8,6 +8,7 @@ class Android < ActiveRecord::Base #:nodoc:
has_many :components, :dependent => :destroy
has_one :sticker
has_many :memories, :foreign_key => 'parent_id'
+ has_and_belongs_to_many :places
is_paranoid
@@ -41,7 +42,7 @@ class Memory < ActiveRecord::Base #:nodoc:
belongs_to :android, :class_name => "Android", :foreign_key => "parent_id"
end
-class Sticker < ActiveRecord::Base #:nodoc
+class Sticker < ActiveRecord::Base #:nodoc:
MM_NAME = "You've got method_missing"
# this simply serves to ensure that we don't break method_missing
@@ -60,6 +61,14 @@ class AndroidWithScopedUniqueness < ActiveRecord::Base #:nodoc:
is_paranoid
end
+class Place < ActiveRecord::Base #:nodoc:
+ is_paranoid
+ has_and_belongs_to_many :androids
+end
+
+class AndroidsPlaces < ActiveRecord::Base #:nodoc:
+end
+
class Ninja < ActiveRecord::Base #:nodoc:
validates_uniqueness_of :name, :scope => :visible
is_paranoid :field => [:visible, false, true]
View
@@ -7,6 +7,16 @@
t.datetime "updated_at"
end
+ create_table "androids_places", :force => true, :id => false do |t|
+ t.integer "android_id"
+ t.integer "place_id"
+ end
+
+ create_table "places", :force => true do |t|
+ t.string "name"
+ t.datetime "deleted_at"
+ end
+
create_table "people", :force => true do |t|
t.string "name"
t.datetime "created_at"

0 comments on commit 97257f4

Please sign in to comment.