railties 3.2.0.rc1 → 3.2.0.rc2
Sign up to get free protection for your applications and to get access to all the features.
- data/CHANGELOG.md +6 -0
- data/guides/assets/images/rails_guides_kindle_cover.jpg +0 -0
- data/guides/assets/stylesheets/kindle.css +11 -0
- data/guides/assets/stylesheets/main.css +8 -0
- data/guides/rails_guides/generator.rb +87 -20
- data/guides/rails_guides/helpers.rb +16 -0
- data/guides/source/3_2_release_notes.textile +452 -0
- data/guides/source/_license.html.erb +2 -0
- data/guides/source/_welcome.html.erb +19 -0
- data/guides/source/active_record_querying.textile +1 -1
- data/guides/source/active_support_core_extensions.textile +0 -10
- data/guides/source/asset_pipeline.textile +69 -67
- data/guides/source/caching_with_rails.textile +22 -0
- data/guides/source/documents.yaml +153 -0
- data/guides/source/getting_started.textile +2 -4
- data/guides/source/index.html.erb +14 -175
- data/guides/source/kindle/KINDLE.md +26 -0
- data/guides/source/kindle/copyright.html.erb +1 -0
- data/guides/source/kindle/layout.html.erb +27 -0
- data/guides/source/kindle/rails_guides.opf.erb +52 -0
- data/guides/source/kindle/toc.html.erb +24 -0
- data/guides/source/kindle/toc.ncx.erb +64 -0
- data/guides/source/kindle/welcome.html.erb +5 -0
- data/guides/source/layout.html.erb +11 -47
- data/guides/source/migrations.textile +1 -1
- data/lib/rails/application/bootstrap.rb +1 -1
- data/lib/rails/application/route_inspector.rb +10 -1
- data/lib/rails/generators/app_base.rb +3 -3
- data/lib/rails/generators/base.rb +3 -3
- data/lib/rails/generators/generated_attribute.rb +57 -3
- data/lib/rails/generators/named_base.rb +2 -3
- data/lib/rails/generators/rails/migration/migration_generator.rb +1 -1
- data/lib/rails/generators/rails/model/model_generator.rb +1 -1
- data/lib/rails/generators/rails/scaffold/USAGE +15 -9
- data/lib/rails/generators/test_case.rb +2 -2
- data/lib/rails/plugin.rb +1 -0
- data/lib/rails/tasks/documentation.rake +1 -1
- data/lib/rails/test_unit/sub_test_task.rb +36 -0
- data/lib/rails/test_unit/testing.rake +10 -50
- data/lib/rails/version.rb +1 -1
- metadata +132 -64
@@ -0,0 +1,19 @@
|
|
1
|
+
<h2>Ruby on Rails Guides (<%= @version %>)</h2>
|
2
|
+
|
3
|
+
<% if @edge %>
|
4
|
+
<p>
|
5
|
+
These are <b>Edge Guides</b>, based on the current <a href="https://github.com/rails/rails/tree/<%= @version %>">master</a> branch.
|
6
|
+
</p>
|
7
|
+
<p>
|
8
|
+
If you are looking for the ones for the stable version, please check
|
9
|
+
<a href="http://guides.rubyonrails.org">http://guides.rubyonrails.org</a> instead.
|
10
|
+
</p>
|
11
|
+
<% else %>
|
12
|
+
<p>
|
13
|
+
These are the new guides for Rails 3.1 based on <a href="https://github.com/rails/rails/tree/<%= @version %>"><%= @version %></a>.
|
14
|
+
These guides are designed to make you immediately productive with Rails, and to help you understand how all of the pieces fit together.
|
15
|
+
</p>
|
16
|
+
<% end %>
|
17
|
+
<p>
|
18
|
+
The guides for Rails 2.3.x are available at <a href="http://guides.rubyonrails.org/v2.3.11/">http://guides.rubyonrails.org/v2.3.11/</a>.
|
19
|
+
</p>
|
@@ -966,7 +966,7 @@ When a +lambda+ is used for a +scope+, it can take arguments:
|
|
966
966
|
|
967
967
|
<ruby>
|
968
968
|
class Post < ActiveRecord::Base
|
969
|
-
scope :1_week_before, lambda { |time| where("created_at < ?", time)
|
969
|
+
scope :1_week_before, lambda { |time| where("created_at < ?", time) }
|
970
970
|
end
|
971
971
|
</ruby>
|
972
972
|
|
@@ -2053,16 +2053,6 @@ end
|
|
2053
2053
|
|
2054
2054
|
NOTE: Defined in +active_support/core_ext/enumerable.rb+.
|
2055
2055
|
|
2056
|
-
h4. +pluck+
|
2057
|
-
|
2058
|
-
The +pluck+ method collects the value of the passed method for each element and returns the result as an array.
|
2059
|
-
|
2060
|
-
<ruby>
|
2061
|
-
people.pluck(:name) # => [ "David Heinemeier Hansson", "Jamie Heinemeier Hansson" ]
|
2062
|
-
</ruby>
|
2063
|
-
|
2064
|
-
NOTE: Defined in +active_support/core_ext/enumerable.rb+.
|
2065
|
-
|
2066
2056
|
h4. +each_with_object+
|
2067
2057
|
|
2068
2058
|
The +inject+ method offers iteration with an accumulator:
|
@@ -6,7 +6,7 @@ By referring to this guide you will be able to:
|
|
6
6
|
* Understand what the asset pipeline is and what it does
|
7
7
|
* Properly organize your application assets
|
8
8
|
* Understand the benefits of the asset pipeline
|
9
|
-
*
|
9
|
+
* Add a pre-processor to the pipeline
|
10
10
|
* Package assets with a gem
|
11
11
|
|
12
12
|
endprologue.
|
@@ -17,7 +17,7 @@ The asset pipeline provides a framework to concatenate and minify or compress Ja
|
|
17
17
|
|
18
18
|
Prior to Rails 3.1 these features were added through third-party Ruby libraries such as Jammit and Sprockets. Rails 3.1 is integrated with Sprockets through Action Pack which depends on the +sprockets+ gem, by default.
|
19
19
|
|
20
|
-
|
20
|
+
Making the asset pipeline a core feature of Rails means that all developers can benefit from the power of having their assets pre-processed, compressed and minified by one central library, Sprockets. This is part of Rails' "fast by default" strategy as outlined by DHH in his keynote at RailsConf 2011.
|
21
21
|
|
22
22
|
In Rails 3.1, the asset pipeline is enabled by default. It can be disabled in +config/application.rb+ by putting this line inside the application class definition:
|
23
23
|
|
@@ -25,34 +25,34 @@ In Rails 3.1, the asset pipeline is enabled by default. It can be disabled in +c
|
|
25
25
|
config.assets.enabled = false
|
26
26
|
</ruby>
|
27
27
|
|
28
|
-
You can also disable
|
28
|
+
You can also disable the asset pipeline while creating a new application by passing the <tt>--skip-sprockets</tt> option.
|
29
29
|
|
30
30
|
<plain>
|
31
31
|
rails new appname --skip-sprockets
|
32
32
|
</plain>
|
33
33
|
|
34
|
-
|
34
|
+
You should use the defaults for all new applications unless you have a specific reason to avoid the asset pipeline.
|
35
35
|
|
36
36
|
|
37
37
|
h4. Main Features
|
38
38
|
|
39
|
-
The first feature of the pipeline is to concatenate assets. This is important in a production environment,
|
39
|
+
The first feature of the pipeline is to concatenate assets. This is important in a production environment, because it can reduce the number of requests that a browser must make to render a web page. Web browsers are limited in the number of requests that they can make in parallel, so fewer requests can mean faster loading for your application.
|
40
40
|
|
41
|
-
|
41
|
+
Rails 2.x introduced the ability to concatenate JavaScript and CSS assets by placing +:cache => true+ at the end of the +javascript_include_tag+ and +stylesheet_link_tag+ methods. But this technique has some limitations. For example, it cannot generate the caches in advance, and it is not able to transparently include assets provided by third-party libraries.
|
42
42
|
|
43
|
-
|
43
|
+
Starting with version 3.1, Rails defaults to concatenating all JavaScript files into one master +.js+ file and all CSS files into one master +.css+ file. As you'll learn later in this guide, you can customize this strategy to group files any way you like. In production, Rails inserts an MD5 fingerprint into each filename so that the file is cached by the web browser. You can invalidate the cache by altering this fingerprint, which happens automatically whenever you change the file contents..
|
44
44
|
|
45
|
-
The second feature is
|
45
|
+
The second feature of the asset pipeline is asset minification or compression. For CSS files, this is done by removing whitespace and comments. For JavaScript, more complex processes can be applied. You can choose from a set of built in options or specify your own.
|
46
46
|
|
47
|
-
The third feature
|
47
|
+
The third feature of the asset pipeline is that it allows coding assets via a higher-level language, with precompilation down to the actual assets. Supported languages include Sass for CSS, CoffeeScript for JavaScript, and ERB for both by default.
|
48
48
|
|
49
49
|
h4. What is Fingerprinting and Why Should I Care?
|
50
50
|
|
51
|
-
Fingerprinting is a technique
|
51
|
+
Fingerprinting is a technique that makes the name of a file dependent on the contents of the file. When the file contents change, the filename is also changed. For content that is static or infrequently changed, this provides an easy way to tell whether two versions of a file are identical, even across different servers or deployment dates.
|
52
52
|
|
53
|
-
When a filename is unique and based on its content, HTTP headers can be set to encourage caches everywhere (at ISPs, in browsers) to keep their own copy of the content. When the content is updated, the fingerprint will change
|
53
|
+
When a filename is unique and based on its content, HTTP headers can be set to encourage caches everywhere (whether at CDNs, at ISPs, in networking equipment, or in web browsers) to keep their own copy of the content. When the content is updated, the fingerprint will change. This will cause the remote clients to request a new copy of the content. This is generally known as _cache busting_.
|
54
54
|
|
55
|
-
The
|
55
|
+
The technique that Rails uses for fingerprinting is to insert a hash of the content into the name, usually at the end. For example a CSS file +global.css+ could be renamed with an MD5 digest of its contents:
|
56
56
|
|
57
57
|
<plain>
|
58
58
|
global-908e25f4bf641868d8683022a5b62f54.css
|
@@ -60,30 +60,32 @@ global-908e25f4bf641868d8683022a5b62f54.css
|
|
60
60
|
|
61
61
|
This is the strategy adopted by the Rails asset pipeline.
|
62
62
|
|
63
|
-
Rails' old strategy was to append a query string to every asset linked with a built-in helper. In the source the generated code looked like this:
|
63
|
+
Rails' old strategy was to append a date-based query string to every asset linked with a built-in helper. In the source the generated code looked like this:
|
64
64
|
|
65
65
|
<plain>
|
66
66
|
/stylesheets/global.css?1309495796
|
67
67
|
</plain>
|
68
68
|
|
69
|
-
|
69
|
+
The query string strategy has several disadvantages:
|
70
70
|
|
71
71
|
<ol>
|
72
72
|
<li>
|
73
|
-
<strong>Not all caches will cache content
|
73
|
+
<strong>Not all caches will reliably cache content where the filename only differs by query parameters</strong>.<br>
|
74
74
|
"Steve Souders recommends":http://www.stevesouders.com/blog/2008/08/23/revving-filenames-dont-use-querystring/, "...avoiding a querystring for cacheable resources". He found that in this case 5-20% of requests will not be cached. Query strings in particular do not work at all with some CDNs for cache invalidation.
|
75
75
|
</li>
|
76
76
|
<li>
|
77
77
|
<strong>The file name can change between nodes in multi-server environments.</strong><br>
|
78
|
-
The query string in Rails is based on the modification time of the files. When assets are deployed to a cluster, there is no guarantee that the timestamps will be the same, resulting in different values being used depending on which server handles the request.
|
78
|
+
The default query string in Rails 2.x is based on the modification time of the files. When assets are deployed to a cluster, there is no guarantee that the timestamps will be the same, resulting in different values being used depending on which server handles the request.
|
79
|
+
</li>
|
80
|
+
<li>
|
81
|
+
<strong>Too much cache invalidation</strong><br />
|
82
|
+
When static assets are deployed with each new release of code, the mtime of _all_ these files changes, forcing all remote clients to fetch them again, even when the content of those assets has not changed.
|
79
83
|
</li>
|
80
84
|
</ol>
|
81
85
|
|
82
|
-
|
83
|
-
|
84
|
-
Fingerprinting fixes these problems by avoiding query strings, and by ensuring filenames are consistent based on their content.
|
86
|
+
Fingerprinting fixes these problems by avoiding query strings, and by ensuring that filenames are consistent based on their content.
|
85
87
|
|
86
|
-
Fingerprinting is enabled by default for production and disabled for all
|
88
|
+
Fingerprinting is enabled by default for production and disabled for all other environments. You can enable or disable it in your configuration through the +config.assets.digest+ option.
|
87
89
|
|
88
90
|
More reading:
|
89
91
|
|
@@ -95,19 +97,19 @@ h3. How to Use the Asset Pipeline
|
|
95
97
|
|
96
98
|
In previous versions of Rails, all assets were located in subdirectories of +public+ such as +images+, +javascripts+ and +stylesheets+. With the asset pipeline, the preferred location for these assets is now the +app/assets+ directory. Files in this directory are served by the Sprockets middleware included in the sprockets gem.
|
97
99
|
|
98
|
-
|
100
|
+
Assets can still be placed in the +public+ hierarchy. Any assets under +public+ will be served as static files by the application or web server. You should use +app/assets+ for files that must undergo some pre-processing before they are served.
|
99
101
|
|
100
|
-
In production,
|
102
|
+
In production, Rails precompiles these files to +public/assets+ by default. The precompiled copies are then served as static assets by the web server. The files in +app/assets+ are never served directly in production.
|
101
103
|
|
102
|
-
When a scaffold or controller
|
104
|
+
When you generate a scaffold or a controller, Rails also generates a JavaScript file (or CoffeeScript file if the +coffee-rails+ gem is in the +Gemfile+) and a Cascading Style Sheet file (or SCSS file if +sass-rails+ is in the +Gemfile+) for that controller.
|
103
105
|
|
104
|
-
For example, if a +ProjectsController
|
106
|
+
For example, if you generate a +ProjectsController+, Rails will also add a new file at +app/assets/javascripts/projects.js.coffee+ and another at +app/assets/stylesheets/projects.css.scss+. You should put any JavaScript or CSS unique to a controller inside their respective asset files, as these files can then be loaded just for these controllers with lines such as +<%= javascript_include_tag params[:controller] %>+ or +<%= stylesheet_link_tag params[:controller] %>+.
|
105
107
|
|
106
|
-
NOTE: You
|
108
|
+
NOTE: You must have an "ExecJS":https://github.com/sstephenson/execjs#readme supported runtime in order to use CoffeeScript. If you are using Mac OS X or Windows you have a JavaScript runtime installed in your operating system. Check "ExecJS":https://github.com/sstephenson/execjs#readme documentation to know all supported JavaScript runtimes.
|
107
109
|
|
108
110
|
h4. Asset Organization
|
109
111
|
|
110
|
-
Assets can be placed inside an application in one of three locations: +app/assets+, +lib/assets+ or +vendor/assets+.
|
112
|
+
Assets that must be precompiled can be placed inside an application in one of three locations: +app/assets+, +lib/assets+ or +vendor/assets+.
|
111
113
|
|
112
114
|
+app/assets+ is for assets that are owned by the application, such as custom images, JavaScript files or stylesheets.
|
113
115
|
|
@@ -115,7 +117,7 @@ Assets can be placed inside an application in one of three locations: +app/asset
|
|
115
117
|
|
116
118
|
+vendor/assets+ is for assets that are owned by outside entities, such as code for JavaScript plugins.
|
117
119
|
|
118
|
-
All subdirectories that exist within these three locations are added to the search path for Sprockets
|
120
|
+
All subdirectories that exist within these three locations are added to the search path for Sprockets. You can see this search path by inspecting +Rails.application.config.assets.paths+ in the Rails console. When a client requests an asset, these paths are traversed (in the order that they occur in the search path) to see if they contain an asset matching the name specified. If an asset is found, it's processed by Sprockets and served.
|
119
121
|
|
120
122
|
You can add additional (fully qualified) paths to the pipeline in +config/application.rb+. For example:
|
121
123
|
|
@@ -138,7 +140,7 @@ In regular views you can access images in the +assets/images+ directory like thi
|
|
138
140
|
<%= image_tag "rails.png" %>
|
139
141
|
</erb>
|
140
142
|
|
141
|
-
Provided that the pipeline is enabled within your application (and not disabled in the current environment context), this file is served by Sprockets. If a file exists at +public/assets/rails.png+ it is served by the
|
143
|
+
Provided that the pipeline is enabled within your application (and not disabled in the current environment context), this file is served by Sprockets. If a file exists at +public/assets/rails.png+ it is served by the web server.
|
142
144
|
|
143
145
|
Alternatively, a request for a file with an MD5 hash such as +public/assets/rails-af27b6a414e6da00003503148be9b409.png+ is treated the same way. How these hashes are generated is covered in the "In Production":#in-production section later on in this guide.
|
144
146
|
|
@@ -152,7 +154,7 @@ Images can also be organized into subdirectories if required, and they can be ac
|
|
152
154
|
|
153
155
|
h5. CSS and ERB
|
154
156
|
|
155
|
-
|
157
|
+
The asset pipeline automatically evaluates ERB. This means that if you add an +erb+ extension to a CSS asset (for example, +application.css.erb+), then helpers like +asset_path+ are available in your CSS rules:
|
156
158
|
|
157
159
|
<plain>
|
158
160
|
.class { background-image: url(<%= asset_path 'image.png' %>) }
|
@@ -194,7 +196,7 @@ $('#logo').attr({
|
|
194
196
|
|
195
197
|
This writes the path to the particular asset being referenced.
|
196
198
|
|
197
|
-
Similarly, you can use the +asset_path+ helper in CoffeeScript files with +erb+ extension (
|
199
|
+
Similarly, you can use the +asset_path+ helper in CoffeeScript files with +erb+ extension (e.g., +application.js.coffee.erb+):
|
198
200
|
|
199
201
|
<plain>
|
200
202
|
$('#logo').attr src: "<%= asset_path('logo.png') %>"
|
@@ -202,9 +204,9 @@ $('#logo').attr src: "<%= asset_path('logo.png') %>"
|
|
202
204
|
|
203
205
|
h4. Manifest Files and Directives
|
204
206
|
|
205
|
-
Sprockets uses manifest files to determine which assets to include and serve. These manifest files contain _directives_ -- instructions that tell Sprockets which files to require in order to build a single CSS or JavaScript file. With these directives, Sprockets loads the files specified, processes them if necessary, concatenates them into one single file and then compresses them (if +Rails.application.config.assets.compress+ is true). By serving one file rather than many, the load time of pages
|
207
|
+
Sprockets uses manifest files to determine which assets to include and serve. These manifest files contain _directives_ -- instructions that tell Sprockets which files to require in order to build a single CSS or JavaScript file. With these directives, Sprockets loads the files specified, processes them if necessary, concatenates them into one single file and then compresses them (if +Rails.application.config.assets.compress+ is true). By serving one file rather than many, the load time of pages can be greatly reduced because the browser makes fewer requests.
|
206
208
|
|
207
|
-
For example,
|
209
|
+
For example, a new Rails application includes a default +app/assets/javascripts/application.js+ file which contains the following lines:
|
208
210
|
|
209
211
|
<plain>
|
210
212
|
// ...
|
@@ -217,11 +219,11 @@ In JavaScript files, the directives begin with +//=+. In this case, the file is
|
|
217
219
|
|
218
220
|
NOTE. In Rails 3.1 the +jquery-rails+ gem provides the +jquery.js+ and +jquery_ujs.js+ files via the asset pipeline. You won't see them in the application tree.
|
219
221
|
|
220
|
-
The +require_tree+ directive tells Sprockets to recursively include _all_ JavaScript files in
|
222
|
+
The +require_tree+ directive tells Sprockets to recursively include _all_ JavaScript files in the specified directory into the output. These paths must be specified relative to the manifest file. You can also use the +require_directory+ directive which includes all JavaScript files only in the directory specified, without recursion.
|
221
223
|
|
222
|
-
Directives are processed top to bottom, but the order in which files are included by +require_tree+ is unspecified. You should not rely on any particular order among those. If you need to ensure some particular JavaScript ends up above some other, require
|
224
|
+
Directives are processed top to bottom, but the order in which files are included by +require_tree+ is unspecified. You should not rely on any particular order among those. If you need to ensure some particular JavaScript ends up above some other in the concatenated file, require the prerequisite file first in the manifest. Note that the family of +require+ directives prevents files from being included twice in the output.
|
223
225
|
|
224
|
-
|
226
|
+
Rails also creates a default +app/assets/stylesheets/application.css+ file which contains these lines:
|
225
227
|
|
226
228
|
<plain>
|
227
229
|
/* ...
|
@@ -230,15 +232,15 @@ There's also a default +app/assets/stylesheets/application.css+ file which conta
|
|
230
232
|
*/
|
231
233
|
</plain>
|
232
234
|
|
233
|
-
The directives that work in the JavaScript files also work in stylesheets
|
235
|
+
The directives that work in the JavaScript files also work in stylesheets (though obviously including stylesheets rather than JavaScript files). The +require_tree+ directive in a CSS manifest works the same way as the JavaScript one, requiring all stylesheets from the current directory.
|
234
236
|
|
235
237
|
In this example +require_self+ is used. This puts the CSS contained within the file (if any) at the precise location of the +require_self+ call. If +require_self+ is called more than once, only the last call is respected.
|
236
238
|
|
237
|
-
NOTE. If you want to use multiple Sass files, use the "Sass +@import+ rule":http://sass-lang.com/docs/yardoc/file.SASS_REFERENCE.html#import instead of
|
239
|
+
NOTE. If you want to use multiple Sass files, you should generally use the "Sass +@import+ rule":http://sass-lang.com/docs/yardoc/file.SASS_REFERENCE.html#import instead of these Sprockets directives. Using Sprockets directives all Sass files exist within their own scope, making variables or mixins only available within the document they were defined in.
|
238
240
|
|
239
241
|
You can have as many manifest files as you need. For example the +admin.css+ and +admin.js+ manifest could contain the JS and CSS files that are used for the admin section of an application.
|
240
242
|
|
241
|
-
The same remarks about ordering made above apply. In particular, you can specify individual files and they are compiled in the order specified:
|
243
|
+
The same remarks about ordering made above apply. In particular, you can specify individual files and they are compiled in the order specified. For example, you might concatenate three CSS files together this way:
|
242
244
|
|
243
245
|
<plain>
|
244
246
|
/* ...
|
@@ -253,15 +255,15 @@ h4. Preprocessing
|
|
253
255
|
|
254
256
|
The file extensions used on an asset determine what preprocessing is applied. When a controller or a scaffold is generated with the default Rails gemset, a CoffeeScript file and a SCSS file are generated in place of a regular JavaScript and CSS file. The example used before was a controller called "projects", which generated an +app/assets/javascripts/projects.js.coffee+ and an +app/assets/stylesheets/projects.css.scss+ file.
|
255
257
|
|
256
|
-
When these files are requested, they are processed by the processors provided by the +coffee-script+ and +sass
|
258
|
+
When these files are requested, they are processed by the processors provided by the +coffee-script+ and +sass+ gems and then sent back to the browser as JavaScript and CSS respectively.
|
257
259
|
|
258
|
-
Additional layers of preprocessing can be requested by adding other extensions, where each extension is processed in a right-to-left manner. These should be used in the order the processing should be applied. For example, a stylesheet called +app/assets/stylesheets/projects.css.scss.erb+ is first processed as ERB, then SCSS and finally served as CSS. The same applies to a JavaScript file -- +app/assets/javascripts/projects.js.coffee.erb+ is processed as ERB, CoffeeScript, and served as JavaScript.
|
260
|
+
Additional layers of preprocessing can be requested by adding other extensions, where each extension is processed in a right-to-left manner. These should be used in the order the processing should be applied. For example, a stylesheet called +app/assets/stylesheets/projects.css.scss.erb+ is first processed as ERB, then SCSS, and finally served as CSS. The same applies to a JavaScript file -- +app/assets/javascripts/projects.js.coffee.erb+ is processed as ERB, then CoffeeScript, and served as JavaScript.
|
259
261
|
|
260
262
|
Keep in mind that the order of these preprocessors is important. For example, if you called your JavaScript file +app/assets/javascripts/projects.js.erb.coffee+ then it would be processed with the CoffeeScript interpreter first, which wouldn't understand ERB and therefore you would run into problems.
|
261
263
|
|
262
264
|
h3. In Development
|
263
265
|
|
264
|
-
In development mode assets are served as separate files in the order they are specified in the manifest file.
|
266
|
+
In development mode, assets are served as separate files in the order they are specified in the manifest file.
|
265
267
|
|
266
268
|
This manifest +app/assets/javascripts/application.js+:
|
267
269
|
|
@@ -289,7 +291,7 @@ You can turn off debug mode by updating +config/environments/development.rb+ to
|
|
289
291
|
config.assets.debug = false
|
290
292
|
</ruby>
|
291
293
|
|
292
|
-
When debug mode is off Sprockets concatenates and runs the necessary preprocessors on all files. With debug mode turned off the manifest above would generate instead:
|
294
|
+
When debug mode is off, Sprockets concatenates and runs the necessary preprocessors on all files. With debug mode turned off the manifest above would generate instead:
|
293
295
|
|
294
296
|
<html>
|
295
297
|
<script src="/assets/application.js" type="text/javascript"></script>
|
@@ -312,7 +314,7 @@ You could potentially also enable compression in development mode as a sanity ch
|
|
312
314
|
|
313
315
|
h3. In Production
|
314
316
|
|
315
|
-
In the production environment Rails uses the fingerprinting scheme outlined above. By default
|
317
|
+
In the production environment Rails uses the fingerprinting scheme outlined above. By default Rails assumes that assets have been precompiled and will be served as static assets by your web server.
|
316
318
|
|
317
319
|
During the precompilation phase an MD5 is generated from the contents of the compiled files, and inserted into the filenames as they are written to disc. These fingerprinted names are used by the Rails helpers in place of the manifest name.
|
318
320
|
|
@@ -330,7 +332,7 @@ generates something like this:
|
|
330
332
|
<link href="/assets/application-4dd5b109ee3439da54f5bdfd78a80473.css" media="screen" rel="stylesheet" type="text/css" />
|
331
333
|
</html>
|
332
334
|
|
333
|
-
The fingerprinting behavior is controlled by the setting of +config.assets.digest+ setting in Rails (which
|
335
|
+
The fingerprinting behavior is controlled by the setting of +config.assets.digest+ setting in Rails (which defaults to +true+ for production and +false+ for everything else).
|
334
336
|
|
335
337
|
NOTE: Under normal circumstances the default option should not be changed. If there are no digests in the filenames, and far-future headers are set, remote clients will never know to refetch the files when their content changes.
|
336
338
|
|
@@ -338,9 +340,9 @@ h4. Precompiling Assets
|
|
338
340
|
|
339
341
|
Rails comes bundled with a rake task to compile the asset manifests and other files in the pipeline to the disk.
|
340
342
|
|
341
|
-
Compiled assets are written to the location specified in +config.assets.prefix+.
|
343
|
+
Compiled assets are written to the location specified in +config.assets.prefix+. By default, this is the +public/assets+ directory.
|
342
344
|
|
343
|
-
You
|
345
|
+
You can call this task on the server during deployment to create compiled versions of your assets directly on the server. If you do not have write access to your production file system, you can call this task locally and then deploy the compiled assets.
|
344
346
|
|
345
347
|
The rake task is:
|
346
348
|
|
@@ -357,7 +359,7 @@ test +rake assets:precompile+ locally before deploying. It may expose bugs where
|
|
357
359
|
your assets reference application objects or methods, since those are still
|
358
360
|
in scope in development mode regardless of the value of this flag.
|
359
361
|
|
360
|
-
Capistrano (v2.8.0 and above)
|
362
|
+
Capistrano (v2.8.0 and above) includes a recipe to handle this in deployment. Add the following line to +Capfile+:
|
361
363
|
|
362
364
|
<erb>
|
363
365
|
load 'deploy/assets'
|
@@ -369,7 +371,7 @@ It is important that this folder is shared between deployments so that remotely
|
|
369
371
|
|
370
372
|
NOTE. If you are precompiling your assets locally, you can use +bundle install --without assets+ on the server to avoid installing the assets gems (the gems in the assets group in the Gemfile).
|
371
373
|
|
372
|
-
The default matcher for compiling files includes +application.js+, +application.css+ and all non-JS/CSS files (
|
374
|
+
The default matcher for compiling files includes +application.js+, +application.css+ and all non-JS/CSS files (i.e., +.coffee+ and +.scss+ files are *not* automatically included as they compile to JS/CSS):
|
373
375
|
|
374
376
|
<ruby>
|
375
377
|
[ Proc.new{ |path| !File.extname(path).in?(['.js', '.css']) }, /application.(css|js)$/ ]
|
@@ -381,7 +383,7 @@ If you have other manifests or individual stylesheets and JavaScript files to in
|
|
381
383
|
config.assets.precompile += ['admin.js', 'admin.css', 'swfObject.js']
|
382
384
|
</erb>
|
383
385
|
|
384
|
-
The rake task also generates a +manifest.yml+ that contains a list with all your assets and their respective fingerprints. This is used by the Rails helper methods
|
386
|
+
The rake task also generates a +manifest.yml+ that contains a list with all your assets and their respective fingerprints. This is used by the Rails helper methods to avoid handing the mapping requests back to Sprockets. A typical manifest file looks like:
|
385
387
|
|
386
388
|
<plain>
|
387
389
|
---
|
@@ -404,7 +406,7 @@ NOTE: If there are missing precompiled files in production you will get an <tt>S
|
|
404
406
|
|
405
407
|
h5. Server Configuration
|
406
408
|
|
407
|
-
Precompiled assets exist on the filesystem and are served directly by your
|
409
|
+
Precompiled assets exist on the filesystem and are served directly by your web server. They do not have far-future headers by default, so to get the benefit of fingerprinting you'll have to update your server configuration to add them.
|
408
410
|
|
409
411
|
For Apache:
|
410
412
|
|
@@ -451,7 +453,7 @@ This directive is available if the core module that provides this feature was co
|
|
451
453
|
|
452
454
|
If you're compiling nginx with Phusion Passenger you'll need to pass that option when prompted.
|
453
455
|
|
454
|
-
|
456
|
+
A robust configuration for Apache is possible but tricky; please Google around. (Or help update this Guide if you have a good example configuration for Apache.)
|
455
457
|
|
456
458
|
h4. Live Compilation
|
457
459
|
|
@@ -467,9 +469,9 @@ On the first request the assets are compiled and cached as outlined in developme
|
|
467
469
|
|
468
470
|
Sprockets also sets the +Cache-Control+ HTTP header to +max-age=31536000+. This signals all caches between your server and the client browser that this content (the file served) can be cached for 1 year. The effect of this is to reduce the number of requests for this asset from your server; the asset has a good chance of being in the local browser cache or some intermediate cache.
|
469
471
|
|
470
|
-
This mode uses more memory, performs
|
472
|
+
This mode uses more memory, performs more poorly than the default and is not recommended.
|
471
473
|
|
472
|
-
|
474
|
+
If you are deploying a production application to a system without any pre-existing JavaScript runtimes, you may want to add one to your Gemfile:
|
473
475
|
|
474
476
|
<plain>
|
475
477
|
group :production do
|
@@ -493,9 +495,9 @@ The +config.assets.compress+ must be set to +true+ to enable CSS compression.
|
|
493
495
|
|
494
496
|
h4. JavaScript Compression
|
495
497
|
|
496
|
-
Possible options for JavaScript compression are +:closure+, +:uglifier+ and +:yui+. These require the use of the +closure-compiler+, +uglifier+ or +yui-compressor+ gems respectively.
|
498
|
+
Possible options for JavaScript compression are +:closure+, +:uglifier+ and +:yui+. These require the use of the +closure-compiler+, +uglifier+ or +yui-compressor+ gems, respectively.
|
497
499
|
|
498
|
-
The default Gemfile includes "uglifier":https://github.com/lautis/uglifier. This gem wraps "UglifierJS":https://github.com/mishoo/UglifyJS (written for NodeJS) in Ruby. It compresses your code by removing white space
|
500
|
+
The default Gemfile includes "uglifier":https://github.com/lautis/uglifier. This gem wraps "UglifierJS":https://github.com/mishoo/UglifyJS (written for NodeJS) in Ruby. It compresses your code by removing white space. It also includes other optimizations such as changing your +if+ and +else+ statements to ternary operators where possible.
|
499
501
|
|
500
502
|
The following line invokes +uglifier+ for JavaScript compression.
|
501
503
|
|
@@ -503,13 +505,13 @@ The following line invokes +uglifier+ for JavaScript compression.
|
|
503
505
|
config.assets.js_compressor = :uglifier
|
504
506
|
</erb>
|
505
507
|
|
506
|
-
|
508
|
+
Note that +config.assets.compress+ must be set to +true+ to enable JavaScript compression
|
507
509
|
|
508
|
-
NOTE: You will need
|
510
|
+
NOTE: You will need an "ExecJS":https://github.com/sstephenson/execjs#readme supported runtime in order to use +uglifier+. If you are using Mac OS X or Windows you have a JavaScript runtime installed in your operating system. Check the "ExecJS":https://github.com/sstephenson/execjs#readme documentation for information on all of the supported JavaScript runtimes.
|
509
511
|
|
510
512
|
h4. Using Your Own Compressor
|
511
513
|
|
512
|
-
The compressor config settings for CSS and JavaScript also take any
|
514
|
+
The compressor config settings for CSS and JavaScript also take any object. This object must have a +compress+ method that takes a string as the sole argument and it must return a string.
|
513
515
|
|
514
516
|
<erb>
|
515
517
|
class Transformer
|
@@ -519,7 +521,7 @@ class Transformer
|
|
519
521
|
end
|
520
522
|
</erb>
|
521
523
|
|
522
|
-
To enable this, pass a +new+
|
524
|
+
To enable this, pass a +new+ object to the config option in +application.rb+:
|
523
525
|
|
524
526
|
<erb>
|
525
527
|
config.assets.css_compressor = Transformer.new
|
@@ -536,20 +538,20 @@ This can be changed to something else:
|
|
536
538
|
config.assets.prefix = "/some_other_path"
|
537
539
|
</erb>
|
538
540
|
|
539
|
-
This is a handy option if you
|
541
|
+
This is a handy option if you are updating an existing project (pre Rails 3.1) that already uses this path or you wish to use this path for a new resource.
|
540
542
|
|
541
543
|
h4. X-Sendfile Headers
|
542
544
|
|
543
|
-
The X-Sendfile header is a directive to the server to ignore the response from the application, and instead serve
|
545
|
+
The X-Sendfile header is a directive to the web server to ignore the response from the application, and instead serve a specified file from disk. This option is off by default, but can be enabled if your server supports it. When enabled, this passes responsibility for serving the file to the web server, which is faster.
|
544
546
|
|
545
|
-
Apache and nginx support this option which
|
547
|
+
Apache and nginx support this option, which can be enabled in <tt>config/environments/production.rb</tt>.
|
546
548
|
|
547
549
|
<erb>
|
548
550
|
# config.action_dispatch.x_sendfile_header = "X-Sendfile" # for apache
|
549
551
|
# config.action_dispatch.x_sendfile_header = 'X-Accel-Redirect' # for nginx
|
550
552
|
</erb>
|
551
553
|
|
552
|
-
WARNING: If you are upgrading an existing application and intend to use this option, take care to paste this configuration option only into +production.rb+
|
554
|
+
WARNING: If you are upgrading an existing application and intend to use this option, take care to paste this configuration option only into +production.rb+ and any other environments you define with production behavior (not +application.rb+).
|
553
555
|
|
554
556
|
h3. How Caching Works
|
555
557
|
|
@@ -569,7 +571,7 @@ TODO: Registering gems on "Tilt":https://github.com/rtomayko/tilt enabling Sproc
|
|
569
571
|
|
570
572
|
h3. Upgrading from Old Versions of Rails
|
571
573
|
|
572
|
-
There are two issues when upgrading. The first is moving the files to the new locations. See "Asset Organization":#asset-organization above for guidance on the correct locations for different file types.
|
574
|
+
There are two issues when upgrading. The first is moving the files from +public/+ to the new locations. See "Asset Organization":#asset-organization above for guidance on the correct locations for different file types.
|
573
575
|
|
574
576
|
The second is updating the various environment files with the correct default options. The following changes reflect the defaults in version 3.1.0.
|
575
577
|
|
@@ -619,7 +621,7 @@ config.assets.digest = true
|
|
619
621
|
# config.assets.precompile += %w( search.js )
|
620
622
|
</erb>
|
621
623
|
|
622
|
-
|
624
|
+
You should not need to change +test.rb+. The defaults in the test environment are: +config.assets.compile+ is true and +config.assets.compress+, +config.assets.debug+ and +config.assets.digest+ are false.
|
623
625
|
|
624
626
|
The following should also be added to +Gemfile+:
|
625
627
|
|
@@ -633,7 +635,7 @@ group :assets do
|
|
633
635
|
end
|
634
636
|
</plain>
|
635
637
|
|
636
|
-
If you use the +assets+ group with Bundler, please make sure that your +config/application.rb+ has the following Bundler require statement
|
638
|
+
If you use the +assets+ group with Bundler, please make sure that your +config/application.rb+ has the following Bundler require statement:
|
637
639
|
|
638
640
|
<ruby>
|
639
641
|
if defined?(Bundler)
|
@@ -644,7 +646,7 @@ if defined?(Bundler)
|
|
644
646
|
end
|
645
647
|
</ruby>
|
646
648
|
|
647
|
-
Instead of the old Rails 3.0
|
649
|
+
Instead of the old Rails 3.0 version:
|
648
650
|
|
649
651
|
<ruby>
|
650
652
|
# If you have a Gemfile, require the gems listed there, including any gems
|