From d304bd59c6be8cf6bd9d3d584cc3c1d5ac856aa9 Mon Sep 17 00:00:00 2001 From: Pavel Kalashnikov Date: Sun, 8 Sep 2024 04:02:32 +0400 Subject: [PATCH] Rails 7.2.0 (#1485) * Hotfix for support of Rails 7.2.0 * Change ActiveRecord compatibility versions * Change ruby versions in test workflow * Update README * Adjust deprecator definition to be more inline with the official API As per the comment from @bvogel here: https://github.com/paper-trail-gem/paper_trail/pull/1485#issuecomment-2324072109 * Update railtie.rb * Decreased test line-coverage by 0.01% --------- Co-authored-by: Tom Brammar <6734934+tbrammar@users.noreply.github.com> --- .github/workflows/test.yml | 4 ++-- Appraisals | 5 +++++ README.md | 2 +- gemfiles/rails_7.2.gemfile | 8 ++++++++ lib/paper_trail.rb | 4 ++++ lib/paper_trail/compatibility.rb | 2 +- lib/paper_trail/frameworks/rails/railtie.rb | 6 +++++- lib/paper_trail/model_config.rb | 4 ++-- paper_trail.gemspec | 2 +- spec/paper_trail/compatibility_spec.rb | 2 +- spec/paper_trail/model_config_spec.rb | 8 ++++---- spec/spec_helper.rb | 2 +- spec/support/paper_trail_spec_migrator.rb | 13 +++++++++---- 13 files changed, 44 insertions(+), 18 deletions(-) create mode 100644 gemfiles/rails_7.2.gemfile diff --git a/.github/workflows/test.yml b/.github/workflows/test.yml index 34444ad3..ea801042 100644 --- a/.github/workflows/test.yml +++ b/.github/workflows/test.yml @@ -59,11 +59,11 @@ jobs: # have set this up with each database as a separate job, but then we'd be # duplicating the matrix configuration three times. matrix: - gemfile: [ 'rails_6.1', 'rails_7.0', 'rails_7.1' ] + gemfile: [ 'rails_6.1', 'rails_7.0', 'rails_7.1', 'rails_7.2' ] # To keep matrix size down, only test highest and lowest rubies. # See "Lowest supported ruby version" in CONTRIBUTING.md - ruby: [ '3.0', '3.2' ] + ruby: [ '3.1', '3.3' ] steps: - name: Checkout source uses: actions/checkout@v4 diff --git a/Appraisals b/Appraisals index c0041883..a76050d0 100644 --- a/Appraisals +++ b/Appraisals @@ -23,3 +23,8 @@ appraise "rails-7.1" do gem "rails", "~> 7.1.0" gem "rails-controller-testing", "~> 1.0.5" end + +appraise "rails-7.2" do + gem "rails", "~> 7.2.0" + gem "rails-controller-testing", "~> 1.0.5" +end diff --git a/README.md b/README.md index 00760fad..88b2dca5 100644 --- a/README.md +++ b/README.md @@ -92,7 +92,7 @@ Choose version: | paper_trail | ruby | activerecord | |-------------|----------|---------------| -| unreleased | >= 3.0.0 | >= 6.1, < 7.2 | +| unreleased | >= 3.1.0 | >= 6.1, <= 7.2 | | 15 | >= 3.0.0 | >= 6.1, < 7.2 | | 14 | >= 2.7.0 | >= 6.0, < 7.1 | | 13 | >= 2.6.0 | >= 5.2, < 7.1 | diff --git a/gemfiles/rails_7.2.gemfile b/gemfiles/rails_7.2.gemfile new file mode 100644 index 00000000..a806930d --- /dev/null +++ b/gemfiles/rails_7.2.gemfile @@ -0,0 +1,8 @@ +# This file was generated by Appraisal + +source "https://rubygems.org" + +gem "rails", "~> 7.2.0" +gem "rails-controller-testing", "~> 1.0.5" + +gemspec path: "../" diff --git a/lib/paper_trail.rb b/lib/paper_trail.rb index b1fdaef3..88ba98aa 100644 --- a/lib/paper_trail.rb +++ b/lib/paper_trail.rb @@ -118,6 +118,10 @@ def version def active_record_gte_7_0? @active_record_gte_7_0 ||= ::ActiveRecord.gem_version >= ::Gem::Version.new("7.0.0") end + + def deprecator + @deprecator ||= ActiveSupport::Deprecation.new("16.0", "PaperTrail") + end end end diff --git a/lib/paper_trail/compatibility.rb b/lib/paper_trail/compatibility.rb index 57947005..820382b9 100644 --- a/lib/paper_trail/compatibility.rb +++ b/lib/paper_trail/compatibility.rb @@ -18,7 +18,7 @@ module PaperTrail # versions. module Compatibility ACTIVERECORD_GTE = ">= 6.1" # enforced in gemspec - ACTIVERECORD_LT = "< 7.2" # not enforced in gemspec + ACTIVERECORD_LT = "< 7.3" # not enforced in gemspec E_INCOMPATIBLE_AR = <<-EOS PaperTrail %s is not compatible with ActiveRecord %s. We allow PT diff --git a/lib/paper_trail/frameworks/rails/railtie.rb b/lib/paper_trail/frameworks/rails/railtie.rb index 2d93672b..607b85f8 100644 --- a/lib/paper_trail/frameworks/rails/railtie.rb +++ b/lib/paper_trail/frameworks/rails/railtie.rb @@ -10,7 +10,7 @@ class Railtie < ::Rails::Railtie # We specify `before: "load_config_initializers"` to ensure that the PT # initializer happens before "app initializers" (those defined in # the app's `config/initalizers`). - initializer "paper_trail", before: "load_config_initializers" do + initializer "paper_trail", before: "load_config_initializers" do |app| # `on_load` is a "lazy load hook". It "declares a block that will be # executed when a Rails component is fully loaded". (See # `active_support/lazy_load_hooks.rb`) @@ -25,6 +25,10 @@ class Railtie < ::Rails::Railtie ActiveSupport.on_load(:active_record) do require "paper_trail/frameworks/active_record" end + + if Gem::Version.new(::Rails::VERSION::STRING) >= Gem::Version.new("7.1") + app.deprecators[:paper_trail] = PaperTrail.deprecator + end end end end diff --git a/lib/paper_trail/model_config.rb b/lib/paper_trail/model_config.rb index 0f3b1ba0..652c8729 100644 --- a/lib/paper_trail/model_config.rb +++ b/lib/paper_trail/model_config.rb @@ -156,7 +156,7 @@ def check_version_class_name(options) # @api private - `version_class_name` @model_class.class_attribute :version_class_name if options[:class_name] - ::ActiveSupport::Deprecation.warn( + PaperTrail.deprecator.warn( format( DPR_CLASS_NAME_OPTION, class_name: options[:class_name].inspect @@ -192,7 +192,7 @@ def define_has_many_versions(options) def ensure_versions_option_is_hash(options) unless options[:versions].is_a?(Hash) if options[:versions] - ::ActiveSupport::Deprecation.warn( + PaperTrail.deprecator.warn( format( DPR_PASSING_ASSOC_NAME_DIRECTLY_TO_VERSIONS_OPTION, versions_name: options[:versions].inspect diff --git a/paper_trail.gemspec b/paper_trail.gemspec index 918f6f5a..08a8e412 100644 --- a/paper_trail.gemspec +++ b/paper_trail.gemspec @@ -56,7 +56,7 @@ has been destroyed. # PT supports request_store versions for 3 years. s.add_dependency "request_store", "~> 1.4" - s.add_development_dependency "appraisal", "~> 2.4.1" + s.add_development_dependency "appraisal", "~> 2.5" s.add_development_dependency "byebug", "~> 11.1" s.add_development_dependency "ffaker", "~> 2.20" s.add_development_dependency "generator_spec", "~> 0.9.4" diff --git a/spec/paper_trail/compatibility_spec.rb b/spec/paper_trail/compatibility_spec.rb index a3fe3aab..f503ea89 100644 --- a/spec/paper_trail/compatibility_spec.rb +++ b/spec/paper_trail/compatibility_spec.rb @@ -14,7 +14,7 @@ module PaperTrail context "when incompatible" do it "writes a warning to stderr" do - ar_version = ::Gem::Version.new("7.2.0") + ar_version = ::Gem::Version.new("8.0.0") expect { described_class.check_activerecord(ar_version) }.to output(/not compatible/).to_stderr diff --git a/spec/paper_trail/model_config_spec.rb b/spec/paper_trail/model_config_spec.rb index accb161b..d5e45fe7 100644 --- a/spec/paper_trail/model_config_spec.rb +++ b/spec/paper_trail/model_config_spec.rb @@ -7,14 +7,14 @@ module PaperTrail describe "has_paper_trail" do describe "versions:" do it "name can be passed instead of an options hash", :deprecated do - allow(::ActiveSupport::Deprecation).to receive(:warn) + allow(PaperTrail.deprecator).to receive(:warn) klass = Class.new(ApplicationRecord) do has_paper_trail versions: :drafts end expect(klass.reflect_on_association(:drafts)).to be_a( ActiveRecord::Reflection::HasManyReflection ) - expect(::ActiveSupport::Deprecation).to have_received(:warn).once.with( + expect(PaperTrail.deprecator).to have_received(:warn).once.with( a_string_starting_with("Passing versions association name"), array_including(/#{__FILE__}:/) ) @@ -78,14 +78,14 @@ module PaperTrail describe "class_name:" do it "can be used instead of versions: {class_name: ...}", :deprecated do - allow(::ActiveSupport::Deprecation).to receive(:warn) + allow(PaperTrail.deprecator).to receive(:warn) klass = Class.new(ApplicationRecord) do has_paper_trail class_name: "NoObjectVersion" end expect(klass.reflect_on_association(:versions).options[:class_name]).to eq( "NoObjectVersion" ) - expect(::ActiveSupport::Deprecation).to have_received(:warn).once.with( + expect(PaperTrail.deprecator).to have_received(:warn).once.with( a_string_starting_with("Passing Version class name"), array_including(/#{__FILE__}:/) ) diff --git a/spec/spec_helper.rb b/spec/spec_helper.rb index b562eb82..6f3011c9 100644 --- a/spec/spec_helper.rb +++ b/spec/spec_helper.rb @@ -7,7 +7,7 @@ SimpleCov.start do add_filter %w[Appraisals Gemfile Rakefile doc gemfiles spec] end -SimpleCov.minimum_coverage(ENV["DB"] == "postgres" ? 96.79 : 92.4) +SimpleCov.minimum_coverage(ENV["DB"] == "postgres" ? 96.71 : 92.4) require "byebug" require_relative "support/pt_arel_helpers" diff --git a/spec/support/paper_trail_spec_migrator.rb b/spec/support/paper_trail_spec_migrator.rb index f53903cb..ad87f89d 100644 --- a/spec/support/paper_trail_spec_migrator.rb +++ b/spec/support/paper_trail_spec_migrator.rb @@ -19,10 +19,15 @@ def initialize end def migrate - ::ActiveRecord::MigrationContext.new( - @migrations_path, - ::ActiveRecord::Base.connection.schema_migration - ).migrate + schema_migration = if Gem::Version.new(ActiveRecord::VERSION::STRING) < Gem::Version.new("7.2") + ::ActiveRecord::Base.connection.schema_migration + else + ::ActiveRecord::SchemaMigration.new( + ActiveRecord::Tasks::DatabaseTasks.migration_connection_pool + ) + end + + ::ActiveRecord::MigrationContext.new(@migrations_path, schema_migration).migrate end # Generate a migration, run it, and delete it. We use this for testing the