Sha256: b9cdabea8f0b06ee52e7a25addbcb12ac9b435d67c122fd96784f9df067c831f

Contents?: true

Size: 1.34 KB

Versions: 8

Compression:

Stored size: 1.34 KB

Contents

module Apartment
  module Spec
    module Setup

      def self.included(base)
        base.instance_eval do
          let(:db1){ Apartment::Test.next_db }
          let(:db2){ Apartment::Test.next_db }
          let(:connection){ ActiveRecord::Base.connection }

          # This around ensures that we run these hooks before and after
          # any before/after hooks defined in individual tests
          # Otherwise these actually get run after test defined hooks
          around(:each) do |example|

            def config
              db = RSpec.current_example.metadata.fetch(:database, :postgresql)

              Apartment::Test.config['connections'][db.to_s].symbolize_keys
            end

            # before
            Apartment::Tenant.reload!(config)
            ActiveRecord::Base.establish_connection config

            example.run

            # after
            Rails.configuration.database_configuration = {}
            ActiveRecord::Base.clear_all_connections!

            Apartment.excluded_models.each do |model|
              klass = model.constantize

              Apartment.connection_class.remove_connection(klass)
              klass.clear_all_connections!
              klass.reset_table_name
            end
            Apartment.reset
            Apartment::Tenant.reload!
          end
        end
      end
    end
  end
end

Version data entries

8 entries across 8 versions & 2 rubygems

Version Path
ros-apartment-2.3.0 spec/support/setup.rb
ros-apartment-2.3.0.alpha2 spec/support/setup.rb
ros-apartment-2.3.0.alpha1 spec/support/setup.rb
apartment-2.2.1 spec/support/setup.rb
apartment-2.2.0 spec/support/setup.rb
apartment-2.1.0 spec/support/setup.rb
apartment-2.0.0 spec/support/setup.rb
apartment-1.2.0 spec/support/setup.rb