Skip to content

Instantly share code, notes, and snippets.

@kevinmtrowbridge
Last active August 29, 2015 14:05
Show Gist options
  • Save kevinmtrowbridge/229e94cab5df114f1d8b to your computer and use it in GitHub Desktop.
Save kevinmtrowbridge/229e94cab5df114f1d8b to your computer and use it in GitHub Desktop.
Nova Fabrica Rails 3 to Rails 4 Cheatsheet

Nova Fabrica Rails 3 to Rails 4 Cheatsheet

Upgrade guides

I think this is a good place to start. It tells you the first steps, it's not exhaustive but it gives you a great starting point. http://railscasts.com/episodes/415-upgrading-to-rails-4?view=asciicast

Another 'getting started' guide: http://pivotallabs.com/rails-4-upgrade/

This the exhaustive official upgrade guide. It's well worth reading through in it detail. This may take several days. There will be changes described in there, that did not apply to AEA. This document only captures how the Rails 4 upgrade worked for AEA. http://edgeguides.rubyonrails.org/upgrading_ruby_on_rails.html

Opportunity for other changes

A big change like this, where everything is examined, and QAed, is a good time to do other slightly ambitious stuff.

For AEA we did:

  • General cleanup. We deleted about 2,000 lines of code!

  • Normalize syntax and indentation standards. NF policy is to double indent (4 spaces) private and protected methods.

  • Reorganize: views that were only used in views/staff were moved into view/staff/shared. We had a discussion about where non-model classes belong -- in lib or app/models. KS said 'app/model's and had the winning argument, his was basically what this blog post says.

Summarized Nova Fabrica experience during AEA upgrade

Be aware of the closed PR from the AEA Rails 4 upgrade, it is the source of most of this information and a great resource.

Changes to Gemfile

  • Rails 4 updates assets pipeline and the assets Gemfile group is no longer necessary. Therefore, move gems related to assets outside of the assets group, and delete the assets group.

  • bcrypt-ruby gem is now just bcrypt

  • We need specific version of sass-rails: 4.0.3 -- symptom was, the asset_pipeline assets were missing their digest, so, application.css vs application-23qefa3rqwfease.css example change

Changes to routes.rb

PUT becomes PATCH, in general: documentation example changes

Changes to config / system files

  • I ran the rake rails:upgrade task and, transitioned over all the config / init files to their Rails 4 formats. example changes ... don't forget about config/environments/staging.rb.

  • The lines that load the Gemfile in application.rb have become more terse. documentation

  • config/initializers/secret_token.rb becomes config/secrets.yml example change ... don't forget about staging environment when creating secrets.yml.

  • I renamed the script folder to bin as per Rails 4 upgrade guides. This also moved the script/delayed_job into bin/delayed_job -- these paths might be in some monitoring or capistrano pieces. example change

Changes to models

  • In Rails 4 ALL scopes must have lambdas. This is because, for one example, if there now a Time.now in the scope, it would get set at boot time, and never change. This was a very hard problem to figure out since, in dev mode the file is frequently reloaded. :(

  • We had a discussion regarding the use of "spiky lambdas"--that's -> vs lambda--we decided we wanted to stick with the lambda spelled out.

  • The behavior of .sum has changed. It's not defined on ActiveRecord in the same way. In some changes we had to change to .to_a.sum example changes

  • The Model.scoped method is deprecated. Now, use Model.all. It returns an ActiveRecord::Relation, which is what you want. discussion argument where folks who say '.all' can replace, win

  • Model.includes(:other_model) must chain a .references call, if the included model is used in a .where SQL statement. discussion example changes

In Rails v4, we should use #includes just like we always have. But in addition, if there is a #where that references one of the included tables, then you must also use #references or it will break. If the #where clause doesn't exist or does not reference an included table, then the #includes alone will do the trick and it will not break.

Changes to controllers

  • 'whitelisted parameters' are now required. This is a big change that we're all familiar with.

  • You can no longer pass :include as an option to a Model.find method. This is part of the larger change "Rails 4.0 has deprecated the old-style hash based finder API. This means that methods which previously accepted "finder options" no longer do." You could search for :include to find that. example changes

  • All methods "find_by_x" are deprecated.

Changes to views

Rails 4.0 deprecates the :confirm option for the link_to helper. You should instead rely on a data attribute (e.g. data: { confirm: 'Are you sure?' }). This deprecation also concerns the helpers based on this one (such as link_to_if or link_to_unless). https://github.com/novafabrica/eventstore/commit/41b948d08efdffc19d0d2cad577298092c39772f

Asset pipeline related changes

  • See the bit about sass-rails above.

Deployment related changes

@FotoVerite
Copy link

it's rake rails:update not upgrade

@kevinskoglund
Copy link

This guide looks good to me. @FotoVerite and @meesterdude, please take a look and either make additions/corrections or give it a +1.

@kevinskoglund
Copy link

I thought of an addition to add here and in AEA:

  • All occurrences of "before_filter" should be renamed "before_action". Not sure that you get a deprecation warning yet, but before_filter is an alias starting with Rails 4.0.(rails/rails@9d62e04)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment