Account namespace updates: part 4 (transfers, singular namespacing) #896

Merged
zachgoll merged 4 commits from zachgoll/account-namespace-updates-part-4 into main 2024-06-21 01:32:44 +08:00
zachgoll commented 2024-06-20 22:54:53 +08:00 (Migrated from github.com)

See #892 for a detailed proposal of these changes. I will be opening several PRs to achieve the final state so there is a more readable history of the changes.

Part 4 addresses the following:

  • Renames Transfer to Account::Transfer
  • Standardizes some of our namespacing (read section below)

Namespacing convention

Previously, we had all of our namespaces in the plural form, which presented a few challenges with automatic path and partial resolutions and forced us to maintain a slightly hard-to-read routes file.

To explain the issue, we can look at Account::Transfer as an example:

@transfer = Account::Transfer.new

@transfer.to_partial_path # account/transfers/transfer

By default, Rails does not automatically "pluralize" the namespaced prefix, Account::. With our prior setup, we had the Transfer partial sitting at /accounts/transfers/_transfer.html.erb.

This made it so the following line would never resolve by "convention":

<%= render @transfer %>

In an effort to make this codebase as predictable as possible for new contributors, I've updated all of these namespaces to match the singular form and use the following convention (using the namespaced Account::Transfer model as example):

Routes

namespace :account do
  resources :transfers, only: %i[ new create destroy ]
end

resources :accounts do
  # other routes

  # Namespaced, nested resource
  resource :logo, only: :show, module: :account
end

Controllers, Views, Locales

  • Controllers
    • AccountsController
    • Account::TransfersController
  • Models
    • Account
    • Account::Transfer
app/controllers
├── account
│   └── transfers_controller.rb
├── accounts_controller.rb
app/views
├── account
│   └── transfers
│       ├── _form.html.erb
│       ├── _transfer.html.erb
│       └── new.html.erb
├── accounts
│   ├── _account.html.erb
│   ├── edit.html.erb
│   ├── index.html.erb
│   ├── new.html.erb
│   ├── show.html.erb
config/locales/views
├── account
│   └── transfers
│       └── en.yml
├── accounts
│   └── en.yml
See #892 for a detailed proposal of these changes. I will be opening several PRs to achieve the final state so there is a more readable history of the changes. Part 4 addresses the following: - Renames Transfer to Account::Transfer - Standardizes some of our namespacing (read section below) ## Namespacing convention Previously, we had all of our namespaces in the plural form, which presented a few challenges with automatic path and partial resolutions and forced us to maintain a slightly hard-to-read routes file. To explain the issue, we can look at `Account::Transfer` as an example: ```rb @transfer = Account::Transfer.new @transfer.to_partial_path # account/transfers/transfer ``` By default, Rails does not automatically "pluralize" the namespaced prefix, `Account::`. With our prior setup, we had the Transfer partial sitting at `/accounts/transfers/_transfer.html.erb`. This made it so the following line would never resolve by "convention": ```erb <%= render @transfer %> ``` In an effort to make this codebase as predictable as possible for new contributors, I've updated all of these namespaces to match the singular form and use the following convention (using the namespaced `Account::Transfer` model as example): ### Routes ```rb namespace :account do resources :transfers, only: %i[ new create destroy ] end resources :accounts do # other routes # Namespaced, nested resource resource :logo, only: :show, module: :account end ``` ### Controllers, Views, Locales - Controllers - `AccountsController` - `Account::TransfersController` - Models - `Account` - `Account::Transfer` ```bash app/controllers ├── account │   └── transfers_controller.rb ├── accounts_controller.rb ``` ```bash app/views ├── account │   └── transfers │   ├── _form.html.erb │   ├── _transfer.html.erb │   └── new.html.erb ├── accounts │   ├── _account.html.erb │   ├── edit.html.erb │   ├── index.html.erb │   ├── new.html.erb │   ├── show.html.erb ``` ```bash config/locales/views ├── account │   └── transfers │   └── en.yml ├── accounts │   └── en.yml ```
Sign in to join this conversation.