Skip to content

Commit

Permalink
Merge pull request #520 from gocardless/joesouthan-rails-71
Browse files Browse the repository at this point in the history
Fixup deprecation in Rails 7.2
  • Loading branch information
JoeSouthan authored Nov 3, 2023
2 parents a45a827 + 517b352 commit 9a4e17d
Show file tree
Hide file tree
Showing 14 changed files with 57 additions and 61 deletions.
22 changes: 12 additions & 10 deletions .github/workflows/tests.yml
Original file line number Diff line number Diff line change
Expand Up @@ -24,15 +24,16 @@ jobs:
strategy:
fail-fast: false
matrix:
ruby-version: ["2.7", "3.0", "3.1", "3.2"]
ruby-version: ["3.0", "3.1", "3.2"]
rails-version:
- "6.1.5"
- "7.0.4"
- "6.1.7.6"
- "7.0.8"
- "7.1.1"
- "main"
postgres-version: ["9.6", "11", "14"]
postgres-version: ["12", "13", "14", "15", "16"]
exclude:
- ruby-version: "3.2"
rails-version: "6.1.5"
rails-version: "6.1.7.6"
runs-on: ubuntu-latest
services:
postgres:
Expand Down Expand Up @@ -66,15 +67,16 @@ jobs:
strategy:
fail-fast: false
matrix:
ruby-version: ["2.7", "3.0", "3.1", "3.2"]
ruby-version: ["3.0", "3.1", "3.2"]
rails-version:
- "6.1.5"
- "7.0.4"
- "6.1.7.6"
- "7.0.8"
- "7.1.1"
- "main"
mysql-version: ["5.7", "8.0"]
mysql-version: ["8.0", "8.2"]
exclude:
- ruby-version: 3.2
rails-version: "6.1.5"
rails-version: "6.1.7.6"
runs-on: ubuntu-latest
services:
mysql:
Expand Down
2 changes: 1 addition & 1 deletion .ruby-version
Original file line number Diff line number Diff line change
@@ -1 +1 @@
3.2.0
3.2.2
9 changes: 9 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,3 +1,12 @@
## v11.0.0 3rd November 2023

### Changed
- Updated to support ActiveRecord > 7.2
- Remove support for:
- Ruby; 2.7
- Postgres; 9.6, 10, 11
- MySQL; 5.7

## v10.2.3 2nd Aug 2023

### Changed
Expand Down
4 changes: 2 additions & 2 deletions Gemfile
Original file line number Diff line number Diff line change
Expand Up @@ -9,8 +9,8 @@ if ENV['RAILS_VERSION'] == 'main'
elsif ENV['RAILS_VERSION']
gem "rails", "~> #{ENV['RAILS_VERSION']}"
end

group :development do
# test/unit is no longer bundled with Ruby 2.2, but required by Rails
gem "pry"
gem "test-unit", "~> 3.3" if Gem::Version.new(RUBY_VERSION) >= Gem::Version.new("2.2.0")
gem "test-unit", "~> 3.3"
end
9 changes: 2 additions & 7 deletions lib/statesman/adapters/active_record.rb
Original file line number Diff line number Diff line change
Expand Up @@ -246,13 +246,8 @@ def next_sort_key
end

def serialized?(transition_class)
if ::ActiveRecord.respond_to?(:gem_version) &&
::ActiveRecord.gem_version >= Gem::Version.new("4.2.0.a")
transition_class.type_for_attribute("metadata").
is_a?(::ActiveRecord::Type::Serialized)
else
transition_class.serialized_attributes.include?("metadata")
end
transition_class.type_for_attribute("metadata").
is_a?(::ActiveRecord::Type::Serialized)
end

def transition_conflict_error?(err)
Expand Down
6 changes: 5 additions & 1 deletion lib/statesman/adapters/active_record_transition.rb
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,11 @@ module ActiveRecordTransition
extend ActiveSupport::Concern

included do
serialize :metadata, JSON
if ::ActiveRecord.gem_version >= Gem::Version.new("7.1")
serialize :metadata, coder: JSON
else
serialize :metadata, JSON
end

class_attribute :updated_timestamp_column
self.updated_timestamp_column = DEFAULT_UPDATED_TIMESTAMP_COLUMN
Expand Down
2 changes: 1 addition & 1 deletion lib/statesman/version.rb
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
# frozen_string_literal: true

module Statesman
VERSION = "10.2.3"
VERSION = "11.0.0"
end
4 changes: 1 addition & 3 deletions spec/spec_helper.rb
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@ def connection_failure
ActiveRecord::Migration.verbose = false
end

config.before(:each, active_record: true) do
config.before(:each, :active_record) do
tables = %w[
my_active_record_models
my_active_record_model_transitions
Expand Down Expand Up @@ -82,7 +82,5 @@ def prepare_sti_transitions_table
CreateStiActiveRecordModelTransitionMigration.migrate(:up)
StiActiveRecordModelTransition.reset_column_information
end

MyNamespace::MyActiveRecordModelTransition.serialize(:metadata, JSON)
end
end
2 changes: 1 addition & 1 deletion spec/statesman/adapters/active_record_queries_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

require "spec_helper"

describe Statesman::Adapters::ActiveRecordQueries, active_record: true do
describe Statesman::Adapters::ActiveRecordQueries, :active_record do
def configure_old(klass, transition_class)
klass.define_singleton_method(:transition_class) { transition_class }
klass.define_singleton_method(:initial_state) { :initial }
Expand Down
38 changes: 11 additions & 27 deletions spec/statesman/adapters/active_record_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -5,12 +5,12 @@
require "statesman/adapters/shared_examples"
require "statesman/exceptions"

describe Statesman::Adapters::ActiveRecord, active_record: true do
describe Statesman::Adapters::ActiveRecord, :active_record do
before do
prepare_model_table
prepare_transitions_table

MyActiveRecordModelTransition.serialize(:metadata, JSON)
# MyActiveRecordModelTransition.serialize(:metadata, JSON)

prepare_sti_model_table
prepare_sti_transitions_table
Expand Down Expand Up @@ -38,15 +38,9 @@
allow(metadata_column).to receive_messages(sql_type: "")
allow(MyActiveRecordModelTransition).to receive_messages(columns_hash:
{ "metadata" => metadata_column })
if ActiveRecord.respond_to?(:gem_version) &&
ActiveRecord.gem_version >= Gem::Version.new("4.2.0.a")
expect(MyActiveRecordModelTransition).
to receive(:type_for_attribute).with("metadata").
and_return(ActiveRecord::Type::Value.new)
else
expect(MyActiveRecordModelTransition).
to receive_messages(serialized_attributes: {})
end
expect(MyActiveRecordModelTransition).
to receive(:type_for_attribute).with("metadata").
and_return(ActiveRecord::Type::Value.new)
end

it "raises an exception" do
Expand Down Expand Up @@ -91,18 +85,12 @@
allow(metadata_column).to receive_messages(sql_type: "jsonb")
allow(MyActiveRecordModelTransition).to receive_messages(columns_hash:
{ "metadata" => metadata_column })
if ActiveRecord.respond_to?(:gem_version) &&
ActiveRecord.gem_version >= Gem::Version.new("4.2.0.a")
serialized_type = ActiveRecord::Type::Serialized.new(
"", ActiveRecord::Coders::JSON
)
expect(MyActiveRecordModelTransition).
to receive(:type_for_attribute).with("metadata").
and_return(serialized_type)
else
expect(MyActiveRecordModelTransition).
to receive_messages(serialized_attributes: { "metadata" => "" })
end
serialized_type = ActiveRecord::Type::Serialized.new(
"", ActiveRecord::Coders::JSON
)
expect(MyActiveRecordModelTransition).
to receive(:type_for_attribute).with("metadata").
and_return(serialized_type)
end

it "raises an exception" do
Expand Down Expand Up @@ -467,10 +455,6 @@
CreateNamespacedARModelTransitionMigration.migrate(:up)
end

before do
MyNamespace::MyActiveRecordModelTransition.serialize(:metadata, JSON)
end

let(:observer) { double(Statesman::Machine, execute: nil) }
let(:model) do
MyNamespace::MyActiveRecordModel.create(current_state: :pending)
Expand Down
6 changes: 5 additions & 1 deletion spec/statesman/adapters/active_record_transition_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,11 @@

describe "including behaviour" do
it "calls Class.serialize" do
expect(transition_class).to receive(:serialize).with(:metadata, JSON).once
if Gem::Version.new(ActiveRecord::VERSION::STRING) >= Gem::Version.new("7.1")
expect(transition_class).to receive(:serialize).with(:metadata, coder: JSON).once
else
expect(transition_class).to receive(:serialize).with(:metadata, JSON).once
end
transition_class.send(:include, described_class)
end
end
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

require "spec_helper"

describe Statesman::Adapters::TypeSafeActiveRecordQueries, active_record: true do
describe Statesman::Adapters::TypeSafeActiveRecordQueries, :active_record do
def configure(klass, transition_class)
klass.send(:extend, described_class)
klass.configure_state_machine(
Expand Down
2 changes: 1 addition & 1 deletion spec/statesman/exceptions_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

require "spec_helper"

describe Statesman do
describe "Exceptions" do
describe "InvalidStateError" do
subject(:error) { Statesman::InvalidStateError.new }

Expand Down
10 changes: 5 additions & 5 deletions spec/support/active_record.rb
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,6 @@ class MyActiveRecordModelTransition < ActiveRecord::Base
include Statesman::Adapters::ActiveRecordTransition

belongs_to :my_active_record_model
serialize :metadata, JSON
end

class MyActiveRecordModel < ActiveRecord::Base
Expand All @@ -51,7 +50,11 @@ class MyActiveRecordModelTransitionWithoutInclude < ActiveRecord::Base
self.table_name = "my_active_record_model_transitions"

belongs_to :my_active_record_model
serialize :metadata, JSON
if ::ActiveRecord.gem_version >= Gem::Version.new("7.1")
serialize :metadata, coder: JSON
else
serialize :metadata, JSON
end
end

class CreateMyActiveRecordModelMigration < MIGRATION_CLASS
Expand Down Expand Up @@ -129,7 +132,6 @@ class OtherActiveRecordModelTransition < ActiveRecord::Base
include Statesman::Adapters::ActiveRecordTransition

belongs_to :other_active_record_model
serialize :metadata, JSON
end

class CreateOtherActiveRecordModelMigration < MIGRATION_CLASS
Expand Down Expand Up @@ -221,7 +223,6 @@ class MyActiveRecordModelTransition < ActiveRecord::Base

belongs_to :my_active_record_model,
class_name: "MyNamespace::MyActiveRecordModel"
serialize :metadata, JSON

def self.table_name_prefix
"my_namespace_"
Expand Down Expand Up @@ -310,7 +311,6 @@ class StiActiveRecordModelTransition < ActiveRecord::Base
include Statesman::Adapters::ActiveRecordTransition

belongs_to :sti_active_record_model
serialize :metadata, JSON
end

class StiAActiveRecordModelTransition < StiActiveRecordModelTransition
Expand Down

0 comments on commit 9a4e17d

Please sign in to comment.