Permalink
Browse files

The controller and resource generators will now automatically produce…

… asset stubs (this can be turned off with --skip-assets). These stubs will use Coffee and Sass, if those libraries are available. [DHH]
  • Loading branch information...
1 parent bd032fe commit 6c91b699bf4c5b9288f8ac0136672d30e4989d7b @dhh dhh committed Apr 14, 2011
View
@@ -1,5 +1,7 @@
*Rails 3.1.0 (unreleased)*
+* The controller and resource generators will now automatically produce asset stubs (this can be turned off with --skip-assets). These stubs will use Coffee and Sass, if those libraries are available. [DHH]
+
* jQuery is the new default JavaScript library. [fxn]
* Changed scaffold and app generator to create Ruby 1.9 style hash when running on Ruby 1.9 [Prem Sichanugrist]
@@ -45,6 +45,7 @@ module Generators
:rails => {
:force_plural => false,
:helper => true,
+ :assets => true,
:orm => nil,
:integration_tool => nil,
:performance_tool => nil,
@@ -0,0 +1,20 @@
+Description:
+ Stubs out a new asset placeholders. Pass the asset name, either CamelCased
+ or under_scored.
+
+ To create assets within a folder, specify the assets name as a
+ path like 'parent/name'.
+
+ This generates a JavaScript stub in app/assets/javascripts and a stylesheet
+ stub in app/assets/stylesheets.
+
+ If CoffeeScript is available, JavaScripts will be generated with the .coffee extension.
+ If Sass 3 is available, stylesheets will be generated with the .css extension.
@micpringle
micpringle Apr 14, 2011

Shouldn't line 12 read

If Sass 3 is available, stylesheets will be generated with the .scss extension.
+
+Example:
+ `rails generate assets posts`
+
+ Posts assets.
+ Javascript: app/assets/javascripts/posts.js
+ Stylesheet: app/assets/stylesheets/posts.css
+
@@ -0,0 +1,37 @@
+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}")
+
+ copy_file "stylesheet.#{stylesheet_extension}",
+ File.join('app/assets/stylesheets', "#{file_name}.#{stylesheet_extension}")
+ end
+
+ private
+ def javascript_extension
+ using_coffee? ? "js.coffee" : "js"
+ end
+
+ def using_coffee?
+ require 'coffee-script'
+ defined?(CoffeeScript)
+ rescue LoadError
+ false
+ end
+
+ def stylesheet_extension
+ using_sass? ? "css.scss" : "css"
@dnch
dnch Apr 14, 2011

I take it this means that there won't be support for Sass's alternate syntax?

@dhh
dhh Apr 14, 2011 Member

No, we'll make it flexible enough that you can do Sass's alternate or Less or whatever else. This is just the first pass.

@josevalim
josevalim Apr 14, 2011 Member

Nothing that can't be changed in a patch written by one of the million of Rails users. ;)

@dnch
dnch Apr 14, 2011

Damn, now I don't get to piss and moan about an un-released change! Cheers guys.

+42

+ end
+
+ def using_sass?
+ require 'sass'
+ defined?(Sass)
+ rescue LoadError
+ false
+ end
+ end
+ end
+end
@@ -0,0 +1,2 @@
+// Place all the behaviors and hooks related to the matching controller here.
+// All this logic will automatically be available in application.js.
@@ -0,0 +1,3 @@
+// Place all the behaviors and hooks related to the matching controller here.
+// All this logic will automatically be available in application.js.
+// You can use CoffeeScript in this file: http://jashkenas.github.com/coffee-script/
@@ -0,0 +1,4 @@
+/*
+ Place all the styles related to the matching controller here.
+ They will automatically be included in application.css.
+*/
@@ -0,0 +1,5 @@
+/*
+ Place all the styles related to the matching controller here.
+ They will automatically be included in application.css.
+ You can use Sass (SCSS) here: http://sass-lang.com/
+*/
@@ -14,7 +14,7 @@ def add_routes
end
end
- hook_for :template_engine, :test_framework, :helper
+ hook_for :template_engine, :test_framework, :helper, :assets
end
end
end
@@ -0,0 +1,32 @@
+require 'generators/generators_test_helper'
+require 'rails/generators/rails/assets/assets_generator'
+
+# FOXME: Silence the 'Could not find task "using_coffee?"' message in tests due to the public stub
@wicz
wicz Apr 15, 2011

oops, FOXME? I guess it won't appear in rake notes, will it?

+class AssetsGeneratorTest < Rails::Generators::TestCase
+ include GeneratorsTestHelper
+ arguments %w(posts)
+
+ def test_vanilla_assets
+ run_generator
+ assert_file "app/assets/javascripts/posts.js"
@roelvanduijnhoven
roelvanduijnhoven Apr 16, 2011

Doesn't this test generate .js.coffe and .css.scss extensions in the case Coffeescript or Sass is available on the testing machine?

Obvious fix:
self.generator_class.any_instance.stubs(:using_coffee?).returns(false)
self.generator_class.any_instance.stubs(:using_sass?).returns(false)

@dhh
dhh Apr 16, 2011 Member

It doesn't because CS and Sass are not named in the Rails gemspec, so they're not in the load path.

+ assert_file "app/assets/stylesheets/posts.css"
+ end
+
+ def test_skipping_assets
+ content = run_generator ["posts", "--skip-assets"]
+ assert_no_file "app/assets/javascripts/posts.js"
+ assert_no_file "app/assets/stylesheets/posts.css"
+ end
+
+ def test_coffee_javascript
+ self.generator_class.any_instance.stubs(:using_coffee?).returns(true)
+ run_generator
+ assert_file "app/assets/javascripts/posts.js.coffee"
+ end
+
+ def test_sass_stylesheet
+ self.generator_class.any_instance.stubs(:using_sass?).returns(true)
+ run_generator
+ assert_file "app/assets/stylesheets/posts.css.scss"
+ end
+end
@@ -37,6 +37,12 @@ def test_does_not_invoke_helper_if_required
assert_no_file "test/unit/helpers/account_helper_test.rb"
end
+ def test_invokes_assets
+ run_generator
+ assert_file "app/assets/javascripts/account.js"
+ assert_file "app/assets/stylesheets/account.css"
+ end
+
def test_invokes_default_test_framework
run_generator
assert_file "test/functional/account_controller_test.rb"

0 comments on commit 6c91b69

Please sign in to comment.