This week in #dev (Nov 29, 2024)

thoughtbot
Edited by Matheus Richard

Welcome to another edition of This Week in #dev, a series of posts where we bring some of our most interesting Slack conversations to the public.

Understanding Rails.env.local?

Neil Carvalho shares a tip: Rails.env.local? returns true if the environment is either development or test.

Localizing models with Active Model

Joël Quenneville shares some exciting tips about ActiveModel. By using ActiveModel::Naming and ActiveModel::Translation, model-level translations can be applied to both persisted and in-memory models, like a Guest object for anonymous users. With a locale file, you can localize model and attribute names:

en:
  activemodel:
    models:
      guest: Customer
    attributes:
      guest:
        login: "Handle"

This allows access to methods for localized names:

@user.model_name.i18n_key           #=> :guest
@user.model_name.human              #=> "Customer"
@user.human_attribute_name("login") # => "Handle"

Additionally, Rails validation errors will also be localized. For more information, check out these resources:

Thanks

This edition was brought to you by Joël Quenneville and Neil Carvalho. Thanks to all contributors! 🎉

About thoughtbot

We've been helping engineering teams deliver exceptional products for over 20 years. Our designers, developers, and product managers work closely with teams to solve your toughest software challenges through collaborative design and development. Learn more about us.