A modern presentation layer for Rails

Magic Presenter: a modern, decoupled alternative to Draper

Magic Presenter is a presentation layer for Rails models designed as a clean-slate replacement for Draper.

Draper popularized the presenter/decorator pattern in Rails, but its internal architecture relies on patterns that cause recurring operational issues:

  • View context and request-state leakage: Draper caches the request object and view context internally via thread-local state (Thread.current). This leads to intermittent runtime failures in non-HTML requests, background jobs, API engines, and multithreaded web servers.
  • Namespaced and STI lookup failures: Inferring decorator classes for namespaced models, engines, or Single Table Inheritance (STI) hierarchies frequently breaks, raising UninferrableDecoratorError.
  • Monolithic architectural coupling: Blending method delegation, view helper execution, routing context, and Active Record collection wrapping into a single coupled layer makes the gem fragile across Action Pack releases.
  • Delegation & Object Identity Pitfalls: Fixes proxy wrapper edge cases where equality checking (==), is_a?, kind_of?, and method delegation break expected Ruby object semantics.
  • Collection & Relation Degradation: Prevents losing ActiveRecord relation scopes, chaining capabilities, and pagination wrappers (Kaminari/WillPaginate) when decorating collections or associations.

Magic Presenter fixes these architectural design flaws by decoupling core responsibilities into specialized, modular engines (magic-decorator and magic-lookup):

  • Decoupled View Context: Propagates view context cleanly through explicit, lightweight mechanics rather than hooking deeply into controller request cycles.
  • Deterministic class discovery: Uses Magic Lookup for class inference, supporting nested namespaces, modules, and STI models without configuration hacks.
  • Transparent delegation: Powered by Magic Decorator (an enhanced SimpleDelegator), providing explicit delegation and automatic collection decoration with minimal runtime overhead.
  • Automatic Class Inference: Infers and instantiates the correct presenter class for an object or collection automatically.
  • Implicit Association & Collection Decoration: Dynamically decorates associations and nested object graphs as they are accessed.
  • More magic features

Repository: GitHub - Alexander-Senko/magic-presenter: Presentation layer for Rails models to replace Draper · GitHub