Skip to content

[Rendering] Custom renderer support#244

Open
anuj-pal27 wants to merge 1 commit intorage-rb:mainfrom
anuj-pal27:feat/custom-renderers-234
Open

[Rendering] Custom renderer support#244
anuj-pal27 wants to merge 1 commit intorage-rb:mainfrom
anuj-pal27:feat/custom-renderers-234

Conversation

@anuj-pal27
Copy link

@anuj-pal27 anuj-pal27 commented Mar 20, 2026

Summary

Implements the config.renderer(:name) { ... } DSL proposed in the issue.
Registers custom renderer blocks and generates render_<name> methods on
RageController::API at boot time.

Key behavior

  • config.renderer(:csv) { |obj, **opts| ... } registers a renderer block
  • After __finalize, every controller automatically gets render_csv, render_phlex, etc.
  • Generated methods support status: as both Symbol (:created) and Integer (201)
  • Unknown status symbols raise ArgumentError instead of setting nil status
  • Renderer blocks run in controller context — headers, request, params all work
  • Double-render is prevented whether render happens via return value or internal render call
  • RageController::API.__custom_renderers registry makes finalize idempotent across re-runs

Tests

9 specs covering:

  • Basic registration and method generation
  • Status handling (symbol, integer, default 200)
  • Duplicate renderer registration guard
  • Method name conflict detection
  • Controller context access (headers, params)
  • nil return value → empty string body
  • Renderer block calling render internally
  • Double-render protection

Notes

Renderer blocks are converted to named methods via define_dynamic_method rather
than using instance_exec per request — this keeps the hot path allocation-free
and consistent with how Rage handles action methods internally.

Fixes: #234

Screenshots

CSV renderer:
Screenshot from 2026-03-21 00-03-07

Phlex renderer:
Screenshot from 2026-03-21 00-06-09

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

[Rendering] Custom renderer support

1 participant