Module: Sinatra::ActiveRecordExtension
- Defined in:
- lib/sinatra/activerecord.rb
Class Method Summary collapse
Instance Method Summary collapse
Class Method Details
.registered(app) ⇒ Object
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 |
# File 'lib/sinatra/activerecord.rb', line 20 def self.registered(app) if ENV['DATABASE_URL'] && File.exist?("#{Dir.pwd}/config/database.yml") path = "#{Dir.pwd}/config/database.yml" url = ENV['DATABASE_URL'] file_path = File.join(root, path) if Pathname(path).relative? and root source = ERB.new(File.read(path)).result file_spec = YAML.respond_to?(:unsafe_load) ? YAML.unsafe_load(source) : YAML.load(source) file_spec ||= {} # ActiveRecord 6.1+ has moved the connection url resolver to another module if Gem.loaded_specs["activerecord"].version >= Gem::Version.create('6.1') url_spec = ActiveRecord::DatabaseConfigurations::ConnectionUrlResolver.new(url).to_hash else url_spec = ActiveRecord::ConnectionAdapters::ConnectionSpecification::ConnectionUrlResolver.new(url).to_hash end # if the configuration concerns only one database, and url_spec exist, url_spec will override the same key # if the configuration has multiple databases (Rails 6.0+ feature), url_spec is discarded # Following Active Record config convention # https://github.com/rails/rails/blob/main/activerecord/lib/active_record/database_configurations.rb#L169 final_spec = file_spec.keys.map do |env| config = file_spec[env] if config.is_a?(Hash) && config.all? { |_k, v| v.is_a?(Hash) } [env, config] else [env, config.merge(url_spec)] end end.to_h app.set :database, final_spec elsif 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!) } app.after { ActiveRecord::Base.clear_active_connections! } end |
Instance Method Details
#database ⇒ Object
97 98 99 |
# File 'lib/sinatra/activerecord.rb', line 97 def database ActiveRecord::Base end |
#database=(spec) ⇒ Object
75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 |
# File 'lib/sinatra/activerecord.rb', line 75 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 if Gem.loaded_specs["activerecord"].version >= Gem::Version.create('6.0') ActiveRecord::Base.configurations ||= ActiveRecord::DatabaseConfigurations.new({}).resolve(spec) else ActiveRecord::Base.configurations ||= {} ActiveRecord::Base.configurations[environment.to_s] = ActiveRecord::ConnectionAdapters::ConnectionSpecification::ConnectionUrlResolver.new(spec).to_hash end ActiveRecord::Base.establish_connection(spec) end end |
#database_file=(path) ⇒ Object
67 68 69 70 71 72 73 |
# File 'lib/sinatra/activerecord.rb', line 67 def database_file=(path) path = File.join(root, path) if Pathname(path).relative? and root source = ERB.new(File.read(path)).result spec = YAML.respond_to?(:unsafe_load) ? YAML.unsafe_load(source) : YAML.load(source) spec ||= {} set :database, spec end |