Sha256: 471a4b15888c36055a8950a1c84f390db77d64c3ae8b5b51273036fea8893e43

Contents?: true

Size: 1.92 KB

Versions: 14

Compression:

Stored size: 1.92 KB

Contents

require 'erb'
ActiveRecord::Migration.verbose = false unless ENV.has_key?('DEBUG')

module ScopedSearch::RSpec::Database

  def self.establish_connection
    if ENV['DATABASE']
      self.establish_named_connection(ENV['DATABASE'])
    else
      self.establish_default_connection
    end
  end

  def self.test_databases_configuration
    file = if RUBY_PLATFORM == 'java'
      File.expand_path("../database.jruby.yml", File.dirname(__FILE__))
    else
      File.expand_path("../database.ruby.yml", File.dirname(__FILE__))
    end

    @database_connections ||= YAML.load(File.read(file))
  end
  
  def self.test_databases
    database_names = test_databases_configuration.keys.sort
    if ENV['EXCLUDE_DATABASE'].present?
      exclude_databases = ENV['EXCLUDE_DATABASE'].split(',')
      database_names -= exclude_databases
    end
    return database_names
  end

  def self.establish_named_connection(name)
    raise "#{name} database not configured" if test_databases_configuration[name.to_s].nil?
    ActiveRecord::Base.establish_connection(test_databases_configuration[name.to_s])
  end

  def self.establish_default_connection
    ActiveRecord::Base.establish_connection(:adapter => 'sqlite3', :database => ':memory:')
  end

  def self.close_connection
    ActiveRecord::Base.remove_connection
  end

  def self.create_model(fields)
    table_name = "model_#{rand}".gsub(/\W/, '')
    ActiveRecord::Migration.create_table(table_name) do |t|
      fields.each do |name, field_type|
        options = (field_type == :decimal) ? { :scale => 2, :precision => 10 } : {}
        t.send(field_type.to_s.gsub(/^unindexed_/, '').to_sym, name, options)
      end
    end

    klass = ScopedSearch::RSpec::Database.const_set(table_name.classify, Class.new(ActiveRecord::Base))
    klass.table_name = table_name
    yield(klass) if block_given?
    return klass
  end

  def self.drop_model(klass)
    ActiveRecord::Migration.drop_table(klass.table_name)
  end
end

Version data entries

14 entries across 14 versions & 1 rubygems

Version Path
scoped_search-3.0.1 spec/lib/database.rb
scoped_search-3.0.0 spec/lib/database.rb
scoped_search-2.7.1 spec/lib/database.rb
scoped_search-2.7.0 spec/lib/database.rb
scoped_search-2.6.5 spec/lib/database.rb
scoped_search-2.6.4 spec/lib/database.rb
scoped_search-2.6.3 spec/lib/database.rb
scoped_search-2.6.2 spec/lib/database.rb
scoped_search-2.6.1 spec/lib/database.rb
scoped_search-2.6.0 spec/lib/database.rb
scoped_search-2.5.1 spec/lib/database.rb
scoped_search-2.5.0 spec/lib/database.rb
scoped_search-2.4.1 spec/lib/database.rb
scoped_search-2.4.0 spec/lib/database.rb