DEV Community

Cover image for Rails guide - Project patterns - Part 6
Augusto Queirantes
Augusto Queirantes

Posted on

Rails guide - Project patterns - Part 6

Hey guys! How are you?

This article is a part of a series that will teach how to create a production ready rails application. In this article we gonna talk about project patterns and how we'll organize our project.

Table of content

Rails way to do things

Rails is a framework known by their conventions, you can find the following quote in Rails official documentation

Rails is opinionated software. It makes the assumption that there is a "best" way to do things, and it's designed to encourage that way - and in some cases to discourage alternatives. If you learn "The Rails Way" you'll probably discover a tremendous increase in productivity. If you persist in bringing old habits from other languages to your Rails development, and trying to use patterns you learned elsewhere, you may have a less happy experience.

This quote clearly says that we need to organize our software in a way that respects Rails standards, so, that's how we gonna do this:

Project patterns

We gonna organize our software following these principles:

  1. Controllers will have no logic, it'll either validate parameters or call another class
  2. Services will interact with thirdy party services
  3. UseCases will create data into our database and interact with our models
  4. Models will store application logic

If you already read about Clean Architecture probably noticed that the project structure follows the ideia that pattern suggests.

Folder organization

In the end of the day, we'll use mostly the following folders in our project:

- app
-- controllers
-- models
-- services
-- use_cases
- lib
Enter fullscreen mode Exit fullscreen mode

The controller folder is our interface to the web, that's where we'll define our routes actions.

The models folder is where we define our entities and some business logic.

The services folder is our interface to third party applications, such as external apis.

The use_cases folder is our interface to our entities.

The lib folder will contain all code that's not directly related to our project core, such as parsers.

Conclusion

I hope everyone understood why we gonna organize the project using this method, please comment if there is any questions.

Top comments (0)