loading...

Navigating Dev.to's Code on GitHub

andyrewlee profile image Andrew Lee ・2 min read

Dev.to is an open source Rails application on GitHub. All Rails applications follow the same conventions which makes jumping from different Rails applications a seamless experience.

We will investigate a specific route, /dashboard, but we can repeat this process for other routes to learn about dev.to incrementally. Let's take a trip down MVC lane.

Routes

It's helpful to start from the routes file to see all the routes that we can access in dev.to. We can access the routes file in config/routes.rb.

Inside of the file we can see the following:

get "/dashboard" => "dashboards#show"

This means that when a user makes a get request to /dashboard, Rails will use the DashboardsController and run the show action.

Controller

We can find the DashboardsController here app/controllers/dashboards_controller.rb. The controller is in charge of gathering data from models and making them available to the view.

In the show action of the controller, we see that we are gathering articles from users.

@articles = target.articles

Let's take a peak at the User and Article models.

Model

A model maps directly to a table in Rails. For example the Article model maps to the articles table in the database. We can define relationships in the model.

User

For example, in the user model, we can see that a single user has many articles.

app/models/user.rb

has_many :articles, dependent: :destroy

Article

Inside of the article model, we see that a single article belongs to a user.

app/models/article.rb

belongs_to :user

View

We can complete our MVC cycle in the view file where the articles are displayed to the user.

app/views/dashboards/show.html.erb

<% @articles.each do |article| %>
  <%= render "dashboard_article", article: article, organization: article.organization, org_admin: true, manage_view: false %>
<% end %>

Conclusion

It's awesome that dev.to is an open source project! We can continue this process to incrementally learn more about the codebase.

  1. Investigate a specific route in the config/routes.rb file.
  2. Find the controller and action that the route is paired with
  3. Investigate the models that the controllers are orchestrating for the view file.
  4. Look at the view file to see what is actually rendered to the user.

Posted on by:

Discussion

markdown guide