Merge pull request #1275 from paper-trail-gem/rails-6-1-jared

Rails 6.1
This commit is contained in:
Jared Beck 2020-12-15 21:15:56 -05:00 committed by GitHub
commit 799b590c2b
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
17 changed files with 72 additions and 24 deletions

View File

@ -51,26 +51,26 @@ Testing is a little awkward because the test suite:
### Test sqlite, AR 6 ### Test sqlite, AR 6
``` ```
DB=sqlite bundle exec appraisal ar-6.0 rake DB=sqlite bundle exec appraisal rails-6.0 rake
``` ```
### Test sqlite, AR 5 ### Test sqlite, AR 5
``` ```
DB=sqlite bundle exec appraisal ar-5.2 rake DB=sqlite bundle exec appraisal rails-5.2 rake
``` ```
### Test mysql, AR 5 ### Test mysql, AR 5
``` ```
DB=mysql bundle exec appraisal ar-5.2 rake DB=mysql bundle exec appraisal rails-5.2 rake
``` ```
### Test postgres, AR 5 ### Test postgres, AR 5
``` ```
createuser --superuser postgres createuser --superuser postgres
DB=postgres bundle exec appraisal ar-5.2 rake DB=postgres bundle exec appraisal rails-5.2 rake
``` ```
## Adding new schema ## Adding new schema

View File

@ -21,13 +21,16 @@ before_install:
- gem update bundler - gem update bundler
gemfile: gemfile:
- gemfiles/ar_5.2.gemfile - gemfiles/rails_5.2.gemfile
- gemfiles/ar_6.0.gemfile - gemfiles/rails_6.0.gemfile
- gemfiles/rails_6.1.gemfile
matrix: matrix:
exclude: exclude:
# rails 6 requires ruby >= 2.5.0 # rails 6 requires ruby >= 2.5.0
- rvm: 2.4 - rvm: 2.4
gemfile: gemfiles/ar_6.0.gemfile gemfile: gemfiles/rails_6.0.gemfile
- rvm: 2.4
gemfile: gemfiles/rails_6.1.gemfile
fast_finish: true fast_finish: true
services: services:
- mysql - mysql

View File

@ -10,12 +10,17 @@
# > https://github.com/thoughtbot/appraisal # > https://github.com/thoughtbot/appraisal
# #
# #
appraise "ar-5.2" do appraise "rails-5.2" do
gem "rails", "~> 5.2.4" gem "rails", "~> 5.2.4"
gem "rails-controller-testing", "~> 1.0.2" gem "rails-controller-testing", "~> 1.0.2"
end end
appraise "ar-6.0" do appraise "rails-6.0" do
gem "rails", "~> 6.0.3" gem "rails", "~> 6.0.3"
gem "rails-controller-testing", "~> 1.0.3" gem "rails-controller-testing", "~> 1.0.3"
end end
appraise "rails-6.1" do
gem "rails", "~> 6.1.0"
gem "rails-controller-testing", "~> 1.0.5"
end

View File

@ -11,7 +11,8 @@ recommendations of [keepachangelog.com](http://keepachangelog.com/).
### Added ### Added
- None - [#1272](https://github.com/paper-trail-gem/paper_trail/issues/1272) -
Rails 6.1 compatibility
### Fixed ### Fixed

View File

@ -83,7 +83,7 @@ has been destroyed.
| paper_trail | branch | ruby | activerecord | | paper_trail | branch | ruby | activerecord |
| -------------- | ---------- | -------- | ------------- | | -------------- | ---------- | -------- | ------------- |
| unreleased | master | >= 2.4.0 | >= 5.2, < 6.1 | | unreleased | master | >= 2.4.0 | >= 5.2, < 6.2 |
| 11 | master | >= 2.4.0 | >= 5.2, < 6.1 | | 11 | master | >= 2.4.0 | >= 5.2, < 6.1 |
| 10 | 10-stable | >= 2.3.0 | >= 4.2, < 6.1 | | 10 | 10-stable | >= 2.3.0 | >= 4.2, < 6.1 |
| 9 | 9-stable | >= 2.3.0 | >= 4.2, < 5.3 | | 9 | 9-stable | >= 2.3.0 | >= 4.2, < 5.3 |

View File

@ -0,0 +1,8 @@
# This file was generated by Appraisal
source "https://rubygems.org"
gem "rails", "~> 6.1.0"
gem "rails-controller-testing", "~> 1.0.5"
gemspec path: "../"

View File

@ -18,7 +18,7 @@ module PaperTrail
# versions. # versions.
module Compatibility module Compatibility
ACTIVERECORD_GTE = ">= 5.2" # enforced in gemspec ACTIVERECORD_GTE = ">= 5.2" # enforced in gemspec
ACTIVERECORD_LT = "< 6.1" # not enforced in gemspec ACTIVERECORD_LT = "< 6.2" # not enforced in gemspec
E_INCOMPATIBLE_AR = <<-EOS E_INCOMPATIBLE_AR = <<-EOS
PaperTrail %s is not compatible with ActiveRecord %s. We allow PT PaperTrail %s is not compatible with ActiveRecord %s. We allow PT

View File

@ -35,7 +35,7 @@ RSpec.describe Gadget, type: :model do
gadget.update_attribute(:updated_at, Time.now + 1) gadget.update_attribute(:updated_at, Time.now + 1)
}.to(change { gadget.versions.size }.by(1)) }.to(change { gadget.versions.size }.by(1))
expect( expect(
YAML.safe_load(gadget.versions.last.object_changes, [::Time]).keys YAML.load(gadget.versions.last.object_changes).keys
).to eq(["updated_at"]) ).to eq(["updated_at"])
end end
end end

View File

@ -27,7 +27,7 @@ RSpec.describe NoObject, versioning: true do
# New feature: destroy populates object_changes # New feature: destroy populates object_changes
# https://github.com/paper-trail-gem/paper_trail/pull/1123 # https://github.com/paper-trail-gem/paper_trail/pull/1123
h = YAML.safe_load(a["object_changes"], [::Time]) h = YAML.load a["object_changes"]
expect(h["id"]).to eq([n.id, nil]) expect(h["id"]).to eq([n.id, nil])
expect(h["letter"]).to eq([n.letter, nil]) expect(h["letter"]).to eq([n.letter, nil])
expect(h["created_at"][0]).to be_present expect(h["created_at"][0]).to be_present

View File

@ -14,7 +14,7 @@ module PaperTrail
context "when incompatible" do context "when incompatible" do
it "writes a warning to stderr" do it "writes a warning to stderr" do
ar_version = ::Gem::Version.new("6.1.0") ar_version = ::Gem::Version.new("6.2.0")
expect { expect {
described_class.check_activerecord(ar_version) described_class.check_activerecord(ar_version)
}.to output(/not compatible/).to_stderr }.to output(/not compatible/).to_stderr

View File

@ -30,7 +30,7 @@ module PaperTrail
matches = described_class. matches = described_class.
where_object_condition(PaperTrail::Version.arel_table[:object], :arg1, "Val 1") where_object_condition(PaperTrail::Version.arel_table[:object], :arg1, "Val 1")
expect(matches.instance_of?(Arel::Nodes::Matches)).to(eq(true)) expect(matches.instance_of?(Arel::Nodes::Matches)).to(eq(true))
expect(matches.right.val).to eq("%\"arg1\":\"Val 1\"%") expect(arel_value(matches.right)).to eq("%\"arg1\":\"Val 1\"%")
end end
end end
@ -39,7 +39,7 @@ module PaperTrail
matches = described_class. matches = described_class.
where_object_condition(PaperTrail::Version.arel_table[:object], :arg1, nil) where_object_condition(PaperTrail::Version.arel_table[:object], :arg1, nil)
expect(matches.instance_of?(Arel::Nodes::Matches)).to(eq(true)) expect(matches.instance_of?(Arel::Nodes::Matches)).to(eq(true))
expect(matches.right.val).to(eq("%\"arg1\":null%")) expect(arel_value(matches.right)).to(eq("%\"arg1\":null%"))
end end
end end
@ -48,9 +48,14 @@ module PaperTrail
grouping = described_class. grouping = described_class.
where_object_condition(PaperTrail::Version.arel_table[:object], :arg1, -3.5) where_object_condition(PaperTrail::Version.arel_table[:object], :arg1, -3.5)
expect(grouping.instance_of?(Arel::Nodes::Grouping)).to(eq(true)) expect(grouping.instance_of?(Arel::Nodes::Grouping)).to(eq(true))
matches = grouping.select { |v| v.instance_of?(Arel::Nodes::Matches) } disjunction = grouping.expr
expect(matches.first.right.val).to eq("%\"arg1\":-3.5,%") expect(disjunction).to be_an(Arel::Nodes::Or)
expect(matches.last.right.val).to eq("%\"arg1\":-3.5}%") dj_left = disjunction.left
expect(dj_left).to be_an(Arel::Nodes::Matches)
expect(arel_value(dj_left.right)).to eq("%\"arg1\":-3.5,%")
dj_right = disjunction.right
expect(dj_right).to be_an(Arel::Nodes::Matches)
expect(arel_value(dj_right.right)).to eq("%\"arg1\":-3.5}%")
end end
end end
end end

View File

@ -39,7 +39,7 @@ module PaperTrail
::PaperTrail::Version.arel_table[:object], :arg1, "Val 1" ::PaperTrail::Version.arel_table[:object], :arg1, "Val 1"
) )
expect(matches.instance_of?(Arel::Nodes::Matches)).to(eq(true)) expect(matches.instance_of?(Arel::Nodes::Matches)).to(eq(true))
expect(matches.right.val).to eq("%\narg1: Val 1\n%") expect(arel_value(matches.right)).to eq("%\narg1: Val 1\n%")
end end
end end
end end

View File

@ -4,6 +4,7 @@ ENV["RAILS_ENV"] ||= "test"
ENV["DB"] ||= "sqlite" ENV["DB"] ||= "sqlite"
require "byebug" require "byebug"
require_relative "support/pt_arel_helpers"
unless File.exist?(File.expand_path("dummy_app/config/database.yml", __dir__)) unless File.exist?(File.expand_path("dummy_app/config/database.yml", __dir__))
warn "No database.yml detected for the dummy app, please run `rake prepare` first" warn "No database.yml detected for the dummy app, please run `rake prepare` first"
@ -25,6 +26,7 @@ RSpec.configure do |config|
config.default_formatter = "doc" config.default_formatter = "doc"
end end
config.order = :random config.order = :random
config.include PTArelHelpers
Kernel.srand config.seed Kernel.srand config.seed
end end

View File

@ -1,5 +1,17 @@
# frozen_string_literal: true # frozen_string_literal: true
# AR 6.1 does not autoload MigrationContext, so we must `require` it.
#
# ```
# # lib/active_record.rb
# autoload :Migration
# autoload :Migrator, "active_record/migration"
# ```
#
# The above may indicate that we should use `Migrator` instead of
# MigrationContext.
require "active_record/migration"
# Manage migrations including running generators to build them, and cleaning up strays # Manage migrations including running generators to build them, and cleaning up strays
class PaperTrailSpecMigrator class PaperTrailSpecMigrator
def initialize def initialize
@ -31,12 +43,13 @@ class PaperTrailSpecMigrator
# - generator [String] - name of generator, eg. "paper_trail:update_sti" # - generator [String] - name of generator, eg. "paper_trail:update_sti"
# - generator_invoke_args [Array] - arguments to `Generators#invoke` # - generator_invoke_args [Array] - arguments to `Generators#invoke`
def generate_and_migrate(generator, generator_invoke_args) def generate_and_migrate(generator, generator_invoke_args)
files = generate(generator, generator_invoke_args) generate(generator, generator_invoke_args)
begin begin
migrate migrate
ensure ensure
files.each do |file| cmd = "git clean -x --force --quiet " + dummy_app_migrations_dir.to_s
File.delete(Rails.root.join(file)) unless system(cmd)
raise "Unable to clean up after PT migration generator test"
end end
end end
end end

View File

@ -0,0 +1,11 @@
# frozen_string_literal: true
module PTArelHelpers
def arel_value(node)
if node.respond_to?(:val) # rails < 6.1
node.val
else
node.value
end
end
end