Pairing Tour Day 5: Upgrading To The Asset Pipeline
I wrapped up last week by pairing with my mentor Mike on an upgrade to our 8th Light University app. Itās a straightforward Rails app that we needed to update to take care of the recently discovered Rails vulnerability. Mike also wanted to use it as an opportunity to make the jump up from Rails 3.0.10. That meant a leap to the asset pipeline and I wanted to document the steps we took in order to get everything working.
We have a pretty spartan Gemfile and use mySql2 so the only two things we needed to do to start were update the versions of these two gems:
gem 'rails', '~> 3.1.10' gem āmysql2ā, ā0.3.11ā
In the first run through of this upgrade we had some problems with mySql so I decided to lock it to version ā0.3.11ā. After this we did a bundle update.
Iād never heard of rake rails:update before but I came across it in this post after hitting some initial troubles and decided to give it a shot. What it does is goes through and updates your necessary files, but prompts you along the way so that you know what will be overwritten. Below is my terminal output as an example, which also includes what files I decided to overwrite and which I didnāt.
Whenever it prompted me to overwrite a file I loaded it in MacVim first, then allowed it to override, then selected āload fileā from the OS X dialog, then toggled between āundoā (u) and āredoā (r) to make sure I wanted to apply the changes:
identical config/boot.rb exist config conflict config/routes.rb Overwrite ~/projects/university/config/routes.rb? (enter "h" for help) [Ynaqdh] n skip config/routes.rb conflict config/application.rb Overwrite ~/projects/university/config/application.rb? (enter "h" for help) [Ynaqdh] Y force config/application.rb identical config/environment.rb exist config/environments conflict config/environments/development.rb Overwrite ~/projects/university/config/environments/development.rb? (enter "h" for help) [Ynaqdh] Y force config/environments/development.rb conflict config/environments/production.rb Overwrite ~/projects/university/config/environments/production.rb? (enter "h" for help) [Ynaqdh] Y force config/environments/production.rb conflict config/environments/test.rb Overwrite ~/projects/university/config/environments/test.rb? (enter "h" for help) [Ynaqdh] Y force config/environments/test.rb exist config/initializers identical config/initializers/backtrace_silencers.rb identical config/initializers/inflections.rb identical config/initializers/mime_types.rb conflict config/initializers/secret_token.rb Overwrite ~/projects/university/config/initializers/secret_token.rb? (enter "h" for help) [Ynaqdh] n skip config/initializers/secret_token.rb conflict config/initializers/session_store.rb Overwrite ~/projects/university/config/initializers/session_store.rb? (enter "h" for help) [Ynaqdh] n skip config/initializers/session_store.rb create config/initializers/wrap_parameters.rb exist config/locales conflict config/locales/en.yml Overwrite ~/projects/university/config/locales/en.yml? (enter "h" for help) [Ynaqdh] Y force config/locales/en.yml exist script identical script/rails
Moving the Assets & Creating Application files
Before the asset pipeline all of the images, stylesheets and javascripts were stored in the āpublicā directory. Now all of those should be moved to app/assets/images, app/assets/javascriptsā and app/assets/stylesheets. These three simple commands should do the trick (depending on the structure of your directories):
mv public/images app/assets/ mv public/stylesheets app/assets/ mv public/javascripts app/assets/
I also needed to add an application.js and application.css file that required my other files. The application.js file looked like this (note the order of the files you are requiring!):
#app/assets/application.js //= require jquery //= require jquery_ujs //= require_self //= require_tree .
and the application.css file looked like this (again, pay attention to order!):
#app/assets/application.css /* *= require reset *= require_self *= require_tree . */
Next, add this group to your Gemfile, followed by a bundle update command:
group :assets do gem 'sass-rails', " ~> 3.1.0" gem 'coffee-rails', " ~> 3.1.0" gem 'uglifier', '1.0.4' end
Changing the config files:
Update or add the following lines in their corresponding files:
#config/development.rb config.action_view.debug_rjs = true #config/production.rb config.serve_static_assets = true
Update your views and CSS
You can fire up your server to see how things are coming along- but most likely youāll have some broken images and javascript that doesnāt work. This is because prior to the asset pipeline it was common to use things like a standard img src="public/assets/gangnam-style.gif"tagā but now we use an image tag like <%= image_tag(āgangnam-style.gifā) %>. You'll have to go in and change each of these image links manually.
We also need to update the headers of our ālayout.html.erbā file to include our new application.js and application.css files. The old links probably looked something like:
<%= stylesheet_link_tag 'reset.css', 'jquery.css', '8thlightu.css' %> <%= javascript_include_tag 'custom/application.js' %>
which can be replaced with:
<%= stylesheet_link_tag 'application.cssā %> <%= javascript_include_tag 'application.jsā %>
At this point all of our RSpec and Cucumber tests were passing (and things looked good when we fired up our server) but none of our Jasmine specs passed. We realized that our spec/javascripts/support/jasmine.yml file still pointed to the old assets. In that file we were able to replace this entire block of lines:
src_files: - public/javascripts/prototype.js - public/javascripts/effects.js - public/javascripts/controls.js - public/javascripts/dragdrop.js - public/javascripts/application.js - public/javascripts/**/*.js
src_files: - assets/application.js
If everything went according to plan then congrats, youāre done! Things should look great and all of your Specs, Features and Jasmine Tests pass⦠Of course, I realize that may not be the case and there might be a hiccup or two along the way. If anyone has experience with alternate upgrade paths, questions, or feedback Iād love to hear it.