rails 3.2.22.5 → 4.0.0.beta1

Sign up to get free protection for your applications and to get access to all the features.

Potentially problematic release.


This version of rails might be problematic. Click here for more details.

Files changed (281) hide show
  1. checksums.yaml +4 -4
  2. data/README.rdoc +77 -0
  3. data/guides/CHANGELOG.md +9 -0
  4. data/guides/Rakefile +77 -0
  5. data/guides/assets/images/belongs_to.png +0 -0
  6. data/guides/assets/images/book_icon.gif +0 -0
  7. data/guides/assets/images/bullet.gif +0 -0
  8. data/guides/assets/images/challenge.png +0 -0
  9. data/guides/assets/images/chapters_icon.gif +0 -0
  10. data/guides/assets/images/check_bullet.gif +0 -0
  11. data/guides/assets/images/credits_pic_blank.gif +0 -0
  12. data/guides/assets/images/csrf.png +0 -0
  13. data/guides/assets/images/edge_badge.png +0 -0
  14. data/guides/assets/images/favicon.ico +0 -0
  15. data/guides/assets/images/feature_tile.gif +0 -0
  16. data/guides/assets/images/footer_tile.gif +0 -0
  17. data/guides/assets/images/fxn.png +0 -0
  18. data/guides/assets/images/getting_started/confirm_dialog.png +0 -0
  19. data/guides/assets/images/getting_started/forbidden_attributes_for_new_post.png +0 -0
  20. data/guides/assets/images/getting_started/form_with_errors.png +0 -0
  21. data/guides/assets/images/getting_started/index_action_with_edit_link.png +0 -0
  22. data/guides/assets/images/getting_started/new_post.png +0 -0
  23. data/guides/assets/images/getting_started/post_with_comments.png +0 -0
  24. data/guides/assets/images/getting_started/routing_error_no_controller.png +0 -0
  25. data/guides/assets/images/getting_started/routing_error_no_route_matches.png +0 -0
  26. data/guides/assets/images/getting_started/show_action_for_posts.png +0 -0
  27. data/guides/assets/images/getting_started/template_is_missing_posts_new.png +0 -0
  28. data/guides/assets/images/getting_started/undefined_method_post_path.png +0 -0
  29. data/guides/assets/images/getting_started/unknown_action_create_for_posts.png +0 -0
  30. data/guides/assets/images/getting_started/unknown_action_new_for_posts.png +0 -0
  31. data/guides/assets/images/grey_bullet.gif +0 -0
  32. data/guides/assets/images/habtm.png +0 -0
  33. data/guides/assets/images/has_many.png +0 -0
  34. data/guides/assets/images/has_many_through.png +0 -0
  35. data/guides/assets/images/has_one.png +0 -0
  36. data/guides/assets/images/has_one_through.png +0 -0
  37. data/guides/assets/images/header_backdrop.png +0 -0
  38. data/guides/assets/images/header_tile.gif +0 -0
  39. data/guides/assets/images/i18n/demo_html_safe.png +0 -0
  40. data/guides/assets/images/i18n/demo_localized_pirate.png +0 -0
  41. data/guides/assets/images/i18n/demo_translated_en.png +0 -0
  42. data/guides/assets/images/i18n/demo_translated_pirate.png +0 -0
  43. data/guides/assets/images/i18n/demo_translation_missing.png +0 -0
  44. data/guides/assets/images/i18n/demo_untranslated.png +0 -0
  45. data/guides/assets/images/icons/README +5 -0
  46. data/guides/assets/images/icons/callouts/1.png +0 -0
  47. data/guides/assets/images/icons/callouts/10.png +0 -0
  48. data/guides/assets/images/icons/callouts/11.png +0 -0
  49. data/guides/assets/images/icons/callouts/12.png +0 -0
  50. data/guides/assets/images/icons/callouts/13.png +0 -0
  51. data/guides/assets/images/icons/callouts/14.png +0 -0
  52. data/guides/assets/images/icons/callouts/15.png +0 -0
  53. data/guides/assets/images/icons/callouts/2.png +0 -0
  54. data/guides/assets/images/icons/callouts/3.png +0 -0
  55. data/guides/assets/images/icons/callouts/4.png +0 -0
  56. data/guides/assets/images/icons/callouts/5.png +0 -0
  57. data/guides/assets/images/icons/callouts/6.png +0 -0
  58. data/guides/assets/images/icons/callouts/7.png +0 -0
  59. data/guides/assets/images/icons/callouts/8.png +0 -0
  60. data/guides/assets/images/icons/callouts/9.png +0 -0
  61. data/guides/assets/images/icons/caution.png +0 -0
  62. data/guides/assets/images/icons/example.png +0 -0
  63. data/guides/assets/images/icons/home.png +0 -0
  64. data/guides/assets/images/icons/important.png +0 -0
  65. data/guides/assets/images/icons/next.png +0 -0
  66. data/guides/assets/images/icons/note.png +0 -0
  67. data/guides/assets/images/icons/prev.png +0 -0
  68. data/guides/assets/images/icons/tip.png +0 -0
  69. data/guides/assets/images/icons/up.png +0 -0
  70. data/guides/assets/images/icons/warning.png +0 -0
  71. data/guides/assets/images/jaimeiniesta.jpg +0 -0
  72. data/guides/assets/images/nav_arrow.gif +0 -0
  73. data/guides/assets/images/oscardelben.jpg +0 -0
  74. data/guides/assets/images/polymorphic.png +0 -0
  75. data/guides/assets/images/radar.png +0 -0
  76. data/guides/assets/images/rails4_features.png +0 -0
  77. data/guides/assets/images/rails_guides_kindle_cover.jpg +0 -0
  78. data/guides/assets/images/rails_guides_logo.gif +0 -0
  79. data/guides/assets/images/rails_logo_remix.gif +0 -0
  80. data/guides/assets/images/rails_welcome.png +0 -0
  81. data/guides/assets/images/session_fixation.png +0 -0
  82. data/guides/assets/images/tab_grey.gif +0 -0
  83. data/guides/assets/images/tab_info.gif +0 -0
  84. data/guides/assets/images/tab_note.gif +0 -0
  85. data/guides/assets/images/tab_red.gif +0 -0
  86. data/guides/assets/images/tab_yellow.gif +0 -0
  87. data/guides/assets/images/tab_yellow.png +0 -0
  88. data/guides/assets/images/vijaydev.jpg +0 -0
  89. data/guides/assets/javascripts/guides.js +57 -0
  90. data/guides/assets/javascripts/jquery.min.js +4 -0
  91. data/guides/assets/javascripts/responsive-tables.js +43 -0
  92. data/guides/assets/javascripts/syntaxhighlighter/shBrushAS3.js +59 -0
  93. data/guides/assets/javascripts/syntaxhighlighter/shBrushAppleScript.js +75 -0
  94. data/guides/assets/javascripts/syntaxhighlighter/shBrushBash.js +59 -0
  95. data/guides/assets/javascripts/syntaxhighlighter/shBrushCSharp.js +65 -0
  96. data/guides/assets/javascripts/syntaxhighlighter/shBrushColdFusion.js +100 -0
  97. data/guides/assets/javascripts/syntaxhighlighter/shBrushCpp.js +97 -0
  98. data/guides/assets/javascripts/syntaxhighlighter/shBrushCss.js +91 -0
  99. data/guides/assets/javascripts/syntaxhighlighter/shBrushDelphi.js +55 -0
  100. data/guides/assets/javascripts/syntaxhighlighter/shBrushDiff.js +41 -0
  101. data/guides/assets/javascripts/syntaxhighlighter/shBrushErlang.js +52 -0
  102. data/guides/assets/javascripts/syntaxhighlighter/shBrushGroovy.js +67 -0
  103. data/guides/assets/javascripts/syntaxhighlighter/shBrushJScript.js +52 -0
  104. data/guides/assets/javascripts/syntaxhighlighter/shBrushJava.js +57 -0
  105. data/guides/assets/javascripts/syntaxhighlighter/shBrushJavaFX.js +58 -0
  106. data/guides/assets/javascripts/syntaxhighlighter/shBrushPerl.js +72 -0
  107. data/guides/assets/javascripts/syntaxhighlighter/shBrushPhp.js +88 -0
  108. data/guides/assets/javascripts/syntaxhighlighter/shBrushPlain.js +33 -0
  109. data/guides/assets/javascripts/syntaxhighlighter/shBrushPowerShell.js +74 -0
  110. data/guides/assets/javascripts/syntaxhighlighter/shBrushPython.js +64 -0
  111. data/guides/assets/javascripts/syntaxhighlighter/shBrushRuby.js +55 -0
  112. data/guides/assets/javascripts/syntaxhighlighter/shBrushSass.js +94 -0
  113. data/guides/assets/javascripts/syntaxhighlighter/shBrushScala.js +51 -0
  114. data/guides/assets/javascripts/syntaxhighlighter/shBrushSql.js +66 -0
  115. data/guides/assets/javascripts/syntaxhighlighter/shBrushVb.js +56 -0
  116. data/guides/assets/javascripts/syntaxhighlighter/shBrushXml.js +69 -0
  117. data/guides/assets/javascripts/syntaxhighlighter/shCore.js +17 -0
  118. data/guides/assets/stylesheets/fixes.css +16 -0
  119. data/guides/assets/stylesheets/kindle.css +11 -0
  120. data/guides/assets/stylesheets/main.css +709 -0
  121. data/guides/assets/stylesheets/print.css +52 -0
  122. data/guides/assets/stylesheets/reset.css +43 -0
  123. data/guides/assets/stylesheets/responsive-tables.css +50 -0
  124. data/guides/assets/stylesheets/style.css +13 -0
  125. data/guides/assets/stylesheets/syntaxhighlighter/shCore.css +226 -0
  126. data/guides/assets/stylesheets/syntaxhighlighter/shCoreDefault.css +328 -0
  127. data/guides/assets/stylesheets/syntaxhighlighter/shCoreDjango.css +331 -0
  128. data/guides/assets/stylesheets/syntaxhighlighter/shCoreEclipse.css +339 -0
  129. data/guides/assets/stylesheets/syntaxhighlighter/shCoreEmacs.css +324 -0
  130. data/guides/assets/stylesheets/syntaxhighlighter/shCoreFadeToGrey.css +328 -0
  131. data/guides/assets/stylesheets/syntaxhighlighter/shCoreMDUltra.css +324 -0
  132. data/guides/assets/stylesheets/syntaxhighlighter/shCoreMidnight.css +324 -0
  133. data/guides/assets/stylesheets/syntaxhighlighter/shCoreRDark.css +324 -0
  134. data/guides/assets/stylesheets/syntaxhighlighter/shThemeDefault.css +117 -0
  135. data/guides/assets/stylesheets/syntaxhighlighter/shThemeDjango.css +120 -0
  136. data/guides/assets/stylesheets/syntaxhighlighter/shThemeEclipse.css +128 -0
  137. data/guides/assets/stylesheets/syntaxhighlighter/shThemeEmacs.css +113 -0
  138. data/guides/assets/stylesheets/syntaxhighlighter/shThemeFadeToGrey.css +117 -0
  139. data/guides/assets/stylesheets/syntaxhighlighter/shThemeMDUltra.css +113 -0
  140. data/guides/assets/stylesheets/syntaxhighlighter/shThemeMidnight.css +113 -0
  141. data/guides/assets/stylesheets/syntaxhighlighter/shThemeRDark.css +113 -0
  142. data/guides/assets/stylesheets/syntaxhighlighter/shThemeRailsGuides.css +116 -0
  143. data/guides/code/getting_started/Gemfile +38 -0
  144. data/guides/code/getting_started/Gemfile.lock +150 -0
  145. data/guides/code/getting_started/README.rdoc +28 -0
  146. data/guides/code/getting_started/Rakefile +6 -0
  147. data/guides/code/getting_started/app/assets/images/rails.png +0 -0
  148. data/guides/code/getting_started/app/assets/javascripts/application.js +16 -0
  149. data/guides/code/getting_started/app/assets/javascripts/comments.js.coffee +3 -0
  150. data/guides/code/getting_started/app/assets/javascripts/posts.js.coffee +3 -0
  151. data/guides/code/getting_started/app/assets/javascripts/welcome.js.coffee +3 -0
  152. data/guides/code/getting_started/app/assets/stylesheets/application.css +13 -0
  153. data/guides/code/getting_started/app/assets/stylesheets/comments.css.scss +3 -0
  154. data/guides/code/getting_started/app/assets/stylesheets/posts.css.scss +3 -0
  155. data/guides/code/getting_started/app/assets/stylesheets/welcome.css.scss +3 -0
  156. data/guides/code/getting_started/app/controllers/application_controller.rb +5 -0
  157. data/guides/code/getting_started/app/controllers/comments_controller.rb +17 -0
  158. data/guides/code/getting_started/app/controllers/posts_controller.rb +47 -0
  159. data/guides/code/getting_started/app/controllers/welcome_controller.rb +4 -0
  160. data/guides/code/getting_started/app/helpers/application_helper.rb +2 -0
  161. data/guides/code/getting_started/app/helpers/comments_helper.rb +2 -0
  162. data/guides/code/getting_started/app/helpers/posts_helper.rb +2 -0
  163. data/guides/code/getting_started/app/helpers/welcome_helper.rb +2 -0
  164. data/guides/code/getting_started/app/models/comment.rb +3 -0
  165. data/guides/code/getting_started/app/models/post.rb +7 -0
  166. data/guides/code/getting_started/app/views/comments/_comment.html.erb +15 -0
  167. data/guides/code/getting_started/app/views/comments/_form.html.erb +13 -0
  168. data/guides/code/getting_started/app/views/layouts/application.html.erb +14 -0
  169. data/guides/code/getting_started/app/views/posts/_form.html.erb +27 -0
  170. data/guides/code/getting_started/app/views/posts/edit.html.erb +5 -0
  171. data/guides/code/getting_started/app/views/posts/index.html.erb +21 -0
  172. data/guides/code/getting_started/app/views/posts/new.html.erb +5 -0
  173. data/guides/code/getting_started/app/views/posts/show.html.erb +18 -0
  174. data/guides/code/getting_started/app/views/welcome/index.html.erb +3 -0
  175. data/guides/code/getting_started/bin/bundle +4 -0
  176. data/guides/code/getting_started/bin/rails +4 -0
  177. data/guides/code/getting_started/bin/rake +4 -0
  178. data/guides/code/getting_started/config.ru +4 -0
  179. data/guides/code/getting_started/config/application.rb +17 -0
  180. data/guides/code/getting_started/config/boot.rb +4 -0
  181. data/guides/code/getting_started/config/database.yml +25 -0
  182. data/guides/code/getting_started/config/environment.rb +5 -0
  183. data/guides/code/getting_started/config/environments/development.rb +30 -0
  184. data/guides/code/getting_started/config/environments/production.rb +80 -0
  185. data/guides/code/getting_started/config/environments/test.rb +36 -0
  186. data/guides/code/getting_started/config/initializers/backtrace_silencers.rb +7 -0
  187. data/guides/code/getting_started/config/initializers/filter_parameter_logging.rb +4 -0
  188. data/guides/code/getting_started/config/initializers/inflections.rb +16 -0
  189. data/guides/code/getting_started/config/initializers/locale.rb +9 -0
  190. data/guides/code/getting_started/config/initializers/mime_types.rb +5 -0
  191. data/guides/code/getting_started/config/initializers/secret_token.rb +12 -0
  192. data/guides/code/getting_started/config/initializers/session_store.rb +3 -0
  193. data/guides/code/getting_started/config/initializers/wrap_parameters.rb +14 -0
  194. data/guides/code/getting_started/config/locales/en.yml +23 -0
  195. data/guides/code/getting_started/config/routes.rb +7 -0
  196. data/guides/code/getting_started/db/migrate/20130122042648_create_posts.rb +10 -0
  197. data/guides/code/getting_started/db/migrate/20130122045842_create_comments.rb +11 -0
  198. data/guides/code/getting_started/db/schema.rb +33 -0
  199. data/guides/code/getting_started/db/seeds.rb +7 -0
  200. data/guides/code/getting_started/public/404.html +27 -0
  201. data/guides/code/getting_started/public/422.html +26 -0
  202. data/guides/code/getting_started/public/500.html +26 -0
  203. data/guides/code/getting_started/public/favicon.ico +0 -0
  204. data/guides/code/getting_started/public/robots.txt +5 -0
  205. data/guides/code/getting_started/test/controllers/comments_controller_test.rb +7 -0
  206. data/guides/code/getting_started/test/controllers/posts_controller_test.rb +7 -0
  207. data/guides/code/getting_started/test/controllers/welcome_controller_test.rb +9 -0
  208. data/guides/code/getting_started/test/fixtures/comments.yml +11 -0
  209. data/guides/code/getting_started/test/fixtures/posts.yml +9 -0
  210. data/guides/code/getting_started/test/helpers/comments_helper_test.rb +4 -0
  211. data/guides/code/getting_started/test/helpers/posts_helper_test.rb +4 -0
  212. data/guides/code/getting_started/test/helpers/welcome_helper_test.rb +4 -0
  213. data/guides/code/getting_started/test/models/comment_test.rb +7 -0
  214. data/guides/code/getting_started/test/models/post_test.rb +7 -0
  215. data/guides/code/getting_started/test/test_helper.rb +15 -0
  216. data/guides/rails_guides.rb +44 -0
  217. data/guides/rails_guides/generator.rb +248 -0
  218. data/guides/rails_guides/helpers.rb +51 -0
  219. data/guides/rails_guides/indexer.rb +68 -0
  220. data/guides/rails_guides/kindle.rb +119 -0
  221. data/guides/rails_guides/levenshtein.rb +31 -0
  222. data/guides/rails_guides/markdown.rb +163 -0
  223. data/guides/rails_guides/markdown/renderer.rb +82 -0
  224. data/guides/source/2_2_release_notes.md +435 -0
  225. data/guides/source/2_3_release_notes.md +621 -0
  226. data/guides/source/3_0_release_notes.md +614 -0
  227. data/guides/source/3_1_release_notes.md +556 -0
  228. data/guides/source/3_2_release_notes.md +565 -0
  229. data/guides/source/4_0_release_notes.md +228 -0
  230. data/guides/source/_license.html.erb +2 -0
  231. data/guides/source/_welcome.html.erb +19 -0
  232. data/guides/source/action_controller_overview.md +872 -0
  233. data/guides/source/action_mailer_basics.md +599 -0
  234. data/guides/source/action_view_overview.md +1565 -0
  235. data/guides/source/active_model_basics.md +200 -0
  236. data/guides/source/active_record_basics.md +370 -0
  237. data/guides/source/active_record_callbacks.md +358 -0
  238. data/guides/source/active_record_querying.md +1621 -0
  239. data/guides/source/active_record_validations.md +1128 -0
  240. data/guides/source/active_support_core_extensions.md +3791 -0
  241. data/guides/source/active_support_instrumentation.md +487 -0
  242. data/guides/source/api_documentation_guidelines.md +209 -0
  243. data/guides/source/asset_pipeline.md +832 -0
  244. data/guides/source/association_basics.md +2129 -0
  245. data/guides/source/caching_with_rails.md +350 -0
  246. data/guides/source/command_line.md +594 -0
  247. data/guides/source/configuring.md +736 -0
  248. data/guides/source/contributing_to_ruby_on_rails.md +455 -0
  249. data/guides/source/credits.html.erb +76 -0
  250. data/guides/source/debugging_rails_applications.md +675 -0
  251. data/guides/source/development_dependencies_install.md +195 -0
  252. data/guides/source/documents.yaml +179 -0
  253. data/guides/source/engines.md +961 -0
  254. data/guides/source/form_helpers.md +955 -0
  255. data/guides/source/generators.md +644 -0
  256. data/guides/source/getting_started.md +1775 -0
  257. data/guides/source/i18n.md +983 -0
  258. data/guides/source/index.html.erb +27 -0
  259. data/guides/source/initialization.md +562 -0
  260. data/guides/source/kindle/KINDLE.md +26 -0
  261. data/guides/source/kindle/copyright.html.erb +1 -0
  262. data/guides/source/kindle/layout.html.erb +27 -0
  263. data/guides/source/kindle/rails_guides.opf.erb +52 -0
  264. data/guides/source/kindle/toc.html.erb +24 -0
  265. data/guides/source/kindle/toc.ncx.erb +64 -0
  266. data/guides/source/kindle/welcome.html.erb +5 -0
  267. data/guides/source/layout.html.erb +148 -0
  268. data/guides/source/layouts_and_rendering.md +1132 -0
  269. data/guides/source/migrations.md +1059 -0
  270. data/guides/source/nested_model_forms.md +225 -0
  271. data/guides/source/plugins.md +435 -0
  272. data/guides/source/rails_application_templates.md +229 -0
  273. data/guides/source/rails_on_rack.md +342 -0
  274. data/guides/source/routing.md +1088 -0
  275. data/guides/source/ruby_on_rails_guides_guidelines.md +124 -0
  276. data/guides/source/security.md +973 -0
  277. data/guides/source/testing.md +981 -0
  278. data/guides/source/upgrading_ruby_on_rails.md +286 -0
  279. data/guides/source/working_with_javascript_in_rails.md +396 -0
  280. data/guides/w3c_validator.rb +95 -0
  281. metadata +315 -31
@@ -0,0 +1,195 @@
1
+ Development Dependencies Install
2
+ ================================
3
+
4
+ This guide covers how to setup an environment for Ruby on Rails core development.
5
+
6
+ After reading this guide, you will know:
7
+
8
+ --------------------------------------------------------------------------------
9
+
10
+ The Easy Way
11
+ ------------
12
+
13
+ The easiest and recommended way to get a development environment ready to hack is to use the [Rails development box](https://github.com/rails/rails-dev-box).
14
+
15
+ The Hard Way
16
+ ------------
17
+
18
+ In case you can't use the Rails development box, see section above, these are the steps to manually build a development box for Ruby on Rails core development.
19
+
20
+ ### Install Git
21
+
22
+ Ruby on Rails uses Git for source code control. The [Git homepage](http://git-scm.com/) has installation instructions. There are a variety of resources on the net that will help you get familiar with Git:
23
+
24
+ * [Try Git course](http://try.github.com/) is an interactive course that will teach you the basics.
25
+ * The [official Documentation](http://git-scm.com/documentation) is pretty comprehensive and also contains some videos with the basics of Git
26
+ * [Everyday Git](http://schacon.github.com/git/everyday.html) will teach you just enough about Git to get by.
27
+ * The [PeepCode screencast](https://peepcode.com/products/git) on Git ($9) is easier to follow.
28
+ * [GitHub](http://help.github.com) offers links to a variety of Git resources.
29
+ * [Pro Git](http://git-scm.com/book) is an entire book about Git with a Creative Commons license.
30
+
31
+ ### Clone the Ruby on Rails Repository
32
+
33
+ Navigate to the folder where you want the Ruby on Rails source code (it will create its own `rails` subdirectory) and run:
34
+
35
+ ```bash
36
+ $ git clone git://github.com/rails/rails.git
37
+ $ cd rails
38
+ ```
39
+
40
+ ### Set up and Run the Tests
41
+
42
+ The test suite must pass with any submitted code. No matter whether you are writing a new patch, or evaluating someone else's, you need to be able to run the tests.
43
+
44
+ Install first libxml2 and libxslt together with their development files for Nokogiri. In Ubuntu that's
45
+
46
+ ```bash
47
+ $ sudo apt-get install libxml2 libxml2-dev libxslt1-dev
48
+ ```
49
+
50
+ If you are on Fedora or CentOS, you can run
51
+
52
+ ```bash
53
+ $ sudo yum install libxml2 libxml2-devel libxslt libxslt-devel
54
+ ```
55
+
56
+ If you have any problems with these libraries, you should install them manually compiling the source code. Just follow the instructions at the [Red Hat/CentOS section of the Nokogiri tutorials](http://nokogiri.org/tutorials/installing_nokogiri.html#red_hat__centos) .
57
+
58
+ Also, SQLite3 and its development files for the `sqlite3-ruby` gem — in Ubuntu you're done with just
59
+
60
+ ```bash
61
+ $ sudo apt-get install sqlite3 libsqlite3-dev
62
+ ```
63
+
64
+ And if you are on Fedora or CentOS, you're done with
65
+
66
+ ```bash
67
+ $ sudo yum install sqlite3 sqlite3-devel
68
+ ```
69
+
70
+ Get a recent version of [Bundler](http://gembundler.com/)
71
+
72
+ ```bash
73
+ $ gem install bundler
74
+ $ gem update bundler
75
+ ```
76
+
77
+ and run:
78
+
79
+ ```bash
80
+ $ bundle install --without db
81
+ ```
82
+
83
+ This command will install all dependencies except the MySQL and PostgreSQL Ruby drivers. We will come back to these soon. With dependencies installed, you can run the test suite with:
84
+
85
+ ```bash
86
+ $ bundle exec rake test
87
+ ```
88
+
89
+ You can also run tests for a specific component, like Action Pack, by going into its directory and executing the same command:
90
+
91
+ ```bash
92
+ $ cd actionpack
93
+ $ bundle exec rake test
94
+ ```
95
+
96
+ If you want to run the tests located in a specific directory use the `TEST_DIR` environment variable. For example, this will run the tests of the `railties/test/generators` directory only:
97
+
98
+ ```bash
99
+ $ cd railties
100
+ $ TEST_DIR=generators bundle exec rake test
101
+ ```
102
+
103
+ You can run any single test separately too:
104
+
105
+ ```bash
106
+ $ cd actionpack
107
+ $ bundle exec ruby -Itest test/template/form_helper_test.rb
108
+ ```
109
+
110
+ ### Active Record Setup
111
+
112
+ The test suite of Active Record attempts to run four times: once for SQLite3, once for each of the two MySQL gems (`mysql` and `mysql2`), and once for PostgreSQL. We are going to see now how to set up the environment for them.
113
+
114
+ WARNING: If you're working with Active Record code, you _must_ ensure that the tests pass for at least MySQL, PostgreSQL, and SQLite3. Subtle differences between the various adapters have been behind the rejection of many patches that looked OK when tested only against MySQL.
115
+
116
+ #### Database Configuration
117
+
118
+ The Active Record test suite requires a custom config file: `activerecord/test/config.yml`. An example is provided in `activerecord/test/config.example.yml` which can be copied and used as needed for your environment.
119
+
120
+ #### MySQL and PostgreSQL
121
+
122
+ To be able to run the suite for MySQL and PostgreSQL we need their gems. Install first the servers, their client libraries, and their development files. In Ubuntu just run
123
+
124
+ ```bash
125
+ $ sudo apt-get install mysql-server libmysqlclient15-dev
126
+ $ sudo apt-get install postgresql postgresql-client postgresql-contrib libpq-dev
127
+ ```
128
+
129
+ On Fedora or CentOS, just run:
130
+
131
+ ```bash
132
+ $ sudo yum install mysql-server mysql-devel
133
+ $ sudo yum install postgresql-server postgresql-devel
134
+ ```
135
+
136
+ After that run:
137
+
138
+ ```bash
139
+ $ rm .bundle/config
140
+ $ bundle install
141
+ ```
142
+
143
+ We need first to delete `.bundle/config` because Bundler remembers in that file that we didn't want to install the "db" group (alternatively you can edit the file).
144
+
145
+ In order to be able to run the test suite against MySQL you need to create a user named `rails` with privileges on the test databases:
146
+
147
+ ```bash
148
+ $ mysql -uroot -p
149
+
150
+ mysql> CREATE USER 'rails'@'localhost';
151
+ mysql> GRANT ALL PRIVILEGES ON activerecord_unittest.*
152
+ to 'rails'@'localhost';
153
+ mysql> GRANT ALL PRIVILEGES ON activerecord_unittest2.*
154
+ to 'rails'@'localhost';
155
+ ```
156
+
157
+ and create the test databases:
158
+
159
+ ```bash
160
+ $ cd activerecord
161
+ $ bundle exec rake mysql:build_databases
162
+ ```
163
+
164
+ PostgreSQL's authentication works differently. A simple way to set up the development environment for example is to run with your development account
165
+
166
+ ```bash
167
+ $ sudo -u postgres createuser --superuser $USER
168
+ ```
169
+
170
+ and then create the test databases with
171
+
172
+ ```bash
173
+ $ cd activerecord
174
+ $ bundle exec rake postgresql:build_databases
175
+ ```
176
+
177
+ It is possible to build databases for both PostgreSQL and MySQL with
178
+
179
+ ```bash
180
+ $ cd activerecord
181
+ $ bundle exec rake db:create
182
+ ```
183
+
184
+ You can cleanup the databases using
185
+
186
+ ```bash
187
+ $ cd activerecord
188
+ $ bundle exec rake db:drop
189
+ ```
190
+
191
+ NOTE: Using the rake task to create the test databases ensures they have the correct character set and collation.
192
+
193
+ NOTE: You'll see the following warning (or localized warning) during activating HStore extension in PostgreSQL 9.1.x or earlier: "WARNING: => is deprecated as an operator".
194
+
195
+ If you’re using another database, check the file `activerecord/test/config.yml` or `activerecord/test/config.example.yml` for default connection information. You can edit `activerecord/test/config.yml` to provide different credentials on your machine if you must, but obviously you should not push any such changes back to Rails.
@@ -0,0 +1,179 @@
1
+ -
2
+ name: Start Here
3
+ documents:
4
+ -
5
+ name: Getting Started with Rails
6
+ url: getting_started.html
7
+ description: Everything you need to know to install Rails and create your first application.
8
+ -
9
+ name: Models
10
+ documents:
11
+ -
12
+ name: Active Record Basics
13
+ url: active_record_basics.html
14
+ description: This guide will get you started with models, persistence to database and the Active Record pattern and library.
15
+ -
16
+ name: Rails Database Migrations
17
+ url: migrations.html
18
+ description: This guide covers how you can use Active Record migrations to alter your database in a structured and organized manner.
19
+ -
20
+ name: Active Record Validations
21
+ url: active_record_validations.html
22
+ description: This guide covers how you can use Active Record validations
23
+ -
24
+ name: Active Record Callbacks
25
+ url: active_record_callbacks.html
26
+ description: This guide covers how you can use Active Record callbacks.
27
+ -
28
+ name: Active Record Associations
29
+ url: association_basics.html
30
+ description: This guide covers all the associations provided by Active Record.
31
+ -
32
+ name: Active Record Query Interface
33
+ url: active_record_querying.html
34
+ description: This guide covers the database query interface provided by Active Record.
35
+ -
36
+ name: Views
37
+ documents:
38
+ -
39
+ name: Layouts and Rendering in Rails
40
+ url: layouts_and_rendering.html
41
+ description: This guide covers the basic layout features of Action Controller and Action View, including rendering and redirecting, using content_for blocks, and working with partials.
42
+ -
43
+ name: Action View Form Helpers
44
+ url: form_helpers.html
45
+ description: Guide to using built-in Form helpers.
46
+ -
47
+ name: Controllers
48
+ documents:
49
+ -
50
+ name: Action Controller Overview
51
+ url: action_controller_overview.html
52
+ description: This guide covers how controllers work and how they fit into the request cycle in your application. It includes sessions, filters, and cookies, data streaming, and dealing with exceptions raised by a request, among other topics.
53
+ -
54
+ name: Rails Routing from the Outside In
55
+ url: routing.html
56
+ description: This guide covers the user-facing features of Rails routing. If you want to understand how to use routing in your own Rails applications, start here.
57
+ -
58
+ name: Digging Deeper
59
+ documents:
60
+ -
61
+ name: Active Support Core Extensions
62
+ url: active_support_core_extensions.html
63
+ description: This guide documents the Ruby core extensions defined in Active Support.
64
+ -
65
+ name: Rails Internationalization API
66
+ url: i18n.html
67
+ description: This guide covers how to add internationalization to your applications. Your application will be able to translate content to different languages, change pluralization rules, use correct date formats for each country and so on.
68
+ -
69
+ name: Action Mailer Basics
70
+ url: action_mailer_basics.html
71
+ work_in_progress: true
72
+ description: This guide describes how to use Action Mailer to send and receive emails.
73
+ -
74
+ name: Testing Rails Applications
75
+ url: testing.html
76
+ work_in_progress: true
77
+ description: This is a rather comprehensive guide to doing both unit and functional tests in Rails. It covers everything from 'What is a test?' to the testing APIs. Enjoy.
78
+ -
79
+ name: Securing Rails Applications
80
+ url: security.html
81
+ description: This guide describes common security problems in web applications and how to avoid them with Rails.
82
+ -
83
+ name: Debugging Rails Applications
84
+ url: debugging_rails_applications.html
85
+ description: This guide describes how to debug Rails applications. It covers the different ways of achieving this and how to understand what is happening "behind the scenes" of your code.
86
+ -
87
+ name: Configuring Rails Applications
88
+ url: configuring.html
89
+ description: This guide covers the basic configuration settings for a Rails application.
90
+ -
91
+ name: Rails Command Line Tools and Rake Tasks
92
+ url: command_line.html
93
+ description: This guide covers the command line tools and rake tasks provided by Rails.
94
+ -
95
+ name: Caching with Rails
96
+ work_in_progress: true
97
+ url: caching_with_rails.html
98
+ description: Various caching techniques provided by Rails.
99
+ -
100
+ name: Asset Pipeline
101
+ url: asset_pipeline.html
102
+ description: This guide documents the asset pipeline.
103
+ -
104
+ name: Working with JavaScript in Rails
105
+ url: working_with_javascript_in_rails.html
106
+ description: This guide covers the built-in Ajax/JavaScript functionality of Rails.
107
+ -
108
+ name: Getting Started with Engines
109
+ url: engines.html
110
+ description: This guide explains how to write a mountable engine.
111
+ work_in_progress: true
112
+ -
113
+ name: The Rails Initialization Process
114
+ work_in_progress: true
115
+ url: initialization.html
116
+ description: This guide explains the internals of the Rails initialization process as of Rails 3.1
117
+ -
118
+ name: Extending Rails
119
+ documents:
120
+ -
121
+ name: The Basics of Creating Rails Plugins
122
+ work_in_progress: true
123
+ url: plugins.html
124
+ description: This guide covers how to build a plugin to extend the functionality of Rails.
125
+ -
126
+ name: Rails on Rack
127
+ url: rails_on_rack.html
128
+ description: This guide covers Rails integration with Rack and interfacing with other Rack components.
129
+ -
130
+ name: Creating and Customizing Rails Generators
131
+ url: generators.html
132
+ description: This guide covers the process of adding a brand new generator to your extension or providing an alternative to an element of a built-in Rails generator (such as providing alternative test stubs for the scaffold generator).
133
+ -
134
+ name: Contributing to Ruby on Rails
135
+ documents:
136
+ -
137
+ name: Contributing to Ruby on Rails
138
+ url: contributing_to_ruby_on_rails.html
139
+ description: Rails is not 'somebody else's framework.' This guide covers a variety of ways that you can get involved in the ongoing development of Rails.
140
+ -
141
+ name: API Documentation Guidelines
142
+ url: api_documentation_guidelines.html
143
+ description: This guide documents the Ruby on Rails API documentation guidelines.
144
+ -
145
+ name: Ruby on Rails Guides Guidelines
146
+ url: ruby_on_rails_guides_guidelines.html
147
+ description: This guide documents the Ruby on Rails guides guidelines.
148
+ -
149
+ name: Release Notes
150
+ documents:
151
+ -
152
+ name: Upgrading Ruby on Rails
153
+ url: upgrading_ruby_on_rails.html
154
+ work_in_progress: true
155
+ description: This guide helps in upgrading applications to latest Ruby on Rails versions.
156
+ -
157
+ name: Ruby on Rails 4.0 Release Notes
158
+ url: 4_0_release_notes.html
159
+ description: Release notes for Rails 4.0.
160
+ -
161
+ name: Ruby on Rails 3.2 Release Notes
162
+ url: 3_2_release_notes.html
163
+ description: Release notes for Rails 3.2.
164
+ -
165
+ name: Ruby on Rails 3.1 Release Notes
166
+ url: 3_1_release_notes.html
167
+ description: Release notes for Rails 3.1.
168
+ -
169
+ name: Ruby on Rails 3.0 Release Notes
170
+ url: 3_0_release_notes.html
171
+ description: Release notes for Rails 3.0.
172
+ -
173
+ name: Ruby on Rails 2.3 Release Notes
174
+ url: 2_3_release_notes.html
175
+ description: Release notes for Rails 2.3.
176
+ -
177
+ name: Ruby on Rails 2.2 Release Notes
178
+ url: 2_2_release_notes.html
179
+ description: Release notes for Rails 2.2.
@@ -0,0 +1,961 @@
1
+ Getting Started with Engines
2
+ ============================
3
+
4
+ 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.
5
+
6
+ After reading this guide, you will know:
7
+
8
+ * What makes an engine.
9
+ * How to generate an engine.
10
+ * Building features for the engine.
11
+ * Hooking the engine into an application.
12
+ * Overriding engine functionality in the application.
13
+
14
+ --------------------------------------------------------------------------------
15
+
16
+ What are engines?
17
+ -----------------
18
+
19
+ 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`.
20
+
21
+ 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.
22
+
23
+ Engines are also closely related to plugins where the two share a common `lib` directory structure and are both generated using the `rails plugin new` generator. The difference being that an engine is considered a "full plugin" by Rails as indicated by the `--full` option that's passed to the generator command, but this guide will refer to them simply as "engines" throughout. An engine **can** be a plugin, and a plugin **can** be an engine.
24
+
25
+ The engine that will be created in this guide will be called "blorgh". The engine will provide blogging functionality to its host applications, allowing for new posts and comments to be created. At the beginning of this guide, you will be working solely within the engine itself, but in later sections you'll see how to hook it into an application.
26
+
27
+ 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 `posts_path` and use an engine also that provides a path also called `posts_path`, and the two would not clash. Along with this, controllers, models and table names are also namespaced. You'll see how to do this later in this guide.
28
+
29
+ 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 has final say in what goes on in the universe (with the universe being the application's environment) where the engine should only be enhancing it, rather than changing it drastically.
30
+
31
+ 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.
32
+
33
+ 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!
34
+
35
+ Generating an engine
36
+ --------------------
37
+
38
+ 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:
39
+
40
+ ```bash
41
+ $ rails plugin new blorgh --mountable
42
+ ```
43
+
44
+ The full list of options for the plugin generator may be seen by typing:
45
+
46
+ ```bash
47
+ $ rails plugin --help
48
+ ```
49
+
50
+ The `--full` option tells the generator that you want to create an engine, including a skeleton structure by providing the following:
51
+
52
+ * An `app` directory tree
53
+ * A `config/routes.rb` file:
54
+
55
+ ```ruby
56
+ Rails.application.routes.draw do
57
+ end
58
+ ```
59
+ * A file at `lib/blorgh/engine.rb` which is identical in function to a standard Rails application's `config/application.rb` file:
60
+
61
+ ```ruby
62
+ module Blorgh
63
+ class Engine < ::Rails::Engine
64
+ end
65
+ end
66
+ ```
67
+
68
+ 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, and will add:
69
+
70
+ * Asset manifest files (`application.js` and `application.css`)
71
+ * A namespaced `ApplicationController` stub
72
+ * A namespaced `ApplicationHelper` stub
73
+ * A layout view template for the engine
74
+ * Namespace isolation to `config/routes.rb`:
75
+
76
+ ```ruby
77
+ Blorgh::Engine.routes.draw do
78
+ end
79
+ ```
80
+
81
+ * Namespace isolation to `lib/blorgh/engine.rb`:
82
+
83
+ ```ruby
84
+ module Blorgh
85
+ class Engine < ::Rails::Engine
86
+ isolate_namespace Blorgh
87
+ end
88
+ end
89
+ ```
90
+
91
+ 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`:
92
+
93
+ ```ruby
94
+ mount Blorgh::Engine, at: "blorgh"
95
+ ```
96
+
97
+ ### Inside an engine
98
+
99
+ #### Critical files
100
+
101
+ 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`:
102
+
103
+ ```ruby
104
+ gem 'blorgh', path: "vendor/engines/blorgh"
105
+ ```
106
+
107
+ 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`.
108
+
109
+ ```ruby
110
+ require "blorgh/engine"
111
+
112
+ module Blorgh
113
+ end
114
+ ```
115
+
116
+ TIP: Some engines choose to use this file to put global configuration options for their engine. It's a relatively good idea, and 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.
117
+
118
+ Within `lib/blorgh/engine.rb` is the base class for the engine:
119
+
120
+ ```ruby
121
+ module Blorgh
122
+ class Engine < Rails::Engine
123
+ isolate_namespace Blorgh
124
+ end
125
+ end
126
+ ```
127
+
128
+ 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.
129
+
130
+ 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 are helpers. Without calling `isolate_namespace`, engine's helpers would be included in an application's controllers.
131
+
132
+ 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.
133
+
134
+ What this isolation of the namespace means is that a model generated by a call to `rails g model` such as `rails g model post` won't be called `Post`, but instead be namespaced and called `Blorgh::Post`. In addition, the table for the model is namespaced, becoming `blorgh_posts`, rather than simply `posts`. Similar to the model namespacing, a controller called `PostsController` becomes `Blorgh::PostsController` and the views for that controller will not be at `app/views/posts`, but `app/views/blorgh/posts` instead. Mailers are namespaced as well.
135
+
136
+ 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.
137
+
138
+ #### `app` directory
139
+
140
+ 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 and so aren't described in this section. We'll look more into models in a future section, when we're writing the engine.
141
+
142
+ 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.
143
+
144
+ Within the `app/controllers` directory there is a `blorgh` directory and inside that 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.
145
+
146
+ 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.
147
+
148
+ Lastly, the `app/views` directory contains a `layouts` folder which contains a file at `blorgh/application.html.erb` 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 application's `app/views/layouts/application.html.erb` file.
149
+
150
+ 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.
151
+
152
+ #### `bin` directory
153
+
154
+ 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 very easily be able to generate new controllers and models for this engine by running commands like this:
155
+
156
+ ```bash
157
+ rails g model
158
+ ```
159
+
160
+ Keeping in mind, of course, that anything generated with these commands inside an engine that has `isolate_namespace` inside the `Engine` class will be namespaced.
161
+
162
+ #### `test` directory
163
+
164
+ 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:
165
+
166
+ ```ruby
167
+ Rails.application.routes.draw do
168
+ mount Blorgh::Engine => "/blorgh"
169
+ end
170
+ ```
171
+
172
+ This line mounts the engine at the path `/blorgh`, which will make it accessible through the application only at that path.
173
+
174
+ In 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 models tests.
175
+
176
+ Providing engine functionality
177
+ ------------------------------
178
+
179
+ The engine that this guide covers provides posting and commenting functionality and follows a similar thread to the [Getting Started Guide](getting_started.html), with some new twists.
180
+
181
+ ### Generating a post resource
182
+
183
+ The first thing to generate for a blog engine is the `Post` model and related controller. To quickly generate this, you can use the Rails scaffold generator.
184
+
185
+ ```bash
186
+ $ rails generate scaffold post title:string text:text
187
+ ```
188
+
189
+ This command will output this information:
190
+
191
+ ```
192
+ invoke active_record
193
+ create db/migrate/[timestamp]_create_blorgh_posts.rb
194
+ create app/models/blorgh/post.rb
195
+ invoke test_unit
196
+ create test/models/blorgh/post_test.rb
197
+ create test/fixtures/blorgh/posts.yml
198
+ route resources :posts
199
+ invoke scaffold_controller
200
+ create app/controllers/blorgh/posts_controller.rb
201
+ invoke erb
202
+ create app/views/blorgh/posts
203
+ create app/views/blorgh/posts/index.html.erb
204
+ create app/views/blorgh/posts/edit.html.erb
205
+ create app/views/blorgh/posts/show.html.erb
206
+ create app/views/blorgh/posts/new.html.erb
207
+ create app/views/blorgh/posts/_form.html.erb
208
+ invoke test_unit
209
+ create test/controllers/blorgh/posts_controller_test.rb
210
+ invoke helper
211
+ create app/helpers/blorgh/posts_helper.rb
212
+ invoke test_unit
213
+ create test/helpers/blorgh/posts_helper_test.rb
214
+ invoke assets
215
+ invoke js
216
+ create app/assets/javascripts/blorgh/posts.js
217
+ invoke css
218
+ create app/assets/stylesheets/blorgh/posts.css
219
+ invoke css
220
+ create app/assets/stylesheets/scaffold.css
221
+ ```
222
+
223
+ 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, however, that the migration is called `create_blorgh_posts` rather than the usual `create_posts`. This is due to the `isolate_namespace` method called in the `Blorgh::Engine` class's definition. The model here is also namespaced, being placed at `app/models/blorgh/post.rb` rather than `app/models/post.rb` due to the `isolate_namespace` call within the `Engine` class.
224
+
225
+ Next, the `test_unit` generator is invoked for this model, generating a model test at `test/models/blorgh/post_test.rb` (rather than `test/models/post_test.rb`) and a fixture at `test/fixtures/blorgh/posts.yml` (rather than `test/fixtures/posts.yml`).
226
+
227
+ After that, a line for the resource is inserted into the `config/routes.rb` file for the engine. This line is simply `resources :posts`, turning the `config/routes.rb` file for the engine into this:
228
+
229
+ ```ruby
230
+ Blorgh::Engine.routes.draw do
231
+ resources :posts
232
+ end
233
+ ```
234
+
235
+ 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
236
+ this guide describes it in details.
237
+
238
+ Next, the `scaffold_controller` generator is invoked, generating a controller called `Blorgh::PostsController` (at `app/controllers/blorgh/posts_controller.rb`) and its related views at `app/views/blorgh/posts`. This generator also generates a test for the controller (`test/controllers/blorgh/posts_controller_test.rb`) and a helper (`app/helpers/blorgh/posts_controller.rb`).
239
+
240
+ Everything this generator has created is neatly namespaced. The controller's class is defined within the `Blorgh` module:
241
+
242
+ ```ruby
243
+ module Blorgh
244
+ class PostsController < ApplicationController
245
+ ...
246
+ end
247
+ end
248
+ ```
249
+
250
+ NOTE: The `ApplicationController` class being inherited from here is the `Blorgh::ApplicationController`, not an application's `ApplicationController`.
251
+
252
+ The helper inside `app/helpers/blorgh/posts_helper.rb` is also namespaced:
253
+
254
+ ```ruby
255
+ module Blorgh
256
+ class PostsHelper
257
+ ...
258
+ end
259
+ end
260
+ ```
261
+
262
+ This helps prevent conflicts with any other engine or application that may have a post resource as well.
263
+
264
+ Finally, two files that are the assets for this resource are generated, `app/assets/javascripts/blorgh/posts.js` and `app/assets/stylesheets/blorgh/posts.css`. You'll see how to use these a little later.
265
+
266
+ By default, the scaffold styling is not applied to the engine as the engine's layout file, `app/views/layouts/blorgh/application.html.erb` doesn't load it. To make this apply, insert this line into the `<head>` tag of this layout:
267
+
268
+ ```erb
269
+ <%= stylesheet_link_tag "scaffold" %>
270
+ ```
271
+
272
+ You can see what the engine has so far by running `rake db:migrate` at the root of our engine to run the migration generated by the scaffold generator, and then running `rails server` in `test/dummy`. When you open `http://localhost:3000/blorgh/posts` you will see the default scaffold that has been generated. Click around! You've just generated your first engine's first functions.
273
+
274
+ If you'd rather play around in the console, `rails console` will also work just like a Rails application. Remember: the `Post` model is namespaced, so to reference it you must call it as `Blorgh::Post`.
275
+
276
+ ```ruby
277
+ >> Blorgh::Post.find(1)
278
+ => #<Blorgh::Post id: 1 ...>
279
+ ```
280
+
281
+ One final thing is that the `posts` 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 `config/routes.rb` file inside the engine:
282
+
283
+ ```ruby
284
+ root to: "posts#index"
285
+ ```
286
+
287
+ Now people will only need to go to the root of the engine to see all the posts, rather than visiting `/posts`. This means that instead of `http://localhost:3000/blorgh/posts`, you only need to go to `http://localhost:3000/blorgh` now.
288
+
289
+ ### Generating a comments resource
290
+
291
+ Now that the engine can create new blog posts, it only makes sense to add commenting functionality as well. To do this, you'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.
292
+
293
+ Run the model generator and tell it to generate a `Comment` model, with the related table having two columns: a `post_id` integer and `text` text column.
294
+
295
+ ```bash
296
+ $ rails generate model Comment post_id:integer text:text
297
+ ```
298
+
299
+ This will output the following:
300
+
301
+ ```
302
+ invoke active_record
303
+ create db/migrate/[timestamp]_create_blorgh_comments.rb
304
+ create app/models/blorgh/comment.rb
305
+ invoke test_unit
306
+ create test/models/blorgh/comment_test.rb
307
+ create test/fixtures/blorgh/comments.yml
308
+ ```
309
+
310
+ 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`.
311
+
312
+ To show the comments on a post, edit `app/views/blorgh/posts/show.html.erb` and add this line before the "Edit" link:
313
+
314
+ ```html+erb
315
+ <h3>Comments</h3>
316
+ <%= render @post.comments %>
317
+ ```
318
+
319
+ This line will require there to be a `has_many` association for comments defined on the `Blorgh::Post` model, which there isn't right now. To define one, open `app/models/blorgh/post.rb` and add this line into the model:
320
+
321
+ ```ruby
322
+ has_many :comments
323
+ ```
324
+
325
+ Turning the model into this:
326
+
327
+ ```ruby
328
+ module Blorgh
329
+ class Post < ActiveRecord::Base
330
+ has_many :comments
331
+ end
332
+ end
333
+ ```
334
+
335
+ 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.
336
+
337
+ 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 `render @post.comments` in `app/views/blorgh/posts/show.html.erb`:
338
+
339
+ ```erb
340
+ <%= render "blorgh/comments/form" %>
341
+ ```
342
+
343
+ 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:
344
+
345
+ ```html+erb
346
+ <h3>New comment</h3>
347
+ <%= form_for [@post, @post.comments.build] do |f| %>
348
+ <p>
349
+ <%= f.label :text %><br />
350
+ <%= f.text_area :text %>
351
+ </p>
352
+ <%= f.submit %>
353
+ <% end %>
354
+ ```
355
+
356
+ When this form is submitted, it is going to attempt to perform a `POST` request to a route of `/posts/:post_id/comments` within the engine. This route doesn't exist at the moment, but can be created by changing the `resources :posts` line inside `config/routes.rb` into these lines:
357
+
358
+ ```ruby
359
+ resources :posts do
360
+ resources :comments
361
+ end
362
+ ```
363
+
364
+ This creates a nested route for the comments, which is what the form requires.
365
+
366
+ The route now exists, but the controller that this route goes to does not. To create it, run this command:
367
+
368
+ ```bash
369
+ $ rails g controller comments
370
+ ```
371
+
372
+ This will generate the following things:
373
+
374
+ ```
375
+ create app/controllers/blorgh/comments_controller.rb
376
+ invoke erb
377
+ exist app/views/blorgh/comments
378
+ invoke test_unit
379
+ create test/controllers/blorgh/comments_controller_test.rb
380
+ invoke helper
381
+ create app/helpers/blorgh/comments_helper.rb
382
+ invoke test_unit
383
+ create test/helpers/blorgh/comments_helper_test.rb
384
+ invoke assets
385
+ invoke js
386
+ create app/assets/javascripts/blorgh/comments.js
387
+ invoke css
388
+ create app/assets/stylesheets/blorgh/comments.css
389
+ ```
390
+
391
+ The form will be making a `POST` request to `/posts/:post_id/comments`, which will correspond with the `create` action in `Blorgh::CommentsController`. This action needs to be created and can be done by putting the following lines inside the class definition in `app/controllers/blorgh/comments_controller.rb`:
392
+
393
+ ```ruby
394
+ def create
395
+ @post = Post.find(params[:post_id])
396
+ @comment = @post.comments.create(params[:comment])
397
+ flash[:notice] = "Comment has been created!"
398
+ redirect_to posts_path
399
+ end
400
+ ```
401
+
402
+ 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:
403
+
404
+ ```
405
+ Missing partial blorgh/comments/comment with {:handlers=>[:erb, :builder], :formats=>[:html], :locale=>[:en, :en]}. Searched in:
406
+ * "/Users/ryan/Sites/side_projects/blorgh/test/dummy/app/views"
407
+ * "/Users/ryan/Sites/side_projects/blorgh/app/views"
408
+ ```
409
+
410
+ 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 this error. The engine knows to look for `blorgh/comments/comment` because the model object it is receiving is from the `Blorgh::Comment` class.
411
+
412
+ 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:
413
+
414
+ ```erb
415
+ <%= comment_counter + 1 %>. <%= comment.text %>
416
+ ```
417
+
418
+ The `comment_counter` local variable is given to us by the `<%= render @post.comments %>` call, as it will define this automatically and increment the 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.
419
+
420
+ That completes the comment function of the blogging engine. Now it's time to use it within an application.
421
+
422
+ Hooking into an application
423
+ ---------------------------
424
+
425
+ 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 ownership for posts and comments within the engine.
426
+
427
+ ### Mounting the engine
428
+
429
+ 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:
430
+
431
+ ```bash
432
+ $ rails new unicorn
433
+ ```
434
+
435
+ Usually, specifying the engine inside the Gemfile would be done by specifying it as a normal, everyday gem.
436
+
437
+ ```ruby
438
+ gem 'devise'
439
+ ```
440
+
441
+ However, because you are developing the `blorgh` engine on your local machine, you will need to specify the `:path` option in your `Gemfile`:
442
+
443
+ ```ruby
444
+ gem 'blorgh', path: "/path/to/blorgh"
445
+ ```
446
+
447
+ As described earlier, by placing the gem in the `Gemfile` it will be loaded when Rails is loaded, as it will first require `lib/blorgh.rb` in the engine and then `lib/blorgh/engine.rb`, which is the file that defines the major pieces of functionality for the engine.
448
+
449
+ To make the engine's functionality accessible from within an application, it needs to be mounted in that application's `config/routes.rb` file:
450
+
451
+ ```ruby
452
+ mount Blorgh::Engine, at: "/blog"
453
+ ```
454
+
455
+ 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`.
456
+
457
+ 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.
458
+
459
+ ### Engine setup
460
+
461
+ The engine contains migrations for the `blorgh_posts` and `blorgh_comments` 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 application use this command:
462
+
463
+ ```bash
464
+ $ rake blorgh:install:migrations
465
+ ```
466
+
467
+ If you have multiple engines that need migrations copied over, use `railties:install:migrations` instead:
468
+
469
+ ```bash
470
+ $ rake railties:install:migrations
471
+ ```
472
+
473
+ 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:
474
+
475
+ ```bash
476
+ Copied migration [timestamp_1]_create_blorgh_posts.rb from blorgh
477
+ Copied migration [timestamp_2]_create_blorgh_comments.rb from blorgh
478
+ ```
479
+
480
+ 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.
481
+
482
+ To run these migrations within the context of the application, simply run `rake db:migrate`. When accessing the engine through `http://localhost:3000/blog`, 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'll find that it's the same as when it was only an engine.
483
+
484
+ If you would like to run migrations only from one engine, you can do it by specifying `SCOPE`:
485
+
486
+ ```bash
487
+ rake db:migrate SCOPE=blorgh
488
+ ```
489
+
490
+ This may be useful if you want to revert engine's migrations before removing it. In order to revert all migrations from blorgh engine you can run such code:
491
+
492
+ ```bash
493
+ rake db:migrate SCOPE=blorgh VERSION=0
494
+ ```
495
+
496
+ ### Using a class provided by the application
497
+
498
+ #### Using a model provided by the application
499
+
500
+ 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 `blorgh` engine, making posts and comments have authors would make a lot of sense.
501
+
502
+ A typical application might have a `User` class that would be used to represent authors for a post 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.
503
+
504
+ To keep it simple in this case, the application will have a class called `User` which will represent the users of the application. It can be generated using this command inside the application:
505
+
506
+ ```bash
507
+ rails g model user name:string
508
+ ```
509
+
510
+ The `rake db:migrate` command needs to be run here to ensure that our application has the `users` table for future use.
511
+
512
+ Also, to keep it simple, the posts form will have a new text field called `author_name` where users can elect to put their name. The engine will then take this name and create a new `User` object from it or find one that already has that name, and then associate the post with it.
513
+
514
+ First, the `author_name` text field needs to be added to the `app/views/blorgh/posts/_form.html.erb` partial inside the engine. This can be added above the `title` field with this code:
515
+
516
+ ```html+erb
517
+ <div class="field">
518
+ <%= f.label :author_name %><br />
519
+ <%= f.text_field :author_name %>
520
+ </div>
521
+ ```
522
+
523
+ The `Blorgh::Post` model should then have some code to convert the `author_name` field into an actual `User` object and associate it as that post's `author` before the post is saved. It will also need to have an `attr_accessor` setup for this field so that the setter and getter methods are defined for it.
524
+
525
+ 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 `app/models/blorgh/post.rb`. The `author` association will be hard-coded to the `User` class for the time being.
526
+
527
+ ```ruby
528
+ attr_accessor :author_name
529
+ belongs_to :author, class_name: "User"
530
+
531
+ before_save :set_author
532
+
533
+ private
534
+ def set_author
535
+ self.author = User.find_or_create_by(name: author_name)
536
+ end
537
+ ```
538
+
539
+ By defining that the `author` association's object is represented by the `User` class a link is established between the engine and the application. There needs to be a way of associating the records in the `blorgh_posts` table with the records in the `users` table. Because the association is called `author`, there should be an `author_id` column added to the `blorgh_posts` table.
540
+
541
+ To generate this new column, run this command within the engine:
542
+
543
+ ```bash
544
+ $ rails g migration add_author_id_to_blorgh_posts author_id:integer
545
+ ```
546
+
547
+ 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.
548
+
549
+ This migration will need to be run on the application. To do that, it must first be copied using this command:
550
+
551
+ ```bash
552
+ $ rake blorgh:install:migrations
553
+ ```
554
+
555
+ Notice here 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.
556
+
557
+ ```
558
+ NOTE Migration [timestamp]_create_blorgh_posts.rb from blorgh has been skipped. Migration with the same name already exists.
559
+ NOTE Migration [timestamp]_create_blorgh_comments.rb from blorgh has been skipped. Migration with the same name already exists.
560
+ Copied migration [timestamp]_add_author_id_to_blorgh_posts.rb from blorgh
561
+ ```
562
+
563
+ Run this migration using this command:
564
+
565
+ ```bash
566
+ $ rake db:migrate
567
+ ```
568
+
569
+ Now with all the pieces in place, an action will take place that will associate an author — represented by a record in the `users` table — with a post, represented by the `blorgh_posts` table from the engine.
570
+
571
+ Finally, the author's name should be displayed on the post's page. Add this code above the "Title" output inside `app/views/blorgh/posts/show.html.erb`:
572
+
573
+ ```html+erb
574
+ <p>
575
+ <b>Author:</b>
576
+ <%= @post.author %>
577
+ </p>
578
+ ```
579
+
580
+ By outputting `@post.author` using the `<%=` tag, the `to_s` method will be called on the object. By default, this will look quite ugly:
581
+
582
+ ```
583
+ #<User:0x00000100ccb3b0>
584
+ ```
585
+
586
+ This is undesirable and it would be much better to have the user's name there. To do this, add a `to_s` method to the `User` class within the application:
587
+
588
+ ```ruby
589
+ def to_s
590
+ name
591
+ end
592
+ ```
593
+
594
+ Now instead of the ugly Ruby object output the author's name will be displayed.
595
+
596
+ #### Using a controller provided by the application
597
+
598
+ Because Rails controllers generally share code for things like authentication and accessing session variables, by default they inherit from `ApplicationController`. 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 should 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:
599
+
600
+ ```ruby
601
+ class Blorgh::ApplicationController < ApplicationController
602
+ end
603
+ ```
604
+
605
+ By default, the engine's controllers inherit from `Blorgh::ApplicationController`. So, after making this change they will have access to the main applications `ApplicationController` as though they were part of the main application.
606
+
607
+ This change does require that the engine is run from a Rails application that has an `ApplicationController`.
608
+
609
+ ### Configuring an engine
610
+
611
+ This section covers how to make the `User` class configurable, followed by general configuration tips for the engine.
612
+
613
+ #### Setting configuration settings in the application
614
+
615
+ The next step is to make the class that represents a `User` in the application customizable for the engine. This is because, as explained before, that class may not always be `User`. To make this customizable, the engine will have a configuration setting called `user_class` that will be used to specify what the class representing users is inside the application.
616
+
617
+ To define this configuration setting, you should use a `mattr_accessor` inside the `Blorgh` module for the engine, located at `lib/blorgh.rb` inside the engine. Inside this module, put this line:
618
+
619
+ ```ruby
620
+ mattr_accessor :user_class
621
+ ```
622
+
623
+ 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.user_class`.
624
+
625
+ The next step is switching the `Blorgh::Post` model over to this new setting. For the `belongs_to` association inside this model (`app/models/blorgh/post.rb`), it will now become this:
626
+
627
+ ```ruby
628
+ belongs_to :author, class_name: Blorgh.user_class
629
+ ```
630
+
631
+ The `set_author` method also located in this class should also use this class:
632
+
633
+ ```ruby
634
+ self.author = Blorgh.user_class.constantize.find_or_create_by(name: author_name)
635
+ ```
636
+
637
+ To save having to call `constantize` on the `user_class` result all the time, you could instead just override the `user_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:
638
+
639
+ ```ruby
640
+ def self.user_class
641
+ @@user_class.constantize
642
+ end
643
+ ```
644
+
645
+ This would then turn the above code for `set_author` into this:
646
+
647
+ ```ruby
648
+ self.author = Blorgh.user_class.find_or_create_by(name: author_name)
649
+ ```
650
+
651
+ Resulting in something a little shorter, and more implicit in its behavior. The `user_class` method should always return a `Class` object.
652
+
653
+ Since we changed the `user_class` method to no longer return a
654
+ `String` but a `Class` we must also modify our `belongs_to` definition
655
+ in the `Blorgh::Post` model:
656
+
657
+ ```ruby
658
+ belongs_to :author, class_name: Blorgh.user_class.to_s
659
+ ```
660
+
661
+ 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.
662
+
663
+ Create a new initializer at `config/initializers/blorgh.rb` inside the application where the `blorgh` engine is installed and put this content in it:
664
+
665
+ ```ruby
666
+ Blorgh.user_class = "User"
667
+ ```
668
+
669
+ 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, which 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.
670
+
671
+ 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 `config/initializers/blorgh.rb` to learn what the class is.
672
+
673
+ 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 associated with a post when it's created. This object, of course, should have some sort of identifier by which it can be referenced.
674
+
675
+ #### General engine configuration
676
+
677
+ 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!
678
+
679
+ 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 functionality is explained in the [Initializers section](http://guides.rubyonrails.org/configuring.html#initializers) of the Configuring guide, and works precisely the same way as the `config/initializers` directory inside an application. Same goes for if you want to use a standard initializer.
680
+
681
+ For locales, simply place the locale files in the `config/locales` directory, just like you would in an application.
682
+
683
+ Testing an engine
684
+ -----------------
685
+
686
+ 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.
687
+
688
+ The `test` directory should be treated like a typical Rails testing environment, allowing for unit, functional and integration tests.
689
+
690
+ ### Functional tests
691
+
692
+ 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:
693
+
694
+ ```ruby
695
+ get :index
696
+ ```
697
+
698
+ 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 do this, you must pass the `:use_route` option (as a parameter) on these requests also:
699
+
700
+ ```ruby
701
+ get :index, use_route: :blorgh
702
+ ```
703
+
704
+ This tells the application that you still want to perform a `GET` request to the `index` action of this controller, just that you want to use the engine's route to get there, rather than the application.
705
+
706
+ Improving engine functionality
707
+ ------------------------------
708
+
709
+ This section explains how to add and/or override engine MVC functionality in the main Rails application.
710
+
711
+ ### Overriding Models and Controllers
712
+
713
+ 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.
714
+
715
+ For simple class modifications use `Class#class_eval`, and for complex class modifications, consider using `ActiveSupport::Concern`.
716
+
717
+ #### Implementing Decorator Pattern Using Class#class_eval
718
+
719
+ **Adding** `Post#time_since_created`,
720
+
721
+ ```ruby
722
+ # MyApp/app/decorators/models/blorgh/post_decorator.rb
723
+
724
+ Blorgh::Post.class_eval do
725
+ def time_since_created
726
+ Time.current - created_at
727
+ end
728
+ end
729
+ ```
730
+
731
+ ```ruby
732
+ # Blorgh/app/models/post.rb
733
+
734
+ class Post < ActiveRecord::Base
735
+ has_many :comments
736
+ end
737
+ ```
738
+
739
+
740
+ **Overriding** `Post#summary`
741
+
742
+ ```ruby
743
+ # MyApp/app/decorators/models/blorgh/post_decorator.rb
744
+
745
+ Blorgh::Post.class_eval do
746
+ def summary
747
+ "#{title} - #{truncate(text)}"
748
+ end
749
+ end
750
+ ```
751
+
752
+ ```ruby
753
+ # Blorgh/app/models/post.rb
754
+
755
+ class Post < ActiveRecord::Base
756
+ has_many :comments
757
+ def summary
758
+ "#{title}"
759
+ end
760
+ end
761
+ ```
762
+
763
+ #### Implementing Decorator Pattern Using ActiveSupport::Concern
764
+
765
+ Using `Class#class_eval` is great for simple adjustments, but for more complex class modifications, you might want to consider using [`ActiveSupport::Concern`](http://edgeapi.rubyonrails.org/classes/ActiveSupport/Concern.html). ActiveSupport::Concern manages load order of interlinked dependent modules and classes at run time allowing you to significantly modularize your code.
766
+
767
+ **Adding** `Post#time_since_created` and **Overriding** `Post#summary`
768
+
769
+ ```ruby
770
+ # MyApp/app/models/blorgh/post.rb
771
+
772
+ class Blorgh::Post < ActiveRecord::Base
773
+ include Blorgh::Concerns::Models::Post
774
+
775
+ def time_since_created
776
+ Time.current - created_at
777
+ end
778
+
779
+ def summary
780
+ "#{title} - #{truncate(text)}"
781
+ end
782
+ end
783
+ ```
784
+
785
+ ```ruby
786
+ # Blorgh/app/models/post.rb
787
+
788
+ class Post < ActiveRecord::Base
789
+ include Blorgh::Concerns::Models::Post
790
+ end
791
+ ```
792
+
793
+ ```ruby
794
+ # Blorgh/lib/concerns/models/post
795
+
796
+ module Blorgh::Concerns::Models::Post
797
+ extend ActiveSupport::Concern
798
+
799
+ # 'included do' causes the included code to be evaluated in the
800
+ # context where it is included (post.rb), rather than be
801
+ # executed in the module's context (blorgh/concerns/models/post).
802
+ included do
803
+ attr_accessor :author_name
804
+ belongs_to :author, class_name: "User"
805
+
806
+ before_save :set_author
807
+
808
+ private
809
+
810
+ def set_author
811
+ self.author = User.find_or_create_by(name: author_name)
812
+ end
813
+ end
814
+
815
+ def summary
816
+ "#{title}"
817
+ end
818
+
819
+ module ClassMethods
820
+ def some_class_method
821
+ 'some class method string'
822
+ end
823
+ end
824
+ end
825
+ ```
826
+
827
+ ### Overriding views
828
+
829
+ 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, then it will check in the `app/views` directories of all engines which have this directory.
830
+
831
+ In the `blorgh` engine, there is a currently a file at `app/views/blorgh/posts/index.html.erb`. When the engine is asked to render the view for `Blorgh::PostsController`'s `index` action, it will first see if it can find it at `app/views/blorgh/posts/index.html.erb` within the application and then if it cannot it will look inside the engine.
832
+
833
+ You can override this view in the application by simply creating a new file at `app/views/blorgh/posts/index.html.erb`. Then you can completely change what this view would normally output.
834
+
835
+ Try this now by creating a new file at `app/views/blorgh/posts/index.html.erb` and put this content in it:
836
+
837
+ ```html+erb
838
+ <h1>Posts</h1>
839
+ <%= link_to "New Post", new_post_path %>
840
+ <% @posts.each do |post| %>
841
+ <h2><%= post.title %></h2>
842
+ <small>By <%= post.author %></small>
843
+ <%= simple_format(post.text) %>
844
+ <hr>
845
+ <% end %>
846
+ ```
847
+
848
+ ### Routes
849
+
850
+ Routes inside an engine are, by default, isolated from the application. 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.
851
+
852
+ Routes inside an engine are drawn on the `Engine` class within `config/routes.rb`, like this:
853
+
854
+ ```ruby
855
+ Blorgh::Engine.routes.draw do
856
+ resources :posts
857
+ end
858
+ ```
859
+
860
+ 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 proxy method. Calls to normal routing methods such as `posts_path` may end up going to undesired locations if both the application and the engine both have such a helper defined.
861
+
862
+ For instance, the following example would go to the application's `posts_path` if that template was rendered from the application, or the engine's `posts_path` if it was rendered from the engine:
863
+
864
+ ```erb
865
+ <%= link_to "Blog posts", posts_path %>
866
+ ```
867
+
868
+ To make this route always use the engine's `posts_path` routing helper method, we must call the method on the routing proxy method that shares the same name as the engine.
869
+
870
+ ```erb
871
+ <%= link_to "Blog posts", blorgh.posts_path %>
872
+ ```
873
+
874
+ If you wish to reference the application inside the engine in a similar way, use the `main_app` helper:
875
+
876
+ ```erb
877
+ <%= link_to "Home", main_app.root_path %>
878
+ ```
879
+
880
+ 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.
881
+
882
+ If a template is rendered from within an engine and it's attempting 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.
883
+
884
+ ### Assets
885
+
886
+ 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 in the engine's `app/assets` and `lib/assets` directories for potential assets.
887
+
888
+ Much like all the other components of an engine, the assets should also be namespaced. This means 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 wasn't namespaced, then 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 all but ignored.
889
+
890
+ 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:
891
+
892
+ ```erb
893
+ <%= stylesheet_link_tag "blorgh/style.css" %>
894
+ ```
895
+
896
+ You can also specify these assets as dependencies of other assets using the Asset Pipeline require statements in processed files:
897
+
898
+ ```
899
+ /*
900
+ *= require blorgh/style
901
+ */
902
+ ```
903
+
904
+ INFO. Remember that in order to use languages like Sass or CoffeeScript, you should add the relevant library to your engine's `.gemspec`.
905
+
906
+ ### Separate Assets & Precompiling
907
+
908
+ There are some situations where your engine's assets are not required by the host application. For example, say that you've created
909
+ an admin functionality that only exists for your engine. In this case, the host application doesn't need to require `admin.css`
910
+ or `admin.js`. Only the gem's admin layout needs these assets. It doesn't make sense for the host app to include `"blorg/admin.css"` in it's stylesheets. In this situation, you should explicitly define these assets for precompilation.
911
+ This tells sprockets to add your engine assets when `rake assets:precompile` is ran.
912
+
913
+ You can define assets for precompilation in `engine.rb`
914
+
915
+ ```ruby
916
+ initializer "blorgh.assets.precompile" do |app|
917
+ app.config.assets.precompile += %w(admin.css admin.js)
918
+ end
919
+ ```
920
+
921
+ For more information, read the [Asset Pipeline guide](http://guides.rubyonrails.org/asset_pipeline.html)
922
+
923
+ ### Other gem dependencies
924
+
925
+ Gem dependencies inside an engine should be specified inside the
926
+ `.gemspec` file at the root of the engine. The reason is that the engine may
927
+ be installed as a gem. If dependencies were to be specified inside the `Gemfile`,
928
+ these would not be recognized by a traditional gem install and so they would not
929
+ be installed, causing the engine to malfunction.
930
+
931
+ To specify a dependency that should be installed with the engine during a
932
+ traditional `gem install`, specify it inside the `Gem::Specification` block
933
+ inside the `.gemspec` file in the engine:
934
+
935
+ ```ruby
936
+ s.add_dependency "moo"
937
+ ```
938
+
939
+ To specify a dependency that should only be installed as a development
940
+ dependency of the application, specify it like this:
941
+
942
+ ```ruby
943
+ s.add_development_dependency "moo"
944
+ ```
945
+
946
+ Both kinds of dependencies will be installed when `bundle install` is run inside
947
+ the application. The development dependencies for the gem will only be used when
948
+ the tests for the engine are running.
949
+
950
+ Note that if you want to immediately require dependencies when the engine is
951
+ required, you should require them before the engine's initialization. For example:
952
+
953
+ ```ruby
954
+ require 'other_engine/engine'
955
+ require 'yet_another_engine/engine'
956
+
957
+ module MyEngine
958
+ class Engine < ::Rails::Engine
959
+ end
960
+ end
961
+ ```