openskies 0.0.5.2 → 0.0.6.0

Sign up to get free protection for your applications and to get access to all the features.
Files changed (491) hide show
  1. data/CHANGELOG.md +0 -87
  2. data/bin/openskies +31 -5
  3. metadata +4 -593
  4. data/guides/assets/images/belongs_to.png +0 -0
  5. data/guides/assets/images/book_icon.gif +0 -0
  6. data/guides/assets/images/bullet.gif +0 -0
  7. data/guides/assets/images/challenge.png +0 -0
  8. data/guides/assets/images/chapters_icon.gif +0 -0
  9. data/guides/assets/images/check_bullet.gif +0 -0
  10. data/guides/assets/images/credits_pic_blank.gif +0 -0
  11. data/guides/assets/images/csrf.png +0 -0
  12. data/guides/assets/images/customized_error_messages.png +0 -0
  13. data/guides/assets/images/edge_badge.png +0 -0
  14. data/guides/assets/images/error_messages.png +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/grey_bullet.gif +0 -0
  19. data/guides/assets/images/habtm.png +0 -0
  20. data/guides/assets/images/has_many.png +0 -0
  21. data/guides/assets/images/has_many_through.png +0 -0
  22. data/guides/assets/images/has_one.png +0 -0
  23. data/guides/assets/images/has_one_through.png +0 -0
  24. data/guides/assets/images/header_backdrop.png +0 -0
  25. data/guides/assets/images/header_tile.gif +0 -0
  26. data/guides/assets/images/i18n/demo_html_safe.png +0 -0
  27. data/guides/assets/images/i18n/demo_localized_pirate.png +0 -0
  28. data/guides/assets/images/i18n/demo_translated_en.png +0 -0
  29. data/guides/assets/images/i18n/demo_translated_pirate.png +0 -0
  30. data/guides/assets/images/i18n/demo_translation_missing.png +0 -0
  31. data/guides/assets/images/i18n/demo_untranslated.png +0 -0
  32. data/guides/assets/images/icons/README +0 -5
  33. data/guides/assets/images/icons/callouts/1.png +0 -0
  34. data/guides/assets/images/icons/callouts/10.png +0 -0
  35. data/guides/assets/images/icons/callouts/11.png +0 -0
  36. data/guides/assets/images/icons/callouts/12.png +0 -0
  37. data/guides/assets/images/icons/callouts/13.png +0 -0
  38. data/guides/assets/images/icons/callouts/14.png +0 -0
  39. data/guides/assets/images/icons/callouts/15.png +0 -0
  40. data/guides/assets/images/icons/callouts/2.png +0 -0
  41. data/guides/assets/images/icons/callouts/3.png +0 -0
  42. data/guides/assets/images/icons/callouts/4.png +0 -0
  43. data/guides/assets/images/icons/callouts/5.png +0 -0
  44. data/guides/assets/images/icons/callouts/6.png +0 -0
  45. data/guides/assets/images/icons/callouts/7.png +0 -0
  46. data/guides/assets/images/icons/callouts/8.png +0 -0
  47. data/guides/assets/images/icons/callouts/9.png +0 -0
  48. data/guides/assets/images/icons/caution.png +0 -0
  49. data/guides/assets/images/icons/example.png +0 -0
  50. data/guides/assets/images/icons/home.png +0 -0
  51. data/guides/assets/images/icons/important.png +0 -0
  52. data/guides/assets/images/icons/next.png +0 -0
  53. data/guides/assets/images/icons/note.png +0 -0
  54. data/guides/assets/images/icons/prev.png +0 -0
  55. data/guides/assets/images/icons/tip.png +0 -0
  56. data/guides/assets/images/icons/up.png +0 -0
  57. data/guides/assets/images/icons/warning.png +0 -0
  58. data/guides/assets/images/jaimeiniesta.jpg +0 -0
  59. data/guides/assets/images/nav_arrow.gif +0 -0
  60. data/guides/assets/images/polymorphic.png +0 -0
  61. data/guides/assets/images/posts_index.png +0 -0
  62. data/guides/assets/images/radar.png +0 -0
  63. data/guides/assets/images/rails_guides_kindle_cover.jpg +0 -0
  64. data/guides/assets/images/rails_guides_logo.gif +0 -0
  65. data/guides/assets/images/rails_logo_remix.gif +0 -0
  66. data/guides/assets/images/rails_welcome.png +0 -0
  67. data/guides/assets/images/session_fixation.png +0 -0
  68. data/guides/assets/images/tab_grey.gif +0 -0
  69. data/guides/assets/images/tab_info.gif +0 -0
  70. data/guides/assets/images/tab_note.gif +0 -0
  71. data/guides/assets/images/tab_red.gif +0 -0
  72. data/guides/assets/images/tab_yellow.gif +0 -0
  73. data/guides/assets/images/tab_yellow.png +0 -0
  74. data/guides/assets/images/validation_error_messages.png +0 -0
  75. data/guides/assets/images/vijaydev.jpg +0 -0
  76. data/guides/assets/javascripts/guides.js +0 -7
  77. data/guides/assets/javascripts/syntaxhighlighter/shBrushAS3.js +0 -59
  78. data/guides/assets/javascripts/syntaxhighlighter/shBrushAppleScript.js +0 -75
  79. data/guides/assets/javascripts/syntaxhighlighter/shBrushBash.js +0 -59
  80. data/guides/assets/javascripts/syntaxhighlighter/shBrushCSharp.js +0 -65
  81. data/guides/assets/javascripts/syntaxhighlighter/shBrushColdFusion.js +0 -100
  82. data/guides/assets/javascripts/syntaxhighlighter/shBrushCpp.js +0 -97
  83. data/guides/assets/javascripts/syntaxhighlighter/shBrushCss.js +0 -91
  84. data/guides/assets/javascripts/syntaxhighlighter/shBrushDelphi.js +0 -55
  85. data/guides/assets/javascripts/syntaxhighlighter/shBrushDiff.js +0 -41
  86. data/guides/assets/javascripts/syntaxhighlighter/shBrushErlang.js +0 -52
  87. data/guides/assets/javascripts/syntaxhighlighter/shBrushGroovy.js +0 -67
  88. data/guides/assets/javascripts/syntaxhighlighter/shBrushJScript.js +0 -52
  89. data/guides/assets/javascripts/syntaxhighlighter/shBrushJava.js +0 -57
  90. data/guides/assets/javascripts/syntaxhighlighter/shBrushJavaFX.js +0 -58
  91. data/guides/assets/javascripts/syntaxhighlighter/shBrushPerl.js +0 -72
  92. data/guides/assets/javascripts/syntaxhighlighter/shBrushPhp.js +0 -88
  93. data/guides/assets/javascripts/syntaxhighlighter/shBrushPlain.js +0 -33
  94. data/guides/assets/javascripts/syntaxhighlighter/shBrushPowerShell.js +0 -74
  95. data/guides/assets/javascripts/syntaxhighlighter/shBrushPython.js +0 -64
  96. data/guides/assets/javascripts/syntaxhighlighter/shBrushRuby.js +0 -55
  97. data/guides/assets/javascripts/syntaxhighlighter/shBrushSass.js +0 -94
  98. data/guides/assets/javascripts/syntaxhighlighter/shBrushScala.js +0 -51
  99. data/guides/assets/javascripts/syntaxhighlighter/shBrushSql.js +0 -66
  100. data/guides/assets/javascripts/syntaxhighlighter/shBrushVb.js +0 -56
  101. data/guides/assets/javascripts/syntaxhighlighter/shBrushXml.js +0 -69
  102. data/guides/assets/javascripts/syntaxhighlighter/shCore.js +0 -17
  103. data/guides/assets/stylesheets/fixes.css +0 -16
  104. data/guides/assets/stylesheets/kindle.css +0 -11
  105. data/guides/assets/stylesheets/main.css +0 -453
  106. data/guides/assets/stylesheets/print.css +0 -52
  107. data/guides/assets/stylesheets/reset.css +0 -43
  108. data/guides/assets/stylesheets/style.css +0 -13
  109. data/guides/assets/stylesheets/syntaxhighlighter/shCore.css +0 -226
  110. data/guides/assets/stylesheets/syntaxhighlighter/shCoreDefault.css +0 -328
  111. data/guides/assets/stylesheets/syntaxhighlighter/shCoreDjango.css +0 -331
  112. data/guides/assets/stylesheets/syntaxhighlighter/shCoreEclipse.css +0 -339
  113. data/guides/assets/stylesheets/syntaxhighlighter/shCoreEmacs.css +0 -324
  114. data/guides/assets/stylesheets/syntaxhighlighter/shCoreFadeToGrey.css +0 -328
  115. data/guides/assets/stylesheets/syntaxhighlighter/shCoreMDUltra.css +0 -324
  116. data/guides/assets/stylesheets/syntaxhighlighter/shCoreMidnight.css +0 -324
  117. data/guides/assets/stylesheets/syntaxhighlighter/shCoreRDark.css +0 -324
  118. data/guides/assets/stylesheets/syntaxhighlighter/shThemeDefault.css +0 -117
  119. data/guides/assets/stylesheets/syntaxhighlighter/shThemeDjango.css +0 -120
  120. data/guides/assets/stylesheets/syntaxhighlighter/shThemeEclipse.css +0 -128
  121. data/guides/assets/stylesheets/syntaxhighlighter/shThemeEmacs.css +0 -113
  122. data/guides/assets/stylesheets/syntaxhighlighter/shThemeFadeToGrey.css +0 -117
  123. data/guides/assets/stylesheets/syntaxhighlighter/shThemeMDUltra.css +0 -113
  124. data/guides/assets/stylesheets/syntaxhighlighter/shThemeMidnight.css +0 -113
  125. data/guides/assets/stylesheets/syntaxhighlighter/shThemeRDark.css +0 -113
  126. data/guides/assets/stylesheets/syntaxhighlighter/shThemeRailsGuides.css +0 -116
  127. data/guides/code/getting_started/Gemfile +0 -38
  128. data/guides/code/getting_started/README.rdoc +0 -261
  129. data/guides/code/getting_started/Rakefile +0 -7
  130. data/guides/code/getting_started/app/assets/images/rails.png +0 -0
  131. data/guides/code/getting_started/app/assets/javascripts/application.js +0 -15
  132. data/guides/code/getting_started/app/assets/javascripts/comments.js.coffee +0 -3
  133. data/guides/code/getting_started/app/assets/javascripts/home.js.coffee +0 -3
  134. data/guides/code/getting_started/app/assets/javascripts/posts.js.coffee +0 -3
  135. data/guides/code/getting_started/app/assets/stylesheets/application.css +0 -13
  136. data/guides/code/getting_started/app/assets/stylesheets/comments.css.scss +0 -3
  137. data/guides/code/getting_started/app/assets/stylesheets/home.css.scss +0 -3
  138. data/guides/code/getting_started/app/assets/stylesheets/posts.css.scss +0 -3
  139. data/guides/code/getting_started/app/assets/stylesheets/scaffolds.css.scss +0 -56
  140. data/guides/code/getting_started/app/controllers/application_controller.rb +0 -3
  141. data/guides/code/getting_started/app/controllers/comments_controller.rb +0 -16
  142. data/guides/code/getting_started/app/controllers/home_controller.rb +0 -5
  143. data/guides/code/getting_started/app/controllers/posts_controller.rb +0 -84
  144. data/guides/code/getting_started/app/helpers/application_helper.rb +0 -2
  145. data/guides/code/getting_started/app/helpers/comments_helper.rb +0 -2
  146. data/guides/code/getting_started/app/helpers/home_helper.rb +0 -2
  147. data/guides/code/getting_started/app/helpers/posts_helper.rb +0 -5
  148. data/guides/code/getting_started/app/models/comment.rb +0 -3
  149. data/guides/code/getting_started/app/models/post.rb +0 -11
  150. data/guides/code/getting_started/app/models/tag.rb +0 -3
  151. data/guides/code/getting_started/app/views/comments/_comment.html.erb +0 -15
  152. data/guides/code/getting_started/app/views/comments/_form.html.erb +0 -13
  153. data/guides/code/getting_started/app/views/home/index.html.erb +0 -2
  154. data/guides/code/getting_started/app/views/layouts/application.html.erb +0 -14
  155. data/guides/code/getting_started/app/views/posts/_form.html.erb +0 -32
  156. data/guides/code/getting_started/app/views/posts/edit.html.erb +0 -6
  157. data/guides/code/getting_started/app/views/posts/index.html.erb +0 -27
  158. data/guides/code/getting_started/app/views/posts/new.html.erb +0 -5
  159. data/guides/code/getting_started/app/views/posts/show.html.erb +0 -31
  160. data/guides/code/getting_started/app/views/tags/_form.html.erb +0 -12
  161. data/guides/code/getting_started/config/application.rb +0 -59
  162. data/guides/code/getting_started/config/boot.rb +0 -6
  163. data/guides/code/getting_started/config/database.yml +0 -25
  164. data/guides/code/getting_started/config/environment.rb +0 -5
  165. data/guides/code/getting_started/config/environments/development.rb +0 -37
  166. data/guides/code/getting_started/config/environments/production.rb +0 -67
  167. data/guides/code/getting_started/config/environments/test.rb +0 -37
  168. data/guides/code/getting_started/config/initializers/backtrace_silencers.rb +0 -7
  169. data/guides/code/getting_started/config/initializers/inflections.rb +0 -15
  170. data/guides/code/getting_started/config/initializers/mime_types.rb +0 -5
  171. data/guides/code/getting_started/config/initializers/secret_token.rb +0 -7
  172. data/guides/code/getting_started/config/initializers/session_store.rb +0 -8
  173. data/guides/code/getting_started/config/initializers/wrap_parameters.rb +0 -14
  174. data/guides/code/getting_started/config/locales/en.yml +0 -5
  175. data/guides/code/getting_started/config/routes.rb +0 -64
  176. data/guides/code/getting_started/config.ru +0 -4
  177. data/guides/code/getting_started/db/migrate/20110901012504_create_posts.rb +0 -11
  178. data/guides/code/getting_started/db/migrate/20110901012815_create_comments.rb +0 -12
  179. data/guides/code/getting_started/db/migrate/20110901013701_create_tags.rb +0 -11
  180. data/guides/code/getting_started/db/schema.rb +0 -43
  181. data/guides/code/getting_started/db/seeds.rb +0 -7
  182. data/guides/code/getting_started/doc/README_FOR_APP +0 -2
  183. data/guides/code/getting_started/public/404.html +0 -26
  184. data/guides/code/getting_started/public/422.html +0 -26
  185. data/guides/code/getting_started/public/500.html +0 -25
  186. data/guides/code/getting_started/public/favicon.ico +0 -0
  187. data/guides/code/getting_started/public/robots.txt +0 -5
  188. data/guides/code/getting_started/script/rails +0 -6
  189. data/guides/code/getting_started/test/fixtures/comments.yml +0 -11
  190. data/guides/code/getting_started/test/fixtures/posts.yml +0 -11
  191. data/guides/code/getting_started/test/fixtures/tags.yml +0 -9
  192. data/guides/code/getting_started/test/functional/comments_controller_test.rb +0 -7
  193. data/guides/code/getting_started/test/functional/home_controller_test.rb +0 -9
  194. data/guides/code/getting_started/test/functional/posts_controller_test.rb +0 -49
  195. data/guides/code/getting_started/test/performance/browsing_test.rb +0 -12
  196. data/guides/code/getting_started/test/test_helper.rb +0 -13
  197. data/guides/code/getting_started/test/unit/comment_test.rb +0 -7
  198. data/guides/code/getting_started/test/unit/helpers/comments_helper_test.rb +0 -4
  199. data/guides/code/getting_started/test/unit/helpers/home_helper_test.rb +0 -4
  200. data/guides/code/getting_started/test/unit/helpers/posts_helper_test.rb +0 -4
  201. data/guides/code/getting_started/test/unit/post_test.rb +0 -7
  202. data/guides/code/getting_started/test/unit/tag_test.rb +0 -7
  203. data/guides/rails_guides/generator.rb +0 -310
  204. data/guides/rails_guides/helpers.rb +0 -45
  205. data/guides/rails_guides/indexer.rb +0 -69
  206. data/guides/rails_guides/levenshtein.rb +0 -31
  207. data/guides/rails_guides/textile_extensions.rb +0 -63
  208. data/guides/rails_guides.rb +0 -50
  209. data/guides/source/2_2_release_notes.textile +0 -422
  210. data/guides/source/2_3_release_notes.textile +0 -610
  211. data/guides/source/3_0_release_notes.textile +0 -595
  212. data/guides/source/3_1_release_notes.textile +0 -553
  213. data/guides/source/3_2_release_notes.textile +0 -540
  214. data/guides/source/_license.html.erb +0 -2
  215. data/guides/source/_welcome.html.erb +0 -19
  216. data/guides/source/action_controller_overview.textile +0 -820
  217. data/guides/source/action_mailer_basics.textile +0 -516
  218. data/guides/source/action_view_overview.textile +0 -1497
  219. data/guides/source/active_model_basics.textile +0 -205
  220. data/guides/source/active_record_basics.textile +0 -218
  221. data/guides/source/active_record_querying.textile +0 -1433
  222. data/guides/source/active_record_validations_callbacks.textile +0 -1283
  223. data/guides/source/active_resource_basics.textile +0 -120
  224. data/guides/source/active_support_core_extensions.textile +0 -3713
  225. data/guides/source/ajax_on_rails.textile +0 -267
  226. data/guides/source/api_documentation_guidelines.textile +0 -185
  227. data/guides/source/asset_pipeline.textile +0 -707
  228. data/guides/source/association_basics.textile +0 -1959
  229. data/guides/source/caching_with_rails.textile +0 -437
  230. data/guides/source/command_line.textile +0 -574
  231. data/guides/source/configuring.textile +0 -641
  232. data/guides/source/contributing_to_ruby_on_rails.textile +0 -448
  233. data/guides/source/credits.html.erb +0 -72
  234. data/guides/source/debugging_rails_applications.textile +0 -714
  235. data/guides/source/documents.yaml +0 -157
  236. data/guides/source/engines.textile +0 -618
  237. data/guides/source/form_helpers.textile +0 -798
  238. data/guides/source/generators.textile +0 -621
  239. data/guides/source/getting_started.textile +0 -1929
  240. data/guides/source/i18n.textile +0 -931
  241. data/guides/source/index.html.erb +0 -30
  242. data/guides/source/initialization.textile +0 -1116
  243. data/guides/source/kindle/KINDLE.md +0 -26
  244. data/guides/source/kindle/copyright.html.erb +0 -1
  245. data/guides/source/kindle/layout.html.erb +0 -27
  246. data/guides/source/kindle/rails_guides.opf.erb +0 -52
  247. data/guides/source/kindle/toc.html.erb +0 -24
  248. data/guides/source/kindle/toc.ncx.erb +0 -64
  249. data/guides/source/kindle/welcome.html.erb +0 -5
  250. data/guides/source/layout.html.erb +0 -124
  251. data/guides/source/layouts_and_rendering.textile +0 -1237
  252. data/guides/source/migrations.textile +0 -900
  253. data/guides/source/nested_model_forms.textile +0 -222
  254. data/guides/source/performance_testing.textile +0 -597
  255. data/guides/source/plugins.textile +0 -464
  256. data/guides/source/rails_application_templates.textile +0 -240
  257. data/guides/source/rails_on_rack.textile +0 -236
  258. data/guides/source/routing.textile +0 -885
  259. data/guides/source/ruby_on_rails_guides_guidelines.textile +0 -79
  260. data/guides/source/security.textile +0 -1004
  261. data/guides/source/testing.textile +0 -947
  262. data/guides/w3c_validator.rb +0 -91
  263. data/lib/rails/all.rb +0 -15
  264. data/lib/rails/application/bootstrap.rb +0 -73
  265. data/lib/rails/application/configuration.rb +0 -151
  266. data/lib/rails/application/finisher.rb +0 -98
  267. data/lib/rails/application/railties.rb +0 -13
  268. data/lib/rails/application/route_inspector.rb +0 -84
  269. data/lib/rails/application/routes_reloader.rb +0 -56
  270. data/lib/rails/application.rb +0 -320
  271. data/lib/rails/backtrace_cleaner.rb +0 -42
  272. data/lib/rails/cli.rb +0 -16
  273. data/lib/rails/code_statistics.rb +0 -118
  274. data/lib/rails/commands/application.rb +0 -36
  275. data/lib/rails/commands/benchmarker.rb +0 -34
  276. data/lib/rails/commands/console.rb +0 -55
  277. data/lib/rails/commands/dbconsole.rb +0 -123
  278. data/lib/rails/commands/destroy.rb +0 -10
  279. data/lib/rails/commands/generate.rb +0 -12
  280. data/lib/rails/commands/plugin.rb +0 -544
  281. data/lib/rails/commands/plugin_new.rb +0 -11
  282. data/lib/rails/commands/profiler.rb +0 -32
  283. data/lib/rails/commands/runner.rb +0 -54
  284. data/lib/rails/commands/server.rb +0 -100
  285. data/lib/rails/commands/update.rb +0 -9
  286. data/lib/rails/commands.rb +0 -104
  287. data/lib/rails/configuration.rb +0 -89
  288. data/lib/rails/console/app.rb +0 -36
  289. data/lib/rails/console/helpers.rb +0 -11
  290. data/lib/rails/engine/commands.rb +0 -43
  291. data/lib/rails/engine/configuration.rb +0 -84
  292. data/lib/rails/engine/railties.rb +0 -33
  293. data/lib/rails/engine.rb +0 -657
  294. data/lib/rails/generators/actions.rb +0 -325
  295. data/lib/rails/generators/active_model.rb +0 -78
  296. data/lib/rails/generators/app_base.rb +0 -266
  297. data/lib/rails/generators/base.rb +0 -390
  298. data/lib/rails/generators/css/assets/assets_generator.rb +0 -13
  299. data/lib/rails/generators/css/assets/templates/stylesheet.css +0 -4
  300. data/lib/rails/generators/css/scaffold/scaffold_generator.rb +0 -16
  301. data/lib/rails/generators/erb/controller/controller_generator.rb +0 -20
  302. data/lib/rails/generators/erb/controller/templates/view.html.erb +0 -2
  303. data/lib/rails/generators/erb/mailer/mailer_generator.rb +0 -13
  304. data/lib/rails/generators/erb/mailer/templates/view.text.erb +0 -3
  305. data/lib/rails/generators/erb/scaffold/scaffold_generator.rb +0 -29
  306. data/lib/rails/generators/erb/scaffold/templates/_form.html.erb +0 -23
  307. data/lib/rails/generators/erb/scaffold/templates/edit.html.erb +0 -6
  308. data/lib/rails/generators/erb/scaffold/templates/index.html.erb +0 -27
  309. data/lib/rails/generators/erb/scaffold/templates/new.html.erb +0 -5
  310. data/lib/rails/generators/erb/scaffold/templates/show.html.erb +0 -12
  311. data/lib/rails/generators/erb.rb +0 -21
  312. data/lib/rails/generators/generated_attribute.rb +0 -107
  313. data/lib/rails/generators/js/assets/assets_generator.rb +0 -13
  314. data/lib/rails/generators/js/assets/templates/javascript.js +0 -2
  315. data/lib/rails/generators/migration.rb +0 -68
  316. data/lib/rails/generators/named_base.rb +0 -202
  317. data/lib/rails/generators/rails/app/USAGE +0 -15
  318. data/lib/rails/generators/rails/app/app_generator.rb +0 -312
  319. data/lib/rails/generators/rails/app/templates/Gemfile +0 -21
  320. data/lib/rails/generators/rails/app/templates/README +0 -261
  321. data/lib/rails/generators/rails/app/templates/Rakefile +0 -7
  322. data/lib/rails/generators/rails/app/templates/app/assets/images/rails.png +0 -0
  323. data/lib/rails/generators/rails/app/templates/app/assets/javascripts/application.js.tt +0 -17
  324. data/lib/rails/generators/rails/app/templates/app/assets/stylesheets/application.css +0 -13
  325. data/lib/rails/generators/rails/app/templates/app/controllers/application_controller.rb +0 -3
  326. data/lib/rails/generators/rails/app/templates/app/helpers/application_helper.rb +0 -2
  327. data/lib/rails/generators/rails/app/templates/app/mailers/.empty_directory +0 -0
  328. data/lib/rails/generators/rails/app/templates/app/models/.empty_directory +0 -0
  329. data/lib/rails/generators/rails/app/templates/app/views/layouts/application.html.erb.tt +0 -14
  330. data/lib/rails/generators/rails/app/templates/config/application.rb +0 -68
  331. data/lib/rails/generators/rails/app/templates/config/boot.rb +0 -6
  332. data/lib/rails/generators/rails/app/templates/config/databases/frontbase.yml +0 -31
  333. data/lib/rails/generators/rails/app/templates/config/databases/ibm_db.yml +0 -86
  334. data/lib/rails/generators/rails/app/templates/config/databases/jdbc.yml +0 -62
  335. data/lib/rails/generators/rails/app/templates/config/databases/jdbcmysql.yml +0 -33
  336. data/lib/rails/generators/rails/app/templates/config/databases/jdbcpostgresql.yml +0 -43
  337. data/lib/rails/generators/rails/app/templates/config/databases/jdbcsqlite3.yml +0 -20
  338. data/lib/rails/generators/rails/app/templates/config/databases/mysql.yml +0 -54
  339. data/lib/rails/generators/rails/app/templates/config/databases/oracle.yml +0 -39
  340. data/lib/rails/generators/rails/app/templates/config/databases/postgresql.yml +0 -55
  341. data/lib/rails/generators/rails/app/templates/config/databases/sqlite3.yml +0 -25
  342. data/lib/rails/generators/rails/app/templates/config/environment.rb +0 -5
  343. data/lib/rails/generators/rails/app/templates/config/environments/development.rb.tt +0 -41
  344. data/lib/rails/generators/rails/app/templates/config/environments/production.rb.tt +0 -73
  345. data/lib/rails/generators/rails/app/templates/config/environments/test.rb.tt +0 -39
  346. data/lib/rails/generators/rails/app/templates/config/initializers/backtrace_silencers.rb +0 -7
  347. data/lib/rails/generators/rails/app/templates/config/initializers/inflections.rb +0 -15
  348. data/lib/rails/generators/rails/app/templates/config/initializers/mime_types.rb +0 -5
  349. data/lib/rails/generators/rails/app/templates/config/initializers/secret_token.rb.tt +0 -7
  350. data/lib/rails/generators/rails/app/templates/config/initializers/session_store.rb.tt +0 -8
  351. data/lib/rails/generators/rails/app/templates/config/initializers/wrap_parameters.rb.tt +0 -16
  352. data/lib/rails/generators/rails/app/templates/config/locales/en.yml +0 -5
  353. data/lib/rails/generators/rails/app/templates/config/routes.rb +0 -58
  354. data/lib/rails/generators/rails/app/templates/config.ru +0 -4
  355. data/lib/rails/generators/rails/app/templates/db/seeds.rb.tt +0 -7
  356. data/lib/rails/generators/rails/app/templates/doc/README_FOR_APP +0 -2
  357. data/lib/rails/generators/rails/app/templates/gitignore +0 -15
  358. data/lib/rails/generators/rails/app/templates/public/404.html +0 -26
  359. data/lib/rails/generators/rails/app/templates/public/422.html +0 -26
  360. data/lib/rails/generators/rails/app/templates/public/500.html +0 -25
  361. data/lib/rails/generators/rails/app/templates/public/robots.txt +0 -5
  362. data/lib/rails/generators/rails/app/templates/public/stylesheets/.empty_directory +0 -0
  363. data/lib/rails/generators/rails/app/templates/script/openskies +0 -5
  364. data/lib/rails/generators/rails/app/templates/test/fixtures/.empty_directory +0 -0
  365. data/lib/rails/generators/rails/app/templates/test/functional/.empty_directory +0 -0
  366. data/lib/rails/generators/rails/app/templates/test/integration/.empty_directory +0 -0
  367. data/lib/rails/generators/rails/app/templates/test/performance/browsing_test.rb +0 -12
  368. data/lib/rails/generators/rails/app/templates/test/test_helper.rb +0 -15
  369. data/lib/rails/generators/rails/app/templates/test/unit/.empty_directory +0 -0
  370. data/lib/rails/generators/rails/assets/USAGE +0 -20
  371. data/lib/rails/generators/rails/assets/assets_generator.rb +0 -25
  372. data/lib/rails/generators/rails/assets/templates/javascript.js +0 -2
  373. data/lib/rails/generators/rails/assets/templates/stylesheet.css +0 -4
  374. data/lib/rails/generators/rails/controller/USAGE +0 -18
  375. data/lib/rails/generators/rails/controller/controller_generator.rb +0 -20
  376. data/lib/rails/generators/rails/controller/templates/controller.rb +0 -13
  377. data/lib/rails/generators/rails/generator/USAGE +0 -12
  378. data/lib/rails/generators/rails/generator/generator_generator.rb +0 -25
  379. data/lib/rails/generators/rails/generator/templates/%file_name%_generator.rb.tt +0 -3
  380. data/lib/rails/generators/rails/generator/templates/USAGE.tt +0 -8
  381. data/lib/rails/generators/rails/generator/templates/templates/.empty_directory +0 -0
  382. data/lib/rails/generators/rails/helper/USAGE +0 -17
  383. data/lib/rails/generators/rails/helper/helper_generator.rb +0 -13
  384. data/lib/rails/generators/rails/helper/templates/helper.rb +0 -4
  385. data/lib/rails/generators/rails/integration_test/USAGE +0 -10
  386. data/lib/rails/generators/rails/integration_test/integration_test_generator.rb +0 -7
  387. data/lib/rails/generators/rails/migration/USAGE +0 -29
  388. data/lib/rails/generators/rails/migration/migration_generator.rb +0 -8
  389. data/lib/rails/generators/rails/model/USAGE +0 -45
  390. data/lib/rails/generators/rails/model/model_generator.rb +0 -8
  391. data/lib/rails/generators/rails/observer/USAGE +0 -12
  392. data/lib/rails/generators/rails/observer/observer_generator.rb +0 -7
  393. data/lib/rails/generators/rails/performance_test/USAGE +0 -10
  394. data/lib/rails/generators/rails/performance_test/performance_test_generator.rb +0 -7
  395. data/lib/rails/generators/rails/plugin_new/USAGE +0 -10
  396. data/lib/rails/generators/rails/plugin_new/plugin_new_generator.rb +0 -318
  397. data/lib/rails/generators/rails/plugin_new/templates/%name%.gemspec +0 -29
  398. data/lib/rails/generators/rails/plugin_new/templates/Gemfile +0 -23
  399. data/lib/rails/generators/rails/plugin_new/templates/MIT-LICENSE +0 -20
  400. data/lib/rails/generators/rails/plugin_new/templates/README.rdoc +0 -3
  401. data/lib/rails/generators/rails/plugin_new/templates/Rakefile +0 -31
  402. data/lib/rails/generators/rails/plugin_new/templates/app/controllers/%name%/application_controller.rb.tt +0 -4
  403. data/lib/rails/generators/rails/plugin_new/templates/app/helpers/%name%/application_helper.rb.tt +0 -4
  404. data/lib/rails/generators/rails/plugin_new/templates/app/mailers/.empty_directory +0 -0
  405. data/lib/rails/generators/rails/plugin_new/templates/app/models/.empty_directory +0 -0
  406. data/lib/rails/generators/rails/plugin_new/templates/app/views/layouts/%name%/application.html.erb.tt +0 -14
  407. data/lib/rails/generators/rails/plugin_new/templates/config/routes.rb +0 -6
  408. data/lib/rails/generators/rails/plugin_new/templates/gitignore +0 -7
  409. data/lib/rails/generators/rails/plugin_new/templates/lib/%name%/engine.rb +0 -7
  410. data/lib/rails/generators/rails/plugin_new/templates/lib/%name%/version.rb +0 -3
  411. data/lib/rails/generators/rails/plugin_new/templates/lib/%name%.rb +0 -6
  412. data/lib/rails/generators/rails/plugin_new/templates/lib/tasks/%name%_tasks.rake +0 -4
  413. data/lib/rails/generators/rails/plugin_new/templates/rails/application.rb +0 -18
  414. data/lib/rails/generators/rails/plugin_new/templates/rails/boot.rb +0 -10
  415. data/lib/rails/generators/rails/plugin_new/templates/rails/routes.rb +0 -4
  416. data/lib/rails/generators/rails/plugin_new/templates/script/rails.tt +0 -7
  417. data/lib/rails/generators/rails/plugin_new/templates/test/%name%_test.rb +0 -7
  418. data/lib/rails/generators/rails/plugin_new/templates/test/integration/navigation_test.rb +0 -12
  419. data/lib/rails/generators/rails/plugin_new/templates/test/test_helper.rb +0 -15
  420. data/lib/rails/generators/rails/resource/USAGE +0 -23
  421. data/lib/rails/generators/rails/resource/resource_generator.rb +0 -20
  422. data/lib/rails/generators/rails/resource_route/resource_route_generator.rb +0 -50
  423. data/lib/rails/generators/rails/scaffold/USAGE +0 -35
  424. data/lib/rails/generators/rails/scaffold/scaffold_generator.rb +0 -23
  425. data/lib/rails/generators/rails/scaffold/templates/scaffold.css +0 -56
  426. data/lib/rails/generators/rails/scaffold_controller/USAGE +0 -19
  427. data/lib/rails/generators/rails/scaffold_controller/scaffold_controller_generator.rb +0 -25
  428. data/lib/rails/generators/rails/scaffold_controller/templates/controller.rb +0 -89
  429. data/lib/rails/generators/rails/session_migration/USAGE +0 -8
  430. data/lib/rails/generators/rails/session_migration/session_migration_generator.rb +0 -8
  431. data/lib/rails/generators/rails/task/USAGE +0 -9
  432. data/lib/rails/generators/rails/task/task_generator.rb +0 -12
  433. data/lib/rails/generators/rails/task/templates/task.rb +0 -8
  434. data/lib/rails/generators/resource_helpers.rb +0 -80
  435. data/lib/rails/generators/test_case.rb +0 -247
  436. data/lib/rails/generators/test_unit/controller/controller_generator.rb +0 -15
  437. data/lib/rails/generators/test_unit/controller/templates/functional_test.rb +0 -19
  438. data/lib/rails/generators/test_unit/helper/helper_generator.rb +0 -13
  439. data/lib/rails/generators/test_unit/helper/templates/helper_test.rb +0 -6
  440. data/lib/rails/generators/test_unit/integration/integration_generator.rb +0 -13
  441. data/lib/rails/generators/test_unit/integration/templates/integration_test.rb +0 -7
  442. data/lib/rails/generators/test_unit/mailer/mailer_generator.rb +0 -14
  443. data/lib/rails/generators/test_unit/mailer/templates/functional_test.rb +0 -21
  444. data/lib/rails/generators/test_unit/model/model_generator.rb +0 -24
  445. data/lib/rails/generators/test_unit/model/templates/fixtures.yml +0 -23
  446. data/lib/rails/generators/test_unit/model/templates/unit_test.rb +0 -9
  447. data/lib/rails/generators/test_unit/observer/observer_generator.rb +0 -13
  448. data/lib/rails/generators/test_unit/observer/templates/unit_test.rb +0 -9
  449. data/lib/rails/generators/test_unit/performance/performance_generator.rb +0 -13
  450. data/lib/rails/generators/test_unit/performance/templates/performance_test.rb +0 -12
  451. data/lib/rails/generators/test_unit/plugin/plugin_generator.rb +0 -13
  452. data/lib/rails/generators/test_unit/plugin/templates/%file_name%_test.rb.tt +0 -7
  453. data/lib/rails/generators/test_unit/plugin/templates/test_helper.rb +0 -3
  454. data/lib/rails/generators/test_unit/scaffold/scaffold_generator.rb +0 -38
  455. data/lib/rails/generators/test_unit/scaffold/templates/functional_test.rb +0 -51
  456. data/lib/rails/generators/test_unit.rb +0 -8
  457. data/lib/rails/generators.rb +0 -339
  458. data/lib/rails/info.rb +0 -116
  459. data/lib/rails/info_controller.rb +0 -15
  460. data/lib/rails/initializable.rb +0 -89
  461. data/lib/rails/paths.rb +0 -211
  462. data/lib/rails/performance_test_help.rb +0 -3
  463. data/lib/rails/plugin.rb +0 -92
  464. data/lib/rails/rack/debugger.rb +0 -24
  465. data/lib/rails/rack/log_tailer.rb +0 -34
  466. data/lib/rails/rack/logger.rb +0 -60
  467. data/lib/rails/rack.rb +0 -7
  468. data/lib/rails/railtie/configurable.rb +0 -35
  469. data/lib/rails/railtie/configuration.rb +0 -90
  470. data/lib/rails/railtie.rb +0 -202
  471. data/lib/rails/ruby_version_check.rb +0 -23
  472. data/lib/rails/rubyprof_ext.rb +0 -35
  473. data/lib/rails/script_rails_loader.rb +0 -29
  474. data/lib/rails/source_annotation_extractor.rb +0 -104
  475. data/lib/rails/tasks/annotations.rake +0 -20
  476. data/lib/rails/tasks/documentation.rake +0 -155
  477. data/lib/rails/tasks/engine.rake +0 -72
  478. data/lib/rails/tasks/framework.rake +0 -76
  479. data/lib/rails/tasks/log.rake +0 -9
  480. data/lib/rails/tasks/middleware.rake +0 -7
  481. data/lib/rails/tasks/misc.rake +0 -67
  482. data/lib/rails/tasks/routes.rake +0 -9
  483. data/lib/rails/tasks/statistics.rake +0 -16
  484. data/lib/rails/tasks/tmp.rake +0 -37
  485. data/lib/rails/tasks.rb +0 -16
  486. data/lib/rails/test_help.rb +0 -56
  487. data/lib/rails/test_unit/railtie.rb +0 -15
  488. data/lib/rails/test_unit/sub_test_task.rb +0 -8
  489. data/lib/rails/test_unit/testing.rake +0 -146
  490. data/lib/rails.rb +0 -121
  491. data/lib/version.rb +0 -16
@@ -1,1283 +0,0 @@
1
- h2. Active Record Validations and Callbacks
2
-
3
- This guide teaches you how to hook into the life cycle of your Active Record objects. You will learn how to validate the state of objects before they go into the database, and how to perform custom operations at certain points in the object life cycle.
4
-
5
- After reading this guide and trying out the presented concepts, we hope that you'll be able to:
6
-
7
- * Understand the life cycle of Active Record objects
8
- * Use the built-in Active Record validation helpers
9
- * Create your own custom validation methods
10
- * Work with the error messages generated by the validation process
11
- * Create callback methods that respond to events in the object life cycle
12
- * Create special classes that encapsulate common behavior for your callbacks
13
- * Create Observers that respond to life cycle events outside of the original class
14
-
15
- endprologue.
16
-
17
- h3. The Object Life Cycle
18
-
19
- During the normal operation of a Rails application, objects may be created, updated, and destroyed. Active Record provides hooks into this <em>object life cycle</em> so that you can control your application and its data.
20
-
21
- Validations allow you to ensure that only valid data is stored in your database. Callbacks and observers allow you to trigger logic before or after an alteration of an object's state.
22
-
23
- h3. Validations Overview
24
-
25
- Before you dive into the detail of validations in Rails, you should understand a bit about how validations fit into the big picture.
26
-
27
- h4. Why Use Validations?
28
-
29
- Validations are used to ensure that only valid data is saved into your database. For example, it may be important to your application to ensure that every user provides a valid email address and mailing address.
30
-
31
- There are several ways to validate data before it is saved into your database, including native database constraints, client-side validations, controller-level validations, and model-level validations:
32
-
33
- * Database constraints and/or stored procedures make the validation mechanisms database-dependent and can make testing and maintenance more difficult. However, if your database is used by other applications, it may be a good idea to use some constraints at the database level. Additionally, database-level validations can safely handle some things (such as uniqueness in heavily-used tables) that can be difficult to implement otherwise.
34
- * Client-side validations can be useful, but are generally unreliable if used alone. If they are implemented using JavaScript, they may be bypassed if JavaScript is turned off in the user's browser. However, if combined with other techniques, client-side validation can be a convenient way to provide users with immediate feedback as they use your site.
35
- * Controller-level validations can be tempting to use, but often become unwieldy and difficult to test and maintain. Whenever possible, it's a good idea to "keep your controllers skinny":http://weblog.jamisbuck.org/2006/10/18/skinny-controller-fat-model, as it will make your application a pleasure to work with in the long run.
36
- * Model-level validations are the best way to ensure that only valid data is saved into your database. They are database agnostic, cannot be bypassed by end users, and are convenient to test and maintain. Rails makes them easy to use, provides built-in helpers for common needs, and allows you to create your own validation methods as well.
37
-
38
- h4. When Does Validation Happen?
39
-
40
- There are two kinds of Active Record objects: those that correspond to a row inside your database and those that do not. When you create a fresh object, for example using the +new+ method, that object does not belong to the database yet. Once you call +save+ upon that object it will be saved into the appropriate database table. Active Record uses the +new_record?+ instance method to determine whether an object is already in the database or not. Consider the following simple Active Record class:
41
-
42
- <ruby>
43
- class Person < ActiveRecord::Base
44
- end
45
- </ruby>
46
-
47
- We can see how it works by looking at some +rails console+ output:
48
-
49
- <ruby>
50
- >> p = Person.new(:name => "John Doe")
51
- => #<Person id: nil, name: "John Doe", created_at: nil, :updated_at: nil>
52
- >> p.new_record?
53
- => true
54
- >> p.save
55
- => true
56
- >> p.new_record?
57
- => false
58
- </ruby>
59
-
60
- Creating and saving a new record will send an SQL +INSERT+ operation to the database. Updating an existing record will send an SQL +UPDATE+ operation instead. Validations are typically run before these commands are sent to the database. If any validations fail, the object will be marked as invalid and Active Record will not perform the +INSERT+ or +UPDATE+ operation. This helps to avoid storing an invalid object in the database. You can choose to have specific validations run when an object is created, saved, or updated.
61
-
62
- CAUTION: There are many ways to change the state of an object in the database. Some methods will trigger validations, but some will not. This means that it's possible to save an object in the database in an invalid state if you aren't careful.
63
-
64
- The following methods trigger validations, and will save the object to the database only if the object is valid:
65
-
66
- * +create+
67
- * +create!+
68
- * +save+
69
- * +save!+
70
- * +update+
71
- * +update_attributes+
72
- * +update_attributes!+
73
-
74
- The bang versions (e.g. +save!+) raise an exception if the record is invalid. The non-bang versions don't: +save+ and +update_attributes+ return +false+, +create+ and +update+ just return the objects.
75
-
76
- h4. Skipping Validations
77
-
78
- The following methods skip validations, and will save the object to the database regardless of its validity. They should be used with caution.
79
-
80
- * +decrement!+
81
- * +decrement_counter+
82
- * +increment!+
83
- * +increment_counter+
84
- * +toggle!+
85
- * +touch+
86
- * +update_all+
87
- * +update_attribute+
88
- * +update_column+
89
- * +update_counters+
90
-
91
- Note that +save+ also has the ability to skip validations if passed +:validate => false+ as argument. This technique should be used with caution.
92
-
93
- * +save(:validate => false)+
94
-
95
- h4. +valid?+ and +invalid?+
96
-
97
- To verify whether or not an object is valid, Rails uses the +valid?+ method. You can also use this method on your own. +valid?+ triggers your validations and returns true if no errors were found in the object, and false otherwise.
98
-
99
- <ruby>
100
- class Person < ActiveRecord::Base
101
- validates :name, :presence => true
102
- end
103
-
104
- Person.create(:name => "John Doe").valid? # => true
105
- Person.create(:name => nil).valid? # => false
106
- </ruby>
107
-
108
- After Active Record has performed validations, any errors found can be accessed through the +errors+ instance method, which returns a collection of errors. By definition, an object is valid if this collection is empty after running validations.
109
-
110
- Note that an object instantiated with +new+ will not report errors even if it's technically invalid, because validations are not run when using +new+.
111
-
112
- <ruby>
113
- class Person < ActiveRecord::Base
114
- validates :name, :presence => true
115
- end
116
-
117
- >> p = Person.new
118
- => #<Person id: nil, name: nil>
119
- >> p.errors
120
- => {}
121
-
122
- >> p.valid?
123
- => false
124
- >> p.errors
125
- => {:name=>["can't be blank"]}
126
-
127
- >> p = Person.create
128
- => #<Person id: nil, name: nil>
129
- >> p.errors
130
- => {:name=>["can't be blank"]}
131
-
132
- >> p.save
133
- => false
134
-
135
- >> p.save!
136
- => ActiveRecord::RecordInvalid: Validation failed: Name can't be blank
137
-
138
- >> Person.create!
139
- => ActiveRecord::RecordInvalid: Validation failed: Name can't be blank
140
- </ruby>
141
-
142
- +invalid?+ is simply the inverse of +valid?+. +invalid?+ triggers your validations, returning true if any errors were found in the object, and false otherwise.
143
-
144
- h4(#validations_overview-errors). +errors[]+
145
-
146
- To verify whether or not a particular attribute of an object is valid, you can use +errors[:attribute]+. It returns an array of all the errors for +:attribute+. If there are no errors on the specified attribute, an empty array is returned.
147
-
148
- This method is only useful _after_ validations have been run, because it only inspects the errors collection and does not trigger validations itself. It's different from the +ActiveRecord::Base#invalid?+ method explained above because it doesn't verify the validity of the object as a whole. It only checks to see whether there are errors found on an individual attribute of the object.
149
-
150
- <ruby>
151
- class Person < ActiveRecord::Base
152
- validates :name, :presence => true
153
- end
154
-
155
- >> Person.new.errors[:name].any? # => false
156
- >> Person.create.errors[:name].any? # => true
157
- </ruby>
158
-
159
- We'll cover validation errors in greater depth in the "Working with Validation Errors":#working-with-validation-errors section. For now, let's turn to the built-in validation helpers that Rails provides by default.
160
-
161
- h3. Validation Helpers
162
-
163
- Active Record offers many pre-defined validation helpers that you can use directly inside your class definitions. These helpers provide common validation rules. Every time a validation fails, an error message is added to the object's +errors+ collection, and this message is associated with the attribute being validated.
164
-
165
- Each helper accepts an arbitrary number of attribute names, so with a single line of code you can add the same kind of validation to several attributes.
166
-
167
- All of them accept the +:on+ and +:message+ options, which define when the validation should be run and what message should be added to the +errors+ collection if it fails, respectively. The +:on+ option takes one of the values +:save+ (the default), +:create+ or +:update+. There is a default error message for each one of the validation helpers. These messages are used when the +:message+ option isn't specified. Let's take a look at each one of the available helpers.
168
-
169
- h4. +acceptance+
170
-
171
- Validates that a checkbox on the user interface was checked when a form was submitted. This is typically used when the user needs to agree to your application's terms of service, confirm reading some text, or any similar concept. This validation is very specific to web applications and this 'acceptance' does not need to be recorded anywhere in your database (if you don't have a field for it, the helper will just create a virtual attribute).
172
-
173
- <ruby>
174
- class Person < ActiveRecord::Base
175
- validates :terms_of_service, :acceptance => true
176
- end
177
- </ruby>
178
-
179
- The default error message for this helper is "_must be accepted_".
180
-
181
- It can receive an +:accept+ option, which determines the value that will be considered acceptance. It defaults to "1" and can be easily changed.
182
-
183
- <ruby>
184
- class Person < ActiveRecord::Base
185
- validates :terms_of_service, :acceptance => { :accept => 'yes' }
186
- end
187
- </ruby>
188
-
189
- h4. +validates_associated+
190
-
191
- You should use this helper when your model has associations with other models and they also need to be validated. When you try to save your object, +valid?+ will be called upon each one of the associated objects.
192
-
193
- <ruby>
194
- class Library < ActiveRecord::Base
195
- has_many :books
196
- validates_associated :books
197
- end
198
- </ruby>
199
-
200
- This validation will work with all of the association types.
201
-
202
- CAUTION: Don't use +validates_associated+ on both ends of your associations. They would call each other in an infinite loop.
203
-
204
- The default error message for +validates_associated+ is "_is invalid_". Note that each associated object will contain its own +errors+ collection; errors do not bubble up to the calling model.
205
-
206
- h4. +confirmation+
207
-
208
- You should use this helper when you have two text fields that should receive exactly the same content. For example, you may want to confirm an email address or a password. This validation creates a virtual attribute whose name is the name of the field that has to be confirmed with "_confirmation" appended.
209
-
210
- <ruby>
211
- class Person < ActiveRecord::Base
212
- validates :email, :confirmation => true
213
- end
214
- </ruby>
215
-
216
- In your view template you could use something like
217
-
218
- <erb>
219
- <%= text_field :person, :email %>
220
- <%= text_field :person, :email_confirmation %>
221
- </erb>
222
-
223
- This check is performed only if +email_confirmation+ is not +nil+. To require confirmation, make sure to add a presence check for the confirmation attribute (we'll take a look at +presence+ later on this guide):
224
-
225
- <ruby>
226
- class Person < ActiveRecord::Base
227
- validates :email, :confirmation => true
228
- validates :email_confirmation, :presence => true
229
- end
230
- </ruby>
231
-
232
- The default error message for this helper is "_doesn't match confirmation_".
233
-
234
- h4. +exclusion+
235
-
236
- This helper validates that the attributes' values are not included in a given set. In fact, this set can be any enumerable object.
237
-
238
- <ruby>
239
- class Account < ActiveRecord::Base
240
- validates :subdomain, :exclusion => { :in => %w(www us ca jp),
241
- :message => "Subdomain %{value} is reserved." }
242
- end
243
- </ruby>
244
-
245
- The +exclusion+ helper has an option +:in+ that receives the set of values that will not be accepted for the validated attributes. The +:in+ option has an alias called +:within+ that you can use for the same purpose, if you'd like to. This example uses the +:message+ option to show how you can include the attribute's value.
246
-
247
- The default error message is "_is reserved_".
248
-
249
- h4. +format+
250
-
251
- This helper validates the attributes' values by testing whether they match a given regular expression, which is specified using the +:with+ option.
252
-
253
- <ruby>
254
- class Product < ActiveRecord::Base
255
- validates :legacy_code, :format => { :with => /\A[a-zA-Z]+\z/,
256
- :message => "Only letters allowed" }
257
- end
258
- </ruby>
259
-
260
- The default error message is "_is invalid_".
261
-
262
- h4. +inclusion+
263
-
264
- This helper validates that the attributes' values are included in a given set. In fact, this set can be any enumerable object.
265
-
266
- <ruby>
267
- class Coffee < ActiveRecord::Base
268
- validates :size, :inclusion => { :in => %w(small medium large),
269
- :message => "%{value} is not a valid size" }
270
- end
271
- </ruby>
272
-
273
- The +inclusion+ helper has an option +:in+ that receives the set of values that will be accepted. The +:in+ option has an alias called +:within+ that you can use for the same purpose, if you'd like to. The previous example uses the +:message+ option to show how you can include the attribute's value.
274
-
275
- The default error message for this helper is "_is not included in the list_".
276
-
277
- h4. +length+
278
-
279
- This helper validates the length of the attributes' values. It provides a variety of options, so you can specify length constraints in different ways:
280
-
281
- <ruby>
282
- class Person < ActiveRecord::Base
283
- validates :name, :length => { :minimum => 2 }
284
- validates :bio, :length => { :maximum => 500 }
285
- validates :password, :length => { :in => 6..20 }
286
- validates :registration_number, :length => { :is => 6 }
287
- end
288
- </ruby>
289
-
290
- The possible length constraint options are:
291
-
292
- * +:minimum+ - The attribute cannot have less than the specified length.
293
- * +:maximum+ - The attribute cannot have more than the specified length.
294
- * +:in+ (or +:within+) - The attribute length must be included in a given interval. The value for this option must be a range.
295
- * +:is+ - The attribute length must be equal to the given value.
296
-
297
- The default error messages depend on the type of length validation being performed. You can personalize these messages using the +:wrong_length+, +:too_long+, and +:too_short+ options and <tt>%{count}</tt> as a placeholder for the number corresponding to the length constraint being used. You can still use the +:message+ option to specify an error message.
298
-
299
- <ruby>
300
- class Person < ActiveRecord::Base
301
- validates :bio, :length => { :maximum => 1000,
302
- :too_long => "%{count} characters is the maximum allowed" }
303
- end
304
- </ruby>
305
-
306
- This helper counts characters by default, but you can split the value in a different way using the +:tokenizer+ option:
307
-
308
- <ruby>
309
- class Essay < ActiveRecord::Base
310
- validates :content, :length => {
311
- :minimum => 300,
312
- :maximum => 400,
313
- :tokenizer => lambda { |str| str.scan(/\w+/) },
314
- :too_short => "must have at least %{count} words",
315
- :too_long => "must have at most %{count} words"
316
- }
317
- end
318
- </ruby>
319
-
320
- Note that the default error messages are plural (e.g., "is too short (minimum is %{count} characters)"). For this reason, when +:minimum+ is 1 you should provide a personalized message or use +validates_presence_of+ instead. When +:in+ or +:within+ have a lower limit of 1, you should either provide a personalized message or call +presence+ prior to +length+.
321
-
322
- The +size+ helper is an alias for +length+.
323
-
324
- h4. +numericality+
325
-
326
- This helper validates that your attributes have only numeric values. By default, it will match an optional sign followed by an integral or floating point number. To specify that only integral numbers are allowed set +:only_integer+ to true.
327
-
328
- If you set +:only_integer+ to +true+, then it will use the
329
-
330
- <ruby>
331
- /\A[<plus>-]?\d<plus>\Z/
332
- </ruby>
333
-
334
- regular expression to validate the attribute's value. Otherwise, it will try to convert the value to a number using +Float+.
335
-
336
- WARNING. Note that the regular expression above allows a trailing newline character.
337
-
338
- <ruby>
339
- class Player < ActiveRecord::Base
340
- validates :points, :numericality => true
341
- validates :games_played, :numericality => { :only_integer => true }
342
- end
343
- </ruby>
344
-
345
- Besides +:only_integer+, this helper also accepts the following options to add constraints to acceptable values:
346
-
347
- * +:greater_than+ - Specifies the value must be greater than the supplied value. The default error message for this option is "_must be greater than %{count}_".
348
- * +:greater_than_or_equal_to+ - Specifies the value must be greater than or equal to the supplied value. The default error message for this option is "_must be greater than or equal to %{count}_".
349
- * +:equal_to+ - Specifies the value must be equal to the supplied value. The default error message for this option is "_must be equal to %{count}_".
350
- * +:less_than+ - Specifies the value must be less than the supplied value. The default error message for this option is "_must be less than %{count}_".
351
- * +:less_than_or_equal_to+ - Specifies the value must be less than or equal the supplied value. The default error message for this option is "_must be less than or equal to %{count}_".
352
- * +:odd+ - Specifies the value must be an odd number if set to true. The default error message for this option is "_must be odd_".
353
- * +:even+ - Specifies the value must be an even number if set to true. The default error message for this option is "_must be even_".
354
-
355
- The default error message is "_is not a number_".
356
-
357
- h4. +presence+
358
-
359
- This helper validates that the specified attributes are not empty. It uses the +blank?+ method to check if the value is either +nil+ or a blank string, that is, a string that is either empty or consists of whitespace.
360
-
361
- <ruby>
362
- class Person < ActiveRecord::Base
363
- validates :name, :login, :email, :presence => true
364
- end
365
- </ruby>
366
-
367
- If you want to be sure that an association is present, you'll need to test whether the foreign key used to map the association is present, and not the associated object itself.
368
-
369
- <ruby>
370
- class LineItem < ActiveRecord::Base
371
- belongs_to :order
372
- validates :order_id, :presence => true
373
- end
374
- </ruby>
375
-
376
- Since +false.blank?+ is true, if you want to validate the presence of a boolean field you should use <tt>validates :field_name, :inclusion => { :in => [true, false] }</tt>.
377
-
378
- The default error message is "_can't be empty_".
379
-
380
- h4. +uniqueness+
381
-
382
- This helper validates that the attribute's value is unique right before the object gets saved. It does not create a uniqueness constraint in the database, so it may happen that two different database connections create two records with the same value for a column that you intend to be unique. To avoid that, you must create a unique index in your database.
383
-
384
- <ruby>
385
- class Account < ActiveRecord::Base
386
- validates :email, :uniqueness => true
387
- end
388
- </ruby>
389
-
390
- The validation happens by performing an SQL query into the model's table, searching for an existing record with the same value in that attribute.
391
-
392
- There is a +:scope+ option that you can use to specify other attributes that are used to limit the uniqueness check:
393
-
394
- <ruby>
395
- class Holiday < ActiveRecord::Base
396
- validates :name, :uniqueness => { :scope => :year,
397
- :message => "should happen once per year" }
398
- end
399
- </ruby>
400
-
401
- There is also a +:case_sensitive+ option that you can use to define whether the uniqueness constraint will be case sensitive or not. This option defaults to true.
402
-
403
- <ruby>
404
- class Person < ActiveRecord::Base
405
- validates :name, :uniqueness => { :case_sensitive => false }
406
- end
407
- </ruby>
408
-
409
- WARNING. Note that some databases are configured to perform case-insensitive searches anyway.
410
-
411
- The default error message is "_has already been taken_".
412
-
413
- h4. +validates_with+
414
-
415
- This helper passes the record to a separate class for validation.
416
-
417
- <ruby>
418
- class Person < ActiveRecord::Base
419
- validates_with GoodnessValidator
420
- end
421
-
422
- class GoodnessValidator < ActiveModel::Validator
423
- def validate(record)
424
- if record.first_name == "Evil"
425
- record.errors[:base] << "This person is evil"
426
- end
427
- end
428
- end
429
- </ruby>
430
-
431
- NOTE: Errors added to +record.errors[:base]+ relate to the state of the record as a whole, and not to a specific attribute.
432
-
433
- The +validates_with+ helper takes a class, or a list of classes to use for validation. There is no default error message for +validates_with+. You must manually add errors to the record's errors collection in the validator class.
434
-
435
- To implement the validate method, you must have a +record+ parameter defined, which is the record to be validated.
436
-
437
- Like all other validations, +validates_with+ takes the +:if+, +:unless+ and +:on+ options. If you pass any other options, it will send those options to the validator class as +options+:
438
-
439
- <ruby>
440
- class Person < ActiveRecord::Base
441
- validates_with GoodnessValidator, :fields => [:first_name, :last_name]
442
- end
443
-
444
- class GoodnessValidator < ActiveModel::Validator
445
- def validate(record)
446
- if options[:fields].any?{|field| record.send(field) == "Evil" }
447
- record.errors[:base] << "This person is evil"
448
- end
449
- end
450
- end
451
- </ruby>
452
-
453
- h4. +validates_each+
454
-
455
- This helper validates attributes against a block. It doesn't have a predefined validation function. You should create one using a block, and every attribute passed to +validates_each+ will be tested against it. In the following example, we don't want names and surnames to begin with lower case.
456
-
457
- <ruby>
458
- class Person < ActiveRecord::Base
459
- validates_each :name, :surname do |record, attr, value|
460
- record.errors.add(attr, 'must start with upper case') if value =~ /\A[a-z]/
461
- end
462
- end
463
- </ruby>
464
-
465
- The block receives the record, the attribute's name and the attribute's value. You can do anything you like to check for valid data within the block. If your validation fails, you should add an error message to the model, therefore making it invalid.
466
-
467
- h3. Common Validation Options
468
-
469
- These are common validation options:
470
-
471
- h4. +:allow_nil+
472
-
473
- The +:allow_nil+ option skips the validation when the value being validated is +nil+.
474
-
475
- <ruby>
476
- class Coffee < ActiveRecord::Base
477
- validates :size, :inclusion => { :in => %w(small medium large),
478
- :message => "%{value} is not a valid size" }, :allow_nil => true
479
- end
480
- </ruby>
481
-
482
- TIP: +:allow_nil+ is ignored by the presence validator.
483
-
484
- h4. +:allow_blank+
485
-
486
- The +:allow_blank+ option is similar to the +:allow_nil+ option. This option will let validation pass if the attribute's value is +blank?+, like +nil+ or an empty string for example.
487
-
488
- <ruby>
489
- class Topic < ActiveRecord::Base
490
- validates :title, :length => { :is => 5 }, :allow_blank => true
491
- end
492
-
493
- Topic.create("title" => "").valid? # => true
494
- Topic.create("title" => nil).valid? # => true
495
- </ruby>
496
-
497
- TIP: +:allow_blank+ is ignored by the presence validator.
498
-
499
- h4. +:message+
500
-
501
- As you've already seen, the +:message+ option lets you specify the message that will be added to the +errors+ collection when validation fails. When this option is not used, Active Record will use the respective default error message for each validation helper.
502
-
503
- h4. +:on+
504
-
505
- The +:on+ option lets you specify when the validation should happen. The default behavior for all the built-in validation helpers is to be run on save (both when you're creating a new record and when you're updating it). If you want to change it, you can use +:on => :create+ to run the validation only when a new record is created or +:on => :update+ to run the validation only when a record is updated.
506
-
507
- <ruby>
508
- class Person < ActiveRecord::Base
509
- # it will be possible to update email with a duplicated value
510
- validates :email, :uniqueness => true, :on => :create
511
-
512
- # it will be possible to create the record with a non-numerical age
513
- validates :age, :numericality => true, :on => :update
514
-
515
- # the default (validates on both create and update)
516
- validates :name, :presence => true, :on => :save
517
- end
518
- </ruby>
519
-
520
- h3. Conditional Validation
521
-
522
- Sometimes it will make sense to validate an object just when a given predicate is satisfied. You can do that by using the +:if+ and +:unless+ options, which can take a symbol, a string or a +Proc+. You may use the +:if+ option when you want to specify when the validation *should* happen. If you want to specify when the validation *should not* happen, then you may use the +:unless+ option.
523
-
524
- h4. Using a Symbol with +:if+ and +:unless+
525
-
526
- You can associate the +:if+ and +:unless+ options with a symbol corresponding to the name of a method that will get called right before validation happens. This is the most commonly used option.
527
-
528
- <ruby>
529
- class Order < ActiveRecord::Base
530
- validates :card_number, :presence => true, :if => :paid_with_card?
531
-
532
- def paid_with_card?
533
- payment_type == "card"
534
- end
535
- end
536
- </ruby>
537
-
538
- h4. Using a String with +:if+ and +:unless+
539
-
540
- You can also use a string that will be evaluated using +eval+ and needs to contain valid Ruby code. You should use this option only when the string represents a really short condition.
541
-
542
- <ruby>
543
- class Person < ActiveRecord::Base
544
- validates :surname, :presence => true, :if => "name.nil?"
545
- end
546
- </ruby>
547
-
548
- h4. Using a Proc with +:if+ and +:unless+
549
-
550
- Finally, it's possible to associate +:if+ and +:unless+ with a +Proc+ object which will be called. Using a +Proc+ object gives you the ability to write an inline condition instead of a separate method. This option is best suited for one-liners.
551
-
552
- <ruby>
553
- class Account < ActiveRecord::Base
554
- validates :password, :confirmation => true,
555
- :unless => Proc.new { |a| a.password.blank? }
556
- end
557
- </ruby>
558
-
559
- h4. Grouping conditional validations
560
-
561
- Sometimes it is useful to have multiple validations use one condition, it can be easily achieved using +with_options+.
562
-
563
- <ruby>
564
- class User < ActiveRecord::Base
565
- with_options :if => :is_admin? do |admin|
566
- admin.validates :password, :length => { :minimum => 10 }
567
- admin.validates :email, :presence => true
568
- end
569
- end
570
- </ruby>
571
-
572
- All validations inside of +with_options+ block will have automatically passed the condition +:if => :is_admin?+
573
-
574
- h3. Performing Custom Validations
575
-
576
- When the built-in validation helpers are not enough for your needs, you can write your own validators or validation methods as you prefer.
577
-
578
- h4. Custom Validators
579
-
580
- Custom validators are classes that extend <tt>ActiveModel::Validator</tt>. These classes must implement a +validate+ method which takes a record as an argument and performs the validation on it. The custom validator is called using the +validates_with+ method.
581
-
582
- <ruby>
583
- class MyValidator < ActiveModel::Validator
584
- def validate(record)
585
- unless record.name.starts_with? 'X'
586
- record.errors[:name] << 'Need a name starting with X please!'
587
- end
588
- end
589
- end
590
-
591
- class Person
592
- include ActiveModel::Validations
593
- validates_with MyValidator
594
- end
595
- </ruby>
596
-
597
- The easiest way to add custom validators for validating individual attributes is with the convenient <tt>ActiveModel::EachValidator</tt>. In this case, the custom validator class must implement a +validate_each+ method which takes three arguments: record, attribute and value which correspond to the instance, the attribute to be validated and the value of the attribute in the passed instance.
598
-
599
- <ruby>
600
- class EmailValidator < ActiveModel::EachValidator
601
- def validate_each(record, attribute, value)
602
- unless value =~ /\A([^@\s]<plus>)@((?:[-a-z0-9]<plus>\.)+[a-z]{2,})\z/i
603
- record.errors[attribute] << (options[:message] || "is not an email")
604
- end
605
- end
606
- end
607
-
608
- class Person < ActiveRecord::Base
609
- validates :email, :presence => true, :email => true
610
- end
611
- </ruby>
612
-
613
- As shown in the example, you can also combine standard validations with your own custom validators.
614
-
615
- h4. Custom Methods
616
-
617
- You can also create methods that verify the state of your models and add messages to the +errors+ collection when they are invalid. You must then register these methods by using the +validate+ class method, passing in the symbols for the validation methods' names.
618
-
619
- You can pass more than one symbol for each class method and the respective validations will be run in the same order as they were registered.
620
-
621
- <ruby>
622
- class Invoice < ActiveRecord::Base
623
- validate :expiration_date_cannot_be_in_the_past,
624
- :discount_cannot_be_greater_than_total_value
625
-
626
- def expiration_date_cannot_be_in_the_past
627
- if !expiration_date.blank? and expiration_date < Date.today
628
- errors.add(:expiration_date, "can't be in the past")
629
- end
630
- end
631
-
632
- def discount_cannot_be_greater_than_total_value
633
- if discount > total_value
634
- errors.add(:discount, "can't be greater than total value")
635
- end
636
- end
637
- end
638
- </ruby>
639
-
640
- By default such validations will run every time you call +valid?+. It is also possible to control when to run these custom validations by giving an +:on+ option to the +validate+ method, with either: +:create+ or +:update+.
641
-
642
- <ruby>
643
- class Invoice < ActiveRecord::Base
644
- validate :active_customer, :on => :create
645
-
646
- def active_customer
647
- errors.add(:customer_id, "is not active") unless customer.active?
648
- end
649
- end
650
- </ruby>
651
-
652
- You can even create your own validation helpers and reuse them in several different models. For example, an application that manages surveys may find it useful to express that a certain field corresponds to a set of choices:
653
-
654
- <ruby>
655
- ActiveRecord::Base.class_eval do
656
- def self.validates_as_choice(attr_name, n, options={})
657
- validates attr_name, :inclusion => { { :in => 1..n }.merge!(options) }
658
- end
659
- end
660
- </ruby>
661
-
662
- Simply reopen +ActiveRecord::Base+ and define a class method like that. You'd typically put this code somewhere in +config/initializers+. You can use this helper like this:
663
-
664
- <ruby>
665
- class Movie < ActiveRecord::Base
666
- validates_as_choice :rating, 5
667
- end
668
- </ruby>
669
-
670
- h3. Working with Validation Errors
671
-
672
- In addition to the +valid?+ and +invalid?+ methods covered earlier, Rails provides a number of methods for working with the +errors+ collection and inquiring about the validity of objects.
673
-
674
- The following is a list of the most commonly used methods. Please refer to the +ActiveModel::Errors+ documentation for a list of all the available methods.
675
-
676
- h4(#working_with_validation_errors-errors). +errors+
677
-
678
- Returns an instance of the class +ActiveModel::Errors+ (which behaves like an ordered hash) containing all errors. Each key is the attribute name and the value is an array of strings with all errors.
679
-
680
- <ruby>
681
- class Person < ActiveRecord::Base
682
- validates :name, :presence => true, :length => { :minimum => 3 }
683
- end
684
-
685
- person = Person.new
686
- person.valid? # => false
687
- person.errors
688
- # => {:name => ["can't be blank", "is too short (minimum is 3 characters)"]}
689
-
690
- person = Person.new(:name => "John Doe")
691
- person.valid? # => true
692
- person.errors # => []
693
- </ruby>
694
-
695
- h4(#working_with_validation_errors-errors-2). +errors[]+
696
-
697
- +errors[]+ is used when you want to check the error messages for a specific attribute. It returns an array of strings with all error messages for the given attribute, each string with one error message. If there are no errors related to the attribute, it returns an empty array.
698
-
699
- <ruby>
700
- class Person < ActiveRecord::Base
701
- validates :name, :presence => true, :length => { :minimum => 3 }
702
- end
703
-
704
- person = Person.new(:name => "John Doe")
705
- person.valid? # => true
706
- person.errors[:name] # => []
707
-
708
- person = Person.new(:name => "JD")
709
- person.valid? # => false
710
- person.errors[:name] # => ["is too short (minimum is 3 characters)"]
711
-
712
- person = Person.new
713
- person.valid? # => false
714
- person.errors[:name]
715
- # => ["can't be blank", "is too short (minimum is 3 characters)"]
716
- </ruby>
717
-
718
- h4. +errors.add+
719
-
720
- The +add+ method lets you manually add messages that are related to particular attributes. You can use the +errors.full_messages+ or +errors.to_a+ methods to view the messages in the form they might be displayed to a user. Those particular messages get the attribute name prepended (and capitalized). +add+ receives the name of the attribute you want to add the message to, and the message itself.
721
-
722
- <ruby>
723
- class Person < ActiveRecord::Base
724
- def a_method_used_for_validation_purposes
725
- errors.add(:name, "cannot contain the characters !@#%*()_-+=")
726
- end
727
- end
728
-
729
- person = Person.create(:name => "!@#")
730
-
731
- person.errors[:name]
732
- # => ["cannot contain the characters !@#%*()_-+="]
733
-
734
- person.errors.full_messages
735
- # => ["Name cannot contain the characters !@#%*()_-+="]
736
- </ruby>
737
-
738
- Another way to do this is using +[]=+ setter
739
-
740
- <ruby>
741
- class Person < ActiveRecord::Base
742
- def a_method_used_for_validation_purposes
743
- errors[:name] = "cannot contain the characters !@#%*()_-+="
744
- end
745
- end
746
-
747
- person = Person.create(:name => "!@#")
748
-
749
- person.errors[:name]
750
- # => ["cannot contain the characters !@#%*()_-+="]
751
-
752
- person.errors.to_a
753
- # => ["Name cannot contain the characters !@#%*()_-+="]
754
- </ruby>
755
-
756
- h4. +errors[:base]+
757
-
758
- You can add error messages that are related to the object's state as a whole, instead of being related to a specific attribute. You can use this method when you want to say that the object is invalid, no matter the values of its attributes. Since +errors[:base]+ is an array, you can simply add a string to it and it will be used as an error message.
759
-
760
- <ruby>
761
- class Person < ActiveRecord::Base
762
- def a_method_used_for_validation_purposes
763
- errors[:base] << "This person is invalid because ..."
764
- end
765
- end
766
- </ruby>
767
-
768
- h4. +errors.clear+
769
-
770
- The +clear+ method is used when you intentionally want to clear all the messages in the +errors+ collection. Of course, calling +errors.clear+ upon an invalid object won't actually make it valid: the +errors+ collection will now be empty, but the next time you call +valid?+ or any method that tries to save this object to the database, the validations will run again. If any of the validations fail, the +errors+ collection will be filled again.
771
-
772
- <ruby>
773
- class Person < ActiveRecord::Base
774
- validates :name, :presence => true, :length => { :minimum => 3 }
775
- end
776
-
777
- person = Person.new
778
- person.valid? # => false
779
- person.errors[:name]
780
- # => ["can't be blank", "is too short (minimum is 3 characters)"]
781
-
782
- person.errors.clear
783
- person.errors.empty? # => true
784
-
785
- p.save # => false
786
-
787
- p.errors[:name]
788
- # => ["can't be blank", "is too short (minimum is 3 characters)"]
789
- </ruby>
790
-
791
- h4. +errors.size+
792
-
793
- The +size+ method returns the total number of error messages for the object.
794
-
795
- <ruby>
796
- class Person < ActiveRecord::Base
797
- validates :name, :presence => true, :length => { :minimum => 3 }
798
- end
799
-
800
- person = Person.new
801
- person.valid? # => false
802
- person.errors.size # => 2
803
-
804
- person = Person.new(:name => "Andrea", :email => "andrea@example.com")
805
- person.valid? # => true
806
- person.errors.size # => 0
807
- </ruby>
808
-
809
- h3. Displaying Validation Errors in the View
810
-
811
- "DynamicForm":https://github.com/joelmoss/dynamic_form provides helpers to display the error messages of your models in your view templates.
812
-
813
- You can install it as a gem by adding this line to your Gemfile:
814
-
815
- <ruby>
816
- gem "dynamic_form"
817
- </ruby>
818
-
819
- Now you will have access to the two helper methods +error_messages+ and +error_messages_for+ in your view templates.
820
-
821
- h4. +error_messages+ and +error_messages_for+
822
-
823
- When creating a form with the +form_for+ helper, you can use the +error_messages+ method on the form builder to render all failed validation messages for the current model instance.
824
-
825
- <ruby>
826
- class Product < ActiveRecord::Base
827
- validates :description, :value, :presence => true
828
- validates :value, :numericality => true, :allow_nil => true
829
- end
830
- </ruby>
831
-
832
- <erb>
833
- <%= form_for(@product) do |f| %>
834
- <%= f.error_messages %>
835
- <p>
836
- <%= f.label :description %><br />
837
- <%= f.text_field :description %>
838
- </p>
839
- <p>
840
- <%= f.label :value %><br />
841
- <%= f.text_field :value %>
842
- </p>
843
- <p>
844
- <%= f.submit "Create" %>
845
- </p>
846
- <% end %>
847
- </erb>
848
-
849
- If you submit the form with empty fields, the result will be similar to the one shown below:
850
-
851
- !images/error_messages.png(Error messages)!
852
-
853
- NOTE: The appearance of the generated HTML will be different from the one shown, unless you have used scaffolding. See "Customizing the Error Messages CSS":#customizing-error-messages-css.
854
-
855
- You can also use the +error_messages_for+ helper to display the error messages of a model assigned to a view template. It is very similar to the previous example and will achieve exactly the same result.
856
-
857
- <erb>
858
- <%= error_messages_for :product %>
859
- </erb>
860
-
861
- The displayed text for each error message will always be formed by the capitalized name of the attribute that holds the error, followed by the error message itself.
862
-
863
- Both the +form.error_messages+ and the +error_messages_for+ helpers accept options that let you customize the +div+ element that holds the messages, change the header text, change the message below the header, and specify the tag used for the header element. For example,
864
-
865
- <erb>
866
- <%= f.error_messages :header_message => "Invalid product!",
867
- :message => "You'll need to fix the following fields:",
868
- :header_tag => :h3 %>
869
- </erb>
870
-
871
- results in:
872
-
873
- !images/customized_error_messages.png(Customized error messages)!
874
-
875
- If you pass +nil+ in any of these options, the corresponding section of the +div+ will be discarded.
876
-
877
- h4(#customizing-error-messages-css). Customizing the Error Messages CSS
878
-
879
- The selectors used to customize the style of error messages are:
880
-
881
- * +.field_with_errors+ - Style for the form fields and labels with errors.
882
- * +#error_explanation+ - Style for the +div+ element with the error messages.
883
- * +#error_explanation h2+ - Style for the header of the +div+ element.
884
- * +#error_explanation p+ - Style for the paragraph holding the message that appears right below the header of the +div+ element.
885
- * +#error_explanation ul li+ - Style for the list items with individual error messages.
886
-
887
- If scaffolding was used, file +app/assets/stylesheets/scaffolds.css.scss+ will have been generated automatically. This file defines the red-based styles you saw in the examples above.
888
-
889
- The name of the class and the id can be changed with the +:class+ and +:id+ options, accepted by both helpers.
890
-
891
- h4. Customizing the Error Messages HTML
892
-
893
- By default, form fields with errors are displayed enclosed by a +div+ element with the +field_with_errors+ CSS class. However, it's possible to override that.
894
-
895
- The way form fields with errors are treated is defined by +ActionView::Base.field_error_proc+. This is a +Proc+ that receives two parameters:
896
-
897
- * A string with the HTML tag
898
- * An instance of +ActionView::Helpers::InstanceTag+.
899
-
900
- Below is a simple example where we change the Rails behavior to always display the error messages in front of each of the form fields in error. The error messages will be enclosed by a +span+ element with a +validation-error+ CSS class. There will be no +div+ element enclosing the +input+ element, so we get rid of that red border around the text field. You can use the +validation-error+ CSS class to style it anyway you want.
901
-
902
- <ruby>
903
- ActionView::Base.field_error_proc = Proc.new do |html_tag, instance|
904
- errors = Array(instance.error_message).join(',')
905
- %(#{html_tag}<span class="validation-error">&nbsp;#{errors}</span>).html_safe
906
- end
907
- </ruby>
908
-
909
- The result looks like the following:
910
-
911
- !images/validation_error_messages.png(Validation error messages)!
912
-
913
- h3. Callbacks Overview
914
-
915
- Callbacks are methods that get called at certain moments of an object's life cycle. With callbacks it is possible to write code that will run whenever an Active Record object is created, saved, updated, deleted, validated, or loaded from the database.
916
-
917
- h4. Callback Registration
918
-
919
- In order to use the available callbacks, you need to register them. You can implement the callbacks as ordinary methods and use a macro-style class method to register them as callbacks:
920
-
921
- <ruby>
922
- class User < ActiveRecord::Base
923
- validates :login, :email, :presence => true
924
-
925
- before_validation :ensure_login_has_a_value
926
-
927
- protected
928
- def ensure_login_has_a_value
929
- if login.nil?
930
- self.login = email unless email.blank?
931
- end
932
- end
933
- end
934
- </ruby>
935
-
936
- The macro-style class methods can also receive a block. Consider using this style if the code inside your block is so short that it fits in a single line:
937
-
938
- <ruby>
939
- class User < ActiveRecord::Base
940
- validates :login, :email, :presence => true
941
-
942
- before_create do |user|
943
- user.name = user.login.capitalize if user.name.blank?
944
- end
945
- end
946
- </ruby>
947
-
948
- It is considered good practice to declare callback methods as protected or private. If left public, they can be called from outside of the model and violate the principle of object encapsulation.
949
-
950
- h3. Available Callbacks
951
-
952
- Here is a list with all the available Active Record callbacks, listed in the same order in which they will get called during the respective operations:
953
-
954
- h4. Creating an Object
955
-
956
- * +before_validation+
957
- * +after_validation+
958
- * +before_save+
959
- * +around_save+
960
- * +before_create+
961
- * +around_create+
962
- * +after_create+
963
- * +after_save+
964
-
965
- h4. Updating an Object
966
-
967
- * +before_validation+
968
- * +after_validation+
969
- * +before_save+
970
- * +around_save+
971
- * +before_update+
972
- * +around_update+
973
- * +after_update+
974
- * +after_save+
975
-
976
- h4. Destroying an Object
977
-
978
- * +before_destroy+
979
- * +around_destroy+
980
- * +after_destroy+
981
-
982
- WARNING. +after_save+ runs both on create and update, but always _after_ the more specific callbacks +after_create+ and +after_update+, no matter the order in which the macro calls were executed.
983
-
984
- h4. +after_initialize+ and +after_find+
985
-
986
- The +after_initialize+ callback will be called whenever an Active Record object is instantiated, either by directly using +new+ or when a record is loaded from the database. It can be useful to avoid the need to directly override your Active Record +initialize+ method.
987
-
988
- The +after_find+ callback will be called whenever Active Record loads a record from the database. +after_find+ is called before +after_initialize+ if both are defined.
989
-
990
- The +after_initialize+ and +after_find+ callbacks have no +before_*+ counterparts, but they can be registered just like the other Active Record callbacks.
991
-
992
- <ruby>
993
- class User < ActiveRecord::Base
994
- after_initialize do |user|
995
- puts "You have initialized an object!"
996
- end
997
-
998
- after_find do |user|
999
- puts "You have found an object!"
1000
- end
1001
- end
1002
-
1003
- >> User.new
1004
- You have initialized an object!
1005
- => #<User id: nil>
1006
-
1007
- >> User.first
1008
- You have found an object!
1009
- You have initialized an object!
1010
- => #<User id: 1>
1011
- </ruby>
1012
-
1013
- h3. Running Callbacks
1014
-
1015
- The following methods trigger callbacks:
1016
-
1017
- * +create+
1018
- * +create!+
1019
- * +decrement!+
1020
- * +destroy+
1021
- * +destroy_all+
1022
- * +increment!+
1023
- * +save+
1024
- * +save!+
1025
- * +save(:validate => false)+
1026
- * +toggle!+
1027
- * +update+
1028
- * +update_attribute+
1029
- * +update_attributes+
1030
- * +update_attributes!+
1031
- * +valid?+
1032
-
1033
- Additionally, the +after_find+ callback is triggered by the following finder methods:
1034
-
1035
- * +all+
1036
- * +first+
1037
- * +find+
1038
- * +find_all_by_<em>attribute</em>+
1039
- * +find_by_<em>attribute</em>+
1040
- * +find_by_<em>attribute</em>!+
1041
- * +last+
1042
-
1043
- The +after_initialize+ callback is triggered every time a new object of the class is initialized.
1044
-
1045
- h3. Skipping Callbacks
1046
-
1047
- Just as with validations, it is also possible to skip callbacks. These methods should be used with caution, however, because important business rules and application logic may be kept in callbacks. Bypassing them without understanding the potential implications may lead to invalid data.
1048
-
1049
- * +decrement+
1050
- * +decrement_counter+
1051
- * +delete+
1052
- * +delete_all+
1053
- * +find_by_sql+
1054
- * +increment+
1055
- * +increment_counter+
1056
- * +toggle+
1057
- * +touch+
1058
- * +update_column+
1059
- * +update_all+
1060
- * +update_counters+
1061
-
1062
- h3. Halting Execution
1063
-
1064
- As you start registering new callbacks for your models, they will be queued for execution. This queue will include all your model's validations, the registered callbacks, and the database operation to be executed.
1065
-
1066
- The whole callback chain is wrapped in a transaction. If any <em>before</em> callback method returns exactly +false+ or raises an exception, the execution chain gets halted and a ROLLBACK is issued; <em>after</em> callbacks can only accomplish that by raising an exception.
1067
-
1068
- WARNING. Raising an arbitrary exception may break code that expects +save+ and its friends not to fail like that. The +ActiveRecord::Rollback+ exception is thought precisely to tell Active Record a rollback is going on. That one is internally captured but not reraised.
1069
-
1070
- h3. Relational Callbacks
1071
-
1072
- Callbacks work through model relationships, and can even be defined by them. Suppose an example where a user has many posts. A user's posts should be destroyed if the user is destroyed. Let's add an +after_destroy+ callback to the +User+ model by way of its relationship to the +Post+ model:
1073
-
1074
- <ruby>
1075
- class User < ActiveRecord::Base
1076
- has_many :posts, :dependent => :destroy
1077
- end
1078
-
1079
- class Post < ActiveRecord::Base
1080
- after_destroy :log_destroy_action
1081
-
1082
- def log_destroy_action
1083
- puts 'Post destroyed'
1084
- end
1085
- end
1086
-
1087
- >> user = User.first
1088
- => #<User id: 1>
1089
- >> user.posts.create!
1090
- => #<Post id: 1, user_id: 1>
1091
- >> user.destroy
1092
- Post destroyed
1093
- => #<User id: 1>
1094
- </ruby>
1095
-
1096
- h3. Conditional Callbacks
1097
-
1098
- As with validations, we can also make the calling of a callback method conditional on the satisfaction of a given predicate. We can do this using the +:if+ and +:unless+ options, which can take a symbol, a string or a +Proc+. You may use the +:if+ option when you want to specify under which conditions the callback *should* be called. If you want to specify the conditions under which the callback *should not* be called, then you may use the +:unless+ option.
1099
-
1100
- h4. Using +:if+ and +:unless+ with a +Symbol+
1101
-
1102
- You can associate the +:if+ and +:unless+ options with a symbol corresponding to the name of a predicate method that will get called right before the callback. When using the +:if+ option, the callback won't be executed if the predicate method returns false; when using the +:unless+ option, the callback won't be executed if the predicate method returns true. This is the most common option. Using this form of registration it is also possible to register several different predicates that should be called to check if the callback should be executed.
1103
-
1104
- <ruby>
1105
- class Order < ActiveRecord::Base
1106
- before_save :normalize_card_number, :if => :paid_with_card?
1107
- end
1108
- </ruby>
1109
-
1110
- h4. Using +:if+ and +:unless+ with a String
1111
-
1112
- You can also use a string that will be evaluated using +eval+ and hence needs to contain valid Ruby code. You should use this option only when the string represents a really short condition:
1113
-
1114
- <ruby>
1115
- class Order < ActiveRecord::Base
1116
- before_save :normalize_card_number, :if => "paid_with_card?"
1117
- end
1118
- </ruby>
1119
-
1120
- h4. Using +:if+ and +:unless+ with a +Proc+
1121
-
1122
- Finally, it is possible to associate +:if+ and +:unless+ with a +Proc+ object. This option is best suited when writing short validation methods, usually one-liners:
1123
-
1124
- <ruby>
1125
- class Order < ActiveRecord::Base
1126
- before_save :normalize_card_number,
1127
- :if => Proc.new { |order| order.paid_with_card? }
1128
- end
1129
- </ruby>
1130
-
1131
- h4. Multiple Conditions for Callbacks
1132
-
1133
- When writing conditional callbacks, it is possible to mix both +:if+ and +:unless+ in the same callback declaration:
1134
-
1135
- <ruby>
1136
- class Comment < ActiveRecord::Base
1137
- after_create :send_email_to_author, :if => :author_wants_emails?,
1138
- :unless => Proc.new { |comment| comment.post.ignore_comments? }
1139
- end
1140
- </ruby>
1141
-
1142
- h3. Callback Classes
1143
-
1144
- Sometimes the callback methods that you'll write will be useful enough to be reused by other models. Active Record makes it possible to create classes that encapsulate the callback methods, so it becomes very easy to reuse them.
1145
-
1146
- Here's an example where we create a class with an +after_destroy+ callback for a +PictureFile+ model:
1147
-
1148
- <ruby>
1149
- class PictureFileCallbacks
1150
- def after_destroy(picture_file)
1151
- if File.exists?(picture_file.filepath)
1152
- File.delete(picture_file.filepath)
1153
- end
1154
- end
1155
- end
1156
- </ruby>
1157
-
1158
- When declared inside a class, as above, the callback methods will receive the model object as a parameter. We can now use the callback class in the model:
1159
-
1160
- <ruby>
1161
- class PictureFile < ActiveRecord::Base
1162
- after_destroy PictureFileCallbacks.new
1163
- end
1164
- </ruby>
1165
-
1166
- Note that we needed to instantiate a new +PictureFileCallbacks+ object, since we declared our callback as an instance method. This is particularly useful if the callbacks make use of the state of the instantiated object. Often, however, it will make more sense to declare the callbacks as class methods:
1167
-
1168
- <ruby>
1169
- class PictureFileCallbacks
1170
- def self.after_destroy(picture_file)
1171
- if File.exists?(picture_file.filepath)
1172
- File.delete(picture_file.filepath)
1173
- end
1174
- end
1175
- end
1176
- </ruby>
1177
-
1178
- If the callback method is declared this way, it won't be necessary to instantiate a +PictureFileCallbacks+ object.
1179
-
1180
- <ruby>
1181
- class PictureFile < ActiveRecord::Base
1182
- after_destroy PictureFileCallbacks
1183
- end
1184
- </ruby>
1185
-
1186
- You can declare as many callbacks as you want inside your callback classes.
1187
-
1188
- h3. Observers
1189
-
1190
- Observers are similar to callbacks, but with important differences. Whereas callbacks can pollute a model with code that isn't directly related to its purpose, observers allow you to add the same functionality without changing the code of the model. For example, it could be argued that a +User+ model should not include code to send registration confirmation emails. Whenever you use callbacks with code that isn't directly related to your model, you may want to consider creating an observer instead.
1191
-
1192
- h4. Creating Observers
1193
-
1194
- For example, imagine a +User+ model where we want to send an email every time a new user is created. Because sending emails is not directly related to our model's purpose, we should create an observer to contain the code implementing this functionality.
1195
-
1196
- <shell>
1197
- $ rails generate observer User
1198
- </shell>
1199
-
1200
- generates +app/models/user_observer.rb+ containing the observer class +UserObserver+:
1201
-
1202
- <ruby>
1203
- class UserObserver < ActiveRecord::Observer
1204
- end
1205
- </ruby>
1206
-
1207
- You may now add methods to be called at the desired occasions:
1208
-
1209
- <ruby>
1210
- class UserObserver < ActiveRecord::Observer
1211
- def after_create(model)
1212
- # code to send confirmation email...
1213
- end
1214
- end
1215
- </ruby>
1216
-
1217
- As with callback classes, the observer's methods receive the observed model as a parameter.
1218
-
1219
- h4. Registering Observers
1220
-
1221
- Observers are conventionally placed inside of your +app/models+ directory and registered in your application's +config/application.rb+ file. For example, the +UserObserver+ above would be saved as +app/models/user_observer.rb+ and registered in +config/application.rb+ this way:
1222
-
1223
- <ruby>
1224
- # Activate observers that should always be running.
1225
- config.active_record.observers = :user_observer
1226
- </ruby>
1227
-
1228
- As usual, settings in +config/environments+ take precedence over those in +config/application.rb+. So, if you prefer that an observer doesn't run in all environments, you can simply register it in a specific environment instead.
1229
-
1230
- h4. Sharing Observers
1231
-
1232
- By default, Rails will simply strip "Observer" from an observer's name to find the model it should observe. However, observers can also be used to add behavior to more than one model, and thus it is possible to explicitly specify the models that our observer should observe:
1233
-
1234
- <ruby>
1235
- class MailerObserver < ActiveRecord::Observer
1236
- observe :registration, :user
1237
-
1238
- def after_create(model)
1239
- # code to send confirmation email...
1240
- end
1241
- end
1242
- </ruby>
1243
-
1244
- In this example, the +after_create+ method will be called whenever a +Registration+ or +User+ is created. Note that this new +MailerObserver+ would also need to be registered in +config/application.rb+ in order to take effect:
1245
-
1246
- <ruby>
1247
- # Activate observers that should always be running.
1248
- config.active_record.observers = :mailer_observer
1249
- </ruby>
1250
-
1251
- h3. Transaction Callbacks
1252
-
1253
- There are two additional callbacks that are triggered by the completion of a database transaction: +after_commit+ and +after_rollback+. These callbacks are very similar to the +after_save+ callback except that they don't execute until after database changes have either been committed or rolled back. They are most useful when your active record models need to interact with external systems which are not part of the database transaction.
1254
-
1255
- Consider, for example, the previous example where the +PictureFile+ model needs to delete a file after the corresponding record is destroyed. If anything raises an exception after the +after_destroy+ callback is called and the transaction rolls back, the file will have been deleted and the model will be left in an inconsistent state. For example, suppose that +picture_file_2+ in the code below is not valid and the +save!+ method raises an error.
1256
-
1257
- <ruby>
1258
- PictureFile.transaction do
1259
- picture_file_1.destroy
1260
- picture_file_2.save!
1261
- end
1262
- </ruby>
1263
-
1264
- By using the +after_commit+ callback we can account for this case.
1265
-
1266
- <ruby>
1267
- class PictureFile < ActiveRecord::Base
1268
- attr_accessor :delete_file
1269
-
1270
- after_destroy do |picture_file|
1271
- picture_file.delete_file = picture_file.filepath
1272
- end
1273
-
1274
- after_commit do |picture_file|
1275
- if picture_file.delete_file && File.exist?(picture_file.delete_file)
1276
- File.delete(picture_file.delete_file)
1277
- picture_file.delete_file = nil
1278
- end
1279
- end
1280
- end
1281
- </ruby>
1282
-
1283
- The +after_commit+ and +after_rollback+ callbacks are guaranteed to be called for all models created, updated, or destroyed within a transaction block. If any exceptions are raised within one of these callbacks, they will be ignored so that they don't interfere with the other callbacks. As such, if your callback code could raise an exception, you'll need to rescue it and handle it appropriately within the callback.