Browse files

Add note about decorator loading in Engines guide.

Because decorators aren't referenced by the application, they won't get
autoloaded. And because we recommend the pattern, it would be irresponsible
to not show how to load them properly.

Fixes #10647.
  • Loading branch information...
steveklabnik committed May 24, 2013
1 parent 432ffdb commit ab28bafc9f1118652f5090fa1260923118930518
Showing with 26 additions and 0 deletions.
  1. +26 −0 guides/source/
@@ -719,6 +719,32 @@ Engine model and controller classes can be extended by open classing them in the
For simple class modifications use `Class#class_eval`, and for complex class modifications, consider using `ActiveSupport::Concern`.
+#### A note on Decorators and loading code
+Because these decorators are not referenced by your Rails application itself,
+Rails' autoloading system will not kick in and load your decorators. This
+means that you need to require them yourself.
+Here is some sample code to do this:
+# lib/blorgh/engine.rb
+module Blorgh
+ class Engine < ::Rails::Engine
+ isolate_namespace Blorgh
+ config.to_prepare do
+ Dir.glob(Rails.root + "app/decorators/**/*_decorator*.rb").each do |c|
+ require_dependency(c)
+ end
+ end
+ end
+This doesn't apply to just Decorators, but anything that you add in an engine
+that isn't referenced by your main application.
#### Implementing Decorator Pattern Using Class#class_eval
**Adding** `Post#time_since_created`,

0 comments on commit ab28baf

Please sign in to comment.