The controller and resource generators will now automatically produce asset...

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]
上级 bd032fe9
*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,
......
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
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
// Place all the behaviors and hooks related to the matching controller here.
// All this logic will automatically be available in application.js.
// 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/
/*
Place all the styles related to the matching controller here.
They will automatically be included in application.css.
*/
/*
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
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
......@@ -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"
......
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册