Skip to content

Commit

Permalink
make polymorphic associations work
Browse files Browse the repository at this point in the history
  • Loading branch information
davidsulc committed May 1, 2011
1 parent 46b6e5c commit 3f5393d
Show file tree
Hide file tree
Showing 5 changed files with 51 additions and 13 deletions.
25 changes: 15 additions & 10 deletions lib/permanent_records.rb
Expand Up @@ -126,16 +126,21 @@ def revive_destroyed_dependent_records
self.class.reflections.select do |name, reflection|
'destroy' == reflection.options[:dependent].to_s && reflection.klass.is_permanent?
end.each do |name, reflection|

send(name).find(:all,
:conditions => [
"#{reflection.quoted_table_name}.deleted_at > ?" +
" AND " +
"#{reflection.quoted_table_name}.deleted_at < ?",
deleted_at - 3.seconds,
deleted_at + 3.seconds
]
).each do |dependent|
cardinality = reflection.macro.to_s.gsub('has_', '')
if cardinality == 'many'
records = send(name).find(:all,
:conditions => [
"#{reflection.quoted_table_name}.deleted_at > ?" +
" AND " +
"#{reflection.quoted_table_name}.deleted_at < ?",
deleted_at - 3.seconds,
deleted_at + 3.seconds
]
)
elsif cardinality == 'one'
records = [] << send(name)
end
records.each do |dependent|
dependent.revive
end

Expand Down
2 changes: 2 additions & 0 deletions test/hole.rb
Expand Up @@ -3,4 +3,6 @@ class Hole < ActiveRecord::Base
has_many :muskrats, :dependent => :destroy
# moles are not permanent
has_many :moles, :dependent => :destroy

has_one :location, :dependent => :destroy
end
3 changes: 3 additions & 0 deletions test/location.rb
@@ -0,0 +1,3 @@
class Location < ActiveRecord::Base
belongs_to :hole
end
28 changes: 25 additions & 3 deletions test/permanent_records_test.rb
@@ -1,6 +1,6 @@
require File.expand_path(File.dirname(__FILE__) + "/test_helper")

%w(hole mole muskrat kitty).each do |a|
%w(hole mole muskrat kitty location).each do |a|
require File.expand_path(File.dirname(__FILE__) + "/" + a)
end

Expand All @@ -17,6 +17,10 @@ def setup
@hole = Hole.create(:number => 14)
@hole.muskrats.create(:name => "Active Muskrat")
@hole.muskrats.create(:name => "Deleted Muskrat", :deleted_at => 5.days.ago)
Location.delete_all
@location = Location.create(:name => "South wall")
@hole.location = @location
@hole.save!
@mole = @hole.moles.create(:name => "Grabowski")
end

Expand Down Expand Up @@ -109,7 +113,7 @@ def test_dependent_non_permanent_records_should_be_destroyed
end
end

def test_dependent_permanent_records_should_be_marked_as_deleted
def test_dependent_permanent_records_with_has_many_cardinality_should_be_marked_as_deleted
assert @hole.is_permanent?
assert @hole.muskrats.first.is_permanent?
assert_no_difference "Muskrat.count" do
Expand All @@ -118,14 +122,32 @@ def test_dependent_permanent_records_should_be_marked_as_deleted
assert @hole.muskrats.first.deleted?
end

def test_dependent_permanent_records_should_be_revived_when_parent_is_revived
def test_dependent_permanent_records_with_has_one_cardinality_should_be_marked_as_deleted
assert @hole.is_permanent?
assert @hole.location.is_permanent?
assert_no_difference "Location.count" do
@hole.destroy
end
assert @hole.location.deleted?
assert Location.find_by_name("South wall").deleted?
end

def test_dependent_permanent_records_with_has_many_cardinality_should_be_revived_when_parent_is_revived
assert @hole.is_permanent?
@hole.destroy
assert @hole.muskrats.find_by_name("Active Muskrat").deleted?
@hole.revive
assert !@hole.muskrats.find_by_name("Active Muskrat").deleted?
end

def test_dependent_permanent_records_with_has_one_cardinality_should_be_revived_when_parent_is_revived
assert @hole.is_permanent?
@hole.destroy
assert Location.find_by_name("South wall").deleted?
@hole.revive
assert !Location.find_by_name("South wall").deleted?
end

def test_old_dependent_permanent_records_should_not_be_revived
assert @hole.is_permanent?
@hole.destroy
Expand Down
6 changes: 6 additions & 0 deletions test/schema.rb
Expand Up @@ -19,5 +19,11 @@
t.string :name
t.references :hole
end

create_table :locations, :force => true do |t|
t.string :name
t.references :hole
t.datetime :deleted_at
end

end

0 comments on commit 3f5393d

Please sign in to comment.