aboutsummaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
-rw-r--r--railties/CHANGELOG2
-rw-r--r--railties/lib/rails/generators.rb1
-rw-r--r--railties/lib/rails/generators/rails/assets/USAGE20
-rw-r--r--railties/lib/rails/generators/rails/assets/assets_generator.rb37
-rw-r--r--railties/lib/rails/generators/rails/assets/templates/javascript.js2
-rw-r--r--railties/lib/rails/generators/rails/assets/templates/javascript.js.coffee3
-rw-r--r--railties/lib/rails/generators/rails/assets/templates/stylesheet.css4
-rw-r--r--railties/lib/rails/generators/rails/assets/templates/stylesheet.css.scss5
-rw-r--r--railties/lib/rails/generators/rails/controller/controller_generator.rb2
-rw-r--r--railties/test/generators/assets_generator_test.rb32
-rw-r--r--railties/test/generators/controller_generator_test.rb6
11 files changed, 113 insertions, 1 deletions
diff --git a/railties/CHANGELOG b/railties/CHANGELOG
index e136dd0279..b4c774d508 100644
--- a/railties/CHANGELOG
+++ b/railties/CHANGELOG
@@ -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]
diff --git a/railties/lib/rails/generators.rb b/railties/lib/rails/generators.rb
index 29e693dfb0..be61b087f9 100644
--- a/railties/lib/rails/generators.rb
+++ b/railties/lib/rails/generators.rb
@@ -45,6 +45,7 @@ module Rails
:rails => {
:force_plural => false,
:helper => true,
+ :assets => true,
:orm => nil,
:integration_tool => nil,
:performance_tool => nil,
diff --git a/railties/lib/rails/generators/rails/assets/USAGE b/railties/lib/rails/generators/rails/assets/USAGE
new file mode 100644
index 0000000000..6667323dbc
--- /dev/null
+++ b/railties/lib/rails/generators/rails/assets/USAGE
@@ -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.
+
+Example:
+ `rails generate assets posts`
+
+ Posts assets.
+ Javascript: app/assets/javascripts/posts.js
+ Stylesheet: app/assets/stylesheets/posts.css
+
diff --git a/railties/lib/rails/generators/rails/assets/assets_generator.rb b/railties/lib/rails/generators/rails/assets/assets_generator.rb
new file mode 100644
index 0000000000..59239d1a03
--- /dev/null
+++ b/railties/lib/rails/generators/rails/assets/assets_generator.rb
@@ -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"
+ end
+
+ def using_sass?
+ require 'sass'
+ defined?(Sass)
+ rescue LoadError
+ false
+ end
+ end
+ end
+end
diff --git a/railties/lib/rails/generators/rails/assets/templates/javascript.js b/railties/lib/rails/generators/rails/assets/templates/javascript.js
new file mode 100644
index 0000000000..dee720facd
--- /dev/null
+++ b/railties/lib/rails/generators/rails/assets/templates/javascript.js
@@ -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.
diff --git a/railties/lib/rails/generators/rails/assets/templates/javascript.js.coffee b/railties/lib/rails/generators/rails/assets/templates/javascript.js.coffee
new file mode 100644
index 0000000000..09b2da094a
--- /dev/null
+++ b/railties/lib/rails/generators/rails/assets/templates/javascript.js.coffee
@@ -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/
diff --git a/railties/lib/rails/generators/rails/assets/templates/stylesheet.css b/railties/lib/rails/generators/rails/assets/templates/stylesheet.css
new file mode 100644
index 0000000000..7594abf268
--- /dev/null
+++ b/railties/lib/rails/generators/rails/assets/templates/stylesheet.css
@@ -0,0 +1,4 @@
+/*
+ Place all the styles related to the matching controller here.
+ They will automatically be included in application.css.
+*/
diff --git a/railties/lib/rails/generators/rails/assets/templates/stylesheet.css.scss b/railties/lib/rails/generators/rails/assets/templates/stylesheet.css.scss
new file mode 100644
index 0000000000..ba95e217cc
--- /dev/null
+++ b/railties/lib/rails/generators/rails/assets/templates/stylesheet.css.scss
@@ -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/
+*/
diff --git a/railties/lib/rails/generators/rails/controller/controller_generator.rb b/railties/lib/rails/generators/rails/controller/controller_generator.rb
index 9788c0d0bc..74aa0432a8 100644
--- a/railties/lib/rails/generators/rails/controller/controller_generator.rb
+++ b/railties/lib/rails/generators/rails/controller/controller_generator.rb
@@ -14,7 +14,7 @@ module Rails
end
end
- hook_for :template_engine, :test_framework, :helper
+ hook_for :template_engine, :test_framework, :helper, :assets
end
end
end
diff --git a/railties/test/generators/assets_generator_test.rb b/railties/test/generators/assets_generator_test.rb
new file mode 100644
index 0000000000..a2a40a28d4
--- /dev/null
+++ b/railties/test/generators/assets_generator_test.rb
@@ -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
+class AssetsGeneratorTest < Rails::Generators::TestCase
+ include GeneratorsTestHelper
+ arguments %w(posts)
+
+ def test_vanilla_assets
+ run_generator
+ assert_file "app/assets/javascripts/posts.js"
+ 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
diff --git a/railties/test/generators/controller_generator_test.rb b/railties/test/generators/controller_generator_test.rb
index be99dc068d..655d8ad450 100644
--- a/railties/test/generators/controller_generator_test.rb
+++ b/railties/test/generators/controller_generator_test.rb
@@ -37,6 +37,12 @@ class ControllerGeneratorTest < Rails::Generators::TestCase
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"