mirror of
https://github.com/paper-trail-gem/paper_trail.git
synced 2022-11-09 11:33:19 -05:00
84368ea289
Introduces event classes, like Events::Create, which wrap the AR instance and have one public method, `#data`, which returns the attributes of nascent `Version` records. See comments in Events::Base. This extraction greatly simplifies RecordTrail. Not only have we moved a lot of code out of RecordTrail, but we've also moved the `@in_after_callback` variable. In its new location, this variable is much easier to understand, and we no longer need the `ensure` blocks to reset it. Preserving compatibility with PT-AT caused some minor difficulties, and we should follow up with them to see if we can simplify the API they are using. Specifically, we would like to delete the `data_for_*` methods.
94 lines
3.4 KiB
Ruby
94 lines
3.4 KiB
Ruby
# frozen_string_literal: true
|
|
|
|
require "spec_helper"
|
|
require "support/custom_json_serializer"
|
|
|
|
RSpec.describe(PaperTrail, versioning: true) do
|
|
context "YAML serializer" do
|
|
it "saves the expected YAML in the object column" do
|
|
customer = Customer.create(name: "Some text.")
|
|
original_attributes = PaperTrail::Events::Base.
|
|
new(customer, false).
|
|
send(:attributes_before_change, false)
|
|
customer.update(name: "Some more text.")
|
|
expect(customer.versions.length).to(eq(2))
|
|
expect(customer.versions[0].reify).to(be_nil)
|
|
expect(customer.versions[1].reify.name).to(eq("Some text."))
|
|
expect(YAML.load(customer.versions[1].object)).to(eq(original_attributes))
|
|
expect(customer.versions[1].object).to(eq(YAML.dump(original_attributes)))
|
|
end
|
|
end
|
|
|
|
context "JSON Serializer" do
|
|
before do
|
|
PaperTrail.configure do |config|
|
|
config.serializer = PaperTrail::Serializers::JSON
|
|
end
|
|
end
|
|
|
|
after do
|
|
PaperTrail.config.serializer = PaperTrail::Serializers::YAML
|
|
end
|
|
|
|
it "reify with JSON serializer" do
|
|
customer = Customer.create(name: "Some text.")
|
|
original_attributes = PaperTrail::Events::Base.
|
|
new(customer, false).
|
|
send(:attributes_before_change, false)
|
|
customer.update(name: "Some more text.")
|
|
expect(customer.versions.length).to(eq(2))
|
|
expect(customer.versions[0].reify).to(be_nil)
|
|
expect(customer.versions[1].reify.name).to(eq("Some text."))
|
|
expect(ActiveSupport::JSON.decode(customer.versions[1].object)).to(eq(original_attributes))
|
|
expect(customer.versions[1].object).to(eq(ActiveSupport::JSON.encode(original_attributes)))
|
|
end
|
|
|
|
describe "#changeset" do
|
|
it "returns the expected hash" do
|
|
customer = Customer.create(name: "Some text.")
|
|
customer.update(name: "Some more text.")
|
|
initial_changeset = { "name" => [nil, "Some text."], "id" => [nil, customer.id] }
|
|
second_changeset = { "name" => ["Some text.", "Some more text."] }
|
|
expect(customer.versions[0].changeset).to(eq(initial_changeset))
|
|
expect(customer.versions[1].changeset).to(eq(second_changeset))
|
|
end
|
|
end
|
|
end
|
|
|
|
context "Custom Serializer" do
|
|
before do
|
|
PaperTrail.configure { |config| config.serializer = CustomJsonSerializer }
|
|
end
|
|
|
|
after do
|
|
PaperTrail.config.serializer = PaperTrail::Serializers::YAML
|
|
end
|
|
|
|
it "reify with custom serializer" do
|
|
customer = Customer.create
|
|
original_attributes = PaperTrail::Events::Base.
|
|
new(customer, false).
|
|
send(:attributes_before_change, false).
|
|
reject { |_k, v| v.nil? }
|
|
customer.update(name: "Some more text.")
|
|
expect(customer.versions.length).to(eq(2))
|
|
expect(customer.versions[0].reify).to(be_nil)
|
|
expect(customer.versions[1].reify.name).to(be_nil)
|
|
expect(
|
|
ActiveSupport::JSON.decode(customer.versions[1].object)
|
|
).to eq(original_attributes)
|
|
expect(
|
|
customer.versions[1].object
|
|
).to eq(ActiveSupport::JSON.encode(original_attributes))
|
|
end
|
|
|
|
describe "#changeset" do
|
|
it "store object_changes" do
|
|
customer = Customer.create
|
|
customer.update(name: "banana")
|
|
expect(customer.versions[0].changeset).to eq("id" => [nil, customer.id])
|
|
expect(customer.versions[1].changeset).to eq("name" => [nil, "banana"])
|
|
end
|
|
end
|
|
end
|
|
end
|