Skip to content
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
21 changes: 10 additions & 11 deletions guides/source/engines.md
Original file line number Diff line number Diff line change
Expand Up @@ -1091,16 +1091,15 @@ main Rails application.
Engine model and controller classes can be extended by open classing them in the
main Rails application (since model and controller classes are just Ruby classes
that inherit Rails specific functionality). Open classing an Engine class
redefines it for use in the main application. This is usually implemented by
using the decorator pattern.
redefines it for use in the main application.

For simple class modifications, use `Class#class_eval`. For complex class
modifications, consider using `ActiveSupport::Concern`.

#### A note on Decorators and Loading Code
#### A note on Overriding 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
Because these overrides are not referenced by your Rails application itself,
Rails' autoloading system will not kick in and load your overrides. This means
that you need to require them yourself.

Here is some sample code to do this:
Expand All @@ -1112,23 +1111,23 @@ module Blorgh
isolate_namespace Blorgh

config.to_prepare do
Dir.glob(Rails.root + "app/decorators/**/*_decorator*.rb").each do |c|
Dir.glob(Rails.root + "app/overrides/**/*_override*.rb").each do |c|
require_dependency(c)
end
end
end
end
```

This doesn't apply to just Decorators, but anything that you add in an engine
This doesn't apply to just overrides, but anything that you add in an engine
that isn't referenced by your main application.

#### Implementing Decorator Pattern Using Class#class_eval
#### Overriding existing classes using Class#class_eval

**Adding** `Article#time_since_created`:

```ruby
# MyApp/app/decorators/models/blorgh/article_decorator.rb
# MyApp/app/overrides/models/blorgh/article_override.rb

Blorgh::Article.class_eval do
def time_since_created
Expand All @@ -1149,7 +1148,7 @@ end
**Overriding** `Article#summary`:

```ruby
# MyApp/app/decorators/models/blorgh/article_decorator.rb
# MyApp/app/overrides/models/blorgh/article_override.rb

Blorgh::Article.class_eval do
def summary
Expand All @@ -1169,7 +1168,7 @@ class Article < ApplicationRecord
end
```

#### Implementing Decorator Pattern Using ActiveSupport::Concern
#### Overriding existing classes using ActiveSupport::Concern

Using `Class#class_eval` is great for simple adjustments, but for more complex
class modifications, you might want to consider using [`ActiveSupport::Concern`]
Expand Down