From 3568af9911eca2083c1dbb8c0493f5bdad1e3e84 Mon Sep 17 00:00:00 2001 From: Ryan Woods Date: Mon, 8 May 2023 14:53:16 +0200 Subject: [PATCH] Make Spree::MigrationHelpers Ruby 3.0 compatible Due to the separation of positional and keyword arguments in Ruby 3.0, the options default value of a hash cannot be coerced into keyword arguments. --- core/lib/spree/migration_helpers.rb | 6 ++-- core/spec/lib/spree/migration_helpers_spec.rb | 33 +++++++++++++++++++ 2 files changed, 36 insertions(+), 3 deletions(-) create mode 100644 core/spec/lib/spree/migration_helpers_spec.rb diff --git a/core/lib/spree/migration_helpers.rb b/core/lib/spree/migration_helpers.rb index f3b07f1826f..6b7a027a089 100644 --- a/core/lib/spree/migration_helpers.rb +++ b/core/lib/spree/migration_helpers.rb @@ -6,9 +6,9 @@ def safe_remove_index(table, column) remove_index(table, column) if index_exists?(table, column) end - def safe_add_index(table, column, options = {}) - if columns_exist?(table, column) && !index_exists?(table, column, options) - add_index(table, column, options) + def safe_add_index(table, column, **options) + if columns_exist?(table, column) && !index_exists?(table, column, **options) + add_index(table, column, **options) end end diff --git a/core/spec/lib/spree/migration_helpers_spec.rb b/core/spec/lib/spree/migration_helpers_spec.rb new file mode 100644 index 00000000000..c868672fd86 --- /dev/null +++ b/core/spec/lib/spree/migration_helpers_spec.rb @@ -0,0 +1,33 @@ +require "rails_helper" + +RSpec.describe Spree::MigrationHelpers do + let(:helper) do + double.as_null_object.tap do |object| + object.extend(described_class) + object.extend(ActiveRecord::ConnectionAdapters::SchemaStatements) + end + end + + subject { helper.safe_add_index(double, double) } + + # regression test + describe "#safe_add_index" do + context "when the column exists" do + context "and the index does" do + it "passes compatible arguments to index_exists?" do + expect { subject }.to_not raise_error(ArgumentError) + end + end + + context "and the index does not" do + before do + allow(helper).to receive(:index_exists?).with(any_args).and_return(false) + end + + it "passes compatible arguments to add_index" do + expect { subject }.to_not raise_error(ArgumentError) + end + end + end + end +end