Fix NoMethodError in load_changeset
Fixes NoMethodError when an STI parent class is unversioned and thus does not respond to `unserialize_attribute_changes_for_paper_trail!` [Fixes #738]
This commit is contained in:
parent
a95f073341
commit
23ffbdc7e1
|
@ -283,7 +283,16 @@ module PaperTrail
|
|||
# and appears to be responsible for custom attribute serializers. For an
|
||||
# example of a custom attribute serializer, see
|
||||
# `Person::TimeZoneSerializer` in the test suite.
|
||||
item_type.constantize.unserialize_attribute_changes_for_paper_trail!(changes)
|
||||
#
|
||||
# Is `item.class` good enough? Does it handle `inheritance_column`
|
||||
# as well as `Reifier#version_reification_class`? We were using
|
||||
# `item_type.constantize`, but that is problematic when the STI parent
|
||||
# is not versioned. (See `Vehicle` and `Car` in the test suite).
|
||||
#
|
||||
# Note: `item` returns nil if `event` is "destroy".
|
||||
unless item.nil?
|
||||
item.class.unserialize_attribute_changes_for_paper_trail!(changes)
|
||||
end
|
||||
|
||||
# Finally, return a Hash mapping each attribute name to
|
||||
# a two-element array representing before and after.
|
||||
|
|
|
@ -0,0 +1,13 @@
|
|||
require "rails_helper"
|
||||
|
||||
describe Car, type: :model do
|
||||
it { is_expected.to be_versioned }
|
||||
|
||||
describe "changeset", versioning: true do
|
||||
it "has the expected keys (see issue 738)" do
|
||||
car = Car.create!(name: "Alice")
|
||||
car.update_attributes(name: "Bob")
|
||||
assert_includes car.versions.last.changeset.keys, "name"
|
||||
end
|
||||
end
|
||||
end
|
|
@ -0,0 +1,5 @@
|
|||
require "rails_helper"
|
||||
|
||||
describe Truck, type: :model do
|
||||
it { is_expected.to_not be_versioned }
|
||||
end
|
|
@ -0,0 +1,5 @@
|
|||
require "rails_helper"
|
||||
|
||||
describe Vehicle, type: :model do
|
||||
it { is_expected.to_not be_versioned }
|
||||
end
|
|
@ -0,0 +1,3 @@
|
|||
class Car < Vehicle
|
||||
has_paper_trail
|
||||
end
|
|
@ -0,0 +1,4 @@
|
|||
class Truck < Vehicle
|
||||
# This STI child class specifically does not call `has_paper_trail`.
|
||||
# Of the sub-classes of `Vehicle`, only `Car` is versioned.
|
||||
end
|
|
@ -0,0 +1,4 @@
|
|||
class Vehicle < ActiveRecord::Base
|
||||
# This STI parent class specifically does not call `has_paper_trail`.
|
||||
# Of the sub-classes of `Vehicle`, only `Car` is versioned.
|
||||
end
|
|
@ -9,6 +9,13 @@ class SetUpTestTables < ActiveRecord::Migration
|
|||
TEXT_BYTES = 1_073_741_823
|
||||
|
||||
def up
|
||||
# Classes: Vehicle, Car, Truck
|
||||
create_table :vehicles, force: true do |t|
|
||||
t.string :name, null: false
|
||||
t.string :type, null: false
|
||||
t.timestamps null: false
|
||||
end
|
||||
|
||||
create_table :skippers, force: true do |t|
|
||||
t.string :name
|
||||
t.datetime :another_timestamp
|
||||
|
|
|
@ -201,6 +201,13 @@ ActiveRecord::Schema.define(version: 20110208155312) do
|
|||
t.string "type"
|
||||
end
|
||||
|
||||
create_table "vehicles", force: :cascade do |t|
|
||||
t.string "name", null: false
|
||||
t.string "type", null: false
|
||||
t.datetime "created_at", null: false
|
||||
t.datetime "updated_at", null: false
|
||||
end
|
||||
|
||||
create_table "version_associations", force: :cascade do |t|
|
||||
t.integer "version_id"
|
||||
t.string "foreign_key_name", null: false
|
||||
|
|
|
@ -433,6 +433,10 @@ class HasPaperTrailModelTest < ActiveSupport::TestCase
|
|||
assert_equal 1, @reified_widget.fluxors.length
|
||||
end
|
||||
|
||||
should "have nil item for last version" do
|
||||
assert_nil(@widget.versions.last.item)
|
||||
end
|
||||
|
||||
should "not have changes" do
|
||||
assert_equal({}, @widget.versions.last.changeset)
|
||||
end
|
||||
|
|
Loading…
Reference in New Issue