How do I divide my application into reusable pieces?

Monoliths You may have heard of monolithic applications. For the last few years they’ve been regarded as a Bad Thing. That huge single application, full of dependencies. Impossible to break into pieces. Impossible to understand its individual pieces in isolation. The answer, according to many, is to use microservices. Microservices Split your application into separate… Continue reading

Four things you should always move into background jobs

Rails is built to serve web-applications. It is 95% focussed on responding to incoming HTTP requests, doing something with that request and then returning a HTTP response. And it makes it really easy to do this. And make your application respond very quickly. However, if you have a slow response to an incoming request, it… Continue reading

Switching from RSpec to Minitest

I like the “specification” style of testing – it makes more sense to me than the “assertion” style. But I’ve recently switched from RSpec to Minitest (with Mocha). Why? RSpec is huge. It makes lovely tests that read like English, but it’s huge. Plus its recent switch from object.should == something to expect(object).to be ==… Continue reading

The problem with ActiveRecord

The time before ActiveRecord I may be showing my age but I remember the time before Active Record. You would either write raw SQL statements, which for a table with many fields meant a lot of tedious typing, plus the ever-present danger of SQL injection bugs. Or you could use an early Object-Relational Mapper, which… Continue reading

How does Rails work?

So you’ve decided to learn Rails. You could dive straight in to learning the code, but Rails is an opinionated framework and it expects certain things to be done a certain way. A high-level understanding of how it’s structured and what goes where is always important. So let’s follow a hypothetical web-request on its journey… Continue reading

Why shouldn’t I pass instance variables to views?

One of the design decisions in Rails that I thoroughly disagree with is the fact that instance variables are magically passed from the controller to the views. For example, take this example from yesterday:

This uses a load of Rails magic. It renders app/views/documents/index.html.erb for you and sends @documents to it; presumably in index.html.erb… Continue reading

How do I name my object when it’s related to other objects?

Given that we know we should add a sensible naming protocol to our objects, sometimes we have to decide what the best name for an object will be. In most apps you often end up with objects that only make sense in the context of other objects. Take the following classes – a Product connects… Continue reading

How do I name my objects?

One thing that is often missed when creating a new class is the to_s method. Ruby uses this all over the place, and in a Rails app, it’s especially useful in making your views more readable. For example, take a Person class:

It requires that you specify a first name and a last name…. Continue reading

If I have a deeply nested set of objects, will “delegate” help me make sense of them?

Imagine you have a Report that is built out of multiple templates and values, where a template is built out of sections, columns and fields:

This is a pretty complex, circular, set of relationships. And in fact, in situations like this, I would use an Entity Relationship Diagram, which is an old-fashioned way of… Continue reading