railties 3.0.11 → 3.0.12.rc1

Sign up to get free protection for your applications and to get access to all the features.
Files changed (166) hide show
  1. data/lib/rails/version.rb +2 -2
  2. metadata +305 -461
  3. data/guides/output/2_2_release_notes.html +0 -565
  4. data/guides/output/2_3_release_notes.html +0 -713
  5. data/guides/output/3_0_release_notes.html +0 -652
  6. data/guides/output/3_1_release_notes.html +0 -670
  7. data/guides/output/action_controller_overview.html +0 -925
  8. data/guides/output/action_mailer_basics.html +0 -658
  9. data/guides/output/action_view_overview.html +0 -1471
  10. data/guides/output/active_model_basics.html +0 -349
  11. data/guides/output/active_record_basics.html +0 -364
  12. data/guides/output/active_record_querying.html +0 -1272
  13. data/guides/output/active_record_validations_callbacks.html +0 -1292
  14. data/guides/output/active_resource_basics.html +0 -252
  15. data/guides/output/active_support_core_extensions.html +0 -3374
  16. data/guides/output/ajax_on_rails.html +0 -412
  17. data/guides/output/api_documentation_guidelines.html +0 -317
  18. data/guides/output/asset_pipeline.html +0 -691
  19. data/guides/output/association_basics.html +0 -1742
  20. data/guides/output/caching_with_rails.html +0 -533
  21. data/guides/output/command_line.html +0 -662
  22. data/guides/output/configuring.html +0 -811
  23. data/guides/output/contribute.html +0 -216
  24. data/guides/output/contributing_to_ruby_on_rails.html +0 -465
  25. data/guides/output/credits.html +0 -210
  26. data/guides/output/debugging_rails_applications.html +0 -791
  27. data/guides/output/engines.html +0 -673
  28. data/guides/output/form_helpers.html +0 -850
  29. data/guides/output/generators.html +0 -725
  30. data/guides/output/getting_started.html +0 -1980
  31. data/guides/output/i18n.html +0 -1054
  32. data/guides/output/images/belongs_to.png +0 -0
  33. data/guides/output/images/book_icon.gif +0 -0
  34. data/guides/output/images/bullet.gif +0 -0
  35. data/guides/output/images/challenge.png +0 -0
  36. data/guides/output/images/chapters_icon.gif +0 -0
  37. data/guides/output/images/check_bullet.gif +0 -0
  38. data/guides/output/images/credits_pic_blank.gif +0 -0
  39. data/guides/output/images/csrf.png +0 -0
  40. data/guides/output/images/customized_error_messages.png +0 -0
  41. data/guides/output/images/edge_badge.png +0 -0
  42. data/guides/output/images/error_messages.png +0 -0
  43. data/guides/output/images/feature_tile.gif +0 -0
  44. data/guides/output/images/footer_tile.gif +0 -0
  45. data/guides/output/images/fxn.png +0 -0
  46. data/guides/output/images/grey_bullet.gif +0 -0
  47. data/guides/output/images/habtm.png +0 -0
  48. data/guides/output/images/has_many.png +0 -0
  49. data/guides/output/images/has_many_through.png +0 -0
  50. data/guides/output/images/has_one.png +0 -0
  51. data/guides/output/images/has_one_through.png +0 -0
  52. data/guides/output/images/header_backdrop.png +0 -0
  53. data/guides/output/images/header_tile.gif +0 -0
  54. data/guides/output/images/i18n/demo_html_safe.png +0 -0
  55. data/guides/output/images/i18n/demo_localized_pirate.png +0 -0
  56. data/guides/output/images/i18n/demo_translated_en.png +0 -0
  57. data/guides/output/images/i18n/demo_translated_pirate.png +0 -0
  58. data/guides/output/images/i18n/demo_translation_missing.png +0 -0
  59. data/guides/output/images/i18n/demo_untranslated.png +0 -0
  60. data/guides/output/images/icons/README +0 -5
  61. data/guides/output/images/icons/callouts/1.png +0 -0
  62. data/guides/output/images/icons/callouts/10.png +0 -0
  63. data/guides/output/images/icons/callouts/11.png +0 -0
  64. data/guides/output/images/icons/callouts/12.png +0 -0
  65. data/guides/output/images/icons/callouts/13.png +0 -0
  66. data/guides/output/images/icons/callouts/14.png +0 -0
  67. data/guides/output/images/icons/callouts/15.png +0 -0
  68. data/guides/output/images/icons/callouts/2.png +0 -0
  69. data/guides/output/images/icons/callouts/3.png +0 -0
  70. data/guides/output/images/icons/callouts/4.png +0 -0
  71. data/guides/output/images/icons/callouts/5.png +0 -0
  72. data/guides/output/images/icons/callouts/6.png +0 -0
  73. data/guides/output/images/icons/callouts/7.png +0 -0
  74. data/guides/output/images/icons/callouts/8.png +0 -0
  75. data/guides/output/images/icons/callouts/9.png +0 -0
  76. data/guides/output/images/icons/caution.png +0 -0
  77. data/guides/output/images/icons/example.png +0 -0
  78. data/guides/output/images/icons/home.png +0 -0
  79. data/guides/output/images/icons/important.png +0 -0
  80. data/guides/output/images/icons/next.png +0 -0
  81. data/guides/output/images/icons/note.png +0 -0
  82. data/guides/output/images/icons/prev.png +0 -0
  83. data/guides/output/images/icons/tip.png +0 -0
  84. data/guides/output/images/icons/up.png +0 -0
  85. data/guides/output/images/icons/warning.png +0 -0
  86. data/guides/output/images/jaimeiniesta.jpg +0 -0
  87. data/guides/output/images/nav_arrow.gif +0 -0
  88. data/guides/output/images/polymorphic.png +0 -0
  89. data/guides/output/images/posts_index.png +0 -0
  90. data/guides/output/images/radar.png +0 -0
  91. data/guides/output/images/rails_guides_logo.gif +0 -0
  92. data/guides/output/images/rails_logo_remix.gif +0 -0
  93. data/guides/output/images/rails_welcome.png +0 -0
  94. data/guides/output/images/session_fixation.png +0 -0
  95. data/guides/output/images/tab_grey.gif +0 -0
  96. data/guides/output/images/tab_info.gif +0 -0
  97. data/guides/output/images/tab_note.gif +0 -0
  98. data/guides/output/images/tab_red.gif +0 -0
  99. data/guides/output/images/tab_yellow.gif +0 -0
  100. data/guides/output/images/tab_yellow.png +0 -0
  101. data/guides/output/images/validation_error_messages.png +0 -0
  102. data/guides/output/images/vijaydev.jpg +0 -0
  103. data/guides/output/index.html +0 -300
  104. data/guides/output/initialization.html +0 -1087
  105. data/guides/output/javascripts/guides.js +0 -7
  106. data/guides/output/javascripts/syntaxhighlighter/shBrushAS3.js +0 -59
  107. data/guides/output/javascripts/syntaxhighlighter/shBrushAppleScript.js +0 -75
  108. data/guides/output/javascripts/syntaxhighlighter/shBrushBash.js +0 -59
  109. data/guides/output/javascripts/syntaxhighlighter/shBrushCSharp.js +0 -65
  110. data/guides/output/javascripts/syntaxhighlighter/shBrushColdFusion.js +0 -100
  111. data/guides/output/javascripts/syntaxhighlighter/shBrushCpp.js +0 -97
  112. data/guides/output/javascripts/syntaxhighlighter/shBrushCss.js +0 -91
  113. data/guides/output/javascripts/syntaxhighlighter/shBrushDelphi.js +0 -55
  114. data/guides/output/javascripts/syntaxhighlighter/shBrushDiff.js +0 -41
  115. data/guides/output/javascripts/syntaxhighlighter/shBrushErlang.js +0 -52
  116. data/guides/output/javascripts/syntaxhighlighter/shBrushGroovy.js +0 -67
  117. data/guides/output/javascripts/syntaxhighlighter/shBrushJScript.js +0 -52
  118. data/guides/output/javascripts/syntaxhighlighter/shBrushJava.js +0 -57
  119. data/guides/output/javascripts/syntaxhighlighter/shBrushJavaFX.js +0 -58
  120. data/guides/output/javascripts/syntaxhighlighter/shBrushPerl.js +0 -72
  121. data/guides/output/javascripts/syntaxhighlighter/shBrushPhp.js +0 -88
  122. data/guides/output/javascripts/syntaxhighlighter/shBrushPlain.js +0 -33
  123. data/guides/output/javascripts/syntaxhighlighter/shBrushPowerShell.js +0 -74
  124. data/guides/output/javascripts/syntaxhighlighter/shBrushPython.js +0 -64
  125. data/guides/output/javascripts/syntaxhighlighter/shBrushRuby.js +0 -55
  126. data/guides/output/javascripts/syntaxhighlighter/shBrushSass.js +0 -94
  127. data/guides/output/javascripts/syntaxhighlighter/shBrushScala.js +0 -51
  128. data/guides/output/javascripts/syntaxhighlighter/shBrushSql.js +0 -66
  129. data/guides/output/javascripts/syntaxhighlighter/shBrushVb.js +0 -56
  130. data/guides/output/javascripts/syntaxhighlighter/shBrushXml.js +0 -69
  131. data/guides/output/javascripts/syntaxhighlighter/shCore.js +0 -17
  132. data/guides/output/layout.html +0 -312
  133. data/guides/output/layouts_and_rendering.html +0 -1257
  134. data/guides/output/migrations.html +0 -751
  135. data/guides/output/nested_model_forms.html +0 -350
  136. data/guides/output/performance_testing.html +0 -858
  137. data/guides/output/plugins.html +0 -590
  138. data/guides/output/rails_application_templates.html +0 -368
  139. data/guides/output/rails_on_rack.html +0 -408
  140. data/guides/output/routing.html +0 -1246
  141. data/guides/output/ruby_on_rails_guides_guidelines.html +0 -218
  142. data/guides/output/security.html +0 -968
  143. data/guides/output/stylesheets/fixes.css +0 -16
  144. data/guides/output/stylesheets/main.css +0 -445
  145. data/guides/output/stylesheets/print.css +0 -52
  146. data/guides/output/stylesheets/reset.css +0 -43
  147. data/guides/output/stylesheets/style.css +0 -13
  148. data/guides/output/stylesheets/syntaxhighlighter/shCore.css +0 -226
  149. data/guides/output/stylesheets/syntaxhighlighter/shCoreDefault.css +0 -328
  150. data/guides/output/stylesheets/syntaxhighlighter/shCoreDjango.css +0 -331
  151. data/guides/output/stylesheets/syntaxhighlighter/shCoreEclipse.css +0 -339
  152. data/guides/output/stylesheets/syntaxhighlighter/shCoreEmacs.css +0 -324
  153. data/guides/output/stylesheets/syntaxhighlighter/shCoreFadeToGrey.css +0 -328
  154. data/guides/output/stylesheets/syntaxhighlighter/shCoreMDUltra.css +0 -324
  155. data/guides/output/stylesheets/syntaxhighlighter/shCoreMidnight.css +0 -324
  156. data/guides/output/stylesheets/syntaxhighlighter/shCoreRDark.css +0 -324
  157. data/guides/output/stylesheets/syntaxhighlighter/shThemeDefault.css +0 -117
  158. data/guides/output/stylesheets/syntaxhighlighter/shThemeDjango.css +0 -120
  159. data/guides/output/stylesheets/syntaxhighlighter/shThemeEclipse.css +0 -128
  160. data/guides/output/stylesheets/syntaxhighlighter/shThemeEmacs.css +0 -113
  161. data/guides/output/stylesheets/syntaxhighlighter/shThemeFadeToGrey.css +0 -117
  162. data/guides/output/stylesheets/syntaxhighlighter/shThemeMDUltra.css +0 -113
  163. data/guides/output/stylesheets/syntaxhighlighter/shThemeMidnight.css +0 -113
  164. data/guides/output/stylesheets/syntaxhighlighter/shThemeRDark.css +0 -113
  165. data/guides/output/stylesheets/syntaxhighlighter/shThemeRailsGuides.css +0 -116
  166. data/guides/output/testing.html +0 -1182
@@ -1,673 +0,0 @@
1
- <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN"
2
- "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
3
-
4
- <html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en" lang="en">
5
- <head>
6
- <meta http-equiv="Content-Type" content="text/html; charset=utf-8"/>
7
-
8
- <title>Ruby on Rails Guides: Getting Started with Engines</title>
9
-
10
- <link rel="stylesheet" type="text/css" href="stylesheets/style.css" />
11
- <link rel="stylesheet" type="text/css" href="stylesheets/print.css" media="print" />
12
-
13
- <link rel="stylesheet" type="text/css" href="stylesheets/syntaxhighlighter/shCore.css" />
14
- <link rel="stylesheet" type="text/css" href="stylesheets/syntaxhighlighter/shThemeRailsGuides.css" />
15
-
16
- <link rel="stylesheet" type="text/css" href="stylesheets/fixes.css" />
17
- </head>
18
- <body class="guide">
19
- <div id="topNav">
20
- <div class="wrapper">
21
- <strong>More at <a href="http://rubyonrails.org/">rubyonrails.org:</a> </strong>
22
- <a href="http://rubyonrails.org/">Overview</a> |
23
- <a href="http://rubyonrails.org/download">Download</a> |
24
- <a href="http://rubyonrails.org/deploy">Deploy</a> |
25
- <a href="https://github.com/rails/rails">Code</a> |
26
- <a href="http://rubyonrails.org/screencasts">Screencasts</a> |
27
- <a href="http://rubyonrails.org/documentation">Documentation</a> |
28
- <a href="http://rubyonrails.org/ecosystem">Ecosystem</a> |
29
- <a href="http://rubyonrails.org/community">Community</a> |
30
- <a href="http://weblog.rubyonrails.org/">Blog</a>
31
- </div>
32
- </div>
33
- <div id="header">
34
- <div class="wrapper clearfix">
35
- <h1><a href="index.html" title="Return to home page">Guides.rubyonrails.org</a></h1>
36
- <p class="hide"><a href="#mainCol">Skip navigation</a>.</p>
37
- <ul class="nav">
38
- <li><a href="index.html">Home</a></li>
39
- <li class="index"><a href="index.html" onclick="guideMenu(); return false;" id="guidesMenu">Guides Index</a>
40
- <div id="guides" class="clearfix" style="display: none;">
41
- <hr />
42
- <dl class="L">
43
- <dt>Start Here</dt>
44
- <dd><a href="getting_started.html">Getting Started with Rails</a></dd>
45
- <dt>Models</dt>
46
- <dd><a href="migrations.html">Rails Database Migrations</a></dd>
47
- <dd><a href="active_record_validations_callbacks.html">Active Record Validations and Callbacks</a></dd>
48
- <dd><a href="association_basics.html">Active Record Associations</a></dd>
49
- <dd><a href="active_record_querying.html">Active Record Query Interface</a></dd>
50
- <dt>Views</dt>
51
- <dd><a href="layouts_and_rendering.html">Layouts and Rendering in Rails</a></dd>
52
- <dd><a href="form_helpers.html">Action View Form Helpers</a></dd>
53
- <dt>Controllers</dt>
54
- <dd><a href="action_controller_overview.html">Action Controller Overview</a></dd>
55
- <dd><a href="routing.html">Rails Routing from the Outside In</a></dd>
56
- </dl>
57
- <dl class="R">
58
- <dt>Digging Deeper</dt>
59
- <dd><a href="active_support_core_extensions.html">Active Support Core Extensions</a></dd>
60
- <dd><a href="i18n.html">Rails Internationalization API</a></dd>
61
- <dd><a href="action_mailer_basics.html">Action Mailer Basics</a></dd>
62
- <dd><a href="testing.html">Testing Rails Applications</a></dd>
63
- <dd><a href="security.html">Securing Rails Applications</a></dd>
64
- <dd><a href="debugging_rails_applications.html">Debugging Rails Applications</a></dd>
65
- <dd><a href="performance_testing.html">Performance Testing Rails Applications</a></dd>
66
- <dd><a href="configuring.html">Configuring Rails Applications</a></dd>
67
- <dd><a href="command_line.html">Rails Command Line Tools and Rake Tasks</a></dd>
68
- <dd><a href="caching_with_rails.html">Caching with Rails</a></dd>
69
- <dd><a href="asset_pipeline.html">Asset Pipeline</a></dd>
70
-
71
- <dt>Extending Rails</dt>
72
- <dd><a href="plugins.html">The Basics of Creating Rails Plugins</a></dd>
73
- <dd><a href="rails_on_rack.html">Rails on Rack</a></dd>
74
- <dd><a href="generators.html">Creating and Customizing Rails Generators</a></dd>
75
-
76
- <dt>Contributing to Ruby on Rails</dt>
77
- <dd><a href="contributing_to_ruby_on_rails.html">Contributing to Ruby on Rails</a></dd>
78
- <dd><a href="api_documentation_guidelines.html">API Documentation Guidelines</a></dd>
79
- <dd><a href="ruby_on_rails_guides_guidelines.html">Ruby on Rails Guides Guidelines</a></dd>
80
-
81
- <dt>Release Notes</dt>
82
- <dd><a href="3_1_release_notes.html">Ruby on Rails 3.1 Release Notes</a></dd>
83
- <dd><a href="3_0_release_notes.html">Ruby on Rails 3.0 Release Notes</a></dd>
84
- <dd><a href="2_3_release_notes.html">Ruby on Rails 2.3 Release Notes</a></dd>
85
- <dd><a href="2_2_release_notes.html">Ruby on Rails 2.2 Release Notes</a></dd>
86
- </dl>
87
- </div>
88
- </li>
89
- <li><a href="contributing_to_ruby_on_rails.html">Contribute</a></li>
90
- <li><a href="credits.html">Credits</a></li>
91
- </ul>
92
- </div>
93
- </div>
94
- <hr class="hide" />
95
-
96
- <div id="feature">
97
- <div class="wrapper">
98
- <h2>Getting Started with Engines</h2>
99
- <p>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. You will learn the following things in this guide:</p>
100
- <ul>
101
- <li>What makes an engine</li>
102
- <li>How to generate an engine</li>
103
- <li>Building features for the engine</li>
104
- <li>Hooking the engine into an application</li>
105
- <li>Overriding engine functionality in the application</li>
106
- </ul>
107
-
108
- <div id="subCol">
109
- <h3 class="chapter"><img src="images/chapters_icon.gif" alt="" />Chapters</h3>
110
- <ol class="chapters">
111
- <li><a href="#what-are-engines">What are engines?</a></li><li><a href="#generating-an-engine">Generating an engine</a><ul><li><a href="#inside-an-engine">Inside an engine</a></li></ul></li><li><a href="#providing-engine-functionality">Providing engine functionality</a><ul><li><a href="#generating-a-post-resource">Generating a post resource</a></li> <li><a href="#generating-a-comments-resource">Generating a comments resource</a></li></ul></li><li><a href="#hooking-into-an-application">Hooking into an application</a><ul><li><a href="#mounting-the-engine">Mounting the engine</a></li> <li><a href="#engine-setup">Engine setup</a></li> <li><a href="#using-a-class-provided-by-the-application">Using a class provided by the application</a></li> <li><a href="#configuring-an-engine">Configuring an engine</a></li></ul></li><li><a href="#extending-engine-functionality">Extending engine functionality</a><ul><li><a href="#overriding-views">Overriding views</a></li> <li><a href="#controllers">Controllers</a></li> <li><a href="#models">Models</a></li> <li><a href="#routes">Routes</a></li></ul></li></ol></div>
112
- </div>
113
- </div>
114
-
115
- <div id="container">
116
- <div class="wrapper">
117
- <div id="mainCol">
118
- <h3 id="what-are-engines">1 What are engines?</h3>
119
- <p>Engines can be considered miniature applications that provide functionality to their host applications. A Rails application is actually just a &#8220;supercharged&#8221; engine, with the <tt>Rails::Application</tt> class inheriting from <tt>Rails::Engine</tt>. Therefore, engines and applications share common functionality but are at the same time two separate beasts. Engines and applications also share a common structure, as you&#8217;ll see throughout this guide.</p>
120
- <p>Engines are also closely related to plugins where the two share a common <tt>lib</tt> directory structure and are both generated using the <tt>rails plugin new</tt> generator.</p>
121
- <p>The engine that will be generated for this guide will be called &#8220;blorgh&#8221;. The engine will provide blogging functionality to its host applications, allowing for new posts and comments to be created. For now, you will be working solely within the engine itself and in later sections you&#8217;ll see how to hook it into an application.</p>
122
- <p>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 <tt>posts_path</tt> and use an engine also that provides a path also called <tt>posts_path</tt>, and the two would not clash. Along with this, controllers, models and table names are also namespaced. You&#8217;ll see how to do this later in this guide.</p>
123
- <p>To see demonstrations of other engines, check out <a href="https://github.com/plataformatec/devise">Devise</a>, an engine that provides authentication for its parent applications, or <a href="https://github.com/radar/forem">Forem</a>, an engine that provides forum functionality.</p>
124
- <p>Finally, engines would not have be 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&#8217;t forget to say thanks!</p>
125
- <h3 id="generating-an-engine">2 Generating an engine</h3>
126
- <p>To generate an engine with Rails 3.1, you will need to run the plugin generator and pass it the <tt>--mountable</tt> option. To generate the beginnings of the &#8220;blorgh&#8221; engine you will need to run this command in a terminal:</p>
127
- <div class="code_container">
128
- <pre class="brush: plain; gutter: false; toolbar: false">
129
- $ rails plugin new blorgh --mountable
130
- </pre>
131
- </div>
132
- <p>The <tt>--mountable</tt> option tells the plugin generator that you want to create an engine (which is a mountable plugin, hence the option name), creating the basic directory structure of an engine by providing things such as the foundations of an <tt>app</tt> folder, as well a <tt>config/routes.rb</tt> file. This generator also provides a file at <tt>lib/blorgh/engine.rb</tt> which is identical in function to an application&#8217;s <tt>config/application.rb</tt> file.</p>
133
- <h4 id="inside-an-engine">2.1 Inside an engine</h4>
134
- <h5 id="critical-files">2.1.1 Critical files</h5>
135
- <p>At the root of the engine&#8217;s directory, lives a <tt>blorgh.gemspec</tt> file. When you include the engine into the application later on, you will do so with this line in a Rails application&#8217;s <tt>Gemfile</tt>:</p>
136
- <div class="code_container">
137
- <pre class="brush: ruby; gutter: false; toolbar: false">
138
- gem 'blorgh', :path =&gt; &quot;vendor/engines/blorgh&quot;
139
- </pre>
140
- </div>
141
- <p>By specifying it as a gem within the <tt>Gemfile</tt>, Bundler will load it as such, parsing this <tt>blorgh.gemspec</tt> file and requiring a file within the <tt>lib</tt> directory called <tt>lib/blorgh.rb</tt>. This file requires the <tt>blorgh/engine.rb</tt> file (located at <tt>lib/blorgh/engine.rb</tt>) and defines a base module called <tt>Blorgh</tt>.</p>
142
- <div class="code_container">
143
- <pre class="brush: ruby; gutter: false; toolbar: false">
144
- require &quot;blorgh/engine&quot;
145
-
146
- module Blorgh
147
- end
148
- </pre>
149
- </div>
150
- <p>Within <tt>lib/blorgh/engine.rb</tt> is the base class for the engine:</p>
151
- <div class="code_container">
152
- <pre class="brush: ruby; gutter: false; toolbar: false">
153
- module Blorgh
154
- class Engine &lt; Rails::Engine
155
- isolate_namespace Blorgh
156
- end
157
- end
158
- </pre>
159
- </div>
160
- <p>By inheriting from the <tt>Rails::Engine</tt> class, this engine gains all the functionality it needs, such as being able to serve requests to its controllers.</p>
161
- <p>The <tt>isolate_namespace</tt> method here deserves special notice. This call is responsible for isolating the controllers, models, routes and other things into their own namespace. Without this, there is a possibility that the engine&#8217;s components could &#8220;leak&#8221; into the application, causing unwanted disruption. It is recommended that this line be left within this file.</p>
162
- <h5 id="app-directory">2.1.2 <tt>app</tt> directory</h5>
163
- <p>Inside the <tt>app</tt> directory there lives the standard <tt>assets</tt>, <tt>controllers</tt>, <tt>helpers</tt>, <tt>mailers</tt>, <tt>models</tt> and <tt>views</tt> directories that you should be familiar with from an application. The <tt>helpers</tt>, <tt>mailers</tt> and <tt>models</tt> directories are empty and so aren&#8217;t described in this section. We&#8217;ll look more into models in a future section.</p>
164
- <p>Within the <tt>app/assets</tt> directory, there is the <tt>images</tt>, <tt>javascripts</tt> and <tt>stylesheets</tt> directories which, again, you should be familiar with due to their similarities of 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.</p>
165
- <p>Within the <tt>app/controllers</tt> directory there is a <tt>blorgh</tt> directory and inside that a file called <tt>application_controller.rb</tt>. This file will provide any common functionality for the controllers of the engine. The <tt>blorgh</tt> 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.</p>
166
- <p>Lastly, the <tt>app/views</tt> directory contains a <tt>layouts</tt> folder which contains file at <tt>blorgh/application.html.erb</tt> which 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 applications <tt>app/views/layouts/application.html.erb</tt> file.</p>
167
- <h5 id="script-directory">2.1.3 <tt>script</tt> directory</h5>
168
- <p>This directory contains one file, <tt>script/rails</tt>, which allows you to use the <tt>rails</tt> sub-commands and generators just like you would within an application. This means that you will very easily be able to generate new controllers and models for this engine.</p>
169
- <h5 id="test-directory">2.1.4 <tt>test</tt> directory</h5>
170
- <p>The <tt>test</tt> 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 <tt>test/dummy</tt>. This application will mount the engine in the <tt>test/dummy/config/routes.rb</tt> file:</p>
171
- <div class="code_container">
172
- <pre class="brush: ruby; gutter: false; toolbar: false">
173
- Rails.application.routes.draw do
174
-
175
- mount Blorgh::Engine =&gt; &quot;/blorgh&quot;
176
- end
177
- </pre>
178
- </div>
179
- <p>This line mounts the engine at the path of <tt>/blorgh</tt>, which will make it accessible through the application only at that path. We will look more into mounting an engine after some features have been developed.</p>
180
- <p>Also in the test directory is the <tt>test/integration</tt> directory, where integration tests for the engine should be placed.</p>
181
- <h3 id="providing-engine-functionality">3 Providing engine functionality</h3>
182
- <p>The engine that this guide covers will provide posting and commenting functionality and follows a similar thread to the <a href="getting-started.html">Getting Started Guide</a>, with some new twists.</p>
183
- <h4 id="generating-a-post-resource">3.1 Generating a post resource</h4>
184
- <p>The first thing to generate for a blog engine is the <tt>Post</tt> model and related controller. To quickly generate this, you can use the Rails scaffold generator.</p>
185
- <div class="code_container">
186
- <pre class="brush: plain; gutter: false; toolbar: false">
187
- $ rails generate scaffold post title:string text:text
188
- </pre>
189
- </div>
190
- <p>This command will output this information:</p>
191
- <div class="code_container">
192
- <pre class="brush: plain; gutter: false; toolbar: false">
193
- invoke active_record
194
- create db/migrate/[timestamp]_create_blorgh_posts.rb
195
- create app/models/blorgh/post.rb
196
- invoke test_unit
197
- create test/unit/blorgh/post_test.rb
198
- create test/fixtures/blorgh/posts.yml
199
- route resources :posts
200
- invoke scaffold_controller
201
- create app/controllers/blorgh/posts_controller.rb
202
- invoke erb
203
- create app/views/blorgh/posts
204
- create app/views/blorgh/posts/index.html.erb
205
- create app/views/blorgh/posts/edit.html.erb
206
- create app/views/blorgh/posts/show.html.erb
207
- create app/views/blorgh/posts/new.html.erb
208
- create app/views/blorgh/posts/_form.html.erb
209
- invoke test_unit
210
- create test/functional/blorgh/posts_controller_test.rb
211
- invoke helper
212
- create app/helpers/blorgh/posts_helper.rb
213
- invoke test_unit
214
- create test/unit/helpers/blorgh/posts_helper_test.rb
215
- invoke assets
216
- invoke js
217
- create app/assets/javascripts/blorgh/posts.js
218
- invoke css
219
- create app/assets/stylesheets/blorgh/posts.css
220
- invoke css
221
- create app/assets/stylesheets/scaffold.css
222
- </pre>
223
- </div>
224
- <p>The first thing that the scaffold generator does is invoke the <tt>active_record</tt> generator, which generates a migration and a model for the resource. Note here, however, that the migration is called <tt>create_blorgh_posts</tt> rather than the usual <tt>create_posts</tt>. This is due to the <tt>isolate_namespace</tt> method called in the <tt>Blorgh::Engine</tt> class&#8217;s definition. The model here is also namespaced, being placed at <tt>app/models/blorgh/post.rb</tt> rather than <tt>app/models/post.rb</tt>.</p>
225
- <p>Next, the <tt>test_unit</tt> generator is invoked for this model, generating a unit test at <tt>test/unit/blorgh/post_test.rb</tt> (rather than <tt>test/unit/post_test.rb</tt>) and a fixture at <tt>test/fixtures/blorgh/posts.yml</tt> (rather than <tt>test/fixtures/posts.yml</tt>).</p>
226
- <p>After that, a line for the resource is inserted into the <tt>config/routes.rb</tt> file for the engine. This line is simply <tt>resources :posts</tt>, turning the <tt>config/routes.rb</tt> file into this:</p>
227
- <div class="code_container">
228
- <pre class="brush: ruby; gutter: false; toolbar: false">
229
- Blorgh::Engine.routes.draw do
230
- resources :posts
231
-
232
- end
233
- </pre>
234
- </div>
235
- <p>Note here that the routes are drawn upon the <tt>Blorgh::Engine</tt> object rather than the <tt>YourApp::Application</tt> 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 <a href="#test-directory">test directory</a> section.</p>
236
- <p>Next, the <tt>scaffold_controller</tt> generator is invoked, generating a controlled called <tt>Blorgh::PostsController</tt> (at <tt>app/controllers/blorgh/posts_controller.rb</tt>) and its related views at <tt>app/views/blorgh/posts</tt>. This generator also generates a functional test for the controller (<tt>test/functional/blorgh/posts_controller_test.rb</tt>) and a helper (<tt>app/helpers/blorgh/posts_controller.rb</tt>).</p>
237
- <p>Everything this generator has generated is neatly namespaced. The controller&#8217;s class is defined within the <tt>Blorgh</tt> module:</p>
238
- <div class="code_container">
239
- <pre class="brush: ruby; gutter: false; toolbar: false">
240
- module Blorgh
241
- class PostsController &lt; ApplicationController
242
- ...
243
- end
244
- end
245
- </pre>
246
- </div>
247
- <div class="note"><p>The <tt>ApplicationController</tt> class being inherited from here is the <tt>Blorgh::ApplicationController</tt>, not an application&#8217;s <tt>ApplicationController</tt>.</p></div>
248
- <p>The helper is also namespaced:</p>
249
- <div class="code_container">
250
- <pre class="brush: ruby; gutter: false; toolbar: false">
251
- module Blorgh
252
- class PostsHelper
253
- ...
254
- end
255
- end
256
- </pre>
257
- </div>
258
- <p>This helps prevent conflicts with any other engine or application that may have a post resource also.</p>
259
- <p>Finally, two files that are the assets for this resource are generated, <tt>app/assets/javascripts/blorgh/posts.js</tt> and <tt>app/assets/javascripts/blorgh/posts.css</tt>. You&#8217;ll see how to use these a little later.</p>
260
- <p>By default, the scaffold styling is not applied to the engine as the engine&#8217;s layout file, <tt>app/views/blorgh/application.html.erb</tt> doesn&#8217;t load it. To make this apply, insert this line into the <tt><head></tt> tag of this layout:</p>
261
- <div class="code_container">
262
- <pre class="brush: ruby; html-script: true; gutter: false; toolbar: false">
263
- &lt;%= stylesheet_link_tag &quot;scaffold&quot; %&gt;
264
- </pre>
265
- </div>
266
- <p>You can see what the engine has so far by running <tt>rake db:migrate</tt> at the root of our engine to run the migration generated by the scaffold generator, and then running <tt>rails server</tt>. When you open <tt>http://localhost:3000/blorgh/posts</tt> you will see the default scaffold that has been generated.</p>
267
- <p><img src="images/engines_scaffold.png" title="Blank engine scaffold" alt="Blank engine scaffold" /></p>
268
- <p>Click around! You&#8217;ve just generated your first engine&#8217;s first functions.</p>
269
- <p>If you&#8217;d rather play around in the console, <tt>rails console</tt> will also work just like a Rails application. Remember: the <tt>Post</tt> model is namespaced, so to reference it you must call it as <tt>Blorgh::Post</tt>.</p>
270
- <div class="code_container">
271
- <pre class="brush: ruby; gutter: false; toolbar: false">
272
- &gt;&gt; Blorgh::Post.find(1)
273
- =&gt; #&lt;Blorgh::Post id: 1 ...&gt;
274
- </pre>
275
- </div>
276
- <p>One final thing is that the <tt>posts</tt> resource for this engine should be the root of the engine. Whenever someone goes to the root path where the engine is mounted, they should be shown a list of posts. This can be made to happen if this line is inserted into the <tt>config/routes.rb</tt> file inside the engine:</p>
277
- <div class="code_container">
278
- <pre class="brush: ruby; gutter: false; toolbar: false">
279
- root :to =&gt; &quot;posts#index&quot;
280
- </pre>
281
- </div>
282
- <p>Now people will only need to go to the root of the engine to see all the posts, rather than visiting <tt>/posts</tt>.</p>
283
- <h4 id="generating-a-comments-resource">3.2 Generating a comments resource</h4>
284
- <p>Now that the engine has the ability to create new blog posts, it only makes sense to add commenting functionality as well. To do get this, you&#8217;ll need to generate a comment model, a comment controller and then modify the posts scaffold to display comments and allow people to create new ones.</p>
285
- <p>Run the model generator and tell it to generate a <tt>Comment</tt> model, with the related table having two columns: a <tt>post_id</tt> integer and <tt>text</tt> text column.</p>
286
- <div class="code_container">
287
- <pre class="brush: plain; gutter: false; toolbar: false">
288
- $ rails generate model Comment post_id:integer text:text
289
- </pre>
290
- </div>
291
- <p>This will output the following:</p>
292
- <div class="code_container">
293
- <pre class="brush: plain; gutter: false; toolbar: false">
294
- invoke active_record
295
- create db/migrate/[timestamp]_create_blorgh_comments.rb
296
- create app/models/blorgh/comment.rb
297
- invoke test_unit
298
- create test/unit/blorgh/comment_test.rb
299
- create test/fixtures/blorgh/comments.yml
300
- </pre>
301
- </div>
302
- <p>This generator call will generate just the necessary model files it needs, namespacing the files under a <tt>blorgh</tt> directory and creating a model class called <tt>Blorgh::Comment</tt>.</p>
303
- <p>To show the comments on a post, edit <tt>app/views/posts/show.html.erb</tt> and add this line before the &#8220;Edit&#8221; link:</p>
304
- <div class="code_container">
305
- <pre class="brush: ruby; html-script: true; gutter: false; toolbar: false">
306
- &lt;h3&gt;Comments&lt;/h3&gt;
307
- &lt;%= render @post.comments %&gt;
308
- </pre>
309
- </div>
310
- <p>This line will require there to be a <tt>has_many</tt> association for comments defined on the <tt>Blorgh::Post</tt> model, which there isn&#8217;t right now. To define one, open <tt>app/models/blorgh/post.rb</tt> and add this line into the model:</p>
311
- <div class="code_container">
312
- <pre class="brush: ruby; gutter: false; toolbar: false">
313
- has_many :comments
314
- </pre>
315
- </div>
316
- <p>Turning the model into this:</p>
317
- <div class="code_container">
318
- <pre class="brush: ruby; gutter: false; toolbar: false">
319
- module Blorgh
320
- class Post &lt; ActiveRecord::Base
321
- has_many :comments
322
- end
323
- end
324
- </pre>
325
- </div>
326
- <p>Because the <tt>has_many</tt> is defined inside a class that is inside the <tt>Blorgh</tt> module, Rails will know that you want to use the <tt>Blorgh::Comment</tt> model for these objects.</p>
327
- <p>Next, there needs to be a form so that comments can be created on a post. To add this, put this line underneath the call to <tt>render @post.comments</tt> in <tt>app/views/blorgh/posts/show.html.erb</tt>:</p>
328
- <div class="code_container">
329
- <pre class="brush: ruby; html-script: true; gutter: false; toolbar: false">
330
- &lt;%= render &quot;blorgh/comments/form&quot; %&gt;
331
- </pre>
332
- </div>
333
- <p>Next, the partial that this line will render needs to exist. Create a new directory at <tt>app/views/blorgh/comments</tt> and in it a new file called <tt>_form.html.erb</tt> which has this content to create the required partial:</p>
334
- <div class="code_container">
335
- <pre class="brush: ruby; html-script: true; gutter: false; toolbar: false">
336
- &lt;h3&gt;New comment&lt;/h3&gt;
337
- &lt;%= form_for [@post, @post.comments.build] do |f| %&gt;
338
- &lt;p&gt;
339
- &lt;%= f.label :text %&gt;&lt;br /&gt;
340
- &lt;%= f.text_area :text %&gt;
341
- &lt;/p&gt;
342
- &lt;%= f.submit %&gt;
343
- &lt;% end %&gt;
344
- </pre>
345
- </div>
346
- <p>This form, when submitted, is going to attempt to post to a route of <tt>posts/:post_id/comments</tt> within the engine. This route doesn&#8217;t exist at the moment, but can be created by changing the <tt>resources :posts</tt> line inside <tt>config/routes.rb</tt> into these lines:</p>
347
- <div class="code_container">
348
- <pre class="brush: ruby; gutter: false; toolbar: false">
349
- resources :posts do
350
- resources :comments
351
- end
352
- </pre>
353
- </div>
354
- <p>The route now will exist, but the controller that this route goes to does not. To create it, run this command:</p>
355
- <div class="code_container">
356
- <pre class="brush: plain; gutter: false; toolbar: false">
357
- $ rails g controller comments
358
- </pre>
359
- </div>
360
- <p>This will generate the following things:</p>
361
- <div class="code_container">
362
- <pre class="brush: plain; gutter: false; toolbar: false">
363
- create app/controllers/blorgh/comments_controller.rb
364
- invoke erb
365
- exist app/views/blorgh/comments
366
- invoke test_unit
367
- create test/functional/blorgh/comments_controller_test.rb
368
- invoke helper
369
- create app/helpers/blorgh/comments_helper.rb
370
- invoke test_unit
371
- create test/unit/helpers/blorgh/comments_helper_test.rb
372
- invoke assets
373
- invoke js
374
- create app/assets/javascripts/blorgh/comments.js
375
- invoke css
376
- create app/assets/stylesheets/blorgh/comments.css
377
- </pre>
378
- </div>
379
- <p>The form will be making a <tt>POST</tt> request to <tt>/posts/:post_id/comments</tt>, which will correspond with the <tt>create</tt> action in <tt>Blorgh::CommentsController</tt>. This action needs to be created and can be done by putting the following lines inside the class definition in <tt>app/controllers/blorgh/comments_controller.rb</tt>:</p>
380
- <div class="code_container">
381
- <pre class="brush: ruby; gutter: false; toolbar: false">
382
- def create
383
- @post = Post.find(params[:post_id])
384
- @comment = @post.comments.build(params[:comment])
385
- flash[:notice] = &quot;Comment has been created!&quot;
386
- redirect_to post_path
387
- end
388
- </pre>
389
- </div>
390
- <p>This is the final part 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:</p>
391
- <text>
392
- Missing partial blorgh/comments/comment with {:handlers=&gt;[:erb, :builder], :formats=&gt;[:html], :locale=&gt;[:en, :en]}. Searched in:
393
- <ul>
394
- <li>&#8220;/Users/ryan/Sites/side_projects/blorgh/test/dummy/app/views&#8221;</li>
395
- <li>&#8220;/Users/ryan/Sites/side_projects/blorgh/app/views&#8221;
396
- </text></li>
397
- </ul>
398
- <p>The engine is unable to find the partial required for rendering the comments. Rails has looked firstly in the application&#8217;s (<tt>test/dummy</tt>) <tt>app/views</tt> directory and then in the engine&#8217;s <tt>app/views</tt> directory. When it can&#8217;t find it, it will throw this error. The engine knows to look for <tt>blorgh/comments/comment</tt> because the model object it is receiving is from the <tt>Blorgh::Comment</tt> class.</p>
399
- <p>This partial will be responsible for rendering just the comment text, for now. Create a new file at <tt>app/views/blorgh/comments/_comment.html.erb</tt> and put this line inside it:</p>
400
- <div class="code_container">
401
- <pre class="brush: ruby; html-script: true; gutter: false; toolbar: false">
402
- &lt;%= comment_counter + 1 %&gt;. &lt;%= comment.text %&gt;
403
- </pre>
404
- </div>
405
- <p>The <tt>comment_counter</tt> local variable is given to us by the <tt><%= render @post.comments %></tt> call, as it will define this automatically and increment the counter as it iterates through each comment. It&#8217;s used in this example to display a small number next to each comment when it&#8217;s created.</p>
406
- <p>That completes the comment function of the blogging engine. Now it&#8217;s time to use it within an application.</p>
407
- <h3 id="hooking-into-an-application">4 Hooking into an application</h3>
408
- <p>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 for it, as well as linking the engine to a <tt>User</tt> class provided by the application to provide ownership for posts and comments within the engine.</p>
409
- <h4 id="mounting-the-engine">4.1 Mounting the engine</h4>
410
- <p>First, the engine needs to be specified inside the application&#8217;s <tt>Gemfile</tt>. If there isn&#8217;t an application handy to test this out in, generate one using the <tt>rails new</tt> command outside of the engine directory like this:</p>
411
- <div class="code_container">
412
- <pre class="brush: plain; gutter: false; toolbar: false">
413
- $ rails new unicorn
414
- </pre>
415
- </div>
416
- <p>Usually, specifying the engine inside the Gemfile would be done by specifying it as a normal, everyday gem.</p>
417
- <div class="code_container">
418
- <pre class="brush: ruby; gutter: false; toolbar: false">
419
- gem 'devise'
420
- </pre>
421
- </div>
422
- <p>Because the <tt>blorgh</tt> engine is still under development, it will need to have a <tt>:path</tt> option for its <tt>Gemfile</tt> specification:</p>
423
- <div class="code_container">
424
- <pre class="brush: ruby; gutter: false; toolbar: false">
425
- gem 'blorgh', :path =&gt; &quot;/path/to/blorgh&quot;
426
- </pre>
427
- </div>
428
- <p>If the whole <tt>blorgh</tt> engine directory is copied to <tt>vendor/engines/blorgh</tt> then it could be specified in the <tt>Gemfile</tt> like this:</p>
429
- <div class="code_container">
430
- <pre class="brush: ruby; gutter: false; toolbar: false">
431
- gem 'blorgh', :path =&gt; &quot;vendor/engines/blorgh&quot;
432
- </pre>
433
- </div>
434
- <p>As described earlier, by placing the gem in the <tt>Gemfile</tt> it will be loaded when Rails is loaded, as it will first require <tt>lib/blorgh.rb</tt> in the engine and then <tt>lib/blorgh/engine.rb</tt>, which is the file that defines the major pieces of functionality for the engine.</p>
435
- <p>To make the engine&#8217;s functionality accessible from within an application, it needs to be mounted in that application&#8217;s <tt>config/routes.rb</tt> file:</p>
436
- <div class="code_container">
437
- <pre class="brush: ruby; gutter: false; toolbar: false">
438
- mount Blorgh::Engine, :at =&gt; &quot;blog&quot;
439
- </pre>
440
- </div>
441
- <p>This line will mount the engine at <tt>blog</tt> in the application. Making it accessible at <tt>http://localhost:3000/blog</tt> when the application runs with <tt>rails s</tt>.</p>
442
- <div class="note"><p>Other engines, such as Devise, handle this a little differently by making you specify custom helpers such as <tt>devise_for</tt> in the routes. These helpers do exactly the same thing, mounting pieces of the engines&#8217;s functionality at a pre-defined path which may be customizable.</p></div>
443
- <h4 id="engine-setup">4.2 Engine setup</h4>
444
- <p>The engine contains migrations for the <tt>blorgh_posts</tt> and <tt>blorgh_comments</tt> table which need to be created in the application&#8217;s database so that the engine&#8217;s models can query them correctly. To copy these migrations into the application use this command:</p>
445
- <div class="code_container">
446
- <pre class="brush: plain; gutter: false; toolbar: false">
447
- $ rake blorgh:install:migrations
448
- </pre>
449
- </div>
450
- <p>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&#8217;t been copied over already. The first run for this command will output something such as this:</p>
451
- <div class="code_container">
452
- <pre class="brush: plain; gutter: false; toolbar: false">
453
- Copied migration [timestamp_1]_create_blorgh_posts.rb from blorgh
454
- Copied migration [timestamp_2]_create_blorgh_comments.rb from blorgh
455
- </pre>
456
- </div>
457
- <p>The first timestamp (<tt>\[timestamp_1\]</tt>) will be the current time and the second timestamp (<tt>\[timestamp_2\]</tt>) 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.</p>
458
- <p>To run these migrations within the context of the application, simply run <tt>rake db:migrate</tt>. When accessing the engine through <tt>http://localhost:3000/blog</tt>, the posts will be empty. This is because the table created inside the application is different from the one created within the engine. Go ahead, play around with the newly mounted engine. You&#8217;ll find that it&#8217;s the same as when it was only an engine.</p>
459
- <h4 id="using-a-class-provided-by-the-application">4.3 Using a class provided by the application</h4>
460
- <p>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 the application. In the case of the <tt>blorgh</tt> engine, making posts and comments have authors would make a lot of sense.</p>
461
- <p>Usually, an application would have a <tt>User</tt> class that would provide the objects that would represent the posts&#8217; and comments&#8217; authors, but there could be a case where the application calls this class something different, such as <tt>Person</tt>. It&#8217;s because of this reason that the engine should not hardcode the associations to be exactly for a <tt>User</tt> class, but should allow for some flexibility around what the class is called.</p>
462
- <p>To keep it simple in this case, the application will have a class called <tt>User</tt> which will represent the users of the application. It can be generated using this command:</p>
463
- <div class="code_container">
464
- <pre class="brush: plain; gutter: false; toolbar: false">
465
- rails g model user name:string
466
- </pre>
467
- </div>
468
- <p>The <tt>rake db:migrate</tt> command needs to be run here to ensure that our application has the <tt>users</tt> table for future use.</p>
469
- <p>Also to keep it simple, the posts form will have a new text field called <tt>author_name_</tt> where users can elect to put their name. The engine will then take this name and create a new <tt>User</tt> object from it or find one that already has that name, and then associate the post with it.</p>
470
- <p>First, the <tt>author_name</tt> text field needs to be added to the <tt>app/views/blorgh/posts/_form.html.erb</tt> partial inside the engine. This can be added above the <tt>title</tt> field with this code:</p>
471
- <div class="code_container">
472
- <pre class="brush: ruby; html-script: true; gutter: false; toolbar: false">
473
- &lt;div class=&quot;field&quot;&gt;
474
- &lt;%= f.label :author_name %&gt;&lt;br /&gt;
475
- &lt;%= f.text_field :author_name %&gt;
476
- &lt;/div&gt;
477
- </pre>
478
- </div>
479
- <p>The <tt>Blorgh::Post</tt> model should then have some code to convert the <tt>author_name</tt> field into an actual <tt>User</tt> object and associate it as that post&#8217;s <tt>author</tt> before the post is saved. It will also need to have an <tt>attr_accessor</tt> setup for this field so that the setter and getter methods are defined for it.</p>
480
- <p>To do all this, you&#8217;ll need to add the <tt>attr_accessor</tt> for <tt>author_name</tt>, the association for the author and the <tt>before_save</tt> call into <tt>app/models/blorgh/post.rb</tt>. The <tt>author</tt> association will be hard-coded to the <tt>User</tt> class for the time being.</p>
481
- <div class="code_container">
482
- <pre class="brush: ruby; gutter: false; toolbar: false">
483
- attr_accessor :author_name
484
- belongs_to :author, :class_name =&gt; &quot;User&quot;
485
-
486
- before_save :set_author
487
-
488
- private
489
- def set_author
490
- self.author = User.find_or_create_by_name(author_name)
491
- end
492
- </pre>
493
- </div>
494
- <p>By defining that the <tt>author</tt> association&#8217;s object is represented by the <tt>User</tt> class a link is established between the engine and the application. There needs to be a way of associating the records in the <tt>blorgh_posts</tt> table with the records in the <tt>users</tt> table. Because the association is called <tt>author</tt>, there should be an <tt>author_id</tt> column added to the <tt>blorgh_posts</tt> table.</p>
495
- <p>To generate this new column, run this command within the engine:</p>
496
- <div class="code_container">
497
- <pre class="brush: plain; gutter: false; toolbar: false">
498
- $ rails g migration add_author_id_to_blorgh_posts author_id:integer
499
- </pre>
500
- </div>
501
- <div class="note"><p>Due to the migration&#8217;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&#8217;t need to tell it any more than this.</p></div>
502
- <p>This migration will need to be run on the application. To do that, it must first be copied using this command:</p>
503
- <div class="code_container">
504
- <pre class="brush: plain; gutter: false; toolbar: false">
505
- $ rake blorgh:install:migrations
506
- </pre>
507
- </div>
508
- <p>Notice here that only <em>one</em> migration was copied over here. This is because the first two migrations were copied over the first time this command was run.</p>
509
- <div class="code_container">
510
- <pre class="brush: plain; gutter: false; toolbar: false">
511
- NOTE: Migration [timestamp]_create_blorgh_posts.rb from blorgh has been skipped. Migration with the same name already exists.
512
- NOTE: Migration [timestamp]_create_blorgh_comments.rb from blorgh has been skipped. Migration with the same name already exists.
513
- Copied migration [timestamp]_add_author_id_to_blorgh_posts.rb from blorgh
514
- </pre>
515
- </div>
516
- <p>Run this migration using this command:</p>
517
- <div class="code_container">
518
- <pre class="brush: plain; gutter: false; toolbar: false">
519
- $ rake db:migrate
520
- </pre>
521
- </div>
522
- <p>Now with all the pieces in place, an action will take place that will associate an author &#8212; represented by a record in the <tt>users</tt> table &#8212; with a post, represented by the <tt>blorgh_posts</tt> table from the engine.</p>
523
- <p>Finally, the author&#8217;s name should be displayed on the post&#8217;s page. Add this code above the &#8220;Title&#8221; output inside <tt>app/views/blorgh/posts/show.html.erb</tt>:</p>
524
- <div class="code_container">
525
- <pre class="brush: ruby; html-script: true; gutter: false; toolbar: false">
526
- &lt;p&gt;
527
- &lt;b&gt;Author:&lt;/b&gt;
528
- &lt;%= @post.author %&gt;
529
- &lt;/p&gt;
530
- </pre>
531
- </div>
532
- <div class="warning"><p>For posts created previously, this will break the <tt>show</tt> page for them. We recommend deleting these posts and starting again, or manually assigning an author using <tt>rails c</tt>.</p></div>
533
- <p>By outputting <tt>@post.author</tt> using the <tt><%=</tt> tag the <tt>to_s</tt> method will be called on the object. By default, this will look quite ugly:</p>
534
- <text>
535
- <p>#<User:0x00000100ccb3b0></p>
536
- </text>
537
- <p>This is undesirable and it would be much better to have the user&#8217;s name there. To do this, add a <tt>to_s</tt> method to the <tt>User</tt> class within the application:</p>
538
- <div class="code_container">
539
- <pre class="brush: ruby; gutter: false; toolbar: false">
540
- def to_s
541
- name
542
- end
543
- </pre>
544
- </div>
545
- <p>Now instead of the ugly Ruby object output the author&#8217;s name will be displayed.</p>
546
- <h4 id="configuring-an-engine">4.4 Configuring an engine</h4>
547
- <p>The next step is to make the class that represents a <tt>User</tt> in the application customizable for the engine. This is because, as explained before, that class may not always be <tt>User</tt>. To make this customizable, the engine will have a configuration setting called <tt>user_class</tt> that will be used to specify what the class representing users is inside the application.</p>
548
- <p>To define this configuration setting, you should use a <tt>mattr_accessor</tt> inside the <tt>Blorgh</tt> module for the engine, located at <tt>lib/blorgh.rb</tt> inside the engine. Inside this module, put this line:</p>
549
- <div class="code_container">
550
- <pre class="brush: ruby; gutter: false; toolbar: false">
551
- mattr_accessor :user_class
552
- </pre>
553
- </div>
554
- <p>This method works like its brothers <tt>attr_accessor</tt> and <tt>cattr_accessor</tt>, but provides a setter and getter method on the module with the specified name. To use it, it must be referenced using <tt>Blorgh.user_class</tt>.</p>
555
- <p>The next step is switching the <tt>Blorgh::Post</tt> model over to this new setting. For the <tt>belongs_to</tt> association inside this model (<tt>app/models/blorgh/post.rb</tt>), it will now become this:</p>
556
- <div class="code_container">
557
- <pre class="brush: ruby; gutter: false; toolbar: false">
558
- belongs_to :author, :class_name =&gt; Blorgh.user_class
559
- </pre>
560
- </div>
561
- <p>The <tt>set_author</tt> method also located in this class should also use this class:</p>
562
- <div class="code_container">
563
- <pre class="brush: ruby; gutter: false; toolbar: false">
564
- self.author = Blorgh.user_class.constantize.find_or_create_by_name(author_name)
565
- </pre>
566
- </div>
567
- <p>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 makes references to the classes of the engine which may depend on this configuration setting existing.</p>
568
- <p>Create a new initializer at <tt>config/initializers/blorgh.rb</tt> inside the application where the <tt>blorgh</tt> engine is installed and put this content in it:</p>
569
- <div class="code_container">
570
- <pre class="brush: ruby; gutter: false; toolbar: false">
571
- Blorgh.user_class = &quot;User&quot;
572
- </pre>
573
- </div>
574
- <div class="warning"><p>It&#8217;s very important here to use the <tt>String</tt> 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, which could lead to problems if the table wasn&#8217;t already existing. Therefore, a <tt>String</tt> should be used and then converted to a class using <tt>constantize</tt> in the engine later on.</p></div>
575
- <p>Go ahead and try to create a new post. You will see that it works exactly in the same way as before, except this time the engine is using the configuration setting in <tt>config/initializers/blorgh.rb</tt> to learn what the class is.</p>
576
- <p>There are now no strict dependencies on what the class is, only what the class&#8217;s <span class="caps">API</span> must be. The engine simply requires this class to define a <tt>find_or_create_by_name</tt> method which returns an object of that class to be associated with a post when it&#8217;s created.</p>
577
- <h3 id="extending-engine-functionality">5 Extending engine functionality</h3>
578
- <p>This section looks at overriding or adding functionality to the views, controllers and models provided by an engine.</p>
579
- <h4 id="overriding-views">5.1 Overriding views</h4>
580
- <p>When Rails looks for a view to render, it will first look in the <tt>app/views</tt> directory of the application. If it cannot find the view there, then it will check in the <tt>app/views</tt> directories of all engines which have this directory.</p>
581
- <p>In the <tt>blorgh</tt> engine, there is a currently a file at <tt>app/views/blorgh/posts/index.html.erb</tt>. When the engine is asked to render the view for <tt>Blorgh::PostsController</tt>&#8217;s <tt>index</tt> action, it will first see if it can find it at <tt>app/views/blorgh/posts/index.html.erb</tt> within the application and then if it cannot it will look inside the engine.</p>
582
- <p>By overriding this view in the application, by simply creating a new file at <tt>app/views/blorgh/posts/index.html.erb</tt>, you can completely change what this view would normally output.</p>
583
- <p>Try this now by creating a new file at <tt>app/views/blorgh/posts/index.html.erb</tt> and put this content in it:</p>
584
- <div class="code_container">
585
- <pre class="brush: ruby; html-script: true; gutter: false; toolbar: false">
586
- &lt;h1&gt;Posts&lt;/h1&gt;
587
- &lt;%= link_to &quot;New Post&quot;, new_post_path %&gt;
588
- &lt;% @posts.each do |post| %&gt;
589
- &lt;h2&gt;&lt;%= post.title %&gt;&lt;/h2&gt;
590
- &lt;small&gt;By &lt;%= post.author %&gt;&lt;/small&gt;
591
- &lt;%= simple_format(post.text) %&gt;
592
- &lt;hr&gt;
593
- &lt;% end %&gt;
594
- </pre>
595
- </div>
596
- <p>Rather than looking like the default scaffold, the page will now look like this:</p>
597
- <p><img src="images/engines_post_override.png" title="Engine scaffold overriden" alt="Engine scaffold overriden" /></p>
598
- <h4 id="controllers">5.2 Controllers</h4>
599
- <p><span class="caps">TODO</span>: Explain how to extend a controller.
600
- <span class="caps">IDEA</span>: I like Devise&#8217;s <tt>devise :controllers => { "sessions" => "sessions" }</tt> idea. Perhaps we could incorporate that into the guide?</p>
601
- <h4 id="models">5.3 Models</h4>
602
- <p><span class="caps">TODO</span>: Explain how to extend models provided by an engine.</p>
603
- <h4 id="routes">5.4 Routes</h4>
604
- <p>Within the application, you may wish to link to some area within the engine. Due to the fact that the engine&#8217;s routes are isolated (by the <tt>isolate_namespace</tt> call within the <tt>lib/blorgh/engine.rb</tt> file), you will need to prefix these routes with the engine name. This means rather than having something such as:</p>
605
- <div class="code_container">
606
- <pre class="brush: ruby; html-script: true; gutter: false; toolbar: false">
607
- &lt;%= link_to &quot;Blog posts&quot;, posts_path %&gt;
608
- </pre>
609
- </div>
610
- <p>It needs to be written as:</p>
611
- <div class="code_container">
612
- <pre class="brush: ruby; html-script: true; gutter: false; toolbar: false">
613
- &lt;%= link_to &quot;Blog posts&quot;, blorgh.posts_path %&gt;
614
- </pre>
615
- </div>
616
- <p>This allows for the engine <em>and</em> the application to both have a <tt>posts_path</tt> routing helper and to not interfere with each other. You may also reference another engine&#8217;s routes from inside an engine using this same syntax.</p>
617
- <p>If you wish to reference the application inside the engine in a similar way, use the <tt>main_app</tt> helper:</p>
618
- <div class="code_container">
619
- <pre class="brush: ruby; html-script: true; gutter: false; toolbar: false">
620
- &lt;%= link_to &quot;Home&quot;, main_app.root_path %&gt;
621
- </pre>
622
- </div>
623
- <p><span class="caps">TODO</span>: Mention how to use assets within an engine?
624
- <span class="caps">TODO</span>: Mention how to depend on external gems, like RedCarpet.</p>
625
-
626
- <h3>Feedback</h3>
627
- <p>
628
- You're encouraged to help improve the quality of this guide.
629
- </p>
630
- <p>
631
- If you see any typos or factual errors you are confident to
632
- patch, please clone <a href="https://github.com/lifo/docrails">docrails</a>
633
- and push the change yourself. That branch of Rails has public write access.
634
- Commits are still reviewed, but that happens after you've submitted your
635
- contribution. <a href="https://github.com/lifo/docrails">docrails</a> is
636
- cross-merged with master periodically.
637
- </p>
638
- <p>
639
- You may also find incomplete content, or stuff that is not up to date.
640
- Please do add any missing documentation for master. Check the
641
- <a href="ruby_on_rails_guides_guidelines.html">Ruby on Rails Guides Guidelines</a>
642
- for style and conventions.
643
- </p>
644
- <p>
645
- If for whatever reason you spot something to fix but cannot patch it yourself, please
646
- <a href="https://github.com/rails/rails/issues">open an issue</a>.
647
- </p>
648
- <p>And last but not least, any kind of discussion regarding Ruby on Rails
649
- documentation is very welcome in the <a href="http://groups.google.com/group/rubyonrails-docs">rubyonrails-docs mailing list</a>.
650
- </p>
651
- </div>
652
- </div>
653
- </div>
654
-
655
- <hr class="hide" />
656
- <div id="footer">
657
- <div class="wrapper">
658
- <p>This work is licensed under a <a href="http://creativecommons.org/licenses/by-sa/3.0/">Creative Commons Attribution-Share Alike 3.0</a> License</p>
659
- <p>"Rails", "Ruby on Rails", and the Rails logo are trademarks of David Heinemeier Hansson. All rights reserved.</p>
660
- </div>
661
- </div>
662
-
663
- <script type="text/javascript" src="javascripts/guides.js"></script>
664
- <script type="text/javascript" src="javascripts/syntaxhighlighter/shCore.js"></script>
665
- <script type="text/javascript" src="javascripts/syntaxhighlighter/shBrushRuby.js"></script>
666
- <script type="text/javascript" src="javascripts/syntaxhighlighter/shBrushXml.js"></script>
667
- <script type="text/javascript" src="javascripts/syntaxhighlighter/shBrushSql.js"></script>
668
- <script type="text/javascript" src="javascripts/syntaxhighlighter/shBrushPlain.js"></script>
669
- <script type="text/javascript">
670
- SyntaxHighlighter.all()
671
- </script>
672
- </body>
673
- </html>