diff --git a/test/dummy/app/models/animal.rb b/test/dummy/app/models/animal.rb index 63798859..99415b05 100644 --- a/test/dummy/app/models/animal.rb +++ b/test/dummy/app/models/animal.rb @@ -1,3 +1,4 @@ class Animal < ActiveRecord::Base has_paper_trail + set_inheritance_column 'species' end diff --git a/test/dummy/app/models/cat.rb b/test/dummy/app/models/cat.rb index 20409230..1450b2ae 100644 --- a/test/dummy/app/models/cat.rb +++ b/test/dummy/app/models/cat.rb @@ -1,3 +1,2 @@ class Cat < Animal - set_inheritance_column 'species' end diff --git a/test/dummy/app/models/dog.rb b/test/dummy/app/models/dog.rb index bb7c2e08..07dc5835 100644 --- a/test/dummy/app/models/dog.rb +++ b/test/dummy/app/models/dog.rb @@ -1,3 +1,2 @@ class Dog < Animal - set_inheritance_column 'species' end diff --git a/test/unit/inheritance_column_test.rb b/test/unit/inheritance_column_test.rb index 2e5cd40d..6ae94089 100644 --- a/test/unit/inheritance_column_test.rb +++ b/test/unit/inheritance_column_test.rb @@ -7,25 +7,36 @@ class InheritanceColumnTest < ActiveSupport::TestCase @animal = Animal.create :name => 'Animal' @animal.update_attributes :name => 'Animal from the Muppets' @animal.update_attributes :name => 'Animal Muppet' + @animal.destroy @dog = Dog.create :name => 'Snoopy' @dog.update_attributes :name => 'Scooby' @dog.update_attributes :name => 'Scooby Doo' + @dog.destroy @cat = Cat.create :name => 'Garfield' @cat.update_attributes :name => 'Garfield (I hate Mondays)' @cat.update_attributes :name => 'Garfield The Cat' + @cat.destroy end should 'work with custom STI inheritance column' do - assert_equal 9, Version.count - assert_equal 3, @animal.versions.count - assert_equal 3, @dog.versions.count - assert_equal 3, @cat.versions.count + assert_equal 12, Version.count + assert_equal 4, @animal.versions.count + assert @animal.versions.first.reify.nil? + @animal.versions[1..-1].each { |v| assert_equal 'Animal', v.reify.class.name } - assert_equal 'Animal from the Muppets', @animal.versions.last.reify.name - assert_equal 'Scooby', @dog.versions.last.reify.name - assert_equal 'Garfield (I hate Mondays)', @cat.versions.last.reify.name + # For some reason `@dog.versions` doesn't include the final `destroy` version. + # Neither do `@dog.versions.scoped` nor `@dog.versions(true)` nor `@dog.versions.reload`. + dog_versions = Version.where(:item_id => @dog.id) + assert_equal 4, dog_versions.count + assert dog_versions.first.reify.nil? + dog_versions[1..-1].each { |v| assert_equal 'Dog', v.reify.class.name } + + cat_versions = Version.where(:item_id => @cat.id) + assert_equal 4, cat_versions.count + assert cat_versions.first.reify.nil? + cat_versions[1..-1].each { |v| assert_equal 'Cat', v.reify.class.name } end end