Design – Where to define view-specific data objects in Rails

designruby-on-rails

Background: there are several places in our app where we want to display date-oriented information on a calendar. The event_calendar gem looks nice and has lots of reviews, so it's what I'm planning to use. However, it's designed to work with a model. I'm not going to update our existing models to meet its needs, so will create a DTO class and a helper function to construct an array of instances from our existing models.

My question is: where should that DTO class live? It seems like it should be defined inside ViewHelper, but that will violate DRY as soon as I use the calendar for two models (note: the helper functions will differ, it's just the DTO class that will remain the same).

So, is there a standard place in the Rails directory tree where non-model-related classes live?

Or is there a better/more idiomatic way to do this? One thought that I had was to make my helper function decorate the existing model instances with the methods that event_calendar wants to see.

Best Answer

I think the equivalent of the Data Transfer Object you mention is called a presenter in the Rails ecosystem.

On projects with few presenters, I have seen them placed in the /lib directory, but you might as well create an /app/presenters directory.

Also check out this blog post on presenters by Steve Klabnik.