Permalink
Browse files

Initial commit

  • Loading branch information...
0 parents commit 9f3a81c9d297dcb7b8c8ded9b5f0de7cc2b96007 @tpope committed Jan 4, 2011
Showing with 93 additions and 0 deletions.
  1. +1 −0 .gitignore
  2. +20 −0 MIT-LICENSE
  3. +10 −0 README.markdown
  4. +8 −0 Rakefile
  5. +38 −0 lib/rails-default-database.rb
  6. +16 −0 rails-default-database.gemspec
@@ -0,0 +1 @@
+/pkg
@@ -0,0 +1,20 @@
+Copyright (c) Tim Pope
+
+Permission is hereby granted, free of charge, to any person obtaining
+a copy of this software and associated documentation files (the
+"Software"), to deal in the Software without restriction, including
+without limitation the rights to use, copy, modify, merge, publish,
+distribute, sublicense, and/or sell copies of the Software, and to
+permit persons to whom the Software is furnished to do so, subject to
+the following conditions:
+
+The above copyright notice and this permission notice shall be
+included in all copies or substantial portions of the Software.
+
+THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
+EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
+MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
+NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
+LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
+OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
+WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
@@ -0,0 +1,10 @@
+# Rails Default Database
+
+Tired of `cp config/database.example.yml config/database.yml`? Me too.
+That's why I wrote this Gem. Simply add this line to your Gemfile and
+PostgreSQL, MySQL, or SQLite3 (depending on which of the 3 is in your
+Gemfile) will be automatically configured with Rails defaults:
+
+ gem 'rails-default-database'
+
+You can still override the defaults by creating `config/database.yml`.
@@ -0,0 +1,8 @@
+begin; require 'rubygems'; rescue LoadError; end
+require 'rake'
+require 'rake/gempackagetask'
+
+spec = eval(File.read(File.join(File.dirname(__FILE__),'rails-default-database.gemspec')))
+Rake::GemPackageTask.new(spec) do |p|
+ p.gem_spec = spec
+end
@@ -0,0 +1,38 @@
+Rails::Application::Configuration.class_eval do
+
+ def database_configuration_with_default
+ database_configuration_without_default
+ rescue Errno::ENOENT
+ name = File.basename(root)
+ begin
+ require 'pg'
+ Hash.new do |h,env|
+ h[env] = {
+ 'adapter' => 'postgresql',
+ 'database' => "#{name}_#{env}"
+ }
+ end
+ rescue LoadError
+ begin
+ require 'mysql'
+ Hash.new do |h,env|
+ h[env] = {
+ 'adapter' => 'mysql',
+ 'username' => 'root',
+ 'database' => "#{name}_#{env}"
+ }
+ end
+ rescue LoadError
+ require 'sqlite3'
+ Hash.new do |h,env|
+ h[env] = {
+ 'adapter' => 'sqlite3',
+ 'database' => "db/#{env}.sqlite3"
+ }
+ end
+ end
+ end
+ end
+
+ alias_method_chain :database_configuration, :default
+end
@@ -0,0 +1,16 @@
+Gem::Specification.new do |s|
+ s.name = "rails-default-database"
+ s.version = "1.0.0"
+
+ s.summary = "Make database.yml optional in Rails"
+ s.description = "Provides a default database configuration for Rails applications that lack one"
+ s.authors = ["Tim Pope"]
+ s.email = "ruby@tpope.o"+'rg'
+ s.homepage = "http://github.com/tpope/rails-default-database"
+ s.files = [
+ "README.markdown",
+ "MIT-LICENSE",
+ "lib/rails-default-database.rb",
+ ]
+ s.add_runtime_dependency("railties")
+end

4 comments on commit 9f3a81c

@bogdan
bogdan commented on 9f3a81c Jan 11, 2011

Why not support all kind of example files in config dir?

e.g. sunspot.yml, resque.yml, whatever.yml

@tpope
Owner
tpope commented on 9f3a81c Jan 11, 2011

It doesn't work that way. It monkey patches AR to provide a default database.yml. I'd have to monkey patch every single library to provide a default whatever-that-library-needs.

@bogdan
bogdan commented on 9f3a81c Jan 11, 2011

Why not just 'cp' as you said in Readme?

Copy all .example files to .yml if they doesn't exists.

@tpope
Owner
tpope commented on 9f3a81c Jan 11, 2011

Convention over configuration. Copying an example file still means I need to create that damn example file (which is always the same for the same database, except the part that depends on the app name), and I need to count on other developers to create that example file for me, when in practice other developers are a bunch of assholes who can't even agree what that example file should be called.

Please sign in to comment.