Notes on upgrading from Rails 4.1 to 4.2
Posted on January 21st, 2015
This one is a fairly easy upgrade although it was a little hard to discover all the issues. The main work was around removing Inherited Resources.
Here are more detailed notes:
- All calls to
.deliver
become.deliver_now
. Including in your tests and stubs. If you had any tests that depended on the code block to be executed, you have to call.deliver_now
as method execution has been made lazy. - The configuration
config.serve_static_assets
is now renamed toconfig.serve_static_files
in yourapplication.rb
or appropriate environment file. - Inherited Resources is no longer compatible with rails 4.2 (simply due to gem spec constraints but still). Removing it is tedious but not very hard. It’s a matter of changing any
actions
call in yourInheritedResource
controllers to actually implement the method. I actually cleaned a lot of filters along with those changes. Overall, I regret having used Inherited Resources in the first place. The laziness of not writing a few simple actions was surely not worth it. - Add the responders gem to your Gemfile if you used
respond_to
on class level controller andrespond_with
within controller actions. - I18n was upgraded as part of Rails 4.2. With the upgrade, assigned locales are now enforced to be in the list of available locales. Rails supports this by allow you to configure i18n in the
application.rb
inside theCurrent::Application
class. Just addconfig.i18n.available_locales = [:en, :your_locale]
.
However, if you are using Guard-Konacha, it’ll try to load your rails configurations but will not consider this configuration. As a result, you are forced to addI18n.available_locales = [:en, :your_locale]
at the top of the application.rb file (and not inside theCurrent::Application
class). Check this issue for more details. - State Machine is broken. More even than in Rails 4.1. Now my
config/initializers/state_machine.rb
is even bigger. I used a version of the code available in this gist. - FactoryGirl causes
exists
to be called on the realActiveRecord
object instead of using.id
so you get a warning like:DEPRECATION WARNING: You are passing an instance of ActiveRecord::Base to `exists?`. Please pass the id of the object by calling '.id’
.
I just left that one in there and decided to update FactoryGirl when a version higher than 4.5.0 comes out. - Formtastic will call
column_for_attribute
for each input in the form. This can cause the following warning to show up:DEPRECATION WARNING: ‘#column_for_attribute' will return a null object for non-existent columns in Rails 5.
Use '#has_attribute?' if you need to check for an attribute's existence.
It means some of the attributes in your form is not in your model. This can happen if you use some confirmation (like password_confirmation) or have a helper (like tags separated by comma). Rails used to be fooled if you just created anattr_accessor
for that attribute but that trick no longer works. This will need an update in formtastic (probably coming in version 4.x).
I left this warning showing as well in hopes of either moving out of Formtastic or upgrade it when the new version comes out.