Extracting the InviteRedeemer class from the Invite model in Discourse – Part 1


After poking around the Discourse project on Code Climate, I found that the Invite#redeem method was given a high complexity score.

Starting Grade

After studying the redeem method for a bit, I decided it would be a good subject for the next refactoring. The method that we started with is shown below.

While looking at the original method, there are a few things I saw that guided this refactoring.  The first is that the class is named Invite but the redeem method performs a lot of actions that go above and beyond what would be considered a single responsibility.

Invite shouldn’t care about how it is redeemed, it just needs to know if it has been redeemed or not.  With this as the starting point, I created a new InviteRedeemer struct that is called from within the Invite#redeem method.

The Invite#redeem method becomes the following:

To get the functionality moved, copy the contents of the redeem method from Invite to InviteRedeemer.  Running the tests will guide you to the next few steps which will be to preface any method calls that were previously called on the invite object itself with the invite attribute that is passed into InviteRedeemer. Repeat this until all of the tests are green again.

Now that the functionality is moved and all the tests are green, the process of refactoring the internals of the InviteRedeemer#redeem method can begin. This is where I’ll pick up in the next post.


Please enter your comment!
Please enter your name here