Skip to content

Commit ac3503c

Browse files
author
tomhuda
committed
Move SCSS generators and default templates from Rails to the Sass Railtie (d435726312601edb3ba6f97b34f562221f72c1f8).
* Sass gem registers a compressor * Sass gem registers generators for assets and scaffold * Create a default stylesheet_engine ("css") for apps that remove the Sass gem
1 parent 9701db1 commit ac3503c

File tree

12 files changed

+61
-124
lines changed

12 files changed

+61
-124
lines changed

actionpack/lib/sprockets/railtie.rb

Lines changed: 2 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -9,15 +9,7 @@ def self.using_coffee?
99
false
1010
end
1111

12-
def self.using_scss?
13-
require 'sass'
14-
defined?(Sass)
15-
rescue LoadError
16-
false
17-
end
18-
1912
config.app_generators.javascript_engine :coffee if using_coffee?
20-
config.app_generators.stylesheet_engine :scss if using_scss?
2113

2214
# Configure ActionController to use sprockets.
2315
initializer "sprockets.set_configs", :after => "action_controller.set_configs" do |app|
@@ -62,8 +54,8 @@ def asset_environment(app)
6254
env.static_root = File.join(app.root.join("public"), assets.prefix)
6355
env.paths.concat assets.paths
6456
env.logger = Rails.logger
65-
env.js_compressor = expand_js_compressor(assets.js_compressor) if app.assets.compress
66-
env.css_compressor = expand_css_compressor(assets.css_compressor) if app.assets.compress
57+
env.js_compressor = expand_js_compressor(assets.js_compressor) if assets.compress
58+
env.css_compressor = expand_css_compressor(assets.css_compressor) if assets.compress
6759
env
6860
end
6961

@@ -85,15 +77,6 @@ def expand_js_compressor(sym)
8577

8678
def expand_css_compressor(sym)
8779
case sym
88-
when :scss
89-
require 'sass'
90-
compressor = Object.new
91-
def compressor.compress(source)
92-
Sass::Engine.new(source,
93-
:syntax => :scss, :style => :compressed
94-
).render
95-
end
96-
compressor
9780
when :yui
9881
require 'yui/compressor'
9982
YUI::CssCompressor.new

railties/lib/rails/generators.rb

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -57,7 +57,7 @@ module Generators
5757
:resource_controller => :controller,
5858
:scaffold_controller => :scaffold_controller,
5959
:stylesheets => true,
60-
:stylesheet_engine => nil,
60+
:stylesheet_engine => :css,
6161
:test_framework => false,
6262
:template_engine => :erb
6363
},
Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
require "rails/generators/named_base"
2+
3+
module Css
4+
module Generators
5+
class AssetsGenerator < Rails::Generators::NamedBase
6+
source_root File.expand_path("../templates", __FILE__)
7+
8+
def copy_stylesheet
9+
copy_file "stylesheet.css", File.join('app/assets/stylesheets', class_path, "#{file_name}.css")
10+
end
11+
end
12+
end
13+
end
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,4 @@
1-
/*
1+
/*
22
Place all the styles related to the matching controller here.
33
They will automatically be included in application.css.
4-
You can use Sass (SCSS) here: http://sass-lang.com/
54
*/
Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
require "rails/generators/named_base"
2+
3+
module Css
4+
module Generators
5+
class ScaffoldGenerator < Rails::Generators::NamedBase
6+
# In order to allow the Sass generators to pick up the default Rails CSS and
7+
# transform it, we leave it in a standard location for the CSS stylesheet
8+
# generators to handle. For the simple, default case, just copy it over.
9+
def copy_stylesheet
10+
dir = Rails::Generators::ScaffoldGenerator.source_root
11+
file = File.join(dir, "scaffold.css")
12+
create_file "app/assets/stylesheets/scaffold.css", File.read(file)
13+
end
14+
end
15+
end
16+
end

railties/lib/rails/generators/rails/assets/assets_generator.rb

Lines changed: 2 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -13,12 +13,6 @@ def create_javascript_files
1313
File.join('app/assets/javascripts', class_path, "#{asset_name}.#{javascript_extension}")
1414
end
1515

16-
def create_stylesheet_files
17-
return unless options.stylesheets?
18-
copy_file "stylesheet.#{stylesheet_extension}",
19-
File.join('app/assets/stylesheets', class_path, "#{asset_name}.#{stylesheet_extension}")
20-
end
21-
2216
protected
2317

2418
def asset_name
@@ -30,9 +24,8 @@ def javascript_extension
3024
"js.#{options.javascript_engine}" : "js"
3125
end
3226

33-
def stylesheet_extension
34-
options.stylesheet_engine.present? ?
35-
"css.#{options.stylesheet_engine}" : "css"
27+
hook_for :stylesheet_engine do |stylesheet_engine|
28+
invoke stylesheet_engine, [name] if options[:stylesheets]
3629
end
3730
end
3831
end

railties/lib/rails/generators/rails/scaffold/scaffold_generator.rb

Lines changed: 2 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -11,21 +11,12 @@ class ScaffoldGenerator < ResourceGenerator #metagenerator
1111

1212
hook_for :scaffold_controller, :required => true
1313

14-
def copy_stylesheets_file
15-
if behavior == :invoke && options.stylesheets?
16-
template "scaffold.#{stylesheet_extension}", "app/assets/stylesheets/scaffold.#{stylesheet_extension}"
17-
end
18-
end
19-
2014
hook_for :assets do |assets|
2115
invoke assets, [controller_name]
2216
end
2317

24-
private
25-
26-
def stylesheet_extension
27-
options.stylesheet_engine.present? ?
28-
"css.#{options.stylesheet_engine}" : "css"
18+
hook_for :stylesheet_engine do |stylesheet_engine|
19+
invoke stylesheet_engine, [controller_name] if options[:stylesheets] && behavior == :invoke
2920
end
3021
end
3122
end

railties/lib/rails/generators/rails/scaffold/templates/scaffold.css.scss

Lines changed: 0 additions & 58 deletions
This file was deleted.

railties/test/generators/assets_generator_test.rb

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -9,17 +9,17 @@ class AssetsGeneratorTest < Rails::Generators::TestCase
99
def test_assets
1010
run_generator
1111
assert_file "app/assets/javascripts/posts.js.coffee"
12-
assert_file "app/assets/stylesheets/posts.css.scss"
12+
assert_file "app/assets/stylesheets/posts.css"
1313
end
1414

1515
def test_skipping_assets
1616
content = run_generator ["posts", "--no-stylesheets", "--no-javascripts"]
1717
assert_no_file "app/assets/javascripts/posts.js.coffee"
18-
assert_no_file "app/assets/stylesheets/posts.css.scss"
18+
assert_no_file "app/assets/stylesheets/posts.css"
1919
end
2020

2121
def test_vanilla_assets
22-
run_generator ["posts", "--no-javascript-engine", "--no-stylesheet-engine"]
22+
run_generator ["posts", "--no-javascript-engine"]
2323
assert_file "app/assets/javascripts/posts.js"
2424
assert_file "app/assets/stylesheets/posts.css"
2525
end

railties/test/generators/controller_generator_test.rb

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -40,7 +40,7 @@ def test_does_not_invoke_helper_if_required
4040
def test_invokes_assets
4141
run_generator
4242
assert_file "app/assets/javascripts/account.js.coffee"
43-
assert_file "app/assets/stylesheets/account.css.scss"
43+
assert_file "app/assets/stylesheets/account.css"
4444
end
4545

4646
def test_invokes_default_test_framework

0 commit comments

Comments
 (0)