Do you remember our article about the most popular RoR gems? There we created the scope of gems are still in use from 2014. Using them you’ll definitely make your work simpler because all you need is already created. But “the times they are a-changin'” as Bob Dylan said. So today we have even better gems to share with you. Syndicode has found a treasure chest with 7 gems to provide better RoR architecture.
If you’re passionate about the application architecture there’s nothing better than new tools to improve it. You should be careful about your application design because at some point you will find yourself in a position where in order to add a new feature it’s just easier to rebuild the whole project from the scratch… And your team is forced to spend a significant amount of time just reading through the source code in order to understand the logic.
We want to share with you 7 gems, which can help you organize your code in a better way.
Interactor is a really outstanding library for writing some complex business logic. Literally, “an interactor is a simple, single-purpose object, used to encapsulate your application’s business logic”. It is not just a service-object.
It’s often used for all of our destructive actions (
DELETE requests) and since we put our interactors in
app/interactors, a glance at that directory gives any developer a quick understanding of everything the application does.
Great features of this gem:
- The first thing is that you are able to organize your simple interactors to an executable chain, which will be executed in proper order. Special variable context is being used to share states between different interactors.
- The second thing is that if one of the interactors fails for some reason, all previous will be rolled back.
If you have ever used custom Rails-helpers, you know how messy they become over time. And in most cases, we use them to display some data in a more fancy way. That’s where decorator design pattern can help us.
Draper adds an object-oriented layer of presentation logic to your Rails application. Without Draper, this functionality might have been tangled up in procedural helpers or adding bulk to your models. With Draper decorators, you can wrap your models with presentation-related logic to organize – and test – this layer of your app much more effectively.
Sometimes using a simple Ruby object is not enough for you. Imagine you have some complex form on a page, where different pieces form should be saved as different models in the database. That’s where virtus can help you.
Virtus allows you to define attributes on classes, modules or class instances with optional information about types, reader/writer method visibility, and coercion behavior. It supports a lot of coercion and advanced mapping of embedded objects and collections. You can use it in many different contexts like:
- Input parameter sanitization and coercion in web applications
- Mapping JSON to domain objects
- Encapsulating data-access in Value Objects
- Domain model prototyping
And probably more.
Cells allow you to encapsulate parts of your UI into components into view models (or cells, which are simple ruby classes that can render templates). Nevertheless, a cell gives you more than just a template renderer. They allow proper OOP, polymorphic builders, nesting, view inheritance, using Rails helpers, asset packaging to bundle JS, CSS or images, simple distribution via gems or Rails engines, encapsulated testing, and caching.
Not all of us ready to apply this whole concept to their existing applications. But sometimes your views become really complicated, with different conditions applied for different types of user and etc. That’s where Cells gem could help. It allows moving part of your views to isolated components, which are just regular ruby-classes.
Retryable gem runs a code block and retries it when an exception occurs. It’s configured using several optional parameters
:sleep_method and runs the passed block. Should an exception occur, it’ll retry for (n-1) times. Should the number of retries be reached without success, the last exception will be raised.
All modern web application do have different types of integrations. Sometimes it’s made through solid API calls, sometimes you have to upload a file to FTP or even use some binary protocol. Problem with all integrations is that sometimes their calls just fail. In some cases – it fails without any reasons. And the best thing you can do is just to try again. But here is Retryable gem to help you!
Decent Exposure allows us to create exposed variables that can be used across the controller’s actions instead repeating instance variables. Exposed variables are accessible from views as well and memorize the resultant values. In other words, it is a helper for creating declarative interfaces in controllers.
In some applications we have a lot of duplications for very simple and standard CRUD actions. With Decent_exposure gem, we can significantly reduce the number of code lines.
Every developer knows that dealing with different time zones is a hard thing. Especially when you are trying to write some aggregation in your database. You can stop worrying about this with Groupdate gem. This is the simplest way to group by day, week, an hour of the day and more.
We hope these gems will help you in creating better RoR architecture!
Subscribe to our weekly newsletter to find more interesting information.