Skip to content

Commit

Permalink
Update scaffold controller to generate scss file if Sass is available
Browse files Browse the repository at this point in the history
  • Loading branch information
sikachu authored and dhh committed Apr 14, 2011
1 parent 93641ed commit 89884c1
Show file tree
Hide file tree
Showing 8 changed files with 89 additions and 9 deletions.
2 changes: 2 additions & 0 deletions railties/CHANGELOG
@@ -1,5 +1,7 @@
*Rails 3.1.0 (unreleased)*

* The scaffold controller will now produce SCSS file if Sass is available [Prem Sichanugrist]

* 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]
Expand Down
Expand Up @@ -822,7 +822,7 @@ The selectors to customize the style of error messages are:
* +#errorExplanation p+ - Style for the paragraph that holds the message that appears right below the header of the +div+ element.
* +#errorExplanation ul li+ - Style for the list items with individual error messages.

Scaffolding for example generates +public/stylesheets/scaffold.css+, which defines the red-based style you saw above.
Scaffolding for example generates +app/assets/stylesheets/scaffold.css.scss+, which later compiles to +app/assets/stylesheets/scaffold.css+ and defines the red-based style you saw above.

The name of the class and the id can be changed with the +:class+ and +:id+ options, accepted by both helpers.

Expand Down
4 changes: 2 additions & 2 deletions railties/guides/source/command_line.textile
Expand Up @@ -215,13 +215,13 @@ $ rails generate scaffold HighScore game:string score:integer
create app/views/layouts/
exists test/functional/
create test/unit/
create public/stylesheets/
create app/assets/stylesheets/
create app/views/high_scores/index.html.erb
create app/views/high_scores/show.html.erb
create app/views/high_scores/new.html.erb
create app/views/high_scores/edit.html.erb
create app/views/layouts/high_scores.html.erb
create public/stylesheets/scaffold.css
create app/assets/stylesheets/scaffold.css.scss
create app/controllers/high_scores_controller.rb
create test/functional/high_scores_controller_test.rb
create app/helpers/high_scores_helper.rb
Expand Down
2 changes: 1 addition & 1 deletion railties/guides/source/generators.textile
Expand Up @@ -190,7 +190,7 @@ $ rails generate scaffold User name:string
invoke test_unit
create test/unit/helpers/users_helper_test.rb
invoke stylesheets
create public/stylesheets/scaffold.css
create app/assets/stylesheets/scaffold.css.scss
</shell>

Looking at this output, it's easy to understand how generators work in Rails 3.0 and above. The scaffold generator doesn't actually generate anything, it just invokes others to do the work. This allows us to add/replace/remove any of those invocations. For instance, the scaffold generator invokes the scaffold_controller generator, which invokes erb, test_unit and helper generators. Since each generator has a single responsibility, they are easy to reuse, avoiding code duplication.
Expand Down
2 changes: 1 addition & 1 deletion railties/guides/source/getting_started.textile
Expand Up @@ -368,7 +368,7 @@ The scaffold generator will build 15 files in your application, along with some
|test/functional/posts_controller_test.rb |Functional testing harness for the posts controller|
|test/unit/helpers/posts_helper_test.rb |Unit testing harness for the posts helper|
|config/routes.rb |Edited to include routing information for posts|
|public/stylesheets/scaffold.css |Cascading style sheet to make the scaffolded views look better|
|app/assets/stylesheets/scaffold.css.scss |Cascading style sheet to make the scaffolded views look better|

h4. Running a Migration

Expand Down
Expand Up @@ -2,7 +2,21 @@ module Rails
module Generators
class StylesheetsGenerator < Base
def copy_stylesheets_file
template "scaffold.css", "app/assets/stylesheets/scaffold.css" if behavior == :invoke
if behavior == :invoke
template "scaffold.#{stylesheet_extension}", "app/assets/stylesheets/scaffold.#{stylesheet_extension}"
end
end

private
def stylesheet_extension
using_sass? ? "css.scss" : "css"
end

def using_sass?
require 'sass'
defined?(Sass)
rescue LoadError
false
end
end
end
Expand Down
@@ -0,0 +1,58 @@
body { background-color: #fff; color: #333; }

body, p, ol, ul, td {
font-family: verdana, arial, helvetica, sans-serif;
font-size: 13px;
line-height: 18px;
}

pre {
background-color: #eee;
padding: 10px;
font-size: 11px;
}

a {
color: #000;
&:visited { color: #666; }
&:hover { color: #fff; background-color:#000; }
}

div.field, div.actions {
margin-bottom: 10px;
}

#notice {
color: green;
}

.field_with_errors {
padding: 2px;
background-color: red;
display: table;
}

#error_explanation {
width: 450px;
border: 2px solid red;
padding: 7px;
padding-bottom: 0;
margin-bottom: 20px;
background-color: #f0f0f0;

h2 {
text-align: left;
font-weight: bold;
padding: 5px 5px 5px 15px;
font-size: 12px;
margin: -7px;
margin-bottom: 0px;
background-color: #c00;
color: #fff;
}

ul li {
font-size: 12px;
list-style: square;
}
}
12 changes: 9 additions & 3 deletions railties/test/generators/stylesheets_generator_test.rb
Expand Up @@ -4,14 +4,20 @@
class StylesheetsGeneratorTest < Rails::Generators::TestCase
include GeneratorsTestHelper

def test_copy_stylesheets
def test_copy_scss_stylesheet
self.generator_class.any_instance.stubs(:using_sass?).returns(true)
run_generator
assert_file "public/stylesheets/scaffold.css"
assert_file "app/assets/stylesheets/scaffold.css.scss"
end

def test_copy_css_stylesheet
run_generator
assert_file "app/assets/stylesheets/scaffold.css"
end

def test_stylesheets_are_not_deleted_on_revoke
run_generator
run_generator [], :behavior => :revoke
assert_file "public/stylesheets/scaffold.css"
assert_file "app/assets/stylesheets/scaffold.css"
end
end

0 comments on commit 89884c1

Please sign in to comment.