iTranslated by AI

The content below is an AI-generated translation. This is an experimental feature, and may contain errors. View original article
📌

[Reading Log] Learning Ruby on Rails through Code Review

に公開

I read Learning Ruby on Rails through Code Reviews, and I've summarized what I learned and my impressions.
Note that due to my procrastination, it's been over a month since I bought it.

Background

My situation:

  • 1 year and 5 months since becoming an engineer from a non-tech background
  • Learned Ruby on Rails after joining the company
  • Recently, opportunities to conduct code reviews have been increasing
  • Currently studying Rails-like coding styles and design by reading various articles and books

Points I Learned

1.6 Setting Constraints in the Database

TIPS: Setting an empty string as a default value along with NOT NULL for string columns was very insightful.

There were cases where I struggled with how to handle NULL values in string columns created by others on the Rails side.
Although it's a simple constraint, it solves implementation concerns by itself, so I want to make sure to apply this constraint when I design databases myself.

2.1 Don't Write Complex Logic in Views

I often hear that "Fat Controller should be avoided," but before that, Fat View should be avoided even more.
If you're not careful, conditional branching occurs in the View, so I often request the creation of a Decorator during code reviews.

I would like to use View Component, but the Gem isn't installed in the repositories I'm involved with, and I don't think it's worth the effort to define rules and implementation examples from scratch.
I try to avoid using helpers as much as possible because they can be called from anywhere and tend to become disorganized.
(I'd really appreciate it if people didn't call them in controllers...)

2.4 Use I18n.l for Time and Date Formatting

I learned about this for the first time. I want to use it actively.
It was also helpful to learn about the case where the value is nil.

3.1 Add 'order' When Fetching Lists

I hadn't been mindful of this either, so it was a good learning point.
Moving slightly away from the content, there was a case where I was ordering by created_at. Since only the date was being referenced where it was called, a bug occurred when two records were created on the same day.
I want to be careful about the order of records in the future.

3.2 Fetching Objects Safely

I didn't have many chances to be conscious of this either, so it was informative.
For cases where records are fetched by an associated ID, I had been writing code like the example on page 37 until now.

As shown in the proposal section's example, starting with fetching values from the logged-in user, I will make an effort to fetch from the parent object when an association exists.

4.2 Show the Basis for Expected Values Within the Test Code Itself

Basically, I used to consolidate everything that could be handled by FactoryBot into FactoryBot, but reading this section changed my mind.

Given the idea that tests serve as documentation, I felt that preparing the values used for expectations within the test code itself makes it more self-contained and improves maintainability.

4.5 Write Input and Expected Values as Literals

I often find myself using variables if I'm not careful, so this was a good reminder.

4.6 Reduce Writing Burden Using factory_bot's trait

This is something I also strive for and often point out in code reviews.
What I learned this time was to avoid creating associated records by default.
When I copied test code from one repository without much thought, I ended up having to rewrite it because the FactoryBot association settings differed between repositories.
I could have avoided such a situation if I had used traits.

Things I noticed in code reviews

Although it's not directly related to the main topic of the book, I'll write about some things I noticed while doing Rails code reviews.

Whether or not to write Model content directly in the View

For example, let's say we have a category column in the Article model and we created a scope to extract specific categories as follows:

class Article < ApplicationRecord
  SPECIFIC_CATEGORIES = ['Category1', 'Category2', 'Category3', 'Category4', 'Category5', 'Category6', 'Category7', 'Category8', 'Category9', 'Category10', 'Category11', 'Category12']
  scope :specific_categories, -> { where(category: SPECIFIC_CATEGORIES) }
end

When calling this in the View, I had been writing it like this for a while:

class ArticlesController < ApplicationController
  def アクション名
    @specific_articles = Article.specific_categories
  end
end
<%= form_with(model: @article, url: articles_path, method: :get) do |form| %>
  <%= form.select :category, options_for_select(@specific_articles), prompt: 'カテゴリを選択してください' %>
  <%= form.submit '検索' %>
<% end %>

However, after reading the following article this spring, I changed it to the description below.

【新人プログラマ応援】Railsにおける良いコントローラ、悪いコントローラについて

<%= form_with(model: @article, url: articles_path, method: :get) do |form| %>
  <%= form.select :category, options_for_select(Article::SPECIFIC_CATEGORIES), prompt: 'カテゴリを選択してください' %>
  <%= form.submit '検索' %>
<% end %>

So, when I see this kind of description during code reviews, I point it out, but I received a comment saying, "No, it's not good to write model content directly in the View, so I'll write it in the controller."
In the article above, it says:

Personally, I believe it's better not to do it unless you "absolutely" have to.

(Bold emphasis added by me)

As it says, there seem to be differences in how people think about what should be written where among the Model, View, and Controller.

GitHubで編集を提案

Discussion