rails 4.0.13 → 4.1.0.beta1
Sign up to get free protection for your applications and to get access to all the features.
Potentially problematic release.
This version of rails might be problematic. Click here for more details.
- checksums.yaml +4 -4
- data/README.md +20 -15
- data/guides/CHANGELOG.md +5 -74
- 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/challenge.png +0 -0
- 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/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/template_is_missing_posts_new.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/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 +2 -0
- data/guides/bug_report_templates/action_controller_master.rb +2 -0
- data/guides/bug_report_templates/active_record_gem.rb +1 -1
- data/guides/bug_report_templates/active_record_master.rb +2 -1
- data/guides/code/getting_started/Gemfile +1 -1
- data/guides/code/getting_started/app/assets/javascripts/application.js +1 -2
- data/guides/code/getting_started/config/environments/development.rb +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/rails_guides/helpers.rb +1 -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 +1 -2
- data/guides/source/3_1_release_notes.md +1 -1
- 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 +601 -0
- data/guides/source/_welcome.html.erb +1 -1
- data/guides/source/action_controller_overview.md +117 -31
- data/guides/source/action_mailer_basics.md +19 -19
- data/guides/source/action_view_overview.md +131 -12
- 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 +67 -39
- data/guides/source/active_record_validations.md +31 -31
- data/guides/source/active_support_core_extensions.md +63 -74
- data/guides/source/active_support_instrumentation.md +13 -4
- data/guides/source/api_documentation_guidelines.md +19 -5
- data/guides/source/asset_pipeline.md +544 -249
- data/guides/source/association_basics.md +81 -22
- data/guides/source/caching_with_rails.md +15 -6
- data/guides/source/command_line.md +28 -19
- data/guides/source/configuring.md +98 -50
- data/guides/source/contributing_to_ruby_on_rails.md +11 -11
- data/guides/source/credits.html.erb +2 -2
- data/guides/source/debugging_rails_applications.md +36 -5
- data/guides/source/development_dependencies_install.md +89 -8
- data/guides/source/documents.yaml +7 -1
- data/guides/source/engines.md +648 -224
- data/guides/source/form_helpers.md +56 -45
- data/guides/source/generators.md +7 -3
- data/guides/source/getting_started.md +379 -164
- data/guides/source/i18n.md +59 -23
- data/guides/source/index.html.erb +1 -1
- data/guides/source/initialization.md +153 -56
- data/guides/source/kindle/toc.html.erb +1 -1
- data/guides/source/layout.html.erb +3 -3
- data/guides/source/layouts_and_rendering.md +12 -11
- data/guides/source/maintenance_policy.md +4 -23
- data/guides/source/migrations.md +41 -37
- data/guides/source/nested_model_forms.md +3 -3
- data/guides/source/plugins.md +27 -23
- data/guides/source/rails_application_templates.md +25 -6
- data/guides/source/rails_on_rack.md +35 -51
- data/guides/source/routing.md +108 -99
- data/guides/source/ruby_on_rails_guides_guidelines.md +2 -2
- data/guides/source/security.md +33 -31
- data/guides/source/testing.md +37 -34
- data/guides/source/upgrading_ruby_on_rails.md +335 -16
- data/guides/source/working_with_javascript_in_rails.md +18 -10
- metadata +66 -39
- data/guides/assets/images/jaimeiniesta.jpg +0 -0
- data/guides/source/kindle/KINDLE.md +0 -26
@@ -5,6 +5,10 @@ This guide covers how to setup an environment for Ruby on Rails core development
|
|
5
5
|
|
6
6
|
After reading this guide, you will know:
|
7
7
|
|
8
|
+
* How to set up your machine for Rails development
|
9
|
+
* How to run specific groups of unit tests from the Rails test suite
|
10
|
+
* How the ActiveRecord portion of the Rails test suite operates
|
11
|
+
|
8
12
|
--------------------------------------------------------------------------------
|
9
13
|
|
10
14
|
The Easy Way
|
@@ -53,9 +57,24 @@ If you are on Fedora or CentOS, you can run
|
|
53
57
|
$ sudo yum install libxml2 libxml2-devel libxslt libxslt-devel
|
54
58
|
```
|
55
59
|
|
56
|
-
If you
|
60
|
+
If you are running Arch Linux, you're done with:
|
61
|
+
|
62
|
+
```bash
|
63
|
+
$ sudo pacman -S libxml2 libxslt
|
64
|
+
```
|
65
|
+
|
66
|
+
On FreeBSD, you just have to run:
|
67
|
+
|
68
|
+
```bash
|
69
|
+
# pkg_add -r libxml2 libxslt
|
70
|
+
```
|
71
|
+
|
72
|
+
Alternatively, you can install the `textproc/libxml2` and `textproc/libxslt`
|
73
|
+
ports.
|
57
74
|
|
58
|
-
|
75
|
+
If you have any problems with these libraries, you can install them manually by compiling the source code. Just follow the instructions at the [Red Hat/CentOS section of the Nokogiri tutorials](http://nokogiri.org/tutorials/installing_nokogiri.html#red_hat__centos) .
|
76
|
+
|
77
|
+
Also, SQLite3 and its development files for the `sqlite3-ruby` gem - in Ubuntu you're done with just
|
59
78
|
|
60
79
|
```bash
|
61
80
|
$ sudo apt-get install sqlite3 libsqlite3-dev
|
@@ -67,6 +86,20 @@ And if you are on Fedora or CentOS, you're done with
|
|
67
86
|
$ sudo yum install sqlite3 sqlite3-devel
|
68
87
|
```
|
69
88
|
|
89
|
+
If you are on Arch Linux, you will need to run:
|
90
|
+
|
91
|
+
```bash
|
92
|
+
$ sudo pacman -S sqlite
|
93
|
+
```
|
94
|
+
|
95
|
+
For FreeBSD users, you're done with:
|
96
|
+
|
97
|
+
```bash
|
98
|
+
# pkg_add -r sqlite3
|
99
|
+
```
|
100
|
+
|
101
|
+
Or compile the `databases/sqlite3` port.
|
102
|
+
|
70
103
|
Get a recent version of [Bundler](http://gembundler.com/)
|
71
104
|
|
72
105
|
```bash
|
@@ -80,7 +113,29 @@ and run:
|
|
80
113
|
$ bundle install --without db
|
81
114
|
```
|
82
115
|
|
83
|
-
This command will install all dependencies except the MySQL and PostgreSQL Ruby drivers. We will come back to these soon.
|
116
|
+
This command will install all dependencies except the MySQL and PostgreSQL Ruby drivers. We will come back to these soon.
|
117
|
+
|
118
|
+
NOTE: If you would like to run the tests that use memcached, you need to ensure that you have it installed and running.
|
119
|
+
|
120
|
+
You can use homebrew to install memcached on OSX:
|
121
|
+
|
122
|
+
```bash
|
123
|
+
$ brew install memcached
|
124
|
+
```
|
125
|
+
|
126
|
+
On Ubuntu you can install it with apt-get:
|
127
|
+
|
128
|
+
```bash
|
129
|
+
$ sudo apt-get install memcached
|
130
|
+
```
|
131
|
+
|
132
|
+
Or use yum on Fedora or CentOS:
|
133
|
+
|
134
|
+
```bash
|
135
|
+
$ sudo yum install memcached
|
136
|
+
```
|
137
|
+
|
138
|
+
With the dependencies now installed, you can run the test suite with:
|
84
139
|
|
85
140
|
```bash
|
86
141
|
$ bundle exec rake test
|
@@ -93,20 +148,27 @@ $ cd actionpack
|
|
93
148
|
$ bundle exec rake test
|
94
149
|
```
|
95
150
|
|
96
|
-
If you want to run the tests located in a specific directory use the `TEST_DIR` environment variable. For example, this will run the tests
|
151
|
+
If you want to run the tests located in a specific directory use the `TEST_DIR` environment variable. For example, this will run the tests in the `railties/test/generators` directory only:
|
97
152
|
|
98
153
|
```bash
|
99
154
|
$ cd railties
|
100
155
|
$ TEST_DIR=generators bundle exec rake test
|
101
156
|
```
|
102
157
|
|
103
|
-
You can run
|
158
|
+
You can run the tests for a particular file by using:
|
104
159
|
|
105
160
|
```bash
|
106
161
|
$ cd actionpack
|
107
162
|
$ bundle exec ruby -Itest test/template/form_helper_test.rb
|
108
163
|
```
|
109
164
|
|
165
|
+
Or, you can run a single test in a particular file:
|
166
|
+
|
167
|
+
```bash
|
168
|
+
$ cd actionpack
|
169
|
+
$ bundle exec ruby -Itest path/to/test.rb -n test_name
|
170
|
+
```
|
171
|
+
|
110
172
|
### Active Record Setup
|
111
173
|
|
112
174
|
The test suite of Active Record attempts to run four times: once for SQLite3, once for each of the two MySQL gems (`mysql` and `mysql2`), and once for PostgreSQL. We are going to see now how to set up the environment for them.
|
@@ -133,14 +195,33 @@ $ sudo yum install mysql-server mysql-devel
|
|
133
195
|
$ sudo yum install postgresql-server postgresql-devel
|
134
196
|
```
|
135
197
|
|
136
|
-
|
198
|
+
If you are running Arch Linux, MySQL isn't supported anymore so you will need to
|
199
|
+
use MariaDB instead (see [this announcement](https://www.archlinux.org/news/mariadb-replaces-mysql-in-repositories/)):
|
200
|
+
|
201
|
+
```bash
|
202
|
+
$ sudo pacman -S mariadb libmariadbclient mariadb-clients
|
203
|
+
$ sudo pacman -S postgresql postgresql-libs
|
204
|
+
```
|
205
|
+
|
206
|
+
FreeBSD users will have to run the following:
|
207
|
+
|
208
|
+
```bash
|
209
|
+
# pkg_add -r mysql56-client mysql56-server
|
210
|
+
# pkg_add -r postgresql92-client postgresql92-server
|
211
|
+
```
|
212
|
+
|
213
|
+
Or install them through ports (they are located under the `databases` folder).
|
214
|
+
If you run into troubles during the installation of MySQL, please see
|
215
|
+
[the MySQL documentation](http://dev.mysql.com/doc/refman/5.1/en/freebsd-installation.html).
|
216
|
+
|
217
|
+
After that, run:
|
137
218
|
|
138
219
|
```bash
|
139
220
|
$ rm .bundle/config
|
140
221
|
$ bundle install
|
141
222
|
```
|
142
223
|
|
143
|
-
|
224
|
+
First, we need to delete `.bundle/config` because Bundler remembers in that file that we didn't want to install the "db" group (alternatively you can edit the file).
|
144
225
|
|
145
226
|
In order to be able to run the test suite against MySQL you need to create a user named `rails` with privileges on the test databases:
|
146
227
|
|
@@ -192,4 +273,4 @@ NOTE: Using the rake task to create the test databases ensures they have the cor
|
|
192
273
|
|
193
274
|
NOTE: You'll see the following warning (or localized warning) during activating HStore extension in PostgreSQL 9.1.x or earlier: "WARNING: => is deprecated as an operator".
|
194
275
|
|
195
|
-
If you
|
276
|
+
If you're using another database, check the file `activerecord/test/config.yml` or `activerecord/test/config.example.yml` for default connection information. You can edit `activerecord/test/config.yml` to provide different credentials on your machine if you must, but obviously you should not push any such changes back to Rails.
|
@@ -117,7 +117,7 @@
|
|
117
117
|
name: The Rails Initialization Process
|
118
118
|
work_in_progress: true
|
119
119
|
url: initialization.html
|
120
|
-
description: This guide explains the internals of the Rails initialization process as of Rails
|
120
|
+
description: This guide explains the internals of the Rails initialization process as of Rails 3.1
|
121
121
|
-
|
122
122
|
name: Extending Rails
|
123
123
|
documents:
|
@@ -162,7 +162,13 @@
|
|
162
162
|
-
|
163
163
|
name: Upgrading Ruby on Rails
|
164
164
|
url: upgrading_ruby_on_rails.html
|
165
|
+
work_in_progress: true
|
165
166
|
description: This guide helps in upgrading applications to latest Ruby on Rails versions.
|
167
|
+
-
|
168
|
+
name: Ruby on Rails 4.1 Release Notes
|
169
|
+
url: 4_1_release_notes.html
|
170
|
+
work_in_progress: true
|
171
|
+
description: Release notes for Rails 4.1.
|
166
172
|
-
|
167
173
|
name: Ruby on Rails 4.0 Release Notes
|
168
174
|
url: 4_0_release_notes.html
|
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,26 +18,59 @@ 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
76
|
$ rails plugin new blorgh --mountable
|
@@ -47,7 +82,8 @@ The full list of options for the plugin generator may be seen by typing:
|
|
47
82
|
$ 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 `rails g model`, such as `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
256
|
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,18 +272,25 @@ 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
296
|
$ rails generate scaffold post title:string text:text
|
@@ -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,38 +392,63 @@ 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
454
|
$ rails generate model Comment post_id:integer text:text
|
@@ -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
|
+
$ 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,7 +541,8 @@ 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
548
|
$ rails g controller comments
|
@@ -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
|
-
```
|
405
|
-
Missing partial blorgh/comments/comment with {:handlers=>[:erb, :builder],
|
406
|
-
|
407
|
-
|
593
|
+
```
|
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
|
+
$ 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
692
|
$ 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,7 +801,11 @@ 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
|
|
@@ -544,31 +813,41 @@ To generate this new column, run this command within the engine:
|
|
544
813
|
$ 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
825
|
$ 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
|
-
```
|
558
|
-
NOTE Migration [timestamp]_create_blorgh_posts.rb from blorgh has been
|
559
|
-
|
560
|
-
|
831
|
+
```
|
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
842
|
$ 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,147 @@ 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
|
-
`
|
655
|
-
|
962
|
+
Since we changed the `author_class` method to return a `String` instead of a
|
963
|
+
`Class`, 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
|
-
Testing an engine
|
689
|
-
-----------------
|
1017
|
+
Testing an engine -----------------
|
690
1018
|
|
691
|
-
When an engine is generated there is a smaller dummy application created inside
|
1019
|
+
When an engine is generated, there is a smaller dummy application created inside
|
1020
|
+
it at `test/dummy`. This application is used as a mounting point for the engine,
|
1021
|
+
to make testing the engine extremely simple. You may extend this application by
|
1022
|
+
generating controllers, models or views from within the directory, and then use
|
1023
|
+
those to test your engine.
|
692
1024
|
|
693
|
-
The `test` directory should be treated like a typical Rails testing environment,
|
1025
|
+
The `test` directory should be treated like a typical Rails testing environment,
|
1026
|
+
allowing for unit, functional and integration tests.
|
694
1027
|
|
695
|
-
### Functional
|
1028
|
+
### Functional Tests
|
696
1029
|
|
697
|
-
A matter worth taking into consideration when writing functional tests is that
|
1030
|
+
A matter worth taking into consideration when writing functional tests is that
|
1031
|
+
the tests are going to be running on an application - the `test/dummy`
|
1032
|
+
application - rather than your engine. This is due to the setup of the testing
|
1033
|
+
environment; an engine needs an application as a host for testing its main
|
1034
|
+
functionality, especially controllers. This means that if you were to make a
|
1035
|
+
typical `GET` to a controller in a controller's functional test like this:
|
698
1036
|
|
699
1037
|
```ruby
|
700
1038
|
get :index
|
701
1039
|
```
|
702
1040
|
|
703
|
-
It may not function correctly. This is because the application doesn't know how
|
1041
|
+
It may not function correctly. This is because the application doesn't know how
|
1042
|
+
to route these requests to the engine unless you explicitly tell it **how**. To
|
1043
|
+
do this, you must also pass the `:use_route` option as a parameter on these
|
1044
|
+
requests:
|
704
1045
|
|
705
1046
|
```ruby
|
706
1047
|
get :index, use_route: :blorgh
|
707
1048
|
```
|
708
1049
|
|
709
|
-
This tells the application that you still want to perform a `GET` request to the
|
1050
|
+
This tells the application that you still want to perform a `GET` request to the
|
1051
|
+
`index` action of this controller, but you want to use the engine's route to get
|
1052
|
+
there, rather than the application's one.
|
710
1053
|
|
711
1054
|
Improving engine functionality
|
712
1055
|
------------------------------
|
713
1056
|
|
714
|
-
This section explains how to add and/or override engine MVC functionality in the
|
1057
|
+
This section explains how to add and/or override engine MVC functionality in the
|
1058
|
+
main Rails application.
|
715
1059
|
|
716
1060
|
### Overriding Models and Controllers
|
717
1061
|
|
718
|
-
Engine model and controller classes can be extended by open classing them in the
|
1062
|
+
Engine model and controller classes can be extended by open classing them in the
|
1063
|
+
main Rails application (since model and controller classes are just Ruby classes
|
1064
|
+
that inherit Rails specific functionality). Open classing an Engine class
|
1065
|
+
redefines it for use in the main application. This is usually implemented by
|
1066
|
+
using the decorator pattern.
|
1067
|
+
|
1068
|
+
For simple class modifications, use `Class#class_eval`. For complex class
|
1069
|
+
modifications, consider using `ActiveSupport::Concern`.
|
1070
|
+
|
1071
|
+
#### A note on Decorators and Loading Code
|
1072
|
+
|
1073
|
+
Because these decorators are not referenced by your Rails application itself,
|
1074
|
+
Rails' autoloading system will not kick in and load your decorators. This means
|
1075
|
+
that you need to require them yourself.
|
719
1076
|
|
720
|
-
|
1077
|
+
Here is some sample code to do this:
|
1078
|
+
|
1079
|
+
```ruby
|
1080
|
+
# lib/blorgh/engine.rb
|
1081
|
+
module Blorgh
|
1082
|
+
class Engine < ::Rails::Engine
|
1083
|
+
isolate_namespace Blorgh
|
1084
|
+
|
1085
|
+
config.to_prepare do
|
1086
|
+
Dir.glob(Rails.root + "app/decorators/**/*_decorator*.rb").each do |c|
|
1087
|
+
require_dependency(c)
|
1088
|
+
end
|
1089
|
+
end
|
1090
|
+
end
|
1091
|
+
end
|
1092
|
+
```
|
1093
|
+
|
1094
|
+
This doesn't apply to just Decorators, but anything that you add in an engine
|
1095
|
+
that isn't referenced by your main application.
|
721
1096
|
|
722
1097
|
#### Implementing Decorator Pattern Using Class#class_eval
|
723
1098
|
|
724
|
-
**Adding** `Post#time_since_created
|
1099
|
+
**Adding** `Post#time_since_created`:
|
725
1100
|
|
726
1101
|
```ruby
|
727
1102
|
# MyApp/app/decorators/models/blorgh/post_decorator.rb
|
@@ -742,7 +1117,7 @@ end
|
|
742
1117
|
```
|
743
1118
|
|
744
1119
|
|
745
|
-
**Overriding** `Post#summary
|
1120
|
+
**Overriding** `Post#summary`:
|
746
1121
|
|
747
1122
|
```ruby
|
748
1123
|
# MyApp/app/decorators/models/blorgh/post_decorator.rb
|
@@ -767,9 +1142,13 @@ end
|
|
767
1142
|
|
768
1143
|
#### Implementing Decorator Pattern Using ActiveSupport::Concern
|
769
1144
|
|
770
|
-
Using `Class#class_eval` is great for simple adjustments, but for more complex
|
1145
|
+
Using `Class#class_eval` is great for simple adjustments, but for more complex
|
1146
|
+
class modifications, you might want to consider using [`ActiveSupport::Concern`]
|
1147
|
+
(http://edgeapi.rubyonrails.org/classes/ActiveSupport/Concern.html).
|
1148
|
+
ActiveSupport::Concern manages load order of interlinked dependent modules and
|
1149
|
+
classes at run time allowing you to significantly modularize your code.
|
771
1150
|
|
772
|
-
**Adding** `Post#time_since_created` and **Overriding** `Post#summary
|
1151
|
+
**Adding** `Post#time_since_created` and **Overriding** `Post#summary`:
|
773
1152
|
|
774
1153
|
```ruby
|
775
1154
|
# MyApp/app/models/blorgh/post.rb
|
@@ -802,7 +1181,7 @@ module Blorgh::Concerns::Models::Post
|
|
802
1181
|
extend ActiveSupport::Concern
|
803
1182
|
|
804
1183
|
# 'included do' causes the included code to be evaluated in the
|
805
|
-
# context where it is included (post.rb), rather than
|
1184
|
+
# context where it is included (post.rb), rather than being
|
806
1185
|
# executed in the module's context (blorgh/concerns/models/post).
|
807
1186
|
included do
|
808
1187
|
attr_accessor :author_name
|
@@ -811,7 +1190,6 @@ module Blorgh::Concerns::Models::Post
|
|
811
1190
|
before_save :set_author
|
812
1191
|
|
813
1192
|
private
|
814
|
-
|
815
1193
|
def set_author
|
816
1194
|
self.author = User.find_or_create_by(name: author_name)
|
817
1195
|
end
|
@@ -829,15 +1207,23 @@ module Blorgh::Concerns::Models::Post
|
|
829
1207
|
end
|
830
1208
|
```
|
831
1209
|
|
832
|
-
### Overriding
|
1210
|
+
### Overriding Views
|
833
1211
|
|
834
|
-
When Rails looks for a view to render, it will first look in the `app/views`
|
1212
|
+
When Rails looks for a view to render, it will first look in the `app/views`
|
1213
|
+
directory of the application. If it cannot find the view there, it will check in
|
1214
|
+
the `app/views` directories of all engines that have this directory.
|
835
1215
|
|
836
|
-
|
1216
|
+
When the application is asked to render the view for `Blorgh::PostsController`'s
|
1217
|
+
index action, it will first look for the path
|
1218
|
+
`app/views/blorgh/posts/index.html.erb` within the application. If it cannot
|
1219
|
+
find it, it will look inside the engine.
|
837
1220
|
|
838
|
-
You can override this view in the application by simply creating a new file at
|
1221
|
+
You can override this view in the application by simply creating a new file at
|
1222
|
+
`app/views/blorgh/posts/index.html.erb`. Then you can completely change what
|
1223
|
+
this view would normally output.
|
839
1224
|
|
840
|
-
Try this now by creating a new file at `app/views/blorgh/posts/index.html.erb`
|
1225
|
+
Try this now by creating a new file at `app/views/blorgh/posts/index.html.erb`
|
1226
|
+
and put this content in it:
|
841
1227
|
|
842
1228
|
```html+erb
|
843
1229
|
<h1>Posts</h1>
|
@@ -852,9 +1238,13 @@ Try this now by creating a new file at `app/views/blorgh/posts/index.html.erb` a
|
|
852
1238
|
|
853
1239
|
### Routes
|
854
1240
|
|
855
|
-
Routes inside an engine are
|
1241
|
+
Routes inside an engine are isolated from the application by default. This is
|
1242
|
+
done by the `isolate_namespace` call inside the `Engine` class. This essentially
|
1243
|
+
means that the application and its engines can have identically named routes and
|
1244
|
+
they will not clash.
|
856
1245
|
|
857
|
-
Routes inside an engine are drawn on the `Engine` class within
|
1246
|
+
Routes inside an engine are drawn on the `Engine` class within
|
1247
|
+
`config/routes.rb`, like this:
|
858
1248
|
|
859
1249
|
```ruby
|
860
1250
|
Blorgh::Engine.routes.draw do
|
@@ -862,43 +1252,71 @@ Blorgh::Engine.routes.draw do
|
|
862
1252
|
end
|
863
1253
|
```
|
864
1254
|
|
865
|
-
By having isolated routes such as this, if you wish to link to an area of an
|
1255
|
+
By having isolated routes such as this, if you wish to link to an area of an
|
1256
|
+
engine from within an application, you will need to use the engine's routing
|
1257
|
+
proxy method. Calls to normal routing methods such as `posts_path` may end up
|
1258
|
+
going to undesired locations if both the application and the engine have such a
|
1259
|
+
helper defined.
|
866
1260
|
|
867
|
-
For instance, the following example would go to the application's `posts_path`
|
1261
|
+
For instance, the following example would go to the application's `posts_path`
|
1262
|
+
if that template was rendered from the application, or the engine's `posts_path`
|
1263
|
+
if it was rendered from the engine:
|
868
1264
|
|
869
1265
|
```erb
|
870
1266
|
<%= link_to "Blog posts", posts_path %>
|
871
1267
|
```
|
872
1268
|
|
873
|
-
To make this route always use the engine's `posts_path` routing helper method,
|
1269
|
+
To make this route always use the engine's `posts_path` routing helper method,
|
1270
|
+
we must call the method on the routing proxy method that shares the same name as
|
1271
|
+
the engine.
|
874
1272
|
|
875
1273
|
```erb
|
876
1274
|
<%= link_to "Blog posts", blorgh.posts_path %>
|
877
1275
|
```
|
878
1276
|
|
879
|
-
If you wish to reference the application inside the engine in a similar way, use
|
1277
|
+
If you wish to reference the application inside the engine in a similar way, use
|
1278
|
+
the `main_app` helper:
|
880
1279
|
|
881
1280
|
```erb
|
882
1281
|
<%= link_to "Home", main_app.root_path %>
|
883
1282
|
```
|
884
1283
|
|
885
|
-
If you were to use this inside an engine, it would **always** go to the
|
1284
|
+
If you were to use this inside an engine, it would **always** go to the
|
1285
|
+
application's root. If you were to leave off the `main_app` "routing proxy"
|
1286
|
+
method call, it could potentially go to the engine's or application's root,
|
1287
|
+
depending on where it was called from.
|
886
1288
|
|
887
|
-
If a template
|
1289
|
+
If a template rendered from within an engine attempts to use one of the
|
1290
|
+
application's routing helper methods, it may result in an undefined method call.
|
1291
|
+
If you encounter such an issue, ensure that you're not attempting to call the
|
1292
|
+
application's routing methods without the `main_app` prefix from within the
|
1293
|
+
engine.
|
888
1294
|
|
889
1295
|
### Assets
|
890
1296
|
|
891
|
-
Assets within an engine work in an identical way to a full application. Because
|
1297
|
+
Assets within an engine work in an identical way to a full application. Because
|
1298
|
+
the engine class inherits from `Rails::Engine`, the application will know to
|
1299
|
+
look up assets in the engine's 'app/assets' and 'lib/assets' directories.
|
892
1300
|
|
893
|
-
|
1301
|
+
Like all of the other components of an engine, the assets should be namespaced.
|
1302
|
+
This means that if you have an asset called `style.css`, it should be placed at
|
1303
|
+
`app/assets/stylesheets/[engine name]/style.css`, rather than
|
1304
|
+
`app/assets/stylesheets/style.css`. If this asset isn't namespaced, there is a
|
1305
|
+
possibility that the host application could have an asset named identically, in
|
1306
|
+
which case the application's asset would take precedence and the engine's one
|
1307
|
+
would be ignored.
|
894
1308
|
|
895
|
-
Imagine that you did have an asset located at
|
1309
|
+
Imagine that you did have an asset located at
|
1310
|
+
`app/assets/stylesheets/blorgh/style.css` To include this asset inside an
|
1311
|
+
application, just use `stylesheet_link_tag` and reference the asset as if it
|
1312
|
+
were inside the engine:
|
896
1313
|
|
897
1314
|
```erb
|
898
1315
|
<%= stylesheet_link_tag "blorgh/style.css" %>
|
899
1316
|
```
|
900
1317
|
|
901
|
-
You can also specify these assets as dependencies of other assets using
|
1318
|
+
You can also specify these assets as dependencies of other assets using Asset
|
1319
|
+
Pipeline require statements in processed files:
|
902
1320
|
|
903
1321
|
```
|
904
1322
|
/*
|
@@ -906,16 +1324,21 @@ You can also specify these assets as dependencies of other assets using the Asse
|
|
906
1324
|
*/
|
907
1325
|
```
|
908
1326
|
|
909
|
-
INFO. Remember that in order to use languages like Sass or CoffeeScript, you
|
1327
|
+
INFO. Remember that in order to use languages like Sass or CoffeeScript, you
|
1328
|
+
should add the relevant library to your engine's `.gemspec`.
|
910
1329
|
|
911
1330
|
### Separate Assets & Precompiling
|
912
1331
|
|
913
|
-
There are some situations where your engine's assets are not required by the
|
914
|
-
|
915
|
-
|
916
|
-
|
1332
|
+
There are some situations where your engine's assets are not required by the
|
1333
|
+
host application. For example, say that you've created an admin functionality
|
1334
|
+
that only exists for your engine. In this case, the host application doesn't
|
1335
|
+
need to require `admin.css` or `admin.js`. Only the gem's admin layout needs
|
1336
|
+
these assets. It doesn't make sense for the host app to include
|
1337
|
+
`"blorgh/admin.css"` in its stylesheets. In this situation, you should
|
1338
|
+
explicitly define these assets for precompilation. This tells sprockets to add
|
1339
|
+
your engine assets when `rake assets:precompile` is triggered.
|
917
1340
|
|
918
|
-
You can define assets for precompilation in `engine.rb
|
1341
|
+
You can define assets for precompilation in `engine.rb`:
|
919
1342
|
|
920
1343
|
```ruby
|
921
1344
|
initializer "blorgh.assets.precompile" do |app|
|
@@ -923,15 +1346,15 @@ initializer "blorgh.assets.precompile" do |app|
|
|
923
1346
|
end
|
924
1347
|
```
|
925
1348
|
|
926
|
-
For more information, read the [Asset Pipeline guide](asset_pipeline.html)
|
1349
|
+
For more information, read the [Asset Pipeline guide](asset_pipeline.html).
|
927
1350
|
|
928
|
-
### Other
|
1351
|
+
### Other Gem Dependencies
|
929
1352
|
|
930
|
-
Gem dependencies inside an engine should be specified inside the
|
931
|
-
|
932
|
-
|
933
|
-
|
934
|
-
|
1353
|
+
Gem dependencies inside an engine should be specified inside the `.gemspec` file
|
1354
|
+
at the root of the engine. The reason is that the engine may be installed as a
|
1355
|
+
gem. If dependencies were to be specified inside the `Gemfile`, these would not
|
1356
|
+
be recognized by a traditional gem install and so they would not be installed,
|
1357
|
+
causing the engine to malfunction.
|
935
1358
|
|
936
1359
|
To specify a dependency that should be installed with the engine during a
|
937
1360
|
traditional `gem install`, specify it inside the `Gem::Specification` block
|
@@ -949,11 +1372,12 @@ s.add_development_dependency "moo"
|
|
949
1372
|
```
|
950
1373
|
|
951
1374
|
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.
|
1375
|
+
of the application. The development dependencies for the gem will only be used
|
1376
|
+
when the tests for the engine are running.
|
954
1377
|
|
955
1378
|
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
|
1379
|
+
required, you should require them before the engine's initialization. For
|
1380
|
+
example:
|
957
1381
|
|
958
1382
|
```ruby
|
959
1383
|
require 'other_engine/engine'
|