maybe-finance / maybe

The OS for your personal finances
https://maybe.co
GNU Affero General Public License v3.0
28.71k stars 2.19k forks source link

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

Closed zachgoll closed 2 weeks ago

zachgoll commented 2 weeks ago

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:

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

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