ADHD in Children

The process analysis aspect of this post is to go more in depth into the process of finding a doctor that is right for the parent and child and that will go about diagnosing your kid the right way…


独家优惠奖金 100% 高达 1 BTC + 180 免费旋转

Policy Objects

Ruby on Rails is a well-known web application framework that follows the Model View Controller pattern. It provides a fair set of tools that let you be productive from day one, but what happens when your business logic starts getting complex and complex? The “Fat model, skinny controller” statement is just a ticking bomb until your codebase ends up having models with multiple responsibilities and mixing concerns that at the end of the day will make maintenance and development experience worse as new requirements come by. In this post, the idea is to show how to enhance a Rails application to start using patterns that let you separate concerns ala Object-Oriented Programming. Nonetheless, I suggest taking a pragmatic approach, I don’t think these patterns should always be used nor implemented in an application from day one, it depends on which stage of development you are and if the mentioned problems start to appear.

In this post I will suggest three possible patterns to implement in your application:

Let’s say that you have a blog where you have two kinds of users: authors and admins. Authors can publish their posts if they haven’t been published yet while admins can do it whenever they want.

Particularly, with Pundit, the boilerplate to connect a controller action to this would be:

It uses reflection to look for a class named like the class of your object (in this case, Post) with a “Policy” suffix.

Service objects encapsulate the actions in your application. These can be triggered by users, events, jobs or whatever triggers an action. There are many different ways to name these particular objects, either as a substantive or verb, exposing a single method named “call” or something more descriptive according to how you have named the class. In my case, I tend to use verbs, but just find a convention and stick to it.

Given our Blog example before, let’s say that now when a Post is published, we want to push notify the author’s subscribers that a new post is available.

We could do many things here:

Controller code:

The decorator pattern is used to wrap an object and extend its functionality without modifying the object itself.

When your view starts to have branching behaviour depending on the object current state, you have two options in a vanilla Rails app: either implement this logic in the template itself or use helpers to encapsulate it a bit more. Despite the latter is better than the first, I think that in bigger applications this is a problem because all the helper methods go into a global namespace accessible by the view when they are supposed to encapsulate code for a fairly low set of models or objects.

So let’s say that in our Post view, we want to show a tag “Published” or “Draft” depending on the Post status. If it’s published, we would like to show the publishing date in a human-readable format, of course.

Implementing it in a view would look like this:


Moving things to a helper would end up with a view that looks like this:


But if we use a decorator, we could encapsulate and end up with something like this:


With the patterns described above, we started to establish more boundaries between the different components that live in our application, separating concerns and letting us extend, maintain and organize our logic in a better way than a vanilla Rails setup, which encourages you to make your controllers or models big. As a side effect, we can test in isolation fairly easy, letting us get picky in scenarios we prefer to test with less boilerplate and noise (if you can’t test all).

Add a comment

Related posts:


Another action concurring this fact is a video from Mark Marson, the author of the subtle art of not giving a f*ck. In this video, love is not enough, he argues that you need more commitment. All…


COSPONSORED a Resolution to recognize May as “National Foster Care Month.” National Foster Care Month was established 27 years ago to build space for foster care issues, highlight the importance of…

Building a Quest System

In this article I would like to share my experience building quest / tutorial systems for simulation games. It will consist of a theoretical and slightly more practical part based on developing…