railties 3.1.0.rc4 → 3.1.0.rc5

Sign up to get free protection for your applications and to get access to all the features.
Files changed (74) hide show
  1. data/CHANGELOG +7 -9
  2. data/bin/rails +2 -0
  3. data/guides/assets/images/rails_welcome.png +0 -0
  4. data/guides/rails_guides/generator.rb +1 -1
  5. data/guides/rails_guides/textile_extensions.rb +18 -17
  6. data/guides/source/3_0_release_notes.textile +21 -21
  7. data/guides/source/action_controller_overview.textile +1 -1
  8. data/guides/source/action_mailer_basics.textile +27 -6
  9. data/guides/source/action_view_overview.textile +6 -6
  10. data/guides/source/active_record_querying.textile +77 -7
  11. data/guides/source/active_record_validations_callbacks.textile +78 -81
  12. data/guides/source/active_support_core_extensions.textile +87 -31
  13. data/guides/source/ajax_on_rails.textile +1 -1
  14. data/guides/source/api_documentation_guidelines.textile +12 -8
  15. data/guides/source/asset_pipeline.textile +416 -0
  16. data/guides/source/association_basics.textile +2 -4
  17. data/guides/source/caching_with_rails.textile +7 -6
  18. data/guides/source/command_line.textile +78 -116
  19. data/guides/source/configuring.textile +34 -17
  20. data/guides/source/contribute.textile +1 -1
  21. data/guides/source/contributing_to_ruby_on_rails.textile +3 -3
  22. data/guides/source/debugging_rails_applications.textile +2 -2
  23. data/guides/source/form_helpers.textile +40 -51
  24. data/guides/source/getting_started.textile +641 -197
  25. data/guides/source/initialization.textile +4 -4
  26. data/guides/source/layouts_and_rendering.textile +2 -2
  27. data/guides/source/migrations.textile +114 -32
  28. data/guides/source/nested_model_forms.textile +6 -6
  29. data/guides/source/performance_testing.textile +6 -6
  30. data/guides/source/plugins.textile +23 -22
  31. data/guides/source/rails_application_templates.textile +8 -14
  32. data/guides/source/routing.textile +57 -51
  33. data/guides/source/ruby_on_rails_guides_guidelines.textile +3 -3
  34. data/guides/source/security.textile +10 -10
  35. data/guides/source/testing.textile +1 -1
  36. data/lib/rails.rb +27 -1
  37. data/lib/rails/all.rb +1 -0
  38. data/lib/rails/application.rb +4 -10
  39. data/lib/rails/application/configuration.rb +3 -12
  40. data/lib/rails/application/railties.rb +1 -1
  41. data/lib/rails/engine.rb +53 -42
  42. data/lib/rails/generators.rb +1 -1
  43. data/lib/rails/generators/app_base.rb +36 -15
  44. data/lib/rails/generators/generated_attribute.rb +1 -1
  45. data/lib/rails/generators/js/assets/assets_generator.rb +13 -0
  46. data/lib/rails/generators/js/assets/templates/javascript.js +2 -0
  47. data/lib/rails/generators/rails/app/app_generator.rb +3 -1
  48. data/lib/rails/generators/rails/app/templates/Gemfile +11 -7
  49. data/lib/rails/generators/rails/app/templates/config/application.rb +7 -3
  50. data/lib/rails/generators/rails/app/templates/config/databases/jdbc.yml +62 -0
  51. data/lib/rails/generators/rails/app/templates/config/databases/jdbcmysql.yml +3 -3
  52. data/lib/rails/generators/rails/app/templates/config/databases/jdbcpostgresql.yml +3 -12
  53. data/lib/rails/generators/rails/app/templates/config/databases/jdbcsqlite3.yml +3 -3
  54. data/lib/rails/generators/rails/app/templates/config/environments/production.rb.tt +0 -3
  55. data/lib/rails/generators/rails/assets/assets_generator.rb +2 -9
  56. data/lib/rails/generators/rails/plugin_new/plugin_new_generator.rb +12 -10
  57. data/lib/rails/generators/rails/plugin_new/templates/%name%.gemspec +5 -2
  58. data/lib/rails/generators/rails/plugin_new/templates/Gemfile +6 -3
  59. data/lib/rails/generators/rails/plugin_new/templates/Rakefile +5 -1
  60. data/lib/rails/generators/rails/plugin_new/templates/app/views/layouts/application.html.erb.tt +14 -0
  61. data/lib/rails/generators/rails/plugin_new/templates/rails/application.rb +5 -3
  62. data/lib/rails/info.rb +4 -0
  63. data/lib/rails/paths.rb +5 -5
  64. data/lib/rails/railtie.rb +4 -4
  65. data/lib/rails/tasks.rb +0 -1
  66. data/lib/rails/tasks/documentation.rake +3 -3
  67. data/lib/rails/tasks/engine.rake +2 -0
  68. data/lib/rails/tasks/framework.rake +3 -3
  69. data/lib/rails/tasks/tmp.rake +1 -1
  70. data/lib/rails/test_unit/testing.rake +2 -2
  71. data/lib/rails/version.rb +1 -1
  72. metadata +19 -33
  73. data/lib/rails/generators/rails/assets/templates/javascript.js.coffee +0 -3
  74. data/lib/rails/tasks/assets.rake +0 -21
@@ -72,7 +72,7 @@ link_to_remote "Add to cart",
72
72
 
73
73
  If the server returns 200, the output of the above example is equivalent to our first, simple one. However, in case of error, the element with the DOM id +error+ is updated rather than the +cart+ element.
74
74
 
75
- ** *position* By default (i.e. when not specifying this option, like in the examples before) the repsonse is injected into the element with the specified DOM id, replacing the original content of the element (if there was any). You might want to alter this behavior by keeping the original content - the only question is where to place the new content? This can specified by the +position+ parameter, with four possibilities:
75
+ ** *position* By default (i.e. when not specifying this option, like in the examples before) the response is injected into the element with the specified DOM id, replacing the original content of the element (if there was any). You might want to alter this behavior by keeping the original content - the only question is where to place the new content? This can specified by the +position+ parameter, with four possibilities:
76
76
  *** +:before+ Inserts the response text just before the target element. More precisely, it creates a text node from the response and inserts it as the left sibling of the target element.
77
77
  *** +:after+ Similar behavior to +:before+, but in this case the response is inserted after the target element.
78
78
  *** +:top+ Inserts the text into the target element, before it's original content. If the target element was empty, this is equivalent with not specifying +:position+ at all.
@@ -33,6 +33,10 @@ Spell names correctly: Arel, Test::Unit, RSpec, HTML, MySQL, JavaScript, ERB. Wh
33
33
 
34
34
  Use the article "an" for "SQL", as in "an SQL statement". Also "an SQLite database".
35
35
 
36
+ h3. English
37
+
38
+ Please use American English (<em>color</em>, <em>center</em>, <em>modularize</em>, etc.). See "a list of American and British English spelling differences here":http://en.wikipedia.org/wiki/American_and_British_English_spelling_differences.
39
+
36
40
  h3. Example Code
37
41
 
38
42
  Choose meaningful examples that depict and cover the basics as well as interesting points or gotchas.
@@ -74,14 +78,14 @@ The result of expressions follow them and are introduced by "# => ", vertically
74
78
  If a line is too long, the comment may be placed on the next line:
75
79
 
76
80
  <ruby>
77
- # label(:post, :title)
78
- # # => <label for="post_title">Title</label>
79
- #
80
- # label(:post, :title, "A short title")
81
- # # => <label for="post_title">A short title</label>
82
- #
83
- # label(:post, :title, "A short title", :class => "title_label")
84
- # # => <label for="post_title" class="title_label">A short title</label>
81
+ # label(:post, :title)
82
+ # # => <label for="post_title">Title</label>
83
+ #
84
+ # label(:post, :title, "A short title")
85
+ # # => <label for="post_title">A short title</label>
86
+ #
87
+ # label(:post, :title, "A short title", :class => "title_label")
88
+ # # => <label for="post_title" class="title_label">A short title</label>
85
89
  </ruby>
86
90
 
87
91
  Avoid using any printing methods like +puts+ or +p+ for that purpose.
@@ -0,0 +1,416 @@
1
+ h2. Asset Pipeline
2
+
3
+ This guide will cover the ideology of the asset pipeline introduced in Rails 3.1.
4
+ By referring to this guide you will be able to:
5
+
6
+ * Understand what the asset pipeline is and what it does
7
+ * Properly organize your application assets
8
+ * Understand the benefits of the asset pipeline
9
+ * Adding a pre-processor to the pipeline
10
+ * Package assets with a gem
11
+
12
+ endprologue.
13
+
14
+ h3. What is the Asset Pipeline?
15
+
16
+ The asset pipeline provides a framework to concatenate and minify or compress JavaScript and CSS assets. It also adds the ability to write these assets in other languages such as CoffeeScript, SCSS and ERB.
17
+
18
+ Prior to Rails 3.1 these features were added through third-party Ruby libraries such as Jammit and Sprockets. Rails 3.1 includes the +sprockets-rails+ gem, which depends on the +sprockets+ gem, by default.
19
+
20
+ By having this as a core feature of Rails, 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 2011 keynote at Railsconf.
21
+
22
+ In new Rails 3.1 application the asset pipeline is enable by default. It can be disabled in +application.rb+ by putting this line inside the +Application+ class definition:
23
+
24
+ <plain>
25
+ config.assets.enabled = false
26
+ </plain>
27
+
28
+ It is recommended that you use the defaults for all new apps.
29
+
30
+
31
+ h4. Main Features
32
+
33
+ The first feature of the pipeline is to concatenate assets. This is important in a production environment, as it reduces the number of requests that a browser needs to make to render a web page. While Rails already has a feature to concatenate these types of asset--by placing +:cache => true+ at the end of tags such as +javascript_include_tag+ and +stylesheet_link_tag+--, many people do not use it.
34
+
35
+ The default behavior in Rails 3.1 and onward is to concatenate all files into one master file each for JS and CSS. However, you can separate files or groups of files if required (see below). In production an MD5 fingerprint is inserted into each filename so that the file is cached by the web browser but can be invalidated if the fingerprint is altered.
36
+
37
+ The second feature is to minify or compress. For CSS, this usually involves 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.
38
+
39
+ The third feature is the ability to code these assets using another language, or language extension. These include SCSS or Sass for CSS, CoffeeScript for JavaScript, and ERB for both.
40
+
41
+ h4. What is Fingerprinting and Why Should I Care?
42
+
43
+ Fingerprinting is a technique where the filenames of content that is static or infrequently updated is altered to be unique to the content contained in the file.
44
+
45
+ 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 and the remote clients will request the new file. This is generally known as _cachebusting_.
46
+
47
+ The most effective technique is to insert a hash of the content into the name, usually at the end. For example a CSS file +global.css+ is hashed and the filename is updated to incorporate the hash.
48
+
49
+ <plain>
50
+ global.css => global-908e25f4bf641868d8683022a5b62f54.css
51
+ </plain>
52
+
53
+ This is the strategy adopted by the Rails asset pipeline.
54
+
55
+ 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:
56
+
57
+ <plain>
58
+ /stylesheets/global.css?1309495796
59
+ </plain>
60
+
61
+ This has several disadvantages:
62
+
63
+ <ol>
64
+ <li>
65
+ <strong>Not all caches will cache content with a query string</strong><br>
66
+ "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 these case 5-20% of requests will not be cached.
67
+ </li>
68
+ <li>
69
+ <strong>The file name can change between nodes in multi-server environments.</strong><br>
70
+ 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.
71
+ </li>
72
+ </ol>
73
+
74
+ The other problem is that 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.
75
+
76
+ Fingerprinting avoids all these problems by ensuring filenames are consistent based on the content.
77
+
78
+ More reading:
79
+
80
+ * "Optimize caching":http://code.google.com/speed/page-speed/docs/caching.html
81
+ * "Revving Filenames: don’t use querystring":http://www.stevesouders.com/blog/2008/08/23/revving-filenames-dont-use-querystring/
82
+
83
+
84
+ h3. How to Use the Asset Pipeline
85
+
86
+ 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 will be served by the Sprockets middleware included in the sprockets gem.
87
+
88
+ This is not to say that assets can (or should) no longer be placed in +public+; they still can be and will be served as static files by the application or web server. You would only use +app/assets+ if you wish your files to undergo some pre-processing before they are served.
89
+
90
+ When a scaffold or controller is generated for the application, Rails will also generate a JavaScript file (or CoffeeScript if the +coffee-script+ gem is in the +Gemfile+) and a Cascading Style Sheet file (or SCSS if +sass-rails+ is in the +Gemfile+) file for that controller.
91
+
92
+ For example, if a +ProjectsController+ is generated, there will be 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] %>+.
93
+
94
+ h4. Asset Organization
95
+
96
+ Assets can be placed inside an application in one of three locations: +app/assets+, +lib/assets+ or +vendor/assets+.
97
+
98
+ +app/assets+ is for assets that are owned by the application, such as custom images, JavaScript files or stylesheets.
99
+
100
+ +lib/assets+ is for your own libraries' code that doesn't really fit into the scope of the application or those libraries which are shared across applications.
101
+
102
+ +vendor/assets+ is for assets that are owned by outside entities, such as code for JavaScript plugins.
103
+
104
+ All subdirectories that exist within these three locations will be added to the search path for Sprockets (visible by calling +Rails.application.config.assets.paths+ in a console). When an asset is requested, these paths will be looked through to see if they contain an asset matching the name specified. Once an asset has been found, it's processed by Sprockets and served.
105
+
106
+ h4. Coding Links to Assets
107
+
108
+ To access assets, we can use the same tags that we are generally familiar with:
109
+
110
+ Sprockets does not add any new methods to require your assets, we still use the familiar +javascript_include_tag+ and +stylesheet_link_tag+.
111
+
112
+ <erb>
113
+ <%= stylesheet_link_tag "application" %>
114
+ <%= javascript_include_tag "application" %>
115
+ </erb>
116
+
117
+ In regular views you can access images in the +assets/images+ directory like this:
118
+
119
+ <erb>
120
+ <%= image_tag "rails.png" %>
121
+ </erb>
122
+
123
+ Images can be organized into directories if required, and they can be accessed by specifying the directory's name in the tag:
124
+
125
+ <erb>
126
+ <%= image_tag "icons/rails.png" %>
127
+ </erb>
128
+
129
+ Providing that assets are enabled within our application (+config.assets.enabled+ in the current environment's file is not set to +false+), this file will be served by Sprockets unless a file at +public/assets/rails.png+ exists, in which case that file will be served.
130
+
131
+ Alternatively, a file with an MD5 hash after its name such as +public/assets/rails-af27b6a414e6da00003503148be9b409.png+ will also be picked up by Sprockets. How these hashes are generated is covered in the "Production Assets":#production_assets section later on in this guide.
132
+
133
+ Otherwise, Sprockets will look through the available paths until it finds a file that matches the name and then will serve it, first looking in the application's assets directories and then falling back to the various engines of the application.
134
+
135
+ If you want to use a "css data URI":http://en.wikipedia.org/wiki/Data_URI_scheme - a method of embedding the image data directly into the CSS file - you can use the +asset_data_uri+ helper.
136
+
137
+ <plain>
138
+ #logo { background: url(<%= asset_data_uri 'logo.png' %>)
139
+ </plain>
140
+
141
+ This will insert a correctly formatted data URI into the CSS source.
142
+
143
+ h5. CSS and ERB
144
+
145
+ If you add an +erb+ extension to a CSS asset, making it something such as +application.css.erb+ then you can use the +asset_path+ helper in your CSS rules:
146
+
147
+ <plain>
148
+ .class{background-image:<%= asset_path 'image.png' %>}
149
+ </plain>
150
+
151
+ This will write the path to the particular asset being referenced. In this example, it would make sense to have an image in one of the asset load paths, such as +app/assets/images/image.png+, which would be referenced here. If this image is already available in +public/assets+ as a fingerprinted file then that path will be referenced.
152
+
153
+ Note that the closing tag cannot be of the style +-%>+.
154
+
155
+ h5. CSS and SCSS
156
+
157
+ When using the asset pipeline, paths to assets must be re-written and +sass-rails+ provides +_url+ and +_path+ helpers for the following asset classes: image, font, video, audio, javascript, stylesheet.
158
+
159
+ * +image_url("rails.png")+ becomes +url(/assets/rails.png)+
160
+ * +image_path("rails.png")+ becomes +"/assets/rails.png"+.
161
+
162
+ The more generic form can also be used but the asset path and class must both be specified:
163
+
164
+ * +asset_url("rails.png", "image")+ becomes +url(/assets/rails.png)+
165
+ * +asset_path("rails.png", "image")+ becomes +"/assets/rails.png"+
166
+
167
+ h4. Manifest Files and Directives
168
+
169
+ 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 will load the files specified, process them if necessary, concatenate them into one single file and then compress them (if +Rails.application.config.assets.compress+ is set to +true+). By serving one file rather than many, a page's load time is greatly reduced as there is not as many requests to make for each file.
170
+
171
+ For example, in the default Rails application there's a +app/assets/javascripts/application.js+ file which contains the following lines:
172
+
173
+ <plain>
174
+ //= require jquery
175
+ //= require jquery_ujs
176
+ //= require_tree .
177
+ </plain>
178
+
179
+ In JavaScript files, directives begin with +//=+. In this case, the following file is using the +require+ directive and the +require_tree+ directive. The +require+ directive tells Sprockets that we would like to require a file called +jquery.js+ that is available somewhere in the search path for Sprockets. By default, this is located inside the +vendor/assets/javascripts+ directory contained within the +jquery-rails+ gem. An identical event takes place for the +jquery_ujs+ require
180
+
181
+ The +require_tree .+ directive tells Sprockets to include _all_ JavaScript files in this directory into the output. Only a path relative to the file can be specified.
182
+
183
+ There's also a default +app/assets/stylesheets/application.css+ file which contains these lines:
184
+
185
+ <plain>
186
+ /* ...
187
+ *= require_self
188
+ *= require_tree .
189
+ */
190
+ </plain>
191
+
192
+ The directives that work in the JavaScript files will also work in stylesheets, obviously including stylesheets rather than JavaScript files. The +require_tree+ directive here works the same way as the JavaScript one, requiring all stylesheets from the current directory.
193
+
194
+ In this example +require_self+ is used. This will put the CSS contained within the file (if any) at the top of any other CSS in this file unless +require_self+ is specified after another +require+ directive.
195
+
196
+ 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.
197
+
198
+ For some assets (like CSS) the compiled order is important. You can specify individual files and they will be compiled in the order specified:
199
+
200
+ <plain>
201
+ /* ...
202
+ *= require reset
203
+ *= require layout
204
+ *= require chrome
205
+ */
206
+ </plain>
207
+
208
+
209
+ h4. Preprocessing
210
+
211
+ The file extensions used on an asset will determine what preprocessing will be applied. When a controller or a scaffold is generated with the default Rails gemset, a CoffeeScript file and a SCSS file will be 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 a +app/assets/stylesheets/projects.css.scss+ file.
212
+
213
+ When these files are requested, they will be processed by the processors provided by the +coffee-script+ and +sass-rails+ gems and then sent back to the browser as JavaScript and CSS respectively.
214
+
215
+ Additional layers of pre-processing can be requested by adding other extensions, where each extension will be 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+ would first be processed as ERB, then SCSS and finally served as CSS. The same applies to a JavaScript file - +app/assets/javascripts/projects.js.coffee.erb+ would be process as ERB, CoffeeScript and served as JavaScript.
216
+
217
+ Keep in mind that the order of these pre-processors is important. For example, if we called our 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 we would run into problems.
218
+
219
+ h3. In Development
220
+
221
+ In the development environment assets are compiled and cached on the first request after the server is started. Sprockets sets a +must-validate+ cache-control http header to reduce request overhead on subsequent requests - on these the browser gets a 304 (not-modified) response.
222
+
223
+ If any of the files in the manifest have changed between requests, the server will respond with a new compiled file.
224
+
225
+ h4. Debugging Assets
226
+
227
+ You can put +?debug_assets=true+ or +?debug_assets=1+ at the end of a URL and Sprockets will expand the lines which load the assets. For example, if we had an +app/assets/javascripts/application.js+ file containing these lines:
228
+
229
+ <plain>
230
+ //= require "projects"
231
+ //= require "tickets"
232
+ </plain>
233
+
234
+ By default, this would only render this line when used with +<%= javascript_include_tag "application" %>+ in a view or layout:
235
+
236
+ <html>
237
+ <script src='/assets/application.js'></script>
238
+ </html>
239
+
240
+ When the +debug_assets+ parameter is set, this line will be expanded out into three separate lines, separating out the combined file into their parts.
241
+
242
+ <html>
243
+ <script src='/assets/application.js'></script>
244
+ <script src='/assets/projects.js'></script>
245
+ <script src='/assets/tickets.js'></script>
246
+ </html>
247
+
248
+ This allows the individual parts of an asset to be rendered and debugged separately.
249
+
250
+ h3. In Production
251
+
252
+ In the production environment, assets are served slightly differently.
253
+
254
+ On the first request the assets are compiled and cached as described above, however the manifest names are altered to include an MD5 hash. Files names typically will look like these:
255
+
256
+ <plain>
257
+ /assets/application-908e25f4bf641868d8683022a5b62f54.js
258
+ /assets/application-4dd5b109ee3439da54f5bdfd78a80473.css
259
+ </plain>
260
+
261
+ The MD5 is generated from the contents of the compiled files, and is included in the http +Content-MD5+ header.
262
+
263
+ 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.
264
+
265
+ This behavior is controlled by the setting of +config.action_controller.perform_caching+ setting in Rails (which is +true+ for production, +false+ for everything else). This value is propagated to Sprockets during initialization for use when action_controller is not available.
266
+
267
+ h4. Precompiling Assets
268
+
269
+ Even though assets are served by Rack::Cache with far-future headers, in high traffic sites this may not be fast enough.
270
+
271
+ Rails comes bundled with a rake task to compile the manifests to files on disc. These are located in the +public/assets+ directory where they will be served by your web server instead of the Rails application.
272
+
273
+ The rake task is:
274
+
275
+ <plain>
276
+ rake assets:precompile
277
+ </plain>
278
+
279
+ You can run this as part of a Capistrano deployment:
280
+
281
+ <erb>
282
+ before 'deploy:symlink' do
283
+ run "cd #{release_path}; RAILS_ENV=#{rails_env} rake assets:precompile"
284
+ end
285
+ </erb>
286
+
287
+ If you are not precompiling your assets, and you are using the default cache file store (which is the file system), you will need to symlink +rails_root/tmp/cache/assets+ from the shared folder that is part of the Capistrano deployment structure in order to persist the cached file between deployments.
288
+
289
+ TODO: Extend above task to allow for this and add task to set it up (See commits 8f0e0b6 and 704ee0df). Note: Capistrano folks are working on a recipe - update this when it available (see https://github.com/capistrano/capistrano/pull/35).
290
+
291
+ The default matcher for compiling files will include +application.js+, +application.css+ and all files that do not end in +js+ or +css+:
292
+
293
+ <ruby>
294
+ [ /\w+\.(?!js|css).+/, /application.(css|js)$/ ]
295
+ </ruby>
296
+
297
+ If you have other manifests or individual stylesheets and JavaScript files to include, you can append them to the +precompile+ array:
298
+
299
+ <erb>
300
+ config.assets.precompile << ['admin.js', 'admin.css', 'swfObject.js']
301
+ </erb>
302
+
303
+ Precompiled assets exist on the filesystem and are served directly by your webserver. 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.
304
+
305
+ For Apache:
306
+
307
+ <plain>
308
+ <LocationMatch "^/assets/.*$">
309
+ # Some browsers still send conditional-GET requests if there's a
310
+ # Last-Modified header or an ETag header even if they haven't
311
+ # reached the expiry date sent in the Expires header.
312
+ Header unset Last-Modified
313
+ Header unset ETag
314
+ FileETag None
315
+ # RFC says only cache for 1 year
316
+ ExpiresActive On
317
+ ExpiresDefault "access plus 1 year"
318
+ </LocationMatch>
319
+ </plain>
320
+
321
+ TODO: NGINX instructions
322
+
323
+ When files are precompiled Sprockets also creates "Gzip":http://en.wikipedia.org/wiki/Gzip (.gz) version of your assets. This avoids the server having to do this for any requests; it can simply read the compressed files from disc. You must configure your server to use gzip compression and serve the compressed assets that will be stored in the public/assets folder. The following configuration options can be used:
324
+
325
+ TODO: Apache instructions
326
+
327
+
328
+ h3. Customizing the Pipeline
329
+
330
+
331
+ h4. CSS Compression
332
+
333
+ There is currently one option for compressing CSS - YUI. This Gem extends the CSS syntax and offers minification.
334
+
335
+ The following line will enable YUI compression, and requires the +yui-compressor+ gem.
336
+
337
+ <erb>
338
+ config.assets.css_compressor = :yui
339
+ </erb>
340
+
341
+ The +config.assets.compress+ must be set to +true+ to enable CSS compression
342
+
343
+ h4. JavaScript
344
+
345
+ Possible options for JavaScript compression are +:closure+, +:uglifier+ and +:yui+. These require the use of the +closure-compiler+, +uglifier+ or +yui-compressor+ gems respectively.
346
+
347
+ 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 compress your code by removing white spaces and other magical things like changing your +if+ and +else+ statements to ternary operators where possible.
348
+
349
+ The following line will invoke uglifier for JavaScript compression.
350
+
351
+ <erb>
352
+ config.assets.js_compressor = :uglifier
353
+ </erb>
354
+
355
+ The +config.assets.compress+ must be set to +true+ to enable JavaScript compression
356
+
357
+ h4. Using Your Own Compressor
358
+
359
+ The compressor config settings for CSS and JavaScript will 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.
360
+
361
+ <erb>
362
+ class Transformer
363
+ def compress(string)
364
+ do_something_returning_a_string(string)
365
+ end
366
+ end
367
+ </erb>
368
+
369
+ To enable this pass a +new+ Object to the config option in +application.rb+:
370
+
371
+ <erb>
372
+ config.assets.css_compressor = Transformer.new
373
+ </erb>
374
+
375
+
376
+ h4. Changing the _assets_ Path
377
+
378
+ The public path that Sprockets uses by default is +/assets+.
379
+
380
+ This can be changed to something else:
381
+
382
+ <erb>
383
+ config.assets.prefix = "/some_other_path"
384
+ </erb>
385
+
386
+ This is a handy option if you have any existing project (pre Rails 3.1) that already uses this path or you wish to use this path for a new resource.
387
+
388
+ h4. X-Sendfile Headers
389
+
390
+ The X-Sendfile header is a directive to the server to ignore the response from the application, and instead serve the file specified in the headers. In production Rails (via Sprockets) does not send the asset - just the location and a zero-length response - relying on the web server to do the file serving, which is usually faster. Both Apache and nginx support this option.
391
+
392
+ The configuration is available in <tt>config/environments/production.rb</tt>.
393
+
394
+ <erb>
395
+ config.action_dispatch.x_sendfile_header = "X-Sendfile" # Use 'X-Accel-Redirect' for nginx
396
+ </erb>
397
+
398
+ You should check that your server or hosting service actually supports this, otherwise comment it out.
399
+
400
+ 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 not +application.rb+) and any other environment you define with production behavior.
401
+
402
+ h3. How Caching Works
403
+
404
+ Sprockets uses the default rails cache store to cache assets in dev and production. The only difference is file names are fingerprinted and get far-future headers in production.
405
+
406
+ TODO: Add more about changing the default store.
407
+
408
+ h3. Adding Assets to Your Gems
409
+
410
+ Assets can also come from external sources in the form of gems.
411
+
412
+ A good example of this is the +jquery-rails+ gem which comes with Rails as the standard JavaScript library gem. This gem contains an engine class which inherits from +Rails::Engine+. By doing this, Rails is informed that the directory for this gem may contain assets and the +app/assets+, +lib/assets+ and +vendor/assets+ directories of this engine are added to the search path of Sprockets.
413
+
414
+ h3. Making Your Library or Gem a Pre-Processor
415
+
416
+ TODO: Registering gems on "Tilt":https://github.com/rtomayko/tilt enabling Sprockets to find them.