From 3b21347810d662d8a9ff3c1d62f9b28f933b97d8 Mon Sep 17 00:00:00 2001 From: alpaca-tc Date: Mon, 11 Nov 2024 18:56:11 +0900 Subject: [PATCH] Support rails 8.0.0 (#260) * Support rails 8.0.0 * fix rubocop warning ``` lib/activerecord-multi-tenant/model_extensions.rb:69:37: C: [Corrected] Style/KeywordArgumentsMerging: Provide additional arguments directly rather than using merge. belongs_to tenant_name, **options.slice(:class_name, :inverse_of, :optional) ... ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ ``` --- .github/workflows/active-record-multi-tenant-tests.yml | 2 ++ Appraisals | 8 ++++++++ lib/activerecord-multi-tenant/model_extensions.rb | 7 +++++-- lib/activerecord-multi-tenant/query_rewriter.rb | 2 +- lib/activerecord-multi-tenant/relation_extension.rb | 4 ++-- spec/activerecord-multi-tenant/model_extensions_spec.rb | 2 +- 6 files changed, 19 insertions(+), 6 deletions(-) diff --git a/.github/workflows/active-record-multi-tenant-tests.yml b/.github/workflows/active-record-multi-tenant-tests.yml index f0b6de4..1a9d91b 100644 --- a/.github/workflows/active-record-multi-tenant-tests.yml +++ b/.github/workflows/active-record-multi-tenant-tests.yml @@ -54,11 +54,13 @@ jobs: - rails-7.0 - rails-7.1 - rails-7.2 + - rails-8.0 - active-record-6.0 - active-record-6.1 - active-record-7.0 - active-record-7.1 - active-record-7.2 + - active-record-8.0 citus_version: - '10' - '11' diff --git a/Appraisals b/Appraisals index b331bd3..e7b745b 100644 --- a/Appraisals +++ b/Appraisals @@ -20,6 +20,10 @@ appraise 'rails-7.2' do gem 'rails', '~> 7.2.0' end +appraise 'rails-8.0' do + gem 'rails', '~> 8.0.0' +end + appraise 'active-record-6.0' do gem 'activerecord', '~> 6.0.3' end @@ -39,3 +43,7 @@ end appraise 'active-record-7.2' do gem 'activerecord', '~> 7.2.0' end + +appraise 'active-record-8.0' do + gem 'activerecord', '~> 8.0.0' +end diff --git a/lib/activerecord-multi-tenant/model_extensions.rb b/lib/activerecord-multi-tenant/model_extensions.rb index 44fce9e..337f03b 100644 --- a/lib/activerecord-multi-tenant/model_extensions.rb +++ b/lib/activerecord-multi-tenant/model_extensions.rb @@ -66,8 +66,11 @@ def inherited(subclass) # Create an implicit belongs_to association only if tenant class exists if MultiTenant.tenant_klass_defined?(tenant_name, options) - belongs_to tenant_name, **options.slice(:class_name, :inverse_of, :optional) - .merge(foreign_key: options[:partition_key]) + belongs_to( + tenant_name, + **options.slice(:class_name, :inverse_of, :optional), + foreign_key: options[:partition_key] + ) end # New instances should have the tenant set diff --git a/lib/activerecord-multi-tenant/query_rewriter.rb b/lib/activerecord-multi-tenant/query_rewriter.rb index aa8aa86..e7e03c6 100644 --- a/lib/activerecord-multi-tenant/query_rewriter.rb +++ b/lib/activerecord-multi-tenant/query_rewriter.rb @@ -375,7 +375,7 @@ def relations_from_node_join(node_join) ActiveRecord::QueryMethods.prepend(MultiTenant::QueryMethodsExtensions) module MultiTenantFindBy - if ActiveRecord::VERSION::MAJOR >= 7 && ActiveRecord::VERSION::MINOR >= 2 + if ActiveRecord.gem_version >= Gem::Version.create('7.2.0') def cached_find_by_statement(connection, key, &block) return super unless respond_to?(:scoped_by_tenant?) && scoped_by_tenant? diff --git a/lib/activerecord-multi-tenant/relation_extension.rb b/lib/activerecord-multi-tenant/relation_extension.rb index 15e7149..2e3d5e3 100644 --- a/lib/activerecord-multi-tenant/relation_extension.rb +++ b/lib/activerecord-multi-tenant/relation_extension.rb @@ -21,7 +21,7 @@ def update_all(updates) stmt = Arel::UpdateManager.new stmt.table(table) - stmt.set Arel.sql(@klass.send(:sanitize_sql_for_assignment, updates)) + stmt.set Arel.sql(klass.send(:sanitize_sql_for_assignment, updates)) stmt.wheres = [generate_in_condition_subquery] klass.connection.update(stmt, "#{klass} Update All").tap { reset } @@ -39,7 +39,7 @@ def generate_in_condition_subquery # Build an Arel query arel = if eager_loading? apply_join_dependency.arel - elsif ActiveRecord::VERSION::MAJOR >= 7 && ActiveRecord::VERSION::MINOR >= 2 + elsif ActiveRecord.gem_version >= Gem::Version.create('7.2.0') build_arel(klass.connection) else build_arel diff --git a/spec/activerecord-multi-tenant/model_extensions_spec.rb b/spec/activerecord-multi-tenant/model_extensions_spec.rb index 6b9edb4..f2d83b8 100644 --- a/spec/activerecord-multi-tenant/model_extensions_spec.rb +++ b/spec/activerecord-multi-tenant/model_extensions_spec.rb @@ -132,7 +132,7 @@ def self.name end describe 'inspect method filters senstive column values' do - if ActiveRecord::VERSION::MAJOR >= 7 && ActiveRecord::VERSION::MINOR >= 2 + if ActiveRecord.gem_version >= Gem::Version.create('7.2.0') # related: https://github.com/rails/rails/pull/49765 around do |example| prev = Account.attributes_for_inspect