Skip to content

Commit

Permalink
Add optional column: item_subtype
Browse files Browse the repository at this point in the history
If present, will be populated with subclass name. This will be
used by PT-AT.
  • Loading branch information
jaredbeck committed Aug 22, 2018
1 parent 9f004a6 commit e907295
Show file tree
Hide file tree
Showing 8 changed files with 40 additions and 30 deletions.
9 changes: 9 additions & 0 deletions lib/paper_trail/events/base.rb
Original file line number Diff line number Diff line change
Expand Up @@ -162,6 +162,15 @@ def ignored_attr_has_changed?
ignored.any? && (changed_in_latest_version & ignored).any?
end

# PT 10 has a new optional column, `item_subtype`
#
# @api private
def merge_item_subtype_into(data)
if @record.class.paper_trail.version_class.columns_hash.key?("item_subtype")
data.merge!(item_subtype: @record.class.name)
end
end

# Updates `data` from the model's `meta` option and from `controller_info`.
# Metadata is always recorded; that means all three events (create, update,
# destroy) and `update_columns`.
Expand Down
1 change: 1 addition & 0 deletions lib/paper_trail/events/create.rb
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ def data
changes = notable_changes
data[:object_changes] = prepare_object_changes(changes)
end
merge_item_subtype_into(data)
merge_metadata_into(data)
end
end
Expand Down
1 change: 1 addition & 0 deletions lib/paper_trail/events/destroy.rb
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@ def data
changes = @record.attributes.map { |attr, value| [attr, [value, nil]] }.to_h
data[:object_changes] = prepare_object_changes(changes)
end
merge_item_subtype_into(data)
merge_metadata_into(data)
end
end
Expand Down
1 change: 1 addition & 0 deletions lib/paper_trail/events/update.rb
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,7 @@ def data
changes = @force_changes.nil? ? notable_changes : @force_changes
data[:object_changes] = prepare_object_changes(changes)
end
merge_item_subtype_into(data)
merge_metadata_into(data)
end

Expand Down
11 changes: 6 additions & 5 deletions spec/dummy_app/db/migrate/20110208155312_set_up_test_tables.rb
Original file line number Diff line number Diff line change
Expand Up @@ -78,9 +78,10 @@ def up
end

create_table :versions, versions_table_options do |t|
t.string :item_type, item_type_options
t.integer :item_id, null: false
t.string :event, null: false
t.string :item_type, item_type_options(null: false)
t.integer :item_id, null: false
t.string :item_subtype, item_type_options(null: true)
t.string :event, null: false
t.string :whodunnit
t.text :object, limit: TEXT_BYTES
t.text :object_changes, limit: TEXT_BYTES
Expand Down Expand Up @@ -377,8 +378,8 @@ def down

private

def item_type_options
opt = { null: false }
def item_type_options(null:)
opt = { null: null }
opt[:limit] = 191 if mysql?
opt
end
Expand Down
37 changes: 17 additions & 20 deletions spec/models/family/celebrity_family_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -3,23 +3,22 @@
require "spec_helper"

module Family
RSpec.describe CelebrityFamily, type: :model, skip: true, versioning: true do
RSpec.describe CelebrityFamily, type: :model, versioning: true do
describe "#create" do
# https://github.com/paper-trail-gem/paper_trail/pull/1108
it "creates a version record with item_type == class.name, not base_class" do
it "creates version with item_subtype == class.name, not base_class" do
carter = described_class.create(
name: "Carter",
path_to_stardom: "Mexican radio"
)
v = carter.versions.last
expect(v[:event]).to eq("create")
expect(v[:item_type]).to eq("Family::CelebrityFamily")
expect(v[:item_subtype]).to eq("Family::CelebrityFamily")
end
end

describe "#reify" do
context "belongs_to" do
it "uses the correct item_type in queries" do
it "uses the correct item_subtype" do
parent = described_class.new(name: "Jermaine Jackson")
parent.path_to_stardom = "Emulating Motown greats such as the Temptations and "\
"The Supremes"
Expand All @@ -30,12 +29,13 @@ module Family
name: "Hazel Gordy",
children_attributes: { id: child1.id, name: "Jay Jackson" }
)
# We expect `reify` for all versions to have item_type 'Family::CelebrityFamily',
# not 'Family::Family'. See PR #1108

expect(parent.versions.count).to eq(2) # A create and an update
parent.versions.each do |parent_version|
expect(parent_version.item_type).to eq(parent.class.name)
expect(parent_version.item_type).to eq("Family::Family")
expect(parent_version.item_subtype).to eq("Family::CelebrityFamily")
end
expect(parent.versions[1].reify).to be_a(::Family::CelebrityFamily)
end
end

Expand All @@ -50,11 +50,10 @@ module Family
parent.children.build(name: "Pugsley")
parent.save!

# We expect `reify` for all versions to have item_type 'Family::CelebrityFamily',
# not 'Family::Family'. See PR #1108
expect(parent.versions.count).to eq(2)
parent.versions.each do |parent_version|
expect(parent_version.item_type).to eq(parent.class.name)
expect(parent_version.item_type).to eq("Family::Family")
expect(parent_version.item_subtype).to eq("Family::CelebrityFamily")
end
end
end
Expand All @@ -71,11 +70,10 @@ module Family
parent.grandsons.build(name: "Rodney")
parent.save!

# We expect `reify` for all versions to have item_type 'Family::CelebrityFamily',
# not 'Family::Family'. See PR #1108
expect(parent.versions.count).to eq(2)
parent.versions.each do |parent_version|
expect(parent_version.item_type).to eq(parent.class.name)
expect(parent_version.item_type).to eq("Family::Family")
expect(parent_version.item_subtype).to eq("Family::CelebrityFamily")
end
end
end
Expand All @@ -93,27 +91,26 @@ module Family
mentee_attributes: { id: parent.mentee.id, name: "Al Shean" }
)

# We expect `reify` for all versions to have item_type 'Family::CelebrityFamily',
# not 'Family::Family'. See PR #1108
expect(parent.versions.count).to eq(2)
parent.versions.each do |parent_version|
expect(parent_version.item_type).to eq(parent.class.name)
expect(parent_version.item_type).to eq("Family::Family")
expect(parent_version.item_subtype).to eq("Family::CelebrityFamily")
end
end
end
end

describe "#update" do
# https://github.com/paper-trail-gem/paper_trail/pull/1108
it "creates a version record with item_type == class.name, not base_class" do
it "creates version with item_subtype == class.name, not base_class" do
carter = described_class.create(
name: "Carter",
path_to_stardom: "Mexican radio"
)
carter.update(path_to_stardom: "Johnny")
v = carter.versions.last
expect(v[:event]).to eq("update")
expect(v[:item_type]).to eq("Family::CelebrityFamily")
expect(v[:item_type]).to eq("Family::Family")
expect(v[:item_subtype]).to eq("Family::CelebrityFamily")
end
end
end
Expand Down
2 changes: 1 addition & 1 deletion spec/models/person_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -166,7 +166,7 @@

expect(person.reload.versions.length).to(eq(3))

# These will work when PT-AT has PR #5 merged:
# These will work when PT-AT adds support for the new `item_subtype` column
# second_version = person.reload.versions.second.reify(has_one: true)
# expect(second_version.car.name).to(eq("BMW 325"))
# expect(second_version.bicycle.name).to(eq("BMX 1.0"))
Expand Down
8 changes: 4 additions & 4 deletions spec/paper_trail/events/destroy_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -5,15 +5,15 @@
module PaperTrail
module Events
::RSpec.describe Destroy do
describe "#data", skip: true, versioning: true do
# https://github.com/paper-trail-gem/paper_trail/pull/1108
it "uses class.name for item_type, not base_class" do
describe "#data", versioning: true do
it "includes correct item_subtype" do
carter = Family::CelebrityFamily.new(
name: "Carter",
path_to_stardom: "Mexican radio"
)
data = PaperTrail::Events::Destroy.new(carter, true).data
expect(data[:item_type]).to eq("Family::CelebrityFamily")
expect(data[:item_type]).to eq("Family::Family")
expect(data[:item_subtype]).to eq("Family::CelebrityFamily")
end
end
end
Expand Down

0 comments on commit e907295

Please sign in to comment.