Skip to content

Commit c10a857

Browse files
Rails 7.0 Compatibility (#1365)
* Make paper_trail work with Rails 7.0 * from class_methods do back to module ClassMethods * add spec for PostgresArraySerializer to boost coverage * lint the spec for PostgresArraySerializer * lint the spec for PostgresArraySerializer again * and now make that linted spec pass again * test object change scopes a bit * round out json and jsonb testing of object scopes * test some other code paths to increase coverage * linting * linting * mess with yaml loading in test * oddball cop for double quotes * use Rails public API for compatibility rather than instance_variable_set Co-authored-by: dfurber <dfurber@truecar.com>
1 parent 210f432 commit c10a857

19 files changed

Lines changed: 179 additions & 18 deletions

Appraisals

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,3 +24,8 @@ appraise "rails-6.1" do
2424
gem "rails", "~> 6.1.0"
2525
gem "rails-controller-testing", "~> 1.0.5"
2626
end
27+
28+
appraise "rails-7.0" do
29+
gem "rails", "~> 7.0.0"
30+
gem "rails-controller-testing", "~> 1.0.5"
31+
end

CHANGELOG.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,8 @@ recommendations of [keepachangelog.com](http://keepachangelog.com/).
1111

1212
### Added
1313

14+
- [#1365](https://github.com/paper-trail-gem/paper_trail/pull/1365) -
15+
Support Rails 7.0
1416
- [#1349](https://github.com/paper-trail-gem/paper_trail/pull/1349) -
1517
`if:` and `unless:` work with `touch` events now.
1618

Rakefile

Lines changed: 8 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -38,11 +38,8 @@ task :clean do
3838
end
3939
end
4040

41-
desc <<~EOS
42-
Write a database.yml for the specified RDBMS, and create database. Does not
43-
migrate. Migration happens later in spec_helper.
44-
EOS
45-
task prepare: %i[clean install_database_yml] do
41+
desc "Create the database."
42+
task :create_db do
4643
puts format("creating %s database", ENV["DB"])
4744
case ENV["DB"]
4845
when "mysql"
@@ -59,6 +56,12 @@ task prepare: %i[clean install_database_yml] do
5956
end
6057
end
6158

59+
desc <<~EOS
60+
Write a database.yml for the specified RDBMS, and create database. Does not
61+
migrate. Migration happens later in spec_helper.
62+
EOS
63+
task prepare: %i[clean install_database_yml create_db]
64+
6265
require "rspec/core/rake_task"
6366
desc "Run tests on PaperTrail with RSpec"
6467
task(:spec).clear

gemfiles/rails_7.0.gemfile

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
# This file was generated by Appraisal
2+
3+
source "https://rubygems.org"
4+
5+
gem "rails", "~> 7.0.0"
6+
gem "rails-controller-testing", "~> 1.0.5"
7+
8+
gemspec path: "../"

lib/paper_trail.rb

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,8 @@ module PaperTrail
2626
named created_at.
2727
EOS
2828

29+
RAILS_GTE_7_0 = ::ActiveRecord.gem_version >= ::Gem::Version.new("7.0.0")
30+
2931
extend PaperTrail::Cleaner
3032

3133
class << self

lib/paper_trail/attribute_serializers/cast_attribute_serializer.rb

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,12 @@ def deserialize(attr, val)
3232
if defined_enums[attr] && val.is_a?(::String)
3333
# Because PT 4 used to save the string version of enums to `object_changes`
3434
val
35+
elsif PaperTrail::RAILS_GTE_7_0 && val.is_a?(ActiveRecord::Type::Time::Value)
36+
# Because Rails 7 time attribute throws a delegation error when you deserialize
37+
# it with the factory.
38+
# See ActiveRecord::Type::Time::Value crashes when loaded from YAML on rails 7.0
39+
# https://github.com/rails/rails/issues/43966
40+
val.instance_variable_get(:@time)
3541
else
3642
AttributeSerializerFactory.for(@klass, attr).deserialize(val)
3743
end

lib/paper_trail/compatibility.rb

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@ module PaperTrail
1818
# versions.
1919
module Compatibility
2020
ACTIVERECORD_GTE = ">= 5.2" # enforced in gemspec
21-
ACTIVERECORD_LT = "< 7.0" # not enforced in gemspec
21+
ACTIVERECORD_LT = "< 7.1" # not enforced in gemspec
2222

2323
E_INCOMPATIBLE_AR = <<-EOS
2424
PaperTrail %s is not compatible with ActiveRecord %s. We allow PT

lib/paper_trail/version_concern.rb

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@ module VersionConcern
1616
extend ::ActiveSupport::Concern
1717

1818
included do
19-
belongs_to :item, polymorphic: true, optional: true
19+
belongs_to :item, polymorphic: true, optional: true, inverse_of: false
2020
validates_presence_of :event
2121
after_create :enforce_version_limit!
2222
end

spec/dummy_app/app/models/car.rb

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,4 +2,6 @@
22

33
class Car < Vehicle
44
has_paper_trail
5+
attribute :color, type: ActiveModel::Type::String
6+
attr_accessor :top_speed
57
end

spec/dummy_app/app/models/vegetable.rb

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22

33
# See also `Fruit` which uses `JsonVersion`.
44
class Vegetable < ActiveRecord::Base
5-
if ENV["DB"] == "postgres"
6-
has_paper_trail versions: { class_name: "JsonbVersion" }
7-
end
5+
has_paper_trail versions: {
6+
class_name: ENV["DB"] == "postgres" ? "JsonbVersion" : "PaperTrail::Version"
7+
}, on: %i[create update]
88
end

0 commit comments

Comments
 (0)