Make generators more modular, add hooks and improve test suite.

This commit is contained in:
José Valim 2011-04-17 11:44:52 +02:00
parent 8f75c3abcd
commit 3a68aec1a1
11 changed files with 115 additions and 86 deletions

View File

@ -13,6 +13,7 @@ gem "rack-test", :git => "git://github.com/brynary/rack-test.git"
gem "sprockets", :git => "git://github.com/sstephenson/sprockets.git"
gem "coffee-script"
gem "sass", ">= 3.0"
gem "rake", ">= 0.8.7"
gem "mocha", ">= 0.9.8"

View File

@ -3,6 +3,23 @@ require "sprockets"
# TODO: Move this to sprockets-rails
# If so, we can move the require to a Gemfile and remove assets.enabled
class Sprockets::Railtie < Rails::Railtie
def self.using_coffee?
require 'coffee-script'
defined?(CoffeeScript)
rescue LoadError
false
end
def self.using_scss?
require 'sass'
defined?(Sass)
rescue LoadError
false
end
config.app_generators.javascript_engine :coffee if using_coffee?
config.app_generators.stylesheet_engine :scss if using_scss?
# Configure ActionController to use sprockets.
initializer "sprockets.set_configs", :after => "action_controller.set_configs" do |app|
ActiveSupport.on_load(:action_controller) do

View File

@ -24,9 +24,11 @@ module Rails
:rails => {
:actions => '-a',
:orm => '-o',
:javascript_engine => '-je',
:resource_controller => '-c',
:scaffold_controller => '-c',
:stylesheets => '-y',
:stylesheet_engine => '-se',
:template_engine => '-e',
:test_framework => '-t'
},
@ -43,15 +45,18 @@ module Rails
DEFAULT_OPTIONS = {
:rails => {
:assets => true,
:force_plural => false,
:helper => true,
:assets => true,
:orm => nil,
:integration_tool => nil,
:javascripts => true,
:javascript_engine => nil,
:orm => nil,
:performance_tool => nil,
:resource_controller => :controller,
:scaffold_controller => :scaffold_controller,
:stylesheets => true,
:stylesheet_engine => nil,
:test_framework => nil,
:template_engine => :erb
},

View File

@ -1,36 +1,38 @@
module Rails
module Generators
# TODO: Add hooks for using other asset pipelines, like Less
class AssetsGenerator < NamedBase
def create_asset_files
copy_file "javascript.#{javascript_extension}",
File.join('app/assets/javascripts', "#{file_name}.#{javascript_extension}")
class_option :javascripts, :type => :boolean, :desc => "Generate javascripts"
class_option :stylesheets, :type => :boolean, :desc => "Generate stylesheets"
class_option :javascript_engine, :desc => "Engine for javascripts"
class_option :stylesheet_engine, :desc => "Engine for stylesheets"
def create_javascript_files
return unless options.javascripts?
copy_file "javascript.#{javascript_extension}",
File.join('app/assets/javascripts', class_path, "#{asset_name}.#{javascript_extension}")
end
def create_stylesheet_files
return unless options.stylesheets?
copy_file "stylesheet.#{stylesheet_extension}",
File.join('app/assets/stylesheets', "#{file_name}.#{stylesheet_extension}")
File.join('app/assets/stylesheets', class_path, "#{asset_name}.#{stylesheet_extension}")
end
private
protected
def asset_name
file_name
end
def javascript_extension
using_coffee? ? "js.coffee" : "js"
options.javascript_engine.present? ?
"js.#{options.javascript_engine}" : "js"
end
def using_coffee?
require 'coffee-script'
defined?(CoffeeScript)
rescue LoadError
false
end
def stylesheet_extension
using_sass? ? "css.scss" : "css"
end
def using_sass?
require 'sass'
defined?(Sass)
rescue LoadError
false
options.stylesheet_engine.present? ?
"css.#{options.stylesheet_engine}" : "css"
end
end
end

View File

@ -6,8 +6,27 @@ module Rails
remove_hook_for :resource_controller
remove_class_option :actions
class_option :stylesheets, :type => :boolean, :desc => "Generate stylesheets"
class_option :stylesheet_engine, :desc => "Engine for stylesheets"
hook_for :scaffold_controller, :required => true
hook_for :stylesheets
def copy_stylesheets_file
if behavior == :invoke && options.stylesheets?
template "scaffold.#{stylesheet_extension}", "app/assets/stylesheets/scaffold.#{stylesheet_extension}"
end
end
hook_for :assets do |assets|
invoke assets, [controller_name]
end
private
def stylesheet_extension
options.stylesheet_engine.present? ?
"css.#{options.stylesheet_engine}" : "css"
end
end
end
end

View File

@ -1,5 +0,0 @@
Description:
Copies scaffold stylesheets to public/stylesheets/.
Examples:
`rails generate stylesheets`

View File

@ -1,23 +0,0 @@
module Rails
module Generators
class StylesheetsGenerator < Base
def copy_stylesheets_file
if behavior == :invoke
template "scaffold.#{stylesheet_extension}", "app/assets/stylesheets/scaffold.#{stylesheet_extension}"
end
end
private
def stylesheet_extension
using_sass? ? "css.scss" : "css"
end
def using_sass?
require 'sass'
defined?(Sass)
rescue LoadError
false
end
end
end
end

View File

@ -79,8 +79,10 @@ class ScaffoldGeneratorTest < Rails::Generators::TestCase
assert_file "app/helpers/product_lines_helper.rb"
assert_file "test/unit/helpers/product_lines_helper_test.rb"
# Stylesheets
assert_file "public/stylesheets/scaffold.css"
# Assets
assert_file "app/assets/stylesheets/scaffold.css.scss"
assert_file "app/assets/javascripts/product_lines.js.coffee"
assert_file "app/assets/stylesheets/product_lines.css.scss"
end
def test_scaffold_on_revoke
@ -110,8 +112,10 @@ class ScaffoldGeneratorTest < Rails::Generators::TestCase
assert_no_file "app/helpers/product_lines_helper.rb"
assert_no_file "test/unit/helpers/product_lines_helper_test.rb"
# Stylesheets (should not be removed)
assert_file "public/stylesheets/scaffold.css"
# Assets
assert_file "app/assets/stylesheets/scaffold.css.scss", /&:visited/
assert_no_file "app/assets/javascripts/product_lines.js.coffee"
assert_no_file "app/assets/stylesheets/product_lines.css.scss"
end
def test_scaffold_with_namespace_on_invoke
@ -184,8 +188,10 @@ class ScaffoldGeneratorTest < Rails::Generators::TestCase
assert_file "app/helpers/admin/roles_helper.rb"
assert_file "test/unit/helpers/admin/roles_helper_test.rb"
# Stylesheets
assert_file "public/stylesheets/scaffold.css"
# Assets
assert_file "app/assets/stylesheets/scaffold.css.scss", /&:visited/
assert_file "app/assets/javascripts/admin/roles.js.coffee"
assert_file "app/assets/stylesheets/admin/roles.css.scss"
end
def test_scaffold_with_namespace_on_revoke
@ -216,8 +222,10 @@ class ScaffoldGeneratorTest < Rails::Generators::TestCase
assert_no_file "app/helpers/admin/roles_helper.rb"
assert_no_file "test/unit/helpers/admin/roles_helper_test.rb"
# Stylesheets (should not be removed)
assert_file "public/stylesheets/scaffold.css"
# Assets
assert_file "app/assets/stylesheets/scaffold.css.scss"
assert_no_file "app/assets/javascripts/admin/roles.js.coffee"
assert_no_file "app/assets/stylesheets/admin/roles.css.scss"
end
def test_scaffold_generator_on_revoke_does_not_mutilate_legacy_map_parameter
@ -235,6 +243,34 @@ class ScaffoldGeneratorTest < Rails::Generators::TestCase
assert_file "config/routes.rb", /\.routes\.draw do\s*\|map\|\s*$/
end
def test_scaffold_generator_no_assets
run_generator [ "posts", "--no-assets" ]
assert_file "app/assets/stylesheets/scaffold.css.scss"
assert_no_file "app/assets/javascripts/posts.js.coffee"
assert_no_file "app/assets/stylesheets/posts.css.scss"
end
def test_scaffold_generator_no_stylesheets
run_generator [ "posts", "--no-stylesheets" ]
assert_no_file "app/assets/stylesheets/scaffold.css.scss"
assert_file "app/assets/javascripts/posts.js.coffee"
assert_no_file "app/assets/stylesheets/posts.css.scss"
end
def test_scaffold_generator_no_javascripts
run_generator [ "posts", "--no-javascripts" ]
assert_file "app/assets/stylesheets/scaffold.css.scss"
assert_no_file "app/assets/javascripts/posts.js.coffee"
assert_file "app/assets/stylesheets/posts.css.scss"
end
def test_scaffold_generator_no_negines
run_generator [ "posts", "--no-javascript-engine", "--no-stylesheet-engine" ]
assert_file "app/assets/stylesheets/scaffold.css"
assert_file "app/assets/javascripts/posts.js"
assert_file "app/assets/stylesheets/posts.css"
end
def test_scaffold_generator_outputs_error_message_on_missing_attribute_type
content = capture(:stderr) { run_generator ["post", "title:string", "body"]}
assert_match(/Missing type for attribute 'body'/, content)

View File

@ -1,23 +0,0 @@
require 'generators/generators_test_helper'
require 'rails/generators/rails/stylesheets/stylesheets_generator'
class StylesheetsGeneratorTest < Rails::Generators::TestCase
include GeneratorsTestHelper
def test_copy_scss_stylesheet
self.generator_class.any_instance.stubs(:using_sass?).returns(true)
run_generator
assert_file "app/assets/stylesheets/scaffold.css.scss"
end
def test_copy_css_stylesheet
run_generator
assert_file "app/assets/stylesheets/scaffold.css"
end
def test_stylesheets_are_not_deleted_on_revoke
run_generator
run_generator [], :behavior => :revoke
assert_file "app/assets/stylesheets/scaffold.css"
end
end