Skip to content
Browse files

Add --full option to 'plugin new' generator, which generates rails en…

…gine
  • Loading branch information...
1 parent fdbd9df commit 671d1469c6f70abb7cbd373bb37f18268ed7069c @drogus committed Oct 26, 2010
View
3 Gemfile
@@ -25,6 +25,9 @@ gem "memcache-client", ">= 1.8.5"
# AM
gem "text-format", "~> 1.0.0"
+# for 'plugin new --full' generator
+gem "capybara"
+
platforms :mri_18 do
gem "system_timer"
gem "ruby-debug", ">= 0.10.3"
View
5 railties/lib/rails/generators/app_base.rb
@@ -26,6 +26,11 @@ def self.add_shared_options_for(name)
class_option :skip_git, :type => :boolean, :aliases => "-G", :default => false,
:desc => "Skip Git ignores and keeps"
+ skip_active_record_desc = "Skip Active Record files"
+ skip_active_record_desc << " for dummy application" if name == "plugin"
+ class_option :skip_active_record, :type => :boolean, :aliases => "-O", :default => false,
+ :desc => skip_active_record_desc
+
class_option :dev, :type => :boolean, :default => false,
:desc => "Setup the #{name} with Gemfile pointing to your Rails checkout"
View
3 railties/lib/rails/generators/rails/app/app_generator.rb
@@ -165,9 +165,6 @@ class AppGenerator < AppBase
class_option :javascript, :type => :string, :aliases => "-j", :default => "prototype",
:desc => "Preconfigure for selected javascript library (options: #{JAVASCRIPTS.join('/')})"
- class_option :skip_active_record, :type => :boolean, :aliases => "-O", :default => false,
- :desc => "Skip Active Record files"
-
class_option :skip_test_unit, :type => :boolean, :aliases => "-T", :default => false,
:desc => "Skip Test::Unit files"
View
25 railties/lib/rails/generators/rails/plugin_new/plugin_new_generator.rb
@@ -1,3 +1,4 @@
+require 'active_support/core_ext/hash/slice'
require 'rails/generators/app_base'
require "rails/generators/rails/app/app_generator"
@@ -28,16 +29,26 @@ def gitignore
end
def lib
- directory "lib"
+ template "lib/%name%.rb"
+ if full?
+ template "lib/%name%/engine.rb"
+ end
end
def test
- directory "test"
+ template "test/test_helper.rb"
+ template "test/%name%_test.rb"
+ if full?
+ template "test/integration/navigation_test.rb"
+ template "test/support/integration_case.rb"
+ end
end
def generate_test_dummy
+ opts = (options || {}).slice("skip_active_record")
+
invoke Rails::Generators::AppGenerator,
- [ File.expand_path(dummy_path, destination_root) ], {}
+ [ File.expand_path(dummy_path, destination_root) ], opts
end
def test_dummy_config
@@ -96,6 +107,9 @@ class PluginNewGenerator < AppBase
alias_method :plugin_path, :app_path
+ class_option :full, :type => :boolean, :default => false,
+ :desc => "Generate rails engine with integration tests"
+
def initialize(*args)
raise Error, "Options should be given after the plugin name. For details run: rails plugin --help" if args[0].blank?
@@ -159,6 +173,10 @@ def bundle_if_dev_or_edge
protected
+ def full?
+ options[:full]
+ end
+
def self.banner
"rails plugin new #{self.arguments.map(&:usage).join(' ')} [options]"
end
@@ -183,6 +201,7 @@ def valid_const?
def application_definition
@application_definition ||= begin
+
dummy_application_path = File.expand_path("#{dummy_path}/config/application.rb", destination_root)
unless options[:pretend] || !File.exists?(dummy_application_path)
contents = File.read(dummy_application_path)
View
5 railties/lib/rails/generators/rails/plugin_new/templates/Gemfile
@@ -2,6 +2,11 @@ source "http://rubygems.org"
<%= rails_gemfile_entry -%>
+<% if full? -%>
+ gem "capybara", ">= 0.3.9"
+ gem "sqlite3-ruby", :require => "sqlite3"
+<% end -%>
+
if RUBY_VERSION < '1.9'
gem "ruby-debug", ">= 0.10.3"
end
View
6 railties/lib/rails/generators/rails/plugin_new/templates/lib/%name%.rb
@@ -0,0 +1,6 @@
+<% if full? %>
+require "<%= name %>/engine"
+
+<% end -%>
+module <%= camelized %>
+end
View
2 railties/lib/rails/generators/rails/plugin_new/templates/lib/%name%.rb.tt
@@ -1,2 +0,0 @@
-module <%= camelized %>
-end
View
4 railties/lib/rails/generators/rails/plugin_new/templates/lib/%name%/engine.rb
@@ -0,0 +1,4 @@
+module <%= camelized %>
+ class Engine < Rails::Engine
+ end
+end
View
8 railties/lib/rails/generators/rails/plugin_new/templates/rails/application.rb
@@ -1,6 +1,14 @@
require File.expand_path('../boot', __FILE__)
+<% unless options[:skip_active_record] -%>
require 'rails/all'
+<% else -%>
+# require "active_record/railtie"
+require "action_controller/railtie"
+require "action_mailer/railtie"
+require "active_resource/railtie"
+require "rails/test_unit/railtie"
+<% end -%>
Bundler.require
require "<%= name %>"
View
0 ...ugin_new/templates/test/%name%_test.rb.tt → .../plugin_new/templates/test/%name%_test.rb
File renamed without changes.
View
7 railties/lib/rails/generators/rails/plugin_new/templates/test/integration/navigation_test.rb
@@ -0,0 +1,7 @@
+require 'test_helper'
+
+class NagivationTest < ActiveSupport::IntegrationCase
+ test "truth" do
+ assert_kind_of Dummy::Application, Rails.application
+ end
+end
View
5 railties/lib/rails/generators/rails/plugin_new/templates/test/support/integration_case.rb
@@ -0,0 +1,5 @@
+# Define a bare test case to use with Capybara
+class ActiveSupport::IntegrationCase < ActiveSupport::TestCase
+ include Capybara
+ include Rails.application.routes.url_helpers
+end
View
16 railties/lib/rails/generators/rails/plugin_new/templates/test/test_helper.rb
@@ -6,5 +6,21 @@
Rails.backtrace_cleaner.remove_silencers!
+<% if full? -%>
+ActionMailer::Base.delivery_method = :test
+ActionMailer::Base.perform_deliveries = true
+ActionMailer::Base.default_url_options[:host] = "test.com"
+
+# Configure capybara for integration testing
+require "capybara/rails"
+Capybara.default_driver = :rack_test
+Capybara.default_selector = :css
+
+# Run any available migration from application
+ActiveRecord::Migrator.migrate File.expand_path("../dummy/db/migrate/", __FILE__)
+# and from engine
+ActiveRecord::Migrator.migrate File.expand_path("../../db/migrate/", __FILE__)
+<% end -%>
+
# Load support files
Dir["#{File.dirname(__FILE__)}/support/**/*.rb"].each { |f| require f }
View
28 railties/test/generators/plugin_new_generator_test.rb
@@ -41,15 +41,28 @@ def test_invalid_plugin_name_is_fixed
def test_generating_test_files
run_generator
assert_file "test/test_helper.rb"
-
assert_file "test/bukkits_test.rb", /assert_kind_of Module, Bukkits/
end
+ def test_generating_test_files_in_full_mode
+ run_generator [destination_root, "--full"]
+ assert_directory "test/support/"
+ assert_directory "test/integration/"
+
+ assert_file "test/integration/navigation_test.rb", /assert_kind_of Dummy::Application, Rails.application/
+ assert_file "test/support/integration_case.rb", /class ActiveSupport::IntegrationCase/
+ end
+
def test_ensure_that_plugin_options_are_not_passed_to_app_generator
FileUtils.cd(Rails.root)
assert_no_match /It works from file!.*It works_from_file/, run_generator([destination_root, "-m", "lib/template.rb"])
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"
+ end
+
def test_template_from_dir_pwd
FileUtils.cd(Rails.root)
assert_match /It works from file!/, run_generator([destination_root, "-m", "lib/template.rb"])
@@ -62,6 +75,19 @@ def test_ensure_that_tests_works
assert_match /1 tests, 1 assertions, 0 failures, 0 errors/, `bundle exec rake test`
end
+ def test_ensure_that_tests_works_in_full_mode
+ run_generator [destination_root, "--full"]
+ FileUtils.cd destination_root
+ `bundle install`
+ assert_match /2 tests, 2 assertions, 0 failures, 0 errors/, `bundle exec rake test`
+ end
+
+ def test_creating_engine_in_full_mode
+ run_generator [destination_root, "--full"]
+ assert_file "lib/bukkits/engine.rb", /module Bukkits\n class Engine < Rails::Engine\n end\nend/
+ assert_file "lib/bukkits.rb", /require "bukkits\/engine"/
+ end
+
protected
def action(*args, &block)

0 comments on commit 671d146

Please sign in to comment.
Something went wrong with that request. Please try again.