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:
Jared Beck 2016-05-01 00:21:51 -04:00
parent a95f073341
commit 23ffbdc7e1
10 changed files with 62 additions and 1 deletions

View File

@ -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.

13
spec/models/car_spec.rb Normal file
View File

@ -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

View File

@ -0,0 +1,5 @@
require "rails_helper"
describe Truck, type: :model do
it { is_expected.to_not be_versioned }
end

View File

@ -0,0 +1,5 @@
require "rails_helper"
describe Vehicle, type: :model do
it { is_expected.to_not be_versioned }
end

View File

@ -0,0 +1,3 @@
class Car < Vehicle
has_paper_trail
end

View File

@ -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

View File

@ -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

View File

@ -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

View File

@ -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

View File

@ -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