Application Structure (feat. Separation of Concerns)
Tags: ruby, filestructure, separationofconcerns, sinatra
Published: 2022-08-22 03:46:00

When I first started learning Ruby and Sinatra framework, it took me quite some time to get myself familiarized with the application structure.  The idea of this whole file structure thing at first looked like this to me:

![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/u0938iydltwt22qktxsj.png)

I mean okay, separation of concern is important, but how do I know what to put, and in where?  For those who may be struggling to understand the application structure concepts, this post can guide you through what each of your folder(directory) and file should contain.

---

## What is "Separation of Concerns"?

To understand the whole idea of creating this complex (and even intimidating) structure before working on your application, understanding _separation of concerns_ would be the key concept to get familiarized with first.  "Separation of concerns" is a design principle for separating a computer program into distinct sections.  Without following it, your application might get very messy and hard to understand for other developers.  It's like having your belongings organized in the right place.  You would want your toothbrush in the toothbrush holder in your bathroom, cups in the cabinet of your kitchen, your mouse to the right-hand side to your laptop or keyboard (given that you use your mouse set up for right-handed people.  (~~TMI: I'm left-handed but use a right-handed mouse.~~), and so on.

![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/9t933jbjzfv16qfwz13o.png)

Just like that, you would want your codes located in the proper file and the proper folder.  Not so related to the topic of separation of concerns, but it is also very important to name your files and folders correctly when it comes to using Active Record (a Ruby gem that will save you lots of time when writing object-relational mapping or ORM), as it is a proponent of using certain formats and grammars to ensure locating correct classes and running correct methods.  You can find more info on the Active Record's naming conventions [here](https://guides.rubyonrails.org/v4.1/active_record_basics.html#naming-conventions).

---

## Sinatra Application Structure

Alright, let's get down to business.  Below is how you should set up your folders and files structure for your application, with each of their own responsibility:

- 
`app/models`: This folder contains Active Record model files with the file extension of `.rb`.  Each model file contains code that accesses and updates data in the database using classes that inherit from `ActiveRecord::Base`.

- 
`config` (usually contains the `environment.rb` file): Code in this folder is responsible for environment setup, like requiring files/gems, and establishing a connection to the database.

- 
`db/migrate` (usually contains `timestamp_create_(model_name_in_plural).rb` files): This folder contains Active Record migration files.  Each migration file contains code that is responsible for creating and altering the structure of the database (making new tables, adding columns to existing tables, etc).

- 
`db/seeds.rb`: This file allows us to easily add sample data to the database.

- 
`Gemfile` (a file with no extension): This file lists all the gems/dependencies the application would need to depend on.

- 
`Rakefile` (a file with no extension): This file contains code for common tasks that you can easily run from the command line, such as `rake console`.


Once you get comfortable with the concept, it's not that complicated after all.  In fact, you will even feel more comfortable following this convention before you know it.

---

## Resources
[https://en.wikipedia.org/wiki/Separation_of_concerns](https://en.wikipedia.org/wiki/Separation_of_concerns)