railties 3.1.12 → 3.2.0.rc1

Sign up to get free protection for your applications and to get access to all the features.
Files changed (210) hide show
  1. data/CHANGELOG.md +2292 -41
  2. data/README.rdoc +14 -5
  3. data/bin/rails +7 -0
  4. data/guides/code/getting_started/Gemfile +27 -0
  5. data/guides/code/getting_started/README.rdoc +261 -0
  6. data/guides/code/getting_started/Rakefile +7 -0
  7. data/guides/code/getting_started/app/assets/images/rails.png +0 -0
  8. data/guides/code/getting_started/app/assets/javascripts/application.js +9 -0
  9. data/guides/code/getting_started/app/assets/javascripts/comments.js.coffee +3 -0
  10. data/guides/code/getting_started/app/assets/javascripts/home.js.coffee +3 -0
  11. data/guides/code/getting_started/app/assets/javascripts/posts.js.coffee +3 -0
  12. data/guides/code/getting_started/app/assets/stylesheets/application.css +7 -0
  13. data/guides/code/getting_started/app/assets/stylesheets/comments.css.scss +3 -0
  14. data/guides/code/getting_started/app/assets/stylesheets/home.css.scss +3 -0
  15. data/guides/code/getting_started/app/assets/stylesheets/posts.css.scss +3 -0
  16. data/guides/code/getting_started/app/assets/stylesheets/scaffolds.css.scss +56 -0
  17. data/guides/code/getting_started/app/controllers/application_controller.rb +3 -0
  18. data/guides/code/getting_started/app/controllers/comments_controller.rb +16 -0
  19. data/guides/code/getting_started/app/controllers/home_controller.rb +5 -0
  20. data/guides/code/getting_started/app/controllers/posts_controller.rb +84 -0
  21. data/guides/code/getting_started/app/helpers/application_helper.rb +2 -0
  22. data/guides/code/getting_started/app/helpers/comments_helper.rb +2 -0
  23. data/guides/code/getting_started/app/helpers/home_helper.rb +2 -0
  24. data/guides/code/getting_started/app/helpers/posts_helper.rb +5 -0
  25. data/guides/code/getting_started/app/models/comment.rb +3 -0
  26. data/guides/code/getting_started/app/models/post.rb +11 -0
  27. data/guides/code/getting_started/app/models/tag.rb +3 -0
  28. data/guides/code/getting_started/app/views/comments/_comment.html.erb +15 -0
  29. data/guides/code/getting_started/app/views/comments/_form.html.erb +13 -0
  30. data/guides/code/getting_started/app/views/home/index.html.erb +2 -0
  31. data/guides/code/getting_started/app/views/layouts/application.html.erb +14 -0
  32. data/guides/code/getting_started/app/views/posts/_form.html.erb +32 -0
  33. data/guides/code/getting_started/app/views/posts/edit.html.erb +6 -0
  34. data/guides/code/getting_started/app/views/posts/index.html.erb +27 -0
  35. data/guides/code/getting_started/app/views/posts/new.html.erb +5 -0
  36. data/guides/code/getting_started/app/views/posts/show.html.erb +31 -0
  37. data/guides/code/getting_started/app/views/tags/_form.html.erb +12 -0
  38. data/guides/code/getting_started/config.ru +4 -0
  39. data/guides/code/getting_started/config/application.rb +53 -0
  40. data/guides/code/getting_started/config/boot.rb +6 -0
  41. data/guides/code/getting_started/config/database.yml +25 -0
  42. data/guides/code/getting_started/config/environment.rb +5 -0
  43. data/guides/code/getting_started/config/environments/development.rb +37 -0
  44. data/guides/code/getting_started/config/environments/production.rb +67 -0
  45. data/guides/code/getting_started/config/environments/test.rb +37 -0
  46. data/guides/code/getting_started/config/initializers/backtrace_silencers.rb +7 -0
  47. data/guides/code/getting_started/config/initializers/inflections.rb +10 -0
  48. data/guides/code/getting_started/config/initializers/mime_types.rb +5 -0
  49. data/guides/code/getting_started/config/initializers/secret_token.rb +7 -0
  50. data/guides/code/getting_started/config/initializers/session_store.rb +8 -0
  51. data/guides/code/getting_started/config/initializers/wrap_parameters.rb +14 -0
  52. data/guides/code/getting_started/config/locales/en.yml +5 -0
  53. data/guides/code/getting_started/config/routes.rb +64 -0
  54. data/guides/code/getting_started/db/migrate/20110901012504_create_posts.rb +11 -0
  55. data/guides/code/getting_started/db/migrate/20110901012815_create_comments.rb +12 -0
  56. data/guides/code/getting_started/db/migrate/20110901013701_create_tags.rb +11 -0
  57. data/guides/code/getting_started/db/schema.rb +43 -0
  58. data/guides/code/getting_started/db/seeds.rb +7 -0
  59. data/guides/code/getting_started/doc/README_FOR_APP +2 -0
  60. data/guides/code/getting_started/public/404.html +26 -0
  61. data/guides/code/getting_started/public/422.html +26 -0
  62. data/guides/code/getting_started/public/500.html +26 -0
  63. data/guides/code/getting_started/public/favicon.ico +0 -0
  64. data/guides/code/getting_started/public/robots.txt +5 -0
  65. data/guides/code/getting_started/script/rails +6 -0
  66. data/guides/code/getting_started/test/fixtures/comments.yml +11 -0
  67. data/guides/code/getting_started/test/fixtures/posts.yml +11 -0
  68. data/guides/code/getting_started/test/fixtures/tags.yml +9 -0
  69. data/guides/code/getting_started/test/functional/comments_controller_test.rb +7 -0
  70. data/guides/code/getting_started/test/functional/home_controller_test.rb +9 -0
  71. data/guides/code/getting_started/test/functional/posts_controller_test.rb +49 -0
  72. data/guides/code/getting_started/test/performance/browsing_test.rb +12 -0
  73. data/guides/code/getting_started/test/test_helper.rb +13 -0
  74. data/guides/code/getting_started/test/unit/comment_test.rb +7 -0
  75. data/guides/code/getting_started/test/unit/helpers/comments_helper_test.rb +4 -0
  76. data/guides/code/getting_started/test/unit/helpers/home_helper_test.rb +4 -0
  77. data/guides/code/getting_started/test/unit/helpers/posts_helper_test.rb +4 -0
  78. data/guides/code/getting_started/test/unit/post_test.rb +7 -0
  79. data/guides/code/getting_started/test/unit/tag_test.rb +7 -0
  80. data/guides/rails_guides/generator.rb +2 -1
  81. data/guides/source/3_0_release_notes.textile +2 -2
  82. data/guides/source/3_1_release_notes.textile +3 -110
  83. data/guides/source/action_controller_overview.textile +11 -13
  84. data/guides/source/action_mailer_basics.textile +7 -18
  85. data/guides/source/action_view_overview.textile +78 -9
  86. data/guides/source/active_model_basics.textile +205 -0
  87. data/guides/source/active_record_basics.textile +31 -31
  88. data/guides/source/active_record_querying.textile +288 -67
  89. data/guides/source/active_record_validations_callbacks.textile +69 -75
  90. data/guides/source/active_resource_basics.textile +48 -2
  91. data/guides/source/active_support_core_extensions.textile +145 -24
  92. data/guides/source/ajax_on_rails.textile +65 -7
  93. data/guides/source/api_documentation_guidelines.textile +0 -6
  94. data/guides/source/asset_pipeline.textile +2 -2
  95. data/guides/source/association_basics.textile +25 -34
  96. data/guides/source/caching_with_rails.textile +12 -17
  97. data/guides/source/command_line.textile +29 -19
  98. data/guides/source/configuring.textile +40 -18
  99. data/guides/source/contributing_to_ruby_on_rails.textile +11 -18
  100. data/guides/source/debugging_rails_applications.textile +10 -21
  101. data/guides/source/engines.textile +618 -0
  102. data/guides/source/form_helpers.textile +1 -12
  103. data/guides/source/generators.textile +9 -11
  104. data/guides/source/getting_started.textile +152 -152
  105. data/guides/source/i18n.textile +4 -5
  106. data/guides/source/index.html.erb +0 -1
  107. data/guides/source/initialization.textile +26 -26
  108. data/guides/source/layouts_and_rendering.textile +97 -61
  109. data/guides/source/migrations.textile +380 -161
  110. data/guides/source/performance_testing.textile +4 -10
  111. data/guides/source/plugins.textile +11 -19
  112. data/guides/source/rails_application_templates.textile +12 -4
  113. data/guides/source/rails_on_rack.textile +25 -19
  114. data/guides/source/routing.textile +6 -13
  115. data/guides/source/ruby_on_rails_guides_guidelines.textile +0 -5
  116. data/guides/source/security.textile +11 -15
  117. data/guides/source/testing.textile +1 -9
  118. data/lib/rails/application.rb +107 -42
  119. data/lib/rails/application/bootstrap.rb +12 -11
  120. data/lib/rails/application/configuration.rb +27 -21
  121. data/lib/rails/application/finisher.rb +40 -17
  122. data/lib/rails/application/route_inspector.rb +75 -0
  123. data/lib/rails/application/routes_reloader.rb +15 -4
  124. data/lib/rails/code_statistics.rb +16 -5
  125. data/lib/rails/commands.rb +6 -5
  126. data/lib/rails/commands/application.rb +8 -1
  127. data/lib/rails/commands/console.rb +2 -0
  128. data/lib/rails/commands/dbconsole.rb +2 -2
  129. data/lib/rails/commands/destroy.rb +0 -2
  130. data/lib/rails/commands/generate.rb +3 -3
  131. data/lib/rails/commands/plugin.rb +161 -159
  132. data/lib/rails/commands/plugin_new.rb +3 -2
  133. data/lib/rails/commands/runner.rb +4 -0
  134. data/lib/rails/console/app.rb +26 -22
  135. data/lib/rails/console/helpers.rb +9 -5
  136. data/lib/rails/engine.rb +70 -34
  137. data/lib/rails/engine/commands.rb +39 -0
  138. data/lib/rails/engine/configuration.rb +1 -1
  139. data/lib/rails/generators.rb +3 -14
  140. data/lib/rails/generators/actions.rb +36 -9
  141. data/lib/rails/generators/app_base.rb +34 -38
  142. data/lib/rails/generators/base.rb +4 -4
  143. data/lib/rails/generators/generated_attribute.rb +1 -1
  144. data/lib/rails/generators/named_base.rb +1 -3
  145. data/lib/rails/generators/rails/app/USAGE +6 -0
  146. data/lib/rails/generators/rails/app/app_generator.rb +6 -2
  147. data/lib/rails/generators/rails/app/templates/Gemfile +4 -3
  148. data/lib/rails/generators/rails/app/templates/app/assets/javascripts/application.js.tt +9 -3
  149. data/lib/rails/generators/rails/app/templates/app/assets/stylesheets/application.css +11 -5
  150. data/lib/rails/generators/rails/app/templates/app/mailers/.empty_directory +0 -0
  151. data/lib/rails/generators/rails/app/templates/app/models/.empty_directory +0 -0
  152. data/lib/rails/generators/rails/app/templates/app/views/layouts/application.html.erb.tt +1 -1
  153. data/lib/rails/generators/rails/app/templates/config/application.rb +11 -0
  154. data/lib/rails/generators/rails/app/templates/config/databases/jdbcpostgresql.yml +1 -1
  155. data/lib/rails/generators/rails/app/templates/config/databases/postgresql.yml +1 -1
  156. data/lib/rails/generators/rails/app/templates/config/environments/development.rb.tt +10 -1
  157. data/lib/rails/generators/rails/app/templates/config/environments/production.rb.tt +10 -1
  158. data/lib/rails/generators/rails/app/templates/config/environments/test.rb.tt +6 -6
  159. data/lib/rails/generators/rails/app/templates/config/initializers/inflections.rb +5 -0
  160. data/lib/rails/generators/rails/app/templates/config/routes.rb +1 -1
  161. data/lib/rails/generators/rails/app/templates/public/500.html +0 -1
  162. data/lib/rails/generators/rails/app/templates/public/index.html +1 -1
  163. data/lib/rails/generators/rails/app/templates/public/stylesheets/.empty_directory +0 -0
  164. data/lib/rails/generators/rails/app/templates/test/fixtures/.empty_directory +0 -0
  165. data/lib/rails/generators/rails/app/templates/test/functional/.empty_directory +0 -0
  166. data/lib/rails/generators/rails/app/templates/test/integration/.empty_directory +0 -0
  167. data/lib/rails/generators/rails/app/templates/test/unit/.empty_directory +0 -0
  168. data/lib/rails/generators/rails/controller/templates/controller.rb +1 -1
  169. data/lib/rails/generators/rails/generator/templates/templates/.empty_directory +0 -0
  170. data/lib/rails/generators/rails/plugin_new/plugin_new_generator.rb +17 -5
  171. data/lib/rails/generators/rails/plugin_new/templates/Rakefile +1 -0
  172. data/lib/rails/generators/rails/plugin_new/templates/app/mailers/.empty_directory +0 -0
  173. data/lib/rails/generators/rails/plugin_new/templates/app/models/.empty_directory +0 -0
  174. data/lib/rails/generators/rails/plugin_new/templates/app/views/layouts/%name%/application.html.erb.tt +1 -1
  175. data/lib/rails/generators/rails/plugin_new/templates/gitignore +4 -3
  176. data/lib/rails/generators/rails/plugin_new/templates/lib/%name%/engine.rb +1 -1
  177. data/lib/rails/generators/rails/plugin_new/templates/rails/application.rb +1 -1
  178. data/lib/rails/generators/rails/plugin_new/templates/script/rails.tt +5 -3
  179. data/lib/rails/generators/rails/scaffold_controller/templates/controller.rb +2 -2
  180. data/lib/rails/generators/rails/task/USAGE +9 -0
  181. data/lib/rails/generators/rails/task/task_generator.rb +12 -0
  182. data/lib/rails/generators/rails/task/templates/task.rb +8 -0
  183. data/lib/rails/generators/resource_helpers.rb +3 -3
  184. data/lib/rails/generators/test_unit/integration/templates/integration_test.rb +0 -2
  185. data/lib/rails/generators/test_unit/scaffold/templates/functional_test.rb +4 -4
  186. data/lib/rails/paths.rb +11 -38
  187. data/lib/rails/rack/debugger.rb +3 -4
  188. data/lib/rails/rack/logger.rb +26 -12
  189. data/lib/rails/railtie.rb +6 -1
  190. data/lib/rails/railtie/configuration.rb +12 -5
  191. data/lib/rails/source_annotation_extractor.rb +12 -10
  192. data/lib/rails/tasks/documentation.rake +3 -1
  193. data/lib/rails/tasks/engine.rake +1 -0
  194. data/lib/rails/tasks/misc.rake +1 -1
  195. data/lib/rails/tasks/routes.rake +3 -23
  196. data/lib/rails/test_help.rb +1 -2
  197. data/lib/rails/test_unit/testing.rake +8 -4
  198. data/lib/rails/version.rb +3 -3
  199. metadata +131 -61
  200. checksums.yaml +0 -7
  201. data/lib/rails/generators/rails/plugin/USAGE +0 -13
  202. data/lib/rails/generators/rails/plugin/plugin_generator.rb +0 -54
  203. data/lib/rails/generators/rails/plugin/templates/MIT-LICENSE.tt +0 -20
  204. data/lib/rails/generators/rails/plugin/templates/README.tt +0 -13
  205. data/lib/rails/generators/rails/plugin/templates/Rakefile.tt +0 -23
  206. data/lib/rails/generators/rails/plugin/templates/init.rb +0 -1
  207. data/lib/rails/generators/rails/plugin/templates/install.rb +0 -1
  208. data/lib/rails/generators/rails/plugin/templates/lib/%file_name%.rb.tt +0 -1
  209. data/lib/rails/generators/rails/plugin/templates/lib/tasks/%file_name%_tasks.rake.tt +0 -4
  210. data/lib/rails/generators/rails/plugin/templates/uninstall.rb +0 -1
@@ -28,7 +28,7 @@ h4. Why Use Validations?
28
28
 
29
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
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.
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
32
 
33
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
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.
@@ -46,7 +46,7 @@ end
46
46
 
47
47
  We can see how it works by looking at some +rails console+ output:
48
48
 
49
- <shell>
49
+ <ruby>
50
50
  >> p = Person.new(:name => "John Doe")
51
51
  => #<Person id: nil, name: "John Doe", created_at: nil, :updated_at: nil>
52
52
  >> p.new_record?
@@ -55,7 +55,7 @@ We can see how it works by looking at some +rails console+ output:
55
55
  => true
56
56
  >> p.new_record?
57
57
  => false
58
- </shell>
58
+ </ruby>
59
59
 
60
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
61
 
@@ -94,7 +94,7 @@ Note that +save+ also has the ability to skip validations if passed +:validate =
94
94
 
95
95
  h4. +valid?+ and +invalid?+
96
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 added to the object, and false otherwise.
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
98
 
99
99
  <ruby>
100
100
  class Person < ActiveRecord::Base
@@ -105,7 +105,7 @@ Person.create(:name => "John Doe").valid? # => true
105
105
  Person.create(:name => nil).valid? # => false
106
106
  </ruby>
107
107
 
108
- When Active Record is performing validations, any errors found can be accessed through the +errors+ instance method. By definition an object is valid if this collection is empty after running validations.
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
109
 
110
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
111
 
@@ -139,7 +139,7 @@ end
139
139
  => ActiveRecord::RecordInvalid: Validation failed: Name can't be blank
140
140
  </ruby>
141
141
 
142
- +invalid?+ is simply the inverse of +valid?+. +invalid?+ triggers your validations and returns true if any errors were added to the object, and false otherwise.
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
143
 
144
144
  h4(#validations_overview-errors). +errors[]+
145
145
 
@@ -160,7 +160,7 @@ We'll cover validation errors in greater depth in the "Working with Validation E
160
160
 
161
161
  h3. Validation Helpers
162
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 field being validated.
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
164
 
165
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
166
 
@@ -428,6 +428,8 @@ class GoodnessValidator < ActiveModel::Validator
428
428
  end
429
429
  </ruby>
430
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
+
431
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.
432
434
 
433
435
  To implement the validate method, you must have a +record+ parameter defined, which is the record to be validated.
@@ -454,13 +456,13 @@ This helper validates attributes against a block. It doesn't have a predefined v
454
456
 
455
457
  <ruby>
456
458
  class Person < ActiveRecord::Base
457
- validates_each :name, :surname do |model, attr, value|
458
- model.errors.add(attr, 'must start with upper case') if value =~ /\A[a-z]/
459
+ validates_each :name, :surname do |record, attr, value|
460
+ record.errors.add(attr, 'must start with upper case') if value =~ /\A[a-z]/
459
461
  end
460
462
  end
461
463
  </ruby>
462
464
 
463
- The block receives the model, 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 can add an error message to the model, therefore making it invalid.
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.
464
466
 
465
467
  h3. Common Validation Options
466
468
 
@@ -580,7 +582,7 @@ Custom validators are classes that extend <tt>ActiveModel::Validator</tt>. These
580
582
  <ruby>
581
583
  class MyValidator < ActiveModel::Validator
582
584
  def validate(record)
583
- if record.name.starts_with? 'X'
585
+ unless record.name.starts_with? 'X'
584
586
  record.errors[:name] << 'Need a name starting with X please!'
585
587
  end
586
588
  end
@@ -661,7 +663,7 @@ The following is a list of the most commonly used methods. Please refer to the +
661
663
 
662
664
  h4(#working_with_validation_errors-errors). +errors+
663
665
 
664
- Returns an OrderedHash with all errors. Each key is the attribute name and the value is an array of strings with all errors.
666
+ 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.
665
667
 
666
668
  <ruby>
667
669
  class Person < ActiveRecord::Base
@@ -741,7 +743,7 @@ Another way to do this is using +[]=+ setter
741
743
 
742
744
  h4. +errors[:base]+
743
745
 
744
- 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 the array and uses it as the error message.
746
+ 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.
745
747
 
746
748
  <ruby>
747
749
  class Person < ActiveRecord::Base
@@ -785,7 +787,7 @@ end
785
787
 
786
788
  person = Person.new
787
789
  person.valid? # => false
788
- person.errors.size # => 3
790
+ person.errors.size # => 2
789
791
 
790
792
  person = Person.new(:name => "Andrea", :email => "andrea@example.com")
791
793
  person.valid? # => true
@@ -794,23 +796,15 @@ person.errors.size # => 0
794
796
 
795
797
  h3. Displaying Validation Errors in the View
796
798
 
797
- Rails maintains an official plugin that provides helpers to display the error messages of your models in your view templates. You can install it as a plugin or as a Gem.
799
+ "DynamicForm":https://github.com/joelmoss/dynamic_form provides helpers to display the error messages of your models in your view templates.
798
800
 
799
- h4. Installing as a plugin
800
-
801
- <shell>
802
- $ rails plugin install git://github.com/joelmoss/dynamic_form.git
803
- </shell>
804
-
805
- h4. Installing as a Gem
806
-
807
- Add this line in your Gemfile:
801
+ You can install it as a gem by adding this line to your Gemfile:
808
802
 
809
803
  <ruby>
810
804
  gem "dynamic_form"
811
805
  </ruby>
812
806
 
813
- Now you will have access to these two methods in your view templates.
807
+ Now you will have access to the two helper methods +error_messages+ and +error_messages_for+ in your view templates.
814
808
 
815
809
  h4. +error_messages+ and +error_messages_for+
816
810
 
@@ -840,11 +834,13 @@ end
840
834
  <% end %>
841
835
  </erb>
842
836
 
843
- To get the idea, if you submit the form with empty fields you typically get this back, though styles are indeed missing by default:
837
+ If you submit the form with empty fields, the result will be similar to the one shown below:
844
838
 
845
839
  !images/error_messages.png(Error messages)!
846
840
 
847
- You can also use the +error_messages_for+ helper to display the error messages of a model assigned to a view template. It's very similar to the previous example and will achieve exactly the same result.
841
+ 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.
842
+
843
+ 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.
848
844
 
849
845
  <erb>
850
846
  <%= error_messages_for :product %>
@@ -852,7 +848,7 @@ You can also use the +error_messages_for+ helper to display the error messages o
852
848
 
853
849
  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.
854
850
 
855
- Both the +form.error_messages+ and the +error_messages_for+ helpers accept options that let you customize the +div+ element that holds the messages, changing the header text, the message below the header text and the tag used for the element that defines the header.
851
+ 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,
856
852
 
857
853
  <erb>
858
854
  <%= f.error_messages :header_message => "Invalid product!",
@@ -860,23 +856,23 @@ Both the +form.error_messages+ and the +error_messages_for+ helpers accept optio
860
856
  :header_tag => :h3 %>
861
857
  </erb>
862
858
 
863
- Which results in the following content:
859
+ results in:
864
860
 
865
861
  !images/customized_error_messages.png(Customized error messages)!
866
862
 
867
- If you pass +nil+ to any of these options, it will get rid of the respective section of the +div+.
863
+ If you pass +nil+ in any of these options, the corresponding section of the +div+ will be discarded.
868
864
 
869
- h4. Customizing the Error Messages CSS
865
+ h4(#customizing-error-messages-css). Customizing the Error Messages CSS
870
866
 
871
- The selectors to customize the style of error messages are:
867
+ The selectors used to customize the style of error messages are:
872
868
 
873
869
  * +.field_with_errors+ - Style for the form fields and labels with errors.
874
- * +#errorExplanation+ - Style for the +div+ element with the error messages.
875
- * +#errorExplanation h2+ - Style for the header of the +div+ element.
876
- * +#errorExplanation p+ - Style for the paragraph that holds the message that appears right below the header of the +div+ element.
877
- * +#errorExplanation ul li+ - Style for the list items with individual error messages.
870
+ * +#error_explanation+ - Style for the +div+ element with the error messages.
871
+ * +#error_explanation h2+ - Style for the header of the +div+ element.
872
+ * +#error_explanation p+ - Style for the paragraph holding the message that appears right below the header of the +div+ element.
873
+ * +#error_explanation ul li+ - Style for the list items with individual error messages.
878
874
 
879
- Scaffolding for example generates +app/assets/stylesheets/scaffold.css.scss+, which later compiles to +app/assets/stylesheets/scaffold.css+ and defines the red-based style you saw above.
875
+ 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.
880
876
 
881
877
  The name of the class and the id can be changed with the +:class+ and +:id+ options, accepted by both helpers.
882
878
 
@@ -889,7 +885,7 @@ The way form fields with errors are treated is defined by +ActionView::Base.fiel
889
885
  * A string with the HTML tag
890
886
  * An instance of +ActionView::Helpers::InstanceTag+.
891
887
 
892
- Here is a simple example where we change the Rails behavior to always display the error messages in front of each of the form fields with errors. 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.
888
+ 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.
893
889
 
894
890
  <ruby>
895
891
  ActionView::Base.field_error_proc = Proc.new do |html_tag, instance|
@@ -903,17 +899,17 @@ ActionView::Base.field_error_proc = Proc.new do |html_tag, instance|
903
899
  end
904
900
  </ruby>
905
901
 
906
- This will result in something like the following:
902
+ The result looks like the following:
907
903
 
908
904
  !images/validation_error_messages.png(Validation error messages)!
909
905
 
910
906
  h3. Callbacks Overview
911
907
 
912
- Callbacks are methods that get called at certain moments of an object's life cycle. With callbacks it's possible to write code that will run whenever an Active Record object is created, saved, updated, deleted, validated, or loaded from the database.
908
+ 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.
913
909
 
914
910
  h4. Callback Registration
915
911
 
916
- In order to use the available callbacks, you need to register them. You can do that by implementing them as ordinary methods, and then using a macro-style class method to register them as callbacks.
912
+ 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:
917
913
 
918
914
  <ruby>
919
915
  class User < ActiveRecord::Base
@@ -930,7 +926,7 @@ class User < ActiveRecord::Base
930
926
  end
931
927
  </ruby>
932
928
 
933
- 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 just one line.
929
+ 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:
934
930
 
935
931
  <ruby>
936
932
  class User < ActiveRecord::Base
@@ -942,7 +938,7 @@ class User < ActiveRecord::Base
942
938
  end
943
939
  </ruby>
944
940
 
945
- It's considered good practice to declare callback methods as being protected or private. If left public, they can be called from outside of the model and violate the principle of object encapsulation.
941
+ 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.
946
942
 
947
943
  h3. Available Callbacks
948
944
 
@@ -1039,7 +1035,7 @@ The +after_initialize+ callback is triggered every time a new object of the clas
1039
1035
 
1040
1036
  h3. Skipping Callbacks
1041
1037
 
1042
- Just as with validations, it's 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.
1038
+ 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.
1043
1039
 
1044
1040
  * +decrement+
1045
1041
  * +decrement_counter+
@@ -1058,13 +1054,13 @@ h3. Halting Execution
1058
1054
 
1059
1055
  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.
1060
1056
 
1061
- 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.
1057
+ 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.
1062
1058
 
1063
- WARNING. Raising an arbitrary exception may break code that expects +save+ and 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.
1059
+ 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.
1064
1060
 
1065
1061
  h3. Relational Callbacks
1066
1062
 
1067
- Callbacks work through model relationships, and can even be defined by them. Let's take an example where a user has many posts. In our example, a user's posts should be destroyed if the user is destroyed. So, we'll add an +after_destroy+ callback to the +User+ model by way of its relationship to the +Post+ model.
1063
+ 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:
1068
1064
 
1069
1065
  <ruby>
1070
1066
  class User < ActiveRecord::Base
@@ -1090,11 +1086,11 @@ Post destroyed
1090
1086
 
1091
1087
  h3. Conditional Callbacks
1092
1088
 
1093
- Like in validations, we can also make our callbacks conditional, calling them only 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 callback *should* get called. If you want to specify when the callback *should not* be called, then you may use the +:unless+ option.
1089
+ 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.
1094
1090
 
1095
- h4. Using +:if+ and +:unless+ with a Symbol
1091
+ h4. Using +:if+ and +:unless+ with a +Symbol+
1096
1092
 
1097
- You can associate the +:if+ and +:unless+ options with a symbol corresponding to the name of a method that will get called right before the callback. When using the +:if+ option, the callback won't be executed if the method returns false; when using the +:unless+ option, the callback won't be executed if the method returns true. This is the most common option. Using this form of registration it's also possible to register several different methods that should be called to check if the callback should be executed.
1093
+ 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.
1098
1094
 
1099
1095
  <ruby>
1100
1096
  class Order < ActiveRecord::Base
@@ -1104,7 +1100,7 @@ end
1104
1100
 
1105
1101
  h4. Using +:if+ and +:unless+ with a String
1106
1102
 
1107
- 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.
1103
+ 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:
1108
1104
 
1109
1105
  <ruby>
1110
1106
  class Order < ActiveRecord::Base
@@ -1112,9 +1108,9 @@ class Order < ActiveRecord::Base
1112
1108
  end
1113
1109
  </ruby>
1114
1110
 
1115
- h4. Using +:if+ and +:unless+ with a Proc
1111
+ h4. Using +:if+ and +:unless+ with a +Proc+
1116
1112
 
1117
- Finally, it's possible to associate +:if+ and +:unless+ with a +Proc+ object. This option is best suited when writing short validation methods, usually one-liners.
1113
+ 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:
1118
1114
 
1119
1115
  <ruby>
1120
1116
  class Order < ActiveRecord::Base
@@ -1125,7 +1121,7 @@ end
1125
1121
 
1126
1122
  h4. Multiple Conditions for Callbacks
1127
1123
 
1128
- When writing conditional callbacks, it's possible to mix both +:if+ and +:unless+ in the same callback declaration.
1124
+ When writing conditional callbacks, it is possible to mix both +:if+ and +:unless+ in the same callback declaration:
1129
1125
 
1130
1126
  <ruby>
1131
1127
  class Comment < ActiveRecord::Base
@@ -1138,7 +1134,7 @@ h3. Callback Classes
1138
1134
 
1139
1135
  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.
1140
1136
 
1141
- Here's an example where we create a class with an +after_destroy+ callback for a +PictureFile+ model.
1137
+ Here's an example where we create a class with an +after_destroy+ callback for a +PictureFile+ model:
1142
1138
 
1143
1139
  <ruby>
1144
1140
  class PictureFileCallbacks
@@ -1150,7 +1146,7 @@ class PictureFileCallbacks
1150
1146
  end
1151
1147
  </ruby>
1152
1148
 
1153
- When declared inside a class the callback method will receive the model object as a parameter. We can now use it this way:
1149
+ 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:
1154
1150
 
1155
1151
  <ruby>
1156
1152
  class PictureFile < ActiveRecord::Base
@@ -1158,7 +1154,7 @@ class PictureFile < ActiveRecord::Base
1158
1154
  end
1159
1155
  </ruby>
1160
1156
 
1161
- Note that we needed to instantiate a new +PictureFileCallbacks+ object, since we declared our callback as an instance method. Sometimes it will make more sense to have it as a class method.
1157
+ 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:
1162
1158
 
1163
1159
  <ruby>
1164
1160
  class PictureFileCallbacks
@@ -1182,16 +1178,25 @@ You can declare as many callbacks as you want inside your callback classes.
1182
1178
 
1183
1179
  h3. Observers
1184
1180
 
1185
- 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 outside of a 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.
1181
+ 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.
1186
1182
 
1187
1183
  h4. Creating Observers
1188
1184
 
1189
- 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 could create an observer to contain this functionality.
1185
+ 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.
1190
1186
 
1191
1187
  <shell>
1192
1188
  $ rails generate observer User
1193
1189
  </shell>
1194
1190
 
1191
+ generates +app/models/user_observer.rb+ containing the observer class +UserObserver+:
1192
+
1193
+ <ruby>
1194
+ class UserObserver < ActiveRecord::Observer
1195
+ end
1196
+ </ruby>
1197
+
1198
+ You may now add methods to be called at the desired occasions:
1199
+
1195
1200
  <ruby>
1196
1201
  class UserObserver < ActiveRecord::Observer
1197
1202
  def after_create(model)
@@ -1207,7 +1212,7 @@ h4. Registering Observers
1207
1212
  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:
1208
1213
 
1209
1214
  <ruby>
1210
- # Activate observers that should always be running
1215
+ # Activate observers that should always be running.
1211
1216
  config.active_record.observers = :user_observer
1212
1217
  </ruby>
1213
1218
 
@@ -1215,7 +1220,7 @@ As usual, settings in +config/environments+ take precedence over those in +confi
1215
1220
 
1216
1221
  h4. Sharing Observers
1217
1222
 
1218
- 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 so it's possible to manually specify the models that our observer should observe.
1223
+ 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:
1219
1224
 
1220
1225
  <ruby>
1221
1226
  class MailerObserver < ActiveRecord::Observer
@@ -1227,10 +1232,10 @@ class MailerObserver < ActiveRecord::Observer
1227
1232
  end
1228
1233
  </ruby>
1229
1234
 
1230
- In this example, the +after_create+ method would be called whenever a +Registration+ or +User+ was created. Note that this new +MailerObserver+ would also need to be registered in +config/application.rb+ in order to take effect.
1235
+ 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:
1231
1236
 
1232
1237
  <ruby>
1233
- # Activate observers that should always be running
1238
+ # Activate observers that should always be running.
1234
1239
  config.active_record.observers = :mailer_observer
1235
1240
  </ruby>
1236
1241
 
@@ -1238,7 +1243,7 @@ h3. Transaction Callbacks
1238
1243
 
1239
1244
  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.
1240
1245
 
1241
- Consider, for example, the previous example where the +PictureFile+ model needs to delete a file after a 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.
1246
+ 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.
1242
1247
 
1243
1248
  <ruby>
1244
1249
  PictureFile.transaction do
@@ -1267,14 +1272,3 @@ end
1267
1272
  </ruby>
1268
1273
 
1269
1274
  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.
1270
-
1271
- h3. Changelog
1272
-
1273
- * February 17, 2011: Add description of transaction callbacks.
1274
- * July 20, 2010: Fixed typos and rephrased some paragraphs for clarity. "Jaime Iniesta":http://jaimeiniesta.com
1275
- * May 24, 2010: Fixed document to validate XHTML 1.0 Strict. "Jaime Iniesta":http://jaimeiniesta.com
1276
- * May 15, 2010: Validation Errors section updated by "Emili Parreño":http://www.eparreno.com
1277
- * March 7, 2009: Callbacks revision by Trevor Turk
1278
- * February 10, 2009: Observers revision by Trevor Turk
1279
- * February 5, 2009: Initial revision by Trevor Turk
1280
- * January 9, 2009: Initial version by "Cássio Marques":credits.html#cmarques
@@ -69,6 +69,52 @@ person = Person.find(1)
69
69
  person.destroy
70
70
  </ruby>
71
71
 
72
- h3. Changelog
72
+ h3. Validations
73
73
 
74
- * July 30, 2011: Initial version by "Vishnu Atrai":http://github.com/vatrai
74
+ Module to support validation and errors with Active Resource objects. The module overrides Base#save to rescue ActiveResource::ResourceInvalid exceptions and parse the errors returned in the web service response. The module also adds an errors collection that mimics the interface of the errors provided by ActiveRecord::Errors.
75
+
76
+ h4. Validating client side resources by overriding validation methods in base class
77
+
78
+ <ruby>
79
+ class Person < ActiveResource::Base
80
+ self.site = "http://api.people.com:3000/"
81
+
82
+ protected
83
+
84
+ def validate
85
+ errors.add("last", "has invalid characters") unless last =~ /[a-zA-Z]*/
86
+ end
87
+ end
88
+ </ruby>
89
+
90
+ h4. Validating client side resources
91
+
92
+ Consider a Person resource on the server requiring both a first_name and a last_name with a validates_presence_of :first_name, :last_name declaration in the model:
93
+
94
+ <ruby>
95
+ person = Person.new(:first_name => "Jim", :last_name => "")
96
+ person.save # => false (server returns an HTTP 422 status code and errors)
97
+ person.valid? # => false
98
+ person.errors.empty? # => false
99
+ person.errors.count # => 1
100
+ person.errors.full_messages # => ["Last name can't be empty"]
101
+ person.errors[:last_name] # => ["can't be empty"]
102
+ person.last_name = "Halpert"
103
+ person.save # => true (and person is now saved to the remote service)
104
+ </ruby>
105
+
106
+ h4. Public instance methods
107
+
108
+ ActiveResource::Validations have three public instance methods
109
+
110
+ h5. errors()
111
+
112
+ This will return errors object that holds all information about attribute error messages
113
+
114
+ h5. save_with_validation(options=nil)
115
+
116
+ This validates the resource with any local validations written in base class and then it will try to POST if there are no errors.
117
+
118
+ h5. valid?
119
+
120
+ Runs all the local validations and will return true if no errors.