Skip to content

Commit

Permalink
Add --full option to 'plugin new' generator, which generates rails en…
Browse files Browse the repository at this point in the history
…gine
  • Loading branch information
drogus committed Nov 2, 2010
1 parent fdbd9df commit 671d146
Show file tree
Hide file tree
Showing 14 changed files with 108 additions and 9 deletions.
3 changes: 3 additions & 0 deletions Gemfile
Expand Up @@ -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"
Expand Down
5 changes: 5 additions & 0 deletions railties/lib/rails/generators/app_base.rb
Expand Up @@ -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"

Expand Down
3 changes: 0 additions & 3 deletions railties/lib/rails/generators/rails/app/app_generator.rb
Expand Up @@ -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"

Expand Down
@@ -1,3 +1,4 @@
require 'active_support/core_ext/hash/slice'
require 'rails/generators/app_base'
require "rails/generators/rails/app/app_generator"

Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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?

Expand Down Expand Up @@ -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
Expand All @@ -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)
Expand Down
Expand Up @@ -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
@@ -0,0 +1,6 @@
<% if full? %>
require "<%= name %>/engine"

<% end -%>
module <%= camelized %>
end

This file was deleted.

@@ -0,0 +1,4 @@
module <%= camelized %>
class Engine < Rails::Engine
end
end
@@ -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 %>"
Expand Down
@@ -0,0 +1,7 @@
require 'test_helper'

class NagivationTest < ActiveSupport::IntegrationCase
test "truth" do
assert_kind_of Dummy::Application, Rails.application
end
end
@@ -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
Expand Up @@ -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 }
28 changes: 27 additions & 1 deletion railties/test/generators/plugin_new_generator_test.rb
Expand Up @@ -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"])
Expand All @@ -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)
Expand Down

0 comments on commit 671d146

Please sign in to comment.