Draper Decorators

Implement the Draper gem in Ruby on Rails to manage presentation logic.

11|Updated Jun 19, 2013
One-click install
npx skills add https://github.com/MadBomber/experiments --skill draper-decorators
Or copy as Structured Prompt for Agent
Please help me install this Agent Skill.
Skill: Draper Decorators
Source: https://github.com/MadBomber/experiments/tree/main/ai_misc/skills/hoblin-draper-decorators
Command: npx skills add https://github.com/MadBomber/experiments --skill draper-decorators

SYSTEM DOCUMENTATION & REQUIREMENTS

💡 This Skill includes references (resource) and examples (resource) components.

What problem does it solve?

This Skill addresses the common challenge in Rails applications of keeping presentation logic out of models and controllers, leading to cleaner, more maintainable code.

Core Features & Use Cases

  • Separation of Concerns: Implements the Draper gem to separate business logic (models) from presentation logic (views).
  • View Helper Integration: Provides easy access to Rails view helpers within decorators.
  • Association Decoration: Automatically decorates associated model objects.
  • Context Passing: Allows passing contextual information (like the current user) to decorators.
  • Use Case: When you need to format dates, combine names, or generate HTML snippets for display in a Rails view, this Skill guides you on how to encapsulate that logic cleanly within a decorator, rather than cluttering your models or controllers.

Quick Start

Use the Draper gem to create a decorator for your User model to format the full name.

Frequently Asked Questions about Draper Decorators

High-intent search queries and answers about installing and using this skill.

FAQPage Schema
How do I separate presentation logic from models in Rails views?

To separate presentation logic from models in Rails views, use the Draper gem to create decorators. This moves formatting and display logic into dedicated decorator classes, keeping models and controllers clean and maintainable.

How do I access Rails view helpers inside a decorator?

You can access Rails view helpers inside a decorator through built-in integration provided by the Draper gem. Decorators automatically expose helper methods, allowing you to generate HTML snippets or format data without cluttering your view templates.

What is the best way to decorate associated model objects in Rails?

The best way to decorate associated model objects in Rails is using Draper's association decoration feature. It automatically wraps related model objects in their respective decorators, ensuring consistent presentation logic across nested data structures.

How do I pass context like the current user to a Rails decorator?

Pass context like the current user to Rails decorators using Draper's context passing mechanism. This injects contextual information into the decorator instance, allowing presentation logic to adapt based on user-specific data or session state.

What are common anti-patterns when using decorators for presentation logic?

Common anti-patterns when using decorators for presentation logic include creating fat decorators with excessive responsibilities, triggering N+1 queries through improper association decoration, and decorating objects too early in the Rails request cycle.

Can I decorate collections of model objects with the Draper gem?

Yes, you can decorate collections of model objects with the Draper gem. It provides specific collection decoration strategies to efficiently apply presentation logic across arrays of records while maintaining clean view code.