From b698d28c5d34eeafb375ddcadf4efd761d01fd19 Mon Sep 17 00:00:00 2001 From: "Marcos G. Zimmermann" Date: Mon, 5 Aug 2024 16:57:20 -0300 Subject: [PATCH] fix: fix specs for rails older than 6.x --- spec/support/models.rb | 47 +++++++++++++++++++++--------------------- 1 file changed, 23 insertions(+), 24 deletions(-) diff --git a/spec/support/models.rb b/spec/support/models.rb index d4708da..30da1ed 100644 --- a/spec/support/models.rb +++ b/spec/support/models.rb @@ -2,24 +2,31 @@ ActiveRecord::Base.logger = Logger.new($stdout) end -active_record_env = ActiveRecord::ConnectionHandling::DEFAULT_ENV.call - -ActiveRecord::Base.configurations = { - active_record_env => { - primary: { - adapter: 'sqlite3', - database: '/tmp/esse-active_record.db', - }, - secondary: { - adapter: 'sqlite3', - database: '/tmp/esse-active_record.db', - } - } +db_env = ActiveRecord::ConnectionHandling::DEFAULT_ENV.call.to_s +db_path = '/tmp/esse-active_record.db' +db_config = { + adapter: 'sqlite3', + database: db_path, } -if File.exist?('/tmp/esse-active_record.db') - File.delete('/tmp/esse-active_record.db') + +if File.exist?(db_path) + File.delete(db_path) +end + +if ::ActiveRecord.gem_version >= Gem::Version.new('6.0.0') + ActiveRecord::Base.configurations = { db_env => { primary: db_config, secondary: db_config } } + ActiveRecord::Base.establish_connection(:primary) + class ApplicationRecord < ActiveRecord::Base + self.abstract_class = true + connects_to database: { writing: :primary, reading: :secondary } + end +else + ActiveRecord::Base.configurations = { db_env => db_config } + ActiveRecord::Base.establish_connection(db_config) + class ApplicationRecord < ActiveRecord::Base + self.abstract_class = true + end end -ActiveRecord::Base.establish_connection(:primary) ActiveRecord::Schema.define do self.verbose = false @@ -45,14 +52,6 @@ end end -class ApplicationRecord < ActiveRecord::Base - self.abstract_class = true - - if ::ActiveRecord.gem_version >= Gem::Version.new('6.0.0') - connects_to database: { writing: :primary, reading: :secondary } - end -end - class Animal < ApplicationRecord end