Filter pattern in Rails

Imagine you have an User < ActiveRecord::Base object and you want to apply filtering implementation to it. The filtering will be based on certain criteria like get me all the adult users or all the users that are females or males. That kan be achieved in different ways. One way is using Rails's scoping or by defining class methods. If you like the idea of the design patterns, it turned out that there is a design pattern for this problem and it is called the Filter design pattern. And to demonstrate, We will create an interface class to outline the behaviour of our Filtering criteria. This interface can then be extending with concrete criteria classes containing concrete implementation. This is the domain class I skissed to describe it better: cd

And the code:

By doing so, the User class will look cleaner for me…

To play around with the newly created filters in rails console:

Thank you!