Module: Merb::Orms::ActiveRecord

Defined in:
lib/merb/orms/active_record/connection.rb

Defined Under Namespace

Classes: Connect, DisconnectBeforeFork

Class Method Summary collapse

Class Method Details

.close_sandbox!Object

Rollback a transaction.

Used by Merb::Rack::Console#close_sandbox!



18
19
20
21
# File 'lib/merb/orms/active_record/connection.rb', line 18

def self.close_sandbox!
  ::ActiveRecord::Base.connection.rollback_db_transaction
  ::ActiveRecord::Base.send :decrement_open_transactions
end

.configObject

Raises:

  • (ArgumentError)


32
33
34
35
36
37
38
39
40
# File 'lib/merb/orms/active_record/connection.rb', line 32

def config
  #If Merb#runs_like specifies a differnt db env, use it.
  env_sym = (Merb.environment_info.nil?) ?
    Merb.environment.to_sym :
    Merb.environment_info[:db_env].to_sym

  raise ArgumentError, "missing environment :#{Merb.environment} in config file #{config_file}" unless configurations.key?(env_sym)
  @config ||= (Merb::Plugins.config[:merb_active_record] = configurations[env_sym])
end

.config_fileObject



24
# File 'lib/merb/orms/active_record/connection.rb', line 24

def config_file() Merb.dir_for(:config) / "database.yml" end

.configurationsObject



42
43
44
45
46
47
48
49
50
51
52
53
54
# File 'lib/merb/orms/active_record/connection.rb', line 42

def configurations
  @configurations ||=
    begin
      #A proc that will recursively intern(a.k.a symbolize) the keys of the hash
      intern_keys = lambda { |x|
        x.inject({}) do |y, (k,v)|
          y[k.to_sym || k] = v.is_a?(Hash) ? intern_keys.call(v) : v
          y
        end
      }
      intern_keys.call(Erubis.load_yaml_file(config_file))
    end
end

.connectObject

Database connects as soon as the gem is loaded



57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
# File 'lib/merb/orms/active_record/connection.rb', line 57

def connect
  if File.exists?(config_file)
    Merb.logger.info!("Connecting to database...")

    Thread.new{ loop{ sleep(60*60); ::ActiveRecord::Base.verify_active_connections! } }

    ::ActiveRecord::Base.logger = Merb.logger
    ::ActiveRecord::Base.configurations = configurations
    ::ActiveRecord::Base.establish_connection config
  else
    copy_sample_config
    Merb.logger.error! "No database.yml file found in #{Merb.root}/config."
    Merb.logger.error! "A sample file was created called database.yml.sample for you to copy and edit."
    exit(1)
  end
end

.copy_sample_configObject



28
29
30
# File 'lib/merb/orms/active_record/connection.rb', line 28

def copy_sample_config
  FileUtils.cp sample_source, sample_dest unless File.exists?(sample_dest)
end

.open_sandbox!Object

Start a transaction.

Used by Merb::Rack::Console#open_sandbox!



10
11
12
13
# File 'lib/merb/orms/active_record/connection.rb', line 10

def self.open_sandbox!
  ::ActiveRecord::Base.send :increment_open_transactions
  ::ActiveRecord::Base.connection.begin_db_transaction
end

.sample_destObject



25
# File 'lib/merb/orms/active_record/connection.rb', line 25

def sample_dest() Merb.dir_for(:config) / "database.yml.sample" end

.sample_sourceObject



26
# File 'lib/merb/orms/active_record/connection.rb', line 26

def sample_source() File.dirname(__FILE__) / "database.yml.sample" end