GitHub Sale: sign up for any paid plan this week and pay nothing until January 1, 2009!  [ hide ]

public
Fork of wycats/merb-plugins
Description: Merb Plugins: Even more modules to hook up your Merb installation
Homepage: http://www.merbivore.com
Clone URL: git://github.com/auser/merb-plugins.git
fabien (author)
Wed Mar 12 13:31:23 -0700 2008
commit  f3ec2c50a0695f6d3a135d28a2a552feac0b5d32
tree    8a5c184b2eb810cc75fe8e90473d6cffe8d1c333
parent  4024820c9f4b1832c65ad4f605513ad0ae5ae012
100644 73 lines (63 sloc) 2.573 kb
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
require 'fileutils'
require 'active_record'
 
module Merb
  module Orms
    module ActiveRecord
      
      # Start a transaction.
      #
      # Used by Merb::Rack::Console#open_sandbox!
      def self.open_sandbox!
        ::ActiveRecord::Base.send :increment_open_transactions
        ::ActiveRecord::Base.connection.begin_db_transaction
      end
      
      # Rollback a transaction.
      #
      # Used by Merb::Rack::Console#open_sandbox!
      def self.close_sandbox!
        ::ActiveRecord::Base.connection.rollback_db_transaction
        ::ActiveRecord::Base.send :decrement_open_transactions
      end
      
      class << self
        def config_file() Merb.dir_for(:config) / "database.yml" end
        def sample_dest() Merb.dir_for(:config) / "database.yml.sample" end
        def sample_source() File.dirname(__FILE__) / "database.yml.sample" end
      
        def copy_sample_config
          FileUtils.cp sample_source, sample_dest unless File.exists?(sample_dest)
        end
      
        def config
          @config ||=
            begin
              # Convert string keys to symbols
              full_config = Erubis.load_yaml_file(config_file)
              config = (Merb::Plugins.config[:merb_active_record] = {})
              (full_config[Merb.environment.to_sym] || full_config[Merb.environment]).each { |k, v| config[k.to_sym] = v }
               ::ActiveRecord::Base.configurations= full_config
              config
            end
        end
 
        # Database connects as soon as the gem is loaded
        def connect
          if File.exists?(config_file)
            Merb.logger.info("Connecting to database...")
 
            Thread.new{ loop{ sleep(60*60); ::ActiveRecord::Base.verify_active_connections! } }.priority = -10
 
            ::ActiveRecord::Base.verification_timeout = 14400
            ::ActiveRecord::Base.logger = Merb.logger
            ::ActiveRecord::Base.establish_connection config
          else
            copy_sample_config
            puts "No database.yml file found in #{Merb.root}/config."
            puts "A sample file was created called database.sample.yml for you to copy and edit."
            exit(1)
          end
        end
        
        # Registering this ORM lets the user choose active_record as a session
        # in merb.yml's session_store: option.
        def register_session_type
          Merb.register_session_type("activerecord",
          "merb/session/active_record_session",
          "Using ActiveRecord database sessions")
        end
      end
    end
  end
end