Note: This is one part of my journey to tame a spaghetti model or god object. Start with Post 1: Unraveling a Spaghetti Model to see how I chose to tackle this problem.

Overview

Your spaghetti model has been a dumping ground of methods and associations for years. All of your developers have built up muscle memory to continue to add to it. Let’s provide a gentle reminder for everyone to stop our existing behavior.

We’ll add tests to prevent the addition of associations and methods.

You’ll want to lock down permissions to this test file, thus requiring GitHub PR approval from your team for modifications to the tests. While others might be initially annoyed that they can’t keep adding to the dumping ground, at least they have been informed that there’s a new way to do this. Feedback is a gift. 🎁 What’s cool is that you don’t have to be the person doing the nagging, the tests will do it on your behalf. These tests then are double gift 🎁 🎁.

The Tests

Let’s write a test that prevents developers from adding new columns:

Let’s write a test that prevents new associations from getting added:

Let’s write a test that prevents developers from adding new methods:

Yes, there’s probably a fancier way to assert for methods getting added =)

Note that this won’t prevent people from adding more junk to the included ActiveSupport::concerns, but at least you’ve stopped people from making the main spaghetti model worse.

Thank you to Alex Evanczuk for writing our first association tests.

Summary

At the start of our journey to unravel the spaghetti model, the first step is to automatically let other developers know that the spaghetti model is no longer our dumping ground of methods and associations. We leverage our test suite to add purposeful friction to the development process.

Originally published at https://sedano.org on July 28, 2023.