In a Rails 8.1 print-shop app, where should a class that builds PDF invoices live: app/models, a new app/services folder, or lib/?
answer
- domain code belongs under app/
- any app/ subfolder is a root
- folder name is not a namespace
- lib/ for code not about the domain
- restart once after a new folder
basics
~20 sDomain code goes under app/: a plain class in app/models or a role folder such as app/services or app/pdfs; lib/ is for code that is not about the print shop and could become a gem. A new app/ folder needs no configuration, only a restart.
solid answer
~40 sThe invoice PDF builder is print-shop domain logic: it knows what an order, a customer and a tax line are. So it belongs under `app/`. `app/models/invoice_pdf.rb` is fine, because `app/models` is not reserved for tables. When several such objects pile up, a role folder like `app/services` or `app/pdfs` keeps them together; Rails treats every subfolder of `app/` present at boot as a root, so `app/services/invoices/pdf_builder.rb` defines `Invoices::PdfBuilder`, not `Services::Invoices::PdfBuilder`, and needs only a server restart. `lib/` is for code that does not depend on the domain, such as a generic table-layout helper you might extract into a gem. Put the test where it mirrors the class, such as `test/services/invoices/pdf_builder_test.rb`.
code
ruby · 15 lines# app/pdfs/invoice_pdf.rb
class InvoicePdf
def initialize(invoice)
@invoice = invoice
end
def render
PdfTable.new(rows: line_rows, widths: [ 300, 80, 80 ]).render
end
private
def line_rows
@invoice.line_items.map { |item| [ item.description, item.quantity, item.total ] }
end
endgo deeper
Recall that domain classes live under app/, that app/models may hold plain classes, and that lib/ is for generic code.
Explain how a new app/ folder becomes a root, why its name is not a namespace, why a restart is needed, and where the matching test goes.
Choose between app/models, a role folder and lib/ by meaning, and keep a services folder from becoming a dumping ground of vague classes.
Set the team's taxonomy for app/ subfolders and when shared code graduates from lib/ to an internal gem.
## The decision in one line **Domain code lives under `app/`; generic code lives in `lib/`.** A class that turns an `Order` into a PDF invoice, with the print shop's logo, tax rules and line items, is domain code, however little it has to do with HTTP or the database. ## Option 1: `app/models` `app/models` is not reserved for Active Record classes. Plain Ruby objects that represent concepts of the business belong there as much as tables do: - `app/models/invoice_pdf.rb` defining `InvoicePdf`, or - `app/models/invoice/pdf.rb` defining `Invoice::Pdf` next to the `Invoice` model. This is the choice with the least ceremony: the class sits next to the records it reads. ## Option 2: a role folder such as `app/services` or `app/pdfs` When a kind of object multiplies (invoice PDFs, packing slips, proof sheets), a dedicated folder groups them. Rails has no list of allowed folders. Facts that matter: 1. **No configuration**: every subfolder of `app/` that exists when the app boots is added as a root for constant lookup, except `assets`, `javascript` and `views`. Create the folder, then restart the server or console once, because the list is built at boot. 2. **The folder name is not a namespace**: `app/services/invoices/pdf_builder.rb` must define `Invoices::PdfBuilder`. Writing `Services::Invoices::PdfBuilder` there fails to load, because `app/services` is a root, just like `app/models`. 3. **Generators do not know the folder**: tests go where they mirror the class, such as `test/services/invoices/pdf_builder_test.rb`, which `bin/rails test` picks up because it runs `test/**/*_test.rb`. | Folder | Good for | Watch out for | |---|---|---| | `app/models` | a few domain objects | crowding once dozens of non-table classes appear | | `app/services` | operations that orchestrate several models | becoming a dumping ground of `*Service` classes | | `app/pdfs` (named by role) | one family of objects | too many tiny folders | | `lib/` | code with no knowledge of the domain | stale code if nobody owns it | ## Option 3: `lib/` `lib/` suits code that would work in any app: a wrapper that lays out a table on a PDF page given rows and widths, or a small client for an external API with no print-shop knowledge. In a Rails 8.1 app, `config.autoload_lib(ignore: %w[assets tasks])` in `config/application.rb` makes those files loadable by constant name, so the choice between `app/` and `lib/` is about **meaning**, not about whether the code loads. A good test: if you could publish it as a gem without renaming anything, it fits `lib/`. ## A reasonable layout for the print shop - `app/models/order.rb`, `app/models/invoice.rb`: tables. - `app/pdfs/invoice_pdf.rb`: builds the invoice document from an `Invoice`. - `lib/pdf_table.rb`: generic table drawing used by `InvoicePdf` and future documents. - `app/jobs/invoice_pdf_job.rb`: renders the PDF in the background and attaches it. ## Mistakes interviewers listen for - Putting domain logic in `lib/` "because it is not a model", which hides it from people browsing `app/`. - Creating `app/services` and then namespacing classes as `Services::...` by habit. - Expecting the new folder to be picked up without a restart. - Building the PDF inside the controller action, so the logic cannot be reused or tested on its own.
- Why does `Services::Invoices::PdfBuilder` in `app/services/invoices/pdf_builder.rb` fail to load?`app/services` is itself a root, like `app/models`, so the path below it maps to `Invoices::PdfBuilder`. The loader expects that constant and finds a different one. To get a `Services` namespace you would need `app/services/services/...`, or a custom namespace setup, which is the autoloading leaf's subject.
- When would you move the PDF table helper from `lib/` into its own gem?When a second app needs it, or when it has its own release cadence and tests. Until then `lib/` keeps it in the same repository with the same review process, without the overhead of versioning and publishing a gem.
- Only Active Record classes may live in app/models.
- A new app/services folder must be added to config/application.rb first.
- Files in app/services must be namespaced Services::Something.
- Anything that is not a model, view or controller goes in lib/.
- New folders under app/ are picked up without restarting the server.