Module: Sinatra::ActiveRecordExtension

Defined in:
lib/sinatra/activerecord.rb

Class Method Summary collapse

Instance Method Summary collapse

Class Method Details

.registered(app) ⇒ Object



18
19
20
21
22
23
24
25
26
27
28
# File 'lib/sinatra/activerecord.rb', line 18

def self.registered(app)
  app.set :database, ENV['DATABASE_URL'] if ENV['DATABASE_URL']
  app.set :database_file, "#{Dir.pwd}/config/database.yml" if File.exists?("#{Dir.pwd}/config/database.yml")
  ActiveRecord::Base.logger = Logger.new(STDOUT)

  app.helpers ActiveRecordHelper

  # re-connect if database connection dropped
  app.before { ActiveRecord::Base.verify_active_connections! if ActiveRecord::Base.respond_to?(:verify_active_connections!) }
  app.after  { ActiveRecord::Base.clear_active_connections! }
end

Instance Method Details

#databaseObject



48
49
50
# File 'lib/sinatra/activerecord.rb', line 48

def database
  ActiveRecord::Base
end

#database=(spec) ⇒ Object



36
37
38
39
40
41
42
43
44
45
46
# File 'lib/sinatra/activerecord.rb', line 36

def database=(spec)
  if spec.is_a?(Hash) and spec.symbolize_keys[environment]
    ActiveRecord::Base.configurations = spec.stringify_keys
    ActiveRecord::Base.establish_connection(environment)
  else
    ActiveRecord::Base.establish_connection(spec)
    ActiveRecord::Base.configurations = {
      environment.to_s => ActiveRecord::Base.connection.pool.spec.config
    }
  end
end

#database_file=(path) ⇒ Object



30
31
32
33
34
# File 'lib/sinatra/activerecord.rb', line 30

def database_file=(path)
  path = File.join(root, path) if Pathname(path).relative? and root
  spec = YAML.load(ERB.new(File.read(path)).result) || {}
  set :database, spec
end