Structuring Rails Applications

Oct 6th, 2013

There’ve been a lot of discussions recently about applying Object Oriented Programming in Rails applications, how ActiveRecord callbacks make testing painful and how Rails makes it hard to do OOP the right way. Is it really true? Rails makes everything easy - you can easily write terrible code, which will be a maintenance nightmare, but is also easy to apply good practices, especially with available gems. What is the good way then to extract logic in Rails applications and the best place to put it?

Standard structure

By default we have four directories where we can put our code: models, views, controllers and helpers. The basic explanation of them is following:

Models - dealing with database, validations, repository, persistence.

Controllers - parsing requests, sessions, rendering views etc.

Views - user interface, data presenting.

Helpers - place to put reusable methods for views.

Does it mean these are the only places where you can put your code? No! It’s just a default structure with basic parts of your application. You can easily extend it by creating new directories and then adding them to autoload paths, e.g.:

How about lib directory?

Unless you are extracting something to a gem, I would discourage you from putting anything there. Some developers put in the lib all the code that doesn’t belong to models/controllers/helpers, but if something is part of your application why not add it to the app directory?

Skinny controllers, fat models

The design mantra for the last few years in Rails was to move logic from controllers to models. Well, it’s partially a good thing - skinny controllers are clear, easy to test and maintain, but why should models be like 1000 lines of code with tens of responsibilities (and no, everything concerning User is not a single responsibility)? It makes models in most cases the most problematic layer in Rails applications. They often include a lot of unrelated methods, conditional validations, cross-models operations, callbacks etc. - all the things that ActiveRecord makes really easy to add. So, the important question is:

Is ActiveRecord evil?

ActiveRecord is a wonderful ORM, which indeed makes everythings easy. But with great power comes great responsibility. Think about callbacks: you can add new logic in a blink of an eye and it does the job. So you keep adding other callbacks until you discover that there are some cases where you don’t want them to be executed. So you add conditionals or bypass-like methods. After some time, the logic in callbacks is so complexed that you waste few hours with other developers to understand why something was executed at all. If other developers join the project, it is even harder for them to understand what model class really does. But it isn’t the worst part. Think about some gems and their integration with Rails. Often it means extending models with another callbacks. Here are some real world problems:

Imagine the situation where you need to implement the ability for admin to register other users and the application uses Devise gem for authentication. Furthermore, the Confirmable module is included in the User class. Accidentally, you forgot to pass a date to :confirmed_at field or use confirm! method. What happens then? The confirmation email is sent to all registered users. Oops, welcome to the wonderful world of callbacks. I don’t want to criticize Devise, because it is a great gem, which I use in almost every project, but I am not sure if sending emails being directly coupled to the model layer was a good design decision. In docs you can read about the skip_confirmation! method and if you use external gem for such a critical part of your application, you should read the entire docs, but it can be really suprising, that the confirmation email is sent in all cases, even if you create user from Rails Console. Oh, and guess what happens if you want to change one’s email from the console and reconfirmable option is enabled? The confirmation email is sent… Well, remember about reading docs, especially when the gem may include callbacks.

Any other examples? Of course. So, you want to implement a generic forum. There is a gem called Forem, which provides you with basic forum functionality. And one day you want to change state of some posts to be approved. So you enter Rails Console and using update or update_attributes you perform the update. What happens then? There is a callback in Forem::Post model:

A lot of emails have been just sent! That was really unexpected. If you are used to skipping callbacks in such situatons by using update_columns method or any other way, you are safe, but callbacks are so tighly coupled to models, that you cannot be sure if you are safe, even in console. What is the conslusion? Beware of callbacks. And read docs and code of the gems you use :).

So, how to avoid unexpected situations and have clean and understandable code?

Structuring Rails applications – my way

I’ve been working on several projects and the best solution in terms of maintenance, understandability and ease of testing is the following:

Models

I use models for: factory methods, queries, scopes, general validations, which are always applicable e.g. presence and uniqueness validations for fields with null: false and / or unique: true constraints, also “domain constraints”, especially with many-to-many associations. The example of domain constraint is assigning users, who belong to the same organization, to some subgroups – assigning users from other organizations is prohibited. Putting this kind of logic in controllers’ before_filters or permission classes is not enough for me, I want to ensure the integrity of the data and make it impossible to bypass this restriction. Here is an example: we have User model and Group model and the many-to-many relationship between them, which is established by has_many , through: macro with GroupsUsers join model. Also, users and groups belong to Organization. Here is a validation for creating relation in join model:

Pretty easy to understand: everytime the record is saved, I want to have parameterized form of name, e.g. for a slug. Also, after the record is saved or destroyed, I want the statistics to be updated. For instance, in real estate search engine application, investment has many apartments and I want to keep track of total count of apartments, average price, minimum and maximum price etc. without performing calculations each time. And one more callback concerning associations: dependent: :destroy option. It is pretty useful and keeps the integrity of data, but you have to be sure when using it. If you think for a moment, these are “low-level” callbacks – they don’t concern business logic and are something that you would like to have on a database level. It can be also achieved by using trigger functions in the database, but Rails callbacks are much easier to handle.

This is not the only right way for using callbacks, if you are absolutely sure that something should really be executed as callback, feel free to use them, but please, don’t send notifications, don’t connect with Facebook API or download files from Dropbox in callbacks. You will be safe, the logic will be easy to understand and testing will be much easier.

Sometimes I use model as an interface for some service objects / usecases / whatever you call it. Here is an example:

It is a great way to have a flexibility in publishing articles – by dependency injection we can control, how it is being published – just pass publisher class as a strategy. Having default publisher makes it easy to use: just call article.publish . Also, calling article.publish in e.g. controller feels much better than calling DefaultPublisher(article).publish. If you have very simple logic, like this one:

don’t bother with extracting it to external class, it would be pointless.

Controllers

Everything related to parsing requests, sessions, rendering templates, redirecting and flash messages should be put in controllers. What about application logic? In most cases it should be limited to a control-flow, for example:

1234567891011121314

classArticlesController<ApplicationControllerdefcreate@article=Article.find(params[:id])if@article.saveflash[:notice]="You have successfully created an article."redirect_toarticles_pathelserender:newendendend

Depending on the action, it could be much more complex. Consider the following:

123456789101112131415161718192021222324252627

classOrdersController<ApplicationControllerdefbuyorder=Order.new(order_params)order_proccesor=BooksOrderProcessor.new(order,current_user)beginorder_processor.payrescueBooksOrderProcessor::InsufficientAmountflash.now[:error]="Not enough books are available."render:newrescueBooksOrderProcessor::InsufficientFoundsflash[:error]="You have run out of funds."redirect_toprofile_path(current_user)elseflash[:notice]="You have bought a book."redirect_tobooks_pathendendprivatedeforder_paramsparams.require(:order).permit!endend

It is still good, such control-flow can take place in a controller, but order processing logic cannot. But what should be done with creating articles and sending notification or logging action? If it is only one additional line of code with method call like Tracker.register(“create”, @article) or NewArticleNotfier.delay.notify, for example:

123456789101112131415

classArticlesController<ApplicationControllerdefcreate@article=Article.find(params[:id])if@article.saveNewArticleNotfier.delay.notifyflash[:notice]="You have successfully created an article."redirect_toarticles_pathelserender:newendendend

don’t extract it to usecase or service object, it is ok to keep it in a controller.

Cells

You have probably had many situations with setting up the same instance variables in several controller actions for some widgets like: tags cloud, recent articles, recent comments, top visited articles etc. and it can be really inconvenient. Fortunately, there’s a great gem: Cells, which are like mini controllers. Consider the following:

In cells/articles/top_visited.html.haml/erb you put the related markup and invoke cells from views by:

1

=render_cell:articles,:top_five

Another great thing about cells is that you can inject a dependency:

1

=render_cell:widgets,:newsletter,newsletter_form:@newsletter_form

You can easily create a widget with newsletter submission form with enabled remote: true option and then render error messages or notice message that the email has been submitted.

Helpers

In most cases I use helpers to extract some things that aren’t related to any particular model – rendering flash messages, titles, html templates etc., so nothing really fancy. Everything else should be extracted to the presenters/decorators. The good example of helper is the following:

Before each section I had to insert header with nested icon and some text, so instead of writing the same thing several times, I extracted it to a helper, which is much cleaner. It doesn’t belong to any model, so helper is a good place to put this kind of code. If you use Boostrap a lot, you may consider writing modal_activator method:

Presenters/Decorators

Helpers aren’t the best place to extract logic related to models – the code in helpers tends to be messy and difficult to maintain and test. The good solution would be to use Presenters – objects that encapsulate presentation logic in a neat way. There’s a gem that is perfect for this kind of problems: Draper. Just create a decorator class, like UserDecorator:

Looks great! You don’t have to keep presentation logic in helpers or even worse in models.
You have an access to Rails helpers via h, also all method calls are delegated to model if it isn’t implemented in a decorator. To decorate model just use decorate method:

1

@user=User.find(params[:id]).decorate

You can also decorate collection by using decorate method.

Forms

Imagine a situation where you need a form concerning more than one model. Also, some conditional validation is required. What would you do? Probably use nested attributes and add some complex validations, which would make model messy and maybe cause some bugs. There’s much better way to do it: use form object and Reform gem. Then you can create following objects:

By using Reform gem, you can easily create clean form objects, which would deal with validations, coercions (thanks to Virtus) and persisting data concerning multiple models. Also, you can put some form interface logic here – consider countries_collection method: instead of passing: collection: Country.all.pluck(:id, :name) to the select field, you can just pass form_object.countries_collection. This example is trivial, but if you had some filtering and ordering logic needed to display collection, then it would be great way to keep everything clean. Using form objects doesn’t change control-flow in controllers:

Form objects are also great way to extract search forms and logic related to filtering. Reform can deal with has_many associations and nested collections, so it is pretty powerful. However, there are some cases where you would still want to use accepts_nested_attributes_for – when you need funcionality provided by nested_form gem. It is not really clean to use accepts_nested_attributes_for macro, but the benefits are great. In other cases, form object is a way to go.

Uploaders

For file uploading I use Carrierwave where the uploaders’ configuration is kept in the /uploaders directory. That’s a really good approach, because thumb-processing strategy etc. has nothing to do with ActiveRecord model, so there’s no reason to keep this kind of information there. Here is an example of general uploader:

Services

You may expect that in /services directory so called service-objects would be placed. Well, not really. I prefer to call them (service objects) usecases and in /services I put some wrappers concerning third party APIs. In one application I’ve been working on, I had to deal with Google Calendar integration and the adapter layer for performing requests to GC was a service, for instance:

1234567891011121314151617

moduleGoogleCalendarclassCalendarsattr_reader:clientdefinitialize(client)@client=clientend#some other methodsdefpatch(calendar_id,calendar_data)client.execute(api_method:client.service.calendars.patch),body:calendar_data,parameters:{"calendarId"=>calendar_id},headers:{'Content-Type'=>'application/json'})endendend

In smaller applications you won’t probably need this layer, otherwise it is a neat way to separate services from the rest of the application.

Usecases

This is a place where most of the business logic should be extracted – almost everything that would be in models, according to “skinny controllers, far models”. The benefits of using usecase objects / service objects are great – they area easy to undestand and maintain, testing is simple and don’t lead to unexpected actions (like the ones I pointed out in callbacks). Let’s take a look again at the user registration process from form object, the implementation of UserRegistration could be following:

Let’s discuss some design choices here: in the constructor I added a possibility to inject some notifiers and provide reasonable defaults using Hash#fetch method to avoid nils. In register method I wrap the persistence process in ActiveRecord::Base.transaction, to ensure that user is not created without the profile if any error occurs (notice the bang methods), if it fails, the exception is raised. Then, some notifiers are called, one is a mailer, the other one connects with an external service and does some stuff. They should be executed asynchronously, in Delayed Job, Resque or Sidekiq to make sure they are completed if failure occurs – there might be a temporary problem with connecting to Gmail, Facebook, Twitter etc. but it’s not a reason for an entire registration process to fail.

Policies

Policy objects are a bit special – it’s a decorator, but Draper decorators are not the best place to put them, because they concern presentation logic. Models, except small applications, are also wrong place to write policy logic as they encapsulate important domain logic which can be quite complex. So it is a good idea to create separate objects – policy objects. Depending on the size of your application, you may have several policy objects or just one for a model, here is an example:

In the constructor I pass an investment and the clock, which by default is DateTime. I had some issues with concept of time, especially in policy objects where I had to implement own Clock, because DateTime was not sufficient, so just to be on a safe side, I add a possibility for a dependency injection. It doesn’t increase a complexity of the class and I wouldn’t consider it as a premature optimization. Then we have some methods that encapsulate promotion logic and one that returns proper status. You will probably use policy objects in many cases – e.g. promotion_status method looks like it could be used in a presenter, which would display proper content, depending on the returned status and the promoted? method could be used in the usecases or in a model class method that would return promoted investments. You can use policy objects in many ways: inject into a model and delegate method calls:

12345678910111213

classInvestment<ActiveRecord::Basedelegate:promoted?,:owner_promotable?,:promotion_statusto::promotion_policy# some methodsprivatedefpromotion_policy@promotion_policy||=InvestmentPromotionPolicy.new(self)endend

In the same way you can use them in presenters if you don’t want to keep it in the model layer. They can also be injected as a dependency into a usecase. Choose whatever suits you better and the complexity of the application.

Value objects

In many applications you may encounter a situation where a concept deserves own abstraction and whose equality isn’t based on identity but on the value, some examples would be Ruby’s Date or Money concept, typical for e-commerce applications. Extraction to a value object (or domain model) is a great convenience. Imagine a situation where you have a hierarchy of roles of users – you will probably want to compare the roles if one is “greater” than another to decide, if some action can be performed – the RoleRank would solve this problem:

What about Observers and Concerns?

There are two more ways to extract logic “the standard way”: observers (removed from Rails 4) and concerns. Observers aren’t really different from callbacks, except they are more difficult to deal with – you will probably forget that you’ve used them and debugging or following the application logic would be even harder than in callbacks and I’m glad they were removed. And the new thing in Rails 4: concerns. They are great, expecially for shared behaviour. When you need to parameterize field name in several models, you can extract it to a module in concerns, and then include Parameterizable concern, some custom finders and factory methods also can be extracted into concerns. If you use the same before_filters in more than controller, extracting them to the concerns would be a good idea. In Presenters, such concern as Presenters::Publishable would be beneficial when you have articles, posts and some other models that act in a similar way, so introducing concerns and encouragement to extract similar behaviour was definitely a good idea.

Any other application layers?

Depending on your application, you may introduce additional layers like JSON representations of models, jobs that should be done in a background or XML importers, but they can be considered as presentation logic (jsons) or usecases / services (importers, background jobs). If XML importers are important part of your business logic, then maybe extracting them from usecases and treating in a special way would be beneficial.

Wrapping up

The concepts mentioned here might not be popular among some developers and be considered as over-engineering. When writing simple CMS, introducing services, form objects etc. probably is a premature optimization and models / contollers / helpers and maybe presenters would be sufficient, including writing business logic in ActiveRecord callbacks. But in more complex application applying some OOP techniques can save you (and other developers you work with) from a lot of problems.

Comments

About Me

Hi, my name is Karol Galanciak, I'm a technical polyglot and domain expert in vacation rental and fitness industries.

I specialize in building APIs, microservices architecture, working with legacy applications, building ambitious Single Page Applications and building payment processors for credit cards processing. Currently I'm working as CTO at BookingSync, so at this moment I'm not available for Ruby on Rails or Ember.js consulting.

Overwhelmed by too much information?

Following all the newsletters, RSS feeds, Twitter and other sources might be overwhelming. Stop wasting your valuable time - subscribe now and by the end of every month I will send you an email with the list of the most interesting Ruby / JavaScript (Ember.js) / PostgreSQL articles with a quick overview.