4_1_release_notes.md 18.1 KB
Newer Older
1 2 3 4 5
Ruby on Rails 4.1 Release Notes
===============================

Highlights in Rails 4.1:

6
* Variants
7
* Spring
8
* Action View extracted from Action Pack
9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31

These release notes cover only the major changes. To know about various bug
fixes and changes, please refer to the change logs or check out the
[list of commits](https://github.com/rails/rails/commits/master) in the main
Rails repository on GitHub.

--------------------------------------------------------------------------------

Upgrading to Rails 4.1
----------------------

If you're upgrading an existing application, it's a great idea to have good test
coverage before going in. You should also first upgrade to Rails 4.0 in case you
haven't and make sure your application still runs as expected before attempting
an update to Rails 4.1. A list of things to watch out for when upgrading is
available in the
[Upgrading to Rails](upgrading_ruby_on_rails.html#upgrading-from-rails-4-0-to-rails-4-1)
guide.


Major Features
--------------

32
### Variants
33

34 35
We often want to render different html/json/xml templates for phones,
tablets, and desktop browsers. Variants makes it easy.
36

37 38
The request variant is a specialization of the request format, like `:tablet`,
`:phone`, or `:desktop`.
39

40
You can set the variant in a before_action:
41

42 43 44
```ruby
request.variant = :tablet if request.user_agent =~ /iPad/
```
45

46
Respond to variants in the action just like you respond to formats:
47

48 49 50 51 52 53 54 55
```ruby
respond_to do |format|
  format.html do |html|
    html.tablet # renders app/views/projects/show.html+tablet.erb
    html.phone { extra_setup; render ... }
  end
end
```
56

57
Provide separate templates for each format and variant:
58

59 60 61 62 63
```
app/views/projects/show.html.erb
app/views/projects/show.html+tablet.erb
app/views/projects/show.html+phone.erb
```
64

65 66 67 68 69 70 71 72 73 74
You can also simplify the variants definition using the inline syntax:

```ruby
respond_to do |format|
  format.js         { render "trash" }
  format.html.phone { redirect_to progress_path }
  format.html.none  { render "trash" }
end
```

75 76 77 78 79 80 81
### Spring

New Rails 4.1 applications will ship with "springified" binstubs. This means
that `bin/rails` and `bin/rake` will automatically take advantage preloaded
spring environments.

**running rake tasks:**
82

83 84 85 86 87
```
bin/rake routes
```

**running tests:**
88

89 90 91 92 93 94 95
```
bin/rake test
bin/rake test test/models
bin/rake test test/models/user_test.rb
```

**running a console:**
96

97 98 99 100 101
```
bin/rails console
```

**spring introspection:**
102

103
```
104
$ bin/spring status
105 106 107 108 109 110 111 112 113 114 115
Spring is running:

 1182 spring server | my_app | started 29 mins ago
 3656 spring app    | my_app | started 23 secs ago | test mode
 3746 spring app    | my_app | started 10 secs ago | development mode
```

Have a look at the
[Spring README](https://github.com/jonleighton/spring/blob/master/README.md) to
see a all available features.

116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135
### Active Record enums

Declare an enum attribute where the values map to integers in the database, but
can be queried by name.

```ruby
class Conversation < ActiveRecord::Base
  enum status: [ :active, :archived ]
end

conversation.archive!
conversation.active? # => false
conversation.status  # => "archived"

Conversation.archived # => Relation for all archived Conversations
```

See
[active_record/enum.rb](https://github.com/rails/rails/blob/4-1-stable/activerecord/lib/active_record/enum.rb#L2-L42)
for a detailed write up.
136

137 138 139 140 141 142 143 144 145 146 147
### Application message verifier.

Create a message verifier that can be used to generate and verify signed
messages in the application.

```ruby
message = Rails.application.message_verifier('salt').generate('my sensible data')
Rails.application.message_verifier('salt').verify(message)
# => 'my sensible data'
```

148 149 150 151 152 153 154 155 156 157 158
Documentation
-------------


Railties
--------

Please refer to the
[Changelog](https://github.com/rails/rails/blob/4-1-stable/railties/CHANGELOG.md)
for detailed changes.

159 160
### Removals

161
* Removed `update:application_controller` rake task.
162

163
* Removed deprecated `Rails.application.railties.engines`.
164

165
* Removed deprecated `threadsafe!` from Rails Config.
166

167
* Removed deprecated `ActiveRecord::Generators::ActiveModel#update_attributes` in
Y
Yves Senn 已提交
168
  favor of `ActiveRecord::Generators::ActiveModel#update`.
169

Y
Yves Senn 已提交
170
* Removed deprecated `config.whiny_nils` option.
171

172 173
* Removed deprecated rake tasks for running tests: `rake test:uncommitted` and
  `rake test:recent`.
174

175 176
### Notable changes

177 178 179 180 181 182
* The [Spring application
  preloader](https://github.com/jonleighton/spring) is now installed
  by default for new applications. It uses the development group of
  the Gemfile, so will not be installed in
  production. ([Pull Request](https://github.com/rails/rails/pull/12958))

183 184 185
* `BACKTRACE` environment variable to show unfiltered backtraces for test
  failures. ([Commit](https://github.com/rails/rails/commit/84eac5dab8b0fe9ee20b51250e52ad7bfea36553))

Y
Yves Senn 已提交
186 187
* Exposed `MiddlewareStack#unshift` to environment
  configuration. ([Pull Request](https://github.com/rails/rails/pull/12479))
188

Y
Yves Senn 已提交
189 190
* Add `Application#message_verifier` method to return a message
  verifier. ([Pull Request](https://github.com/rails/rails/pull/12995))
191 192 193 194 195 196 197 198 199 200

Action Mailer
-------------

Please refer to the
[Changelog](https://github.com/rails/rails/blob/4-1-stable/actionmailer/CHANGELOG.md)
for detailed changes.

### Notable changes

201 202
* Instrument the generation of Action Mailer messages. The time it takes to
  generate a message is written to the log. ([Pull Request](https://github.com/rails/rails/pull/12556))
203

204 205 206 207 208 209 210 211

Active Model
------------

Please refer to the
[Changelog](https://github.com/rails/rails/blob/4-1-stable/activemodel/CHANGELOG.md)
for detailed changes.

212 213 214 215 216
### Deprecations

* Deprecate `Validator#setup`. This should be done manually now in the
  validator's constructor. ([Commit](https://github.com/rails/rails/commit/7d84c3a2f7ede0e8d04540e9c0640de7378e9b3a))

217 218
### Notable changes

219 220 221
* Added new API methods `reset_changes` and `changes_applied` to
  `ActiveModel::Dirty` that control changes state.

222 223 224 225 226 227 228 229

Active Support
--------------

Please refer to the
[Changelog](https://github.com/rails/rails/blob/4-1-stable/activesupport/CHANGELOG.md)
for detailed changes.

230 231 232

### Removals

G
Godfrey Chan 已提交
233 234 235 236 237 238 239 240 241 242 243
* Removed `MultiJSON` dependency. As a result, `ActiveSupport::JSON.decode`
  no longer accepts an options hash for `MultiJSON`. ([Pull Request](https://github.com/rails/rails/pull/10576) / [More Details](upgrading_ruby_on_rails.html#changes-in-json-handling))

* Removed support for the `encode_json` hook used for encoding custom objects into
  JSON. This feature has been extracted into the [activesupport-json_encoder](https://github.com/rails/activesupport-json_encoder)
  gem.
  ([Related Pull Request](https://github.com/rails/rails/pull/12183) /
  [More Details](upgrading_ruby_on_rails.html#changes-in-json-handling))

* Removed deprecated `ActiveSupport::JSON::Variable` with no replacement.

244
* Removed deprecated `String#encoding_aware?` core extensions (`core_ext/string/encoding`).
245

246
* Removed deprecated `Module#local_constant_names` in favor of `Module#local_constants`.
247

248
* Removed deprecated `DateTime.local_offset` in favor of `DateTime.civil_from_fromat`.
249

250
* Removed deprecated `Logger` core extensions (`core_ext/logger.rb`).
251

252
* Removed deprecated `Time#time_with_datetime_fallback`, `Time#utc_time` and
253 254
  `Time#local_time` in favor of `Time#utc` and `Time#local`.

255
* Removed deprecated `Hash#diff` with no replacement.
256

257
* Removed deprecated `Date#to_time_in_current_zone` in favor of `Date#in_time_zone`.
258

259
* Removed deprecated `Proc#bind` with no replacement.
260

261
* Removed deprecated `Array#uniq_by` and `Array#uniq_by!`, use native
262 263
  `Array#uniq` and `Array#uniq!` instead.

264
* Removed deprecated `ActiveSupport::BasicObject`, use
265 266
  `ActiveSupport::ProxyObject` instead.

267
* Removed deprecated `BufferedLogger`, use `ActiveSupport::Logger` instead.
268

269
* Removed deprecated `assert_present` and `assert_blank` methods, use `assert
270 271 272 273 274 275 276 277
  object.blank?` and `assert object.present?` instead.

### Deprecations

* Deprecated `Numeric#{ago,until,since,from_now}`, the user is expected to
  explicitly convert the value into an AS::Duration, i.e. `5.ago` => `5.seconds.ago`
  ([Pull Request](https://github.com/rails/rails/pull/12389))

G
Godfrey Chan 已提交
278 279 280 281 282 283 284 285 286 287 288 289 290 291 292
* Deprecated the require path `active_support/core_ext/object/to_json`. Require
  `active_support/core_ext/object/json` instead. ([Pull Request](https://github.com/rails/rails/pull/12203))

* Deprecated `ActiveSupport::JSON::Encoding::CircularReferenceError`. This feature
  has been extracted into the [activesupport-json_encoder](https://github.com/rails/activesupport-json_encoder)
  gem.
  ([Pull Request](https://github.com/rails/rails/pull/12785) /
  [More Details](upgrading_ruby_on_rails.html#changes-in-json-handling))

* Deprecated `ActiveSupport.encode_big_decimal_as_string` option. This feature has
  been extracetd into the [activesupport-json_encoder](https://github.com/rails/activesupport-json_encoder)
  gem.
  ([Pull Request](https://github.com/rails/rails/pull/13060) /
  [More Details](upgrading_ruby_on_rails.html#changes-in-json-handling))

293 294
### Notable changes

G
Godfrey Chan 已提交
295 296 297 298 299 300 301 302 303
* `ActiveSupport`'s JSON encoder has been rewritten to take advantage of the
  JSON gem rather than doing custom encoding in pure-Ruby.
  ([Pull Request](https://github.com/rails/rails/pull/12183) /
  [More Details](upgrading_ruby_on_rails.html#changes-in-json-handling))

* Improved compatibility with the JSON gem.
  ([Pull Request](https://github.com/rails/rails/pull/12862) /
  [More Details](upgrading_ruby_on_rails.html#changes-in-json-handling))

304 305 306 307
* Added `ActiveSupport::Testing::TimeHelpers#travel` and `#travel_to`. These
  methods change current time to the given time or time difference by stubbing
  `Time.now` and
  `Date.today`. ([Pull Request](https://github.com/rails/rails/pull/12824))
308 309 310 311 312

* Added `Numeric#in_milliseconds`, like `1.hour.in_milliseconds`, so we can feed
  them to JavaScript functions like
  `getTime()`. ([Commit](https://github.com/rails/rails/commit/423249504a2b468d7a273cbe6accf4f21cb0e643))

313
* Added `Date#middle_of_day`, `DateTime#middle_of_day` and `Time#middle_of_day`
314 315 316 317
  methods. Also added `midday`, `noon`, `at_midday`, `at_noon` and
  `at_middle_of_day` as
  aliases. ([Pull Request](https://github.com/rails/rails/pull/10879))

318
* Added `String#remove(pattern)` as a short-hand for the common pattern of
319 320
  `String#gsub(pattern,'')`. ([Commit](https://github.com/rails/rails/commit/5da23a3f921f0a4a3139495d2779ab0d3bd4cb5f))

321
* Removed 'cow' => 'kine' irregular inflection from default
322
  inflections. ([Commit](https://github.com/rails/rails/commit/c300dca9963bda78b8f358dbcb59cabcdc5e1dc9))
323 324 325 326 327 328 329 330

Action Pack
-----------

Please refer to the
[Changelog](https://github.com/rails/rails/blob/4-1-stable/actionpack/CHANGELOG.md)
for detailed changes.

331 332
### Removals

333 334
* Removed deprecated Rails application fallback for integration testing, set
  `ActionDispatch.test_app` instead.
335

336
* Removed deprecated `page_cache_extension` config.
337

338 339 340
* Removed deprecated `ActionController::RecordIdentifier`, use
  `ActionView::RecordIdentifier` instead.

341
* Removed deprecated constants from Action Controller:
342

343 344 345 346 347 348 349
      ActionController::AbstractRequest  => ActionDispatch::Request
      ActionController::Request          => ActionDispatch::Request
      ActionController::AbstractResponse => ActionDispatch::Response
      ActionController::Response         => ActionDispatch::Response
      ActionController::Routing          => ActionDispatch::Routing
      ActionController::Integration      => ActionDispatch::Integration
      ActionController::IntegrationTest  => ActionDispatch::IntegrationTest
350

351 352
### Notable changes

353 354
* `#url_for` takes a hash with options inside an
  array. ([Pull Request](https://github.com/rails/rails/pull/9599))
355

356
* Added `session#fetch` method fetch behaves similarly to
357 358 359 360
  [Hash#fetch](http://www.ruby-doc.org/core-1.9.3/Hash.html#method-i-fetch),
  with the exception that the returned value is always saved into the
  session. ([Pull Request](https://github.com/rails/rails/pull/12692))

361
* Separated Action View completely from Action
362 363
  Pack. ([Pull Request](https://github.com/rails/rails/pull/11032))

364 365 366 367 368 369 370 371

Active Record
-------------

Please refer to the
[Changelog](https://github.com/rails/rails/blob/4-1-stable/activerecord/CHANGELOG.md)
for detailed changes.

372 373
### Removals

374
* Removed deprecated nil-passing to the following `SchemaCache` methods:
375 376
  `primary_keys`, `tables`, `columns` and `columns_hash`.

377
* Removed deprecated block filter from `ActiveRecord::Migrator#migrate`.
378

379
* Removed deprecated String constructor from `ActiveRecord::Migrator`.
380

381
* Removed deprecated `scope` use without passing a callable object.
382

383 384
* Removed deprecated `transaction_joinable=` in favor of `begin_transaction`
  with `d:joinable` option.
385

386
* Removed deprecated `decrement_open_transactions`.
387

388
* Removed deprecated `increment_open_transactions`.
389

390 391
* Removed deprecated `PostgreSQLAdapter#outside_transaction?`
  methodd. You can use `#transaction_open?` instead.
392

393
* Removed deprecated `ActiveRecord::Fixtures.find_table_name` in favor of
394 395 396 397
  `ActiveRecord::Fixtures.default_fixture_model_name`.

* Removed deprecated `columns_for_remove` from `SchemaStatements`.

398
* Removed deprecated `SchemaStatements#distinct`.
399

400
* Moved deprecated `ActiveRecord::TestCase` into the Rails test
401 402 403 404 405 406
  suite. The class is no longer public and is only used for internal
  Rails tests.

* Removed support for deprecated option `:restrict` for `:dependent`
  in associations.

407 408
* Removed support for deprecated `:delete_sql`, `:insert_sql`, `:finder_sql`
  and `:counter_sql` options in associations.
409 410 411

* Removed deprecated method `type_cast_code` from Column.

412
* Removed deprecated `ActiveRecord::Base#connection` method.
413 414
  Make sure to access it via the class.

415
* Removed deprecation warning for `auto_explain_threshold_in_seconds`.
416

417
* Removed deprecated `:distinct` option from `Relation#count`.
418 419 420 421

* Removed deprecated methods `partial_updates`, `partial_updates?` and
  `partial_updates=`.

Y
Yves Senn 已提交
422
* Removed deprecated method `scoped`.
423

Y
Yves Senn 已提交
424
* Removed deprecated method `default_scopes?`.
425 426 427

* Remove implicit join references that were deprecated in 4.0.

Y
Yves Senn 已提交
428
* Removed `activerecord-deprecated_finders` as a dependency.
429

430
* Removed usage of `implicit_readonly`. Please use `readonly` method
431
  explicitly to mark records as
432
  `readonly`. ([Pull Request](https://github.com/rails/rails/pull/10769))
433 434 435

### Deprecations

436
* Deprecated `quoted_locking_column` method, which isn't used anywhere.
437

438
* Deprecated the delegation of Array bang methods for associations.
439 440 441 442
  To use them, instead first call `#to_a` on the association to access the
  array to be acted
  on. ([Pull Request](https://github.com/rails/rails/pull/12129))

443
* Deprecated `ConnectionAdapters::SchemaStatements#distinct`,
444 445
  as it is no longer used by internals. ([Pull Request](https://github.com/rails/rails/pull/10556))

446 447
### Notable changes

448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463
* Added `ActiveRecord::Base.to_param` for convenient "pretty" URLs derived from
  a model's attribute or
  method. ([Pull Request](https://github.com/rails/rails/pull/12891))

* Added `ActiveRecord::Base.no_touching`, which allows ignoring touch on
  models. ([Pull Request](https://github.com/rails/rails/pull/12772))

* Unify boolean type casting for `MysqlAdapter` and `Mysql2Adapter`.
  `type_cast` will return `1` for `true` and `0` for `false`. ([Pull Request](https://github.com/rails/rails/pull/12425))

* `.unscope` now removes conditions specified in
  `default_scope`. ([Commit](https://github.com/rails/rails/commit/94924dc32baf78f13e289172534c2e71c9c8cade))

* Added `ActiveRecord::QueryMethods#rewhere` which will overwrite an existing,
  named where condition. ([Commit](https://github.com/rails/rails/commit/f950b2699f97749ef706c6939a84dfc85f0b05f2))

464
* Extended `ActiveRecord::Base#cache_key` to take an optional list of timestamp
465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492
  attributes of which the highest will be used. ([Commit](https://github.com/rails/rails/commit/e94e97ca796c0759d8fcb8f946a3bbc60252d329))

* Added `ActiveRecord::Base#enum` for declaring enum attributes where the values
  map to integers in the database, but can be queried by
  name. ([Commit](https://github.com/rails/rails/commit/db41eb8a6ea88b854bf5cd11070ea4245e1639c5))

* Type cast json values on write, so that the value is consistent with reading
  from the database. ([Pull Request](https://github.com/rails/rails/pull/12643))

* Type cast hstore values on write, so that the value is consistent
  with reading from the database. ([Commit](https://github.com/rails/rails/commit/5ac2341fab689344991b2a4817bd2bc8b3edac9d))

* Make `next_migration_number` accessible for third party
  generators. ([Pull Request](https://github.com/rails/rails/pull/12407))

* Calling `update_attributes` will now throw an `ArgumentError` whenever it
  gets a `nil` argument. More specifically, it will throw an error if the
  argument that it gets passed does not respond to to
  `stringify_keys`. ([Pull Request](https://github.com/rails/rails/pull/9860))

* `CollectionAssociation#first`/`#last` (e.g. `has_many`) use a `LIMIT`ed
  query to fetch results rather than loading the entire
  collection. ([Pull Request](https://github.com/rails/rails/pull/12137))

* `inspect` on Active Record model classes does not initiate a new
  connection. This means that calling `inspect`, when the database is missing,
  will no longer raise an exception. ([Pull Request](https://github.com/rails/rails/pull/11014))

493
* Removed column restrictions for `count`, let the database raise if the SQL is
494 495 496 497 498 499 500 501 502
  invalid. ([Pull Request](https://github.com/rails/rails/pull/10710))

* Rails now automatically detects inverse associations. If you do not set the
  `:inverse_of` option on the association, then Active Record will guess the
  inverse association based on heuristics. ([Pull Request](https://github.com/rails/rails/pull/10886))

* Handle aliased attributes in ActiveRecord::Relation. When using symbol keys,
  ActiveRecord will now translate aliased attribute names to the actual column
  name used in the database. ([Pull Request](https://github.com/rails/rails/pull/7839))
503

504 505 506 507
* The ERB in fixture files is no longer evaluated in the context of the main
  object. Helper methods used by multiple fixtures should be defined on modules
  included in `ActiveRecord::FixtureSet.context_class`. ([Pull Request](https://github.com/rails/rails/pull/13022))

508 509 510 511 512 513 514
Credits
-------

See the
[full list of contributors to Rails](http://contributors.rubyonrails.org/) for
the many people who spent many hours making Rails, the stable and robust
framework it is. Kudos to all of them.