2011-03-31 12:47:56 -04:00
|
|
|
require 'test_helper'
|
|
|
|
|
|
|
|
class InheritanceColumnTest < ActiveSupport::TestCase
|
|
|
|
context 'STI models' do
|
|
|
|
setup do
|
|
|
|
@animal = Animal.create :name => 'Animal'
|
|
|
|
@animal.update_attributes :name => 'Animal from the Muppets'
|
|
|
|
@animal.update_attributes :name => 'Animal Muppet'
|
2011-04-01 05:36:37 -04:00
|
|
|
@animal.destroy
|
2011-03-31 12:47:56 -04:00
|
|
|
|
|
|
|
@dog = Dog.create :name => 'Snoopy'
|
|
|
|
@dog.update_attributes :name => 'Scooby'
|
|
|
|
@dog.update_attributes :name => 'Scooby Doo'
|
2011-04-01 05:36:37 -04:00
|
|
|
@dog.destroy
|
2011-03-31 12:47:56 -04:00
|
|
|
|
|
|
|
@cat = Cat.create :name => 'Garfield'
|
|
|
|
@cat.update_attributes :name => 'Garfield (I hate Mondays)'
|
|
|
|
@cat.update_attributes :name => 'Garfield The Cat'
|
2011-04-01 05:36:37 -04:00
|
|
|
@cat.destroy
|
2011-03-31 12:47:56 -04:00
|
|
|
end
|
|
|
|
|
|
|
|
should 'work with custom STI inheritance column' do
|
2013-05-29 17:19:05 -04:00
|
|
|
assert_equal 12, PaperTrail::Version.count
|
2011-04-01 05:36:37 -04:00
|
|
|
assert_equal 4, @animal.versions.count
|
2014-03-15 18:12:52 -04:00
|
|
|
assert_nil @animal.versions.first.reify
|
2011-04-01 05:36:37 -04:00
|
|
|
@animal.versions[1..-1].each { |v| assert_equal 'Animal', v.reify.class.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`.
|
2015-12-22 23:09:33 -05:00
|
|
|
dog_versions = PaperTrail::Version.where(:item_id => @dog.id).
|
|
|
|
order(PaperTrail.timestamp_field)
|
2011-04-01 05:36:37 -04:00
|
|
|
assert_equal 4, dog_versions.count
|
2014-03-15 18:12:52 -04:00
|
|
|
assert_nil dog_versions.first.reify
|
2015-12-22 23:09:33 -05:00
|
|
|
assert_equal %w[NilClass Dog Dog Dog], dog_versions.map { |v| v.reify.class.name }
|
2011-04-01 05:36:37 -04:00
|
|
|
|
2015-12-22 23:09:33 -05:00
|
|
|
cat_versions = PaperTrail::Version.where(:item_id => @cat.id).
|
|
|
|
order(PaperTrail.timestamp_field)
|
2011-04-01 05:36:37 -04:00
|
|
|
assert_equal 4, cat_versions.count
|
2014-03-15 18:12:52 -04:00
|
|
|
assert_nil cat_versions.first.reify
|
2015-12-22 23:09:33 -05:00
|
|
|
assert_equal %w[NilClass Cat Cat Cat], cat_versions.map { |v| v.reify.class.name }
|
2011-03-31 12:47:56 -04:00
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|