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 31 32 33 34 35 36 |
# File 'lib/sinatra/activerecord.rb', line 18 def self.registered(app) if ENV['DATABASE_URL'] app.set :database, ENV['DATABASE_URL'] elsif File.exist?("#{Dir.pwd}/config/database.yml") app.set :database_file, "#{Dir.pwd}/config/database.yml" end 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!) } if ActiveRecord::ConnectionAdapters.const_defined?(:ConnectionManagement) app.use ActiveRecord::ConnectionAdapters::ConnectionManagement end end |
Instance Method Details
#database ⇒ Object
58 59 60 |
# File 'lib/sinatra/activerecord.rb', line 58 def database ActiveRecord::Base end |
#database=(spec) ⇒ Object
44 45 46 47 48 49 50 51 52 53 54 55 56 |
# File 'lib/sinatra/activerecord.rb', line 44 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) elsif spec.is_a?(Hash) ActiveRecord::Base.configurations[environment.to_s] = spec.stringify_keys ActiveRecord::Base.establish_connection(spec.stringify_keys) else ActiveRecord::Base.establish_connection(spec) ActiveRecord::Base.configurations ||= {} ActiveRecord::Base.configurations[environment.to_s] = ActiveRecord::ConnectionAdapters::ConnectionSpecification::ConnectionUrlResolver.new(spec).to_hash end end |