When to Use Decorators vs Modern Alternatives
Before reaching for a decorator, check if your use case is better served by a modern alternative:| Use Case | Instead of Decorator | Use This |
|---|---|---|
| After-save hooks (sync to external service) | Model decorator with after_save | Events subscriber |
| Notify external service on changes | Model decorator with callbacks | Webhooks |
| Custom add-to-cart logic | Service decorator | Dependencies injection |
| Custom API responses | Serializer decorator | Dependencies injection |
| Add admin menu item | Controller decorator | Admin Navigation API |
| Add section to admin form | View decorator/override | Admin Partials injection |
| Add searchable/filterable field | Model decorator with ransackable_attributes | Ransack configuration |
| Add association to core model | - | Decorator (still appropriate) |
| Add validation to core model | - | Decorator (still appropriate) |
| Add new method to core model | - | Decorator (still appropriate) |
Decorators are still appropriate for structural changes like adding associations, validations, scopes, and new methods to models. Use modern alternatives for behavioral changes like callbacks, hooks, and side effects.
Overview
All of Spree’s models, controllers, helpers, etc can easily be extended or overridden to meet your exact requirements using standard Ruby idioms. Standard practice for including such changes in your application or extension is to create a file within the relevant app/models/spree or app/controllers/spree directory with the original class name with _decorator appended.Why Use Decorators?
When working with Spree, you’ll often need to add functionality to existing models likeSpree::Product or Spree::Order. However, you shouldn’t modify these files directly because:
- Upgrades - Your changes would be lost when updating Spree
- Maintainability - It’s hard to track what you’ve customized
- Conflicts - Direct modifications can conflict with Spree’s code
How Decorators Work
In Ruby, classes are “open” - you can add methods to them at any time. Decorators leverage this by:- Creating a module with your new methods
- Using
Module#prependto inject your module into the class’s inheritance chain - Your methods run first, and can call
superto invoke the original method
Product.prepend(ProductDecorator) - this inserts your module at the beginning of the method lookup chain, so your methods are found first.
Generating Decorators
Spree provides generators to create decorator files with the correct structure:Model Decorator Generator
app/models/spree/product_decorator.rb:
Controller Decorator Generator
app/controllers/spree/admin/products_controller_decorator.rb:
Decorating Models
Changing Behavior of Existing Methods
The most common use case is changing the behavior of existing methods. When overriding a method, you can callsuper to invoke the original implementation:
app/models/spree/product_decorator.rb
Adding New Methods
Add new instance methods directly in the decorator module:app/models/spree/product_decorator.rb
Adding Associations
Use theself.prepended(base) callback to add associations:
app/models/spree/product_decorator.rb
Adding Validations
app/models/spree/product_decorator.rb
Adding Scopes
app/models/spree/product_decorator.rb
Adding Class Methods
Useextend within the prepended callback to add class methods:
app/models/spree/product_decorator.rb
Decorating Controllers
Recommended: Create a New Controller
Instead of decoratingSpree::ProductsController to add a new action, create your own controller:
app/controllers/spree/product_quick_views_controller.rb
config/routes.rb
- Won’t break when Spree updates
ProductsController - Is easier to test in isolation
- Makes your customizations clearly visible in your codebase
Adding a New Action via Decorator
If you must add an action to an existing controller:app/controllers/spree/products_controller_decorator.rb
config/routes.rb
Modifying Existing Actions
app/controllers/spree/admin/products_controller_decorator.rb
Better alternative: For post-action side effects like notifications, use Events subscribers instead. Subscribe to
product.created to be notified when products are created, without coupling to controller internals.Adding Before Actions
app/controllers/spree/checkout_controller_decorator.rb
Best Practices
Use the prepended callback
Always use
self.prepended(base) for class-level additions like associations, validations, scopes, and callbacks.Keep decorators focused
Each decorator should have a single responsibility. Create multiple decorators for different concerns if needed.
Call super when overriding
When overriding methods, call
super to preserve original behavior unless you intentionally want to replace it entirely.Test decorated behavior
Write tests specifically for your decorated functionality to catch regressions during upgrades.
Organizing Multiple Decorators
If you have many customizations for a single class, consider splitting them into focused decorators:app/models/spree/product_decorator.rb
Common Pitfalls
Forgetting to Call Super
Using Instance Variables in prepended
Circular Dependencies
Be careful when decorators depend on each other:Migrating from Decorators to Modern Patterns
If you have existing decorators that use callbacks for side effects, consider migrating them to Events subscribers for better maintainability.Example: Migrating an After-Save Callback
Before (Decorator with callback):app/models/spree/product_decorator.rb
app/subscribers/my_app/product_sync_subscriber.rb
Benefits of Migration
Loose coupling
Your code doesn’t depend on Spree internals. Events provide a stable interface.
Easier upgrades
Events-based code is less likely to break when Spree is updated.
Better testability
Subscribers can be tested in isolation without loading the full model.
Async by default
Subscribers run via ActiveJob, keeping your requests fast.
Related Documentation
- Events - Learn about Spree’s event system
- Webhooks - HTTP callbacks for external integrations
- Dependencies - Swap core services with your own
- Admin Navigation - Extend admin menu without decorators
- Admin Partials - Extend admin UI without view decorators
- Extending Core Models Tutorial - Step-by-step guide to connecting custom models with Spree core
- Customization Overview - General customization patterns
- Logic Customization - Customizing business logic

