Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
Browse files

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

  • Loading branch information...
commit 3a68aec1a1c6329bfcbf0f87bdc2fbd75cb98feb 1 parent 8f75c3a
@josevalim josevalim authored
View
1  Gemfile
@@ -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
17 actionpack/lib/sprockets/railtie.rb
@@ -3,6 +3,23 @@
# 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
9 railties/lib/rails/generators.rb
@@ -24,9 +24,11 @@ module Generators
: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 Generators
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
48 railties/lib/rails/generators/rails/assets/assets_generator.rb
@@ -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
+ 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', "#{file_name}.#{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
- def javascript_extension
- using_coffee? ? "js.coffee" : "js"
+
+ protected
+
+ def asset_name
+ file_name
end
-
- def using_coffee?
- require 'coffee-script'
- defined?(CoffeeScript)
- rescue LoadError
- false
+
+ def javascript_extension
+ options.javascript_engine.present? ?
+ "js.#{options.javascript_engine}" : "js"
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
21 railties/lib/rails/generators/rails/scaffold/scaffold_generator.rb
@@ -6,8 +6,27 @@ class ScaffoldGenerator < ResourceGenerator #metagenerator
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
0  .../rails/stylesheets/templates/scaffold.css → ...ors/rails/scaffold/templates/scaffold.css
File renamed without changes
View
0  ...s/stylesheets/templates/scaffold.css.scss → ...ails/scaffold/templates/scaffold.css.scss
File renamed without changes
View
5 railties/lib/rails/generators/rails/stylesheets/USAGE
@@ -1,5 +0,0 @@
-Description:
- Copies scaffold stylesheets to public/stylesheets/.
-
-Examples:
- `rails generate stylesheets`
View
23 railties/lib/rails/generators/rails/stylesheets/stylesheets_generator.rb
@@ -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
52 railties/test/generators/scaffold_generator_test.rb
@@ -79,8 +79,10 @@ def test_scaffold_on_invoke
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 @@ def test_scaffold_on_revoke
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 @@ def test_scaffold_with_namespace_on_invoke
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 @@ def test_scaffold_with_namespace_on_revoke
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 @@ def test_scaffold_generator_on_revoke_does_not_mutilate_legacy_map_parameter
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
23 railties/test/generators/stylesheets_generator_test.rb
@@ -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
Please sign in to comment.
Something went wrong with that request. Please try again.