aboutsummaryrefslogtreecommitdiffstats
path: root/railties
diff options
context:
space:
mode:
authorPiotr Sarnacki <drogus@gmail.com>2010-11-02 14:23:45 +0100
committerPiotr Sarnacki <drogus@gmail.com>2010-11-02 17:14:53 +0100
commit2133495b8cdcb40a68a03aa786c4353031abe49e (patch)
tree0eacb22b97e5b53df1e14382b94269ddba58efe6 /railties
parentb6497d3b5a84ca5e7e15700419ddf44c096c57a2 (diff)
downloadrails-2133495b8cdcb40a68a03aa786c4353031abe49e.tar.gz
rails-2133495b8cdcb40a68a03aa786c4353031abe49e.tar.bz2
rails-2133495b8cdcb40a68a03aa786c4353031abe49e.zip
Properly handle other databases in 'plugin new' generator
Diffstat (limited to 'railties')
-rw-r--r--railties/lib/rails/generators/app_base.rb27
-rw-r--r--railties/lib/rails/generators/rails/app/app_generator.rb18
-rw-r--r--railties/lib/rails/generators/rails/app/templates/Gemfile4
-rw-r--r--railties/lib/rails/generators/rails/plugin_new/templates/Gemfile2
-rw-r--r--railties/test/generators/app_generator_test.rb5
-rw-r--r--railties/test/generators/plugin_new_generator_test.rb17
-rw-r--r--railties/test/generators/shared_generator_tests.rb5
7 files changed, 51 insertions, 27 deletions
diff --git a/railties/lib/rails/generators/app_base.rb b/railties/lib/rails/generators/app_base.rb
index 0bf5ee86b1..b7a4b16f10 100644
--- a/railties/lib/rails/generators/app_base.rb
+++ b/railties/lib/rails/generators/app_base.rb
@@ -108,6 +108,15 @@ module Rails
end
end
+ def database_gemfile_entry
+ entry = ""
+ unless options[:skip_active_record]
+ entry = "gem '#{gem_for_database}'"
+ entry << ", :require => '#{require_for_database}'" if require_for_database
+ end
+ entry
+ end
+
def rails_gemfile_entry
if options.dev?
<<-GEMFILE
@@ -133,6 +142,24 @@ gem 'rails', '#{Rails::VERSION::STRING}'
end
end
+ def gem_for_database
+ # %w( mysql oracle postgresql sqlite3 frontbase ibm_db )
+ case options[:database]
+ when "oracle" then "ruby-oci8"
+ when "postgresql" then "pg"
+ when "sqlite3" then "sqlite3-ruby"
+ when "frontbase" then "ruby-frontbase"
+ when "mysql" then "mysql2"
+ else options[:database]
+ end
+ end
+
+ def require_for_database
+ case options[:database]
+ when "sqlite3" then "sqlite3"
+ end
+ end
+
def bundle_if_dev_or_edge
bundle_command = File.basename(Thor::Util.ruby_command).sub(/ruby/, 'bundle')
run "#{bundle_command} install" if dev_or_edge?
diff --git a/railties/lib/rails/generators/rails/app/app_generator.rb b/railties/lib/rails/generators/rails/app/app_generator.rb
index e2d2ae71ba..7a6a7972d2 100644
--- a/railties/lib/rails/generators/rails/app/app_generator.rb
+++ b/railties/lib/rails/generators/rails/app/app_generator.rb
@@ -306,24 +306,6 @@ module Rails
ActiveSupport::SecureRandom.hex(64)
end
- def gem_for_database
- # %w( mysql oracle postgresql sqlite3 frontbase ibm_db )
- case options[:database]
- when "oracle" then "ruby-oci8"
- when "postgresql" then "pg"
- when "sqlite3" then "sqlite3-ruby"
- when "frontbase" then "ruby-frontbase"
- when "mysql" then "mysql2"
- else options[:database]
- end
- end
-
- def require_for_database
- case options[:database]
- when "sqlite3" then "sqlite3"
- end
- end
-
def mysql_socket
@mysql_socket ||= [
"/tmp/mysql.sock", # default
diff --git a/railties/lib/rails/generators/rails/app/templates/Gemfile b/railties/lib/rails/generators/rails/app/templates/Gemfile
index 4a37f675ad..86b9e8f40c 100644
--- a/railties/lib/rails/generators/rails/app/templates/Gemfile
+++ b/railties/lib/rails/generators/rails/app/templates/Gemfile
@@ -2,9 +2,7 @@ source 'http://rubygems.org'
<%= rails_gemfile_entry -%>
-<% unless options[:skip_active_record] -%>
-gem '<%= gem_for_database %>'<% if require_for_database %>, :require => '<%= require_for_database %>'<% end %>
-<% end -%>
+<%= database_gemfile_entry -%>
# Use unicorn as the web server
# gem 'unicorn'
diff --git a/railties/lib/rails/generators/rails/plugin_new/templates/Gemfile b/railties/lib/rails/generators/rails/plugin_new/templates/Gemfile
index 928ea8b9c4..29900c93dc 100644
--- a/railties/lib/rails/generators/rails/plugin_new/templates/Gemfile
+++ b/railties/lib/rails/generators/rails/plugin_new/templates/Gemfile
@@ -3,7 +3,7 @@ source "http://rubygems.org"
<%= rails_gemfile_entry -%>
<% if full? -%>
- gem "sqlite3-ruby", :require => "sqlite3"
+<%= database_gemfile_entry -%>
<% end -%>
if RUBY_VERSION < '1.9'
diff --git a/railties/test/generators/app_generator_test.rb b/railties/test/generators/app_generator_test.rb
index 42a49eb03c..ddd8272db6 100644
--- a/railties/test/generators/app_generator_test.rb
+++ b/railties/test/generators/app_generator_test.rb
@@ -53,11 +53,6 @@ class AppGeneratorTest < Rails::Generators::TestCase
assert_no_file "public/stylesheets/application.css"
end
- def test_invalid_database_option_raises_an_error
- content = capture(:stderr){ run_generator([destination_root, "-d", "unknown"]) }
- assert_match /Invalid value for \-\-database option/, content
- end
-
def test_invalid_application_name_raises_an_error
content = capture(:stderr){ run_generator [File.join(destination_root, "43-things")] }
assert_equal "Invalid application name 43-things. Please give a name which does not start with numbers.\n", content
diff --git a/railties/test/generators/plugin_new_generator_test.rb b/railties/test/generators/plugin_new_generator_test.rb
index 20c0d0a9bf..6b7095ba78 100644
--- a/railties/test/generators/plugin_new_generator_test.rb
+++ b/railties/test/generators/plugin_new_generator_test.rb
@@ -56,6 +56,23 @@ class PluginNewGeneratorTest < Rails::Generators::TestCase
assert_no_match /It works from file!.*It works_from_file/, run_generator([destination_root, "-m", "lib/template.rb"])
end
+ def test_database_entry_is_assed_by_default_in_full_mode
+ run_generator([destination_root, "--full"])
+ assert_file "test/dummy/config/database.yml", /sqlite/
+ assert_file "Gemfile", /^gem\s+["']sqlite3-ruby["'],\s+:require\s+=>\s+["']sqlite3["']$/
+ end
+
+ def test_config_another_database
+ run_generator([destination_root, "-d", "mysql", "--full"])
+ assert_file "test/dummy/config/database.yml", /mysql/
+ assert_file "Gemfile", /^gem\s+["']mysql2["']$/
+ end
+
+ def test_active_record_is_removed_from_frameworks_if_skip_active_record_is_given
+ run_generator [destination_root, "--skip-active-record"]
+ assert_file "test/dummy/config/application.rb", /#\s+require\s+["']active_record\/railtie["']/
+ end
+
def test_ensure_that_skip_active_record_option_is_passed_to_app_generator
run_generator [destination_root, "--skip_active_record"]
assert_no_file "test/dummy/config/database.yml"
diff --git a/railties/test/generators/shared_generator_tests.rb b/railties/test/generators/shared_generator_tests.rb
index 71cde6785c..054a3d008a 100644
--- a/railties/test/generators/shared_generator_tests.rb
+++ b/railties/test/generators/shared_generator_tests.rb
@@ -30,6 +30,11 @@ module SharedGeneratorTests
default_files.each{ |path| assert_no_file path }
end
+ def test_invalid_database_option_raises_an_error
+ content = capture(:stderr){ run_generator([destination_root, "-d", "unknown"]) }
+ assert_match /Invalid value for \-\-database option/, content
+ end
+
def test_options_before_application_name_raises_an_error
content = capture(:stderr){ run_generator(["--pretend", destination_root]) }
assert_match /Options should be given after the \w+ name. For details run: rails( plugin)? --help\n/, content