Clutch

7 gems to provide better RoR architecture

7 gems to provide better RoR architecture
Average rating: 0
(0 votes)

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.

1. Interactor

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 (POSTPUT and 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.

2. Draper

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.

3. Virtus

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.

4. Cells

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.

5. Retryable

Retryable gem runs a code block and retries it when an exception occurs. It’s configured using several optional parameters :tries:on:sleep:matching:ensure:exception_cb:not: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!

6. Decent_exposure

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.

7. Groupdate

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.

Rate this article, if you like it

Thanks! You’ve rated this material!

Got a project? Let's discuss it!

*By submitting this form you agree with our Privacy Policy.

Mailing & Legal Address

Syndicode Inc. 340 S Lemon Ave #3299, Walnut CA, 91789, USA

Visiting & Headquarters address
Kyiv Sofiivska 1/2a, 01001, Kyiv, Ukraine
Dnipro Hlinky 2, of. 1003, 49000, Dnipro, Ukraine
Email info@syndicode.com
Phone (+1) 9035021111