Skip to content

Commit

Permalink
Move Coffee generators and templates to Coffee Railtie and create jav…
Browse files Browse the repository at this point in the history
…ascript_engine ("js") for apps that remove Coffee gem
  • Loading branch information
spastorino committed Jun 30, 2011
1 parent 4d256bc commit 539752a
Show file tree
Hide file tree
Showing 10 changed files with 29 additions and 46 deletions.
9 changes: 0 additions & 9 deletions actionpack/lib/sprockets/railtie.rb
Expand Up @@ -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
Expand Down
2 changes: 1 addition & 1 deletion railties/lib/rails/generators.rb
Expand Up @@ -53,7 +53,7 @@ module Generators
:helper => true,
:integration_tool => nil,
:javascripts => true,
:javascript_engine => nil,
:javascript_engine => :js,
:orm => false,
:performance_tool => nil,
:resource_controller => :controller,
Expand Down
13 changes: 13 additions & 0 deletions railties/lib/rails/generators/js/assets/assets_generator.rb
@@ -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
@@ -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.
2 changes: 1 addition & 1 deletion railties/lib/rails/generators/rails/app/templates/Gemfile
Expand Up @@ -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

Expand Down
11 changes: 2 additions & 9 deletions railties/lib/rails/generators/rails/assets/assets_generator.rb
Expand Up @@ -7,21 +7,14 @@ class AssetsGenerator < NamedBase
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|
Expand Down

This file was deleted.

10 changes: 2 additions & 8 deletions railties/test/generators/assets_generator_test.rb
Expand Up @@ -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
2 changes: 1 addition & 1 deletion railties/test/generators/controller_generator_test.rb
Expand Up @@ -39,7 +39,7 @@ def test_does_not_invoke_helper_if_required

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

Expand Down
21 changes: 7 additions & 14 deletions railties/test/generators/scaffold_generator_test.rb
Expand Up @@ -81,7 +81,7 @@ def test_scaffold_on_invoke

# 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

Expand Down Expand Up @@ -114,7 +114,7 @@ def test_scaffold_on_revoke

# 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

Expand Down Expand Up @@ -190,7 +190,7 @@ def test_scaffold_with_namespace_on_invoke

# 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

Expand Down Expand Up @@ -224,7 +224,7 @@ def test_scaffold_with_namespace_on_revoke

# 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

Expand All @@ -246,28 +246,21 @@ def test_scaffold_generator_on_revoke_does_not_mutilate_legacy_map_parameter
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

Expand Down

0 comments on commit 539752a

Please sign in to comment.