railties 3.0.0.beta

Sign up to get free protection for your applications and to get access to all the features.
Files changed (334) hide show
  1. data/CHANGELOG +2201 -0
  2. data/README +286 -0
  3. data/bin/rails +11 -0
  4. data/builtin/rails_info/rails/info.rb +147 -0
  5. data/builtin/rails_info/rails/info_controller.rb +15 -0
  6. data/builtin/rails_info/rails/info_helper.rb +2 -0
  7. data/builtin/routes.rb +3 -0
  8. data/guides/files/javascripts/code_highlighter.js +188 -0
  9. data/guides/files/javascripts/guides.js +7 -0
  10. data/guides/files/javascripts/highlighters.js +90 -0
  11. data/guides/files/stylesheets/main.css +441 -0
  12. data/guides/files/stylesheets/print.css +52 -0
  13. data/guides/files/stylesheets/reset.css +43 -0
  14. data/guides/files/stylesheets/style.css +13 -0
  15. data/guides/files/stylesheets/syntax.css +31 -0
  16. data/guides/images/belongs_to.png +0 -0
  17. data/guides/images/book_icon.gif +0 -0
  18. data/guides/images/bullet.gif +0 -0
  19. data/guides/images/chapters_icon.gif +0 -0
  20. data/guides/images/check_bullet.gif +0 -0
  21. data/guides/images/credits_pic_blank.gif +0 -0
  22. data/guides/images/csrf.png +0 -0
  23. data/guides/images/customized_error_messages.png +0 -0
  24. data/guides/images/error_messages.png +0 -0
  25. data/guides/images/feature_tile.gif +0 -0
  26. data/guides/images/footer_tile.gif +0 -0
  27. data/guides/images/fxn.png +0 -0
  28. data/guides/images/grey_bullet.gif +0 -0
  29. data/guides/images/habtm.png +0 -0
  30. data/guides/images/has_many.png +0 -0
  31. data/guides/images/has_many_through.png +0 -0
  32. data/guides/images/has_one.png +0 -0
  33. data/guides/images/has_one_through.png +0 -0
  34. data/guides/images/header_backdrop.png +0 -0
  35. data/guides/images/header_tile.gif +0 -0
  36. data/guides/images/i18n/demo_localized_pirate.png +0 -0
  37. data/guides/images/i18n/demo_translated_en.png +0 -0
  38. data/guides/images/i18n/demo_translated_pirate.png +0 -0
  39. data/guides/images/i18n/demo_translation_missing.png +0 -0
  40. data/guides/images/i18n/demo_untranslated.png +0 -0
  41. data/guides/images/icons/README +5 -0
  42. data/guides/images/icons/callouts/1.png +0 -0
  43. data/guides/images/icons/callouts/10.png +0 -0
  44. data/guides/images/icons/callouts/11.png +0 -0
  45. data/guides/images/icons/callouts/12.png +0 -0
  46. data/guides/images/icons/callouts/13.png +0 -0
  47. data/guides/images/icons/callouts/14.png +0 -0
  48. data/guides/images/icons/callouts/15.png +0 -0
  49. data/guides/images/icons/callouts/2.png +0 -0
  50. data/guides/images/icons/callouts/3.png +0 -0
  51. data/guides/images/icons/callouts/4.png +0 -0
  52. data/guides/images/icons/callouts/5.png +0 -0
  53. data/guides/images/icons/callouts/6.png +0 -0
  54. data/guides/images/icons/callouts/7.png +0 -0
  55. data/guides/images/icons/callouts/8.png +0 -0
  56. data/guides/images/icons/callouts/9.png +0 -0
  57. data/guides/images/icons/caution.png +0 -0
  58. data/guides/images/icons/example.png +0 -0
  59. data/guides/images/icons/home.png +0 -0
  60. data/guides/images/icons/important.png +0 -0
  61. data/guides/images/icons/next.png +0 -0
  62. data/guides/images/icons/note.png +0 -0
  63. data/guides/images/icons/prev.png +0 -0
  64. data/guides/images/icons/tip.png +0 -0
  65. data/guides/images/icons/up.png +0 -0
  66. data/guides/images/icons/warning.png +0 -0
  67. data/guides/images/nav_arrow.gif +0 -0
  68. data/guides/images/polymorphic.png +0 -0
  69. data/guides/images/posts_index.png +0 -0
  70. data/guides/images/rails_guides_logo.gif +0 -0
  71. data/guides/images/rails_logo_remix.gif +0 -0
  72. data/guides/images/rails_welcome.png +0 -0
  73. data/guides/images/session_fixation.png +0 -0
  74. data/guides/images/tab_grey.gif +0 -0
  75. data/guides/images/tab_info.gif +0 -0
  76. data/guides/images/tab_note.gif +0 -0
  77. data/guides/images/tab_red.gif +0 -0
  78. data/guides/images/tab_yellow.gif +0 -0
  79. data/guides/images/tab_yellow.png +0 -0
  80. data/guides/images/validation_error_messages.png +0 -0
  81. data/guides/rails_guides.rb +44 -0
  82. data/guides/rails_guides/generator.rb +178 -0
  83. data/guides/rails_guides/helpers.rb +34 -0
  84. data/guides/rails_guides/indexer.rb +55 -0
  85. data/guides/rails_guides/levenshtein.rb +31 -0
  86. data/guides/rails_guides/textile_extensions.rb +41 -0
  87. data/guides/source/2_2_release_notes.textile +422 -0
  88. data/guides/source/2_3_release_notes.textile +610 -0
  89. data/guides/source/3_0_release_notes.textile +560 -0
  90. data/guides/source/action_controller_overview.textile +783 -0
  91. data/guides/source/action_mailer_basics.textile +437 -0
  92. data/guides/source/action_view_overview.textile +1481 -0
  93. data/guides/source/active_record_basics.textile +204 -0
  94. data/guides/source/active_record_querying.textile +961 -0
  95. data/guides/source/active_support_core_extensions.textile +1835 -0
  96. data/guides/source/activerecord_validations_callbacks.textile +1127 -0
  97. data/guides/source/ajax_on_rails.textile +342 -0
  98. data/guides/source/association_basics.textile +1816 -0
  99. data/guides/source/caching_with_rails.textile +390 -0
  100. data/guides/source/command_line.textile +589 -0
  101. data/guides/source/configuring.textile +290 -0
  102. data/guides/source/contribute.textile +71 -0
  103. data/guides/source/contributing_to_rails.textile +272 -0
  104. data/guides/source/credits.textile.erb +52 -0
  105. data/guides/source/debugging_rails_applications.textile +709 -0
  106. data/guides/source/form_helpers.textile +768 -0
  107. data/guides/source/generators.textile +378 -0
  108. data/guides/source/getting_started.textile +1310 -0
  109. data/guides/source/i18n.textile +879 -0
  110. data/guides/source/index.textile.erb +124 -0
  111. data/guides/source/layout.html.erb +103 -0
  112. data/guides/source/layouts_and_rendering.textile +979 -0
  113. data/guides/source/migrations.textile +591 -0
  114. data/guides/source/nested_model_forms.textile +222 -0
  115. data/guides/source/performance_testing.textile +531 -0
  116. data/guides/source/plugins.textile +1512 -0
  117. data/guides/source/rails_application_templates.textile +238 -0
  118. data/guides/source/rails_on_rack.textile +306 -0
  119. data/guides/source/routing.textile +903 -0
  120. data/guides/source/security.textile +984 -0
  121. data/guides/source/testing.textile +946 -0
  122. data/lib/generators/erb.rb +8 -0
  123. data/lib/generators/erb/controller/controller_generator.rb +21 -0
  124. data/lib/generators/erb/controller/templates/view.html.erb +2 -0
  125. data/lib/generators/erb/mailer/mailer_generator.rb +20 -0
  126. data/lib/generators/erb/mailer/templates/view.text.erb +3 -0
  127. data/lib/generators/erb/scaffold/scaffold_generator.rb +53 -0
  128. data/lib/generators/erb/scaffold/templates/_form.html.erb +13 -0
  129. data/lib/generators/erb/scaffold/templates/edit.html.erb +6 -0
  130. data/lib/generators/erb/scaffold/templates/index.html.erb +27 -0
  131. data/lib/generators/erb/scaffold/templates/layout.html.erb +16 -0
  132. data/lib/generators/erb/scaffold/templates/new.html.erb +5 -0
  133. data/lib/generators/erb/scaffold/templates/show.html.erb +10 -0
  134. data/lib/generators/rails/app/USAGE +9 -0
  135. data/lib/generators/rails/app/app_generator.rb +262 -0
  136. data/lib/generators/rails/app/templates/Gemfile +34 -0
  137. data/lib/generators/rails/app/templates/README +243 -0
  138. data/lib/generators/rails/app/templates/Rakefile +10 -0
  139. data/lib/generators/rails/app/templates/app/controllers/application_controller.rb +3 -0
  140. data/lib/generators/rails/app/templates/app/helpers/application_helper.rb +2 -0
  141. data/lib/generators/rails/app/templates/app/models/.empty_directory +0 -0
  142. data/lib/generators/rails/app/templates/app/views/layouts/.empty_directory +0 -0
  143. data/lib/generators/rails/app/templates/config.ru +4 -0
  144. data/lib/generators/rails/app/templates/config/application.rb +51 -0
  145. data/lib/generators/rails/app/templates/config/boot.rb +17 -0
  146. data/lib/generators/rails/app/templates/config/databases/frontbase.yml +28 -0
  147. data/lib/generators/rails/app/templates/config/databases/ibm_db.yml +71 -0
  148. data/lib/generators/rails/app/templates/config/databases/mysql.yml +60 -0
  149. data/lib/generators/rails/app/templates/config/databases/oracle.yml +39 -0
  150. data/lib/generators/rails/app/templates/config/databases/postgresql.yml +51 -0
  151. data/lib/generators/rails/app/templates/config/databases/sqlite3.yml +22 -0
  152. data/lib/generators/rails/app/templates/config/environment.rb +5 -0
  153. data/lib/generators/rails/app/templates/config/environments/development.rb.tt +19 -0
  154. data/lib/generators/rails/app/templates/config/environments/production.rb.tt +33 -0
  155. data/lib/generators/rails/app/templates/config/environments/test.rb.tt +29 -0
  156. data/lib/generators/rails/app/templates/config/initializers/backtrace_silencers.rb +7 -0
  157. data/lib/generators/rails/app/templates/config/initializers/cookie_verification_secret.rb.tt +7 -0
  158. data/lib/generators/rails/app/templates/config/initializers/inflections.rb +10 -0
  159. data/lib/generators/rails/app/templates/config/initializers/mime_types.rb +5 -0
  160. data/lib/generators/rails/app/templates/config/initializers/session_store.rb.tt +15 -0
  161. data/lib/generators/rails/app/templates/config/locales/en.yml +5 -0
  162. data/lib/generators/rails/app/templates/config/routes.rb +58 -0
  163. data/lib/generators/rails/app/templates/db/seeds.rb +7 -0
  164. data/lib/generators/rails/app/templates/doc/README_FOR_APP +2 -0
  165. data/lib/generators/rails/app/templates/gitignore +4 -0
  166. data/lib/generators/rails/app/templates/public/404.html +26 -0
  167. data/lib/generators/rails/app/templates/public/422.html +26 -0
  168. data/lib/generators/rails/app/templates/public/500.html +26 -0
  169. data/lib/generators/rails/app/templates/public/favicon.ico +0 -0
  170. data/lib/generators/rails/app/templates/public/images/rails.png +0 -0
  171. data/lib/generators/rails/app/templates/public/index.html +278 -0
  172. data/lib/generators/rails/app/templates/public/javascripts/application.js +2 -0
  173. data/lib/generators/rails/app/templates/public/javascripts/controls.js +963 -0
  174. data/lib/generators/rails/app/templates/public/javascripts/dragdrop.js +973 -0
  175. data/lib/generators/rails/app/templates/public/javascripts/effects.js +1128 -0
  176. data/lib/generators/rails/app/templates/public/javascripts/prototype.js +4320 -0
  177. data/lib/generators/rails/app/templates/public/javascripts/rails.js +110 -0
  178. data/lib/generators/rails/app/templates/public/robots.txt +5 -0
  179. data/lib/generators/rails/app/templates/public/stylesheets/.empty_directory +0 -0
  180. data/lib/generators/rails/app/templates/script/rails +9 -0
  181. data/lib/generators/rails/app/templates/test/fixtures/.empty_directory +0 -0
  182. data/lib/generators/rails/app/templates/test/functional/.empty_directory +0 -0
  183. data/lib/generators/rails/app/templates/test/integration/.empty_directory +0 -0
  184. data/lib/generators/rails/app/templates/test/performance/browsing_test.rb +9 -0
  185. data/lib/generators/rails/app/templates/test/test_helper.rb +13 -0
  186. data/lib/generators/rails/app/templates/test/unit/.empty_directory +0 -0
  187. data/lib/generators/rails/controller/USAGE +18 -0
  188. data/lib/generators/rails/controller/controller_generator.rb +14 -0
  189. data/lib/generators/rails/controller/templates/controller.rb +7 -0
  190. data/lib/generators/rails/generator/USAGE +11 -0
  191. data/lib/generators/rails/generator/generator_generator.rb +25 -0
  192. data/lib/generators/rails/generator/templates/%file_name%_generator.rb.tt +5 -0
  193. data/lib/generators/rails/generator/templates/USAGE.tt +8 -0
  194. data/lib/generators/rails/generator/templates/templates/.empty_directory +0 -0
  195. data/lib/generators/rails/helper/USAGE +17 -0
  196. data/lib/generators/rails/helper/helper_generator.rb +13 -0
  197. data/lib/generators/rails/helper/templates/helper.rb +2 -0
  198. data/lib/generators/rails/integration_test/USAGE +10 -0
  199. data/lib/generators/rails/integration_test/integration_test_generator.rb +7 -0
  200. data/lib/generators/rails/mailer/USAGE +15 -0
  201. data/lib/generators/rails/mailer/mailer_generator.rb +14 -0
  202. data/lib/generators/rails/mailer/templates/mailer.rb +16 -0
  203. data/lib/generators/rails/metal/USAGE +8 -0
  204. data/lib/generators/rails/metal/metal_generator.rb +11 -0
  205. data/lib/generators/rails/metal/templates/metal.rb +12 -0
  206. data/lib/generators/rails/migration/USAGE +29 -0
  207. data/lib/generators/rails/migration/migration_generator.rb +8 -0
  208. data/lib/generators/rails/model/USAGE +30 -0
  209. data/lib/generators/rails/model/model_generator.rb +8 -0
  210. data/lib/generators/rails/model_subclass/model_subclass_generator.rb +12 -0
  211. data/lib/generators/rails/observer/USAGE +12 -0
  212. data/lib/generators/rails/observer/observer_generator.rb +7 -0
  213. data/lib/generators/rails/performance_test/USAGE +10 -0
  214. data/lib/generators/rails/performance_test/performance_test_generator.rb +7 -0
  215. data/lib/generators/rails/plugin/USAGE +13 -0
  216. data/lib/generators/rails/plugin/plugin_generator.rb +47 -0
  217. data/lib/generators/rails/plugin/templates/MIT-LICENSE.tt +20 -0
  218. data/lib/generators/rails/plugin/templates/README.tt +13 -0
  219. data/lib/generators/rails/plugin/templates/Rakefile.tt +10 -0
  220. data/lib/generators/rails/plugin/templates/init.rb +1 -0
  221. data/lib/generators/rails/plugin/templates/install.rb +1 -0
  222. data/lib/generators/rails/plugin/templates/lib/%file_name%.rb.tt +1 -0
  223. data/lib/generators/rails/plugin/templates/lib/tasks/%file_name%_tasks.rake.tt +4 -0
  224. data/lib/generators/rails/plugin/templates/uninstall.rb +1 -0
  225. data/lib/generators/rails/resource/USAGE +23 -0
  226. data/lib/generators/rails/resource/resource_generator.rb +34 -0
  227. data/lib/generators/rails/scaffold/USAGE +29 -0
  228. data/lib/generators/rails/scaffold/scaffold_generator.rb +13 -0
  229. data/lib/generators/rails/scaffold_controller/USAGE +20 -0
  230. data/lib/generators/rails/scaffold_controller/scaffold_controller_generator.rb +27 -0
  231. data/lib/generators/rails/scaffold_controller/templates/controller.rb +85 -0
  232. data/lib/generators/rails/session_migration/USAGE +8 -0
  233. data/lib/generators/rails/session_migration/session_migration_generator.rb +8 -0
  234. data/lib/generators/rails/stylesheets/USAGE +5 -0
  235. data/lib/generators/rails/stylesheets/stylesheets_generator.rb +9 -0
  236. data/lib/generators/rails/stylesheets/templates/scaffold.css +61 -0
  237. data/lib/generators/test_unit.rb +8 -0
  238. data/lib/generators/test_unit/controller/controller_generator.rb +14 -0
  239. data/lib/generators/test_unit/controller/templates/functional_test.rb +8 -0
  240. data/lib/generators/test_unit/helper/helper_generator.rb +13 -0
  241. data/lib/generators/test_unit/helper/templates/helper_test.rb +4 -0
  242. data/lib/generators/test_unit/integration/integration_generator.rb +13 -0
  243. data/lib/generators/test_unit/integration/templates/integration_test.rb +10 -0
  244. data/lib/generators/test_unit/mailer/mailer_generator.rb +21 -0
  245. data/lib/generators/test_unit/mailer/templates/fixture +3 -0
  246. data/lib/generators/test_unit/mailer/templates/functional_test.rb +22 -0
  247. data/lib/generators/test_unit/model/model_generator.rb +24 -0
  248. data/lib/generators/test_unit/model/templates/fixtures.yml +23 -0
  249. data/lib/generators/test_unit/model/templates/unit_test.rb +8 -0
  250. data/lib/generators/test_unit/observer/observer_generator.rb +13 -0
  251. data/lib/generators/test_unit/observer/templates/unit_test.rb +8 -0
  252. data/lib/generators/test_unit/performance/performance_generator.rb +13 -0
  253. data/lib/generators/test_unit/performance/templates/performance_test.rb +9 -0
  254. data/lib/generators/test_unit/plugin/plugin_generator.rb +13 -0
  255. data/lib/generators/test_unit/plugin/templates/%file_name%_test.rb.tt +8 -0
  256. data/lib/generators/test_unit/plugin/templates/test_helper.rb +3 -0
  257. data/lib/generators/test_unit/scaffold/scaffold_generator.rb +18 -0
  258. data/lib/generators/test_unit/scaffold/templates/functional_test.rb +47 -0
  259. data/lib/rails.rb +95 -0
  260. data/lib/rails/all.rb +14 -0
  261. data/lib/rails/application.rb +125 -0
  262. data/lib/rails/application/bootstrap.rb +85 -0
  263. data/lib/rails/application/configurable.rb +19 -0
  264. data/lib/rails/application/configuration.rb +86 -0
  265. data/lib/rails/application/finisher.rb +49 -0
  266. data/lib/rails/application/metal_loader.rb +50 -0
  267. data/lib/rails/application/railties.rb +31 -0
  268. data/lib/rails/application/routes_reloader.rb +46 -0
  269. data/lib/rails/backtrace_cleaner.rb +54 -0
  270. data/lib/rails/code_statistics.rb +107 -0
  271. data/lib/rails/commands.rb +72 -0
  272. data/lib/rails/commands/application.rb +13 -0
  273. data/lib/rails/commands/console.rb +55 -0
  274. data/lib/rails/commands/dbconsole.rb +104 -0
  275. data/lib/rails/commands/destroy.rb +9 -0
  276. data/lib/rails/commands/generate.rb +9 -0
  277. data/lib/rails/commands/performance/benchmarker.rb +23 -0
  278. data/lib/rails/commands/performance/profiler.rb +46 -0
  279. data/lib/rails/commands/plugin.rb +542 -0
  280. data/lib/rails/commands/runner.rb +51 -0
  281. data/lib/rails/commands/server.rb +75 -0
  282. data/lib/rails/commands/update.rb +9 -0
  283. data/lib/rails/configuration.rb +191 -0
  284. data/lib/rails/console/app.rb +33 -0
  285. data/lib/rails/console/helpers.rb +5 -0
  286. data/lib/rails/console/sandbox.rb +6 -0
  287. data/lib/rails/deprecation.rb +62 -0
  288. data/lib/rails/dispatcher.rb +24 -0
  289. data/lib/rails/engine.rb +130 -0
  290. data/lib/rails/engine/configurable.rb +25 -0
  291. data/lib/rails/engine/configuration.rb +49 -0
  292. data/lib/rails/generators.rb +289 -0
  293. data/lib/rails/generators/actions.rb +309 -0
  294. data/lib/rails/generators/active_model.rb +78 -0
  295. data/lib/rails/generators/base.rb +362 -0
  296. data/lib/rails/generators/generated_attribute.rb +48 -0
  297. data/lib/rails/generators/migration.rb +64 -0
  298. data/lib/rails/generators/named_base.rb +95 -0
  299. data/lib/rails/generators/resource_helpers.rb +80 -0
  300. data/lib/rails/generators/test_case.rb +239 -0
  301. data/lib/rails/initializable.rb +97 -0
  302. data/lib/rails/paths.rb +144 -0
  303. data/lib/rails/performance_test_help.rb +3 -0
  304. data/lib/rails/plugin.rb +64 -0
  305. data/lib/rails/rack.rb +8 -0
  306. data/lib/rails/rack/debugger.rb +23 -0
  307. data/lib/rails/rack/log_tailer.rb +33 -0
  308. data/lib/rails/rack/logger.rb +34 -0
  309. data/lib/rails/rack/static.rb +5 -0
  310. data/lib/rails/railtie.rb +74 -0
  311. data/lib/rails/railtie/configurable.rb +23 -0
  312. data/lib/rails/railtie/configuration.rb +9 -0
  313. data/lib/rails/railties_path.rb +1 -0
  314. data/lib/rails/ruby_version_check.rb +10 -0
  315. data/lib/rails/rubyprof_ext.rb +35 -0
  316. data/lib/rails/source_annotation_extractor.rb +102 -0
  317. data/lib/rails/subscriber.rb +108 -0
  318. data/lib/rails/subscriber/test_helper.rb +98 -0
  319. data/lib/rails/tasks.rb +16 -0
  320. data/lib/rails/tasks/annotations.rake +20 -0
  321. data/lib/rails/tasks/documentation.rake +96 -0
  322. data/lib/rails/tasks/framework.rake +69 -0
  323. data/lib/rails/tasks/log.rake +9 -0
  324. data/lib/rails/tasks/middleware.rake +7 -0
  325. data/lib/rails/tasks/misc.rake +71 -0
  326. data/lib/rails/tasks/routes.rake +17 -0
  327. data/lib/rails/tasks/statistics.rake +16 -0
  328. data/lib/rails/tasks/tmp.rake +37 -0
  329. data/lib/rails/test_help.rb +30 -0
  330. data/lib/rails/test_unit/railtie.rb +17 -0
  331. data/lib/rails/test_unit/testing.rake +130 -0
  332. data/lib/rails/version.rb +9 -0
  333. data/lib/rails/webrick_server.rb +156 -0
  334. metadata +427 -0
@@ -0,0 +1,238 @@
1
+ h2. Rails Application Templates
2
+
3
+ Application templates are simple ruby files containing DSL for adding plugins/gems/initializers etc. to your freshly created Rails project or an existing Rails project.
4
+
5
+ By referring to this guide, you will be able to:
6
+
7
+ * Use templates to generate/customize Rails applications
8
+ * Write your own reusable application templates using the Rails template API
9
+
10
+ endprologue.
11
+
12
+ h3. Usage
13
+
14
+ To apply a template, you need to provide the Rails generator with the location of the template you wish to apply, using -m option :
15
+
16
+ <shell>
17
+ $ rails blog -m ~/template.rb
18
+ </shell>
19
+
20
+ It's also possible to apply a template using a URL :
21
+
22
+ <shell>
23
+ $ rails blog -m http://gist.github.com/31208.txt
24
+ </shell>
25
+
26
+ Alternatively, you can use the rake task +rails:template+ to apply a template to an existing Rails application :
27
+
28
+ <shell>
29
+ $ rake rails:template LOCATION=~/template.rb
30
+ </shell>
31
+
32
+ h3. Template API
33
+
34
+ Rails templates API is very self explanatory and easy to understand. Here's an example of a typical Rails template :
35
+
36
+ <ruby>
37
+ # template.rb
38
+ run "rm public/index.html"
39
+ generate(:scaffold, "person name:string")
40
+ route "map.root :controller => 'people'"
41
+ rake("db:migrate")
42
+
43
+ git :init
44
+ git :add => "."
45
+ git :commit => "-a -m 'Initial commit'"
46
+ </ruby>
47
+
48
+ The following sections outlines the primary methods provided by the API :
49
+
50
+ h4. gem(name, options = {})
51
+
52
+ Adds a +config.gem+ entry for the supplied gem to the generated application’s +config/environment.rb+.
53
+
54
+ For example, if your application depends on the gems +bj+ and +hpricot+ :
55
+
56
+ <ruby>
57
+ gem "bj"
58
+ gem "hpricot", :version => '0.6', :source => "http://code.whytheluckystiff.net"
59
+ </ruby>
60
+
61
+ Please note that this will NOT install the gems for you. So you may want to run the +rake gems:install+ task too :
62
+
63
+ <ruby>
64
+ rake "gems:install"
65
+ </ruby>
66
+
67
+ And let Rails take care of installing the required gems if they’re not already installed.
68
+
69
+ h4. plugin(name, options = {})
70
+
71
+ Installs a plugin to the generated application.
72
+
73
+ Plugin can be installed from Git :
74
+
75
+ <ruby>
76
+ plugin 'authentication', :git => 'git://github.com/foor/bar.git'
77
+ </ruby>
78
+
79
+ You can even install plugins as git submodules :
80
+
81
+ <ruby>
82
+ plugin 'authentication', :git => 'git://github.com/foor/bar.git',
83
+ :submodule => true
84
+ </ruby>
85
+
86
+ Please note that you need to +git :init+ before you can install a plugin as a submodule.
87
+
88
+ Or use plain old SVN :
89
+
90
+ <ruby>
91
+ plugin 'usingsvn', :svn => 'svn://example.com/usingsvn/trunk'
92
+ </ruby>
93
+
94
+ h4. vendor/lib/file/initializer(filename, data = nil, &block)
95
+
96
+ Adds an initializer to the generated application’s +config/initializers+ directory.
97
+
98
+ Lets say you like using +Object#not_nil?+ and +Object#not_blank?+ :
99
+
100
+ <ruby>
101
+ initializer 'bloatlol.rb', <<-CODE
102
+ class Object
103
+ def not_nil?
104
+ !nil?
105
+ end
106
+
107
+ def not_blank?
108
+ !blank?
109
+ end
110
+ end
111
+ CODE
112
+ </ruby>
113
+
114
+ Similarly +lib()+ creates a file in the +lib/+ directory and +vendor()+ creates a file in the +vendor/+ directory.
115
+
116
+ There is even +file()+, which accepts a relative path from +RAILS_ROOT+ and creates all the directories/file needed :
117
+
118
+ <ruby>
119
+ file 'app/components/foo.rb', <<-CODE
120
+ class Foo
121
+ end
122
+ CODE
123
+ </ruby>
124
+
125
+ That’ll create +app/components+ directory and put +foo.rb+ in there.
126
+
127
+ h4. rakefile(filename, data = nil, &block)
128
+
129
+ Creates a new rake file under +lib/tasks+ with the supplied tasks :
130
+
131
+ <ruby>
132
+ rakefile("bootstrap.rake") do
133
+ <<-TASK
134
+ namespace :boot do
135
+ task :strap do
136
+ puts "i like boots!"
137
+ end
138
+ end
139
+ TASK
140
+ end
141
+ </ruby>
142
+
143
+ The above creates +lib/tasks/bootstrap.rake+ with a +boot:strap+ rake task.
144
+
145
+ h4. generate(what, args)
146
+
147
+ Runs the supplied rails generator with given arguments. For example, I love to scaffold some whenever I’m playing with Rails :
148
+
149
+ <ruby>
150
+ generate(:scaffold, "person", "name:string", "address:text", "age:number")
151
+ </ruby>
152
+
153
+ h4. run(command)
154
+
155
+ Executes an arbitrary command. Just like the backticks. Let's say you want to remove the +public/index.html+ file :
156
+
157
+ <ruby>
158
+ run "rm public/index.html"
159
+ </ruby>
160
+
161
+ h4. rake(command, options = {})
162
+
163
+ Runs the supplied rake tasks in the Rails application. Let's say you want to migrate the database :
164
+
165
+ <ruby>
166
+ rake "db:migrate"
167
+ </ruby>
168
+
169
+ You can also run rake tasks with a different Rails environment :
170
+
171
+ <ruby>
172
+ rake "db:migrate", :env => 'production'
173
+ </ruby>
174
+
175
+ Or even use sudo :
176
+
177
+ <ruby>
178
+ rake "gems:install", :sudo => true
179
+ </ruby>
180
+
181
+ h4. route(routing_code)
182
+
183
+ This adds a routing entry to the +config/routes.rb+ file. In above steps, we generated a person scaffold and also removed +public/index.html+. Now to make +PeopleController#index+ as the default page for the application :
184
+
185
+ <ruby>
186
+ route "map.root :controller => :person"
187
+ </ruby>
188
+
189
+ h4. inside(dir)
190
+
191
+ I have my edge rails lying at +~/commit-rails/rails+. So every time i have to manually symlink edge from my new app. But now :
192
+
193
+ <ruby>
194
+ inside('vendor') do
195
+ run "ln -s ~/commit-rails/rails rails"
196
+ end
197
+ </ruby>
198
+
199
+ So +inside()+ runs the command from the given directory.
200
+
201
+ h4. ask(question)
202
+
203
+ +ask()+ gives you a chance to get some feedback from the user and use it in your templates. Lets say you want your user to name the new shiny library you’re adding :
204
+
205
+ <ruby>
206
+ lib_name = ask("What do you want to call the shiny library ?")
207
+ lib_name << ".rb" unless lib_name.index(".rb")
208
+
209
+ lib lib_name, <<-CODE
210
+ class Shiny
211
+ end
212
+ CODE
213
+ </ruby>
214
+
215
+ h4. yes?(question) or no?(question)
216
+
217
+ These methods let you ask questions from templates and decide the flow based on the user’s answer. Lets say you want to freeze rails only if the user want to :
218
+
219
+ <ruby>
220
+ rake("rails:freeze:gems") if yes?("Freeze rails gems ?")
221
+ no?(question) acts just the opposite.
222
+ </ruby>
223
+
224
+ h4. git(:must => "-a love")
225
+
226
+ Rails templates let you run any git command :
227
+
228
+ <ruby>
229
+ git :init
230
+ git :add => "."
231
+ git :commit => "-a -m 'Initial commit'"
232
+ </ruby>
233
+
234
+ h3. Changelog
235
+
236
+ "Lighthouse ticket":http://rails.lighthouseapp.com/projects/16213-rails-guides/tickets/78
237
+
238
+ * April 29, 2009: Initial version by "Pratik":credits.html#lifo
@@ -0,0 +1,306 @@
1
+ h2. Rails on Rack
2
+
3
+ This guide covers Rails integration with Rack and interfacing with other Rack components. By referring to this guide, you will be able to:
4
+
5
+ * Create Rails Metal applications
6
+ * Use Rack Middlewares in your Rails applications
7
+ * Understand Action Pack's internal Middleware stack
8
+ * Define a custom Middleware stack
9
+
10
+ endprologue.
11
+
12
+ WARNING: This guide assumes a working knowledge of Rack protocol and Rack concepts such as middlewares, url maps and +Rack::Builder+.
13
+
14
+ h3. Introduction to Rack
15
+
16
+ bq. Rack provides a minimal, modular and adaptable interface for developing web applications in Ruby. By wrapping HTTP requests and responses in the simplest way possible, it unifies and distills the API for web servers, web frameworks, and software in between (the so-called middleware) into a single method call.
17
+
18
+ - "Rack API Documentation":http://rack.rubyforge.org/doc/
19
+
20
+ Explaining Rack is not really in the scope of this guide. In case you are not familiar with Rack's basics, you should check out the following links:
21
+
22
+ * "Official Rack Website":http://rack.github.com
23
+ * "Introducing Rack":http://chneukirchen.org/blog/archive/2007/02/introducing-rack.html
24
+ * "Ruby on Rack #1 - Hello Rack!":http://m.onkey.org/2008/11/17/ruby-on-rack-1
25
+ * "Ruby on Rack #2 - The Builder":http://m.onkey.org/2008/11/18/ruby-on-rack-2-rack-builder
26
+
27
+ h3. Rails on Rack
28
+
29
+ h4. Rails Application's Rack Object
30
+
31
+ <tt>ActionController::Dispatcher.new</tt> is the primary Rack application object of a Rails application. Any Rack compliant web server should be using +ActionController::Dispatcher.new+ object to serve a Rails application.</p>
32
+
33
+ h4. +script/server+
34
+
35
+ <tt>script/server</tt> does the basic job of creating a +Rack::Builder+ object and starting the webserver. This is Rails' equivalent of Rack's +rackup+ script.
36
+
37
+ Here's how +script/server+ creates an instance of +Rack::Builder+
38
+
39
+ <ruby>
40
+ app = Rack::Builder.new {
41
+ use Rails::Rack::LogTailer unless options[:detach]
42
+ use Rails::Rack::Debugger if options[:debugger]
43
+ use ActionDispatch::Static
44
+ run ActionController::Dispatcher.new
45
+ }.to_app
46
+ </ruby>
47
+
48
+ Middlewares used in the code above are primarily useful only in the development environment. The following table explains their usage:
49
+
50
+ |_.Middleware|_.Purpose|
51
+ |+Rails::Rack::LogTailer+|Appends log file output to console|
52
+ |+ActionDispatch::Static+|Serves static files inside +RAILS_ROOT/public+ directory|
53
+ |+Rails::Rack::Debugger+|Starts Debugger|
54
+
55
+ h4. +rackup+
56
+
57
+ To use +rackup+ instead of Rails' +script/server+, you can put the following inside +config.ru+ of your Rails application's root directory:
58
+
59
+ <ruby>
60
+ # RAILS_ROOT/config.ru
61
+ require "config/environment"
62
+
63
+ use Rails::Rack::LogTailer
64
+ use ActionDispatch::Static
65
+ run ActionController::Dispatcher.new
66
+ </ruby>
67
+
68
+ And start the server:
69
+
70
+ <shell>
71
+ [lifo@null application]$ rackup config.ru
72
+ </shell>
73
+
74
+ To find out more about different +rackup+ options:
75
+
76
+ <shell>
77
+ [lifo@null application]$ rackup --help
78
+ </shell>
79
+
80
+ h3. Action Controller Middleware Stack
81
+
82
+ Many of Action Controller's internal components are implemented as Rack middlewares. +ActionController::Dispatcher+ uses +ActionController::MiddlewareStack+ to combine various internal and external middlewares to form a complete Rails Rack application.
83
+
84
+ NOTE: +ActionController::MiddlewareStack+ is Rails' equivalent of +Rack::Builder+, but built for better flexibility and more features to meet Rails' requirements.
85
+
86
+ h4. Inspecting Middleware Stack
87
+
88
+ Rails has a handy rake task for inspecting the middleware stack in use:
89
+
90
+ <shell>
91
+ $ rake middleware
92
+ </shell>
93
+
94
+ For a freshly generated Rails application, this might produce something like:
95
+
96
+ <ruby>
97
+ use Rack::Lock
98
+ use ActionController::Failsafe
99
+ use ActionController::Session::CookieStore, , {:secret=>"<secret>", :session_key=>"_<app>_session"}
100
+ use Rails::Rack::Metal
101
+ use ActionDispatch::RewindableInput
102
+ use ActionController::ParamsParser
103
+ use Rack::MethodOverride
104
+ use Rack::Head
105
+ use ActiveRecord::QueryCache
106
+ run ActionController::Dispatcher.new
107
+ </ruby>
108
+
109
+ Purpose of each of this middlewares is explained in the "Internal Middlewares":#internal-middleware-stack section.
110
+
111
+ h4. Configuring Middleware Stack
112
+
113
+ Rails provides a simple configuration interface +config.middleware+ for adding, removing and modifying the middlewares in the middleware stack via +environment.rb+ or the environment specific configuration file <tt>environments/&lt;environment&gt;.rb</tt>.
114
+
115
+ h5. Adding a Middleware
116
+
117
+ You can add a new middleware to the middleware stack using any of the following methods:
118
+
119
+ * +config.middleware.use(new_middleware, args)+ - Adds the new middleware at the bottom of the middleware stack.
120
+
121
+ * +config.middleware.insert_before(existing_middleware, new_middleware, args)+ - Adds the new middleware before the specified existing middleware in the middleware stack.
122
+
123
+ * +config.middleware.insert_after(existing_middleware, new_middleware, args)+ - Adds the new middleware after the specified existing middleware in the middleware stack.
124
+
125
+ <strong>Example:</strong>
126
+
127
+ <ruby>
128
+ # config/environment.rb
129
+
130
+ # Push Rack::BounceFavicon at the bottom
131
+ config.middleware.use Rack::BounceFavicon
132
+
133
+ # Add Lifo::Cache after ActiveRecord::QueryCache.
134
+ # Pass { :page_cache => false } argument to Lifo::Cache.
135
+ config.middleware.insert_after ActiveRecord::QueryCache, Lifo::Cache, :page_cache => false
136
+ </ruby>
137
+
138
+ h5. Swapping a Middleware
139
+
140
+ You can swap an existing middleware in the middleware stack using +config.middleware.swap+.
141
+
142
+ <strong>Example:</strong>
143
+
144
+ <ruby>
145
+ # config/environment.rb
146
+
147
+ # Replace ActionController::Failsafe with Lifo::Failsafe
148
+ config.middleware.swap ActionController::Failsafe, Lifo::Failsafe
149
+ </ruby>
150
+
151
+ h5. Middleware Stack is an Array
152
+
153
+ The middleware stack behaves just like a normal +Array+. You can use any +Array+ methods to insert, reorder, or remove items from the stack. Methods described in the section above are just convenience methods.
154
+
155
+ For example, the following removes the middleware matching the supplied class name:
156
+
157
+ <ruby>
158
+ config.middleware.delete(middleware)
159
+ </ruby>
160
+
161
+ h4. Internal Middleware Stack
162
+
163
+ Much of Action Controller's functionality is implemented as Middlewares. The following table explains the purpose of each of them:
164
+
165
+ |_.Middleware|_.Purpose|
166
+ |+Rack::Lock+|Sets +env["rack.multithread"]+ flag to +true+ and wraps the application within a Mutex.|
167
+ |+ActionController::Failsafe+|Returns HTTP Status +500+ to the client if an exception gets raised while dispatching.|
168
+ |+ActiveRecord::QueryCache+|Enable the Active Record query cache.|
169
+ |+ActionController::Session::CookieStore+|Uses the cookie based session store.|
170
+ |+ActionController::Session::MemCacheStore+|Uses the memcached based session store.|
171
+ |+ActiveRecord::SessionStore+|Uses the database based session store.|
172
+ |+Rack::MethodOverride+|Sets HTTP method based on +_method+ parameter or +env["HTTP_X_HTTP_METHOD_OVERRIDE"]+.|
173
+ |+Rack::Head+|Discards the response body if the client sends a +HEAD+ request.|
174
+
175
+ TIP: It's possible to use any of the above middlewares in your custom Rack stack.
176
+
177
+ h4. Customizing Internal Middleware Stack
178
+
179
+ It's possible to replace the entire middleware stack with a custom stack using +ActionController::Dispatcher.middleware=+.
180
+
181
+ <strong>Example:</strong>
182
+
183
+ Put the following in an initializer:
184
+
185
+ <ruby>
186
+ # config/initializers/stack.rb
187
+ ActionController::Dispatcher.middleware = ActionController::MiddlewareStack.new do |m|
188
+ m.use ActionController::Failsafe
189
+ m.use ActiveRecord::QueryCache
190
+ m.use Rack::Head
191
+ end
192
+ </ruby>
193
+
194
+ And now inspecting the middleware stack:
195
+
196
+ <shell>
197
+ $ rake middleware
198
+ (in /Users/lifo/Rails/blog)
199
+ use ActionController::Failsafe
200
+ use ActiveRecord::QueryCache
201
+ use Rack::Head
202
+ run ActionController::Dispatcher.new
203
+ </shell>
204
+
205
+ h4. Using Rack Builder
206
+
207
+ The following shows how to replace use +Rack::Builder+ instead of the Rails supplied +MiddlewareStack+.
208
+
209
+ <strong>Clear the existing Rails middleware stack</strong>
210
+
211
+ <ruby>
212
+ # environment.rb
213
+ config.middleware.clear
214
+ </ruby>
215
+
216
+ <br />
217
+ <strong>Add a +config.ru+ file to +RAILS_ROOT+</strong>
218
+
219
+ <ruby>
220
+ # config.ru
221
+ use MyOwnStackFromStratch
222
+ run ActionController::Dispatcher.new
223
+ </ruby>
224
+
225
+ h3. Rails Metal Applications
226
+
227
+ Rails Metal applications are minimal Rack applications specially designed for integrating with a typical Rails application. As Rails Metal Applications skip all of the Action Controller stack, serving a request has no overhead from the Rails framework itself. This is especially useful for infrequent cases where the performance of the full stack Rails framework is an issue.
228
+
229
+ Ryan Bates' "Railscast on Rails Metal":http://railscasts.com/episodes/150-rails-metal provides a nice walkthrough generating and using Rails Metal.
230
+
231
+ h4. Generating a Metal Application
232
+
233
+ Rails provides a generator called +metal+ for creating a new Metal application:
234
+
235
+ <shell>
236
+ $ script/generate metal poller
237
+ </shell>
238
+
239
+ This generates +poller.rb+ in the +app/metal+ directory:
240
+
241
+ <ruby>
242
+ # Allow the metal piece to run in isolation
243
+ require(File.dirname(__FILE__) + "/../../config/environment") unless defined?(Rails)
244
+
245
+ class Poller
246
+ def self.call(env)
247
+ if env["PATH_INFO"] =~ /^\/poller/
248
+ [200, {"Content-Type" => "text/html"}, ["Hello, World!"]]
249
+ else
250
+ [404, {"Content-Type" => "text/html"}, ["Not Found"]]
251
+ end
252
+ end
253
+ end
254
+ </ruby>
255
+
256
+ Metal applications within +app/metal+ folders in plugins will also be discovered and added to the list.
257
+
258
+ Metal applications are an optimization. You should make sure to "understand the related performance implications":http://weblog.rubyonrails.org/2008/12/20/performance-of-rails-metal before using it.
259
+
260
+ h4. Execution Order
261
+
262
+ All Metal Applications are executed by +Rails::Rack::Metal+ middleware, which is a part of the +ActionController::MiddlewareStack+ chain.
263
+
264
+ Here's the primary method responsible for running the Metal applications:
265
+
266
+ <ruby>
267
+ def call(env)
268
+ @metals.keys.each do |app|
269
+ result = app.call(env)
270
+ return result unless result[0].to_i == 404
271
+ end
272
+ @app.call(env)
273
+ end
274
+ </ruby>
275
+
276
+ In the code above, +@metals+ is an ordered hash of metal applications. Due to the default alphabetical ordering, +aaa.rb+ will come before +bbb.rb+ in the metal chain.
277
+
278
+ It is, however, possible to override the default ordering in your environment. Simply add a line like the following to +config/environment.rb+
279
+
280
+ <ruby>
281
+ config.metals = ["Bbb", "Aaa"]
282
+ </ruby>
283
+
284
+ Each string in the array should be the name of your metal class. If you do this then be warned that any metal applications not listed will not be loaded.
285
+
286
+ WARNING: Metal applications cannot return the HTTP Status +404+ to a client, as it is used for continuing the Metal chain execution. Please use normal Rails controllers or a custom middleware if returning +404+ is a requirement.
287
+
288
+ h3. Resources
289
+
290
+ h4. Learning Rack
291
+
292
+ * "Official Rack Website":http://rack.github.com
293
+ * "Introducing Rack":http://chneukirchen.org/blog/archive/2007/02/introducing-rack.html
294
+ * "Ruby on Rack #1 - Hello Rack!":http://m.onkey.org/2008/11/17/ruby-on-rack-1
295
+ * "Ruby on Rack #2 - The Builder":http://m.onkey.org/2008/11/18/ruby-on-rack-2-rack-builder
296
+
297
+ h4. Understanding Middlewares
298
+
299
+ * "Railscast on Rack Middlewares":http://railscasts.com/episodes/151-rack-middleware
300
+
301
+ h3. Changelog
302
+
303
+ "Lighthouse ticket":http://rails.lighthouseapp.com/projects/16213-rails-guides/tickets/58
304
+
305
+ * February 7, 2009: Second version by "Pratik":credits.html#lifo
306
+ * January 11, 2009: First version by "Pratik":credits.html#lifo