Module Switch
In: app/models/switch.rb

This is a mixin used by any other models that need to do work with non- RailsDB application databases.

Methods

Public Instance methods

This method wraps work done to alternate databases

[Source]

    # File app/models/switch.rb, line 39
39:   def switch( database )
40:     switch_db( database )
41:     begin
42:       begin
43:         yield
44:       rescue  ArgumentError
45:         raise "Argument error: #{ $!.to_s }"
46:       rescue ActiveRecord::StatementInvalid
47:         raise "#{ $!.to_s }"
48:       rescue Mysql::Error, NameError, PGError, TypeError
49:         raise "Database Error: #{ $!.to_s }"
50:       ensure
51:         switch_back
52:       end
53:     rescue NameError
54: 
55:     ensure
56:       switch_back
57:     end
58:   end

Switch RailsDB‘s database connection to a different database.

(Add newly discovered broken table names in config/environment.rb.)

This is magic:

[Source]

    # File app/models/switch.rb, line 14
14:   def switch_ar( database, name )
15:     switch( database ) do
16:       begin
17:         c = name.singularize.camelize.constantize
18:       rescue NameError
19:         klass = Class.new ActiveRecord::Base
20:         Object.const_set name.singularize.camelize, klass
21:         klass.set_table_name name
22:         begin
23:           c = name.singularize.camelize.constantize
24:         rescue NameError
25:           raise "NameError: Cannot constantize #{ name }"
26:         end
27:       end
28:       klass = Class.new ActiveRecord::Base
29:       class_name = ALT_TABLE_NAMES.include?( name ) ? ALT_TABLE_NAMES[ name ] : name
30:       Object.const_set class_name.singularize.camelize, klass
31:       klass.set_table_name name
32:       yield class_name.singularize.camelize.constantize
33:     end
34:   end

This method is used to re-establish the RailsDB application database. It‘s usually called after a call to switch_db.

[Source]

    # File app/models/switch.rb, line 87
87:   def switch_back
88:     ActiveRecord::Base.establish_connection( RAILS_ENV.to_sym )
89:   end

This method establishes a new ActiveRecord connection using the database passed. If you call this you need to finish by calling switch_back.

[Source]

    # File app/models/switch.rb, line 65
65:   def switch_db( database )
66:     options = { :adapter  => database.driver.name }
67:     case database.driver.name
68:       when  'sqlite3'
69:         options[:database] = database.path
70:       when  'mysql',
71:             'postgresql',
72:             'oracle'
73:         options[:database] = database.name
74:         options[:host]     = database.host
75:         options[:username] = database.username
76:         options[:password] = database.password
77:       else
78:         raise "#{ database.driver.name } driver not available"
79:     end
80:     ActiveRecord::Base.establish_connection( options )
81:   end

[Validate]