Module: UseDbPlugin

Included in:
UseDbTest
Defined in:
lib/use_db/use_db_plugin.rb

Defined Under Namespace

Modules: ClassMixin

Constant Summary collapse

@@use_dbs =

options can have one or the other of the following options:

:prefix - Specify the prefix to append to the RAILS_ENV when finding the adapter secification in database.yml
:suffix - Just like :prefix, only contactentated

OR

:adapter
:host
:username
:password

… etc … same as the options in establish_connection

Set the following to true in your test environment to enable extended debugging printing during testing … UseDbPlugin.debug_print = true

[ActiveRecord::Base]
@@debug_print =
false

Class Method Summary collapse

Instance Method Summary collapse

Class Method Details

.all_use_dbsObject



29
30
31
# File 'lib/use_db/use_db_plugin.rb', line 29

def self.all_use_dbs
	return @@use_dbs
end

.debug_printObject



33
34
35
# File 'lib/use_db/use_db_plugin.rb', line 33

def self.debug_print
	return @@debug_print
end

.debug_print=(newval) ⇒ Object



37
38
39
# File 'lib/use_db/use_db_plugin.rb', line 37

def self.debug_print=(newval)
	@@debug_print = newval
end

Instance Method Details

#get_use_db_conn_spec(options) ⇒ Object



47
48
49
50
51
52
53
54
55
56
57
58
59
60
# File 'lib/use_db/use_db_plugin.rb', line 47

def get_use_db_conn_spec(options)
	options.symbolize_keys!				#	without the bang, this was pointless
	suffix = options.delete(:suffix)
	prefix = options.delete(:prefix)
	rails_env = options.delete(:rails_env) || RAILS_ENV
	if (options[:adapter])
		return options
	else
		str = "#{prefix}#{rails_env}#{suffix}"
		connections = ActiveRecord::Base.configurations
		raise "Cannot find database specification.	Configuration '#{str}' expected in config/database.yml" if (connections[str].nil?)			
		return connections[str]
	end
end

#use_db(options) ⇒ Object



20
21
22
23
24
25
26
27
# File 'lib/use_db/use_db_plugin.rb', line 20

def use_db(options)
	options_dup = options.dup
	conn_spec = get_use_db_conn_spec(options)
	puts "Establishing connecting on behalf of #{self.to_s} to #{conn_spec.inspect}" if UseDbPlugin.debug_print
	establish_connection(conn_spec)
	extend ClassMixin
	@@use_dbs << self unless @@use_dbs.include?(self) || self.to_s.starts_with?("TestModel")
end