WDI week 6: JavaScript testing, Underscore.js and more advanced RoR

This week felt like a lot of new material at this stage. They warned us it's common for people to experience a dip in confidence around the halfway point in the course, and I can definitely relate to that, as I finished this week feeling pretty overwhelmed.

I think it's just that being expected to learn new stuff on a shaky foundation of existing knowledge feels difficult and a bit counterintuitive, but I'll talk about what we covered this week anyway.

We began on Monday with an RSpec recap and then a lecture about refactoring, with a view to making the code from our Rails projects from last week better. Something I hadn't realized before was that 'fat models and skinny controllers' is a convention that's considered good practice. This means that the business logic of an application should live in the model, leaving the corresponding controller with minimal code. I'd written the bulk of my logic in one controller, so I refactored my app to reflect this newly-learned convention. It's still not finished, but hopefully enough tiny steps will amount to something eventually!

Tuesday morning we learned JavaScript testing. We used Mocha as our testing framework, which is equivalent to RSpec but for JavaScript. We used it in combination with Chai.js (an assertion library), Sinon.js (standalone test spies, stubs and mocks for JavaScript) and Konacha (a Rails engine that allows you to test your JavaScript with Mocha and Chai).

We spent the afternoon talking about unobtrusive JavaScript in Rails, which is the process of separating out your JavaScript from your HTML, rather than writing it inline. This means easier debugging and cleaner code, and only became available in Rails 3, before which JavaScript was obtrusive.

On Wednesday we talked about the asset pipeline in Rails, and then learned about JavaScript templates (JST). Using the EJS gem, which stands for embedded JavaScript, you can separate your presentation markup into a separate file called a template, then embed JavaScript to control any in-browser logic.

Later on, we learned about Underscore.js, which is a library that makes many of the familiar Ruby methods like .map, .min, .max, .range and .keys available to JavaScript. Lo-Dash is an alternative that does basically the same thing but is apparently slightly better.

Thursday we did pagination and infinite scroll, which was pretty cool as I'd always wondered how these worked. We used the will_paginate gem, but I think it'd be fun to try to figure out how this works in a more standalone way at some point.

Friday was a mixture of JavaScript review and callbacks in Rails. On callbacks, here's the explanation from the Rails API:

Callbacks are hooks into the life cycle of an ActiveRecord object that allow you to trigger logic before or after an alteration of the object state. This can be used to make sure that associated and dependent objects are deleted when destroy is called (by overwriting before_destroy) or to massage attributes before they're validated (by overwriting before_validation).

As I mentioned before, I feel like we touched on a lot of new, different things this week, so I need to spend some more time making sense of all this. But at least I have it in notes for future reference, even if I'm hazy on the details right now.