Sha256: 65fbbf1632226d80cabb4527593fa9159b8f73117f746a5fc3ad244f6d4874f2

Contents?: true

Size: 1.23 KB

Versions: 3

Compression:

Stored size: 1.23 KB

Contents

module Multiplicity
  module Adapters
    class ActiveRecord
      def self.connection_pool
        Thread.current[:multiplicity_connection_pool] ||= ::ActiveRecord::Base.connection_pool
      end

      def self.connection_pool=(pool)
        Thread.current[:multiplicity_connection_pool] = pool
      end

      def self.find_by(field, value)
        table   = Arel::Table.new(Multiplicity.table_name)
        columns = Multiplicity::Tenant.column_names.map{|col| table[col] }
        query   = table.where(table[field].eq(value)).project(columns)

        connection_pool.with_connection do |connection|
          connection.select_one query
        end
      end
    end
  end
end

begin
  gem 'activerecord', '>= 3.1'
rescue Gem::LoadError => e
  raise Gem::LoadError, "You are using functionality requiring
     the optional gem dependency `#{e.name}`, but the gem is not
     loaded, or is not using an acceptable version. Add
     `gem '#{e.name}'` to your Gemfile. Version #{Multiplicity::VERSION}
     of multiplicity requires #{e.name} that matches #{e.requirement}".gsub(/\n/, '').gsub(/\s+/, ' ')
end

require 'active_support/lazy_load_hooks'
ActiveSupport.on_load(:active_record) do
  Multiplicity.adapter = Multiplicity::Adapters::ActiveRecord
end

Version data entries

3 entries across 3 versions & 1 rubygems

Version Path
multiplicity-0.2.0 lib/multiplicity/adapters/active_record.rb
multiplicity-0.1.1 lib/multiplicity/adapters/active_record.rb
multiplicity-0.1.0 lib/multiplicity/adapters/active_record.rb