2015-01-14 01:22:13 -05:00
**DO NOT READ THIS FILE ON GITHUB, GUIDES ARE PUBLISHED ON http://guides.rubyonrails.org.**
2014-12-23 17:32:50 -05:00
2013-10-24 07:33:13 -04:00
Getting Started with Engines
2012-09-01 17:25:58 -04:00
============================
2011-10-04 17:37:38 -04:00
2013-12-14 15:28:08 -05:00
In this guide you will learn about engines and how they can be used to provide
additional functionality to their host applications through a clean and very
easy-to-use interface.
2012-11-29 17:25:02 -05:00
After reading this guide, you will know:
2011-10-04 17:37:38 -04:00
2012-11-29 08:14:08 -05:00
* What makes an engine.
* How to generate an engine.
* Building features for the engine.
* Hooking the engine into an application.
* Overriding engine functionality in the application.
2011-10-04 17:37:38 -04:00
2012-09-01 17:25:58 -04:00
--------------------------------------------------------------------------------
2011-10-04 17:37:38 -04:00
2012-09-01 17:25:58 -04:00
What are engines?
-----------------
2011-10-04 17:37:38 -04:00
2013-12-14 15:28:08 -05:00
Engines can be considered miniature applications that provide functionality to
their host applications. A Rails application is actually just a "supercharged"
engine, with the `Rails::Application` class inheriting a lot of its behavior
from `Rails::Engine` .
Therefore, engines and applications can be thought of almost the same thing,
just with subtle differences, as you'll see throughout this guide. Engines and
applications also share a common structure.
2015-06-11 08:23:33 -04:00
Engines are also closely related to plugins. The two share a common `lib`
2013-12-14 15:28:08 -05:00
directory structure, and are both generated using the `rails plugin new`
generator. The difference is that an engine is considered a "full plugin" by
Rails (as indicated by the `--full` option that's passed to the generator
2014-06-27 13:54:58 -04:00
command). We'll actually be using the `--mountable` option here, which includes
2014-11-23 03:15:47 -05:00
all the features of `--full` , and then some. This guide will refer to these
2014-06-27 13:54:58 -04:00
"full plugins" simply as "engines" throughout. An engine **can** be a plugin,
and a plugin **can** be an engine.
2013-12-14 15:28:08 -05:00
2014-06-27 13:54:58 -04:00
The engine that will be created in this guide will be called "blorgh". This
2013-12-14 15:28:08 -05:00
engine will provide blogging functionality to its host applications, allowing
2014-05-21 21:47:18 -04:00
for new articles and comments to be created. At the beginning of this guide, you
2013-12-14 15:28:08 -05:00
will be working solely within the engine itself, but in later sections you'll
see how to hook it into an application.
Engines can also be isolated from their host applications. This means that an
application is able to have a path provided by a routing helper such as
2014-05-21 21:47:18 -04:00
`articles_path` and use an engine also that provides a path also called
`articles_path` , and the two would not clash. Along with this, controllers, models
2013-12-14 15:28:08 -05:00
and table names are also namespaced. You'll see how to do this later in this
guide.
It's important to keep in mind at all times that the application should
**always** take precedence over its engines. An application is the object that
2014-06-27 13:54:58 -04:00
has final say in what goes on in its environment. The engine should
only be enhancing it, rather than changing it drastically.
2013-12-14 15:28:08 -05:00
To see demonstrations of other engines, check out
[Devise ](https://github.com/plataformatec/devise ), an engine that provides
authentication for its parent applications, or
[Forem ](https://github.com/radar/forem ), an engine that provides forum
functionality. There's also [Spree ](https://github.com/spree/spree ) which
provides an e-commerce platform, and
[RefineryCMS ](https://github.com/refinery/refinerycms ), a CMS engine.
Finally, engines would not have been possible without the work of James Adam,
Piotr Sarnacki, the Rails Core Team, and a number of other people. If you ever
meet them, don't forget to say thanks!
2011-10-06 03:20:38 -04:00
2012-09-01 17:25:58 -04:00
Generating an engine
--------------------
2011-10-04 17:37:38 -04:00
2013-12-14 15:28:08 -05:00
To generate an engine, you will need to run the plugin generator and pass it
options as appropriate to the need. For the "blorgh" example, you will need to
create a "mountable" engine, running this command in a terminal:
2011-10-05 17:40:36 -04:00
2012-09-01 20:45:26 -04:00
```bash
2014-09-28 10:12:46 -04:00
$ rails plugin new blorgh --mountable
2012-09-01 17:08:06 -04:00
```
2011-10-05 17:40:36 -04:00
2012-05-26 12:53:16 -04:00
The full list of options for the plugin generator may be seen by typing:
2012-09-01 20:45:26 -04:00
```bash
2014-09-28 10:12:46 -04:00
$ rails plugin --help
2012-09-01 17:08:06 -04:00
```
2012-05-26 12:53:16 -04:00
2014-02-19 10:37:31 -05:00
The `--mountable` option tells the generator that you want to create a
"mountable" and namespace-isolated engine. This generator will provide the same
skeleton structure as would the `--full` option. The `--full` option tells the
generator that you want to create an engine, including a skeleton structure
that provides the following:
2012-10-07 23:19:23 -04:00
* An `app` directory tree
* A `config/routes.rb` file:
```ruby
Rails.application.routes.draw do
end
```
2013-12-14 15:28:08 -05:00
* A file at `lib/blorgh/engine.rb` , which is identical in function to a
2014-06-19 18:58:29 -04:00
standard Rails application's `config/application.rb` file:
2013-02-11 09:19:09 -05:00
2012-10-07 23:19:23 -04:00
```ruby
module Blorgh
class Engine < ::Rails::Engine
end
end
```
2014-02-19 10:37:31 -05:00
The `--mountable` option will add to the `--full` option:
2012-10-07 23:19:23 -04:00
* Asset manifest files (`application.js` and `application.css` )
* A namespaced `ApplicationController` stub
* A namespaced `ApplicationHelper` stub
* A layout view template for the engine
* Namespace isolation to `config/routes.rb` :
2013-02-11 09:19:09 -05:00
2012-10-07 23:19:23 -04:00
```ruby
Blorgh::Engine.routes.draw do
end
```
2013-02-11 09:19:09 -05:00
2012-10-07 23:19:23 -04:00
* Namespace isolation to `lib/blorgh/engine.rb` :
```ruby
module Blorgh
class Engine < ::Rails::Engine
isolate_namespace Blorgh
end
end
```
2013-12-14 15:28:08 -05:00
Additionally, the `--mountable` option tells the generator to mount the engine
inside the dummy testing application located at `test/dummy` by adding the
following to the dummy application's routes file at
`test/dummy/config/routes.rb` :
2012-02-12 21:56:11 -05:00
2012-09-01 17:08:06 -04:00
```ruby
2014-08-10 09:10:15 -04:00
mount Blorgh::Engine => "/blorgh"
2012-09-01 17:08:06 -04:00
```
2011-10-05 17:40:36 -04:00
2013-12-14 15:27:45 -05:00
### Inside an Engine
2011-10-06 03:28:50 -04:00
2013-12-14 15:27:45 -05:00
#### Critical Files
2011-10-05 17:40:36 -04:00
2013-12-14 15:28:08 -05:00
At the root of this brand new engine's directory lives a `blorgh.gemspec` file.
When you include the engine into an application later on, you will do so with
this line in the Rails application's `Gemfile` :
2011-10-05 17:47:06 -04:00
2012-09-01 17:08:06 -04:00
```ruby
2015-09-23 14:29:17 -04:00
gem 'blorgh', path: 'engines/blorgh'
2012-09-01 17:08:06 -04:00
```
2011-10-05 17:47:06 -04:00
2013-12-14 15:28:08 -05:00
Don't forget to run `bundle install` as usual. By specifying it as a gem within
the `Gemfile` , Bundler will load it as such, parsing this `blorgh.gemspec` file
and requiring a file within the `lib` directory called `lib/blorgh.rb` . This
file requires the `blorgh/engine.rb` file (located at `lib/blorgh/engine.rb` )
and defines a base module called `Blorgh` .
2011-10-05 17:47:06 -04:00
2012-09-01 17:08:06 -04:00
```ruby
2011-10-05 17:47:06 -04:00
require "blorgh/engine"
2011-10-05 17:40:36 -04:00
2011-10-05 17:47:06 -04:00
module Blorgh
end
2012-09-01 17:08:06 -04:00
```
2011-10-04 17:37:38 -04:00
2013-12-14 15:28:08 -05:00
TIP: Some engines choose to use this file to put global configuration options
for their engine. It's a relatively good idea, so if you want to offer
configuration options, the file where your engine's `module` is defined is
perfect for that. Place the methods inside the module and you'll be good to go.
2012-02-13 17:59:39 -05:00
2012-09-01 21:37:59 -04:00
Within `lib/blorgh/engine.rb` is the base class for the engine:
2011-10-05 17:47:06 -04:00
2012-09-01 17:08:06 -04:00
```ruby
2011-10-06 03:28:50 -04:00
module Blorgh
2014-08-10 09:10:15 -04:00
class Engine < ::Rails::Engine
2011-10-06 03:28:50 -04:00
isolate_namespace Blorgh
end
end
2012-09-01 17:08:06 -04:00
```
2011-10-05 17:47:06 -04:00
2013-12-14 15:28:08 -05:00
By inheriting from the `Rails::Engine` class, this gem notifies Rails that
there's an engine at the specified path, and will correctly mount the engine
inside the application, performing tasks such as adding the `app` directory of
the engine to the load path for models, mailers, controllers and views.
The `isolate_namespace` method here deserves special notice. This call is
responsible for isolating the controllers, models, routes and other things into
their own namespace, away from similar components inside the application.
Without this, there is a possibility that the engine's components could "leak"
into the application, causing unwanted disruption, or that important engine
components could be overridden by similarly named things within the application.
One of the examples of such conflicts is helpers. Without calling
`isolate_namespace` , the engine's helpers would be included in an application's
controllers.
NOTE: It is **highly** recommended that the `isolate_namespace` line be left
within the `Engine` class definition. Without it, classes generated in an engine
**may** conflict with an application.
What this isolation of the namespace means is that a model generated by a call
2014-05-21 21:47:18 -04:00
to `bin/rails g model` , such as `bin/rails g model article` , won't be called `Article` , but
instead be namespaced and called `Blorgh::Article` . In addition, the table for the
model is namespaced, becoming `blorgh_articles` , rather than simply `articles` .
Similar to the model namespacing, a controller called `ArticlesController` becomes
`Blorgh::ArticlesController` and the views for that controller will not be at
`app/views/articles` , but `app/views/blorgh/articles` instead. Mailers are namespaced
2013-12-14 15:28:08 -05:00
as well.
Finally, routes will also be isolated within the engine. This is one of the most
important parts about namespacing, and is discussed later in the
[Routes ](#routes ) section of this guide.
2011-10-06 03:28:50 -04:00
2013-12-14 15:27:45 -05:00
#### `app` Directory
2011-10-06 03:28:50 -04:00
2013-12-14 15:28:08 -05:00
Inside the `app` directory are the standard `assets` , `controllers` , `helpers` ,
`mailers` , `models` and `views` directories that you should be familiar with
from an application. The `helpers` , `mailers` and `models` directories are
empty, so they aren't described in this section. We'll look more into models in
a future section, when we're writing the engine.
Within the `app/assets` directory, there are the `images` , `javascripts` and
`stylesheets` directories which, again, you should be familiar with due to their
similarity to an application. One difference here, however, is that each
directory contains a sub-directory with the engine name. Because this engine is
going to be namespaced, its assets should be too.
Within the `app/controllers` directory there is a `blorgh` directory that
contains a file called `application_controller.rb` . This file will provide any
common functionality for the controllers of the engine. The `blorgh` directory
is where the other controllers for the engine will go. By placing them within
this namespaced directory, you prevent them from possibly clashing with
identically-named controllers within other engines or even within the
application.
NOTE: The `ApplicationController` class inside an engine is named just like a
Rails application in order to make it easier for you to convert your
applications into engines.
2015-11-01 14:52:21 -05:00
NOTE: Because of the way that Ruby does constant lookup you may run into a situation
where your engine controller is inheriting from the main application controller and
2015-11-13 03:18:04 -05:00
not your engine's application controller. Ruby is able to resolve the `ApplicationController` constant, and therefore the autoloading mechanism is not triggered. See the section [When Constants Aren't Missed ](autoloading_and_reloading_constants.html#when-constants-aren-t-missed ) of the [Autoloading and Reloading Constants ](autoloading_and_reloading_constants.html ) guide for further details. The best way to prevent this from
2015-11-01 14:52:21 -05:00
happening is to use `require_dependency` to ensure that the engine's application
controller is loaded. For example:
``` ruby
# app/controllers/blorgh/articles_controller.rb:
require_dependency "blorgh/application_controller"
2015-10-29 03:54:45 -04:00
module Blorgh
class ArticlesController < ApplicationController
...
end
end
```
2015-11-01 14:52:21 -05:00
WARNING: Don't use `require` because it will break the automatic reloading of classes
in the development environment - using `require_dependency` ensures that classes are
loaded and unloaded in the correct manner.
2013-12-14 15:28:08 -05:00
Lastly, the `app/views` directory contains a `layouts` folder, which contains a
file at `blorgh/application.html.erb` . This file allows you to specify a layout
for the engine. If this engine is to be used as a stand-alone engine, then you
would add any customization to its layout in this file, rather than the
application's `app/views/layouts/application.html.erb` file.
If you don't want to force a layout on to users of the engine, then you can
delete this file and reference a different layout in the controllers of your
engine.
2012-02-13 17:59:39 -05:00
2013-12-14 15:27:45 -05:00
#### `bin` Directory
2011-10-06 03:28:50 -04:00
2013-12-14 15:28:08 -05:00
This directory contains one file, `bin/rails` , which enables you to use the
`rails` sub-commands and generators just like you would within an application.
This means that you will be able to generate new controllers and models for this
engine very easily by running commands like this:
2012-02-13 17:59:39 -05:00
2012-09-01 20:45:26 -04:00
```bash
2014-05-20 07:29:18 -04:00
$ bin/rails g model
2012-09-01 17:08:06 -04:00
```
2012-02-13 17:59:39 -05:00
2013-12-14 15:28:08 -05:00
Keep in mind, of course, that anything generated with these commands inside of
an engine that has `isolate_namespace` in the `Engine` class will be namespaced.
2011-10-06 03:28:50 -04:00
2013-12-14 15:27:45 -05:00
#### `test` Directory
2011-10-06 03:28:50 -04:00
2013-12-14 15:28:08 -05:00
The `test` directory is where tests for the engine will go. To test the engine,
there is a cut-down version of a Rails application embedded within it at
`test/dummy` . This application will mount the engine in the
`test/dummy/config/routes.rb` file:
2011-10-06 03:28:50 -04:00
2012-09-01 17:08:06 -04:00
```ruby
2011-10-06 03:28:50 -04:00
Rails.application.routes.draw do
mount Blorgh::Engine => "/blorgh"
end
2012-09-01 17:08:06 -04:00
```
2011-10-06 03:28:50 -04:00
2013-12-14 15:28:08 -05:00
This line mounts the engine at the path `/blorgh` , which will make it accessible
through the application only at that path.
2011-10-06 03:28:50 -04:00
2013-12-14 15:28:08 -05:00
Inside the test directory there is the `test/integration` directory, where
integration tests for the engine should be placed. Other directories can be
created in the `test` directory as well. For example, you may wish to create a
`test/models` directory for your model tests.
2011-10-06 03:28:50 -04:00
2012-09-01 17:25:58 -04:00
Providing engine functionality
------------------------------
2011-10-04 17:37:38 -04:00
2014-05-21 21:47:18 -04:00
The engine that this guide covers provides submitting articles and commenting
functionality and follows a similar thread to the [Getting Started
2013-12-14 15:28:08 -05:00
Guide](getting_started.html), with some new twists.
2011-10-06 18:06:43 -04:00
2014-05-21 21:47:18 -04:00
### Generating an Article Resource
2011-10-06 18:06:43 -04:00
2014-05-21 21:47:18 -04:00
The first thing to generate for a blog engine is the `Article` model and related
2013-12-14 15:28:08 -05:00
controller. To quickly generate this, you can use the Rails scaffold generator.
2011-10-06 18:06:43 -04:00
2012-09-01 20:45:26 -04:00
```bash
2014-05-21 21:47:18 -04:00
$ bin/rails generate scaffold article title:string text:text
2012-09-01 17:08:06 -04:00
```
2011-10-13 16:51:07 -04:00
This command will output this information:
2012-09-03 21:21:24 -04:00
```
2011-10-06 18:06:43 -04:00
invoke active_record
2014-05-21 21:47:18 -04:00
create db/migrate/[timestamp]_create_blorgh_articles.rb
create app/models/blorgh/article.rb
2011-10-06 18:06:43 -04:00
invoke test_unit
2014-05-21 21:47:18 -04:00
create test/models/blorgh/article_test.rb
create test/fixtures/blorgh/articles.yml
2013-12-14 15:27:45 -05:00
invoke resource_route
2014-05-21 21:47:18 -04:00
route resources :articles
2011-10-06 18:06:43 -04:00
invoke scaffold_controller
2014-05-21 21:47:18 -04:00
create app/controllers/blorgh/articles_controller.rb
2011-10-06 18:06:43 -04:00
invoke erb
2014-05-21 21:47:18 -04:00
create app/views/blorgh/articles
create app/views/blorgh/articles/index.html.erb
create app/views/blorgh/articles/edit.html.erb
create app/views/blorgh/articles/show.html.erb
create app/views/blorgh/articles/new.html.erb
create app/views/blorgh/articles/_form.html.erb
2011-10-06 18:06:43 -04:00
invoke test_unit
2014-05-21 21:47:18 -04:00
create test/controllers/blorgh/articles_controller_test.rb
2011-10-06 18:06:43 -04:00
invoke helper
2014-05-21 21:47:18 -04:00
create app/helpers/blorgh/articles_helper.rb
2011-10-06 18:06:43 -04:00
invoke assets
invoke js
2014-05-21 21:47:18 -04:00
create app/assets/javascripts/blorgh/articles.js
2011-10-06 18:06:43 -04:00
invoke css
2014-05-21 21:47:18 -04:00
create app/assets/stylesheets/blorgh/articles.css
2011-10-06 18:06:43 -04:00
invoke css
create app/assets/stylesheets/scaffold.css
2012-09-01 17:08:06 -04:00
```
2011-10-06 18:06:43 -04:00
2013-12-14 15:28:08 -05:00
The first thing that the scaffold generator does is invoke the `active_record`
generator, which generates a migration and a model for the resource. Note here,
2014-05-21 21:47:18 -04:00
however, that the migration is called `create_blorgh_articles` rather than the
usual `create_articles` . This is due to the `isolate_namespace` method called in
2013-12-14 15:28:08 -05:00
the `Blorgh::Engine` class's definition. The model here is also namespaced,
2014-05-21 21:47:18 -04:00
being placed at `app/models/blorgh/article.rb` rather than `app/models/article.rb` due
2013-12-14 15:28:08 -05:00
to the `isolate_namespace` call within the `Engine` class.
2011-10-06 18:06:43 -04:00
2013-12-14 15:28:08 -05:00
Next, the `test_unit` generator is invoked for this model, generating a model
2014-05-21 21:47:18 -04:00
test at `test/models/blorgh/article_test.rb` (rather than
`test/models/article_test.rb` ) and a fixture at `test/fixtures/blorgh/articles.yml`
(rather than `test/fixtures/articles.yml` ).
2011-10-06 18:06:43 -04:00
2013-12-14 15:28:08 -05:00
After that, a line for the resource is inserted into the `config/routes.rb` file
2014-05-21 21:47:18 -04:00
for the engine. This line is simply `resources :articles` , turning the
2013-12-14 15:28:08 -05:00
`config/routes.rb` file for the engine into this:
2011-10-06 18:06:43 -04:00
2012-09-01 17:08:06 -04:00
```ruby
2011-10-06 18:06:43 -04:00
Blorgh::Engine.routes.draw do
2014-05-21 21:47:18 -04:00
resources :articles
2011-10-06 18:06:43 -04:00
end
2012-09-01 17:08:06 -04:00
```
2011-10-06 18:06:43 -04:00
2013-12-14 15:28:08 -05:00
Note here that the routes are drawn upon the `Blorgh::Engine` object rather than
the `YourApp::Application` class. This is so that the engine routes are confined
to the engine itself and can be mounted at a specific point as shown in the
[test directory ](#test-directory ) section. It also causes the engine's routes to
be isolated from those routes that are within the application. The
[Routes ](#routes ) section of this guide describes it in detail.
2011-10-06 18:06:43 -04:00
2013-12-14 15:28:08 -05:00
Next, the `scaffold_controller` generator is invoked, generating a controller
2014-05-21 21:47:18 -04:00
called `Blorgh::ArticlesController` (at
`app/controllers/blorgh/articles_controller.rb` ) and its related views at
`app/views/blorgh/articles` . This generator also generates a test for the
controller (`test/controllers/blorgh/articles_controller_test.rb`) and a helper
2015-04-10 15:35:46 -04:00
(`app/helpers/blorgh/articles_helper.rb`).
2011-10-09 17:42:27 -04:00
2013-12-14 15:28:08 -05:00
Everything this generator has created is neatly namespaced. The controller's
class is defined within the `Blorgh` module:
2011-10-09 17:42:27 -04:00
2012-09-01 17:08:06 -04:00
```ruby
2011-10-09 17:42:27 -04:00
module Blorgh
2014-05-21 21:47:18 -04:00
class ArticlesController < ApplicationController
2011-10-09 17:42:27 -04:00
...
end
end
2012-09-01 17:08:06 -04:00
```
2011-10-09 17:42:27 -04:00
2016-02-18 02:08:07 -05:00
NOTE: The `ArticlesController` class inherits from
2016-02-18 10:18:19 -05:00
`Blorgh::ApplicationController` , not the application's `ApplicationController` .
2011-10-09 17:42:27 -04:00
2014-05-21 21:47:18 -04:00
The helper inside `app/helpers/blorgh/articles_helper.rb` is also namespaced:
2011-10-09 17:42:27 -04:00
2012-09-01 17:08:06 -04:00
```ruby
2011-10-09 17:42:27 -04:00
module Blorgh
2014-05-21 21:47:18 -04:00
module ArticlesHelper
2011-10-09 17:42:27 -04:00
...
end
end
2012-09-01 17:08:06 -04:00
```
2011-10-09 17:42:27 -04:00
2013-12-14 15:28:08 -05:00
This helps prevent conflicts with any other engine or application that may have
2014-06-27 10:50:41 -04:00
an article resource as well.
2011-10-09 17:42:27 -04:00
2013-12-14 15:28:08 -05:00
Finally, the assets for this resource are generated in two files:
2014-05-21 21:47:18 -04:00
`app/assets/javascripts/blorgh/articles.js` and
`app/assets/stylesheets/blorgh/articles.css` . You'll see how to use these a little
2013-12-14 15:28:08 -05:00
later.
2011-10-09 17:42:27 -04:00
2016-01-19 06:08:56 -05:00
You can see what the engine has so far by running `bin/rails db:migrate` at the root
2013-12-14 15:28:08 -05:00
of our engine to run the migration generated by the scaffold generator, and then
running `rails server` in `test/dummy` . When you open
2014-05-21 21:47:18 -04:00
`http://localhost:3000/blorgh/articles` you will see the default scaffold that has
2013-12-14 15:28:08 -05:00
been generated. Click around! You've just generated your first engine's first
functions.
2011-10-09 17:42:27 -04:00
2013-12-14 15:28:08 -05:00
If you'd rather play around in the console, `rails console` will also work just
2014-05-21 21:47:18 -04:00
like a Rails application. Remember: the `Article` model is namespaced, so to
reference it you must call it as `Blorgh::Article` .
2011-10-10 06:03:55 -04:00
2012-09-01 17:08:06 -04:00
```ruby
2014-05-21 21:47:18 -04:00
>> Blorgh::Article.find(1)
=> #< Blorgh::Article id: 1 . . . >
2012-09-01 17:08:06 -04:00
```
2011-10-10 06:03:55 -04:00
2014-05-21 21:47:18 -04:00
One final thing is that the `articles` resource for this engine should be the root
2013-12-14 15:28:08 -05:00
of the engine. Whenever someone goes to the root path where the engine is
2014-05-21 21:47:18 -04:00
mounted, they should be shown a list of articles. This can be made to happen if
2013-12-14 15:28:08 -05:00
this line is inserted into the `config/routes.rb` file inside the engine:
2011-10-14 03:43:49 -04:00
2012-09-01 17:08:06 -04:00
```ruby
2014-05-21 21:47:18 -04:00
root to: "articles#index"
2012-09-01 17:08:06 -04:00
```
2011-10-14 03:43:49 -04:00
2014-05-21 21:47:18 -04:00
Now people will only need to go to the root of the engine to see all the articles,
rather than visiting `/articles` . This means that instead of
`http://localhost:3000/blorgh/articles` , you only need to go to
2013-12-14 15:28:08 -05:00
`http://localhost:3000/blorgh` now.
2011-10-14 03:43:49 -04:00
2013-12-14 15:27:45 -05:00
### Generating a Comments Resource
2011-10-09 17:42:27 -04:00
2014-05-21 21:47:18 -04:00
Now that the engine can create new articles, it only makes sense to add
2013-12-14 15:28:08 -05:00
commenting functionality as well. To do this, you'll need to generate a comment
2014-05-21 21:47:18 -04:00
model, a comment controller and then modify the articles scaffold to display
2013-12-14 15:28:08 -05:00
comments and allow people to create new ones.
2011-10-10 06:03:55 -04:00
2013-12-14 15:28:08 -05:00
From the application root, run the model generator. Tell it to generate a
2016-02-13 06:36:16 -05:00
`Comment` model, with the related table having two columns: an `article_id` integer
2013-12-14 15:28:08 -05:00
and `text` text column.
2011-10-10 06:03:55 -04:00
2012-09-01 20:45:26 -04:00
```bash
2014-05-21 21:47:18 -04:00
$ bin/rails generate model Comment article_id:integer text:text
2012-09-01 17:08:06 -04:00
```
2011-10-10 06:03:55 -04:00
2011-10-13 16:51:45 -04:00
This will output the following:
2011-10-10 06:03:55 -04:00
2012-09-03 21:21:24 -04:00
```
2011-10-13 16:51:45 -04:00
invoke active_record
create db/migrate/[timestamp]_create_blorgh_comments.rb
create app/models/blorgh/comment.rb
invoke test_unit
2012-10-08 00:59:42 -04:00
create test/models/blorgh/comment_test.rb
2011-10-13 16:51:45 -04:00
create test/fixtures/blorgh/comments.yml
2012-09-01 17:08:06 -04:00
```
2011-10-10 06:03:55 -04:00
2013-12-14 15:28:08 -05:00
This generator call will generate just the necessary model files it needs,
namespacing the files under a `blorgh` directory and creating a model class
called `Blorgh::Comment` . Now run the migration to create our blorgh_comments
table:
2013-09-22 07:00:38 -04:00
```bash
2016-01-19 06:08:56 -05:00
$ bin/rails db:migrate
2013-09-22 07:00:38 -04:00
```
2011-10-10 06:03:55 -04:00
2014-05-21 21:47:18 -04:00
To show the comments on an article, edit `app/views/blorgh/articles/show.html.erb` and
2013-12-14 15:28:08 -05:00
add this line before the "Edit" link:
2011-10-10 06:03:55 -04:00
2012-09-03 21:21:24 -04:00
```html+erb
2011-10-14 03:05:45 -04:00
< h3 > Comments< / h3 >
2014-05-21 21:47:18 -04:00
< %= render @article .comments %>
2012-09-01 17:08:06 -04:00
```
2011-10-10 06:03:55 -04:00
2013-12-14 15:28:08 -05:00
This line will require there to be a `has_many` association for comments defined
2014-05-21 21:47:18 -04:00
on the `Blorgh::Article` model, which there isn't right now. To define one, open
`app/models/blorgh/article.rb` and add this line into the model:
2011-10-10 06:03:55 -04:00
2012-09-01 17:08:06 -04:00
```ruby
2011-10-13 03:04:55 -04:00
has_many :comments
2012-09-01 17:08:06 -04:00
```
2011-10-10 06:03:55 -04:00
2011-10-13 03:04:55 -04:00
Turning the model into this:
2011-10-10 06:03:55 -04:00
2012-09-01 17:08:06 -04:00
```ruby
2011-10-10 06:03:55 -04:00
module Blorgh
2015-12-12 08:25:00 -05:00
class Article < ApplicationRecord
2011-10-13 03:04:55 -04:00
has_many :comments
2011-10-10 06:03:55 -04:00
end
end
2012-09-01 17:08:06 -04:00
```
2011-10-10 06:03:55 -04:00
2013-12-14 15:28:08 -05:00
NOTE: Because the `has_many` is defined inside a class that is inside the
`Blorgh` module, Rails will know that you want to use the `Blorgh::Comment`
model for these objects, so there's no need to specify that using the
`:class_name` option here.
2011-10-10 06:03:55 -04:00
2014-06-27 10:50:41 -04:00
Next, there needs to be a form so that comments can be created on an article. To
add this, put this line underneath the call to `render @article.comments` in
2014-05-21 21:47:18 -04:00
`app/views/blorgh/articles/show.html.erb` :
2011-10-10 06:03:55 -04:00
2012-09-01 17:08:06 -04:00
```erb
2011-10-13 16:45:00 -04:00
< %= render "blorgh/comments/form" %>
2012-09-01 17:08:06 -04:00
```
2011-10-10 06:03:55 -04:00
2013-12-14 15:28:08 -05:00
Next, the partial that this line will render needs to exist. Create a new
directory at `app/views/blorgh/comments` and in it a new file called
`_form.html.erb` which has this content to create the required partial:
2011-10-10 06:03:55 -04:00
2012-09-03 21:21:24 -04:00
```html+erb
2011-10-14 03:06:23 -04:00
< h3 > New comment< / h3 >
2014-05-21 21:47:18 -04:00
< %= form_for [@article, @article .comments.build] do |f| %>
2011-10-13 03:04:55 -04:00
< p >
2013-07-05 23:04:25 -04:00
< %= f.label :text %>< br >
2011-10-13 03:04:55 -04:00
< %= f.text_area :text %>
< / p >
2011-10-14 03:06:23 -04:00
< %= f.submit %>
2011-10-13 03:04:55 -04:00
< % end %>
2012-09-01 17:08:06 -04:00
```
2011-10-04 17:37:38 -04:00
2013-12-14 15:28:08 -05:00
When this form is submitted, it is going to attempt to perform a `POST` request
2014-05-21 21:47:18 -04:00
to a route of `/articles/:article_id/comments` within the engine. This route doesn't
exist at the moment, but can be created by changing the `resources :articles` line
2013-12-14 15:28:08 -05:00
inside `config/routes.rb` into these lines:
2011-10-10 06:03:55 -04:00
2012-09-01 17:08:06 -04:00
```ruby
2014-05-21 21:47:18 -04:00
resources :articles do
2011-10-14 03:06:23 -04:00
resources :comments
end
2012-09-01 17:08:06 -04:00
```
2011-10-10 06:03:55 -04:00
2012-03-13 19:25:06 -04:00
This creates a nested route for the comments, which is what the form requires.
2012-02-13 17:59:39 -05:00
2013-12-14 15:28:08 -05:00
The route now exists, but the controller that this route goes to does not. To
create it, run this command from the application root:
2011-10-14 03:06:23 -04:00
2012-09-01 20:45:26 -04:00
```bash
2014-05-20 07:29:18 -04:00
$ bin/rails g controller comments
2012-09-01 17:08:06 -04:00
```
2011-10-14 03:06:23 -04:00
This will generate the following things:
2012-09-03 21:21:24 -04:00
```
2011-10-14 03:06:23 -04:00
create app/controllers/blorgh/comments_controller.rb
invoke erb
exist app/views/blorgh/comments
invoke test_unit
2012-10-08 00:59:42 -04:00
create test/controllers/blorgh/comments_controller_test.rb
2011-10-14 03:06:23 -04:00
invoke helper
create app/helpers/blorgh/comments_helper.rb
invoke assets
invoke js
create app/assets/javascripts/blorgh/comments.js
invoke css
create app/assets/stylesheets/blorgh/comments.css
2012-09-01 17:08:06 -04:00
```
2011-10-14 03:06:23 -04:00
2014-05-21 21:47:18 -04:00
The form will be making a `POST` request to `/articles/:article_id/comments` , which
2013-12-14 15:28:08 -05:00
will correspond with the `create` action in `Blorgh::CommentsController` . This
action needs to be created, which can be done by putting the following lines
inside the class definition in `app/controllers/blorgh/comments_controller.rb` :
2011-10-10 06:03:55 -04:00
2012-09-01 17:08:06 -04:00
```ruby
2011-10-14 03:06:23 -04:00
def create
2014-05-21 21:47:18 -04:00
@article = Article.find(params[:article_id])
@comment = @article .comments.create(comment_params)
2011-10-14 03:06:23 -04:00
flash[:notice] = "Comment has been created!"
2014-05-21 21:47:18 -04:00
redirect_to articles_path
2011-10-14 03:06:23 -04:00
end
2013-07-19 03:54:24 -04:00
private
2013-09-06 14:41:04 -04:00
def comment_params
params.require(:comment).permit(:text)
end
2012-09-01 17:08:06 -04:00
```
2011-10-10 06:03:55 -04:00
2013-12-14 15:28:08 -05:00
This is the final step required to get the new comment form working. Displaying
the comments, however, is not quite right yet. If you were to create a comment
right now, you would see this error:
2011-10-14 03:06:23 -04:00
2014-05-21 21:47:18 -04:00
```
2015-05-09 12:21:06 -04:00
Missing partial blorgh/comments/_comment with {:handlers=>[:erb, :builder],
2013-12-14 15:28:08 -05:00
:formats=>[:html], :locale=>[:en, :en]}. Searched in: *
"/Users/ryan/Sites/side_projects/blorgh/test/dummy/app/views" *
2014-05-21 21:47:18 -04:00
"/Users/ryan/Sites/side_projects/blorgh/app/views"
2012-09-01 17:08:06 -04:00
```
2011-10-14 03:06:23 -04:00
2013-12-14 15:28:08 -05:00
The engine is unable to find the partial required for rendering the comments.
Rails looks first in the application's (`test/dummy`) `app/views` directory and
then in the engine's `app/views` directory. When it can't find it, it will throw
2015-05-09 12:21:06 -04:00
this error. The engine knows to look for `blorgh/comments/_comment` because the
2013-12-14 15:28:08 -05:00
model object it is receiving is from the `Blorgh::Comment` class.
2011-10-14 03:06:23 -04:00
2013-12-14 15:28:08 -05:00
This partial will be responsible for rendering just the comment text, for now.
Create a new file at `app/views/blorgh/comments/_comment.html.erb` and put this
line inside it:
2011-10-14 03:06:23 -04:00
2012-09-01 17:08:06 -04:00
```erb
2011-10-14 03:06:23 -04:00
< %= comment_counter + 1 %>. < %= comment.text %>
2012-09-01 17:08:06 -04:00
```
2011-10-14 03:06:23 -04:00
2013-12-14 15:28:08 -05:00
The `comment_counter` local variable is given to us by the `< %= render
2014-05-21 21:47:18 -04:00
@article .comments %>` call, which will define it automatically and increment the
2013-12-14 15:28:08 -05:00
counter as it iterates through each comment. It's used in this example to
display a small number next to each comment when it's created.
2011-10-14 03:06:23 -04:00
2013-12-14 15:28:08 -05:00
That completes the comment function of the blogging engine. Now it's time to use
it within an application.
2011-10-06 18:56:50 -04:00
2013-12-14 15:27:45 -05:00
Hooking Into an Application
2012-09-01 17:25:58 -04:00
---------------------------
2011-10-04 17:37:38 -04:00
2013-12-14 15:28:08 -05:00
Using an engine within an application is very easy. This section covers how to
mount the engine into an application and the initial setup required, as well as
linking the engine to a `User` class provided by the application to provide
2014-05-21 21:47:18 -04:00
ownership for articles and comments within the engine.
2011-10-14 18:51:30 -04:00
2013-12-14 15:27:45 -05:00
### Mounting the Engine
2011-10-14 18:51:30 -04:00
2013-12-14 15:28:08 -05:00
First, the engine needs to be specified inside the application's `Gemfile` . If
there isn't an application handy to test this out in, generate one using the
`rails new` command outside of the engine directory like this:
2011-10-10 06:03:55 -04:00
2012-09-01 20:45:26 -04:00
```bash
2011-10-14 03:43:49 -04:00
$ rails new unicorn
2012-09-01 17:08:06 -04:00
```
2011-10-14 03:43:49 -04:00
2013-12-14 15:28:08 -05:00
Usually, specifying the engine inside the Gemfile would be done by specifying it
as a normal, everyday gem.
2011-10-10 06:03:55 -04:00
2012-09-01 17:08:06 -04:00
```ruby
2011-10-14 03:43:49 -04:00
gem 'devise'
2012-09-01 17:08:06 -04:00
```
2011-10-10 06:03:55 -04:00
2013-12-14 15:28:08 -05:00
However, because you are developing the `blorgh` engine on your local machine,
you will need to specify the `:path` option in your `Gemfile` :
2011-10-10 06:03:55 -04:00
2012-09-01 17:08:06 -04:00
```ruby
2015-09-23 14:29:17 -04:00
gem 'blorgh', path: 'engines/blorgh'
2012-09-01 17:08:06 -04:00
```
2011-10-10 06:03:55 -04:00
2013-12-14 15:27:45 -05:00
Then run `bundle` to install the gem.
2013-12-14 15:28:08 -05:00
As described earlier, by placing the gem in the `Gemfile` it will be loaded when
Rails is loaded. It will first require `lib/blorgh.rb` from the engine, then
`lib/blorgh/engine.rb` , which is the file that defines the major pieces of
functionality for the engine.
2011-10-14 03:43:49 -04:00
2013-12-14 15:28:08 -05:00
To make the engine's functionality accessible from within an application, it
needs to be mounted in that application's `config/routes.rb` file:
2011-10-14 03:43:49 -04:00
2012-09-01 17:08:06 -04:00
```ruby
2012-11-15 16:56:55 -05:00
mount Blorgh::Engine, at: "/blog"
2012-09-01 17:08:06 -04:00
```
2011-10-10 06:03:55 -04:00
2013-12-14 15:28:08 -05:00
This line will mount the engine at `/blog` in the application. Making it
accessible at `http://localhost:3000/blog` when the application runs with `rails
server`.
2011-10-14 18:52:07 -04:00
2013-12-14 15:28:08 -05:00
NOTE: Other engines, such as Devise, handle this a little differently by making
you specify custom helpers (such as `devise_for` ) in the routes. These helpers
do exactly the same thing, mounting pieces of the engines's functionality at a
pre-defined path which may be customizable.
2011-10-04 17:37:38 -04:00
2012-09-01 17:25:58 -04:00
### Engine setup
2011-10-14 18:52:07 -04:00
2014-05-21 21:47:18 -04:00
The engine contains migrations for the `blorgh_articles` and `blorgh_comments`
2013-12-14 15:28:08 -05:00
table which need to be created in the application's database so that the
engine's models can query them correctly. To copy these migrations into the
2015-10-19 19:33:02 -04:00
application run the following command from the `test/dummy` directory of your Rails engine:
2011-10-14 18:52:07 -04:00
2012-09-01 20:45:26 -04:00
```bash
2016-01-19 06:08:56 -05:00
$ bin/rails blorgh:install:migrations
2012-09-01 17:08:06 -04:00
```
2011-10-14 18:52:07 -04:00
2013-12-14 15:28:08 -05:00
If you have multiple engines that need migrations copied over, use
`railties:install:migrations` instead:
2012-02-12 21:56:11 -05:00
2012-09-01 20:45:26 -04:00
```bash
2016-01-19 06:08:56 -05:00
$ bin/rails railties:install:migrations
2012-09-01 17:08:06 -04:00
```
2012-02-12 21:56:11 -05:00
2013-12-14 15:28:08 -05:00
This command, when run for the first time, will copy over all the migrations
from the engine. When run the next time, it will only copy over migrations that
haven't been copied over already. The first run for this command will output
something such as this:
2011-10-14 18:52:07 -04:00
2012-09-01 20:45:26 -04:00
```bash
2015-05-30 11:57:25 -04:00
Copied migration [timestamp_1]_create_blorgh_articles.blorgh.rb from blorgh
Copied migration [timestamp_2]_create_blorgh_comments.blorgh.rb from blorgh
2012-09-01 17:08:06 -04:00
```
2011-10-14 18:52:07 -04:00
2013-12-14 15:28:08 -05:00
The first timestamp (`[timestamp_1]`) will be the current time, and the second
timestamp (`[timestamp_2]`) will be the current time plus a second. The reason
for this is so that the migrations for the engine are run after any existing
migrations in the application.
2011-10-06 18:56:50 -04:00
2016-01-19 06:08:56 -05:00
To run these migrations within the context of the application, simply run `bin/rails
2013-12-14 15:28:08 -05:00
db:migrate`. When accessing the engine through `http://localhost:3000/blog` , the
2014-05-21 21:47:18 -04:00
articles will be empty. This is because the table created inside the application is
2013-12-14 15:28:08 -05:00
different from the one created within the engine. Go ahead, play around with the
newly mounted engine. You'll find that it's the same as when it was only an
engine.
2011-10-04 17:37:38 -04:00
2013-12-14 15:28:08 -05:00
If you would like to run migrations only from one engine, you can do it by
specifying `SCOPE` :
2012-03-13 19:25:06 -04:00
2012-09-01 20:45:26 -04:00
```bash
2016-01-19 06:08:56 -05:00
bin/rails db:migrate SCOPE=blorgh
2012-09-01 17:08:06 -04:00
```
2012-03-13 19:25:06 -04:00
2013-12-14 15:28:08 -05:00
This may be useful if you want to revert engine's migrations before removing it.
To revert all migrations from blorgh engine you can run code such as:
2012-03-13 19:25:06 -04:00
2012-09-01 20:45:26 -04:00
```bash
2016-01-19 06:08:56 -05:00
bin/rails db:migrate SCOPE=blorgh VERSION=0
2012-09-01 17:08:06 -04:00
```
2012-03-13 19:25:06 -04:00
2013-12-14 15:27:45 -05:00
### Using a Class Provided by the Application
2011-10-15 18:53:26 -04:00
2013-12-14 15:27:45 -05:00
#### Using a Model Provided by the Application
2012-05-11 14:42:37 -04:00
2013-12-14 15:28:08 -05:00
When an engine is created, it may want to use specific classes from an
application to provide links between the pieces of the engine and the pieces of
2014-05-21 21:47:18 -04:00
the application. In the case of the `blorgh` engine, making articles and comments
2013-12-14 15:28:08 -05:00
have authors would make a lot of sense.
2011-10-15 18:53:26 -04:00
2013-12-14 15:28:08 -05:00
A typical application might have a `User` class that would be used to represent
2014-06-27 10:50:41 -04:00
authors for an article or a comment. But there could be a case where the
application calls this class something different, such as `Person` . For this
reason, the engine should not hardcode associations specifically for a `User`
class.
2011-10-15 18:53:26 -04:00
2013-12-14 15:28:08 -05:00
To keep it simple in this case, the application will have a class called `User`
2014-06-27 13:54:58 -04:00
that represents the users of the application (we'll get into making this
configurable further on). It can be generated using this command inside the
application:
2011-10-15 18:53:26 -04:00
2012-09-01 20:45:26 -04:00
```bash
2011-10-15 18:53:26 -04:00
rails g model user name:string
2012-09-01 17:08:06 -04:00
```
2011-10-15 18:53:26 -04:00
2016-01-19 06:08:56 -05:00
The `bin/rails db:migrate` command needs to be run here to ensure that our
2013-12-14 15:28:08 -05:00
application has the `users` table for future use.
2011-10-17 05:25:45 -04:00
2014-05-21 21:47:18 -04:00
Also, to keep it simple, the articles form will have a new text field called
2013-12-14 15:28:08 -05:00
`author_name` , where users can elect to put their name. The engine will then
take this name and either create a new `User` object from it, or find one that
2014-05-21 21:47:18 -04:00
already has that name. The engine will then associate the article with the found or
2013-12-14 15:28:08 -05:00
created `User` object.
2011-10-15 18:53:26 -04:00
2013-12-14 15:28:08 -05:00
First, the `author_name` text field needs to be added to the
2014-05-21 21:47:18 -04:00
`app/views/blorgh/articles/_form.html.erb` partial inside the engine. This can be
2013-12-14 15:28:08 -05:00
added above the `title` field with this code:
2011-10-15 18:53:26 -04:00
2012-09-03 21:21:24 -04:00
```html+erb
2011-10-15 18:53:26 -04:00
< div class = "field" >
2013-07-05 23:04:25 -04:00
< %= f.label :author_name %>< br >
2011-10-16 17:55:32 -04:00
< %= f.text_field :author_name %>
2011-10-15 18:53:26 -04:00
< / div >
2012-09-01 17:08:06 -04:00
```
2011-10-15 18:53:26 -04:00
2014-05-21 21:47:18 -04:00
Next, we need to update our `Blorgh::ArticleController#article_params` method to
2013-12-14 15:28:08 -05:00
permit the new form parameter:
2013-08-10 06:58:24 -04:00
```ruby
2014-05-21 21:47:18 -04:00
def article_params
params.require(:article).permit(:title, :text, :author_name)
2013-08-10 06:58:24 -04:00
end
```
2014-05-21 21:47:18 -04:00
The `Blorgh::Article` model should then have some code to convert the `author_name`
field into an actual `User` object and associate it as that article's `author`
before the article is saved. It will also need to have an `attr_accessor` set up
2013-12-14 15:28:08 -05:00
for this field, so that the setter and getter methods are defined for it.
2011-10-16 17:55:32 -04:00
2013-12-14 15:28:08 -05:00
To do all this, you'll need to add the `attr_accessor` for `author_name` , the
association for the author and the `before_save` call into
2014-05-21 21:47:18 -04:00
`app/models/blorgh/article.rb` . The `author` association will be hard-coded to the
2013-12-14 15:28:08 -05:00
`User` class for the time being.
2011-10-16 17:55:32 -04:00
2012-09-01 17:08:06 -04:00
```ruby
2011-10-16 17:55:32 -04:00
attr_accessor :author_name
2012-11-15 16:56:55 -05:00
belongs_to :author, class_name: "User"
2011-10-16 17:55:32 -04:00
before_save :set_author
private
def set_author
2013-01-01 15:07:32 -05:00
self.author = User.find_or_create_by(name: author_name)
2011-10-16 17:55:32 -04:00
end
2012-09-01 17:08:06 -04:00
```
2011-10-16 17:55:32 -04:00
2013-12-14 15:28:08 -05:00
By representing the `author` association's object with the `User` class, a link
is established between the engine and the application. There needs to be a way
2014-05-21 21:47:18 -04:00
of associating the records in the `blorgh_articles` table with the records in the
2013-12-14 15:28:08 -05:00
`users` table. Because the association is called `author` , there should be an
2014-05-21 21:47:18 -04:00
`author_id` column added to the `blorgh_articles` table.
2011-10-17 05:26:37 -04:00
To generate this new column, run this command within the engine:
2012-09-01 20:45:26 -04:00
```bash
2014-05-21 21:47:18 -04:00
$ bin/rails g migration add_author_id_to_blorgh_articles author_id:integer
2012-09-01 17:08:06 -04:00
```
2011-10-17 05:26:37 -04:00
2013-12-14 15:28:08 -05:00
NOTE: Due to the migration's name and the column specification after it, Rails
will automatically know that you want to add a column to a specific table and
write that into the migration for you. You don't need to tell it any more than
this.
2011-10-17 05:26:37 -04:00
2013-12-14 15:28:08 -05:00
This migration will need to be run on the application. To do that, it must first
be copied using this command:
2011-10-17 05:26:37 -04:00
2012-09-01 20:45:26 -04:00
```bash
2016-01-19 06:08:56 -05:00
$ bin/rails blorgh:install:migrations
2012-09-01 17:08:06 -04:00
```
2011-10-17 05:26:37 -04:00
2013-12-14 15:28:08 -05:00
Notice that only _one_ migration was copied over here. This is because the first
two migrations were copied over the first time this command was run.
2011-10-17 05:26:37 -04:00
2014-05-21 21:47:18 -04:00
```
2015-05-30 11:57:25 -04:00
NOTE Migration [timestamp]_create_blorgh_articles.blorgh.rb from blorgh has been skipped. Migration with the same name already exists.
NOTE Migration [timestamp]_create_blorgh_comments.blorgh.rb from blorgh has been skipped. Migration with the same name already exists.
Copied migration [timestamp]_add_author_id_to_blorgh_articles.blorgh.rb from blorgh
2012-09-01 17:08:06 -04:00
```
2011-10-20 02:32:34 -04:00
2013-12-14 15:27:45 -05:00
Run the migration using:
2011-10-17 05:26:37 -04:00
2012-09-01 20:45:26 -04:00
```bash
2016-01-19 06:08:56 -05:00
$ bin/rails db:migrate
2012-09-01 17:08:06 -04:00
```
2011-10-17 05:26:37 -04:00
2013-12-14 15:28:08 -05:00
Now with all the pieces in place, an action will take place that will associate
2014-05-21 21:47:18 -04:00
an author - represented by a record in the `users` table - with an article,
represented by the `blorgh_articles` table from the engine.
2011-10-17 05:26:37 -04:00
2014-05-21 21:47:18 -04:00
Finally, the author's name should be displayed on the article's page. Add this code
above the "Title" output inside `app/views/blorgh/articles/show.html.erb` :
2011-10-17 05:26:37 -04:00
2012-09-03 21:21:24 -04:00
```html+erb
2011-10-17 05:26:37 -04:00
< p >
< b > Author:< / b >
2015-09-24 12:15:52 -04:00
< %= @article .author.name %>
2011-10-17 05:26:37 -04:00
< / p >
2012-09-01 17:08:06 -04:00
```
2011-10-17 05:26:37 -04:00
2013-12-14 15:27:45 -05:00
#### Using a Controller Provided by the Application
2013-10-24 07:33:13 -04:00
2013-12-14 15:28:08 -05:00
Because Rails controllers generally share code for things like authentication
and accessing session variables, they inherit from `ApplicationController` by
default. Rails engines, however are scoped to run independently from the main
application, so each engine gets a scoped `ApplicationController` . This
namespace prevents code collisions, but often engine controllers need to access
methods in the main application's `ApplicationController` . An easy way to
provide this access is to change the engine's scoped `ApplicationController` to
inherit from the main application's `ApplicationController` . For our Blorgh
engine this would be done by changing
`app/controllers/blorgh/application_controller.rb` to look like:
2012-05-11 14:42:37 -04:00
2012-09-01 17:08:06 -04:00
```ruby
2015-02-21 07:34:22 -05:00
module Blorgh
class ApplicationController < ::ApplicationController
end
2012-05-12 08:37:10 -04:00
end
2012-09-01 17:08:06 -04:00
```
2012-05-11 14:42:37 -04:00
2013-12-14 15:28:08 -05:00
By default, the engine's controllers inherit from
`Blorgh::ApplicationController` . So, after making this change they will have
access to the main application's `ApplicationController` , as though they were
part of the main application.
2012-05-11 14:42:37 -04:00
2013-12-14 15:28:08 -05:00
This change does require that the engine is run from a Rails application that
has an `ApplicationController` .
2012-05-11 14:42:37 -04:00
2013-12-14 15:27:45 -05:00
### Configuring an Engine
2011-10-16 17:55:32 -04:00
2013-12-14 15:28:08 -05:00
This section covers how to make the `User` class configurable, followed by
general configuration tips for the engine.
2011-10-28 03:46:18 -04:00
2013-12-14 15:27:45 -05:00
#### Setting Configuration Settings in the Application
2011-10-28 03:46:18 -04:00
2013-12-14 15:28:08 -05:00
The next step is to make the class that represents a `User` in the application
customizable for the engine. This is because that class may not always be
`User` , as previously explained. To make this setting customizable, the engine
will have a configuration setting called `author_class` that will be used to
specify which class represents users inside the application.
2011-10-17 05:26:37 -04:00
2013-12-14 15:28:08 -05:00
To define this configuration setting, you should use a `mattr_accessor` inside
the `Blorgh` module for the engine. Add this line to `lib/blorgh.rb` inside the
engine:
2011-10-17 05:26:37 -04:00
2012-09-01 17:08:06 -04:00
```ruby
2013-04-08 09:56:20 -04:00
mattr_accessor :author_class
2012-09-01 17:08:06 -04:00
```
2011-10-17 05:26:37 -04:00
2013-12-14 15:28:08 -05:00
This method works like its brothers, `attr_accessor` and `cattr_accessor` , but
provides a setter and getter method on the module with the specified name. To
use it, it must be referenced using `Blorgh.author_class` .
2011-10-17 05:26:37 -04:00
2014-05-21 21:47:18 -04:00
The next step is to switch the `Blorgh::Article` model over to this new setting.
2013-12-14 15:28:08 -05:00
Change the `belongs_to` association inside this model
2014-05-21 21:47:18 -04:00
(`app/models/blorgh/article.rb`) to this:
2011-10-17 05:26:37 -04:00
2012-09-01 17:08:06 -04:00
```ruby
2013-04-08 09:56:20 -04:00
belongs_to :author, class_name: Blorgh.author_class
2012-09-01 17:08:06 -04:00
```
2011-10-17 05:26:37 -04:00
2014-05-21 21:47:18 -04:00
The `set_author` method in the `Blorgh::Article` model should also use this class:
2011-10-17 05:26:37 -04:00
2012-09-01 17:08:06 -04:00
```ruby
2013-04-08 09:56:20 -04:00
self.author = Blorgh.author_class.constantize.find_or_create_by(name: author_name)
2012-09-01 17:08:06 -04:00
```
2011-10-17 05:26:37 -04:00
2013-12-14 15:28:08 -05:00
To save having to call `constantize` on the `author_class` result all the time,
you could instead just override the `author_class` getter method inside the
`Blorgh` module in the `lib/blorgh.rb` file to always call `constantize` on the
saved value before returning the result:
2012-02-13 17:59:39 -05:00
2012-09-01 17:08:06 -04:00
```ruby
2013-04-08 09:56:20 -04:00
def self.author_class
@@author_class.constantize
2012-04-02 14:11:22 -04:00
end
2012-09-01 17:08:06 -04:00
```
2012-02-13 17:59:39 -05:00
2012-09-01 21:37:59 -04:00
This would then turn the above code for `set_author` into this:
2012-02-13 17:59:39 -05:00
2012-09-01 17:08:06 -04:00
```ruby
2013-04-08 09:56:20 -04:00
self.author = Blorgh.author_class.find_or_create_by(name: author_name)
2012-09-01 17:08:06 -04:00
```
2012-02-13 17:59:39 -05:00
2013-12-14 15:28:08 -05:00
Resulting in something a little shorter, and more implicit in its behavior. The
`author_class` method should always return a `Class` object.
2012-02-13 17:59:39 -05:00
2013-12-28 05:24:01 -05:00
Since we changed the `author_class` method to return a `Class` instead of a
2014-05-21 21:47:18 -04:00
`String` , we must also modify our `belongs_to` definition in the `Blorgh::Article`
2013-12-14 15:28:08 -05:00
model:
2013-02-11 09:19:38 -05:00
```ruby
2013-04-08 09:56:20 -04:00
belongs_to :author, class_name: Blorgh.author_class.to_s
2013-02-11 09:19:38 -05:00
```
2013-12-14 15:28:08 -05:00
To set this configuration setting within the application, an initializer should
be used. By using an initializer, the configuration will be set up before the
application starts and calls the engine's models, which may depend on this
configuration setting existing.
2011-10-17 05:26:37 -04:00
2013-12-14 15:28:08 -05:00
Create a new initializer at `config/initializers/blorgh.rb` inside the
application where the `blorgh` engine is installed and put this content in it:
2011-10-17 05:26:37 -04:00
2012-09-01 17:08:06 -04:00
```ruby
2013-04-08 09:56:20 -04:00
Blorgh.author_class = "User"
2012-09-01 17:08:06 -04:00
```
2011-10-17 05:26:37 -04:00
2013-12-14 15:28:08 -05:00
WARNING: It's very important here to use the `String` version of the class,
rather than the class itself. If you were to use the class, Rails would attempt
to load that class and then reference the related table. This could lead to
problems if the table wasn't already existing. Therefore, a `String` should be
used and then converted to a class using `constantize` in the engine later on.
2011-10-17 05:26:37 -04:00
2014-05-21 21:47:18 -04:00
Go ahead and try to create a new article. You will see that it works exactly in the
2013-12-14 15:28:08 -05:00
same way as before, except this time the engine is using the configuration
setting in `config/initializers/blorgh.rb` to learn what the class is.
2011-10-17 05:26:37 -04:00
2013-12-14 15:28:08 -05:00
There are now no strict dependencies on what the class is, only what the API for
the class must be. The engine simply requires this class to define a
`find_or_create_by` method which returns an object of that class, to be
2014-05-21 21:47:18 -04:00
associated with an article when it's created. This object, of course, should have
2013-12-14 15:28:08 -05:00
some sort of identifier by which it can be referenced.
2011-10-04 17:37:38 -04:00
2013-12-14 15:27:45 -05:00
#### General Engine Configuration
2011-10-28 03:46:18 -04:00
2013-12-14 15:28:08 -05:00
Within an engine, there may come a time where you wish to use things such as
initializers, internationalization or other configuration options. The great
news is that these things are entirely possible, because a Rails engine shares
much the same functionality as a Rails application. In fact, a Rails
application's functionality is actually a superset of what is provided by
engines!
2011-10-28 03:46:18 -04:00
2013-08-23 11:59:11 -04:00
If you wish to use an initializer - code that should run before the engine is
loaded - the place for it is the `config/initializers` folder. This directory's
2013-12-14 15:28:08 -05:00
functionality is explained in the [Initializers
section](configuring.html#initializers) of the Configuring guide, and works
precisely the same way as the `config/initializers` directory inside an
application. The same thing goes if you want to use a standard initializer.
2011-10-28 03:46:18 -04:00
2013-12-14 15:28:08 -05:00
For locales, simply place the locale files in the `config/locales` directory,
just like you would in an application.
2011-10-28 03:46:18 -04:00
2013-12-28 05:24:01 -05:00
Testing an engine
-----------------
2012-02-12 21:56:11 -05:00
2013-12-14 15:28:08 -05:00
When an engine is generated, there is a smaller dummy application created inside
it at `test/dummy` . This application is used as a mounting point for the engine,
to make testing the engine extremely simple. You may extend this application by
generating controllers, models or views from within the directory, and then use
those to test your engine.
2012-02-12 21:56:11 -05:00
2013-12-14 15:28:08 -05:00
The `test` directory should be treated like a typical Rails testing environment,
allowing for unit, functional and integration tests.
2012-02-12 21:56:11 -05:00
2013-12-14 15:27:45 -05:00
### Functional Tests
2012-02-13 17:59:39 -05:00
2013-12-14 15:28:08 -05:00
A matter worth taking into consideration when writing functional tests is that
the tests are going to be running on an application - the `test/dummy`
application - rather than your engine. This is due to the setup of the testing
environment; an engine needs an application as a host for testing its main
functionality, especially controllers. This means that if you were to make a
typical `GET` to a controller in a controller's functional test like this:
2012-02-13 17:59:39 -05:00
2012-09-01 17:08:06 -04:00
```ruby
2014-11-23 03:15:47 -05:00
module Blorgh
2015-12-12 13:48:52 -05:00
class FooControllerTest < ActionDispatch::IntegrationTest
2016-02-04 23:09:23 -05:00
include Engine.routes.url_helpers
2014-11-23 03:15:47 -05:00
def test_index
2015-12-12 13:48:52 -05:00
get foos_url
2014-11-23 03:15:47 -05:00
...
end
end
end
2012-09-01 17:08:06 -04:00
```
2012-02-13 17:59:39 -05:00
2013-12-14 15:28:08 -05:00
It may not function correctly. This is because the application doesn't know how
to route these requests to the engine unless you explicitly tell it **how** . To
2014-11-23 03:15:47 -05:00
do this, you must set the `@routes` instance variable to the engine's route set
in your setup code:
2012-02-13 17:59:39 -05:00
2012-09-01 17:08:06 -04:00
```ruby
2014-11-23 03:15:47 -05:00
module Blorgh
2015-12-12 13:48:52 -05:00
class FooControllerTest < ActionDispatch::IntegrationTest
2016-02-04 23:09:23 -05:00
include Engine.routes.url_helpers
2014-11-23 03:15:47 -05:00
setup do
@routes = Engine.routes
end
def test_index
2015-12-12 13:48:52 -05:00
get foos_url
2014-11-23 03:15:47 -05:00
...
end
end
end
2012-09-01 17:08:06 -04:00
```
2012-02-13 17:59:39 -05:00
2013-12-14 15:28:08 -05:00
This tells the application that you still want to perform a `GET` request to the
`index` action of this controller, but you want to use the engine's route to get
there, rather than the application's one.
2012-02-13 17:59:39 -05:00
2014-11-23 03:15:47 -05:00
This also ensures that the engine's URL helpers will work as expected in your
tests.
2012-02-13 17:59:39 -05:00
2013-10-24 07:33:13 -04:00
Improving engine functionality
2012-09-01 17:25:58 -04:00
------------------------------
2011-10-04 17:37:38 -04:00
2013-12-14 15:28:08 -05:00
This section explains how to add and/or override engine MVC functionality in the
main Rails application.
2011-10-14 03:43:49 -04:00
2012-09-01 17:25:58 -04:00
### Overriding Models and Controllers
2012-07-22 17:24:49 -04:00
2013-12-14 15:28:08 -05:00
Engine model and controller classes can be extended by open classing them in the
main Rails application (since model and controller classes are just Ruby classes
that inherit Rails specific functionality). Open classing an Engine class
redefines it for use in the main application. This is usually implemented by
using the decorator pattern.
2012-07-22 17:24:49 -04:00
2013-12-14 15:28:08 -05:00
For simple class modifications, use `Class#class_eval` . For complex class
modifications, consider using `ActiveSupport::Concern` .
2012-07-22 17:24:49 -04:00
2013-12-14 15:27:45 -05:00
#### A note on Decorators and Loading Code
2013-05-24 12:24:00 -04:00
Because these decorators are not referenced by your Rails application itself,
2013-12-14 15:28:08 -05:00
Rails' autoloading system will not kick in and load your decorators. This means
that you need to require them yourself.
2013-05-24 12:24:00 -04:00
Here is some sample code to do this:
```ruby
# lib/blorgh/engine.rb
2013-10-24 07:33:13 -04:00
module Blorgh
2013-05-24 12:24:00 -04:00
class Engine < ::Rails::Engine
isolate_namespace Blorgh
config.to_prepare do
Dir.glob(Rails.root + "app/decorators/**/*_decorator*.rb").each do |c|
require_dependency(c)
end
end
end
end
```
This doesn't apply to just Decorators, but anything that you add in an engine
that isn't referenced by your main application.
2012-09-01 17:25:58 -04:00
#### Implementing Decorator Pattern Using Class#class_eval
2013-10-24 07:33:13 -04:00
2014-05-21 21:47:18 -04:00
**Adding** `Article#time_since_created` :
2012-07-22 17:24:49 -04:00
2012-09-01 17:08:06 -04:00
```ruby
2014-05-21 21:47:18 -04:00
# MyApp/app/decorators/models/blorgh/article_decorator.rb
2013-10-24 07:33:13 -04:00
2014-05-21 21:47:18 -04:00
Blorgh::Article.class_eval do
2012-07-22 17:24:49 -04:00
def time_since_created
Time.current - created_at
end
end
2012-09-01 17:08:06 -04:00
```
2012-07-22 17:24:49 -04:00
2012-09-01 17:08:06 -04:00
```ruby
2014-05-21 21:47:18 -04:00
# Blorgh/app/models/article.rb
2013-10-24 07:33:13 -04:00
2015-12-12 08:25:00 -05:00
class Article < ApplicationRecord
2012-09-07 16:17:27 -04:00
has_many :comments
2012-08-04 20:12:24 -04:00
end
2012-09-01 17:08:06 -04:00
```
2012-08-04 20:12:24 -04:00
2014-05-21 21:47:18 -04:00
**Overriding** `Article#summary` :
2012-07-22 17:24:49 -04:00
2012-09-01 17:08:06 -04:00
```ruby
2014-05-21 21:47:18 -04:00
# MyApp/app/decorators/models/blorgh/article_decorator.rb
2013-10-24 07:33:13 -04:00
2014-05-21 21:47:18 -04:00
Blorgh::Article.class_eval do
2012-08-04 20:12:24 -04:00
def summary
"#{title} - #{truncate(text)}"
end
end
2012-09-01 17:08:06 -04:00
```
2012-08-04 20:12:24 -04:00
2012-09-01 17:08:06 -04:00
```ruby
2014-05-21 21:47:18 -04:00
# Blorgh/app/models/article.rb
2013-10-24 07:33:13 -04:00
2015-12-12 08:25:00 -05:00
class Article < ApplicationRecord
2012-09-07 16:17:27 -04:00
has_many :comments
2012-08-04 20:12:24 -04:00
def summary
"#{title}"
end
end
2012-09-01 17:08:06 -04:00
```
2012-08-04 20:12:24 -04:00
2012-09-01 17:25:58 -04:00
#### Implementing Decorator Pattern Using ActiveSupport::Concern
2013-10-24 07:33:13 -04:00
2013-12-14 15:28:08 -05:00
Using `Class#class_eval` is great for simple adjustments, but for more complex
class modifications, you might want to consider using [`ActiveSupport::Concern`]
2014-11-25 07:34:43 -05:00
(http://api.rubyonrails.org/classes/ActiveSupport/Concern.html).
2013-12-14 15:28:08 -05:00
ActiveSupport::Concern manages load order of interlinked dependent modules and
classes at run time allowing you to significantly modularize your code.
2012-08-04 20:12:24 -04:00
2014-05-21 21:47:18 -04:00
**Adding** `Article#time_since_created` and **Overriding** `Article#summary` :
2012-07-22 17:24:49 -04:00
2012-09-01 17:08:06 -04:00
```ruby
2014-05-21 21:47:18 -04:00
# MyApp/app/models/blorgh/article.rb
2013-10-24 07:33:13 -04:00
2015-12-12 08:25:00 -05:00
class Blorgh::Article < ApplicationRecord
2014-05-21 21:47:18 -04:00
include Blorgh::Concerns::Models::Article
2012-07-22 17:24:49 -04:00
def time_since_created
Time.current - created_at
end
2012-08-04 20:12:24 -04:00
def summary
"#{title} - #{truncate(text)}"
end
end
2012-09-01 17:08:06 -04:00
```
2012-07-22 17:24:49 -04:00
2012-09-01 17:08:06 -04:00
```ruby
2014-05-21 21:47:18 -04:00
# Blorgh/app/models/article.rb
2013-10-24 07:33:13 -04:00
2015-12-12 08:25:00 -05:00
class Article < ApplicationRecord
2014-05-21 21:47:18 -04:00
include Blorgh::Concerns::Models::Article
2012-07-22 17:24:49 -04:00
end
2012-09-01 17:08:06 -04:00
```
2012-07-22 17:24:49 -04:00
2012-09-01 17:08:06 -04:00
```ruby
2015-04-10 16:07:04 -04:00
# Blorgh/lib/concerns/models/article.rb
2012-07-22 17:24:49 -04:00
2014-05-21 21:47:18 -04:00
module Blorgh::Concerns::Models::Article
2012-07-22 17:24:49 -04:00
extend ActiveSupport::Concern
2012-07-22 20:51:17 -04:00
2012-08-04 20:12:24 -04:00
# 'included do' causes the included code to be evaluated in the
2014-05-21 21:47:18 -04:00
# context where it is included (article.rb), rather than being
# executed in the module's context (blorgh/concerns/models/article).
2012-07-22 17:24:49 -04:00
included do
attr_accessor :author_name
2012-11-15 16:56:55 -05:00
belongs_to :author, class_name: "User"
2012-07-22 20:51:17 -04:00
2012-07-22 17:24:49 -04:00
before_save :set_author
2012-07-22 20:51:17 -04:00
2012-07-22 17:24:49 -04:00
private
2013-01-24 03:42:40 -05:00
def set_author
self.author = User.find_or_create_by(name: author_name)
end
2012-07-22 17:24:49 -04:00
end
2012-08-04 20:12:24 -04:00
def summary
"#{title}"
2012-07-22 17:24:49 -04:00
end
module ClassMethods
def some_class_method
'some class method string'
end
end
end
2012-09-01 17:08:06 -04:00
```
2012-07-22 17:24:49 -04:00
2013-12-14 15:27:45 -05:00
### Overriding Views
2011-10-20 03:30:50 -04:00
2013-12-14 15:28:08 -05:00
When Rails looks for a view to render, it will first look in the `app/views`
directory of the application. If it cannot find the view there, it will check in
the `app/views` directories of all engines that have this directory.
2011-10-20 03:30:50 -04:00
2014-05-21 21:47:18 -04:00
When the application is asked to render the view for `Blorgh::ArticlesController` 's
2013-12-14 15:28:08 -05:00
index action, it will first look for the path
2014-05-21 21:47:18 -04:00
`app/views/blorgh/articles/index.html.erb` within the application. If it cannot
2013-12-14 15:28:08 -05:00
find it, it will look inside the engine.
2011-10-20 03:30:50 -04:00
2013-12-14 15:28:08 -05:00
You can override this view in the application by simply creating a new file at
2014-05-21 21:47:18 -04:00
`app/views/blorgh/articles/index.html.erb` . Then you can completely change what
2013-12-14 15:28:08 -05:00
this view would normally output.
2011-10-20 03:30:50 -04:00
2014-05-21 21:47:18 -04:00
Try this now by creating a new file at `app/views/blorgh/articles/index.html.erb`
2013-12-14 15:28:08 -05:00
and put this content in it:
2011-10-20 03:30:50 -04:00
2012-09-03 21:21:24 -04:00
```html+erb
2014-05-21 21:47:18 -04:00
< h1 > Articles< / h1 >
< %= link_to "New Article", new_article_path %>
< % @articles .each do |article| %>
< h2 > < %= article.title %>< / h2 >
< small > By < %= article.author %>< / small >
< %= simple_format(article.text) %>
2011-10-20 03:30:50 -04:00
< hr >
< % end %>
2012-09-01 17:08:06 -04:00
```
2011-10-20 03:30:50 -04:00
2012-09-01 17:25:58 -04:00
### Routes
2011-10-20 03:30:50 -04:00
2013-12-14 15:28:08 -05:00
Routes inside an engine are isolated from the application by default. This is
done by the `isolate_namespace` call inside the `Engine` class. This essentially
means that the application and its engines can have identically named routes and
they will not clash.
2012-02-12 23:15:59 -05:00
2013-12-14 15:28:08 -05:00
Routes inside an engine are drawn on the `Engine` class within
`config/routes.rb` , like this:
2012-02-12 23:15:59 -05:00
2012-09-01 17:08:06 -04:00
```ruby
2012-04-02 14:11:22 -04:00
Blorgh::Engine.routes.draw do
2014-05-21 21:47:18 -04:00
resources :articles
2012-04-02 14:11:22 -04:00
end
2012-09-01 17:08:06 -04:00
```
2012-02-12 23:15:59 -05:00
2013-12-14 15:28:08 -05:00
By having isolated routes such as this, if you wish to link to an area of an
engine from within an application, you will need to use the engine's routing
2014-05-21 21:47:18 -04:00
proxy method. Calls to normal routing methods such as `articles_path` may end up
2013-12-14 15:28:08 -05:00
going to undesired locations if both the application and the engine have such a
helper defined.
2012-02-12 23:15:59 -05:00
2014-05-21 21:47:18 -04:00
For instance, the following example would go to the application's `articles_path`
if that template was rendered from the application, or the engine's `articles_path`
2013-12-14 15:28:08 -05:00
if it was rendered from the engine:
2011-10-20 03:30:50 -04:00
2012-09-01 17:08:06 -04:00
```erb
2014-05-21 21:47:18 -04:00
< %= link_to "Blog articles", articles_path %>
2012-09-01 17:08:06 -04:00
```
2011-10-20 03:30:50 -04:00
2014-05-21 21:47:18 -04:00
To make this route always use the engine's `articles_path` routing helper method,
2013-12-14 15:28:08 -05:00
we must call the method on the routing proxy method that shares the same name as
the engine.
2011-10-20 03:30:50 -04:00
2012-09-01 17:08:06 -04:00
```erb
2014-05-21 21:47:18 -04:00
< %= link_to "Blog articles", blorgh.articles_path %>
2012-09-01 17:08:06 -04:00
```
2011-10-20 03:30:50 -04:00
2013-12-14 15:28:08 -05:00
If you wish to reference the application inside the engine in a similar way, use
the `main_app` helper:
2011-10-20 03:30:50 -04:00
2012-09-01 17:08:06 -04:00
```erb
2011-10-20 03:30:50 -04:00
< %= link_to "Home", main_app.root_path %>
2012-09-01 17:08:06 -04:00
```
2011-10-20 03:30:50 -04:00
2013-12-14 15:28:08 -05:00
If you were to use this inside an engine, it would **always** go to the
application's root. If you were to leave off the `main_app` "routing proxy"
method call, it could potentially go to the engine's or application's root,
depending on where it was called from.
2012-02-12 21:56:11 -05:00
2013-12-14 15:28:08 -05:00
If a template rendered from within an engine attempts to use one of the
application's routing helper methods, it may result in an undefined method call.
If you encounter such an issue, ensure that you're not attempting to call the
application's routing methods without the `main_app` prefix from within the
engine.
2012-02-12 23:15:59 -05:00
2012-09-01 17:25:58 -04:00
### Assets
2012-02-12 21:56:11 -05:00
2013-12-14 15:28:08 -05:00
Assets within an engine work in an identical way to a full application. Because
the engine class inherits from `Rails::Engine` , the application will know to
look up assets in the engine's 'app/assets' and 'lib/assets' directories.
2012-02-12 21:56:11 -05:00
2013-12-14 15:28:08 -05:00
Like all of the other components of an engine, the assets should be namespaced.
This means that if you have an asset called `style.css` , it should be placed at
`app/assets/stylesheets/[engine name]/style.css` , rather than
`app/assets/stylesheets/style.css` . If this asset isn't namespaced, there is a
possibility that the host application could have an asset named identically, in
which case the application's asset would take precedence and the engine's one
would be ignored.
2012-02-12 21:56:11 -05:00
2013-12-14 15:28:08 -05:00
Imagine that you did have an asset located at
`app/assets/stylesheets/blorgh/style.css` To include this asset inside an
application, just use `stylesheet_link_tag` and reference the asset as if it
were inside the engine:
2012-02-12 21:56:11 -05:00
2012-09-01 17:08:06 -04:00
```erb
2012-02-12 21:56:11 -05:00
< %= stylesheet_link_tag "blorgh/style.css" %>
2012-09-01 17:08:06 -04:00
```
2012-02-12 21:56:11 -05:00
2013-12-14 15:28:08 -05:00
You can also specify these assets as dependencies of other assets using Asset
Pipeline require statements in processed files:
2012-02-13 17:59:39 -05:00
2012-09-01 17:08:06 -04:00
```
2012-02-13 17:59:39 -05:00
/*
*= require blorgh/style
*/
2012-09-01 17:08:06 -04:00
```
2012-02-13 17:59:39 -05:00
2013-12-14 15:28:08 -05:00
INFO. Remember that in order to use languages like Sass or CoffeeScript, you
should add the relevant library to your engine's `.gemspec` .
2012-07-09 16:31:24 -04:00
2012-09-01 17:25:58 -04:00
### Separate Assets & Precompiling
2012-03-22 07:05:28 -04:00
2013-12-14 15:28:08 -05:00
There are some situations where your engine's assets are not required by the
host application. For example, say that you've created an admin functionality
that only exists for your engine. In this case, the host application doesn't
need to require `admin.css` or `admin.js` . Only the gem's admin layout needs
these assets. It doesn't make sense for the host app to include
`"blorgh/admin.css"` in its stylesheets. In this situation, you should
explicitly define these assets for precompilation. This tells sprockets to add
2016-01-19 06:08:56 -05:00
your engine assets when `bin/rails assets:precompile` is triggered.
2012-03-22 07:05:28 -04:00
2013-12-14 15:27:45 -05:00
You can define assets for precompilation in `engine.rb` :
2012-03-22 07:05:28 -04:00
2012-09-01 17:08:06 -04:00
```ruby
2012-05-17 22:16:42 -04:00
initializer "blorgh.assets.precompile" do |app|
2012-03-22 07:05:28 -04:00
app.config.assets.precompile += %w(admin.css admin.js)
end
2012-09-01 17:08:06 -04:00
```
2012-03-22 07:05:28 -04:00
2013-12-14 15:27:45 -05:00
For more information, read the [Asset Pipeline guide ](asset_pipeline.html ).
2012-02-13 17:59:39 -05:00
2013-12-14 15:27:45 -05:00
### Other Gem Dependencies
2012-02-12 21:56:11 -05:00
2013-12-14 15:28:08 -05:00
Gem dependencies inside an engine should be specified inside the `.gemspec` file
at the root of the engine. The reason is that the engine may be installed as a
gem. If dependencies were to be specified inside the `Gemfile` , these would not
be recognized by a traditional gem install and so they would not be installed,
causing the engine to malfunction.
2012-02-12 21:56:11 -05:00
2012-07-12 14:39:26 -04:00
To specify a dependency that should be installed with the engine during a
2012-09-01 21:37:59 -04:00
traditional `gem install` , specify it inside the `Gem::Specification` block
inside the `.gemspec` file in the engine:
2012-02-12 21:56:11 -05:00
2012-09-01 17:08:06 -04:00
```ruby
2012-02-12 21:56:11 -05:00
s.add_dependency "moo"
2012-09-01 17:08:06 -04:00
```
2012-02-12 21:56:11 -05:00
2013-12-14 15:28:08 -05:00
To specify a dependency that should only be installed as a development
2012-07-12 14:39:26 -04:00
dependency of the application, specify it like this:
2012-02-12 21:56:11 -05:00
2012-09-01 17:08:06 -04:00
```ruby
2012-02-12 21:56:11 -05:00
s.add_development_dependency "moo"
2012-09-01 17:08:06 -04:00
```
2012-02-12 21:56:11 -05:00
2012-09-01 21:37:59 -04:00
Both kinds of dependencies will be installed when `bundle install` is run inside
2013-12-14 15:28:08 -05:00
of the application. The development dependencies for the gem will only be used
when the tests for the engine are running.
2012-07-12 14:39:26 -04:00
Note that if you want to immediately require dependencies when the engine is
2013-12-14 15:28:08 -05:00
required, you should require them before the engine's initialization. For
example:
2012-07-12 14:39:26 -04:00
2012-09-01 17:08:06 -04:00
```ruby
2012-07-12 14:39:26 -04:00
require 'other_engine/engine'
require 'yet_another_engine/engine'
module MyEngine
class Engine < ::Rails::Engine
end
end
2012-09-01 17:08:06 -04:00
```