1
0
Fork 0
mirror of https://github.com/rails/rails.git synced 2022-11-09 12:12:34 -05:00

Move Coffee generators and templates to Coffee Railtie and create javascript_engine ("js") for apps that remove Coffee gem

This commit is contained in:
Santiago Pastorino 2011-06-27 21:54:03 -03:00
parent 4d256bc6b1
commit 539752a54c
10 changed files with 29 additions and 46 deletions

View file

@ -3,15 +3,6 @@ module Sprockets
# TODO: Get rid of config.assets.enabled
class Railtie < ::Rails::Railtie
def self.using_coffee?
require 'coffee-script'
defined?(CoffeeScript)
rescue LoadError
false
end
config.app_generators.javascript_engine :coffee if using_coffee?
rake_tasks do
load "sprockets/assets.rake"
end

View file

@ -53,7 +53,7 @@ module Rails
:helper => true,
:integration_tool => nil,
:javascripts => true,
:javascript_engine => nil,
:javascript_engine => :js,
:orm => false,
:performance_tool => nil,
:resource_controller => :controller,

View file

@ -0,0 +1,13 @@
require "rails/generators/named_base"
module Js
module Generators
class AssetsGenerator < Rails::Generators::NamedBase
source_root File.expand_path("../templates", __FILE__)
def copy_javascript
copy_file "javascript.js", File.join('app/assets/javascripts', class_path, "#{file_name}.js")
end
end
end
end

View file

@ -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.

View file

@ -11,7 +11,7 @@ source 'http://rubygems.org'
# in production environments by default.
group :assets do
gem 'sass-rails'
gem 'coffee-script'
gem 'coffee-rails'
gem 'uglifier'
end

View file

@ -7,21 +7,14 @@ module Rails
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
protected
def asset_name
file_name
end
def javascript_extension
options.javascript_engine.present? ?
"js.#{options.javascript_engine}" : "js"
hook_for :javascript_engine do |javascript_engine|
invoke javascript_engine, [name] if options[:javascripts]
end
hook_for :stylesheet_engine do |stylesheet_engine|

View file

@ -1,3 +0,0 @@
# 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/

View file

@ -8,19 +8,13 @@ class AssetsGeneratorTest < Rails::Generators::TestCase
def test_assets
run_generator
assert_file "app/assets/javascripts/posts.js.coffee"
assert_file "app/assets/javascripts/posts.js"
assert_file "app/assets/stylesheets/posts.css"
end
def test_skipping_assets
content = run_generator ["posts", "--no-stylesheets", "--no-javascripts"]
assert_no_file "app/assets/javascripts/posts.js.coffee"
assert_no_file "app/assets/javascripts/posts.js"
assert_no_file "app/assets/stylesheets/posts.css"
end
def test_vanilla_assets
run_generator ["posts", "--no-javascript-engine"]
assert_file "app/assets/javascripts/posts.js"
assert_file "app/assets/stylesheets/posts.css"
end
end

View file

@ -39,7 +39,7 @@ class ControllerGeneratorTest < Rails::Generators::TestCase
def test_invokes_assets
run_generator
assert_file "app/assets/javascripts/account.js.coffee"
assert_file "app/assets/javascripts/account.js"
assert_file "app/assets/stylesheets/account.css"
end

View file

@ -81,7 +81,7 @@ class ScaffoldGeneratorTest < Rails::Generators::TestCase
# Assets
assert_file "app/assets/stylesheets/scaffold.css"
assert_file "app/assets/javascripts/product_lines.js.coffee"
assert_file "app/assets/javascripts/product_lines.js"
assert_file "app/assets/stylesheets/product_lines.css"
end
@ -114,7 +114,7 @@ class ScaffoldGeneratorTest < Rails::Generators::TestCase
# Assets
assert_file "app/assets/stylesheets/scaffold.css", /:visited/
assert_no_file "app/assets/javascripts/product_lines.js.coffee"
assert_no_file "app/assets/javascripts/product_lines.js"
assert_no_file "app/assets/stylesheets/product_lines.css"
end
@ -190,7 +190,7 @@ class ScaffoldGeneratorTest < Rails::Generators::TestCase
# Assets
assert_file "app/assets/stylesheets/scaffold.css", /:visited/
assert_file "app/assets/javascripts/admin/roles.js.coffee"
assert_file "app/assets/javascripts/admin/roles.js"
assert_file "app/assets/stylesheets/admin/roles.css"
end
@ -224,7 +224,7 @@ class ScaffoldGeneratorTest < Rails::Generators::TestCase
# Assets
assert_file "app/assets/stylesheets/scaffold.css"
assert_no_file "app/assets/javascripts/admin/roles.js.coffee"
assert_no_file "app/assets/javascripts/admin/roles.js"
assert_no_file "app/assets/stylesheets/admin/roles.css"
end
@ -246,28 +246,21 @@ class ScaffoldGeneratorTest < Rails::Generators::TestCase
def test_scaffold_generator_no_assets
run_generator [ "posts", "--no-assets" ]
assert_file "app/assets/stylesheets/scaffold.css"
assert_no_file "app/assets/javascripts/posts.js.coffee"
assert_no_file "app/assets/javascripts/posts.js"
assert_no_file "app/assets/stylesheets/posts.css"
end
def test_scaffold_generator_no_stylesheets
run_generator [ "posts", "--no-stylesheets" ]
assert_no_file "app/assets/stylesheets/scaffold.css"
assert_file "app/assets/javascripts/posts.js.coffee"
assert_file "app/assets/javascripts/posts.js"
assert_no_file "app/assets/stylesheets/posts.css"
end
def test_scaffold_generator_no_javascripts
run_generator [ "posts", "--no-javascripts" ]
assert_file "app/assets/stylesheets/scaffold.css"
assert_no_file "app/assets/javascripts/posts.js.coffee"
assert_file "app/assets/stylesheets/posts.css"
end
def test_scaffold_generator_no_engines
run_generator [ "posts", "--no-javascript-engine" ]
assert_file "app/assets/stylesheets/scaffold.css"
assert_file "app/assets/javascripts/posts.js"
assert_no_file "app/assets/javascripts/posts.js"
assert_file "app/assets/stylesheets/posts.css"
end