rails 3.2.22.5 → 4.0.0.beta1

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

Potentially problematic release.


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

Files changed (281) hide show
  1. checksums.yaml +4 -4
  2. data/README.rdoc +77 -0
  3. data/guides/CHANGELOG.md +9 -0
  4. data/guides/Rakefile +77 -0
  5. data/guides/assets/images/belongs_to.png +0 -0
  6. data/guides/assets/images/book_icon.gif +0 -0
  7. data/guides/assets/images/bullet.gif +0 -0
  8. data/guides/assets/images/challenge.png +0 -0
  9. data/guides/assets/images/chapters_icon.gif +0 -0
  10. data/guides/assets/images/check_bullet.gif +0 -0
  11. data/guides/assets/images/credits_pic_blank.gif +0 -0
  12. data/guides/assets/images/csrf.png +0 -0
  13. data/guides/assets/images/edge_badge.png +0 -0
  14. data/guides/assets/images/favicon.ico +0 -0
  15. data/guides/assets/images/feature_tile.gif +0 -0
  16. data/guides/assets/images/footer_tile.gif +0 -0
  17. data/guides/assets/images/fxn.png +0 -0
  18. data/guides/assets/images/getting_started/confirm_dialog.png +0 -0
  19. data/guides/assets/images/getting_started/forbidden_attributes_for_new_post.png +0 -0
  20. data/guides/assets/images/getting_started/form_with_errors.png +0 -0
  21. data/guides/assets/images/getting_started/index_action_with_edit_link.png +0 -0
  22. data/guides/assets/images/getting_started/new_post.png +0 -0
  23. data/guides/assets/images/getting_started/post_with_comments.png +0 -0
  24. data/guides/assets/images/getting_started/routing_error_no_controller.png +0 -0
  25. data/guides/assets/images/getting_started/routing_error_no_route_matches.png +0 -0
  26. data/guides/assets/images/getting_started/show_action_for_posts.png +0 -0
  27. data/guides/assets/images/getting_started/template_is_missing_posts_new.png +0 -0
  28. data/guides/assets/images/getting_started/undefined_method_post_path.png +0 -0
  29. data/guides/assets/images/getting_started/unknown_action_create_for_posts.png +0 -0
  30. data/guides/assets/images/getting_started/unknown_action_new_for_posts.png +0 -0
  31. data/guides/assets/images/grey_bullet.gif +0 -0
  32. data/guides/assets/images/habtm.png +0 -0
  33. data/guides/assets/images/has_many.png +0 -0
  34. data/guides/assets/images/has_many_through.png +0 -0
  35. data/guides/assets/images/has_one.png +0 -0
  36. data/guides/assets/images/has_one_through.png +0 -0
  37. data/guides/assets/images/header_backdrop.png +0 -0
  38. data/guides/assets/images/header_tile.gif +0 -0
  39. data/guides/assets/images/i18n/demo_html_safe.png +0 -0
  40. data/guides/assets/images/i18n/demo_localized_pirate.png +0 -0
  41. data/guides/assets/images/i18n/demo_translated_en.png +0 -0
  42. data/guides/assets/images/i18n/demo_translated_pirate.png +0 -0
  43. data/guides/assets/images/i18n/demo_translation_missing.png +0 -0
  44. data/guides/assets/images/i18n/demo_untranslated.png +0 -0
  45. data/guides/assets/images/icons/README +5 -0
  46. data/guides/assets/images/icons/callouts/1.png +0 -0
  47. data/guides/assets/images/icons/callouts/10.png +0 -0
  48. data/guides/assets/images/icons/callouts/11.png +0 -0
  49. data/guides/assets/images/icons/callouts/12.png +0 -0
  50. data/guides/assets/images/icons/callouts/13.png +0 -0
  51. data/guides/assets/images/icons/callouts/14.png +0 -0
  52. data/guides/assets/images/icons/callouts/15.png +0 -0
  53. data/guides/assets/images/icons/callouts/2.png +0 -0
  54. data/guides/assets/images/icons/callouts/3.png +0 -0
  55. data/guides/assets/images/icons/callouts/4.png +0 -0
  56. data/guides/assets/images/icons/callouts/5.png +0 -0
  57. data/guides/assets/images/icons/callouts/6.png +0 -0
  58. data/guides/assets/images/icons/callouts/7.png +0 -0
  59. data/guides/assets/images/icons/callouts/8.png +0 -0
  60. data/guides/assets/images/icons/callouts/9.png +0 -0
  61. data/guides/assets/images/icons/caution.png +0 -0
  62. data/guides/assets/images/icons/example.png +0 -0
  63. data/guides/assets/images/icons/home.png +0 -0
  64. data/guides/assets/images/icons/important.png +0 -0
  65. data/guides/assets/images/icons/next.png +0 -0
  66. data/guides/assets/images/icons/note.png +0 -0
  67. data/guides/assets/images/icons/prev.png +0 -0
  68. data/guides/assets/images/icons/tip.png +0 -0
  69. data/guides/assets/images/icons/up.png +0 -0
  70. data/guides/assets/images/icons/warning.png +0 -0
  71. data/guides/assets/images/jaimeiniesta.jpg +0 -0
  72. data/guides/assets/images/nav_arrow.gif +0 -0
  73. data/guides/assets/images/oscardelben.jpg +0 -0
  74. data/guides/assets/images/polymorphic.png +0 -0
  75. data/guides/assets/images/radar.png +0 -0
  76. data/guides/assets/images/rails4_features.png +0 -0
  77. data/guides/assets/images/rails_guides_kindle_cover.jpg +0 -0
  78. data/guides/assets/images/rails_guides_logo.gif +0 -0
  79. data/guides/assets/images/rails_logo_remix.gif +0 -0
  80. data/guides/assets/images/rails_welcome.png +0 -0
  81. data/guides/assets/images/session_fixation.png +0 -0
  82. data/guides/assets/images/tab_grey.gif +0 -0
  83. data/guides/assets/images/tab_info.gif +0 -0
  84. data/guides/assets/images/tab_note.gif +0 -0
  85. data/guides/assets/images/tab_red.gif +0 -0
  86. data/guides/assets/images/tab_yellow.gif +0 -0
  87. data/guides/assets/images/tab_yellow.png +0 -0
  88. data/guides/assets/images/vijaydev.jpg +0 -0
  89. data/guides/assets/javascripts/guides.js +57 -0
  90. data/guides/assets/javascripts/jquery.min.js +4 -0
  91. data/guides/assets/javascripts/responsive-tables.js +43 -0
  92. data/guides/assets/javascripts/syntaxhighlighter/shBrushAS3.js +59 -0
  93. data/guides/assets/javascripts/syntaxhighlighter/shBrushAppleScript.js +75 -0
  94. data/guides/assets/javascripts/syntaxhighlighter/shBrushBash.js +59 -0
  95. data/guides/assets/javascripts/syntaxhighlighter/shBrushCSharp.js +65 -0
  96. data/guides/assets/javascripts/syntaxhighlighter/shBrushColdFusion.js +100 -0
  97. data/guides/assets/javascripts/syntaxhighlighter/shBrushCpp.js +97 -0
  98. data/guides/assets/javascripts/syntaxhighlighter/shBrushCss.js +91 -0
  99. data/guides/assets/javascripts/syntaxhighlighter/shBrushDelphi.js +55 -0
  100. data/guides/assets/javascripts/syntaxhighlighter/shBrushDiff.js +41 -0
  101. data/guides/assets/javascripts/syntaxhighlighter/shBrushErlang.js +52 -0
  102. data/guides/assets/javascripts/syntaxhighlighter/shBrushGroovy.js +67 -0
  103. data/guides/assets/javascripts/syntaxhighlighter/shBrushJScript.js +52 -0
  104. data/guides/assets/javascripts/syntaxhighlighter/shBrushJava.js +57 -0
  105. data/guides/assets/javascripts/syntaxhighlighter/shBrushJavaFX.js +58 -0
  106. data/guides/assets/javascripts/syntaxhighlighter/shBrushPerl.js +72 -0
  107. data/guides/assets/javascripts/syntaxhighlighter/shBrushPhp.js +88 -0
  108. data/guides/assets/javascripts/syntaxhighlighter/shBrushPlain.js +33 -0
  109. data/guides/assets/javascripts/syntaxhighlighter/shBrushPowerShell.js +74 -0
  110. data/guides/assets/javascripts/syntaxhighlighter/shBrushPython.js +64 -0
  111. data/guides/assets/javascripts/syntaxhighlighter/shBrushRuby.js +55 -0
  112. data/guides/assets/javascripts/syntaxhighlighter/shBrushSass.js +94 -0
  113. data/guides/assets/javascripts/syntaxhighlighter/shBrushScala.js +51 -0
  114. data/guides/assets/javascripts/syntaxhighlighter/shBrushSql.js +66 -0
  115. data/guides/assets/javascripts/syntaxhighlighter/shBrushVb.js +56 -0
  116. data/guides/assets/javascripts/syntaxhighlighter/shBrushXml.js +69 -0
  117. data/guides/assets/javascripts/syntaxhighlighter/shCore.js +17 -0
  118. data/guides/assets/stylesheets/fixes.css +16 -0
  119. data/guides/assets/stylesheets/kindle.css +11 -0
  120. data/guides/assets/stylesheets/main.css +709 -0
  121. data/guides/assets/stylesheets/print.css +52 -0
  122. data/guides/assets/stylesheets/reset.css +43 -0
  123. data/guides/assets/stylesheets/responsive-tables.css +50 -0
  124. data/guides/assets/stylesheets/style.css +13 -0
  125. data/guides/assets/stylesheets/syntaxhighlighter/shCore.css +226 -0
  126. data/guides/assets/stylesheets/syntaxhighlighter/shCoreDefault.css +328 -0
  127. data/guides/assets/stylesheets/syntaxhighlighter/shCoreDjango.css +331 -0
  128. data/guides/assets/stylesheets/syntaxhighlighter/shCoreEclipse.css +339 -0
  129. data/guides/assets/stylesheets/syntaxhighlighter/shCoreEmacs.css +324 -0
  130. data/guides/assets/stylesheets/syntaxhighlighter/shCoreFadeToGrey.css +328 -0
  131. data/guides/assets/stylesheets/syntaxhighlighter/shCoreMDUltra.css +324 -0
  132. data/guides/assets/stylesheets/syntaxhighlighter/shCoreMidnight.css +324 -0
  133. data/guides/assets/stylesheets/syntaxhighlighter/shCoreRDark.css +324 -0
  134. data/guides/assets/stylesheets/syntaxhighlighter/shThemeDefault.css +117 -0
  135. data/guides/assets/stylesheets/syntaxhighlighter/shThemeDjango.css +120 -0
  136. data/guides/assets/stylesheets/syntaxhighlighter/shThemeEclipse.css +128 -0
  137. data/guides/assets/stylesheets/syntaxhighlighter/shThemeEmacs.css +113 -0
  138. data/guides/assets/stylesheets/syntaxhighlighter/shThemeFadeToGrey.css +117 -0
  139. data/guides/assets/stylesheets/syntaxhighlighter/shThemeMDUltra.css +113 -0
  140. data/guides/assets/stylesheets/syntaxhighlighter/shThemeMidnight.css +113 -0
  141. data/guides/assets/stylesheets/syntaxhighlighter/shThemeRDark.css +113 -0
  142. data/guides/assets/stylesheets/syntaxhighlighter/shThemeRailsGuides.css +116 -0
  143. data/guides/code/getting_started/Gemfile +38 -0
  144. data/guides/code/getting_started/Gemfile.lock +150 -0
  145. data/guides/code/getting_started/README.rdoc +28 -0
  146. data/guides/code/getting_started/Rakefile +6 -0
  147. data/guides/code/getting_started/app/assets/images/rails.png +0 -0
  148. data/guides/code/getting_started/app/assets/javascripts/application.js +16 -0
  149. data/guides/code/getting_started/app/assets/javascripts/comments.js.coffee +3 -0
  150. data/guides/code/getting_started/app/assets/javascripts/posts.js.coffee +3 -0
  151. data/guides/code/getting_started/app/assets/javascripts/welcome.js.coffee +3 -0
  152. data/guides/code/getting_started/app/assets/stylesheets/application.css +13 -0
  153. data/guides/code/getting_started/app/assets/stylesheets/comments.css.scss +3 -0
  154. data/guides/code/getting_started/app/assets/stylesheets/posts.css.scss +3 -0
  155. data/guides/code/getting_started/app/assets/stylesheets/welcome.css.scss +3 -0
  156. data/guides/code/getting_started/app/controllers/application_controller.rb +5 -0
  157. data/guides/code/getting_started/app/controllers/comments_controller.rb +17 -0
  158. data/guides/code/getting_started/app/controllers/posts_controller.rb +47 -0
  159. data/guides/code/getting_started/app/controllers/welcome_controller.rb +4 -0
  160. data/guides/code/getting_started/app/helpers/application_helper.rb +2 -0
  161. data/guides/code/getting_started/app/helpers/comments_helper.rb +2 -0
  162. data/guides/code/getting_started/app/helpers/posts_helper.rb +2 -0
  163. data/guides/code/getting_started/app/helpers/welcome_helper.rb +2 -0
  164. data/guides/code/getting_started/app/models/comment.rb +3 -0
  165. data/guides/code/getting_started/app/models/post.rb +7 -0
  166. data/guides/code/getting_started/app/views/comments/_comment.html.erb +15 -0
  167. data/guides/code/getting_started/app/views/comments/_form.html.erb +13 -0
  168. data/guides/code/getting_started/app/views/layouts/application.html.erb +14 -0
  169. data/guides/code/getting_started/app/views/posts/_form.html.erb +27 -0
  170. data/guides/code/getting_started/app/views/posts/edit.html.erb +5 -0
  171. data/guides/code/getting_started/app/views/posts/index.html.erb +21 -0
  172. data/guides/code/getting_started/app/views/posts/new.html.erb +5 -0
  173. data/guides/code/getting_started/app/views/posts/show.html.erb +18 -0
  174. data/guides/code/getting_started/app/views/welcome/index.html.erb +3 -0
  175. data/guides/code/getting_started/bin/bundle +4 -0
  176. data/guides/code/getting_started/bin/rails +4 -0
  177. data/guides/code/getting_started/bin/rake +4 -0
  178. data/guides/code/getting_started/config.ru +4 -0
  179. data/guides/code/getting_started/config/application.rb +17 -0
  180. data/guides/code/getting_started/config/boot.rb +4 -0
  181. data/guides/code/getting_started/config/database.yml +25 -0
  182. data/guides/code/getting_started/config/environment.rb +5 -0
  183. data/guides/code/getting_started/config/environments/development.rb +30 -0
  184. data/guides/code/getting_started/config/environments/production.rb +80 -0
  185. data/guides/code/getting_started/config/environments/test.rb +36 -0
  186. data/guides/code/getting_started/config/initializers/backtrace_silencers.rb +7 -0
  187. data/guides/code/getting_started/config/initializers/filter_parameter_logging.rb +4 -0
  188. data/guides/code/getting_started/config/initializers/inflections.rb +16 -0
  189. data/guides/code/getting_started/config/initializers/locale.rb +9 -0
  190. data/guides/code/getting_started/config/initializers/mime_types.rb +5 -0
  191. data/guides/code/getting_started/config/initializers/secret_token.rb +12 -0
  192. data/guides/code/getting_started/config/initializers/session_store.rb +3 -0
  193. data/guides/code/getting_started/config/initializers/wrap_parameters.rb +14 -0
  194. data/guides/code/getting_started/config/locales/en.yml +23 -0
  195. data/guides/code/getting_started/config/routes.rb +7 -0
  196. data/guides/code/getting_started/db/migrate/20130122042648_create_posts.rb +10 -0
  197. data/guides/code/getting_started/db/migrate/20130122045842_create_comments.rb +11 -0
  198. data/guides/code/getting_started/db/schema.rb +33 -0
  199. data/guides/code/getting_started/db/seeds.rb +7 -0
  200. data/guides/code/getting_started/public/404.html +27 -0
  201. data/guides/code/getting_started/public/422.html +26 -0
  202. data/guides/code/getting_started/public/500.html +26 -0
  203. data/guides/code/getting_started/public/favicon.ico +0 -0
  204. data/guides/code/getting_started/public/robots.txt +5 -0
  205. data/guides/code/getting_started/test/controllers/comments_controller_test.rb +7 -0
  206. data/guides/code/getting_started/test/controllers/posts_controller_test.rb +7 -0
  207. data/guides/code/getting_started/test/controllers/welcome_controller_test.rb +9 -0
  208. data/guides/code/getting_started/test/fixtures/comments.yml +11 -0
  209. data/guides/code/getting_started/test/fixtures/posts.yml +9 -0
  210. data/guides/code/getting_started/test/helpers/comments_helper_test.rb +4 -0
  211. data/guides/code/getting_started/test/helpers/posts_helper_test.rb +4 -0
  212. data/guides/code/getting_started/test/helpers/welcome_helper_test.rb +4 -0
  213. data/guides/code/getting_started/test/models/comment_test.rb +7 -0
  214. data/guides/code/getting_started/test/models/post_test.rb +7 -0
  215. data/guides/code/getting_started/test/test_helper.rb +15 -0
  216. data/guides/rails_guides.rb +44 -0
  217. data/guides/rails_guides/generator.rb +248 -0
  218. data/guides/rails_guides/helpers.rb +51 -0
  219. data/guides/rails_guides/indexer.rb +68 -0
  220. data/guides/rails_guides/kindle.rb +119 -0
  221. data/guides/rails_guides/levenshtein.rb +31 -0
  222. data/guides/rails_guides/markdown.rb +163 -0
  223. data/guides/rails_guides/markdown/renderer.rb +82 -0
  224. data/guides/source/2_2_release_notes.md +435 -0
  225. data/guides/source/2_3_release_notes.md +621 -0
  226. data/guides/source/3_0_release_notes.md +614 -0
  227. data/guides/source/3_1_release_notes.md +556 -0
  228. data/guides/source/3_2_release_notes.md +565 -0
  229. data/guides/source/4_0_release_notes.md +228 -0
  230. data/guides/source/_license.html.erb +2 -0
  231. data/guides/source/_welcome.html.erb +19 -0
  232. data/guides/source/action_controller_overview.md +872 -0
  233. data/guides/source/action_mailer_basics.md +599 -0
  234. data/guides/source/action_view_overview.md +1565 -0
  235. data/guides/source/active_model_basics.md +200 -0
  236. data/guides/source/active_record_basics.md +370 -0
  237. data/guides/source/active_record_callbacks.md +358 -0
  238. data/guides/source/active_record_querying.md +1621 -0
  239. data/guides/source/active_record_validations.md +1128 -0
  240. data/guides/source/active_support_core_extensions.md +3791 -0
  241. data/guides/source/active_support_instrumentation.md +487 -0
  242. data/guides/source/api_documentation_guidelines.md +209 -0
  243. data/guides/source/asset_pipeline.md +832 -0
  244. data/guides/source/association_basics.md +2129 -0
  245. data/guides/source/caching_with_rails.md +350 -0
  246. data/guides/source/command_line.md +594 -0
  247. data/guides/source/configuring.md +736 -0
  248. data/guides/source/contributing_to_ruby_on_rails.md +455 -0
  249. data/guides/source/credits.html.erb +76 -0
  250. data/guides/source/debugging_rails_applications.md +675 -0
  251. data/guides/source/development_dependencies_install.md +195 -0
  252. data/guides/source/documents.yaml +179 -0
  253. data/guides/source/engines.md +961 -0
  254. data/guides/source/form_helpers.md +955 -0
  255. data/guides/source/generators.md +644 -0
  256. data/guides/source/getting_started.md +1775 -0
  257. data/guides/source/i18n.md +983 -0
  258. data/guides/source/index.html.erb +27 -0
  259. data/guides/source/initialization.md +562 -0
  260. data/guides/source/kindle/KINDLE.md +26 -0
  261. data/guides/source/kindle/copyright.html.erb +1 -0
  262. data/guides/source/kindle/layout.html.erb +27 -0
  263. data/guides/source/kindle/rails_guides.opf.erb +52 -0
  264. data/guides/source/kindle/toc.html.erb +24 -0
  265. data/guides/source/kindle/toc.ncx.erb +64 -0
  266. data/guides/source/kindle/welcome.html.erb +5 -0
  267. data/guides/source/layout.html.erb +148 -0
  268. data/guides/source/layouts_and_rendering.md +1132 -0
  269. data/guides/source/migrations.md +1059 -0
  270. data/guides/source/nested_model_forms.md +225 -0
  271. data/guides/source/plugins.md +435 -0
  272. data/guides/source/rails_application_templates.md +229 -0
  273. data/guides/source/rails_on_rack.md +342 -0
  274. data/guides/source/routing.md +1088 -0
  275. data/guides/source/ruby_on_rails_guides_guidelines.md +124 -0
  276. data/guides/source/security.md +973 -0
  277. data/guides/source/testing.md +981 -0
  278. data/guides/source/upgrading_ruby_on_rails.md +286 -0
  279. data/guides/source/working_with_javascript_in_rails.md +396 -0
  280. data/guides/w3c_validator.rb +95 -0
  281. metadata +315 -31
@@ -0,0 +1,225 @@
1
+ Rails nested model forms
2
+ ========================
3
+
4
+ Creating a form for a model _and_ its associations can become quite tedious. Therefore Rails provides helpers to assist in dealing with the complexities of generating these forms _and_ the required CRUD operations to create, update, and destroy associations.
5
+
6
+ After reading this guide, you will know:
7
+
8
+ * do stuff.
9
+
10
+ --------------------------------------------------------------------------------
11
+
12
+ NOTE: This guide assumes the user knows how to use the [Rails form helpers](form_helpers.html) in general. Also, it’s **not** an API reference. For a complete reference please visit [the Rails API documentation](http://api.rubyonrails.org/).
13
+
14
+
15
+ Model setup
16
+ -----------
17
+
18
+ To be able to use the nested model functionality in your forms, the model will need to support some basic operations.
19
+
20
+ First of all, it needs to define a writer method for the attribute that corresponds to the association you are building a nested model form for. The `fields_for` form helper will look for this method to decide whether or not a nested model form should be build.
21
+
22
+ If the associated object is an array a form builder will be yielded for each object, else only a single form builder will be yielded.
23
+
24
+ Consider a Person model with an associated Address. When asked to yield a nested FormBuilder for the `:address` attribute, the `fields_for` form helper will look for a method on the Person instance named `address_attributes=`.
25
+
26
+ ### ActiveRecord::Base model
27
+
28
+ For an ActiveRecord::Base model and association this writer method is commonly defined with the `accepts_nested_attributes_for` class method:
29
+
30
+ #### has_one
31
+
32
+ ```ruby
33
+ class Person < ActiveRecord::Base
34
+ has_one :address
35
+ accepts_nested_attributes_for :address
36
+ end
37
+ ```
38
+
39
+ #### belongs_to
40
+
41
+ ```ruby
42
+ class Person < ActiveRecord::Base
43
+ belongs_to :firm
44
+ accepts_nested_attributes_for :firm
45
+ end
46
+ ```
47
+
48
+ #### has_many / has_and_belongs_to_many
49
+
50
+ ```ruby
51
+ class Person < ActiveRecord::Base
52
+ has_many :projects
53
+ accepts_nested_attributes_for :projects
54
+ end
55
+ ```
56
+
57
+ ### Custom model
58
+
59
+ As you might have inflected from this explanation, you _don’t_ necessarily need an ActiveRecord::Base model to use this functionality. The following examples are sufficient to enable the nested model form behavior:
60
+
61
+ #### Single associated object
62
+
63
+ ```ruby
64
+ class Person
65
+ def address
66
+ Address.new
67
+ end
68
+
69
+ def address_attributes=(attributes)
70
+ # ...
71
+ end
72
+ end
73
+ ```
74
+
75
+ #### Association collection
76
+
77
+ ```ruby
78
+ class Person
79
+ def projects
80
+ [Project.new, Project.new]
81
+ end
82
+
83
+ def projects_attributes=(attributes)
84
+ # ...
85
+ end
86
+ end
87
+ ```
88
+
89
+ NOTE: See (TODO) in the advanced section for more information on how to deal with the CRUD operations in your custom model.
90
+
91
+ Views
92
+ -----
93
+
94
+ ### Controller code
95
+
96
+ A nested model form will _only_ be built if the associated object(s) exist. This means that for a new model instance you would probably want to build the associated object(s) first.
97
+
98
+ Consider the following typical RESTful controller which will prepare a new Person instance and its `address` and `projects` associations before rendering the `new` template:
99
+
100
+ ```ruby
101
+ class PeopleController < ApplicationController
102
+ def new
103
+ @person = Person.new
104
+ @person.built_address
105
+ 2.times { @person.projects.build }
106
+ end
107
+
108
+ def create
109
+ @person = Person.new(params[:person])
110
+ if @person.save
111
+ # ...
112
+ end
113
+ end
114
+ end
115
+ ```
116
+
117
+ NOTE: Obviously the instantiation of the associated object(s) can become tedious and not DRY, so you might want to move that into the model itself. ActiveRecord::Base provides an `after_initialize` callback which is a good way to refactor this.
118
+
119
+ ### Form code
120
+
121
+ Now that you have a model instance, with the appropriate methods and associated object(s), you can start building the nested model form.
122
+
123
+ #### Standard form
124
+
125
+ Start out with a regular RESTful form:
126
+
127
+ ```erb
128
+ <%= form_for @person do |f| %>
129
+ <%= f.text_field :name %>
130
+ <% end %>
131
+ ```
132
+
133
+ This will generate the following html:
134
+
135
+ ```html
136
+ <form action="/people" class="new_person" id="new_person" method="post">
137
+ <input id="person_name" name="person[name]" type="text" />
138
+ </form>
139
+ ```
140
+
141
+ #### Nested form for a single associated object
142
+
143
+ Now add a nested form for the `address` association:
144
+
145
+ ```erb
146
+ <%= form_for @person do |f| %>
147
+ <%= f.text_field :name %>
148
+
149
+ <%= f.fields_for :address do |af| %>
150
+ <%= af.text_field :street %>
151
+ <% end %>
152
+ <% end %>
153
+ ```
154
+
155
+ This generates:
156
+
157
+ ```html
158
+ <form action="/people" class="new_person" id="new_person" method="post">
159
+ <input id="person_name" name="person[name]" type="text" />
160
+
161
+ <input id="person_address_attributes_street" name="person[address_attributes][street]" type="text" />
162
+ </form>
163
+ ```
164
+
165
+ Notice that `fields_for` recognized the `address` as an association for which a nested model form should be built by the way it has namespaced the `name` attribute.
166
+
167
+ When this form is posted the Rails parameter parser will construct a hash like the following:
168
+
169
+ ```ruby
170
+ {
171
+ "person" => {
172
+ "name" => "Eloy Duran",
173
+ "address_attributes" => {
174
+ "street" => "Nieuwe Prinsengracht"
175
+ }
176
+ }
177
+ }
178
+ ```
179
+
180
+ That’s it. The controller will simply pass this hash on to the model from the `create` action. The model will then handle building the `address` association for you and automatically save it when the parent (`person`) is saved.
181
+
182
+ #### Nested form for a collection of associated objects
183
+
184
+ The form code for an association collection is pretty similar to that of a single associated object:
185
+
186
+ ```erb
187
+ <%= form_for @person do |f| %>
188
+ <%= f.text_field :name %>
189
+
190
+ <%= f.fields_for :projects do |pf| %>
191
+ <%= pf.text_field :name %>
192
+ <% end %>
193
+ <% end %>
194
+ ```
195
+
196
+ Which generates:
197
+
198
+ ```html
199
+ <form action="/people" class="new_person" id="new_person" method="post">
200
+ <input id="person_name" name="person[name]" type="text" />
201
+
202
+ <input id="person_projects_attributes_0_name" name="person[projects_attributes][0][name]" type="text" />
203
+ <input id="person_projects_attributes_1_name" name="person[projects_attributes][1][name]" type="text" />
204
+ </form>
205
+ ```
206
+
207
+ As you can see it has generated 2 `project name` inputs, one for each new `project` that was built in the controller's `new` action. Only this time the `name` attribute of the input contains a digit as an extra namespace. This will be parsed by the Rails parameter parser as:
208
+
209
+ ```ruby
210
+ {
211
+ "person" => {
212
+ "name" => "Eloy Duran",
213
+ "projects_attributes" => {
214
+ "0" => { "name" => "Project 1" },
215
+ "1" => { "name" => "Project 2" }
216
+ }
217
+ }
218
+ }
219
+ ```
220
+
221
+ You can basically see the `projects_attributes` hash as an array of attribute hashes, one for each model instance.
222
+
223
+ NOTE: The reason that `fields_for` constructed a form which would result in a hash instead of an array is that it won't work for any forms nested deeper than one level deep.
224
+
225
+ TIP: You _can_ however pass an array to the writer method generated by `accepts_nested_attributes_for` if you're using plain Ruby or some other API access. See (TODO) for more info and example.
@@ -0,0 +1,435 @@
1
+ The Basics of Creating Rails Plugins
2
+ ====================================
3
+
4
+ A Rails plugin is either an extension or a modification of the core framework. Plugins provide:
5
+
6
+ * a way for developers to share bleeding-edge ideas without hurting the stable code base
7
+ * a segmented architecture so that units of code can be fixed or updated on their own release schedule
8
+ * an outlet for the core developers so that they don’t have to include every cool new feature under the sun
9
+
10
+ After reading this guide, you will know:
11
+
12
+ * How to create a plugin from scratch.
13
+ * How to write and run tests for the plugin.
14
+
15
+ This guide describes how to build a test-driven plugin that will:
16
+
17
+ * Extend core Ruby classes like Hash and String.
18
+ * Add methods to ActiveRecord::Base in the tradition of the 'acts_as' plugins.
19
+ * Give you information about where to put generators in your plugin.
20
+
21
+ For the purpose of this guide pretend for a moment that you are an avid bird watcher.
22
+ Your favorite bird is the Yaffle, and you want to create a plugin that allows other developers to share in the Yaffle
23
+ goodness.
24
+
25
+ --------------------------------------------------------------------------------
26
+
27
+ Setup
28
+ -----
29
+
30
+ Currently, Rails plugins are built as gems, _gemified plugins_. They can be shared across
31
+ different rails applications using RubyGems and Bundler if desired.
32
+
33
+ ### Generate a gemified plugin.
34
+
35
+
36
+ Rails ships with a `rails plugin new` command which creates a
37
+ skeleton for developing any kind of Rails extension with the ability
38
+ to run integration tests using a dummy Rails application. See usage
39
+ and options by asking for help:
40
+
41
+ ```bash
42
+ $ rails plugin --help
43
+ ```
44
+
45
+ Testing your newly generated plugin
46
+ -----------------------------------
47
+
48
+ You can navigate to the directory that contains the plugin, run the `bundle install` command
49
+ and run the one generated test using the `rake` command.
50
+
51
+ You should see:
52
+
53
+ ```bash
54
+ 2 tests, 2 assertions, 0 failures, 0 errors, 0 skips
55
+ ```
56
+
57
+ This will tell you that everything got generated properly and you are ready to start adding functionality.
58
+
59
+ Extending Core Classes
60
+ ----------------------
61
+
62
+ This section will explain how to add a method to String that will be available anywhere in your rails application.
63
+
64
+ In this example you will add a method to String named `to_squawk`. To begin, create a new test file with a few assertions:
65
+
66
+ ```ruby
67
+ # yaffle/test/core_ext_test.rb
68
+
69
+ require 'test_helper'
70
+
71
+ class CoreExtTest < Test::Unit::TestCase
72
+ def test_to_squawk_prepends_the_word_squawk
73
+ assert_equal "squawk! Hello World", "Hello World".to_squawk
74
+ end
75
+ end
76
+ ```
77
+
78
+ Run `rake` to run the test. This test should fail because we haven't implemented the `to_squawk` method:
79
+
80
+ ```bash
81
+ 1) Error:
82
+ test_to_squawk_prepends_the_word_squawk(CoreExtTest):
83
+ NoMethodError: undefined method `to_squawk' for [Hello World](String)
84
+ test/core_ext_test.rb:5:in `test_to_squawk_prepends_the_word_squawk'
85
+ ```
86
+
87
+ Great - now you are ready to start development.
88
+
89
+ Then in `lib/yaffle.rb` add `require "yaffle/core_ext"`:
90
+
91
+ ```ruby
92
+ # yaffle/lib/yaffle.rb
93
+
94
+ require "yaffle/core_ext"
95
+
96
+ module Yaffle
97
+ end
98
+ ```
99
+
100
+ Finally, create the `core_ext.rb` file and add the `to_squawk` method:
101
+
102
+ ```ruby
103
+ # yaffle/lib/yaffle/core_ext.rb
104
+
105
+ String.class_eval do
106
+ def to_squawk
107
+ "squawk! #{self}".strip
108
+ end
109
+ end
110
+ ```
111
+
112
+ To test that your method does what it says it does, run the unit tests with `rake` from your plugin directory.
113
+
114
+ ```bash
115
+ 3 tests, 3 assertions, 0 failures, 0 errors, 0 skips
116
+ ```
117
+
118
+ To see this in action, change to the test/dummy directory, fire up a console and start squawking:
119
+
120
+ ```bash
121
+ $ rails console
122
+ >> "Hello World".to_squawk
123
+ => "squawk! Hello World"
124
+ ```
125
+
126
+ Add an "acts_as" Method to Active Record
127
+ ----------------------------------------
128
+
129
+ A common pattern in plugins is to add a method called 'acts_as_something' to models. In this case, you
130
+ want to write a method called 'acts_as_yaffle' that adds a 'squawk' method to your Active Record models.
131
+
132
+ To begin, set up your files so that you have:
133
+
134
+ ```ruby
135
+ # yaffle/test/acts_as_yaffle_test.rb
136
+
137
+ require 'test_helper'
138
+
139
+ class ActsAsYaffleTest < Test::Unit::TestCase
140
+ end
141
+ ```
142
+
143
+ ```ruby
144
+ # yaffle/lib/yaffle.rb
145
+
146
+ require "yaffle/core_ext"
147
+ require 'yaffle/acts_as_yaffle'
148
+
149
+ module Yaffle
150
+ end
151
+ ```
152
+
153
+ ```ruby
154
+ # yaffle/lib/yaffle/acts_as_yaffle.rb
155
+
156
+ module Yaffle
157
+ module ActsAsYaffle
158
+ # your code will go here
159
+ end
160
+ end
161
+ ```
162
+
163
+ ### Add a Class Method
164
+
165
+ This plugin will expect that you've added a method to your model named 'last_squawk'. However, the
166
+ plugin users might have already defined a method on their model named 'last_squawk' that they use
167
+ for something else. This plugin will allow the name to be changed by adding a class method called 'yaffle_text_field'.
168
+
169
+ To start out, write a failing test that shows the behavior you'd like:
170
+
171
+ ```ruby
172
+ # yaffle/test/acts_as_yaffle_test.rb
173
+
174
+ require 'test_helper'
175
+
176
+ class ActsAsYaffleTest < Test::Unit::TestCase
177
+
178
+ def test_a_hickwalls_yaffle_text_field_should_be_last_squawk
179
+ assert_equal "last_squawk", Hickwall.yaffle_text_field
180
+ end
181
+
182
+ def test_a_wickwalls_yaffle_text_field_should_be_last_tweet
183
+ assert_equal "last_tweet", Wickwall.yaffle_text_field
184
+ end
185
+
186
+ end
187
+ ```
188
+
189
+ When you run `rake`, you should see the following:
190
+
191
+ ```
192
+ 1) Error:
193
+ test_a_hickwalls_yaffle_text_field_should_be_last_squawk(ActsAsYaffleTest):
194
+ NameError: uninitialized constant ActsAsYaffleTest::Hickwall
195
+ test/acts_as_yaffle_test.rb:6:in `test_a_hickwalls_yaffle_text_field_should_be_last_squawk'
196
+
197
+ 2) Error:
198
+ test_a_wickwalls_yaffle_text_field_should_be_last_tweet(ActsAsYaffleTest):
199
+ NameError: uninitialized constant ActsAsYaffleTest::Wickwall
200
+ test/acts_as_yaffle_test.rb:10:in `test_a_wickwalls_yaffle_text_field_should_be_last_tweet'
201
+
202
+ 5 tests, 3 assertions, 0 failures, 2 errors, 0 skips
203
+ ```
204
+
205
+ This tells us that we don't have the necessary models (Hickwall and Wickwall) that we are trying to test.
206
+ We can easily generate these models in our "dummy" Rails application by running the following commands from the
207
+ test/dummy directory:
208
+
209
+ ```bash
210
+ $ cd test/dummy
211
+ $ rails generate model Hickwall last_squawk:string
212
+ $ rails generate model Wickwall last_squawk:string last_tweet:string
213
+ ```
214
+
215
+ Now you can create the necessary database tables in your testing database by navigating to your dummy app
216
+ and migrating the database. First
217
+
218
+ ```bash
219
+ $ cd test/dummy
220
+ $ rake db:migrate
221
+ $ rake db:test:prepare
222
+ ```
223
+
224
+ While you are here, change the Hickwall and Wickwall models so that they know that they are supposed to act
225
+ like yaffles.
226
+
227
+ ```ruby
228
+ # test/dummy/app/models/hickwall.rb
229
+
230
+ class Hickwall < ActiveRecord::Base
231
+ acts_as_yaffle
232
+ end
233
+
234
+ # test/dummy/app/models/wickwall.rb
235
+
236
+ class Wickwall < ActiveRecord::Base
237
+ acts_as_yaffle yaffle_text_field: :last_tweet
238
+ end
239
+
240
+ ```
241
+
242
+ We will also add code to define the acts_as_yaffle method.
243
+
244
+ ```ruby
245
+ # yaffle/lib/yaffle/acts_as_yaffle.rb
246
+ module Yaffle
247
+ module ActsAsYaffle
248
+ extend ActiveSupport::Concern
249
+
250
+ included do
251
+ end
252
+
253
+ module ClassMethods
254
+ def acts_as_yaffle(options = {})
255
+ # your code will go here
256
+ end
257
+ end
258
+ end
259
+ end
260
+
261
+ ActiveRecord::Base.send :include, Yaffle::ActsAsYaffle
262
+ ```
263
+
264
+ You can then return to the root directory (`cd ../..`) of your plugin and rerun the tests using `rake`.
265
+
266
+ ```
267
+ 1) Error:
268
+ test_a_hickwalls_yaffle_text_field_should_be_last_squawk(ActsAsYaffleTest):
269
+ NoMethodError: undefined method `yaffle_text_field' for #<Class:0x000001016661b8>
270
+ /Users/xxx/.rvm/gems/ruby-1.9.2-p136@xxx/gems/activerecord-3.0.3/lib/active_record/base.rb:1008:in `method_missing'
271
+ test/acts_as_yaffle_test.rb:5:in `test_a_hickwalls_yaffle_text_field_should_be_last_squawk'
272
+
273
+ 2) Error:
274
+ test_a_wickwalls_yaffle_text_field_should_be_last_tweet(ActsAsYaffleTest):
275
+ NoMethodError: undefined method `yaffle_text_field' for #<Class:0x00000101653748>
276
+ Users/xxx/.rvm/gems/ruby-1.9.2-p136@xxx/gems/activerecord-3.0.3/lib/active_record/base.rb:1008:in `method_missing'
277
+ test/acts_as_yaffle_test.rb:9:in `test_a_wickwalls_yaffle_text_field_should_be_last_tweet'
278
+
279
+ 5 tests, 3 assertions, 0 failures, 2 errors, 0 skips
280
+
281
+ ```
282
+
283
+ Getting closer... Now we will implement the code of the acts_as_yaffle method to make the tests pass.
284
+
285
+ ```ruby
286
+ # yaffle/lib/yaffle/acts_as_yaffle.rb
287
+
288
+ module Yaffle
289
+ module ActsAsYaffle
290
+ extend ActiveSupport::Concern
291
+
292
+ included do
293
+ end
294
+
295
+ module ClassMethods
296
+ def acts_as_yaffle(options = {})
297
+ cattr_accessor :yaffle_text_field
298
+ self.yaffle_text_field = (options[:yaffle_text_field] || :last_squawk).to_s
299
+ end
300
+ end
301
+ end
302
+ end
303
+
304
+ ActiveRecord::Base.send :include, Yaffle::ActsAsYaffle
305
+ ```
306
+
307
+ When you run `rake` you should see the tests all pass:
308
+
309
+ ```bash
310
+ 5 tests, 5 assertions, 0 failures, 0 errors, 0 skips
311
+ ```
312
+
313
+ ### Add an Instance Method
314
+
315
+ This plugin will add a method named 'squawk' to any Active Record object that calls 'acts_as_yaffle'. The 'squawk'
316
+ method will simply set the value of one of the fields in the database.
317
+
318
+ To start out, write a failing test that shows the behavior you'd like:
319
+
320
+ ```ruby
321
+ # yaffle/test/acts_as_yaffle_test.rb
322
+ require 'test_helper'
323
+
324
+ class ActsAsYaffleTest < Test::Unit::TestCase
325
+
326
+ def test_a_hickwalls_yaffle_text_field_should_be_last_squawk
327
+ assert_equal "last_squawk", Hickwall.yaffle_text_field
328
+ end
329
+
330
+ def test_a_wickwalls_yaffle_text_field_should_be_last_tweet
331
+ assert_equal "last_tweet", Wickwall.yaffle_text_field
332
+ end
333
+
334
+ def test_hickwalls_squawk_should_populate_last_squawk
335
+ hickwall = Hickwall.new
336
+ hickwall.squawk("Hello World")
337
+ assert_equal "squawk! Hello World", hickwall.last_squawk
338
+ end
339
+
340
+ def test_wickwalls_squawk_should_populate_last_tweet
341
+ wickwall = Wickwall.new
342
+ wickwall.squawk("Hello World")
343
+ assert_equal "squawk! Hello World", wickwall.last_tweet
344
+ end
345
+ end
346
+ ```
347
+
348
+ Run the test to make sure the last two tests fail with an error that contains "NoMethodError: undefined method `squawk'",
349
+ then update 'acts_as_yaffle.rb' to look like this:
350
+
351
+ ```ruby
352
+ # yaffle/lib/yaffle/acts_as_yaffle.rb
353
+
354
+ module Yaffle
355
+ module ActsAsYaffle
356
+ extend ActiveSupport::Concern
357
+
358
+ included do
359
+ end
360
+
361
+ module ClassMethods
362
+ def acts_as_yaffle(options = {})
363
+ cattr_accessor :yaffle_text_field
364
+ self.yaffle_text_field = (options[:yaffle_text_field] || :last_squawk).to_s
365
+
366
+ include Yaffle::ActsAsYaffle::LocalInstanceMethods
367
+ end
368
+ end
369
+
370
+ module LocalInstanceMethods
371
+ def squawk(string)
372
+ write_attribute(self.class.yaffle_text_field, string.to_squawk)
373
+ end
374
+ end
375
+ end
376
+ end
377
+
378
+ ActiveRecord::Base.send :include, Yaffle::ActsAsYaffle
379
+ ```
380
+
381
+ Run `rake` one final time and you should see:
382
+
383
+ ```
384
+ 7 tests, 7 assertions, 0 failures, 0 errors, 0 skips
385
+ ```
386
+
387
+ NOTE: The use of `write_attribute` to write to the field in model is just one example of how a plugin can interact with the model, and will not always be the right method to use. For example, you could also use `send("#{self.class.yaffle_text_field}=", string.to_squawk)`.
388
+
389
+ Generators
390
+ ----------
391
+
392
+ Generators can be included in your gem simply by creating them in a lib/generators directory of your plugin. More information about
393
+ the creation of generators can be found in the [Generators Guide](generators.html)
394
+
395
+ Publishing your Gem
396
+ -------------------
397
+
398
+ Gem plugins currently in development can easily be shared from any Git repository. To share the Yaffle gem with others, simply
399
+ commit the code to a Git repository (like GitHub) and add a line to the Gemfile of the application in question:
400
+
401
+ ```ruby
402
+ gem 'yaffle', git: 'git://github.com/yaffle_watcher/yaffle.git'
403
+ ```
404
+
405
+ After running `bundle install`, your gem functionality will be available to the application.
406
+
407
+ When the gem is ready to be shared as a formal release, it can be published to [RubyGems](http://www.rubygems.org).
408
+ For more information about publishing gems to RubyGems, see: [Creating and Publishing Your First Ruby Gem](http://blog.thepete.net/2010/11/creating-and-publishing-your-first-ruby.html)
409
+
410
+ RDoc Documentation
411
+ ------------------
412
+
413
+ Once your plugin is stable and you are ready to deploy do everyone else a favor and document it! Luckily, writing documentation for your plugin is easy.
414
+
415
+ The first step is to update the README file with detailed information about how to use your plugin. A few key things to include are:
416
+
417
+ * Your name
418
+ * How to install
419
+ * How to add the functionality to the app (several examples of common use cases)
420
+ * Warnings, gotchas or tips that might help users and save them time
421
+
422
+ Once your README is solid, go through and add rdoc comments to all of the methods that developers will use. It's also customary to add '#:nodoc:' comments to those parts of the code that are not included in the public API.
423
+
424
+ Once your comments are good to go, navigate to your plugin directory and run:
425
+
426
+ ```bash
427
+ $ rake rdoc
428
+ ```
429
+
430
+ ### References
431
+
432
+ * [Developing a RubyGem using Bundler](https://github.com/radar/guides/blob/master/gem-development.md)
433
+ * [Using .gemspecs as Intended](http://yehudakatz.com/2010/04/02/using-gemspecs-as-intended/)
434
+ * [Gemspec Reference](http://docs.rubygems.org/read/chapter/20)
435
+ * [GemPlugins: A Brief Introduction to the Future of Rails Plugins](http://www.intridea.com/blog/2008/6/11/gemplugins-a-brief-introduction-to-the-future-of-rails-plugins)