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 29 30 |
# File 'lib/sinatra/activerecord.rb', line 18 def self.registered(app) app.set :database_file, "#{Dir.pwd}/config/database.yml" if File.exist?("#{Dir.pwd}/config/database.yml") app.set :database, ENV['DATABASE_URL'] if ENV['DATABASE_URL'] unless defined?(Rake) || [:test, :production].include?(app.settings.environment) ActiveRecord::Base.logger = Logger.new(STDOUT) end app.helpers ActiveRecordHelper # re-connect if database connection dropped (Rails 3 only) app.before { ActiveRecord::Base.verify_active_connections! if ActiveRecord::Base.respond_to?(:verify_active_connections!) } app.use ActiveRecord::ConnectionAdapters::ConnectionManagement end |
Instance Method Details
#database ⇒ Object
49 50 51 |
# File 'lib/sinatra/activerecord.rb', line 49 def database ActiveRecord::Base end |
#database=(spec) ⇒ Object
38 39 40 41 42 43 44 45 46 47 |
# File 'lib/sinatra/activerecord.rb', line 38 def database=(spec) if spec.is_a?(Hash) and spec.symbolize_keys[environment.to_sym] ActiveRecord::Base.configurations = spec.stringify_keys ActiveRecord::Base.establish_connection(environment.to_sym) else ActiveRecord::Base.establish_connection(spec) ActiveRecord::Base.configurations ||= {} ActiveRecord::Base.configurations[environment.to_s] = ActiveRecord::Base.connection.pool.spec.config end end |
#database_file=(path) ⇒ Object
32 33 34 35 36 |
# File 'lib/sinatra/activerecord.rb', line 32 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 |