SOLID Ruby Code with Pub/Sub
If you are a programmer, very likely you've heard of pub/sub.
Today I am going to show you how this pattern can be useful when applied with one of the SOLID principles.
Imagine we have a user approval feature. After approving a user, an email must be sent and a job be triggered.
This code may seem ok, but the UserApproval
class is doing so much and breaking the single responsibility principle (SRP), with time it can turn into a mess.
Let's refactor it to use the pub/sub pattern (through the Wisper gem):
Here I removed the UserApproval
class and I am calling the approve!
method directly on controller.
This method just updates the instance and broadcasts a message with a payload.
So, two distinct classes (subscribers) are responsible for doing just one thing each.
Our code now has less complexity, it's easier to test and maintain.
See you.