rails 4.0.13 → 4.1.16
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- checksums.yaml +4 -4
- data/README.md +22 -17
- data/guides/CHANGELOG.md +68 -34
- data/guides/assets/images/edge_badge.png +0 -0
- data/guides/assets/images/feature_tile.gif +0 -0
- data/guides/assets/images/footer_tile.gif +0 -0
- data/guides/assets/images/fxn.png +0 -0
- data/guides/assets/images/getting_started/article_with_comments.png +0 -0
- data/guides/assets/images/getting_started/challenge.png +0 -0
- data/guides/assets/images/getting_started/confirm_dialog.png +0 -0
- data/guides/assets/images/getting_started/forbidden_attributes_for_new_article.png +0 -0
- data/guides/assets/images/getting_started/form_with_errors.png +0 -0
- data/guides/assets/images/getting_started/index_action_with_edit_link.png +0 -0
- data/guides/assets/images/getting_started/new_article.png +0 -0
- data/guides/assets/images/getting_started/rails_welcome.png +0 -0
- data/guides/assets/images/getting_started/routing_error_no_controller.png +0 -0
- data/guides/assets/images/getting_started/routing_error_no_route_matches.png +0 -0
- data/guides/assets/images/getting_started/show_action_for_articles.png +0 -0
- data/guides/assets/images/getting_started/template_is_missing_articles_new.png +0 -0
- data/guides/assets/images/getting_started/unknown_action_create_for_articles.png +0 -0
- data/guides/assets/images/getting_started/unknown_action_new_for_articles.png +0 -0
- data/guides/assets/images/header_tile.gif +0 -0
- data/guides/assets/images/icons/README +1 -1
- data/guides/assets/images/icons/callouts/11.png +0 -0
- data/guides/assets/images/icons/callouts/12.png +0 -0
- data/guides/assets/images/icons/callouts/13.png +0 -0
- data/guides/assets/images/icons/callouts/15.png +0 -0
- data/guides/assets/images/icons/caution.png +0 -0
- data/guides/assets/images/icons/example.png +0 -0
- data/guides/assets/images/radar.png +0 -0
- data/guides/assets/images/rails4_features.png +0 -0
- data/guides/assets/images/rails_guides_kindle_cover.jpg +0 -0
- data/guides/assets/images/vijaydev.jpg +0 -0
- data/guides/assets/javascripts/guides.js +30 -34
- data/guides/assets/stylesheets/main.css +2 -1
- data/guides/assets/stylesheets/print.css +1 -1
- data/guides/bug_report_templates/action_controller_gem.rb +9 -4
- data/guides/bug_report_templates/action_controller_master.rb +4 -2
- data/guides/bug_report_templates/active_record_gem.rb +5 -2
- data/guides/bug_report_templates/active_record_master.rb +2 -1
- data/guides/bug_report_templates/generic_gem.rb +15 -0
- data/guides/bug_report_templates/generic_master.rb +26 -0
- data/guides/code/getting_started/Gemfile +21 -24
- data/guides/code/getting_started/Gemfile.lock +78 -73
- data/guides/code/getting_started/Rakefile +1 -1
- data/guides/code/getting_started/app/assets/javascripts/application.js +1 -2
- data/guides/code/getting_started/app/views/layouts/application.html.erb +2 -2
- data/guides/code/getting_started/config/environment.rb +1 -1
- data/guides/code/getting_started/config/environments/development.rb +2 -2
- data/guides/code/getting_started/config/environments/production.rb +3 -3
- data/guides/code/getting_started/config/environments/test.rb +2 -2
- data/guides/code/getting_started/config/initializers/secret_token.rb +1 -1
- data/guides/code/getting_started/config/initializers/session_store.rb +1 -1
- data/guides/code/getting_started/config/routes.rb +1 -1
- data/guides/code/getting_started/config.ru +1 -1
- data/guides/code/getting_started/public/404.html +2 -0
- data/guides/code/getting_started/public/422.html +2 -0
- data/guides/code/getting_started/public/500.html +2 -0
- data/guides/code/getting_started/test/test_helper.rb +0 -3
- data/guides/rails_guides/helpers.rb +3 -1
- data/guides/source/2_2_release_notes.md +2 -2
- data/guides/source/2_3_release_notes.md +8 -8
- data/guides/source/3_0_release_notes.md +2 -3
- data/guides/source/3_1_release_notes.md +2 -2
- data/guides/source/3_2_release_notes.md +12 -12
- data/guides/source/4_0_release_notes.md +79 -46
- data/guides/source/4_1_release_notes.md +731 -0
- data/guides/source/_welcome.html.erb +5 -2
- data/guides/source/action_controller_overview.md +189 -40
- data/guides/source/action_mailer_basics.md +27 -27
- data/guides/source/action_view_overview.md +131 -20
- data/guides/source/active_model_basics.md +6 -6
- data/guides/source/active_record_basics.md +15 -15
- data/guides/source/active_record_callbacks.md +18 -16
- data/guides/source/active_record_querying.md +93 -51
- data/guides/source/active_record_validations.md +26 -24
- data/guides/source/active_support_core_extensions.md +72 -118
- data/guides/source/active_support_instrumentation.md +13 -4
- data/guides/source/api_documentation_guidelines.md +104 -6
- data/guides/source/asset_pipeline.md +573 -244
- data/guides/source/association_basics.md +94 -22
- data/guides/source/caching_with_rails.md +15 -6
- data/guides/source/command_line.md +55 -46
- data/guides/source/configuring.md +248 -52
- data/guides/source/contributing_to_ruby_on_rails.md +18 -17
- data/guides/source/credits.html.erb +2 -2
- data/guides/source/debugging_rails_applications.md +39 -8
- data/guides/source/development_dependencies_install.md +91 -8
- data/guides/source/documents.yaml +4 -0
- data/guides/source/engines.md +678 -232
- data/guides/source/form_helpers.md +53 -35
- data/guides/source/generators.md +19 -15
- data/guides/source/getting_started.md +758 -497
- data/guides/source/i18n.md +64 -28
- data/guides/source/index.html.erb +1 -1
- data/guides/source/initialization.md +155 -58
- data/guides/source/kindle/toc.html.erb +1 -1
- data/guides/source/layout.html.erb +2 -2
- data/guides/source/layouts_and_rendering.md +59 -26
- data/guides/source/maintenance_policy.md +3 -3
- data/guides/source/migrations.md +101 -62
- data/guides/source/nested_model_forms.md +3 -3
- data/guides/source/plugins.md +34 -31
- data/guides/source/rails_application_templates.md +27 -8
- data/guides/source/rails_on_rack.md +41 -58
- data/guides/source/routing.md +115 -104
- data/guides/source/ruby_on_rails_guides_guidelines.md +2 -2
- data/guides/source/security.md +81 -36
- data/guides/source/testing.md +56 -79
- data/guides/source/upgrading_ruby_on_rails.md +531 -21
- data/guides/source/working_with_javascript_in_rails.md +19 -11
- metadata +51 -23
- data/guides/assets/images/getting_started/forbidden_attributes_for_new_post.png +0 -0
- data/guides/assets/images/getting_started/new_post.png +0 -0
- data/guides/assets/images/getting_started/post_with_comments.png +0 -0
- data/guides/assets/images/getting_started/show_action_for_posts.png +0 -0
- data/guides/assets/images/getting_started/template_is_missing_posts_new.png +0 -0
- data/guides/assets/images/getting_started/undefined_method_post_path.png +0 -0
- data/guides/assets/images/getting_started/unknown_action_create_for_posts.png +0 -0
- data/guides/assets/images/getting_started/unknown_action_new_for_posts.png +0 -0
- data/guides/assets/images/jaimeiniesta.jpg +0 -0
- data/guides/source/kindle/KINDLE.md +0 -26
data/guides/source/engines.md
CHANGED
|
@@ -1,7 +1,9 @@
|
|
|
1
1
|
Getting Started with Engines
|
|
2
2
|
============================
|
|
3
3
|
|
|
4
|
-
In this guide you will learn about engines and how they can be used to provide
|
|
4
|
+
In this guide you will learn about engines and how they can be used to provide
|
|
5
|
+
additional functionality to their host applications through a clean and very
|
|
6
|
+
easy-to-use interface.
|
|
5
7
|
|
|
6
8
|
After reading this guide, you will know:
|
|
7
9
|
|
|
@@ -16,38 +18,72 @@ After reading this guide, you will know:
|
|
|
16
18
|
What are engines?
|
|
17
19
|
-----------------
|
|
18
20
|
|
|
19
|
-
Engines can be considered miniature applications that provide functionality to
|
|
20
|
-
|
|
21
|
-
|
|
22
|
-
|
|
23
|
-
|
|
24
|
-
|
|
25
|
-
|
|
26
|
-
|
|
27
|
-
|
|
28
|
-
|
|
29
|
-
|
|
30
|
-
|
|
31
|
-
|
|
32
|
-
|
|
33
|
-
|
|
21
|
+
Engines can be considered miniature applications that provide functionality to
|
|
22
|
+
their host applications. A Rails application is actually just a "supercharged"
|
|
23
|
+
engine, with the `Rails::Application` class inheriting a lot of its behavior
|
|
24
|
+
from `Rails::Engine`.
|
|
25
|
+
|
|
26
|
+
Therefore, engines and applications can be thought of almost the same thing,
|
|
27
|
+
just with subtle differences, as you'll see throughout this guide. Engines and
|
|
28
|
+
applications also share a common structure.
|
|
29
|
+
|
|
30
|
+
Engines are also closely related to plugins. The two share a common `lib`
|
|
31
|
+
directory structure, and are both generated using the `rails plugin new`
|
|
32
|
+
generator. The difference is that an engine is considered a "full plugin" by
|
|
33
|
+
Rails (as indicated by the `--full` option that's passed to the generator
|
|
34
|
+
command). This guide will refer to them simply as "engines" throughout. An
|
|
35
|
+
engine **can** be a plugin, and a plugin **can** be an engine.
|
|
36
|
+
|
|
37
|
+
The engine that will be created in this guide will be called "blorgh". The
|
|
38
|
+
engine will provide blogging functionality to its host applications, allowing
|
|
39
|
+
for new posts and comments to be created. At the beginning of this guide, you
|
|
40
|
+
will be working solely within the engine itself, but in later sections you'll
|
|
41
|
+
see how to hook it into an application.
|
|
42
|
+
|
|
43
|
+
Engines can also be isolated from their host applications. This means that an
|
|
44
|
+
application is able to have a path provided by a routing helper such as
|
|
45
|
+
`posts_path` and use an engine also that provides a path also called
|
|
46
|
+
`posts_path`, and the two would not clash. Along with this, controllers, models
|
|
47
|
+
and table names are also namespaced. You'll see how to do this later in this
|
|
48
|
+
guide.
|
|
49
|
+
|
|
50
|
+
It's important to keep in mind at all times that the application should
|
|
51
|
+
**always** take precedence over its engines. An application is the object that
|
|
52
|
+
has final say in what goes on in the universe (with the universe being the
|
|
53
|
+
application's environment) where the engine should only be enhancing it, rather
|
|
54
|
+
than changing it drastically.
|
|
55
|
+
|
|
56
|
+
To see demonstrations of other engines, check out
|
|
57
|
+
[Devise](https://github.com/plataformatec/devise), an engine that provides
|
|
58
|
+
authentication for its parent applications, or
|
|
59
|
+
[Forem](https://github.com/radar/forem), an engine that provides forum
|
|
60
|
+
functionality. There's also [Spree](https://github.com/spree/spree) which
|
|
61
|
+
provides an e-commerce platform, and
|
|
62
|
+
[RefineryCMS](https://github.com/refinery/refinerycms), a CMS engine.
|
|
63
|
+
|
|
64
|
+
Finally, engines would not have been possible without the work of James Adam,
|
|
65
|
+
Piotr Sarnacki, the Rails Core Team, and a number of other people. If you ever
|
|
66
|
+
meet them, don't forget to say thanks!
|
|
34
67
|
|
|
35
68
|
Generating an engine
|
|
36
69
|
--------------------
|
|
37
70
|
|
|
38
|
-
To generate an engine, you will need to run the plugin generator and pass it
|
|
71
|
+
To generate an engine, you will need to run the plugin generator and pass it
|
|
72
|
+
options as appropriate to the need. For the "blorgh" example, you will need to
|
|
73
|
+
create a "mountable" engine, running this command in a terminal:
|
|
39
74
|
|
|
40
75
|
```bash
|
|
41
|
-
$ rails plugin new blorgh --mountable
|
|
76
|
+
$ bin/rails plugin new blorgh --mountable
|
|
42
77
|
```
|
|
43
78
|
|
|
44
79
|
The full list of options for the plugin generator may be seen by typing:
|
|
45
80
|
|
|
46
81
|
```bash
|
|
47
|
-
$ rails plugin --help
|
|
82
|
+
$ bin/rails plugin --help
|
|
48
83
|
```
|
|
49
84
|
|
|
50
|
-
The `--full` option tells the generator that you want to create an engine,
|
|
85
|
+
The `--full` option tells the generator that you want to create an engine,
|
|
86
|
+
including a skeleton structure that provides the following:
|
|
51
87
|
|
|
52
88
|
* An `app` directory tree
|
|
53
89
|
* A `config/routes.rb` file:
|
|
@@ -56,7 +92,9 @@ The `--full` option tells the generator that you want to create an engine, inclu
|
|
|
56
92
|
Rails.application.routes.draw do
|
|
57
93
|
end
|
|
58
94
|
```
|
|
59
|
-
|
|
95
|
+
|
|
96
|
+
* A file at `lib/blorgh/engine.rb`, which is identical in function to a
|
|
97
|
+
* standard Rails application's `config/application.rb` file:
|
|
60
98
|
|
|
61
99
|
```ruby
|
|
62
100
|
module Blorgh
|
|
@@ -65,7 +103,9 @@ The `--full` option tells the generator that you want to create an engine, inclu
|
|
|
65
103
|
end
|
|
66
104
|
```
|
|
67
105
|
|
|
68
|
-
The `--mountable` option tells the generator that you want to create a
|
|
106
|
+
The `--mountable` option tells the generator that you want to create a
|
|
107
|
+
"mountable" and namespace-isolated engine. This generator will provide the same
|
|
108
|
+
skeleton structure as would the `--full` option, and will add:
|
|
69
109
|
|
|
70
110
|
* Asset manifest files (`application.js` and `application.css`)
|
|
71
111
|
* A namespaced `ApplicationController` stub
|
|
@@ -88,23 +128,32 @@ The `--mountable` option tells the generator that you want to create a "mountabl
|
|
|
88
128
|
end
|
|
89
129
|
```
|
|
90
130
|
|
|
91
|
-
Additionally, the `--mountable` option tells the generator to mount the engine
|
|
131
|
+
Additionally, the `--mountable` option tells the generator to mount the engine
|
|
132
|
+
inside the dummy testing application located at `test/dummy` by adding the
|
|
133
|
+
following to the dummy application's routes file at
|
|
134
|
+
`test/dummy/config/routes.rb`:
|
|
92
135
|
|
|
93
136
|
```ruby
|
|
94
137
|
mount Blorgh::Engine, at: "blorgh"
|
|
95
138
|
```
|
|
96
139
|
|
|
97
|
-
### Inside an
|
|
140
|
+
### Inside an Engine
|
|
98
141
|
|
|
99
|
-
#### Critical
|
|
142
|
+
#### Critical Files
|
|
100
143
|
|
|
101
|
-
At the root of this brand new engine's directory lives a `blorgh.gemspec` file.
|
|
144
|
+
At the root of this brand new engine's directory lives a `blorgh.gemspec` file.
|
|
145
|
+
When you include the engine into an application later on, you will do so with
|
|
146
|
+
this line in the Rails application's `Gemfile`:
|
|
102
147
|
|
|
103
148
|
```ruby
|
|
104
149
|
gem 'blorgh', path: "vendor/engines/blorgh"
|
|
105
150
|
```
|
|
106
151
|
|
|
107
|
-
|
|
152
|
+
Don't forget to run `bundle install` as usual. By specifying it as a gem within
|
|
153
|
+
the `Gemfile`, Bundler will load it as such, parsing this `blorgh.gemspec` file
|
|
154
|
+
and requiring a file within the `lib` directory called `lib/blorgh.rb`. This
|
|
155
|
+
file requires the `blorgh/engine.rb` file (located at `lib/blorgh/engine.rb`)
|
|
156
|
+
and defines a base module called `Blorgh`.
|
|
108
157
|
|
|
109
158
|
```ruby
|
|
110
159
|
require "blorgh/engine"
|
|
@@ -113,7 +162,10 @@ module Blorgh
|
|
|
113
162
|
end
|
|
114
163
|
```
|
|
115
164
|
|
|
116
|
-
TIP: Some engines choose to use this file to put global configuration options
|
|
165
|
+
TIP: Some engines choose to use this file to put global configuration options
|
|
166
|
+
for their engine. It's a relatively good idea, so if you want to offer
|
|
167
|
+
configuration options, the file where your engine's `module` is defined is
|
|
168
|
+
perfect for that. Place the methods inside the module and you'll be good to go.
|
|
117
169
|
|
|
118
170
|
Within `lib/blorgh/engine.rb` is the base class for the engine:
|
|
119
171
|
|
|
@@ -125,43 +177,94 @@ module Blorgh
|
|
|
125
177
|
end
|
|
126
178
|
```
|
|
127
179
|
|
|
128
|
-
By inheriting from the `Rails::Engine` class, this gem notifies Rails that
|
|
129
|
-
|
|
130
|
-
|
|
131
|
-
|
|
132
|
-
|
|
133
|
-
|
|
134
|
-
|
|
135
|
-
|
|
136
|
-
|
|
137
|
-
|
|
138
|
-
|
|
139
|
-
|
|
140
|
-
|
|
141
|
-
|
|
142
|
-
|
|
143
|
-
|
|
144
|
-
|
|
145
|
-
|
|
146
|
-
|
|
147
|
-
|
|
148
|
-
|
|
149
|
-
|
|
150
|
-
|
|
151
|
-
|
|
152
|
-
|
|
153
|
-
|
|
154
|
-
|
|
180
|
+
By inheriting from the `Rails::Engine` class, this gem notifies Rails that
|
|
181
|
+
there's an engine at the specified path, and will correctly mount the engine
|
|
182
|
+
inside the application, performing tasks such as adding the `app` directory of
|
|
183
|
+
the engine to the load path for models, mailers, controllers and views.
|
|
184
|
+
|
|
185
|
+
The `isolate_namespace` method here deserves special notice. This call is
|
|
186
|
+
responsible for isolating the controllers, models, routes and other things into
|
|
187
|
+
their own namespace, away from similar components inside the application.
|
|
188
|
+
Without this, there is a possibility that the engine's components could "leak"
|
|
189
|
+
into the application, causing unwanted disruption, or that important engine
|
|
190
|
+
components could be overridden by similarly named things within the application.
|
|
191
|
+
One of the examples of such conflicts is helpers. Without calling
|
|
192
|
+
`isolate_namespace`, the engine's helpers would be included in an application's
|
|
193
|
+
controllers.
|
|
194
|
+
|
|
195
|
+
NOTE: It is **highly** recommended that the `isolate_namespace` line be left
|
|
196
|
+
within the `Engine` class definition. Without it, classes generated in an engine
|
|
197
|
+
**may** conflict with an application.
|
|
198
|
+
|
|
199
|
+
What this isolation of the namespace means is that a model generated by a call
|
|
200
|
+
to `bin/rails g model`, such as `bin/rails g model post`, won't be called `Post`, but
|
|
201
|
+
instead be namespaced and called `Blorgh::Post`. In addition, the table for the
|
|
202
|
+
model is namespaced, becoming `blorgh_posts`, rather than simply `posts`.
|
|
203
|
+
Similar to the model namespacing, a controller called `PostsController` becomes
|
|
204
|
+
`Blorgh::PostsController` and the views for that controller will not be at
|
|
205
|
+
`app/views/posts`, but `app/views/blorgh/posts` instead. Mailers are namespaced
|
|
206
|
+
as well.
|
|
207
|
+
|
|
208
|
+
Finally, routes will also be isolated within the engine. This is one of the most
|
|
209
|
+
important parts about namespacing, and is discussed later in the
|
|
210
|
+
[Routes](#routes) section of this guide.
|
|
211
|
+
|
|
212
|
+
#### `app` Directory
|
|
213
|
+
|
|
214
|
+
Inside the `app` directory are the standard `assets`, `controllers`, `helpers`,
|
|
215
|
+
`mailers`, `models` and `views` directories that you should be familiar with
|
|
216
|
+
from an application. The `helpers`, `mailers` and `models` directories are
|
|
217
|
+
empty, so they aren't described in this section. We'll look more into models in
|
|
218
|
+
a future section, when we're writing the engine.
|
|
219
|
+
|
|
220
|
+
Within the `app/assets` directory, there are the `images`, `javascripts` and
|
|
221
|
+
`stylesheets` directories which, again, you should be familiar with due to their
|
|
222
|
+
similarity to an application. One difference here, however, is that each
|
|
223
|
+
directory contains a sub-directory with the engine name. Because this engine is
|
|
224
|
+
going to be namespaced, its assets should be too.
|
|
225
|
+
|
|
226
|
+
Within the `app/controllers` directory there is a `blorgh` directory that
|
|
227
|
+
contains a file called `application_controller.rb`. This file will provide any
|
|
228
|
+
common functionality for the controllers of the engine. The `blorgh` directory
|
|
229
|
+
is where the other controllers for the engine will go. By placing them within
|
|
230
|
+
this namespaced directory, you prevent them from possibly clashing with
|
|
231
|
+
identically-named controllers within other engines or even within the
|
|
232
|
+
application.
|
|
233
|
+
|
|
234
|
+
NOTE: The `ApplicationController` class inside an engine is named just like a
|
|
235
|
+
Rails application in order to make it easier for you to convert your
|
|
236
|
+
applications into engines.
|
|
237
|
+
|
|
238
|
+
Lastly, the `app/views` directory contains a `layouts` folder, which contains a
|
|
239
|
+
file at `blorgh/application.html.erb`. This file allows you to specify a layout
|
|
240
|
+
for the engine. If this engine is to be used as a stand-alone engine, then you
|
|
241
|
+
would add any customization to its layout in this file, rather than the
|
|
242
|
+
application's `app/views/layouts/application.html.erb` file.
|
|
243
|
+
|
|
244
|
+
If you don't want to force a layout on to users of the engine, then you can
|
|
245
|
+
delete this file and reference a different layout in the controllers of your
|
|
246
|
+
engine.
|
|
247
|
+
|
|
248
|
+
#### `bin` Directory
|
|
249
|
+
|
|
250
|
+
This directory contains one file, `bin/rails`, which enables you to use the
|
|
251
|
+
`rails` sub-commands and generators just like you would within an application.
|
|
252
|
+
This means that you will be able to generate new controllers and models for this
|
|
253
|
+
engine very easily by running commands like this:
|
|
155
254
|
|
|
156
255
|
```bash
|
|
157
|
-
rails g model
|
|
256
|
+
$ bin/rails g model
|
|
158
257
|
```
|
|
159
258
|
|
|
160
|
-
|
|
259
|
+
Keep in mind, of course, that anything generated with these commands inside of
|
|
260
|
+
an engine that has `isolate_namespace` in the `Engine` class will be namespaced.
|
|
161
261
|
|
|
162
|
-
#### `test`
|
|
262
|
+
#### `test` Directory
|
|
163
263
|
|
|
164
|
-
The `test` directory is where tests for the engine will go. To test the engine,
|
|
264
|
+
The `test` directory is where tests for the engine will go. To test the engine,
|
|
265
|
+
there is a cut-down version of a Rails application embedded within it at
|
|
266
|
+
`test/dummy`. This application will mount the engine in the
|
|
267
|
+
`test/dummy/config/routes.rb` file:
|
|
165
268
|
|
|
166
269
|
```ruby
|
|
167
270
|
Rails.application.routes.draw do
|
|
@@ -169,21 +272,28 @@ Rails.application.routes.draw do
|
|
|
169
272
|
end
|
|
170
273
|
```
|
|
171
274
|
|
|
172
|
-
This line mounts the engine at the path `/blorgh`, which will make it accessible
|
|
275
|
+
This line mounts the engine at the path `/blorgh`, which will make it accessible
|
|
276
|
+
through the application only at that path.
|
|
173
277
|
|
|
174
|
-
|
|
278
|
+
Inside the test directory there is the `test/integration` directory, where
|
|
279
|
+
integration tests for the engine should be placed. Other directories can be
|
|
280
|
+
created in the `test` directory as well. For example, you may wish to create a
|
|
281
|
+
`test/models` directory for your model tests.
|
|
175
282
|
|
|
176
283
|
Providing engine functionality
|
|
177
284
|
------------------------------
|
|
178
285
|
|
|
179
|
-
The engine that this guide covers provides posting and commenting functionality
|
|
286
|
+
The engine that this guide covers provides posting and commenting functionality
|
|
287
|
+
and follows a similar thread to the [Getting Started
|
|
288
|
+
Guide](getting_started.html), with some new twists.
|
|
180
289
|
|
|
181
|
-
### Generating a
|
|
290
|
+
### Generating a Post Resource
|
|
182
291
|
|
|
183
|
-
The first thing to generate for a blog engine is the `Post` model and related
|
|
292
|
+
The first thing to generate for a blog engine is the `Post` model and related
|
|
293
|
+
controller. To quickly generate this, you can use the Rails scaffold generator.
|
|
184
294
|
|
|
185
295
|
```bash
|
|
186
|
-
$ rails generate scaffold post title:string text:text
|
|
296
|
+
$ bin/rails generate scaffold post title:string text:text
|
|
187
297
|
```
|
|
188
298
|
|
|
189
299
|
This command will output this information:
|
|
@@ -195,7 +305,8 @@ create app/models/blorgh/post.rb
|
|
|
195
305
|
invoke test_unit
|
|
196
306
|
create test/models/blorgh/post_test.rb
|
|
197
307
|
create test/fixtures/blorgh/posts.yml
|
|
198
|
-
|
|
308
|
+
invoke resource_route
|
|
309
|
+
route resources :posts
|
|
199
310
|
invoke scaffold_controller
|
|
200
311
|
create app/controllers/blorgh/posts_controller.rb
|
|
201
312
|
invoke erb
|
|
@@ -220,11 +331,22 @@ invoke css
|
|
|
220
331
|
create app/assets/stylesheets/scaffold.css
|
|
221
332
|
```
|
|
222
333
|
|
|
223
|
-
The first thing that the scaffold generator does is invoke the `active_record`
|
|
334
|
+
The first thing that the scaffold generator does is invoke the `active_record`
|
|
335
|
+
generator, which generates a migration and a model for the resource. Note here,
|
|
336
|
+
however, that the migration is called `create_blorgh_posts` rather than the
|
|
337
|
+
usual `create_posts`. This is due to the `isolate_namespace` method called in
|
|
338
|
+
the `Blorgh::Engine` class's definition. The model here is also namespaced,
|
|
339
|
+
being placed at `app/models/blorgh/post.rb` rather than `app/models/post.rb` due
|
|
340
|
+
to the `isolate_namespace` call within the `Engine` class.
|
|
224
341
|
|
|
225
|
-
Next, the `test_unit` generator is invoked for this model, generating a model
|
|
342
|
+
Next, the `test_unit` generator is invoked for this model, generating a model
|
|
343
|
+
test at `test/models/blorgh/post_test.rb` (rather than
|
|
344
|
+
`test/models/post_test.rb`) and a fixture at `test/fixtures/blorgh/posts.yml`
|
|
345
|
+
(rather than `test/fixtures/posts.yml`).
|
|
226
346
|
|
|
227
|
-
After that, a line for the resource is inserted into the `config/routes.rb` file
|
|
347
|
+
After that, a line for the resource is inserted into the `config/routes.rb` file
|
|
348
|
+
for the engine. This line is simply `resources :posts`, turning the
|
|
349
|
+
`config/routes.rb` file for the engine into this:
|
|
228
350
|
|
|
229
351
|
```ruby
|
|
230
352
|
Blorgh::Engine.routes.draw do
|
|
@@ -232,12 +354,22 @@ Blorgh::Engine.routes.draw do
|
|
|
232
354
|
end
|
|
233
355
|
```
|
|
234
356
|
|
|
235
|
-
Note here that the routes are drawn upon the `Blorgh::Engine` object rather than
|
|
236
|
-
|
|
357
|
+
Note here that the routes are drawn upon the `Blorgh::Engine` object rather than
|
|
358
|
+
the `YourApp::Application` class. This is so that the engine routes are confined
|
|
359
|
+
to the engine itself and can be mounted at a specific point as shown in the
|
|
360
|
+
[test directory](#test-directory) section. It also causes the engine's routes to
|
|
361
|
+
be isolated from those routes that are within the application. The
|
|
362
|
+
[Routes](#routes) section of this guide describes it in detail.
|
|
237
363
|
|
|
238
|
-
Next, the `scaffold_controller` generator is invoked, generating a controller
|
|
364
|
+
Next, the `scaffold_controller` generator is invoked, generating a controller
|
|
365
|
+
called `Blorgh::PostsController` (at
|
|
366
|
+
`app/controllers/blorgh/posts_controller.rb`) and its related views at
|
|
367
|
+
`app/views/blorgh/posts`. This generator also generates a test for the
|
|
368
|
+
controller (`test/controllers/blorgh/posts_controller_test.rb`) and a helper
|
|
369
|
+
(`app/helpers/blorgh/posts_controller.rb`).
|
|
239
370
|
|
|
240
|
-
Everything this generator has created is neatly namespaced. The controller's
|
|
371
|
+
Everything this generator has created is neatly namespaced. The controller's
|
|
372
|
+
class is defined within the `Blorgh` module:
|
|
241
373
|
|
|
242
374
|
```ruby
|
|
243
375
|
module Blorgh
|
|
@@ -247,7 +379,8 @@ module Blorgh
|
|
|
247
379
|
end
|
|
248
380
|
```
|
|
249
381
|
|
|
250
|
-
NOTE: The `ApplicationController` class being inherited from here is the
|
|
382
|
+
NOTE: The `ApplicationController` class being inherited from here is the
|
|
383
|
+
`Blorgh::ApplicationController`, not an application's `ApplicationController`.
|
|
251
384
|
|
|
252
385
|
The helper inside `app/helpers/blorgh/posts_helper.rb` is also namespaced:
|
|
253
386
|
|
|
@@ -259,41 +392,66 @@ module Blorgh
|
|
|
259
392
|
end
|
|
260
393
|
```
|
|
261
394
|
|
|
262
|
-
This helps prevent conflicts with any other engine or application that may have
|
|
395
|
+
This helps prevent conflicts with any other engine or application that may have
|
|
396
|
+
a post resource as well.
|
|
263
397
|
|
|
264
|
-
Finally,
|
|
398
|
+
Finally, the assets for this resource are generated in two files:
|
|
399
|
+
`app/assets/javascripts/blorgh/posts.js` and
|
|
400
|
+
`app/assets/stylesheets/blorgh/posts.css`. You'll see how to use these a little
|
|
401
|
+
later.
|
|
265
402
|
|
|
266
|
-
By default, the scaffold styling is not applied to the engine
|
|
403
|
+
By default, the scaffold styling is not applied to the engine because the
|
|
404
|
+
engine's layout file, `app/views/layouts/blorgh/application.html.erb`, doesn't
|
|
405
|
+
load it. To make the scaffold styling apply, insert this line into the `<head>`
|
|
406
|
+
tag of this layout:
|
|
267
407
|
|
|
268
408
|
```erb
|
|
269
409
|
<%= stylesheet_link_tag "scaffold" %>
|
|
270
410
|
```
|
|
271
411
|
|
|
272
|
-
You can see what the engine has so far by running `rake db:migrate` at the root
|
|
412
|
+
You can see what the engine has so far by running `rake db:migrate` at the root
|
|
413
|
+
of our engine to run the migration generated by the scaffold generator, and then
|
|
414
|
+
running `rails server` in `test/dummy`. When you open
|
|
415
|
+
`http://localhost:3000/blorgh/posts` you will see the default scaffold that has
|
|
416
|
+
been generated. Click around! You've just generated your first engine's first
|
|
417
|
+
functions.
|
|
273
418
|
|
|
274
|
-
If you'd rather play around in the console, `rails console` will also work just
|
|
419
|
+
If you'd rather play around in the console, `rails console` will also work just
|
|
420
|
+
like a Rails application. Remember: the `Post` model is namespaced, so to
|
|
421
|
+
reference it you must call it as `Blorgh::Post`.
|
|
275
422
|
|
|
276
423
|
```ruby
|
|
277
424
|
>> Blorgh::Post.find(1)
|
|
278
425
|
=> #<Blorgh::Post id: 1 ...>
|
|
279
426
|
```
|
|
280
427
|
|
|
281
|
-
One final thing is that the `posts` resource for this engine should be the root
|
|
428
|
+
One final thing is that the `posts` resource for this engine should be the root
|
|
429
|
+
of the engine. Whenever someone goes to the root path where the engine is
|
|
430
|
+
mounted, they should be shown a list of posts. This can be made to happen if
|
|
431
|
+
this line is inserted into the `config/routes.rb` file inside the engine:
|
|
282
432
|
|
|
283
433
|
```ruby
|
|
284
434
|
root to: "posts#index"
|
|
285
435
|
```
|
|
286
436
|
|
|
287
|
-
Now people will only need to go to the root of the engine to see all the posts,
|
|
437
|
+
Now people will only need to go to the root of the engine to see all the posts,
|
|
438
|
+
rather than visiting `/posts`. This means that instead of
|
|
439
|
+
`http://localhost:3000/blorgh/posts`, you only need to go to
|
|
440
|
+
`http://localhost:3000/blorgh` now.
|
|
288
441
|
|
|
289
|
-
### Generating a
|
|
442
|
+
### Generating a Comments Resource
|
|
290
443
|
|
|
291
|
-
Now that the engine can create new blog posts, it only makes sense to add
|
|
444
|
+
Now that the engine can create new blog posts, it only makes sense to add
|
|
445
|
+
commenting functionality as well. To do this, you'll need to generate a comment
|
|
446
|
+
model, a comment controller and then modify the posts scaffold to display
|
|
447
|
+
comments and allow people to create new ones.
|
|
292
448
|
|
|
293
|
-
|
|
449
|
+
From the application root, run the model generator. Tell it to generate a
|
|
450
|
+
`Comment` model, with the related table having two columns: a `post_id` integer
|
|
451
|
+
and `text` text column.
|
|
294
452
|
|
|
295
453
|
```bash
|
|
296
|
-
$ rails generate model Comment post_id:integer text:text
|
|
454
|
+
$ bin/rails generate model Comment post_id:integer text:text
|
|
297
455
|
```
|
|
298
456
|
|
|
299
457
|
This will output the following:
|
|
@@ -307,16 +465,26 @@ create test/models/blorgh/comment_test.rb
|
|
|
307
465
|
create test/fixtures/blorgh/comments.yml
|
|
308
466
|
```
|
|
309
467
|
|
|
310
|
-
This generator call will generate just the necessary model files it needs,
|
|
468
|
+
This generator call will generate just the necessary model files it needs,
|
|
469
|
+
namespacing the files under a `blorgh` directory and creating a model class
|
|
470
|
+
called `Blorgh::Comment`. Now run the migration to create our blorgh_comments
|
|
471
|
+
table:
|
|
311
472
|
|
|
312
|
-
|
|
473
|
+
```bash
|
|
474
|
+
$ bin/rake db:migrate
|
|
475
|
+
```
|
|
476
|
+
|
|
477
|
+
To show the comments on a post, edit `app/views/blorgh/posts/show.html.erb` and
|
|
478
|
+
add this line before the "Edit" link:
|
|
313
479
|
|
|
314
480
|
```html+erb
|
|
315
481
|
<h3>Comments</h3>
|
|
316
482
|
<%= render @post.comments %>
|
|
317
483
|
```
|
|
318
484
|
|
|
319
|
-
This line will require there to be a `has_many` association for comments defined
|
|
485
|
+
This line will require there to be a `has_many` association for comments defined
|
|
486
|
+
on the `Blorgh::Post` model, which there isn't right now. To define one, open
|
|
487
|
+
`app/models/blorgh/post.rb` and add this line into the model:
|
|
320
488
|
|
|
321
489
|
```ruby
|
|
322
490
|
has_many :comments
|
|
@@ -332,28 +500,38 @@ module Blorgh
|
|
|
332
500
|
end
|
|
333
501
|
```
|
|
334
502
|
|
|
335
|
-
NOTE: Because the `has_many` is defined inside a class that is inside the
|
|
503
|
+
NOTE: Because the `has_many` is defined inside a class that is inside the
|
|
504
|
+
`Blorgh` module, Rails will know that you want to use the `Blorgh::Comment`
|
|
505
|
+
model for these objects, so there's no need to specify that using the
|
|
506
|
+
`:class_name` option here.
|
|
336
507
|
|
|
337
|
-
Next, there needs to be a form so that comments can be created on a post. To add
|
|
508
|
+
Next, there needs to be a form so that comments can be created on a post. To add
|
|
509
|
+
this, put this line underneath the call to `render @post.comments` in
|
|
510
|
+
`app/views/blorgh/posts/show.html.erb`:
|
|
338
511
|
|
|
339
512
|
```erb
|
|
340
513
|
<%= render "blorgh/comments/form" %>
|
|
341
514
|
```
|
|
342
515
|
|
|
343
|
-
Next, the partial that this line will render needs to exist. Create a new
|
|
516
|
+
Next, the partial that this line will render needs to exist. Create a new
|
|
517
|
+
directory at `app/views/blorgh/comments` and in it a new file called
|
|
518
|
+
`_form.html.erb` which has this content to create the required partial:
|
|
344
519
|
|
|
345
520
|
```html+erb
|
|
346
521
|
<h3>New comment</h3>
|
|
347
522
|
<%= form_for [@post, @post.comments.build] do |f| %>
|
|
348
523
|
<p>
|
|
349
|
-
<%= f.label :text %><br
|
|
524
|
+
<%= f.label :text %><br>
|
|
350
525
|
<%= f.text_area :text %>
|
|
351
526
|
</p>
|
|
352
527
|
<%= f.submit %>
|
|
353
528
|
<% end %>
|
|
354
529
|
```
|
|
355
530
|
|
|
356
|
-
When this form is submitted, it is going to attempt to perform a `POST` request
|
|
531
|
+
When this form is submitted, it is going to attempt to perform a `POST` request
|
|
532
|
+
to a route of `/posts/:post_id/comments` within the engine. This route doesn't
|
|
533
|
+
exist at the moment, but can be created by changing the `resources :posts` line
|
|
534
|
+
inside `config/routes.rb` into these lines:
|
|
357
535
|
|
|
358
536
|
```ruby
|
|
359
537
|
resources :posts do
|
|
@@ -363,10 +541,11 @@ end
|
|
|
363
541
|
|
|
364
542
|
This creates a nested route for the comments, which is what the form requires.
|
|
365
543
|
|
|
366
|
-
The route now exists, but the controller that this route goes to does not. To
|
|
544
|
+
The route now exists, but the controller that this route goes to does not. To
|
|
545
|
+
create it, run this command from the application root:
|
|
367
546
|
|
|
368
547
|
```bash
|
|
369
|
-
$ rails g controller comments
|
|
548
|
+
$ bin/rails g controller comments
|
|
370
549
|
```
|
|
371
550
|
|
|
372
551
|
This will generate the following things:
|
|
@@ -388,141 +567,227 @@ invoke css
|
|
|
388
567
|
create app/assets/stylesheets/blorgh/comments.css
|
|
389
568
|
```
|
|
390
569
|
|
|
391
|
-
The form will be making a `POST` request to `/posts/:post_id/comments`, which
|
|
570
|
+
The form will be making a `POST` request to `/posts/:post_id/comments`, which
|
|
571
|
+
will correspond with the `create` action in `Blorgh::CommentsController`. This
|
|
572
|
+
action needs to be created, which can be done by putting the following lines
|
|
573
|
+
inside the class definition in `app/controllers/blorgh/comments_controller.rb`:
|
|
392
574
|
|
|
393
575
|
```ruby
|
|
394
576
|
def create
|
|
395
577
|
@post = Post.find(params[:post_id])
|
|
396
|
-
@comment = @post.comments.create(
|
|
578
|
+
@comment = @post.comments.create(comment_params)
|
|
397
579
|
flash[:notice] = "Comment has been created!"
|
|
398
580
|
redirect_to posts_path
|
|
399
581
|
end
|
|
582
|
+
|
|
583
|
+
private
|
|
584
|
+
def comment_params
|
|
585
|
+
params.require(:comment).permit(:text)
|
|
586
|
+
end
|
|
400
587
|
```
|
|
401
588
|
|
|
402
|
-
This is the final
|
|
589
|
+
This is the final step required to get the new comment form working. Displaying
|
|
590
|
+
the comments, however, is not quite right yet. If you were to create a comment
|
|
591
|
+
right now, you would see this error:
|
|
403
592
|
|
|
404
593
|
```
|
|
405
|
-
Missing partial blorgh/comments/comment with {:handlers=>[:erb, :builder],
|
|
406
|
-
|
|
407
|
-
|
|
594
|
+
Missing partial blorgh/comments/comment with {:handlers=>[:erb, :builder],
|
|
595
|
+
:formats=>[:html], :locale=>[:en, :en]}. Searched in: *
|
|
596
|
+
"/Users/ryan/Sites/side_projects/blorgh/test/dummy/app/views" *
|
|
597
|
+
"/Users/ryan/Sites/side_projects/blorgh/app/views"
|
|
408
598
|
```
|
|
409
599
|
|
|
410
|
-
The engine is unable to find the partial required for rendering the comments.
|
|
600
|
+
The engine is unable to find the partial required for rendering the comments.
|
|
601
|
+
Rails looks first in the application's (`test/dummy`) `app/views` directory and
|
|
602
|
+
then in the engine's `app/views` directory. When it can't find it, it will throw
|
|
603
|
+
this error. The engine knows to look for `blorgh/comments/comment` because the
|
|
604
|
+
model object it is receiving is from the `Blorgh::Comment` class.
|
|
411
605
|
|
|
412
|
-
This partial will be responsible for rendering just the comment text, for now.
|
|
606
|
+
This partial will be responsible for rendering just the comment text, for now.
|
|
607
|
+
Create a new file at `app/views/blorgh/comments/_comment.html.erb` and put this
|
|
608
|
+
line inside it:
|
|
413
609
|
|
|
414
610
|
```erb
|
|
415
611
|
<%= comment_counter + 1 %>. <%= comment.text %>
|
|
416
612
|
```
|
|
417
613
|
|
|
418
|
-
The `comment_counter` local variable is given to us by the `<%= render
|
|
614
|
+
The `comment_counter` local variable is given to us by the `<%= render
|
|
615
|
+
@post.comments %>` call, which will define it automatically and increment the
|
|
616
|
+
counter as it iterates through each comment. It's used in this example to
|
|
617
|
+
display a small number next to each comment when it's created.
|
|
419
618
|
|
|
420
|
-
That completes the comment function of the blogging engine. Now it's time to use
|
|
619
|
+
That completes the comment function of the blogging engine. Now it's time to use
|
|
620
|
+
it within an application.
|
|
421
621
|
|
|
422
|
-
Hooking
|
|
622
|
+
Hooking Into an Application
|
|
423
623
|
---------------------------
|
|
424
624
|
|
|
425
|
-
Using an engine within an application is very easy. This section covers how to
|
|
625
|
+
Using an engine within an application is very easy. This section covers how to
|
|
626
|
+
mount the engine into an application and the initial setup required, as well as
|
|
627
|
+
linking the engine to a `User` class provided by the application to provide
|
|
628
|
+
ownership for posts and comments within the engine.
|
|
426
629
|
|
|
427
|
-
### Mounting the
|
|
630
|
+
### Mounting the Engine
|
|
428
631
|
|
|
429
|
-
First, the engine needs to be specified inside the application's `Gemfile`. If
|
|
632
|
+
First, the engine needs to be specified inside the application's `Gemfile`. If
|
|
633
|
+
there isn't an application handy to test this out in, generate one using the
|
|
634
|
+
`rails new` command outside of the engine directory like this:
|
|
430
635
|
|
|
431
636
|
```bash
|
|
432
637
|
$ rails new unicorn
|
|
433
638
|
```
|
|
434
639
|
|
|
435
|
-
Usually, specifying the engine inside the Gemfile would be done by specifying it
|
|
640
|
+
Usually, specifying the engine inside the Gemfile would be done by specifying it
|
|
641
|
+
as a normal, everyday gem.
|
|
436
642
|
|
|
437
643
|
```ruby
|
|
438
644
|
gem 'devise'
|
|
439
645
|
```
|
|
440
646
|
|
|
441
|
-
However, because you are developing the `blorgh` engine on your local machine,
|
|
647
|
+
However, because you are developing the `blorgh` engine on your local machine,
|
|
648
|
+
you will need to specify the `:path` option in your `Gemfile`:
|
|
442
649
|
|
|
443
650
|
```ruby
|
|
444
651
|
gem 'blorgh', path: "/path/to/blorgh"
|
|
445
652
|
```
|
|
446
653
|
|
|
447
|
-
|
|
654
|
+
Then run `bundle` to install the gem.
|
|
448
655
|
|
|
449
|
-
|
|
656
|
+
As described earlier, by placing the gem in the `Gemfile` it will be loaded when
|
|
657
|
+
Rails is loaded. It will first require `lib/blorgh.rb` from the engine, then
|
|
658
|
+
`lib/blorgh/engine.rb`, which is the file that defines the major pieces of
|
|
659
|
+
functionality for the engine.
|
|
660
|
+
|
|
661
|
+
To make the engine's functionality accessible from within an application, it
|
|
662
|
+
needs to be mounted in that application's `config/routes.rb` file:
|
|
450
663
|
|
|
451
664
|
```ruby
|
|
452
665
|
mount Blorgh::Engine, at: "/blog"
|
|
453
666
|
```
|
|
454
667
|
|
|
455
|
-
This line will mount the engine at `/blog` in the application. Making it
|
|
668
|
+
This line will mount the engine at `/blog` in the application. Making it
|
|
669
|
+
accessible at `http://localhost:3000/blog` when the application runs with `rails
|
|
670
|
+
server`.
|
|
456
671
|
|
|
457
|
-
NOTE: Other engines, such as Devise, handle this a little differently by making
|
|
672
|
+
NOTE: Other engines, such as Devise, handle this a little differently by making
|
|
673
|
+
you specify custom helpers (such as `devise_for`) in the routes. These helpers
|
|
674
|
+
do exactly the same thing, mounting pieces of the engines's functionality at a
|
|
675
|
+
pre-defined path which may be customizable.
|
|
458
676
|
|
|
459
677
|
### Engine setup
|
|
460
678
|
|
|
461
|
-
The engine contains migrations for the `blorgh_posts` and `blorgh_comments`
|
|
679
|
+
The engine contains migrations for the `blorgh_posts` and `blorgh_comments`
|
|
680
|
+
table which need to be created in the application's database so that the
|
|
681
|
+
engine's models can query them correctly. To copy these migrations into the
|
|
682
|
+
application use this command:
|
|
462
683
|
|
|
463
684
|
```bash
|
|
464
|
-
$ rake
|
|
685
|
+
$ bin/rake blorgh:install:migrations
|
|
465
686
|
```
|
|
466
687
|
|
|
467
|
-
If you have multiple engines that need migrations copied over, use
|
|
688
|
+
If you have multiple engines that need migrations copied over, use
|
|
689
|
+
`railties:install:migrations` instead:
|
|
468
690
|
|
|
469
691
|
```bash
|
|
470
|
-
$ rake railties:install:migrations
|
|
692
|
+
$ bin/rake railties:install:migrations
|
|
471
693
|
```
|
|
472
694
|
|
|
473
|
-
This command, when run for the first time, will copy over all the migrations
|
|
695
|
+
This command, when run for the first time, will copy over all the migrations
|
|
696
|
+
from the engine. When run the next time, it will only copy over migrations that
|
|
697
|
+
haven't been copied over already. The first run for this command will output
|
|
698
|
+
something such as this:
|
|
474
699
|
|
|
475
700
|
```bash
|
|
476
701
|
Copied migration [timestamp_1]_create_blorgh_posts.rb from blorgh
|
|
477
702
|
Copied migration [timestamp_2]_create_blorgh_comments.rb from blorgh
|
|
478
703
|
```
|
|
479
704
|
|
|
480
|
-
The first timestamp (`[timestamp_1]`) will be the current time and the second
|
|
705
|
+
The first timestamp (`[timestamp_1]`) will be the current time, and the second
|
|
706
|
+
timestamp (`[timestamp_2]`) will be the current time plus a second. The reason
|
|
707
|
+
for this is so that the migrations for the engine are run after any existing
|
|
708
|
+
migrations in the application.
|
|
481
709
|
|
|
482
|
-
To run these migrations within the context of the application, simply run `rake
|
|
710
|
+
To run these migrations within the context of the application, simply run `rake
|
|
711
|
+
db:migrate`. When accessing the engine through `http://localhost:3000/blog`, the
|
|
712
|
+
posts will be empty. This is because the table created inside the application is
|
|
713
|
+
different from the one created within the engine. Go ahead, play around with the
|
|
714
|
+
newly mounted engine. You'll find that it's the same as when it was only an
|
|
715
|
+
engine.
|
|
483
716
|
|
|
484
|
-
If you would like to run migrations only from one engine, you can do it by
|
|
717
|
+
If you would like to run migrations only from one engine, you can do it by
|
|
718
|
+
specifying `SCOPE`:
|
|
485
719
|
|
|
486
720
|
```bash
|
|
487
721
|
rake db:migrate SCOPE=blorgh
|
|
488
722
|
```
|
|
489
723
|
|
|
490
|
-
This may be useful if you want to revert engine's migrations before removing it.
|
|
724
|
+
This may be useful if you want to revert engine's migrations before removing it.
|
|
725
|
+
To revert all migrations from blorgh engine you can run code such as:
|
|
491
726
|
|
|
492
727
|
```bash
|
|
493
728
|
rake db:migrate SCOPE=blorgh VERSION=0
|
|
494
729
|
```
|
|
495
730
|
|
|
496
|
-
### Using a
|
|
731
|
+
### Using a Class Provided by the Application
|
|
497
732
|
|
|
498
|
-
#### Using a
|
|
733
|
+
#### Using a Model Provided by the Application
|
|
499
734
|
|
|
500
|
-
When an engine is created, it may want to use specific classes from an
|
|
735
|
+
When an engine is created, it may want to use specific classes from an
|
|
736
|
+
application to provide links between the pieces of the engine and the pieces of
|
|
737
|
+
the application. In the case of the `blorgh` engine, making posts and comments
|
|
738
|
+
have authors would make a lot of sense.
|
|
501
739
|
|
|
502
|
-
A typical application might have a `User` class that would be used to represent
|
|
740
|
+
A typical application might have a `User` class that would be used to represent
|
|
741
|
+
authors for a post or a comment. But there could be a case where the application
|
|
742
|
+
calls this class something different, such as `Person`. For this reason, the
|
|
743
|
+
engine should not hardcode associations specifically for a `User` class.
|
|
503
744
|
|
|
504
|
-
To keep it simple in this case, the application will have a class called `User`
|
|
745
|
+
To keep it simple in this case, the application will have a class called `User`
|
|
746
|
+
that represents the users of the application. It can be generated using this
|
|
747
|
+
command inside the application:
|
|
505
748
|
|
|
506
749
|
```bash
|
|
507
750
|
rails g model user name:string
|
|
508
751
|
```
|
|
509
752
|
|
|
510
|
-
The `rake db:migrate` command needs to be run here to ensure that our
|
|
753
|
+
The `rake db:migrate` command needs to be run here to ensure that our
|
|
754
|
+
application has the `users` table for future use.
|
|
511
755
|
|
|
512
|
-
Also, to keep it simple, the posts form will have a new text field called
|
|
756
|
+
Also, to keep it simple, the posts form will have a new text field called
|
|
757
|
+
`author_name`, where users can elect to put their name. The engine will then
|
|
758
|
+
take this name and either create a new `User` object from it, or find one that
|
|
759
|
+
already has that name. The engine will then associate the post with the found or
|
|
760
|
+
created `User` object.
|
|
513
761
|
|
|
514
|
-
First, the `author_name` text field needs to be added to the
|
|
762
|
+
First, the `author_name` text field needs to be added to the
|
|
763
|
+
`app/views/blorgh/posts/_form.html.erb` partial inside the engine. This can be
|
|
764
|
+
added above the `title` field with this code:
|
|
515
765
|
|
|
516
766
|
```html+erb
|
|
517
767
|
<div class="field">
|
|
518
|
-
<%= f.label :author_name %><br
|
|
768
|
+
<%= f.label :author_name %><br>
|
|
519
769
|
<%= f.text_field :author_name %>
|
|
520
770
|
</div>
|
|
521
771
|
```
|
|
522
772
|
|
|
523
|
-
|
|
773
|
+
Next, we need to update our `Blorgh::PostController#post_params` method to
|
|
774
|
+
permit the new form parameter:
|
|
775
|
+
|
|
776
|
+
```ruby
|
|
777
|
+
def post_params
|
|
778
|
+
params.require(:post).permit(:title, :text, :author_name)
|
|
779
|
+
end
|
|
780
|
+
```
|
|
781
|
+
|
|
782
|
+
The `Blorgh::Post` model should then have some code to convert the `author_name`
|
|
783
|
+
field into an actual `User` object and associate it as that post's `author`
|
|
784
|
+
before the post is saved. It will also need to have an `attr_accessor` set up
|
|
785
|
+
for this field, so that the setter and getter methods are defined for it.
|
|
524
786
|
|
|
525
|
-
To do all this, you'll need to add the `attr_accessor` for `author_name`, the
|
|
787
|
+
To do all this, you'll need to add the `attr_accessor` for `author_name`, the
|
|
788
|
+
association for the author and the `before_save` call into
|
|
789
|
+
`app/models/blorgh/post.rb`. The `author` association will be hard-coded to the
|
|
790
|
+
`User` class for the time being.
|
|
526
791
|
|
|
527
792
|
```ruby
|
|
528
793
|
attr_accessor :author_name
|
|
@@ -536,39 +801,53 @@ private
|
|
|
536
801
|
end
|
|
537
802
|
```
|
|
538
803
|
|
|
539
|
-
By
|
|
804
|
+
By representing the `author` association's object with the `User` class, a link
|
|
805
|
+
is established between the engine and the application. There needs to be a way
|
|
806
|
+
of associating the records in the `blorgh_posts` table with the records in the
|
|
807
|
+
`users` table. Because the association is called `author`, there should be an
|
|
808
|
+
`author_id` column added to the `blorgh_posts` table.
|
|
540
809
|
|
|
541
810
|
To generate this new column, run this command within the engine:
|
|
542
811
|
|
|
543
812
|
```bash
|
|
544
|
-
$ rails g migration add_author_id_to_blorgh_posts author_id:integer
|
|
813
|
+
$ bin/rails g migration add_author_id_to_blorgh_posts author_id:integer
|
|
545
814
|
```
|
|
546
815
|
|
|
547
|
-
NOTE: Due to the migration's name and the column specification after it, Rails
|
|
816
|
+
NOTE: Due to the migration's name and the column specification after it, Rails
|
|
817
|
+
will automatically know that you want to add a column to a specific table and
|
|
818
|
+
write that into the migration for you. You don't need to tell it any more than
|
|
819
|
+
this.
|
|
548
820
|
|
|
549
|
-
This migration will need to be run on the application. To do that, it must first
|
|
821
|
+
This migration will need to be run on the application. To do that, it must first
|
|
822
|
+
be copied using this command:
|
|
550
823
|
|
|
551
824
|
```bash
|
|
552
|
-
$ rake blorgh:install:migrations
|
|
825
|
+
$ bin/rake blorgh:install:migrations
|
|
553
826
|
```
|
|
554
827
|
|
|
555
|
-
Notice
|
|
828
|
+
Notice that only _one_ migration was copied over here. This is because the first
|
|
829
|
+
two migrations were copied over the first time this command was run.
|
|
556
830
|
|
|
557
831
|
```
|
|
558
|
-
NOTE Migration [timestamp]_create_blorgh_posts.rb from blorgh has been
|
|
559
|
-
|
|
560
|
-
|
|
832
|
+
NOTE Migration [timestamp]_create_blorgh_posts.rb from blorgh has been
|
|
833
|
+
skipped. Migration with the same name already exists. NOTE Migration
|
|
834
|
+
[timestamp]_create_blorgh_comments.rb from blorgh has been skipped. Migration
|
|
835
|
+
with the same name already exists. Copied migration
|
|
836
|
+
[timestamp]_add_author_id_to_blorgh_posts.rb from blorgh
|
|
561
837
|
```
|
|
562
838
|
|
|
563
|
-
Run
|
|
839
|
+
Run the migration using:
|
|
564
840
|
|
|
565
841
|
```bash
|
|
566
|
-
$ rake db:migrate
|
|
842
|
+
$ bin/rake db:migrate
|
|
567
843
|
```
|
|
568
844
|
|
|
569
|
-
Now with all the pieces in place, an action will take place that will associate
|
|
845
|
+
Now with all the pieces in place, an action will take place that will associate
|
|
846
|
+
an author - represented by a record in the `users` table - with a post,
|
|
847
|
+
represented by the `blorgh_posts` table from the engine.
|
|
570
848
|
|
|
571
|
-
Finally, the author's name should be displayed on the post's page. Add this code
|
|
849
|
+
Finally, the author's name should be displayed on the post's page. Add this code
|
|
850
|
+
above the "Title" output inside `app/views/blorgh/posts/show.html.erb`:
|
|
572
851
|
|
|
573
852
|
```html+erb
|
|
574
853
|
<p>
|
|
@@ -577,13 +856,15 @@ Finally, the author's name should be displayed on the post's page. Add this code
|
|
|
577
856
|
</p>
|
|
578
857
|
```
|
|
579
858
|
|
|
580
|
-
By outputting `@post.author` using the `<%=` tag, the `to_s` method will be
|
|
859
|
+
By outputting `@post.author` using the `<%=` tag, the `to_s` method will be
|
|
860
|
+
called on the object. By default, this will look quite ugly:
|
|
581
861
|
|
|
582
862
|
```
|
|
583
863
|
#<User:0x00000100ccb3b0>
|
|
584
864
|
```
|
|
585
865
|
|
|
586
|
-
This is undesirable
|
|
866
|
+
This is undesirable. It would be much better to have the user's name there. To
|
|
867
|
+
do this, add a `to_s` method to the `User` class within the application:
|
|
587
868
|
|
|
588
869
|
```ruby
|
|
589
870
|
def to_s
|
|
@@ -591,50 +872,77 @@ def to_s
|
|
|
591
872
|
end
|
|
592
873
|
```
|
|
593
874
|
|
|
594
|
-
Now instead of the ugly Ruby object output the author's name will be displayed.
|
|
875
|
+
Now instead of the ugly Ruby object output, the author's name will be displayed.
|
|
595
876
|
|
|
596
|
-
#### Using a
|
|
877
|
+
#### Using a Controller Provided by the Application
|
|
597
878
|
|
|
598
|
-
Because Rails controllers generally share code for things like authentication
|
|
879
|
+
Because Rails controllers generally share code for things like authentication
|
|
880
|
+
and accessing session variables, they inherit from `ApplicationController` by
|
|
881
|
+
default. Rails engines, however are scoped to run independently from the main
|
|
882
|
+
application, so each engine gets a scoped `ApplicationController`. This
|
|
883
|
+
namespace prevents code collisions, but often engine controllers need to access
|
|
884
|
+
methods in the main application's `ApplicationController`. An easy way to
|
|
885
|
+
provide this access is to change the engine's scoped `ApplicationController` to
|
|
886
|
+
inherit from the main application's `ApplicationController`. For our Blorgh
|
|
887
|
+
engine this would be done by changing
|
|
888
|
+
`app/controllers/blorgh/application_controller.rb` to look like:
|
|
599
889
|
|
|
600
890
|
```ruby
|
|
601
891
|
class Blorgh::ApplicationController < ApplicationController
|
|
602
892
|
end
|
|
603
893
|
```
|
|
604
894
|
|
|
605
|
-
By default, the engine's controllers inherit from
|
|
895
|
+
By default, the engine's controllers inherit from
|
|
896
|
+
`Blorgh::ApplicationController`. So, after making this change they will have
|
|
897
|
+
access to the main application's `ApplicationController`, as though they were
|
|
898
|
+
part of the main application.
|
|
606
899
|
|
|
607
|
-
This change does require that the engine is run from a Rails application that
|
|
900
|
+
This change does require that the engine is run from a Rails application that
|
|
901
|
+
has an `ApplicationController`.
|
|
608
902
|
|
|
609
|
-
### Configuring an
|
|
903
|
+
### Configuring an Engine
|
|
610
904
|
|
|
611
|
-
This section covers how to make the `User` class configurable, followed by
|
|
905
|
+
This section covers how to make the `User` class configurable, followed by
|
|
906
|
+
general configuration tips for the engine.
|
|
612
907
|
|
|
613
|
-
#### Setting
|
|
908
|
+
#### Setting Configuration Settings in the Application
|
|
614
909
|
|
|
615
|
-
The next step is to make the class that represents a `User` in the application
|
|
910
|
+
The next step is to make the class that represents a `User` in the application
|
|
911
|
+
customizable for the engine. This is because that class may not always be
|
|
912
|
+
`User`, as previously explained. To make this setting customizable, the engine
|
|
913
|
+
will have a configuration setting called `author_class` that will be used to
|
|
914
|
+
specify which class represents users inside the application.
|
|
616
915
|
|
|
617
|
-
To define this configuration setting, you should use a `mattr_accessor` inside
|
|
916
|
+
To define this configuration setting, you should use a `mattr_accessor` inside
|
|
917
|
+
the `Blorgh` module for the engine. Add this line to `lib/blorgh.rb` inside the
|
|
918
|
+
engine:
|
|
618
919
|
|
|
619
920
|
```ruby
|
|
620
921
|
mattr_accessor :author_class
|
|
621
922
|
```
|
|
622
923
|
|
|
623
|
-
This method works like its brothers `attr_accessor` and `cattr_accessor`, but
|
|
924
|
+
This method works like its brothers, `attr_accessor` and `cattr_accessor`, but
|
|
925
|
+
provides a setter and getter method on the module with the specified name. To
|
|
926
|
+
use it, it must be referenced using `Blorgh.author_class`.
|
|
624
927
|
|
|
625
|
-
The next step is
|
|
928
|
+
The next step is to switch the `Blorgh::Post` model over to this new setting.
|
|
929
|
+
Change the `belongs_to` association inside this model
|
|
930
|
+
(`app/models/blorgh/post.rb`) to this:
|
|
626
931
|
|
|
627
932
|
```ruby
|
|
628
933
|
belongs_to :author, class_name: Blorgh.author_class
|
|
629
934
|
```
|
|
630
935
|
|
|
631
|
-
The `set_author` method
|
|
936
|
+
The `set_author` method in the `Blorgh::Post` model should also use this class:
|
|
632
937
|
|
|
633
938
|
```ruby
|
|
634
939
|
self.author = Blorgh.author_class.constantize.find_or_create_by(name: author_name)
|
|
635
940
|
```
|
|
636
941
|
|
|
637
|
-
To save having to call `constantize` on the `author_class` result all the time,
|
|
942
|
+
To save having to call `constantize` on the `author_class` result all the time,
|
|
943
|
+
you could instead just override the `author_class` getter method inside the
|
|
944
|
+
`Blorgh` module in the `lib/blorgh.rb` file to always call `constantize` on the
|
|
945
|
+
saved value before returning the result:
|
|
638
946
|
|
|
639
947
|
```ruby
|
|
640
948
|
def self.author_class
|
|
@@ -648,80 +956,169 @@ This would then turn the above code for `set_author` into this:
|
|
|
648
956
|
self.author = Blorgh.author_class.find_or_create_by(name: author_name)
|
|
649
957
|
```
|
|
650
958
|
|
|
651
|
-
Resulting in something a little shorter, and more implicit in its behavior. The
|
|
959
|
+
Resulting in something a little shorter, and more implicit in its behavior. The
|
|
960
|
+
`author_class` method should always return a `Class` object.
|
|
652
961
|
|
|
653
|
-
Since we changed the `author_class` method to
|
|
654
|
-
`String
|
|
655
|
-
|
|
962
|
+
Since we changed the `author_class` method to return a `Class` instead of a
|
|
963
|
+
`String`, we must also modify our `belongs_to` definition in the `Blorgh::Post`
|
|
964
|
+
model:
|
|
656
965
|
|
|
657
966
|
```ruby
|
|
658
967
|
belongs_to :author, class_name: Blorgh.author_class.to_s
|
|
659
968
|
```
|
|
660
969
|
|
|
661
|
-
To set this configuration setting within the application, an initializer should
|
|
970
|
+
To set this configuration setting within the application, an initializer should
|
|
971
|
+
be used. By using an initializer, the configuration will be set up before the
|
|
972
|
+
application starts and calls the engine's models, which may depend on this
|
|
973
|
+
configuration setting existing.
|
|
662
974
|
|
|
663
|
-
Create a new initializer at `config/initializers/blorgh.rb` inside the
|
|
975
|
+
Create a new initializer at `config/initializers/blorgh.rb` inside the
|
|
976
|
+
application where the `blorgh` engine is installed and put this content in it:
|
|
664
977
|
|
|
665
978
|
```ruby
|
|
666
979
|
Blorgh.author_class = "User"
|
|
667
980
|
```
|
|
668
981
|
|
|
669
|
-
WARNING: It's very important here to use the `String` version of the class,
|
|
982
|
+
WARNING: It's very important here to use the `String` version of the class,
|
|
983
|
+
rather than the class itself. If you were to use the class, Rails would attempt
|
|
984
|
+
to load that class and then reference the related table. This could lead to
|
|
985
|
+
problems if the table wasn't already existing. Therefore, a `String` should be
|
|
986
|
+
used and then converted to a class using `constantize` in the engine later on.
|
|
670
987
|
|
|
671
|
-
Go ahead and try to create a new post. You will see that it works exactly in the
|
|
988
|
+
Go ahead and try to create a new post. You will see that it works exactly in the
|
|
989
|
+
same way as before, except this time the engine is using the configuration
|
|
990
|
+
setting in `config/initializers/blorgh.rb` to learn what the class is.
|
|
672
991
|
|
|
673
|
-
There are now no strict dependencies on what the class is, only what the API for
|
|
992
|
+
There are now no strict dependencies on what the class is, only what the API for
|
|
993
|
+
the class must be. The engine simply requires this class to define a
|
|
994
|
+
`find_or_create_by` method which returns an object of that class, to be
|
|
995
|
+
associated with a post when it's created. This object, of course, should have
|
|
996
|
+
some sort of identifier by which it can be referenced.
|
|
674
997
|
|
|
675
|
-
#### General
|
|
998
|
+
#### General Engine Configuration
|
|
676
999
|
|
|
677
|
-
Within an engine, there may come a time where you wish to use things such as
|
|
1000
|
+
Within an engine, there may come a time where you wish to use things such as
|
|
1001
|
+
initializers, internationalization or other configuration options. The great
|
|
1002
|
+
news is that these things are entirely possible, because a Rails engine shares
|
|
1003
|
+
much the same functionality as a Rails application. In fact, a Rails
|
|
1004
|
+
application's functionality is actually a superset of what is provided by
|
|
1005
|
+
engines!
|
|
678
1006
|
|
|
679
|
-
If you wish to use an initializer
|
|
680
|
-
loaded
|
|
681
|
-
functionality is explained in the
|
|
682
|
-
|
|
683
|
-
|
|
684
|
-
|
|
1007
|
+
If you wish to use an initializer - code that should run before the engine is
|
|
1008
|
+
loaded - the place for it is the `config/initializers` folder. This directory's
|
|
1009
|
+
functionality is explained in the [Initializers
|
|
1010
|
+
section](configuring.html#initializers) of the Configuring guide, and works
|
|
1011
|
+
precisely the same way as the `config/initializers` directory inside an
|
|
1012
|
+
application. The same thing goes if you want to use a standard initializer.
|
|
685
1013
|
|
|
686
|
-
For locales, simply place the locale files in the `config/locales` directory,
|
|
1014
|
+
For locales, simply place the locale files in the `config/locales` directory,
|
|
1015
|
+
just like you would in an application.
|
|
687
1016
|
|
|
688
1017
|
Testing an engine
|
|
689
1018
|
-----------------
|
|
690
1019
|
|
|
691
|
-
When an engine is generated there is a smaller dummy application created inside
|
|
1020
|
+
When an engine is generated, there is a smaller dummy application created inside
|
|
1021
|
+
it at `test/dummy`. This application is used as a mounting point for the engine,
|
|
1022
|
+
to make testing the engine extremely simple. You may extend this application by
|
|
1023
|
+
generating controllers, models or views from within the directory, and then use
|
|
1024
|
+
those to test your engine.
|
|
692
1025
|
|
|
693
|
-
The `test` directory should be treated like a typical Rails testing environment,
|
|
1026
|
+
The `test` directory should be treated like a typical Rails testing environment,
|
|
1027
|
+
allowing for unit, functional and integration tests.
|
|
694
1028
|
|
|
695
|
-
### Functional
|
|
1029
|
+
### Functional Tests
|
|
696
1030
|
|
|
697
|
-
A matter worth taking into consideration when writing functional tests is that
|
|
1031
|
+
A matter worth taking into consideration when writing functional tests is that
|
|
1032
|
+
the tests are going to be running on an application - the `test/dummy`
|
|
1033
|
+
application - rather than your engine. This is due to the setup of the testing
|
|
1034
|
+
environment; an engine needs an application as a host for testing its main
|
|
1035
|
+
functionality, especially controllers. This means that if you were to make a
|
|
1036
|
+
typical `GET` to a controller in a controller's functional test like this:
|
|
698
1037
|
|
|
699
1038
|
```ruby
|
|
700
|
-
|
|
1039
|
+
module Blorgh
|
|
1040
|
+
class FooControllerTest < ActionController::TestCase
|
|
1041
|
+
def test_index
|
|
1042
|
+
get :index
|
|
1043
|
+
...
|
|
1044
|
+
end
|
|
1045
|
+
end
|
|
1046
|
+
end
|
|
701
1047
|
```
|
|
702
1048
|
|
|
703
|
-
It may not function correctly. This is because the application doesn't know how
|
|
1049
|
+
It may not function correctly. This is because the application doesn't know how
|
|
1050
|
+
to route these requests to the engine unless you explicitly tell it **how**. To
|
|
1051
|
+
do this, you must set the `@routes` instance variable to the engine's route set
|
|
1052
|
+
in your setup code:
|
|
704
1053
|
|
|
705
1054
|
```ruby
|
|
706
|
-
|
|
1055
|
+
module Blorgh
|
|
1056
|
+
class FooControllerTest < ActionController::TestCase
|
|
1057
|
+
setup do
|
|
1058
|
+
@routes = Engine.routes
|
|
1059
|
+
end
|
|
1060
|
+
|
|
1061
|
+
def test_index
|
|
1062
|
+
get :index
|
|
1063
|
+
...
|
|
1064
|
+
end
|
|
1065
|
+
end
|
|
1066
|
+
end
|
|
707
1067
|
```
|
|
708
1068
|
|
|
709
|
-
This tells the application that you still want to perform a `GET` request to the
|
|
1069
|
+
This tells the application that you still want to perform a `GET` request to the
|
|
1070
|
+
`index` action of this controller, but you want to use the engine's route to get
|
|
1071
|
+
there, rather than the application's one.
|
|
1072
|
+
|
|
1073
|
+
This also ensures that the engine's URL helpers will work as expected in your
|
|
1074
|
+
tests.
|
|
710
1075
|
|
|
711
1076
|
Improving engine functionality
|
|
712
1077
|
------------------------------
|
|
713
1078
|
|
|
714
|
-
This section explains how to add and/or override engine MVC functionality in the
|
|
1079
|
+
This section explains how to add and/or override engine MVC functionality in the
|
|
1080
|
+
main Rails application.
|
|
715
1081
|
|
|
716
1082
|
### Overriding Models and Controllers
|
|
717
1083
|
|
|
718
|
-
Engine model and controller classes can be extended by open classing them in the
|
|
1084
|
+
Engine model and controller classes can be extended by open classing them in the
|
|
1085
|
+
main Rails application (since model and controller classes are just Ruby classes
|
|
1086
|
+
that inherit Rails specific functionality). Open classing an Engine class
|
|
1087
|
+
redefines it for use in the main application. This is usually implemented by
|
|
1088
|
+
using the decorator pattern.
|
|
1089
|
+
|
|
1090
|
+
For simple class modifications, use `Class#class_eval`. For complex class
|
|
1091
|
+
modifications, consider using `ActiveSupport::Concern`.
|
|
719
1092
|
|
|
720
|
-
|
|
1093
|
+
#### A note on Decorators and Loading Code
|
|
1094
|
+
|
|
1095
|
+
Because these decorators are not referenced by your Rails application itself,
|
|
1096
|
+
Rails' autoloading system will not kick in and load your decorators. This means
|
|
1097
|
+
that you need to require them yourself.
|
|
1098
|
+
|
|
1099
|
+
Here is some sample code to do this:
|
|
1100
|
+
|
|
1101
|
+
```ruby
|
|
1102
|
+
# lib/blorgh/engine.rb
|
|
1103
|
+
module Blorgh
|
|
1104
|
+
class Engine < ::Rails::Engine
|
|
1105
|
+
isolate_namespace Blorgh
|
|
1106
|
+
|
|
1107
|
+
config.to_prepare do
|
|
1108
|
+
Dir.glob(Rails.root + "app/decorators/**/*_decorator*.rb").each do |c|
|
|
1109
|
+
require_dependency(c)
|
|
1110
|
+
end
|
|
1111
|
+
end
|
|
1112
|
+
end
|
|
1113
|
+
end
|
|
1114
|
+
```
|
|
1115
|
+
|
|
1116
|
+
This doesn't apply to just Decorators, but anything that you add in an engine
|
|
1117
|
+
that isn't referenced by your main application.
|
|
721
1118
|
|
|
722
1119
|
#### Implementing Decorator Pattern Using Class#class_eval
|
|
723
1120
|
|
|
724
|
-
**Adding** `Post#time_since_created
|
|
1121
|
+
**Adding** `Post#time_since_created`:
|
|
725
1122
|
|
|
726
1123
|
```ruby
|
|
727
1124
|
# MyApp/app/decorators/models/blorgh/post_decorator.rb
|
|
@@ -742,7 +1139,7 @@ end
|
|
|
742
1139
|
```
|
|
743
1140
|
|
|
744
1141
|
|
|
745
|
-
**Overriding** `Post#summary
|
|
1142
|
+
**Overriding** `Post#summary`:
|
|
746
1143
|
|
|
747
1144
|
```ruby
|
|
748
1145
|
# MyApp/app/decorators/models/blorgh/post_decorator.rb
|
|
@@ -767,9 +1164,13 @@ end
|
|
|
767
1164
|
|
|
768
1165
|
#### Implementing Decorator Pattern Using ActiveSupport::Concern
|
|
769
1166
|
|
|
770
|
-
Using `Class#class_eval` is great for simple adjustments, but for more complex
|
|
1167
|
+
Using `Class#class_eval` is great for simple adjustments, but for more complex
|
|
1168
|
+
class modifications, you might want to consider using [`ActiveSupport::Concern`]
|
|
1169
|
+
(http://edgeapi.rubyonrails.org/classes/ActiveSupport/Concern.html).
|
|
1170
|
+
ActiveSupport::Concern manages load order of interlinked dependent modules and
|
|
1171
|
+
classes at run time allowing you to significantly modularize your code.
|
|
771
1172
|
|
|
772
|
-
**Adding** `Post#time_since_created` and **Overriding** `Post#summary
|
|
1173
|
+
**Adding** `Post#time_since_created` and **Overriding** `Post#summary`:
|
|
773
1174
|
|
|
774
1175
|
```ruby
|
|
775
1176
|
# MyApp/app/models/blorgh/post.rb
|
|
@@ -802,7 +1203,7 @@ module Blorgh::Concerns::Models::Post
|
|
|
802
1203
|
extend ActiveSupport::Concern
|
|
803
1204
|
|
|
804
1205
|
# 'included do' causes the included code to be evaluated in the
|
|
805
|
-
# context where it is included (post.rb), rather than
|
|
1206
|
+
# context where it is included (post.rb), rather than being
|
|
806
1207
|
# executed in the module's context (blorgh/concerns/models/post).
|
|
807
1208
|
included do
|
|
808
1209
|
attr_accessor :author_name
|
|
@@ -811,7 +1212,6 @@ module Blorgh::Concerns::Models::Post
|
|
|
811
1212
|
before_save :set_author
|
|
812
1213
|
|
|
813
1214
|
private
|
|
814
|
-
|
|
815
1215
|
def set_author
|
|
816
1216
|
self.author = User.find_or_create_by(name: author_name)
|
|
817
1217
|
end
|
|
@@ -829,15 +1229,23 @@ module Blorgh::Concerns::Models::Post
|
|
|
829
1229
|
end
|
|
830
1230
|
```
|
|
831
1231
|
|
|
832
|
-
### Overriding
|
|
1232
|
+
### Overriding Views
|
|
833
1233
|
|
|
834
|
-
When Rails looks for a view to render, it will first look in the `app/views`
|
|
1234
|
+
When Rails looks for a view to render, it will first look in the `app/views`
|
|
1235
|
+
directory of the application. If it cannot find the view there, it will check in
|
|
1236
|
+
the `app/views` directories of all engines that have this directory.
|
|
835
1237
|
|
|
836
|
-
|
|
1238
|
+
When the application is asked to render the view for `Blorgh::PostsController`'s
|
|
1239
|
+
index action, it will first look for the path
|
|
1240
|
+
`app/views/blorgh/posts/index.html.erb` within the application. If it cannot
|
|
1241
|
+
find it, it will look inside the engine.
|
|
837
1242
|
|
|
838
|
-
You can override this view in the application by simply creating a new file at
|
|
1243
|
+
You can override this view in the application by simply creating a new file at
|
|
1244
|
+
`app/views/blorgh/posts/index.html.erb`. Then you can completely change what
|
|
1245
|
+
this view would normally output.
|
|
839
1246
|
|
|
840
|
-
Try this now by creating a new file at `app/views/blorgh/posts/index.html.erb`
|
|
1247
|
+
Try this now by creating a new file at `app/views/blorgh/posts/index.html.erb`
|
|
1248
|
+
and put this content in it:
|
|
841
1249
|
|
|
842
1250
|
```html+erb
|
|
843
1251
|
<h1>Posts</h1>
|
|
@@ -852,9 +1260,13 @@ Try this now by creating a new file at `app/views/blorgh/posts/index.html.erb` a
|
|
|
852
1260
|
|
|
853
1261
|
### Routes
|
|
854
1262
|
|
|
855
|
-
Routes inside an engine are
|
|
1263
|
+
Routes inside an engine are isolated from the application by default. This is
|
|
1264
|
+
done by the `isolate_namespace` call inside the `Engine` class. This essentially
|
|
1265
|
+
means that the application and its engines can have identically named routes and
|
|
1266
|
+
they will not clash.
|
|
856
1267
|
|
|
857
|
-
Routes inside an engine are drawn on the `Engine` class within
|
|
1268
|
+
Routes inside an engine are drawn on the `Engine` class within
|
|
1269
|
+
`config/routes.rb`, like this:
|
|
858
1270
|
|
|
859
1271
|
```ruby
|
|
860
1272
|
Blorgh::Engine.routes.draw do
|
|
@@ -862,43 +1274,71 @@ Blorgh::Engine.routes.draw do
|
|
|
862
1274
|
end
|
|
863
1275
|
```
|
|
864
1276
|
|
|
865
|
-
By having isolated routes such as this, if you wish to link to an area of an
|
|
1277
|
+
By having isolated routes such as this, if you wish to link to an area of an
|
|
1278
|
+
engine from within an application, you will need to use the engine's routing
|
|
1279
|
+
proxy method. Calls to normal routing methods such as `posts_path` may end up
|
|
1280
|
+
going to undesired locations if both the application and the engine have such a
|
|
1281
|
+
helper defined.
|
|
866
1282
|
|
|
867
|
-
For instance, the following example would go to the application's `posts_path`
|
|
1283
|
+
For instance, the following example would go to the application's `posts_path`
|
|
1284
|
+
if that template was rendered from the application, or the engine's `posts_path`
|
|
1285
|
+
if it was rendered from the engine:
|
|
868
1286
|
|
|
869
1287
|
```erb
|
|
870
1288
|
<%= link_to "Blog posts", posts_path %>
|
|
871
1289
|
```
|
|
872
1290
|
|
|
873
|
-
To make this route always use the engine's `posts_path` routing helper method,
|
|
1291
|
+
To make this route always use the engine's `posts_path` routing helper method,
|
|
1292
|
+
we must call the method on the routing proxy method that shares the same name as
|
|
1293
|
+
the engine.
|
|
874
1294
|
|
|
875
1295
|
```erb
|
|
876
1296
|
<%= link_to "Blog posts", blorgh.posts_path %>
|
|
877
1297
|
```
|
|
878
1298
|
|
|
879
|
-
If you wish to reference the application inside the engine in a similar way, use
|
|
1299
|
+
If you wish to reference the application inside the engine in a similar way, use
|
|
1300
|
+
the `main_app` helper:
|
|
880
1301
|
|
|
881
1302
|
```erb
|
|
882
1303
|
<%= link_to "Home", main_app.root_path %>
|
|
883
1304
|
```
|
|
884
1305
|
|
|
885
|
-
If you were to use this inside an engine, it would **always** go to the
|
|
1306
|
+
If you were to use this inside an engine, it would **always** go to the
|
|
1307
|
+
application's root. If you were to leave off the `main_app` "routing proxy"
|
|
1308
|
+
method call, it could potentially go to the engine's or application's root,
|
|
1309
|
+
depending on where it was called from.
|
|
886
1310
|
|
|
887
|
-
If a template
|
|
1311
|
+
If a template rendered from within an engine attempts to use one of the
|
|
1312
|
+
application's routing helper methods, it may result in an undefined method call.
|
|
1313
|
+
If you encounter such an issue, ensure that you're not attempting to call the
|
|
1314
|
+
application's routing methods without the `main_app` prefix from within the
|
|
1315
|
+
engine.
|
|
888
1316
|
|
|
889
1317
|
### Assets
|
|
890
1318
|
|
|
891
|
-
Assets within an engine work in an identical way to a full application. Because
|
|
1319
|
+
Assets within an engine work in an identical way to a full application. Because
|
|
1320
|
+
the engine class inherits from `Rails::Engine`, the application will know to
|
|
1321
|
+
look up assets in the engine's 'app/assets' and 'lib/assets' directories.
|
|
892
1322
|
|
|
893
|
-
|
|
1323
|
+
Like all of the other components of an engine, the assets should be namespaced.
|
|
1324
|
+
This means that if you have an asset called `style.css`, it should be placed at
|
|
1325
|
+
`app/assets/stylesheets/[engine name]/style.css`, rather than
|
|
1326
|
+
`app/assets/stylesheets/style.css`. If this asset isn't namespaced, there is a
|
|
1327
|
+
possibility that the host application could have an asset named identically, in
|
|
1328
|
+
which case the application's asset would take precedence and the engine's one
|
|
1329
|
+
would be ignored.
|
|
894
1330
|
|
|
895
|
-
Imagine that you did have an asset located at
|
|
1331
|
+
Imagine that you did have an asset located at
|
|
1332
|
+
`app/assets/stylesheets/blorgh/style.css` To include this asset inside an
|
|
1333
|
+
application, just use `stylesheet_link_tag` and reference the asset as if it
|
|
1334
|
+
were inside the engine:
|
|
896
1335
|
|
|
897
1336
|
```erb
|
|
898
1337
|
<%= stylesheet_link_tag "blorgh/style.css" %>
|
|
899
1338
|
```
|
|
900
1339
|
|
|
901
|
-
You can also specify these assets as dependencies of other assets using
|
|
1340
|
+
You can also specify these assets as dependencies of other assets using Asset
|
|
1341
|
+
Pipeline require statements in processed files:
|
|
902
1342
|
|
|
903
1343
|
```
|
|
904
1344
|
/*
|
|
@@ -906,16 +1346,21 @@ You can also specify these assets as dependencies of other assets using the Asse
|
|
|
906
1346
|
*/
|
|
907
1347
|
```
|
|
908
1348
|
|
|
909
|
-
INFO. Remember that in order to use languages like Sass or CoffeeScript, you
|
|
1349
|
+
INFO. Remember that in order to use languages like Sass or CoffeeScript, you
|
|
1350
|
+
should add the relevant library to your engine's `.gemspec`.
|
|
910
1351
|
|
|
911
1352
|
### Separate Assets & Precompiling
|
|
912
1353
|
|
|
913
|
-
There are some situations where your engine's assets are not required by the
|
|
914
|
-
|
|
915
|
-
|
|
916
|
-
|
|
1354
|
+
There are some situations where your engine's assets are not required by the
|
|
1355
|
+
host application. For example, say that you've created an admin functionality
|
|
1356
|
+
that only exists for your engine. In this case, the host application doesn't
|
|
1357
|
+
need to require `admin.css` or `admin.js`. Only the gem's admin layout needs
|
|
1358
|
+
these assets. It doesn't make sense for the host app to include
|
|
1359
|
+
`"blorgh/admin.css"` in its stylesheets. In this situation, you should
|
|
1360
|
+
explicitly define these assets for precompilation. This tells sprockets to add
|
|
1361
|
+
your engine assets when `rake assets:precompile` is triggered.
|
|
917
1362
|
|
|
918
|
-
You can define assets for precompilation in `engine.rb
|
|
1363
|
+
You can define assets for precompilation in `engine.rb`:
|
|
919
1364
|
|
|
920
1365
|
```ruby
|
|
921
1366
|
initializer "blorgh.assets.precompile" do |app|
|
|
@@ -923,15 +1368,15 @@ initializer "blorgh.assets.precompile" do |app|
|
|
|
923
1368
|
end
|
|
924
1369
|
```
|
|
925
1370
|
|
|
926
|
-
For more information, read the [Asset Pipeline guide](asset_pipeline.html)
|
|
1371
|
+
For more information, read the [Asset Pipeline guide](asset_pipeline.html).
|
|
927
1372
|
|
|
928
|
-
### Other
|
|
1373
|
+
### Other Gem Dependencies
|
|
929
1374
|
|
|
930
|
-
Gem dependencies inside an engine should be specified inside the
|
|
931
|
-
|
|
932
|
-
|
|
933
|
-
|
|
934
|
-
|
|
1375
|
+
Gem dependencies inside an engine should be specified inside the `.gemspec` file
|
|
1376
|
+
at the root of the engine. The reason is that the engine may be installed as a
|
|
1377
|
+
gem. If dependencies were to be specified inside the `Gemfile`, these would not
|
|
1378
|
+
be recognized by a traditional gem install and so they would not be installed,
|
|
1379
|
+
causing the engine to malfunction.
|
|
935
1380
|
|
|
936
1381
|
To specify a dependency that should be installed with the engine during a
|
|
937
1382
|
traditional `gem install`, specify it inside the `Gem::Specification` block
|
|
@@ -949,11 +1394,12 @@ s.add_development_dependency "moo"
|
|
|
949
1394
|
```
|
|
950
1395
|
|
|
951
1396
|
Both kinds of dependencies will be installed when `bundle install` is run inside
|
|
952
|
-
the application. The development dependencies for the gem will only be used
|
|
953
|
-
the tests for the engine are running.
|
|
1397
|
+
of the application. The development dependencies for the gem will only be used
|
|
1398
|
+
when the tests for the engine are running.
|
|
954
1399
|
|
|
955
1400
|
Note that if you want to immediately require dependencies when the engine is
|
|
956
|
-
required, you should require them before the engine's initialization. For
|
|
1401
|
+
required, you should require them before the engine's initialization. For
|
|
1402
|
+
example:
|
|
957
1403
|
|
|
958
1404
|
```ruby
|
|
959
1405
|
require 'other_engine/engine'
|